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