-
Notifications
You must be signed in to change notification settings - Fork 206
/
CopyProperties.cpp
596 lines (489 loc) · 14.2 KB
/
CopyProperties.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// CopyProperties.cpp : implementation file
//
#include "stdafx.h"
#include "cp_main.h"
#include "CopyProperties.h"
#include ".\copyproperties.h"
#include "Md5.h"
#include "Shared\TextConvert.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCopyProperties dialog
CCopyProperties::CCopyProperties(long lCopyID, CWnd* pParent, CClip *pMemoryClip)
: CDialog(CCopyProperties::IDD, pParent)
{
m_lCopyID = lCopyID;
m_bDeletedData = false;
m_bChangedText = false;
m_bHandleKillFocus = false;
m_bHideOnKillFocus = false;
m_lGroupChangedTo = -1;
m_pMemoryClip = pMemoryClip;
m_bSetToTopMost = true;
//{{AFX_DATA_INIT(CCopyProperties)
m_eDate = _T("");
m_lastPasteDate = _T("");
m_bNeverAutoDelete = FALSE;
//}}AFX_DATA_INIT
}
void CCopyProperties::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCopyProperties)
DDX_Control(pDX, IDC_EDIT_QUICK_PASTE, m_QuickPasteText);
DDX_Control(pDX, IDC_EDIT_PROPERTIES, m_description);
DDX_Control(pDX, IDC_COMBO1, m_GroupCombo);
DDX_Control(pDX, IDC_HOTKEY, m_HotKey);
DDX_Control(pDX, IDC_COPY_DATA, m_lCopyData);
DDX_Text(pDX, IDC_DATE, m_eDate);
DDX_Text(pDX, IDC_DATE_LAST_USED, m_lastPasteDate);
DDX_Check(pDX, IDC_NEVER_AUTO_DELETE, m_bNeverAutoDelete);
DDX_Check(pDX, IDC_HOT_KEY_GLOBAL, m_hotKeyGlobal);
DDX_Control(pDX, IDC_HOTKEY_MOVE_TO_GROUP, m_MoveToGrouHotKey);
DDX_Check(pDX, IDC_HOT_KEY_GLOBAL_MOVE_TO_GROUP, m_moveToGroupHotKeyGlobal);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCopyProperties, CDialog)
//{{AFX_MSG_MAP(CCopyProperties)
ON_BN_CLICKED(IDC_DELETE_COPY_DATA, OnDeleteCopyData)
ON_WM_ACTIVATE()
ON_WM_SIZE()
//}}AFX_MSG_MAP
ON_WM_CTLCOLOR()
ON_LBN_SELCHANGE(IDC_COPY_DATA, &CCopyProperties::OnLbnSelchangeCopyData)
ON_WM_NCLBUTTONDOWN()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCopyProperties message handlers
BOOL CCopyProperties::OnInitDialog()
{
CDialog::OnInitDialog();
//remove the default icon in top left of window
int extendedStyle = GetWindowLong(m_hWnd, GWL_EXSTYLE);
SetWindowLong(m_hWnd, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME);
SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
m_GroupCombo.FillCombo();
try
{
if(m_lCopyID == -1 && m_pMemoryClip != NULL)
{
LoadDataFromCClip(*m_pMemoryClip);
}
else
{
if(m_clip.LoadMainTable(m_lCopyID))
{
m_clip.LoadFormats(m_lCopyID);
LoadDataFromCClip(m_clip);
}
}
}
CATCH_SQLITE_EXCEPTION
UpdateData(FALSE);
if(m_bSetToTopMost)
SetWindowPos(&CWnd::wndTopMost, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
if(m_lCopyID == -1 && m_pMemoryClip != NULL)
{
GetDlgItem(IDOK)->SetFocus();
}
else
{
m_description.SetFocus();
}
m_Resize.SetParent(m_hWnd);
m_Resize.AddControl(IDC_EDIT_PROPERTIES, DR_SizeHeight | DR_SizeWidth);
m_Resize.AddControl(IDC_STATIC_FORMATS, DR_MoveTop);
m_Resize.AddControl(IDC_COPY_DATA, DR_MoveTop | DR_SizeWidth);
m_Resize.AddControl(IDC_DELETE_COPY_DATA, DR_MoveTop);
m_Resize.AddControl(IDOK, DR_MoveTop | DR_MoveLeft);
m_Resize.AddControl(IDCANCEL, DR_MoveTop | DR_MoveLeft);
m_Resize.AddControl(IDC_EDIT_QUICK_PASTE, DR_SizeWidth);
m_Resize.AddControl(IDC_COMBO1, DR_SizeWidth);
m_Resize.AddControl(IDC_STATIC_MD5, DR_MoveTop | DR_MoveLeft);
m_Resize.AddControl(IDC_EDIT_MD5, DR_MoveTop | DR_MoveLeft);
theApp.m_Language.UpdateClipProperties(this);
if (m_clip.ID() > 0)
{
CString text;
this->GetWindowText(text);
CString newText;
newText.Format(_T("%s - %d"), text, m_clip.ID());
this->SetWindowText(newText);
}
return FALSE;
}
void CCopyProperties::LoadDataFromCClip(CClip &Clip)
{
COleDateTime dtTime(Clip.m_Time.GetTime());
m_eDate = dtTime.Format();
COleDateTime lastPasteDate(Clip.m_lastPasteDate.GetTime());
m_lastPasteDate = lastPasteDate.Format();
m_description.SetWindowText(Clip.m_Desc);
if(Clip.m_dontAutoDelete)
{
m_bNeverAutoDelete = TRUE;
}
else
{
m_bNeverAutoDelete = FALSE;
}
m_hotKeyGlobal = Clip.m_globalShortCut;
m_GroupCombo.SetCurSelOnItemData(Clip.m_parentId);
DWORD shortcut = ACCEL_MAKEKEY(LOBYTE(Clip.m_shortCut), ((HIBYTE(Clip.m_shortCut)) &~HOTKEYF_EXT));
switch (LOBYTE(shortcut))
{
case VK_LEFT:
case VK_UP:
case VK_RIGHT:
case VK_DOWN: // arrow keys
case VK_PRIOR:
case VK_NEXT: // page up and page down
case VK_END:
case VK_HOME:
case VK_INSERT:
case VK_DELETE:
case VK_DIVIDE: // numpad slash
case VK_NUMLOCK:
{
shortcut = ACCEL_MAKEKEY(LOBYTE(shortcut), (HIBYTE(shortcut) | HOTKEYF_EXT));
}
break;
}
m_HotKey.SetHotKey(LOBYTE(shortcut), (HIBYTE(shortcut)));
m_HotKey.SetRules(HKCOMB_A, 0);
if(HIBYTE(Clip.m_shortCut) & HOTKEYF_EXT)
{
::CheckDlgButton(m_hWnd, IDC_CHECK_WIN, BST_CHECKED);
}
m_moveToGroupHotKeyGlobal = Clip.m_globalMoveToGroupShortCut;
m_MoveToGrouHotKey.SetHotKey(LOBYTE(Clip.m_moveToGroupShortCut), (HIBYTE(Clip.m_moveToGroupShortCut) & ~HOTKEYF_EXT));
m_MoveToGrouHotKey.SetRules(HKCOMB_A, 0);
if(HIBYTE(Clip.m_moveToGroupShortCut) & HOTKEYF_EXT)
{
::CheckDlgButton(m_hWnd, IDC_CHECK_WIN_MOVE_TO_GROUP, BST_CHECKED);
}
m_QuickPasteText.SetWindowText(Clip.m_csQuickPaste);
CString cs;
CClipFormat* pCF;
INT_PTR count = Clip.m_Formats.GetSize();
for(int i = 0; i < count; i++)
{
pCF = &Clip.m_Formats.GetData()[i];
if(pCF)
{
cs.Format(_T("%s, %d"), GetFormatName(pCF->m_cfType), GlobalSize(pCF->m_hgData));
int nIndex = m_lCopyData.AddString(cs);
if(m_lCopyID == -1 && pCF->m_dataId == -1)
m_lCopyData.SetItemData(nIndex, i);
else
m_lCopyData.SetItemData(nIndex, pCF->m_dataId);
}
}
int selectedRow = m_lCopyData.GetCount()-1;
if(selectedRow >= 0 && selectedRow < m_lCopyData.GetCount())
{
m_lCopyData.SetSel(selectedRow);
m_lCopyData.SetCurSel(selectedRow);
m_lCopyData.SetCaretIndex(selectedRow);
m_lCopyData.SetAnchorIndex(selectedRow);
}
//show the selected data md5
OnLbnSelchangeCopyData();
if(Clip.m_bIsGroup == FALSE)
{
::ShowWindow(::GetDlgItem(m_hWnd, IDC_STATIC_HOT_KEY_MOVE_TO_GROUP), SW_HIDE);
::ShowWindow(::GetDlgItem(m_hWnd, IDC_HOTKEY_MOVE_TO_GROUP), SW_HIDE);
::ShowWindow(::GetDlgItem(m_hWnd, IDC_CHECK_WIN_MOVE_TO_GROUP), SW_HIDE);
::ShowWindow(::GetDlgItem(m_hWnd, IDC_HOT_KEY_GLOBAL_MOVE_TO_GROUP), SW_HIDE);
CRect anchorRect;
::GetWindowRect(::GetDlgItem(m_hWnd, IDC_STATIC_HOT_KEY_MOVE_TO_GROUP), &anchorRect);
ScreenToClient(&anchorRect);
HWND hwnd = ::GetTopWindow(this->GetSafeHwnd());
// while we have a valid hwnd,
// loop through all child windows
while (hwnd)
{
CRect rect;
::GetWindowRect(hwnd, &rect);
ScreenToClient(&rect);
if(rect.top > anchorRect.bottom)
{
::MoveWindow(hwnd, rect.left,
rect.top - (anchorRect.Height()+4), rect.Width(),
rect.Height(), TRUE);
}
// do something with the hwnd
// and get the next child control's hwnd
hwnd = ::GetNextWindow(hwnd, GW_HWNDNEXT);
}
CRect rect2;
::GetWindowRect(m_hWnd, &rect2);
::MoveWindow(m_hWnd, rect2.left,
rect2.top, rect2.Width(),
rect2.Height() - (anchorRect.Height()+4), TRUE);
}
}
void CCopyProperties::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
CDialog::OnActivate(nState, pWndOther, bMinimized);
if (nState == WA_INACTIVE)
{
if(m_bHideOnKillFocus)
{
if(!m_bHandleKillFocus)
{
EndDialog(-1);
m_bHandleKillFocus = false;
}
}
}
else if (nState == WA_ACTIVE)
{
SetFocus();
::SetForegroundWindow(m_hWnd);
}
}
void CCopyProperties::OnOK()
{
UpdateData();
try
{
if(m_lCopyID == -1 && m_pMemoryClip != NULL)
{
LoadDataIntoCClip(*m_pMemoryClip);
m_DeletedData.SortDescending();
INT_PTR count = m_DeletedData.GetSize();
for(int i = 0; i < count; i++)
{
m_pMemoryClip->m_Formats.RemoveAt(m_DeletedData[i]);
}
}
else
{
CClip clip;
if(clip.LoadMainTable(m_lCopyID))
{
LoadDataIntoCClip(clip);
if(CheckGlobalHotKey(clip) == FALSE)
{
if(MessageBox(_T("Error registering global hot key\n\nContinue?"), _T(""), MB_YESNO|MB_ICONWARNING) == IDNO)
{
return;
}
}
if(CheckMoveToGroupGlobalHotKey(clip) == FALSE)
{
if(MessageBox(_T("Error registering global move to group hot key\n\nContinue?"), _T(""), MB_YESNO|MB_ICONWARNING) == IDNO)
{
return;
}
}
if(clip.ModifyMainTable())
{
if(m_bDeletedData)
{
DeleteFormats(m_lCopyID, m_DeletedData);
}
}
}
}
m_bHandleKillFocus = true;
}
CATCH_SQLITE_EXCEPTION
CDialog::OnOK();
}
BOOL CCopyProperties::CheckGlobalHotKey(CClip &clip)
{
BOOL ret = FALSE;
if(clip.m_globalShortCut)
{
ret = g_HotKeys.ValidateClip(clip.m_id, clip.m_shortCut, clip.m_Desc, CHotKey::PASTE_OPEN_CLIP);
}
else
{
g_HotKeys.Remove(clip.m_id, CHotKey::PASTE_OPEN_CLIP);
ret = TRUE;
}
return ret;
}
BOOL CCopyProperties::CheckMoveToGroupGlobalHotKey(CClip &clip)
{
BOOL ret = FALSE;
if(clip.m_globalMoveToGroupShortCut)
{
ret = g_HotKeys.ValidateClip(clip.m_id, clip.m_moveToGroupShortCut, clip.m_Desc, CHotKey::MOVE_TO_GROUP);
}
else
{
g_HotKeys.Remove(clip.m_id, CHotKey::MOVE_TO_GROUP);
ret = TRUE;
}
return ret;
}
void CCopyProperties::LoadDataIntoCClip(CClip &Clip)
{
long lHotKey = m_HotKey.GetHotKey();
short sKeyKode = LOBYTE(m_HotKey.GetHotKey());
short sModifers = (HIBYTE(m_HotKey.GetHotKey())) & ~HOTKEYF_EXT;
if(sKeyKode && ::IsDlgButtonChecked(m_hWnd, IDC_CHECK_WIN))
{
sModifers |= HOTKEYF_EXT;
}
Clip.m_shortCut = MAKEWORD(sKeyKode, sModifers);
long moveToGroupHotKey = m_MoveToGrouHotKey.GetHotKey();
short moveToGroupKeyKode = LOBYTE(m_MoveToGrouHotKey.GetHotKey());
short moveToGroupModifers = HIBYTE(m_MoveToGrouHotKey.GetHotKey());
if(moveToGroupKeyKode && ::IsDlgButtonChecked(m_hWnd, IDC_CHECK_WIN_MOVE_TO_GROUP))
{
moveToGroupModifers |= HOTKEYF_EXT;
}
Clip.m_moveToGroupShortCut = MAKEWORD(moveToGroupKeyKode, moveToGroupModifers);
//remove any others that have the same hot key
if(Clip.m_shortCut > 0)
{
theApp.m_db.execDMLEx(_T("UPDATE Main SET lShortCut = 0 where lShortCut = %d AND lID <> %d;"), Clip.m_shortCut, m_lCopyID);
}
if(Clip.m_moveToGroupShortCut > 0)
{
theApp.m_db.execDMLEx(_T("UPDATE Main SET MoveToGroupShortCut = 0 where MoveToGroupShortCut = %d AND lID <> %d;"), Clip.m_shortCut, m_lCopyID);
}
m_description.GetWindowText(Clip.m_Desc);
m_QuickPasteText.GetWindowText(Clip.m_csQuickPaste);
Clip.m_parentId = m_GroupCombo.GetItemDataFromCursel();
//If we are going from no group to a group or the
//don't auto delete check box is checked
if(m_bNeverAutoDelete)
{
Clip.m_dontAutoDelete = (int)CTime::GetCurrentTime().GetTime();
}
else if(m_bNeverAutoDelete == FALSE)
{
Clip.m_dontAutoDelete = FALSE;
}
Clip.m_globalShortCut = m_hotKeyGlobal;
Clip.m_globalMoveToGroupShortCut = m_moveToGroupHotKeyGlobal;
}
void CCopyProperties::OnDeleteCopyData()
{
int nCount = m_lCopyData.GetSelCount();
if(nCount)
{
m_bDeletedData = true;
//Get the selected indexes
ARRAY items;
items.SetSize(nCount);
m_lCopyData.GetSelItems(nCount, items.GetData());
items.SortDescending();
//Get the selected itemdata
for(int i = 0; i < nCount; i++)
{
int row = items[i];
m_DeletedData.Add((int)m_lCopyData.GetItemData(row));
m_lCopyData.DeleteString(row);
int newRow = row-1;
if(newRow < 0)
{
newRow = 0;
}
if(newRow >= 0 && newRow < m_lCopyData.GetCount())
{
m_lCopyData.SetSel(newRow);
m_lCopyData.SetCurSel(newRow);
m_lCopyData.SetCaretIndex(newRow);
m_lCopyData.SetAnchorIndex(newRow);
}
}
}
}
void CCopyProperties::OnCancel()
{
m_bHandleKillFocus = true;
CDialog::OnCancel();
}
void CCopyProperties::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if (((GetKeyState(VK_LBUTTON) & 0x100) != 0) &&
m_mouseDownOnCaption == false)
{
m_Resize.MoveControls(CSize(cx, cy));
}
else
{
m_Resize.SetParent(m_hWnd);
}
}
HBRUSH CCopyProperties::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
// TODO: Return a different brush if the default is not desired
return hbr;
}
void CCopyProperties::OnLbnSelchangeCopyData()
{
int selCount = m_lCopyData.GetSelCount();
if (selCount > 0)
{
m_bDeletedData = true;
//Get the selected indexes
ARRAY items;
items.SetSize(selCount);
m_lCopyData.GetSelItems(selCount, items.GetData());
items.SortDescending();
for (int i = 0; i < selCount; i++)
{
int row = items[i];
int itemData = (int)m_lCopyData.GetItemData(row);
CClip *pClip = NULL;
if (m_lCopyID == -1 && m_pMemoryClip != NULL)
{
pClip = m_pMemoryClip;
}
else
{
pClip = &m_clip;
}
if (pClip != NULL)
{
CClipFormat* pCF;
INT_PTR dataCount = pClip->m_Formats.GetSize();
for (int i = 0; i < dataCount; i++)
{
pCF = &pClip->m_Formats.GetData()[i];
if (pCF)
{
if (pCF->m_dataId == itemData)
{
CMd5 md5;
md5.MD5Init();
SIZE_T size = ::GlobalSize(pCF->Data());
void* pv = GlobalLock(pCF->Data());
if (pv != NULL)
{
md5.MD5Update((unsigned char*)pv, (unsigned int)size);
GlobalUnlock(pCF->Data());
CStringA md5String = md5.MD5FinalToString();
this->SetDlgItemText(IDC_EDIT_MD5, CTextConvert::AnsiToUnicode(md5String));
}
}
}
}
}
break;
}
}
}
void CCopyProperties::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
m_mouseDownOnCaption = false;
if (nHitTest == HTCAPTION)
{
m_mouseDownOnCaption = true;
}
CDialog::OnNcLButtonDown(nHitTest, point);
}