OSDN Git Service

Merge with stable
[winmerge-jp/winmerge-jp.git] / Src / PropEditor.cpp
1 /** 
2  * @file  PropEditor.cpp
3  *
4  * @brief Implementation of PropEditor propertysheet
5  */
6
7 #include "stdafx.h"
8 #include "PropEditor.h"
9 #include "Merge.h"
10 #include "OptionsDef.h"
11 #include "OptionsMgr.h"
12 #include "OptionsPanel.h"
13 #include "DDXHelper.h"
14
15 #ifdef _DEBUG
16 #define new DEBUG_NEW
17 #undef THIS_FILE
18 static char THIS_FILE[] = __FILE__;
19 #endif
20
21 /** @brief Maximum size for tabs in spaces. */
22 static const int MAX_TABSIZE = 64;
23
24 /** 
25  * @brief Constructor.
26  * @param [in] optionsMgr Pointer to options manager for handling options.
27  */
28 PropEditor::PropEditor(COptionsMgr *optionsMgr) 
29 : OptionsPanel(optionsMgr, PropEditor::IDD)
30 , m_bHiliteSyntax(false)
31 , m_nTabType(-1)
32 , m_nTabSize(0)
33 , m_bAutomaticRescan(false)
34 , m_bAllowMixedEol(false)
35 , m_bViewLineDifferences(false)
36 , m_bBreakOnWords(false)
37 , m_nBreakType(0)
38 {
39 }
40
41 /** 
42  * @brief Function handling dialog data exchange between GUI and variables.
43  */
44 void PropEditor::DoDataExchange(CDataExchange* pDX)
45 {
46         CDialog::DoDataExchange(pDX);
47         //{{AFX_DATA_MAP(PropEditor)
48         DDX_Check(pDX, IDC_HILITE_CHECK, m_bHiliteSyntax);
49         DDX_Radio(pDX, IDC_PROP_INSERT_TABS, m_nTabType);
50         DDX_Text(pDX, IDC_TAB_EDIT, m_nTabSize);
51         DDX_Check(pDX, IDC_AUTOMRESCAN_CHECK, m_bAutomaticRescan);
52         DDX_Check(pDX, IDC_MIXED_EOL, m_bAllowMixedEol);
53         DDX_Check(pDX, IDC_VIEW_LINE_DIFFERENCES, m_bViewLineDifferences);
54         DDX_Radio(pDX, IDC_EDITOR_CHARLEVEL, m_bBreakOnWords);
55         DDX_CBIndex(pDX, IDC_BREAK_TYPE, m_nBreakType);
56         DDX_Text(pDX, IDC_BREAK_CHARS, m_breakChars);
57         //}}AFX_DATA_MAP
58 }
59
60
61 BEGIN_MESSAGE_MAP(PropEditor, CDialog)
62         //{{AFX_MSG_MAP(PropEditor)
63         ON_BN_CLICKED(IDC_VIEW_LINE_DIFFERENCES, OnLineDiffControlClicked)
64         ON_BN_CLICKED(IDC_EDITOR_CHARLEVEL, OnLineDiffControlClicked)
65         ON_BN_CLICKED(IDC_EDITOR_WORDLEVEL, OnLineDiffControlClicked)
66         ON_EN_KILLFOCUS(IDC_TAB_EDIT, OnEnKillfocusTabEdit)
67         //}}AFX_MSG_MAP
68 END_MESSAGE_MAP()
69
70 /** 
71  * @brief Reads options values from storage to UI.
72  */
73 void PropEditor::ReadOptions()
74 {
75         m_nTabSize = GetOptionsMgr()->GetInt(OPT_TAB_SIZE);
76         m_nTabType = GetOptionsMgr()->GetInt(OPT_TAB_TYPE);
77         m_bAutomaticRescan = GetOptionsMgr()->GetBool(OPT_AUTOMATIC_RESCAN);
78         m_bHiliteSyntax = GetOptionsMgr()->GetBool(OPT_SYNTAX_HIGHLIGHT);
79         m_bAllowMixedEol = GetOptionsMgr()->GetBool(OPT_ALLOW_MIXED_EOL);
80         m_bViewLineDifferences = GetOptionsMgr()->GetBool(OPT_WORDDIFF_HIGHLIGHT);
81         m_bBreakOnWords = GetOptionsMgr()->GetBool(OPT_BREAK_ON_WORDS);
82         m_nBreakType = GetOptionsMgr()->GetInt(OPT_BREAK_TYPE);
83         m_breakChars = GetOptionsMgr()->GetString(OPT_BREAK_SEPARATORS).c_str();
84 }
85
86 /** 
87  * @brief Writes options values from UI to storage.
88  */
89 void PropEditor::WriteOptions()
90 {
91         // Sanity check tabsize
92         if (m_nTabSize < 1)
93                 m_nTabSize = 1;
94         if (m_nTabSize > MAX_TABSIZE)
95                 m_nTabSize = MAX_TABSIZE;
96         GetOptionsMgr()->SaveOption(OPT_TAB_SIZE, (int)m_nTabSize);
97         GetOptionsMgr()->SaveOption(OPT_TAB_TYPE, (int)m_nTabType);
98         GetOptionsMgr()->SaveOption(OPT_AUTOMATIC_RESCAN, m_bAutomaticRescan);
99         GetOptionsMgr()->SaveOption(OPT_ALLOW_MIXED_EOL, m_bAllowMixedEol);
100         GetOptionsMgr()->SaveOption(OPT_SYNTAX_HIGHLIGHT, m_bHiliteSyntax);
101         GetOptionsMgr()->SaveOption(OPT_WORDDIFF_HIGHLIGHT, !!m_bViewLineDifferences);
102         GetOptionsMgr()->SaveOption(OPT_BREAK_ON_WORDS, !!m_bBreakOnWords);
103         GetOptionsMgr()->SaveOption(OPT_BREAK_TYPE, m_nBreakType);
104         GetOptionsMgr()->SaveOption(OPT_BREAK_SEPARATORS, String(m_breakChars));
105 }
106
107 /** 
108  * @brief Called before propertysheet is drawn.
109  */
110 BOOL PropEditor::OnInitDialog() 
111 {
112         theApp.TranslateDialog(m_hWnd);
113         CPropertyPage::OnInitDialog();
114
115         CEdit * pEdit = (CEdit *) GetDlgItem(IDC_TAB_EDIT);
116
117         // Limit max text of tabsize to 2 chars
118         if (pEdit != NULL)
119                 pEdit->SetLimitText(2);
120
121         LoadBreakTypeStrings();
122         UpdateDataToWindow();
123         UpdateLineDiffControls();
124
125         return TRUE;  // return TRUE unless you set the focus to a control
126                       // EXCEPTION: OCX Property Pages should return FALSE
127 }
128
129 /**
130  * @brief Load strings (from resource) into combobox for break type
131  */
132 void PropEditor::LoadBreakTypeStrings()
133 {
134         CComboBox * cbo = (CComboBox *)GetDlgItem(IDC_BREAK_TYPE);
135         cbo->AddString(_("Break at whitespace").c_str());
136         cbo->AddString(_("Break at whitespace or punctuation").c_str());
137 }
138
139 /**
140  * @brief Handlers any clicks in any of the line differencing controls
141  */
142 void PropEditor::OnLineDiffControlClicked()
143 {
144         UpdateLineDiffControls();
145 }
146
147 /**
148  * @brief Shortcut to enable or disable a control
149  * @param [in] item ID of dialog control to enable/disable.
150  * @param [in] enable if true control is enabled, else disabled.
151  */
152 void PropEditor::EnableDlgItem(int item, bool enable)
153 {
154         GetDlgItem(item)->EnableWindow(!!enable);
155 }
156
157 /** 
158  * @brief Update availability of line difference controls
159  */
160 void PropEditor::UpdateLineDiffControls()
161 {
162         UpdateDataFromWindow();
163         // Can only choose char/word level if line differences are enabled
164         EnableDlgItem(IDC_EDITOR_CHARLEVEL, !!m_bViewLineDifferences);
165         EnableDlgItem(IDC_EDITOR_WORDLEVEL, !!m_bViewLineDifferences);
166         // Can only choose break type if line differences are enabled & we're breaking on words
167         EnableDlgItem(IDC_BREAK_TYPE, !!m_bViewLineDifferences);
168 }
169
170 /** 
171  * @brief Check tabsize value when control loses focus.
172  */
173 void PropEditor::OnEnKillfocusTabEdit()
174 {
175         CEdit * pEdit = (CEdit *)GetDlgItem(IDC_TAB_EDIT);
176         String valueAsText;
177         pEdit->GetWindowText(PopString(valueAsText));
178         int value = 0;
179         try { value = string_stoi(valueAsText); } catch (...) {};       
180         if (value < 1 || value > MAX_TABSIZE)
181         {
182                 String msg = string_format_string1(
183                         _("Value in Tab size -field is not in range WinMerge accepts.\n\nPlease use values 1 - %1."),
184                         string_to_str(MAX_TABSIZE));
185                 AfxMessageBox(msg.c_str(), MB_ICONWARNING);
186         }
187 }