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