Misc.cpp 45 KB

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