]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxGUIScriptingInterface.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkWxGUIScriptingInterface.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkWxGUIScriptingInterface.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/03/21 11:46:41 $
7   Version:   $Revision: 1.1 $
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 "bbtkWxGUIScriptingInterface.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   WxGUIScriptingInterface::WxGUIScriptingInterface( 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 = new bbtk::Interpreter();
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     /*
100     m_mgr.AddPane(CreateSizeReportCtrl(), wxAuiPaneInfo().
101                   Name(wxT("test4")).Caption(wxT("Pane Caption")).
102                   Left());
103     */
104
105    //==============
106     // Notebook
107     
108     //    wxFlexGridSizer *sizer = new wxFlexGridSizer(1);
109     
110 //EED    wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
111 //    mwxNotebook = new wxNotebook(this,-1,wxDefaultPosition, wxDefaultSize, 0);
112 /*
113     mwxNotebook = new wxAuiNotebook(this,  
114                                     -1,
115                                     wxPoint(0, 0),
116                                     wxSize(500,500),
117                                     wxAUI_NB_TAB_SPLIT | wxAUI_NB_TAB_EXTERNAL_MOVE | wxNO_BORDER);
118     
119     mwxPageCommand = new wxPanel(mwxNotebook,-1);        
120     mwxPageHelp = new wxPanel(mwxNotebook,-1);    
121     
122     
123     wxBoxSizer *cmdsizer = new wxBoxSizer(wxVERTICAL);
124     
125     mwxPageCommand->SetAutoLayout(true);    
126     mwxPageCommand->SetSizer(cmdsizer);
127     cmdsizer->Fit(mwxPageCommand);
128     cmdsizer->SetSizeHints(mwxPageCommand);
129
130     wxBoxSizer *helpsizer = new wxBoxSizer(wxVERTICAL);
131     
132     mwxPageHelp->SetAutoLayout(true);    
133     mwxPageHelp->SetSizer(helpsizer);
134     helpsizer->Fit(mwxPageHelp);
135     helpsizer->SetSizeHints(mwxPageHelp);
136 */ 
137     mWxGUITextEditor = new WxGUITextEditor(this);
138     mWxGUITextEditor->SetFileNameFilter("*.bbs");
139
140       mWxGUIHtmlBrowser = new WxGUIHtmlBrowser(this, //mwxPageHelp,
141                                              //EED                                wxSize(1200,0));
142                                              wxSize(200,0));
143
144     //    mWxGUIHtmlBrowser->SetSize(wxSize(800,1000));
145     //   helpsizer->Add (mWxGUIHtmlBrowser,1, wxGROW|wxLEFT|wxRIGHT|wxTOP, 5  );
146     //    helpsizer->Add ( new wxButton(mwxPageHelp,-1,"perro"), 0,  wxEXPAND  );
147     
148     wxBitmap bmp_run(cc_run_xpm);
149     mwxButtonRun = new wxBitmapButton( this, ID_Button_Run,bmp_run);//_T("Run")  );
150     // helpsizer->Add( mwxButtonRun, 0, wxALL, 5  );
151
152   
153     //==============
154     // Command page 
155     mWxGUIOutputMessages = new WxGUIOutputMessages(this);
156
157     mWxGUICommand = new WxGUICommand(this,this);
158    
159     //    mWxGUICommand->SetFocus();
160
161     //    cmdsizer->Add (mWxGUIOutputMessages, 1, wxALL | wxGROW, 5);
162     //    cmdsizer->Add (mWxGUICommand, 0, wxALL | wxGROW, 5);
163
164         
165     // Creates and sets the parent window of all bbtk windows
166     wxWindow* top = new wxPanel(this,-1);//,_T("top"));
167     top->Hide();
168     Wx::SetTopWindow(top);
169     
170
171     // Layout
172 //EED    SetSizer(sizer);
173 /*
174     mwxNotebook->AddPage( mwxPageCommand, _T("Command"));
175     mwxNotebook->AddPage( mwxPageHelp, _T("Help"));
176 */
177
178     m_mgr.AddPane(mWxGUITextEditor,
179                   wxAuiPaneInfo().Name(wxT("editor_content"))
180                   .Top());     
181     m_mgr.AddPane(mWxGUIOutputMessages,
182                   wxAuiPaneInfo().Name(wxT("messages_content"))
183                   .Caption(wxT("Messages"))
184                   .Center());     
185     m_mgr.AddPane(mWxGUIHtmlBrowser,
186                   wxAuiPaneInfo().Name(wxT("browser_content"))
187                   .Right());     
188     m_mgr.AddPane(mWxGUICommand,
189                   wxAuiPaneInfo().Name(wxT("command_content"))
190                   .Bottom());     
191     m_mgr.AddPane(mwxButtonRun,
192                   wxAuiPaneInfo().Name(wxT("button_run_content")));     
193
194
195 //.PaneBorder(false)); 
196     m_mgr.Update();
197         
198     SetAutoLayout(true);
199     Layout();
200 //    Refresh();
201 //    m_mgr.Update();
202   }
203   //================================================================
204
205  //================================================================
206   WxGUIScriptingInterface::~WxGUIScriptingInterface()
207   {
208     m_mgr.UnInit();
209   }
210   //================================================================
211
212   //================================================================
213   void WxGUIScriptingInterface::Open(const std::string& filename)
214   {
215     mWxGUITextEditor->Open(filename);
216   }
217   //================================================================
218
219   //================================================================
220   void WxGUIScriptingInterface::WxGUICommandEnter(const std::string& command)
221   {
222     std::string s("> ");
223     s += command + "\n";
224     mWxGUIOutputMessages->Print(s,wxRED);
225
226     if (  mInterpreter->InterpretLine( command ) == 
227           Interpreter::QUIT )
228       {
229         Close(true); 
230       }
231   }
232   //================================================================
233
234   //================================================================
235   /// Runs the interpretation of a file
236   bool WxGUIScriptingInterface::InterpretFile( const std::string& filename) 
237   { 
238     if ( mInterpreter->InterpretFile(filename) ==
239          Interpreter::ERROR ) 
240       {
241         return false;
242       }
243     return true;
244   }
245   //================================================================
246  
247   //================================================================
248   void WxGUIScriptingInterface::OnButtonRun(wxCommandEvent& WXUNUSED(event))
249   {
250     wxString per = m_mgr.SavePerspective();
251     std::cout << per<< std::endl;
252
253 //    wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
254     std::string filename = mWxGUIHtmlBrowser->GetCurrentPage();//wx2std(temp);
255     size_t s = filename.length();
256
257     Interpreter* I = new Interpreter;
258     
259     if ((s>3) && (filename[s-1]=='s')
260         && (filename[s-2]=='b')
261         && (filename[s-3]=='b')
262         && (filename[s-4]=='.'))
263       {
264         std::string tmp("Executing ");
265         tmp += filename;
266         SetStatusText(std2wx(tmp));
267         I->InterpretFile(filename);
268       }
269     else
270       {
271         SetStatusText(_T("The current page is not a bbs file : cannot execute it"));
272       }
273     
274     delete I;
275   }
276   //================================================================  
277
278   //================================================================
279   void WxGUIScriptingInterface::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
280   {
281     Close(true);
282   }
283   //================================================================
284
285
286   //================================================================
287   void WxGUIScriptingInterface::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
288   {
289     m_mgr.Update();
290         Refresh();
291     wxMessageBox(_T("  bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS-LRMN 2008"),
292                  _T("About ..."), wxOK | wxICON_INFORMATION,
293                  this);
294   }
295   //================================================================
296
297
298   //================================================================
299   void WxGUIScriptingInterface::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
300   {
301         std::string commandStr;
302     std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
303 #ifdef WIN32
304         commandStr = "notepad.exe ";
305 #else
306         commandStr = "gedit ";
307 #endif 
308         commandStr = commandStr + configFile;
309         std::cout << "system: " << commandStr << std::endl;
310         system ( commandStr.c_str() );
311   }
312   //================================================================
313
314
315   //================================================================
316   void WxGUIScriptingInterface::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
317   {
318 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 01 \n");
319     std::string command("toolsbbtk/appli/CreatePackage");
320 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 02 \n");
321
322     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
323 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 03 \n");
324     Interpreter* I = new Interpreter;    
325 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 04 \n");
326     I->InterpretFile(command);
327 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 05 \n");
328     delete I;
329 printf("EED WxGUIScriptingInterface::OnMenuCreatePackage 06 \n");
330   }
331   //================================================================
332
333
334   //================================================================
335   void WxGUIScriptingInterface::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
336   {
337     std::string command("toolsbbtk/appli/CreateBlackBox");
338     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
339     Interpreter* I = new Interpreter;    
340     I->InterpretFile(command);
341     delete I;
342   }
343   //================================================================
344   
345   //================================================================
346   void WxGUIScriptingInterface::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
347   {
348     std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
349
350 #if defined(WIN32)
351     std::string strappli="start ";
352 #else
353     std::string strappli="gnome-open ";
354 #endif
355     std::string strcommand = strappli +default_temp_dir+"/temp_dir/workspace_workspacePrototype.png";
356         std::cout << "system: " << strcommand << std::endl;
357     system ( strcommand.c_str() );
358
359   }
360   //================================================================
361
362
363   //================================================================
364   void WxGUIScriptingInterface::OnMenuCreateIndex(wxCommandEvent& WXUNUSED(event))
365   {
366     std::string doc_path = ConfigurationFile::GetInstance().Get_doc_path();
367     std::string filepath = doc_path+"/bbdoc/make-index.bbs";
368     Interpreter* I = new Interpreter;    
369
370 //EED   std::cout << "bbi: include "<<filepath<<std::endl;
371 //EED   I->InterpretFile( filepath );
372
373     I->InterpretLine( "exec freeze");
374     I->InterpretLine( "include *");
375     I->InterpretLine( "index "+doc_path+"/bbdoc/index-alpha.html Initials");
376     I->InterpretLine( "index "+doc_path+"/bbdoc/index-package.html Packages");
377     I->InterpretLine( "index "+doc_path+"/bbdoc/index-category.html Categories");
378     I->InterpretLine( "index "+doc_path+"/bbdoc/index-adaptors.html Adaptors");
379     
380     delete I;
381   }
382   //================================================================
383
384
385   //================================================================
386   void WxGUIScriptingInterface::InterpreterUserViewHtmlPage(const std::string& page)
387   {
388     std::string s(page);
389     //  std::cout << "WxGUIScriptingInterface::ShowHtmlPage('"<<page<<"')"<<std::endl;
390     if (mWxGUIHtmlBrowser->GoTo(s)) 
391       {
392 //EED   mwxNotebook->ChangeSelection(1);
393         mwxNotebook->SetSelection(1);
394       }
395     else 
396       {
397          // std::cout << "ERROR html"<<std::endl;
398       }
399   } 
400   //================================================================  
401   
402     
403   //================================================================  
404   BEGIN_EVENT_TABLE(WxGUIScriptingInterface, wxFrame)
405     EVT_MENU(ID_Menu_Quit, WxGUIScriptingInterface::OnMenuQuit)
406     EVT_MENU(ID_Menu_About, WxGUIScriptingInterface::OnMenuAbout)
407     EVT_MENU(ID_Menu_EditConfig, WxGUIScriptingInterface::OnMenuEditConfig)
408     EVT_MENU(ID_Menu_CreatePackage, WxGUIScriptingInterface::OnMenuCreatePackage)
409     EVT_MENU(ID_Menu_CreateBlackBox, WxGUIScriptingInterface::OnMenuCreateBlackBox)
410     EVT_MENU(ID_Menu_ShowImageGraph, WxGUIScriptingInterface::OnMenuShowImageGraph)
411     EVT_MENU(ID_Menu_CreateIndex, WxGUIScriptingInterface::OnMenuCreateIndex)
412     EVT_BUTTON(ID_Button_Run, WxGUIScriptingInterface::OnButtonRun )
413     END_EVENT_TABLE()
414   //================================================================
415
416 } // namespace bbtk
417
418
419 #endif //_USE_WXWIDGETS_