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