This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClipboardXX.hpp
159 lines (118 loc) · 3.6 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
151
152
153
154
155
156
157
158
159
//https://github.com/Arian8j2/ClipboardXX
#pragma once
#include <string>
#include <exception>
#include <string.h>
#if defined(_WIN32) || defined(WIN32)
#define WINDOWS 1
#elif defined(__unix__)
#define LINUX 1
#else
#error "Not Supported OS"
#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));
strncpy((char *)pGlobal, pText, Length);
SetClipboardData(CF_TEXT, pGlobal);
GlobalFree(pGlobal);
}
void PasteText(std::string &sString){
char* pResult = (char*) GetClipboardData(CF_TEXT);
if(pResult == NULL)
throw CExceptionXX("Clipboard has no data to paste!");
sString = pResult;
GlobalFree(pResult);
}
friend class CClipboardXX;
};
#elif LINUX
#include <gtk/gtk.h>
class CClipboardLinux: public IClipboardOS{
private:
GtkClipboard *m_pClip;
CClipboardLinux(){
gtk_init(0, 0);
m_pClip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
}
static void copy_callback(GtkClipboard *pClip, const gchar *pText, gpointer pData){
if(pData){
gtk_clipboard_set_text(
pClip,
(const gchar*) pData,
strlen( (const char*) pData)
);
}
gtk_main_quit();
}
static void paste_callback(GtkClipboard *pClip, const gchar *pText, gpointer pData){
std::string* pString = (std::string*)pData;
(*pString) = pText;
gtk_main_quit();
}
public:
void CopyText(const char* pText, size_t Length){
gtk_clipboard_request_text(m_pClip, copy_callback, (gpointer)pText);
gtk_main();
}
void PasteText(std::string &sString){
gtk_clipboard_request_text(m_pClip, paste_callback, (gpointer)&sString);
gtk_main();
}
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);
}
};