OptionsGeneral.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 "AccessToSqlite.h"
  11. #include "AdvGeneral.h"
  12. #include "DimWnd.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. }
  56. BEGIN_MESSAGE_MAP(COptionsGeneral, CPropertyPage)
  57. //{{AFX_MSG_MAP(COptionsGeneral)
  58. ON_BN_CLICKED(IDC_GET_PATH, OnGetPath)
  59. ON_BN_CLICKED(IDC_BUTTON_ABOUT, OnButtonAbout)
  60. //}}AFX_MSG_MAP
  61. ON_BN_CLICKED(IDC_BUTTON_ADVANCED, &COptionsGeneral::OnBnClickedButtonAdvanced)
  62. ON_WM_CTLCOLOR()
  63. ON_BN_CLICKED(IDC_BUTTON_THEME, &COptionsGeneral::OnBnClickedButtonTheme)
  64. ON_BN_CLICKED(IDC_BUTTON_DEFAULT_FAULT, &COptionsGeneral::OnBnClickedButtonDefaultFault)
  65. ON_BN_CLICKED(IDC_BUTTON_FONT, &COptionsGeneral::OnBnClickedButtonFont)
  66. END_MESSAGE_MAP()
  67. /////////////////////////////////////////////////////////////////////////////
  68. // COptionsGeneral message handlers
  69. BOOL COptionsGeneral::OnInitDialog()
  70. {
  71. CPropertyPage::OnInitDialog();
  72. m_brush.CreateSolidBrush(RGB(251, 251, 251));
  73. m_pParent = (COptionsSheet *)GetParent();
  74. #ifndef _DEBUG
  75. if (CGetSetOptions::GetIsWindowsApp())
  76. {
  77. m_btRunOnStartup.SetCheck(BST_CHECKED);
  78. GetDlgItem(IDC_START_ON_STARTUP)->EnableWindow(FALSE);
  79. }
  80. else
  81. {
  82. m_btRunOnStartup.SetCheck(CGetSetOptions::GetRunOnStartUp());
  83. }
  84. #endif
  85. m_btMaximumCheck.SetCheck(CGetSetOptions::GetCheckForMaxEntries());
  86. m_btExpire.SetCheck(CGetSetOptions::GetCheckForExpiredEntries());
  87. m_eExpireAfter.SetNumber(CGetSetOptions::GetExpiredEntries());
  88. m_eMaxSavedCopies.SetNumber(CGetSetOptions::GetMaxEntries());
  89. m_copyAppInclude.SetWindowText(g_Opt.GetCopyAppInclude());
  90. m_copyAppExclude.SetWindowText(g_Opt.GetCopyAppExclude());
  91. CString csPath = CGetSetOptions::GetDBPath();
  92. m_ePath.SetWindowText(csPath);
  93. if (CGetSetOptions::GetFont(m_LogFont))
  94. {
  95. m_Font.CreateFontIndirect(&m_LogFont);
  96. m_btFont.SetFont(&m_Font);
  97. }
  98. else
  99. {
  100. CFont *ft = m_btFont.GetFont();
  101. ft->GetLogFont(&m_LogFont);
  102. }
  103. CString cs;
  104. cs.Format(_T("Font - %s (%d)"), m_LogFont.lfFaceName, abs(theApp.m_metrics.PixelsToPoints(m_LogFont.lfHeight)));
  105. m_btFont.SetWindowText(cs);
  106. FillThemes();
  107. FillLanguages();
  108. int caretPos = m_popupPositionCombo.AddString(_T("At Caret"));
  109. m_popupPositionCombo.SetItemData(caretPos, POS_AT_CARET);
  110. int cursorPos = m_popupPositionCombo.AddString(_T("At Cursor"));
  111. m_popupPositionCombo.SetItemData(cursorPos, POS_AT_CURSOR);
  112. int prevPos = m_popupPositionCombo.AddString(_T("At Previous Position"));
  113. m_popupPositionCombo.SetItemData(prevPos, POS_AT_PREVIOUS);
  114. switch (CGetSetOptions::GetQuickPastePosition())
  115. {
  116. case POS_AT_CARET:
  117. m_popupPositionCombo.SetCurSel(caretPos);
  118. break;
  119. case POS_AT_CURSOR:
  120. m_popupPositionCombo.SetCurSel(cursorPos);
  121. break;
  122. case POS_AT_PREVIOUS:
  123. m_popupPositionCombo.SetCurSel(prevPos);
  124. break;
  125. default:
  126. m_popupPositionCombo.SetCurSel(cursorPos);
  127. }
  128. UpdateData(FALSE);
  129. theApp.m_Language.UpdateOptionGeneral(this);
  130. return TRUE;
  131. }
  132. #define NO_MATCH -2
  133. #define FOUND_MATCH -1
  134. void COptionsGeneral::FillLanguages()
  135. {
  136. CString csFile = CGetSetOptions::GetPath(PATH_LANGUAGE);
  137. csFile += "*.xml";
  138. CString csLanguage = CGetSetOptions::GetLanguageFile();
  139. CFileFind find;
  140. BOOL bCont = find.FindFile(csFile);
  141. int nEnglishIndex = NO_MATCH;
  142. while(bCont)
  143. {
  144. bCont = find.FindNextFile();
  145. int nIndex = m_cbLanguage.AddString(find.GetFileTitle());
  146. if(find.GetFileTitle() == csLanguage)
  147. {
  148. nEnglishIndex = -1;
  149. m_cbLanguage.SetCurSel(nIndex);
  150. }
  151. else if(find.GetFileTitle() == _T("English"))
  152. {
  153. if(nEnglishIndex == NO_MATCH)
  154. nEnglishIndex = nIndex;
  155. }
  156. }
  157. if(nEnglishIndex >= 0)
  158. {
  159. m_cbLanguage.SetCurSel(nEnglishIndex);
  160. }
  161. }
  162. BOOL COptionsGeneral::OnApply()
  163. {
  164. UpdateData();
  165. #ifndef _DEBUG
  166. if (CGetSetOptions::GetIsWindowsApp() == FALSE)
  167. {
  168. CGetSetOptions::SetRunOnStartUp(m_btRunOnStartup.GetCheck());
  169. }
  170. #endif
  171. CGetSetOptions::SetCheckForMaxEntries(m_btMaximumCheck.GetCheck());
  172. CGetSetOptions::SetCheckForExpiredEntries(m_btExpire.GetCheck());
  173. CGetSetOptions::SetMaxEntries(m_eMaxSavedCopies.GetNumber());
  174. CGetSetOptions::SetExpiredEntries(m_eExpireAfter.GetNumber());
  175. CString stringVal;
  176. m_copyAppInclude.GetWindowText(stringVal);
  177. g_Opt.SetCopyAppInclude(stringVal);
  178. m_copyAppExclude.GetWindowText(stringVal);
  179. g_Opt.SetCopyAppExclude(stringVal);
  180. CString csLanguage;
  181. if(m_cbLanguage.GetCurSel() >= 0)
  182. {
  183. m_cbLanguage.GetLBText(m_cbLanguage.GetCurSel(), csLanguage);
  184. g_Opt.SetLanguageFile(csLanguage);
  185. }
  186. if(csLanguage.IsEmpty() == FALSE)
  187. {
  188. if(!theApp.m_Language.LoadLanguageFile(csLanguage))
  189. {
  190. CString cs;
  191. cs.Format(_T("Error loading language file - %s - \n\n%s"), csLanguage, theApp.m_Language.m_csLastError);
  192. MessageBox(cs, _T("Ditto"), MB_OK);
  193. }
  194. }
  195. CString csPath;
  196. m_ePath.GetWindowText(csPath);
  197. bool bOpenNewDatabase = false;
  198. if(csPath.IsEmpty() == FALSE)
  199. {
  200. if(FileExists(csPath) == FALSE)
  201. {
  202. CString cs;
  203. cs.Format(_T("The database %s does not exist.\n\nCreate a new database?"), csPath);
  204. if(MessageBox(cs, _T("Ditto"), MB_YESNO) == IDYES)
  205. {
  206. // -- create a new one
  207. if(CreateDB(csPath))
  208. {
  209. bOpenNewDatabase = true;
  210. }
  211. else
  212. MessageBox(_T("Error Creating Database"));
  213. }
  214. else
  215. return FALSE;
  216. }
  217. else
  218. {
  219. if(ValidDB(csPath) == FALSE)
  220. {
  221. MessageBox(_T("Invalid Database"), _T("Ditto"), MB_OK);
  222. m_ePath.SetFocus();
  223. return FALSE;
  224. }
  225. else
  226. {
  227. bOpenNewDatabase = true;
  228. }
  229. }
  230. if(bOpenNewDatabase)
  231. {
  232. if(OpenDatabase(csPath) == FALSE)
  233. {
  234. MessageBox(_T("Error Opening new database"), _T("Ditto"), MB_OK);
  235. m_ePath.SetFocus();
  236. return FALSE;
  237. }
  238. else
  239. {
  240. theApp.RefreshView();
  241. }
  242. }
  243. }
  244. CGetSetOptions::SetQuickPastePosition(m_popupPositionCombo.GetItemData(m_popupPositionCombo.GetCurSel()));
  245. if (m_LogFont.lfWeight != 0)
  246. {
  247. CGetSetOptions::SetFont(m_LogFont);
  248. }
  249. CString currentTheme = g_Opt.GetTheme();
  250. CString csTheme;
  251. if (m_cbTheme.GetCurSel() >= 0)
  252. {
  253. m_cbTheme.GetLBText(m_cbTheme.GetCurSel(), csTheme);
  254. if (csTheme == DEFAULT_THEME)
  255. {
  256. g_Opt.SetTheme("");
  257. csTheme = _T("");
  258. }
  259. else
  260. g_Opt.SetTheme(csTheme);
  261. }
  262. else
  263. {
  264. g_Opt.SetTheme("");
  265. }
  266. if (currentTheme != csTheme)
  267. {
  268. m_pParent->m_themeChanged = TRUE;
  269. }
  270. return CPropertyPage::OnApply();
  271. }
  272. BOOL COptionsGeneral::OnSetActive()
  273. {
  274. return CPropertyPage::OnSetActive();
  275. }
  276. //void COptionsGeneral::OnSetDbPath()
  277. //{
  278. // if(m_btSetDatabasePath.GetCheck() == BST_CHECKED)
  279. // {
  280. // m_ePath.EnableWindow(TRUE);
  281. // m_btGetPath.EnableWindow(TRUE);
  282. // }
  283. // else
  284. // {
  285. // m_ePath.EnableWindow(FALSE);
  286. // m_btGetPath.EnableWindow(FALSE);
  287. // }
  288. //}
  289. void COptionsGeneral::OnGetPath()
  290. {
  291. OPENFILENAME FileName;
  292. TCHAR szFileName[400];
  293. TCHAR szDir[400];
  294. memset(&FileName, 0, sizeof(FileName));
  295. memset(szFileName, 0, sizeof(szFileName));
  296. memset(&szDir, 0, sizeof(szDir));
  297. FileName.lStructSize = sizeof(FileName);
  298. FileName.lpstrTitle = _T("Open Database");
  299. FileName.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR;
  300. FileName.nMaxFile = 400;
  301. FileName.lpstrFile = szFileName;
  302. FileName.lpstrInitialDir = szDir;
  303. FileName.lpstrFilter = _T("Ditto Databases (*.db; *.mdb)\0*.db;*.mdb\0\0");
  304. FileName.lpstrDefExt = _T("db");
  305. FileName.hwndOwner = m_hWnd;
  306. if(GetOpenFileName(&FileName) == 0)
  307. return;
  308. CString csPath(FileName.lpstrFile);
  309. CPath path(FileName.lpstrFile);
  310. if(path.GetExtension() == _T("mdb"))
  311. {
  312. CString cs;
  313. cs.Format(_T("The database '%s' must be converted to a Sqlite Database (Version 3 format).\n\nConvert database?"), FileName.lpstrFile);
  314. if(MessageBox(cs, _T("Ditto"), MB_YESNO) == IDNO)
  315. return;
  316. CString csNewDBPath = path.RemoveExtension();
  317. //Make sure the db name is unique
  318. CString csTempName;
  319. csTempName.Format(_T("%s.db"), csNewDBPath);
  320. int i = 1;
  321. while(FileExists(csTempName))
  322. {
  323. csTempName.Format(_T("%s_%d.db"), csNewDBPath, i);
  324. i++;
  325. }
  326. csNewDBPath = csTempName;
  327. CreateDB(csNewDBPath);
  328. CAccessToSqlite Convert;
  329. if(Convert.ConvertDatabase(csNewDBPath, FileName.lpstrFile))
  330. {
  331. csPath = csNewDBPath;
  332. }
  333. else
  334. {
  335. MessageBox(_T("Error converting database."), _T("Ditto"), MB_OK);
  336. DeleteFile(csNewDBPath);
  337. return;
  338. }
  339. }
  340. if(FileExists(csPath))
  341. {
  342. if(ValidDB(csPath) == FALSE)
  343. {
  344. MessageBox(_T("Invalid Database"), _T("Ditto"), MB_OK);
  345. m_ePath.SetFocus();
  346. }
  347. else
  348. {
  349. m_ePath.SetWindowText(csPath);
  350. }
  351. }
  352. else
  353. {
  354. m_ePath.SetWindowText(csPath);
  355. }
  356. }
  357. void COptionsGeneral::OnButtonAbout()
  358. {
  359. CDimWnd dim(this->GetParent());
  360. CMultiLanguage Lang;
  361. CString csLanguage;
  362. m_cbLanguage.GetLBText(m_cbLanguage.GetCurSel(), csLanguage);
  363. Lang.SetOnlyGetHeader(true);
  364. if(Lang.LoadLanguageFile(csLanguage))
  365. {
  366. CString csMessage;
  367. csMessage.Format(_T("Language - %s\n")
  368. _T("Version - %d\n")
  369. _T("Author - %s\n")
  370. _T("Notes - %s"), csLanguage,
  371. Lang.GetVersion(),
  372. Lang.GetAuthor(),
  373. Lang.GetNotes());
  374. MessageBox(csMessage, _T("Ditto"), MB_OK);
  375. }
  376. else
  377. {
  378. CString csError;
  379. csError.Format(_T("Error loading language file - %s - reason = "), csLanguage, Lang.m_csLastError);
  380. MessageBox(csError, _T("Ditto"), MB_OK);
  381. }
  382. }
  383. void COptionsGeneral::OnBnClickedButtonAdvanced()
  384. {
  385. CDimWnd dim(this->GetParent());
  386. CAdvGeneral adv(this);
  387. adv.DoModal();
  388. }
  389. HBRUSH COptionsGeneral::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
  390. {
  391. HBRUSH hbr = CPropertyPage::OnCtlColor(pDC, pWnd, nCtlColor);
  392. switch(nCtlColor)
  393. {
  394. case CTLCOLOR_DLG: // dialog
  395. case CTLCOLOR_STATIC: // static, checkbox, read-only edit, etc.
  396. //pDC->SetBkColor(RGB(251, 251, 251));
  397. //hbr = m_brush;
  398. break;
  399. }
  400. return hbr;
  401. }
  402. void COptionsGeneral::FillThemes()
  403. {
  404. CString csFile = CGetSetOptions::GetPath(PATH_THEMES);
  405. csFile += "*.xml";
  406. CString csTheme = CGetSetOptions::GetTheme();
  407. CFileFind find;
  408. BOOL bCont = find.FindFile(csFile);
  409. bool bSetCurSel = false;
  410. while (bCont)
  411. {
  412. bCont = find.FindNextFile();
  413. CTheme theme;
  414. if (theme.Load(find.GetFileTitle(), true, false))
  415. {
  416. if (theme.FileVersion() >= 2 && theme.FileVersion() < 100)
  417. {
  418. int nIndex = m_cbTheme.AddString(find.GetFileTitle());
  419. if (find.GetFileTitle() == csTheme)
  420. {
  421. m_cbTheme.SetCurSel(nIndex);
  422. bSetCurSel = true;
  423. }
  424. }
  425. }
  426. }
  427. int nIndex = m_cbTheme.AddString(DEFAULT_THEME);
  428. if (bSetCurSel == false)
  429. {
  430. m_cbTheme.SetCurSel(nIndex);
  431. }
  432. }
  433. void COptionsGeneral::OnBnClickedButtonTheme()
  434. {
  435. CDimWnd dim(this->GetParent());
  436. CTheme theme;
  437. CString csTheme;
  438. m_cbTheme.GetLBText(m_cbTheme.GetCurSel(), csTheme);
  439. if (csTheme == DEFAULT_THEME)
  440. return;
  441. if (theme.Load(csTheme, true, false))
  442. {
  443. CString csMessage;
  444. csMessage.Format(_T("Theme - %s\n")
  445. _T("Version - %d\n")
  446. _T("Author - %s\n")
  447. _T("Notes - %s"), csTheme,
  448. theme.FileVersion(),
  449. theme.Author(),
  450. theme.Notes());
  451. MessageBox(csMessage, _T("Ditto"), MB_OK);
  452. }
  453. else
  454. {
  455. CString csError;
  456. csError.Format(_T("Error loading theme file - %s - reason = "), csTheme, theme.LastError());
  457. MessageBox(csError, _T("Ditto"), MB_OK);
  458. }
  459. }
  460. void COptionsGeneral::OnBnClickedButtonDefaultFault()
  461. {
  462. CFont *ft = m_btDefaultButton.GetFont();
  463. ft->GetLogFont(&m_LogFont);
  464. memset(&m_LogFont, 0, sizeof(m_LogFont));
  465. m_LogFont.lfHeight = -theApp.m_metrics.PointsToPixels(10);
  466. m_LogFont.lfWeight = 400;
  467. m_LogFont.lfCharSet = 1;
  468. STRCPY(m_LogFont.lfFaceName, _T("Segoe UI"));
  469. m_Font.DeleteObject();
  470. m_Font.CreateFontIndirect(&m_LogFont);
  471. m_btFont.SetFont(&m_Font);
  472. CString cs;
  473. cs.Format(_T("Font - %s (%d)"), m_LogFont.lfFaceName, abs(theApp.m_metrics.PixelsToPoints(m_LogFont.lfHeight)));
  474. m_btFont.SetWindowText(cs);
  475. this->SetFont(&m_Font);
  476. }
  477. void COptionsGeneral::OnBnClickedButtonFont()
  478. {
  479. CFontDialog dlg(&m_LogFont, (CF_TTONLY | CF_SCREENFONTS), 0, this);
  480. if (dlg.DoModal() == IDOK)
  481. {
  482. m_Font.DeleteObject();
  483. memcpy(&m_LogFont, dlg.m_cf.lpLogFont, sizeof(LOGFONT));
  484. m_Font.CreateFontIndirect(&m_LogFont);
  485. m_btFont.SetFont(&m_Font);
  486. CString cs;
  487. cs.Format(_T("Font - %s (%d)"), m_LogFont.lfFaceName, abs(theApp.m_metrics.PixelsToPoints(m_LogFont.lfHeight)));
  488. m_btFont.SetWindowText(cs);
  489. }
  490. }