Misc.cpp 45 KB

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