1 /*=========================================================================
3 Module: $RCSfile: bbtkWxGUIScriptingInterface.cxx,v $
5 Date: $Date: 2008/10/21 14:38:31 $
6 Version: $Revision: 1.31 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
14 * This software is governed by the CeCILL-B license under French law and
15 * abiding by the rules of distribution of free software. You can use,
16 * modify and/ or redistribute the software under the terms of the CeCILL-B
17 * license as circulated by CEA, CNRS and INRIA at the following URL
18 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 * or in the file LICENSE.txt.
21 * As a counterpart to the access to the source code and rights to copy,
22 * modify and redistribute granted by the license, users are provided only
23 * with a limited warranty and the software's author, the holder of the
24 * economic rights, and the successive licensors have only limited
27 * The fact that you are presently reading this means that you have had
28 * knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
33 * \brief Short description in one line
35 * Long description which
36 * can span multiple lines
48 #ifdef _USE_WXWIDGETS_
51 #include "bbtkWxGUIScriptingInterface.h"
52 #include "bbtkWxBlackBox.h"
53 #include "bbtkConfigurationFile.h"
54 #include "bbtkWxStreamRedirector.h"
55 #include "bbtkUtilities.h"
57 #include <wx/tipwin.h>
59 //#include "icons/cc_run.xpm"
69 ID_Menu_CreatePackage,
70 ID_Menu_CreateBlackBox,
71 ID_Menu_ShowImageGraph,
72 ID_Menu_RegeneratePackageDoc,
73 ID_Menu_RegenerateBoxesLists,
74 ID_Menu_RegenerateAll,
76 ID_Menu_Windows_Files,
78 ID_Menu_Windows_Messages,
79 ID_Menu_Windows_Command,
81 ID_Menu_Windows_PackageBrowser
86 //================================================================
87 WxGUIScriptingInterface::WxGUIScriptingInterface( wxWindow *parent )
88 : wxFrame((wxFrame *)parent, -1, _T("bbStudio"),
89 wxDefaultPosition, wxSize(1200,800) )
91 // m_mgr = new wxAuiManager(this);
92 m_mgr.SetManagedWindow(this);
94 mInterpreter = bbtk::Interpreter::New();
95 mInterpreter->SetUser(this);
96 mInterpreter->SetCommandLine(true);
97 mInterpreter->SetThrow(true);
101 wxInitAllImageHandlers();
103 wxMenu *menuFile = new wxMenu;
104 menuFile->Append( ID_Menu_EditConfig, _T("Open bbtk &Config file") );
105 menuFile->Append( ID_Menu_Quit, _T("&Quit") );
107 wxMenu *menuAbout = new wxMenu;
108 menuAbout->Append( ID_Menu_About, _T("&About...") );
110 wxMenu *menuTools = new wxMenu;
111 menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
112 menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &black box") );
113 menuTools->Append( ID_Menu_PlugPackage, _T("&Plug package") );
114 menuTools->AppendSeparator();
115 menuTools->Append( ID_Menu_RegeneratePackageDoc,_T("Regenerate package &doc") );
116 menuTools->Append( ID_Menu_RegenerateBoxesLists,_T("Regenerate boxes &lists") );
117 menuTools->Append( ID_Menu_RegenerateAll,_T("Regenerate &all") );
118 menuTools->AppendSeparator();
119 menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last graph") );
121 wxMenu *menuWindows = new wxMenu;
122 menuWindows->AppendCheckItem(ID_Menu_Windows_Files,
123 _T("Show 'files' panel") )->Check();
124 menuWindows->AppendCheckItem(ID_Menu_Windows_Help,
125 _T("Show 'help' panel") )->Check();
126 menuWindows->AppendCheckItem(ID_Menu_Windows_Messages,
127 _T("Show 'messages' panel") )->Check();
128 menuWindows->AppendCheckItem(ID_Menu_Windows_Command,
129 _T("Show 'command' panel") )->Check();
130 menuWindows->AppendSeparator();
131 menuWindows->Append( ID_Menu_Windows_PackageBrowser, _T("Start Package &Browser") );
133 // menuWindows->AppendSeparator();
134 // menuWindows->Append ( ID_Menu_Windows_Save, _T("Save interface configuration"));
136 wxMenu *menuOptions = new wxMenu;
137 mwxMenuItemReset = menuOptions->AppendCheckItem(-1,
138 _T("Reset before running") );
139 mwxMenuItemReset->Check();
141 wxMenuBar *menuBar = new wxMenuBar;
142 menuBar->Append( menuFile, _T("&File") );
143 menuBar->Append( menuTools, _T("&Tools") );
144 menuBar->Append( menuOptions, _T("&Options") );
145 menuBar->Append( menuWindows, _T("&Windows") );
146 menuBar->Append( menuAbout, _T("About") );
148 SetMenuBar( menuBar );
153 SetStatusText( _T("Welcome to bbStudio !") );
156 // Panes (Files, Messages, Help, Command)
157 mWxGUITextEditor = new WxGUITextEditor(this,this);
158 mWxGUITextEditor->SetFileNameFilter("*.bbs");
160 mWxGUIHtmlBrowser = new WxGUIHtmlBrowser(this,wxSize(200,0),this);
162 mWxGUIOutputMessages = new WxGUIOutputMessages(this);
164 mWxGUICommand = new WxGUICommand(this,this);
166 mWxGUICommand->SetFocus();
171 mwxNotebook = new wxAuiNotebook(this,
177 | wxAUI_NB_TAB_EXTERNAL_MOVE
178 //| wxAUI_NB_WINDOWLIST_BUTTON
179 //|wxAUI_NB_SCROLL_BUTTONS
180 // | wxAUI_NB_CLOSE_BUTTON
181 //| wxAUI_NB_CLOSE_ON_ACTIVE_TAB
182 //| wxAUI_NB_CLOSE_ON_ALL_TABS
186 mwxNotebook->AddPage(mWxGUITextEditor,_T("Files"),true);
187 mwxNotebook->AddPage(mWxGUIHtmlBrowser,_T("Help"),true);
189 mwxNotebook->AddPage(mWxGUIOutputMessages,_T("Messages"),true);
191 mwxNotebook->AddPage(mWxGUICommand,_T("Command"),true);
194 m_mgr.AddPane(mwxNotebook,
195 wxAuiPaneInfo().Name(wxT("nb"))
196 .CaptionVisible(false)
197 .MinimizeButton(false)
198 .MaximizeButton(false)
200 // .MinSize(wxSize(100,100))
205 m_mgr.AddPane(mWxGUITextEditor,
206 wxAuiPaneInfo().Name(wxT("editor_content"))
207 .Caption(wxT("Files"))
208 .MinimizeButton(true)
209 .MaximizeButton(true)
211 .MinSize(wxSize(100,100))
214 m_mgr.AddPane(mWxGUIHtmlBrowser,
215 wxAuiPaneInfo().Name(wxT("browser_content"))
216 .Caption(wxT("Help"))
217 .MinimizeButton(true)
218 .MaximizeButton(true)
221 .MinSize(wxSize(400,100))
224 m_mgr.AddPane(mWxGUIOutputMessages,
225 wxAuiPaneInfo().Name(wxT("messages_content"))
226 .Caption(wxT("Messages"))
227 .MinimizeButton(true)
228 .MaximizeButton(true)
230 .MinSize(wxSize(100,100))
232 m_mgr.AddPane(mWxGUICommand,
233 wxAuiPaneInfo().Name(wxT("command_content"))
234 .Caption(wxT("Command"))
235 .MinimizeButton(true)
236 .MaximizeButton(true)
239 .MinSize(wxSize(100,100))
243 // parent window of all bbtk windows will be a child of this
244 Wx::SetTopWindowParent(this);
245 // Add the method OnWxSignal as a Wx::Signal observer
246 //bbtkAddWxSignalObserver(WxGUIPackageBrowser2Window::OnWxSignal);
248 //.PaneBorder(false));
249 // Load the interface appearance saved on last closing
252 // Done in LoadPerspective
257 // mwxNotebook->SetSelection(1);
258 mWxGUIHtmlBrowser->GoHome();
261 // LoadPerspective();
264 wxTipWindow* tip = new wxTipWindow(this,
265 _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);
266 tip->CenterOnParent();
269 //================================================================
271 //================================================================
272 WxGUIScriptingInterface::~WxGUIScriptingInterface()
277 //================================================================
279 //================================================================
280 void WxGUIScriptingInterface::Open(const std::string& filename)
282 mWxGUITextEditor->Open(filename);
284 //================================================================
286 #define CATCH_MACRO \
287 catch (QuitException e) \
289 std::cout << "* quit exception caught"<<std::endl; \
291 catch (InterpreterError e) \
293 std::cerr << "* IERROR : "<<e.GetErrorMessage()<<std::endl; \
294 if (e.IsInScriptFile()) \
295 std::cerr << "* FILE : '"<<e.GetScriptFile()<<"'"<<std::endl; \
296 std::cerr << "* LINE : "<<e.GetScriptLine()<<std::endl; \
297 int lev = bbtk::MessageManager::GetMessageLevel("Error"); \
299 std::cerr << "* Exception thrown : "<<std::endl; \
300 std::cerr << "* OBJECT : " <<e.GetObject()<<std::endl; \
301 std::cerr << "* FILE : " <<e.GetSourceFile()<<std::endl; \
305 //================================================================
306 void WxGUIScriptingInterface::WxGUICommandEnter(const std::string& command)
310 mWxGUIOutputMessages->Print(s,wxRED);
314 mInterpreter->InterpretLine( command );
318 //================================================================
320 //================================================================
321 /// Runs the interpretation of a file
322 bool WxGUIScriptingInterface::InterpretFile( const std::string& filename)
326 mInterpreter->InterpretFile(filename);
331 //================================================================
333 //================================================================
334 void WxGUIScriptingInterface::WxGUITextEditorRun()
336 // wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
337 std::stringstream* buf = new std::stringstream;
338 (*buf) << mWxGUITextEditor->GetCurrentPage()->GetText();
341 // std::cout << "RUN"<<std::endl;
342 if (mwxMenuItemReset->IsChecked()) WxGUICommandEnter("reset");
343 mInterpreter->InterpretBuffer(buf);
344 // std::cout << "EO RUN"<<std::endl;
347 // std::cout << "EO RUN 3"<<std::endl;
349 //================================================================
351 //================================================================
352 void WxGUIScriptingInterface::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
354 if (!mWxGUITextEditor->CloseAllPages()) return;
357 //================================================================
360 //================================================================
361 void WxGUIScriptingInterface::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
365 wxMessageBox(_T(" bbStudio\nThe Black Box Toolkit Development Studio\n(c) CREATIS-LRMN 2008"),
366 _T("About ..."), wxOK | wxICON_INFORMATION,
369 //================================================================
372 //================================================================
373 void WxGUIScriptingInterface::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
375 std::string configFile =
376 ConfigurationFile::GetInstance().Get_config_xml_full_path();
379 //================================================================
381 //================================================================
382 void WxGUIScriptingInterface::OnMenuWindowsPackageBrowser(wxCommandEvent& WXUNUSED(event))
385 WxGUIPackageBrowser2Window *helpbrowser = new
386 WxGUIPackageBrowser2Window(this,_T("Package Browser"), wxSize(600,600) );
391 WxGUIPackageBrowser2 *browser = new WxGUIPackageBrowser2(this);
392 browser->IncludeAll();
395 // mwxNotebook->AddPage(browser,_T("Package Browser"),true);
398 m_mgr.AddPane(browser,
399 wxAuiPaneInfo().Name(wxT("package_brower"))
400 .Caption(wxT("Package browser"))
401 .MinimizeButton(true)
402 .MaximizeButton(true)
405 .MinSize(wxSize(100,100))
410 //================================================================
414 //================================================================
415 void WxGUIScriptingInterface::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
418 std::string command("toolsbbtk/appli/GUICreatePackage");
419 bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
421 Interpreter::Pointer I = Interpreter::New();
422 I->InterpretFile(command);
424 std::string command("reset");
425 mWxGUICommand->SendCommand(command);
426 command = "include toolsbbtk/appli/GUICreatePackage";
427 bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
428 mWxGUICommand->SendCommand(command);
431 //================================================================
434 //================================================================
435 void WxGUIScriptingInterface::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
438 std::string command("toolsbbtk/appli/GUICreateBlackBox");
439 bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
441 Interpreter::Pointer I = Interpreter::New();
442 I->InterpretFile(command);
444 std::string command("reset");
445 mWxGUICommand->SendCommand(command);
446 command = "include toolsbbtk/appli/GUICreateBlackBox";
447 bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
448 mWxGUICommand->SendCommand(command);
450 //================================================================
452 //================================================================
453 void WxGUIScriptingInterface::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
455 std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
458 std::string strappli="start ";
460 std::string strappli="gnome-open ";
462 std::string strcommand = strappli +default_temp_dir+"/temp_dir/workspace_workspacePrototype.png";
463 // std::cout << "system: " << strcommand << std::endl;
464 system ( strcommand.c_str() );
467 //================================================================
469 void WxGUIScriptingInterface::DoRegeneratePackageDoc( const std::string& pack )
475 command += ConfigurationFile::GetInstance().Get_bin_path();
476 command += ConfigurationFile::GetInstance().Get_file_separator();
477 command += "bbRegeneratePackageDoc";
481 command += " " + pack + " -q";
482 bbtkMessage("debug",1,"Executing system command '"<<command<<"'"<<std::endl);
485 if ( ! system ( command.c_str() ) )
487 wxMessageBox(_T("Done !"),_T("Regenerate package '")+std2wx(pack)+_T("' doc"),
488 wxOK | wxICON_INFORMATION);
492 wxString err(_T("An error occured while running '"));
493 err += bbtk::std2wx(command) + _T("'");
494 wxMessageBox(err,_T("Regenerate package doc"),wxOK | wxICON_ERROR);
499 //================================================================
500 void WxGUIScriptingInterface::OnMenuRegeneratePackageDoc(wxCommandEvent& WXUNUSED(event))
502 wxString name = wxGetTextFromUser(_T("Enter package name (-a for all)"),
503 _T("Regenerate package doc"),
505 if (name.IsEmpty()) return;
506 std::string pack = bbtk::wx2std(name);
508 DoRegeneratePackageDoc(pack);
510 //================================================================
513 void WxGUIScriptingInterface::DoRegenerateBoxesLists()
519 command += ConfigurationFile::GetInstance().Get_bin_path();
520 command += ConfigurationFile::GetInstance().Get_file_separator();
521 command += "bbRegenerateBoxesLists";
526 bbtkMessage("debug",1,"Executing system command '"<<command<<"'"<<std::endl);
528 if ( ! system ( command.c_str() ) )
530 wxMessageBox(_T("Done !"),_T("Regenerate boxes lists"),
531 wxOK | wxICON_INFORMATION);
535 wxString err(_T("An error occured while running '"));
536 err += bbtk::std2wx(command) + _T("'");
537 wxMessageBox(err,_T("Regenerate boxes lists"),wxOK | wxICON_ERROR);
541 //================================================================
542 void WxGUIScriptingInterface::OnMenuRegenerateBoxesLists(wxCommandEvent& WXUNUSED(event))
544 DoRegenerateBoxesLists();
546 //================================================================
548 //================================================================
549 void WxGUIScriptingInterface::OnMenuRegenerateAll(wxCommandEvent& WXUNUSED(event))
552 DoRegeneratePackageDoc("-a");
553 DoRegenerateBoxesLists();
555 //================================================================
558 //================================================================
559 void WxGUIScriptingInterface::OnMenuPlugPackage(wxCommandEvent& WXUNUSED(event))
561 long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
564 _T("Select package directory"),
568 if (FD->ShowModal()==wxID_OK)
570 std::string path = wx2std (FD->GetPath()) ;
571 std::string fname = path + "/bbtkPackage";
572 if ( ! Utilities::FileExists( fname ) )
574 wxString err(_T("The directory does not contain a 'bbtkPackage' file"));
575 wxMessageBox(err,_T("Plug package"),wxOK | wxICON_ERROR);
580 f.open(fname.c_str());
586 bbtk::ConfigurationFile::GetInstance().AddPackagePathsAndWrite( path );
588 DoRegeneratePackageDoc(pname);
589 DoRegenerateBoxesLists();
593 //================================================================
595 //================================================================
596 void WxGUIScriptingInterface::InterpreterUserViewHtmlPage(const std::string& page)
599 // std::cout << "WxGUIScriptingInterface::ShowHtmlPage('"<<page<<"')"<<std::endl;
600 if (mWxGUIHtmlBrowser->GoTo(s))
602 //EED mwxNotebook->ChangeSelection(1);
603 // mwxNotebook->SetSelection(1);
607 // std::cout << "ERROR html"<<std::endl;
610 //================================================================
612 //================================================================
613 bool WxGUIScriptingInterface::WxGUIHtmlBrowserUserOnLinkClicked(const std::string& target)
615 size_t s = target.length();
616 if ((s>3) && (target[s-1]=='s')
617 && (target[s-2]=='b')
618 && (target[s-3]=='b')
619 && (target[s-4]=='.'))
621 mWxGUITextEditor->Open(target);
623 mwxNotebook->SetSelection(0);
625 mWxGUITextEditor->SetFocus();
630 //================================================================
632 void WxGUIScriptingInterface::OnMenuWindowsCheck( wxCommandEvent& event,
635 bool checked = event.IsChecked();
636 bool shown = m_mgr.GetPane(w).IsShown();
637 // std::cout << "checked = "<<checked<<" - shown = "<<shown<<std::endl;
640 m_mgr.GetPane(w).Show(checked);
646 //================================================================
647 void WxGUIScriptingInterface::OnMenuWindowsFiles(wxCommandEvent& event)
649 OnMenuWindowsCheck(event,mWxGUITextEditor);
651 //================================================================
653 //================================================================
654 void WxGUIScriptingInterface::OnMenuWindowsHelp(wxCommandEvent& event)
656 OnMenuWindowsCheck(event,mWxGUIHtmlBrowser);
658 //================================================================
660 //================================================================
661 void WxGUIScriptingInterface::OnMenuWindowsMessages(wxCommandEvent& event)
663 OnMenuWindowsCheck(event,mWxGUIOutputMessages);
665 //================================================================
667 //================================================================
668 void WxGUIScriptingInterface::OnMenuWindowsCommand(wxCommandEvent& event)
670 OnMenuWindowsCheck(event,mWxGUICommand);
672 //================================================================
674 //================================================================
675 void WxGUIScriptingInterface::OnMenuWindowsSave( wxCommandEvent& WXUNUSED(event))
679 //================================================================
681 //================================================================
682 void WxGUIScriptingInterface::SavePerspective()
684 // std::cout << "Saving configuration..."<<std::endl;
685 std::string conf = wx2std(m_mgr.SavePerspective());
686 std::string fname = Utilities::MakeUserSettingsFullFileName("bbStudio.aui");
688 f.open(fname.c_str(), std::ios::out );
689 f << conf << std::endl;
692 // GetPosition(&x,&y);
693 // f << x << " " << y << std::endl;
699 f << w << " " << h << std::endl;
703 //================================================================
705 //================================================================
706 void WxGUIScriptingInterface::LoadPerspective()
709 Utilities::MakeUserSettingsFullFileName("bbStudio.aui");
710 if ( Utilities::FileExists( fname ))
712 // std::cout << "Loading configuration..."<<std::endl;
715 f.open(fname.c_str());
727 // std::cout << conf << std::endl;
731 m_mgr.LoadPerspective(std2wx(conf));
738 //================================================================
740 //================================================================
741 void WxGUIScriptingInterface::OnPaneClose(wxAuiManagerEvent& evt)
743 std::cout << "Closing panel '"<<evt.pane->name<<"'"<<std::endl;
745 // OnMenuWindowsCheck( wxCommandEvent& event, wxWindow* w);
747 //================================================================
750 //================================================================
751 BEGIN_EVENT_TABLE(WxGUIScriptingInterface, wxFrame)
752 EVT_MENU(ID_Menu_Quit, WxGUIScriptingInterface::OnMenuQuit)
753 EVT_MENU(ID_Menu_About, WxGUIScriptingInterface::OnMenuAbout)
754 EVT_MENU(ID_Menu_EditConfig, WxGUIScriptingInterface::OnMenuEditConfig)
755 EVT_MENU(ID_Menu_CreatePackage, WxGUIScriptingInterface::OnMenuCreatePackage)
756 EVT_MENU(ID_Menu_CreateBlackBox, WxGUIScriptingInterface::OnMenuCreateBlackBox)
757 EVT_MENU(ID_Menu_ShowImageGraph, WxGUIScriptingInterface::OnMenuShowImageGraph)
758 EVT_MENU(ID_Menu_RegeneratePackageDoc, WxGUIScriptingInterface::OnMenuRegeneratePackageDoc)
759 EVT_MENU(ID_Menu_RegenerateBoxesLists, WxGUIScriptingInterface::OnMenuRegenerateBoxesLists)
760 EVT_MENU(ID_Menu_RegenerateAll, WxGUIScriptingInterface::OnMenuRegenerateAll)
761 EVT_MENU(ID_Menu_PlugPackage, WxGUIScriptingInterface::OnMenuPlugPackage)
762 EVT_MENU(ID_Menu_Windows_Files, WxGUIScriptingInterface::OnMenuWindowsFiles)
763 EVT_MENU(ID_Menu_Windows_Help, WxGUIScriptingInterface::OnMenuWindowsHelp)
764 EVT_MENU(ID_Menu_Windows_Messages, WxGUIScriptingInterface::OnMenuWindowsMessages)
765 EVT_MENU(ID_Menu_Windows_Command, WxGUIScriptingInterface::OnMenuWindowsCommand)
766 EVT_MENU(ID_Menu_Windows_Save, WxGUIScriptingInterface::OnMenuWindowsSave)
767 EVT_MENU(ID_Menu_Windows_PackageBrowser, WxGUIScriptingInterface::OnMenuWindowsPackageBrowser)
768 EVT_AUI_PANE_CLOSE(WxGUIScriptingInterface::OnPaneClose)
769 // EVT_BUTTON(ID_Button_Run, WxGUIScriptingInterface::OnButtonRun )
771 //================================================================
776 #endif //_USE_WXWIDGETS_