Debian Codegraph @

wxcrafter/src/properties_list_view.cpp

raw
1 #include "properties_list_view.h"
2
3 #include "event_notifier.h"
4 #include "globals.h"
5 #include "int_property.h"
6 #include "wxc_widget.h"
7 #include "wxgui_helpers.h"
8 #include "wxguicraft_main_view.h"
9
10 #include <unordered_set>
11 #include <wx/dcmemory.h>
12 #include <wx/msgdlg.h>
13 #include <wx/propgrid/advprops.h>
14 #include <wx/regex.h>
15 #include <wx/renderer.h>
16
17 /////////////////////////////////////////////////////////////////////////
18
19 namespace
20 {
21 wxcPGChoiceAndButtonEditor* gChoiceButtonEditor = NULL;
22 // a list of properties that if changed, there is no need to refresh the view
23 std::unordered_set<wxString> DO_NOT_NOTIFY_SET = { PROP_SUBCLASS_INCLUDE, PROP_SUBCLASS_NAME, PROP_SUBCLASS_STYLE };
24 } // namespace
25
26 PropertiesListView::PropertiesListView(wxWindow* win)
27 : wxPanel(win, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxBORDER_NONE)
28 , m_wxcWidget(NULL)
29 {
30 wxBoxSizer* sz = new wxBoxSizer(wxVERTICAL);
31 SetSizer(sz);
32
33 m_pgMgr =
34 new wxPropertyGridManager(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
35 wxPG_SPLITTER_AUTO_CENTER | wxPG_BOLD_MODIFIED | wxPG_DESCRIPTION | wxBORDER_NONE);
36
37 m_pg = m_pgMgr->GetGrid();
38 m_pgMgr->Bind(wxEVT_PG_CHANGED, &PropertiesListView::OnCellChanged, this);
39 m_pgMgr->Bind(wxEVT_PG_CHANGING, &PropertiesListView::OnCellChanging, this);
40 m_pg->Bind(wxEVT_UPDATE_UI, &PropertiesListView::OnPropertyGridUpdateUI, this);
41 sz->Add(m_pgMgr, 1, wxEXPAND);
42 GetSizer()->Layout();
43 }
44
45 PropertiesListView::~PropertiesListView()
46 {
47 m_pgMgr->Unbind(wxEVT_PG_CHANGED, &PropertiesListView::OnCellChanged, this);
48 m_pgMgr->Unbind(wxEVT_PG_CHANGING, &PropertiesListView::OnCellChanging, this);
49 m_pg->Unbind(wxEVT_UPDATE_UI, &PropertiesListView::OnPropertyGridUpdateUI, this);
50 }
51
52 void PropertiesListView::ConstructProjectSettings()
53 {
54 DoClear();
55 m_type = Type_Project_Metadata;
56
57 wxString filesStr = wxCrafter::Join(wxcProjectMetadata::Get().GetIncludeFiles(), ";");
58 m_pg->Append(new wxPropertyCategory(_("wxCrafter Project Settings")));
59 AddDirPicker(_("Output Directory"), wxcProjectMetadata::Get().GetGeneratedFilesDir(),
60 _("The generated files' output directory"));
61 AddDirPicker(wxGetTranslation(PROP_OUTPUT_FILE_NAME), wxcProjectMetadata::Get().GetOutputFileName(),
62 _("The generated files' name.\nwxCrafter will generate a $(FILE).cpp and $(FILE).hpp"));
63 AddLongTextProp(_("Additional Include Files"), filesStr,
64 _("List of additional include files that should be included in the "
65 "generated files\ne.g. my_header.hpp ; string.h"));
66 AddTextProp(_("Bitmap Files"), wxcProjectMetadata::Get().GetBitmapsFile(),
67 _("Set here the file name for the bitmaps\nwxCrafter converts all the bitmaps in the project into C++ "
68 "code and will place them in this file"));
69
70 m_pg->Append(new wxPropertyCategory(_("wxWindow ID Generation")));
71 AddBoolProp(
72 _("Generate Window ID"), wxcProjectMetadata::Get().IsUseEnum(),
73 _("Auto generate enum in the base class for the wxWindow IDs which are not a stock window ID nor wrapped with "
74 "the XRCID macro\nWhen left unchecked it is up to the user to provide a header file with the wxWindow IDs"));
75 AddIntegerProp(_("First Window ID"), wxcProjectMetadata::Get().GetFirstWindowId(),
76 _("When 'Generate Window ID' is checked, use this as the first enumerator value"));
77
78 m_pg->Append(new wxPropertyCategory(_("Code Generation")));
79 AddBoolProp(_("Generate Translatable Strings"), wxcProjectMetadata::Get().IsUseUnderscoreMacro(),
80 _("When enabled, all generated strings are wrapped with the \"_\" macro, otherwise allow users to "
81 "directly enter native text string encapsulated by wxT() macro"));
82 AddBoolProp(_("Add wxWidgets Handlers if missing"), wxcProjectMetadata::Get().IsAddHandlers(),
83 _("When enabled, wxCrafter will add missing handlers (e.g. wxBitmapXmlHandler)"));
84 }
85
86 void PropertiesListView::Construct(wxcWidget* wb)
87 {
88 DoClear();
89 m_type = Type_Control;
90 m_wxcWidget = wb;
91 if(!m_wxcWidget) {
92 return;
93 }
94
95 // Populate the table
96 const wxcWidget::MapProperties_t& props = wb->GetProperties();
97 wxcWidget::MapProperties_t::const_iterator iter = props.begin();
98 for(; iter != props.end(); ++iter) {
99 PropertyBase* property = iter->second;
100 if(!property) {
101 continue;
102 }
103
104 wxPGProperty* pgProp = NULL;
105 if(property->GetType() == PT_CATEGORY) {
106 pgProp = m_pg->Append(new wxPropertyCategory(property->GetValue()));
107
108 } else if(property->GetType() == PT_BOOL) {
109 pgProp = AddBoolProp(property->GetLabelForUI(), property->GetValue() == "1", property->GetTooltip());
110
111 } else if(property->GetType() == PT_INT) {
112 IntProperty* intProp = dynamic_cast<IntProperty*>(property);
113 if(intProp) {
114 if(intProp->IsUninitialized()) {
115 pgProp = AddIntegerProp(property->GetLabelForUI(), property->GetTooltip());
116 } else {
117 pgProp = AddIntegerProp(property->GetLabelForUI(), wxCrafter::ToNumber(property->GetValue(), -1),
118 property->GetTooltip());
119 }
120 }
121
122 } else if(property->GetType() == PT_COMBOX) {
123 pgProp = AddComboxProp(property->GetLabelForUI(), property->GetOptions(), property->GetValue(),
124 property->GetTooltip());
125
126 } else if(property->GetType() == PT_STRING) {
127 pgProp = AddTextProp(property->GetLabelForUI(), property->GetValue(), property->GetTooltip());
128
129 } else if(property->GetType() == PT_CHOICE) {
130 wxArrayString ops = property->GetOptions();
131 pgProp =
132 AddChoiceProp(property->GetLabelForUI(), ops, ops.Index(property->GetValue()), property->GetTooltip());
133
134 } else if(property->GetType() == PT_MUTLI_STRING_PICKER) {
135 pgProp = AddLongTextProp(property->GetLabelForUI(), property->GetValue(), property->GetTooltip());
136
137 } else if(property->GetType() == PT_COLOR_PICKER) {
138 pgProp = AddColorProp(property->GetLabelForUI(), property->GetValue(), property->GetTooltip());
139
140 } else if(property->GetType() == PT_FONT_PICKER) {
141 pgProp = AddFontProp(property->GetLabelForUI(), property->GetValue(), property->GetTooltip());
142
143 } else if(property->GetType() == PT_FILE_PICKER) {
144 pgProp = AddFilePicker(property->GetLabelForUI(), property->GetValue(), property->GetTooltip());
145
146 } else if(property->GetType() == PT_BITMAP) {
147 pgProp = AddBitmapPicker(property->GetLabelForUI(), property->GetValue(), property->GetTooltip());
148
149 } else if(property->GetType() == PT_BITMAPTEXT_TABLE) {
150 pgProp = AddBmpTextPicker(property->GetLabelForUI(), property->GetValue(), property->GetTooltip());
151
152 } else if(property->GetType() == PT_VIRTUAL_FOLDER_PICKER) {
153 pgProp = AddVDPicker(property->GetLabelForUI(), property->GetValue(), property->GetTooltip());
154
155 } else if(property->GetType() == PT_COL_HEADER_FLAGS) {
156 pgProp = AddFlags(property->GetLabelForUI(), property->GetOptions(), property->GetOptionsValues(),
157 property->GetValueLong(), property->GetTooltip());
158 }
159
160 if(pgProp) {
161 pgProp->SetClientData(property);
162 }
163 }
164 ::wxPGPropertyBooleanUseCheckbox(m_pg);
165 }
166
167 wxPGProperty* PropertiesListView::AddBoolProp(const wxString& label, bool checked, const wxString& tip)
168 {
169 wxPGProperty* prop = m_pg->Append(new wxBoolProperty(label, wxPG_LABEL, checked));
170 prop->SetHelpString(tip);
171 return prop;
172 }
173
174 wxPGProperty* PropertiesListView::AddChoiceProp(const wxString& label, const wxArrayString& value, int sel,
175 const wxString& tip)
176 {
177 wxPGProperty* prop = m_pg->Append(new wxEnumProperty(label, wxPG_LABEL, value));
178 prop->SetHelpString(tip);
179 prop->SetChoiceSelection(sel);
180 return prop;
181 }
182
183 wxPGProperty* PropertiesListView::AddFilePicker(const wxString& label, const wxString& value, const wxString& tip)
184 {
185 wxPGProperty* prop =
186 m_pg->Append(new wxPG_FilePickerProperty(label, wxcProjectMetadata::Get().GetProjectPath(), wxPG_LABEL, value));
187 prop->SetHelpString(tip);
188 return prop;
189 }
190
191 wxPGProperty* PropertiesListView::AddTextProp(const wxString& label, const wxString& value, const wxString& tip)
192 {
193 wxPGProperty* prop = m_pg->Append(new wxStringProperty(label, label, value));
194 prop->SetHelpString(tip);
195 return prop;
196 }
197
198 wxPGProperty* PropertiesListView::AddLongTextProp(const wxString& label, const wxString& value, const wxString& tip)
199 {
200 wxPGProperty* prop = m_pg->Append(new wxPG_MultiStringProperty(label, wxPG_LABEL, value));
201 prop->SetHelpString(tip);
202 return prop;
203 }
204
205 wxPGProperty* PropertiesListView::AddColorProp(const wxString& label, const wxString& value, const wxString& tip)
206 {
207 wxColourPropertyValue cpv;
208 int colorIdx = wxCrafter::GetColourSysIndex(value);
209 if(colorIdx == -1 && value != "<Default>") {
210
211 wxColour c = wxCrafter::NameToColour(value);
212 cpv = wxColourPropertyValue(wxPG_COLOUR_CUSTOM, c);
213
214 } else if(colorIdx != -1) {
215 cpv = wxColourPropertyValue(colorIdx);
216 }
217
218 wxPGProperty* prop = m_pg->Append(new wxPG_Colour(label, wxPG_LABEL, cpv));
219 if(!gChoiceButtonEditor) {
220 gChoiceButtonEditor = new wxcPGChoiceAndButtonEditor();
221 }
222
223 m_pg->SetPropertyEditor(prop, gChoiceButtonEditor);
224 prop->SetHelpString(tip);
225
226 if(value == "<Default>") {
227 prop->SetValueToUnspecified();
228 }
229 return prop;
230 }
231
232 wxPGProperty* PropertiesListView::AddComboxProp(const wxString& label, const wxArrayString& options,
233 const wxString& value, const wxString& tip)
234 {
235 wxPGProperty* prop = m_pg->Append(new wxEditEnumProperty(label, wxPG_LABEL, options));
236
237 prop->SetHelpString(tip);
238 prop->SetValueFromString(value);
239 return prop;
240 }
241
242 wxPGProperty* PropertiesListView::AddFontProp(const wxString& label, const wxString& value, const wxString& tip)
243 {
244 wxPGProperty* prop = m_pg->Append(new wxPG_FontProperty(label, wxPG_LABEL, value));
245 prop->SetHelpString(tip);
246 return prop;
247 }
248
249 wxPGProperty* PropertiesListView::AddBitmapPicker(const wxString& label, const wxString& value, const wxString& tip)
250 {
251 wxPGProperty* prop = m_pg->Append(new wxPG_BitmapProperty(label, wxPG_LABEL, value));
252 prop->SetHelpString(tip);
253 return prop;
254 }
255
256 void PropertiesListView::DoClear()
257 {
258 m_type = Type_Control;
259 m_pg->Clear();
260 m_pgMgr->SetDescription("", "");
261 m_properties.clear();
262 m_wxcWidget = NULL;
263 }
264
265 void PropertiesListView::OnCellChanged(wxPropertyGridEvent& e)
266 {
267 wxPGProperty* pgp = e.GetProperty();
268 if(m_type == Type_Project_Metadata) {
269 wxPGProperty* p = NULL;
270
271 p = m_pg->GetProperty(_("Output Directory"));
272 if(p) {
273 wxcProjectMetadata::Get().SetGeneratedFilesDir(p->GetValueAsString());
274 }
275
276 p = m_pg->GetProperty(_("Additional Include Files"));
277 if(p) {
278 wxcProjectMetadata::Get().SetIncludeFiles(wxCrafter::Split(p->GetValueAsString(), ";"));
279 }
280
281 p = m_pg->GetProperty(_("Bitmap Files"));
282 if(p) {
283 wxcProjectMetadata::Get().SetBitmapsFile(p->GetValueAsString());
284 }
285
286 p = m_pg->GetProperty(wxGetTranslation(PROP_OUTPUT_FILE_NAME));
287 if(p) {
288 wxcProjectMetadata::Get().SetOutputFileName(p->GetValueAsString());
289 }
290
291 p = m_pg->GetProperty(_("Generate Window ID"));
292 if(p) {
293 wxcProjectMetadata::Get().SetUseEnum(p->GetValue().GetBool());
294 }
295
296 p = m_pg->GetProperty(_("First Window ID"));
297 if(p) {
298 wxcProjectMetadata::Get().SetFirstWindowId(p->GetValue().GetInteger());
299 }
300
301 p = m_pg->GetProperty(_("Generate Translatable Strings"));
302 if(p) {
303 wxcProjectMetadata::Get().SetUseUnderscoreMacro(p->GetValue().GetBool());
304 }
305
306 p = m_pg->GetProperty(_("Add wxWidgets Handlers if missing"));
307 if(p) {
308 wxcProjectMetadata::Get().SetAddHandlers(p->GetValue().GetBool());
309 }
310
311 wxCommandEvent evt(wxEVT_PROJECT_METADATA_MODIFIED);
312 EventNotifier::Get()->AddPendingEvent(evt);
313
314 } else {
315
316 PropertyBase* pb = (PropertyBase*)pgp->GetClientData();
317 if(pb) {
318 // Check if the label modified is the "Name"
319 // if it is the "Name" - make sure there is no other wxcWidget with this name
320 if(pb->GetLabelForUI() == _("Name")) {
321 if(m_wxcWidget->GetTopLevel() && !m_wxcWidget->GetTopLevel()->IsNameUnique(pgp->GetValueAsString())) {
322 pgp->SetValueFromString(pb->GetValue(), 0);
323 ::wxMessageBox(_("A control with this name already exists!"), "wxCrafter",
324 wxICON_WARNING | wxOK | wxCENTER);
325 return;
326 }
327 }
328
329 wxBoolProperty* boolProp = dynamic_cast<wxBoolProperty*>(pgp);
330 if(boolProp) {
331 pb->SetValue(boolProp->GetValue().GetBool() ? "1" : "0");
332 } else {
333 pb->SetValue(pgp->GetValueAsString());
334 }
335
336 // we dont always want to notify about changes
337 if(DO_NOT_NOTIFY_SET.count(pb->GetLabel()) == 0) {
338 // Notify about modifications
339 wxCommandEvent evt(wxEVT_PROPERTIES_MODIFIED);
340 if(pb->GetLabel() == PROP_NAME) {
341 evt.SetString(pgp->GetValueAsString());
342 evt.SetClientData(m_wxcWidget);
343 }
344 EventNotifier::Get()->AddPendingEvent(evt);
345 }
346 }
347 }
348 }
349
350 wxPGProperty* PropertiesListView::AddVDPicker(const wxString& label, const wxString& value, const wxString& tip)
351 {
352 wxPGProperty* prop = m_pg->Append(new wxPG_VDPickerProperty(label, wxPG_LABEL, value));
353 prop->SetHelpString(tip);
354 return prop;
355 }
356
357 wxPGProperty* PropertiesListView::AddIntegerProp(const wxString& label, const wxString& tip)
358 {
359 wxPGProperty* prop = m_pg->Append(new wxIntProperty(label, wxPG_LABEL));
360 prop->SetValueToUnspecified();
361 prop->SetHelpString(tip);
362 return prop;
363 }
364
365 wxPGProperty* PropertiesListView::AddIntegerProp(const wxString& label, int value, const wxString& tip)
366 {
367 wxPGProperty* prop = m_pg->Append(new wxIntProperty(label, wxPG_LABEL, value));
368 prop->SetHelpString(tip);
369 return prop;
370 }
371
372 wxPGProperty* PropertiesListView::AddDirPicker(const wxString& label, const wxString& value, const wxString& tip)
373 {
374 wxPGProperty* prop =
375 m_pg->Append(new wxPG_DirPickerProperty(label, wxcProjectMetadata::Get().GetProjectPath(), wxPG_LABEL, value));
376 prop->SetHelpString(tip);
377 return prop;
378 }
379
380 wxPGProperty* PropertiesListView::AddFlags(const wxString& label, const wxArrayString& strings,
381 const wxArrayInt& values, long value, const wxString& tip)
382 {
383 wxPGProperty* prop = m_pg->Append(new wxFlagsProperty(label, wxPG_LABEL, strings, values, value));
384 prop->SetHelpString(tip);
385 m_pg->Expand(prop);
386 return prop;
387 }
388
389 void PropertiesListView::OnCellChanging(wxPropertyGridEvent& e)
390 {
391 wxString propName = e.GetPropertyName();
392 wxString sizeLabel = PROP_SIZE;
393 sizeLabel.EndsWith(":", &sizeLabel);
394
395 wxString minSizeLabel = PROP_MINSIZE;
396 minSizeLabel.EndsWith(":", &minSizeLabel);
397
398 // Validate Size / Minimum Size properties
399 if(propName == sizeLabel || propName == minSizeLabel) {
400
401 // size regex
402 static wxRegEx reSize("[-0-9]+,[ \t]*[-0-9]+");
403 wxString propValue = e.GetPropertyValue().GetString();
404 propValue.Trim().Trim(false);
405
406 // Validate against the regex (only if not empty)
407 if(!propValue.IsEmpty() && !reSize.Matches(propValue)) {
408 e.Veto();
409
410 } else {
411 e.Skip();
412 }
413
414 } else {
415 e.Skip();
416 }
417 }
418
419 void PropertiesListView::OnPropertyGridUpdateUI(wxUpdateUIEvent& event)
420 {
421 // UpdateUI for whether to show the PROP_DROPDOWN_MENU property i.e. when the tool's a dropdown
422 // GetPropertyByLabel() didn't work here. Nor did e.g. PROP_KIND. It has to be exactly this!
423 wxPGProperty* kind = m_pgMgr->GetPropertyByLabel(_("Kind"));
424 wxPGProperty* ddm = m_pgMgr->GetPropertyByLabel(_("Construct the Dropdown Menu"));
425
426 if(kind && ddm) {
427 // Doing ddm->Hide(kind->GetValueAsString() != ITEM_DROPDOWN) worked, but it was impossible to change the
428 // properties value as it was constantly being refreshed
429 // So only Show/Hide it when it's actually needed
430 if(!ddm->HasFlag(wxPG_PROP_HIDDEN) && (kind->GetValueAsString() != ITEM_DROPDOWN)) {
431 ddm->Hide(true);
432 } else if(ddm->HasFlag(wxPG_PROP_HIDDEN) && (kind->GetValueAsString() == ITEM_DROPDOWN)) {
433 ddm->Hide(true);
434 }
435 }
436 }
437
438 wxPGProperty* PropertiesListView::AddBmpTextPicker(const wxString& label, const wxString& value, const wxString& tip)
439 {
440 wxPGProperty* prop = m_pg->Append(new wxPG_BmpTextProperty(label, wxPG_LABEL, value));
441 prop->SetHelpString(tip);
442 return prop;
443 }