Misc.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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. if( GetKeyState(VK_LWIN) & 0x8000 )
  887. m |= HOTKEYF_EXT;
  888. if( GetKeyState(VK_RWIN) & 0x8000 )
  889. m |= HOTKEYF_EXT;
  890. return m;
  891. }
  892. /*------------------------------------------------------------------*\
  893. CTokenizer - Tokenizes a string using given delimiters
  894. \*------------------------------------------------------------------*/
  895. CTokenizer::CTokenizer(const CString& cs, const CString& csDelim):
  896. m_cs(cs),
  897. m_nCurPos(0)
  898. {
  899. SetDelimiters(csDelim);
  900. }
  901. void CTokenizer::SetDelimiters(const CString& csDelim)
  902. {
  903. for(int i = 0; i < csDelim.GetLength(); ++i)
  904. m_delim.Add(csDelim[i]);
  905. m_delim.SortAscending();
  906. }
  907. bool CTokenizer::Next(CString& cs)
  908. {
  909. cs.Empty();
  910. int len = m_cs.GetLength();
  911. while (m_nCurPos < len && m_delim.Find(m_cs[m_nCurPos]))
  912. ++ m_nCurPos;
  913. if (m_nCurPos >= len)
  914. return false;
  915. int nStartPos = m_nCurPos;
  916. while (m_nCurPos < len && !m_delim.Find(m_cs[m_nCurPos]))
  917. ++ m_nCurPos;
  918. cs = m_cs.Mid(nStartPos, m_nCurPos - nStartPos);
  919. return true;
  920. }
  921. CString CTokenizer::Tail()
  922. {
  923. int len = m_cs.GetLength();
  924. int nCurPos = m_nCurPos;
  925. while(nCurPos < len && m_delim.Find(m_cs[nCurPos]))
  926. ++nCurPos;
  927. CString csResult;
  928. if(nCurPos < len)
  929. csResult = m_cs.Mid(nCurPos);
  930. return csResult;
  931. }
  932. /*------------------------------------------------------------------*\
  933. Global ToolTip Manual Control Functions
  934. \*------------------------------------------------------------------*/
  935. void InitToolInfo( TOOLINFO& ti )
  936. {
  937. // INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE
  938. ti.cbSize = sizeof(TOOLINFO);
  939. ti.uFlags = TTF_ABSOLUTE | TTF_TRACK;
  940. ti.hwnd = NULL;
  941. ti.hinst = NULL;
  942. ti.uId = 0; // CPopup only uses uid 0
  943. ti.lpszText = NULL;
  944. // ToolTip control will cover the whole window
  945. ti.rect.left = 0;
  946. ti.rect.top = 0;
  947. ti.rect.right = 0;
  948. ti.rect.bottom = 0;
  949. }
  950. /*------------------------------------------------------------------*\
  951. CPopup - a tooltip that pops up manually (when Show is called).
  952. - technique learned from codeproject "ToolTipZen" by "Zarembo Maxim"
  953. \*------------------------------------------------------------------*/
  954. CPopup::CPopup()
  955. {
  956. Init();
  957. }
  958. // HWND_TOP
  959. CPopup::CPopup( int x, int y, HWND hWndPosRelativeTo, HWND hWndInsertAfter )
  960. {
  961. Init();
  962. m_hWndPosRelativeTo = hWndPosRelativeTo;
  963. m_hWndInsertAfter = hWndInsertAfter;
  964. SetPos( CPoint(x,y) );
  965. }
  966. CPopup::~CPopup()
  967. {
  968. Hide();
  969. if( m_bOwnTT && ::IsWindow(m_hTTWnd) )
  970. ::DestroyWindow( m_hTTWnd );
  971. }
  972. void CPopup::Init()
  973. {
  974. // initialize variables
  975. m_bOwnTT = false;
  976. m_hTTWnd = NULL;
  977. m_bIsShowing = false;
  978. m_bAllowShow = true; // used by AllowShow()
  979. m_Pos.x = m_Pos.y = 0;
  980. m_bTop = true;
  981. m_bLeft = true;
  982. m_bCenterX = false;
  983. m_bCenterY = false;
  984. m_hWndPosRelativeTo = NULL;
  985. RECT rcScreen;
  986. GetMonitorRect(-1, &rcScreen);
  987. m_ScreenMaxX = rcScreen.right;
  988. m_ScreenMaxY = rcScreen.bottom;
  989. m_hWndInsertAfter = HWND_TOP; //HWND_TOPMOST
  990. SetTTWnd();
  991. }
  992. void CPopup::SetTTWnd( HWND hTTWnd, TOOLINFO* pTI )
  993. {
  994. if( pTI )
  995. m_TI = *pTI;
  996. else
  997. InitToolInfo( m_TI );
  998. if( m_bOwnTT && ::IsWindow(m_hTTWnd) )
  999. {
  1000. if( !::IsWindow(hTTWnd) )
  1001. return; // we would have to recreate the one that already exists
  1002. ::DestroyWindow( m_hTTWnd );
  1003. }
  1004. m_hTTWnd = hTTWnd;
  1005. if( ::IsWindow(m_hTTWnd) )
  1006. {
  1007. m_bOwnTT = false;
  1008. // if our uid tooltip already exists, get the data, else add it.
  1009. if( ! ::SendMessage(m_hTTWnd, TTM_GETTOOLINFO, 0, (LPARAM)(LPTOOLINFO) &m_TI) )
  1010. ::SendMessage(m_hTTWnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  1011. }
  1012. else
  1013. {
  1014. m_bOwnTT = true;
  1015. CreateToolTip();
  1016. }
  1017. }
  1018. void CPopup::CreateToolTip()
  1019. {
  1020. if( m_hTTWnd != NULL )
  1021. return;
  1022. // CREATE A TOOLTIP WINDOW
  1023. m_hTTWnd = CreateWindowEx(
  1024. WS_EX_TOPMOST,
  1025. TOOLTIPS_CLASS,
  1026. NULL,
  1027. TTS_NOPREFIX | TTS_ALWAYSTIP,
  1028. CW_USEDEFAULT,
  1029. CW_USEDEFAULT,
  1030. CW_USEDEFAULT,
  1031. CW_USEDEFAULT,
  1032. NULL,
  1033. NULL,
  1034. NULL,
  1035. NULL
  1036. );
  1037. m_bOwnTT = true;
  1038. // SEND AN ADDTOOL MESSAGE TO THE TOOLTIP CONTROL WINDOW
  1039. ::SendMessage(m_hTTWnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  1040. }
  1041. void CPopup::SetTimeout( int timeout )
  1042. {
  1043. if( m_hTTWnd == NULL )
  1044. return;
  1045. ::SendMessage(m_hTTWnd, TTM_SETDELAYTIME, TTDT_AUTOMATIC, timeout);
  1046. }
  1047. void CPopup::SetPos( CPoint& pos )
  1048. {
  1049. m_Pos = pos;
  1050. }
  1051. void CPopup::SetPosInfo( bool bTop, bool bCenterY, bool bLeft, bool bCenterX )
  1052. {
  1053. m_bTop = bTop;
  1054. m_bCenterY = bCenterY;
  1055. m_bLeft = bLeft;
  1056. m_bCenterX = bCenterX;
  1057. }
  1058. void CPopup::AdjustPos( CPoint& pos )
  1059. {
  1060. CRect rel(0,0,0,0);
  1061. CRect rect(0,0,0,0);
  1062. // ::SendMessage(m_hTTWnd, TTM_ADJUSTRECT, TRUE, (LPARAM)&rect);
  1063. ::GetWindowRect(m_hTTWnd,&rect);
  1064. if( ::IsWindow(m_hWndPosRelativeTo) )
  1065. ::GetWindowRect(m_hWndPosRelativeTo, &rel);
  1066. // move the rect to the relative origin
  1067. rect.bottom = rect.Height() + rel.top;
  1068. rect.top = rel.top;
  1069. rect.right = rect.Width() + rel.left;
  1070. rect.left = rel.left;
  1071. // adjust the y position
  1072. rect.OffsetRect( 0, pos.y - (m_bCenterY? rect.Height()/2: (m_bTop? 0: rect.Height())) );
  1073. if( rect.bottom > m_ScreenMaxY )
  1074. rect.OffsetRect( 0, m_ScreenMaxY - rect.bottom );
  1075. // adjust the x position
  1076. rect.OffsetRect( pos.x - (m_bCenterX? rect.Width()/2: (m_bLeft? 0: rect.Width())), 0 );
  1077. if( rect.right > m_ScreenMaxX )
  1078. rect.OffsetRect( m_ScreenMaxX - rect.right, 0 );
  1079. pos.x = rect.left;
  1080. pos.y = rect.top;
  1081. }
  1082. void CPopup::SendToolTipText( CString text )
  1083. {
  1084. m_csToolTipText = text;
  1085. //Replace the tabs with spaces, the tooltip didn't like the \t s
  1086. text.Replace(_T("\t"), _T(" "));
  1087. m_TI.lpszText = (LPTSTR) (LPCTSTR) text;
  1088. // this allows \n and \r to be interpreted correctly
  1089. ::SendMessage(m_hTTWnd, TTM_SETMAXTIPWIDTH, 0, 500);
  1090. // set the text
  1091. ::SendMessage(m_hTTWnd, TTM_SETTOOLINFO, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  1092. }
  1093. void CPopup::Show( CString text, CPoint pos, bool bAdjustPos )
  1094. {
  1095. if( m_hTTWnd == NULL )
  1096. return;
  1097. m_csToolTipText = text;
  1098. if( !m_bIsShowing )
  1099. ::SendMessage(m_hTTWnd, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(-10000,-10000));
  1100. SendToolTipText( text );
  1101. ::SendMessage(m_hTTWnd, TTM_TRACKACTIVATE, true, (LPARAM)(LPTOOLINFO) &m_TI);
  1102. if( bAdjustPos )
  1103. AdjustPos(pos);
  1104. // set the position
  1105. ::SendMessage(m_hTTWnd, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(pos.x,pos.y));
  1106. // make sure the tooltip will be on top.
  1107. ::SetWindowPos( m_hTTWnd, m_hWndInsertAfter, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE );
  1108. m_bIsShowing = true;
  1109. }
  1110. void CPopup::Show( CString text )
  1111. {
  1112. m_csToolTipText = text;
  1113. Show( text, m_Pos );
  1114. }
  1115. void CPopup::AllowShow( CString text )
  1116. {
  1117. m_csToolTipText = text;
  1118. if( m_bAllowShow )
  1119. Show( text, m_Pos );
  1120. }
  1121. void CPopup::Hide()
  1122. {
  1123. if( m_hTTWnd == NULL )
  1124. return;
  1125. // deactivate if it is currently activated
  1126. ::SendMessage(m_hTTWnd, TTM_TRACKACTIVATE, FALSE, (LPARAM)(LPTOOLINFO) &m_TI);
  1127. m_bIsShowing = false;
  1128. }
  1129. /*------------------------------------------------------------------*\
  1130. ID based Globals
  1131. \*------------------------------------------------------------------*/
  1132. long NewGroupID(long lParentID, CString text)
  1133. {
  1134. long lID=0;
  1135. CTime time;
  1136. time = CTime::GetCurrentTime();
  1137. try
  1138. {
  1139. //sqlite doesn't like single quotes ' replace them with double ''
  1140. if(text.IsEmpty())
  1141. text = time.Format("NewGroup %y/%m/%d %H:%M:%S");
  1142. text.Replace(_T("'"), _T("''"));
  1143. CString cs;
  1144. cs.Format(_T("insert into Main values(NULL, %d, '%s', 0, %d, 0, 1, %d, '');"),
  1145. (long)time.GetTime(),
  1146. text,
  1147. (long)time.GetTime(),
  1148. lParentID);
  1149. theApp.m_db.execDML(cs);
  1150. lID = (long)theApp.m_db.lastRowId();
  1151. }
  1152. CATCH_SQLITE_EXCEPTION_AND_RETURN(0)
  1153. return lID;
  1154. }
  1155. // deletes the given item
  1156. BOOL DeleteID(long lID)
  1157. {
  1158. BOOL bRet = FALSE;
  1159. try
  1160. {
  1161. bool bCont = false;
  1162. bool bGroup = false;
  1163. {
  1164. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT bIsGroup FROM Main WHERE lId = %d"), lID);
  1165. bCont = !q.eof();
  1166. if(bCont)
  1167. {
  1168. bGroup = q.getIntField(_T("bIsGroup")) > 0;
  1169. }
  1170. }
  1171. if(bCont)
  1172. {
  1173. if(bGroup)
  1174. {
  1175. theApp.m_db.execDMLEx(_T("UPDATE Main SET lParentID = -1 WHERE lParentID = %d;"), lID);
  1176. }
  1177. //now is deleted from a trigger
  1178. //theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lParentID = %d;"), lID);
  1179. theApp.m_db.execDMLEx(_T("DELETE FROM Main WHERE lID = %d;"), lID);
  1180. bRet = TRUE;
  1181. }
  1182. theApp.OnDeleteID(lID);
  1183. }
  1184. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  1185. return bRet;
  1186. }
  1187. BOOL DeleteAllIDs()
  1188. {
  1189. try
  1190. {
  1191. theApp.m_db.execDML(_T("DELETE FROM Data;"));
  1192. theApp.m_db.execDML(_T("DELETE FROM Main;"));
  1193. }
  1194. CATCH_SQLITE_EXCEPTION
  1195. return TRUE;
  1196. }
  1197. BOOL DeleteFormats(long lParentID, ARRAY& formatIDs)
  1198. {
  1199. if(formatIDs.GetSize() <= 0)
  1200. return TRUE;
  1201. try
  1202. {
  1203. //Delete the requested data formats
  1204. int nCount = formatIDs.GetSize();
  1205. for(int i = 0; i < nCount; i++)
  1206. {
  1207. theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lID = %d;"), formatIDs[i]);
  1208. }
  1209. CClip clip;
  1210. if(clip.LoadFormats(lParentID))
  1211. {
  1212. DWORD CRC = clip.GenerateCRC();
  1213. //Update the main table with new size
  1214. theApp.m_db.execDMLEx(_T("UPDATE Main SET CRC = %d WHERE lID = %d"), CRC, lParentID);
  1215. }
  1216. }
  1217. CATCH_SQLITE_EXCEPTION
  1218. return TRUE;
  1219. }
  1220. BOOL EnsureWindowVisible(CRect *pcrRect)
  1221. {
  1222. int nMonitor = GetMonitorFromRect(pcrRect);
  1223. if(nMonitor < 0)
  1224. {
  1225. GetMonitorRect(0, pcrRect);
  1226. pcrRect->right = pcrRect->left + 300;
  1227. pcrRect->bottom = pcrRect->top + 300;
  1228. return TRUE;
  1229. }
  1230. CRect crMonitor;
  1231. GetMonitorRect(nMonitor, crMonitor);
  1232. //Validate the left
  1233. long lDiff = pcrRect->left - crMonitor.left;
  1234. if(lDiff < 0)
  1235. {
  1236. pcrRect->left += abs(lDiff);
  1237. pcrRect->right += abs(lDiff);
  1238. }
  1239. //Right side
  1240. lDiff = pcrRect->right - crMonitor.right;
  1241. if(lDiff > 0)
  1242. {
  1243. pcrRect->left -= abs(lDiff);
  1244. pcrRect->right -= abs(lDiff);
  1245. }
  1246. //Top
  1247. lDiff = pcrRect->top - crMonitor.top;
  1248. if(lDiff < 0)
  1249. {
  1250. pcrRect->top += abs(lDiff);
  1251. pcrRect->bottom += abs(lDiff);
  1252. }
  1253. //Bottom
  1254. lDiff = pcrRect->bottom - crMonitor.bottom;
  1255. if(lDiff > 0)
  1256. {
  1257. pcrRect->top -= abs(lDiff);
  1258. pcrRect->bottom -= abs(lDiff);
  1259. }
  1260. return TRUE;
  1261. }
  1262. __int64 GetLastWriteTime(const CString &csFile)
  1263. {
  1264. __int64 nLastWrite = 0;
  1265. CFileFind finder;
  1266. BOOL bResult = finder.FindFile(csFile);
  1267. if (bResult)
  1268. {
  1269. finder.FindNextFile();
  1270. FILETIME ft;
  1271. finder.GetLastWriteTime(&ft);
  1272. memcpy(&nLastWrite, &ft, sizeof(ft));
  1273. }
  1274. return nLastWrite;
  1275. }