]> 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/22 14:30:25 $
7   Version:   $Revision: 1.6 $
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     // 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
308     std::string command("toolsbbtk/appli/CreatePackage");
309     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
310     
311     Interpreter::Pointer I = Interpreter::New();    
312     I->InterpretFile(command);
313
314   }
315   //================================================================
316
317
318   //================================================================
319   void WxGUIScriptingInterface::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
320   {
321     std::string command("toolsbbtk/appli/CreateBlackBox");
322     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
323     
324     Interpreter::Pointer I = Interpreter::New();    
325     I->InterpretFile(command);
326   }
327   //================================================================
328   
329   //================================================================
330   void WxGUIScriptingInterface::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
331   {
332     std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
333
334 #if defined(WIN32)
335     std::string strappli="start ";
336 #else
337     std::string strappli="gnome-open ";
338 #endif
339     std::string strcommand = strappli +default_temp_dir+"/temp_dir/workspace_workspacePrototype.png";
340     //  std::cout << "system: " << strcommand << std::endl;
341     system ( strcommand.c_str() );
342
343   }
344   //================================================================
345
346
347   //================================================================
348   void WxGUIScriptingInterface::OnMenuCreateIndex(wxCommandEvent& WXUNUSED(event))
349   {
350     std::string doc_path = ConfigurationFile::GetInstance().Get_doc_path();
351     std::string filepath = doc_path+"/bbdoc/make-index.bbs";
352
353     Interpreter::Pointer I = Interpreter::New();    
354
355     I->InterpretLine( "exec freeze");
356     I->InterpretLine( "include *");
357     I->InterpretLine( "index "+doc_path+"/bbdoc/index-alpha.html Initials");
358     I->InterpretLine( "index "+doc_path+"/bbdoc/index-package.html Packages");
359     I->InterpretLine( "index "+doc_path+"/bbdoc/index-category.html Categories");
360     I->InterpretLine( "index "+doc_path+"/bbdoc/index-adaptors.html Adaptors");
361     
362   }
363   //================================================================
364
365
366   //================================================================
367   void WxGUIScriptingInterface::InterpreterUserViewHtmlPage(const std::string& page)
368   {
369     std::string s(page);
370     //  std::cout << "WxGUIScriptingInterface::ShowHtmlPage('"<<page<<"')"<<std::endl;
371     if (mWxGUIHtmlBrowser->GoTo(s)) 
372       {
373 //EED   mwxNotebook->ChangeSelection(1);
374 //      mwxNotebook->SetSelection(1);
375       }
376     else 
377       {
378          // std::cout << "ERROR html"<<std::endl;
379       }
380   } 
381   //================================================================  
382   
383   //================================================================  
384   bool WxGUIScriptingInterface::WxGUIHtmlBrowserUserOnLinkClicked(const std::string& target)
385   {
386     size_t s = target.length();
387     if ((s>3) && (target[s-1]=='s')
388         && (target[s-2]=='b')
389         && (target[s-3]=='b')
390         && (target[s-4]=='.'))
391       {
392         mWxGUITextEditor->Open(target);
393         return false;
394       }
395     return true;
396   }
397   //================================================================  
398
399   void WxGUIScriptingInterface::OnMenuWindowsCheck( wxCommandEvent& event, 
400                                                     wxWindow* w)
401   {
402     bool checked = event.IsChecked();
403     bool shown = m_mgr.GetPane(w).IsShown();
404     //    std::cout << "checked = "<<checked<<" - shown = "<<shown<<std::endl;
405     if (checked ^ shown) 
406       {
407         m_mgr.GetPane(w).Show(checked);
408         m_mgr.Update();
409       }
410   }
411
412
413   //================================================================  
414   void WxGUIScriptingInterface::OnMenuWindowsFiles(wxCommandEvent& event)
415   {
416     OnMenuWindowsCheck(event,mWxGUITextEditor);
417   }
418   //================================================================  
419
420   //================================================================  
421   void  WxGUIScriptingInterface::OnMenuWindowsHelp(wxCommandEvent& event)
422   {
423     OnMenuWindowsCheck(event,mWxGUIHtmlBrowser);
424   }
425   //================================================================  
426
427   //================================================================  
428   void  WxGUIScriptingInterface::OnMenuWindowsMessages(wxCommandEvent& event)
429   {
430     OnMenuWindowsCheck(event,mWxGUIOutputMessages);
431   }
432   //================================================================  
433
434   //================================================================  
435   void  WxGUIScriptingInterface::OnMenuWindowsCommand(wxCommandEvent& event)
436   {
437     OnMenuWindowsCheck(event,mWxGUICommand);
438   }
439   //================================================================  
440
441   //================================================================  
442   void WxGUIScriptingInterface::OnMenuWindowsSave( wxCommandEvent& WXUNUSED(event))
443   {
444     // TO DO : Save it into ConfigurationFile
445     //    m_mgr.SavePerspective();
446   }
447   //================================================================  
448
449
450   //================================================================  
451   void WxGUIScriptingInterface::OnPaneClose(wxAuiManagerEvent& evt)
452   {
453     std::cout  << "Closing panel '"<<evt.pane->name<<"'"<<std::endl;
454     
455   }
456   //================================================================  
457
458   
459   //================================================================  
460   BEGIN_EVENT_TABLE(WxGUIScriptingInterface, wxFrame)
461     EVT_MENU(ID_Menu_Quit, WxGUIScriptingInterface::OnMenuQuit)
462     EVT_MENU(ID_Menu_About, WxGUIScriptingInterface::OnMenuAbout)
463     EVT_MENU(ID_Menu_EditConfig, WxGUIScriptingInterface::OnMenuEditConfig)
464     EVT_MENU(ID_Menu_CreatePackage, WxGUIScriptingInterface::OnMenuCreatePackage)
465     EVT_MENU(ID_Menu_CreateBlackBox, WxGUIScriptingInterface::OnMenuCreateBlackBox)
466     EVT_MENU(ID_Menu_ShowImageGraph, WxGUIScriptingInterface::OnMenuShowImageGraph)
467     EVT_MENU(ID_Menu_CreateIndex, WxGUIScriptingInterface::OnMenuCreateIndex)
468     EVT_MENU(ID_Menu_Windows_Files, WxGUIScriptingInterface::OnMenuWindowsFiles)
469     EVT_MENU(ID_Menu_Windows_Help, WxGUIScriptingInterface::OnMenuWindowsHelp)
470     EVT_MENU(ID_Menu_Windows_Messages, WxGUIScriptingInterface::OnMenuWindowsMessages)
471     EVT_MENU(ID_Menu_Windows_Command, WxGUIScriptingInterface::OnMenuWindowsCommand)
472     EVT_AUI_PANE_CLOSE(WxGUIScriptingInterface::OnPaneClose)
473   //    EVT_BUTTON(ID_Button_Run, WxGUIScriptingInterface::OnButtonRun )
474     END_EVENT_TABLE()
475   //================================================================
476
477 } // namespace bbtk
478
479
480 #endif //_USE_WXWIDGETS_