OptionsGeneral.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. // OptionsGeneral.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "OptionsGeneral.h"
  6. #include "InternetUpdate.h"
  7. #include <io.h>
  8. #include <Mmsystem.h> //play sound
  9. #include "Path.h"
  10. #include "AdvGeneral.h"
  11. #include "DimWnd.h"
  12. #include "HyperLink.h"
  13. using namespace nsPath;
  14. #define DEFAULT_THEME _T("(Default)")
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. /////////////////////////////////////////////////////////////////////////////
  21. // COptionsGeneral property page
  22. IMPLEMENT_DYNCREATE(COptionsGeneral, CPropertyPage)
  23. COptionsGeneral::COptionsGeneral() : CPropertyPage(COptionsGeneral::IDD)
  24. {
  25. m_csTitle = theApp.m_Language.GetString("GeneralTitle", "General");
  26. m_psp.pszTitle = m_csTitle;
  27. m_psp.dwFlags |= PSP_USETITLE;
  28. memset(&m_LogFont, 0, sizeof(LOGFONT));
  29. //{{AFX_DATA_INIT(COptionsGeneral)
  30. //}}AFX_DATA_INIT
  31. }
  32. COptionsGeneral::~COptionsGeneral()
  33. {
  34. }
  35. void COptionsGeneral::DoDataExchange(CDataExchange* pDX)
  36. {
  37. CPropertyPage::DoDataExchange(pDX);
  38. //{{AFX_DATA_MAP(COptionsGeneral)
  39. //DDX_Control(pDX, IDC_ENSURE, m_EnsureConnected);
  40. // DDX_Control(pDX, IDC_EDIT_SAVE_DELAY, m_SaveDelay);
  41. DDX_Control(pDX, IDC_COMBO_LANGUAGE, m_cbLanguage);
  42. DDX_Control(pDX, IDC_PATH, m_ePath);
  43. DDX_Control(pDX, IDC_EXPIRE_AFTER, m_eExpireAfter);
  44. DDX_Control(pDX, IDC_MAX_SAVED_COPIES, m_eMaxSavedCopies);
  45. DDX_Control(pDX, IDC_EXPIRE, m_btExpire);
  46. DDX_Control(pDX, IDC_START_ON_STARTUP, m_btRunOnStartup);
  47. DDX_Control(pDX, IDC_EDIT_APP_COPY_INCLUDE, m_copyAppInclude);
  48. DDX_Control(pDX, IDC_EDIT_APP_COPY_EXCLUDE, m_copyAppExclude);
  49. DDX_Control(pDX, IDC_MAXIMUM, m_btMaximumCheck);
  50. //}}AFX_DATA_MAP
  51. DDX_Control(pDX, IDC_COMBO_THEME, m_cbTheme);
  52. DDX_Control(pDX, IDC_BUTTON_FONT, m_btFont);
  53. DDX_Control(pDX, IDC_BUTTON_DEFAULT_FAULT, m_btDefaultButton);
  54. DDX_Control(pDX, IDC_COMBO_POPUP_POSITION, m_popupPositionCombo);
  55. DDX_Control(pDX, IDC_MFCLINK_ENV_VAR, m_envVarLink);
  56. }
  57. BEGIN_MESSAGE_MAP(COptionsGeneral, CPropertyPage)
  58. //{{AFX_MSG_MAP(COptionsGeneral)
  59. ON_BN_CLICKED(IDC_GET_PATH, OnGetPath)
  60. ON_BN_CLICKED(IDC_BUTTON_ABOUT, OnButtonAbout)
  61. //}}AFX_MSG_MAP
  62. ON_BN_CLICKED(IDC_BUTTON_ADVANCED, &COptionsGeneral::OnBnClickedButtonAdvanced)
  63. ON_WM_CTLCOLOR()
  64. ON_BN_CLICKED(IDC_BUTTON_THEME, &COptionsGeneral::OnBnClickedButtonTheme)
  65. ON_BN_CLICKED(IDC_BUTTON_DEFAULT_FAULT, &COptionsGeneral::OnBnClickedButtonDefaultFault)
  66. ON_BN_CLICKED(IDC_BUTTON_FONT, &COptionsGeneral::OnBnClickedButtonFont)
  67. ON_EN_CHANGE(IDC_PATH, &COptionsGeneral::OnEnChangePath)
  68. END_MESSAGE_MAP()
  69. /////////////////////////////////////////////////////////////////////////////
  70. // COptionsGeneral message handlers
  71. BOOL COptionsGeneral::OnInitDialog()
  72. {
  73. CPropertyPage::OnInitDialog();
  74. CString url = _T("https://sourceforge.net/p/ditto-cp/wiki/EnvironmentVariables/");
  75. /*m_envVarLink.SetURL(_T("\"") + url);
  76. m_envVarLink.SetFontSize(-9);*/
  77. m_brush.CreateSolidBrush(RGB(251, 251, 251));
  78. m_pParent = (COptionsSheet *)GetParent();
  79. #ifndef _DEBUG
  80. if (CGetSetOptions::GetIsWindowsApp())
  81. {
  82. m_btRunOnStartup.SetCheck(BST_CHECKED);
  83. GetDlgItem(IDC_START_ON_STARTUP)->EnableWindow(FALSE);
  84. }
  85. else
  86. {
  87. m_btRunOnStartup.SetCheck(CGetSetOptions::GetRunOnStartUp());
  88. }
  89. #endif
  90. m_btMaximumCheck.SetCheck(CGetSetOptions::GetCheckForMaxEntries());
  91. m_btExpire.SetCheck(CGetSetOptions::GetCheckForExpiredEntries());
  92. m_eExpireAfter.SetNumber(CGetSetOptions::GetExpiredEntries());
  93. m_eMaxSavedCopies.SetNumber(CGetSetOptions::GetMaxEntries());
  94. m_copyAppInclude.SetWindowText(g_Opt.GetCopyAppInclude());
  95. m_copyAppExclude.SetWindowText(g_Opt.GetCopyAppExclude());
  96. CString csPath = CGetSetOptions::GetDBPath(false);
  97. m_ePath.SetWindowText(csPath);
  98. CGetSetOptions::GetFont(m_LogFont);
  99. CString cs;
  100. cs.Format(_T("Font - %s (%d)"), m_LogFont.lfFaceName, GetFontSize(m_hWnd, m_LogFont));
  101. m_btFont.SetWindowText(cs);
  102. FillThemes();
  103. FillLanguages();
  104. int caretPos = m_popupPositionCombo.AddString(_T("At Caret"));
  105. m_popupPositionCombo.SetItemData(caretPos, POS_AT_CARET);
  106. int cursorPos = m_popupPositionCombo.AddString(_T("At Cursor"));
  107. m_popupPositionCombo.SetItemData(cursorPos, POS_AT_CURSOR);
  108. int prevPos = m_popupPositionCombo.AddString(_T("At Previous Position"));
  109. m_popupPositionCombo.SetItemData(prevPos, POS_AT_PREVIOUS);
  110. switch (CGetSetOptions::GetQuickPastePosition())
  111. {
  112. case POS_AT_CARET:
  113. m_popupPositionCombo.SetCurSel(caretPos);
  114. break;
  115. case POS_AT_CURSOR:
  116. m_popupPositionCombo.SetCurSel(cursorPos);
  117. break;
  118. case POS_AT_PREVIOUS:
  119. m_popupPositionCombo.SetCurSel(prevPos);
  120. break;
  121. default:
  122. m_popupPositionCombo.SetCurSel(cursorPos);
  123. }
  124. UpdateData(FALSE);
  125. theApp.m_Language.UpdateOptionGeneral(this);
  126. OnEnChangePath();
  127. return TRUE;
  128. }
  129. #define NO_MATCH -2
  130. #define FOUND_MATCH -1
  131. void COptionsGeneral::FillLanguages()
  132. {
  133. CString csFile = CGetSetOptions::GetPath(PATH_LANGUAGE);
  134. csFile += "*.xml";
  135. CString csLanguage = CGetSetOptions::GetLanguageFile();
  136. CFileFind find;
  137. BOOL bCont = find.FindFile(csFile);
  138. int nEnglishIndex = NO_MATCH;
  139. while(bCont)
  140. {
  141. bCont = find.FindNextFile();
  142. int nIndex = m_cbLanguage.AddString(find.GetFileTitle());
  143. if(find.GetFileTitle() == csLanguage)
  144. {
  145. nEnglishIndex = -1;
  146. m_cbLanguage.SetCurSel(nIndex);
  147. }
  148. else if(find.GetFileTitle() == _T("English"))
  149. {
  150. if(nEnglishIndex == NO_MATCH)
  151. nEnglishIndex = nIndex;
  152. }
  153. }
  154. if(nEnglishIndex >= 0)
  155. {
  156. m_cbLanguage.SetCurSel(nEnglishIndex);
  157. }
  158. }
  159. BOOL COptionsGeneral::OnApply()
  160. {
  161. UpdateData();
  162. #ifndef _DEBUG
  163. if (CGetSetOptions::GetIsWindowsApp() == FALSE)
  164. {
  165. CGetSetOptions::SetRunOnStartUp(m_btRunOnStartup.GetCheck());
  166. }
  167. #endif
  168. CGetSetOptions::SetCheckForMaxEntries(m_btMaximumCheck.GetCheck());
  169. CGetSetOptions::SetCheckForExpiredEntries(m_btExpire.GetCheck());
  170. CGetSetOptions::SetMaxEntries(m_eMaxSavedCopies.GetNumber());
  171. CGetSetOptions::SetExpiredEntries(m_eExpireAfter.GetNumber());
  172. CString stringVal;
  173. m_copyAppInclude.GetWindowText(stringVal);
  174. g_Opt.SetCopyAppInclude(stringVal);
  175. m_copyAppExclude.GetWindowText(stringVal);
  176. g_Opt.SetCopyAppExclude(stringVal);
  177. CString csLanguage;
  178. if(m_cbLanguage.GetCurSel() >= 0)
  179. {
  180. m_cbLanguage.GetLBText(m_cbLanguage.GetCurSel(), csLanguage);
  181. g_Opt.SetLanguageFile(csLanguage);
  182. }
  183. if(csLanguage.IsEmpty() == FALSE)
  184. {
  185. if(!theApp.m_Language.LoadLanguageFile(csLanguage))
  186. {
  187. CString cs;
  188. cs.Format(_T("Error loading language file - %s - \n\n%s"), csLanguage, theApp.m_Language.m_csLastError);
  189. MessageBox(cs, _T("Ditto"), MB_OK);
  190. }
  191. }
  192. CString toSavePath;
  193. m_ePath.GetWindowText(toSavePath);
  194. CString resolvedPath = CGetSetOptions::ResolvePath(toSavePath);
  195. bool bOpenNewDatabase = false;
  196. if(resolvedPath.IsEmpty() == FALSE)
  197. {
  198. if(FileExists(resolvedPath) == FALSE)
  199. {
  200. CString cs;
  201. cs.Format(_T("The database %s does not exist.\n\nCreate a new database?"), resolvedPath);
  202. if(MessageBox(cs, _T("Ditto"), MB_YESNO) == IDYES)
  203. {
  204. // -- create a new one
  205. if(CreateDB(resolvedPath))
  206. {
  207. bOpenNewDatabase = true;
  208. }
  209. else
  210. MessageBox(_T("Error Creating Database"));
  211. }
  212. else
  213. return FALSE;
  214. }
  215. else
  216. {
  217. if(ValidDB(resolvedPath) == FALSE)
  218. {
  219. MessageBox(_T("Invalid Database"), _T("Ditto"), MB_OK);
  220. m_ePath.SetFocus();
  221. return FALSE;
  222. }
  223. else
  224. {
  225. bOpenNewDatabase = true;
  226. }
  227. }
  228. if(bOpenNewDatabase)
  229. {
  230. CGetSetOptions::SetDBPath(toSavePath);
  231. if(OpenDatabase(resolvedPath) == FALSE)
  232. {
  233. MessageBox(_T("Error Opening new database"), _T("Ditto"), MB_OK);
  234. m_ePath.SetFocus();
  235. return FALSE;
  236. }
  237. else
  238. {
  239. theApp.RefreshView();
  240. }
  241. }
  242. }
  243. CGetSetOptions::SetQuickPastePosition(m_popupPositionCombo.GetItemData(m_popupPositionCombo.GetCurSel()));
  244. if (m_LogFont.lfWeight != 0)
  245. {
  246. CGetSetOptions::SetFont(m_LogFont);
  247. }
  248. CString currentTheme = g_Opt.GetTheme();
  249. CString csTheme;
  250. if (m_cbTheme.GetCurSel() >= 0)
  251. {
  252. m_cbTheme.GetLBText(m_cbTheme.GetCurSel(), csTheme);
  253. if (csTheme == DEFAULT_THEME)
  254. {
  255. g_Opt.SetTheme("");
  256. csTheme = _T("");
  257. }
  258. else
  259. g_Opt.SetTheme(csTheme);
  260. }
  261. else
  262. {
  263. g_Opt.SetTheme("");
  264. }
  265. if (currentTheme != csTheme)
  266. {
  267. m_pParent->m_themeChanged = TRUE;
  268. }
  269. return CPropertyPage::OnApply();
  270. }
  271. BOOL COptionsGeneral::OnSetActive()
  272. {
  273. return CPropertyPage::OnSetActive();
  274. }
  275. //void COptionsGeneral::OnSetDbPath()
  276. //{
  277. // if(m_btSetDatabasePath.GetCheck() == BST_CHECKED)
  278. // {
  279. // m_ePath.EnableWindow(TRUE);
  280. // m_btGetPath.EnableWindow(TRUE);
  281. // }
  282. // else
  283. // {
  284. // m_ePath.EnableWindow(FALSE);
  285. // m_btGetPath.EnableWindow(FALSE);
  286. // }
  287. //}
  288. void COptionsGeneral::OnGetPath()
  289. {
  290. OPENFILENAME FileName;
  291. TCHAR szFileName[400];
  292. TCHAR szDir[400];
  293. memset(&FileName, 0, sizeof(FileName));
  294. memset(szFileName, 0, sizeof(szFileName));
  295. memset(&szDir, 0, sizeof(szDir));
  296. FileName.lStructSize = sizeof(FileName);
  297. FileName.lpstrTitle = _T("Open Database");
  298. FileName.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR;
  299. FileName.nMaxFile = 400;
  300. FileName.lpstrFile = szFileName;
  301. FileName.lpstrInitialDir = szDir;
  302. FileName.lpstrFilter = _T("Ditto Databases (*.db; *.mdb)\0*.db;*.mdb\0\0");
  303. FileName.lpstrDefExt = _T("db");
  304. FileName.hwndOwner = m_hWnd;
  305. if(GetOpenFileName(&FileName) == 0)
  306. return;
  307. CString csPath(FileName.lpstrFile);
  308. if(FileExists(csPath))
  309. {
  310. if(ValidDB(csPath) == FALSE)
  311. {
  312. MessageBox(_T("Invalid Database"), _T("Ditto"), MB_OK);
  313. m_ePath.SetFocus();
  314. }
  315. else
  316. {
  317. m_ePath.SetWindowText(csPath);
  318. }
  319. }
  320. else
  321. {
  322. m_ePath.SetWindowText(csPath);
  323. }
  324. }
  325. void COptionsGeneral::OnButtonAbout()
  326. {
  327. CDimWnd dim(this->GetParent());
  328. CMultiLanguage Lang;
  329. CString csLanguage;
  330. m_cbLanguage.GetLBText(m_cbLanguage.GetCurSel(), csLanguage);
  331. Lang.SetOnlyGetHeader(true);
  332. if(Lang.LoadLanguageFile(csLanguage))
  333. {
  334. CString csMessage;
  335. csMessage.Format(_T("Language - %s\n")
  336. _T("Version - %d\n")
  337. _T("Author - %s\n")
  338. _T("Notes - %s"), csLanguage,
  339. Lang.GetVersion(),
  340. Lang.GetAuthor(),
  341. Lang.GetNotes());
  342. MessageBox(csMessage, _T("Ditto"), MB_OK);
  343. }
  344. else
  345. {
  346. CString csError;
  347. csError.Format(_T("Error loading language file - %s - reason = "), csLanguage, Lang.m_csLastError);
  348. MessageBox(csError, _T("Ditto"), MB_OK);
  349. }
  350. }
  351. void COptionsGeneral::OnBnClickedButtonAdvanced()
  352. {
  353. CDimWnd dim(this->GetParent());
  354. CAdvGeneral adv(this);
  355. adv.DoModal();
  356. }
  357. HBRUSH COptionsGeneral::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
  358. {
  359. HBRUSH hbr = CPropertyPage::OnCtlColor(pDC, pWnd, nCtlColor);
  360. switch(nCtlColor)
  361. {
  362. case CTLCOLOR_DLG: // dialog
  363. case CTLCOLOR_STATIC: // static, checkbox, read-only edit, etc.
  364. //pDC->SetBkColor(RGB(251, 251, 251));
  365. //hbr = m_brush;
  366. break;
  367. }
  368. return hbr;
  369. }
  370. void COptionsGeneral::FillThemes()
  371. {
  372. CString csFile = CGetSetOptions::GetPath(PATH_THEMES);
  373. csFile += "*.xml";
  374. CString csTheme = CGetSetOptions::GetTheme();
  375. CFileFind find;
  376. BOOL bCont = find.FindFile(csFile);
  377. bool bSetCurSel = false;
  378. while (bCont)
  379. {
  380. bCont = find.FindNextFile();
  381. CTheme theme;
  382. if (theme.Load(find.GetFileTitle(), true, false))
  383. {
  384. if (theme.FileVersion() >= 2 && theme.FileVersion() < 100)
  385. {
  386. int nIndex = m_cbTheme.AddString(find.GetFileTitle());
  387. if (find.GetFileTitle() == csTheme)
  388. {
  389. m_cbTheme.SetCurSel(nIndex);
  390. bSetCurSel = true;
  391. }
  392. }
  393. }
  394. }
  395. int nIndex = m_cbTheme.AddString(DEFAULT_THEME);
  396. if (bSetCurSel == false)
  397. {
  398. m_cbTheme.SetCurSel(nIndex);
  399. }
  400. }
  401. void COptionsGeneral::OnBnClickedButtonTheme()
  402. {
  403. CDimWnd dim(this->GetParent());
  404. CTheme theme;
  405. CString csTheme;
  406. m_cbTheme.GetLBText(m_cbTheme.GetCurSel(), csTheme);
  407. if (csTheme == DEFAULT_THEME)
  408. return;
  409. if (theme.Load(csTheme, true, false))
  410. {
  411. CString csMessage;
  412. csMessage.Format(_T("Theme - %s\n")
  413. _T("Version - %d\n")
  414. _T("Author - %s\n")
  415. _T("Notes - %s"), csTheme,
  416. theme.FileVersion(),
  417. theme.Author(),
  418. theme.Notes());
  419. MessageBox(csMessage, _T("Ditto"), MB_OK);
  420. }
  421. else
  422. {
  423. CString csError;
  424. csError.Format(_T("Error loading theme file - %s - reason = "), csTheme, theme.LastError());
  425. MessageBox(csError, _T("Ditto"), MB_OK);
  426. }
  427. }
  428. void COptionsGeneral::OnBnClickedButtonDefaultFault()
  429. {
  430. memset(&m_LogFont, 0, sizeof(m_LogFont));
  431. m_LogFont.lfHeight = -13;
  432. m_LogFont.lfWeight = 400;
  433. m_LogFont.lfCharSet = 1;
  434. STRCPY(m_LogFont.lfFaceName, _T("Segoe UI"));
  435. CString cs;
  436. cs.Format(_T("Font - %s (%d)"), m_LogFont.lfFaceName, GetFontSize(m_hWnd, m_LogFont));
  437. m_btFont.SetWindowText(cs);
  438. }
  439. int COptionsGeneral::GetFontSize(HWND hWnd, const LOGFONT& lf)
  440. {
  441. int nFontSize = 0;
  442. HDC hDC = ::GetWindowDC(hWnd);
  443. if (lf.lfHeight < 0)
  444. {
  445. nFontSize = -::MulDiv(lf.lfHeight, 72, ::GetDeviceCaps(hDC, LOGPIXELSY));
  446. }
  447. else
  448. {
  449. TEXTMETRIC tm;
  450. ::ZeroMemory(&tm, sizeof(TEXTMETRIC));
  451. ::GetTextMetrics(hDC, &tm);
  452. nFontSize = ::MulDiv(lf.lfHeight - tm.tmInternalLeading, 72, ::GetDeviceCaps(hDC, LOGPIXELSY));
  453. }
  454. ::ReleaseDC(hWnd, hDC);
  455. return nFontSize;
  456. }
  457. void COptionsGeneral::OnBnClickedButtonFont()
  458. {
  459. CFontDialog dlg(&m_LogFont, (CF_TTONLY | CF_SCREENFONTS), 0, this);
  460. if (dlg.DoModal() == IDOK)
  461. {
  462. memcpy(&m_LogFont, dlg.m_cf.lpLogFont, sizeof(LOGFONT));
  463. CString cs;
  464. cs.Format(_T("Font - %s (%d)"), m_LogFont.lfFaceName, GetFontSize(m_hWnd, m_LogFont));
  465. m_btFont.SetWindowText(cs);
  466. }
  467. }
  468. //void COptionsGeneral::OnNMClickSyslinkEnvVarInfo(NMHDR *pNMHDR, LRESULT *pResult)
  469. //{
  470. // CString url = _T("https:////sourceforge.net//p//ditto-cp//wiki//EnvironmentVariables//");
  471. //
  472. // CHyperLink::GotoURL(url, SW_SHOW);
  473. //
  474. // *pResult = 0;
  475. //}
  476. //
  477. //
  478. //void COptionsGeneral::OnEnChangePath()
  479. //{
  480. // // TODO: If this is a RICHEDIT control, the control will not
  481. // // send this notification unless you override the CPropertyPage::OnInitDialog()
  482. // // function and call CRichEditCtrl().SetEventMask()
  483. // // with the ENM_CHANGE flag ORed into the mask.
  484. //
  485. // // TODO: Add your control notification handler code here
  486. //}
  487. void COptionsGeneral::OnEnChangePath()
  488. {
  489. // TODO: If this is a RICHEDIT control, the control will not
  490. // send this notification unless you override the CPropertyPage::OnInitDialog()
  491. // function and call CRichEditCtrl().SetEventMask()
  492. // with the ENM_CHANGE flag ORed into the mask.
  493. // TODO: Add your control notification handler code here
  494. CString toSavePath;
  495. m_ePath.GetWindowText(toSavePath);
  496. if (toSavePath.Find(_T("%")) >= 0)
  497. {
  498. CString resolvedPath = CGetSetOptions::ResolvePath(toSavePath);
  499. m_envVarLink.SetWindowText(resolvedPath);
  500. }
  501. else
  502. {
  503. m_envVarLink.SetWindowText(_T("Environment Variables"));
  504. }
  505. }