Misc.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include "Misc.h"
  4. #include "OptionsSheet.h"
  5. #ifdef AFTER_98
  6. #include "AlphaBlend.h"
  7. #endif
  8. long DoOptions(CWnd *pParent)
  9. {
  10. //Don't let it open up more than once
  11. if(theApp.m_bShowingOptions)
  12. return FALSE;
  13. theApp.m_bShowingOptions = true;
  14. COptionsSheet Sheet("Copy Pro Options", pParent);
  15. int nRet = Sheet.DoModal();
  16. theApp.m_bShowingOptions = false;
  17. return nRet;
  18. }
  19. HWND GetActiveWnd(CPoint *pPointCaret)
  20. {
  21. HWND hWndFocus = NULL;
  22. if (pPointCaret)
  23. *pPointCaret = CPoint(-1, -1);
  24. HWND hWndForground = GetForegroundWindow(); // Get the desktop's foreground window
  25. if (hWndForground != NULL)
  26. {
  27. DWORD ProcID;
  28. DWORD ThreadID = GetWindowThreadProcessId(hWndForground, &ProcID);
  29. // Attach other thread's message queue to our own to ensure GetFocus() is working properly
  30. BOOL ARes = AttachThreadInput(ThreadID, GetCurrentThreadId(), TRUE);
  31. if (ARes)
  32. {
  33. // Get the other thread's focussed window
  34. CWnd *pWnd = CWnd::FromHandle(hWndForground);
  35. if (pWnd)
  36. {
  37. CWnd *pWndFocus = pWnd->GetFocus();
  38. if (pWndFocus)
  39. {
  40. hWndFocus = pWndFocus->m_hWnd;
  41. if (pPointCaret)
  42. {
  43. *pPointCaret = pWndFocus->GetCaretPos();
  44. pWndFocus->ClientToScreen(pPointCaret);
  45. }
  46. }
  47. }
  48. // Detach other thread's message queue from our own again
  49. ARes = AttachThreadInput(ThreadID, GetCurrentThreadId(), FALSE);
  50. }
  51. }
  52. return hWndFocus;
  53. }
  54. //Do not change these these are stored in the database
  55. CLIPFORMAT GetFormatID(LPCSTR cbName)
  56. {
  57. if(strcmp(cbName, "CF_TEXT") == 0)
  58. return CF_TEXT;
  59. else if(strcmp(cbName, "CF_METAFILEPICT") == 0)
  60. return CF_METAFILEPICT;
  61. else if(strcmp(cbName, "CF_SYLK") == 0)
  62. return CF_SYLK;
  63. else if(strcmp(cbName, "CF_DIF") == 0)
  64. return CF_DIF;
  65. else if(strcmp(cbName, "CF_TIFF") == 0)
  66. return CF_TIFF;
  67. else if(strcmp(cbName, "CF_OEMTEXT") == 0)
  68. return CF_OEMTEXT;
  69. else if(strcmp(cbName, "CF_DIB") == 0)
  70. return CF_DIB;
  71. else if(strcmp(cbName, "CF_PALETTE") == 0)
  72. return CF_PALETTE;
  73. else if(strcmp(cbName, "CF_PENDATA") == 0)
  74. return CF_PENDATA;
  75. else if(strcmp(cbName, "CF_RIFF") == 0)
  76. return CF_RIFF;
  77. else if(strcmp(cbName, "CF_WAVE") == 0)
  78. return CF_WAVE;
  79. else if(strcmp(cbName, "CF_UNICODETEXT") == 0)
  80. return CF_UNICODETEXT;
  81. else if(strcmp(cbName, "CF_ENHMETAFILE") == 0)
  82. return CF_ENHMETAFILE;
  83. else if(strcmp(cbName, "CF_HDROP") == 0)
  84. return CF_HDROP;
  85. else if(strcmp(cbName, "CF_LOCALE") == 0)
  86. return CF_LOCALE;
  87. else if(strcmp(cbName, "CF_OWNERDISPLAY") == 0)
  88. return CF_OWNERDISPLAY;
  89. else if(strcmp(cbName, "CF_DSPTEXT") == 0)
  90. return CF_DSPTEXT;
  91. else if(strcmp(cbName, "CF_DSPBITMAP") == 0)
  92. return CF_DSPBITMAP;
  93. else if(strcmp(cbName, "CF_DSPMETAFILEPICT") == 0)
  94. return CF_DSPMETAFILEPICT;
  95. else if(strcmp(cbName, "CF_DSPENHMETAFILE") == 0)
  96. return CF_DSPENHMETAFILE;
  97. return ::RegisterClipboardFormat(cbName);
  98. }
  99. //Do not change these these are stored in the database
  100. CString GetFormatName(CLIPFORMAT cbType)
  101. {
  102. switch(cbType)
  103. {
  104. case CF_TEXT:
  105. return "CF_TEXT";
  106. case CF_BITMAP:
  107. return "CF_BITMAP";
  108. case CF_METAFILEPICT:
  109. return "CF_METAFILEPICT";
  110. case CF_SYLK:
  111. return "CF_SYLK";
  112. case CF_DIF:
  113. return "CF_DIF";
  114. case CF_TIFF:
  115. return "CF_TIFF";
  116. case CF_OEMTEXT:
  117. return "CF_OEMTEXT";
  118. case CF_DIB:
  119. return "CF_DIB";
  120. case CF_PALETTE:
  121. return "CF_PALETTE";
  122. case CF_PENDATA:
  123. return "CF_PENDATA";
  124. case CF_RIFF:
  125. return "CF_RIFF";
  126. case CF_WAVE:
  127. return "CF_WAVE";
  128. case CF_UNICODETEXT:
  129. return "CF_UNICODETEXT";
  130. case CF_ENHMETAFILE:
  131. return "CF_ENHMETAFILE";
  132. case CF_HDROP:
  133. return "CF_HDROP";
  134. case CF_LOCALE:
  135. return "CF_LOCALE";
  136. case CF_OWNERDISPLAY:
  137. return "CF_OWNERDISPLAY";
  138. case CF_DSPTEXT:
  139. return "CF_DSPTEXT";
  140. case CF_DSPBITMAP:
  141. return "CF_DSPBITMAP";
  142. case CF_DSPMETAFILEPICT:
  143. return "CF_DSPMETAFILEPICT";
  144. case CF_DSPENHMETAFILE:
  145. return "CF_DSPENHMETAFILE";
  146. default:
  147. {
  148. //Not a default type get the name from the clipboard
  149. if (cbType != 0)
  150. {
  151. TCHAR szFormat[256];
  152. GetClipboardFormatName(cbType, szFormat, 256);
  153. return szFormat;
  154. }
  155. break;
  156. }
  157. }
  158. return "ERROR";
  159. }
  160. CString GetFilePath(CString csFileName)
  161. {
  162. long lSlash = csFileName.ReverseFind('\\');
  163. if(lSlash > -1)
  164. {
  165. csFileName = csFileName.Left(lSlash + 1);
  166. }
  167. return csFileName;
  168. }
  169. CGetSetOptions::CGetSetOptions()
  170. {
  171. }
  172. CGetSetOptions::~CGetSetOptions()
  173. {
  174. }
  175. long CGetSetOptions::GetProfileLong(CString csName, long bDefaultValue)
  176. {
  177. HKEY hkKey;
  178. long lResult = RegOpenKeyEx(HKEY_CURRENT_USER, _T(REG_PATH),
  179. NULL, KEY_READ, &hkKey);
  180. if(lResult != ERROR_SUCCESS)
  181. return bDefaultValue;
  182. DWORD buffer;
  183. DWORD len = sizeof(buffer);
  184. DWORD type;
  185. lResult = ::RegQueryValueEx(hkKey, csName, 0, &type, (LPBYTE)&buffer, &len);
  186. RegCloseKey(hkKey);
  187. if(lResult == ERROR_SUCCESS)
  188. return (long)buffer;
  189. return bDefaultValue;
  190. }
  191. CString CGetSetOptions::GetProfileString(CString csName, CString csDefault)
  192. {
  193. HKEY hkKey;
  194. long lResult = RegOpenKeyEx(HKEY_CURRENT_USER, _T(REG_PATH),
  195. NULL, KEY_READ, &hkKey);
  196. char szString[256];
  197. DWORD dwBufLen = 256;
  198. lResult = ::RegQueryValueEx(hkKey , csName, NULL, NULL, (LPBYTE)szString, &dwBufLen);
  199. if(lResult != ERROR_SUCCESS)
  200. return csDefault;
  201. return CString(szString);
  202. }
  203. BOOL CGetSetOptions::SetProfileLong(CString csName, long lValue)
  204. {
  205. HKEY hkKey;
  206. DWORD dWord;
  207. long lResult = RegCreateKeyEx(HKEY_CURRENT_USER, _T(REG_PATH), NULL,
  208. NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
  209. NULL, &hkKey, &dWord);
  210. if(lResult != ERROR_SUCCESS)
  211. return FALSE;
  212. DWORD val = (DWORD)lValue;
  213. lResult = ::RegSetValueEx(hkKey, csName, 0, REG_DWORD, (LPBYTE)&val, sizeof(DWORD));
  214. RegCloseKey(hkKey);
  215. return lResult == ERROR_SUCCESS;
  216. }
  217. BOOL CGetSetOptions::SetProfileString(CString csName, CString csValue)
  218. {
  219. HKEY hkKey;
  220. DWORD dWord;
  221. long lResult = RegCreateKeyEx(HKEY_CURRENT_USER, _T(REG_PATH), NULL,
  222. NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
  223. NULL, &hkKey, &dWord);
  224. if(lResult != ERROR_SUCCESS)
  225. return FALSE;
  226. ::RegSetValueEx(hkKey, csName, NULL, REG_SZ,
  227. (BYTE*)(LPCTSTR)csValue, csValue.GetLength()+sizeof(TCHAR));
  228. RegCloseKey(hkKey);
  229. return lResult == ERROR_SUCCESS;
  230. }
  231. BOOL CGetSetOptions::GetShowIconInSysTray()
  232. {
  233. return GetProfileLong("ShowIconInSystemTray", TRUE);
  234. }
  235. BOOL CGetSetOptions::SetShowIconInSysTray(BOOL bShow)
  236. {
  237. return SetProfileLong("ShowIconInSystemTray", bShow);
  238. }
  239. DWORD CGetSetOptions::GetHotKey()
  240. {
  241. //704 is ctrl-tilda
  242. return (DWORD)GetProfileLong("HotKey", 704);
  243. }
  244. BOOL CGetSetOptions::SetHotKey(DWORD dwHotKey)
  245. {
  246. return SetProfileLong("HotKey", dwHotKey);
  247. }
  248. DWORD CGetSetOptions::GetNamedCopyHotKey()
  249. {
  250. return (DWORD)GetProfileLong("NamedCopyHotKey", 0);
  251. }
  252. BOOL CGetSetOptions::SetNamedCopyHotKey(long lHotKey)
  253. {
  254. return SetProfileLong("NamedCopyHotKey", lHotKey);
  255. }
  256. BOOL CGetSetOptions::SetEnableTransparency(BOOL bCheck)
  257. {
  258. return SetProfileLong("EnableTransparency", bCheck);
  259. }
  260. BOOL CGetSetOptions::GetEnableTransparency()
  261. {
  262. return GetProfileLong("EnableTransparency", FALSE);
  263. }
  264. BOOL CGetSetOptions::SetTransparencyPercent(long lPercent)
  265. {
  266. #ifdef AFTER_98
  267. if(lPercent > OPACITY_MAX)
  268. lPercent = OPACITY_MAX;
  269. if(lPercent < 0)
  270. lPercent = 0;
  271. return SetProfileLong("TransparencyPercent", lPercent);
  272. #endif
  273. return FALSE;
  274. }
  275. long CGetSetOptions::GetTransparencyPercent()
  276. {
  277. #ifdef AFTER_98
  278. long lValue = GetProfileLong("TransparencyPercent", 14);
  279. if(lValue > OPACITY_MAX) lValue = OPACITY_MAX;
  280. if(lValue < 0) lValue = 0;
  281. return lValue;
  282. #endif
  283. return 0;
  284. }
  285. BOOL CGetSetOptions::SetLinesPerRow(long lLines)
  286. {
  287. return SetProfileLong("LinesPerRow", lLines);
  288. }
  289. long CGetSetOptions::GetLinesPerRow()
  290. {
  291. return GetProfileLong("LinesPerRow", 2);
  292. }
  293. BOOL CGetSetOptions::GetRunOnStartUp()
  294. {
  295. HKEY hkRun;
  296. LONG nResult = RegOpenKeyEx(HKEY_CURRENT_USER,
  297. _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
  298. NULL, KEY_READ, &hkRun);
  299. if(nResult != ERROR_SUCCESS)
  300. return FALSE;
  301. nResult = RegQueryValueEx(hkRun, GetAppName(), NULL, NULL, NULL, NULL);
  302. RegCloseKey(hkRun);
  303. return nResult == ERROR_SUCCESS;
  304. }
  305. void CGetSetOptions::SetRunOnStartUp(BOOL bRun)
  306. {
  307. if(bRun == GetRunOnStartUp())
  308. return;
  309. HKEY hkRun;
  310. LONG nResult = RegOpenKeyEx(HKEY_CURRENT_USER,
  311. _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
  312. NULL, KEY_ALL_ACCESS, &hkRun);
  313. if(nResult != ERROR_SUCCESS)
  314. return;
  315. if(bRun)
  316. {
  317. CString sExeName = GetExeFileName();
  318. ::RegSetValueEx(hkRun, GetAppName(), NULL, REG_SZ,
  319. (BYTE*)(LPCTSTR)sExeName, sExeName.GetLength()+sizeof(TCHAR));
  320. }
  321. else
  322. {
  323. ::RegDeleteValue(hkRun, GetAppName());
  324. }
  325. ::RegCloseKey(hkRun);
  326. }
  327. CString CGetSetOptions::GetExeFileName()
  328. {
  329. CString sExeName;
  330. GetModuleFileName(NULL, sExeName.GetBuffer(_MAX_PATH),_MAX_PATH);
  331. sExeName.ReleaseBuffer();
  332. return sExeName;
  333. }
  334. CString CGetSetOptions::GetAppName()
  335. {
  336. return "Ditto";
  337. }
  338. BOOL CGetSetOptions::SetQuickPastePosition(long lPosition)
  339. {
  340. return SetProfileLong("ShowQuickPastePosition", lPosition);
  341. }
  342. long CGetSetOptions::GetQuickPastePosition()
  343. {
  344. return GetProfileLong("ShowQuickPastePosition", POS_AT_PREVIOUS);
  345. }
  346. BOOL CGetSetOptions::RegisterHotKey(HWND hWnd, DWORD wHotKey, ATOM atomID)
  347. {
  348. if(wHotKey == 0)
  349. return FALSE;
  350. return ::RegisterHotKey(hWnd, atomID, GetModifier(wHotKey), LOBYTE(wHotKey));
  351. }
  352. UINT CGetSetOptions::GetModifier(DWORD wHotKey)
  353. {
  354. UINT uMod = 0;
  355. if( HIBYTE(wHotKey) & HOTKEYF_SHIFT ) uMod |= MOD_SHIFT;
  356. if( HIBYTE(wHotKey) & HOTKEYF_CONTROL ) uMod |= MOD_CONTROL;
  357. if( HIBYTE(wHotKey) & HOTKEYF_ALT ) uMod |= MOD_ALT;
  358. if( HIBYTE(wHotKey) & HOTKEYF_EXT ) uMod |= MOD_WIN;
  359. return uMod;
  360. }
  361. BOOL CGetSetOptions::SetQuickPasteSize(CSize size)
  362. {
  363. BOOL bRet = SetProfileLong("QuickPasteCX", size.cx);
  364. bRet = SetProfileLong("QuickPasteCY", size.cy);
  365. return bRet;
  366. }
  367. void CGetSetOptions::GetQuickPasteSize(CSize &size)
  368. {
  369. size.cx = GetProfileLong("QuickPasteCX", 300);
  370. size.cy = GetProfileLong("QuickPasteCY", 300);
  371. }
  372. BOOL CGetSetOptions::SetQuickPastePoint(CPoint point)
  373. {
  374. BOOL bRet = SetProfileLong("QuickPasteX", point.x);
  375. bRet = SetProfileLong("QuickPasteY", point.y);
  376. return bRet;
  377. }
  378. void CGetSetOptions::GetQuickPastePoint(CPoint &point)
  379. {
  380. point.x = GetProfileLong("QuickPasteX", 300);
  381. point.y = GetProfileLong("QuickPasteY", 300);
  382. }
  383. long CGetSetOptions::GetCopyGap()
  384. {
  385. return GetProfileLong("CopyGap", 150);
  386. }
  387. BOOL CGetSetOptions::SetDBPath(CString csPath)
  388. {
  389. return SetProfileString("DBPath", csPath);
  390. }
  391. CString CGetSetOptions::GetDBPath(BOOL bDefault/* = TRUE*/)
  392. {
  393. //First check the reg string
  394. CString csDefaultPath = GetProfileString("DBPath", "");
  395. //If there is nothing in the regesty then get the default
  396. //In the users application data in my documents
  397. if(bDefault)
  398. {
  399. if(csDefaultPath.IsEmpty())
  400. csDefaultPath = GetDefaultDBName();
  401. }
  402. return csDefaultPath;
  403. }
  404. void CGetSetOptions::SetCheckForMaxEntries(BOOL bVal)
  405. {
  406. SetProfileLong("CheckForMaxEntries", bVal);
  407. }
  408. BOOL CGetSetOptions::GetCheckForMaxEntries()
  409. {
  410. return GetProfileLong("CheckForMaxEntries", 0);
  411. }
  412. void CGetSetOptions::SetCheckForExpiredEntries(BOOL bVal)
  413. {
  414. SetProfileLong("CheckForExpiredEntries", bVal);
  415. }
  416. BOOL CGetSetOptions::GetCheckForExpiredEntries()
  417. {
  418. return GetProfileLong("CheckForExpiredEntries", 0);
  419. }
  420. void CGetSetOptions::SetMaxEntries(long lVal)
  421. {
  422. SetProfileLong("MaxEntries", lVal);
  423. }
  424. long CGetSetOptions::GetMaxEntries()
  425. {
  426. return GetProfileLong("MaxEntries", 500);
  427. }
  428. void CGetSetOptions::SetExpiredEntries(long lVal)
  429. {
  430. SetProfileLong("ExpiredEntries", lVal);
  431. }
  432. long CGetSetOptions::GetExpiredEntries()
  433. {
  434. return GetProfileLong("ExpiredEntries", 5);
  435. }
  436. void CGetSetOptions::SetTripCopyCount(long lVal)
  437. {
  438. if(lVal == -1)
  439. lVal = GetTripCopyCount() + 1;
  440. if(GetTripDate() == 0)
  441. SetTripDate(-1);
  442. SetProfileLong("TripCopies", lVal);
  443. }
  444. long CGetSetOptions::GetTripCopyCount()
  445. {
  446. return GetProfileLong("TripCopies", 0);
  447. }
  448. void CGetSetOptions::SetTripPasteCount(long lVal)
  449. {
  450. if(lVal == -1)
  451. lVal = GetTripPasteCount() + 1;
  452. if(GetTripDate() == 0)
  453. SetTripDate(-1);
  454. SetProfileLong("TripPastes", lVal);
  455. }
  456. long CGetSetOptions::GetTripPasteCount()
  457. {
  458. return GetProfileLong("TripPastes", 0);
  459. }
  460. void CGetSetOptions::SetTripDate(long lDate)
  461. {
  462. if(lDate == -1)
  463. lDate = (long)CTime::GetCurrentTime().GetTime();
  464. SetProfileLong("TripDate", lDate);
  465. }
  466. long CGetSetOptions::GetTripDate()
  467. {
  468. return GetProfileLong("TripDate", 0);
  469. }
  470. void CGetSetOptions::SetTotalCopyCount(long lVal)
  471. {
  472. if(lVal == -1)
  473. lVal = GetTotalCopyCount() + 1;
  474. if(GetTotalDate() == 0)
  475. SetTotalDate(-1);
  476. SetProfileLong("TotalCopies", lVal);
  477. }
  478. long CGetSetOptions::GetTotalCopyCount()
  479. {
  480. return GetProfileLong("TotalCopies", 0);
  481. }
  482. void CGetSetOptions::SetTotalPasteCount(long lVal)
  483. {
  484. if(lVal == -1)
  485. lVal = GetTotalPasteCount() + 1;
  486. if(GetTotalDate() == 0)
  487. SetTotalDate(-1);
  488. SetProfileLong("TotalPastes", lVal);
  489. }
  490. long CGetSetOptions::GetTotalPasteCount()
  491. {
  492. return GetProfileLong("TotalPastes", 0);
  493. }
  494. void CGetSetOptions::SetTotalDate(long lDate)
  495. {
  496. if(lDate == -1)
  497. lDate = (long)CTime::GetCurrentTime().GetTime();
  498. SetProfileLong("TotalDate", lDate);
  499. }
  500. long CGetSetOptions::GetTotalDate()
  501. {
  502. return GetProfileLong("TotalDate", 0);
  503. }
  504. void CGetSetOptions::SetCompactAndRepairOnExit(BOOL bVal)
  505. {
  506. SetProfileLong("CompactAndRepairOnExit", bVal);
  507. }
  508. BOOL CGetSetOptions::GetCompactAndRepairOnExit()
  509. {
  510. return GetProfileLong("CompactAndRepairOnExit", 0);
  511. }