Misc.cpp 47 KB

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