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