forked from sabrogden/Ditto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMisc.cpp
1736 lines (1486 loc) · 39 KB
/
Misc.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdafx.h"
#include "CP_Main.h"
#include "Misc.h"
#include "OptionsSheet.h"
#include "shared/TextConvert.h"
#include "AlphaBlend.h"
#include "Tlhelp32.h"
#include <Wininet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "Path.h"
#include <regex>
#include <vector>
CString GetIPAddress()
{
WORD wVersionRequested;
WSADATA wsaData;
char name[255];
CString IP;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD(2,0);
if (WSAStartup(wVersionRequested, &wsaData)==0)
{
if(gethostname(name, sizeof(name))==0)
{
if((hostinfo=gethostbyname(name)) != NULL)
{
IP = inet_ntoa(*(struct in_addr*)* hostinfo->h_addr_list);
}
}
WSACleanup();
}
IP.MakeUpper();
return IP;
}
CString GetComputerName()
{
TCHAR ComputerName[MAX_COMPUTERNAME_LENGTH+1] = _T("");
DWORD Size=MAX_COMPUTERNAME_LENGTH+1;
GetComputerName(ComputerName, &Size);
CString cs(ComputerName);
cs.MakeUpper();
return cs;
}
void AppendToFile(const TCHAR* fn, const TCHAR* msg)
{
#ifdef _UNICODE
FILE *file = _wfopen(fn, _T("a"));
#else
FILE *file = fopen(fn, _T("a"));
#endif
ASSERT( file );
if(file != NULL)
{
#ifdef _UNICODE
fwprintf(file, _T("%s"), msg);
#else
fprintf(file, _T("%s"),msg);
#endif
fclose(file);
}
}
void log(const TCHAR* msg, bool bFromSendRecieve, CString csFile, long lLine)
{
ASSERT(AfxIsValidString(msg));
SYSTEMTIME st;
GetLocalTime(&st);
CString csText;
csText.Format(_T("[%d/%d/%d %02d:%02d:%02d.%03d - "), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
CString csFileLine;
csFile = GetFileName(csFile);
csFileLine.Format(_T("%s %d] "), csFile, lLine);
csText += csFileLine;
csText += msg;
csText += "\n";
#ifndef _DEBUG
if(CGetSetOptions::m_outputDebugStringLogging)
#endif
{
OutputDebugString(csText);
}
#ifndef _DEBUG
if(!bFromSendRecieve)
{
if(!g_Opt.m_bEnableDebugLogging)
return;
}
#endif
CString csExeFile = CGetSetOptions::GetPath(PATH_LOG_FILE);
csExeFile += "Ditto.log";
AppendToFile(csExeFile, csText);
}
void logsendrecieveinfo(CString cs, CString csFile, long lLine)
{
if(g_Opt.m_bLogSendReceiveErrors)
log(cs, true, csFile, lLine);
}
CString GetErrorString( int err )
{
CString str;
LPVOID lpMsgBuf;
::FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
str = (LPCTSTR) lpMsgBuf;
// Display the string.
// ::MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
::LocalFree( lpMsgBuf );
return str;
}
int g_funnyGetTickCountAdjustment = -1;
double IdleSeconds()
{
LASTINPUTINFO info;
info.cbSize = sizeof(info);
GetLastInputInfo(&info);
DWORD currentTick = GetTickCount();
if(g_funnyGetTickCountAdjustment == -1)
{
if(currentTick < info.dwTime)
{
g_funnyGetTickCountAdjustment = 1;
}
else
{
g_funnyGetTickCountAdjustment = 0;
}
}
if(g_funnyGetTickCountAdjustment == 1 || g_funnyGetTickCountAdjustment == 2)
{
//Output message the first time
if(g_funnyGetTickCountAdjustment == 1)
{
Log(StrF(_T("Adjusting time of get tickcount by: %d, on startup we found GetTickCount to be less than last input"), CGetSetOptions::GetFunnyTickCountAdjustment()));
g_funnyGetTickCountAdjustment = 2;
}
currentTick += CGetSetOptions::GetFunnyTickCountAdjustment();
}
double idleSeconds = (currentTick - info.dwTime)/1000.0;
return idleSeconds;
}
CString StrF(const TCHAR * pszFormat, ...)
{
ASSERT( AfxIsValidString( pszFormat ) );
CString str;
va_list argList;
va_start( argList, pszFormat );
str.FormatV( pszFormat, argList );
va_end( argList );
return str;
}
BYTE GetEscapeChar( BYTE ch )
{
switch(ch)
{
case '\'': return '\''; // Single quotation mark (') = 39 or 0x27
case '\"': return '\"'; // Double quotation mark (") = 34 or 0x22
case '?': return '\?'; // Question mark (?) = 63 or 0x3f
case '\\': return '\\'; // Backslash (\) = 92 or 0x5c
case 'a': return '\a'; // Alert (BEL) = 7
case 'b': return '\b'; // Backspace (BS) = 8
case 'f': return '\f'; // Formfeed (FF) = 12 or 0x0c
case 'n': return '\n'; // Newline (NL or LF) = 10 or 0x0a
case 'r': return '\r'; // Carriage Return (CR) = 13 or 0x0d
case 't': return '\t'; // Horizontal tab (HT) = 9
case 'v': return '\v'; // Vertical tab (VT) = 11 or 0x0b
case '0': return '\0'; // Null character (NUL) = 0
}
return 0; // invalid
}
CString RemoveEscapes( const TCHAR* str )
{
ASSERT( str );
CString ret;
TCHAR* pSrc = (TCHAR*) str;
TCHAR* pDest = ret.GetBuffer((int)STRLEN(pSrc));
TCHAR* pStart = pDest;
while( *pSrc != '\0' )
{
if( *pSrc == '\\' )
{
pSrc++;
*pDest = GetEscapeChar((BYTE)pSrc );
}
else
*pDest = *pSrc;
pSrc++;
pDest++;
}
ret.ReleaseBuffer((int)(pDest - pStart));
return ret;
}
CString GetWndText(HWND hWnd)
{
TCHAR cWindowText[200];
HWND hParent = hWnd;
::GetWindowText(hParent, cWindowText, 100);
int nCount = 0;
while (STRLEN(cWindowText) <= 0)
{
hParent = ::GetParent(hParent);
if (hParent == NULL)
break;
::GetWindowText(hParent, cWindowText, 100);
nCount++;
if (nCount > 100)
{
Log(_T("GetTargetName reached maximum search depth of 100"));
break;
}
}
return cWindowText;
}
CString TopLevelWindowText(DWORD pid)
{
std::pair<CString, DWORD> params = { _T(""), pid };
// Enumerate the windows using a lambda to process each window
BOOL bResult = EnumWindows([](HWND hwnd, LPARAM lParam) -> BOOL
{
auto pParams = (std::pair<CString, DWORD>*)(lParam);
DWORD processId;
if (GetWindowThreadProcessId(hwnd, &processId) &&
processId == pParams->second &&
::GetWindow(hwnd, GW_OWNER) == 0)
{
TCHAR cWindowText[500];
::GetWindowText(hwnd, cWindowText, 500);
if (STRLEN(cWindowText) > 0)
{
pParams->first = cWindowText;
return FALSE;
}
}
// Continue enumerating
return TRUE;
}, (LPARAM)¶ms);
return params.first;
}
bool IsAppWnd( HWND hWnd )
{
DWORD dwMyPID = ::GetCurrentProcessId();
DWORD dwTestPID;
::GetWindowThreadProcessId( hWnd, &dwTestPID );
return dwMyPID == dwTestPID;
}
/*----------------------------------------------------------------------------*\
Global Memory Helper Functions
\*----------------------------------------------------------------------------*/
// make sure the given HGLOBAL is valid.
BOOL IsValid(HGLOBAL hGlobal)
{
void* pvData = ::GlobalLock(hGlobal);
::GlobalUnlock(hGlobal);
return (pvData != NULL);
}
// asserts if hDest isn't big enough
void CopyToGlobalHP(HGLOBAL hDest, LPVOID pBuf, SIZE_T ulBufLen)
{
ASSERT(hDest && pBuf && ulBufLen);
LPVOID pvData = GlobalLock(hDest);
ASSERT(pvData);
SIZE_T size = GlobalSize(hDest);
ASSERT(size >= ulBufLen); // assert if hDest isn't big enough
memcpy(pvData, pBuf, ulBufLen);
GlobalUnlock(hDest);
}
void CopyToGlobalHH(HGLOBAL hDest, HGLOBAL hSource, SIZE_T ulBufLen)
{
ASSERT(hDest && hSource && ulBufLen);
LPVOID pvData = GlobalLock(hSource);
ASSERT(pvData );
SIZE_T size = GlobalSize(hSource);
ASSERT(size >= ulBufLen); // assert if hSource isn't big enough
CopyToGlobalHP(hDest, pvData, ulBufLen);
GlobalUnlock(hSource);
}
HGLOBAL NewGlobalP(LPVOID pBuf, SIZE_T nLen)
{
ASSERT(pBuf && nLen);
HGLOBAL hDest = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, nLen);
ASSERT(hDest );
CopyToGlobalHP(hDest, pBuf, nLen);
return hDest;
}
HGLOBAL NewGlobal(SIZE_T nLen)
{
ASSERT(nLen);
HGLOBAL hDest = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, nLen);
return hDest;
}
HGLOBAL NewGlobalH(HGLOBAL hSource, SIZE_T nLen)
{
ASSERT(hSource && nLen);
LPVOID pvData = GlobalLock(hSource);
HGLOBAL hDest = NewGlobalP(pvData, nLen);
GlobalUnlock(hSource);
return hDest;
}
int CompareGlobalHP(HGLOBAL hLeft, LPVOID pBuf, SIZE_T ulBufLen)
{
ASSERT(hLeft && pBuf && ulBufLen);
LPVOID pvData = GlobalLock(hLeft);
ASSERT(pvData);
ASSERT(ulBufLen <= GlobalSize(hLeft));
int result = memcmp(pvData, pBuf, ulBufLen);
GlobalUnlock(hLeft);
return result;
}
int CompareGlobalHH( HGLOBAL hLeft, HGLOBAL hRight, SIZE_T ulBufLen)
{
ASSERT(hLeft && hRight && ulBufLen);
ASSERT(ulBufLen <= GlobalSize(hRight));
LPVOID pvData = GlobalLock(hRight);
ASSERT(pvData);
int result = CompareGlobalHP(hLeft, pvData, ulBufLen);
GlobalUnlock(hLeft);
return result;
}
// https://learn.microsoft.com/en-us/windows/win32/dataxchg/standard-clipboard-formats
std::vector<CLIPFORMAT> GetSystemClipFormats()
{
std::vector<CLIPFORMAT> v = {
CF_TEXT,
CF_BITMAP,
CF_METAFILEPICT,
CF_SYLK,
CF_DIF,
CF_TIFF,
CF_OEMTEXT,
CF_DIB,
CF_PALETTE,
CF_PENDATA,
CF_RIFF,
CF_WAVE,
CF_UNICODETEXT,
CF_ENHMETAFILE,
CF_HDROP,
CF_LOCALE,
CF_OWNERDISPLAY,
CF_DSPTEXT,
CF_DSPBITMAP,
CF_DSPMETAFILEPICT,
CF_DSPENHMETAFILE
};
return v;
}
//Do not change these these are stored in the database
CLIPFORMAT GetFormatID(LPCTSTR cbName)
{
if(STRCMP(cbName, _T("CF_TEXT")) == 0)
return CF_TEXT;
else if(STRCMP(cbName, _T("CF_METAFILEPICT")) == 0)
return CF_METAFILEPICT;
else if(STRCMP(cbName, _T("CF_SYLK")) == 0)
return CF_SYLK;
else if(STRCMP(cbName, _T("CF_DIF")) == 0)
return CF_DIF;
else if(STRCMP(cbName, _T("CF_TIFF")) == 0)
return CF_TIFF;
else if(STRCMP(cbName, _T("CF_OEMTEXT")) == 0)
return CF_OEMTEXT;
else if(STRCMP(cbName, _T("CF_DIB")) == 0)
return CF_DIB;
else if(STRCMP(cbName, _T("CF_PALETTE")) == 0)
return CF_PALETTE;
else if(STRCMP(cbName, _T("CF_PENDATA")) == 0)
return CF_PENDATA;
else if(STRCMP(cbName, _T("CF_RIFF")) == 0)
return CF_RIFF;
else if(STRCMP(cbName, _T("CF_WAVE")) == 0)
return CF_WAVE;
else if(STRCMP(cbName, _T("CF_UNICODETEXT")) == 0)
return CF_UNICODETEXT;
else if(STRCMP(cbName, _T("CF_ENHMETAFILE")) == 0)
return CF_ENHMETAFILE;
else if(STRCMP(cbName, _T("CF_HDROP")) == 0)
return CF_HDROP;
else if(STRCMP(cbName, _T("CF_LOCALE")) == 0)
return CF_LOCALE;
else if(STRCMP(cbName, _T("CF_OWNERDISPLAY")) == 0)
return CF_OWNERDISPLAY;
else if(STRCMP(cbName, _T("CF_DSPTEXT")) == 0)
return CF_DSPTEXT;
else if(STRCMP(cbName, _T("CF_DSPBITMAP")) == 0)
return CF_DSPBITMAP;
else if(STRCMP(cbName, _T("CF_DSPMETAFILEPICT")) == 0)
return CF_DSPMETAFILEPICT;
else if(STRCMP(cbName, _T("CF_DSPENHMETAFILE")) == 0)
return CF_DSPENHMETAFILE;
return ::RegisterClipboardFormat(cbName);
}
//Do not change these these are stored in the database
CString GetFormatName(CLIPFORMAT cbType)
{
switch(cbType)
{
case CF_TEXT:
return _T("CF_TEXT");
case CF_BITMAP:
return _T("CF_BITMAP");
case CF_METAFILEPICT:
return _T("CF_METAFILEPICT");
case CF_SYLK:
return _T("CF_SYLK");
case CF_DIF:
return _T("CF_DIF");
case CF_TIFF:
return _T("CF_TIFF");
case CF_OEMTEXT:
return _T("CF_OEMTEXT");
case CF_DIB:
return _T("CF_DIB");
case CF_PALETTE:
return _T("CF_PALETTE");
case CF_PENDATA:
return _T("CF_PENDATA");
case CF_RIFF:
return _T("CF_RIFF");
case CF_WAVE:
return _T("CF_WAVE");
case CF_UNICODETEXT:
return _T("CF_UNICODETEXT");
case CF_ENHMETAFILE:
return _T("CF_ENHMETAFILE");
case CF_HDROP:
return _T("CF_HDROP");
case CF_LOCALE:
return _T("CF_LOCALE");
case CF_OWNERDISPLAY:
return _T("CF_OWNERDISPLAY");
case CF_DSPTEXT:
return _T("CF_DSPTEXT");
case CF_DSPBITMAP:
return _T("CF_DSPBITMAP");
case CF_DSPMETAFILEPICT:
return _T("CF_DSPMETAFILEPICT");
case CF_DSPENHMETAFILE:
return _T("CF_DSPENHMETAFILE");
default:
//Not a default type get the name from the clipboard
if (cbType != 0)
{
TCHAR szFormat[256];
GetClipboardFormatName(cbType, szFormat, 256);
return szFormat;
}
break;
}
return "ERROR";
}
CString GetFilePath(CString csFileName)
{
long lSlash = csFileName.ReverseFind('\\');
if(lSlash > -1)
{
csFileName = csFileName.Left(lSlash + 1);
}
return csFileName;
}
CString GetFileName(CString csFileName)
{
long lSlash = csFileName.ReverseFind('\\');
if(lSlash > -1)
{
csFileName = csFileName.Right(csFileName.GetLength() - lSlash - 1);
}
return csFileName;
}
/****************************************************************************************************
BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
***************************************************************************************************/
typedef struct
{
long lFlags; // Flags
LPRECT pVirtualRect; // Ptr to rect that receives the results, or the src of the monitor search method
int iMonitor; // Ndx to the mointor to look at, -1 for all, -or- result of the monitor search method
int nMonitorCount; // Total number of monitors found, -1 for monitor search method
} MONITOR_ENUM_PARAM;
#define MONITOR_SEARCH_METOHD 0x00000001
BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
// Typecast param
MONITOR_ENUM_PARAM* pParam = (MONITOR_ENUM_PARAM*)dwData;
if(pParam)
{
// If a dest rect was passed
if(pParam->pVirtualRect)
{
// If MONITOR_SEARCH_METOHD then we are being asked for the index of the monitor
// that the rect falls inside of
if(pParam->lFlags & MONITOR_SEARCH_METOHD)
{
if( (pParam->pVirtualRect->right < lprcMonitor->left) ||
(pParam->pVirtualRect->left > lprcMonitor->right) ||
(pParam->pVirtualRect->bottom < lprcMonitor->top) ||
(pParam->pVirtualRect->top > lprcMonitor->bottom))
{
// Nothing
}
else
{
// This is the one
pParam->iMonitor = pParam->nMonitorCount;
// Stop the enumeration
return FALSE;
}
}
else
{
if(pParam->iMonitor == pParam->nMonitorCount)
{
*pParam->pVirtualRect = *lprcMonitor;
}
else
if(pParam->iMonitor == -1)
{
pParam->pVirtualRect->left = min(pParam->pVirtualRect->left, lprcMonitor->left);
pParam->pVirtualRect->top = min(pParam->pVirtualRect->top, lprcMonitor->top);
pParam->pVirtualRect->right = max(pParam->pVirtualRect->right, lprcMonitor->right);
pParam->pVirtualRect->bottom = max(pParam->pVirtualRect->bottom, lprcMonitor->bottom);
}
}
}
// Up the count if necessary
pParam->nMonitorCount++;
}
return TRUE;
}
int GetScreenWidth(void)
{
OSVERSIONINFO OS_Version_Info;
DWORD dwPlatform = 0;
if(GetVersionEx(&OS_Version_Info) != 0)
{
dwPlatform = OS_Version_Info.dwPlatformId;
}
if(dwPlatform == VER_PLATFORM_WIN32_NT)
{
int width, height;
width = GetSystemMetrics(SM_CXSCREEN);
height = GetSystemMetrics(SM_CYSCREEN);
switch(width)
{
default:
case 640:
case 800:
case 1024:
return(width);
case 1280:
if(height == 480)
{
return(width / 2);
}
return(width);
case 1600:
if(height == 600)
{
return(width / 2);
}
return(width);
case 2048:
if(height == 768)
{
return(width / 2);
}
return(width);
}
}
else
{
return(GetSystemMetrics(SM_CXVIRTUALSCREEN));
}
}
int GetScreenHeight(void)
{
OSVERSIONINFO OS_Version_Info;
DWORD dwPlatform = 0;
if(GetVersionEx(&OS_Version_Info) != 0)
{
dwPlatform = OS_Version_Info.dwPlatformId;
}
if(dwPlatform == VER_PLATFORM_WIN32_NT)
{
int width, height;
width = GetSystemMetrics(SM_CXSCREEN);
height = GetSystemMetrics(SM_CYSCREEN);
switch(height)
{
default:
case 480:
case 600:
case 768:
return(height);
case 960:
if(width == 640)
{
return(height / 2);
}
return(height);
case 1200:
if(width == 800)
{
return(height / 2);
}
return(height);
case 1536:
if(width == 1024)
{
return(height / 2);
}
return(height);
}
}
else
{
return(GetSystemMetrics(SM_CYVIRTUALSCREEN));
}
}
/*------------------------------------------------------------------*\
ID based Globals
\*------------------------------------------------------------------*/
long NewGroupID(int parentID, CString text)
{
long lID=0;
CTime time;
time = CTime::GetCurrentTime();
try
{
//sqlite doesn't like single quotes ' replace them with double ''
if(text.IsEmpty())
text = time.Format("NewGroup %y/%m/%d %H:%M:%S");
text.Replace(_T("'"), _T("''"));
CString cs;
cs.Format(_T("insert into Main (lDate, mText, lDontAutoDelete, bIsGroup, lParentID, stickyClipOrder, stickyClipGroupOrder) values(%d, '%s', %d, 1, %d, -(2147483647), -(2147483647));"),
(int)time.GetTime(),
text,
(int)time.GetTime(),
parentID);
theApp.m_db.execDML(cs);
lID = (long)theApp.m_db.lastRowId();
}
CATCH_SQLITE_EXCEPTION_AND_RETURN(0)
return lID;
}
BOOL DeleteAllIDs()
{
try
{
theApp.m_db.execDML(_T("DELETE FROM Data;"));
theApp.m_db.execDML(_T("DELETE FROM Main;"));
}
CATCH_SQLITE_EXCEPTION
return TRUE;
}
BOOL DeleteFormats(int parentID, ARRAY& formatIDs)
{
if(formatIDs.GetSize() <= 0)
return TRUE;
try
{
//Delete the requested data formats
INT_PTR count = formatIDs.GetSize();
for(int i = 0; i < count; i++)
{
int count = theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lID = %d;"), formatIDs[i]);
int k = 0;
}
CClip clip;
if(clip.LoadFormats(parentID))
{
DWORD CRC = clip.GenerateCRC();
//Update the main table with new size
theApp.m_db.execDMLEx(_T("UPDATE Main SET CRC = %d WHERE lID = %d"), CRC, parentID);
}
}
CATCH_SQLITE_EXCEPTION
return TRUE;
}
CRect CenterRect(CRect startingRect)
{
CRect crMonitor;
HMONITOR monitorHandle = MonitorFromPoint(startingRect.TopLeft(), MONITOR_DEFAULTTONEAREST);
if (monitorHandle == NULL)
{
monitorHandle = MonitorFromPoint(startingRect.TopLeft(), MONITOR_DEFAULTTOPRIMARY);
MONITORINFO lpmi;
lpmi.cbSize = sizeof(MONITORINFO);
if (GetMonitorInfo(monitorHandle, &lpmi))
{
crMonitor.CopyRect(&lpmi.rcWork);
}
}
else
{
MONITORINFO lpmi;
lpmi.cbSize = sizeof(MONITORINFO);
if (GetMonitorInfo(monitorHandle, &lpmi))
{
crMonitor.CopyRect(&lpmi.rcWork);
}
}
return CenterRectFromRect(startingRect, crMonitor);
}
CRect CenterRectFromRect(CRect startingRect, CRect outerRect)
{
CPoint center = outerRect.CenterPoint();
CRect centerRect;
centerRect.left = center.x - (startingRect.Width() / 2);
centerRect.top = center.y - (startingRect.Height() / 2);
centerRect.right = centerRect.left + startingRect.Width();
centerRect.bottom = centerRect.top + startingRect.Height();
return centerRect;
}
CRect DefaultMonitorRect()
{
CRect crMonitor;
CRect invalidRect(INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX);
HMONITOR monitorHandle = MonitorFromPoint(invalidRect.TopLeft(), MONITOR_DEFAULTTOPRIMARY);
MONITORINFO lpmi;
lpmi.cbSize = sizeof(MONITORINFO);
if (GetMonitorInfo(monitorHandle, &lpmi))
{
crMonitor.CopyRect(&lpmi.rcWork);
}
return crMonitor;
}
CRect MonitorRectFromRect(CRect rect)
{
BOOL ret = FALSE;
CRect crMonitor;
HMONITOR monitorHandle = MonitorFromPoint(rect.TopLeft(), MONITOR_DEFAULTTONEAREST);
if (monitorHandle == NULL)
{
monitorHandle = MonitorFromPoint(rect.TopLeft(), MONITOR_DEFAULTTOPRIMARY);
MONITORINFO lpmi;
lpmi.cbSize = sizeof(MONITORINFO);
if (GetMonitorInfo(monitorHandle, &lpmi))
{
crMonitor.CopyRect(&lpmi.rcWork);
}
}
else
{
MONITORINFO lpmi;
lpmi.cbSize = sizeof(MONITORINFO);
if (GetMonitorInfo(monitorHandle, &lpmi))
{
crMonitor.CopyRect(&lpmi.rcWork);
}
}
return crMonitor;
}
BOOL EnsureWindowVisible(CRect *pcrRect)
{
BOOL ret = FALSE;
CRect crMonitor;
HMONITOR monitorHandle = MonitorFromRect(pcrRect, MONITOR_DEFAULTTONEAREST);
if (monitorHandle == NULL)
{
monitorHandle = MonitorFromRect(pcrRect, MONITOR_DEFAULTTOPRIMARY);
MONITORINFO lpmi;
lpmi.cbSize = sizeof(MONITORINFO);
if (GetMonitorInfo(monitorHandle, &lpmi))
{
crMonitor.CopyRect(&lpmi.rcWork);
*pcrRect = CenterRectFromRect(*pcrRect, crMonitor);
}
}
else
{
MONITORINFO lpmi;
lpmi.cbSize = sizeof(MONITORINFO);
if (GetMonitorInfo(monitorHandle, &lpmi))
{
crMonitor.CopyRect(&lpmi.rcWork);
}
}
bool movedLeft = false;
//Validate the left
long lDiff = pcrRect->left - crMonitor.left;
if (lDiff < 0)
{
pcrRect->left += abs(lDiff);
pcrRect->right += abs(lDiff);
ret = TRUE;
movedLeft = true;
}
//Right side
lDiff = pcrRect->right - crMonitor.right;
if (lDiff > 0)
{
if (movedLeft == false)
{
pcrRect->left -= abs(lDiff);
}
pcrRect->right -= abs(lDiff);
ret = TRUE;
}
bool movedTop = false;
//Top
lDiff = pcrRect->top - crMonitor.top;
if (lDiff < 0)
{
pcrRect->top += abs(lDiff);
pcrRect->bottom += abs(lDiff);
ret = TRUE;
movedTop = true;
}
//Bottom
lDiff = pcrRect->bottom - crMonitor.bottom;
if (lDiff > 0)
{
if (movedTop == false)
{
pcrRect->top -= abs(lDiff);
}
pcrRect->bottom -= abs(lDiff);
ret = TRUE;
}
return ret;
}
__int64 GetLastWriteTime(const CString &csFile)
{
__int64 nLastWrite = 0;
CFileFind finder;
BOOL bResult = finder.FindFile(csFile);
if (bResult)
{
finder.FindNextFile();
FILETIME ft;
finder.GetLastWriteTime(&ft);
memcpy(&nLastWrite, &ft, sizeof(ft));
}
return nLastWrite;
}
typedef struct
{
DWORD ownerpid;
DWORD childpid;
} windowinfo;
BOOL CALLBACK EnumChildWindowsCallback(HWND hWnd, LPARAM lp)
{
windowinfo* info = (windowinfo*)lp;
DWORD pid = 0;
GetWindowThreadProcessId(hWnd, &pid);
if (pid != info->ownerpid)
info->childpid = pid;
return TRUE;
}
CString UWP_AppName(HWND active_window, DWORD ownerpid)
{
CString uwpAppName;
windowinfo info = { 0 };
info.ownerpid = ownerpid;
info.childpid = info.ownerpid;
EnumChildWindows(active_window, EnumChildWindowsCallback, (LPARAM)&info);
HANDLE active_process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, info.childpid);
if (active_process != NULL)
{
WCHAR image_name[MAX_PATH] = { 0 };
DWORD bufsize = MAX_PATH;
QueryFullProcessImageName(active_process, 0, image_name, &bufsize);
CloseHandle(active_process);