1 /*=========================================================================
4 Module: $RCSfile: bbtkWxGUIScriptingInterface.cxx,v $
6 Date: $Date: 2008/04/18 12:59:16 $
7 Version: $Revision: 1.5 $
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"
42 #include "icons/cc_run.xpm"
52 ID_Menu_CreatePackage,
53 ID_Menu_CreateBlackBox,
54 ID_Menu_ShowImageGraph,
56 ID_Menu_Windows_Files,
58 ID_Menu_Windows_Messages,
59 ID_Menu_Windows_Command,
65 //================================================================
66 WxGUIScriptingInterface::WxGUIScriptingInterface( wxWindow *parent, wxString title, wxSize size)
67 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
69 // m_mgr = new wxAuiManager(this);
70 m_mgr.SetManagedWindow(this);
72 mInterpreter = bbtk::Interpreter::New();
73 mInterpreter->SetUser(this);
74 mInterpreter->SetCommandLine(true);
75 mInterpreter->SetThrow(true);
78 wxInitAllImageHandlers();
80 wxMenu *menuFile = new wxMenu;
81 menuFile->Append( ID_Menu_Quit, _T("&Quit") );
83 wxMenu *menuAbout = new wxMenu;
84 menuAbout->Append( ID_Menu_About, _T("&About...") );
86 wxMenu *menuTools = new wxMenu;
87 menuTools->Append( ID_Menu_EditConfig, _T("&Edit bbtk config") );
88 menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
89 menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &blackbox") );
90 menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
91 menuTools->Append( ID_Menu_CreateIndex, _T("&Generate index") );
93 wxMenu *menuWindows = new wxMenu;
94 menuWindows->AppendCheckItem(ID_Menu_Windows_Files,
95 _T("Show 'files' panel") )->Check();
96 menuWindows->AppendCheckItem(ID_Menu_Windows_Help,
97 _T("Show 'help' panel") )->Check();
98 menuWindows->AppendCheckItem(ID_Menu_Windows_Messages,
99 _T("Show 'messages' panel") )->Check();
100 menuWindows->AppendCheckItem(ID_Menu_Windows_Command,
101 _T("Show 'command' panel") )->Check();
103 menuWindows->AppendSeparator();
104 menuWindows->Append ( ID_Menu_Windows_Save, _T("Save interface configuration"));
106 wxMenu *menuOptions = new wxMenu;
107 mwxMenuItemReset = menuOptions->AppendCheckItem(-1,
108 _T("Reset before running") );
109 mwxMenuItemReset->Check();
111 wxMenuBar *menuBar = new wxMenuBar;
112 menuBar->Append( menuFile, _T("&File") );
113 menuBar->Append( menuTools, _T("&Tools") );
114 menuBar->Append( menuOptions, _T("&Options") );
115 menuBar->Append( menuWindows, _T("&Windows") );
116 menuBar->Append( menuAbout, _T("About") );
118 SetMenuBar( menuBar );
121 SetStatusText( _T("Welcome to bbi !") );
124 mWxGUITextEditor = new WxGUITextEditor(this,this);
125 mWxGUITextEditor->SetFileNameFilter("*.bbs");
127 mWxGUIHtmlBrowser = new WxGUIHtmlBrowser(this,wxSize(200,0),this);
130 wxBitmap bmp_run(cc_run_xpm);
131 mwxButtonRun = new wxBitmapButton( this, ID_Button_Run,bmp_run);//_T("Run") );
134 mWxGUIOutputMessages = new WxGUIOutputMessages(this);
136 mWxGUICommand = new WxGUICommand(this,this);
138 mWxGUICommand->SetFocus();
140 // Creates and sets the parent window of all bbtk windows
141 wxWindow* top = new wxPanel(this,-1);//,_T("top"));
143 Wx::SetTopWindow(top);
147 m_mgr.AddPane(mWxGUITextEditor,
148 wxAuiPaneInfo().Name(wxT("editor_content"))
149 .Caption(wxT("Files"))
150 .MinimizeButton(true)
151 .MaximizeButton(true)
153 .MinSize(wxSize(100,100))
155 m_mgr.AddPane(mWxGUIHtmlBrowser,
156 wxAuiPaneInfo().Name(wxT("browser_content"))
157 .Caption(wxT("Help"))
158 .MinimizeButton(true)
159 .MaximizeButton(true)
161 .MinSize(wxSize(100,100))
163 m_mgr.AddPane(mWxGUIOutputMessages,
164 wxAuiPaneInfo().Name(wxT("messages_content"))
165 .Caption(wxT("Messages"))
166 .MinimizeButton(true)
167 .MaximizeButton(true)
169 .MinSize(wxSize(100,100))
171 m_mgr.AddPane(mWxGUICommand,
172 wxAuiPaneInfo().Name(wxT("command_content"))
173 .Caption(wxT("Command"))
174 .MinimizeButton(true)
175 .MaximizeButton(true)
178 .MinSize(wxSize(100,100))
180 // m_mgr.AddPane(mwxButtonRun,
181 // wxAuiPaneInfo().Name(wxT("button_run_content")));
184 //.PaneBorder(false));
192 //================================================================
194 //================================================================
195 WxGUIScriptingInterface::~WxGUIScriptingInterface()
199 //================================================================
201 //================================================================
202 void WxGUIScriptingInterface::Open(const std::string& filename)
204 mWxGUITextEditor->Open(filename);
206 //================================================================
208 #define CATCH_MACRO \
209 catch (InterpreterError e) \
211 std::cerr << "* IERROR : "<<e.GetMessage()<<std::endl; \
212 if (e.IsInScriptFile()) \
213 std::cerr << "* FILE : '"<<e.GetScriptFile()<<"'"<<std::endl; \
214 std::cerr << "* LINE : "<<e.GetScriptLine()<<std::endl; \
215 int lev = bbtk::MessageManager::GetMessageLevel("Error"); \
217 std::cerr << "* Exception thrown : "<<std::endl; \
218 std::cerr << "* OBJECT : " <<e.GetObject()<<std::endl; \
219 std::cerr << "* FILE : " <<e.GetSourceFile()<<std::endl; \
223 //================================================================
224 void WxGUIScriptingInterface::WxGUICommandEnter(const std::string& command)
228 mWxGUIOutputMessages->Print(s,wxRED);
232 mInterpreter->InterpretLine( command );
236 //================================================================
238 //================================================================
239 /// Runs the interpretation of a file
240 bool WxGUIScriptingInterface::InterpretFile( const std::string& filename)
244 mInterpreter->InterpretFile(filename);
249 //================================================================
251 //================================================================
252 void WxGUIScriptingInterface::WxGUITextEditorRun()
254 // wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
255 std::stringstream* buf = new std::stringstream;
256 (*buf) << mWxGUITextEditor->GetCurrentPage()->GetText();
259 if (mwxMenuItemReset->IsChecked()) WxGUICommandEnter("reset");
260 mInterpreter->InterpretBuffer(buf);
264 //================================================================
266 //================================================================
267 void WxGUIScriptingInterface::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
269 if (!mWxGUITextEditor->CloseAllPages()) return;
272 //================================================================
275 //================================================================
276 void WxGUIScriptingInterface::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
280 wxMessageBox(_T(" bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS-LRMN 2008"),
281 _T("About ..."), wxOK | wxICON_INFORMATION,
284 //================================================================
287 //================================================================
288 void WxGUIScriptingInterface::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
290 std::string commandStr;
291 std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
293 commandStr = "notepad.exe ";
295 commandStr = "gedit ";
297 commandStr = commandStr + configFile;
298 std::cout << "system: " << commandStr << std::endl;
299 system ( commandStr.c_str() );
301 //================================================================
304 //================================================================
305 void WxGUIScriptingInterface::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
308 std::string command("toolsbbtk/appli/CreatePackage");
309 bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
311 Interpreter::Pointer I = Interpreter::New();
312 I->InterpretFile(command);
315 //================================================================
318 //================================================================
319 void WxGUIScriptingInterface::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
321 std::string command("toolsbbtk/appli/CreateBlackBox");
322 bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
324 Interpreter::Pointer I = Interpreter::New();
325 I->InterpretFile(command);
327 //================================================================
329 //================================================================
330 void WxGUIScriptingInterface::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
332 std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
335 std::string strappli="start ";
337 std::string strappli="gnome-open ";
339 std::string strcommand = strappli +default_temp_dir+"/temp_dir/workspace_workspacePrototype.png";
340 std::cout << "system: " << strcommand << std::endl;
341 system ( strcommand.c_str() );
344 //================================================================
347 //================================================================
348 void WxGUIScriptingInterface::OnMenuCreateIndex(wxCommandEvent& WXUNUSED(event))
350 std::string doc_path = ConfigurationFile::GetInstance().Get_doc_path();
351 std::string filepath = doc_path+"/bbdoc/make-index.bbs";
353 Interpreter::Pointer I = Interpreter::New();
355 I->InterpretLine( "exec freeze");
356 I->InterpretLine( "include *");
357 I->InterpretLine( "index "+doc_path+"/bbdoc/index-alpha.html Initials");
358 I->InterpretLine( "index "+doc_path+"/bbdoc/index-package.html Packages");
359 I->InterpretLine( "index "+doc_path+"/bbdoc/index-category.html Categories");
360 I->InterpretLine( "index "+doc_path+"/bbdoc/index-adaptors.html Adaptors");
363 //================================================================
366 //================================================================
367 void WxGUIScriptingInterface::InterpreterUserViewHtmlPage(const std::string& page)
370 // std::cout << "WxGUIScriptingInterface::ShowHtmlPage('"<<page<<"')"<<std::endl;
371 if (mWxGUIHtmlBrowser->GoTo(s))
373 //EED mwxNotebook->ChangeSelection(1);
374 // mwxNotebook->SetSelection(1);
378 // std::cout << "ERROR html"<<std::endl;
381 //================================================================
383 //================================================================
384 bool WxGUIScriptingInterface::WxGUIHtmlBrowserUserOnLinkClicked(const std::string& target)
386 std::cout << "OnLink"<<std::endl;
387 size_t s = target.length();
388 if ((s>3) && (target[s-1]=='s')
389 && (target[s-2]=='b')
390 && (target[s-3]=='b')
391 && (target[s-4]=='.'))
393 mWxGUITextEditor->Open(target);
398 //================================================================
400 void WxGUIScriptingInterface::OnMenuWindowsCheck( wxCommandEvent& event,
403 bool checked = event.IsChecked();
404 bool shown = m_mgr.GetPane(w).IsShown();
405 // std::cout << "checked = "<<checked<<" - shown = "<<shown<<std::endl;
408 m_mgr.GetPane(w).Show(checked);
414 //================================================================
415 void WxGUIScriptingInterface::OnMenuWindowsFiles(wxCommandEvent& event)
417 OnMenuWindowsCheck(event,mWxGUITextEditor);
419 //================================================================
421 //================================================================
422 void WxGUIScriptingInterface::OnMenuWindowsHelp(wxCommandEvent& event)
424 OnMenuWindowsCheck(event,mWxGUIHtmlBrowser);
426 //================================================================
428 //================================================================
429 void WxGUIScriptingInterface::OnMenuWindowsMessages(wxCommandEvent& event)
431 OnMenuWindowsCheck(event,mWxGUIOutputMessages);
433 //================================================================
435 //================================================================
436 void WxGUIScriptingInterface::OnMenuWindowsCommand(wxCommandEvent& event)
438 OnMenuWindowsCheck(event,mWxGUICommand);
440 //================================================================
442 //================================================================
443 void WxGUIScriptingInterface::OnMenuWindowsSave( wxCommandEvent& WXUNUSED(event))
445 // TO DO : Save it into ConfigurationFile
446 // m_mgr.SavePerspective();
448 //================================================================
451 //================================================================
452 void WxGUIScriptingInterface::OnPaneClose(wxAuiManagerEvent& evt)
454 std::cout << "Closing panel '"<<evt.pane->name<<"'"<<std::endl;
457 //================================================================
460 //================================================================
461 BEGIN_EVENT_TABLE(WxGUIScriptingInterface, wxFrame)
462 EVT_MENU(ID_Menu_Quit, WxGUIScriptingInterface::OnMenuQuit)
463 EVT_MENU(ID_Menu_About, WxGUIScriptingInterface::OnMenuAbout)
464 EVT_MENU(ID_Menu_EditConfig, WxGUIScriptingInterface::OnMenuEditConfig)
465 EVT_MENU(ID_Menu_CreatePackage, WxGUIScriptingInterface::OnMenuCreatePackage)
466 EVT_MENU(ID_Menu_CreateBlackBox, WxGUIScriptingInterface::OnMenuCreateBlackBox)
467 EVT_MENU(ID_Menu_ShowImageGraph, WxGUIScriptingInterface::OnMenuShowImageGraph)
468 EVT_MENU(ID_Menu_CreateIndex, WxGUIScriptingInterface::OnMenuCreateIndex)
469 EVT_MENU(ID_Menu_Windows_Files, WxGUIScriptingInterface::OnMenuWindowsFiles)
470 EVT_MENU(ID_Menu_Windows_Help, WxGUIScriptingInterface::OnMenuWindowsHelp)
471 EVT_MENU(ID_Menu_Windows_Messages, WxGUIScriptingInterface::OnMenuWindowsMessages)
472 EVT_MENU(ID_Menu_Windows_Command, WxGUIScriptingInterface::OnMenuWindowsCommand)
473 EVT_AUI_PANE_CLOSE(WxGUIScriptingInterface::OnPaneClose)
474 // EVT_BUTTON(ID_Button_Run, WxGUIScriptingInterface::OnButtonRun )
476 //================================================================
481 #endif //_USE_WXWIDGETS_