OptionsGeneral.cpp 13 KB

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