OSDN Git Service

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