Misc.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include "Misc.h"
  4. #include "OptionsSheet.h"
  5. #include "shared/TextConvert.h"
  6. #include "AlphaBlend.h"
  7. #include "Tlhelp32.h"
  8. CString GetIPAddress()
  9. {
  10. WORD wVersionRequested;
  11. WSADATA wsaData;
  12. char name[255];
  13. CString IP;
  14. PHOSTENT hostinfo;
  15. wVersionRequested = MAKEWORD(2,0);
  16. if (WSAStartup(wVersionRequested, &wsaData)==0)
  17. {
  18. if(gethostname(name, sizeof(name))==0)
  19. {
  20. if((hostinfo=gethostbyname(name)) != NULL)
  21. {
  22. IP = inet_ntoa(*(struct in_addr*)* hostinfo->h_addr_list);
  23. }
  24. }
  25. WSACleanup();
  26. }
  27. IP.MakeUpper();
  28. return IP;
  29. }
  30. CString GetComputerName()
  31. {
  32. TCHAR ComputerName[MAX_COMPUTERNAME_LENGTH+1] = _T("");
  33. DWORD Size=MAX_COMPUTERNAME_LENGTH+1;
  34. GetComputerName(ComputerName, &Size);
  35. CString cs(ComputerName);
  36. cs.MakeUpper();
  37. return cs;
  38. }
  39. void AppendToFile(const TCHAR* fn, const TCHAR* msg)
  40. {
  41. #ifdef _UNICODE
  42. FILE *file = _wfopen(fn, _T("a"));
  43. #else
  44. FILE *file = fopen(fn, _T("a"));
  45. #endif
  46. ASSERT( file );
  47. if(file != NULL)
  48. {
  49. #ifdef _UNICODE
  50. fwprintf(file, _T("%s"), msg);
  51. #else
  52. fprintf(file, _T("%s"),msg);
  53. #endif
  54. fclose(file);
  55. }
  56. }
  57. void log(const TCHAR* msg, bool bFromSendRecieve, CString csFile, long lLine)
  58. {
  59. ASSERT(AfxIsValidString(msg));
  60. SYSTEMTIME st;
  61. GetLocalTime(&st);
  62. CString csText;
  63. csText.Format(_T("[%d/%d/%d %02d:%02d:%02d.%03d - "), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
  64. CString csFileLine;
  65. csFile = GetFileName(csFile);
  66. csFileLine.Format(_T("%s %d] "), csFile, lLine);
  67. csText += csFileLine;
  68. csText += msg;
  69. csText += "\n";
  70. #ifndef _DEBUG
  71. if(CGetSetOptions::m_bOutputDebugString)
  72. #endif
  73. {
  74. OutputDebugString(csText);
  75. }
  76. #ifndef _DEBUG
  77. if(!bFromSendRecieve)
  78. {
  79. if(!g_Opt.m_bEnableDebugLogging)
  80. return;
  81. }
  82. #endif
  83. CString csExeFile = CGetSetOptions::GetPath(PATH_LOG_FILE);
  84. csExeFile += "Ditto.log";
  85. AppendToFile(csExeFile, csText);
  86. }
  87. void logsendrecieveinfo(CString cs, CString csFile, long lLine)
  88. {
  89. if(g_Opt.m_bLogSendReceiveErrors)
  90. log(cs, true, csFile, lLine);
  91. }
  92. CString GetErrorString( int err )
  93. {
  94. CString str;
  95. LPVOID lpMsgBuf;
  96. ::FormatMessage(
  97. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  98. NULL,
  99. err,
  100. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  101. (LPTSTR) &lpMsgBuf,
  102. 0,
  103. NULL
  104. );
  105. str = (LPCTSTR) lpMsgBuf;
  106. // Display the string.
  107. // ::MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
  108. ::LocalFree( lpMsgBuf );
  109. return str;
  110. }
  111. int g_funnyGetTickCountAdjustment = -1;
  112. double IdleSeconds()
  113. {
  114. LASTINPUTINFO info;
  115. info.cbSize = sizeof(info);
  116. GetLastInputInfo(&info);
  117. DWORD currentTick = GetTickCount();
  118. if(g_funnyGetTickCountAdjustment == -1)
  119. {
  120. if(currentTick < info.dwTime)
  121. {
  122. g_funnyGetTickCountAdjustment = 1;
  123. }
  124. else
  125. {
  126. g_funnyGetTickCountAdjustment = 0;
  127. }
  128. }
  129. if(g_funnyGetTickCountAdjustment == 1 || g_funnyGetTickCountAdjustment == 2)
  130. {
  131. //Output message the first time
  132. if(g_funnyGetTickCountAdjustment == 1)
  133. {
  134. Log(StrF(_T("Adjusting time of get tickcount by: %d, on startup we found GetTickCount to be less than last input"), CGetSetOptions::GetFunnyTickCountAdjustment()));
  135. g_funnyGetTickCountAdjustment = 2;
  136. }
  137. currentTick += CGetSetOptions::GetFunnyTickCountAdjustment();
  138. }
  139. double idleSeconds = (currentTick - info.dwTime)/1000.0;
  140. return idleSeconds;
  141. }
  142. CString StrF(const TCHAR * pszFormat, ...)
  143. {
  144. ASSERT( AfxIsValidString( pszFormat ) );
  145. CString str;
  146. va_list argList;
  147. va_start( argList, pszFormat );
  148. str.FormatV( pszFormat, argList );
  149. va_end( argList );
  150. return str;
  151. }
  152. BYTE GetEscapeChar( BYTE ch )
  153. {
  154. switch(ch)
  155. {
  156. case '\'': return '\''; // Single quotation mark (') = 39 or 0x27
  157. case '\"': return '\"'; // Double quotation mark (") = 34 or 0x22
  158. case '?': return '\?'; // Question mark (?) = 63 or 0x3f
  159. case '\\': return '\\'; // Backslash (\) = 92 or 0x5c
  160. case 'a': return '\a'; // Alert (BEL) = 7
  161. case 'b': return '\b'; // Backspace (BS) = 8
  162. case 'f': return '\f'; // Formfeed (FF) = 12 or 0x0c
  163. case 'n': return '\n'; // Newline (NL or LF) = 10 or 0x0a
  164. case 'r': return '\r'; // Carriage Return (CR) = 13 or 0x0d
  165. case 't': return '\t'; // Horizontal tab (HT) = 9
  166. case 'v': return '\v'; // Vertical tab (VT) = 11 or 0x0b
  167. case '0': return '\0'; // Null character (NUL) = 0
  168. }
  169. return 0; // invalid
  170. }
  171. CString RemoveEscapes( const TCHAR* str )
  172. {
  173. ASSERT( str );
  174. CString ret;
  175. TCHAR* pSrc = (TCHAR*) str;
  176. TCHAR* pDest = ret.GetBuffer((int)STRLEN(pSrc));
  177. TCHAR* pStart = pDest;
  178. while( *pSrc != '\0' )
  179. {
  180. if( *pSrc == '\\' )
  181. {
  182. pSrc++;
  183. *pDest = GetEscapeChar((BYTE)pSrc );
  184. }
  185. else
  186. *pDest = *pSrc;
  187. pSrc++;
  188. pDest++;
  189. }
  190. ret.ReleaseBuffer((int)(pDest - pStart));
  191. return ret;
  192. }
  193. CString GetWndText( HWND hWnd )
  194. {
  195. CString text;
  196. if( !IsWindow(hWnd) )
  197. return "! NOT A VALID WINDOW !";
  198. CWnd* pWnd = CWnd::FromHandle(hWnd);
  199. pWnd->GetWindowText(text);
  200. return text;
  201. }
  202. bool IsAppWnd( HWND hWnd )
  203. {
  204. DWORD dwMyPID = ::GetCurrentProcessId();
  205. DWORD dwTestPID;
  206. ::GetWindowThreadProcessId( hWnd, &dwTestPID );
  207. return dwMyPID == dwTestPID;
  208. }
  209. /*----------------------------------------------------------------------------*\
  210. Global Memory Helper Functions
  211. \*----------------------------------------------------------------------------*/
  212. // make sure the given HGLOBAL is valid.
  213. BOOL IsValid(HGLOBAL hGlobal)
  214. {
  215. void* pvData = ::GlobalLock(hGlobal);
  216. ::GlobalUnlock(hGlobal);
  217. return (pvData != NULL);
  218. }
  219. // asserts if hDest isn't big enough
  220. void CopyToGlobalHP(HGLOBAL hDest, LPVOID pBuf, SIZE_T ulBufLen)
  221. {
  222. ASSERT(hDest && pBuf && ulBufLen);
  223. LPVOID pvData = GlobalLock(hDest);
  224. ASSERT(pvData);
  225. SIZE_T size = GlobalSize(hDest);
  226. ASSERT(size >= ulBufLen); // assert if hDest isn't big enough
  227. memcpy(pvData, pBuf, ulBufLen);
  228. GlobalUnlock(hDest);
  229. }
  230. void CopyToGlobalHH(HGLOBAL hDest, HGLOBAL hSource, SIZE_T ulBufLen)
  231. {
  232. ASSERT(hDest && hSource && ulBufLen);
  233. LPVOID pvData = GlobalLock(hSource);
  234. ASSERT(pvData );
  235. SIZE_T size = GlobalSize(hSource);
  236. ASSERT(size >= ulBufLen); // assert if hSource isn't big enough
  237. CopyToGlobalHP(hDest, pvData, ulBufLen);
  238. GlobalUnlock(hSource);
  239. }
  240. HGLOBAL NewGlobalP(LPVOID pBuf, SIZE_T nLen)
  241. {
  242. ASSERT(pBuf && nLen);
  243. HGLOBAL hDest = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, nLen);
  244. ASSERT(hDest );
  245. CopyToGlobalHP(hDest, pBuf, nLen);
  246. return hDest;
  247. }
  248. HGLOBAL NewGlobal(SIZE_T nLen)
  249. {
  250. ASSERT(nLen);
  251. HGLOBAL hDest = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, nLen);
  252. return hDest;
  253. }
  254. HGLOBAL NewGlobalH(HGLOBAL hSource, SIZE_T nLen)
  255. {
  256. ASSERT(hSource && nLen);
  257. LPVOID pvData = GlobalLock(hSource);
  258. HGLOBAL hDest = NewGlobalP(pvData, nLen);
  259. GlobalUnlock(hSource);
  260. return hDest;
  261. }
  262. int CompareGlobalHP(HGLOBAL hLeft, LPVOID pBuf, SIZE_T ulBufLen)
  263. {
  264. ASSERT(hLeft && pBuf && ulBufLen);
  265. LPVOID pvData = GlobalLock(hLeft);
  266. ASSERT(pvData);
  267. ASSERT(ulBufLen <= GlobalSize(hLeft));
  268. int result = memcmp(pvData, pBuf, ulBufLen);
  269. GlobalUnlock(hLeft);
  270. return result;
  271. }
  272. int CompareGlobalHH( HGLOBAL hLeft, HGLOBAL hRight, SIZE_T ulBufLen)
  273. {
  274. ASSERT(hLeft && hRight && ulBufLen);
  275. ASSERT(ulBufLen <= GlobalSize(hRight));
  276. LPVOID pvData = GlobalLock(hRight);
  277. ASSERT(pvData);
  278. int result = CompareGlobalHP(hLeft, pvData, ulBufLen);
  279. GlobalUnlock(hLeft);
  280. return result;
  281. }
  282. //Do not change these these are stored in the database
  283. CLIPFORMAT GetFormatID(LPCTSTR cbName)
  284. {
  285. if(STRCMP(cbName, _T("CF_TEXT")) == 0)
  286. return CF_TEXT;
  287. else if(STRCMP(cbName, _T("CF_METAFILEPICT")) == 0)
  288. return CF_METAFILEPICT;
  289. else if(STRCMP(cbName, _T("CF_SYLK")) == 0)
  290. return CF_SYLK;
  291. else if(STRCMP(cbName, _T("CF_DIF")) == 0)
  292. return CF_DIF;
  293. else if(STRCMP(cbName, _T("CF_TIFF")) == 0)
  294. return CF_TIFF;
  295. else if(STRCMP(cbName, _T("CF_OEMTEXT")) == 0)
  296. return CF_OEMTEXT;
  297. else if(STRCMP(cbName, _T("CF_DIB")) == 0)
  298. return CF_DIB;
  299. else if(STRCMP(cbName, _T("CF_PALETTE")) == 0)
  300. return CF_PALETTE;
  301. else if(STRCMP(cbName, _T("CF_PENDATA")) == 0)
  302. return CF_PENDATA;
  303. else if(STRCMP(cbName, _T("CF_RIFF")) == 0)
  304. return CF_RIFF;
  305. else if(STRCMP(cbName, _T("CF_WAVE")) == 0)
  306. return CF_WAVE;
  307. else if(STRCMP(cbName, _T("CF_UNICODETEXT")) == 0)
  308. return CF_UNICODETEXT;
  309. else if(STRCMP(cbName, _T("CF_ENHMETAFILE")) == 0)
  310. return CF_ENHMETAFILE;
  311. else if(STRCMP(cbName, _T("CF_HDROP")) == 0)
  312. return CF_HDROP;
  313. else if(STRCMP(cbName, _T("CF_LOCALE")) == 0)
  314. return CF_LOCALE;
  315. else if(STRCMP(cbName, _T("CF_OWNERDISPLAY")) == 0)
  316. return CF_OWNERDISPLAY;
  317. else if(STRCMP(cbName, _T("CF_DSPTEXT")) == 0)
  318. return CF_DSPTEXT;
  319. else if(STRCMP(cbName, _T("CF_DSPBITMAP")) == 0)
  320. return CF_DSPBITMAP;
  321. else if(STRCMP(cbName, _T("CF_DSPMETAFILEPICT")) == 0)
  322. return CF_DSPMETAFILEPICT;
  323. else if(STRCMP(cbName, _T("CF_DSPENHMETAFILE")) == 0)
  324. return CF_DSPENHMETAFILE;
  325. return ::RegisterClipboardFormat(cbName);
  326. }
  327. //Do not change these these are stored in the database
  328. CString GetFormatName(CLIPFORMAT cbType)
  329. {
  330. switch(cbType)
  331. {
  332. case CF_TEXT:
  333. return _T("CF_TEXT");
  334. case CF_BITMAP:
  335. return _T("CF_BITMAP");
  336. case CF_METAFILEPICT:
  337. return _T("CF_METAFILEPICT");
  338. case CF_SYLK:
  339. return _T("CF_SYLK");
  340. case CF_DIF:
  341. return _T("CF_DIF");
  342. case CF_TIFF:
  343. return _T("CF_TIFF");
  344. case CF_OEMTEXT:
  345. return _T("CF_OEMTEXT");
  346. case CF_DIB:
  347. return _T("CF_DIB");
  348. case CF_PALETTE:
  349. return _T("CF_PALETTE");
  350. case CF_PENDATA:
  351. return _T("CF_PENDATA");
  352. case CF_RIFF:
  353. return _T("CF_RIFF");
  354. case CF_WAVE:
  355. return _T("CF_WAVE");
  356. case CF_UNICODETEXT:
  357. return _T("CF_UNICODETEXT");
  358. case CF_ENHMETAFILE:
  359. return _T("CF_ENHMETAFILE");
  360. case CF_HDROP:
  361. return _T("CF_HDROP");
  362. case CF_LOCALE:
  363. return _T("CF_LOCALE");
  364. case CF_OWNERDISPLAY:
  365. return _T("CF_OWNERDISPLAY");
  366. case CF_DSPTEXT:
  367. return _T("CF_DSPTEXT");
  368. case CF_DSPBITMAP:
  369. return _T("CF_DSPBITMAP");
  370. case CF_DSPMETAFILEPICT:
  371. return _T("CF_DSPMETAFILEPICT");
  372. case CF_DSPENHMETAFILE:
  373. return _T("CF_DSPENHMETAFILE");
  374. default:
  375. //Not a default type get the name from the clipboard
  376. if (cbType != 0)
  377. {
  378. TCHAR szFormat[256];
  379. GetClipboardFormatName(cbType, szFormat, 256);
  380. return szFormat;
  381. }
  382. break;
  383. }
  384. return "ERROR";
  385. }
  386. CString GetFilePath(CString csFileName)
  387. {
  388. long lSlash = csFileName.ReverseFind('\\');
  389. if(lSlash > -1)
  390. {
  391. csFileName = csFileName.Left(lSlash + 1);
  392. }
  393. return csFileName;
  394. }
  395. CString GetFileName(CString csFileName)
  396. {
  397. long lSlash = csFileName.ReverseFind('\\');
  398. if(lSlash > -1)
  399. {
  400. csFileName = csFileName.Right(csFileName.GetLength() - lSlash - 1);
  401. }
  402. return csFileName;
  403. }
  404. /****************************************************************************************************
  405. BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
  406. ***************************************************************************************************/
  407. typedef struct
  408. {
  409. long lFlags; // Flags
  410. LPRECT pVirtualRect; // Ptr to rect that receives the results, or the src of the monitor search method
  411. int iMonitor; // Ndx to the mointor to look at, -1 for all, -or- result of the monitor search method
  412. int nMonitorCount; // Total number of monitors found, -1 for monitor search method
  413. } MONITOR_ENUM_PARAM;
  414. #define MONITOR_SEARCH_METOHD 0x00000001
  415. BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
  416. {
  417. // Typecast param
  418. MONITOR_ENUM_PARAM* pParam = (MONITOR_ENUM_PARAM*)dwData;
  419. if(pParam)
  420. {
  421. // If a dest rect was passed
  422. if(pParam->pVirtualRect)
  423. {
  424. // If MONITOR_SEARCH_METOHD then we are being asked for the index of the monitor
  425. // that the rect falls inside of
  426. if(pParam->lFlags & MONITOR_SEARCH_METOHD)
  427. {
  428. if( (pParam->pVirtualRect->right < lprcMonitor->left) ||
  429. (pParam->pVirtualRect->left > lprcMonitor->right) ||
  430. (pParam->pVirtualRect->bottom < lprcMonitor->top) ||
  431. (pParam->pVirtualRect->top > lprcMonitor->bottom))
  432. {
  433. // Nothing
  434. }
  435. else
  436. {
  437. // This is the one
  438. pParam->iMonitor = pParam->nMonitorCount;
  439. // Stop the enumeration
  440. return FALSE;
  441. }
  442. }
  443. else
  444. {
  445. if(pParam->iMonitor == pParam->nMonitorCount)
  446. {
  447. *pParam->pVirtualRect = *lprcMonitor;
  448. }
  449. else
  450. if(pParam->iMonitor == -1)
  451. {
  452. pParam->pVirtualRect->left = min(pParam->pVirtualRect->left, lprcMonitor->left);
  453. pParam->pVirtualRect->top = min(pParam->pVirtualRect->top, lprcMonitor->top);
  454. pParam->pVirtualRect->right = max(pParam->pVirtualRect->right, lprcMonitor->right);
  455. pParam->pVirtualRect->bottom = max(pParam->pVirtualRect->bottom, lprcMonitor->bottom);
  456. }
  457. }
  458. }
  459. // Up the count if necessary
  460. pParam->nMonitorCount++;
  461. }
  462. return TRUE;
  463. }
  464. int GetScreenWidth(void)
  465. {
  466. OSVERSIONINFO OS_Version_Info;
  467. DWORD dwPlatform = 0;
  468. if(GetVersionEx(&OS_Version_Info) != 0)
  469. {
  470. dwPlatform = OS_Version_Info.dwPlatformId;
  471. }
  472. if(dwPlatform == VER_PLATFORM_WIN32_NT)
  473. {
  474. int width, height;
  475. width = GetSystemMetrics(SM_CXSCREEN);
  476. height = GetSystemMetrics(SM_CYSCREEN);
  477. switch(width)
  478. {
  479. default:
  480. case 640:
  481. case 800:
  482. case 1024:
  483. return(width);
  484. case 1280:
  485. if(height == 480)
  486. {
  487. return(width / 2);
  488. }
  489. return(width);
  490. case 1600:
  491. if(height == 600)
  492. {
  493. return(width / 2);
  494. }
  495. return(width);
  496. case 2048:
  497. if(height == 768)
  498. {
  499. return(width / 2);
  500. }
  501. return(width);
  502. }
  503. }
  504. else
  505. {
  506. return(GetSystemMetrics(SM_CXSCREEN));
  507. }
  508. }
  509. int GetScreenHeight(void)
  510. {
  511. OSVERSIONINFO OS_Version_Info;
  512. DWORD dwPlatform = 0;
  513. if(GetVersionEx(&OS_Version_Info) != 0)
  514. {
  515. dwPlatform = OS_Version_Info.dwPlatformId;
  516. }
  517. if(dwPlatform == VER_PLATFORM_WIN32_NT)
  518. {
  519. int width, height;
  520. width = GetSystemMetrics(SM_CXSCREEN);
  521. height = GetSystemMetrics(SM_CYSCREEN);
  522. switch(height)
  523. {
  524. default:
  525. case 480:
  526. case 600:
  527. case 768:
  528. return(height);
  529. case 960:
  530. if(width == 640)
  531. {
  532. return(height / 2);
  533. }
  534. return(height);
  535. case 1200:
  536. if(width == 800)
  537. {
  538. return(height / 2);
  539. }
  540. return(height);
  541. case 1536:
  542. if(width == 1024)
  543. {
  544. return(height / 2);
  545. }
  546. return(height);
  547. }
  548. }
  549. else
  550. {
  551. return(GetSystemMetrics(SM_CYSCREEN));
  552. }
  553. }
  554. int GetMonitorFromRect(LPRECT lpMonitorRect)
  555. {
  556. // Build up the param
  557. MONITOR_ENUM_PARAM EnumParam;
  558. ZeroMemory(&EnumParam, sizeof(EnumParam));
  559. EnumParam.lFlags = MONITOR_SEARCH_METOHD;
  560. EnumParam.pVirtualRect = lpMonitorRect;
  561. EnumParam.iMonitor = -1;
  562. // Enum Displays
  563. EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (long)&EnumParam);
  564. // Return the result
  565. return EnumParam.iMonitor;
  566. }
  567. void GetMonitorRect(int iMonitor, LPRECT lpDestRect)
  568. {
  569. // Build up the param
  570. MONITOR_ENUM_PARAM EnumParam;
  571. ZeroMemory(&EnumParam, sizeof(EnumParam));
  572. EnumParam.iMonitor = iMonitor;
  573. EnumParam.pVirtualRect = lpDestRect;
  574. // Zero out dest rect
  575. lpDestRect->bottom = lpDestRect->left = lpDestRect->right = lpDestRect->top = 0;
  576. // Enum Displays
  577. EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (long)&EnumParam);
  578. // If not successful, default to the screen dimentions
  579. if(lpDestRect->right == 0 || lpDestRect->bottom == 0)
  580. {
  581. lpDestRect->right = GetScreenWidth();
  582. lpDestRect->bottom = GetScreenHeight();
  583. }
  584. //adjust the rect for the taskbar
  585. APPBARDATA appBarData;
  586. appBarData.cbSize=sizeof(appBarData);
  587. if (SHAppBarMessage(ABM_GETTASKBARPOS, &appBarData))
  588. {
  589. switch(appBarData.uEdge)
  590. {
  591. case ABE_LEFT:
  592. lpDestRect->left += appBarData.rc.right - appBarData.rc.left;
  593. break;
  594. case ABE_RIGHT:
  595. lpDestRect->right -= appBarData.rc.right - appBarData.rc.left;
  596. break;
  597. case ABE_TOP:
  598. lpDestRect->top += appBarData.rc.bottom - appBarData.rc.top;
  599. break;
  600. case ABE_BOTTOM:
  601. lpDestRect->bottom -= appBarData.rc.bottom - appBarData.rc.top;
  602. break;
  603. }
  604. return;
  605. }
  606. }
  607. /*------------------------------------------------------------------*\
  608. ID based Globals
  609. \*------------------------------------------------------------------*/
  610. long NewGroupID(int parentID, CString text)
  611. {
  612. long lID=0;
  613. CTime time;
  614. time = CTime::GetCurrentTime();
  615. try
  616. {
  617. //sqlite doesn't like single quotes ' replace them with double ''
  618. if(text.IsEmpty())
  619. text = time.Format("NewGroup %y/%m/%d %H:%M:%S");
  620. text.Replace(_T("'"), _T("''"));
  621. CString cs;
  622. cs.Format(_T("insert into Main (lDate, mText, lDontAutoDelete, bIsGroup, lParentID) values(%d, '%s', %d, 1, %d);"),
  623. (int)time.GetTime(),
  624. text,
  625. (int)time.GetTime(),
  626. parentID);
  627. theApp.m_db.execDML(cs);
  628. lID = (long)theApp.m_db.lastRowId();
  629. }
  630. CATCH_SQLITE_EXCEPTION_AND_RETURN(0)
  631. return lID;
  632. }
  633. BOOL DeleteAllIDs()
  634. {
  635. try
  636. {
  637. theApp.m_db.execDML(_T("DELETE FROM Data;"));
  638. theApp.m_db.execDML(_T("DELETE FROM Main;"));
  639. }
  640. CATCH_SQLITE_EXCEPTION
  641. return TRUE;
  642. }
  643. BOOL DeleteFormats(int parentID, ARRAY& formatIDs)
  644. {
  645. if(formatIDs.GetSize() <= 0)
  646. return TRUE;
  647. try
  648. {
  649. //Delete the requested data formats
  650. INT_PTR count = formatIDs.GetSize();
  651. for(int i = 0; i < count; i++)
  652. {
  653. theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lID = %d;"), formatIDs[i]);
  654. }
  655. CClip clip;
  656. if(clip.LoadFormats(parentID))
  657. {
  658. DWORD CRC = clip.GenerateCRC();
  659. //Update the main table with new size
  660. theApp.m_db.execDMLEx(_T("UPDATE Main SET CRC = %d WHERE lID = %d"), CRC, parentID);
  661. }
  662. }
  663. CATCH_SQLITE_EXCEPTION
  664. return TRUE;
  665. }
  666. CRect CenterRect(CRect startingRect)
  667. {
  668. CRect crMonitor;
  669. int nMonitor = GetMonitorFromRect(&startingRect);
  670. if(nMonitor < 0)
  671. {
  672. GetMonitorRect(0, crMonitor);
  673. }
  674. else
  675. {
  676. GetMonitorRect(nMonitor, crMonitor);
  677. }
  678. return CenterRectFromRect(startingRect, crMonitor);
  679. }
  680. CRect CenterRectFromRect(CRect startingRect, CRect outerRect)
  681. {
  682. CPoint center = outerRect.CenterPoint();
  683. CRect centerRect;
  684. centerRect.left = center.x - (startingRect.Width() / 2);
  685. centerRect.top = center.y - (startingRect.Height() / 2);
  686. centerRect.right = centerRect.left + startingRect.Width();
  687. centerRect.bottom = centerRect.top + startingRect.Height();
  688. return centerRect;
  689. }
  690. BOOL EnsureWindowVisible(CRect *pcrRect)
  691. {
  692. int nMonitor = GetMonitorFromRect(pcrRect);
  693. if(nMonitor < 0)
  694. {
  695. GetMonitorRect(0, pcrRect);
  696. pcrRect->right = pcrRect->left + 300;
  697. pcrRect->bottom = pcrRect->top + 300;
  698. return TRUE;
  699. }
  700. BOOL ret = FALSE;
  701. CRect crMonitor;
  702. GetMonitorRect(nMonitor, crMonitor);
  703. bool movedLeft = false;
  704. //Validate the left
  705. long lDiff = pcrRect->left - crMonitor.left;
  706. if(lDiff < 0)
  707. {
  708. pcrRect->left += abs(lDiff);
  709. pcrRect->right += abs(lDiff);
  710. ret = TRUE;
  711. movedLeft = true;
  712. }
  713. //Right side
  714. lDiff = pcrRect->right - crMonitor.right;
  715. if(lDiff > 0)
  716. {
  717. if(movedLeft == false)
  718. {
  719. pcrRect->left -= abs(lDiff);
  720. }
  721. pcrRect->right -= abs(lDiff);
  722. ret = TRUE;
  723. }
  724. bool movedTop = false;
  725. //Top
  726. lDiff = pcrRect->top - crMonitor.top;
  727. if(lDiff < 0)
  728. {
  729. pcrRect->top += abs(lDiff);
  730. pcrRect->bottom += abs(lDiff);
  731. ret = TRUE;
  732. movedTop = true;
  733. }
  734. //Bottom
  735. lDiff = pcrRect->bottom - crMonitor.bottom;
  736. if(lDiff > 0)
  737. {
  738. if(movedTop == false)
  739. {
  740. pcrRect->top -= abs(lDiff);
  741. }
  742. pcrRect->bottom -= abs(lDiff);
  743. ret = TRUE;
  744. }
  745. return ret;
  746. }
  747. __int64 GetLastWriteTime(const CString &csFile)
  748. {
  749. __int64 nLastWrite = 0;
  750. CFileFind finder;
  751. BOOL bResult = finder.FindFile(csFile);
  752. if (bResult)
  753. {
  754. finder.FindNextFile();
  755. FILETIME ft;
  756. finder.GetLastWriteTime(&ft);
  757. memcpy(&nLastWrite, &ft, sizeof(ft));
  758. }
  759. return nLastWrite;
  760. }
  761. CString GetProcessName(HWND hWnd)
  762. {
  763. DWORD startTick = GetTickCount();
  764. DWORD Id;
  765. CString strProcessName ;
  766. GetWindowThreadProcessId(hWnd, &Id);
  767. PROCESSENTRY32 processEntry = { 0 };
  768. HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  769. processEntry.dwSize = sizeof(PROCESSENTRY32);
  770. if (Process32First(hSnapShot, &processEntry))
  771. {
  772. do
  773. {
  774. if (processEntry.th32ProcessID == Id)
  775. {
  776. strProcessName = processEntry.szExeFile;
  777. break;
  778. }
  779. } while(Process32Next(hSnapShot, &processEntry));
  780. }
  781. CloseHandle(hSnapShot);
  782. DWORD endTick = GetTickCount();
  783. DWORD diff = endTick - startTick;
  784. if(diff > 5)
  785. {
  786. Log(StrF(_T("GetProcessName Time (ms): %d"), endTick-startTick));
  787. }
  788. return strProcessName;
  789. }
  790. BOOL IsVista()
  791. {
  792. OSVERSIONINFO osver;
  793. osver.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
  794. if (::GetVersionEx( &osver ) &&
  795. osver.dwPlatformId == VER_PLATFORM_WIN32_NT &&
  796. (osver.dwMajorVersion >= 6 ) )
  797. {
  798. return TRUE;
  799. }
  800. return FALSE;
  801. }
  802. bool IsRunningLimited()
  803. {
  804. LPCTSTR pszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
  805. LPCTSTR pszValue = _T("EnableLUA");
  806. DWORD dwType = 0;
  807. DWORD dwValue = 0;
  808. DWORD dwValueSize = sizeof(DWORD);
  809. if(ERROR_SUCCESS != SHGetValue(HKEY_LOCAL_MACHINE, pszSubKey, pszValue, &dwType, &dwValue, &dwValueSize))
  810. {
  811. //failed to read the reg key, either it's not there or we don't have access to the registry
  812. //If we are vista then assume we don't have access and we are running as a limited app
  813. //otherwise we are xp and the reg key probably doesn't exist and we are not a limited running app
  814. if(IsVista())
  815. {
  816. OutputDebugString(_T("Ditto - Failed to read registry entry finding UAC, Running as limited application"));
  817. return true;
  818. }
  819. }
  820. if(dwValue == 1)
  821. {
  822. OutputDebugString(_T("Ditto - UAC ENABLED, Running as limited application"));
  823. return true;
  824. }
  825. OutputDebugString(_T("Ditto - Running as standard application"));
  826. return false;
  827. }
  828. void DeleteReceivedFiles(CString csDir)
  829. {
  830. if(csDir.Find(_T("\\ReceivedFiles\\")) == -1)
  831. return;
  832. FIX_CSTRING_PATH(csDir);
  833. CTime ctOld = CTime::GetCurrentTime();
  834. CTime ctFile;
  835. ctOld -= CTimeSpan(0, 0, 0, 1);
  836. CFileFind Find;
  837. CString csFindString;
  838. csFindString.Format(_T("%s*.*"), csDir);
  839. BOOL bFound = Find.FindFile(csFindString);
  840. while(bFound)
  841. {
  842. bFound = Find.FindNextFile();
  843. if(Find.IsDots())
  844. continue;
  845. if(Find.IsDirectory())
  846. {
  847. CString csDir(Find.GetFilePath());
  848. DeleteReceivedFiles(csDir);
  849. RemoveDirectory(csDir);
  850. }
  851. if(Find.GetLastAccessTime(ctFile))
  852. {
  853. //Delete the remote copied file if it has'nt been used for the last day
  854. if(ctFile < ctOld)
  855. {
  856. DeleteFile(Find.GetFilePath());
  857. }
  858. }
  859. else
  860. {
  861. DeleteFile(Find.GetFilePath());
  862. }
  863. }
  864. }
  865. __int64 FileSize(const TCHAR *fileName)
  866. {
  867. __stat64 buf;
  868. if (_wstat64(fileName, &buf) != 0)
  869. return -1; // error, could use errno to find out more
  870. return buf.st_size;
  871. }
  872. int FindNoCaseAndInsert(CString& mainStr, CString& findStr, CString preInsert, CString postInsert)
  873. {
  874. int replaceCount = 0;
  875. //Prevent infinite loop when user tries to replace nothing.
  876. if (findStr != "")
  877. {
  878. int oldLen = findStr.GetLength();
  879. int nPos = 0;
  880. int newPos = 0;
  881. int insertedLength = 0;
  882. //Copies of the main string and the string to be replaced that
  883. //are made lower case
  884. CString mainStr_low(mainStr);
  885. CString findStr_low(findStr);
  886. mainStr_low.MakeLower();
  887. findStr_low.MakeLower();
  888. int preLength = preInsert.GetLength();
  889. int postLength = postInsert.GetLength();
  890. while ((nPos = mainStr_low.Find(findStr_low, nPos)) != -1)
  891. {
  892. newPos = nPos + insertedLength;
  893. mainStr.Insert(newPos, preInsert);
  894. mainStr.Insert(newPos + preLength + oldLen, postInsert);
  895. nPos += oldLen;
  896. insertedLength += preLength + postLength;
  897. replaceCount++;
  898. }
  899. }
  900. return replaceCount;
  901. }
  902. void OnInitMenuPopupEx(CMenu *pPopupMenu, UINT nIndex, BOOL bSysMenu, CWnd *pWnd)
  903. {
  904. ASSERT(pPopupMenu != NULL);
  905. // Check the enabled state of various menu items.
  906. CCmdUI state;
  907. state.m_pMenu = pPopupMenu;
  908. ASSERT(state.m_pOther == NULL);
  909. ASSERT(state.m_pParentMenu == NULL);
  910. // Determine if menu is popup in top-level menu and set m_pOther to
  911. // it if so (m_pParentMenu == NULL indicates that it is secondary popup).
  912. HMENU hParentMenu;
  913. if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)
  914. {
  915. state.m_pParentMenu = pPopupMenu; // Parent == child for tracking popup.
  916. }
  917. else if ((hParentMenu = ::GetMenu(pWnd->m_hWnd)) != NULL)
  918. {
  919. CWnd* pParent = pWnd;
  920. // Child windows don't have menus--need to go to the top!
  921. if (pParent != NULL &&
  922. (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
  923. {
  924. int nIndexMax = ::GetMenuItemCount(hParentMenu);
  925. for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
  926. {
  927. if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)
  928. {
  929. // When popup is found, m_pParentMenu is containing menu.
  930. state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
  931. break;
  932. }
  933. }
  934. }
  935. }
  936. state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
  937. for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
  938. state.m_nIndex++)
  939. {
  940. state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
  941. if (state.m_nID == 0)
  942. continue; // Menu separator or invalid cmd - ignore it.
  943. ASSERT(state.m_pOther == NULL);
  944. ASSERT(state.m_pMenu != NULL);
  945. if (state.m_nID == (UINT)-1)
  946. {
  947. // Possibly a popup menu, route to first item of that popup.
  948. state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
  949. if (state.m_pSubMenu == NULL ||
  950. (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
  951. state.m_nID == (UINT)-1)
  952. {
  953. continue; // First item of popup can't be routed to.
  954. }
  955. state.DoUpdate(pWnd, TRUE); // Popups are never auto disabled.
  956. }
  957. else
  958. {
  959. // Normal menu item.
  960. // Auto enable/disable if frame window has m_bAutoMenuEnable
  961. // set and command is _not_ a system command.
  962. state.m_pSubMenu = NULL;
  963. state.DoUpdate(pWnd, FALSE);
  964. }
  965. // Adjust for menu deletions and additions.
  966. UINT nCount = pPopupMenu->GetMenuItemCount();
  967. if (nCount < state.m_nIndexMax)
  968. {
  969. state.m_nIndex -= (state.m_nIndexMax - nCount);
  970. while (state.m_nIndex < nCount &&
  971. pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
  972. {
  973. state.m_nIndex++;
  974. }
  975. }
  976. state.m_nIndexMax = nCount;
  977. }
  978. }