forked from eranif/codelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeformatterdlg.cpp
299 lines (273 loc) · 9.67 KB
/
codeformatterdlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : codeformatterdlg.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "codeformatter.h"
#include "windowattrmanager.h"
#include "codeformatterdlg.h"
CodeFormatterDlg::CodeFormatterDlg( wxWindow* parent, CodeFormatter *cf, size_t flags, const wxString &sampleCode )
: CodeFormatterBaseDlg( parent )
, m_cf(cf)
, m_sampleCode(sampleCode)
{
// center the dialog
Centre();
m_options.SetOption(flags);
m_buttonOK->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(CodeFormatterDlg::OnOK), NULL, this);
m_buttonHelp->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(CodeFormatterDlg::OnHelp), NULL, this);
// Initialise dialog
m_textCtrlPreview->SetValue(m_sampleCode);
InitDialog();
GetSizer()->Fit(this);
UpdatePreview();
m_radioBoxPredefinedStyle->SetFocus();
WindowAttrManager::Load(this, wxT("CodeFormatterDlgAttr"), m_cf->GetManager()->GetConfigTool());
}
void CodeFormatterDlg::UpdateCheckBox(wxCheckBox *obj, size_t flag)
{
obj->SetValue(m_options.GetOptions() & flag ? true : false);
}
void CodeFormatterDlg::InitDialog()
{
UpdateCheckBox(m_checkBoxFormatBreakBlocks,AS_BREAK_BLOCKS);
UpdateCheckBox(m_checkBoxFormatBreakBlocksAll,AS_BREAK_BLOCKS_ALL);
UpdateCheckBox(m_checkBoxFormatBreakElseif,AS_BREAK_ELSEIF);
UpdateCheckBox(m_checkBoxFormatFillEmptyLines,AS_FILL_EMPTY_LINES);
UpdateCheckBox(m_checkBoxFormatOneLineKeepBlocks,AS_ONE_LINE_KEEP_BLOCKS);
UpdateCheckBox(m_checkBoxFormatOneLineKeepStmnt,AS_ONE_LINE_KEEP_STATEMENT);
UpdateCheckBox(m_checkBoxFormatPadOperators,AS_PAD_OPER);
UpdateCheckBox(m_checkBoxFormatPadParenth,AS_PAD_PARENTHESIS);
UpdateCheckBox(m_checkBoxFormatPadParentIn,AS_PAD_PARENTHESIS_IN);
UpdateCheckBox(m_checkBoxFormatPadParentOut,AS_PAD_PARENTHESIS_OUT);
UpdateCheckBox(m_checkBoxFormatUnPadParent,AS_UNPAD_PARENTHESIS);
UpdateCheckBox(m_checkBoxIndentBrackets,AS_INDENT_BRACKETS);
UpdateCheckBox(m_checkBoxIndentLabels,AS_INDENT_LABELS);
UpdateCheckBox(m_checkBoxIndentMaxInst,AS_MAX_INSTATEMENT_INDENT);
UpdateCheckBox(m_checkBoxIndentMinCond,AS_MIN_COND_INDENT);
UpdateCheckBox(m_checkBoxIndentNamespaces,AS_INDENT_NAMESPACES);
UpdateCheckBox(m_checkBoxIndentPreprocessors,AS_INDENT_PREPROCESSORS);
UpdateCheckBox(m_checkBoxIndetBlocks,AS_INDENT_BLOCKS);
UpdateCheckBox(m_checkBoxIndetCase,AS_INDENT_CASE);
UpdateCheckBox(m_checkBoxIndetClass,AS_INDENT_CLASS);
UpdateCheckBox(m_checkBoxIndetSwitch,AS_INDENT_SWITCHES);
//update the two radio box controls
int selection(3); //AS_LINUX
if (m_options.GetOptions() & AS_LINUX) {
selection = 3;
} else if (m_options.GetOptions() & AS_GNU) {
selection =0;
} else if (m_options.GetOptions() & AS_ANSI) {
selection = 4;
} else if (m_options.GetOptions() & AS_JAVA) {
selection = 1;
} else if (m_options.GetOptions() & AS_KR) {
selection = 2;
}
m_radioBoxPredefinedStyle->SetSelection(selection);
selection = 4; // None
if (m_options.GetOptions() & AS_BRACKETS_BREAK_CLOSING) {
selection = 0;
} else if (m_options.GetOptions() & AS_BRACKETS_ATTACH) {
selection = 1;
} else if (m_options.GetOptions() & AS_BRACKETS_LINUX) {
selection = 2;
} else if (m_options.GetOptions() & AS_BRACKETS_BREAK) {
selection = 3;
}
m_radioBoxBrackets->SetSelection(selection);
}
void CodeFormatterDlg::OnRadioBoxPredefinedStyle( wxCommandEvent& event )
{
int sel = event.GetSelection();
//remove all predefined styles
size_t options = m_options.GetOptions();
options &= ~(AS_ANSI);//4
options &= ~(AS_GNU); //0
options &= ~(AS_LINUX);//3
options &= ~(AS_JAVA);//1
options &= ~(AS_KR);//2
size_t flag = AS_LINUX;
switch (sel) {
case 0:
flag = AS_GNU;
break;
case 1:
flag = AS_JAVA;
break;
case 2:
flag = AS_KR;
break;
case 3:
flag = AS_LINUX;
break;
case 4:
flag = AS_ANSI;
break;
default:
flag = AS_LINUX;
break;
}
m_options.SetOption(options | flag);
UpdatePreview();
}
void CodeFormatterDlg::OnRadioBoxBrackets( wxCommandEvent& event )
{
int sel = event.GetSelection();
//remove all predefined styles
size_t options = m_options.GetOptions();
options &= ~(AS_BRACKETS_BREAK_CLOSING);//0
options &= ~(AS_BRACKETS_ATTACH); //1
options &= ~(AS_BRACKETS_LINUX);//2
options &= ~(AS_BRACKETS_BREAK);//3
size_t flag = AS_LINUX;
switch (sel) {
case 0:
flag = AS_BRACKETS_BREAK_CLOSING;
break;
case 1:
flag = AS_BRACKETS_ATTACH;
break;
case 2:
flag = AS_BRACKETS_LINUX;
break;
case 3:
flag = AS_BRACKETS_BREAK;
break;
}
m_options.SetOption(options | flag);
UpdatePreview();
}
void CodeFormatterDlg::OnCheckBox( wxCommandEvent& event )
{
size_t flag(0);
wxObject *obj = event.GetEventObject();
if (obj == m_checkBoxFormatBreakBlocks) {
flag = AS_BREAK_BLOCKS;
} else if (obj == m_checkBoxFormatBreakBlocksAll) {
flag = AS_BREAK_BLOCKS_ALL;
} else if (obj == m_checkBoxFormatBreakElseif) {
flag = AS_BREAK_ELSEIF;
} else if (obj == m_checkBoxFormatFillEmptyLines) {
flag = AS_FILL_EMPTY_LINES;
} else if (obj == m_checkBoxFormatOneLineKeepBlocks) {
flag = AS_ONE_LINE_KEEP_BLOCKS;
} else if (obj == m_checkBoxFormatOneLineKeepStmnt) {
flag = AS_ONE_LINE_KEEP_STATEMENT;
} else if (obj == m_checkBoxFormatPadOperators) {
flag = AS_PAD_OPER;
} else if (obj == m_checkBoxFormatPadParenth) {
flag = AS_PAD_PARENTHESIS;
} else if (obj == m_checkBoxFormatPadParentIn) {
flag = AS_PAD_PARENTHESIS_IN;
} else if (obj == m_checkBoxFormatPadParentOut) {
flag = AS_PAD_PARENTHESIS_OUT;
} else if (obj == m_checkBoxFormatUnPadParent) {
flag = AS_UNPAD_PARENTHESIS;
} else if (obj == m_checkBoxIndentBrackets) {
flag = AS_INDENT_BRACKETS;
} else if (obj == m_checkBoxIndentLabels) {
flag = AS_INDENT_LABELS;
} else if (obj == m_checkBoxIndentMaxInst) {
flag = AS_MAX_INSTATEMENT_INDENT;
} else if (obj == m_checkBoxIndentMinCond) {
flag = AS_MIN_COND_INDENT;
} else if (obj == m_checkBoxIndentNamespaces) {
flag = AS_INDENT_NAMESPACES;
} else if (obj == m_checkBoxIndentPreprocessors) {
flag = AS_INDENT_PREPROCESSORS;
} else if (obj == m_checkBoxIndetBlocks) {
flag = AS_INDENT_BLOCKS;
} else if (obj == m_checkBoxIndetCase) {
flag = AS_INDENT_CASE;
} else if (obj == m_checkBoxIndetClass) {
flag = AS_INDENT_CLASS;
} else if (obj == m_checkBoxIndetSwitch) {
flag = AS_INDENT_SWITCHES;
}
size_t options = m_options.GetOptions();
EnableFlag(options, flag, event.IsChecked());
m_options.SetOption(options);
UpdatePreview();
}
void CodeFormatterDlg::EnableFlag(size_t &options, size_t flag, bool enable)
{
if (enable) {
options |= flag;
} else {
options &= ~(flag);
}
}
void CodeFormatterDlg::OnOK(wxCommandEvent &e)
{
wxUnusedVar(e);
//Save the options
EndModal(wxID_OK);
}
void CodeFormatterDlg::OnHelp(wxCommandEvent &e)
{
wxUnusedVar(e);
static wxString helpUrl(wxT("http://astyle.sourceforge.net/astyle.html"));
wxLaunchDefaultBrowser(helpUrl);
}
void CodeFormatterDlg::UpdatePreview()
{
wxString output;
m_cf->AstyleFormat(m_sampleCode, m_options.ToString(), output);
m_textCtrlPreview->SetValue(output);
UpdatePredefinedHelpText();
}
void CodeFormatterDlg::UpdatePredefinedHelpText()
{
int sel = m_radioBoxPredefinedStyle->GetSelection();
switch ( sel ) {
case 0: // AS_GNU
m_staticTextPredefineHelp->SetLabel(
wxString(_("GNU style formatting/indenting. Brackets are broken,\n")) +
wxString(_("blocks are indented, and indentation is 2 spaces. \n")) +
wxString(_("Namespaces, classes, and switches are NOT indented.")));
break;
case 1: // AS_JAVA
m_staticTextPredefineHelp->SetLabel(
wxString(_("Java style formatting/indenting. Brackets are attached,\n")) +
wxString(_("indentation is 4 spaces. Switches are NOT indented."))
);
break;
case 2: // AS_KR
m_staticTextPredefineHelp->SetLabel(
_("Kernighan & Ritchie style formatting/indenting.\nBrackets are attached, indentation is 4 spaces.\nNamespaces, classes, and switches are NOT indented.")
);
break;
case 3: // AS_KR
m_staticTextPredefineHelp->SetLabel(
_("Linux style formatting/indenting.\nAll brackets are linux style, indentation is 8 spaces.\nNamespaces, classes, and switches are NOT indented.")
);
break;
case 4: // AS_ANSI
m_staticTextPredefineHelp->SetLabel(_("ANSI style formatting/indenting.\nBrackets are broken, indentation is 4 spaces.\nNamespaces, classes, and switches are NOT indented."));
break;
}
}
CodeFormatterDlg::~CodeFormatterDlg()
{
WindowAttrManager::Save(this, wxT("CodeFormatterDlgAttr"), m_cf->GetManager()->GetConfigTool());
}