Misc.cpp 32 KB

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