Minsky
clipboard.cc
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2021
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "clipboard.h"
21 
22 #ifdef _WIN32
23 #include <windows.h>
24 #else
25 #include "libclipboard.h"
26 #endif
27 
28 using namespace std;
29 
30 namespace minsky
31 {
33  {
34 #ifndef _WIN32
35  clipboard_c* clipboard;
36  Impl(): clipboard(clipboard_new(nullptr)) {}
37  ~Impl() {clipboard_free(clipboard);}
38 #endif
39  };
40 
41  Clipboard::Clipboard(): pimpl(make_shared<Impl>()) {}
42 
43  std::string Clipboard::getClipboard() const
44  {
45 #if defined(_WIN32)
46  string r;
47  OpenClipboard(nullptr);
48  if (HANDLE h=GetClipboardData(CF_TEXT))
49  {
50  r=static_cast<const char*>(GlobalLock(h));
51  GlobalUnlock(h);
52  }
53  CloseClipboard();
54  return r;
55 #else
56  if (pimpl->clipboard)
57  {
58  auto s=clipboard_text(pimpl->clipboard);
59  return s? s: "";
60  }
61  return {};
62 #endif
63  }
64 
65  void Clipboard::putClipboard(const std::string& text) const
66  {
67  // libclipboard didn't work on Windows???
68 #if defined(_WIN32)
69  HWND hwnd=nullptr;//TODO enumerate top level windows to find one belonging to this
70  OpenClipboard(hwnd);
71  EmptyClipboard();
72  HGLOBAL h=GlobalAlloc(GMEM_MOVEABLE, text.length()+1);
73  LPTSTR hh=static_cast<LPTSTR>(GlobalLock(h));
74  if (hh)
75  {
76  strcpy(hh,text.c_str());
77  GlobalUnlock(h);
78  if (SetClipboardData(CF_TEXT, h)==nullptr)
79  GlobalFree(h);
80  }
81  CloseClipboard();
82 #else
83  if (pimpl->clipboard)
84  {
85  if (text.empty())
86  clipboard_clear(pimpl->clipboard, LCB_CLIPBOARD);
87  else
88  clipboard_set_text(pimpl->clipboard, text.c_str());
89  }
90 #endif
91  }
92 
93 
94 }
std::string getClipboard() const
return clipboard contents as UTF8 text
Definition: clipboard.cc:43
STL namespace.
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
std::shared_ptr< Impl > pimpl
Definition: clipboard.h:30
void putClipboard(const std::string &) const
puts UTF8 text string on clipboard
Definition: clipboard.cc:65
clipboard_c * clipboard
Definition: clipboard.cc:35