-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClipboardXX.hpp
150 lines (110 loc) · 3.27 KB
/
ClipboardXX.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Taken from https://github.com/Arian8j2/ClipboardXX/blob/master/include/ClipboardXX.hpp
// To support crossplatform clipboard operations
#pragma once
#include <string>
#include <exception>
#include <string.h>
#if defined(_WIN32) || defined(WIN32)
#define WINDOWS 1
#else
#define LINUX 1
#endif
class CExceptionXX : public std::exception{
private:
const char* m_pReason;
public:
CExceptionXX(const char* pReason) : m_pReason(pReason){};
const char* what(){
return m_pReason;
}
};
class IClipboardOS{
protected:
// not accessable from other files :)
IClipboardOS(){};
public:
virtual void CopyText(const char* pText, size_t Length){};
virtual void PasteText(std::string &sString){};
};
class CClipboardXX;
#ifdef WINDOWS
#include <Windows.h>
class CClipboardWindows: public IClipboardOS{
private:
CClipboardWindows(){
if(!OpenClipboard(0)){
throw CExceptionXX("Cannot open clipboard!");
}
}
public:
~CClipboardWindows(){
CloseClipboard();
}
void CopyText(const char* pText, size_t Length){
if(!EmptyClipboard())
throw CExceptionXX("Cannot empty clipboard!");
HGLOBAL pGlobal = GlobalAlloc(GMEM_FIXED, (Length + 1)* sizeof(char));
#ifdef _MSC_VER
strcpy_s((char *)pGlobal, (Length + 1)*sizeof(char), pText);
#else
strncpy((char *)pGlobal, pText, Length);
#endif
SetClipboardData(CF_TEXT, pGlobal);
GlobalFree(pGlobal);
}
void PasteText(std::string &sString){
char* pResult = (char*) GetClipboardData(CF_TEXT);
if(pResult != NULL){
sString = pResult;
GlobalFree(pResult);
}
}
friend class CClipboardXX;
};
#elif LINUX
#include <QGuiApplication>
#include <QClipboard>
// for some reason have to declare Qt App in global (idk why)
static int gs_FakeArgc = 1;
static QGuiApplication gs_GuiApp(gs_FakeArgc, nullptr);
class CClipboardLinux: public IClipboardOS{
private:
QClipboard* m_pClipboard;
CClipboardLinux(){
m_pClipboard = gs_GuiApp.clipboard();
}
public:
~CClipboardLinux(){
gs_GuiApp.quit();
}
void CopyText(const char* pText, size_t Length){
m_pClipboard->setText(pText, QClipboard::Mode::Clipboard);
}
void PasteText(std::string &sString){
sString = m_pClipboard->text().toStdString();
}
friend class CClipboardXX;
};
#endif
class CClipboardXX{
private:
IClipboardOS* m_pClipboard = new
#ifdef WINDOWS
CClipboardWindows();
#elif LINUX
CClipboardLinux();
#endif
public:
~CClipboardXX(){
delete m_pClipboard;
}
void operator<<(const char* pText){
m_pClipboard->CopyText(pText, strlen(pText));
}
void operator<<(std::string &sText){
m_pClipboard->CopyText(sText.c_str(), sText.size());
}
void operator>>(std::string &sResult){
m_pClipboard->PasteText(sResult);
}
};