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