Debian Codegraph @

wxcrafter/controls/top_level_win_wrapper.cpp

raw
1 #include "top_level_win_wrapper.h"
2
3 #include "allocator_mgr.h"
4 #include "choice_property.h"
5 #include "virtual_folder_property.h"
6 #include "wxc_bitmap_code_generator.h"
7 #include "wxc_notebook_code_helper.h"
8 #include "wxgui_helpers.h"
9
10 #include <wx/arrstr.h>
11 #include <wx/ffile.h>
12 #include <wx/msgdlg.h>
13 #include <wx/richmsgdlg.h>
14 #include <wx/tokenzr.h>
15 #include <wx/toplevel.h>
16
17 TopLevelWinWrapper::TopLevelWinWrapper(int type)
18 : wxcWidget(type)
19 , m_auiDropDownMenuHelperRegistered(false)
20 {
21 PREPEND_STYLE(wxSTAY_ON_TOP, false);
22 PREPEND_STYLE(wxCLOSE_BOX, false);
23 PREPEND_STYLE(wxSYSTEM_MENU, false);
24 PREPEND_STYLE(wxMINIMIZE_BOX, false);
25 PREPEND_STYLE(wxMAXIMIZE_BOX, false);
26 PREPEND_STYLE(wxRESIZE_BORDER, false);
27 PREPEND_STYLE(wxCAPTION, false);
28
29 if(type == ID_WXFRAME || type == ID_WXDIALOG) {
30 // a real top level windows
31 // add support for wxTopLevelWindow events
32 RegisterEvent("wxEVT_MAXIMIZE", "wxMaximizeEvent", _("Process a wxEVT_MAXIMIZE event"));
33 RegisterEvent("wxEVT_MOVE", "wxMoveEvent",
34 _("Process a wxEVT_MOVE event, which is generated when a window is moved"));
35 RegisterEvent("wxEVT_MOVE_START", "wxMoveEvent",
36 _("Process a wxEVT_MOVE_START event, which is generated when the "
37 "user starts to move or size a window. Windows only"));
38 RegisterEvent("wxEVT_MOVE_END", "wxMoveEvent",
39 _("Process a wxEVT_MOVE_END event, which is generated when "
40 "the user stops moving or sizing a window. Windows only"));
41 RegisterEvent("wxEVT_SHOW", "wxShowEvent", _("Process a wxEVT_SHOW event"));
42 }
43
44 // Default size for top level windows
45 DoSetPropertyStringValue(PROP_SIZE, "500,300");
46 if(IsWxTopLevelWindow()) {
47 // Add a persistency object support
48 AddBool(PROP_PERSISTENT,
49 _("When enabled, the generated code will add support for wxPersistenceManager (i.e. the Window will "
50 "remember its size, position and any child wxBookCtrlBase control will remember its selection)"),
51 true);
52 }
53
54 wxArrayString arr;
55 arr.Add("");
56 arr.Add("wxBOTH");
57 arr.Add("wxVERTICAL");
58 arr.Add("wxHORIZONTAL");
59
60 AddProperty(new StringProperty(PROP_TITLE, "", _("The title, if any")));
61 AddProperty(
62 new VirtualFolderProperty(PROP_VIRTUAL_FOLDER, "", _("CodeLite's virtual folder for the generated files")));
63 AddProperty(new ChoiceProperty(PROP_CENTRE_ON_SCREEN, arr, 1,
64 _("Centre on parent. This may be in both dimensions (the default); only "
65 "vertically or horizontally; or not at all.")));
66
67 AddProperty(new CategoryProperty(_("Inherited C++ Class Properties")));
68 AddProperty(new StringProperty(PROP_INHERITED_CLASS, "",
69 _("Inherited class name\nFill this field to generate a class that inherits from the "
70 "base class,\nwhere you should place all your application logic.\ne.g. for a "
71 "generated class 'FooDialogBase', you might enter 'FooDialog' here.")));
72 AddProperty(new StringProperty(PROP_FILE, "",
73 _("The name for the inherited class's files (without any file "
74 "extension).\nwxCrafter will generate a $(FILE).cpp and $(FILE).hpp\ne.g. "
75 "for an inherited class 'FooDialog', you might enter 'foodialog' here.")));
76 AddProperty(new StringProperty(PROP_CLASS_DECORATOR, "",
77 _("MSW Only\nC++ macro decorator - allows exporting this class from a DLL")));
78
79 if(m_properties.Contains(PROP_NAME)) {
80 m_properties.Item(PROP_NAME)->SetTooltip(_("The generated C++ class name"));
81 }
82
83 DelProperty(PROP_WINDOW_ID);
84 }
85
86 TopLevelWinWrapper::~TopLevelWinWrapper() {}
87
88 wxString TopLevelWinWrapper::FormatCode(const wxString& chunk) const
89 {
90 // Format the code a bit
91 wxString formattedCode;
92 wxArrayString codeArr = ::wxStringTokenize(chunk, "\n\r", wxTOKEN_RET_EMPTY_ALL);
93 for(size_t i = 0; i < codeArr.GetCount(); i++) {
94 formattedCode << " " << codeArr.Item(i) << "\n";
95 }
96
97 formattedCode.Replace("|@@|", "");
98
99 // Remove any double empty lines
100 while(formattedCode.Replace(" \n \n", " \n")) {}
101 return formattedCode;
102 }
103
104 void TopLevelWinWrapper::GenerateCode(const wxcProjectMetadata& project, bool promptUser, bool baseOnly,
105 wxString& baseCpp, wxString& baseHeader, wxArrayString& headers,
106 wxStringMap_t& additionalFiles)
107 {
108 wxString size = PropertyString(PROP_SIZE);
109 wxString filename = PropertyFile(PROP_FILE);
110
111 if(promptUser && (GetType() != ID_WXIMAGELIST)) { // By design we dont provide a subclass for wxImageList
112 if(filename.IsEmpty() && !wxcSettings::Get().HasFlag(wxcSettings::DONT_PROMPT_ABOUT_MISSING_SUBCLASS)) {
113 wxString message;
114 message << _("You did not set the 'Inherited C++ Class Properties -> File name' property for the top level "
115 "window: '")
116 << GetName() << "'\n"
117 << _("This means that only base class code will be generated\nTo fix this, select the toplevel "
118 "entry from the tree-view and provide an Inherited class name and file name");
119
120 wxRichMessageDialog dlg(NULL, message, "wxCrafter", wxOK | wxOK_DEFAULT | wxCENTER | wxICON_WARNING);
121 dlg.SetOKLabel(_("OK, continue with code generation"));
122 dlg.ShowCheckBox(_("Don't show this message again"));
123
124 if(dlg.ShowModal() == wxID_CANCEL) {
125 return;
126 }
127 if(dlg.IsCheckBoxChecked()) {
128 wxcSettings::Get().EnableFlag(wxcSettings::DONT_PROMPT_ABOUT_MISSING_SUBCLASS, true);
129 }
130 }
131 }
132
133 /// Clear all window IDs related to this top level window
134 m_auiDropDownMenuHelperRegistered = false;
135 wxcCodeGeneratorHelper::Get().ClearWindowIds();
136 wxcCodeGeneratorHelper::Get().ClearIcons(); // Icons should be cleared between top-level windows
137
138 wxcNotebookCodeHelper::Get().Clear();
139 wxString ctorBody, dtorCode, membersChunk, eventFunctions, eventConnectCode, eventDisconnectCode,
140 extraFunctionsCodeImpl, extraFunctionsCodeDecl;
141 headers.Add("#include <wx/settings.h>");
142 headers.Add("#include <wx/xrc/xmlres.h>");
143 headers.Add("#include <wx/xrc/xh_bmp.h>");
144
145 DoTraverseAndGenCode(headers, ctorBody, membersChunk, eventFunctions, eventConnectCode, additionalFiles, dtorCode,
146 extraFunctionsCodeImpl, extraFunctionsCodeDecl);
147
148 // Format the code a bit
149 ctorBody = FormatCode(ctorBody);
150
151 // Window colors
152 if(IsWindow()) {
153 wxString bgcol = wxCrafter::ColourToCpp(PropertyString(PROP_BG));
154 wxString fgcol = wxCrafter::ColourToCpp(PropertyString(PROP_FG));
155
156 // Add colors and fonts here
157 if(bgcol.IsEmpty() == false) {
158 ctorBody << " SetBackgroundColour(" << bgcol << ");\n";
159 }
160
161 if(fgcol.IsEmpty() == false) {
162 ctorBody << " SetForegroundColour(" << fgcol << ");\n";
163 }
164 }
165
166 ctorBody << FormatCode(wxcNotebookCodeHelper::Get().Code());
167 wxcNotebookCodeHelper::Get().Clear();
168
169 if(IsWindow()) {
170 // Set the window name
171 ctorBody << " SetName(" << wxCrafter::WXT(GetName()) << ");\n";
172 wxSize sz = wxCrafter::DecodeSize(PropertyString(PROP_MINSIZE));
173 if(sz != wxDefaultSize) {
174 ctorBody << " SetMinClientSize(wxSize(" << wxCrafter::EncodeSize(sz) << "));\n";
175 }
176
177 if(GetType() != ID_WXAUITOOLBARTOPLEVEL) {
178 ctorBody << " SetSize(" << wxCrafter::GetSizeAsDlgUnits(GetSize(), "this") << ");\n";
179 ctorBody << " if (GetSizer()) {\n";
180 ctorBody << " GetSizer()->Fit(this);\n";
181 ctorBody << " }\n";
182
183 if(IsWxTopLevelWindow()) {
184 ctorBody << " if(GetParent()) {\n";
185 ctorBody << " CentreOnParent(" << PropertyString(PROP_CENTRE_ON_SCREEN) << ");\n";
186 ctorBody << " } else {\n";
187 ctorBody << " CentreOnScreen(" << PropertyString(PROP_CENTRE_ON_SCREEN) << ");\n";
188 ctorBody << " }\n";
189 }
190
191 // Add support for wxPersistenceManager object
192 if(IsWxTopLevelWindow() && IsPropertyChecked(PROP_PERSISTENT)) {
193 ctorBody << " if(!wxPersistenceManager::Get().Find(this)) {\n";
194 ctorBody << " wxPersistenceManager::Get().RegisterAndRestore(this);\n";
195 ctorBody << " } else {\n";
196 ctorBody << " wxPersistenceManager::Get().Restore(this);\n";
197 ctorBody << " }\n";
198 }
199 }
200 }
201
202 // Format the function code
203 eventFunctions = FormatCode(eventFunctions);
204 eventConnectCode = FormatCode(eventConnectCode);
205
206 // use Unbind syntax
207 eventDisconnectCode = eventConnectCode;
208 eventDisconnectCode.Replace("->Bind(", "->Unbind(");
209
210 wxString baseClassName = CreateBaseclassName();
211
212 wxString cppDecorator = PropertyString(PROP_CLASS_DECORATOR);
213 cppDecorator.Trim().Trim(false);
214
215 wxString superclass = GetRealClassName();
216 wxString derivedSuperclass = baseClassName;
217
218 if(baseClassName.IsEmpty()) { // meaning that it was empty until BASE_CLASS_SUFFIX was appended
219 wxString msg;
220 msg << _("Can not generate code.\nMake sure that all toplevel windows have a valid C++ class name");
221 wxMessageBox(msg, "wxCrafter", wxOK | wxICON_WARNING | wxCENTER);
222 return;
223 }
224
225 if(filename.IsEmpty()) {
226 baseOnly = true;
227 }
228
229 wxFileName headerFile, sourceFile;
230 wxFileName baseFile(wxcProjectMetadata::Get().GetGeneratedFilesDir(),
231 wxcProjectMetadata::Get().GetOutputFileName());
232
233 // If the files are relative make them abs
234 if(baseFile.IsRelative()) {
235 baseFile.MakeAbsolute(project.GetProjectPath());
236 }
237
238 wxString dbg = baseFile.GetFullPath();
239
240 headerFile = baseFile;
241 sourceFile = baseFile;
242
243 headerFile.SetExt(wxcProjectMetadata::Get().GetHeaderFileExt());
244 sourceFile.SetExt("cpp");
245
246 dtorCode.Prepend(eventDisconnectCode);
247
248 // Write the C++ file
249 baseCpp << "\n"
250 << BaseCtorImplPrefix() << "{\n"
251 << wxcCodeGeneratorHelper::Get().GenerateInitCode(this) << ctorBody;
252
253 if(eventConnectCode.IsEmpty() == false) {
254 baseCpp << " // Connect events\n" << eventConnectCode;
255 }
256
257 // Now Connect() any auitoolbar dropdown menu; it must be *after* the normal Connect()s, otherwise it won't be
258 // called if the user-code forgets to event.Skip()
259 if(IsAuiToolBarDropDownHelpersRegistered()) {
260 baseCpp << " this->Connect("
261 << "wxID_ANY, "
262 << "wxEVT_COMMAND_AUITOOLBAR_TOOL_DROPDOWN, wxAuiToolBarEventHandler(" << GetName()
263 << "::" << DEFAULT_AUI_DROPDOWN_FUNCTION << "), NULL, this);\n";
264
265 // We need to disconnect it too
266 dtorCode << " this->Disconnect("
267 << "wxID_ANY, "
268 << "wxEVT_COMMAND_AUITOOLBAR_TOOL_DROPDOWN, wxAuiToolBarEventHandler(" << GetName()
269 << "::" << DEFAULT_AUI_DROPDOWN_FUNCTION << "), NULL, this);\n";
270 }
271
272 baseCpp << "}\n\n"
273 << baseClassName << "::~" << baseClassName << "()\n"
274 << "{\n"
275 << dtorCode << "}\n";
276
277 // Write any extra (i.e. not ctor/dtor) functions
278 if(!extraFunctionsCodeImpl.empty()) {
279 baseCpp << extraFunctionsCodeImpl; // Don't use FormatCode() here; it'd indent the signature etc too
280 }
281
282 // prepare the enum block for the
283 // Write the header file
284
285 baseHeader << "\n"
286 << "class " << (cppDecorator.IsEmpty() ? "" : cppDecorator + " ") << baseClassName << " : public "
287 << superclass << "\n"
288 << "{\n"
289 << wxcCodeGeneratorHelper::Get().GenerateWinIdEnum() << "protected:\n"
290 << membersChunk << "\n"
291 << "protected:\n"
292 << eventFunctions << "\n"
293 << "public:\n"
294 << extraFunctionsCodeDecl << BaseCtorDecl() << " virtual ~" << baseClassName << "();\n"
295 << "};\n\n";
296
297 if(baseOnly) {
298 return;
299 }
300
301 wxString inheritedClass = PropertyString(PROP_INHERITED_CLASS);
302 inheritedClass.Trim().Trim(false);
303
304 if(inheritedClass.IsEmpty()) {
305 return;
306 }
307
308 if(inheritedClass == baseClassName) {
309 ::wxMessageBox(_("Base class and inherited class have the same name"), "wxCrafter",
310 wxOK | wxICON_WARNING | wxCENTER);
311 return;
312 }
313
314 /////////////////////////////////////////////////////////////////////////////////////////
315 // Create the derived classes files
316 /////////////////////////////////////////////////////////////////////////////////////////
317
318 wxFileName derivedClassFileCPP(filename);
319 wxFileName derivedClassFileHPP(filename);
320
321 derivedClassFileCPP.SetExt("cpp");
322 derivedClassFileHPP.SetExt(wxcProjectMetadata::Get().GetHeaderFileExt());
323
324 // Fix the paths if needed
325 // i.e. if the derived classes are relative, make them use the same
326 // path as the base classes file
327 if(derivedClassFileCPP.IsRelative()) {
328 derivedClassFileCPP.SetPath(headerFile.GetPath());
329 }
330
331 if(derivedClassFileHPP.IsRelative()) {
332 derivedClassFileHPP.SetPath(headerFile.GetPath());
333 }
334
335 wxString dCpp, dH, dBlockGuard;
336 dBlockGuard << inheritedClass;
337 dBlockGuard << "_" << derivedClassFileHPP.GetExt();
338 dBlockGuard.MakeUpper();
339
340 // Prepare the Header file content
341 dH << "#ifndef " << dBlockGuard << "\n"
342 << "#define " << dBlockGuard << "\n"
343 << "#include \"" << headerFile.GetFullName() << "\"\n"
344 << "\n"
345 << "class " << (cppDecorator.IsEmpty() ? "" : cppDecorator + " ") << inheritedClass << " : public "
346 << derivedSuperclass << "\n"
347 << "{\n"
348 << "public:\n"
349 << " " << inheritedClass << GetDerivedClassCtorSignature() << ";\n"
350 << " virtual ~" << inheritedClass << "();\n"
351 << "};\n"
352 << "#endif // " << dBlockGuard << "\n";
353
354 // Prepare the CPP file content
355 dCpp << "#include \"" << derivedClassFileHPP.GetFullName() << "\"\n\n"
356 << inheritedClass << "::" << inheritedClass << GetDerivedClassCtorSignature() << "\n"
357 << " : " << baseClassName << GetParentCtorInitArgumentList() << "\n"
358 << "{\n"
359 << "}\n\n"
360 << inheritedClass << "::~" << inheritedClass << "()\n"
361 << "{\n"
362 << "}\n\n";
363
364 // Keep track of the generated files
365 if(WantsSubclass()) {
366 wxcProjectMetadata::Get().SetGeneratedHeader(derivedClassFileHPP);
367 wxcProjectMetadata::Get().SetGeneratedSource(derivedClassFileCPP);
368 }
369
370 wxcProjectMetadata::Get().SetGeneratedClassName(inheritedClass);
371 wxcProjectMetadata::Get().SetVirtualFolder(GetVirtualFolder());
372
373 if(WantsSubclass() && wxCrafter::IsTheSame(derivedClassFileHPP, dH) == false) {
374 wxCrafter::WriteFile(derivedClassFileHPP, dH, false);
375 }
376
377 if(WantsSubclass() && wxCrafter::IsTheSame(derivedClassFileCPP, dCpp) == false) {
378 wxCrafter::WriteFile(derivedClassFileCPP, dCpp, false);
379 }
380
381 // Tell CodeLite to reload externall modified files
382 EventNotifier::Get()->PostReloadExternallyModifiedEvent(true);
383 }
384
385 // static
386 void TopLevelWinWrapper::WrapXRC(wxString& text)
387 {
388 wxString prefix, sifa;
389 prefix << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>"
390 << "<resource xmlns=\"http://www.wxwidgets.org/wxxrc\">";
391 sifa << "</resource>";
392 text.Prepend(prefix).Append(sifa);
393 }
394
395 bool TopLevelWinWrapper::IsWxTopLevelWindow() const
396 {
397 return (GetType() == ID_WXWIZARD || GetType() == ID_WXDIALOG || GetType() == ID_WXFRAME);
398 }
399
400 wxString TopLevelWinWrapper::CppCtorCode() const { return CPPCommonAttributes(); }