]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxGUIConsole.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkWxGUIConsole.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkWxGUIConsole.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/04/24 10:11:28 $
7   Version:   $Revision: 1.9 $
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 "bbtkWxGUIConsole.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   WxGUIConsole::WxGUIConsole( 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 = bbtk::Interpreter::New();
68     mInterpreter->SetUser(this);
69     mInterpreter->SetCommandLine(true);
70     mInterpreter->SetThrow(false);
71     //==============
72     // Menu
73     wxInitAllImageHandlers();
74     
75     wxMenu *menuFile = new wxMenu;
76     menuFile->Append( ID_Menu_Quit, _T("&Quit") );
77     
78     wxMenu *menuAbout = new wxMenu;
79     menuAbout->Append( ID_Menu_About, _T("&About...") );
80
81     wxMenu *menuTools = new wxMenu;
82     menuTools->Append( ID_Menu_EditConfig, _T("&Edit bbtk config") );
83     menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
84     menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &blackbox") );
85     menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
86     menuTools->Append( ID_Menu_CreateIndex, _T("&Generate index") );
87     
88     
89     wxMenuBar *menuBar = new wxMenuBar;
90     menuBar->Append( menuFile, _T("&File") );
91     menuBar->Append( menuTools, _T("&Tools") );
92     menuBar->Append( menuAbout, _T("About") );
93     
94     SetMenuBar( menuBar );
95     
96     CreateStatusBar();
97     SetStatusText( _T("Welcome to bbi !") );
98     
99     //==============
100     // Notebook
101     
102     //    wxFlexGridSizer *sizer = new wxFlexGridSizer(1);
103     
104 //EED    wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
105 //    mwxNotebook = new wxNotebook(this,-1,wxDefaultPosition, wxDefaultSize, 0);
106     mwxNotebook = new wxAuiNotebook(this,  
107                                     -1,
108                                     wxPoint(0, 0),
109                                     wxSize(500,500),
110                                     wxAUI_NB_TAB_SPLIT | wxAUI_NB_TAB_EXTERNAL_MOVE | wxNO_BORDER);
111     
112     mwxPageCommand = new wxPanel(mwxNotebook,-1);        
113     mwxPageHelp = new wxPanel(mwxNotebook,-1);    
114     
115     
116     wxBoxSizer *cmdsizer = new wxBoxSizer(wxVERTICAL);
117     
118     mwxPageCommand->SetAutoLayout(true);    
119     mwxPageCommand->SetSizer(cmdsizer);
120     cmdsizer->Fit(mwxPageCommand);
121     cmdsizer->SetSizeHints(mwxPageCommand);
122
123     wxBoxSizer *helpsizer = new wxBoxSizer(wxVERTICAL);
124     
125     mwxPageHelp->SetAutoLayout(true);    
126     mwxPageHelp->SetSizer(helpsizer);
127     helpsizer->Fit(mwxPageHelp);
128     helpsizer->SetSizeHints(mwxPageHelp);
129    
130     mWxGUIHtmlBrowser = new WxGUIHtmlBrowser(mwxPageHelp,
131                                              //EED                                wxSize(1200,0));
132                                              wxSize(200,0));
133
134     //    mWxGUIHtmlBrowser->SetSize(wxSize(800,1000));
135     helpsizer->Add (mWxGUIHtmlBrowser,1, wxGROW|wxLEFT|wxRIGHT|wxTOP, 5  );
136 //    helpsizer->Add ( new wxButton(mwxPageHelp,-1,"perro"), 0,  wxEXPAND  );
137     wxBitmap bmp_run(cc_run_xpm);
138     mwxButtonRun = new wxBitmapButton( mwxPageHelp,ID_Button_Run,bmp_run);//_T("Run")  );
139     helpsizer->Add( mwxButtonRun, 0, wxALL, 5  );
140
141   
142     //==============
143     // Command page 
144     mWxGUIOutputMessages = new WxGUIOutputMessages(mwxPageCommand);
145
146     mWxGUICommand = new WxGUICommand(mwxPageCommand,this);
147    
148     mWxGUICommand->SetFocus();
149
150     cmdsizer->Add (mWxGUIOutputMessages, 1, wxALL | wxGROW, 5);
151     cmdsizer->Add (mWxGUICommand, 0, wxALL | wxGROW, 5);
152
153         
154     // Creates the parent window of all bbtk windows as a child of this
155     Wx::CreateTopWindow(this,true);
156     // Add the method OnWxSignal as a Wx::Signal observer 
157     bbtkAddWxSignalObserver(WxGUIConsole::OnWxSignal);
158
159
160     // Layout
161 //EED    SetSizer(sizer);
162
163     mwxNotebook->AddPage( mwxPageCommand, _T("Command"));
164     mwxNotebook->AddPage( mwxPageHelp, _T("Help"));
165     m_mgr.AddPane(mwxNotebook, wxAuiPaneInfo().Name(wxT("notebook_content")).CenterPane().PaneBorder(false)); 
166     m_mgr.Update();
167         
168     SetAutoLayout(true);
169     Layout();
170 //    Refresh();
171 //    m_mgr.Update();
172   }
173   //================================================================
174
175  //================================================================
176   WxGUIConsole::~WxGUIConsole()
177   {
178     m_mgr.UnInit();
179   }
180   //================================================================
181
182   //================================================================
183   void WxGUIConsole::OnWxSignal()
184   {
185     if ((!Wx::TopWindowExists())&&(!IsShown())) 
186       {
187         bbtkDebugMessage("wx",2,"  --> bbtk top window destroyed and WxGUIConsole not shown => destructing"<<std::endl);
188         Close();
189       }
190   }
191   //================================================================
192
193   //================================================================
194   void WxGUIConsole::WxGUICommandEnter(const std::string& command)
195   {
196     std::string s("> ");
197     s += command + "\n";
198     mWxGUIOutputMessages->Print(s,wxRED);
199
200     if (  mInterpreter->InterpretLine( command ) == 
201           Interpreter::Interpreter_QUIT )
202       {
203         Close(true); 
204       }
205   }
206   //================================================================
207
208   //================================================================
209   /// Runs the interpretation of a file
210   bool WxGUIConsole::InterpretFile( const std::string& filename) 
211   { 
212     if ( mInterpreter->InterpretFile(filename) ==
213          Interpreter::Interpreter_ERROR ) 
214       {
215         return false;
216       }
217     return true;
218   }
219   //================================================================
220  
221   //================================================================
222   void WxGUIConsole::OnButtonRun(wxCommandEvent& WXUNUSED(event))
223   {
224 //    wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
225     std::string filename = mWxGUIHtmlBrowser->GetCurrentPage();//wx2std(temp);
226     size_t s = filename.length();
227
228     Interpreter::Pointer I = Interpreter::New();
229     
230     if ((s>3) && (filename[s-1]=='s')
231         && (filename[s-2]=='b')
232         && (filename[s-3]=='b')
233         && (filename[s-4]=='.'))
234       {
235         std::string tmp("Executing ");
236         tmp += filename;
237         SetStatusText(std2wx(tmp));
238         I->InterpretFile(filename);
239       }
240     else
241       {
242         SetStatusText(_T("The current page is not a bbs file : cannot execute it"));
243       }
244
245   }
246   //================================================================  
247
248   //================================================================
249   void WxGUIConsole::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
250   {
251     Close(true);
252   }
253   //================================================================
254
255
256   //================================================================
257   void WxGUIConsole::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
258   {
259     m_mgr.Update();
260         Refresh();
261     wxMessageBox(_T("  bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS-LRMN 2008"),
262                  _T("About ..."), wxOK | wxICON_INFORMATION,
263                  this);
264   }
265   //================================================================
266
267
268   //================================================================
269   void WxGUIConsole::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
270   {
271         std::string commandStr;
272     std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
273 #ifdef WIN32
274         commandStr = "notepad.exe ";
275 #else
276         commandStr = "gedit ";
277 #endif 
278         commandStr = commandStr + configFile;
279         std::cout << "system: " << commandStr << std::endl;
280         system ( commandStr.c_str() );
281   }
282   //================================================================
283
284
285   //================================================================
286   void WxGUIConsole::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
287   {
288     std::string command("toolsbbtk/appli/CreatePackage");
289
290     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
291     Interpreter::Pointer I = Interpreter::New();    
292     I->InterpretFile(command);
293   }
294   //================================================================
295
296
297   //================================================================
298   void WxGUIConsole::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
299   {
300     std::string command("toolsbbtk/appli/CreateBlackBox");
301     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
302     Interpreter::Pointer I = Interpreter::New();    
303     I->InterpretFile(command);
304   }
305   //================================================================
306   
307   //================================================================
308   void WxGUIConsole::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
309   {
310     std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
311
312 #if defined(WIN32)
313     std::string strappli="start ";
314 #else
315     std::string strappli="gnome-open ";
316 #endif
317     std::string strcommand = strappli +default_temp_dir+"/temp_dir/workspace_workspacePrototype.png";
318         std::cout << "system: " << strcommand << std::endl;
319     system ( strcommand.c_str() );
320
321   }
322   //================================================================
323
324
325   //================================================================
326   void WxGUIConsole::OnMenuCreateIndex(wxCommandEvent& WXUNUSED(event))
327   {
328 /*
329     std::string doc_path = ConfigurationFile::GetInstance().Get_doc_path();
330     std::string filepath = doc_path+"/bbdoc/make-index.bbs";
331     Interpreter* I = new Interpreter;    
332     I->InterpretLine( "exec freeze");
333     I->InterpretLine( "include *");
334     I->InterpretLine( "help packages");
335     I->InterpretLine( "index "+doc_path+"/bbdoc/index-alpha.html Initials");
336     I->InterpretLine( "index "+doc_path+"/bbdoc/index-package.html Packages");
337     I->InterpretLine( "index "+doc_path+"/bbdoc/index-category.html Categories");
338     I->InterpretLine( "index "+doc_path+"/bbdoc/index-adaptors.html Adaptors");    
339     delete I;
340 */
341
342         std::string bin_path            = ConfigurationFile::GetInstance().Get_bin_path();
343         std::string doc_path            = ConfigurationFile::GetInstance().Get_doc_path();
344         std::string bbdoc_path          = doc_path+"/bbdoc";
345     std::string make_index_path = "\"" + bbdoc_path+"/make-index.bbs\"";
346
347         std::string strcommand0         = "cd "+bbdoc_path+" && mkdir tmp && cd tmp";
348
349         std::string strcommand1         = "cd "+bbdoc_path+"/tmp";
350     std::string strcommand2             = "\""+bin_path+"/bbi\" -n "+make_index_path;
351 #ifdef WIN32 
352     std::string strcommand3             = "move index*.html ../.";
353 #else
354     std::string strcommand3             = "mv index*.html ../.";
355 #endif
356
357         std::string strcommand          =       strcommand1 +" && "+
358                                                                         strcommand2 +" && "+
359                                                                         strcommand3;
360
361         std::cout << "system: " << strcommand0 << std::endl;
362         std::cout << "system: " << strcommand << std::endl;
363
364         system ( strcommand0.c_str() );
365         system ( strcommand.c_str() );
366   }
367   //================================================================
368
369
370   //================================================================
371   void WxGUIConsole::InterpreterUserViewHtmlPage(const std::string& page)
372   {
373     std::string s(page);
374     //  std::cout << "WxGUIConsole::ShowHtmlPage('"<<page<<"')"<<std::endl;
375     if (mWxGUIHtmlBrowser->GoTo(s)) 
376       {
377 //EED   mwxNotebook->ChangeSelection(1);
378         mwxNotebook->SetSelection(1);
379       }
380     else 
381       {
382          // std::cout << "ERROR html"<<std::endl;
383       }
384   } 
385   //================================================================  
386   
387   //================================================================  
388   // Overloaded Show method to handle bbtk::Wx::TopWindow auto-destruction
389   bool WxGUIConsole::Show(bool show)
390   {
391     Wx::AutoDestroyTopWindow(!show);
392     wxFrame::Show(show);
393   }
394   //================================================================  
395    
396   //================================================================  
397   BEGIN_EVENT_TABLE(WxGUIConsole, wxFrame)
398     EVT_MENU(ID_Menu_Quit, WxGUIConsole::OnMenuQuit)
399     EVT_MENU(ID_Menu_About, WxGUIConsole::OnMenuAbout)
400     EVT_MENU(ID_Menu_EditConfig, WxGUIConsole::OnMenuEditConfig)
401     EVT_MENU(ID_Menu_CreatePackage, WxGUIConsole::OnMenuCreatePackage)
402     EVT_MENU(ID_Menu_CreateBlackBox, WxGUIConsole::OnMenuCreateBlackBox)
403     EVT_MENU(ID_Menu_ShowImageGraph, WxGUIConsole::OnMenuShowImageGraph)
404     EVT_MENU(ID_Menu_CreateIndex, WxGUIConsole::OnMenuCreateIndex)
405     EVT_BUTTON(ID_Button_Run, WxGUIConsole::OnButtonRun )
406     END_EVENT_TABLE()
407   //================================================================
408
409 } // namespace bbtk
410
411
412 #endif //_USE_WXWIDGETS_