]> 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/09/10 09:25:19 $
7   Version:   $Revision: 1.17 $
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 #include "bbtkUtilities.h"
41
42
43 //#include "icons/cc_run.xpm"
44
45 namespace bbtk
46 {
47
48     enum
49     {
50       ID_Menu_Quit = 1,
51       ID_Menu_About,
52       ID_Menu_EditConfig,
53       ID_Menu_CreatePackage,
54       ID_Menu_CreateBlackBox,
55       ID_Menu_ShowImageGraph,
56       ID_Menu_CreateIndex,
57       ID_Menu_Windows_Files,
58       ID_Menu_Windows_Help,
59       ID_Menu_Windows_Messages,
60       ID_Menu_Windows_Command,
61       ID_Menu_Windows_Save,
62       ID_Menu_Windows_PackageBrowser
63       //      ID_Button_Run
64     };
65     
66   
67   //================================================================
68   WxGUIScriptingInterface::WxGUIScriptingInterface( wxWindow *parent )
69     : wxFrame((wxFrame *)parent, -1, _T("bbStudio"), 
70               wxDefaultPosition, wxSize(1200,800) )
71   {     
72     //    m_mgr = new wxAuiManager(this);
73     m_mgr.SetManagedWindow(this);
74     
75     mInterpreter = bbtk::Interpreter::New();
76     mInterpreter->SetUser(this);
77     mInterpreter->SetCommandLine(true);
78     mInterpreter->SetThrow(true);
79     
80     //==============
81     // Menu
82     wxInitAllImageHandlers();
83     
84     wxMenu *menuFile = new wxMenu;
85     menuFile->Append( ID_Menu_EditConfig, _T("Open bbtk &Config file") );
86     menuFile->Append( ID_Menu_Quit, _T("&Quit") );
87     
88     wxMenu *menuAbout = new wxMenu;
89     menuAbout->Append( ID_Menu_About, _T("&About...") );
90
91     wxMenu *menuTools = new wxMenu;
92     menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
93     menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &black box") );
94     menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
95     menuTools->Append( ID_Menu_CreateIndex, _T("&Generate index") );
96
97     wxMenu *menuWindows = new wxMenu;
98     menuWindows->AppendCheckItem(ID_Menu_Windows_Files,
99                                  _T("Show 'files' panel") )->Check();
100     menuWindows->AppendCheckItem(ID_Menu_Windows_Help,
101                                  _T("Show 'help' panel") )->Check();
102     menuWindows->AppendCheckItem(ID_Menu_Windows_Messages,
103                                  _T("Show 'messages' panel") )->Check();
104     menuWindows->AppendCheckItem(ID_Menu_Windows_Command,
105                                  _T("Show 'command' panel") )->Check();
106     menuWindows->AppendSeparator();
107     menuWindows->Append( ID_Menu_Windows_PackageBrowser, _T("Launch Package &Browser") );
108     
109     //    menuWindows->AppendSeparator();
110     //    menuWindows->Append ( ID_Menu_Windows_Save, _T("Save interface configuration"));
111
112     wxMenu *menuOptions = new wxMenu;
113     mwxMenuItemReset = menuOptions->AppendCheckItem(-1,
114                                                     _T("Reset before running") );
115     mwxMenuItemReset->Check();
116
117     wxMenuBar *menuBar = new wxMenuBar;
118     menuBar->Append( menuFile, _T("&File") );
119     menuBar->Append( menuTools, _T("&Tools") );
120     menuBar->Append( menuOptions, _T("&Options") );
121     menuBar->Append( menuWindows, _T("&Windows") );
122     menuBar->Append( menuAbout, _T("About") );
123     
124     SetMenuBar( menuBar );
125     
126     //=== 
127     // Status bar
128     CreateStatusBar();
129     SetStatusText( _T("Welcome to bbStudio !") );
130     
131     //===
132     // Panes (Files, Messages, Help, Command)
133     mWxGUITextEditor = new WxGUITextEditor(this,this);
134     mWxGUITextEditor->SetFileNameFilter("*.bbs");
135         
136     mWxGUIHtmlBrowser = new WxGUIHtmlBrowser(this,wxSize(200,0),this);
137
138     mWxGUIOutputMessages = new WxGUIOutputMessages(this);
139
140     mWxGUICommand = new WxGUICommand(this,this);
141    
142     mWxGUICommand->SetFocus();
143
144  
145 #if NOTEBOOK
146
147     mwxNotebook = new wxAuiNotebook(this,  
148                                     -1,
149                                     wxPoint(0, 0),
150                                     wxSize(500,500),
151                                     wxAUI_NB_TAB_SPLIT 
152                                     | wxAUI_NB_TAB_MOVE
153                                     | wxAUI_NB_TAB_EXTERNAL_MOVE
154                                     //| wxAUI_NB_WINDOWLIST_BUTTON
155                                     //|wxAUI_NB_SCROLL_BUTTONS
156                                     // | wxAUI_NB_CLOSE_BUTTON 
157                                     //| wxAUI_NB_CLOSE_ON_ACTIVE_TAB
158                                     //| wxAUI_NB_CLOSE_ON_ALL_TABS
159                                     | wxNO_BORDER);
160   
161
162     mwxNotebook->AddPage(mWxGUITextEditor,_T("Files"),true);
163     mwxNotebook->AddPage(mWxGUIHtmlBrowser,_T("Help"),true);
164
165     mwxNotebook->AddPage(mWxGUIOutputMessages,_T("Messages"),true);
166
167     mwxNotebook->AddPage(mWxGUICommand,_T("Command"),true);
168
169
170     m_mgr.AddPane(mwxNotebook,
171                   wxAuiPaneInfo().Name(wxT("nb"))
172                   .CaptionVisible(false)
173                   .MinimizeButton(false)
174                   .MaximizeButton(false)
175                   .Center()
176                   //              .MinSize(wxSize(100,100))
177                   );   
178 #else 
179
180     //
181     m_mgr.AddPane(mWxGUITextEditor,
182                   wxAuiPaneInfo().Name(wxT("editor_content"))
183                   .Caption(wxT("Files"))
184                   .MinimizeButton(true)
185                   .MaximizeButton(true)
186                   .Center()
187                   .MinSize(wxSize(100,100))
188                   );   
189   
190     m_mgr.AddPane(mWxGUIHtmlBrowser,
191                   wxAuiPaneInfo().Name(wxT("browser_content"))
192                   .Caption(wxT("Help"))
193                   .MinimizeButton(true)
194                   .MaximizeButton(true)
195                   .Right()
196                   .Layer(2)
197                   .MinSize(wxSize(400,100))
198                   );
199
200     m_mgr.AddPane(mWxGUIOutputMessages,
201                   wxAuiPaneInfo().Name(wxT("messages_content"))
202                   .Caption(wxT("Messages"))
203                   .MinimizeButton(true)
204                   .MaximizeButton(true)
205                   .Bottom()
206                   .MinSize(wxSize(100,100))
207                   );
208     m_mgr.AddPane(mWxGUICommand,
209                   wxAuiPaneInfo().Name(wxT("command_content"))
210                   .Caption(wxT("Command"))
211                   .MinimizeButton(true)
212                   .MaximizeButton(true)
213                   .Bottom()
214                   .Layer(1)
215                   .MinSize(wxSize(100,100))
216                   );     
217 #endif
218
219     // parent window of all bbtk windows will be a child of this
220     Wx::SetTopWindowParent(this);
221     // Add the method OnWxSignal as a Wx::Signal observer 
222     //bbtkAddWxSignalObserver(WxGUIPackageBrowser2Window::OnWxSignal);
223
224     //.PaneBorder(false)); 
225     // Load the interface appearance saved on last closing
226     LoadPerspective();
227
228     // Done in LoadPerspective
229     //    m_mgr.Update();
230         
231     SetAutoLayout(true);
232     Layout();
233     //    mwxNotebook->SetSelection(1);
234     mWxGUIHtmlBrowser->GoHome();
235 //    Refresh();
236 //    m_mgr.Update();
237     //   LoadPerspective();
238
239   }
240   //================================================================
241
242  //================================================================
243   WxGUIScriptingInterface::~WxGUIScriptingInterface()
244   {
245     SavePerspective();
246     m_mgr.UnInit();
247   }
248   //================================================================
249
250   //================================================================
251   void WxGUIScriptingInterface::Open(const std::string& filename)
252   {
253     mWxGUITextEditor->Open(filename);
254   }
255   //================================================================
256   
257 #define CATCH_MACRO                                                     \
258   catch (QuitException e)                                               \
259     {                                                                   \
260       std::cout << "* quit exception caught"<<std::endl;                \
261     }                                                                   \
262   catch (InterpreterError e)                                            \
263     {                                                                   \
264       std::cerr << "* IERROR : "<<e.GetMessage()<<std::endl;            \
265       if (e.IsInScriptFile())                                           \
266         std::cerr << "* FILE   : '"<<e.GetScriptFile()<<"'"<<std::endl; \
267       std::cerr << "* LINE   : "<<e.GetScriptLine()<<std::endl;         \
268       int lev = bbtk::MessageManager::GetMessageLevel("Error");         \
269       if (lev > 0) {                                                    \
270         std::cerr << "* Exception thrown : "<<std::endl;                \
271         std::cerr << "*  OBJECT : " <<e.GetObject()<<std::endl;         \
272         std::cerr << "*  FILE   : " <<e.GetSourceFile()<<std::endl;     \
273       }                                                                 \
274     }
275   
276   //================================================================
277   void WxGUIScriptingInterface::WxGUICommandEnter(const std::string& command)
278   {
279     std::string s("> ");
280     s += command + "\n";
281     mWxGUIOutputMessages->Print(s,wxRED);
282     
283     try
284       {
285         mInterpreter->InterpretLine( command );
286       }
287     CATCH_MACRO;
288   }
289   //================================================================
290   
291   //================================================================
292   /// Runs the interpretation of a file
293   bool WxGUIScriptingInterface::InterpretFile( const std::string& filename) 
294   { 
295     try
296       {
297         mInterpreter->InterpretFile(filename);
298       }
299     CATCH_MACRO;
300     return true;
301   }
302   //================================================================
303  
304   //================================================================
305   void WxGUIScriptingInterface::WxGUITextEditorRun()
306   {
307     //    wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
308     std::stringstream* buf = new std::stringstream;
309     (*buf) << mWxGUITextEditor->GetCurrentPage()->GetText();
310     try 
311       {
312         //      std::cout << "RUN"<<std::endl;
313         if (mwxMenuItemReset->IsChecked()) WxGUICommandEnter("reset");
314         mInterpreter->InterpretBuffer(buf);
315         //      std::cout << "EO RUN"<<std::endl;
316       }
317     CATCH_MACRO;
318     //    std::cout << "EO RUN 3"<<std::endl;
319   }
320   //================================================================  
321
322   //================================================================
323   void WxGUIScriptingInterface::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
324   {
325     if (!mWxGUITextEditor->CloseAllPages()) return;
326     Close(true);
327   }
328   //================================================================
329
330
331   //================================================================
332   void WxGUIScriptingInterface::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
333   {
334     m_mgr.Update();
335         Refresh();
336     wxMessageBox(_T("  bbStudio\nThe Black Box Toolkit Development Studio\n(c) CREATIS-LRMN 2008"),
337                  _T("About ..."), wxOK | wxICON_INFORMATION,
338                  this);
339   }
340   //================================================================
341
342
343   //================================================================
344   void WxGUIScriptingInterface::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
345   {
346     std::string configFile = 
347       ConfigurationFile::GetInstance().Get_config_xml_full_path();
348     Open(configFile);
349   }
350   //================================================================
351
352   //================================================================
353   void WxGUIScriptingInterface::OnMenuWindowsPackageBrowser(wxCommandEvent& WXUNUSED(event))
354   {
355     wxBusyCursor wait;
356     WxGUIPackageBrowser2Window *helpbrowser = new
357       WxGUIPackageBrowser2Window(this,_T("Package Browser"), wxSize(600,600) );
358     helpbrowser->Show();
359     
360     /*
361     wxBusyCursor wait;
362     WxGUIPackageBrowser2 *browser = new WxGUIPackageBrowser2(this);
363     browser->IncludeAll();
364     */
365 #if NOTEBOOK
366     //     mwxNotebook->AddPage(browser,_T("Package Browser"),true);
367 #else
368      /*
369      m_mgr.AddPane(browser,
370                    wxAuiPaneInfo().Name(wxT("package_brower"))
371                    .Caption(wxT("Package browser"))
372                    .MinimizeButton(true)
373                    .MaximizeButton(true)
374                    .Bottom()
375                    .Position(1)
376                    .MinSize(wxSize(100,100))
377                    );     
378      */
379 #endif
380   }
381   //================================================================
382
383
384
385   //================================================================
386   void WxGUIScriptingInterface::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
387   {
388
389     std::string command("toolsbbtk/appli/GUICreatePackage");
390     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
391     
392     Interpreter::Pointer I = Interpreter::New();    
393     I->InterpretFile(command);
394
395   }
396   //================================================================
397
398
399   //================================================================
400   void WxGUIScriptingInterface::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
401   {
402     std::string command("toolsbbtk/appli/GUICreateBlackBox");
403     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
404     
405     Interpreter::Pointer I = Interpreter::New();    
406     I->InterpretFile(command);
407   }
408   //================================================================
409   
410   //================================================================
411   void WxGUIScriptingInterface::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
412   {
413     std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
414
415 #if defined(WIN32)
416     std::string strappli="start ";
417 #else
418     std::string strappli="gnome-open ";
419 #endif
420     std::string strcommand = strappli +default_temp_dir+"/temp_dir/workspace_workspacePrototype.png";
421     //  std::cout << "system: " << strcommand << std::endl;
422     system ( strcommand.c_str() );
423
424   }
425   //================================================================
426
427
428   //================================================================
429   void WxGUIScriptingInterface::OnMenuCreateIndex(wxCommandEvent& WXUNUSED(event))
430   {
431     std::string doc_path = ConfigurationFile::GetInstance().Get_doc_path();
432     std::string filepath = doc_path+"/bbdoc/make-index.bbs";
433
434     Interpreter::Pointer I = Interpreter::New();    
435
436     I->InterpretLine( "config");
437     I->InterpretLine( "message echo 2");
438     I->InterpretLine( "exec freeze");
439     I->InterpretLine( "include *");
440     I->InterpretLine( "help packages");
441     I->InterpretLine( "index "+doc_path+"/bbdoc/index-alpha.html Initials");
442     I->InterpretLine( "index "+doc_path+"/bbdoc/index-package.html Packages");
443     I->InterpretLine( "index "+doc_path+"/bbdoc/index-category.html Categories");
444     I->InterpretLine( "index "+doc_path+"/bbdoc/index-adaptors.html Adaptors");
445     
446   }
447   //================================================================
448
449
450   //================================================================
451   void WxGUIScriptingInterface::InterpreterUserViewHtmlPage(const std::string& page)
452   {
453     std::string s(page);
454     //  std::cout << "WxGUIScriptingInterface::ShowHtmlPage('"<<page<<"')"<<std::endl;
455     if (mWxGUIHtmlBrowser->GoTo(s)) 
456       {
457 //EED   mwxNotebook->ChangeSelection(1);
458 //      mwxNotebook->SetSelection(1);
459       }
460     else 
461       {
462          // std::cout << "ERROR html"<<std::endl;
463       }
464   } 
465   //================================================================  
466   
467   //================================================================  
468   bool WxGUIScriptingInterface::WxGUIHtmlBrowserUserOnLinkClicked(const std::string& target)
469   {
470     size_t s = target.length();
471     if ((s>3) && (target[s-1]=='s')
472         && (target[s-2]=='b')
473         && (target[s-3]=='b')
474         && (target[s-4]=='.'))
475       {
476         mWxGUITextEditor->Open(target);
477 #if NOTEBOOK
478         mwxNotebook->SetSelection(0);
479 #endif
480         mWxGUITextEditor->SetFocus();
481         return false;
482       }
483     return true;
484   }
485   //================================================================  
486
487   void WxGUIScriptingInterface::OnMenuWindowsCheck( wxCommandEvent& event, 
488                                                     wxWindow* w)
489   {
490     bool checked = event.IsChecked();
491     bool shown = m_mgr.GetPane(w).IsShown();
492     //    std::cout << "checked = "<<checked<<" - shown = "<<shown<<std::endl;
493     if (checked ^ shown) 
494       {
495         m_mgr.GetPane(w).Show(checked);
496         m_mgr.Update();
497       }
498   }
499
500
501   //================================================================  
502   void WxGUIScriptingInterface::OnMenuWindowsFiles(wxCommandEvent& event)
503   {
504     OnMenuWindowsCheck(event,mWxGUITextEditor);
505   }
506   //================================================================  
507
508   //================================================================  
509   void  WxGUIScriptingInterface::OnMenuWindowsHelp(wxCommandEvent& event)
510   {
511     OnMenuWindowsCheck(event,mWxGUIHtmlBrowser);
512   }
513   //================================================================  
514
515   //================================================================  
516   void  WxGUIScriptingInterface::OnMenuWindowsMessages(wxCommandEvent& event)
517   {
518     OnMenuWindowsCheck(event,mWxGUIOutputMessages);
519   }
520   //================================================================  
521
522   //================================================================  
523   void  WxGUIScriptingInterface::OnMenuWindowsCommand(wxCommandEvent& event)
524   {
525     OnMenuWindowsCheck(event,mWxGUICommand);
526   }
527   //================================================================  
528
529   //================================================================  
530   void WxGUIScriptingInterface::OnMenuWindowsSave( wxCommandEvent& WXUNUSED(event))
531   {
532     SavePerspective();
533   }
534   //================================================================  
535
536   //================================================================  
537   void WxGUIScriptingInterface::SavePerspective()
538   {
539     //    std::cout  << "Saving configuration..."<<std::endl;
540     std::string conf = wx2std(m_mgr.SavePerspective());
541     std::string fname = Utilities::MakeUserSettingsFullFileName("bbStudio.aui");
542     std::ofstream f;
543     f.open(fname.c_str(), std::ios::out );
544     f << conf << std::endl;
545
546     int w,h;
547     GetSize(&w,&h);
548
549     f << w << " " << h << std::endl;
550
551     f.close();
552   }
553   //================================================================  
554
555   //================================================================  
556   void WxGUIScriptingInterface::LoadPerspective()
557   {
558     std::string fname = 
559       Utilities::MakeUserSettingsFullFileName("bbStudio.aui");
560     if ( Utilities::FileExists( fname ))
561       {
562         //      std::cout  << "Loading configuration..."<<std::endl;
563
564         std::ifstream f;
565         f.open(fname.c_str());
566         std::string conf;
567         f >> conf;
568
569         int w,h;
570
571         f >> w >> h ;
572
573         f.close();
574         
575         //      std::cout << conf << std::endl;
576
577         SetSize(w,h);
578         m_mgr.LoadPerspective(std2wx(conf));
579       }
580     else 
581       {
582         m_mgr.Update();
583       }
584   }
585   //================================================================  
586
587   //================================================================  
588   void WxGUIScriptingInterface::OnPaneClose(wxAuiManagerEvent& evt)
589   {
590     std::cout  << "Closing panel '"<<evt.pane->name<<"'"<<std::endl;
591     // TO DO : Uncheck 
592     //    OnMenuWindowsCheck( wxCommandEvent& event, wxWindow* w);
593   }
594   //================================================================  
595
596   
597   //================================================================  
598   BEGIN_EVENT_TABLE(WxGUIScriptingInterface, wxFrame)
599     EVT_MENU(ID_Menu_Quit, WxGUIScriptingInterface::OnMenuQuit)
600     EVT_MENU(ID_Menu_About, WxGUIScriptingInterface::OnMenuAbout)
601     EVT_MENU(ID_Menu_EditConfig, WxGUIScriptingInterface::OnMenuEditConfig)
602     EVT_MENU(ID_Menu_CreatePackage, WxGUIScriptingInterface::OnMenuCreatePackage)
603     EVT_MENU(ID_Menu_CreateBlackBox, WxGUIScriptingInterface::OnMenuCreateBlackBox)
604     EVT_MENU(ID_Menu_ShowImageGraph, WxGUIScriptingInterface::OnMenuShowImageGraph)
605     EVT_MENU(ID_Menu_CreateIndex, WxGUIScriptingInterface::OnMenuCreateIndex)
606     EVT_MENU(ID_Menu_Windows_Files, WxGUIScriptingInterface::OnMenuWindowsFiles)
607     EVT_MENU(ID_Menu_Windows_Help, WxGUIScriptingInterface::OnMenuWindowsHelp)
608     EVT_MENU(ID_Menu_Windows_Messages, WxGUIScriptingInterface::OnMenuWindowsMessages)
609     EVT_MENU(ID_Menu_Windows_Command, WxGUIScriptingInterface::OnMenuWindowsCommand)
610     EVT_MENU(ID_Menu_Windows_Save, WxGUIScriptingInterface::OnMenuWindowsSave)
611     EVT_MENU(ID_Menu_Windows_PackageBrowser, WxGUIScriptingInterface::OnMenuWindowsPackageBrowser)
612     EVT_AUI_PANE_CLOSE(WxGUIScriptingInterface::OnPaneClose)
613   //    EVT_BUTTON(ID_Button_Run, WxGUIScriptingInterface::OnButtonRun )
614     END_EVENT_TABLE()
615   //================================================================
616
617 } // namespace bbtk
618
619
620 #endif //_USE_WXWIDGETS_