Misc.cpp 39 KB

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