Debian Codegraph @

wxcrafter/src/main.cpp

raw
1 #include "main.h"
2
3 #include "ColoursAndFontsManager.h"
4 #include "bitmap_loader.h"
5 #include "cl_aui_dock_art.h"
6 #include "editor_config.h"
7 #include "event_notifier.h"
8 #include "file_logger.h"
9 #include "globals.h"
10 #include "imanager.h"
11 #include "plugin.h"
12 #include "windowattrmanager.h"
13 #include "wxc_aui_tool_stickiness.h"
14 #include "wxc_bitmap_code_generator.h"
15 #include "wxc_project_metadata.h"
16 #include "wxgui_bitmaploader.h"
17 #include "wxgui_defs.h"
18 #include "wxguicraft_main_view.h"
19
20 #include <wx/dcmemory.h>
21 #include <wx/ffile.h>
22 #include <wx/filedlg.h>
23 #include <wx/frame.h>
24 #include <wx/iconbndl.h>
25 #include <wx/msgdlg.h>
26 #include <wx/socket.h>
27 #include <wx/sysopt.h>
28 #include <wx/toplevel.h>
29
30 #define CHECK_IF_FOCUS_ON_READONLY_STC() \
31 wxStyledTextCtrl* stc = GetActiveSTC(); \
32 if(stc) { \
33 return; \
34 }
35
36 #if STANDALONE_BUILD
37
38 static const wxCmdLineEntryDesc cmdLineDesc[] = {
39 { wxCMD_LINE_SWITCH, "v", "version", "Print current version", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
40 { wxCMD_LINE_SWITCH, "h", "help", "Print usage", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
41 { wxCMD_LINE_SWITCH, "s", "server", "Start in server mode (hidden)", wxCMD_LINE_VAL_STRING,
42 wxCMD_LINE_PARAM_OPTIONAL },
43 { wxCMD_LINE_PARAM, NULL, NULL, "Input file", wxCMD_LINE_VAL_STRING,
44 wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
45 { wxCMD_LINE_NONE }
46 };
47
48 IMPLEMENT_APP(wxcApp)
49
50 #ifdef __WXMSW__
51 typedef BOOL WINAPI (*SetProcessDPIAwareFunc)();
52 #endif
53
54 wxcApp::wxcApp()
55 : m_hiddenMainFrame(false)
56 {
57 }
58
59 wxcApp::~wxcApp() {}
60
61 bool wxcApp::OnInit()
62 {
63 // parse command line
64 wxCmdLineParser parser;
65 parser.SetDesc(cmdLineDesc);
66 parser.SetCmdLine(wxAppBase::argc, wxAppBase::argv);
67 if(parser.Parse() != 0) {
68 return false;
69 }
70
71 if(parser.Found("h")) {
72 // print usage
73 parser.Usage();
74 return false;
75 }
76 m_hiddenMainFrame = parser.Found("s");
77
78 wxSystemOptions::SetOption(_T("msw.remap"), 0);
79 wxSystemOptions::SetOption("msw.notebook.themed-background", 0);
80 wxXmlResource::Get()->InitAllHandlers();
81 wxImage::AddHandler(new wxPNGHandler);
82 wxImage::AddHandler(new wxCURHandler);
83 wxImage::AddHandler(new wxICOHandler);
84 wxImage::AddHandler(new wxXPMHandler);
85 wxImage::AddHandler(new wxGIFHandler);
86 // InitXmlResource();
87
88 wxLog::EnableLogging(false);
89
90 #ifdef __WXGTK__
91 // Redirect stdout/error to a file
92 wxFileName stdout_err(wxStandardPaths::Get().GetUserDataDir(), "wxcrafter-stdout-stderr.log");
93 #ifndef NDEBUG
94 stdout_err.SetPath(stdout_err.GetPath() + "-dbg");
95 #endif
96 FILE* new_stdout = ::freopen(stdout_err.GetFullPath().mb_str(wxConvISO8859_1).data(), "a+b", stdout);
97 FILE* new_stderr = ::freopen(stdout_err.GetFullPath().mb_str(wxConvISO8859_1).data(), "a+b", stderr);
98 wxUnusedVar(new_stderr);
99 wxUnusedVar(new_stdout);
100 #endif
101
102 #ifdef __WXGTK__
103 wxString installPrefix = INSTALL_PREFIX;
104 wxStandardPaths::Get().SetInstallPrefix(installPrefix);
105 EditorConfigST::Get()->SetInstallDir(wxString() << INSTALL_PREFIX << "/share/wxcrafter");
106
107 #elif defined(__WXMSW__)
108 HINSTANCE m_user32Dll = LoadLibrary(L"User32.dll");
109 if(m_user32Dll) {
110 SetProcessDPIAwareFunc pFunc = (SetProcessDPIAwareFunc)GetProcAddress(m_user32Dll, "SetProcessDPIAware");
111 if(pFunc) {
112 pFunc();
113 }
114 FreeLibrary(m_user32Dll);
115 m_user32Dll = NULL;
116 }
117
118 EditorConfigST::Get()->SetInstallDir(wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetPath());
119 #endif
120 EditorConfigST::Get()->Init("", "2.0.2");
121 EditorConfigST::Get()->Load();
122
123 for(size_t i = 0; i < parser.GetParamCount(); i++) {
124 wxString argument = parser.GetParam(i);
125
126 // convert to full path and open it
127 wxFileName fn(argument);
128 fn.MakeAbsolute(wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetPath());
129
130 if(fn.GetExt() == "wxcp") {
131 wxCommandEvent evtOpen(wxEVT_WXC_OPEN_PROJECT);
132 evtOpen.SetString(fn.GetFullPath());
133 EventNotifier::Get()->AddPendingEvent(evtOpen);
134 }
135 }
136
137 // initialize the socket library
138 wxSocketBase::Initialize();
139
140 // Initialize the edit manager
141 wxcEditManager::Get();
142
143 // Initialize the colours and font manager
144 ColoursAndFontsManager::Get().Load();
145 ColoursAndFontsManager::Get().RestoreDefaults();
146
147 // Open log file
148 FileLogger::OpenLog("wxcrafter.log", FileLogger::Dbg);
149 clDEBUG() << "wxCrafter started" << endl;
150
151 m_wxcPlugin = new wxCrafterPlugin(NULL, false);
152 SetTopWindow(m_wxcPlugin->GetMainFrame());
153 if(false) {
154 // Don't show the top window
155 return true;
156
157 } else {
158 return GetTopWindow()->Show();
159 }
160 }
161
162 int wxcApp::OnExit()
163 {
164 wxDELETE(m_wxcPlugin);
165 return TRUE;
166 }
167
168 bool wxcApp::OnCmdLineParsed(wxCmdLineParser& parser) { return true; }
169
170 #endif
171
172 // --------------------------------------------------------------------------------------------------
173 // Main Frame
174 // --------------------------------------------------------------------------------------------------
175
176 BEGIN_EVENT_TABLE(MainFrame, wxFrame)
177 EVT_FIND_CLOSE(wxID_ANY, MainFrame::OnFindDlgClose)
178 EVT_FIND(wxID_ANY, MainFrame::OnFindFirst)
179 EVT_FIND_NEXT(wxID_ANY, MainFrame::OnFindNext)
180 END_EVENT_TABLE()
181
182 MainFrame::MainFrame(wxWindow* parent, bool hidden)
183 : MainFrameBase(parent, wxID_ANY, "wxCrafter", wxDefaultPosition, wxDefaultSize,
184 wxDEFAULT_FRAME_STYLE | wxTAB_TRAVERSAL)
185 , m_wxcView(NULL)
186 , m_treeView(NULL)
187 , m_findReplaceDialog(NULL)
188 , m_exiting(false)
189 {
190 m_mainToolbar->SetMiniToolBar(false);
191 auto images = m_mainToolbar->GetBitmapsCreateIfNeeded();
192
193 #if !STANDALONE_BUILD
194 m_mainToolbar->AddTool(wxID_BACKWARD, _("Back to CodeLite"), images->Add("back"));
195 #endif
196
197 m_mainToolbar->AddTool(wxID_NEW, _("New Project"), images->Add("file_new"));
198 m_mainToolbar->AddTool(wxID_OPEN, _("Open Project"), images->Add("file_open"), "", wxITEM_DROPDOWN);
199 m_mainToolbar->AddTool(wxID_CLOSE, _("Close"), images->Add("file_close"));
200 m_mainToolbar->AddTool(wxID_SAVE, _("Save"), images->Add("file_save"));
201 m_mainToolbar->AddSeparator();
202 m_mainToolbar->AddTool(wxID_UNDO, _("Undo"), images->Add("undo"));
203 m_mainToolbar->AddTool(wxID_REDO, _("Redo"), images->Add("redo"));
204 m_mainToolbar->AddSeparator();
205 m_mainToolbar->AddTool(wxID_FIND, _("Find"), images->Add("find"));
206 m_mainToolbar->AddSeparator();
207 m_mainToolbar->AddTool(XRCID("generate-code"), _("Generate Code"), images->Add("execute"));
208 m_mainToolbar->Realize();
209
210 #if !STANDALONE_BUILD
211 m_mainToolbar->Bind(wxEVT_TOOL, &MainFrame::OnSwitchToCodelite, this, wxID_BACKWARD);
212 m_mainToolbar->Bind(wxEVT_UPDATE_UI, &MainFrame::OnSwitchToCodeliteUI, this, wxID_BACKWARD);
213 #endif
214
215 m_mainToolbar->Bind(wxEVT_TOOL, &MainFrame::OnNewProject, this, wxID_NEW);
216 m_mainToolbar->Bind(wxEVT_TOOL, &MainFrame::OnOpen, this, wxID_OPEN);
217 m_mainToolbar->Bind(wxEVT_TOOL_DROPDOWN, &MainFrame::OnOpenMenu, this, wxID_OPEN);
218 m_mainToolbar->Bind(wxEVT_UPDATE_UI, &MainFrame::OnOpenUI, this, wxID_OPEN);
219
220 m_mainToolbar->Bind(wxEVT_TOOL, &MainFrame::OnClose, this, wxID_CLOSE);
221 m_mainToolbar->Bind(wxEVT_UPDATE_UI, &MainFrame::OnCloseUI, this, wxID_CLOSE);
222
223 m_mainToolbar->Bind(wxEVT_TOOL, &MainFrame::OnSave, this, wxID_SAVE);
224 m_mainToolbar->Bind(wxEVT_UPDATE_UI, &MainFrame::OnSaveUI, this, wxID_SAVE);
225
226 m_mainToolbar->Bind(wxEVT_TOOL, &MainFrame::OnUndo, this, wxID_UNDO);
227 m_mainToolbar->Bind(wxEVT_UPDATE_UI, &MainFrame::OnUndoUI, this, wxID_UNDO);
228
229 m_mainToolbar->Bind(wxEVT_TOOL, &MainFrame::OnRedo, this, wxID_REDO);
230 m_mainToolbar->Bind(wxEVT_UPDATE_UI, &MainFrame::OnRedoUI, this, wxID_REDO);
231
232 m_mainToolbar->Bind(wxEVT_TOOL, &MainFrame::OnOpenFindDialog, this, wxID_FIND);
233 m_mainToolbar->Bind(wxEVT_UPDATE_UI, &MainFrame::OnFindUI, this, wxID_FIND);
234
235 m_mainToolbar->Bind(wxEVT_TOOL, &MainFrame::OnGenerateCode, this, XRCID("generate-code"));
236 m_mainToolbar->Bind(wxEVT_UPDATE_UI, &MainFrame::OnGenerateCodeUI, this, XRCID("generate-code"));
237
238 wxIconBundle icons;
239 wxCrafter::ResourceLoader rl;
240
241 {
242 wxBitmap bmp = rl.Bitmap("wxc-logo-16");
243 wxIcon icn;
244 icn.CopyFromBitmap(bmp);
245 icons.AddIcon(icn);
246 }
247
248 {
249 wxBitmap bmp = rl.Bitmap("wxc-logo-32");
250 wxIcon icn;
251 icn.CopyFromBitmap(bmp);
252 icons.AddIcon(icn);
253 }
254
255 {
256 wxBitmap bmp = rl.Bitmap("wxc-logo-64");
257 wxIcon icn;
258 icn.CopyFromBitmap(bmp);
259 icons.AddIcon(icn);
260 }
261
262 {
263 wxBitmap bmp = rl.Bitmap("wxc-logo-128");
264 wxIcon icn;
265 icn.CopyFromBitmap(bmp);
266 icons.AddIcon(icn);
267 }
268
269 SetIcons(icons);
270
271 EventNotifier::Get()->Connect(wxEVT_CMD_WXCRAFTER_PROJECT_MODIFIED,
272 wxCommandEventHandler(MainFrame::OnProjectModified), NULL, this);
273 EventNotifier::Get()->Connect(wxEVT_CMD_WXCRAFTER_PROJECT_SYNCHED,
274 wxCommandEventHandler(MainFrame::OnProjectSynched), NULL, this);
275 EventNotifier::Get()->Connect(wxEVT_WXC_PROJECT_LOADED, wxCommandEventHandler(MainFrame::OnProjectLoaded), NULL,
276 this);
277 EventNotifier::Get()->Connect(wxEVT_WXC_CLOSE_PROJECT, wxCommandEventHandler(MainFrame::OnProjectClosed), NULL,
278 this);
279 EventNotifier::Get()->Bind(wxEVT_WORKSPACE_CLOSED, &MainFrame::OnWorkspaceClosed, this);
280 EventNotifier::Get()->Connect(wxEVT_CODELITE_MAINFRAME_GOT_FOCUS,
281 wxCommandEventHandler(MainFrame::OnCodeLiteGotFocus), NULL, this);
282 EventNotifier::Get()->Connect(wxEVT_WXC_CODE_PREVIEW_PAGE_CHANGED,
283 wxCommandEventHandler(MainFrame::OnCodeEditorSelected), NULL, this);
284
285 #if !STANDALONE_BUILD
286 Hide();
287 SetCanFocus(true);
288 SetName("MainFrame");
289 WindowAttrManager::Load(this);
290 #else
291 SetName("MainFrame");
292 WindowAttrManager::Load(this);
293 #endif
294 }
295
296 MainFrame::~MainFrame()
297 {
298 EventNotifier::Get()->Disconnect(wxEVT_CMD_WXCRAFTER_PROJECT_MODIFIED,
299 wxCommandEventHandler(MainFrame::OnProjectModified), NULL, this);
300 EventNotifier::Get()->Disconnect(wxEVT_CMD_WXCRAFTER_PROJECT_SYNCHED,
301 wxCommandEventHandler(MainFrame::OnProjectSynched), NULL, this);
302 EventNotifier::Get()->Disconnect(wxEVT_WXC_PROJECT_LOADED, wxCommandEventHandler(MainFrame::OnProjectLoaded), NULL,
303 this);
304 EventNotifier::Get()->Disconnect(wxEVT_WXC_CLOSE_PROJECT, wxCommandEventHandler(MainFrame::OnProjectClosed), NULL,
305 this);
306 EventNotifier::Get()->Unbind(wxEVT_WORKSPACE_CLOSED, &MainFrame::OnWorkspaceClosed, this);
307 EventNotifier::Get()->Disconnect(wxEVT_CODELITE_MAINFRAME_GOT_FOCUS,
308 wxCommandEventHandler(MainFrame::OnCodeLiteGotFocus), NULL, this);
309 EventNotifier::Get()->Disconnect(wxEVT_WXC_CODE_PREVIEW_PAGE_CHANGED,
310 wxCommandEventHandler(MainFrame::OnCodeEditorSelected), NULL, this);
311
312 #if STANDALONE_BUILD
313 if(m_findReplaceDialog) {
314 m_findReplaceDialog->Destroy();
315 m_findReplaceDialog = NULL;
316 }
317 #endif
318 }
319
320 void MainFrame::OnCloseFrame(wxCloseEvent& event)
321 {
322 #if STANDALONE_BUILD
323 #ifndef __WXMSW__
324 // We support task bar icon on Windows only
325 event.Skip();
326 wxcCodeGeneratorHelper::Get().UnInitialize();
327 #else
328 /// In a standalone mode, close the frame
329 event.Skip();
330 wxcCodeGeneratorHelper::Get().UnInitialize();
331 #endif
332
333 #else
334 wxUnusedVar(event);
335 HideDesigner();
336 #endif
337 }
338
339 void MainFrame::Add(GUICraftMainPanel* view)
340 {
341 m_wxcView = view;
342 m_splitterPageDesigner->GetSizer()->Add(view, 1, wxEXPAND);
343 m_splitterPageDesigner->GetSizer()->Layout();
344 }
345
346 void MainFrame::Add(wxcTreeView* tree)
347 {
348 m_treeView = tree;
349 m_splitterPageTreeView->GetSizer()->Add(tree, 1, wxEXPAND);
350 m_splitterPageTreeView->GetSizer()->Layout();
351 }
352
353 void MainFrame::OnClose(wxCommandEvent& event)
354 {
355 wxCommandEvent evtClose(wxEVT_WXC_CLOSE_PROJECT);
356 EventNotifier::Get()->AddPendingEvent(evtClose);
357 }
358
359 void MainFrame::OnSave(wxCommandEvent& event) { m_treeView->SaveProject(); }
360
361 void MainFrame::OnProjectModified(wxCommandEvent& e)
362 {
363 e.Skip();
364 wxString title = GetTitle();
365 if(!title.StartsWith("*")) {
366 title.Prepend("*");
367 SetTitle(title);
368 }
369 }
370
371 void MainFrame::OnProjectSynched(wxCommandEvent& e)
372 {
373 e.Skip();
374 wxString title = GetTitle();
375 if(title.StartsWith("*")) {
376 title.Remove(0, 1);
377 SetTitle(title);
378 }
379 }
380
381 void MainFrame::OnProjectLoaded(wxCommandEvent& e)
382 {
383 e.Skip();
384 SetTitle("wxCrafter - " + e.GetString());
385 }
386
387 void MainFrame::OnDeleteItem(wxCommandEvent& event)
388 {
389 wxTextCtrl* text = GetActiveTextCtrl();
390 CHECK_IF_FOCUS_ON_READONLY_STC();
391
392 if(text) {
393 event.StopPropagation();
394
395 long from, to;
396 text->GetSelection(&from, &to);
397 text->Replace(from, to, "");
398
399 } else {
400 wxCommandEvent evt(wxEVT_COMMAND_MENU_SELECTED, ID_DELETE_NODE);
401 m_wxcView->GetEventHandler()->AddPendingEvent(evt);
402 }
403 }
404
405 void MainFrame::OnDeleteItemUI(wxUpdateUIEvent& event)
406 {
407 GUICraftItemData* itemData = m_wxcView->GetSelItemData();
408 event.Enable(itemData);
409 }
410
411 void MainFrame::OnPreview(wxCommandEvent& event)
412 {
413 wxCommandEvent evt(wxEVT_COMMAND_MENU_SELECTED, ID_SHOW_PREVIEW);
414 m_wxcView->GetEventHandler()->AddPendingEvent(evt);
415 }
416
417 void MainFrame::OnPreviewUI(wxUpdateUIEvent& event)
418 {
419 event.Enable(!m_wxcView->IsPreviewAlive() && wxcProjectMetadata::Get().IsLoaded());
420 }
421
422 void MainFrame::OnWorkspaceClosed(clWorkspaceEvent& e)
423 {
424 e.Skip();
425 SetTitle("wxCrafter");
426 }
427
428 void MainFrame::OnCopy(wxCommandEvent& event)
429 {
430 wxTextCtrl* text = GetActiveTextCtrl();
431 wxStyledTextCtrl* stc = GetActiveSTC();
432 if(text) {
433 event.StopPropagation();
434 if(text->CanCopy()) {
435 text->Copy();
436 }
437
438 } else if(stc) {
439 event.StopPropagation();
440 if(stc->CanCopy()) {
441 stc->Copy();
442 }
443
444 } else {
445 wxCommandEvent evt(wxEVT_COMMAND_MENU_SELECTED, ID_COPY);
446 m_wxcView->GetEventHandler()->AddPendingEvent(evt);
447 }
448 }
449
450 void MainFrame::OnCut(wxCommandEvent& event)
451 {
452 CHECK_IF_FOCUS_ON_READONLY_STC();
453 wxTextCtrl* text = GetActiveTextCtrl();
454
455 if(text) {
456
457 event.StopPropagation();
458 if(text->CanCut()) {
459 text->Cut();
460 }
461 } else {
462 wxCommandEvent evt(wxEVT_COMMAND_MENU_SELECTED, ID_CUT);
463 m_wxcView->GetEventHandler()->AddPendingEvent(evt);
464 }
465 }
466
467 void MainFrame::OnPaste(wxCommandEvent& event)
468 {
469 CHECK_IF_FOCUS_ON_READONLY_STC();
470
471 wxTextCtrl* text = GetActiveTextCtrl();
472 if(text) {
473 event.StopPropagation();
474 if(text->CanPaste()) {
475 text->Paste();
476 }
477
478 } else {
479
480 wxCommandEvent evt(wxEVT_COMMAND_MENU_SELECTED, ID_PASTE);
481 m_wxcView->GetEventHandler()->AddPendingEvent(evt);
482 }
483 }
484
485 void MainFrame::OnRedo(wxCommandEvent& event)
486 {
487 CHECK_IF_FOCUS_ON_READONLY_STC();
488 wxTextCtrl* text = GetActiveTextCtrl();
489 if(text) {
490 event.StopPropagation();
491 if(text->CanRedo()) {
492 text->Redo();
493 }
494
495 } else {
496 wxCommandEvent evt(wxEVT_COMMAND_MENU_SELECTED, wxID_REDO);
497 wxTheApp->AddPendingEvent(evt);
498 }
499 }
500
501 void MainFrame::OnRedoUI(wxUpdateUIEvent& event)
502 {
503 wxTextCtrl* text = GetActiveTextCtrl();
504 if(text) {
505 event.Enable(text->CanRedo());
506
507 } else {
508 event.Enable(wxcEditManager::Get().CanRedo());
509 }
510 }
511
512 void MainFrame::OnSelectionUI(wxUpdateUIEvent& event)
513 {
514 GUICraftItemData* itemData = m_wxcView->GetSelItemData();
515 event.Enable(itemData && itemData->m_wxcWidget);
516 }
517
518 void MainFrame::OnUndo(wxCommandEvent& event)
519 {
520 CHECK_IF_FOCUS_ON_READONLY_STC();
521 wxTextCtrl* text = GetActiveTextCtrl();
522 if(text) {
523 event.StopPropagation();
524 if(text->CanUndo()) {
525 text->Undo();
526 }
527
528 } else {
529 wxCommandEvent evt(wxEVT_COMMAND_MENU_SELECTED, wxID_UNDO);
530 wxTheApp->AddPendingEvent(evt);
531 }
532 }
533
534 void MainFrame::OnUndoUI(wxUpdateUIEvent& event)
535 {
536 wxTextCtrl* text = GetActiveTextCtrl();
537 if(text) {
538 event.Enable(text->CanUndo());
539 } else {
540 event.Enable(wxcEditManager::Get().CanUndo());
541 }
542 }
543
544 void MainFrame::OnBuild(wxCommandEvent& event)
545 {
546 wxCommandEvent evtBuild(wxEVT_COMMAND_MENU_SELECTED, XRCID("build_active_project"));
547 evtBuild.SetEventObject(this);
548 EventNotifier::Get()->TopFrame()->GetEventHandler()->AddPendingEvent(evtBuild);
549
550 // Also, notify to self to switch codelite
551 wxCommandEvent evtSwitch(wxEVT_COMMAND_MENU_SELECTED, wxID_BACKWARD);
552 evtSwitch.SetEventObject(this);
553 GetEventHandler()->AddPendingEvent(evtSwitch);
554 }
555
556 void MainFrame::OnBuildUI(wxUpdateUIEvent& event)
557 {
558 #if STANDALONE_BUILD
559 event.Enable(false);
560 #endif
561 }
562
563 void MainFrame::OnSwitchToCodelite(wxCommandEvent& event)
564 {
565 wxUnusedVar(event);
566 MinimizeDesigner();
567 }
568
569 void MainFrame::OnRename(wxCommandEvent& event)
570 {
571 wxCommandEvent evtRename(wxEVT_COMMAND_MENU_SELECTED, ID_RENAME);
572 m_wxcView->GetEventHandler()->AddPendingEvent(evtRename);
573 }
574
575 wxTextCtrl* MainFrame::GetActiveTextCtrl()
576 {
577 wxWindow* focusWin = wxWindow::FindFocus();
578 if(focusWin) {
579 return dynamic_cast<wxTextCtrl*>(focusWin);
580 }
581 return NULL;
582 }
583 void MainFrame::OnImportFB(wxCommandEvent& event)
584 {
585 wxCommandEvent evtImportFB(wxEVT_COMMAND_MENU_SELECTED, XRCID("import_wxFB_project"));
586 wxTheApp->AddPendingEvent(evtImportFB);
587 }
588
589 void MainFrame::OnImportSmith(wxCommandEvent& event)
590 {
591 wxCommandEvent evtImportFB(wxEVT_COMMAND_MENU_SELECTED, XRCID("import_wxSmith_project"));
592 wxTheApp->AddPendingEvent(evtImportFB);
593 }
594
595 void MainFrame::OnImportXRC(wxCommandEvent& event)
596 {
597 wxCommandEvent evtImportFB(wxEVT_COMMAND_MENU_SELECTED, XRCID("import_XRC_project"));
598 wxTheApp->AddPendingEvent(evtImportFB);
599 }
600
601 void MainFrame::OnAbout(wxCommandEvent& event)
602 {
603 wxUnusedVar(event);
604 wxCommandEvent evtAbout(wxEVT_COMMAND_MENU_SELECTED, XRCID("wxcp_about"));
605 wxTheApp->AddPendingEvent(evtAbout);
606 }
607
608 void MainFrame::OnHide(wxCommandEvent& event)
609 {
610 wxUnusedVar(event);
611 #if STANDALONE_BUILD
612 m_exiting = true;
613 Close();
614 #else
615 HideDesigner();
616 #endif
617 }
618
619 void MainFrame::OnSettings(wxCommandEvent& event)
620 {
621 wxCommandEvent e(wxEVT_COMMAND_MENU_SELECTED, event.GetId());
622 wxTheApp->AddPendingEvent(e);
623 }
624
625 void MainFrame::DisplayDesigner()
626 {
627 if(!IsShown()) {
628 Show();
629 }
630 if(IsIconized()) {
631 Restore();
632 }
633 Raise();
634 SetFocus();
635 }
636
637 void MainFrame::MinimizeDesigner()
638 {
639 if(IsShown() && !IsIconized()) {
640 // minimize to the task bar
641 this->Iconize();
642
643 wxFrame* mainFrame = EventNotifier::Get()->TopFrame();
644 if(mainFrame) {
645 mainFrame->Raise();
646 }
647 }
648 }
649
650 void MainFrame::HideDesigner()
651 {
652 if(IsShown()) {
653 // hide the designer
654 this->Hide();
655
656 wxFrame* mainFrame = EventNotifier::Get()->TopFrame();
657 if(mainFrame) {
658 mainFrame->Raise();
659 }
660 }
661 }
662
663 void MainFrame::OnProjectClosed(wxCommandEvent& event)
664 {
665 event.Skip();
666 SetTitle("wxCrafter");
667 }
668
669 void MainFrame::OnSwitchToCodeliteUI(wxUpdateUIEvent& event)
670 {
671 #if STANDALONE_BUILD
672 event.Enable(false);
673 #else
674 event.Enable(true);
675 #endif
676 }
677
678 void MainFrame::OnCodeLiteGotFocus(wxCommandEvent& e) { e.Skip(); }
679
680 void MainFrame::OnPasteUI(wxUpdateUIEvent& event) { event.Enable(true); }
681
682 void MainFrame::OnSaveUI(wxUpdateUIEvent& event) { event.Enable(wxcEditManager::Get().IsDirty()); }
683
684 void MainFrame::OnCloseUI(wxUpdateUIEvent& event) { event.Enable(wxcProjectMetadata::Get().IsLoaded()); }
685
686 void MainFrame::OnOpenUI(wxUpdateUIEvent& event) { event.Enable(true); }
687
688 void MainFrame::OnNewProject(wxCommandEvent& event)
689 {
690 static size_t Counter = 0;
691 wxString title;
692 title << "UntitledProject" << ++Counter << ".wxcp";
693
694 wxString wxcpFile = ::wxFileSelector(_("Create an empty wxCrafter project"), wxEmptyString, title, wxEmptyString,
695 "wxCrafter Project (*.wxcp)|*.wxcp", wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
696 if(wxcpFile.IsEmpty()) {
697 return;
698 }
699
700 wxFFile fp(wxcpFile, "w+b");
701 if(fp.IsOpened()) {
702 fp.Close();
703
704 } else {
705 wxString msg;
706 msg << _("Could not create resource file '") << wxcpFile << "'";
707 ::wxMessageBox(msg, _("wxCrafter"), wxOK | wxICON_WARNING | wxCENTRE);
708 return;
709 }
710
711 wxCommandEvent evtOpen(wxEVT_WXC_OPEN_PROJECT);
712 evtOpen.SetString(wxcpFile);
713 EventNotifier::Get()->ProcessEvent(evtOpen);
714 }
715
716 #define ID_RECENT_DOC_FIRST 2000
717
718 void MainFrame::OnOpen(wxCommandEvent& event) { DoOpenWxcpProject(); }
719
720 void MainFrame::OnOpenMenu(wxCommandEvent& event)
721 {
722 wxMenu menu;
723 wxArrayString history;
724 DoCreateRecentMenu(menu, history);
725
726 int selection = m_mainToolbar->GetMenuSelectionFromUser(event.GetId(), &menu);
727 if(selection == wxID_NONE) {
728 /// user cancelled
729 return;
730 }
731
732 size_t idx = selection - ID_RECENT_DOC_FIRST;
733 wxString file_name = history.Item(idx);
734 wxFileName fn(file_name);
735
736 wxCommandEvent evtClose(wxEVT_WXC_CLOSE_PROJECT);
737 EventNotifier::Get()->ProcessEvent(evtClose);
738
739 wxCommandEvent evtOpen(wxEVT_WXC_OPEN_PROJECT);
740 evtOpen.SetString(fn.GetFullPath());
741 EventNotifier::Get()->ProcessEvent(evtOpen);
742 }
743
744 void MainFrame::OnDeleteCustomControl(wxCommandEvent& event)
745 {
746 wxUnusedVar(event);
747 wxCommandEvent e(wxEVT_COMMAND_MENU_SELECTED, XRCID("delete_custom_controls"));
748 wxTheApp->AddPendingEvent(e);
749 }
750 void MainFrame::OnEditCustomControl(wxCommandEvent& event)
751 {
752 wxUnusedVar(event);
753 wxCommandEvent e(wxEVT_COMMAND_MENU_SELECTED, XRCID("edit_custom_controls"));
754 wxTheApp->AddPendingEvent(e);
755 }
756
757 void MainFrame::OnNewCustomControl(wxCommandEvent& event)
758 {
759 wxUnusedVar(event);
760 wxCommandEvent e(wxEVT_COMMAND_MENU_SELECTED, XRCID("define_custom_controls"));
761 wxTheApp->AddPendingEvent(e);
762 }
763
764 wxStyledTextCtrl* MainFrame::GetActiveSTC()
765 {
766 wxWindow* focusWin = wxWindow::FindFocus();
767 if(focusWin) {
768 return dynamic_cast<wxStyledTextCtrl*>(focusWin);
769 }
770 return NULL;
771 }
772
773 void MainFrame::OnGenerateCode(wxCommandEvent& event)
774 {
775 wxCommandEvent evtGenCode(wxEVT_WXC_CMD_GENERATE_CODE);
776 EventNotifier::Get()->ProcessEvent(evtGenCode);
777 }
778
779 void MainFrame::OnGenerateCodeUI(wxUpdateUIEvent& event) { event.Enable(wxcProjectMetadata::Get().IsLoaded()); }
780
781 void MainFrame::OnOpenFindDialog(wxCommandEvent& event)
782 {
783 #if STANDALONE_BUILD
784 if(m_findReplaceDialog) {
785 m_findReplaceDialog->Raise();
786 return;
787 }
788
789 wxStyledTextCtrl* stc = m_wxcView->GetPreviewEditor();
790 if(stc) {
791 m_findReplaceDialog = new wxFindReplaceDialog(stc, &m_findData, _("Find"), wxFR_NOUPDOWN);
792 m_findReplaceDialog->Show();
793 }
794 #endif
795 }
796
797 void MainFrame::OnCodeEditorSelected(wxCommandEvent& e)
798 {
799 e.Skip();
800 #if STANDALONE_BUILD
801 bool needToDisplayAgain = false;
802 // if the dialog was show, close it
803 if(m_findReplaceDialog) {
804 m_findReplaceDialog->Destroy();
805 m_findReplaceDialog = NULL;
806 needToDisplayAgain = true;
807 }
808
809 if(needToDisplayAgain && e.GetClientData()) {
810 // Reshow the dialog, this time for the proper parent
811 wxStyledTextCtrl* parent = reinterpret_cast<wxStyledTextCtrl*>(e.GetClientData());
812 if(parent) {
813 m_findReplaceDialog = new wxFindReplaceDialog(parent, &m_findData, _("Find"), wxFR_NOUPDOWN);
814 m_findReplaceDialog->Show();
815 }
816 }
817 #endif
818 }
819
820 void MainFrame::OnFindDlgClose(wxFindDialogEvent& event)
821 {
822 #if STANDALONE_BUILD
823 m_findReplaceDialog = NULL;
824 #endif
825 }
826
827 void MainFrame::OnFindFirst(wxFindDialogEvent& event)
828 {
829 #if STANDALONE_BUILD
830 wxStyledTextCtrl* stc = dynamic_cast<wxStyledTextCtrl*>(m_findReplaceDialog->GetParent());
831 if(stc) {
832 int curpos = stc->GetCurrentPos();
833 if(!DoFindText(stc, *(m_findReplaceDialog->GetData()), false)) {
834 // Try from start
835 stc->SetCurrentPos(0);
836 if(!DoFindText(stc, *(m_findReplaceDialog->GetData()), false)) {
837 // Could not find from the start as well, restore the caret position
838 stc->SetCurrentPos(curpos);
839 stc->EnsureCaretVisible();
840 ::wxMessageBox(wxString()
841 << "Could not find string: " << m_findReplaceDialog->GetData()->GetFindString(),
842 "wxCrafter", wxOK | wxICON_WARNING | wxOK_DEFAULT | wxCENTER, this);
843 m_findReplaceDialog->Raise();
844 }
845 }
846 }
847 #endif
848 }
849
850 void MainFrame::OnFindNext(wxFindDialogEvent& event)
851 {
852 #if STANDALONE_BUILD
853 wxStyledTextCtrl* stc = dynamic_cast<wxStyledTextCtrl*>(m_findReplaceDialog->GetParent());
854 if(stc) {
855 int curpos = stc->GetCurrentPos();
856 if(!DoFindText(stc, *(m_findReplaceDialog->GetData()), true)) {
857 // Try from start
858 stc->SetCurrentPos(0);
859 if(!DoFindText(stc, *(m_findReplaceDialog->GetData()), false)) {
860 // Could not find from the start as well, restore the caret position
861 stc->SetCurrentPos(curpos);
862 stc->EnsureCaretVisible();
863 }
864 }
865 }
866 #endif
867 }
868
869 bool MainFrame::DoFindText(wxStyledTextCtrl* stc, const wxFindReplaceData& frd, bool findNext)
870 {
871 int endPos = stc->GetLastPosition();
872 int startPos;
873
874 if(findNext) {
875 wxString selectedText = stc->GetSelectedText();
876 if(selectedText == frd.GetFindString()) {
877 startPos = stc->GetSelectionEnd();
878 } else {
879 startPos = stc->GetCurrentPos();
880 }
881
882 } else {
883 startPos = stc->GetCurrentPos();
884 }
885
886 int flags = frd.GetFlags();
887 int stcSearchFlags = 0;
888
889 if(flags & wxFR_MATCHCASE) {
890 stcSearchFlags |= wxSTC_FIND_MATCHCASE;
891 }
892
893 if(flags & wxFR_WHOLEWORD) {
894 stcSearchFlags |= wxSTC_FIND_WHOLEWORD;
895 }
896
897 int where = stc->FindText(startPos, endPos, frd.GetFindString(), stcSearchFlags);
898 if(where != wxNOT_FOUND) {
899 stc->SelectNone();
900 stc->SetSelection(where, where + frd.GetFindString().length());
901 stc->EnsureCaretVisible();
902 return true;
903
904 } else {
905 return false;
906 }
907 }
908
909 void MainFrame::OnFindUI(wxUpdateUIEvent& event)
910 {
911 wxStyledTextCtrl* stc = m_wxcView->GetPreviewEditor();
912 event.Enable(stc != NULL);
913 }
914
915 void MainFrame::EnsureVisibile() { DisplayDesigner(); }
916
917 void MainFrame::OnBatchGenerateCode(wxCommandEvent& event)
918 {
919 wxCommandEvent evtGenCode(wxEVT_WXC_CMD_BATCH_GENERATE_CODE);
920 EventNotifier::Get()->ProcessEvent(evtGenCode);
921 }
922
923 void MainFrame::OnBatchGenerateCodeUI(wxUpdateUIEvent& event)
924 {
925 #ifdef STANDALONE_BUILD
926 event.Enable(!wxcProjectMetadata::Get().IsLoaded());
927 #else
928 event.Enable(false);
929 #endif
930 }
931
932 void MainFrame::DoOpenWxcpProject()
933 {
934 wxString file_name = ::wxFileSelector(_("Open a wxCrafter file"), wxEmptyString, wxEmptyString, wxEmptyString,
935 "wxCrafter Project (*.wxcp)|*.wxcp");
936
937 if(!file_name.IsEmpty()) {
938 wxFileName fn(file_name);
939 wxCommandEvent evtOpen(wxEVT_WXC_OPEN_PROJECT);
940 evtOpen.SetString(fn.GetFullPath());
941 EventNotifier::Get()->ProcessEvent(evtOpen);
942 }
943 }
944
945 void MainFrame::OnFileOpen(wxCommandEvent& event) { DoOpenWxcpProject(); }
946
947 void MainFrame::DoCreateRecentMenu(wxMenu& menu, wxArrayString& history)
948 {
949 history = wxcSettings::Get().GetHistory();
950 if(history.IsEmpty()) {
951 return;
952 }
953
954 wxArrayString tmpHistory;
955 for(size_t i = 0; i < history.GetCount(); ++i) {
956 if(wxFileName(history.Item(i)).Exists()) {
957 tmpHistory.Add(history.Item(i));
958 }
959 }
960
961 history.swap(tmpHistory);
962 history.Sort();
963
964 // Update the history
965 wxcSettings::Get().SetHistory(history);
966 wxcSettings::Get().Save();
967
968 for(size_t i = 0; i < history.GetCount(); ++i) {
969 menu.Append(ID_RECENT_DOC_FIRST + i, history.Item(i));
970 }
971 }
972
973 void MainFrame::SetStatusMessage(const wxString& message)
974 {
975 if(GetStatusBar()) {
976 GetStatusBar()->SetStatusText(message, 0);
977 }
978 }