]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxGUIConsole.cxx
51abe49e8fbb755dfe3936f6035e874912a6c4ef
[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 printf( "EED WxGUIConsole Start \n" );
88 //    m_mgr = new wxAuiManager(this);
89         m_mgr.SetManagedWindow(this);
90 printf( "EED WxGUIConsole 0.1 \n" );
91    
92         mInterpreter = bbtk::Interpreter::New();
93 printf( "EED WxGUIConsole 0.2 \n" );
94     mInterpreter->SetUser(this);
95 printf( "EED WxGUIConsole 0.3 \n" );
96     mInterpreter->SetCommandLine(true);
97 printf( "EED WxGUIConsole 0.4 \n" );
98     mInterpreter->SetThrow(false);
99 printf( "EED WxGUIConsole 1\n" );
100         
101     //==============
102     // Menu
103     wxInitAllImageHandlers();
104     
105     wxMenu *menuFile = new wxMenu;
106     menuFile->Append( ID_Menu_Quit, _T("&Quit") );
107     
108     wxMenu *menuAbout = new wxMenu;
109     menuAbout->Append( ID_Menu_About, _T("&About...") );
110
111     wxMenu *menuTools = new wxMenu;
112     menuTools->Append( ID_Menu_EditConfig, _T("&Edit bbtk config") );
113     menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
114     menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &blackbox") );
115     menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
116     menuTools->Append( ID_Menu_ShowHTMLDoc, _T("Show &HTML documentation") );
117     menuTools->Append( ID_Menu_CreateIndex, _T("&Generate index") );
118     
119     
120     wxMenuBar *menuBar = new wxMenuBar;
121     menuBar->Append( menuFile, _T("&File") );
122     menuBar->Append( menuTools, _T("&Tools") );
123     menuBar->Append( menuAbout, _T("About") );
124     
125     SetMenuBar( menuBar );
126     
127     CreateStatusBar();
128     SetStatusText( _T("Welcome to bbi !") );
129 printf( "EED WxGUIConsole 2\n" );
130     
131     //==============
132     // Notebook
133     
134     //    wxFlexGridSizer *sizer = new wxFlexGridSizer(1);
135     
136 //EED    wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
137 //    mwxNotebook = new wxNotebook(this,-1,wxDefaultPosition, wxDefaultSize, 0);
138     mwxNotebook = new wxAuiNotebook(this,  
139                                     -1,
140                                     wxPoint(0, 0),
141                                     wxSize(500,500),
142                                     wxAUI_NB_TAB_SPLIT | wxAUI_NB_TAB_EXTERNAL_MOVE | wxNO_BORDER);
143     
144     mwxPageCommand = new wxPanel(mwxNotebook,-1);        
145     mwxPageHelp = new wxPanel(mwxNotebook,-1);    
146     
147     
148     wxBoxSizer *cmdsizer = new wxBoxSizer(wxVERTICAL);
149     
150     mwxPageCommand->SetAutoLayout(true);    
151     mwxPageCommand->SetSizer(cmdsizer);
152     cmdsizer->Fit(mwxPageCommand);
153     cmdsizer->SetSizeHints(mwxPageCommand);
154
155     wxBoxSizer *helpsizer = new wxBoxSizer(wxVERTICAL);
156     
157     mwxPageHelp->SetAutoLayout(true);    
158     mwxPageHelp->SetSizer(helpsizer);
159     helpsizer->Fit(mwxPageHelp);
160     helpsizer->SetSizeHints(mwxPageHelp);
161   printf( "EED WxGUIConsole 3\n" );
162  
163     mWxGUIHtmlBrowser = new WxGUIHtmlBrowser(mwxPageHelp,
164                                              //EED                                wxSize(1200,0));
165                                              wxSize(200,0));
166
167     //    mWxGUIHtmlBrowser->SetSize(wxSize(800,1000));
168     helpsizer->Add (mWxGUIHtmlBrowser,1, wxGROW|wxLEFT|wxRIGHT|wxTOP, 5  );
169 //    helpsizer->Add ( new wxButton(mwxPageHelp,-1,"perro"), 0,  wxEXPAND  );
170     wxBitmap bmp_run(cc_run_xpm);
171     mwxButtonRun = new wxBitmapButton( mwxPageHelp,ID_Button_Run,bmp_run);//_T("Run")  );
172     helpsizer->Add( mwxButtonRun, 0, wxALL, 5  );
173
174   
175     //==============
176     // Command page 
177     mWxGUIOutputMessages = new WxGUIOutputMessages(mwxPageCommand);
178
179     mWxGUICommand = new WxGUICommand(mwxPageCommand,this);
180    
181     mWxGUICommand->SetFocus();
182
183     cmdsizer->Add (mWxGUIOutputMessages, 1, wxALL | wxGROW, 5);
184     cmdsizer->Add (mWxGUICommand, 0, wxALL | wxGROW, 5);
185
186 printf( "EED WxGUIConsole 4\n" );
187         
188     // Set the parent window of all bbtk windows as a child of this
189     //    bbtk::Wx::SetTopWindowParent(this);
190     bbtk::Wx::SetTopWindow(this);
191     // Top Window Auto Destroys when no more black box window alive 
192     // : this is the default 
193     //    bbtk::Wx::SetAutoDestroyTopWindow(true);
194     // Add the method OnWxSignal as a Wx::Signal observer 
195     bbtkAddWxSignalObserver(WxGUIConsole::OnWxSignal);
196
197
198     // Layout
199 //EED    SetSizer(sizer);
200
201     mwxNotebook->AddPage( mwxPageCommand, _T("Command"));
202     mwxNotebook->AddPage( mwxPageHelp, _T("Help"));
203     m_mgr.AddPane(mwxNotebook, wxAuiPaneInfo().Name(wxT("notebook_content")).CenterPane().PaneBorder(false)); 
204     m_mgr.Update();
205         
206     SetAutoLayout(true);
207     Layout();
208 //    Refresh();
209 //    m_mgr.Update();
210 printf( "EED WxGUIConsole End\n" );
211
212   }
213   //================================================================
214
215  //================================================================
216   WxGUIConsole::~WxGUIConsole()
217   {
218     m_mgr.UnInit();
219   }
220   //================================================================
221
222   //================================================================
223   void WxGUIConsole::OnClose(wxCloseEvent& event)
224   {
225     bbtkDebugMessage("widget",9,"bbi::OnClose()"<<std::endl);
226     //Wx::SetWindowsHaveBeenDestroyedByParent();
227     //mInterpreter.reset();
228     wxWindowList& list = GetChildren();
229     wxWindowList::iterator iter;
230     for (iter = list.begin(); iter != list.end(); ++iter)
231       {
232         (*iter)->Close();
233       }
234     this->Destroy();
235   }
236   //================================================================
237
238   //================================================================
239   void WxGUIConsole::OnWxSignal()
240   {
241     if ((!bbtk::Wx::IsSomeWindowAlive())&&(!IsShown())) 
242       {
243         bbtkDebugMessage("wx",2,"  --> bbtk top window destroyed and WxGUIConsole not shown => destructing"<<std::endl);
244         /*
245         wxWindowList& list = GetChildren();
246         wxWindowList::iterator iter;
247         for (iter = list.begin(); iter != list.end(); ++iter)
248           {
249             (*iter)->Close();
250           }
251         */
252         Close();
253       }
254   }
255   //================================================================
256
257   //================================================================
258   void WxGUIConsole::WxGUICommandEnter(const std::string& command)
259   {
260     std::string s("> ");
261     s += command + "\n";
262     mWxGUIOutputMessages->Print(s,wxRED);
263
264     if (  mInterpreter->InterpretLine( command ) == 
265           Interpreter::Interpreter_QUIT )
266       {
267         Close(true); 
268       }
269   }
270   //================================================================
271
272   //================================================================
273   /// Runs the interpretation of a file
274   bool WxGUIConsole::InterpretFile( const std::string& filename) 
275   { 
276     if ( mInterpreter->InterpretFile(filename) ==
277          Interpreter::Interpreter_ERROR ) 
278       {
279         return false;
280       }
281     return true;
282   }
283   //================================================================
284  
285   //================================================================
286   void WxGUIConsole::OnButtonRun(wxCommandEvent& WXUNUSED(event))
287   {
288 //    wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
289     std::string filename = mWxGUIHtmlBrowser->GetCurrentPage();//wx2std(temp);
290     size_t s = filename.length();
291
292     Interpreter::Pointer I = Interpreter::New();
293     
294     if ((s>3) && (filename[s-1]=='s')
295         && (filename[s-2]=='b')
296         && (filename[s-3]=='b')
297         && (filename[s-4]=='.'))
298       {
299         std::string tmp("Executing ");
300         tmp += filename;
301         SetStatusText(std2wx(tmp));
302         I->InterpretFile(filename);
303       }
304     else
305       {
306         SetStatusText(_T("The current page is not a bbs file : cannot execute it"));
307       }
308
309   }
310   //================================================================  
311
312   //================================================================
313   void WxGUIConsole::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
314   {
315     Close(true);
316   }
317   //================================================================
318
319
320   //================================================================
321   void WxGUIConsole::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
322   {
323     m_mgr.Update();
324         Refresh();
325     wxMessageBox(_T("  bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS 2008"),
326                  _T("About ..."), wxOK | wxICON_INFORMATION,
327                  this);
328   }
329   //================================================================
330
331
332   //================================================================
333   void WxGUIConsole::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
334   {
335         std::string commandStr;
336     std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
337 #ifdef WIN32
338         commandStr = "notepad.exe ";
339 #else
340         commandStr = "gedit ";
341 #endif 
342         commandStr = commandStr + configFile;
343         std::cout << "system: " << commandStr << std::endl;
344         system ( commandStr.c_str() );
345   }
346   //================================================================
347
348
349   //================================================================
350   void WxGUIConsole::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
351   {
352     std::string command("toolsbbtk/appli/GUICreatePackage");
353
354     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
355     Interpreter::Pointer I = Interpreter::New();    
356     I->InterpretFile(command);
357   }
358   //================================================================
359
360
361   //================================================================
362   void WxGUIConsole::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
363   {
364     std::string command("toolsbbtk/appli/GUICreateBlackBox");
365     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
366     Interpreter::Pointer I = Interpreter::New();    
367     I->InterpretFile(command);
368   }
369   //================================================================
370   
371   //================================================================
372   void WxGUIConsole::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
373   {
374     std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
375
376 #if defined(WIN32)
377     std::string strappli="start ";
378 #else
379     std::string strappli="gnome-open ";
380 #endif
381     std::string strcommand0 = "cd \"" + default_temp_dir+"/temp_dir/" +"\"";
382     std::string strcommand1 = strappli + "workspace_workspacePrototype.png";
383     std::string strcommand = strcommand0 + " && " + strcommand1;
384         std::cout << "system: " << strcommand << std::endl;
385     system ( strcommand.c_str() );
386
387   }
388   //================================================================
389
390   //================================================================
391   void WxGUIConsole::OnMenuShowHTMLDoc(wxCommandEvent& WXUNUSED(event))
392   {
393         std::string doc_path            = ConfigurationFile::GetInstance().Get_doc_path();
394
395 #if defined(WIN32)
396     std::string strappli="start ";
397 #else
398     std::string strappli="gnome-open ";
399 #endif
400     std::string strcommand0 = "cd \"" + doc_path+"\"";
401     std::string strcommand1 = strappli + "index.html";
402     std::string strcommand = strcommand0 + " && " + strcommand1;
403         std::cout << "system: " << strcommand << std::endl;
404     system ( strcommand.c_str() );
405
406   }
407   //================================================================
408
409
410
411   //================================================================
412   void WxGUIConsole::OnMenuCreateIndex(wxCommandEvent& WXUNUSED(event))
413   {
414 /*
415     std::string doc_path = ConfigurationFile::GetInstance().Get_doc_path();
416     std::string filepath = doc_path+"/bbdoc/make-index.bbs";
417     Interpreter* I = new Interpreter;    
418     I->InterpretLine( "exec freeze");
419     I->InterpretLine( "include *");
420     I->InterpretLine( "help packages");
421     I->InterpretLine( "index "+doc_path+"/bbdoc/index-alpha.html Initials");
422     I->InterpretLine( "index "+doc_path+"/bbdoc/index-package.html Packages");
423     I->InterpretLine( "index "+doc_path+"/bbdoc/index-category.html Categories");
424     I->InterpretLine( "index "+doc_path+"/bbdoc/index-adaptors.html Adaptors");    
425     delete I;
426 */
427
428         std::string bin_path            = ConfigurationFile::GetInstance().Get_bin_path();
429         std::string doc_path            = ConfigurationFile::GetInstance().Get_doc_path();
430         std::string bbdoc_path          = doc_path+"/bbdoc";
431     std::string make_index_path = "\"" + bbdoc_path+"/make-index.bbs\"";
432
433         std::string strcommand0         = "cd "+bbdoc_path+" && mkdir tmp && cd tmp";
434
435         std::string strcommand1         = "cd "+bbdoc_path+"/tmp";
436     std::string strcommand2             = "\""+bin_path+"/bbi\" -N "+make_index_path;
437 #ifdef WIN32 
438     std::string strcommand3             = "move index*.html ../.";
439 #else
440     std::string strcommand3             = "mv index*.html ../.";
441 #endif
442
443         std::string strcommand          =       strcommand1 +" && "+
444                                                                         strcommand2 +" && "+
445                                                                         strcommand3;
446
447         std::cout << "system: " << strcommand0 << std::endl;
448         std::cout << "system: " << strcommand << std::endl;
449
450         system ( strcommand0.c_str() );
451         system ( strcommand.c_str() );
452   }
453   //================================================================
454
455
456   //================================================================
457   void WxGUIConsole::InterpreterUserViewHtmlPage(const std::string& page)
458   {
459     std::string s(page);
460     //  std::cout << "WxGUIConsole::ShowHtmlPage('"<<page<<"')"<<std::endl;
461     if (mWxGUIHtmlBrowser->GoTo(s)) 
462       {
463 //EED   mwxNotebook->ChangeSelection(1);
464         mwxNotebook->SetSelection(1);
465       }
466     else 
467       {
468          // std::cout << "ERROR html"<<std::endl;
469       }
470   } 
471   //================================================================  
472   
473   //================================================================  
474   // Overloaded Show method to handle bbtk::Wx::TopWindow auto-destruction
475   // OBSOLETE !!!
476   bool WxGUIConsole::Show(bool show)
477   {
478     //    bbtk::Wx::SetAutoDestroyTopWindow(!show);
479     return wxFrame::Show(show);
480   }
481   //================================================================  
482    
483   //================================================================  
484   BEGIN_EVENT_TABLE(WxGUIConsole, wxFrame)
485     EVT_CLOSE(  WxGUIConsole::OnClose)
486     EVT_MENU(ID_Menu_Quit                       , WxGUIConsole::OnMenuQuit)
487     EVT_MENU(ID_Menu_About                      , WxGUIConsole::OnMenuAbout)
488     EVT_MENU(ID_Menu_EditConfig         , WxGUIConsole::OnMenuEditConfig)
489     EVT_MENU(ID_Menu_CreatePackage      , WxGUIConsole::OnMenuCreatePackage)
490     EVT_MENU(ID_Menu_CreateBlackBox     , WxGUIConsole::OnMenuCreateBlackBox)
491     EVT_MENU(ID_Menu_ShowImageGraph     , WxGUIConsole::OnMenuShowImageGraph)
492     EVT_MENU(ID_Menu_ShowHTMLDoc        , WxGUIConsole::OnMenuShowHTMLDoc)
493     EVT_MENU(ID_Menu_CreateIndex        , WxGUIConsole::OnMenuCreateIndex)
494     EVT_BUTTON(ID_Button_Run            , WxGUIConsole::OnButtonRun )
495     END_EVENT_TABLE()
496   //================================================================
497
498 } // namespace bbtk
499
500
501 #endif //_USE_WXWIDGETS_