]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxGUIScriptingInterface.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkWxGUIScriptingInterface.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkWxGUIScriptingInterface.cxx,v $
4   Language:  C++
5   Date:      $Date: 2010/03/19 08:18:41 $
6   Version:   $Revision: 1.42 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30    
31
32 /**
33  * \brief Short description in one line
34  * 
35  * Long description which 
36  * can span multiple lines
37  */
38 /**
39  * \file 
40  * \brief 
41  */
42 /**
43  * \class bbtk::
44  * \brief 
45  */
46
47
48 #ifdef _USE_WXWIDGETS_
49
50 #include <iostream>     
51 #include "bbtkWxGUIScriptingInterface.h"
52 #include "bbtkWxBlackBox.h"
53 #include "bbtkConfigurationFile.h"
54 #include "bbtkWxStreamRedirector.h"
55 #include "bbtkUtilities.h"
56
57 #include <wx/tipwin.h>
58 #include <wx/splash.h>
59
60
61 //#include "icons/cc_run.xpm"
62
63 namespace bbtk
64 {
65
66     enum
67     {
68       ID_Menu_Quit = 1,
69       ID_Menu_About,
70       ID_Menu_EditConfig,
71       ID_Menu_CreatePackage,
72       ID_Menu_CreateBlackBox,
73       ID_Menu_ShowImageGraph,
74       ID_Menu_RegeneratePackageDoc,
75       ID_Menu_RegenerateBoxesLists,
76       ID_Menu_RegenerateAll,
77       ID_Menu_PlugPackage,
78       ID_Menu_Windows_Files,
79       ID_Menu_Windows_Help,
80       ID_Menu_Windows_Messages,
81       ID_Menu_Windows_Command,
82       ID_Menu_Windows_Save,
83       ID_Menu_Windows_PackageBrowser
84       //      ID_Button_Run
85     };
86     
87   
88   //================================================================
89   WxGUIScriptingInterface::WxGUIScriptingInterface( wxWindow *parent )
90     : wxFrame((wxFrame *)parent, -1, _T("bbStudio"), 
91               wxDefaultPosition, wxSize(1200,800) )
92   {     
93     m_mgr.SetManagedWindow(this);
94     
95     mInterpreter = bbtk::Interpreter::New();
96     mInterpreter->SetUser(this);
97         mInterpreter->SetCommandLine(true);
98     mInterpreter->SetThrow(true);
99     //    mInterpreter->AddBreakObserver
100     //boost::bind( &WxGUIScriptingInterface::InterpreterUserOnBreak, this ));
101     //==============
102     // Menu
103     wxInitAllImageHandlers();
104     
105     wxMenu *menuFile = new wxMenu;
106     menuFile->Append( ID_Menu_EditConfig, _T("Open bbtk &Config file") );
107     menuFile->Append( ID_Menu_Quit, _T("&Quit") );
108     
109     wxMenu *menuAbout = new wxMenu;
110     menuAbout->Append( ID_Menu_About, _T("&About...") );
111
112     wxMenu *menuTools = new wxMenu;
113     menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
114     menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &black box") );
115     menuTools->Append( ID_Menu_PlugPackage, _T("&Plug package") );
116     menuTools->AppendSeparator();
117     menuTools->Append( ID_Menu_RegeneratePackageDoc,_T("Regenerate package &doc") );
118     menuTools->Append( ID_Menu_RegenerateBoxesLists,_T("Regenerate boxes &lists") );
119     menuTools->Append( ID_Menu_RegenerateAll,_T("Regenerate &all") );
120     menuTools->AppendSeparator();
121     menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last graph") );
122
123     wxMenu *menuWindows = new wxMenu;
124     menuWindows->AppendCheckItem(ID_Menu_Windows_Files,
125                                  _T("Show 'files' panel") )->Check();
126     menuWindows->AppendCheckItem(ID_Menu_Windows_Help,
127                                  _T("Show 'help' panel") )->Check();
128     menuWindows->AppendCheckItem(ID_Menu_Windows_Messages,
129                                  _T("Show 'messages' panel") )->Check();
130     menuWindows->AppendCheckItem(ID_Menu_Windows_Command,
131                                  _T("Show 'command' panel") )->Check();
132     menuWindows->AppendSeparator();
133     menuWindows->Append( ID_Menu_Windows_PackageBrowser, _T("Start Package &Browser") );
134     
135     //    menuWindows->AppendSeparator();
136     //    menuWindows->Append ( ID_Menu_Windows_Save, _T("Save interface configuration"));
137
138     wxMenu *menuOptions = new wxMenu;
139     mwxMenuItemReset = menuOptions->AppendCheckItem(-1,
140                                                     _T("Reset before running") );
141     mwxMenuItemReset->Check();
142
143     wxMenuBar *menuBar = new wxMenuBar;
144     menuBar->Append( menuFile, _T("&File") );
145     menuBar->Append( menuTools, _T("&Tools") );
146     menuBar->Append( menuOptions, _T("&Options") );
147     menuBar->Append( menuWindows, _T("&Windows") );
148     menuBar->Append( menuAbout, _T("About") );
149     
150     SetMenuBar( menuBar );
151     
152     //=== 
153     // Status bar
154     CreateStatusBar();
155     SetStatusText( _T("Welcome to bbStudio !") );
156     
157     //===
158     // Panes (Files, Messages, Help, Command)
159     mWxGUITextEditor = new WxGUITextEditor(this,this);
160     mWxGUITextEditor->SetFileNameFilter("*.bbs");
161         
162     mWxGUIHtmlBrowser = new WxGUIHtmlBrowser(this,wxSize(200,0),this);
163
164     mWxGUIOutputMessages = new WxGUIOutputMessages(this);
165
166     mWxGUICommand = new WxGUICommand(this,this);
167    
168     mWxGUICommand->SetFocus();
169
170  
171 #if NOTEBOOK
172
173     mwxNotebook = new wxAuiNotebook(this,  
174                                     -1,
175                                     wxPoint(0, 0),
176                                     wxSize(500,500),
177                                     wxAUI_NB_TAB_SPLIT 
178                                     | wxAUI_NB_TAB_MOVE
179                                     | wxAUI_NB_TAB_EXTERNAL_MOVE
180                                     //| wxAUI_NB_WINDOWLIST_BUTTON
181                                     //|wxAUI_NB_SCROLL_BUTTONS
182                                     // | wxAUI_NB_CLOSE_BUTTON 
183                                     //| wxAUI_NB_CLOSE_ON_ACTIVE_TAB
184                                     //| wxAUI_NB_CLOSE_ON_ALL_TABS
185                                     | wxNO_BORDER);
186   
187
188     mwxNotebook->AddPage(mWxGUITextEditor,_T("Files"),true);
189     mwxNotebook->AddPage(mWxGUIHtmlBrowser,_T("Help"),true);
190
191     mwxNotebook->AddPage(mWxGUIOutputMessages,_T("Messages"),true);
192
193     mwxNotebook->AddPage(mWxGUICommand,_T("Command"),true);
194
195
196     m_mgr.AddPane(mwxNotebook,
197                   wxAuiPaneInfo().Name(wxT("nb"))
198                   .CaptionVisible(false)
199                   .MinimizeButton(false)
200                   .MaximizeButton(false)
201                   .Center()
202                   //              .MinSize(wxSize(100,100))
203                   );   
204 #else 
205
206     //
207     m_mgr.AddPane(mWxGUITextEditor,
208                   wxAuiPaneInfo().Name(wxT("editor_content"))
209                   .Caption(wxT("Files"))
210                   .MinimizeButton(true)
211                   .MaximizeButton(true)
212                   .Center()
213                   .MinSize(wxSize(100,100))
214                   );   
215   
216     m_mgr.AddPane(mWxGUIHtmlBrowser,
217                   wxAuiPaneInfo().Name(wxT("browser_content"))
218                   .Caption(wxT("Help"))
219                   .MinimizeButton(true)
220                   .MaximizeButton(true)
221                   .Right()
222                   .Layer(2)
223                   .MinSize(wxSize(400,100))
224                   );
225
226     m_mgr.AddPane(mWxGUIOutputMessages,
227                   wxAuiPaneInfo().Name(wxT("messages_content"))
228                   .Caption(wxT("Messages"))
229                   .MinimizeButton(true)
230                   .MaximizeButton(true)
231                   .Bottom()
232                   .MinSize(wxSize(100,100))
233                   );
234           
235     m_mgr.AddPane(mWxGUICommand,
236                   wxAuiPaneInfo().Name(wxT("command_content"))
237                   .Caption(wxT("Command"))
238                   .MinimizeButton(true)
239                   .MaximizeButton(true)
240                   .Bottom()
241                   .Layer(1)
242                   .MinSize(wxSize(100,100))
243                   );     
244 #endif
245
246     // parent window of all bbtk windows will be a child of this
247     //    Wx::SetTopWindowParent(this);
248     Wx::SetTopWindow(this);
249     // Wx::SetAutoDestroyTopWindow(false);
250   // Add the method OnWxSignal as a Wx::Signal observer 
251     //bbtkAddWxSignalObserver(WxGUIPackageBrowser2Window::OnWxSignal);
252
253     //.PaneBorder(false)); 
254     // Load the interface appearance saved on last closing
255     LoadPerspective();
256
257     // Done in LoadPerspective
258     //    m_mgr.Update();
259         
260     SetAutoLayout(true);
261     Layout();
262     //    mwxNotebook->SetSelection(1);
263     mWxGUIHtmlBrowser->GoHome();
264 //    Refresh();
265     m_mgr.Update();
266     //   LoadPerspective();
267
268     mBreaked = false;   
269
270     wxBitmap bitmap;   
271     wxSplashScreen* splash;
272     long style = wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_TIMEOUT;
273     if (ConfigurationFile::GetInstance().DotBbtkIsNew())
274       style = wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT;
275     std::string splash_file = ConfigurationFile::GetInstance().Get_data_path();
276     splash_file += "/kernel/icons/bbStudioSplashScreen.png";
277
278     if (bitmap.LoadFile(std2wx(splash_file),wxBITMAP_TYPE_PNG))
279       splash = 
280         new wxSplashScreen(bitmap,
281                            style,
282                            1000, 0, -1, wxDefaultPosition, wxDefaultSize,
283                            wxSIMPLE_BORDER|wxSTAY_ON_TOP);
284         
285     
286     if (ConfigurationFile::GetInstance().DotBbtkIsNew())
287       {
288
289         DoRegeneratePackageDoc("-a");   
290         DoRegenerateBoxesLists();
291
292         /*
293         wxTipWindow* tip = new wxTipWindow(this,
294                                            _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);
295         tip->CenterOnParent();
296         tip->Show();
297         */
298         if (splash) splash->Destroy();
299       }
300
301   }
302   //================================================================
303
304  //================================================================
305   WxGUIScriptingInterface::~WxGUIScriptingInterface()
306   {
307   bbtkDebugMessage("widget",9,"bbStudio::~bbStudio()"<<std::endl);
308     SavePerspective();
309     m_mgr.UnInit();
310   }
311   //================================================================
312
313   //================================================================
314   void WxGUIScriptingInterface::OnClose(wxCloseEvent& event)
315   {
316     bbtkDebugMessage("widget",9,"bbStudio::OnClose()"<<std::endl);
317     //Wx::SetWindowsHaveBeenDestroyedByParent();
318     //mInterpreter.reset();
319     wxWindowList& list = GetChildren();
320     wxWindowList::iterator iter;
321     for (iter = list.begin(); iter != list.end(); ++iter)
322       {
323         (*iter)->Close();
324       }
325     this->Destroy();
326   }
327   //================================================================
328
329
330   //================================================================
331   void WxGUIScriptingInterface::Open(const std::string& filename)
332   {
333     mWxGUITextEditor->Open(filename);
334   }
335   //================================================================
336   /*
337   catch (BreakException e)                                              \
338     {                                                                   \
339       std::cout << "* BREAK"<<std::endl;                                \
340       if (e.IsInScriptFile())                                           \
341         std::cout << "* FILE   : '"<<e.GetScriptFile()<<"'"<<std::endl; \
342       std::cout << "* LINE   : "<<e.GetScriptLine()<<std::endl;         \
343     }                                                                   \
344   catch (QuitException e)                                               \
345     {                                                                   \
346       std::cout << "* QUIT"<<std::endl;                                 \
347       if (e.IsInScriptFile())                                           \
348         std::cout << "* FILE   : '"<<e.GetScriptFile()<<"'"<<std::endl; \
349       std::cout << "* LINE   : "<<e.GetScriptLine()<<std::endl;         \
350     }                                                                   \
351 */
352   
353 #define CATCH_MACRO                                                     \
354   catch (InterpreterException e)                                        \
355     {                                                                   \
356       if (e.GetErrorMessage()=="quit")                                  \
357         {                                                               \
358           std::cerr << "* QUIT ENCOUNTERED"<<std::endl;                 \
359           if (e.IsInScriptFile())                                       \
360             std::cerr << "* FILE   : '"<<e.GetScriptFile()<<"'"<<std::endl; \
361           std::cerr << "* LINE   : "<<e.GetScriptLine()<<std::endl;     \
362         }                                                               \
363       else if (e.GetErrorMessage()=="break")                            \
364         {                                                               \
365           mBreaked = true;                                              \
366           std::cerr << "* BREAK"<<std::endl;                            \
367           if (e.IsInScriptFile())                                       \
368             std::cerr << "* FILE   : '"<<e.GetScriptFile()<<"'"<<std::endl; \
369           std::cerr << "* LINE   : "<<e.GetScriptLine()<<std::endl;     \
370         }                                                               \
371       else                                                              \
372         {                                                               \
373           std::cerr << "* ERROR : "<<e.GetErrorMessage()<<std::endl;    \
374           if (e.IsInScriptFile())                                       \
375             std::cerr << "* FILE   : '"<<e.GetScriptFile()<<"'"<<std::endl; \
376           std::cerr << "* LINE   : "<<e.GetScriptLine()<<std::endl;     \
377           int lev = bbtk::MessageManager::GetMessageLevel("Error");     \
378           if (lev > 0) {                                                \
379             std::cerr << "* Exception thrown : "<<std::endl;            \
380             std::cerr << "*  OBJECT : " <<e.GetObject()<<std::endl;     \
381             std::cerr << "*  FILE   : " <<e.GetSourceFile()<<std::endl; \
382           }                                                             \
383         }                                                               \
384     }
385   
386   //================================================================
387   void WxGUIScriptingInterface::WxGUICommandEnter(const std::string& command)
388   {
389     std::string s("> ");
390     s += command + "\n";
391     mWxGUIOutputMessages->Print(s,wxRED);
392     
393     try
394       {
395         mInterpreter->InterpretLine( command );
396       }
397     CATCH_MACRO;
398   }
399   //================================================================
400   
401   //================================================================
402   /// Runs the interpretation of a file
403   bool WxGUIScriptingInterface::InterpretFile( const std::string& filename) 
404   { 
405     try
406       {
407         mInterpreter->InterpretFile(filename);
408       }
409     CATCH_MACRO;
410     return true;
411   }
412   //================================================================
413  
414         
415   //================================================================
416         void WxGUIScriptingInterface::WxGUITextEditorGraphSimple()
417         {
418                 WxGUICommandEnter("exec freeze_no_error");
419                 WxGUITextEditorRun();
420                 WxGUICommandEnter("exec unfreeze");
421                 WxGUICommandEnter("graph");
422         }
423   //================================================================
424         
425         //================================================================
426         void WxGUIScriptingInterface::WxGUITextEditorGraphDetail()
427         {
428                 WxGUICommandEnter("exec freeze_no_error");
429                 WxGUITextEditorRun();
430                 WxGUICommandEnter("exec unfreeze");
431                 WxGUICommandEnter("graph . 1");
432         }
433         //================================================================
434
435         
436         //================================================================
437         void WxGUIScriptingInterface::WxGUITextEditorRunBBI()
438         {
439           std::string separator = ConfigurationFile::GetInstance().Get_file_separator ();
440           std::string dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
441           std::string filename = dir + separator + "tmp_bbtk.bbs";
442           mWxGUITextEditor->GetCurrentPage()->SaveFile(filename);
443         
444                 std::string command = "\"\"";
445           command += ConfigurationFile::GetInstance().Get_bin_path();
446 #ifdef MACOSX
447           command += separator + "bbi.app/Contents/MacOS/bbi\" ";
448 #else 
449           command += separator + "bbi.exe\" ";
450 #endif
451           command += "\""+filename + "\"\"";
452           command += " & ";
453           
454           printf ("EED WxGUIScriptingInterface::WxGUITextEditorRunBBI %s \n" , command.c_str() );
455           system( command.c_str() );
456         }
457         //================================================================
458         
459         
460   //================================================================
461   void WxGUIScriptingInterface::WxGUITextEditorRun()
462   {
463     //    wxString temp = mWxGUIHtmlBrowser->GetCurrentPage();
464     std::stringstream* buf = new std::stringstream;
465     (*buf) << mWxGUITextEditor->GetCurrentPage()->GetText();
466     try 
467       {
468         if (!mBreaked)
469           {
470             if (mwxMenuItemReset->IsChecked()) WxGUICommandEnter("reset");
471             mInterpreter->InterpretBuffer(buf);
472           }
473         else 
474           {
475             mBreaked = false;
476             mInterpreter->InterpretCurrentStreams();
477           }
478         //      std::cout << "EO RUN"<<std::endl;
479       }
480     CATCH_MACRO;
481     //    std::cout << "EO RUN 3"<<std::endl;
482   }
483   //================================================================  
484
485   //================================================================
486   void WxGUIScriptingInterface::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
487   {
488     if (!mWxGUITextEditor->CloseAllPages()) return;
489     Close(true);
490   }
491   //================================================================
492
493
494   //================================================================
495   void WxGUIScriptingInterface::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
496   {
497     m_mgr.Update();
498     Refresh();
499     wxMessageBox(_T("  bbStudio\nThe Black Box Toolkit Development Studio\n(c) CREATIS-LRMN 2008\n\n http://www.creatis.insa-lyon.fr/creatools"),
500                  _T("About ..."), wxOK | wxICON_INFORMATION,
501                  this);
502   }
503   //================================================================
504
505
506   //================================================================
507   void WxGUIScriptingInterface::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
508   {
509
510     std::string configFile = 
511       ConfigurationFile::GetInstance().Get_config_xml_full_path();
512     Open(configFile);
513         wxMessageDialog ww(NULL,_T("If you change the bbtk_config.xml, you have to restart this appliaction..."), _T("Alert !"), wxOK);
514         ww.ShowModal();
515   }
516   //================================================================
517
518   //================================================================
519   void WxGUIScriptingInterface::OnMenuWindowsPackageBrowser(wxCommandEvent& WXUNUSED(event))
520   {
521     wxBusyCursor wait;
522     WxGUIPackageBrowser2Window *helpbrowser = new
523       WxGUIPackageBrowser2Window(this,_T("Package Browser"), wxSize(600,600) );
524     helpbrowser->Show();
525     
526     /*
527     wxBusyCursor wait;
528     WxGUIPackageBrowser2 *browser = new WxGUIPackageBrowser2(this);
529     browser->IncludeAll();
530     */
531 #if NOTEBOOK
532     //     mwxNotebook->AddPage(browser,_T("Package Browser"),true);
533 #else
534      /*
535      m_mgr.AddPane(browser,
536                    wxAuiPaneInfo().Name(wxT("package_brower"))
537                    .Caption(wxT("Package browser"))
538                    .MinimizeButton(true)
539                    .MaximizeButton(true)
540                    .Bottom()
541                    .Position(1)
542                    .MinSize(wxSize(100,100))
543                    );     
544      */
545 #endif
546   }
547   //================================================================
548
549
550
551   //================================================================
552   void WxGUIScriptingInterface::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
553   {
554 /*
555     std::string command("toolsbbtk/appli/GUICreatePackage");
556     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
557   
558         Interpreter::Pointer I = Interpreter::New();    
559     I->InterpretFile(command);
560 */
561     std::string command("reset");
562         mWxGUICommand->SendCommand(command);
563     command = "include toolsbbtk/appli/GUICreatePackage";
564     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
565         mWxGUICommand->SendCommand(command);
566
567   }
568   //================================================================
569
570
571   //================================================================
572   void WxGUIScriptingInterface::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
573   {
574           /*
575     std::string command("toolsbbtk/appli/GUICreateBlackBox");
576     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
577     
578     Interpreter::Pointer I = Interpreter::New();    
579     I->InterpretFile(command);
580 */
581     std::string command("reset");
582         mWxGUICommand->SendCommand(command);
583     command = "include toolsbbtk/appli/GUICreateBlackBox";
584     bbtkMessage("Debug",1,"Executing : '"<<command<<"'"<<std::endl);
585         mWxGUICommand->SendCommand(command);
586   }
587   //================================================================
588   
589   //================================================================
590   void WxGUIScriptingInterface::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
591   {
592
593       std::string doc_path = bbtk::ConfigurationFile::GetInstance().Get_doc_path();
594       doc_path += bbtk::ConfigurationFile::GetInstance().Get_file_separator();
595       doc_path += "bbdoc";
596       doc_path += bbtk::ConfigurationFile::GetInstance().Get_file_separator();
597           
598           std::string pack_name("User");
599           std::string pack_path = doc_path + pack_name;
600           
601           
602 #if defined(WIN32)
603     std::string strappli="start ";
604 #else
605   #if defined(MACOSX)
606       std::string strappli="open ";
607   #else
608       std::string strappli="gnome-open ";         
609   #endif
610 #endif
611           
612           std::string strcommand = strappli +pack_path+"/workspace_workspacePrototype.png";
613     //  std::cout << "system: " << strcommand << std::endl;
614     system ( strcommand.c_str() );
615
616   }
617   //================================================================
618
619   void WxGUIScriptingInterface::DoRegeneratePackageDoc( const std::string& pack )
620   {
621     std::string mess("Regenerating doc for package '");
622     if (pack!="-a") 
623       mess += pack + "'";
624     else 
625       mess = "Regenerating doc for all packages";
626     mess += " ... please wait";
627
628     SetStatusText( std2wx(mess) );
629
630     BBTK_BUSY_CURSOR;
631
632     std::string command;
633 #if defined(WIN32)
634     command = "\"";
635 #endif
636     command += ConfigurationFile::GetInstance().Get_bin_path();
637     command += ConfigurationFile::GetInstance().Get_file_separator();
638     command += "bbRegeneratePackageDoc";
639 #if defined(WIN32)
640         command += "\"";
641 #endif
642         command += " " + pack + " -q";
643     bbtkMessage("debug",1,"Executing system command '"<<command<<"'"<<std::endl);
644
645
646     if ( ! system ( command.c_str() ) )
647       {
648         SetStatusText( _T("Done !"));
649         /*
650         wxMessageBox(_T("Done !"),_T("Regenerate package '")
651                      +std2wx(pack)+_T("' doc"),
652                      wxOK | wxICON_INFORMATION);
653         */
654       }
655     else 
656       {
657         SetStatusText( _T("Done !"));
658         wxString err(_T("An error occured while running '"));
659         err +=  bbtk::std2wx(command) + _T("'");
660         wxMessageBox(err,_T("Regenerate package doc"),wxOK | wxICON_ERROR);      
661       }
662
663
664
665   }
666
667
668   //================================================================
669   void WxGUIScriptingInterface::OnMenuRegeneratePackageDoc(wxCommandEvent& WXUNUSED(event))
670   {
671     wxString name = wxGetTextFromUser(_T("Enter package name (-a for all)"),
672                                       _T("Regenerate package doc"),
673                                       _T(""));
674     if (name.IsEmpty()) return;
675     std::string pack = bbtk::wx2std(name);
676     
677         DoRegeneratePackageDoc(pack);
678   }
679   //================================================================
680
681
682   //================================================================
683   void WxGUIScriptingInterface::DoRegenerateBoxesLists()
684   {
685     SetStatusText( _T("Regenerating boxes lists ... please wait") );
686     BBTK_BUSY_CURSOR ;
687
688     std::string command;
689 #if defined(WIN32)
690         command = "\"";
691 #endif
692     command += ConfigurationFile::GetInstance().Get_bin_path();
693     command += ConfigurationFile::GetInstance().Get_file_separator();
694     command += "bbRegenerateBoxesLists";
695 #if defined(WIN32)
696         command += "\"";
697 #endif
698         command += " -q";
699         bbtkMessage("debug",1,"Executing system command '"<<command<<"'"<<std::endl);
700
701     if ( ! system ( command.c_str() ) )
702       {
703         SetStatusText( _T("Done !"));
704         /*
705         wxMessageBox(_T("Done !"),_T("Regenerate boxes lists"),
706                      wxOK | wxICON_INFORMATION);
707         */
708       }
709     else 
710       {
711         SetStatusText( _T("Done !"));
712         wxString err(_T("An error occured while running '"));
713         err +=  bbtk::std2wx(command) + _T("'");
714         wxMessageBox(err,_T("Regenerate boxes lists"),wxOK | wxICON_ERROR);      
715       }
716   }
717   //================================================================
718
719
720   //================================================================
721   void WxGUIScriptingInterface::OnMenuRegenerateBoxesLists(wxCommandEvent& WXUNUSED(event))
722   {
723         DoRegenerateBoxesLists();
724   }
725   //================================================================
726
727   //================================================================
728   void WxGUIScriptingInterface::OnMenuRegenerateAll(wxCommandEvent& WXUNUSED(event))
729   {
730         
731     DoRegeneratePackageDoc("-a");       
732         DoRegenerateBoxesLists();
733   }
734   //================================================================
735
736  
737   //================================================================
738   void WxGUIScriptingInterface::OnMenuPlugPackage(wxCommandEvent& WXUNUSED(event))
739   {
740     long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
741     wxDirDialog* FD = 
742       new wxDirDialog( 0, 
743                        _T("Select package directory"),
744                        _T(""),
745                        style);
746     
747     if (FD->ShowModal()==wxID_OK)
748       {
749         std::string path = wx2std (FD->GetPath()) ;
750         std::string fname = path + "/bbtkPackage"; 
751         if ( ! Utilities::FileExists( fname ) )
752           {
753             wxString err(_T("The directory does not contain a 'bbtkPackage' file"));
754             wxMessageBox(err,_T("Plug package"),wxOK | wxICON_ERROR);      
755             return;
756           }
757         
758         std::ifstream f;
759         f.open(fname.c_str());
760         std::string pname;
761         f >> pname;
762         f.close();
763         
764         
765         bbtk::ConfigurationFile::GetInstance().AddPackagePathsAndWrite( path );
766         
767         DoRegeneratePackageDoc(pname);
768         DoRegenerateBoxesLists();
769
770           }     
771   }
772   //================================================================
773
774   //================================================================
775   void WxGUIScriptingInterface::InterpreterUserViewHtmlPage(const std::string& page)
776   {
777     std::string s(page);
778     //  std::cout << "WxGUIScriptingInterface::ShowHtmlPage('"<<page<<"')"<<std::endl;
779     if (mWxGUIHtmlBrowser->GoTo(s)) 
780       {
781 //EED   mwxNotebook->ChangeSelection(1);
782 //      mwxNotebook->SetSelection(1);
783       }
784     else 
785       {
786          // std::cout << "ERROR html"<<std::endl;
787       }
788   } 
789   //================================================================  
790   
791   /*
792   //================================================================
793   void WxGUIScriptingInterface::InterpreterUserOnBreak()
794   {
795     //    std::cout << "Break" << std::endl;
796     
797   }
798   //================================================================
799   */
800
801   //================================================================  
802   bool WxGUIScriptingInterface::WxGUIHtmlBrowserUserOnLinkClicked(const std::string& target)
803   {
804     size_t s = target.length();
805     if ((s>3) && (target[s-1]=='s')
806                 && (target[s-2]=='b')
807                 && (target[s-3]=='b')
808                 && (target[s-4]=='.'))
809         {
810                         mWxGUITextEditor->Open(target);
811                         #if NOTEBOOK
812                                 mwxNotebook->SetSelection(0);
813                         #endif
814                         mWxGUITextEditor->SetFocus();
815                         return false;
816         }
817
818         if ((s>3) && (target[s-1]=='f')
819                 && (target[s-2]=='d')
820                 && (target[s-3]=='p')
821                 && (target[s-4]=='.'))
822         {
823
824                 // Open pdf in linux
825                   // gnome-open target
826
827                 // Open pdf in macOS
828
829                 // Open pdf in windows
830                         int size=target.size();
831                         int pos=target.rfind("\\");
832                         std::string commandStart("start ");
833                         std::string commandPath("/D \""+target.substr(0,pos) +"\"");
834                         std::string commandPDF( target.substr(pos+1,size-pos+1) );
835
836                         printf("EED WxGUIScriptingInterface::WxGUIHtmlBrowserUserOnLinkClicked cc %s \n",commandPath.c_str() );
837                         // cleanning path
838                         while (commandPath.rfind("\\\\")!=-1 )
839                         {
840                                 commandPath.erase( commandPath.rfind("\\\\") , 1 );
841                         printf("EED WxGUIScriptingInterface::WxGUIHtmlBrowserUserOnLinkClicked cc %s \n",commandPath.c_str() );
842                         }
843
844                         std::string command = commandStart+commandPath+" "+commandPDF;
845                         printf("EED WxGUIScriptingInterface::WxGUIHtmlBrowserUserOnLinkClicked BB %s \n",command.c_str() );
846                         system( command.c_str() );
847                         return false;
848      }
849     return true;
850   }
851   //================================================================  
852
853   void WxGUIScriptingInterface::OnMenuWindowsCheck( wxCommandEvent& event, 
854                                                     wxWindow* w)
855   {
856     bool checked = event.IsChecked();
857     bool shown = m_mgr.GetPane(w).IsShown();
858     //    std::cout << "checked = "<<checked<<" - shown = "<<shown<<std::endl;
859     if (checked ^ shown) 
860       {
861         m_mgr.GetPane(w).Show(checked);
862         m_mgr.Update();
863       }
864   }
865
866
867   //================================================================  
868   void WxGUIScriptingInterface::OnMenuWindowsFiles(wxCommandEvent& event)
869   {
870     OnMenuWindowsCheck(event,mWxGUITextEditor);
871   }
872   //================================================================  
873
874   //================================================================  
875   void  WxGUIScriptingInterface::OnMenuWindowsHelp(wxCommandEvent& event)
876   {
877     OnMenuWindowsCheck(event,mWxGUIHtmlBrowser);
878   }
879   //================================================================  
880
881   //================================================================  
882   void  WxGUIScriptingInterface::OnMenuWindowsMessages(wxCommandEvent& event)
883   {
884     OnMenuWindowsCheck(event,mWxGUIOutputMessages);
885   }
886   //================================================================  
887
888   //================================================================  
889   void  WxGUIScriptingInterface::OnMenuWindowsCommand(wxCommandEvent& event)
890   {
891     OnMenuWindowsCheck(event,mWxGUICommand);
892   }
893   //================================================================  
894
895   //================================================================  
896   void WxGUIScriptingInterface::OnMenuWindowsSave( wxCommandEvent& WXUNUSED(event))
897   {
898     SavePerspective();
899   }
900   //================================================================  
901
902   //================================================================  
903   void WxGUIScriptingInterface::SavePerspective()
904   {
905     //    std::cout  << "Saving configuration..."<<std::endl;
906     std::string conf = wx2std(m_mgr.SavePerspective());
907     std::string fname = Utilities::MakeUserSettingsFullFileName("bbStudio.aui");
908     std::ofstream f;
909     f.open(fname.c_str(), std::ios::out );
910     f << conf << std::endl;
911
912     //    int x,y;
913     //    GetPosition(&x,&y);
914     //    f << x << "  " << y << std::endl;
915
916
917     int w,h;
918     GetSize(&w,&h);
919
920     f << w << " " << h << std::endl;
921
922     f.close();
923   }
924   //================================================================  
925
926   //================================================================  
927   void WxGUIScriptingInterface::LoadPerspective()
928   {
929     std::string fname = 
930       Utilities::MakeUserSettingsFullFileName("bbStudio.aui");
931     if ( Utilities::FileExists( fname ))
932       {
933         //      std::cout  << "Loading configuration..."<<std::endl;
934
935         std::ifstream f;
936         f.open(fname.c_str());
937         std::string conf;
938         f >> conf;
939
940         //      int x,y;
941         //      f >> x >> y ;
942
943         int w,h;
944         f >> w >> h ;
945
946         f.close();
947         
948         //      std::cout << conf << std::endl;
949
950         //      Move(x,y);
951         SetSize(w,h);
952         m_mgr.LoadPerspective(std2wx(conf));
953       }
954     else 
955       {
956         m_mgr.Update();
957       }
958   }
959   //================================================================  
960
961   //================================================================  
962   void WxGUIScriptingInterface::OnPaneClose(wxAuiManagerEvent& evt)
963   {
964     std::cout  << "Closing panel '"<<evt.pane->name<<"'"<<std::endl;
965     // TO DO : Uncheck 
966     //    OnMenuWindowsCheck( wxCommandEvent& event, wxWindow* w);
967   }
968   //================================================================  
969
970   
971   //================================================================  
972   BEGIN_EVENT_TABLE(WxGUIScriptingInterface, wxFrame)
973     EVT_CLOSE(  WxGUIScriptingInterface::OnClose)
974     EVT_MENU(ID_Menu_Quit, WxGUIScriptingInterface::OnMenuQuit)
975     EVT_MENU(ID_Menu_About, WxGUIScriptingInterface::OnMenuAbout)
976     EVT_MENU(ID_Menu_EditConfig, WxGUIScriptingInterface::OnMenuEditConfig)
977     EVT_MENU(ID_Menu_CreatePackage, WxGUIScriptingInterface::OnMenuCreatePackage)
978     EVT_MENU(ID_Menu_CreateBlackBox, WxGUIScriptingInterface::OnMenuCreateBlackBox)
979     EVT_MENU(ID_Menu_ShowImageGraph, WxGUIScriptingInterface::OnMenuShowImageGraph)
980     EVT_MENU(ID_Menu_RegeneratePackageDoc, WxGUIScriptingInterface::OnMenuRegeneratePackageDoc)
981     EVT_MENU(ID_Menu_RegenerateBoxesLists, WxGUIScriptingInterface::OnMenuRegenerateBoxesLists)
982     EVT_MENU(ID_Menu_RegenerateAll, WxGUIScriptingInterface::OnMenuRegenerateAll)
983     EVT_MENU(ID_Menu_PlugPackage, WxGUIScriptingInterface::OnMenuPlugPackage)
984     EVT_MENU(ID_Menu_Windows_Files, WxGUIScriptingInterface::OnMenuWindowsFiles)
985     EVT_MENU(ID_Menu_Windows_Help, WxGUIScriptingInterface::OnMenuWindowsHelp)
986     EVT_MENU(ID_Menu_Windows_Messages, WxGUIScriptingInterface::OnMenuWindowsMessages)
987     EVT_MENU(ID_Menu_Windows_Command, WxGUIScriptingInterface::OnMenuWindowsCommand)
988     EVT_MENU(ID_Menu_Windows_Save, WxGUIScriptingInterface::OnMenuWindowsSave)
989     EVT_MENU(ID_Menu_Windows_PackageBrowser, WxGUIScriptingInterface::OnMenuWindowsPackageBrowser)
990     EVT_AUI_PANE_CLOSE(WxGUIScriptingInterface::OnPaneClose)
991   //    EVT_BUTTON(ID_Button_Run, WxGUIScriptingInterface::OnButtonRun )
992     END_EVENT_TABLE()
993   //================================================================
994
995 } // namespace bbtk
996
997
998 #endif //_USE_WXWIDGETS_