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