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