]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxGUIConsole.cxx
Feature #1774
[bbtk.git] / kernel / src / bbtkWxGUIConsole.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbtkWxGUIConsole.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:49:01 $
33   Version:   $Revision: 1.18 $
34 =========================================================================*/
35
36
37
38
39 /**
40  * \brief Short description in one line
41  * 
42  * Long description which 
43  * can span multiple lines
44  */
45 /**
46  * \file 
47  * \brief 
48  */
49 /**
50  * \class bbtk::
51  * \brief 
52  */
53
54
55 #ifdef _USE_WXWIDGETS_
56
57 #include <iostream>     
58 #include "bbtkWxGUIConsole.h"
59 #include "bbtkWxBlackBox.h"
60 #include "bbtkConfigurationFile.h"
61 #include "bbtkWxStreamRedirector.h"
62  
63
64 #include "icons/cc_run.xpm"
65
66 namespace bbtk
67 {
68
69     enum
70     {
71       ID_Menu_Quit = 1,
72       ID_Menu_About,
73       ID_Menu_EditConfig,
74       ID_Menu_CreatePackage,
75       ID_Menu_CreateBlackBox,
76       ID_Menu_ShowImageGraph,
77       ID_Menu_ShowHTMLDoc,
78       ID_Menu_CreateIndex,
79       ID_Button_Run
80     };
81     
82   
83   //================================================================
84   WxGUIConsole::WxGUIConsole( wxWindow *parent, wxString title, wxSize size)
85     : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
86   {     
87 //    m_mgr = new wxAuiManager(this);
88         m_mgr.SetManagedWindow(this);
89    
90         mInterpreter = bbtk::Interpreter::New();
91     mInterpreter->SetUser(this);
92     mInterpreter->SetCommandLine(true);
93     mInterpreter->SetThrow(false);
94     //==============
95     // Menu
96     wxInitAllImageHandlers();
97     
98     wxMenu *menuFile = new wxMenu;
99     menuFile->Append( ID_Menu_Quit, _T("&Quit") );
100     
101     wxMenu *menuAbout = new wxMenu;
102     menuAbout->Append( ID_Menu_About, _T("&About...") );
103
104     wxMenu *menuTools = new wxMenu;
105     menuTools->Append( ID_Menu_EditConfig, _T("&Edit bbtk config") );
106     menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
107     menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &blackbox") );
108     menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
109     menuTools->Append( ID_Menu_ShowHTMLDoc, _T("Show &HTML documentation") );
110     menuTools->Append( ID_Menu_CreateIndex, _T("&Generate index") );
111     
112     
113     wxMenuBar *menuBar = new wxMenuBar;
114     menuBar->Append( menuFile, _T("&File") );
115     menuBar->Append( menuTools, _T("&Tools") );
116     menuBar->Append( menuAbout, _T("About") );
117     
118     SetMenuBar( menuBar );
119     
120     CreateStatusBar();
121     SetStatusText( _T("Welcome to bbi !") );
122     
123     //==============
124     // Notebook
125     
126     //    wxFlexGridSizer *sizer = new wxFlexGridSizer(1);
127     
128 //EED    wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
129 //    mwxNotebook = new wxNotebook(this,-1,wxDefaultPosition, wxDefaultSize, 0);
130     mwxNotebook = new wxAuiNotebook(this,  
131                                     -1,
132                                     wxPoint(0, 0),
133                                     wxSize(500,500),
134                                     wxAUI_NB_TAB_SPLIT | wxAUI_NB_TAB_EXTERNAL_MOVE | wxNO_BORDER);
135     
136     mwxPageCommand = new wxPanel(mwxNotebook,-1);        
137     mwxPageHelp = new wxPanel(mwxNotebook,-1);    
138     
139     
140     wxBoxSizer *cmdsizer = new wxBoxSizer(wxVERTICAL);
141     
142     mwxPageCommand->SetAutoLayout(true);    
143     mwxPageCommand->SetSizer(cmdsizer);
144     cmdsizer->Fit(mwxPageCommand);
145     cmdsizer->SetSizeHints(mwxPageCommand);
146
147     wxBoxSizer *helpsizer = new wxBoxSizer(wxVERTICAL);
148     
149     mwxPageHelp->SetAutoLayout(true);    
150     mwxPageHelp->SetSizer(helpsizer);
151     helpsizer->Fit(mwxPageHelp);
152     helpsizer->SetSizeHints(mwxPageHelp);
153    
154     mWxGUIHtmlBrowser = new WxGUIHtmlBrowser(mwxPageHelp,
155                                              //EED                                wxSize(1200,0));
156                                              wxSize(200,0));
157
158     //    mWxGUIHtmlBrowser->SetSize(wxSize(800,1000));
159     helpsizer->Add (mWxGUIHtmlBrowser,1, wxGROW|wxLEFT|wxRIGHT|wxTOP, 5  );
160 //    helpsizer->Add ( new wxButton(mwxPageHelp,-1,"perro"), 0,  wxEXPAND  );
161     wxBitmap bmp_run(cc_run_xpm);
162     mwxButtonRun = new wxBitmapButton( mwxPageHelp,ID_Button_Run,bmp_run);//_T("Run")  );
163     helpsizer->Add( mwxButtonRun, 0, wxALL, 5  );
164
165   
166     //==============
167     // Command page 
168     mWxGUIOutputMessages = new WxGUIOutputMessages(mwxPageCommand);
169
170     mWxGUICommand = new WxGUICommand(mwxPageCommand,this);
171    
172     mWxGUICommand->SetFocus();
173
174     cmdsizer->Add (mWxGUIOutputMessages, 1, wxALL | wxGROW, 5);
175     cmdsizer->Add (mWxGUICommand, 0, wxALL | wxGROW, 5);
176
177         
178     // Set the parent window of all bbtk windows as a child of this
179     //    bbtk::Wx::SetTopWindowParent(this);
180     bbtk::Wx::SetTopWindow(this);
181     // Top Window Auto Destroys when no more black box window alive 
182     // : this is the default 
183     //    bbtk::Wx::SetAutoDestroyTopWindow(true);
184     // Add the method OnWxSignal as a Wx::Signal observer 
185     bbtkAddWxSignalObserver(WxGUIConsole::OnWxSignal);
186
187
188     // Layout
189 //EED    SetSizer(sizer);
190
191     mwxNotebook->AddPage( mwxPageCommand, _T("Command"));
192     mwxNotebook->AddPage( mwxPageHelp, _T("Help"));
193     m_mgr.AddPane(mwxNotebook, wxAuiPaneInfo().Name(wxT("notebook_content")).CenterPane().PaneBorder(false)); 
194     m_mgr.Update();
195         
196     SetAutoLayout(true);
197     Layout();
198 //    Refresh();
199 //    m_mgr.Update();
200   }
201   //================================================================
202
203  //================================================================
204   WxGUIConsole::~WxGUIConsole()
205   {
206     m_mgr.UnInit();
207   }
208   //================================================================
209
210   //================================================================
211   void WxGUIConsole::OnClose(wxCloseEvent& event)
212   {
213     bbtkDebugMessage("widget",9,"bbi::OnClose()"<<std::endl);
214     //Wx::SetWindowsHaveBeenDestroyedByParent();
215     //mInterpreter.reset();
216     wxWindowList& list = GetChildren();
217     wxWindowList::iterator iter;
218     for (iter = list.begin(); iter != list.end(); ++iter)
219       {
220         (*iter)->Close();
221       }
222     this->Destroy();
223   }
224   //================================================================
225
226   //================================================================
227   void WxGUIConsole::OnWxSignal()
228   {
229     if ((!bbtk::Wx::IsSomeWindowAlive())&&(!IsShown())) 
230       {
231         bbtkDebugMessage("wx",2,"  --> bbtk top window destroyed and WxGUIConsole not shown => destructing"<<std::endl);
232         /*
233         wxWindowList& list = GetChildren();
234         wxWindowList::iterator iter;
235         for (iter = list.begin(); iter != list.end(); ++iter)
236           {
237             (*iter)->Close();
238           }
239         */
240         Close();
241       }
242   }
243   //================================================================
244
245   //================================================================
246   void WxGUIConsole::WxGUICommandEnter(const std::string& command)
247   {
248     std::string s("> ");
249     s += command + "\n";
250     mWxGUIOutputMessages->Print(s,wxRED);
251
252     if (  mInterpreter->InterpretLine( command ) == 
253           Interpreter::Interpreter_QUIT )
254       {
255         Close(true); 
256       }
257   }
258   //================================================================
259
260   //================================================================
261   /// Runs the interpretation of a file
262   bool WxGUIConsole::InterpretFile( const std::string& filename) 
263   { 
264     if ( mInterpreter->InterpretFile(filename) ==
265          Interpreter::Interpreter_ERROR ) 
266       {
267         return false;
268       }
269     return true;
270   }
271   //================================================================
272  
273   //================================================================
274   void WxGUIConsole::OnButtonRun(wxCommandEvent& WXUNUSED(event))
275   {
276 //    wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
277     std::string filename = mWxGUIHtmlBrowser->GetCurrentPage();//wx2std(temp);
278     size_t s = filename.length();
279
280     Interpreter::Pointer I = Interpreter::New();
281     
282     if ((s>3) && (filename[s-1]=='s')
283         && (filename[s-2]=='b')
284         && (filename[s-3]=='b')
285         && (filename[s-4]=='.'))
286       {
287         std::string tmp("Executing ");
288         tmp += filename;
289         SetStatusText(std2wx(tmp));
290         I->InterpretFile(filename);
291       }
292     else
293       {
294         SetStatusText(_T("The current page is not a bbs file : cannot execute it"));
295       }
296
297   }
298   //================================================================  
299
300   //================================================================
301   void WxGUIConsole::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
302   {
303     Close(true);
304   }
305   //================================================================
306
307
308   //================================================================
309   void WxGUIConsole::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
310   {
311     m_mgr.Update();
312         Refresh();
313     wxMessageBox(_T("  bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS 2008"),
314                  _T("About ..."), wxOK | wxICON_INFORMATION,
315                  this);
316   }
317   //================================================================
318
319
320   //================================================================
321   void WxGUIConsole::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
322   {
323         std::string commandStr;
324     std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
325 #ifdef WIN32
326         commandStr = "notepad.exe ";
327 #else
328         commandStr = "gedit ";
329 #endif 
330         commandStr = commandStr + configFile;
331         std::cout << "system: " << commandStr << std::endl;
332         system ( commandStr.c_str() );
333   }
334   //================================================================
335
336
337   //================================================================
338   void WxGUIConsole::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
339   {
340     std::string command("toolsbbtk/appli/GUICreatePackage");
341
342     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
343     Interpreter::Pointer I = Interpreter::New();    
344     I->InterpretFile(command);
345   }
346   //================================================================
347
348
349   //================================================================
350   void WxGUIConsole::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
351   {
352     std::string command("toolsbbtk/appli/GUICreateBlackBox");
353     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
354     Interpreter::Pointer I = Interpreter::New();    
355     I->InterpretFile(command);
356   }
357   //================================================================
358   
359   //================================================================
360   void WxGUIConsole::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
361   {
362     std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
363
364 #if defined(WIN32)
365     std::string strappli="start ";
366 #else
367     std::string strappli="gnome-open ";
368 #endif
369     std::string strcommand0 = "cd \"" + default_temp_dir+"/temp_dir/" +"\"";
370     std::string strcommand1 = strappli + "workspace_workspacePrototype.png";
371     std::string strcommand = strcommand0 + " && " + strcommand1;
372         std::cout << "system: " << strcommand << std::endl;
373     system ( strcommand.c_str() );
374
375   }
376   //================================================================
377
378   //================================================================
379   void WxGUIConsole::OnMenuShowHTMLDoc(wxCommandEvent& WXUNUSED(event))
380   {
381         std::string doc_path            = ConfigurationFile::GetInstance().Get_doc_path();
382
383 #if defined(WIN32)
384     std::string strappli="start ";
385 #else
386     std::string strappli="gnome-open ";
387 #endif
388     std::string strcommand0 = "cd \"" + doc_path+"\"";
389     std::string strcommand1 = strappli + "index.html";
390     std::string strcommand = strcommand0 + " && " + strcommand1;
391         std::cout << "system: " << strcommand << std::endl;
392     system ( strcommand.c_str() );
393
394   }
395   //================================================================
396
397
398
399   //================================================================
400   void WxGUIConsole::OnMenuCreateIndex(wxCommandEvent& WXUNUSED(event))
401   {
402 /*
403     std::string doc_path = ConfigurationFile::GetInstance().Get_doc_path();
404     std::string filepath = doc_path+"/bbdoc/make-index.bbs";
405     Interpreter* I = new Interpreter;    
406     I->InterpretLine( "exec freeze");
407     I->InterpretLine( "include *");
408     I->InterpretLine( "help packages");
409     I->InterpretLine( "index "+doc_path+"/bbdoc/index-alpha.html Initials");
410     I->InterpretLine( "index "+doc_path+"/bbdoc/index-package.html Packages");
411     I->InterpretLine( "index "+doc_path+"/bbdoc/index-category.html Categories");
412     I->InterpretLine( "index "+doc_path+"/bbdoc/index-adaptors.html Adaptors");    
413     delete I;
414 */
415
416         std::string bin_path            = ConfigurationFile::GetInstance().Get_bin_path();
417         std::string doc_path            = ConfigurationFile::GetInstance().Get_doc_path();
418         std::string bbdoc_path          = doc_path+"/bbdoc";
419     std::string make_index_path = "\"" + bbdoc_path+"/make-index.bbs\"";
420
421         std::string strcommand0         = "cd "+bbdoc_path+" && mkdir tmp && cd tmp";
422
423         std::string strcommand1         = "cd "+bbdoc_path+"/tmp";
424     std::string strcommand2             = "\""+bin_path+"/bbi\" -N "+make_index_path;
425 #ifdef WIN32 
426     std::string strcommand3             = "move index*.html ../.";
427 #else
428     std::string strcommand3             = "mv index*.html ../.";
429 #endif
430
431         std::string strcommand          =       strcommand1 +" && "+
432                                                                         strcommand2 +" && "+
433                                                                         strcommand3;
434
435         std::cout << "system: " << strcommand0 << std::endl;
436         std::cout << "system: " << strcommand << std::endl;
437
438         system ( strcommand0.c_str() );
439         system ( strcommand.c_str() );
440   }
441   //================================================================
442
443
444   //================================================================
445   void WxGUIConsole::InterpreterUserViewHtmlPage(const std::string& page)
446   {
447     std::string s(page);
448     //  std::cout << "WxGUIConsole::ShowHtmlPage('"<<page<<"')"<<std::endl;
449     if (mWxGUIHtmlBrowser->GoTo(s)) 
450       {
451 //EED   mwxNotebook->ChangeSelection(1);
452         mwxNotebook->SetSelection(1);
453       }
454     else 
455       {
456          // std::cout << "ERROR html"<<std::endl;
457       }
458   } 
459   //================================================================  
460   
461   //================================================================  
462   // Overloaded Show method to handle bbtk::Wx::TopWindow auto-destruction
463   // OBSOLETE !!!
464   bool WxGUIConsole::Show(bool show)
465   {
466     //    bbtk::Wx::SetAutoDestroyTopWindow(!show);
467     return wxFrame::Show(show);
468   }
469   //================================================================  
470    
471   //================================================================  
472   BEGIN_EVENT_TABLE(WxGUIConsole, wxFrame)
473     EVT_CLOSE(  WxGUIConsole::OnClose)
474     EVT_MENU(ID_Menu_Quit                       , WxGUIConsole::OnMenuQuit)
475     EVT_MENU(ID_Menu_About                      , WxGUIConsole::OnMenuAbout)
476     EVT_MENU(ID_Menu_EditConfig         , WxGUIConsole::OnMenuEditConfig)
477     EVT_MENU(ID_Menu_CreatePackage      , WxGUIConsole::OnMenuCreatePackage)
478     EVT_MENU(ID_Menu_CreateBlackBox     , WxGUIConsole::OnMenuCreateBlackBox)
479     EVT_MENU(ID_Menu_ShowImageGraph     , WxGUIConsole::OnMenuShowImageGraph)
480     EVT_MENU(ID_Menu_ShowHTMLDoc        , WxGUIConsole::OnMenuShowHTMLDoc)
481     EVT_MENU(ID_Menu_CreateIndex        , WxGUIConsole::OnMenuCreateIndex)
482     EVT_BUTTON(ID_Button_Run            , WxGUIConsole::OnButtonRun )
483     END_EVENT_TABLE()
484   //================================================================
485
486 } // namespace bbtk
487
488
489 #endif //_USE_WXWIDGETS_