Debian Codegraph @

wxcrafter/src/NewFormWizard.cpp

raw
1 #include "NewFormWizard.h"
2
3 #include "VirtualDirectorySelectorDlg.h"
4 #include "allocator_mgr.h"
5 #include "clFileSystemWorkspace.hpp"
6 #include "wxc_project_metadata.h"
7 #include "wxc_settings.h"
8
9 #include <macros.h>
10 #include <project.h>
11 #include <workspace.h>
12 #include <wx/dirdlg.h>
13 #include <wx/filedlg.h>
14 #include <wx/msgdlg.h>
15 #include <wx/textdlg.h>
16
17 static wxString GetDisplayName(const wxFileName& fn)
18 {
19 if(!fn.IsOk()) {
20 return wxEmptyString;
21 }
22 wxString displayName;
23 if(fn.GetDirCount()) {
24 displayName << fn.GetDirs().Last() << wxFileName::GetPathSeparator();
25 }
26 displayName << fn.GetFullName();
27 return displayName;
28 }
29
30 NewFormWizard::NewFormWizard(wxWindow* parent, IManager* mgr, int type)
31 : NewFormWizardBaseClass(parent)
32 , m_mgr(mgr)
33 {
34 wxString stringSelection;
35 switch(type) {
36 case ID_WXDIALOG:
37 stringSelection = wxT("wxDialog");
38 break;
39 case ID_WXFRAME:
40 stringSelection = wxT("wxFrame");
41 break;
42 case ID_WXWIZARD:
43 stringSelection = wxT("wxWizard");
44 break;
45 case ID_WXPANEL_TOPLEVEL:
46 stringSelection = wxT("wxPanel");
47 break;
48 case ID_WXIMAGELIST:
49 stringSelection = wxT("wxImageList");
50 break;
51 case ID_WXPOPUPWINDOW:
52 stringSelection = wxT("wxPopupWindow");
53 break;
54 case ID_WXAUITOOLBARTOPLEVEL:
55 stringSelection = "wxAuiToolBar";
56 break;
57 }
58
59 if(stringSelection.IsEmpty() == false)
60 m_choiceFormType->SetStringSelection(stringSelection);
61
62 if(m_mgr) {
63 TreeItemInfo item = m_mgr->GetSelectedTreeItemInfo(TreeFileView);
64 if(item.m_item.IsOk() && item.m_itemType == ProjectItem::TypeVirtualDirectory) {
65 wxString path = VirtualDirectorySelectorDlg::DoGetPath(m_mgr->GetWorkspaceTree(), item.m_item, false);
66 if(path.IsEmpty() == false) {
67 m_textCtrlVirtualFolder->ChangeValue(path);
68 m_textCtrlVirtualFolder->SetEditable(false);
69 }
70 }
71 }
72
73 // Populate the *wxcp files
74 std::set<wxString> uniqueFiles;
75 std::vector<std::pair<wxString, wxString>> wxcp_files;
76
77 if(clCxxWorkspaceST::Get()->IsOpen()) {
78 wxArrayString allFiles;
79 clCxxWorkspaceST::Get()->GetWorkspaceFiles(allFiles);
80 for(size_t i = 0; i < allFiles.GetCount(); ++i) {
81 const wxString& filename = allFiles.Item(i);
82 wxFileName fn(filename);
83 if(fn.GetExt().Lower() == "wxcp") {
84 if(uniqueFiles.count(filename) == 0) {
85 uniqueFiles.insert(filename);
86
87 // we keep {display name:fullpath} pairs
88 wxcp_files.push_back({ GetDisplayName(fn), fn.GetFullPath() });
89 }
90 }
91 }
92
93 for(const auto& p : wxcp_files) {
94 m_choiceWxcp->Append(p.first, new wxStringClientData(p.second));
95 }
96 }
97
98 if(wxcProjectMetadata::Get().IsLoaded()) {
99 wxFileName fn(wxcProjectMetadata::Get().GetProjectFile());
100 m_choiceWxcp->Append(GetDisplayName(fn), new wxStringClientData(fn.GetFullPath()));
101 }
102
103 if(!m_choiceWxcp->IsEmpty()) {
104 m_choiceWxcp->SetSelection(0);
105 }
106
107 wxFileName fnProject(wxcProjectMetadata::Get().GetProjectFile());
108 int where = m_choiceWxcp->FindString(GetDisplayName(fnProject));
109 if(where != wxNOT_FOUND) {
110 m_choiceWxcp->SetSelection(where);
111 }
112 }
113
114 NewFormWizard::~NewFormWizard() {}
115
116 void NewFormWizard::OnFormTypeSelected(wxCommandEvent& event) { event.Skip(); }
117
118 void NewFormWizard::OnSelectVirtualFolder(wxCommandEvent& event)
119 {
120 wxString suggestedName = m_textCtrlVirtualFolder->GetValue();
121 if(suggestedName.empty() && clCxxWorkspaceST::Get()) {
122 suggestedName = clCxxWorkspaceST::Get()->GetActiveProjectName();
123 }
124 VirtualDirectorySelectorDlg selector(this, clCxxWorkspaceST::Get(), suggestedName);
125 if(selector.ShowModal() == wxID_OK) {
126 m_textCtrlVirtualFolder->SetEditable(true);
127 m_textCtrlVirtualFolder->ChangeValue(selector.GetVirtualDirectoryPath());
128 m_textCtrlVirtualFolder->SetEditable(false);
129 }
130 }
131
132 void NewFormWizard::OnFinishClicked(wxWizardEvent& event) { event.Skip(); }
133
134 int NewFormWizard::GetFormType() const
135 {
136 if(IsDialog())
137 return ID_WXDIALOG;
138 else if(IsFrame())
139 return ID_WXFRAME;
140 else if(IsWizard())
141 return ID_WXWIZARD;
142 else if(IsPanel())
143 return ID_WXPANEL_TOPLEVEL;
144 else if(IsImageList())
145 return ID_WXIMAGELIST;
146 else if(IsAuiToolBar())
147 return ID_WXAUITOOLBARTOPLEVEL;
148 else if(IsPopupWindow())
149 return ID_WXPOPUPWINDOW;
150 return wxNOT_FOUND;
151 }
152
153 wxString NewFormWizard::GetVirtualFolder() const { return m_textCtrlVirtualFolder->GetValue(); }
154
155 wxString NewFormWizard::GetClassName() const { return m_textCtrlClassName->GetValue(); }
156
157 wxString NewFormWizard::GetGeneratedFileBaseName() const { return m_textCtrFileName->GetValue(); }
158
159 void NewFormWizard::OnWizardPageChanging(wxWizardEvent& event)
160 {
161 if(event.GetDirection() && m_wizardPageGeneratedCode == event.GetPage()) {
162
163 #if STANDALONE_BUILD
164 if(m_choiceWxcp->IsEmpty()) {
165 ::wxMessageBox(wxString() << _("You must create a project before you can add new forms"), "wxCrafter",
166 wxOK | wxCENTER | wxICON_WARNING);
167 event.Veto();
168 return;
169 }
170 #endif
171
172 if(m_textCtrFileName->IsEmpty() &&
173 (GetFormType() != ID_WXIMAGELIST && GetFormType() != ID_WXAUITOOLBARTOPLEVEL)) {
174 ::wxMessageBox(_("Please enter a file name"), wxT("wxCrafter"), wxICON_WARNING | wxCENTER | wxOK);
175 event.Veto();
176 return;
177 }
178
179 if(m_textCtrlClassName->IsEmpty()) {
180 ::wxMessageBox(_("Please enter a class name"), wxT("wxCrafter"), wxICON_WARNING | wxCENTER | wxOK);
181 event.Veto();
182 return;
183 }
184
185 #if !STANDALONE_BUILD
186 if(!clFileSystemWorkspace::Get().IsOpen() && m_textCtrlVirtualFolder->IsEmpty()) {
187 ::wxMessageBox(_("Please select a virtual folder for the generated code"), wxT("wxCrafter"),
188 wxICON_WARNING | wxCENTER | wxOK);
189 event.Veto();
190 return;
191 }
192 #endif
193 }
194 event.Skip();
195 }
196
197 void NewFormWizard::OnMakeSkeletonAppUI(wxUpdateUIEvent& event)
198 {
199 int formType = GetFormType();
200 event.Enable(formType != ID_WXPANEL_TOPLEVEL);
201 }
202
203 wxString NewFormWizard::GetTitle() const { return m_textCtrlTitle->GetValue(); }
204
205 void NewFormWizard::OnNewWxcpProject(wxCommandEvent& event)
206 {
207 wxString msg;
208 msg << _("Enter the new wxCrafter file a name (full path):");
209
210 wxFileName defaultValue(::wxGetCwd(), "my_wxcp_file.wxcp");
211
212 if(m_mgr) {
213 ProjectPtr project = m_mgr->GetSelectedProject();
214 if(project) {
215 defaultValue.SetPath(project->GetFileName().GetPath());
216 } else {
217 defaultValue.SetPath(::wxGetCwd());
218 }
219 defaultValue.SetFullName(wxT("my_wxcp_file.wxcp"));
220 }
221
222 wxString newfile = ::wxGetTextFromUser(msg, wxT("wxCrafter"), defaultValue.GetFullPath());
223 if(newfile.IsEmpty())
224 return;
225
226 wxFileName fn(newfile);
227 fn.SetExt(wxT("wxcp"));
228
229 wxString displayName = GetDisplayName(fn);
230 int where = m_choiceWxcp->FindString(displayName);
231 if(where == wxNOT_FOUND) {
232 int newpos = m_choiceWxcp->Append(displayName, new wxStringClientData(fn.GetFullPath()));
233 m_choiceWxcp->SetSelection(newpos);
234
235 } else {
236 // already exists
237 m_choiceWxcp->SetSelection(where);
238 }
239 }
240
241 wxString NewFormWizard::GetWxcpFile() const
242 {
243 int selection = m_choiceWxcp->GetSelection();
244 if(selection == wxNOT_FOUND) {
245 return "";
246 }
247
248 wxStringClientData* cd = reinterpret_cast<wxStringClientData*>(m_choiceWxcp->GetClientObject(selection));
249 wxFileName fn(cd->GetData());
250 return fn.GetFullPath();
251 }
252
253 void NewFormWizard::OnTitleUI(wxUpdateUIEvent& event)
254 {
255 event.Enable(!IsImageList() && !IsPanel() && !IsPopupWindow() && !IsAuiToolBar());
256 }
257
258 void NewFormWizard::OnFilenameUI(wxUpdateUIEvent& event) { event.Enable(!IsImageList()); }
259
260 void NewFormWizard::OnInheritedClassNameUI(wxUpdateUIEvent& event) { event.Enable(!IsImageList()); }
261
262 wxString NewFormWizard::GetInheritedClassName() const
263 {
264 return m_textCtrlInheritedClassName->GetValue().Trim().Trim(false);
265 }
266
267 void NewFormWizard::OnStandloneAppUI(wxUpdateUIEvent& event)
268 {
269 #if STANDALONE_BUILD
270 event.Enable(false);
271 #else
272 event.Enable(true);
273 #endif
274 }
275
276 void NewFormWizard::OnInheritedNameFocus(wxFocusEvent& event)
277 {
278 event.Skip();
279
280 if(!m_textCtrlInheritedClassName->IsEmpty() || m_textCtrlClassName->IsEmpty()) {
281 // There's nothing to copy, or we already have an entry
282 return;
283 }
284
285 wxString suggestion = m_textCtrlClassName->GetValue();
286 if(suggestion.Replace("Base", "") || suggestion.Replace("base", "")) {
287 m_textCtrlInheritedClassName->ChangeValue(suggestion);
288 }
289 }
290
291 void NewFormWizard::OnFilenameFocus(wxFocusEvent& event)
292 {
293 event.Skip();
294
295 if(!m_textCtrFileName->IsEmpty() || m_textCtrlInheritedClassName->IsEmpty()) {
296 // There's nothing to copy, or we already have an entry
297 return;
298 }
299
300 // Make the default file name same as the class name
301 wxString suggestion = m_textCtrlInheritedClassName->GetValue();
302 m_textCtrFileName->ChangeValue(suggestion);
303 }
304
305 bool NewFormWizard::IsAuiToolBar() const { return m_choiceFormType->GetStringSelection() == "wxAuiToolBar"; }
306
307 bool NewFormWizard::IsDialog() const { return m_choiceFormType->GetStringSelection() == "wxDialog"; }
308
309 bool NewFormWizard::IsFrame() const { return m_choiceFormType->GetStringSelection() == "wxFrame"; }
310
311 bool NewFormWizard::IsImageList() const { return m_choiceFormType->GetStringSelection() == "wxImageList"; }
312
313 bool NewFormWizard::IsPanel() const { return m_choiceFormType->GetStringSelection() == "wxPanel"; }
314
315 bool NewFormWizard::IsPopupWindow() const { return m_choiceFormType->GetStringSelection() == "wxPopupWindow"; }
316
317 bool NewFormWizard::IsWizard() const { return m_choiceFormType->GetStringSelection() == "wxWizard"; }
318
319 void NewFormWizard::OnSelectVDUI(wxUpdateUIEvent& event)
320 {
321 #if STANDALONE_BUILD
322 event.Enable(false);
323 #else
324 event.Enable(!clFileSystemWorkspace::Get().IsOpen());
325 #endif
326 }
327
328 void NewFormWizard::OnBrowseWxcpFile(wxCommandEvent& event)
329 {
330 wxString path = wxFileSelector(_("Select wxCrafter file"), wxEmptyString, wxEmptyString, "*.wxcp");
331 if(path.empty()) {
332 return;
333 }
334
335 wxFileName fn(path);
336 wxString displayName = GetDisplayName(fn);
337 int where = m_choiceWxcp->FindString(displayName);
338 if(where == wxNOT_FOUND) {
339 where = m_choiceWxcp->Append(displayName, new wxStringClientData(path));
340 }
341 m_choiceWxcp->SetSelection(where);
342 }