]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxGUIScriptingInterface.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkWxGUIScriptingInterface.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkWxGUIScriptingInterface.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/06/10 19:19:42 $
7   Version:   $Revision: 1.11 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*//**
18  * \brief Short description in one line
19  * 
20  * Long description which 
21  * can span multiple lines
22  */
23 /**
24  * \file 
25  * \brief 
26  */
27 /**
28  * \class bbtk::
29  * \brief 
30  */
31
32
33 #ifdef _USE_WXWIDGETS_
34
35 #include <iostream>     
36 #include "bbtkWxGUIScriptingInterface.h"
37 #include "bbtkWxBlackBox.h"
38 #include "bbtkConfigurationFile.h"
39 #include "bbtkWxStreamRedirector.h"
40  
41
42 #include "icons/cc_run.xpm"
43
44 namespace bbtk
45 {
46
47     enum
48     {
49       ID_Menu_Quit = 1,
50       ID_Menu_About,
51       ID_Menu_EditConfig,
52       ID_Menu_CreatePackage,
53       ID_Menu_CreateBlackBox,
54       ID_Menu_ShowImageGraph,
55       ID_Menu_CreateIndex,
56       ID_Menu_Windows_Files,
57       ID_Menu_Windows_Help,
58       ID_Menu_Windows_Messages,
59       ID_Menu_Windows_Command,
60       ID_Menu_Windows_Save,
61           ID_Menu_Windows_HelpBrowser
62       //      ID_Button_Run
63     };
64     
65   
66   //================================================================
67   WxGUIScriptingInterface::WxGUIScriptingInterface( wxWindow *parent, wxString title, wxSize size)
68     : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
69   {     
70 //    m_mgr = new wxAuiManager(this);
71         m_mgr.SetManagedWindow(this);
72    
73         mInterpreter = bbtk::Interpreter::New();
74     mInterpreter->SetUser(this);
75     mInterpreter->SetCommandLine(true);
76     mInterpreter->SetThrow(true);
77     //==============
78     // Menu
79     wxInitAllImageHandlers();
80     
81     wxMenu *menuFile = new wxMenu;
82     menuFile->Append( ID_Menu_Quit, _T("&Quit") );
83     
84     wxMenu *menuAbout = new wxMenu;
85     menuAbout->Append( ID_Menu_About, _T("&About...") );
86
87     wxMenu *menuTools = new wxMenu;
88     menuTools->Append( ID_Menu_EditConfig, _T("&Edit bbtk config") );
89     menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
90     menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &blackbox") );
91     menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
92     menuTools->Append( ID_Menu_CreateIndex, _T("&Generate index") );
93
94     wxMenu *menuWindows = new wxMenu;
95     menuWindows->AppendCheckItem(ID_Menu_Windows_Files,
96                                  _T("Show 'files' panel") )->Check();
97     menuWindows->AppendCheckItem(ID_Menu_Windows_Help,
98                                  _T("Show 'help' panel") )->Check();
99     menuWindows->AppendCheckItem(ID_Menu_Windows_Messages,
100                                  _T("Show 'messages' panel") )->Check();
101     menuWindows->AppendCheckItem(ID_Menu_Windows_Command,
102                                  _T("Show 'command' panel") )->Check();
103     menuWindows->AppendSeparator();
104     menuWindows->Append( ID_Menu_Windows_HelpBrowser, _T("Show help &Browser") );
105     
106     menuWindows->AppendSeparator();
107     menuWindows->Append ( ID_Menu_Windows_Save, _T("Save interface configuration"));
108
109     wxMenu *menuOptions = new wxMenu;
110     mwxMenuItemReset = menuOptions->AppendCheckItem(-1,
111                                                     _T("Reset before running") );
112     mwxMenuItemReset->Check();
113
114     wxMenuBar *menuBar = new wxMenuBar;
115     menuBar->Append( menuFile, _T("&File") );
116     menuBar->Append( menuTools, _T("&Tools") );
117     menuBar->Append( menuOptions, _T("&Options") );
118     menuBar->Append( menuWindows, _T("&Windows") );
119     menuBar->Append( menuAbout, _T("About") );
120     
121     SetMenuBar( menuBar );
122     
123     CreateStatusBar();
124     SetStatusText( _T("Welcome to bbi !") );
125     
126     //
127     mWxGUITextEditor = new WxGUITextEditor(this,this);
128     mWxGUITextEditor->SetFileNameFilter("*.bbs");
129         
130     mWxGUIHtmlBrowser           = new WxGUIHtmlBrowser(this,wxSize(200,0),this);
131
132
133         /*    
134           wxBitmap bmp_run(cc_run_xpm);
135           mwxButtonRun = new wxBitmapButton( this, ID_Button_Run,bmp_run);//_T("Run")  );
136     */
137   
138     mWxGUIOutputMessages = new WxGUIOutputMessages(this);
139
140     mWxGUICommand = new WxGUICommand(this,this);
141    
142     mWxGUICommand->SetFocus();
143
144  
145
146     mwxNotebook = new wxAuiNotebook(this,  
147                                     -1,
148                                     wxPoint(0, 0),
149                                     wxSize(500,500),
150                                     wxAUI_NB_TAB_SPLIT 
151                                     | wxAUI_NB_TAB_MOVE
152                                     | wxAUI_NB_TAB_EXTERNAL_MOVE
153                                     //| wxAUI_NB_WINDOWLIST_BUTTON
154                                     //|wxAUI_NB_SCROLL_BUTTONS
155                                     // | wxAUI_NB_CLOSE_BUTTON 
156                                     //| wxAUI_NB_CLOSE_ON_ACTIVE_TAB
157                                     //| wxAUI_NB_CLOSE_ON_ALL_TABS
158                                     | wxNO_BORDER);
159   
160
161     mwxNotebook->AddPage(mWxGUITextEditor,_T("Files"),true);
162     mwxNotebook->AddPage(mWxGUIHtmlBrowser,_T("Help"),true);
163
164     mwxNotebook->AddPage(mWxGUIOutputMessages,_T("Messages"),true);
165
166     mwxNotebook->AddPage(mWxGUICommand,_T("Command"),true);
167
168
169     m_mgr.AddPane(mwxNotebook,
170                   wxAuiPaneInfo().Name(wxT("nb"))
171                   .CaptionVisible(false)
172                   .MinimizeButton(false)
173                   .MaximizeButton(false)
174                   .Center()
175                   //              .MinSize(wxSize(100,100))
176                   );   
177
178   /*
179     //
180     m_mgr.AddPane(mWxGUITextEditor,
181                   wxAuiPaneInfo().Name(wxT("editor_content"))
182                   .Caption(wxT("Files"))
183                   .MinimizeButton(true)
184                   .MaximizeButton(true)
185                   .Center()
186                   .MinSize(wxSize(100,100))
187                   );   
188   
189     m_mgr.AddPane(mWxGUIHtmlBrowser,
190                   wxAuiPaneInfo().Name(wxT("browser_content"))
191                   .Caption(wxT("Help"))
192                   .MinimizeButton(true)
193                   .MaximizeButton(true)
194                   .Right()
195                   .MinSize(wxSize(200,100))
196                   );
197
198     m_mgr.AddPane(mWxGUIOutputMessages,
199                   wxAuiPaneInfo().Name(wxT("messages_content"))
200                   .Caption(wxT("Messages"))
201                   .MinimizeButton(true)
202                   .MaximizeButton(true)
203                   .Bottom()
204                   .MinSize(wxSize(100,100))
205                   );
206     m_mgr.AddPane(mWxGUICommand,
207                   wxAuiPaneInfo().Name(wxT("command_content"))
208                   .Caption(wxT("Command"))
209                   .MinimizeButton(true)
210                   .MaximizeButton(true)
211                   .Bottom()
212                   .Position(1)
213                   .MinSize(wxSize(100,100))
214                   );     
215     */
216     //    m_mgr.AddPane(mwxButtonRun,
217     //            wxAuiPaneInfo().Name(wxT("button_run_content")));     
218
219     // Creates the parent window of all bbtk windows as a child of this
220     Wx::CreateTopWindow(this);
221     // Add the method OnWxSignal as a Wx::Signal observer 
222     //bbtkAddWxSignalObserver(WxGUIPackageBrowser2Window::OnWxSignal);
223
224 //.PaneBorder(false)); 
225     m_mgr.Update();
226         
227     SetAutoLayout(true);
228     Layout();
229     mwxNotebook->SetSelection(1);
230     mWxGUIHtmlBrowser->GoHome();
231 //    Refresh();
232 //    m_mgr.Update();
233   }
234   //================================================================
235
236  //================================================================
237   WxGUIScriptingInterface::~WxGUIScriptingInterface()
238   {
239     m_mgr.UnInit();
240   }
241   //================================================================
242
243   //================================================================
244   void WxGUIScriptingInterface::Open(const std::string& filename)
245   {
246     mWxGUITextEditor->Open(filename);
247   }
248   //================================================================
249   
250 #define CATCH_MACRO                                                     \
251   catch (QuitException e)                                               \
252     {                                                                   \
253       std::cout << "* quit exception caught"<<std::endl;                \
254     }                                                                   \
255   catch (InterpreterError e)                                            \
256     {                                                                   \
257       std::cerr << "* IERROR : "<<e.GetMessage()<<std::endl;            \
258       if (e.IsInScriptFile())                                           \
259         std::cerr << "* FILE   : '"<<e.GetScriptFile()<<"'"<<std::endl; \
260       std::cerr << "* LINE   : "<<e.GetScriptLine()<<std::endl;         \
261       int lev = bbtk::MessageManager::GetMessageLevel("Error");         \
262       if (lev > 0) {                                                    \
263         std::cerr << "* Exception thrown : "<<std::endl;                \
264         std::cerr << "*  OBJECT : " <<e.GetObject()<<std::endl;         \
265         std::cerr << "*  FILE   : " <<e.GetSourceFile()<<std::endl;     \
266       }                                                                 \
267     }
268   
269   //================================================================
270   void WxGUIScriptingInterface::WxGUICommandEnter(const std::string& command)
271   {
272     std::string s("> ");
273     s += command + "\n";
274     mWxGUIOutputMessages->Print(s,wxRED);
275     
276     try
277       {
278         mInterpreter->InterpretLine( command );
279       }
280     CATCH_MACRO;
281   }
282   //================================================================
283   
284   //================================================================
285   /// Runs the interpretation of a file
286   bool WxGUIScriptingInterface::InterpretFile( const std::string& filename) 
287   { 
288     try
289       {
290         mInterpreter->InterpretFile(filename);
291       }
292     CATCH_MACRO;
293     return true;
294   }
295   //================================================================
296  
297   //================================================================
298   void WxGUIScriptingInterface::WxGUITextEditorRun()
299   {
300     //    wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
301     std::stringstream* buf = new std::stringstream;
302     (*buf) << mWxGUITextEditor->GetCurrentPage()->GetText();
303     try 
304       {
305         std::cout << "RUN"<<std::endl;
306         if (mwxMenuItemReset->IsChecked()) WxGUICommandEnter("reset");
307         mInterpreter->InterpretBuffer(buf);
308         std::cout << "EO RUN"<<std::endl;
309       }
310     CATCH_MACRO;
311     std::cout << "EO RUN 3"<<std::endl;
312   }
313   //================================================================  
314
315   //================================================================
316   void WxGUIScriptingInterface::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
317   {
318     if (!mWxGUITextEditor->CloseAllPages()) return;
319     Close(true);
320   }
321   //================================================================
322
323
324   //================================================================
325   void WxGUIScriptingInterface::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
326   {
327     m_mgr.Update();
328         Refresh();
329     wxMessageBox(_T("  bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS-LRMN 2008"),
330                  _T("About ..."), wxOK | wxICON_INFORMATION,
331                  this);
332   }
333   //================================================================
334
335
336   //================================================================
337   void WxGUIScriptingInterface::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
338   {
339         std::string commandStr;
340     std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
341 #ifdef WIN32
342         commandStr = "notepad.exe ";
343 #else
344         commandStr = "gedit ";
345 #endif 
346         commandStr = commandStr + configFile;
347         //      std::cout << "system: " << commandStr << std::endl;
348         system ( commandStr.c_str() );
349   }
350   //================================================================
351
352   //================================================================
353   void WxGUIScriptingInterface::OnMenuWindowsHelpBrowser(wxCommandEvent& WXUNUSED(event))
354   {
355          wxBusyCursor wait;
356      WxGUIPackageBrowser2 *browser = new WxGUIPackageBrowser2(this);
357      browser->IncludeAll();
358          mwxNotebook->AddPage(browser,_T("Help Browser"),true);
359   }
360   //================================================================
361
362
363
364   //================================================================
365   void WxGUIScriptingInterface::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
366   {
367
368     std::string command("toolsbbtk/appli/GUICreatePackage");
369     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
370     
371     Interpreter::Pointer I = Interpreter::New();    
372     I->InterpretFile(command);
373
374   }
375   //================================================================
376
377
378   //================================================================
379   void WxGUIScriptingInterface::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
380   {
381     std::string command("toolsbbtk/appli/GUICreateBlackBox");
382     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
383     
384     Interpreter::Pointer I = Interpreter::New();    
385     I->InterpretFile(command);
386   }
387   //================================================================
388   
389   //================================================================
390   void WxGUIScriptingInterface::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
391   {
392     std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
393
394 #if defined(WIN32)
395     std::string strappli="start ";
396 #else
397     std::string strappli="gnome-open ";
398 #endif
399     std::string strcommand = strappli +default_temp_dir+"/temp_dir/workspace_workspacePrototype.png";
400     //  std::cout << "system: " << strcommand << std::endl;
401     system ( strcommand.c_str() );
402
403   }
404   //================================================================
405
406
407   //================================================================
408   void WxGUIScriptingInterface::OnMenuCreateIndex(wxCommandEvent& WXUNUSED(event))
409   {
410     std::string doc_path = ConfigurationFile::GetInstance().Get_doc_path();
411     std::string filepath = doc_path+"/bbdoc/make-index.bbs";
412
413     Interpreter::Pointer I = Interpreter::New();    
414
415     I->InterpretLine( "exec freeze");
416     I->InterpretLine( "include *");
417     I->InterpretLine( "index "+doc_path+"/bbdoc/index-alpha.html Initials");
418     I->InterpretLine( "index "+doc_path+"/bbdoc/index-package.html Packages");
419     I->InterpretLine( "index "+doc_path+"/bbdoc/index-category.html Categories");
420     I->InterpretLine( "index "+doc_path+"/bbdoc/index-adaptors.html Adaptors");
421     
422   }
423   //================================================================
424
425
426   //================================================================
427   void WxGUIScriptingInterface::InterpreterUserViewHtmlPage(const std::string& page)
428   {
429     std::string s(page);
430     //  std::cout << "WxGUIScriptingInterface::ShowHtmlPage('"<<page<<"')"<<std::endl;
431     if (mWxGUIHtmlBrowser->GoTo(s)) 
432       {
433 //EED   mwxNotebook->ChangeSelection(1);
434 //      mwxNotebook->SetSelection(1);
435       }
436     else 
437       {
438          // std::cout << "ERROR html"<<std::endl;
439       }
440   } 
441   //================================================================  
442   
443   //================================================================  
444   bool WxGUIScriptingInterface::WxGUIHtmlBrowserUserOnLinkClicked(const std::string& target)
445   {
446     size_t s = target.length();
447     if ((s>3) && (target[s-1]=='s')
448         && (target[s-2]=='b')
449         && (target[s-3]=='b')
450         && (target[s-4]=='.'))
451       {
452         mWxGUITextEditor->Open(target);
453         mwxNotebook->SetSelection(0);
454         mWxGUITextEditor->SetFocus();
455         return false;
456       }
457     return true;
458   }
459   //================================================================  
460
461   void WxGUIScriptingInterface::OnMenuWindowsCheck( wxCommandEvent& event, 
462                                                     wxWindow* w)
463   {
464     bool checked = event.IsChecked();
465     bool shown = m_mgr.GetPane(w).IsShown();
466     //    std::cout << "checked = "<<checked<<" - shown = "<<shown<<std::endl;
467     if (checked ^ shown) 
468       {
469         m_mgr.GetPane(w).Show(checked);
470         m_mgr.Update();
471       }
472   }
473
474
475   //================================================================  
476   void WxGUIScriptingInterface::OnMenuWindowsFiles(wxCommandEvent& event)
477   {
478     OnMenuWindowsCheck(event,mWxGUITextEditor);
479   }
480   //================================================================  
481
482   //================================================================  
483   void  WxGUIScriptingInterface::OnMenuWindowsHelp(wxCommandEvent& event)
484   {
485     OnMenuWindowsCheck(event,mWxGUIHtmlBrowser);
486   }
487   //================================================================  
488
489   //================================================================  
490   void  WxGUIScriptingInterface::OnMenuWindowsMessages(wxCommandEvent& event)
491   {
492     OnMenuWindowsCheck(event,mWxGUIOutputMessages);
493   }
494   //================================================================  
495
496   //================================================================  
497   void  WxGUIScriptingInterface::OnMenuWindowsCommand(wxCommandEvent& event)
498   {
499     OnMenuWindowsCheck(event,mWxGUICommand);
500   }
501   //================================================================  
502
503   //================================================================  
504   void WxGUIScriptingInterface::OnMenuWindowsSave( wxCommandEvent& WXUNUSED(event))
505   {
506     // TO DO : Save it into ConfigurationFile
507     //    m_mgr.SavePerspective();
508   }
509   //================================================================  
510
511
512   //================================================================  
513   void WxGUIScriptingInterface::OnPaneClose(wxAuiManagerEvent& evt)
514   {
515     std::cout  << "Closing panel '"<<evt.pane->name<<"'"<<std::endl;
516     
517   }
518   //================================================================  
519
520   
521   //================================================================  
522   BEGIN_EVENT_TABLE(WxGUIScriptingInterface, wxFrame)
523     EVT_MENU(ID_Menu_Quit, WxGUIScriptingInterface::OnMenuQuit)
524     EVT_MENU(ID_Menu_About, WxGUIScriptingInterface::OnMenuAbout)
525     EVT_MENU(ID_Menu_EditConfig, WxGUIScriptingInterface::OnMenuEditConfig)
526     EVT_MENU(ID_Menu_CreatePackage, WxGUIScriptingInterface::OnMenuCreatePackage)
527     EVT_MENU(ID_Menu_CreateBlackBox, WxGUIScriptingInterface::OnMenuCreateBlackBox)
528     EVT_MENU(ID_Menu_ShowImageGraph, WxGUIScriptingInterface::OnMenuShowImageGraph)
529     EVT_MENU(ID_Menu_CreateIndex, WxGUIScriptingInterface::OnMenuCreateIndex)
530     EVT_MENU(ID_Menu_Windows_Files, WxGUIScriptingInterface::OnMenuWindowsFiles)
531     EVT_MENU(ID_Menu_Windows_Help, WxGUIScriptingInterface::OnMenuWindowsHelp)
532     EVT_MENU(ID_Menu_Windows_Messages, WxGUIScriptingInterface::OnMenuWindowsMessages)
533     EVT_MENU(ID_Menu_Windows_Command, WxGUIScriptingInterface::OnMenuWindowsCommand)
534     EVT_MENU(ID_Menu_Windows_HelpBrowser, WxGUIScriptingInterface::OnMenuWindowsHelpBrowser)
535     EVT_AUI_PANE_CLOSE(WxGUIScriptingInterface::OnPaneClose)
536   //    EVT_BUTTON(ID_Button_Run, WxGUIScriptingInterface::OnButtonRun )
537     END_EVENT_TABLE()
538   //================================================================
539
540 } // namespace bbtk
541
542
543 #endif //_USE_WXWIDGETS_