]> 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/04/25 07:33:17 $
7   Version:   $Revision: 1.8 $
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     m_mgr.AddPane(mWxGUITextEditor,
158                   wxAuiPaneInfo().Name(wxT("editor_content"))
159                   .Caption(wxT("Files"))
160                   .MinimizeButton(true)
161                   .MaximizeButton(true)
162                   .Center()
163                   .MinSize(wxSize(100,100))
164                   );   
165   
166     m_mgr.AddPane(mWxGUIHtmlBrowser,
167                   wxAuiPaneInfo().Name(wxT("browser_content"))
168                   .Caption(wxT("Help"))
169                   .MinimizeButton(true)
170                   .MaximizeButton(true)
171                   .Right()
172                   .MinSize(wxSize(200,100))
173                   );
174
175     m_mgr.AddPane(mWxGUIOutputMessages,
176                   wxAuiPaneInfo().Name(wxT("messages_content"))
177                   .Caption(wxT("Messages"))
178                   .MinimizeButton(true)
179                   .MaximizeButton(true)
180                   .Bottom()
181                   .MinSize(wxSize(100,100))
182                   );
183     m_mgr.AddPane(mWxGUICommand,
184                   wxAuiPaneInfo().Name(wxT("command_content"))
185                   .Caption(wxT("Command"))
186                   .MinimizeButton(true)
187                   .MaximizeButton(true)
188                   .Bottom()
189                   .Position(1)
190                   .MinSize(wxSize(100,100))
191                   );     
192     //    m_mgr.AddPane(mwxButtonRun,
193     //            wxAuiPaneInfo().Name(wxT("button_run_content")));     
194
195     // Creates the parent window of all bbtk windows as a child of this
196     Wx::CreateTopWindow(this);
197     // Add the method OnWxSignal as a Wx::Signal observer 
198     //bbtkAddWxSignalObserver(WxGUIPackageBrowser2Window::OnWxSignal);
199
200 //.PaneBorder(false)); 
201     m_mgr.Update();
202         
203     SetAutoLayout(true);
204     Layout();
205 //    Refresh();
206 //    m_mgr.Update();
207   }
208   //================================================================
209
210  //================================================================
211   WxGUIScriptingInterface::~WxGUIScriptingInterface()
212   {
213     m_mgr.UnInit();
214   }
215   //================================================================
216
217   //================================================================
218   void WxGUIScriptingInterface::Open(const std::string& filename)
219   {
220     mWxGUITextEditor->Open(filename);
221   }
222   //================================================================
223   
224 #define CATCH_MACRO                                                     \
225   catch (QuitException e)                                               \
226     {                                                                   \
227       std::cout << "* quit exception caught"<<std::endl;                \
228     }                                                                   \
229   catch (InterpreterError e)                                            \
230     {                                                                   \
231       std::cerr << "* IERROR : "<<e.GetMessage()<<std::endl;            \
232       if (e.IsInScriptFile())                                           \
233         std::cerr << "* FILE   : '"<<e.GetScriptFile()<<"'"<<std::endl; \
234       std::cerr << "* LINE   : "<<e.GetScriptLine()<<std::endl;         \
235       int lev = bbtk::MessageManager::GetMessageLevel("Error");         \
236       if (lev > 0) {                                                    \
237         std::cerr << "* Exception thrown : "<<std::endl;                \
238         std::cerr << "*  OBJECT : " <<e.GetObject()<<std::endl;         \
239         std::cerr << "*  FILE   : " <<e.GetSourceFile()<<std::endl;     \
240       }                                                                 \
241     }
242   
243   //================================================================
244   void WxGUIScriptingInterface::WxGUICommandEnter(const std::string& command)
245   {
246     std::string s("> ");
247     s += command + "\n";
248     mWxGUIOutputMessages->Print(s,wxRED);
249     
250     try
251       {
252         mInterpreter->InterpretLine( command );
253       }
254     CATCH_MACRO;
255   }
256   //================================================================
257   
258   //================================================================
259   /// Runs the interpretation of a file
260   bool WxGUIScriptingInterface::InterpretFile( const std::string& filename) 
261   { 
262     try
263       {
264         mInterpreter->InterpretFile(filename);
265       }
266     CATCH_MACRO;
267     return true;
268   }
269   //================================================================
270  
271   //================================================================
272   void WxGUIScriptingInterface::WxGUITextEditorRun()
273   {
274     //    wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
275     std::stringstream* buf = new std::stringstream;
276     (*buf) << mWxGUITextEditor->GetCurrentPage()->GetText();
277     try 
278       {
279         std::cout << "RUN"<<std::endl;
280         if (mwxMenuItemReset->IsChecked()) WxGUICommandEnter("reset");
281         mInterpreter->InterpretBuffer(buf);
282         std::cout << "EO RUN"<<std::endl;
283       }
284     CATCH_MACRO;
285     std::cout << "EO RUN 3"<<std::endl;
286   }
287   //================================================================  
288
289   //================================================================
290   void WxGUIScriptingInterface::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
291   {
292     if (!mWxGUITextEditor->CloseAllPages()) return;
293     Close(true);
294   }
295   //================================================================
296
297
298   //================================================================
299   void WxGUIScriptingInterface::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
300   {
301     m_mgr.Update();
302         Refresh();
303     wxMessageBox(_T("  bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS-LRMN 2008"),
304                  _T("About ..."), wxOK | wxICON_INFORMATION,
305                  this);
306   }
307   //================================================================
308
309
310   //================================================================
311   void WxGUIScriptingInterface::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
312   {
313         std::string commandStr;
314     std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
315 #ifdef WIN32
316         commandStr = "notepad.exe ";
317 #else
318         commandStr = "gedit ";
319 #endif 
320         commandStr = commandStr + configFile;
321         //      std::cout << "system: " << commandStr << std::endl;
322         system ( commandStr.c_str() );
323   }
324   //================================================================
325
326
327   //================================================================
328   void WxGUIScriptingInterface::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
329   {
330
331     std::string command("toolsbbtk/appli/CreatePackage");
332     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
333     
334     Interpreter::Pointer I = Interpreter::New();    
335     I->InterpretFile(command);
336
337   }
338   //================================================================
339
340
341   //================================================================
342   void WxGUIScriptingInterface::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
343   {
344     std::string command("toolsbbtk/appli/CreateBlackBox");
345     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
346     
347     Interpreter::Pointer I = Interpreter::New();    
348     I->InterpretFile(command);
349   }
350   //================================================================
351   
352   //================================================================
353   void WxGUIScriptingInterface::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
354   {
355     std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
356
357 #if defined(WIN32)
358     std::string strappli="start ";
359 #else
360     std::string strappli="gnome-open ";
361 #endif
362     std::string strcommand = strappli +default_temp_dir+"/temp_dir/workspace_workspacePrototype.png";
363     //  std::cout << "system: " << strcommand << std::endl;
364     system ( strcommand.c_str() );
365
366   }
367   //================================================================
368
369
370   //================================================================
371   void WxGUIScriptingInterface::OnMenuCreateIndex(wxCommandEvent& WXUNUSED(event))
372   {
373     std::string doc_path = ConfigurationFile::GetInstance().Get_doc_path();
374     std::string filepath = doc_path+"/bbdoc/make-index.bbs";
375
376     Interpreter::Pointer I = Interpreter::New();    
377
378     I->InterpretLine( "exec freeze");
379     I->InterpretLine( "include *");
380     I->InterpretLine( "index "+doc_path+"/bbdoc/index-alpha.html Initials");
381     I->InterpretLine( "index "+doc_path+"/bbdoc/index-package.html Packages");
382     I->InterpretLine( "index "+doc_path+"/bbdoc/index-category.html Categories");
383     I->InterpretLine( "index "+doc_path+"/bbdoc/index-adaptors.html Adaptors");
384     
385   }
386   //================================================================
387
388
389   //================================================================
390   void WxGUIScriptingInterface::InterpreterUserViewHtmlPage(const std::string& page)
391   {
392     std::string s(page);
393     //  std::cout << "WxGUIScriptingInterface::ShowHtmlPage('"<<page<<"')"<<std::endl;
394     if (mWxGUIHtmlBrowser->GoTo(s)) 
395       {
396 //EED   mwxNotebook->ChangeSelection(1);
397 //      mwxNotebook->SetSelection(1);
398       }
399     else 
400       {
401          // std::cout << "ERROR html"<<std::endl;
402       }
403   } 
404   //================================================================  
405   
406   //================================================================  
407   bool WxGUIScriptingInterface::WxGUIHtmlBrowserUserOnLinkClicked(const std::string& target)
408   {
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_