Misc.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  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( AfxIsValidString( 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. HWND GetFocusWnd(CPoint *pPointCaret)
  130. {
  131. HWND hWndFocus = NULL;
  132. if (pPointCaret)
  133. *pPointCaret = CPoint(-1, -1);
  134. HWND hWndForground = GetForegroundWindow(); // Get the desktop's foreground window
  135. if (hWndForground != NULL)
  136. {
  137. DWORD ProcID;
  138. DWORD ThreadID = GetWindowThreadProcessId(hWndForground, &ProcID);
  139. // Attach other thread's message queue to our own to ensure GetFocus() is working properly
  140. BOOL ARes = AttachThreadInput(ThreadID, GetCurrentThreadId(), TRUE);
  141. if (ARes)
  142. {
  143. // Get the other thread's focussed window
  144. CWnd *pWnd = CWnd::FromHandle(hWndForground);
  145. if (pWnd)
  146. {
  147. CWnd *pWndFocus = pWnd->GetFocus();
  148. if (pWndFocus)
  149. {
  150. hWndFocus = pWndFocus->m_hWnd;
  151. if (pPointCaret)
  152. {
  153. *pPointCaret = pWndFocus->GetCaretPos();
  154. pWndFocus->ClientToScreen(pPointCaret);
  155. }
  156. }
  157. }
  158. // Detach other thread's message queue from our own again
  159. ARes = AttachThreadInput(ThreadID, GetCurrentThreadId(), FALSE);
  160. }
  161. }
  162. return hWndFocus;
  163. }
  164. /*----------------------------------------------------------------------------*\
  165. Global Memory Helper Functions
  166. \*----------------------------------------------------------------------------*/
  167. // make sure the given HGLOBAL is valid.
  168. BOOL IsValid( HGLOBAL hGlobal )
  169. {
  170. void* pvData = ::GlobalLock( hGlobal );
  171. ::GlobalUnlock( hGlobal );
  172. return ( pvData != NULL );
  173. }
  174. // asserts if hDest isn't big enough
  175. void CopyToGlobalHP( HGLOBAL hDest, LPVOID pBuf, ULONG ulBufLen )
  176. {
  177. ASSERT( hDest && pBuf && ulBufLen );
  178. LPVOID pvData = GlobalLock(hDest);
  179. ASSERT( pvData );
  180. ULONG size = GlobalSize(hDest);
  181. ASSERT( size >= ulBufLen ); // assert if hDest isn't big enough
  182. memcpy(pvData, pBuf, ulBufLen);
  183. GlobalUnlock(hDest);
  184. }
  185. void CopyToGlobalHH( HGLOBAL hDest, HGLOBAL hSource, ULONG ulBufLen )
  186. {
  187. ASSERT( hDest && hSource && ulBufLen );
  188. LPVOID pvData = GlobalLock(hSource);
  189. ASSERT( pvData );
  190. ULONG size = GlobalSize(hSource);
  191. ASSERT( size >= ulBufLen ); // assert if hSource isn't big enough
  192. CopyToGlobalHP(hDest, pvData, ulBufLen);
  193. GlobalUnlock(hSource);
  194. }
  195. HGLOBAL NewGlobalP( LPVOID pBuf, UINT nLen )
  196. {
  197. ASSERT( pBuf && nLen );
  198. HGLOBAL hDest = GlobalAlloc( GMEM_MOVEABLE | GMEM_SHARE, nLen );
  199. ASSERT( hDest );
  200. CopyToGlobalHP( hDest, pBuf, nLen );
  201. return hDest;
  202. }
  203. HGLOBAL NewGlobalH( HGLOBAL hSource, UINT nLen )
  204. {
  205. ASSERT( hSource && nLen );
  206. LPVOID pvData = GlobalLock( hSource );
  207. HGLOBAL hDest = NewGlobalP( pvData, nLen );
  208. GlobalUnlock( hSource );
  209. return hDest;
  210. }
  211. int CompareGlobalHP( HGLOBAL hLeft, LPVOID pBuf, ULONG ulBufLen )
  212. {
  213. ASSERT( hLeft && pBuf && ulBufLen );
  214. LPVOID pvData = GlobalLock( hLeft );
  215. ASSERT( pvData );
  216. ASSERT( ulBufLen <= GlobalSize(hLeft) );
  217. int result = memcmp(pvData, pBuf, ulBufLen);
  218. GlobalUnlock( hLeft );
  219. return result;
  220. }
  221. int CompareGlobalHH( HGLOBAL hLeft, HGLOBAL hRight, ULONG ulBufLen )
  222. {
  223. ASSERT( hLeft && hRight && ulBufLen );
  224. ASSERT( ulBufLen <= GlobalSize(hRight) );
  225. LPVOID pvData = GlobalLock(hRight);
  226. ASSERT( pvData );
  227. int result = CompareGlobalHP( hLeft, pvData, ulBufLen );
  228. GlobalUnlock( hLeft );
  229. return result;
  230. }
  231. long DoOptions(CWnd *pParent)
  232. {
  233. //Don't let it open up more than once
  234. if(theApp.m_bShowingOptions)
  235. return FALSE;
  236. theApp.m_bShowingOptions = true;
  237. COptionsSheet Sheet("Copy Pro Options", pParent);
  238. int nRet = Sheet.DoModal();
  239. theApp.m_bShowingOptions = false;
  240. return nRet;
  241. }
  242. //Do not change these these are stored in the database
  243. CLIPFORMAT GetFormatID(LPCSTR cbName)
  244. {
  245. if(strcmp(cbName, "CF_TEXT") == 0)
  246. return CF_TEXT;
  247. else if(strcmp(cbName, "CF_METAFILEPICT") == 0)
  248. return CF_METAFILEPICT;
  249. else if(strcmp(cbName, "CF_SYLK") == 0)
  250. return CF_SYLK;
  251. else if(strcmp(cbName, "CF_DIF") == 0)
  252. return CF_DIF;
  253. else if(strcmp(cbName, "CF_TIFF") == 0)
  254. return CF_TIFF;
  255. else if(strcmp(cbName, "CF_OEMTEXT") == 0)
  256. return CF_OEMTEXT;
  257. else if(strcmp(cbName, "CF_DIB") == 0)
  258. return CF_DIB;
  259. else if(strcmp(cbName, "CF_PALETTE") == 0)
  260. return CF_PALETTE;
  261. else if(strcmp(cbName, "CF_PENDATA") == 0)
  262. return CF_PENDATA;
  263. else if(strcmp(cbName, "CF_RIFF") == 0)
  264. return CF_RIFF;
  265. else if(strcmp(cbName, "CF_WAVE") == 0)
  266. return CF_WAVE;
  267. else if(strcmp(cbName, "CF_UNICODETEXT") == 0)
  268. return CF_UNICODETEXT;
  269. else if(strcmp(cbName, "CF_ENHMETAFILE") == 0)
  270. return CF_ENHMETAFILE;
  271. else if(strcmp(cbName, "CF_HDROP") == 0)
  272. return CF_HDROP;
  273. else if(strcmp(cbName, "CF_LOCALE") == 0)
  274. return CF_LOCALE;
  275. else if(strcmp(cbName, "CF_OWNERDISPLAY") == 0)
  276. return CF_OWNERDISPLAY;
  277. else if(strcmp(cbName, "CF_DSPTEXT") == 0)
  278. return CF_DSPTEXT;
  279. else if(strcmp(cbName, "CF_DSPBITMAP") == 0)
  280. return CF_DSPBITMAP;
  281. else if(strcmp(cbName, "CF_DSPMETAFILEPICT") == 0)
  282. return CF_DSPMETAFILEPICT;
  283. else if(strcmp(cbName, "CF_DSPENHMETAFILE") == 0)
  284. return CF_DSPENHMETAFILE;
  285. return ::RegisterClipboardFormat(cbName);
  286. }
  287. //Do not change these these are stored in the database
  288. CString GetFormatName(CLIPFORMAT cbType)
  289. {
  290. switch(cbType)
  291. {
  292. case CF_TEXT:
  293. return "CF_TEXT";
  294. case CF_BITMAP:
  295. return "CF_BITMAP";
  296. case CF_METAFILEPICT:
  297. return "CF_METAFILEPICT";
  298. case CF_SYLK:
  299. return "CF_SYLK";
  300. case CF_DIF:
  301. return "CF_DIF";
  302. case CF_TIFF:
  303. return "CF_TIFF";
  304. case CF_OEMTEXT:
  305. return "CF_OEMTEXT";
  306. case CF_DIB:
  307. return "CF_DIB";
  308. case CF_PALETTE:
  309. return "CF_PALETTE";
  310. case CF_PENDATA:
  311. return "CF_PENDATA";
  312. case CF_RIFF:
  313. return "CF_RIFF";
  314. case CF_WAVE:
  315. return "CF_WAVE";
  316. case CF_UNICODETEXT:
  317. return "CF_UNICODETEXT";
  318. case CF_ENHMETAFILE:
  319. return "CF_ENHMETAFILE";
  320. case CF_HDROP:
  321. return "CF_HDROP";
  322. case CF_LOCALE:
  323. return "CF_LOCALE";
  324. case CF_OWNERDISPLAY:
  325. return "CF_OWNERDISPLAY";
  326. case CF_DSPTEXT:
  327. return "CF_DSPTEXT";
  328. case CF_DSPBITMAP:
  329. return "CF_DSPBITMAP";
  330. case CF_DSPMETAFILEPICT:
  331. return "CF_DSPMETAFILEPICT";
  332. case CF_DSPENHMETAFILE:
  333. return "CF_DSPENHMETAFILE";
  334. default:
  335. //Not a default type get the name from the clipboard
  336. if (cbType != 0)
  337. {
  338. TCHAR szFormat[256];
  339. GetClipboardFormatName(cbType, szFormat, 256);
  340. return szFormat;
  341. }
  342. break;
  343. }
  344. return "ERROR";
  345. }
  346. CString GetFilePath(CString csFileName)
  347. {
  348. long lSlash = csFileName.ReverseFind('\\');
  349. if(lSlash > -1)
  350. {
  351. csFileName = csFileName.Left(lSlash + 1);
  352. }
  353. return csFileName;
  354. }
  355. /*------------------------------------------------------------------*\
  356. CGetSetOptions
  357. \*------------------------------------------------------------------*/
  358. long CGetSetOptions::m_nLinesPerRow;
  359. BOOL CGetSetOptions::m_bUseCtrlNumAccel;
  360. BOOL CGetSetOptions::m_bAllowDuplicates;
  361. BOOL CGetSetOptions::m_bUpdateTimeOnPaste;
  362. BOOL CGetSetOptions::m_bSaveMultiPaste;
  363. BOOL CGetSetOptions::m_bShowPersistent;
  364. BOOL CGetSetOptions::m_bHistoryStartTop;
  365. long CGetSetOptions::m_bDescTextSize;
  366. BOOL CGetSetOptions::m_bDescShowLeadingWhiteSpace;
  367. BOOL CGetSetOptions::m_bAllwaysShowDescription;
  368. long CGetSetOptions::m_bDoubleClickingOnCaptionDoes;
  369. BOOL CGetSetOptions::m_bPrompForNewGroupName;
  370. BOOL CGetSetOptions::m_bSendPasteOnFirstTenHotKeys;
  371. CGetSetOptions g_Opt;
  372. CGetSetOptions::CGetSetOptions()
  373. {
  374. m_nLinesPerRow = GetLinesPerRow();
  375. m_bUseCtrlNumAccel = GetUseCtrlNumForFirstTenHotKeys();
  376. m_bAllowDuplicates = GetAllowDuplicates();
  377. m_bUpdateTimeOnPaste = GetUpdateTimeOnPaste();
  378. m_bSaveMultiPaste = GetSaveMultiPaste();
  379. m_bShowPersistent = GetShowPersistent();
  380. m_bHistoryStartTop = GetHistoryStartTop();
  381. m_bDescTextSize = GetDescTextSize();
  382. m_bDescShowLeadingWhiteSpace = GetDescShowLeadingWhiteSpace();
  383. m_bAllwaysShowDescription = GetAllwaysShowDescription();
  384. m_bDoubleClickingOnCaptionDoes = GetDoubleClickingOnCaptionDoes();
  385. m_bPrompForNewGroupName = GetPrompForNewGroupName();
  386. m_bSendPasteOnFirstTenHotKeys = GetSendPasteOnFirstTenHotKeys();
  387. }
  388. CGetSetOptions::~CGetSetOptions()
  389. {
  390. }
  391. long CGetSetOptions::GetProfileLong(CString csName, long bDefaultValue)
  392. {
  393. HKEY hkKey;
  394. long lResult = RegOpenKeyEx(HKEY_CURRENT_USER, _T(REG_PATH),
  395. NULL, KEY_READ, &hkKey);
  396. if(lResult != ERROR_SUCCESS)
  397. return bDefaultValue;
  398. DWORD buffer;
  399. DWORD len = sizeof(buffer);
  400. DWORD type;
  401. lResult = ::RegQueryValueEx(hkKey, csName, 0, &type, (LPBYTE)&buffer, &len);
  402. RegCloseKey(hkKey);
  403. if(lResult == ERROR_SUCCESS)
  404. return (long)buffer;
  405. return bDefaultValue;
  406. }
  407. CString CGetSetOptions::GetProfileString(CString csName, CString csDefault)
  408. {
  409. HKEY hkKey;
  410. long lResult = RegOpenKeyEx(HKEY_CURRENT_USER, _T(REG_PATH),
  411. NULL, KEY_READ, &hkKey);
  412. char szString[256];
  413. DWORD dwBufLen = 256;
  414. lResult = ::RegQueryValueEx(hkKey , csName, NULL, NULL, (LPBYTE)szString, &dwBufLen);
  415. if(lResult != ERROR_SUCCESS)
  416. return csDefault;
  417. return CString(szString);
  418. }
  419. BOOL CGetSetOptions::SetProfileLong(CString csName, long lValue)
  420. {
  421. HKEY hkKey;
  422. DWORD dWord;
  423. long lResult = RegCreateKeyEx(HKEY_CURRENT_USER, _T(REG_PATH), NULL,
  424. NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
  425. NULL, &hkKey, &dWord);
  426. if(lResult != ERROR_SUCCESS)
  427. return FALSE;
  428. DWORD val = (DWORD)lValue;
  429. lResult = ::RegSetValueEx(hkKey, csName, 0, REG_DWORD, (LPBYTE)&val, sizeof(DWORD));
  430. RegCloseKey(hkKey);
  431. return lResult == ERROR_SUCCESS;
  432. }
  433. BOOL CGetSetOptions::SetProfileString(CString csName, CString csValue)
  434. {
  435. HKEY hkKey;
  436. DWORD dWord;
  437. long lResult = RegCreateKeyEx(HKEY_CURRENT_USER, _T(REG_PATH), NULL,
  438. NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
  439. NULL, &hkKey, &dWord);
  440. if(lResult != ERROR_SUCCESS)
  441. return FALSE;
  442. ::RegSetValueEx(hkKey, csName, NULL, REG_SZ,
  443. (BYTE*)(LPCTSTR)csValue, csValue.GetLength()+sizeof(TCHAR));
  444. RegCloseKey(hkKey);
  445. return lResult == ERROR_SUCCESS;
  446. }
  447. BOOL CGetSetOptions::GetShowIconInSysTray()
  448. {
  449. return GetProfileLong("ShowIconInSystemTray", TRUE);
  450. }
  451. BOOL CGetSetOptions::SetShowIconInSysTray(BOOL bShow)
  452. {
  453. return SetProfileLong("ShowIconInSystemTray", bShow);
  454. }
  455. BOOL CGetSetOptions::SetEnableTransparency(BOOL bCheck)
  456. {
  457. return SetProfileLong("EnableTransparency", bCheck);
  458. }
  459. BOOL CGetSetOptions::GetEnableTransparency()
  460. {
  461. return GetProfileLong("EnableTransparency", FALSE);
  462. }
  463. BOOL CGetSetOptions::SetTransparencyPercent(long lPercent)
  464. {
  465. #ifdef AFTER_98
  466. if(lPercent > OPACITY_MAX)
  467. lPercent = OPACITY_MAX;
  468. if(lPercent < 0)
  469. lPercent = 0;
  470. return SetProfileLong("TransparencyPercent", lPercent);
  471. #endif
  472. return FALSE;
  473. }
  474. long CGetSetOptions::GetTransparencyPercent()
  475. {
  476. #ifdef AFTER_98
  477. long lValue = GetProfileLong("TransparencyPercent", 14);
  478. if(lValue > OPACITY_MAX) lValue = OPACITY_MAX;
  479. if(lValue < 0) lValue = 0;
  480. return lValue;
  481. #endif
  482. return 0;
  483. }
  484. BOOL CGetSetOptions::SetLinesPerRow(long lLines)
  485. {
  486. m_nLinesPerRow = lLines;
  487. return SetProfileLong("LinesPerRow", lLines);
  488. }
  489. long CGetSetOptions::GetLinesPerRow()
  490. {
  491. return GetProfileLong("LinesPerRow", 2);
  492. }
  493. BOOL CGetSetOptions::GetRunOnStartUp()
  494. {
  495. HKEY hkRun;
  496. LONG nResult = RegOpenKeyEx(HKEY_CURRENT_USER,
  497. _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
  498. NULL, KEY_READ, &hkRun);
  499. if(nResult != ERROR_SUCCESS)
  500. return FALSE;
  501. nResult = RegQueryValueEx(hkRun, GetAppName(), NULL, NULL, NULL, NULL);
  502. RegCloseKey(hkRun);
  503. return nResult == ERROR_SUCCESS;
  504. }
  505. void CGetSetOptions::SetRunOnStartUp(BOOL bRun)
  506. {
  507. if(bRun == GetRunOnStartUp())
  508. return;
  509. HKEY hkRun;
  510. LONG nResult = RegOpenKeyEx(HKEY_CURRENT_USER,
  511. _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
  512. NULL, KEY_ALL_ACCESS, &hkRun);
  513. if(nResult != ERROR_SUCCESS)
  514. return;
  515. if(bRun)
  516. {
  517. CString sExeName = GetExeFileName();
  518. ::RegSetValueEx(hkRun, GetAppName(), NULL, REG_SZ,
  519. (BYTE*)(LPCTSTR)sExeName, sExeName.GetLength()+sizeof(TCHAR));
  520. }
  521. else
  522. {
  523. ::RegDeleteValue(hkRun, GetAppName());
  524. }
  525. ::RegCloseKey(hkRun);
  526. }
  527. CString CGetSetOptions::GetExeFileName()
  528. {
  529. CString sExeName;
  530. GetModuleFileName(NULL, sExeName.GetBuffer(_MAX_PATH),_MAX_PATH);
  531. sExeName.ReleaseBuffer();
  532. return sExeName;
  533. }
  534. CString CGetSetOptions::GetAppName()
  535. {
  536. return "Ditto";
  537. }
  538. BOOL CGetSetOptions::SetQuickPastePosition(long lPosition)
  539. {
  540. return SetProfileLong("ShowQuickPastePosition", lPosition);
  541. }
  542. long CGetSetOptions::GetQuickPastePosition()
  543. {
  544. return GetProfileLong("ShowQuickPastePosition", POS_AT_PREVIOUS);
  545. }
  546. BOOL CGetSetOptions::SetQuickPasteSize(CSize size)
  547. {
  548. BOOL bRet = SetProfileLong("QuickPasteCX", size.cx);
  549. bRet = SetProfileLong("QuickPasteCY", size.cy);
  550. return bRet;
  551. }
  552. void CGetSetOptions::GetQuickPasteSize(CSize &size)
  553. {
  554. size.cx = GetProfileLong("QuickPasteCX", 300);
  555. size.cy = GetProfileLong("QuickPasteCY", 300);
  556. }
  557. BOOL CGetSetOptions::SetQuickPastePoint(CPoint point)
  558. {
  559. BOOL bRet = SetProfileLong("QuickPasteX", point.x);
  560. bRet = SetProfileLong("QuickPasteY", point.y);
  561. return bRet;
  562. }
  563. void CGetSetOptions::GetQuickPastePoint(CPoint &point)
  564. {
  565. point.x = GetProfileLong("QuickPasteX", 300);
  566. point.y = GetProfileLong("QuickPasteY", 300);
  567. }
  568. long CGetSetOptions::GetCopyGap()
  569. {
  570. return GetProfileLong("CopyGap", 150);
  571. }
  572. BOOL CGetSetOptions::SetDBPath(CString csPath)
  573. {
  574. return SetProfileString("DBPath", csPath);
  575. }
  576. CString CGetSetOptions::GetDBPath(BOOL bDefault/* = TRUE*/)
  577. {
  578. //First check the reg string
  579. CString csDefaultPath = GetProfileString("DBPath", "");
  580. //If there is nothing in the regesty then get the default
  581. //In the users application data in my documents
  582. if(bDefault)
  583. {
  584. if(csDefaultPath.IsEmpty())
  585. csDefaultPath = GetDefaultDBName();
  586. }
  587. return csDefaultPath;
  588. }
  589. void CGetSetOptions::SetCheckForMaxEntries(BOOL bVal)
  590. {
  591. SetProfileLong("CheckForMaxEntries", bVal);
  592. }
  593. BOOL CGetSetOptions::GetCheckForMaxEntries()
  594. {
  595. return GetProfileLong("CheckForMaxEntries", 0);
  596. }
  597. void CGetSetOptions::SetCheckForExpiredEntries(BOOL bVal)
  598. {
  599. SetProfileLong("CheckForExpiredEntries", bVal);
  600. }
  601. BOOL CGetSetOptions::GetCheckForExpiredEntries()
  602. {
  603. return GetProfileLong("CheckForExpiredEntries", 0);
  604. }
  605. void CGetSetOptions::SetMaxEntries(long lVal)
  606. {
  607. SetProfileLong("MaxEntries", lVal);
  608. }
  609. long CGetSetOptions::GetMaxEntries()
  610. {
  611. return GetProfileLong("MaxEntries", 500);
  612. }
  613. void CGetSetOptions::SetExpiredEntries(long lVal)
  614. {
  615. SetProfileLong("ExpiredEntries", lVal);
  616. }
  617. long CGetSetOptions::GetExpiredEntries()
  618. {
  619. return GetProfileLong("ExpiredEntries", 5);
  620. }
  621. void CGetSetOptions::SetTripCopyCount(long lVal)
  622. {
  623. // negative means a relative offset
  624. if(lVal < 0)
  625. lVal = GetTripCopyCount() - lVal; // add the absolute value
  626. if(GetTripDate() == 0)
  627. SetTripDate(-1);
  628. SetProfileLong("TripCopies", lVal);
  629. }
  630. long CGetSetOptions::GetTripCopyCount()
  631. {
  632. return GetProfileLong("TripCopies", 0);
  633. }
  634. void CGetSetOptions::SetTripPasteCount(long lVal)
  635. {
  636. // negative means a relative offset
  637. if(lVal < 0)
  638. lVal = GetTripPasteCount() - lVal; // add the absolute value
  639. if(GetTripDate() == 0)
  640. SetTripDate(-1);
  641. SetProfileLong("TripPastes", lVal);
  642. }
  643. long CGetSetOptions::GetTripPasteCount()
  644. {
  645. return GetProfileLong("TripPastes", 0);
  646. }
  647. void CGetSetOptions::SetTripDate(long lDate)
  648. {
  649. if(lDate == -1)
  650. lDate = (long)CTime::GetCurrentTime().GetTime();
  651. SetProfileLong("TripDate", lDate);
  652. }
  653. long CGetSetOptions::GetTripDate()
  654. {
  655. return GetProfileLong("TripDate", 0);
  656. }
  657. void CGetSetOptions::SetTotalCopyCount(long lVal)
  658. {
  659. // negative means a relative offset
  660. if(lVal < 0)
  661. lVal = GetTotalCopyCount() - lVal; // add the absolute value
  662. if(GetTotalDate() == 0)
  663. SetTotalDate(-1);
  664. SetProfileLong("TotalCopies", lVal);
  665. }
  666. long CGetSetOptions::GetTotalCopyCount()
  667. {
  668. return GetProfileLong("TotalCopies", 0);
  669. }
  670. void CGetSetOptions::SetTotalPasteCount(long lVal)
  671. {
  672. // negative means a relative offset
  673. if(lVal < 0)
  674. lVal = GetTotalPasteCount() - lVal; // add the absolute value
  675. if(GetTotalDate() == 0)
  676. SetTotalDate(-1);
  677. SetProfileLong("TotalPastes", lVal);
  678. }
  679. long CGetSetOptions::GetTotalPasteCount()
  680. {
  681. return GetProfileLong("TotalPastes", 0);
  682. }
  683. void CGetSetOptions::SetTotalDate(long lDate)
  684. {
  685. if(lDate == -1)
  686. lDate = (long)CTime::GetCurrentTime().GetTime();
  687. SetProfileLong("TotalDate", lDate);
  688. }
  689. long CGetSetOptions::GetTotalDate()
  690. {
  691. return GetProfileLong("TotalDate", 0);
  692. }
  693. void CGetSetOptions::SetCompactAndRepairOnExit(BOOL bVal)
  694. {
  695. SetProfileLong("CompactAndRepairOnExit", bVal);
  696. }
  697. BOOL CGetSetOptions::GetCompactAndRepairOnExit()
  698. {
  699. return GetProfileLong("CompactAndRepairOnExit", 0);
  700. }
  701. // the implementations for the following functions were moved out-of-line.
  702. // when they were declared inline, the compiler failed to notice when
  703. // these functions were changed (the linker used an old compiled version)
  704. // (maybe because they are also static?)
  705. CString CGetSetOptions::GetUpdateFilePath() { return GetProfileString("UpdateFilePath", ""); }
  706. BOOL CGetSetOptions::SetUpdateFilePath(CString cs) { return SetProfileString("UpdateFilePath", cs); }
  707. CString CGetSetOptions::GetUpdateInstallPath() { return GetProfileString("UpdateInstallPath", ""); }
  708. BOOL CGetSetOptions::SetUpdateInstallPath(CString cs) { return SetProfileString("UpdateInstallPath", cs); }
  709. long CGetSetOptions::GetLastUpdate() { return GetProfileLong("LastUpdateDay", 0); }
  710. long CGetSetOptions::SetLastUpdate(long lValue) { return SetProfileLong("LastUpdateDay", lValue); }
  711. BOOL CGetSetOptions::GetCheckForUpdates() { return GetProfileLong("CheckForUpdates", TRUE); }
  712. BOOL CGetSetOptions::SetCheckForUpdates(BOOL bCheck) { return SetProfileLong("CheckForUpdates", bCheck); }
  713. void CGetSetOptions::SetUseCtrlNumForFirstTenHotKeys(BOOL bVal) { SetProfileLong("UseCtrlNumForFirstTenHotKeys", bVal); m_bUseCtrlNumAccel = bVal; }
  714. BOOL CGetSetOptions::GetUseCtrlNumForFirstTenHotKeys() { return GetProfileLong("UseCtrlNumForFirstTenHotKeys", 0); }
  715. void CGetSetOptions::SetAllowDuplicates(BOOL bVal) { SetProfileLong("AllowDuplicates", bVal); m_bAllowDuplicates = bVal; }
  716. BOOL CGetSetOptions::GetAllowDuplicates() { return GetProfileLong("AllowDuplicates", 0); }
  717. void CGetSetOptions::SetUpdateTimeOnPaste(BOOL bVal) { SetProfileLong("UpdateTimeOnPaste", bVal); m_bUpdateTimeOnPaste = bVal; }
  718. BOOL CGetSetOptions::GetUpdateTimeOnPaste() { return GetProfileLong("UpdateTimeOnPaste", TRUE); }
  719. void CGetSetOptions::SetSaveMultiPaste(BOOL bVal) { SetProfileLong("SaveMultiPaste", bVal); m_bSaveMultiPaste = bVal; }
  720. BOOL CGetSetOptions::GetSaveMultiPaste() { return GetProfileLong("SaveMultiPaste", 0); }
  721. void CGetSetOptions::SetShowPersistent(BOOL bVal) { SetProfileLong("ShowPersistent", bVal); m_bShowPersistent = bVal; }
  722. BOOL CGetSetOptions::GetShowPersistent() { return GetProfileLong("ShowPersistent", 0); }
  723. void CGetSetOptions::SetHistoryStartTop(BOOL bVal) { SetProfileLong("HistoryStartTop", bVal); m_bHistoryStartTop = bVal; }
  724. BOOL CGetSetOptions::GetHistoryStartTop() { return GetProfileLong("HistoryStartTop", TRUE); }
  725. void CGetSetOptions::SetShowTextForFirstTenHotKeys(BOOL bVal) { SetProfileLong("ShowTextForFirstTenHotKeys", bVal); }
  726. BOOL CGetSetOptions::GetShowTextForFirstTenHotKeys() { return GetProfileLong("ShowTextForFirstTenHotKeys", TRUE); }
  727. void CGetSetOptions::SetMainHWND(long lhWnd) { SetProfileLong("MainhWnd", lhWnd); }
  728. BOOL CGetSetOptions::GetMainHWND() { return GetProfileLong("MainhWnd", 0); }
  729. void CGetSetOptions::SetCaptionPos(long lPos) { SetProfileLong("CaptionPos", lPos); }
  730. long CGetSetOptions::GetCaptionPos() { return GetProfileLong("CaptionPos", CAPTION_RIGHT); }
  731. void CGetSetOptions::SetAutoHide(BOOL bAutoHide){ SetProfileLong("AutoHide", bAutoHide); }
  732. BOOL CGetSetOptions::GetAutoHide() { return GetProfileLong("AutoHide", FALSE); }
  733. void CGetSetOptions::SetDescTextSize(long lSize){ SetProfileLong("DescTextSize", lSize); m_bDescTextSize = lSize; }
  734. long CGetSetOptions::GetDescTextSize() { return GetProfileLong("DescTextSize", 500); }
  735. void CGetSetOptions::SetDescShowLeadingWhiteSpace(BOOL bVal){ SetProfileLong("DescShowLeadingWhiteSpace", bVal); m_bDescShowLeadingWhiteSpace = bVal; }
  736. BOOL CGetSetOptions::GetDescShowLeadingWhiteSpace() { return GetProfileLong("DescShowLeadingWhiteSpace", FALSE); }
  737. void CGetSetOptions::SetAllwaysShowDescription(long bShow) { SetProfileLong("AllwaysShowDescription", bShow); m_bAllwaysShowDescription = bShow; }
  738. BOOL CGetSetOptions::GetAllwaysShowDescription() { return GetProfileLong("AllwaysShowDescription", FALSE); }
  739. void CGetSetOptions::SetDoubleClickingOnCaptionDoes(long lOption) { SetProfileLong("DoubleClickingOnCaptionDoes", lOption); m_bDoubleClickingOnCaptionDoes = lOption; }
  740. long CGetSetOptions::GetDoubleClickingOnCaptionDoes() { return GetProfileLong("DoubleClickingOnCaptionDoes", TOGGLES_ALLWAYS_ON_TOP); }
  741. void CGetSetOptions::SetPrompForNewGroupName(BOOL bOption) { SetProfileLong("PrompForNewGroupName", bOption); m_bPrompForNewGroupName = bOption; }
  742. BOOL CGetSetOptions::GetPrompForNewGroupName() { return GetProfileLong("PrompForNewGroupName", TRUE); }
  743. void CGetSetOptions::SetSendPasteOnFirstTenHotKeys(BOOL bOption) { SetProfileLong("SendPasteOnFirstTenHotKeys", bOption); m_bSendPasteOnFirstTenHotKeys = bOption; }
  744. BOOL CGetSetOptions::GetSendPasteOnFirstTenHotKeys() { return GetProfileLong("SendPasteOnFirstTenHotKeys", TRUE); }
  745. /*------------------------------------------------------------------*\
  746. CHotKey - a single system-wide hotkey
  747. \*------------------------------------------------------------------*/
  748. CHotKey::CHotKey( CString name, DWORD defKey, bool bUnregOnShowDitto )
  749. : m_Name(name), m_bIsRegistered(false), m_bUnRegisterOnShowDitto(bUnregOnShowDitto)
  750. {
  751. m_Atom = ::GlobalAddAtom( m_Name );
  752. ASSERT( m_Atom );
  753. m_Key = (DWORD) g_Opt.GetProfileLong( m_Name, (long) defKey );
  754. g_HotKeys.Add( this );
  755. }
  756. CHotKey::~CHotKey()
  757. {
  758. Unregister();
  759. }
  760. void CHotKey::SetKey( DWORD key, bool bSave )
  761. {
  762. if( m_Key == key )
  763. return;
  764. if( m_bIsRegistered )
  765. Unregister();
  766. m_Key = key;
  767. if( bSave )
  768. SaveKey();
  769. }
  770. void CHotKey::LoadKey()
  771. {
  772. SetKey( (DWORD) g_Opt.GetProfileLong( m_Name, 0 ) );
  773. }
  774. bool CHotKey::SaveKey()
  775. {
  776. return g_Opt.SetProfileLong( m_Name, (long) m_Key ) != FALSE;
  777. }
  778. // CString GetKeyAsText();
  779. // void SetKeyFromText( CString text );
  780. BOOL CHotKey::ValidateHotKey(DWORD dwHotKey)
  781. {
  782. ATOM id = ::GlobalAddAtom("HK_VALIDATE");
  783. BOOL bResult = ::RegisterHotKey( g_HotKeys.m_hWnd,
  784. id,
  785. GetModifier(dwHotKey),
  786. LOBYTE(dwHotKey) );
  787. if(bResult)
  788. ::UnregisterHotKey(g_HotKeys.m_hWnd, id);
  789. ::GlobalDeleteAtom(id);
  790. return bResult;
  791. }
  792. UINT CHotKey::GetModifier(DWORD dwHotKey)
  793. {
  794. UINT uMod = 0;
  795. if( HIBYTE(dwHotKey) & HOTKEYF_SHIFT ) uMod |= MOD_SHIFT;
  796. if( HIBYTE(dwHotKey) & HOTKEYF_CONTROL ) uMod |= MOD_CONTROL;
  797. if( HIBYTE(dwHotKey) & HOTKEYF_ALT ) uMod |= MOD_ALT;
  798. if( HIBYTE(dwHotKey) & HOTKEYF_EXT ) uMod |= MOD_WIN;
  799. return uMod;
  800. }
  801. bool CHotKey::Register()
  802. {
  803. if( m_Key )
  804. {
  805. if(m_bIsRegistered == false)
  806. {
  807. ASSERT( g_HotKeys.m_hWnd );
  808. m_bIsRegistered = ::RegisterHotKey( g_HotKeys.m_hWnd,
  809. m_Atom,
  810. GetModifier(),
  811. LOBYTE(m_Key) ) == TRUE;
  812. }
  813. }
  814. else
  815. m_bIsRegistered = true;
  816. return m_bIsRegistered;
  817. }
  818. bool CHotKey::Unregister(bool bOnShowingDitto)
  819. {
  820. if( !m_bIsRegistered )
  821. return true;
  822. if(bOnShowingDitto)
  823. {
  824. if(m_bUnRegisterOnShowDitto == false)
  825. return true;
  826. }
  827. if(m_Key)
  828. {
  829. ASSERT(g_HotKeys.m_hWnd);
  830. if( ::UnregisterHotKey( g_HotKeys.m_hWnd, m_Atom ) )
  831. {
  832. m_bIsRegistered = false;
  833. return true;
  834. }
  835. else
  836. {
  837. LOG("Unregister" "FAILED!");
  838. ASSERT(0);
  839. }
  840. }
  841. else
  842. {
  843. m_bIsRegistered = false;
  844. }
  845. return false;
  846. }
  847. /*------------------------------------------------------------------*\
  848. CHotKeys - Manages system-wide hotkeys
  849. \*------------------------------------------------------------------*/
  850. CHotKeys g_HotKeys;
  851. CHotKeys::CHotKeys() : m_hWnd(NULL) {}
  852. CHotKeys::~CHotKeys()
  853. {
  854. CHotKey* pHotKey;
  855. int count = GetSize();
  856. for( int i=0; i < count; i++ )
  857. {
  858. pHotKey = ElementAt(i);
  859. if( pHotKey )
  860. delete pHotKey;
  861. }
  862. }
  863. int CHotKeys::Find( CHotKey* pHotKey )
  864. {
  865. int count = GetSize();
  866. for( int i=0; i < count; i++ )
  867. {
  868. if( pHotKey == ElementAt(i) )
  869. return i;
  870. }
  871. return -1;
  872. }
  873. bool CHotKeys::Remove( CHotKey* pHotKey )
  874. {
  875. int i = Find(pHotKey);
  876. if( i >= 0 )
  877. {
  878. RemoveAt(i);
  879. return true;
  880. }
  881. return false;
  882. }
  883. void CHotKeys::LoadAllKeys()
  884. {
  885. int count = GetSize();
  886. for( int i=0; i < count; i++ )
  887. ElementAt(i)->LoadKey();
  888. }
  889. void CHotKeys::SaveAllKeys()
  890. {
  891. int count = GetSize();
  892. for( int i=0; i < count; i++ )
  893. ElementAt(i)->SaveKey();
  894. }
  895. void CHotKeys::RegisterAll( bool bMsgOnError )
  896. {
  897. CString str;
  898. CHotKey* pHotKey;
  899. int count = GetSize();
  900. for( int i=0; i < count; i++ )
  901. {
  902. pHotKey = ElementAt(i);
  903. if( !pHotKey->Register() )
  904. {
  905. str = "Error Registering ";
  906. str += pHotKey->GetName();
  907. LOG( str );
  908. if( bMsgOnError )
  909. AfxMessageBox(str);
  910. }
  911. }
  912. }
  913. void CHotKeys::UnregisterAll(bool bMsgOnError, bool bOnShowDitto)
  914. {
  915. CString str;
  916. CHotKey* pHotKey;
  917. int count = GetSize();
  918. for( int i=0; i < count; i++ )
  919. {
  920. pHotKey = ElementAt(i);
  921. if(!pHotKey->Unregister(bOnShowDitto))
  922. {
  923. str = "Error Unregistering ";
  924. str += pHotKey->GetName();
  925. LOG( str );
  926. if( bMsgOnError )
  927. AfxMessageBox(str);
  928. }
  929. }
  930. }
  931. void CHotKeys::GetKeys( ARRAY& keys )
  932. {
  933. int count = GetSize();
  934. keys.SetSize( count );
  935. for( int i=0; i < count; i++ )
  936. keys[i] = ElementAt(i)->GetKey();
  937. }
  938. // caution! this alters hotkeys based upon corresponding indexes
  939. void CHotKeys::SetKeys( ARRAY& keys, bool bSave )
  940. {
  941. int count = GetSize();
  942. ASSERT( count == keys.GetSize() );
  943. for( int i=0; i < count; i++ )
  944. ElementAt(i)->SetKey( keys[i], bSave );
  945. }
  946. bool CHotKeys::FindFirstConflict( ARRAY& keys, int* pX, int* pY )
  947. {
  948. bool bConflict = false;
  949. int i, j;
  950. int count = keys.GetSize();
  951. DWORD key;
  952. for( i=0; i < count && !bConflict; i++ )
  953. {
  954. key = keys.ElementAt(i);
  955. // only check valid keys
  956. if( key == 0 )
  957. continue;
  958. // scan the array for a duplicate
  959. for( j=i+1; j < count; j++ )
  960. {
  961. if( keys.ElementAt(j) == key )
  962. {
  963. bConflict = true;
  964. break;
  965. }
  966. }
  967. }
  968. if( bConflict )
  969. {
  970. if( pX )
  971. *pX = i-1;
  972. if( pY )
  973. *pY = j;
  974. }
  975. return bConflict;
  976. }
  977. // if true, pX and pY (if valid) are set to the indexes of the conflicting hotkeys.
  978. bool CHotKeys::FindFirstConflict( int* pX, int* pY )
  979. {
  980. ARRAY keys;
  981. GetKeys( keys );
  982. return FindFirstConflict( keys, pX, pY );
  983. }
  984. /****************************************************************************************************
  985. BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
  986. ***************************************************************************************************/
  987. typedef struct
  988. {
  989. long lFlags; // Flags
  990. LPRECT pVirtualRect; // Ptr to rect that receives the results, or the src of the monitor search method
  991. int iMonitor; // Ndx to the mointor to look at, -1 for all, -or- result of the monitor search method
  992. int nMonitorCount; // Total number of monitors found, -1 for monitor search method
  993. } MONITOR_ENUM_PARAM;
  994. #define MONITOR_SEARCH_METOHD 0x00000001
  995. BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
  996. {
  997. // Typecast param
  998. MONITOR_ENUM_PARAM* pParam = (MONITOR_ENUM_PARAM*)dwData;
  999. if(pParam)
  1000. {
  1001. // If a dest rect was passed
  1002. if(pParam->pVirtualRect)
  1003. {
  1004. // If MONITOR_SEARCH_METOHD then we are being asked for the index of the monitor
  1005. // that the rect falls inside of
  1006. if(pParam->lFlags & MONITOR_SEARCH_METOHD)
  1007. {
  1008. if( (pParam->pVirtualRect->right < lprcMonitor->left) ||
  1009. (pParam->pVirtualRect->left > lprcMonitor->right) ||
  1010. (pParam->pVirtualRect->bottom < lprcMonitor->top) ||
  1011. (pParam->pVirtualRect->top > lprcMonitor->bottom))
  1012. {
  1013. // Nothing
  1014. }
  1015. else
  1016. {
  1017. // This is the one
  1018. pParam->iMonitor = pParam->nMonitorCount;
  1019. // Stop the enumeration
  1020. return FALSE;
  1021. }
  1022. }
  1023. else
  1024. {
  1025. if(pParam->iMonitor == pParam->nMonitorCount)
  1026. {
  1027. *pParam->pVirtualRect = *lprcMonitor;
  1028. }
  1029. else
  1030. if(pParam->iMonitor == -1)
  1031. {
  1032. pParam->pVirtualRect->left = min(pParam->pVirtualRect->left, lprcMonitor->left);
  1033. pParam->pVirtualRect->top = min(pParam->pVirtualRect->top, lprcMonitor->top);
  1034. pParam->pVirtualRect->right = max(pParam->pVirtualRect->right, lprcMonitor->right);
  1035. pParam->pVirtualRect->bottom = max(pParam->pVirtualRect->bottom, lprcMonitor->bottom);
  1036. }
  1037. }
  1038. }
  1039. // Up the count if necessary
  1040. pParam->nMonitorCount++;
  1041. }
  1042. return TRUE;
  1043. }
  1044. int GetScreenWidth(void)
  1045. {
  1046. OSVERSIONINFO OS_Version_Info;
  1047. DWORD dwPlatform = 0;
  1048. if(GetVersionEx(&OS_Version_Info) != 0)
  1049. {
  1050. dwPlatform = OS_Version_Info.dwPlatformId;
  1051. }
  1052. if(dwPlatform == VER_PLATFORM_WIN32_NT)
  1053. {
  1054. int width, height;
  1055. width = GetSystemMetrics(SM_CXSCREEN);
  1056. height = GetSystemMetrics(SM_CYSCREEN);
  1057. switch(width)
  1058. {
  1059. default:
  1060. case 640:
  1061. case 800:
  1062. case 1024:
  1063. return(width);
  1064. case 1280:
  1065. if(height == 480)
  1066. {
  1067. return(width / 2);
  1068. }
  1069. return(width);
  1070. case 1600:
  1071. if(height == 600)
  1072. {
  1073. return(width / 2);
  1074. }
  1075. return(width);
  1076. case 2048:
  1077. if(height == 768)
  1078. {
  1079. return(width / 2);
  1080. }
  1081. return(width);
  1082. }
  1083. }
  1084. else
  1085. {
  1086. return(GetSystemMetrics(SM_CXSCREEN));
  1087. }
  1088. }
  1089. int GetScreenHeight(void)
  1090. {
  1091. OSVERSIONINFO OS_Version_Info;
  1092. DWORD dwPlatform = 0;
  1093. if(GetVersionEx(&OS_Version_Info) != 0)
  1094. {
  1095. dwPlatform = OS_Version_Info.dwPlatformId;
  1096. }
  1097. if(dwPlatform == VER_PLATFORM_WIN32_NT)
  1098. {
  1099. int width, height;
  1100. width = GetSystemMetrics(SM_CXSCREEN);
  1101. height = GetSystemMetrics(SM_CYSCREEN);
  1102. switch(height)
  1103. {
  1104. default:
  1105. case 480:
  1106. case 600:
  1107. case 768:
  1108. return(height);
  1109. case 960:
  1110. if(width == 640)
  1111. {
  1112. return(height / 2);
  1113. }
  1114. return(height);
  1115. case 1200:
  1116. if(width == 800)
  1117. {
  1118. return(height / 2);
  1119. }
  1120. return(height);
  1121. case 1536:
  1122. if(width == 1024)
  1123. {
  1124. return(height / 2);
  1125. }
  1126. return(height);
  1127. }
  1128. }
  1129. else
  1130. {
  1131. return(GetSystemMetrics(SM_CYSCREEN));
  1132. }
  1133. }
  1134. int GetMonitorFromRect(LPRECT lpMonitorRect)
  1135. {
  1136. // Build up the param
  1137. MONITOR_ENUM_PARAM EnumParam;
  1138. ZeroMemory(&EnumParam, sizeof(EnumParam));
  1139. EnumParam.lFlags = MONITOR_SEARCH_METOHD;
  1140. EnumParam.pVirtualRect = lpMonitorRect;
  1141. // Enum Displays
  1142. EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (long)&EnumParam);
  1143. // Return the result
  1144. return EnumParam.iMonitor;
  1145. }
  1146. void GetMonitorRect(int iMonitor, LPRECT lpDestRect)
  1147. {
  1148. // Build up the param
  1149. MONITOR_ENUM_PARAM EnumParam;
  1150. ZeroMemory(&EnumParam, sizeof(EnumParam));
  1151. EnumParam.iMonitor = iMonitor;
  1152. EnumParam.pVirtualRect = lpDestRect;
  1153. // Zero out dest rect
  1154. lpDestRect->bottom = lpDestRect->left = lpDestRect->right = lpDestRect->top = 0;
  1155. // Enum Displays
  1156. EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (long)&EnumParam);
  1157. // If not successful, default to the screen dimentions
  1158. if(lpDestRect->right == 0 || lpDestRect->bottom == 0)
  1159. {
  1160. lpDestRect->right = GetScreenWidth();
  1161. lpDestRect->bottom = GetScreenHeight();
  1162. }
  1163. }
  1164. /*------------------------------------------------------------------*\
  1165. CAccel - an Accelerator (in-app hotkey)
  1166. - the win32 CreateAcceleratorTable using ACCEL was insufficient
  1167. because it only allowed a WORD for the cmd associated with it.
  1168. \*------------------------------------------------------------------*/
  1169. /*------------------------------------------------------------------*\
  1170. CAccels - Manages a set of CAccel
  1171. \*------------------------------------------------------------------*/
  1172. int CompareAccel( const void* pLeft, const void* pRight )
  1173. {
  1174. WORD w;
  1175. int l,r;
  1176. // swap bytes: place the VirtualKey in the MSB and the modifier in the LSB
  1177. // so that Accels based upon the same vkey are grouped together.
  1178. // this is required by our use of m_Index
  1179. // alternatively, we could store them this way in CAccel.
  1180. w = (WORD) ((CAccel*)pLeft)->Key;
  1181. l = (ACCEL_VKEY(w) << 8) | ACCEL_MOD(w);
  1182. w = (WORD) ((CAccel*)pRight)->Key;
  1183. r = (ACCEL_VKEY(w) << 8) | ACCEL_MOD(w);
  1184. return l - r;
  1185. }
  1186. CAccels::CAccels()
  1187. {}
  1188. void CAccels::AddAccel( CAccel& a )
  1189. {
  1190. m_Map.SetAt(a.Key, a.Cmd);
  1191. }
  1192. bool CAccels::OnMsg( MSG* pMsg, DWORD &dID)
  1193. {
  1194. // bit 30 (0x40000000) is 1 if this is NOT the first msg of the key
  1195. // i.e. auto-repeat may cause multiple msgs of the same key
  1196. if( (pMsg->lParam & 0x40000000) ||
  1197. (pMsg->message != WM_KEYDOWN &&
  1198. pMsg->message != WM_SYSKEYDOWN) )
  1199. {
  1200. return NULL;
  1201. }
  1202. if( !pMsg || m_Map.GetCount() <= 0 )
  1203. return NULL;
  1204. BYTE vkey = LOBYTE(pMsg->wParam);
  1205. BYTE mod = GetKeyStateModifiers();
  1206. DWORD key = ACCEL_MAKEKEY( vkey, mod );
  1207. if(m_Map.Lookup(key, dID))
  1208. return true;;
  1209. return false;
  1210. }
  1211. BYTE GetKeyStateModifiers()
  1212. {
  1213. BYTE m=0;
  1214. if( GetKeyState(VK_SHIFT) & 0x8000 )
  1215. m |= HOTKEYF_SHIFT;
  1216. if( GetKeyState(VK_CONTROL) & 0x8000 )
  1217. m |= HOTKEYF_CONTROL;
  1218. if( GetKeyState(VK_MENU) & 0x8000 )
  1219. m |= HOTKEYF_ALT;
  1220. return m;
  1221. }
  1222. /*------------------------------------------------------------------*\
  1223. CTokenizer - Tokenizes a string using given delimiters
  1224. \*------------------------------------------------------------------*/
  1225. CTokenizer::CTokenizer(const CString& cs, const CString& csDelim):
  1226. m_cs(cs),
  1227. m_nCurPos(0)
  1228. {
  1229. SetDelimiters(csDelim);
  1230. }
  1231. void CTokenizer::SetDelimiters(const CString& csDelim)
  1232. {
  1233. for(int i = 0; i < csDelim.GetLength(); ++i)
  1234. m_delim.set(static_cast<BYTE>(csDelim[i]));
  1235. }
  1236. bool CTokenizer::Next(CString& cs)
  1237. {
  1238. int len = m_cs.GetLength();
  1239. cs.Empty();
  1240. while(m_nCurPos < len && m_delim[static_cast<BYTE>(m_cs[m_nCurPos])])
  1241. ++m_nCurPos;
  1242. if(m_nCurPos >= len)
  1243. return false;
  1244. int nStartPos = m_nCurPos;
  1245. while(m_nCurPos < len && !m_delim[static_cast<BYTE>(m_cs[m_nCurPos])])
  1246. ++m_nCurPos;
  1247. cs = m_cs.Mid(nStartPos, m_nCurPos - nStartPos);
  1248. return true;
  1249. }
  1250. CString CTokenizer::Tail() const
  1251. {
  1252. int len = m_cs.GetLength();
  1253. int nCurPos = m_nCurPos;
  1254. while(nCurPos < len && m_delim[static_cast<BYTE>(m_cs[nCurPos])])
  1255. ++nCurPos;
  1256. CString csResult;
  1257. if(nCurPos < len)
  1258. csResult = m_cs.Mid(nCurPos);
  1259. return csResult;
  1260. }
  1261. /*------------------------------------------------------------------*\
  1262. Global ToolTip Manual Control Functions
  1263. \*------------------------------------------------------------------*/
  1264. void InitToolInfo( TOOLINFO& ti )
  1265. {
  1266. // INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE
  1267. ti.cbSize = sizeof(TOOLINFO);
  1268. ti.uFlags = TTF_ABSOLUTE | TTF_TRACK;
  1269. ti.hwnd = NULL;
  1270. ti.hinst = NULL;
  1271. ti.uId = 0; // CPopup only uses uid 0
  1272. ti.lpszText = NULL;
  1273. // ToolTip control will cover the whole window
  1274. ti.rect.left = 0;
  1275. ti.rect.top = 0;
  1276. ti.rect.right = 0;
  1277. ti.rect.bottom = 0;
  1278. }
  1279. /*------------------------------------------------------------------*\
  1280. CPopup - a tooltip that pops up manually (when Show is called).
  1281. - technique learned from codeproject "ToolTipZen" by "Zarembo Maxim"
  1282. \*------------------------------------------------------------------*/
  1283. CPopup::CPopup()
  1284. {
  1285. Init();
  1286. }
  1287. // HWND_TOP
  1288. CPopup::CPopup( int x, int y, HWND hWndPosRelativeTo, HWND hWndInsertAfter )
  1289. {
  1290. Init();
  1291. m_hWndPosRelativeTo = hWndPosRelativeTo;
  1292. m_hWndInsertAfter = hWndInsertAfter;
  1293. SetPos( CPoint(x,y) );
  1294. }
  1295. CPopup::~CPopup()
  1296. {
  1297. Hide();
  1298. if( m_bOwnTT && ::IsWindow(m_hTTWnd) )
  1299. ::DestroyWindow( m_hTTWnd );
  1300. }
  1301. void CPopup::Init()
  1302. {
  1303. // initialize variables
  1304. m_bOwnTT = false;
  1305. m_hTTWnd = NULL;
  1306. m_bIsShowing = false;
  1307. m_bAllowShow = true; // used by AllowShow()
  1308. m_Pos.x = m_Pos.y = 0;
  1309. m_bTop = true;
  1310. m_bLeft = true;
  1311. m_bCenterX = false;
  1312. m_bCenterY = false;
  1313. m_hWndPosRelativeTo = NULL;
  1314. RECT rcScreen;
  1315. GetMonitorRect(-1, &rcScreen);
  1316. m_ScreenMaxX = rcScreen.right;
  1317. m_ScreenMaxY = rcScreen.bottom;
  1318. m_hWndInsertAfter = HWND_TOP; //HWND_TOPMOST
  1319. SetTTWnd();
  1320. }
  1321. void CPopup::SetTTWnd( HWND hTTWnd, TOOLINFO* pTI )
  1322. {
  1323. if( pTI )
  1324. m_TI = *pTI;
  1325. else
  1326. InitToolInfo( m_TI );
  1327. if( m_bOwnTT && ::IsWindow(m_hTTWnd) )
  1328. {
  1329. if( !::IsWindow(hTTWnd) )
  1330. return; // we would have to recreate the one that already exists
  1331. ::DestroyWindow( m_hTTWnd );
  1332. }
  1333. m_hTTWnd = hTTWnd;
  1334. if( ::IsWindow(m_hTTWnd) )
  1335. {
  1336. m_bOwnTT = false;
  1337. // if our uid tooltip already exists, get the data, else add it.
  1338. if( ! ::SendMessage(m_hTTWnd, TTM_GETTOOLINFO, 0, (LPARAM)(LPTOOLINFO) &m_TI) )
  1339. ::SendMessage(m_hTTWnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  1340. }
  1341. else
  1342. {
  1343. m_bOwnTT = true;
  1344. CreateToolTip();
  1345. }
  1346. }
  1347. void CPopup::CreateToolTip()
  1348. {
  1349. if( m_hTTWnd != NULL )
  1350. return;
  1351. // CREATE A TOOLTIP WINDOW
  1352. m_hTTWnd = CreateWindowEx(
  1353. WS_EX_TOPMOST,
  1354. TOOLTIPS_CLASS,
  1355. NULL,
  1356. TTS_NOPREFIX | TTS_ALWAYSTIP,
  1357. CW_USEDEFAULT,
  1358. CW_USEDEFAULT,
  1359. CW_USEDEFAULT,
  1360. CW_USEDEFAULT,
  1361. NULL,
  1362. NULL,
  1363. NULL,
  1364. NULL
  1365. );
  1366. m_bOwnTT = true;
  1367. // SEND AN ADDTOOL MESSAGE TO THE TOOLTIP CONTROL WINDOW
  1368. ::SendMessage(m_hTTWnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  1369. }
  1370. void CPopup::SetTimeout( int timeout )
  1371. {
  1372. if( m_hTTWnd == NULL )
  1373. return;
  1374. ::SendMessage(m_hTTWnd, TTM_SETDELAYTIME, TTDT_AUTOMATIC, timeout);
  1375. }
  1376. void CPopup::SetPos( CPoint& pos )
  1377. {
  1378. m_Pos = pos;
  1379. }
  1380. void CPopup::SetPosInfo( bool bTop, bool bCenterY, bool bLeft, bool bCenterX )
  1381. {
  1382. m_bTop = bTop;
  1383. m_bCenterY = bCenterY;
  1384. m_bLeft = bLeft;
  1385. m_bCenterX = bCenterX;
  1386. }
  1387. void CPopup::AdjustPos( CPoint& pos )
  1388. {
  1389. CRect rel(0,0,0,0);
  1390. CRect rect(0,0,0,0);
  1391. // ::SendMessage(m_hTTWnd, TTM_ADJUSTRECT, TRUE, (LPARAM)&rect);
  1392. ::GetWindowRect(m_hTTWnd,&rect);
  1393. if( ::IsWindow(m_hWndPosRelativeTo) )
  1394. ::GetWindowRect(m_hWndPosRelativeTo, &rel);
  1395. // move the rect to the relative origin
  1396. rect.bottom = rect.Height() + rel.top;
  1397. rect.top = rel.top;
  1398. rect.right = rect.Width() + rel.left;
  1399. rect.left = rel.left;
  1400. // adjust the y position
  1401. rect.OffsetRect( 0, pos.y - (m_bCenterY? rect.Height()/2: (m_bTop? 0: rect.Height())) );
  1402. if( rect.bottom > m_ScreenMaxY )
  1403. rect.OffsetRect( 0, m_ScreenMaxY - rect.bottom );
  1404. // adjust the x position
  1405. rect.OffsetRect( pos.x - (m_bCenterX? rect.Width()/2: (m_bLeft? 0: rect.Width())), 0 );
  1406. if( rect.right > m_ScreenMaxX )
  1407. rect.OffsetRect( m_ScreenMaxX - rect.right, 0 );
  1408. pos.x = rect.left;
  1409. pos.y = rect.top;
  1410. }
  1411. void CPopup::SendToolTipText( CString text )
  1412. {
  1413. //Replace the tabs with spaces, the tooltip didn't like the \t s
  1414. text.Replace("\t", " ");
  1415. m_TI.lpszText = (LPSTR) (LPCTSTR) text;
  1416. // this allows \n and \r to be interpreted correctly
  1417. ::SendMessage(m_hTTWnd, TTM_SETMAXTIPWIDTH, 0, 500);
  1418. // set the text
  1419. ::SendMessage(m_hTTWnd, TTM_SETTOOLINFO, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  1420. }
  1421. void CPopup::Show( CString text, CPoint pos, bool bAdjustPos )
  1422. {
  1423. if( m_hTTWnd == NULL )
  1424. return;
  1425. if( !m_bIsShowing )
  1426. ::SendMessage(m_hTTWnd, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(-10000,-10000));
  1427. SendToolTipText( text );
  1428. ::SendMessage(m_hTTWnd, TTM_TRACKACTIVATE, true, (LPARAM)(LPTOOLINFO) &m_TI);
  1429. if( bAdjustPos )
  1430. AdjustPos(pos);
  1431. // set the position
  1432. ::SendMessage(m_hTTWnd, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(pos.x,pos.y));
  1433. // make sure the tooltip will be on top.
  1434. ::SetWindowPos( m_hTTWnd, m_hWndInsertAfter, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE );
  1435. m_bIsShowing = true;
  1436. }
  1437. void CPopup::Show( CString text )
  1438. { Show( text, m_Pos ); }
  1439. void CPopup::AllowShow( CString text )
  1440. {
  1441. if( m_bAllowShow )
  1442. Show( text, m_Pos );
  1443. }
  1444. void CPopup::Hide()
  1445. {
  1446. if( m_hTTWnd == NULL )
  1447. return;
  1448. // deactivate if it is currently activated
  1449. ::SendMessage(m_hTTWnd, TTM_TRACKACTIVATE, FALSE, (LPARAM)(LPTOOLINFO) &m_TI);
  1450. m_bIsShowing = false;
  1451. }