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