OSDN Git Service

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