Misc.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include "Misc.h"
  4. #include "OptionsSheet.h"
  5. #ifdef AFTER_98
  6. #include "AlphaBlend.h"
  7. #endif
  8. // Debug Functions
  9. void AppendToFile( const char* fn, const char* msg )
  10. {
  11. FILE *file = fopen(fn, "a");
  12. ASSERT( file );
  13. fprintf(file, msg);
  14. fclose(file);
  15. }
  16. void Log( const char* msg )
  17. {
  18. ASSERT( AfxIsValidString(msg) );
  19. CTime time = CTime::GetCurrentTime();
  20. CString csText = time.Format("[%Y/%m/%d %I:%M:%S %p] ");
  21. //CString csTemp;
  22. // csTemp.Format( "%04x ", AfxGetInstanceHandle() );
  23. csText += msg;
  24. csText += "\n";
  25. AppendToFile( "Ditto.log", csText ); //(LPCTSTR)
  26. }
  27. CString GetErrorString( int err )
  28. {
  29. CString str;
  30. LPVOID lpMsgBuf;
  31. ::FormatMessage(
  32. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  33. NULL,
  34. err,
  35. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  36. (LPTSTR) &lpMsgBuf,
  37. 0,
  38. NULL
  39. );
  40. str = (LPCTSTR) lpMsgBuf;
  41. // Display the string.
  42. // ::MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
  43. ::LocalFree( lpMsgBuf );
  44. return str;
  45. }
  46. void SetThreadName(DWORD dwThreadID, LPCTSTR szThreadName)
  47. {
  48. THREADNAME_INFO info;
  49. info.dwType = 0x1000;
  50. info.szName = szThreadName;
  51. info.dwThreadID = dwThreadID;
  52. info.dwFlags = 0;
  53. __try
  54. {
  55. RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(DWORD), (DWORD *)&info);
  56. }
  57. __except (EXCEPTION_CONTINUE_EXECUTION)
  58. {
  59. }
  60. }
  61. // Utility Functions
  62. CString StrF(const char * pszFormat, ...)
  63. {
  64. ASSERT( AtlIsValidString( pszFormat ) );
  65. CString str;
  66. va_list argList;
  67. va_start( argList, pszFormat );
  68. str.FormatV( pszFormat, argList );
  69. va_end( argList );
  70. return str;
  71. }
  72. BYTE GetEscapeChar( BYTE ch )
  73. {
  74. switch(ch)
  75. {
  76. case '\'': return '\''; // Single quotation mark (') = 39 or 0x27
  77. case '\"': return '\"'; // Double quotation mark (") = 34 or 0x22
  78. case '?': return '\?'; // Question mark (?) = 63 or 0x3f
  79. case '\\': return '\\'; // Backslash (\) = 92 or 0x5c
  80. case 'a': return '\a'; // Alert (BEL) = 7
  81. case 'b': return '\b'; // Backspace (BS) = 8
  82. case 'f': return '\f'; // Formfeed (FF) = 12 or 0x0c
  83. case 'n': return '\n'; // Newline (NL or LF) = 10 or 0x0a
  84. case 'r': return '\r'; // Carriage Return (CR) = 13 or 0x0d
  85. case 't': return '\t'; // Horizontal tab (HT) = 9
  86. case 'v': return '\v'; // Vertical tab (VT) = 11 or 0x0b
  87. case '0': return '\0'; // Null character (NUL) = 0
  88. }
  89. return 0; // invalid
  90. }
  91. CString RemoveEscapes( const char* str )
  92. {
  93. ASSERT( str );
  94. CString ret;
  95. char* pSrc = (char*) str;
  96. char* pDest = ret.GetBuffer( strlen(pSrc) );
  97. char* pStart = pDest;
  98. while( *pSrc != '\0' )
  99. {
  100. if( *pSrc == '\\' )
  101. {
  102. pSrc++;
  103. *pDest = GetEscapeChar( *pSrc );
  104. }
  105. else
  106. *pDest = *pSrc;
  107. pSrc++;
  108. pDest++;
  109. }
  110. ret.ReleaseBuffer( pDest - pStart );
  111. return ret;
  112. }
  113. CString GetWndText( HWND hWnd )
  114. {
  115. CString text;
  116. if( !IsWindow(hWnd) )
  117. return "! NOT A VALID WINDOW !";
  118. CWnd* pWnd = CWnd::FromHandle(hWnd);
  119. pWnd->GetWindowText(text);
  120. return text;
  121. }
  122. bool IsAppWnd( HWND hWnd )
  123. {
  124. DWORD dwMyPID = ::GetCurrentProcessId();
  125. DWORD dwTestPID;
  126. ::GetWindowThreadProcessId( hWnd, &dwTestPID );
  127. return dwMyPID == dwTestPID;
  128. }
  129. /* !!!!!
  130. HWND GetFocusWnd( CPoint *pPointCaret )
  131. {
  132. HWND hFocusWnd = 0;
  133. CPoint pt;
  134. if( pPointCaret )
  135. *pPointCaret = CPoint(-1, -1);
  136. HWND hForeWnd = ::GetForegroundWindow();
  137. if( !::IsWindow(hForeWnd) )
  138. return 0;
  139. DWORD dwMyThread = ::GetCurrentThreadId();
  140. DWORD dwTargetPID;
  141. DWORD dwTargetThread = ::GetWindowThreadProcessId( hForeWnd, &dwTargetPID );
  142. // get the focus window's caret position
  143. // attach to new focus window
  144. if( ::AttachThreadInput(dwMyThread,dwTargetThread,TRUE) )
  145. {
  146. hFocusWnd = ::GetFocus();
  147. ::GetCaretPos( &pt );
  148. ::ClientToScreen( hFocusWnd, &pt );
  149. // detach
  150. ::AttachThreadInput(dwMyThread,dwTargetThread,FALSE);
  151. }
  152. if( pPointCaret )
  153. *pPointCaret = pt;
  154. return hFocusWnd;
  155. }
  156. */
  157. HWND GetFocusWnd(CPoint *pPointCaret)
  158. {
  159. HWND hWndFocus = NULL;
  160. if (pPointCaret)
  161. *pPointCaret = CPoint(-1, -1);
  162. HWND hWndForground = GetForegroundWindow(); // Get the desktop's foreground window
  163. if (hWndForground != NULL)
  164. {
  165. DWORD ProcID;
  166. DWORD ThreadID = GetWindowThreadProcessId(hWndForground, &ProcID);
  167. // Attach other thread's message queue to our own to ensure GetFocus() is working properly
  168. BOOL ARes = AttachThreadInput(ThreadID, GetCurrentThreadId(), TRUE);
  169. if (ARes)
  170. {
  171. // Get the other thread's focussed window
  172. CWnd *pWnd = CWnd::FromHandle(hWndForground);
  173. if (pWnd)
  174. {
  175. CWnd *pWndFocus = pWnd->GetFocus();
  176. if (pWndFocus)
  177. {
  178. hWndFocus = pWndFocus->m_hWnd;
  179. if (pPointCaret)
  180. {
  181. *pPointCaret = pWndFocus->GetCaretPos();
  182. pWndFocus->ClientToScreen(pPointCaret);
  183. }
  184. }
  185. }
  186. // Detach other thread's message queue from our own again
  187. ARes = AttachThreadInput(ThreadID, GetCurrentThreadId(), FALSE);
  188. }
  189. }
  190. return hWndFocus;
  191. }
  192. /*----------------------------------------------------------------------------*\
  193. Global Memory Helper Functions
  194. \*----------------------------------------------------------------------------*/
  195. // asserts if hDest isn't big enough
  196. void CopyToGlobalHP( HGLOBAL hDest, LPVOID pBuf, ULONG ulBufLen )
  197. {
  198. ASSERT( hDest && pBuf && ulBufLen );
  199. LPVOID pvData = GlobalLock(hDest);
  200. ASSERT( pvData );
  201. ULONG size = GlobalSize(hDest);
  202. ASSERT( size >= ulBufLen ); // assert if hDest isn't big enough
  203. memcpy(pvData, pBuf, ulBufLen);
  204. GlobalUnlock(hDest);
  205. }
  206. void CopyToGlobalHH( HGLOBAL hDest, HGLOBAL hSource, ULONG ulBufLen )
  207. {
  208. ASSERT( hDest && hSource && ulBufLen );
  209. LPVOID pvData = GlobalLock(hSource);
  210. ASSERT( pvData );
  211. ULONG size = GlobalSize(hSource);
  212. ASSERT( size >= ulBufLen ); // assert if hSource isn't big enough
  213. CopyToGlobalHP(hDest, pvData, ulBufLen);
  214. GlobalUnlock(hSource);
  215. }
  216. HGLOBAL NewGlobalP( LPVOID pBuf, UINT nLen )
  217. {
  218. ASSERT( pBuf && nLen );
  219. HGLOBAL hDest = GlobalAlloc( GMEM_MOVEABLE | GMEM_SHARE, nLen );
  220. ASSERT( hDest );
  221. CopyToGlobalHP( hDest, pBuf, nLen );
  222. return hDest;
  223. }
  224. HGLOBAL NewGlobalH( HGLOBAL hSource, UINT nLen )
  225. {
  226. ASSERT( hSource && nLen );
  227. LPVOID pvData = GlobalLock( hSource );
  228. HGLOBAL hDest = NewGlobalP( pvData, nLen );
  229. GlobalUnlock( hSource );
  230. return hDest;
  231. }
  232. int CompareGlobalHP( HGLOBAL hLeft, LPVOID pBuf, ULONG ulBufLen )
  233. {
  234. ASSERT( hLeft && pBuf && ulBufLen );
  235. LPVOID pvData = GlobalLock( hLeft );
  236. ASSERT( pvData );
  237. ASSERT( ulBufLen <= GlobalSize(hLeft) );
  238. int result = memcmp(pvData, pBuf, ulBufLen);
  239. GlobalUnlock( hLeft );
  240. return result;
  241. }
  242. int CompareGlobalHH( HGLOBAL hLeft, HGLOBAL hRight, ULONG ulBufLen )
  243. {
  244. ASSERT( hLeft && hRight && ulBufLen );
  245. ASSERT( ulBufLen <= GlobalSize(hRight) );
  246. LPVOID pvData = GlobalLock(hRight);
  247. ASSERT( pvData );
  248. int result = CompareGlobalHP( hLeft, pvData, ulBufLen );
  249. GlobalUnlock( hLeft );
  250. return result;
  251. }
  252. long DoOptions(CWnd *pParent)
  253. {
  254. //Don't let it open up more than once
  255. if(theApp.m_bShowingOptions)
  256. return FALSE;
  257. theApp.m_bShowingOptions = true;
  258. COptionsSheet Sheet("Copy Pro Options", pParent);
  259. int nRet = Sheet.DoModal();
  260. theApp.m_bShowingOptions = false;
  261. return nRet;
  262. }
  263. //Do not change these these are stored in the database
  264. CLIPFORMAT GetFormatID(LPCSTR cbName)
  265. {
  266. if(strcmp(cbName, "CF_TEXT") == 0)
  267. return CF_TEXT;
  268. else if(strcmp(cbName, "CF_METAFILEPICT") == 0)
  269. return CF_METAFILEPICT;
  270. else if(strcmp(cbName, "CF_SYLK") == 0)
  271. return CF_SYLK;
  272. else if(strcmp(cbName, "CF_DIF") == 0)
  273. return CF_DIF;
  274. else if(strcmp(cbName, "CF_TIFF") == 0)
  275. return CF_TIFF;
  276. else if(strcmp(cbName, "CF_OEMTEXT") == 0)
  277. return CF_OEMTEXT;
  278. else if(strcmp(cbName, "CF_DIB") == 0)
  279. return CF_DIB;
  280. else if(strcmp(cbName, "CF_PALETTE") == 0)
  281. return CF_PALETTE;
  282. else if(strcmp(cbName, "CF_PENDATA") == 0)
  283. return CF_PENDATA;
  284. else if(strcmp(cbName, "CF_RIFF") == 0)
  285. return CF_RIFF;
  286. else if(strcmp(cbName, "CF_WAVE") == 0)
  287. return CF_WAVE;
  288. else if(strcmp(cbName, "CF_UNICODETEXT") == 0)
  289. return CF_UNICODETEXT;
  290. else if(strcmp(cbName, "CF_ENHMETAFILE") == 0)
  291. return CF_ENHMETAFILE;
  292. else if(strcmp(cbName, "CF_HDROP") == 0)
  293. return CF_HDROP;
  294. else if(strcmp(cbName, "CF_LOCALE") == 0)
  295. return CF_LOCALE;
  296. else if(strcmp(cbName, "CF_OWNERDISPLAY") == 0)
  297. return CF_OWNERDISPLAY;
  298. else if(strcmp(cbName, "CF_DSPTEXT") == 0)
  299. return CF_DSPTEXT;
  300. else if(strcmp(cbName, "CF_DSPBITMAP") == 0)
  301. return CF_DSPBITMAP;
  302. else if(strcmp(cbName, "CF_DSPMETAFILEPICT") == 0)
  303. return CF_DSPMETAFILEPICT;
  304. else if(strcmp(cbName, "CF_DSPENHMETAFILE") == 0)
  305. return CF_DSPENHMETAFILE;
  306. return ::RegisterClipboardFormat(cbName);
  307. }
  308. //Do not change these these are stored in the database
  309. CString GetFormatName(CLIPFORMAT cbType)
  310. {
  311. switch(cbType)
  312. {
  313. case CF_TEXT:
  314. return "CF_TEXT";
  315. case CF_BITMAP:
  316. return "CF_BITMAP";
  317. case CF_METAFILEPICT:
  318. return "CF_METAFILEPICT";
  319. case CF_SYLK:
  320. return "CF_SYLK";
  321. case CF_DIF:
  322. return "CF_DIF";
  323. case CF_TIFF:
  324. return "CF_TIFF";
  325. case CF_OEMTEXT:
  326. return "CF_OEMTEXT";
  327. case CF_DIB:
  328. return "CF_DIB";
  329. case CF_PALETTE:
  330. return "CF_PALETTE";
  331. case CF_PENDATA:
  332. return "CF_PENDATA";
  333. case CF_RIFF:
  334. return "CF_RIFF";
  335. case CF_WAVE:
  336. return "CF_WAVE";
  337. case CF_UNICODETEXT:
  338. return "CF_UNICODETEXT";
  339. case CF_ENHMETAFILE:
  340. return "CF_ENHMETAFILE";
  341. case CF_HDROP:
  342. return "CF_HDROP";
  343. case CF_LOCALE:
  344. return "CF_LOCALE";
  345. case CF_OWNERDISPLAY:
  346. return "CF_OWNERDISPLAY";
  347. case CF_DSPTEXT:
  348. return "CF_DSPTEXT";
  349. case CF_DSPBITMAP:
  350. return "CF_DSPBITMAP";
  351. case CF_DSPMETAFILEPICT:
  352. return "CF_DSPMETAFILEPICT";
  353. case CF_DSPENHMETAFILE:
  354. return "CF_DSPENHMETAFILE";
  355. default:
  356. //Not a default type get the name from the clipboard
  357. if (cbType != 0)
  358. {
  359. TCHAR szFormat[256];
  360. GetClipboardFormatName(cbType, szFormat, 256);
  361. return szFormat;
  362. }
  363. break;
  364. }
  365. return "ERROR";
  366. }
  367. CString GetFilePath(CString csFileName)
  368. {
  369. long lSlash = csFileName.ReverseFind('\\');
  370. if(lSlash > -1)
  371. {
  372. csFileName = csFileName.Left(lSlash + 1);
  373. }
  374. return csFileName;
  375. }
  376. /*------------------------------------------------------------------*\
  377. CGetSetOptions
  378. \*------------------------------------------------------------------*/
  379. BOOL CGetSetOptions::m_bUseCtrlNumAccel;
  380. BOOL CGetSetOptions::m_bAllowDuplicates;
  381. BOOL CGetSetOptions::m_bUpdateTimeOnPaste;
  382. BOOL CGetSetOptions::m_bSaveMultiPaste;
  383. BOOL CGetSetOptions::m_bShowPersistent;
  384. CGetSetOptions g_Opt;
  385. CGetSetOptions::CGetSetOptions()
  386. {
  387. m_bUseCtrlNumAccel = GetUseCtrlNumForFirstTenHotKeys();
  388. m_bAllowDuplicates = GetAllowDuplicates();
  389. m_bUpdateTimeOnPaste = GetUpdateTimeOnPaste();
  390. m_bSaveMultiPaste = GetSaveMultiPaste();
  391. m_bShowPersistent = GetShowPersistent();
  392. }
  393. CGetSetOptions::~CGetSetOptions()
  394. {
  395. }
  396. long CGetSetOptions::GetProfileLong(CString csName, long bDefaultValue)
  397. {
  398. HKEY hkKey;
  399. long lResult = RegOpenKeyEx(HKEY_CURRENT_USER, _T(REG_PATH),
  400. NULL, KEY_READ, &hkKey);
  401. if(lResult != ERROR_SUCCESS)
  402. return bDefaultValue;
  403. DWORD buffer;
  404. DWORD len = sizeof(buffer);
  405. DWORD type;
  406. lResult = ::RegQueryValueEx(hkKey, csName, 0, &type, (LPBYTE)&buffer, &len);
  407. RegCloseKey(hkKey);
  408. if(lResult == ERROR_SUCCESS)
  409. return (long)buffer;
  410. return bDefaultValue;
  411. }
  412. CString CGetSetOptions::GetProfileString(CString csName, CString csDefault)
  413. {
  414. HKEY hkKey;
  415. long lResult = RegOpenKeyEx(HKEY_CURRENT_USER, _T(REG_PATH),
  416. NULL, KEY_READ, &hkKey);
  417. char szString[256];
  418. DWORD dwBufLen = 256;
  419. lResult = ::RegQueryValueEx(hkKey , csName, NULL, NULL, (LPBYTE)szString, &dwBufLen);
  420. if(lResult != ERROR_SUCCESS)
  421. return csDefault;
  422. return CString(szString);
  423. }
  424. BOOL CGetSetOptions::SetProfileLong(CString csName, long lValue)
  425. {
  426. HKEY hkKey;
  427. DWORD dWord;
  428. long lResult = RegCreateKeyEx(HKEY_CURRENT_USER, _T(REG_PATH), NULL,
  429. NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
  430. NULL, &hkKey, &dWord);
  431. if(lResult != ERROR_SUCCESS)
  432. return FALSE;
  433. DWORD val = (DWORD)lValue;
  434. lResult = ::RegSetValueEx(hkKey, csName, 0, REG_DWORD, (LPBYTE)&val, sizeof(DWORD));
  435. RegCloseKey(hkKey);
  436. return lResult == ERROR_SUCCESS;
  437. }
  438. BOOL CGetSetOptions::SetProfileString(CString csName, CString csValue)
  439. {
  440. HKEY hkKey;
  441. DWORD dWord;
  442. long lResult = RegCreateKeyEx(HKEY_CURRENT_USER, _T(REG_PATH), NULL,
  443. NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
  444. NULL, &hkKey, &dWord);
  445. if(lResult != ERROR_SUCCESS)
  446. return FALSE;
  447. ::RegSetValueEx(hkKey, csName, NULL, REG_SZ,
  448. (BYTE*)(LPCTSTR)csValue, csValue.GetLength()+sizeof(TCHAR));
  449. RegCloseKey(hkKey);
  450. return lResult == ERROR_SUCCESS;
  451. }
  452. BOOL CGetSetOptions::GetShowIconInSysTray()
  453. {
  454. return GetProfileLong("ShowIconInSystemTray", TRUE);
  455. }
  456. BOOL CGetSetOptions::SetShowIconInSysTray(BOOL bShow)
  457. {
  458. return SetProfileLong("ShowIconInSystemTray", bShow);
  459. }
  460. BOOL CGetSetOptions::SetEnableTransparency(BOOL bCheck)
  461. {
  462. return SetProfileLong("EnableTransparency", bCheck);
  463. }
  464. BOOL CGetSetOptions::GetEnableTransparency()
  465. {
  466. return GetProfileLong("EnableTransparency", FALSE);
  467. }
  468. BOOL CGetSetOptions::SetTransparencyPercent(long lPercent)
  469. {
  470. #ifdef AFTER_98
  471. if(lPercent > OPACITY_MAX)
  472. lPercent = OPACITY_MAX;
  473. if(lPercent < 0)
  474. lPercent = 0;
  475. return SetProfileLong("TransparencyPercent", lPercent);
  476. #endif
  477. return FALSE;
  478. }
  479. long CGetSetOptions::GetTransparencyPercent()
  480. {
  481. #ifdef AFTER_98
  482. long lValue = GetProfileLong("TransparencyPercent", 14);
  483. if(lValue > OPACITY_MAX) lValue = OPACITY_MAX;
  484. if(lValue < 0) lValue = 0;
  485. return lValue;
  486. #endif
  487. return 0;
  488. }
  489. BOOL CGetSetOptions::SetLinesPerRow(long lLines)
  490. {
  491. return SetProfileLong("LinesPerRow", lLines);
  492. }
  493. long CGetSetOptions::GetLinesPerRow()
  494. {
  495. return GetProfileLong("LinesPerRow", 2);
  496. }
  497. BOOL CGetSetOptions::GetRunOnStartUp()
  498. {
  499. HKEY hkRun;
  500. LONG nResult = RegOpenKeyEx(HKEY_CURRENT_USER,
  501. _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
  502. NULL, KEY_READ, &hkRun);
  503. if(nResult != ERROR_SUCCESS)
  504. return FALSE;
  505. nResult = RegQueryValueEx(hkRun, GetAppName(), NULL, NULL, NULL, NULL);
  506. RegCloseKey(hkRun);
  507. return nResult == ERROR_SUCCESS;
  508. }
  509. void CGetSetOptions::SetRunOnStartUp(BOOL bRun)
  510. {
  511. if(bRun == GetRunOnStartUp())
  512. return;
  513. HKEY hkRun;
  514. LONG nResult = RegOpenKeyEx(HKEY_CURRENT_USER,
  515. _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
  516. NULL, KEY_ALL_ACCESS, &hkRun);
  517. if(nResult != ERROR_SUCCESS)
  518. return;
  519. if(bRun)
  520. {
  521. CString sExeName = GetExeFileName();
  522. ::RegSetValueEx(hkRun, GetAppName(), NULL, REG_SZ,
  523. (BYTE*)(LPCTSTR)sExeName, sExeName.GetLength()+sizeof(TCHAR));
  524. }
  525. else
  526. {
  527. ::RegDeleteValue(hkRun, GetAppName());
  528. }
  529. ::RegCloseKey(hkRun);
  530. }
  531. CString CGetSetOptions::GetExeFileName()
  532. {
  533. CString sExeName;
  534. GetModuleFileName(NULL, sExeName.GetBuffer(_MAX_PATH),_MAX_PATH);
  535. sExeName.ReleaseBuffer();
  536. return sExeName;
  537. }
  538. CString CGetSetOptions::GetAppName()
  539. {
  540. return "Ditto";
  541. }
  542. BOOL CGetSetOptions::SetQuickPastePosition(long lPosition)
  543. {
  544. return SetProfileLong("ShowQuickPastePosition", lPosition);
  545. }
  546. long CGetSetOptions::GetQuickPastePosition()
  547. {
  548. return GetProfileLong("ShowQuickPastePosition", POS_AT_PREVIOUS);
  549. }
  550. BOOL CGetSetOptions::SetQuickPasteSize(CSize size)
  551. {
  552. BOOL bRet = SetProfileLong("QuickPasteCX", size.cx);
  553. bRet = SetProfileLong("QuickPasteCY", size.cy);
  554. return bRet;
  555. }
  556. void CGetSetOptions::GetQuickPasteSize(CSize &size)
  557. {
  558. size.cx = GetProfileLong("QuickPasteCX", 300);
  559. size.cy = GetProfileLong("QuickPasteCY", 300);
  560. }
  561. BOOL CGetSetOptions::SetQuickPastePoint(CPoint point)
  562. {
  563. BOOL bRet = SetProfileLong("QuickPasteX", point.x);
  564. bRet = SetProfileLong("QuickPasteY", point.y);
  565. return bRet;
  566. }
  567. void CGetSetOptions::GetQuickPastePoint(CPoint &point)
  568. {
  569. point.x = GetProfileLong("QuickPasteX", 300);
  570. point.y = GetProfileLong("QuickPasteY", 300);
  571. }
  572. long CGetSetOptions::GetCopyGap()
  573. {
  574. return GetProfileLong("CopyGap", 150);
  575. }
  576. BOOL CGetSetOptions::SetDBPath(CString csPath)
  577. {
  578. return SetProfileString("DBPath", csPath);
  579. }
  580. CString CGetSetOptions::GetDBPath(BOOL bDefault/* = TRUE*/)
  581. {
  582. //First check the reg string
  583. CString csDefaultPath = GetProfileString("DBPath", "");
  584. //If there is nothing in the regesty then get the default
  585. //In the users application data in my documents
  586. if(bDefault)
  587. {
  588. if(csDefaultPath.IsEmpty())
  589. csDefaultPath = GetDefaultDBName();
  590. }
  591. return csDefaultPath;
  592. }
  593. void CGetSetOptions::SetCheckForMaxEntries(BOOL bVal)
  594. {
  595. SetProfileLong("CheckForMaxEntries", bVal);
  596. }
  597. BOOL CGetSetOptions::GetCheckForMaxEntries()
  598. {
  599. return GetProfileLong("CheckForMaxEntries", 0);
  600. }
  601. void CGetSetOptions::SetCheckForExpiredEntries(BOOL bVal)
  602. {
  603. SetProfileLong("CheckForExpiredEntries", bVal);
  604. }
  605. BOOL CGetSetOptions::GetCheckForExpiredEntries()
  606. {
  607. return GetProfileLong("CheckForExpiredEntries", 0);
  608. }
  609. void CGetSetOptions::SetMaxEntries(long lVal)
  610. {
  611. SetProfileLong("MaxEntries", lVal);
  612. }
  613. long CGetSetOptions::GetMaxEntries()
  614. {
  615. return GetProfileLong("MaxEntries", 500);
  616. }
  617. void CGetSetOptions::SetExpiredEntries(long lVal)
  618. {
  619. SetProfileLong("ExpiredEntries", lVal);
  620. }
  621. long CGetSetOptions::GetExpiredEntries()
  622. {
  623. return GetProfileLong("ExpiredEntries", 5);
  624. }
  625. void CGetSetOptions::SetTripCopyCount(long lVal)
  626. {
  627. // negative means a relative offset
  628. if(lVal < 0)
  629. lVal = GetTripCopyCount() - lVal; // add the absolute value
  630. if(GetTripDate() == 0)
  631. SetTripDate(-1);
  632. SetProfileLong("TripCopies", lVal);
  633. }
  634. long CGetSetOptions::GetTripCopyCount()
  635. {
  636. return GetProfileLong("TripCopies", 0);
  637. }
  638. void CGetSetOptions::SetTripPasteCount(long lVal)
  639. {
  640. // negative means a relative offset
  641. if(lVal < 0)
  642. lVal = GetTripPasteCount() - lVal; // add the absolute value
  643. if(GetTripDate() == 0)
  644. SetTripDate(-1);
  645. SetProfileLong("TripPastes", lVal);
  646. }
  647. long CGetSetOptions::GetTripPasteCount()
  648. {
  649. return GetProfileLong("TripPastes", 0);
  650. }
  651. void CGetSetOptions::SetTripDate(long lDate)
  652. {
  653. if(lDate == -1)
  654. lDate = (long)CTime::GetCurrentTime().GetTime();
  655. SetProfileLong("TripDate", lDate);
  656. }
  657. long CGetSetOptions::GetTripDate()
  658. {
  659. return GetProfileLong("TripDate", 0);
  660. }
  661. void CGetSetOptions::SetTotalCopyCount(long lVal)
  662. {
  663. // negative means a relative offset
  664. if(lVal < 0)
  665. lVal = GetTotalCopyCount() - lVal; // add the absolute value
  666. if(GetTotalDate() == 0)
  667. SetTotalDate(-1);
  668. SetProfileLong("TotalCopies", lVal);
  669. }
  670. long CGetSetOptions::GetTotalCopyCount()
  671. {
  672. return GetProfileLong("TotalCopies", 0);
  673. }
  674. void CGetSetOptions::SetTotalPasteCount(long lVal)
  675. {
  676. // negative means a relative offset
  677. if(lVal < 0)
  678. lVal = GetTotalPasteCount() - lVal; // add the absolute value
  679. if(GetTotalDate() == 0)
  680. SetTotalDate(-1);
  681. SetProfileLong("TotalPastes", lVal);
  682. }
  683. long CGetSetOptions::GetTotalPasteCount()
  684. {
  685. return GetProfileLong("TotalPastes", 0);
  686. }
  687. void CGetSetOptions::SetTotalDate(long lDate)
  688. {
  689. if(lDate == -1)
  690. lDate = (long)CTime::GetCurrentTime().GetTime();
  691. SetProfileLong("TotalDate", lDate);
  692. }
  693. long CGetSetOptions::GetTotalDate()
  694. {
  695. return GetProfileLong("TotalDate", 0);
  696. }
  697. void CGetSetOptions::SetCompactAndRepairOnExit(BOOL bVal)
  698. {
  699. SetProfileLong("CompactAndRepairOnExit", bVal);
  700. }
  701. BOOL CGetSetOptions::GetCompactAndRepairOnExit()
  702. {
  703. return GetProfileLong("CompactAndRepairOnExit", 0);
  704. }
  705. // the implementations for the following functions were moved out-of-line.
  706. // when they were declared inline, the compiler failed to notice when
  707. // these functions were changed (the linker used an old compiled version)
  708. // (maybe because they are also static?)
  709. CString CGetSetOptions::GetUpdateFilePath() { return GetProfileString("UpdateFilePath", ""); }
  710. BOOL CGetSetOptions::SetUpdateFilePath(CString cs) { return SetProfileString("UpdateFilePath", cs); }
  711. CString CGetSetOptions::GetUpdateInstallPath() { return GetProfileString("UpdateInstallPath", ""); }
  712. BOOL CGetSetOptions::SetUpdateInstallPath(CString cs) { return SetProfileString("UpdateInstallPath", cs); }
  713. long CGetSetOptions::GetLastUpdate() { return GetProfileLong("LastUpdateDay", 0); }
  714. long CGetSetOptions::SetLastUpdate(long lValue) { return SetProfileLong("LastUpdateDay", lValue); }
  715. BOOL CGetSetOptions::GetCheckForUpdates() { return GetProfileLong("CheckForUpdates", TRUE); }
  716. BOOL CGetSetOptions::SetCheckForUpdates(BOOL bCheck) { return SetProfileLong("CheckForUpdates", bCheck); }
  717. void CGetSetOptions::SetUseCtrlNumForFirstTenHotKeys(BOOL bVal) { SetProfileLong("UseCtrlNumForFirstTenHotKeys", bVal); m_bUseCtrlNumAccel = bVal; }
  718. BOOL CGetSetOptions::GetUseCtrlNumForFirstTenHotKeys() { return GetProfileLong("UseCtrlNumForFirstTenHotKeys", 0); }
  719. void CGetSetOptions::SetAllowDuplicates(BOOL bVal) { SetProfileLong("AllowDuplicates", bVal); m_bAllowDuplicates = bVal; }
  720. BOOL CGetSetOptions::GetAllowDuplicates() { return GetProfileLong("AllowDuplicates", 0); }
  721. void CGetSetOptions::SetUpdateTimeOnPaste(BOOL bVal) { SetProfileLong("UpdateTimeOnPaste", bVal); m_bUpdateTimeOnPaste = bVal; }
  722. BOOL CGetSetOptions::GetUpdateTimeOnPaste() { return GetProfileLong("UpdateTimeOnPaste", 0); }
  723. void CGetSetOptions::SetSaveMultiPaste(BOOL bVal) { SetProfileLong("SaveMultiPaste", bVal); m_bSaveMultiPaste = bVal; }
  724. BOOL CGetSetOptions::GetSaveMultiPaste() { return GetProfileLong("SaveMultiPaste", 0); }
  725. void CGetSetOptions::SetShowPersistent(BOOL bVal) { SetProfileLong("ShowPersistent", bVal); m_bShowPersistent = bVal; }
  726. BOOL CGetSetOptions::GetShowPersistent() { return GetProfileLong("ShowPersistent", 0); }
  727. void CGetSetOptions::SetShowTextForFirstTenHotKeys(BOOL bVal) { SetProfileLong("ShowTextForFirstTenHotKeys", bVal); }
  728. BOOL CGetSetOptions::GetShowTextForFirstTenHotKeys() { return GetProfileLong("ShowTextForFirstTenHotKeys", TRUE); }
  729. void CGetSetOptions::SetMainHWND(long lhWnd) { SetProfileLong("MainhWnd", lhWnd); }
  730. BOOL CGetSetOptions::GetMainHWND() { return GetProfileLong("MainhWnd", 0); }
  731. /*------------------------------------------------------------------*\
  732. CHotKey - a single system-wide hotkey
  733. \*------------------------------------------------------------------*/
  734. CHotKey::CHotKey( CString name, DWORD defKey ) : m_Name(name), m_bIsRegistered(false)
  735. {
  736. m_Atom = ::GlobalAddAtom( m_Name );
  737. ASSERT( m_Atom );
  738. m_Key = (DWORD) g_Opt.GetProfileLong( m_Name, (long) defKey );
  739. g_HotKeys.Add( this );
  740. }
  741. CHotKey::~CHotKey()
  742. {
  743. Unregister();
  744. }
  745. void CHotKey::SetKey( DWORD key, bool bSave )
  746. {
  747. if( m_Key == key )
  748. return;
  749. if( m_bIsRegistered )
  750. Unregister();
  751. m_Key = key;
  752. if( bSave )
  753. SaveKey();
  754. }
  755. void CHotKey::LoadKey()
  756. {
  757. SetKey( (DWORD) g_Opt.GetProfileLong( m_Name, 0 ) );
  758. }
  759. bool CHotKey::SaveKey()
  760. {
  761. return g_Opt.SetProfileLong( m_Name, (long) m_Key ) != FALSE;
  762. }
  763. // CString GetKeyAsText();
  764. // void SetKeyFromText( CString text );
  765. BOOL CHotKey::ValidateHotKey(DWORD dwHotKey)
  766. {
  767. ATOM id = ::GlobalAddAtom("HK_VALIDATE");
  768. BOOL bResult = ::RegisterHotKey( g_HotKeys.m_hWnd,
  769. id,
  770. GetModifier(dwHotKey),
  771. LOBYTE(dwHotKey) );
  772. if(bResult)
  773. ::UnregisterHotKey(g_HotKeys.m_hWnd, id);
  774. ::GlobalDeleteAtom(id);
  775. return bResult;
  776. }
  777. UINT CHotKey::GetModifier(DWORD dwHotKey)
  778. {
  779. UINT uMod = 0;
  780. if( HIBYTE(dwHotKey) & HOTKEYF_SHIFT ) uMod |= MOD_SHIFT;
  781. if( HIBYTE(dwHotKey) & HOTKEYF_CONTROL ) uMod |= MOD_CONTROL;
  782. if( HIBYTE(dwHotKey) & HOTKEYF_ALT ) uMod |= MOD_ALT;
  783. if( HIBYTE(dwHotKey) & HOTKEYF_EXT ) uMod |= MOD_WIN;
  784. return uMod;
  785. }
  786. bool CHotKey::Register()
  787. {
  788. if( m_Key )
  789. {
  790. ASSERT( g_HotKeys.m_hWnd );
  791. m_bIsRegistered = ::RegisterHotKey( g_HotKeys.m_hWnd,
  792. m_Atom,
  793. GetModifier(),
  794. LOBYTE(m_Key) ) == TRUE;
  795. }
  796. return m_bIsRegistered;
  797. }
  798. bool CHotKey::Unregister()
  799. {
  800. if( !m_bIsRegistered )
  801. return true;
  802. ASSERT(g_HotKeys.m_hWnd);
  803. if( ::UnregisterHotKey( g_HotKeys.m_hWnd, m_Atom ) )
  804. {
  805. m_bIsRegistered = false;
  806. return true;
  807. }
  808. else
  809. {
  810. LOG(FUNC "FAILED!");
  811. ASSERT(0);
  812. }
  813. return false;
  814. }
  815. /*------------------------------------------------------------------*\
  816. CHotKeys - Manages system-wide hotkeys
  817. \*------------------------------------------------------------------*/
  818. CHotKeys g_HotKeys;
  819. CHotKeys::CHotKeys() : m_hWnd(NULL) {}
  820. CHotKeys::~CHotKeys()
  821. {
  822. CHotKey* pHotKey;
  823. int count = GetCount();
  824. for( int i=0; i < count; i++ )
  825. {
  826. pHotKey = GetAt(i);
  827. if( pHotKey )
  828. delete pHotKey;
  829. }
  830. }
  831. int CHotKeys::Find( CHotKey* pHotKey )
  832. {
  833. int count = GetCount();
  834. for( int i=0; i < count; i++ )
  835. {
  836. if( pHotKey == GetAt(i) )
  837. return i;
  838. }
  839. return -1;
  840. }
  841. bool CHotKeys::Remove( CHotKey* pHotKey )
  842. {
  843. int i = Find(pHotKey);
  844. if( i >= 0 )
  845. {
  846. RemoveAt(i);
  847. return true;
  848. }
  849. return false;
  850. }
  851. void CHotKeys::LoadAllKeys()
  852. {
  853. int count = GetCount();
  854. for( int i=0; i < count; i++ )
  855. GetAt(i)->LoadKey();
  856. }
  857. void CHotKeys::SaveAllKeys()
  858. {
  859. int count = GetCount();
  860. for( int i=0; i < count; i++ )
  861. GetAt(i)->SaveKey();
  862. }
  863. void CHotKeys::RegisterAll( bool bMsgOnError )
  864. {
  865. CString str;
  866. CHotKey* pHotKey;
  867. int count = GetCount();
  868. for( int i=0; i < count; i++ )
  869. {
  870. pHotKey = GetAt(i);
  871. if( !pHotKey->Register() )
  872. {
  873. str = "Error Registering ";
  874. str += pHotKey->GetName();
  875. LOG( str );
  876. if( bMsgOnError )
  877. AfxMessageBox(str);
  878. }
  879. }
  880. }
  881. void CHotKeys::UnregisterAll( bool bMsgOnError )
  882. {
  883. CString str;
  884. CHotKey* pHotKey;
  885. int count = GetCount();
  886. for( int i=0; i < count; i++ )
  887. {
  888. pHotKey = GetAt(i);
  889. if( !pHotKey->Unregister() )
  890. {
  891. str = "Error Unregistering ";
  892. str += pHotKey->GetName();
  893. LOG( str );
  894. if( bMsgOnError )
  895. AfxMessageBox(str);
  896. }
  897. }
  898. }
  899. void CHotKeys::GetKeys( ARRAY& keys )
  900. {
  901. int count = GetCount();
  902. keys.SetSize( count );
  903. for( int i=0; i < count; i++ )
  904. keys[i] = GetAt(i)->GetKey();
  905. }
  906. // caution! this alters hotkeys based upon corresponding indexes
  907. void CHotKeys::SetKeys( ARRAY& keys, bool bSave )
  908. {
  909. int count = GetCount();
  910. ASSERT( count == keys.GetCount() );
  911. for( int i=0; i < count; i++ )
  912. GetAt(i)->SetKey( keys[i], bSave );
  913. }
  914. bool CHotKeys::FindFirstConflict( ARRAY& keys, int* pX, int* pY )
  915. {
  916. bool bConflict = false;
  917. int i, j;
  918. int count = keys.GetCount();
  919. DWORD key;
  920. for( i=0; i < count && !bConflict; i++ )
  921. {
  922. key = keys.GetAt(i);
  923. // only check valid keys
  924. if( key == 0 )
  925. continue;
  926. // scan the array for a duplicate
  927. for( j=i+1; j < count; j++ )
  928. {
  929. if( keys.GetAt(j) == key )
  930. {
  931. bConflict = true;
  932. break;
  933. }
  934. }
  935. }
  936. if( bConflict )
  937. {
  938. if( pX )
  939. *pX = i;
  940. if( pY )
  941. *pY = j;
  942. }
  943. return bConflict;
  944. }
  945. // if true, pX and pY (if valid) are set to the indexes of the conflicting hotkeys.
  946. bool CHotKeys::FindFirstConflict( int* pX, int* pY )
  947. {
  948. ARRAY keys;
  949. GetKeys( keys );
  950. return FindFirstConflict( keys, pX, pY );
  951. }