Misc.cpp 41 KB

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