Misc.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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. long DoOptions(CWnd *pParent)
  268. {
  269. //Don't let it open up more than once
  270. if(theApp.m_bShowingOptions)
  271. return FALSE;
  272. theApp.m_bShowingOptions = true;
  273. COptionsSheet Sheet(_T("Copy Pro Options"), pParent);
  274. int nRet = Sheet.DoModal();
  275. theApp.m_bShowingOptions = false;
  276. return nRet;
  277. }
  278. //Do not change these these are stored in the database
  279. CLIPFORMAT GetFormatID(LPCTSTR cbName)
  280. {
  281. if(STRCMP(cbName, _T("CF_TEXT")) == 0)
  282. return CF_TEXT;
  283. else if(STRCMP(cbName, _T("CF_METAFILEPICT")) == 0)
  284. return CF_METAFILEPICT;
  285. else if(STRCMP(cbName, _T("CF_SYLK")) == 0)
  286. return CF_SYLK;
  287. else if(STRCMP(cbName, _T("CF_DIF")) == 0)
  288. return CF_DIF;
  289. else if(STRCMP(cbName, _T("CF_TIFF")) == 0)
  290. return CF_TIFF;
  291. else if(STRCMP(cbName, _T("CF_OEMTEXT")) == 0)
  292. return CF_OEMTEXT;
  293. else if(STRCMP(cbName, _T("CF_DIB")) == 0)
  294. return CF_DIB;
  295. else if(STRCMP(cbName, _T("CF_PALETTE")) == 0)
  296. return CF_PALETTE;
  297. else if(STRCMP(cbName, _T("CF_PENDATA")) == 0)
  298. return CF_PENDATA;
  299. else if(STRCMP(cbName, _T("CF_RIFF")) == 0)
  300. return CF_RIFF;
  301. else if(STRCMP(cbName, _T("CF_WAVE")) == 0)
  302. return CF_WAVE;
  303. else if(STRCMP(cbName, _T("CF_UNICODETEXT")) == 0)
  304. return CF_UNICODETEXT;
  305. else if(STRCMP(cbName, _T("CF_ENHMETAFILE")) == 0)
  306. return CF_ENHMETAFILE;
  307. else if(STRCMP(cbName, _T("CF_HDROP")) == 0)
  308. return CF_HDROP;
  309. else if(STRCMP(cbName, _T("CF_LOCALE")) == 0)
  310. return CF_LOCALE;
  311. else if(STRCMP(cbName, _T("CF_OWNERDISPLAY")) == 0)
  312. return CF_OWNERDISPLAY;
  313. else if(STRCMP(cbName, _T("CF_DSPTEXT")) == 0)
  314. return CF_DSPTEXT;
  315. else if(STRCMP(cbName, _T("CF_DSPBITMAP")) == 0)
  316. return CF_DSPBITMAP;
  317. else if(STRCMP(cbName, _T("CF_DSPMETAFILEPICT")) == 0)
  318. return CF_DSPMETAFILEPICT;
  319. else if(STRCMP(cbName, _T("CF_DSPENHMETAFILE")) == 0)
  320. return CF_DSPENHMETAFILE;
  321. return ::RegisterClipboardFormat(cbName);
  322. }
  323. //Do not change these these are stored in the database
  324. CString GetFormatName(CLIPFORMAT cbType)
  325. {
  326. switch(cbType)
  327. {
  328. case CF_TEXT:
  329. return _T("CF_TEXT");
  330. case CF_BITMAP:
  331. return _T("CF_BITMAP");
  332. case CF_METAFILEPICT:
  333. return _T("CF_METAFILEPICT");
  334. case CF_SYLK:
  335. return _T("CF_SYLK");
  336. case CF_DIF:
  337. return _T("CF_DIF");
  338. case CF_TIFF:
  339. return _T("CF_TIFF");
  340. case CF_OEMTEXT:
  341. return _T("CF_OEMTEXT");
  342. case CF_DIB:
  343. return _T("CF_DIB");
  344. case CF_PALETTE:
  345. return _T("CF_PALETTE");
  346. case CF_PENDATA:
  347. return _T("CF_PENDATA");
  348. case CF_RIFF:
  349. return _T("CF_RIFF");
  350. case CF_WAVE:
  351. return _T("CF_WAVE");
  352. case CF_UNICODETEXT:
  353. return _T("CF_UNICODETEXT");
  354. case CF_ENHMETAFILE:
  355. return _T("CF_ENHMETAFILE");
  356. case CF_HDROP:
  357. return _T("CF_HDROP");
  358. case CF_LOCALE:
  359. return _T("CF_LOCALE");
  360. case CF_OWNERDISPLAY:
  361. return _T("CF_OWNERDISPLAY");
  362. case CF_DSPTEXT:
  363. return _T("CF_DSPTEXT");
  364. case CF_DSPBITMAP:
  365. return _T("CF_DSPBITMAP");
  366. case CF_DSPMETAFILEPICT:
  367. return _T("CF_DSPMETAFILEPICT");
  368. case CF_DSPENHMETAFILE:
  369. return _T("CF_DSPENHMETAFILE");
  370. default:
  371. //Not a default type get the name from the clipboard
  372. if (cbType != 0)
  373. {
  374. TCHAR szFormat[256];
  375. GetClipboardFormatName(cbType, szFormat, 256);
  376. return szFormat;
  377. }
  378. break;
  379. }
  380. return "ERROR";
  381. }
  382. CString GetFilePath(CString csFileName)
  383. {
  384. long lSlash = csFileName.ReverseFind('\\');
  385. if(lSlash > -1)
  386. {
  387. csFileName = csFileName.Left(lSlash + 1);
  388. }
  389. return csFileName;
  390. }
  391. CString GetFileName(CString csFileName)
  392. {
  393. long lSlash = csFileName.ReverseFind('\\');
  394. if(lSlash > -1)
  395. {
  396. csFileName = csFileName.Right(csFileName.GetLength() - lSlash - 1);
  397. }
  398. return csFileName;
  399. }
  400. /*------------------------------------------------------------------*\
  401. CHotKey - a single system-wide hotkey
  402. \*------------------------------------------------------------------*/
  403. CHotKey::CHotKey( CString name, DWORD defKey, bool bUnregOnShowDitto )
  404. : m_Name(name), m_bIsRegistered(false), m_bUnRegisterOnShowDitto(bUnregOnShowDitto)
  405. {
  406. m_Atom = ::GlobalAddAtom( m_Name );
  407. ASSERT( m_Atom );
  408. m_Key = (DWORD) g_Opt.GetProfileLong( m_Name, (long) defKey );
  409. g_HotKeys.Add( this );
  410. }
  411. CHotKey::~CHotKey()
  412. {
  413. Unregister();
  414. }
  415. void CHotKey::SetKey( DWORD key, bool bSave )
  416. {
  417. if( m_Key == key )
  418. return;
  419. if( m_bIsRegistered )
  420. Unregister();
  421. m_Key = key;
  422. if( bSave )
  423. SaveKey();
  424. }
  425. void CHotKey::LoadKey()
  426. {
  427. SetKey( (DWORD) g_Opt.GetProfileLong( m_Name, 0 ) );
  428. }
  429. bool CHotKey::SaveKey()
  430. {
  431. return g_Opt.SetProfileLong( m_Name, (long) m_Key ) != FALSE;
  432. }
  433. // CString GetKeyAsText();
  434. // void SetKeyFromText( CString text );
  435. BOOL CHotKey::ValidateHotKey(DWORD dwHotKey)
  436. {
  437. ATOM id = ::GlobalAddAtom(_T("HK_VALIDATE"));
  438. BOOL bResult = ::RegisterHotKey( g_HotKeys.m_hWnd,
  439. id,
  440. GetModifier(dwHotKey),
  441. LOBYTE(dwHotKey) );
  442. if(bResult)
  443. ::UnregisterHotKey(g_HotKeys.m_hWnd, id);
  444. ::GlobalDeleteAtom(id);
  445. return bResult;
  446. }
  447. void CHotKey::CopyFromCtrl(CHotKeyCtrl& ctrl, HWND hParent, int nWindowsCBID)
  448. {
  449. long lHotKey = ctrl.GetHotKey();
  450. short sKeyKode = LOBYTE(lHotKey);
  451. short sModifers = HIBYTE(lHotKey);
  452. if(lHotKey && ::IsDlgButtonChecked(hParent, nWindowsCBID))
  453. {
  454. sModifers |= HOTKEYF_EXT;
  455. }
  456. SetKey(MAKEWORD(sKeyKode, sModifers));
  457. }
  458. void CHotKey::CopyToCtrl(CHotKeyCtrl& ctrl, HWND hParent, int nWindowsCBID)
  459. {
  460. long lModifiers = HIBYTE(m_Key);
  461. ctrl.SetHotKey(LOBYTE(m_Key), (WORD)lModifiers);
  462. if(lModifiers & HOTKEYF_EXT)
  463. {
  464. ::CheckDlgButton(hParent, nWindowsCBID, BST_CHECKED);
  465. }
  466. }
  467. UINT CHotKey::GetModifier(DWORD dwHotKey)
  468. {
  469. UINT uMod = 0;
  470. if( HIBYTE(dwHotKey) & HOTKEYF_SHIFT ) uMod |= MOD_SHIFT;
  471. if( HIBYTE(dwHotKey) & HOTKEYF_CONTROL ) uMod |= MOD_CONTROL;
  472. if( HIBYTE(dwHotKey) & HOTKEYF_ALT ) uMod |= MOD_ALT;
  473. if( HIBYTE(dwHotKey) & HOTKEYF_EXT ) uMod |= MOD_WIN;
  474. return uMod;
  475. }
  476. bool CHotKey::Register()
  477. {
  478. if(m_Key)
  479. {
  480. if(m_bIsRegistered == false)
  481. {
  482. ASSERT(g_HotKeys.m_hWnd);
  483. m_bIsRegistered = ::RegisterHotKey(g_HotKeys.m_hWnd,
  484. m_Atom,
  485. GetModifier(),
  486. LOBYTE(m_Key) ) == TRUE;
  487. }
  488. }
  489. else
  490. m_bIsRegistered = true;
  491. return m_bIsRegistered;
  492. }
  493. bool CHotKey::Unregister(bool bOnShowingDitto)
  494. {
  495. if(!m_bIsRegistered)
  496. return true;
  497. if(bOnShowingDitto)
  498. {
  499. if(m_bUnRegisterOnShowDitto == false)
  500. return true;
  501. }
  502. if(m_Key)
  503. {
  504. ASSERT(g_HotKeys.m_hWnd);
  505. if(::UnregisterHotKey( g_HotKeys.m_hWnd, m_Atom))
  506. {
  507. m_bIsRegistered = false;
  508. return true;
  509. }
  510. else
  511. {
  512. Log(_T("Unregister FAILED!"));
  513. ASSERT(0);
  514. }
  515. }
  516. else
  517. {
  518. m_bIsRegistered = false;
  519. return true;
  520. }
  521. return false;
  522. }
  523. /*------------------------------------------------------------------*\
  524. CHotKeys - Manages system-wide hotkeys
  525. \*------------------------------------------------------------------*/
  526. CHotKeys g_HotKeys;
  527. CHotKeys::CHotKeys() : m_hWnd(NULL) {}
  528. CHotKeys::~CHotKeys()
  529. {
  530. CHotKey* pHotKey;
  531. int count = GetSize();
  532. for(int i=0; i < count; i++)
  533. {
  534. pHotKey = ElementAt(i);
  535. if(pHotKey)
  536. delete pHotKey;
  537. }
  538. }
  539. int CHotKeys::Find( CHotKey* pHotKey )
  540. {
  541. int count = GetSize();
  542. for(int i=0; i < count; i++)
  543. {
  544. if( pHotKey == ElementAt(i) )
  545. return i;
  546. }
  547. return -1;
  548. }
  549. bool CHotKeys::Remove( CHotKey* pHotKey )
  550. {
  551. int i = Find(pHotKey);
  552. if(i >= 0)
  553. {
  554. RemoveAt(i);
  555. return true;
  556. }
  557. return false;
  558. }
  559. void CHotKeys::LoadAllKeys()
  560. {
  561. int count = GetSize();
  562. for(int i=0; i < count; i++)
  563. ElementAt(i)->LoadKey();
  564. }
  565. void CHotKeys::SaveAllKeys()
  566. {
  567. int count = GetSize();
  568. for(int i=0; i < count; i++)
  569. ElementAt(i)->SaveKey();
  570. }
  571. void CHotKeys::RegisterAll(bool bMsgOnError)
  572. {
  573. CString str;
  574. CHotKey* pHotKey;
  575. int count = GetSize();
  576. for(int i = 0; i < count; i++)
  577. {
  578. pHotKey = ElementAt(i);
  579. if(!pHotKey->Register())
  580. {
  581. str = "Error Registering ";
  582. str += pHotKey->GetName();
  583. Log(str);
  584. if(bMsgOnError)
  585. AfxMessageBox(str);
  586. }
  587. }
  588. }
  589. void CHotKeys::UnregisterAll(bool bMsgOnError, bool bOnShowDitto)
  590. {
  591. CString str;
  592. CHotKey* pHotKey;
  593. int count = GetSize();
  594. for(int i = 0; i < count; i++)
  595. {
  596. pHotKey = ElementAt(i);
  597. if(!pHotKey->Unregister(bOnShowDitto))
  598. {
  599. str = "Error Unregistering ";
  600. str += pHotKey->GetName();
  601. Log(str);
  602. if(bMsgOnError)
  603. AfxMessageBox(str);
  604. }
  605. }
  606. }
  607. void CHotKeys::GetKeys(ARRAY& keys)
  608. {
  609. int count = GetSize();
  610. keys.SetSize(count);
  611. for(int i=0; i < count; i++)
  612. keys[i] = ElementAt(i)->GetKey();
  613. }
  614. // caution! this alters hotkeys based upon corresponding indexes
  615. void CHotKeys::SetKeys(ARRAY& keys, bool bSave)
  616. {
  617. int count = GetSize();
  618. ASSERT(count == keys.GetSize());
  619. for(int i=0; i < count; i++)
  620. ElementAt(i)->SetKey(keys[i], bSave);
  621. }
  622. bool CHotKeys::FindFirstConflict(ARRAY& keys, int* pX, int* pY)
  623. {
  624. bool bConflict = false;
  625. int i, j;
  626. int count = keys.GetSize();
  627. DWORD key;
  628. for(i = 0; i < count && !bConflict; i++)
  629. {
  630. key = keys.ElementAt(i);
  631. // only check valid keys
  632. if(key == 0)
  633. continue;
  634. // scan the array for a duplicate
  635. for(j = i+1; j < count; j++ )
  636. {
  637. if(keys.ElementAt(j) == key)
  638. {
  639. bConflict = true;
  640. break;
  641. }
  642. }
  643. }
  644. if(bConflict)
  645. {
  646. if(pX)
  647. *pX = i-1;
  648. if(pY)
  649. *pY = j;
  650. }
  651. return bConflict;
  652. }
  653. // if true, pX and pY (if valid) are set to the indexes of the conflicting hotkeys.
  654. bool CHotKeys::FindFirstConflict(int* pX, int* pY)
  655. {
  656. ARRAY keys;
  657. GetKeys(keys);
  658. return FindFirstConflict(keys, pX, pY);
  659. }
  660. /****************************************************************************************************
  661. BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
  662. ***************************************************************************************************/
  663. typedef struct
  664. {
  665. long lFlags; // Flags
  666. LPRECT pVirtualRect; // Ptr to rect that receives the results, or the src of the monitor search method
  667. int iMonitor; // Ndx to the mointor to look at, -1 for all, -or- result of the monitor search method
  668. int nMonitorCount; // Total number of monitors found, -1 for monitor search method
  669. } MONITOR_ENUM_PARAM;
  670. #define MONITOR_SEARCH_METOHD 0x00000001
  671. BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
  672. {
  673. // Typecast param
  674. MONITOR_ENUM_PARAM* pParam = (MONITOR_ENUM_PARAM*)dwData;
  675. if(pParam)
  676. {
  677. // If a dest rect was passed
  678. if(pParam->pVirtualRect)
  679. {
  680. // If MONITOR_SEARCH_METOHD then we are being asked for the index of the monitor
  681. // that the rect falls inside of
  682. if(pParam->lFlags & MONITOR_SEARCH_METOHD)
  683. {
  684. if( (pParam->pVirtualRect->right < lprcMonitor->left) ||
  685. (pParam->pVirtualRect->left > lprcMonitor->right) ||
  686. (pParam->pVirtualRect->bottom < lprcMonitor->top) ||
  687. (pParam->pVirtualRect->top > lprcMonitor->bottom))
  688. {
  689. // Nothing
  690. }
  691. else
  692. {
  693. // This is the one
  694. pParam->iMonitor = pParam->nMonitorCount;
  695. // Stop the enumeration
  696. return FALSE;
  697. }
  698. }
  699. else
  700. {
  701. if(pParam->iMonitor == pParam->nMonitorCount)
  702. {
  703. *pParam->pVirtualRect = *lprcMonitor;
  704. }
  705. else
  706. if(pParam->iMonitor == -1)
  707. {
  708. pParam->pVirtualRect->left = min(pParam->pVirtualRect->left, lprcMonitor->left);
  709. pParam->pVirtualRect->top = min(pParam->pVirtualRect->top, lprcMonitor->top);
  710. pParam->pVirtualRect->right = max(pParam->pVirtualRect->right, lprcMonitor->right);
  711. pParam->pVirtualRect->bottom = max(pParam->pVirtualRect->bottom, lprcMonitor->bottom);
  712. }
  713. }
  714. }
  715. // Up the count if necessary
  716. pParam->nMonitorCount++;
  717. }
  718. return TRUE;
  719. }
  720. int GetScreenWidth(void)
  721. {
  722. OSVERSIONINFO OS_Version_Info;
  723. DWORD dwPlatform = 0;
  724. if(GetVersionEx(&OS_Version_Info) != 0)
  725. {
  726. dwPlatform = OS_Version_Info.dwPlatformId;
  727. }
  728. if(dwPlatform == VER_PLATFORM_WIN32_NT)
  729. {
  730. int width, height;
  731. width = GetSystemMetrics(SM_CXSCREEN);
  732. height = GetSystemMetrics(SM_CYSCREEN);
  733. switch(width)
  734. {
  735. default:
  736. case 640:
  737. case 800:
  738. case 1024:
  739. return(width);
  740. case 1280:
  741. if(height == 480)
  742. {
  743. return(width / 2);
  744. }
  745. return(width);
  746. case 1600:
  747. if(height == 600)
  748. {
  749. return(width / 2);
  750. }
  751. return(width);
  752. case 2048:
  753. if(height == 768)
  754. {
  755. return(width / 2);
  756. }
  757. return(width);
  758. }
  759. }
  760. else
  761. {
  762. return(GetSystemMetrics(SM_CXSCREEN));
  763. }
  764. }
  765. int GetScreenHeight(void)
  766. {
  767. OSVERSIONINFO OS_Version_Info;
  768. DWORD dwPlatform = 0;
  769. if(GetVersionEx(&OS_Version_Info) != 0)
  770. {
  771. dwPlatform = OS_Version_Info.dwPlatformId;
  772. }
  773. if(dwPlatform == VER_PLATFORM_WIN32_NT)
  774. {
  775. int width, height;
  776. width = GetSystemMetrics(SM_CXSCREEN);
  777. height = GetSystemMetrics(SM_CYSCREEN);
  778. switch(height)
  779. {
  780. default:
  781. case 480:
  782. case 600:
  783. case 768:
  784. return(height);
  785. case 960:
  786. if(width == 640)
  787. {
  788. return(height / 2);
  789. }
  790. return(height);
  791. case 1200:
  792. if(width == 800)
  793. {
  794. return(height / 2);
  795. }
  796. return(height);
  797. case 1536:
  798. if(width == 1024)
  799. {
  800. return(height / 2);
  801. }
  802. return(height);
  803. }
  804. }
  805. else
  806. {
  807. return(GetSystemMetrics(SM_CYSCREEN));
  808. }
  809. }
  810. int GetMonitorFromRect(LPRECT lpMonitorRect)
  811. {
  812. // Build up the param
  813. MONITOR_ENUM_PARAM EnumParam;
  814. ZeroMemory(&EnumParam, sizeof(EnumParam));
  815. EnumParam.lFlags = MONITOR_SEARCH_METOHD;
  816. EnumParam.pVirtualRect = lpMonitorRect;
  817. EnumParam.iMonitor = -1;
  818. // Enum Displays
  819. EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (long)&EnumParam);
  820. // Return the result
  821. return EnumParam.iMonitor;
  822. }
  823. void GetMonitorRect(int iMonitor, LPRECT lpDestRect)
  824. {
  825. // Build up the param
  826. MONITOR_ENUM_PARAM EnumParam;
  827. ZeroMemory(&EnumParam, sizeof(EnumParam));
  828. EnumParam.iMonitor = iMonitor;
  829. EnumParam.pVirtualRect = lpDestRect;
  830. // Zero out dest rect
  831. lpDestRect->bottom = lpDestRect->left = lpDestRect->right = lpDestRect->top = 0;
  832. // Enum Displays
  833. EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (long)&EnumParam);
  834. // If not successful, default to the screen dimentions
  835. if(lpDestRect->right == 0 || lpDestRect->bottom == 0)
  836. {
  837. lpDestRect->right = GetScreenWidth();
  838. lpDestRect->bottom = GetScreenHeight();
  839. }
  840. }
  841. /*------------------------------------------------------------------*\
  842. CAccel - an Accelerator (in-app hotkey)
  843. - the win32 CreateAcceleratorTable using ACCEL was insufficient
  844. because it only allowed a WORD for the cmd associated with it.
  845. \*------------------------------------------------------------------*/
  846. /*------------------------------------------------------------------*\
  847. CAccels - Manages a set of CAccel
  848. \*------------------------------------------------------------------*/
  849. int CompareAccel( const void* pLeft, const void* pRight )
  850. {
  851. WORD w;
  852. int l,r;
  853. // swap bytes: place the VirtualKey in the MSB and the modifier in the LSB
  854. // so that Accels based upon the same vkey are grouped together.
  855. // this is required by our use of m_Index
  856. // alternatively, we could store them this way in CAccel.
  857. w = (WORD) ((CAccel*)pLeft)->Key;
  858. l = (ACCEL_VKEY(w) << 8) | ACCEL_MOD(w);
  859. w = (WORD) ((CAccel*)pRight)->Key;
  860. r = (ACCEL_VKEY(w) << 8) | ACCEL_MOD(w);
  861. return l - r;
  862. }
  863. CAccels::CAccels()
  864. {}
  865. void CAccels::AddAccel( CAccel& a )
  866. {
  867. m_Map.SetAt(a.Key, a.Cmd);
  868. }
  869. bool CAccels::OnMsg( MSG* pMsg, DWORD &dID)
  870. {
  871. // bit 30 (0x40000000) is 1 if this is NOT the first msg of the key
  872. // i.e. auto-repeat may cause multiple msgs of the same key
  873. if( (pMsg->lParam & 0x40000000) ||
  874. (pMsg->message != WM_KEYDOWN &&
  875. pMsg->message != WM_SYSKEYDOWN) )
  876. {
  877. return NULL;
  878. }
  879. if( !pMsg || m_Map.GetCount() <= 0 )
  880. return NULL;
  881. BYTE vkey = LOBYTE(pMsg->wParam);
  882. BYTE mod = GetKeyStateModifiers();
  883. DWORD key = ACCEL_MAKEKEY( vkey, mod );
  884. if(m_Map.Lookup(key, dID))
  885. return true;;
  886. return false;
  887. }
  888. BYTE GetKeyStateModifiers()
  889. {
  890. BYTE m=0;
  891. if( GetKeyState(VK_SHIFT) & 0x8000 )
  892. m |= HOTKEYF_SHIFT;
  893. if( GetKeyState(VK_CONTROL) & 0x8000 )
  894. m |= HOTKEYF_CONTROL;
  895. if( GetKeyState(VK_MENU) & 0x8000 )
  896. m |= HOTKEYF_ALT;
  897. return m;
  898. }
  899. /*------------------------------------------------------------------*\
  900. CTokenizer - Tokenizes a string using given delimiters
  901. \*------------------------------------------------------------------*/
  902. CTokenizer::CTokenizer(const CString& cs, const CString& csDelim):
  903. m_cs(cs),
  904. m_nCurPos(0)
  905. {
  906. SetDelimiters(csDelim);
  907. }
  908. void CTokenizer::SetDelimiters(const CString& csDelim)
  909. {
  910. for(int i = 0; i < csDelim.GetLength(); ++i)
  911. m_delim.Add(csDelim[i]);
  912. m_delim.SortAscending();
  913. }
  914. bool CTokenizer::Next(CString& cs)
  915. {
  916. cs.Empty();
  917. int len = m_cs.GetLength();
  918. while (m_nCurPos < len && m_delim.Find(m_cs[m_nCurPos]))
  919. ++ m_nCurPos;
  920. if (m_nCurPos >= len)
  921. return false;
  922. int nStartPos = m_nCurPos;
  923. while (m_nCurPos < len && !m_delim.Find(m_cs[m_nCurPos]))
  924. ++ m_nCurPos;
  925. cs = m_cs.Mid(nStartPos, m_nCurPos - nStartPos);
  926. return true;
  927. }
  928. CString CTokenizer::Tail() const
  929. {
  930. int len = m_cs.GetLength();
  931. int nCurPos = m_nCurPos;
  932. while(nCurPos < len && m_delim[static_cast<BYTE>(m_cs[nCurPos])])
  933. ++nCurPos;
  934. CString csResult;
  935. if(nCurPos < len)
  936. csResult = m_cs.Mid(nCurPos);
  937. return csResult;
  938. }
  939. /*------------------------------------------------------------------*\
  940. Global ToolTip Manual Control Functions
  941. \*------------------------------------------------------------------*/
  942. void InitToolInfo( TOOLINFO& ti )
  943. {
  944. // INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE
  945. ti.cbSize = sizeof(TOOLINFO);
  946. ti.uFlags = TTF_ABSOLUTE | TTF_TRACK;
  947. ti.hwnd = NULL;
  948. ti.hinst = NULL;
  949. ti.uId = 0; // CPopup only uses uid 0
  950. ti.lpszText = NULL;
  951. // ToolTip control will cover the whole window
  952. ti.rect.left = 0;
  953. ti.rect.top = 0;
  954. ti.rect.right = 0;
  955. ti.rect.bottom = 0;
  956. }
  957. /*------------------------------------------------------------------*\
  958. CPopup - a tooltip that pops up manually (when Show is called).
  959. - technique learned from codeproject "ToolTipZen" by "Zarembo Maxim"
  960. \*------------------------------------------------------------------*/
  961. CPopup::CPopup()
  962. {
  963. Init();
  964. }
  965. // HWND_TOP
  966. CPopup::CPopup( int x, int y, HWND hWndPosRelativeTo, HWND hWndInsertAfter )
  967. {
  968. Init();
  969. m_hWndPosRelativeTo = hWndPosRelativeTo;
  970. m_hWndInsertAfter = hWndInsertAfter;
  971. SetPos( CPoint(x,y) );
  972. }
  973. CPopup::~CPopup()
  974. {
  975. Hide();
  976. if( m_bOwnTT && ::IsWindow(m_hTTWnd) )
  977. ::DestroyWindow( m_hTTWnd );
  978. }
  979. void CPopup::Init()
  980. {
  981. // initialize variables
  982. m_bOwnTT = false;
  983. m_hTTWnd = NULL;
  984. m_bIsShowing = false;
  985. m_bAllowShow = true; // used by AllowShow()
  986. m_Pos.x = m_Pos.y = 0;
  987. m_bTop = true;
  988. m_bLeft = true;
  989. m_bCenterX = false;
  990. m_bCenterY = false;
  991. m_hWndPosRelativeTo = NULL;
  992. RECT rcScreen;
  993. GetMonitorRect(-1, &rcScreen);
  994. m_ScreenMaxX = rcScreen.right;
  995. m_ScreenMaxY = rcScreen.bottom;
  996. m_hWndInsertAfter = HWND_TOP; //HWND_TOPMOST
  997. SetTTWnd();
  998. }
  999. void CPopup::SetTTWnd( HWND hTTWnd, TOOLINFO* pTI )
  1000. {
  1001. if( pTI )
  1002. m_TI = *pTI;
  1003. else
  1004. InitToolInfo( m_TI );
  1005. if( m_bOwnTT && ::IsWindow(m_hTTWnd) )
  1006. {
  1007. if( !::IsWindow(hTTWnd) )
  1008. return; // we would have to recreate the one that already exists
  1009. ::DestroyWindow( m_hTTWnd );
  1010. }
  1011. m_hTTWnd = hTTWnd;
  1012. if( ::IsWindow(m_hTTWnd) )
  1013. {
  1014. m_bOwnTT = false;
  1015. // if our uid tooltip already exists, get the data, else add it.
  1016. if( ! ::SendMessage(m_hTTWnd, TTM_GETTOOLINFO, 0, (LPARAM)(LPTOOLINFO) &m_TI) )
  1017. ::SendMessage(m_hTTWnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  1018. }
  1019. else
  1020. {
  1021. m_bOwnTT = true;
  1022. CreateToolTip();
  1023. }
  1024. }
  1025. void CPopup::CreateToolTip()
  1026. {
  1027. if( m_hTTWnd != NULL )
  1028. return;
  1029. // CREATE A TOOLTIP WINDOW
  1030. m_hTTWnd = CreateWindowEx(
  1031. WS_EX_TOPMOST,
  1032. TOOLTIPS_CLASS,
  1033. NULL,
  1034. TTS_NOPREFIX | TTS_ALWAYSTIP,
  1035. CW_USEDEFAULT,
  1036. CW_USEDEFAULT,
  1037. CW_USEDEFAULT,
  1038. CW_USEDEFAULT,
  1039. NULL,
  1040. NULL,
  1041. NULL,
  1042. NULL
  1043. );
  1044. m_bOwnTT = true;
  1045. // SEND AN ADDTOOL MESSAGE TO THE TOOLTIP CONTROL WINDOW
  1046. ::SendMessage(m_hTTWnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  1047. }
  1048. void CPopup::SetTimeout( int timeout )
  1049. {
  1050. if( m_hTTWnd == NULL )
  1051. return;
  1052. ::SendMessage(m_hTTWnd, TTM_SETDELAYTIME, TTDT_AUTOMATIC, timeout);
  1053. }
  1054. void CPopup::SetPos( CPoint& pos )
  1055. {
  1056. m_Pos = pos;
  1057. }
  1058. void CPopup::SetPosInfo( bool bTop, bool bCenterY, bool bLeft, bool bCenterX )
  1059. {
  1060. m_bTop = bTop;
  1061. m_bCenterY = bCenterY;
  1062. m_bLeft = bLeft;
  1063. m_bCenterX = bCenterX;
  1064. }
  1065. void CPopup::AdjustPos( CPoint& pos )
  1066. {
  1067. CRect rel(0,0,0,0);
  1068. CRect rect(0,0,0,0);
  1069. // ::SendMessage(m_hTTWnd, TTM_ADJUSTRECT, TRUE, (LPARAM)&rect);
  1070. ::GetWindowRect(m_hTTWnd,&rect);
  1071. if( ::IsWindow(m_hWndPosRelativeTo) )
  1072. ::GetWindowRect(m_hWndPosRelativeTo, &rel);
  1073. // move the rect to the relative origin
  1074. rect.bottom = rect.Height() + rel.top;
  1075. rect.top = rel.top;
  1076. rect.right = rect.Width() + rel.left;
  1077. rect.left = rel.left;
  1078. // adjust the y position
  1079. rect.OffsetRect( 0, pos.y - (m_bCenterY? rect.Height()/2: (m_bTop? 0: rect.Height())) );
  1080. if( rect.bottom > m_ScreenMaxY )
  1081. rect.OffsetRect( 0, m_ScreenMaxY - rect.bottom );
  1082. // adjust the x position
  1083. rect.OffsetRect( pos.x - (m_bCenterX? rect.Width()/2: (m_bLeft? 0: rect.Width())), 0 );
  1084. if( rect.right > m_ScreenMaxX )
  1085. rect.OffsetRect( m_ScreenMaxX - rect.right, 0 );
  1086. pos.x = rect.left;
  1087. pos.y = rect.top;
  1088. }
  1089. void CPopup::SendToolTipText( CString text )
  1090. {
  1091. m_csToolTipText = text;
  1092. //Replace the tabs with spaces, the tooltip didn't like the \t s
  1093. text.Replace(_T("\t"), _T(" "));
  1094. m_TI.lpszText = (LPTSTR) (LPCTSTR) text;
  1095. // this allows \n and \r to be interpreted correctly
  1096. ::SendMessage(m_hTTWnd, TTM_SETMAXTIPWIDTH, 0, 500);
  1097. // set the text
  1098. ::SendMessage(m_hTTWnd, TTM_SETTOOLINFO, 0, (LPARAM) (LPTOOLINFO) &m_TI);
  1099. }
  1100. void CPopup::Show( CString text, CPoint pos, bool bAdjustPos )
  1101. {
  1102. if( m_hTTWnd == NULL )
  1103. return;
  1104. m_csToolTipText = text;
  1105. if( !m_bIsShowing )
  1106. ::SendMessage(m_hTTWnd, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(-10000,-10000));
  1107. SendToolTipText( text );
  1108. ::SendMessage(m_hTTWnd, TTM_TRACKACTIVATE, true, (LPARAM)(LPTOOLINFO) &m_TI);
  1109. if( bAdjustPos )
  1110. AdjustPos(pos);
  1111. // set the position
  1112. ::SendMessage(m_hTTWnd, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(pos.x,pos.y));
  1113. // make sure the tooltip will be on top.
  1114. ::SetWindowPos( m_hTTWnd, m_hWndInsertAfter, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE );
  1115. m_bIsShowing = true;
  1116. }
  1117. void CPopup::Show( CString text )
  1118. {
  1119. m_csToolTipText = text;
  1120. Show( text, m_Pos );
  1121. }
  1122. void CPopup::AllowShow( CString text )
  1123. {
  1124. m_csToolTipText = text;
  1125. if( m_bAllowShow )
  1126. Show( text, m_Pos );
  1127. }
  1128. void CPopup::Hide()
  1129. {
  1130. if( m_hTTWnd == NULL )
  1131. return;
  1132. // deactivate if it is currently activated
  1133. ::SendMessage(m_hTTWnd, TTM_TRACKACTIVATE, FALSE, (LPARAM)(LPTOOLINFO) &m_TI);
  1134. m_bIsShowing = false;
  1135. }
  1136. /*------------------------------------------------------------------*\
  1137. ID based Globals
  1138. \*------------------------------------------------------------------*/
  1139. BOOL MarkClipAsPasted(long lID)
  1140. {
  1141. CGetSetOptions::SetTripPasteCount(-1);
  1142. CGetSetOptions::SetTotalPasteCount(-1);
  1143. if( !g_Opt.m_bUpdateTimeOnPaste )
  1144. return FALSE;
  1145. try
  1146. {
  1147. //Update the time it was copied so that it appears at the top of the
  1148. //paste list. Items are sorted by this time.
  1149. CTime now = CTime::GetCurrentTime();
  1150. try
  1151. {
  1152. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT lDate FROM Main ORDER BY lDate DESC LIMIT 1"));
  1153. if(q.eof() == false)
  1154. {
  1155. long lLatestDate = q.getIntField(_T("lDate"));
  1156. if(now.GetTime() <= lLatestDate)
  1157. {
  1158. now = lLatestDate + 1;
  1159. }
  1160. }
  1161. }
  1162. CATCH_SQLITE_EXCEPTION
  1163. theApp.m_db.execDMLEx(_T("UPDATE Main SET lDate = %d where lID = %d;"), (long)now.GetTime(), lID);
  1164. return TRUE;
  1165. }
  1166. CATCH_SQLITE_EXCEPTION
  1167. return FALSE;
  1168. }
  1169. long NewGroupID(long lParentID, CString text)
  1170. {
  1171. long lID=0;
  1172. CTime time;
  1173. time = CTime::GetCurrentTime();
  1174. try
  1175. {
  1176. //sqlite doesn't like single quotes ' replace them with double ''
  1177. if(text.IsEmpty())
  1178. text = time.Format("NewGroup %y/%m/%d %H:%M:%S");
  1179. text.Replace(_T("'"), _T("''"));
  1180. CString cs;
  1181. cs.Format(_T("insert into Main values(NULL, %d, '%s', 0, %d, 0, 1, %d, '');"),
  1182. (long)time.GetTime(),
  1183. text,
  1184. (long)time.GetTime(),
  1185. lParentID);
  1186. theApp.m_db.execDML(cs);
  1187. lID = (long)theApp.m_db.lastRowId();
  1188. }
  1189. CATCH_SQLITE_EXCEPTION_AND_RETURN(0)
  1190. return lID;
  1191. }
  1192. // deletes the given item
  1193. BOOL DeleteID(long lID)
  1194. {
  1195. BOOL bRet = FALSE;
  1196. try
  1197. {
  1198. bool bCont = false;
  1199. bool bGroup = false;
  1200. {
  1201. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT bIsGroup FROM Main WHERE lId = %d"), lID);
  1202. bCont = !q.eof();
  1203. if(bCont)
  1204. {
  1205. bGroup = q.getIntField(_T("bIsGroup")) > 0;
  1206. }
  1207. }
  1208. if(bCont)
  1209. {
  1210. if(bGroup)
  1211. {
  1212. theApp.m_db.execDMLEx(_T("UPDATE Main SET lParentID = -1 WHERE lParentID = %d;"), lID);
  1213. }
  1214. //now is deleted from a trigger
  1215. //theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lParentID = %d;"), lID);
  1216. theApp.m_db.execDMLEx(_T("DELETE FROM Main WHERE lID = %d;"), lID);
  1217. bRet = TRUE;
  1218. }
  1219. theApp.OnDeleteID(lID);
  1220. }
  1221. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  1222. return bRet;
  1223. }
  1224. BOOL DeleteAllIDs()
  1225. {
  1226. try
  1227. {
  1228. theApp.m_db.execDML(_T("DELETE FROM Data;"));
  1229. theApp.m_db.execDML(_T("DELETE FROM Main;"));
  1230. }
  1231. CATCH_SQLITE_EXCEPTION
  1232. return TRUE;
  1233. }
  1234. BOOL DeleteFormats(long lParentID, ARRAY& formatIDs)
  1235. {
  1236. if(formatIDs.GetSize() <= 0)
  1237. return TRUE;
  1238. try
  1239. {
  1240. //Delete the requested data formats
  1241. int nCount = formatIDs.GetSize();
  1242. for(int i = 0; i < nCount; i++)
  1243. {
  1244. theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lID = %d;"), formatIDs[i]);
  1245. }
  1246. CClip clip;
  1247. if(clip.LoadFormats(lParentID))
  1248. {
  1249. DWORD CRC = clip.GenerateCRC();
  1250. //Update the main table with new size
  1251. theApp.m_db.execDMLEx(_T("UPDATE Main SET CRC = %d WHERE lID = %d"), CRC, lParentID);
  1252. }
  1253. }
  1254. CATCH_SQLITE_EXCEPTION
  1255. return TRUE;
  1256. }
  1257. BOOL EnsureWindowVisible(CRect *pcrRect)
  1258. {
  1259. int nMonitor = GetMonitorFromRect(pcrRect);
  1260. if(nMonitor < 0)
  1261. {
  1262. GetMonitorRect(0, pcrRect);
  1263. pcrRect->right = pcrRect->left + 300;
  1264. pcrRect->bottom = pcrRect->top + 300;
  1265. return TRUE;
  1266. }
  1267. CRect crMonitor;
  1268. GetMonitorRect(nMonitor, crMonitor);
  1269. //Validate the left
  1270. long lDiff = pcrRect->left - crMonitor.left;
  1271. if(lDiff < 0)
  1272. {
  1273. pcrRect->left += abs(lDiff);
  1274. pcrRect->right += abs(lDiff);
  1275. }
  1276. //Right side
  1277. lDiff = pcrRect->right - crMonitor.right;
  1278. if(lDiff > 0)
  1279. {
  1280. pcrRect->left -= abs(lDiff);
  1281. pcrRect->right -= abs(lDiff);
  1282. }
  1283. //Top
  1284. lDiff = pcrRect->top - crMonitor.top;
  1285. if(lDiff < 0)
  1286. {
  1287. pcrRect->top += abs(lDiff);
  1288. pcrRect->bottom += abs(lDiff);
  1289. }
  1290. //Bottom
  1291. lDiff = pcrRect->bottom - crMonitor.bottom;
  1292. if(lDiff > 0)
  1293. {
  1294. pcrRect->top -= abs(lDiff);
  1295. pcrRect->bottom -= abs(lDiff);
  1296. }
  1297. return TRUE;
  1298. }
  1299. __int64 GetLastWriteTime(const CString &csFile)
  1300. {
  1301. __int64 nLastWrite = 0;
  1302. CFileFind finder;
  1303. BOOL bResult = finder.FindFile(csFile);
  1304. if (bResult)
  1305. {
  1306. finder.FindNextFile();
  1307. FILETIME ft;
  1308. finder.GetLastWriteTime(&ft);
  1309. memcpy(&nLastWrite, &ft, sizeof(ft));
  1310. }
  1311. return nLastWrite;
  1312. }