Misc.cpp 32 KB

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