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