forked from sabrogden/Ditto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDittoRulerRichEditCtrl.cpp
304 lines (254 loc) · 6.39 KB
/
DittoRulerRichEditCtrl.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "stdafx.h"
#include "clip.h"
#include "CP_Main.h"
#include "shared/TextConvert.h"
#include ".\dittorulerricheditctrl.h"
#include "CopyProperties.h"
CDittoRulerRichEditCtrl::CDittoRulerRichEditCtrl(void)
{
m_lID = -1;
}
CDittoRulerRichEditCtrl::~CDittoRulerRichEditCtrl(void)
{
}
bool CDittoRulerRichEditCtrl::LoadItem(long lID, CString csDesc)
{
bool bSetText = false;
CClipFormat Clip;
m_lID = lID;
m_csDescription = csDesc;
//If creating a new clip
if(m_lID < 0)
{
m_rtf.SetModify(FALSE);
return false;
}
Clip.m_cfType = RegisterClipboardFormat(CF_RTF);
if(theApp.GetClipData(lID, Clip) && Clip.m_hgData)
{
CString cs(Clip.GetAsCStringA());
SetRTF(cs);
bSetText = true;
Clip.Free();
Clip.Clear();
}
if(bSetText == false)
{
Clip.m_cfType = CF_UNICODETEXT;
if(theApp.GetClipData(lID, Clip) && Clip.m_hgData)
{
SetText(Clip.GetAsCString());
bSetText = true;
Clip.Free();
Clip.Clear();
}
}
if(bSetText == false)
{
Clip.m_cfType = CF_TEXT;
if(theApp.GetClipData(lID, Clip) && Clip.m_hgData)
{
CString csText(Clip.GetAsCStringA());
SetText(csText);
bSetText = true;
Clip.Free();
Clip.Clear();
}
}
m_rtf.SetModify(FALSE);
return bSetText;
}
long CDittoRulerRichEditCtrl::GetTypeFlags(long lID)
{
long lRet = stNONE;
try
{
CLIPFORMAT cfType = CF_TEXT;
CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Data WHERE lParentID = %d AND strClipboardFormat = '%s'"), lID, GetFormatName(cfType));
if(q.eof() == false)
{
lRet |= stCF_TEXT;
}
cfType = CF_UNICODETEXT;
q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Data WHERE lParentID = %d AND strClipboardFormat = '%s'"), lID, GetFormatName(cfType));
if(q.eof() == false)
{
lRet |= stCF_UNICODETEXT;
}
cfType = RegisterClipboardFormat(_T("Rich Text Format"));
q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Data WHERE lParentID = %d AND strClipboardFormat = '%s'"), lID, GetFormatName(cfType));
if(q.eof() == false)
{
lRet |= stRTF;
}
}
CATCH_SQLITE_EXCEPTION
return lRet;
}
void CDittoRulerRichEditCtrl::d()
{
CString cs = m_rtf.GetText();
CString s;
s.Format(_T("error = %d, %s"), GetLastError(), cs);
MessageBox(s);
}
int CDittoRulerRichEditCtrl::SaveToDB(BOOL bUpdateDesc)
{
int nRet = FALSE;
if(m_rtf.GetModify() == FALSE)
{
Log(_T("Clip has not been modified"));
return DIDNT_NEED_TO_SAVE;
}
bool bSetModifyToFalse = true;
try
{
//only save the types if they have them set as save types, mainly rtf type
int saveTypes = 0;
CClipTypes* pTypes = theApp.LoadTypesFromDB();
INT_PTR numTypes = pTypes->GetSize();
for (int i = 0; i < numTypes; i++)
{
if (pTypes->ElementAt(i) == theApp.m_RTFFormat)
{
saveTypes |= stRTF;
}
else if (pTypes->ElementAt(i) == CF_TEXT ||
pTypes->ElementAt(i) == CF_UNICODETEXT)
{
saveTypes |= stCF_TEXT;
saveTypes |= stCF_UNICODETEXT;
}
}
CClip Clip;
Clip.m_id = m_lID;
if(saveTypes & stRTF)
{
LoadRTFData(Clip);
}
if(saveTypes & stCF_TEXT || saveTypes & stCF_UNICODETEXT)
{
LoadTextData(Clip);
}
if(Clip.m_Formats.GetSize() <= 0)
{
return FALSE;
}
theApp.m_db.execDML(_T("begin transaction;"));
if(m_lID >= 0)
{
Clip.SaveFromEditWnd(bUpdateDesc);
}
else
{
bSetModifyToFalse = false;
Clip.MakeLatestOrder();
CCopyProperties Prop(-1, this, &Clip);
Prop.SetHandleKillFocus(true);
Prop.SetToTopMost(false);
if(Prop.DoModal() == IDOK)
{
Clip.AddToDB();
m_csDescription = Clip.m_Desc;
m_lID = Clip.m_id;
bUpdateDesc = TRUE;
bSetModifyToFalse = true;
}
}
nRet = SAVED_CLIP_TO_DB;
theApp.m_db.execDML(_T("commit transaction;"));
if(bUpdateDesc)
theApp.RefreshView();
}
CATCH_SQLITE_EXCEPTION
if(bSetModifyToFalse)
m_rtf.SetModify(FALSE);
return nRet;
}
bool CDittoRulerRichEditCtrl::LoadRTFData(CClip &Clip)
{
CString csRTFOriginal = GetRTF();
if(csRTFOriginal.IsEmpty())
{
Log(_T("Rtf is empty, returning"));
return false;
}
//remove the line feed at the end, not sure why the righ text always adds this
CString right = csRTFOriginal.Right(9);
if (right == _T("\\par\r\n}\r\n"))
{
CString r = csRTFOriginal.Left(csRTFOriginal.GetLength() - 9);
r += _T("}");
csRTFOriginal = r;
}
CStringA csRTF = CTextConvert::UnicodeToAnsi(csRTFOriginal);
CClipFormat format;
format.m_cfType = RegisterClipboardFormat(_T("Rich Text Format"));
int nLength = csRTF.GetLength() + 1;
format.m_hgData = NewGlobalP(csRTF.GetBuffer(nLength), nLength);
Clip.m_Formats.Add(format);
format.m_hgData = NULL; //Clip.m_formats owns data now
return true;
}
bool CDittoRulerRichEditCtrl::LoadTextData(CClip &Clip)
{
CString csText = GetText();
if(csText.IsEmpty())
{
for(int i = 0; i < 20; i++)
{
Sleep(100);
csText = GetText();
if(csText.IsEmpty() == FALSE)
break;
Log(StrF(_T("Get Text still empty pass = %d"), i));
}
if(csText.IsEmpty())
{
Log(_T("Get Text still empty pass returning"));
return false;
}
}
CClipFormat format;
#ifdef _UNICODE
format.m_cfType = CF_UNICODETEXT;
#else
format.m_cfType = CF_TEXT;
#endif
int nLength = csText.GetLength() * sizeof(TCHAR) + sizeof(TCHAR);
format.m_hgData = NewGlobalP(csText.GetBuffer(nLength), nLength);
Clip.SetDescFromText(format.m_hgData, true);
m_csDescription = Clip.m_Desc;
m_csDescription = m_csDescription.Left(15);
Clip.m_Formats.Add(format);
format.m_hgData = NULL; //Clip.m_formats owns data now
return true;
}
bool CDittoRulerRichEditCtrl::CloseEdit(bool bPrompt, BOOL bUpdateDesc)
{
if(m_rtf.GetModify())
{
int nRet = IDYES;
if(bPrompt)
{
CString cs;
cs.Format(_T("%s '%s'"), theApp.m_Language.GetString("SaveChanges", "Do you want to save changes to"), m_csDescription);
::SetForegroundWindow(m_hWnd);
nRet = MessageBox(cs, _T("Ditto"), MB_YESNOCANCEL);
}
if(nRet == IDYES)
{
if(SaveToDB(bUpdateDesc) == false)
{
CString cs;
cs.Format(_T("%s '%s'"), theApp.m_Language.GetString("ErrorSaving", "Error saving clip"), m_csDescription);
MessageBox(cs, _T("Ditto"), MB_OK);
}
}
else if(nRet == IDCANCEL)
{
return false;
}
}
return true;
}