1 /*=========================================================================
4 Module: $RCSfile: bbtkWxGUIScriptingInterface.cxx,v $
6 Date: $Date: 2008/10/15 15:09:45 $
7 Version: $Revision: 1.27 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*//**
18 * \brief Short description in one line
20 * Long description which
21 * can span multiple lines
33 #ifdef _USE_WXWIDGETS_
36 #include "bbtkWxGUIScriptingInterface.h"
37 #include "bbtkWxBlackBox.h"
38 #include "bbtkConfigurationFile.h"
39 #include "bbtkWxStreamRedirector.h"
40 #include "bbtkUtilities.h"
42 #include <wx/tipwin.h>
44 //#include "icons/cc_run.xpm"
54 ID_Menu_CreatePackage,
55 ID_Menu_CreateBlackBox,
56 ID_Menu_ShowImageGraph,
57 ID_Menu_RegeneratePackageDoc,
58 ID_Menu_RegenerateBoxesLists,
59 ID_Menu_RegenerateAll,
61 ID_Menu_Windows_Files,
63 ID_Menu_Windows_Messages,
64 ID_Menu_Windows_Command,
66 ID_Menu_Windows_PackageBrowser
71 //================================================================
72 WxGUIScriptingInterface::WxGUIScriptingInterface( wxWindow *parent )
73 : wxFrame((wxFrame *)parent, -1, _T("bbStudio"),
74 wxDefaultPosition, wxSize(1200,800) )
76 // m_mgr = new wxAuiManager(this);
77 m_mgr.SetManagedWindow(this);
79 mInterpreter = bbtk::Interpreter::New();
80 mInterpreter->SetUser(this);
81 mInterpreter->SetCommandLine(true);
82 mInterpreter->SetThrow(true);
86 wxInitAllImageHandlers();
88 wxMenu *menuFile = new wxMenu;
89 menuFile->Append( ID_Menu_EditConfig, _T("Open bbtk &Config file") );
90 menuFile->Append( ID_Menu_Quit, _T("&Quit") );
92 wxMenu *menuAbout = new wxMenu;
93 menuAbout->Append( ID_Menu_About, _T("&About...") );
95 wxMenu *menuTools = new wxMenu;
96 menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
97 menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &black box") );
98 menuTools->Append( ID_Menu_PlugPackage, _T("&Plug package") );
99 menuTools->AppendSeparator();
100 menuTools->Append( ID_Menu_RegeneratePackageDoc,_T("Regenerate package &doc") );
101 menuTools->Append( ID_Menu_RegenerateBoxesLists,_T("Regenerate boxes &lists") );
102 menuTools->Append( ID_Menu_RegenerateAll,_T("Regenerate &all") );
103 menuTools->AppendSeparator();
104 menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
106 wxMenu *menuWindows = new wxMenu;
107 menuWindows->AppendCheckItem(ID_Menu_Windows_Files,
108 _T("Show 'files' panel") )->Check();
109 menuWindows->AppendCheckItem(ID_Menu_Windows_Help,
110 _T("Show 'help' panel") )->Check();
111 menuWindows->AppendCheckItem(ID_Menu_Windows_Messages,
112 _T("Show 'messages' panel") )->Check();
113 menuWindows->AppendCheckItem(ID_Menu_Windows_Command,
114 _T("Show 'command' panel") )->Check();
115 menuWindows->AppendSeparator();
116 menuWindows->Append( ID_Menu_Windows_PackageBrowser, _T("Launch Package &Browser") );
118 // menuWindows->AppendSeparator();
119 // menuWindows->Append ( ID_Menu_Windows_Save, _T("Save interface configuration"));
121 wxMenu *menuOptions = new wxMenu;
122 mwxMenuItemReset = menuOptions->AppendCheckItem(-1,
123 _T("Reset before running") );
124 mwxMenuItemReset->Check();
126 wxMenuBar *menuBar = new wxMenuBar;
127 menuBar->Append( menuFile, _T("&File") );
128 menuBar->Append( menuTools, _T("&Tools") );
129 menuBar->Append( menuOptions, _T("&Options") );
130 menuBar->Append( menuWindows, _T("&Windows") );
131 menuBar->Append( menuAbout, _T("About") );
133 SetMenuBar( menuBar );
138 SetStatusText( _T("Welcome to bbStudio !") );
141 // Panes (Files, Messages, Help, Command)
142 mWxGUITextEditor = new WxGUITextEditor(this,this);
143 mWxGUITextEditor->SetFileNameFilter("*.bbs");
145 mWxGUIHtmlBrowser = new WxGUIHtmlBrowser(this,wxSize(200,0),this);
147 mWxGUIOutputMessages = new WxGUIOutputMessages(this);
149 mWxGUICommand = new WxGUICommand(this,this);
151 mWxGUICommand->SetFocus();
156 mwxNotebook = new wxAuiNotebook(this,
162 | wxAUI_NB_TAB_EXTERNAL_MOVE
163 //| wxAUI_NB_WINDOWLIST_BUTTON
164 //|wxAUI_NB_SCROLL_BUTTONS
165 // | wxAUI_NB_CLOSE_BUTTON
166 //| wxAUI_NB_CLOSE_ON_ACTIVE_TAB
167 //| wxAUI_NB_CLOSE_ON_ALL_TABS
171 mwxNotebook->AddPage(mWxGUITextEditor,_T("Files"),true);
172 mwxNotebook->AddPage(mWxGUIHtmlBrowser,_T("Help"),true);
174 mwxNotebook->AddPage(mWxGUIOutputMessages,_T("Messages"),true);
176 mwxNotebook->AddPage(mWxGUICommand,_T("Command"),true);
179 m_mgr.AddPane(mwxNotebook,
180 wxAuiPaneInfo().Name(wxT("nb"))
181 .CaptionVisible(false)
182 .MinimizeButton(false)
183 .MaximizeButton(false)
185 // .MinSize(wxSize(100,100))
190 m_mgr.AddPane(mWxGUITextEditor,
191 wxAuiPaneInfo().Name(wxT("editor_content"))
192 .Caption(wxT("Files"))
193 .MinimizeButton(true)
194 .MaximizeButton(true)
196 .MinSize(wxSize(100,100))
199 m_mgr.AddPane(mWxGUIHtmlBrowser,
200 wxAuiPaneInfo().Name(wxT("browser_content"))
201 .Caption(wxT("Help"))
202 .MinimizeButton(true)
203 .MaximizeButton(true)
206 .MinSize(wxSize(400,100))
209 m_mgr.AddPane(mWxGUIOutputMessages,
210 wxAuiPaneInfo().Name(wxT("messages_content"))
211 .Caption(wxT("Messages"))
212 .MinimizeButton(true)
213 .MaximizeButton(true)
215 .MinSize(wxSize(100,100))
217 m_mgr.AddPane(mWxGUICommand,
218 wxAuiPaneInfo().Name(wxT("command_content"))
219 .Caption(wxT("Command"))
220 .MinimizeButton(true)
221 .MaximizeButton(true)
224 .MinSize(wxSize(100,100))
228 // parent window of all bbtk windows will be a child of this
229 Wx::SetTopWindowParent(this);
230 // Add the method OnWxSignal as a Wx::Signal observer
231 //bbtkAddWxSignalObserver(WxGUIPackageBrowser2Window::OnWxSignal);
233 //.PaneBorder(false));
234 // Load the interface appearance saved on last closing
237 // Done in LoadPerspective
242 // mwxNotebook->SetSelection(1);
243 mWxGUIHtmlBrowser->GoHome();
246 // LoadPerspective();
249 wxTipWindow* tip = new wxTipWindow(this,
250 _T("\n Welcome to bbStudio !\n\n To run a demo or example:\n 1. click on the 'Demos' or 'Examples' link\n 2. select a demo or example\n 3. click on the '[source]' link : the source file is loaded in bbStudio\n 4. click on the 'Run' button (the arrow at the bottom right of the source file) \n"),1000);
251 tip->CenterOnParent();
254 //================================================================
256 //================================================================
257 WxGUIScriptingInterface::~WxGUIScriptingInterface()
262 //================================================================
264 //================================================================
265 void WxGUIScriptingInterface::Open(const std::string& filename)
267 mWxGUITextEditor->Open(filename);
269 //================================================================
271 #define CATCH_MACRO \
272 catch (QuitException e) \
274 std::cout << "* quit exception caught"<<std::endl; \
276 catch (InterpreterError e) \
278 std::cerr << "* IERROR : "<<e.GetErrorMessage()<<std::endl; \
279 if (e.IsInScriptFile()) \
280 std::cerr << "* FILE : '"<<e.GetScriptFile()<<"'"<<std::endl; \
281 std::cerr << "* LINE : "<<e.GetScriptLine()<<std::endl; \
282 int lev = bbtk::MessageManager::GetMessageLevel("Error"); \
284 std::cerr << "* Exception thrown : "<<std::endl; \
285 std::cerr << "* OBJECT : " <<e.GetObject()<<std::endl; \
286 std::cerr << "* FILE : " <<e.GetSourceFile()<<std::endl; \
290 //================================================================
291 void WxGUIScriptingInterface::WxGUICommandEnter(const std::string& command)
295 mWxGUIOutputMessages->Print(s,wxRED);
299 mInterpreter->InterpretLine( command );
303 //================================================================
305 //================================================================
306 /// Runs the interpretation of a file
307 bool WxGUIScriptingInterface::InterpretFile( const std::string& filename)
311 mInterpreter->InterpretFile(filename);
316 //================================================================
318 //================================================================
319 void WxGUIScriptingInterface::WxGUITextEditorRun()
321 // wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
322 std::stringstream* buf = new std::stringstream;
323 (*buf) << mWxGUITextEditor->GetCurrentPage()->GetText();
326 // std::cout << "RUN"<<std::endl;
327 if (mwxMenuItemReset->IsChecked()) WxGUICommandEnter("reset");
328 mInterpreter->InterpretBuffer(buf);
329 // std::cout << "EO RUN"<<std::endl;
332 // std::cout << "EO RUN 3"<<std::endl;
334 //================================================================
336 //================================================================
337 void WxGUIScriptingInterface::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
339 if (!mWxGUITextEditor->CloseAllPages()) return;
342 //================================================================
345 //================================================================
346 void WxGUIScriptingInterface::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
350 wxMessageBox(_T(" bbStudio\nThe Black Box Toolkit Development Studio\n(c) CREATIS-LRMN 2008"),
351 _T("About ..."), wxOK | wxICON_INFORMATION,
354 //================================================================
357 //================================================================
358 void WxGUIScriptingInterface::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
360 std::string configFile =
361 ConfigurationFile::GetInstance().Get_config_xml_full_path();
364 //================================================================
366 //================================================================
367 void WxGUIScriptingInterface::OnMenuWindowsPackageBrowser(wxCommandEvent& WXUNUSED(event))
370 WxGUIPackageBrowser2Window *helpbrowser = new
371 WxGUIPackageBrowser2Window(this,_T("Package Browser"), wxSize(600,600) );
376 WxGUIPackageBrowser2 *browser = new WxGUIPackageBrowser2(this);
377 browser->IncludeAll();
380 // mwxNotebook->AddPage(browser,_T("Package Browser"),true);
383 m_mgr.AddPane(browser,
384 wxAuiPaneInfo().Name(wxT("package_brower"))
385 .Caption(wxT("Package browser"))
386 .MinimizeButton(true)
387 .MaximizeButton(true)
390 .MinSize(wxSize(100,100))
395 //================================================================
399 //================================================================
400 void WxGUIScriptingInterface::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
403 std::string command("toolsbbtk/appli/GUICreatePackage");
404 bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
406 Interpreter::Pointer I = Interpreter::New();
407 I->InterpretFile(command);
410 //================================================================
413 //================================================================
414 void WxGUIScriptingInterface::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
416 std::string command("toolsbbtk/appli/GUICreateBlackBox");
417 bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
419 Interpreter::Pointer I = Interpreter::New();
420 I->InterpretFile(command);
422 //================================================================
424 //================================================================
425 void WxGUIScriptingInterface::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
427 std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
430 std::string strappli="start ";
432 std::string strappli="gnome-open ";
434 std::string strcommand = strappli +default_temp_dir+"/temp_dir/workspace_workspacePrototype.png";
435 // std::cout << "system: " << strcommand << std::endl;
436 system ( strcommand.c_str() );
439 //================================================================
441 void WxGUIScriptingInterface::DoRegeneratePackageDoc( const std::string& pack )
447 command += ConfigurationFile::GetInstance().Get_bin_path();
448 command += ConfigurationFile::GetInstance().Get_file_separator();
449 command += "bbRegeneratePackageDoc";
453 command += " " + pack + " -q";
454 bbtkMessage("debug",1,"Executing system command '"<<command<<"'"<<std::endl);
457 if ( ! system ( command.c_str() ) )
459 wxMessageBox(_T("Done !"),_T("Regenerate package '")+pack+_T("' doc"),
460 wxOK | wxICON_INFORMATION);
464 wxString err(_T("An error occured while running '"));
465 err += bbtk::std2wx(command) + _T("'");
466 wxMessageBox(err,_T("Regenerate package doc"),wxOK | wxICON_ERROR);
471 //================================================================
472 void WxGUIScriptingInterface::OnMenuRegeneratePackageDoc(wxCommandEvent& WXUNUSED(event))
474 wxString name = wxGetTextFromUser(_T("Enter package name (-a for all)"),
475 _T("Regenerate package doc"),
477 if (name.IsEmpty()) return;
478 std::string pack = bbtk::wx2std(name);
480 DoRegeneratePackageDoc(pack);
482 //================================================================
485 void WxGUIScriptingInterface::DoRegenerateBoxesLists()
491 command += ConfigurationFile::GetInstance().Get_bin_path();
492 command += ConfigurationFile::GetInstance().Get_file_separator();
493 command += "bbRegenerateBoxesLists";
498 bbtkMessage("debug",1,"Executing system command '"<<command<<"'"<<std::endl);
500 if ( ! system ( command.c_str() ) )
502 wxMessageBox(_T("Done !"),_T("Regenerate boxes lists"),
503 wxOK | wxICON_INFORMATION);
507 wxString err(_T("An error occured while running '"));
508 err += bbtk::std2wx(command) + _T("'");
509 wxMessageBox(err,_T("Regenerate boxes lists"),wxOK | wxICON_ERROR);
513 //================================================================
514 void WxGUIScriptingInterface::OnMenuRegenerateBoxesLists(wxCommandEvent& WXUNUSED(event))
516 DoRegenerateBoxesLists();
518 //================================================================
520 //================================================================
521 void WxGUIScriptingInterface::OnMenuRegenerateAll(wxCommandEvent& WXUNUSED(event))
524 DoRegeneratePackageDoc("-a");
525 DoRegenerateBoxesLists();
527 //================================================================
530 //================================================================
531 void WxGUIScriptingInterface::OnMenuPlugPackage(wxCommandEvent& WXUNUSED(event))
533 long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
536 _T("Select package directory"),
540 if (FD->ShowModal()==wxID_OK)
542 std::string path = wx2std (FD->GetPath()) ;
543 std::string fname = path + "/bbtkPackage";
544 if ( ! Utilities::FileExists( fname ) )
546 wxString err(_T("The directory does not contain a 'bbtkPackage' file"));
547 wxMessageBox(err,_T("Plug package"),wxOK | wxICON_ERROR);
552 f.open(fname.c_str());
558 bbtk::ConfigurationFile::GetInstance().AddPackagePathsAndWrite( path );
560 DoRegeneratePackageDoc(pname);
561 DoRegenerateBoxesLists();
565 //================================================================
567 //================================================================
568 void WxGUIScriptingInterface::InterpreterUserViewHtmlPage(const std::string& page)
571 // std::cout << "WxGUIScriptingInterface::ShowHtmlPage('"<<page<<"')"<<std::endl;
572 if (mWxGUIHtmlBrowser->GoTo(s))
574 //EED mwxNotebook->ChangeSelection(1);
575 // mwxNotebook->SetSelection(1);
579 // std::cout << "ERROR html"<<std::endl;
582 //================================================================
584 //================================================================
585 bool WxGUIScriptingInterface::WxGUIHtmlBrowserUserOnLinkClicked(const std::string& target)
587 size_t s = target.length();
588 if ((s>3) && (target[s-1]=='s')
589 && (target[s-2]=='b')
590 && (target[s-3]=='b')
591 && (target[s-4]=='.'))
593 mWxGUITextEditor->Open(target);
595 mwxNotebook->SetSelection(0);
597 mWxGUITextEditor->SetFocus();
602 //================================================================
604 void WxGUIScriptingInterface::OnMenuWindowsCheck( wxCommandEvent& event,
607 bool checked = event.IsChecked();
608 bool shown = m_mgr.GetPane(w).IsShown();
609 // std::cout << "checked = "<<checked<<" - shown = "<<shown<<std::endl;
612 m_mgr.GetPane(w).Show(checked);
618 //================================================================
619 void WxGUIScriptingInterface::OnMenuWindowsFiles(wxCommandEvent& event)
621 OnMenuWindowsCheck(event,mWxGUITextEditor);
623 //================================================================
625 //================================================================
626 void WxGUIScriptingInterface::OnMenuWindowsHelp(wxCommandEvent& event)
628 OnMenuWindowsCheck(event,mWxGUIHtmlBrowser);
630 //================================================================
632 //================================================================
633 void WxGUIScriptingInterface::OnMenuWindowsMessages(wxCommandEvent& event)
635 OnMenuWindowsCheck(event,mWxGUIOutputMessages);
637 //================================================================
639 //================================================================
640 void WxGUIScriptingInterface::OnMenuWindowsCommand(wxCommandEvent& event)
642 OnMenuWindowsCheck(event,mWxGUICommand);
644 //================================================================
646 //================================================================
647 void WxGUIScriptingInterface::OnMenuWindowsSave( wxCommandEvent& WXUNUSED(event))
651 //================================================================
653 //================================================================
654 void WxGUIScriptingInterface::SavePerspective()
656 // std::cout << "Saving configuration..."<<std::endl;
657 std::string conf = wx2std(m_mgr.SavePerspective());
658 std::string fname = Utilities::MakeUserSettingsFullFileName("bbStudio.aui");
660 f.open(fname.c_str(), std::ios::out );
661 f << conf << std::endl;
664 // GetPosition(&x,&y);
665 // f << x << " " << y << std::endl;
671 f << w << " " << h << std::endl;
675 //================================================================
677 //================================================================
678 void WxGUIScriptingInterface::LoadPerspective()
681 Utilities::MakeUserSettingsFullFileName("bbStudio.aui");
682 if ( Utilities::FileExists( fname ))
684 // std::cout << "Loading configuration..."<<std::endl;
687 f.open(fname.c_str());
699 // std::cout << conf << std::endl;
703 m_mgr.LoadPerspective(std2wx(conf));
710 //================================================================
712 //================================================================
713 void WxGUIScriptingInterface::OnPaneClose(wxAuiManagerEvent& evt)
715 std::cout << "Closing panel '"<<evt.pane->name<<"'"<<std::endl;
717 // OnMenuWindowsCheck( wxCommandEvent& event, wxWindow* w);
719 //================================================================
722 //================================================================
723 BEGIN_EVENT_TABLE(WxGUIScriptingInterface, wxFrame)
724 EVT_MENU(ID_Menu_Quit, WxGUIScriptingInterface::OnMenuQuit)
725 EVT_MENU(ID_Menu_About, WxGUIScriptingInterface::OnMenuAbout)
726 EVT_MENU(ID_Menu_EditConfig, WxGUIScriptingInterface::OnMenuEditConfig)
727 EVT_MENU(ID_Menu_CreatePackage, WxGUIScriptingInterface::OnMenuCreatePackage)
728 EVT_MENU(ID_Menu_CreateBlackBox, WxGUIScriptingInterface::OnMenuCreateBlackBox)
729 EVT_MENU(ID_Menu_ShowImageGraph, WxGUIScriptingInterface::OnMenuShowImageGraph)
730 EVT_MENU(ID_Menu_RegeneratePackageDoc, WxGUIScriptingInterface::OnMenuRegeneratePackageDoc)
731 EVT_MENU(ID_Menu_RegenerateBoxesLists, WxGUIScriptingInterface::OnMenuRegenerateBoxesLists)
732 EVT_MENU(ID_Menu_RegenerateAll, WxGUIScriptingInterface::OnMenuRegenerateAll)
733 EVT_MENU(ID_Menu_PlugPackage, WxGUIScriptingInterface::OnMenuPlugPackage)
734 EVT_MENU(ID_Menu_Windows_Files, WxGUIScriptingInterface::OnMenuWindowsFiles)
735 EVT_MENU(ID_Menu_Windows_Help, WxGUIScriptingInterface::OnMenuWindowsHelp)
736 EVT_MENU(ID_Menu_Windows_Messages, WxGUIScriptingInterface::OnMenuWindowsMessages)
737 EVT_MENU(ID_Menu_Windows_Command, WxGUIScriptingInterface::OnMenuWindowsCommand)
738 EVT_MENU(ID_Menu_Windows_Save, WxGUIScriptingInterface::OnMenuWindowsSave)
739 EVT_MENU(ID_Menu_Windows_PackageBrowser, WxGUIScriptingInterface::OnMenuWindowsPackageBrowser)
740 EVT_AUI_PANE_CLOSE(WxGUIScriptingInterface::OnPaneClose)
741 // EVT_BUTTON(ID_Button_Run, WxGUIScriptingInterface::OnButtonRun )
743 //================================================================
748 #endif //_USE_WXWIDGETS_