]> 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 15:47:54 $
7   Version:   $Revision: 1.4 $
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     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     // Creates and sets the parent window of all bbtk windows
141     wxWindow* top = new wxPanel(this,-1);//,_T("top"));
142     top->Hide();
143     Wx::SetTopWindow(top);
144     
145
146     //
147     m_mgr.AddPane(mWxGUITextEditor,
148                   wxAuiPaneInfo().Name(wxT("editor_content"))
149                   .Caption(wxT("Files"))
150                   .MinimizeButton(true)
151                   .MaximizeButton(true)
152                   .Center()
153                   .MinSize(wxSize(100,100))
154                   );     
155     m_mgr.AddPane(mWxGUIHtmlBrowser,
156                   wxAuiPaneInfo().Name(wxT("browser_content"))
157                   .Caption(wxT("Help"))
158                   .MinimizeButton(true)
159                   .MaximizeButton(true)
160                   .Right()
161                   .MinSize(wxSize(100,100))
162                   );
163     m_mgr.AddPane(mWxGUIOutputMessages,
164                   wxAuiPaneInfo().Name(wxT("messages_content"))
165                   .Caption(wxT("Messages"))
166                   .MinimizeButton(true)
167                   .MaximizeButton(true)
168                   .Bottom()
169                   .MinSize(wxSize(100,100))
170                   );
171     m_mgr.AddPane(mWxGUICommand,
172                   wxAuiPaneInfo().Name(wxT("command_content"))
173                   .Caption(wxT("Command"))
174                   .MinimizeButton(true)
175                   .MaximizeButton(true)
176                   .Bottom()
177                   .Position(1)
178                   .MinSize(wxSize(100,100))
179                   );     
180     //    m_mgr.AddPane(mwxButtonRun,
181     //            wxAuiPaneInfo().Name(wxT("button_run_content")));     
182
183
184 //.PaneBorder(false)); 
185     m_mgr.Update();
186         
187     SetAutoLayout(true);
188     Layout();
189 //    Refresh();
190 //    m_mgr.Update();
191   }
192   //================================================================
193
194  //================================================================
195   WxGUIScriptingInterface::~WxGUIScriptingInterface()
196   {
197     m_mgr.UnInit();
198   }
199   //================================================================
200
201   //================================================================
202   void WxGUIScriptingInterface::Open(const std::string& filename)
203   {
204     mWxGUITextEditor->Open(filename);
205   }
206   //================================================================
207   
208 #define CATCH_MACRO                                                     \
209   catch (InterpreterError e)                                            \
210     {                                                                   \
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");         \
216       if (lev > 0) {                                                    \
217         std::cerr << "* Exception thrown : "<<std::endl;                \
218         std::cerr << "*  OBJECT : " <<e.GetObject()<<std::endl;         \
219         std::cerr << "*  FILE   : " <<e.GetSourceFile()<<std::endl;     \
220       }                                                                 \
221     }
222   
223   //================================================================
224   void WxGUIScriptingInterface::WxGUICommandEnter(const std::string& command)
225   {
226     std::string s("> ");
227     s += command + "\n";
228     mWxGUIOutputMessages->Print(s,wxRED);
229     
230     try
231       {
232         mInterpreter->InterpretLine( command );
233       }
234     CATCH_MACRO;
235   }
236   //================================================================
237   
238   //================================================================
239   /// Runs the interpretation of a file
240   bool WxGUIScriptingInterface::InterpretFile( const std::string& filename) 
241   { 
242     try
243       {
244         mInterpreter->InterpretFile(filename);
245       }
246     CATCH_MACRO;
247     return true;
248   }
249   //================================================================
250  
251   //================================================================
252   void WxGUIScriptingInterface::WxGUITextEditorRun()
253   {
254     //    wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
255     std::stringstream* buf = new std::stringstream;
256     (*buf) << mWxGUITextEditor->GetCurrentPage()->GetText();
257     try 
258       {
259         if (mwxMenuItemReset->IsChecked()) WxGUICommandEnter("reset");
260         mInterpreter->InterpretBuffer(buf);
261       }
262     CATCH_MACRO
263   }
264   //================================================================  
265
266   //================================================================
267   void WxGUIScriptingInterface::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
268   {
269     if (!mWxGUITextEditor->CloseAllPages()) return;
270     Close(true);
271   }
272   //================================================================
273
274
275   //================================================================
276   void WxGUIScriptingInterface::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
277   {
278     m_mgr.Update();
279         Refresh();
280     wxMessageBox(_T("  bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS-LRMN 2008"),
281                  _T("About ..."), wxOK | wxICON_INFORMATION,
282                  this);
283   }
284   //================================================================
285
286
287   //================================================================
288   void WxGUIScriptingInterface::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
289   {
290         std::string commandStr;
291     std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
292 #ifdef WIN32
293         commandStr = "notepad.exe ";
294 #else
295         commandStr = "gedit ";
296 #endif 
297         commandStr = commandStr + configFile;
298         std::cout << "system: " << commandStr << std::endl;
299         system ( commandStr.c_str() );
300   }
301   //================================================================
302
303
304   //================================================================
305   void WxGUIScriptingInterface::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
306   {
307 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 01 \n");
308     std::string command("toolsbbtk/appli/CreatePackage");
309 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 02 \n");
310
311     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
312 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 03 \n");
313     Interpreter* I = new Interpreter;    
314 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 04 \n");
315     I->InterpretFile(command);
316 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 05 \n");
317     delete I;
318 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 06 \n");
319   }
320   //================================================================
321
322
323   //================================================================
324   void WxGUIScriptingInterface::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
325   {
326     std::string command("toolsbbtk/appli/CreateBlackBox");
327     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
328     Interpreter* I = new Interpreter;    
329     I->InterpretFile(command);
330     delete I;
331   }
332   //================================================================
333   
334   //================================================================
335   void WxGUIScriptingInterface::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
336   {
337     std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
338
339 #if defined(WIN32)
340     std::string strappli="start ";
341 #else
342     std::string strappli="gnome-open ";
343 #endif
344     std::string strcommand = strappli +default_temp_dir+"/temp_dir/workspace_workspacePrototype.png";
345         std::cout << "system: " << strcommand << std::endl;
346     system ( strcommand.c_str() );
347
348   }
349   //================================================================
350
351
352   //================================================================
353   void WxGUIScriptingInterface::OnMenuCreateIndex(wxCommandEvent& WXUNUSED(event))
354   {
355     std::string doc_path = ConfigurationFile::GetInstance().Get_doc_path();
356     std::string filepath = doc_path+"/bbdoc/make-index.bbs";
357     Interpreter* I = new Interpreter;    
358
359 //EED   std::cout << "bbi: include "<<filepath<<std::endl;
360 //EED   I->InterpretFile( filepath );
361
362     I->InterpretLine( "exec freeze");
363     I->InterpretLine( "include *");
364     I->InterpretLine( "index "+doc_path+"/bbdoc/index-alpha.html Initials");
365     I->InterpretLine( "index "+doc_path+"/bbdoc/index-package.html Packages");
366     I->InterpretLine( "index "+doc_path+"/bbdoc/index-category.html Categories");
367     I->InterpretLine( "index "+doc_path+"/bbdoc/index-adaptors.html Adaptors");
368     
369     delete I;
370   }
371   //================================================================
372
373
374   //================================================================
375   void WxGUIScriptingInterface::InterpreterUserViewHtmlPage(const std::string& page)
376   {
377     std::string s(page);
378     //  std::cout << "WxGUIScriptingInterface::ShowHtmlPage('"<<page<<"')"<<std::endl;
379     if (mWxGUIHtmlBrowser->GoTo(s)) 
380       {
381 //EED   mwxNotebook->ChangeSelection(1);
382 //      mwxNotebook->SetSelection(1);
383       }
384     else 
385       {
386          // std::cout << "ERROR html"<<std::endl;
387       }
388   } 
389   //================================================================  
390   
391   //================================================================  
392   bool WxGUIScriptingInterface::WxGUIHtmlBrowserUserOnLinkClicked(const std::string& target)
393   {
394     std::cout << "OnLink"<<std::endl; 
395     size_t s = target.length();
396     if ((s>3) && (target[s-1]=='s')
397         && (target[s-2]=='b')
398         && (target[s-3]=='b')
399         && (target[s-4]=='.'))
400       {
401         mWxGUITextEditor->Open(target);
402         return false;
403       }
404     return true;
405   }
406   //================================================================  
407
408   void WxGUIScriptingInterface::OnMenuWindowsCheck( wxCommandEvent& event, 
409                                                     wxWindow* w)
410   {
411     bool checked = event.IsChecked();
412     bool shown = m_mgr.GetPane(w).IsShown();
413     //    std::cout << "checked = "<<checked<<" - shown = "<<shown<<std::endl;
414     if (checked ^ shown) 
415       {
416         m_mgr.GetPane(w).Show(checked);
417         m_mgr.Update();
418       }
419   }
420
421
422   //================================================================  
423   void WxGUIScriptingInterface::OnMenuWindowsFiles(wxCommandEvent& event)
424   {
425     OnMenuWindowsCheck(event,mWxGUITextEditor);
426   }
427   //================================================================  
428
429   //================================================================  
430   void  WxGUIScriptingInterface::OnMenuWindowsHelp(wxCommandEvent& event)
431   {
432     OnMenuWindowsCheck(event,mWxGUIHtmlBrowser);
433   }
434   //================================================================  
435
436   //================================================================  
437   void  WxGUIScriptingInterface::OnMenuWindowsMessages(wxCommandEvent& event)
438   {
439     OnMenuWindowsCheck(event,mWxGUIOutputMessages);
440   }
441   //================================================================  
442
443   //================================================================  
444   void  WxGUIScriptingInterface::OnMenuWindowsCommand(wxCommandEvent& event)
445   {
446     OnMenuWindowsCheck(event,mWxGUICommand);
447   }
448   //================================================================  
449
450   //================================================================  
451   void WxGUIScriptingInterface::OnMenuWindowsSave( wxCommandEvent& WXUNUSED(event))
452   {
453     // TO DO : Save it into ConfigurationFile
454     //    m_mgr.SavePerspective();
455   }
456   //================================================================  
457
458
459   //================================================================  
460   void WxGUIScriptingInterface::OnPaneClose(wxAuiManagerEvent& evt)
461   {
462     std::cout  << "Closing panel '"<<evt.pane->name<<"'"<<std::endl;
463     
464   }
465   //================================================================  
466
467   
468   //================================================================  
469   BEGIN_EVENT_TABLE(WxGUIScriptingInterface, wxFrame)
470     EVT_MENU(ID_Menu_Quit, WxGUIScriptingInterface::OnMenuQuit)
471     EVT_MENU(ID_Menu_About, WxGUIScriptingInterface::OnMenuAbout)
472     EVT_MENU(ID_Menu_EditConfig, WxGUIScriptingInterface::OnMenuEditConfig)
473     EVT_MENU(ID_Menu_CreatePackage, WxGUIScriptingInterface::OnMenuCreatePackage)
474     EVT_MENU(ID_Menu_CreateBlackBox, WxGUIScriptingInterface::OnMenuCreateBlackBox)
475     EVT_MENU(ID_Menu_ShowImageGraph, WxGUIScriptingInterface::OnMenuShowImageGraph)
476     EVT_MENU(ID_Menu_CreateIndex, WxGUIScriptingInterface::OnMenuCreateIndex)
477     EVT_MENU(ID_Menu_Windows_Files, WxGUIScriptingInterface::OnMenuWindowsFiles)
478     EVT_MENU(ID_Menu_Windows_Help, WxGUIScriptingInterface::OnMenuWindowsHelp)
479     EVT_MENU(ID_Menu_Windows_Messages, WxGUIScriptingInterface::OnMenuWindowsMessages)
480     EVT_MENU(ID_Menu_Windows_Command, WxGUIScriptingInterface::OnMenuWindowsCommand)
481     EVT_AUI_PANE_CLOSE(WxGUIScriptingInterface::OnPaneClose)
482   //    EVT_BUTTON(ID_Button_Run, WxGUIScriptingInterface::OnButtonRun )
483     END_EVENT_TABLE()
484   //================================================================
485
486 } // namespace bbtk
487
488
489 #endif //_USE_WXWIDGETS_