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