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