OSDN Git Service

Avoid an assertion failure when loading settings from winmerge.ini
[winmerge-jp/winmerge-jp.git] / Src / PropTextColors.cpp
1 /** 
2  * @file  PropTextColors.cpp
3  *
4  * @brief Implementation of PropTextColors propertysheet
5  */
6
7 #include "stdafx.h"
8 #include "PropTextColors.h"
9 #include "SyntaxColors.h"
10 #include "OptionsCustomColors.h"
11 #include "OptionsDef.h"
12 #include "OptionsMgr.h"
13 #include "OptionsPanel.h"
14
15 #ifdef _DEBUG
16 #define new DEBUG_NEW
17 #endif
18
19 /** @brief Section name for settings in registry. */
20 static const TCHAR Section[] = _T("Custom Colors");
21
22 /** 
23  * @brief Default constructor.
24  */
25 PropTextColors::PropTextColors(COptionsMgr *optionsMgr, SyntaxColors *pColors)
26  : OptionsPanel(optionsMgr, PropTextColors::IDD)
27 , m_bCustomColors(false)
28 , m_pTempColors(pColors)
29 , m_cCustColors()
30 {
31 }
32
33 void PropTextColors::DoDataExchange(CDataExchange* pDX)
34 {
35         CDialog::DoDataExchange(pDX);
36         //{{AFX_DATA_MAP(PropTextColors)
37         DDX_Check(pDX, IDC_DEFAULT_STANDARD_COLORS, m_bCustomColors);
38         DDX_Control(pDX, IDC_WHITESPACE_BKGD_COLOR, m_btnWhitespaceBackground);
39         DDX_Control(pDX, IDC_REGULAR_BKGD_COLOR, m_btnRegularBackground);
40         DDX_Control(pDX, IDC_REGULAR_TEXT_COLOR, m_btnRegularText);
41         DDX_Control(pDX, IDC_SELECTION_BKGD_COLOR, m_btnSelectionBackground);
42         DDX_Control(pDX, IDC_SELECTION_TEXT_COLOR, m_btnSelectionText);
43         DDX_Control(pDX, IDC_MARGIN_BKGD_COLOR, m_btnMarginBackground);
44         //}}AFX_DATA_MAP
45         EnableColorButtons(m_bCustomColors);
46 }
47
48
49 BEGIN_MESSAGE_MAP(PropTextColors, OptionsPanel)
50         //{{AFX_MSG_MAP(PropTextColors)
51         ON_BN_CLICKED(IDC_DEFAULT_STANDARD_COLORS, OnDefaultsStandardColors)
52         ON_BN_CLICKED(IDC_WHITESPACE_BKGD_COLOR, OnWhitespaceBackgroundColor)
53         ON_BN_CLICKED(IDC_REGULAR_BKGD_COLOR, OnRegularBackgroundColor)
54         ON_BN_CLICKED(IDC_REGULAR_TEXT_COLOR, OnRegularTextColor)
55         ON_BN_CLICKED(IDC_SELECTION_BKGD_COLOR, OnSelectionBackgroundColor)
56         ON_BN_CLICKED(IDC_SELECTION_TEXT_COLOR, OnSelectionTextColor)
57         ON_BN_CLICKED(IDC_MARGIN_BKGD_COLOR, OnMarginBackgroundColor)
58         //}}AFX_MSG_MAP
59 END_MESSAGE_MAP()
60
61 /** 
62  * @brief Reads options values from storage to UI.
63  * (Property sheet calls this before displaying all property pages)
64  */
65 void PropTextColors::ReadOptions()
66 {
67         m_bCustomColors = GetOptionsMgr()->GetBool(OPT_CLR_DEFAULT_TEXT_COLORING) ? false : true;
68         SerializeColorsToFromScreen(LOAD_COLORS);
69 }
70
71 /** 
72  * @brief Writes options values from UI to storage.
73  * (Property sheet calls this after displaying all property pages)
74  */
75 void PropTextColors::WriteOptions()
76 {
77         GetOptionsMgr()->SaveOption(OPT_CLR_DEFAULT_TEXT_COLORING, !m_bCustomColors);
78         // User can only change colors via BrowseColorAndSave,
79         // which writes to m_pTempColors
80         // so user's latest choices are in m_pTempColors
81         // (we don't have to read them from screen)
82
83         // Also, CPropSyntaxColors writes m_pTempColors out, so we don't have to
84         // We share m_pTempColors with CPropSyntaxColors
85 }
86
87 /** 
88  * @brief Let user browse common color dialog, and select a color
89  * @param [in] colorButton Button for which to change color.
90  * @param [in] colorIndex Index to color table.
91  */
92 void PropTextColors::BrowseColorAndSave(CColorButton & colorButton, int colorIndex)
93 {
94         // Ignore user if colors are slaved to system
95         if (IsDlgButtonChecked(IDC_DEFAULT_STANDARD_COLORS) == BST_UNCHECKED)
96                 return;
97
98         COLORREF currentColor = m_pTempColors->GetColor(colorIndex);
99         CColorDialog dialog(currentColor);
100         Options::CustomColors::Load(GetOptionsMgr(), m_cCustColors.data());
101         dialog.m_cc.lpCustColors = m_cCustColors.data();
102         
103         if (dialog.DoModal() == IDOK)
104         {
105                 currentColor = dialog.GetColor();
106                 colorButton.SetColor(currentColor);
107                 m_pTempColors->SetColor(colorIndex, currentColor);
108         }
109         Options::CustomColors::Save(GetOptionsMgr(), m_cCustColors.data());
110 }
111
112 /** 
113  * @brief User wants to change whitespace color
114  */
115 void PropTextColors::OnWhitespaceBackgroundColor() 
116 {
117         BrowseColorAndSave(m_btnWhitespaceBackground, COLORINDEX_WHITESPACE);
118 }
119
120 /** 
121  * @brief User wants to change regular background color
122  */
123 void PropTextColors::OnRegularBackgroundColor() 
124 {
125         BrowseColorAndSave(m_btnRegularBackground, COLORINDEX_BKGND);
126 }
127
128 /** 
129  * @brief User wants to change regular text color
130  */
131 void PropTextColors::OnRegularTextColor() 
132 {
133         BrowseColorAndSave(m_btnRegularText, COLORINDEX_NORMALTEXT);
134 }
135
136 /** 
137  * @brief User wants to change regular selection background color
138  */
139 void PropTextColors::OnSelectionBackgroundColor() 
140 {
141         BrowseColorAndSave(m_btnSelectionBackground, COLORINDEX_SELBKGND);
142 }
143
144 /** 
145  * @brief User wants to change regular selection text color
146  */
147 void PropTextColors::OnSelectionTextColor() 
148 {
149         BrowseColorAndSave(m_btnSelectionText, COLORINDEX_SELTEXT);
150 }
151
152 /** 
153  * @brief User wants to change margin background color
154  */
155 void PropTextColors::OnMarginBackgroundColor() 
156 {
157         BrowseColorAndSave(m_btnMarginBackground, COLORINDEX_SELMARGIN);
158 }
159
160 /**
161  * @brief Load all colors, Save all colors, or set all colors to default
162  * @param [in] op Operation to do, one of
163  *  - SET_DEFAULTS : Sets colors to defaults
164  *  - LOAD_COLORS : Loads colors from registry
165  * (No save operation because BrowseColorAndSave saves immediately when user chooses)
166  */
167 void PropTextColors::SerializeColorsToFromScreen(OPERATION op)
168 {
169         if (op == SET_DEFAULTS)
170                 m_pTempColors->SetDefaults();
171
172         SerializeColorToFromScreen(op, m_btnWhitespaceBackground, COLORINDEX_WHITESPACE);
173
174         SerializeColorToFromScreen(op, m_btnRegularBackground, COLORINDEX_BKGND);
175         SerializeColorToFromScreen(op, m_btnRegularText, COLORINDEX_NORMALTEXT);
176
177         SerializeColorToFromScreen(op, m_btnSelectionBackground, COLORINDEX_SELBKGND);
178         SerializeColorToFromScreen(op, m_btnSelectionText, COLORINDEX_SELTEXT);
179
180         SerializeColorToFromScreen(op, m_btnMarginBackground, COLORINDEX_SELMARGIN);
181 }
182
183 /**
184  * @brief Load color to button, Save color from button, or set button color to default
185  * @param [in] op Operation to do, one of
186  *  - SET_DEFAULTS : Sets colors to defaults
187  *  - LOAD_COLORS : Loads colors from registry
188  * (No save operation because BrowseColorAndSave saves immediately when user chooses)
189  */
190 void PropTextColors::SerializeColorToFromScreen(OPERATION op, CColorButton & btn, int colorIndex)
191 {
192         switch (op)
193         {
194         case SET_DEFAULTS:
195         case LOAD_COLORS:
196                 btn.SetColor(m_pTempColors->GetColor(colorIndex));
197                 break;
198         }
199 }
200
201 /** 
202  * @brief Set colors to track standard (theme) colors
203  */
204 void PropTextColors::OnDefaultsStandardColors()
205 {
206         // Reset all text colors to default every time user checks defaults button
207         SerializeColorsToFromScreen(SET_DEFAULTS);
208
209         UpdateData();
210 }
211
212 /** 
213  * @brief Enable / disable color controls on dialog.
214  * @param [in] bEnable If `true` color controls are enabled.
215  */
216 void PropTextColors::EnableColorButtons(bool bEnable)
217 {
218         EnableDlgItem(IDC_WHITESPACE_COLOR_LABEL, bEnable);
219         EnableDlgItem(IDC_TEXT_COLOR_LABEL, bEnable);
220         EnableDlgItem(IDC_SELECTION_COLOR_LABEL, bEnable);
221         EnableDlgItem(IDC_MARGIN_COLOR_LABEL, bEnable);
222         EnableDlgItem(IDC_BACKGROUND_COLUMN_LABEL, bEnable);
223         EnableDlgItem(IDC_TEXT_COLUMN_LABEL, bEnable);
224 }