]> 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/06/19 09:46:41 $
7   Version:   $Revision: 1.13 $
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     // Creates the parent window of all bbtk windows as a child of this
157     Wx::CreateTopWindow(this,true);
158     // Add the method OnWxSignal as a Wx::Signal observer 
159     bbtkAddWxSignalObserver(WxGUIConsole::OnWxSignal);
160
161
162     // Layout
163 //EED    SetSizer(sizer);
164
165     mwxNotebook->AddPage( mwxPageCommand, _T("Command"));
166     mwxNotebook->AddPage( mwxPageHelp, _T("Help"));
167     m_mgr.AddPane(mwxNotebook, wxAuiPaneInfo().Name(wxT("notebook_content")).CenterPane().PaneBorder(false)); 
168     m_mgr.Update();
169         
170     SetAutoLayout(true);
171     Layout();
172 //    Refresh();
173 //    m_mgr.Update();
174   }
175   //================================================================
176
177  //================================================================
178   WxGUIConsole::~WxGUIConsole()
179   {
180     m_mgr.UnInit();
181   }
182   //================================================================
183
184   //================================================================
185   void WxGUIConsole::OnWxSignal()
186   {
187     if ((!Wx::TopWindowExists())&&(!IsShown())) 
188       {
189         bbtkDebugMessage("wx",2,"  --> bbtk top window destroyed and WxGUIConsole not shown => destructing"<<std::endl);
190         Close();
191       }
192   }
193   //================================================================
194
195   //================================================================
196   void WxGUIConsole::WxGUICommandEnter(const std::string& command)
197   {
198     std::string s("> ");
199     s += command + "\n";
200     mWxGUIOutputMessages->Print(s,wxRED);
201
202     if (  mInterpreter->InterpretLine( command ) == 
203           Interpreter::Interpreter_QUIT )
204       {
205         Close(true); 
206       }
207   }
208   //================================================================
209
210   //================================================================
211   /// Runs the interpretation of a file
212   bool WxGUIConsole::InterpretFile( const std::string& filename) 
213   { 
214     if ( mInterpreter->InterpretFile(filename) ==
215          Interpreter::Interpreter_ERROR ) 
216       {
217         return false;
218       }
219     return true;
220   }
221   //================================================================
222  
223   //================================================================
224   void WxGUIConsole::OnButtonRun(wxCommandEvent& WXUNUSED(event))
225   {
226 //    wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
227     std::string filename = mWxGUIHtmlBrowser->GetCurrentPage();//wx2std(temp);
228     size_t s = filename.length();
229
230     Interpreter::Pointer I = Interpreter::New();
231     
232     if ((s>3) && (filename[s-1]=='s')
233         && (filename[s-2]=='b')
234         && (filename[s-3]=='b')
235         && (filename[s-4]=='.'))
236       {
237         std::string tmp("Executing ");
238         tmp += filename;
239         SetStatusText(std2wx(tmp));
240         I->InterpretFile(filename);
241       }
242     else
243       {
244         SetStatusText(_T("The current page is not a bbs file : cannot execute it"));
245       }
246
247   }
248   //================================================================  
249
250   //================================================================
251   void WxGUIConsole::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
252   {
253     Close(true);
254   }
255   //================================================================
256
257
258   //================================================================
259   void WxGUIConsole::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
260   {
261     m_mgr.Update();
262         Refresh();
263     wxMessageBox(_T("  bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS-LRMN 2008"),
264                  _T("About ..."), wxOK | wxICON_INFORMATION,
265                  this);
266   }
267   //================================================================
268
269
270   //================================================================
271   void WxGUIConsole::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
272   {
273         std::string commandStr;
274     std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
275 #ifdef WIN32
276         commandStr = "notepad.exe ";
277 #else
278         commandStr = "gedit ";
279 #endif 
280         commandStr = commandStr + configFile;
281         std::cout << "system: " << commandStr << std::endl;
282         system ( commandStr.c_str() );
283   }
284   //================================================================
285
286
287   //================================================================
288   void WxGUIConsole::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
289   {
290     std::string command("toolsbbtk/appli/GUICreatePackage");
291
292     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
293     Interpreter::Pointer I = Interpreter::New();    
294     I->InterpretFile(command);
295   }
296   //================================================================
297
298
299   //================================================================
300   void WxGUIConsole::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
301   {
302     std::string command("toolsbbtk/appli/GUICreateBlackBox");
303     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
304     Interpreter::Pointer I = Interpreter::New();    
305     I->InterpretFile(command);
306   }
307   //================================================================
308   
309   //================================================================
310   void WxGUIConsole::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
311   {
312     std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
313
314 #if defined(WIN32)
315     std::string strappli="start ";
316 #else
317     std::string strappli="gnome-open ";
318 #endif
319     std::string strcommand0 = "cd \"" + default_temp_dir+"/temp_dir/" +"\"";
320     std::string strcommand1 = strappli + "workspace_workspacePrototype.png";
321     std::string strcommand = strcommand0 + " && " + strcommand1;
322         std::cout << "system: " << strcommand << std::endl;
323     system ( strcommand.c_str() );
324
325   }
326   //================================================================
327
328   //================================================================
329   void WxGUIConsole::OnMenuShowHTMLDoc(wxCommandEvent& WXUNUSED(event))
330   {
331         std::string doc_path            = ConfigurationFile::GetInstance().Get_doc_path();
332
333 #if defined(WIN32)
334     std::string strappli="start ";
335 #else
336     std::string strappli="gnome-open ";
337 #endif
338     std::string strcommand0 = "cd \"" + doc_path+"\"";
339     std::string strcommand1 = strappli + "index.html";
340     std::string strcommand = strcommand0 + " && " + strcommand1;
341         std::cout << "system: " << strcommand << std::endl;
342     system ( strcommand.c_str() );
343
344   }
345   //================================================================
346
347
348
349   //================================================================
350   void WxGUIConsole::OnMenuCreateIndex(wxCommandEvent& WXUNUSED(event))
351   {
352 /*
353     std::string doc_path = ConfigurationFile::GetInstance().Get_doc_path();
354     std::string filepath = doc_path+"/bbdoc/make-index.bbs";
355     Interpreter* I = new Interpreter;    
356     I->InterpretLine( "exec freeze");
357     I->InterpretLine( "include *");
358     I->InterpretLine( "help packages");
359     I->InterpretLine( "index "+doc_path+"/bbdoc/index-alpha.html Initials");
360     I->InterpretLine( "index "+doc_path+"/bbdoc/index-package.html Packages");
361     I->InterpretLine( "index "+doc_path+"/bbdoc/index-category.html Categories");
362     I->InterpretLine( "index "+doc_path+"/bbdoc/index-adaptors.html Adaptors");    
363     delete I;
364 */
365
366         std::string bin_path            = ConfigurationFile::GetInstance().Get_bin_path();
367         std::string doc_path            = ConfigurationFile::GetInstance().Get_doc_path();
368         std::string bbdoc_path          = doc_path+"/bbdoc";
369     std::string make_index_path = "\"" + bbdoc_path+"/make-index.bbs\"";
370
371         std::string strcommand0         = "cd "+bbdoc_path+" && mkdir tmp && cd tmp";
372
373         std::string strcommand1         = "cd "+bbdoc_path+"/tmp";
374     std::string strcommand2             = "\""+bin_path+"/bbi\" -N "+make_index_path;
375 #ifdef WIN32 
376     std::string strcommand3             = "move index*.html ../.";
377 #else
378     std::string strcommand3             = "mv index*.html ../.";
379 #endif
380
381         std::string strcommand          =       strcommand1 +" && "+
382                                                                         strcommand2 +" && "+
383                                                                         strcommand3;
384
385         std::cout << "system: " << strcommand0 << std::endl;
386         std::cout << "system: " << strcommand << std::endl;
387
388         system ( strcommand0.c_str() );
389         system ( strcommand.c_str() );
390   }
391   //================================================================
392
393
394   //================================================================
395   void WxGUIConsole::InterpreterUserViewHtmlPage(const std::string& page)
396   {
397     std::string s(page);
398     //  std::cout << "WxGUIConsole::ShowHtmlPage('"<<page<<"')"<<std::endl;
399     if (mWxGUIHtmlBrowser->GoTo(s)) 
400       {
401 //EED   mwxNotebook->ChangeSelection(1);
402         mwxNotebook->SetSelection(1);
403       }
404     else 
405       {
406          // std::cout << "ERROR html"<<std::endl;
407       }
408   } 
409   //================================================================  
410   
411   //================================================================  
412   // Overloaded Show method to handle bbtk::Wx::TopWindow auto-destruction
413   bool WxGUIConsole::Show(bool show)
414   {
415     Wx::AutoDestroyTopWindow(!show);
416     return wxFrame::Show(show);
417   }
418   //================================================================  
419    
420   //================================================================  
421   BEGIN_EVENT_TABLE(WxGUIConsole, wxFrame)
422     EVT_MENU(ID_Menu_Quit                       , WxGUIConsole::OnMenuQuit)
423     EVT_MENU(ID_Menu_About                      , WxGUIConsole::OnMenuAbout)
424     EVT_MENU(ID_Menu_EditConfig         , WxGUIConsole::OnMenuEditConfig)
425     EVT_MENU(ID_Menu_CreatePackage      , WxGUIConsole::OnMenuCreatePackage)
426     EVT_MENU(ID_Menu_CreateBlackBox     , WxGUIConsole::OnMenuCreateBlackBox)
427     EVT_MENU(ID_Menu_ShowImageGraph     , WxGUIConsole::OnMenuShowImageGraph)
428     EVT_MENU(ID_Menu_ShowHTMLDoc        , WxGUIConsole::OnMenuShowHTMLDoc)
429     EVT_MENU(ID_Menu_CreateIndex        , WxGUIConsole::OnMenuCreateIndex)
430     EVT_BUTTON(ID_Button_Run            , WxGUIConsole::OnButtonRun )
431     END_EVENT_TABLE()
432   //================================================================
433
434 } // namespace bbtk
435
436
437 #endif //_USE_WXWIDGETS_