]> 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/21 14:59:39 $
7   Version:   $Revision: 1.2 $
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_Button_Run
57     };
58     
59   
60   //================================================================
61   WxGUIScriptingInterface::WxGUIScriptingInterface( wxWindow *parent, wxString title, wxSize size)
62     : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
63   {     
64 //    m_mgr = new wxAuiManager(this);
65         m_mgr.SetManagedWindow(this);
66    
67     mInterpreter = new bbtk::Interpreter();
68     mInterpreter->SetUser(this);
69     mInterpreter->SetCommandLine(true);
70     //==============
71     // Menu
72     wxInitAllImageHandlers();
73     
74     wxMenu *menuFile = new wxMenu;
75     menuFile->Append( ID_Menu_Quit, _T("&Quit") );
76     
77     wxMenu *menuAbout = new wxMenu;
78     menuAbout->Append( ID_Menu_About, _T("&About...") );
79
80     wxMenu *menuTools = new wxMenu;
81     menuTools->Append( ID_Menu_EditConfig, _T("&Edit bbtk config") );
82     menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
83     menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &blackbox") );
84     menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
85     menuTools->Append( ID_Menu_CreateIndex, _T("&Generate index") );
86     
87     
88     wxMenuBar *menuBar = new wxMenuBar;
89     menuBar->Append( menuFile, _T("&File") );
90     menuBar->Append( menuTools, _T("&Tools") );
91     menuBar->Append( menuAbout, _T("About") );
92     
93     SetMenuBar( menuBar );
94     
95     CreateStatusBar();
96     SetStatusText( _T("Welcome to bbi !") );
97     
98     //
99     mWxGUITextEditor = new WxGUITextEditor(this,this);
100     mWxGUITextEditor->SetFileNameFilter("*.bbs");
101
102     mWxGUIHtmlBrowser = new WxGUIHtmlBrowser(this,wxSize(200,0),this);
103
104     /*    
105           wxBitmap bmp_run(cc_run_xpm);
106           mwxButtonRun = new wxBitmapButton( this, ID_Button_Run,bmp_run);//_T("Run")  );
107     */
108   
109     mWxGUIOutputMessages = new WxGUIOutputMessages(this);
110
111     mWxGUICommand = new WxGUICommand(this,this);
112    
113     mWxGUICommand->SetFocus();
114
115     // Creates and sets the parent window of all bbtk windows
116     wxWindow* top = new wxPanel(this,-1);//,_T("top"));
117     top->Hide();
118     Wx::SetTopWindow(top);
119     
120
121     //
122     m_mgr.AddPane(mWxGUITextEditor,
123                   wxAuiPaneInfo().Name(wxT("editor_content"))
124                   .Caption(wxT("Files"))
125                   .MinimizeButton(true)
126                   .MaximizeButton(true)
127                   .Center()
128                   );     
129     m_mgr.AddPane(mWxGUIHtmlBrowser,
130                   wxAuiPaneInfo().Name(wxT("browser_content"))
131                   .Caption(wxT("Help"))
132                   .MinimizeButton(true)
133                   .MaximizeButton(true)
134                   .Right()
135                   );
136     m_mgr.AddPane(mWxGUIOutputMessages,
137                   wxAuiPaneInfo().Name(wxT("messages_content"))
138                   .Caption(wxT("Messages"))
139                   .MinimizeButton(true)
140                   .MaximizeButton(true)
141                   .Bottom()
142                   );
143     m_mgr.AddPane(mWxGUICommand,
144                   wxAuiPaneInfo().Name(wxT("command_content"))
145                   .Caption(wxT("Command"))
146                   .MinimizeButton(true)
147                   .MaximizeButton(true)
148                   .Bottom()
149                   .Position(1)
150 );     
151     //    m_mgr.AddPane(mwxButtonRun,
152     //            wxAuiPaneInfo().Name(wxT("button_run_content")));     
153
154
155 //.PaneBorder(false)); 
156     m_mgr.Update();
157         
158     SetAutoLayout(true);
159     Layout();
160 //    Refresh();
161 //    m_mgr.Update();
162   }
163   //================================================================
164
165  //================================================================
166   WxGUIScriptingInterface::~WxGUIScriptingInterface()
167   {
168     m_mgr.UnInit();
169   }
170   //================================================================
171
172   //================================================================
173   void WxGUIScriptingInterface::Open(const std::string& filename)
174   {
175     mWxGUITextEditor->Open(filename);
176   }
177   //================================================================
178
179   //================================================================
180   void WxGUIScriptingInterface::WxGUICommandEnter(const std::string& command)
181   {
182     std::string s("> ");
183     s += command + "\n";
184     mWxGUIOutputMessages->Print(s,wxRED);
185
186     if (  mInterpreter->InterpretLine( command ) == 
187           Interpreter::Interpreter_QUIT )
188       {
189         Close(true); 
190       }
191   }
192   //================================================================
193
194   //================================================================
195   /// Runs the interpretation of a file
196   bool WxGUIScriptingInterface::InterpretFile( const std::string& filename) 
197   { 
198     if ( mInterpreter->InterpretFile(filename) ==
199          Interpreter::Interpreter_ERROR ) 
200       {
201         return false;
202       }
203     return true;
204   }
205   //================================================================
206  
207   //================================================================
208   void WxGUIScriptingInterface::WxGUITextEditorRun()
209   {
210     //    wxString per = m_mgr.SavePerspective();
211     //    std::cout << per<< std::endl;
212
213     //    wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
214     std::stringstream* buf = new std::stringstream;
215     (*buf) << mWxGUITextEditor->GetCurrentPage()->GetText();
216
217     mInterpreter->SetThrow(true);
218     try 
219       {
220         mInterpreter->InterpretBuffer(buf);
221       }
222     catch (InterpreterError e)
223       {
224         std::cerr << "* IERROR : "<<e.GetMessage()<<std::endl; 
225         if (e.IsInScriptFile())  
226           std::cerr << "* FILE   : '"<<e.GetScriptFile()<<"'"<<std::endl;
227         std::cerr << "* LINE   : "<<e.GetScriptLine()<<std::endl;
228         int lev = bbtk::MessageManager::GetMessageLevel("Error");
229         if (lev > 0) {
230           std::cerr << "* Exception thrown : "<<std::endl;
231           std::cerr << "*  OBJECT : " <<e.GetObject()<<std::endl;
232           std::cerr << "*  FILE   : " <<e.GetSourceFile()<<std::endl;
233         }         
234       }
235     /*
236    //    wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
237     std::string filename = mWxGUITextEditor->GetCurrentPage();//wx2std(temp);
238     size_t s = filename.length();
239
240     Interpreter* I = new Interpreter;
241     
242     if ((s>3) && (filename[s-1]=='s')
243         && (filename[s-2]=='b')
244         && (filename[s-3]=='b')
245         && (filename[s-4]=='.'))
246       {
247         std::string tmp("Executing ");
248         tmp += filename;
249         SetStatusText(std2wx(tmp));
250         I->InterpretFile(filename);
251       }
252     else
253       {
254         SetStatusText(_T("The current page is not a bbs file : cannot execute it"));
255       }
256     
257     delete I;
258     */
259   }
260   //================================================================  
261
262   //================================================================
263   void WxGUIScriptingInterface::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
264   {
265     Close(true);
266   }
267   //================================================================
268
269
270   //================================================================
271   void WxGUIScriptingInterface::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
272   {
273     m_mgr.Update();
274         Refresh();
275     wxMessageBox(_T("  bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS-LRMN 2008"),
276                  _T("About ..."), wxOK | wxICON_INFORMATION,
277                  this);
278   }
279   //================================================================
280
281
282   //================================================================
283   void WxGUIScriptingInterface::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
284   {
285         std::string commandStr;
286     std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
287 #ifdef WIN32
288         commandStr = "notepad.exe ";
289 #else
290         commandStr = "gedit ";
291 #endif 
292         commandStr = commandStr + configFile;
293         std::cout << "system: " << commandStr << std::endl;
294         system ( commandStr.c_str() );
295   }
296   //================================================================
297
298
299   //================================================================
300   void WxGUIScriptingInterface::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
301   {
302 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 01 \n");
303     std::string command("toolsbbtk/appli/CreatePackage");
304 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 02 \n");
305
306     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
307 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 03 \n");
308     Interpreter* I = new Interpreter;    
309 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 04 \n");
310     I->InterpretFile(command);
311 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 05 \n");
312     delete I;
313 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 06 \n");
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     Interpreter* I = new Interpreter;    
324     I->InterpretFile(command);
325     delete I;
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     Interpreter* I = new Interpreter;    
353
354 //EED   std::cout << "bbi: include "<<filepath<<std::endl;
355 //EED   I->InterpretFile( filepath );
356
357     I->InterpretLine( "exec freeze");
358     I->InterpretLine( "include *");
359     I->InterpretLine( "index "+doc_path+"/bbdoc/index-alpha.html Initials");
360     I->InterpretLine( "index "+doc_path+"/bbdoc/index-package.html Packages");
361     I->InterpretLine( "index "+doc_path+"/bbdoc/index-category.html Categories");
362     I->InterpretLine( "index "+doc_path+"/bbdoc/index-adaptors.html Adaptors");
363     
364     delete I;
365   }
366   //================================================================
367
368
369   //================================================================
370   void WxGUIScriptingInterface::InterpreterUserViewHtmlPage(const std::string& page)
371   {
372     std::string s(page);
373     //  std::cout << "WxGUIScriptingInterface::ShowHtmlPage('"<<page<<"')"<<std::endl;
374     if (mWxGUIHtmlBrowser->GoTo(s)) 
375       {
376 //EED   mwxNotebook->ChangeSelection(1);
377 //      mwxNotebook->SetSelection(1);
378       }
379     else 
380       {
381          // std::cout << "ERROR html"<<std::endl;
382       }
383   } 
384   //================================================================  
385   
386   //================================================================  
387   bool WxGUIScriptingInterface::WxGUIHtmlBrowserUserOnLinkClicked(const std::string& target)
388   {
389     std::cout << "OnLink"<<std::endl; 
390     size_t s = target.length();
391     if ((s>3) && (target[s-1]=='s')
392         && (target[s-2]=='b')
393         && (target[s-3]=='b')
394         && (target[s-4]=='.'))
395       {
396         mWxGUITextEditor->Open(target);
397         return false;
398       }
399     return true;
400   }
401   //================================================================  
402
403   //================================================================  
404   BEGIN_EVENT_TABLE(WxGUIScriptingInterface, wxFrame)
405     EVT_MENU(ID_Menu_Quit, WxGUIScriptingInterface::OnMenuQuit)
406     EVT_MENU(ID_Menu_About, WxGUIScriptingInterface::OnMenuAbout)
407     EVT_MENU(ID_Menu_EditConfig, WxGUIScriptingInterface::OnMenuEditConfig)
408     EVT_MENU(ID_Menu_CreatePackage, WxGUIScriptingInterface::OnMenuCreatePackage)
409     EVT_MENU(ID_Menu_CreateBlackBox, WxGUIScriptingInterface::OnMenuCreateBlackBox)
410     EVT_MENU(ID_Menu_ShowImageGraph, WxGUIScriptingInterface::OnMenuShowImageGraph)
411     EVT_MENU(ID_Menu_CreateIndex, WxGUIScriptingInterface::OnMenuCreateIndex)
412   //    EVT_BUTTON(ID_Button_Run, WxGUIScriptingInterface::OnButtonRun )
413     END_EVENT_TABLE()
414   //================================================================
415
416 } // namespace bbtk
417
418
419 #endif //_USE_WXWIDGETS_