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