OSDN Git Service

refactor
[winmerge-jp/winmerge-jp.git] / Src / PropColorSchemes.cpp
1 /** 
2  * @file  PropColorSchemes.cpp
3  *
4  * @brief Implementation of PropColorSchemes propertysheet
5  */
6
7 #include "stdafx.h"
8 #include "PropColorSchemes.h"
9 #include "OptionsDef.h"
10 #include "OptionsInit.h"
11 #include "OptionsMgr.h"
12 #include "RegOptionsMgr.h"
13 #include "OptionsPanel.h"
14 #include "DirItem.h"
15 #include "DirTravel.h"
16 #include "paths.h"
17 #include "Environment.h"
18
19 #ifdef _DEBUG
20 #define new DEBUG_NEW
21 #endif
22
23 /** 
24  * @brief Default constructor.
25  */
26 PropColorSchemes::PropColorSchemes(COptionsMgr *optionsMgr)
27  : OptionsPanel(optionsMgr, PropColorSchemes::IDD)
28 {
29 }
30
31 void PropColorSchemes::DoDataExchange(CDataExchange* pDX)
32 {
33         CDialog::DoDataExchange(pDX);
34         //{{AFX_DATA_MAP(PropColorSchemes)
35         //}}AFX_DATA_MAP
36 }
37
38
39 BEGIN_MESSAGE_MAP(PropColorSchemes, OptionsPanel)
40         //{{AFX_MSG_MAP(PropColorSchemes)
41         ON_CBN_SELCHANGE(IDC_COLOR_SCHEMES, OnCbnSelchangeColorSchemes)
42         //}}AFX_MSG_MAP
43 END_MESSAGE_MAP()
44
45 /** 
46  * @brief Reads options values from storage to UI.
47  */
48 void PropColorSchemes::ReadOptions()
49 {
50         m_sColorScheme = GetOptionsMgr()->GetString(OPT_COLOR_SCHEME);
51 }
52
53 /** 
54  * @brief Writes options values from UI to storage.
55  */
56 void PropColorSchemes::WriteOptions()
57 {
58         GetOptionsMgr()->SaveOption(OPT_COLOR_SCHEME, m_sColorScheme);
59 }
60
61 static String GetColorSchemesFolder()
62 {
63         return paths::ConcatPath(env::GetProgPath(), _T("ColorSchemes"));
64 }
65
66 static std::vector<String> GetColorSchemeNames()
67 {
68         DirItemArray dirs, files;
69         std::vector<String> names;
70
71         LoadAndSortFiles(GetColorSchemesFolder(), &dirs, &files, false);
72
73         for (DirItem& item : files)
74         {
75                 String filename;
76                 String ext;
77                 paths::SplitFilename(item.filename, nullptr, &filename, &ext);
78                 if (strutils::compare_nocase(ext, _T("ini")) == 0)
79                         names.push_back(filename);
80         }
81
82         return names;
83 }
84
85 /** 
86  * @brief Called before propertysheet is drawn.
87  */
88 BOOL PropColorSchemes::OnInitDialog()
89 {
90         CComboBox * combo = (CComboBox*) GetDlgItem(IDC_COLOR_SCHEMES);
91
92         for (auto& name : GetColorSchemeNames())
93         {
94                 combo->AddString(name.c_str());
95                 if (strutils::compare_nocase(name, m_sColorScheme) == 0)
96                         combo->SetCurSel(combo->GetCount() - 1);
97         }
98         if (combo->GetCurSel() == -1 && combo->GetCount() > 0)
99                 combo->SetCurSel(0);
100
101         OptionsPanel::OnInitDialog();
102         return TRUE;  // return TRUE unless you set the focus to a control
103 }
104
105 void PropColorSchemes::OnCbnSelchangeColorSchemes()
106 {
107         String sColorScheme;
108         GetDlgItemText(IDC_COLOR_SCHEMES, sColorScheme);
109         m_sColorScheme = sColorScheme;
110         WriteOptions();
111         String path = paths::ConcatPath(GetColorSchemesFolder(), sColorScheme + _T(".ini"));
112         if (GetOptionsMgr()->ImportOptions(path) != COption::OPT_OK)
113         {
114                 LangMessageBox(IDS_OPT_IMPORT_ERR, MB_ICONWARNING);
115                 return;
116         }
117         GetParent()->GetParent()->PostMessage(WM_APP + IDC_COLOR_SCHEMES);
118 }
119