Misc.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include "Misc.h"
  4. #include "OptionsSheet.h"
  5. #include "TextConvert.h"
  6. #include "AlphaBlend.h"
  7. // Debug Functions
  8. CString GetIPAddress()
  9. {
  10. WORD wVersionRequested;
  11. WSADATA wsaData;
  12. char name[255];
  13. CString IP;
  14. PHOSTENT hostinfo;
  15. wVersionRequested = MAKEWORD(2,0);
  16. if (WSAStartup(wVersionRequested, &wsaData)==0)
  17. {
  18. if(gethostname(name, sizeof(name))==0)
  19. {
  20. if((hostinfo=gethostbyname(name)) != NULL)
  21. {
  22. IP = inet_ntoa(*(struct in_addr*)* hostinfo->h_addr_list);
  23. }
  24. }
  25. WSACleanup();
  26. }
  27. IP.MakeUpper();
  28. return IP;
  29. }
  30. CString GetComputerName()
  31. {
  32. TCHAR ComputerName[MAX_COMPUTERNAME_LENGTH+1] = _T("");
  33. DWORD Size=MAX_COMPUTERNAME_LENGTH+1;
  34. GetComputerName(ComputerName, &Size);
  35. CString cs(ComputerName);
  36. cs.MakeUpper();
  37. return cs;
  38. }
  39. void AppendToFile(const TCHAR* fn, const TCHAR* msg)
  40. {
  41. #ifdef _UNICODE
  42. FILE *file = _wfopen(fn, _T("a"));
  43. #else
  44. FILE *file = fopen(fn, _T("a"));
  45. #endif
  46. ASSERT( file );
  47. #ifdef _UNICODE
  48. fwprintf(file, msg);
  49. #else
  50. fprintf(file, msg);
  51. #endif
  52. fclose(file);
  53. }
  54. void log(const TCHAR* msg, bool bFromSendRecieve, CString csFile, long lLine)
  55. {
  56. ASSERT(AfxIsValidString(msg));
  57. CTime time = CTime::GetCurrentTime();
  58. CString csText = time.Format("[%Y/%m/%d %I:%M:%S %p - ");
  59. CString csFileLine;
  60. csFile = GetFileName(csFile);
  61. csFileLine.Format(_T("%s %d] "), csFile, lLine);
  62. csText += csFileLine;
  63. csText += msg;
  64. csText += "\n";
  65. #ifndef _DEBUG
  66. if(CGetSetOptions::m_bOutputDebugString)
  67. #endif
  68. {
  69. OutputDebugString(csText);
  70. }
  71. #ifndef _DEBUG
  72. if(!bFromSendRecieve)
  73. {
  74. if(!g_Opt.m_bEnableDebugLogging)
  75. return;
  76. }
  77. #endif
  78. CString csExeFile = CGetSetOptions::GetPath(PATH_LOG_FILE);
  79. csExeFile += "Ditto.log";
  80. AppendToFile(csExeFile, csText);
  81. }
  82. void logsendrecieveinfo(CString cs, CString csFile, long lLine)
  83. {
  84. if(g_Opt.m_bLogSendReceiveErrors)
  85. log(cs, true, csFile, lLine);
  86. }
  87. CString GetErrorString( int err )
  88. {
  89. CString str;
  90. LPVOID lpMsgBuf;
  91. ::FormatMessage(
  92. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  93. NULL,
  94. err,
  95. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  96. (LPTSTR) &lpMsgBuf,
  97. 0,
  98. NULL
  99. );
  100. str = (LPCTSTR) lpMsgBuf;
  101. // Display the string.
  102. // ::MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
  103. ::LocalFree( lpMsgBuf );
  104. return str;
  105. }
  106. // Utility Functions
  107. CString StrF(const TCHAR * pszFormat, ...)
  108. {
  109. ASSERT( AfxIsValidString( pszFormat ) );
  110. CString str;
  111. va_list argList;
  112. va_start( argList, pszFormat );
  113. str.FormatV( pszFormat, argList );
  114. va_end( argList );
  115. return str;
  116. }
  117. BYTE GetEscapeChar( BYTE ch )
  118. {
  119. switch(ch)
  120. {
  121. case '\'': return '\''; // Single quotation mark (') = 39 or 0x27
  122. case '\"': return '\"'; // Double quotation mark (") = 34 or 0x22
  123. case '?': return '\?'; // Question mark (?) = 63 or 0x3f
  124. case '\\': return '\\'; // Backslash (\) = 92 or 0x5c
  125. case 'a': return '\a'; // Alert (BEL) = 7
  126. case 'b': return '\b'; // Backspace (BS) = 8
  127. case 'f': return '\f'; // Formfeed (FF) = 12 or 0x0c
  128. case 'n': return '\n'; // Newline (NL or LF) = 10 or 0x0a
  129. case 'r': return '\r'; // Carriage Return (CR) = 13 or 0x0d
  130. case 't': return '\t'; // Horizontal tab (HT) = 9
  131. case 'v': return '\v'; // Vertical tab (VT) = 11 or 0x0b
  132. case '0': return '\0'; // Null character (NUL) = 0
  133. }
  134. return 0; // invalid
  135. }
  136. CString RemoveEscapes( const TCHAR* str )
  137. {
  138. ASSERT( str );
  139. CString ret;
  140. TCHAR* pSrc = (TCHAR*) str;
  141. TCHAR* pDest = ret.GetBuffer(STRLEN(pSrc));
  142. TCHAR* pStart = pDest;
  143. while( *pSrc != '\0' )
  144. {
  145. if( *pSrc == '\\' )
  146. {
  147. pSrc++;
  148. *pDest = GetEscapeChar((BYTE)pSrc );
  149. }
  150. else
  151. *pDest = *pSrc;
  152. pSrc++;
  153. pDest++;
  154. }
  155. ret.ReleaseBuffer( pDest - pStart );
  156. return ret;
  157. }
  158. CString GetWndText( HWND hWnd )
  159. {
  160. CString text;
  161. if( !IsWindow(hWnd) )
  162. return "! NOT A VALID WINDOW !";
  163. CWnd* pWnd = CWnd::FromHandle(hWnd);
  164. pWnd->GetWindowText(text);
  165. return text;
  166. }
  167. bool IsAppWnd( HWND hWnd )
  168. {
  169. DWORD dwMyPID = ::GetCurrentProcessId();
  170. DWORD dwTestPID;
  171. ::GetWindowThreadProcessId( hWnd, &dwTestPID );
  172. return dwMyPID == dwTestPID;
  173. }
  174. CPoint GetFocusedCaretPos()
  175. {
  176. CPoint pt(-1, -1);
  177. if(theApp.m_hTargetWnd)
  178. {
  179. GUITHREADINFO guiThreadInfo;
  180. guiThreadInfo.cbSize = sizeof(GUITHREADINFO);
  181. DWORD OtherThreadID = GetWindowThreadProcessId(theApp.m_hTargetWnd, NULL);
  182. if(GetGUIThreadInfo(OtherThreadID, &guiThreadInfo))
  183. {
  184. CRect rc(guiThreadInfo.rcCaret);
  185. if(rc.IsRectEmpty() == FALSE)
  186. {
  187. pt = rc.BottomRight();
  188. ::ClientToScreen(theApp.m_hTargetWnd, &pt);
  189. }
  190. }
  191. }
  192. return pt;
  193. }
  194. /*----------------------------------------------------------------------------*\
  195. Global Memory Helper Functions
  196. \*----------------------------------------------------------------------------*/
  197. // make sure the given HGLOBAL is valid.
  198. BOOL IsValid( HGLOBAL hGlobal )
  199. {
  200. void* pvData = ::GlobalLock( hGlobal );
  201. ::GlobalUnlock( hGlobal );
  202. return ( pvData != NULL );
  203. }
  204. // asserts if hDest isn't big enough
  205. void CopyToGlobalHP( HGLOBAL hDest, LPVOID pBuf, ULONG ulBufLen )
  206. {
  207. ASSERT( hDest && pBuf && ulBufLen );
  208. LPVOID pvData = GlobalLock(hDest);
  209. ASSERT( pvData );
  210. ULONG size = GlobalSize(hDest);
  211. ASSERT( size >= ulBufLen ); // assert if hDest isn't big enough
  212. memcpy(pvData, pBuf, ulBufLen);
  213. GlobalUnlock(hDest);
  214. }
  215. void CopyToGlobalHH( HGLOBAL hDest, HGLOBAL hSource, ULONG ulBufLen )
  216. {
  217. ASSERT( hDest && hSource && ulBufLen );
  218. LPVOID pvData = GlobalLock(hSource);
  219. ASSERT( pvData );
  220. ULONG size = GlobalSize(hSource);
  221. ASSERT( size >= ulBufLen ); // assert if hSource isn't big enough
  222. CopyToGlobalHP(hDest, pvData, ulBufLen);
  223. GlobalUnlock(hSource);
  224. }
  225. HGLOBAL NewGlobalP( LPVOID pBuf, UINT nLen )
  226. {
  227. ASSERT( pBuf && nLen );
  228. HGLOBAL hDest = GlobalAlloc( GMEM_MOVEABLE | GMEM_SHARE, nLen );
  229. ASSERT( hDest );
  230. CopyToGlobalHP( hDest, pBuf, nLen );
  231. return hDest;
  232. }
  233. HGLOBAL NewGlobal(UINT nLen)
  234. {
  235. ASSERT(nLen);
  236. HGLOBAL hDest = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, nLen);
  237. return hDest;
  238. }
  239. HGLOBAL NewGlobalH( HGLOBAL hSource, UINT nLen )
  240. {
  241. ASSERT( hSource && nLen );
  242. LPVOID pvData = GlobalLock( hSource );
  243. HGLOBAL hDest = NewGlobalP( pvData, nLen );
  244. GlobalUnlock( hSource );
  245. return hDest;
  246. }
  247. int CompareGlobalHP(HGLOBAL hLeft, LPVOID pBuf, ULONG ulBufLen)
  248. {
  249. ASSERT(hLeft && pBuf && ulBufLen);
  250. LPVOID pvData = GlobalLock(hLeft);
  251. ASSERT(pvData);
  252. ASSERT(ulBufLen <= GlobalSize(hLeft));
  253. int result = memcmp(pvData, pBuf, ulBufLen);
  254. GlobalUnlock(hLeft);
  255. return result;
  256. }
  257. int CompareGlobalHH( HGLOBAL hLeft, HGLOBAL hRight, ULONG ulBufLen )
  258. {
  259. ASSERT( hLeft && hRight && ulBufLen );
  260. ASSERT( ulBufLen <= GlobalSize(hRight) );
  261. LPVOID pvData = GlobalLock(hRight);
  262. ASSERT( pvData );
  263. int result = CompareGlobalHP( hLeft, pvData, ulBufLen );
  264. GlobalUnlock( hLeft );
  265. return result;
  266. }
  267. //Do not change these these are stored in the database
  268. CLIPFORMAT GetFormatID(LPCTSTR cbName)
  269. {
  270. if(STRCMP(cbName, _T("CF_TEXT")) == 0)
  271. return CF_TEXT;
  272. else if(STRCMP(cbName, _T("CF_METAFILEPICT")) == 0)
  273. return CF_METAFILEPICT;
  274. else if(STRCMP(cbName, _T("CF_SYLK")) == 0)
  275. return CF_SYLK;
  276. else if(STRCMP(cbName, _T("CF_DIF")) == 0)
  277. return CF_DIF;
  278. else if(STRCMP(cbName, _T("CF_TIFF")) == 0)
  279. return CF_TIFF;
  280. else if(STRCMP(cbName, _T("CF_OEMTEXT")) == 0)
  281. return CF_OEMTEXT;
  282. else if(STRCMP(cbName, _T("CF_DIB")) == 0)
  283. return CF_DIB;
  284. else if(STRCMP(cbName, _T("CF_PALETTE")) == 0)
  285. return CF_PALETTE;
  286. else if(STRCMP(cbName, _T("CF_PENDATA")) == 0)
  287. return CF_PENDATA;
  288. else if(STRCMP(cbName, _T("CF_RIFF")) == 0)
  289. return CF_RIFF;
  290. else if(STRCMP(cbName, _T("CF_WAVE")) == 0)
  291. return CF_WAVE;
  292. else if(STRCMP(cbName, _T("CF_UNICODETEXT")) == 0)
  293. return CF_UNICODETEXT;
  294. else if(STRCMP(cbName, _T("CF_ENHMETAFILE")) == 0)
  295. return CF_ENHMETAFILE;
  296. else if(STRCMP(cbName, _T("CF_HDROP")) == 0)
  297. return CF_HDROP;
  298. else if(STRCMP(cbName, _T("CF_LOCALE")) == 0)
  299. return CF_LOCALE;
  300. else if(STRCMP(cbName, _T("CF_OWNERDISPLAY")) == 0)
  301. return CF_OWNERDISPLAY;
  302. else if(STRCMP(cbName, _T("CF_DSPTEXT")) == 0)
  303. return CF_DSPTEXT;
  304. else if(STRCMP(cbName, _T("CF_DSPBITMAP")) == 0)
  305. return CF_DSPBITMAP;
  306. else if(STRCMP(cbName, _T("CF_DSPMETAFILEPICT")) == 0)
  307. return CF_DSPMETAFILEPICT;
  308. else if(STRCMP(cbName, _T("CF_DSPENHMETAFILE")) == 0)
  309. return CF_DSPENHMETAFILE;
  310. return ::RegisterClipboardFormat(cbName);
  311. }
  312. //Do not change these these are stored in the database
  313. CString GetFormatName(CLIPFORMAT cbType)
  314. {
  315. switch(cbType)
  316. {
  317. case CF_TEXT:
  318. return _T("CF_TEXT");
  319. case CF_BITMAP:
  320. return _T("CF_BITMAP");
  321. case CF_METAFILEPICT:
  322. return _T("CF_METAFILEPICT");
  323. case CF_SYLK:
  324. return _T("CF_SYLK");
  325. case CF_DIF:
  326. return _T("CF_DIF");
  327. case CF_TIFF:
  328. return _T("CF_TIFF");
  329. case CF_OEMTEXT:
  330. return _T("CF_OEMTEXT");
  331. case CF_DIB:
  332. return _T("CF_DIB");
  333. case CF_PALETTE:
  334. return _T("CF_PALETTE");
  335. case CF_PENDATA:
  336. return _T("CF_PENDATA");
  337. case CF_RIFF:
  338. return _T("CF_RIFF");
  339. case CF_WAVE:
  340. return _T("CF_WAVE");
  341. case CF_UNICODETEXT:
  342. return _T("CF_UNICODETEXT");
  343. case CF_ENHMETAFILE:
  344. return _T("CF_ENHMETAFILE");
  345. case CF_HDROP:
  346. return _T("CF_HDROP");
  347. case CF_LOCALE:
  348. return _T("CF_LOCALE");
  349. case CF_OWNERDISPLAY:
  350. return _T("CF_OWNERDISPLAY");
  351. case CF_DSPTEXT:
  352. return _T("CF_DSPTEXT");
  353. case CF_DSPBITMAP:
  354. return _T("CF_DSPBITMAP");
  355. case CF_DSPMETAFILEPICT:
  356. return _T("CF_DSPMETAFILEPICT");
  357. case CF_DSPENHMETAFILE:
  358. return _T("CF_DSPENHMETAFILE");
  359. default:
  360. //Not a default type get the name from the clipboard
  361. if (cbType != 0)
  362. {
  363. TCHAR szFormat[256];
  364. GetClipboardFormatName(cbType, szFormat, 256);
  365. return szFormat;
  366. }
  367. break;
  368. }
  369. return "ERROR";
  370. }
  371. CString GetFilePath(CString csFileName)
  372. {
  373. long lSlash = csFileName.ReverseFind('\\');
  374. if(lSlash > -1)
  375. {
  376. csFileName = csFileName.Left(lSlash + 1);
  377. }
  378. return csFileName;
  379. }
  380. CString GetFileName(CString csFileName)
  381. {
  382. long lSlash = csFileName.ReverseFind('\\');
  383. if(lSlash > -1)
  384. {
  385. csFileName = csFileName.Right(csFileName.GetLength() - lSlash - 1);
  386. }
  387. return csFileName;
  388. }
  389. /*------------------------------------------------------------------*\
  390. CHotKey - a single system-wide hotkey
  391. \*------------------------------------------------------------------*/
  392. CHotKey::CHotKey( CString name, DWORD defKey, bool bUnregOnShowDitto )
  393. : m_Name(name), m_bIsRegistered(false), m_bUnRegisterOnShowDitto(bUnregOnShowDitto)
  394. {
  395. m_Atom = ::GlobalAddAtom( m_Name );
  396. ASSERT( m_Atom );
  397. m_Key = (DWORD) g_Opt.GetProfileLong( m_Name, (long) defKey );
  398. g_HotKeys.Add( this );
  399. }
  400. CHotKey::~CHotKey()
  401. {
  402. Unregister();
  403. }
  404. void CHotKey::SetKey( DWORD key, bool bSave )
  405. {
  406. if( m_Key == key )
  407. return;
  408. if( m_bIsRegistered )
  409. Unregister();
  410. m_Key = key;
  411. if( bSave )
  412. SaveKey();
  413. }
  414. void CHotKey::LoadKey()
  415. {
  416. SetKey( (DWORD) g_Opt.GetProfileLong( m_Name, 0 ) );
  417. }
  418. bool CHotKey::SaveKey()
  419. {
  420. return g_Opt.SetProfileLong( m_Name, (long) m_Key ) != FALSE;
  421. }
  422. // CString GetKeyAsText();
  423. // void SetKeyFromText( CString text );
  424. BOOL CHotKey::ValidateHotKey(DWORD dwHotKey)
  425. {
  426. ATOM id = ::GlobalAddAtom(_T("HK_VALIDATE"));
  427. BOOL bResult = ::RegisterHotKey( g_HotKeys.m_hWnd,
  428. id,
  429. GetModifier(dwHotKey),
  430. LOBYTE(dwHotKey) );
  431. if(bResult)
  432. ::UnregisterHotKey(g_HotKeys.m_hWnd, id);
  433. ::GlobalDeleteAtom(id);
  434. return bResult;
  435. }
  436. void CHotKey::CopyFromCtrl(CHotKeyCtrl& ctrl, HWND hParent, int nWindowsCBID)
  437. {
  438. long lHotKey = ctrl.GetHotKey();
  439. short sKeyKode = LOBYTE(lHotKey);
  440. short sModifers = HIBYTE(lHotKey);
  441. if(lHotKey && ::IsDlgButtonChecked(hParent, nWindowsCBID))
  442. {
  443. sModifers |= HOTKEYF_EXT;
  444. }
  445. SetKey(MAKEWORD(sKeyKode, sModifers));
  446. }
  447. void CHotKey::CopyToCtrl(CHotKeyCtrl& ctrl, HWND hParent, int nWindowsCBID)
  448. {
  449. long lModifiers = HIBYTE(m_Key);
  450. ctrl.SetHotKey(LOBYTE(m_Key), (WORD)lModifiers);
  451. if(lModifiers & HOTKEYF_EXT)
  452. {
  453. ::CheckDlgButton(hParent, nWindowsCBID, BST_CHECKED);
  454. }
  455. }
  456. UINT CHotKey::GetModifier(DWORD dwHotKey)
  457. {
  458. UINT uMod = 0;
  459. if( HIBYTE(dwHotKey) & HOTKEYF_SHIFT ) uMod |= MOD_SHIFT;
  460. if( HIBYTE(dwHotKey) & HOTKEYF_CONTROL ) uMod |= MOD_CONTROL;
  461. if( HIBYTE(dwHotKey) & HOTKEYF_ALT ) uMod |= MOD_ALT;
  462. if( HIBYTE(dwHotKey) & HOTKEYF_EXT ) uMod |= MOD_WIN;
  463. return uMod;
  464. }
  465. bool CHotKey::Register()
  466. {
  467. if(m_Key)
  468. {
  469. if(m_bIsRegistered == false)
  470. {
  471. ASSERT(g_HotKeys.m_hWnd);
  472. m_bIsRegistered = ::RegisterHotKey(g_HotKeys.m_hWnd,
  473. m_Atom,
  474. GetModifier(),
  475. LOBYTE(m_Key) ) == TRUE;
  476. }
  477. }
  478. else
  479. m_bIsRegistered = true;
  480. return m_bIsRegistered;
  481. }
  482. bool CHotKey::Unregister(bool bOnShowingDitto)
  483. {
  484. if(!m_bIsRegistered)
  485. return true;
  486. if(bOnShowingDitto)
  487. {
  488. if(m_bUnRegisterOnShowDitto == false)
  489. return true;
  490. }
  491. if(m_Key)
  492. {
  493. ASSERT(g_HotKeys.m_hWnd);
  494. if(::UnregisterHotKey( g_HotKeys.m_hWnd, m_Atom))
  495. {
  496. m_bIsRegistered = false;
  497. return true;
  498. }
  499. else
  500. {
  501. Log(_T("Unregister FAILED!"));
  502. ASSERT(0);
  503. }
  504. }
  505. else
  506. {
  507. m_bIsRegistered = false;
  508. return true;
  509. }
  510. return false;
  511. }
  512. /*------------------------------------------------------------------*\
  513. CHotKeys - Manages system-wide hotkeys
  514. \*------------------------------------------------------------------*/
  515. CHotKeys g_HotKeys;
  516. CHotKeys::CHotKeys() : m_hWnd(NULL) {}
  517. CHotKeys::~CHotKeys()
  518. {
  519. CHotKey* pHotKey;
  520. int count = GetSize();
  521. for(int i=0; i < count; i++)
  522. {
  523. pHotKey = ElementAt(i);
  524. if(pHotKey)
  525. delete pHotKey;
  526. }
  527. }
  528. int CHotKeys::Find( CHotKey* pHotKey )
  529. {
  530. int count = GetSize();
  531. for(int i=0; i < count; i++)
  532. {
  533. if( pHotKey == ElementAt(i) )
  534. return i;
  535. }
  536. return -1;
  537. }
  538. bool CHotKeys::Remove( CHotKey* pHotKey )
  539. {
  540. int i = Find(pHotKey);
  541. if(i >= 0)
  542. {
  543. RemoveAt(i);
  544. return true;
  545. }
  546. return false;
  547. }
  548. void CHotKeys::LoadAllKeys()
  549. {
  550. int count = GetSize();
  551. for(int i=0; i < count; i++)
  552. ElementAt(i)->LoadKey();
  553. }
  554. void CHotKeys::SaveAllKeys()
  555. {
  556. int count = GetSize();
  557. for(int i=0; i < count; i++)
  558. ElementAt(i)->SaveKey();
  559. }
  560. void CHotKeys::RegisterAll(bool bMsgOnError)
  561. {
  562. CString str;
  563. CHotKey* pHotKey;
  564. int count = GetSize();
  565. for(int i = 0; i < count; i++)
  566. {
  567. pHotKey = ElementAt(i);
  568. if(!pHotKey->Register())
  569. {
  570. str = "Error Registering ";
  571. str += pHotKey->GetName();
  572. Log(str);
  573. if(bMsgOnError)
  574. AfxMessageBox(str);
  575. }
  576. }
  577. }
  578. void CHotKeys::UnregisterAll(bool bMsgOnError, bool bOnShowDitto)
  579. {
  580. CString str;
  581. CHotKey* pHotKey;
  582. int count = GetSize();
  583. for(int i = 0; i < count; i++)
  584. {
  585. pHotKey = ElementAt(i);
  586. if(!pHotKey->Unregister(bOnShowDitto))
  587. {
  588. str = "Error Unregistering ";
  589. str += pHotKey->GetName();
  590. Log(str);
  591. if(bMsgOnError)
  592. AfxMessageBox(str);
  593. }
  594. }
  595. }
  596. void CHotKeys::GetKeys(ARRAY& keys)
  597. {
  598. int count = GetSize();
  599. keys.SetSize(count);
  600. for(int i=0; i < count; i++)
  601. keys[i] = ElementAt(i)->GetKey();
  602. }
  603. // caution! this alters hotkeys based upon corresponding indexes
  604. void CHotKeys::SetKeys(ARRAY& keys, bool bSave)
  605. {
  606. int count = GetSize();
  607. ASSERT(count == keys.GetSize());
  608. for(int i=0; i < count; i++)
  609. ElementAt(i)->SetKey(keys[i], bSave);
  610. }
  611. bool CHotKeys::FindFirstConflict(ARRAY& keys, int* pX, int* pY)
  612. {
  613. bool bConflict = false;
  614. int i, j;
  615. int count = keys.GetSize();
  616. DWORD key;
  617. for(i = 0; i < count && !bConflict; i++)
  618. {
  619. key = keys.ElementAt(i);
  620. // only check valid keys
  621. if(key == 0)
  622. continue;
  623. // scan the array for a duplicate
  624. for(j = i+1; j < count; j++ )
  625. {
  626. if(keys.ElementAt(j) == key)
  627. {
  628. bConflict = true;
  629. break;
  630. }
  631. }
  632. }
  633. if(bConflict)
  634. {
  635. if(pX)
  636. *pX = i-1;
  637. if(pY)
  638. *pY = j;
  639. }
  640. return bConflict;
  641. }
  642. // if true, pX and pY (if valid) are set to the indexes of the conflicting hotkeys.
  643. bool CHotKeys::FindFirstConflict(int* pX, int* pY)
  644. {
  645. ARRAY keys;
  646. GetKeys(keys);
  647. return FindFirstConflict(keys, pX, pY);
  648. }
  649. /****************************************************************************************************
  650. BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
  651. ***************************************************************************************************/
  652. typedef struct
  653. {
  654. long lFlags; // Flags
  655. LPRECT pVirtualRect; // Ptr to rect that receives the results, or the src of the monitor search method
  656. int iMonitor; // Ndx to the mointor to look at, -1 for all, -or- result of the monitor search method
  657. int nMonitorCount; // Total number of monitors found, -1 for monitor search method
  658. } MONITOR_ENUM_PARAM;
  659. #define MONITOR_SEARCH_METOHD 0x00000001
  660. BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
  661. {
  662. // Typecast param
  663. MONITOR_ENUM_PARAM* pParam = (MONITOR_ENUM_PARAM*)dwData;
  664. if(pParam)
  665. {
  666. // If a dest rect was passed
  667. if(pParam->pVirtualRect)
  668. {
  669. // If MONITOR_SEARCH_METOHD then we are being asked for the index of the monitor
  670. // that the rect falls inside of
  671. if(pParam->lFlags & MONITOR_SEARCH_METOHD)
  672. {
  673. if( (pParam->pVirtualRect->right < lprcMonitor->left) ||
  674. (pParam->pVirtualRect->left > lprcMonitor->right) ||
  675. (pParam->pVirtualRect->bottom < lprcMonitor->top) ||
  676. (pParam->pVirtualRect->top > lprcMonitor->bottom))
  677. {
  678. // Nothing
  679. }
  680. else
  681. {
  682. // This is the one
  683. pParam->iMonitor = pParam->nMonitorCount;
  684. // Stop the enumeration
  685. return FALSE;
  686. }
  687. }
  688. else
  689. {
  690. if(pParam->iMonitor == pParam->nMonitorCount)
  691. {
  692. *pParam->pVirtualRect = *lprcMonitor;
  693. }
  694. else
  695. if(pParam->iMonitor == -1)
  696. {
  697. pParam->pVirtualRect->left = min(pParam->pVirtualRect->left, lprcMonitor->left);
  698. pParam->pVirtualRect->top = min(pParam->pVirtualRect->top, lprcMonitor->top);
  699. pParam->pVirtualRect->right = max(pParam->pVirtualRect->right, lprcMonitor->right);
  700. pParam->pVirtualRect->bottom = max(pParam->pVirtualRect->bottom, lprcMonitor->bottom);
  701. }
  702. }
  703. }
  704. // Up the count if necessary
  705. pParam->nMonitorCount++;
  706. }
  707. return TRUE;
  708. }
  709. int GetScreenWidth(void)
  710. {
  711. OSVERSIONINFO OS_Version_Info;
  712. DWORD dwPlatform = 0;
  713. if(GetVersionEx(&OS_Version_Info) != 0)
  714. {
  715. dwPlatform = OS_Version_Info.dwPlatformId;
  716. }
  717. if(dwPlatform == VER_PLATFORM_WIN32_NT)
  718. {
  719. int width, height;
  720. width = GetSystemMetrics(SM_CXSCREEN);
  721. height = GetSystemMetrics(SM_CYSCREEN);
  722. switch(width)
  723. {
  724. default:
  725. case 640:
  726. case 800:
  727. case 1024:
  728. return(width);
  729. case 1280:
  730. if(height == 480)
  731. {
  732. return(width / 2);
  733. }
  734. return(width);
  735. case 1600:
  736. if(height == 600)
  737. {
  738. return(width / 2);
  739. }
  740. return(width);
  741. case 2048:
  742. if(height == 768)
  743. {
  744. return(width / 2);
  745. }
  746. return(width);
  747. }
  748. }
  749. else
  750. {
  751. return(GetSystemMetrics(SM_CXSCREEN));
  752. }
  753. }
  754. int GetScreenHeight(void)
  755. {
  756. OSVERSIONINFO OS_Version_Info;
  757. DWORD dwPlatform = 0;
  758. if(GetVersionEx(&OS_Version_Info) != 0)
  759. {
  760. dwPlatform = OS_Version_Info.dwPlatformId;
  761. }
  762. if(dwPlatform == VER_PLATFORM_WIN32_NT)
  763. {
  764. int width, height;
  765. width = GetSystemMetrics(SM_CXSCREEN);
  766. height = GetSystemMetrics(SM_CYSCREEN);
  767. switch(height)
  768. {
  769. default:
  770. case 480:
  771. case 600:
  772. case 768:
  773. return(height);
  774. case 960:
  775. if(width == 640)
  776. {
  777. return(height / 2);
  778. }
  779. return(height);
  780. case 1200:
  781. if(width == 800)
  782. {
  783. return(height / 2);
  784. }
  785. return(height);
  786. case 1536:
  787. if(width == 1024)
  788. {
  789. return(height / 2);
  790. }
  791. return(height);
  792. }
  793. }
  794. else
  795. {
  796. return(GetSystemMetrics(SM_CYSCREEN));
  797. }
  798. }
  799. int GetMonitorFromRect(LPRECT lpMonitorRect)
  800. {
  801. // Build up the param
  802. MONITOR_ENUM_PARAM EnumParam;
  803. ZeroMemory(&EnumParam, sizeof(EnumParam));
  804. EnumParam.lFlags = MONITOR_SEARCH_METOHD;
  805. EnumParam.pVirtualRect = lpMonitorRect;
  806. EnumParam.iMonitor = -1;
  807. // Enum Displays
  808. EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (long)&EnumParam);
  809. // Return the result
  810. return EnumParam.iMonitor;
  811. }
  812. void GetMonitorRect(int iMonitor, LPRECT lpDestRect)
  813. {
  814. // Build up the param
  815. MONITOR_ENUM_PARAM EnumParam;
  816. ZeroMemory(&EnumParam, sizeof(EnumParam));
  817. EnumParam.iMonitor = iMonitor;
  818. EnumParam.pVirtualRect = lpDestRect;
  819. // Zero out dest rect
  820. lpDestRect->bottom = lpDestRect->left = lpDestRect->right = lpDestRect->top = 0;
  821. // Enum Displays
  822. EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (long)&EnumParam);
  823. // If not successful, default to the screen dimentions
  824. if(lpDestRect->right == 0 || lpDestRect->bottom == 0)
  825. {
  826. lpDestRect->right = GetScreenWidth();
  827. lpDestRect->bottom = GetScreenHeight();
  828. }
  829. }
  830. /*------------------------------------------------------------------*\
  831. CAccel - an Accelerator (in-app hotkey)
  832. - the win32 CreateAcceleratorTable using ACCEL was insufficient
  833. because it only allowed a WORD for the cmd associated with it.
  834. \*------------------------------------------------------------------*/
  835. /*------------------------------------------------------------------*\
  836. CAccels - Manages a set of CAccel
  837. \*------------------------------------------------------------------*/
  838. int CompareAccel( const void* pLeft, const void* pRight )
  839. {
  840. WORD w;
  841. int l,r;
  842. // swap bytes: place the VirtualKey in the MSB and the modifier in the LSB
  843. // so that Accels based upon the same vkey are grouped together.
  844. // this is required by our use of m_Index
  845. // alternatively, we could store them this way in CAccel.
  846. w = (WORD) ((CAccel*)pLeft)->Key;
  847. l = (ACCEL_VKEY(w) << 8) | ACCEL_MOD(w);
  848. w = (WORD) ((CAccel*)pRight)->Key;
  849. r = (ACCEL_VKEY(w) << 8) | ACCEL_MOD(w);
  850. return l - r;
  851. }
  852. CAccels::CAccels()
  853. {}
  854. void CAccels::AddAccel( CAccel& a )
  855. {
  856. m_Map.SetAt(a.Key, a.Cmd);
  857. }
  858. bool CAccels::OnMsg( MSG* pMsg, DWORD &dID)
  859. {
  860. // bit 30 (0x40000000) is 1 if this is NOT the first msg of the key
  861. // i.e. auto-repeat may cause multiple msgs of the same key
  862. if( (pMsg->lParam & 0x40000000) ||
  863. (pMsg->message != WM_KEYDOWN &&
  864. pMsg->message != WM_SYSKEYDOWN) )
  865. {
  866. return NULL;
  867. }
  868. if( !pMsg || m_Map.GetCount() <= 0 )
  869. return NULL;
  870. BYTE vkey = LOBYTE(pMsg->wParam);
  871. BYTE mod = GetKeyStateModifiers();
  872. DWORD key = ACCEL_MAKEKEY( vkey, mod );
  873. if(m_Map.Lookup(key, dID))
  874. return true;;
  875. return false;
  876. }
  877. BYTE GetKeyStateModifiers()
  878. {
  879. BYTE m=0;
  880. if( GetKeyState(VK_SHIFT) & 0x8000 )
  881. m |= HOTKEYF_SHIFT;
  882. if( GetKeyState(VK_CONTROL) & 0x8000 )
  883. m |= HOTKEYF_CONTROL;
  884. if( GetKeyState(VK_MENU) & 0x8000 )
  885. m |= HOTKEYF_ALT;
  886. return m;
  887. }
  888. /*------------------------------------------------------------------*\
  889. CTokenizer - Tokenizes a string using given delimiters
  890. \*------------------------------------------------------------------*/
  891. CTokenizer::CTokenizer(const CString& cs, const CString& csDelim):
  892. m_cs(cs),
  893. m_nCurPos(0)
  894. {
  895. SetDelimiters(csDelim);
  896. }
  897. void CTokenizer::SetDelimiters(const CString& csDelim)
  898. {
  899. for(int i = 0; i < csDelim.GetLength(); ++i)
  900. m_delim.Add(csDelim[i]);
  901. m_delim.SortAscending();
  902. }
  903. bool CTokenizer::Next(CString& cs)
  904. {
  905. cs.Empty();
  906. int len = m_cs.GetLength();
  907. while (m_nCurPos < len && m_delim.Find(m_cs[m_nCurPos]))
  908. ++ m_nCurPos;
  909. if (m_nCurPos >= len)
  910. return false;
  911. int nStartPos = m_nCurPos;
  912. while (m_nCurPos < len && !m_delim.Find(m_cs[m_nCurPos]))
  913. ++ m_nCurPos;
  914. cs = m_cs.Mid(nStartPos, m_nCurPos - nStartPos);
  915. return true;
  916. }
  917. CString CTokenizer::Tail() const
  918. {
  919. int len = m_cs.GetLength();
  920. int nCurPos = m_nCurPos;
  921. while(nCurPos < len && m_delim[static_cast<BYTE>(m_cs[nCurPos])])
  922. ++nCurPos;
  923. CString csResult;
  924. if(nCurPos < len)
  925. csResult = m_cs.Mid(nCurPos);
  926. return csResult;
  927. }
  928. /*------------------------------------------------------------------*\
  929. Global ToolTip Manual Control Functions
  930. \*------------------------------------------------------------------*/
  931. void InitToolInfo( TOOLINFO& ti )
  932. {
  933. // INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE
  934. ti.cbSize = sizeof(TOOLINFO);
  935. ti.uFlags = TTF_ABSOLUTE | TTF_TRACK;
  936. ti.hwnd = NULL;
  937. ti.hinst = NULL;
  938. ti.uId = 0; // CPopup only uses uid 0
  939. ti.lpszText = NULL;
  940. // ToolTip control will cover the whole window
  941. ti.rect.left = 0;
  942. ti.rect.top = 0;
  943. ti.rect.right = 0;
  944. ti.rect.bottom = 0;
  945. }
  946. /*------------------------------------------------------------------*\
  947. CPopup - a tooltip that pops up manually (when Show is called).
  948. - technique learned from codeproject "ToolTipZen" by "Zarembo Maxim"
  949. \*------------------------------------------------------------------*/
  950. CPopup::CPopup()
  951. {
  952. Init();
  953. }
  954. // HWND_TOP
  955. CPopup::CPopup( int x, int y, HWND hWndPosRelativeTo, HWND hWndInsertAfter )
  956. {
  957. Init();
  958. m_hWndPosRelativeTo = hWndPosRelativeTo;
  959. m_hWndInsertAfter = hWndInsertAfter;
  960. SetPos( CPoint(x,y) );
  961. }
  962. CPopup::~CPopup()
  963. {
  964. Hide();
  965. if( m_bOwnTT && ::IsWindow(m_hTTWnd) )
  966. ::DestroyWindow( m_hTTWnd );
  967. }
  968. void CPopup::Init()
  969. {
  970. // initialize variables
  971. m_bOwnTT = false;
  972. m_hTTWnd = NULL;
  973. m_bIsShowing = false;
  974. m_bAllowShow = true; // used by AllowShow()
  975. m_Pos.x = m_Pos.y = 0;
  976. m_bTop = true;
  977. m_bLeft = true;
  978. m_bCenterX = false;
  979. m_bCenterY = false;
  980. m_hWndPosRelativeTo = NULL;
  981. RECT rcScreen;
  982. GetMonitorRect(-1, &rcScreen);
  983. m_ScreenMaxX = rcScreen.right;
  984. m_ScreenMaxY = rcScreen.bottom;
  985. m_hWndInsertAfter = HWND_TOP; //HWND_TOPMOST
  986. SetTTWnd();
  987. }
  988. void CPopup::SetTTWnd( HWND hTTWnd, TOOLINFO* pTI )
  989. {
  990. if( pTI )
  991. m_TI = *pTI;
  992. else
  993. InitToolInfo( m_TI );
  994. if( m_bOwnTT && ::IsWindow(m_hTTWnd) )
  995. {
  996. if( !::IsWindow(hTTWnd) )
  997. return; // we would have to recreate the one that already exists
  998. ::DestroyWindow( m_hTTWnd );
  999. }
  1000. m_hTTWnd = hTTWnd;
  1001. if( ::IsWindow(m_hTTWnd) )
  1002. {
  1003. m_bOwnTT = false;
  1004. // if our uid tooltip already exists, get the data, else add it.
  1005. if( ! ::SendMessage(m_hTTWnd, TTM_GETTOOLINFO, 0, (LPARAM)(LPTOOLINFO) &m_TI) )
  1006. ::SendMessage(m_hTTWnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  1007. }
  1008. else
  1009. {
  1010. m_bOwnTT = true;
  1011. CreateToolTip();
  1012. }
  1013. }
  1014. void CPopup::CreateToolTip()
  1015. {
  1016. if( m_hTTWnd != NULL )
  1017. return;
  1018. // CREATE A TOOLTIP WINDOW
  1019. m_hTTWnd = CreateWindowEx(
  1020. WS_EX_TOPMOST,
  1021. TOOLTIPS_CLASS,
  1022. NULL,
  1023. TTS_NOPREFIX | TTS_ALWAYSTIP,
  1024. CW_USEDEFAULT,
  1025. CW_USEDEFAULT,
  1026. CW_USEDEFAULT,
  1027. CW_USEDEFAULT,
  1028. NULL,
  1029. NULL,
  1030. NULL,
  1031. NULL
  1032. );
  1033. m_bOwnTT = true;
  1034. // SEND AN ADDTOOL MESSAGE TO THE TOOLTIP CONTROL WINDOW
  1035. ::SendMessage(m_hTTWnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  1036. }
  1037. void CPopup::SetTimeout( int timeout )
  1038. {
  1039. if( m_hTTWnd == NULL )
  1040. return;
  1041. ::SendMessage(m_hTTWnd, TTM_SETDELAYTIME, TTDT_AUTOMATIC, timeout);
  1042. }
  1043. void CPopup::SetPos( CPoint& pos )
  1044. {
  1045. m_Pos = pos;
  1046. }
  1047. void CPopup::SetPosInfo( bool bTop, bool bCenterY, bool bLeft, bool bCenterX )
  1048. {
  1049. m_bTop = bTop;
  1050. m_bCenterY = bCenterY;
  1051. m_bLeft = bLeft;
  1052. m_bCenterX = bCenterX;
  1053. }
  1054. void CPopup::AdjustPos( CPoint& pos )
  1055. {
  1056. CRect rel(0,0,0,0);
  1057. CRect rect(0,0,0,0);
  1058. // ::SendMessage(m_hTTWnd, TTM_ADJUSTRECT, TRUE, (LPARAM)&rect);
  1059. ::GetWindowRect(m_hTTWnd,&rect);
  1060. if( ::IsWindow(m_hWndPosRelativeTo) )
  1061. ::GetWindowRect(m_hWndPosRelativeTo, &rel);
  1062. // move the rect to the relative origin
  1063. rect.bottom = rect.Height() + rel.top;
  1064. rect.top = rel.top;
  1065. rect.right = rect.Width() + rel.left;
  1066. rect.left = rel.left;
  1067. // adjust the y position
  1068. rect.OffsetRect( 0, pos.y - (m_bCenterY? rect.Height()/2: (m_bTop? 0: rect.Height())) );
  1069. if( rect.bottom > m_ScreenMaxY )
  1070. rect.OffsetRect( 0, m_ScreenMaxY - rect.bottom );
  1071. // adjust the x position
  1072. rect.OffsetRect( pos.x - (m_bCenterX? rect.Width()/2: (m_bLeft? 0: rect.Width())), 0 );
  1073. if( rect.right > m_ScreenMaxX )
  1074. rect.OffsetRect( m_ScreenMaxX - rect.right, 0 );
  1075. pos.x = rect.left;
  1076. pos.y = rect.top;
  1077. }
  1078. void CPopup::SendToolTipText( CString text )
  1079. {
  1080. m_csToolTipText = text;
  1081. //Replace the tabs with spaces, the tooltip didn't like the \t s
  1082. text.Replace(_T("\t"), _T(" "));
  1083. m_TI.lpszText = (LPTSTR) (LPCTSTR) text;
  1084. // this allows \n and \r to be interpreted correctly
  1085. ::SendMessage(m_hTTWnd, TTM_SETMAXTIPWIDTH, 0, 500);
  1086. // set the text
  1087. ::SendMessage(m_hTTWnd, TTM_SETTOOLINFO, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  1088. }
  1089. void CPopup::Show( CString text, CPoint pos, bool bAdjustPos )
  1090. {
  1091. if( m_hTTWnd == NULL )
  1092. return;
  1093. m_csToolTipText = text;
  1094. if( !m_bIsShowing )
  1095. ::SendMessage(m_hTTWnd, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(-10000,-10000));
  1096. SendToolTipText( text );
  1097. ::SendMessage(m_hTTWnd, TTM_TRACKACTIVATE, true, (LPARAM)(LPTOOLINFO) &m_TI);
  1098. if( bAdjustPos )
  1099. AdjustPos(pos);
  1100. // set the position
  1101. ::SendMessage(m_hTTWnd, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(pos.x,pos.y));
  1102. // make sure the tooltip will be on top.
  1103. ::SetWindowPos( m_hTTWnd, m_hWndInsertAfter, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE );
  1104. m_bIsShowing = true;
  1105. }
  1106. void CPopup::Show( CString text )
  1107. {
  1108. m_csToolTipText = text;
  1109. Show( text, m_Pos );
  1110. }
  1111. void CPopup::AllowShow( CString text )
  1112. {
  1113. m_csToolTipText = text;
  1114. if( m_bAllowShow )
  1115. Show( text, m_Pos );
  1116. }
  1117. void CPopup::Hide()
  1118. {
  1119. if( m_hTTWnd == NULL )
  1120. return;
  1121. // deactivate if it is currently activated
  1122. ::SendMessage(m_hTTWnd, TTM_TRACKACTIVATE, FALSE, (LPARAM)(LPTOOLINFO) &m_TI);
  1123. m_bIsShowing = false;
  1124. }
  1125. /*------------------------------------------------------------------*\
  1126. ID based Globals
  1127. \*------------------------------------------------------------------*/
  1128. long NewGroupID(long lParentID, CString text)
  1129. {
  1130. long lID=0;
  1131. CTime time;
  1132. time = CTime::GetCurrentTime();
  1133. try
  1134. {
  1135. //sqlite doesn't like single quotes ' replace them with double ''
  1136. if(text.IsEmpty())
  1137. text = time.Format("NewGroup %y/%m/%d %H:%M:%S");
  1138. text.Replace(_T("'"), _T("''"));
  1139. CString cs;
  1140. cs.Format(_T("insert into Main values(NULL, %d, '%s', 0, %d, 0, 1, %d, '');"),
  1141. (long)time.GetTime(),
  1142. text,
  1143. (long)time.GetTime(),
  1144. lParentID);
  1145. theApp.m_db.execDML(cs);
  1146. lID = (long)theApp.m_db.lastRowId();
  1147. }
  1148. CATCH_SQLITE_EXCEPTION_AND_RETURN(0)
  1149. return lID;
  1150. }
  1151. // deletes the given item
  1152. BOOL DeleteID(long lID)
  1153. {
  1154. BOOL bRet = FALSE;
  1155. try
  1156. {
  1157. bool bCont = false;
  1158. bool bGroup = false;
  1159. {
  1160. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT bIsGroup FROM Main WHERE lId = %d"), lID);
  1161. bCont = !q.eof();
  1162. if(bCont)
  1163. {
  1164. bGroup = q.getIntField(_T("bIsGroup")) > 0;
  1165. }
  1166. }
  1167. if(bCont)
  1168. {
  1169. if(bGroup)
  1170. {
  1171. theApp.m_db.execDMLEx(_T("UPDATE Main SET lParentID = -1 WHERE lParentID = %d;"), lID);
  1172. }
  1173. //now is deleted from a trigger
  1174. //theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lParentID = %d;"), lID);
  1175. theApp.m_db.execDMLEx(_T("DELETE FROM Main WHERE lID = %d;"), lID);
  1176. bRet = TRUE;
  1177. }
  1178. theApp.OnDeleteID(lID);
  1179. }
  1180. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  1181. return bRet;
  1182. }
  1183. BOOL DeleteAllIDs()
  1184. {
  1185. try
  1186. {
  1187. theApp.m_db.execDML(_T("DELETE FROM Data;"));
  1188. theApp.m_db.execDML(_T("DELETE FROM Main;"));
  1189. }
  1190. CATCH_SQLITE_EXCEPTION
  1191. return TRUE;
  1192. }
  1193. BOOL DeleteFormats(long lParentID, ARRAY& formatIDs)
  1194. {
  1195. if(formatIDs.GetSize() <= 0)
  1196. return TRUE;
  1197. try
  1198. {
  1199. //Delete the requested data formats
  1200. int nCount = formatIDs.GetSize();
  1201. for(int i = 0; i < nCount; i++)
  1202. {
  1203. theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lID = %d;"), formatIDs[i]);
  1204. }
  1205. CClip clip;
  1206. if(clip.LoadFormats(lParentID))
  1207. {
  1208. DWORD CRC = clip.GenerateCRC();
  1209. //Update the main table with new size
  1210. theApp.m_db.execDMLEx(_T("UPDATE Main SET CRC = %d WHERE lID = %d"), CRC, lParentID);
  1211. }
  1212. }
  1213. CATCH_SQLITE_EXCEPTION
  1214. return TRUE;
  1215. }
  1216. BOOL EnsureWindowVisible(CRect *pcrRect)
  1217. {
  1218. int nMonitor = GetMonitorFromRect(pcrRect);
  1219. if(nMonitor < 0)
  1220. {
  1221. GetMonitorRect(0, pcrRect);
  1222. pcrRect->right = pcrRect->left + 300;
  1223. pcrRect->bottom = pcrRect->top + 300;
  1224. return TRUE;
  1225. }
  1226. CRect crMonitor;
  1227. GetMonitorRect(nMonitor, crMonitor);
  1228. //Validate the left
  1229. long lDiff = pcrRect->left - crMonitor.left;
  1230. if(lDiff < 0)
  1231. {
  1232. pcrRect->left += abs(lDiff);
  1233. pcrRect->right += abs(lDiff);
  1234. }
  1235. //Right side
  1236. lDiff = pcrRect->right - crMonitor.right;
  1237. if(lDiff > 0)
  1238. {
  1239. pcrRect->left -= abs(lDiff);
  1240. pcrRect->right -= abs(lDiff);
  1241. }
  1242. //Top
  1243. lDiff = pcrRect->top - crMonitor.top;
  1244. if(lDiff < 0)
  1245. {
  1246. pcrRect->top += abs(lDiff);
  1247. pcrRect->bottom += abs(lDiff);
  1248. }
  1249. //Bottom
  1250. lDiff = pcrRect->bottom - crMonitor.bottom;
  1251. if(lDiff > 0)
  1252. {
  1253. pcrRect->top -= abs(lDiff);
  1254. pcrRect->bottom -= abs(lDiff);
  1255. }
  1256. return TRUE;
  1257. }
  1258. __int64 GetLastWriteTime(const CString &csFile)
  1259. {
  1260. __int64 nLastWrite = 0;
  1261. CFileFind finder;
  1262. BOOL bResult = finder.FindFile(csFile);
  1263. if (bResult)
  1264. {
  1265. finder.FindNextFile();
  1266. FILETIME ft;
  1267. finder.GetLastWriteTime(&ft);
  1268. memcpy(&nLastWrite, &ft, sizeof(ft));
  1269. }
  1270. return nLastWrite;
  1271. }