]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/bbtkwxGUIEditorGraphicBBS.cxx
Bug #1878
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsWxGUIEditorGraphic / bbtkwxGUIEditorGraphicBBS.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 #
8 #  This software is governed by the CeCILL-B license under French law and 
9 #  abiding by the rules of distribution of free software. You can  use, 
10 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
11 #  license as circulated by CEA, CNRS and INRIA at the following URL 
12 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
13 #  or in the file LICENSE.txt.
14 #
15 #  As a counterpart to the access to the source code and  rights to copy,
16 #  modify and redistribute granted by the license, users are provided only
17 #  with a limited warranty  and the software's author,  the holder of the
18 #  economic rights,  and the successive licensors  have only  limited
19 #  liability. 
20 #
21 #  The fact that you are presently reading this means that you have had
22 #  knowledge of the CeCILL-B license and that you accept its terms.
23 # ------------------------------------------------------------------------  
24 */
25
26 /*=========================================================================
27  Program:   bbtk
28  Module:    $RCSfile$
29  Language:  C++
30  Date:      $Date$
31  Version:   $Revision$
32  =========================================================================*/
33
34 /* ---------------------------------------------------------------------
35
36  * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
37  * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
38  *
39  *  This software is governed by the CeCILL-B license under French law and
40  *  abiding by the rules of distribution of free software. You can  use,
41  *  modify and/ or redistribute the software under the terms of the CeCILL-B
42  *  license as circulated by CEA, CNRS and INRIA at the following URL
43  *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
44  *  or in the file LICENSE.txt.
45  *
46  *  As a counterpart to the access to the source code and  rights to copy,
47  *  modify and redistribute granted by the license, users are provided only
48  *  with a limited warranty  and the software's author,  the holder of the
49  *  economic rights,  and the successive licensors  have only  limited
50  *  liability.
51  *
52  *  The fact that you are presently reading this means that you have had
53  *  knowledge of the CeCILL-B license and that you accept its terms.
54  * ------------------------------------------------------------------------ */
55
56 /**
57  *  \file
58  *  \brief Class bbtk::WxInterfaceEditorGraphicBBS .
59  */
60
61 #include "bbtkwxGUIEditorGraphicBBS.h"
62 #include "creaWx.h"
63 #include "creaSystem.h"
64 //must go #include "bbtkUtilities.h"
65 //must go #include "bbtkConfigurationFile.h"
66 #include <wx/defs.h>
67
68 #include <InterpreterBBS.h>
69
70 namespace bbtk {
71
72 //=========================================================================
73 wxGUIEditorGraphicBBS::wxGUIEditorGraphicBBS(wxFrame *parent) 
74         : wxFrame( parent, -1, _T("bbtkGEditor"), wxDefaultPosition, wxSize(1200,800) ) 
75 {
76         _pkgBrowser = NULL;
77         _frameAUIMgr = new wxAuiManager(this);
78
79         /*std::string datadir( crea::System::GetExecutablePath() );
80          std::string datadir (".");
81          cout<<"RaC //------------------"<<endl;
82          cout<<"RaC //TODO wxGUIEditorGraphicBBS constructor.Initialize datadir with  crea::System::GetExecutablePath() ."<<endl;
83          cout<<"RaC //------------------"<<endl;
84
85          #ifdef LINUX // assume this is OSX
86          datadir=datadir+"/../share/creaContours";
87          #endif // MACOSX
88
89          #ifdef MACOSX // assume this is OSX
90          datadir=datadir+"/../../../../share/creaContours";
91          #endif // MACOSX
92
93          _dataDir = datadir;*/
94
95         initMenu();
96         initToolbar();
97         initPackageBrowser();
98         initTabPanelsManager();
99         // JGGR & CM  initHelpHTMLBrowser();
100         _helpHtmlBrowser = NULL;
101         CreateStatusBar();
102         _frameAUIMgr->Update();
103         _actualPkgBrowserBoxName = "";
104         _actualPkgBrowserPkgName = "";
105         refreshGUIControls();
106
107         /////////////////////////////////
108         /// IMPORTANT!! FOR SAVE&LOAD PROCESSES THE VERSION IS VERY IMPORTANT.
109         /// RaC 2012 2nd Hackfest
110         _currentBBGversion="1.4";
111 }
112
113 //=========================================================================
114 wxGUIEditorGraphicBBS::~wxGUIEditorGraphicBBS() {
115
116         printf("EED %p ~wxGUIEditorGraphicBBS()\n", this);
117
118         //EED02JUIN2010         delete _tabsMgr;
119
120         _frameAUIMgr->UnInit();
121         // FCY memory leaks
122         delete _frameAUIMgr;
123         delete _notebook;
124         delete _tabsMgr;
125
126 }
127
128 //=========================================================================
129 void wxGUIEditorGraphicBBS::initToolbar() {
130         wxBitmap bmp_new(new_xpm);
131         wxBitmap bmp_open(open_xpm);
132         wxBitmap bmp_save(save_xpm);
133         wxBitmap bmp_run(run_xpm);
134         wxBitmap bmp_delete(delete_xpm);
135         wxBitmap bmp_centerview(centerview_xpm);
136         wxBitmap bmp_box(box_xpm);
137         wxBitmap bmp_executablebox(executablebox_xpm);
138         wxBitmap bmp_complexbox(complexbox_xpm);
139         wxBitmap bmp_complexinputport(complexinputport_xpm);
140         wxBitmap bmp_complexoutputport(complexoutputport_xpm);
141         wxBitmap bmp_undo(undo_xpm);
142         wxBitmap bmp_redo(redo_xpm);
143         wxBitmap bmp_editProperties(editProperties_xpm);
144         wxToolBar *toolbar = new wxToolBar(this, wxID_ANY);
145
146         //Adds a tool btn to the toolbar
147         toolbar->AddTool(ID_NEW, _T("New"), bmp_new, wxNullBitmap, wxITEM_NORMAL,
148                         _T("New tab"), _T("Create a new panel tab"));
149         toolbar->AddTool(ID_OPEN, _T("Open diagram"), bmp_open, wxNullBitmap,
150                         wxITEM_NORMAL, _T("Open a diagram"), _T("Open a diagram"));
151         toolbar->AddTool(ID_SAVE_DIAGRAM, _T("Save Diagram"), bmp_save,
152                         wxNullBitmap, wxITEM_NORMAL, _T("Saves the current diagram"),
153                         _T("Saves the current diagram")); //DFCH
154         //toolbar->AddTool(ID_SAVE_AS_DIAGRAM,_T("Save Diagram As"),bmp_save, wxNullBitmap, wxITEM_NORMAL,_T("Saves the current diagram"), _T("Saves the current diagram")); //DFCH
155         toolbar->AddTool(ID_RUN, _T("Run"), bmp_run, wxNullBitmap, wxITEM_NORMAL,
156                         _T("Execute actual diagram"), _T("Execute actual diagram"));
157         toolbar->AddTool(ID_DELETEALL, _T("Delete all"), bmp_delete, wxNullBitmap,
158                         wxITEM_NORMAL, _T("Delete all boxes"), _T("Delete all boxes"));
159         toolbar->AddTool(ID_CENTERVIEW, _T("Center view"), bmp_centerview,
160                         wxNullBitmap, wxITEM_NORMAL, _T("Center view"), _T("Center view"));
161         toolbar->AddSeparator();
162         toolbar->AddTool(ID_BTNBOX, _T("New Box"), bmp_box, wxNullBitmap,
163                         wxITEM_NORMAL, _T("New Box"), _T("Nex Box"));
164         toolbar->AddSeparator();
165         toolbar->AddTool(ID_BTEXECUTABLEBOX, _T("Select as executable Box"),
166                         bmp_executablebox, wxNullBitmap, wxITEM_NORMAL,
167                         _T("Select as executable Box"), _T("Select as executable Box"));
168         toolbar->AddSeparator();
169         toolbar->AddTool(ID_BTNCOMPLEXBOX, _T("Complex box"), complexbox_xpm,
170                         wxNullBitmap, wxITEM_NORMAL, _T("Complex box"), _T("Complex box"));
171         toolbar->SetToggle(ID_BTNCOMPLEXBOX, true);
172
173         // RaC 2012 2nd HackFest - Now Complex inputs are any external input
174         //              toolbar->AddTool(ID_ADDCOMPLEXBOXINPUT, _T("Add input to Complex box"),
175         //              bmp_complexinputport, wxNullBitmap, wxITEM_NORMAL,
176         //              _T("Add input to Complex box"), _T("Add input to Complex box"));
177         toolbar->AddTool(ID_ADDCOMPLEXBOXINPUT, _T("Add external input (Complex box or console app)"),
178                         bmp_complexinputport, wxNullBitmap, wxITEM_NORMAL,
179                         _T("Add external input (Complex box or console app)"), _T("Add external input (Complex box or console app)"));
180
181         toolbar->AddTool(ID_ADDCOMPLEXBOXOUTPUT, _T("Add output to Complex box"),
182                         bmp_complexoutputport, wxNullBitmap, wxITEM_NORMAL,
183                         _T("Add output Complex box"), _T("Add output Complex box"));
184
185         // toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, false); RaC2012 2nd HackFest
186         toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, true);
187
188         toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT, false);
189         toolbar->AddSeparator();
190         toolbar->AddTool(ID_UNDO, _T("Undo"),bmp_undo, wxNullBitmap, wxITEM_NORMAL,     _T("Undo"), _T("Undo"));
191         toolbar->AddTool(ID_REDO, _T("Redo"),bmp_redo, wxNullBitmap, wxITEM_NORMAL,     _T("Redo"), _T("Redo"));
192         toolbar->AddSeparator();
193         toolbar->AddTool(ID_EDITPROPERTIES, _T("Edit diagram properties"),bmp_editProperties, wxNullBitmap, wxITEM_NORMAL,      _T("Edit diagram properties"), _T("Edit diagram properties"));
194         toolbar->EnableTool(ID_EDITPROPERTIES, false);
195         
196         toolbar->EnableTool(ID_UNDO, false);
197         toolbar->EnableTool(ID_REDO, false);
198         toolbar->SetMargins(2, 2);
199         toolbar->Realize();
200         SetToolBar(toolbar);
201
202         
203         // connect command event handlers
204
205         Connect(ID_NEW, wxEVT_COMMAND_TOOL_CLICKED,
206                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCreateNewTab));
207         Connect(ID_OPEN, wxEVT_COMMAND_TOOL_CLICKED,
208                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnOpenDiagram));
209         Connect(ID_SAVE_DIAGRAM, wxEVT_COMMAND_TOOL_CLICKED,
210                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveActualDiagram)); //DFCH
211         Connect(ID_SAVE_AS_DIAGRAM, wxEVT_COMMAND_TOOL_CLICKED,
212                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveAsActualDiagram)); //DFCH
213         Connect(
214                         ID_RUN,
215                         wxEVT_COMMAND_TOOL_CLICKED,
216                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnExecuteActualDiagram));
217         Connect(
218                         ID_DELETEALL,
219                         wxEVT_COMMAND_TOOL_CLICKED,
220                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnDeleteAllBoxesActualDiagram));
221         Connect(
222                         ID_CENTERVIEW,
223                         wxEVT_COMMAND_TOOL_CLICKED,
224                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCenterViewActualDiagram));
225         Connect(ID_BTNBOX, wxEVT_COMMAND_TOOL_CLICKED,
226                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnClickBtnBox));
227         Connect(
228                         ID_BTEXECUTABLEBOX,
229                         wxEVT_COMMAND_TOOL_CLICKED,
230                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnClickBtnExecutableBox));
231         Connect(ID_BTNCOMPLEXBOX, wxEVT_COMMAND_TOOL_CLICKED,
232                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnClickBtnComplexBox));
233         Connect(ID_ADDCOMPLEXBOXINPUT, wxEVT_COMMAND_TOOL_CLICKED,
234                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnAddComplexBoxInput));
235         Connect(ID_ADDCOMPLEXBOXOUTPUT, wxEVT_COMMAND_TOOL_CLICKED,
236                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnAddComplexBoxOutput));
237         Connect(ID_UNDO,wxEVT_COMMAND_TOOL_CLICKED,     wxCommandEventHandler(wxGUIEditorGraphicBBS::OnUndo));
238         Connect(ID_REDO,wxEVT_COMMAND_TOOL_CLICKED,     wxCommandEventHandler(wxGUIEditorGraphicBBS::OnRedo));
239         Connect(ID_CHANGENAME, wxEVT_COMMAND_RIGHT_CLICK, wxCommandEventHandler(wxGUIEditorGraphicBBS::OnChangeName));
240         Connect(ID_EDITPROPERTIES,wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler(wxGUIEditorGraphicBBS::OnEditDiagramProperties));
241
242 }
243 //=========================================================================
244 void wxGUIEditorGraphicBBS::initHelpHTMLBrowser() {
245         // JGRR & CM 
246         _helpHtmlBrowser = new WxGUIHtmlBrowser(this, wxSize(200, 0));
247    
248         // EO JGRR & CM 
249         
250 }
251
252 //=========================================================================
253 void wxGUIEditorGraphicBBS::initMenu() {
254         // Create File menu and its items
255         wxMenu *fileMenu = new wxMenu();
256         fileMenu->Append(ID_NEW, _T("New diagram"), _T("New"));
257         fileMenu->Append(ID_SAVE_DIAGRAM, _T("Save Diagram"), _T("Save Diagram")); //DFCH
258         fileMenu->Append(ID_SAVE_AS_DIAGRAM, _T("Save Diagram As"),
259                         _T("Save Diagram As")); //DFCH
260         fileMenu->Append(ID_OPEN_BBS, _T("Open BBS"), _T("Open BBS"));
261         // CM
262         fileMenu->Append(ID_OPEN, _T("Open a diagram"), _T("Open a diagram"));
263         fileMenu->Append(ID_SAVE_AS_BBS, _T("Save BBS"), _T("Save BBS"));
264         fileMenu->Append(ID_SAVE_AS_COMPLEXBOX, _T("Save complex box"),
265                         _T("Save complex box"));
266         fileMenu->Append(ID_EXPORT_CONSOLEAPP, _T("Export console commands"),
267                                 _T("Export console execution commands"));
268         fileMenu->Append(wxID_EXIT, _T("E&xit"), _T("Exit"));
269
270         Connect(ID_NEW, wxEVT_COMMAND_MENU_SELECTED,
271                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCreateNewTab));
272         Connect(ID_SAVE_DIAGRAM, wxEVT_COMMAND_MENU_SELECTED,
273                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveActualDiagram)); //DFCH
274         Connect(ID_SAVE_AS_DIAGRAM, wxEVT_COMMAND_MENU_SELECTED,
275                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveAsActualDiagram));
276         Connect(ID_OPEN_BBS, wxEVT_COMMAND_MENU_SELECTED,
277                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnOpenBBS));
278         Connect(ID_SAVE_AS_BBS, wxEVT_COMMAND_MENU_SELECTED,
279                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveActualBBS));
280         Connect( ID_SAVE_AS_COMPLEXBOX, wxEVT_COMMAND_MENU_SELECTED,
281                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveActualComplexBox));
282         Connect( ID_EXPORT_CONSOLEAPP, wxEVT_COMMAND_MENU_SELECTED,
283                                 wxCommandEventHandler(wxGUIEditorGraphicBBS::OnExportConsoleExecCommand));
284         Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
285                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnExit));
286
287         // Create Edit menu and its items
288         wxMenu *editMenu = new wxMenu;
289         editMenu->Append(ID_COPY_TO_COMPLEXDIAGRAM, _T("Copy selected to complex box"), _T("Creates a new complex box diagram with selected boxes"));
290         editMenu->Append(ID_EDIT_COMPLEXBOX_SCRIPT, _T("Edit Complex Box"), _T("Edit Complex Box"));
291         Connect( ID_COPY_TO_COMPLEXDIAGRAM, wxEVT_COMMAND_MENU_SELECTED,
292                          wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCopySelectedToComplexDiagram));
293         Connect( ID_EDIT_COMPLEXBOX_SCRIPT, wxEVT_COMMAND_MENU_SELECTED,
294                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnEditComplexBoxScript));
295
296         // Create Help menu and its items
297         wxMenu *helpMenu = new wxMenu;
298         helpMenu->Append(wxID_ABOUT, _T("&About..."), _T("About"));
299         helpMenu->Append(ID_HELP_BBEDITOR, _T("&Help..."), _T("Help"));
300         Connect(ID_HELP_BBEDITOR, wxEVT_COMMAND_MENU_SELECTED,
301                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnHelpBBeditor));
302
303         // Create Tool menu
304         wxMenu *menuTools = new wxMenu;
305         menuTools->Append(ID_bbEditor_Tool_Menu_CreatePackage,
306                         _T("Create &package") );
307         menuTools->Append(ID_bbEditor_Tool_Menu_CreateBlackBox,
308                         _T("Create &blackbox") );
309         menuTools->Append(ID_bbEditor_Tool_Menu_PlugPackage, _T("Plug package") );
310         menuTools->Append(ID_bbEditor_Tool_Menu_EditConfig, _T("&Edit bbtk config") );
311         menuTools->Append(ID_bbEditor_Tool_Menu_ShowHTMLDoc,
312                         _T("Show &HTML documentation") );
313         menuTools->Append(ID_bbEditor_Tool_Menu_CreateIndex, _T("&Generate index") );
314
315         Connect(ID_bbEditor_Tool_Menu_CreatePackage, wxEVT_COMMAND_MENU_SELECTED,
316                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCreatePackage));
317         Connect(ID_bbEditor_Tool_Menu_CreateBlackBox, wxEVT_COMMAND_MENU_SELECTED,
318                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCreateBlackBox));
319         Connect(ID_bbEditor_Tool_Menu_PlugPackage, wxEVT_COMMAND_MENU_SELECTED,
320                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnPlugPackage));
321         Connect(ID_bbEditor_Tool_Menu_EditConfig, wxEVT_COMMAND_MENU_SELECTED,
322                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnEditConfig));
323         Connect(ID_bbEditor_Tool_Menu_ShowHTMLDoc, wxEVT_COMMAND_MENU_SELECTED,
324                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnShowHTMLDoc));
325         Connect(ID_bbEditor_Tool_Menu_CreateIndex, wxEVT_COMMAND_MENU_SELECTED,
326                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCreateIndex));
327
328         // Append the created menu to the menu bar
329         wxMenuBar *menuBar = new wxMenuBar();
330         menuBar->Append(fileMenu, _T("&File") );
331         menuBar->Append(editMenu, _T("&Edit") );
332         menuBar->Append(menuTools, _T("&Tool") );
333         menuBar->Append(helpMenu, _T("&Help") );
334
335         // Attach this menu bar to the frame
336         SetMenuBar(menuBar);
337 }
338
339 //=========================================================================
340 void wxGUIEditorGraphicBBS::initTabPanelsManager() 
341 {
342         _notebook = new wxAuiNotebook(this, wxID_NOTEBOOK, wxDefaultPosition,
343                         wxDefaultSize, wxAUI_NB_TOP | wxAUI_NB_TAB_MOVE
344                                         | wxAUI_NB_CLOSE_ON_ACTIVE_TAB | wxAUI_NB_TAB_FIXED_WIDTH
345                                         | wxAUI_NB_WINDOWLIST_BUTTON);
346         _tabsMgr = new wxTabPanelsManager(this);
347         _frameAUIMgr->AddPane(_notebook, wxAuiPaneInfo().CenterPane());
348 }
349
350 //=========================================================================
351 void wxGUIEditorGraphicBBS::initPackageBrowser() 
352 {
353         _pkgBrowser = new WxGUIPackageBrowser2(this);
354         _pkgBrowser->IncludeAll();
355         _frameAUIMgr->AddPane(_pkgBrowser, wxAuiPaneInfo().Left().MinSize(30, 500).BestSize(750,500).CloseButton(false));
356 }
357
358 //================================================================
359
360 void wxGUIEditorGraphicBBS::DoRegeneratePackageDoc(const std::string& pack) 
361 {
362         std::string mess("Regenerating doc for package '");
363         if (pack != "-a") {
364                 mess += pack + "'";
365         } else {
366                 mess = "Regenerating doc for all packages";
367         }
368
369         mess += " ... please wait";
370
371         SetStatusText(std2wx(mess));
372
373         //BBTK_BUSY_CURSOR;
374
375         std::string command;
376 #if defined(WIN32)
377         command = "\"";
378 #endif
379         command += ConfigurationFile::GetInstance().Get_bin_path();
380         command += ConfigurationFile::GetInstance().Get_file_separator();
381         command += "bbRegeneratePackageDoc";
382 #if defined(WIN32)
383         command += "\"";
384 #endif
385
386         command += " " + pack + " -q";
387         bbtkMessage("debug",1,"Executing system command '"<<command<<"'"<<std::endl);
388
389         if (!system(command.c_str())) {
390                 SetStatusText(_T("Done !"));
391                 /*
392                  wxMessageBox(_T("Done !"),_T("Regenerate package '")
393                  +std2wx(pack)+_T("' doc"),
394                  wxOK | wxICON_INFORMATION);
395                  */
396         } else {
397                 SetStatusText(_T("Done !"));
398                 wxString err(_T("An error occured while running '"));
399                 err += bbtk::std2wx(command) + _T("'");
400                 wxMessageBox(err, _T("Regenerate package doc"), wxOK | wxICON_ERROR);
401         }
402 }
403
404 //================================================================
405 void wxGUIEditorGraphicBBS::DoRegenerateBoxesLists() {
406         SetStatusText(_T("Regenerating boxes lists ... please wait"));
407         //BBTK_BUSY_CURSOR ;
408
409         std::string command;
410 #if defined(WIN32)
411         command = "\"";
412 #endif
413         command += ConfigurationFile::GetInstance().Get_bin_path();
414         command += ConfigurationFile::GetInstance().Get_file_separator();
415         command += "bbRegenerateBoxesLists";
416
417 #if defined(WIN32)
418         command += "\"";
419 #endif
420         command += " -q";
421
422         bbtkMessage("debug",1,"Executing system command '"<<command<<"'"<<std::endl);
423
424         if (!system(command.c_str())) {
425                 SetStatusText(_T("Done !"));
426                 /*
427                  wxMessageBox(_T("Done !"),_T("Regenerate boxes lists"),
428                  wxOK | wxICON_INFORMATION);
429                  */
430         } else {
431                 SetStatusText(_T("Done !"));
432                 wxString err(_T("An error occured while running '"));
433                 err += bbtk::std2wx(command) + _T("'");
434                 wxMessageBox(err, _T("Regenerate boxes lists"), wxOK | wxICON_ERROR);
435         }
436 }
437
438 //================================================================
439 void wxGUIEditorGraphicBBS::RegenerateAll() {
440         DoRegeneratePackageDoc("-a");
441         DoRegenerateBoxesLists();
442 }
443
444 //================================================================
445
446 wxAuiNotebook* wxGUIEditorGraphicBBS::getAuiNotebook() {
447         return _notebook;
448 }
449
450 //================================================================
451
452 void wxGUIEditorGraphicBBS::displayBlackBoxInfo(std::string packageName, std::string boxName) 
453 {
454         if (_actualPkgBrowserPkgName != packageName || _actualPkgBrowserBoxName != boxName) 
455         {
456                 _actualPkgBrowserPkgName = packageName;
457                 _actualPkgBrowserBoxName = boxName;
458
459 //EED 15 oct 2012               BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxName);
460                 
461                 Package::Pointer k;
462                 BlackBoxDescriptor::Pointer descriptor;
463                 std::map< std::string, BlackBoxDescriptor::Pointer> mapDesc;
464                 k                       = _pkgBrowser->GetFactory()->GetPackage(packageName);
465                 mapDesc         = k->GetDescriptorMap();                
466                 descriptor      = mapDesc[boxName];
467                 
468                 _pkgBrowser->WxGUIBlackBoxListUserOnSelected(descriptor.get());
469         }
470 }
471
472 //=========================================================================
473
474 void wxGUIEditorGraphicBBS::editBlackBox(GBlackBoxModel *bbmodel) {
475         wxBlackBoxEditionDialog* dialog =
476                         new wxBlackBoxEditionDialog(this, bbmodel);
477         dialog->Show();
478 }
479
480 //=========================================================================
481
482 bool wxGUIEditorGraphicBBS::boxNameExists(std::string boxname) {
483  
484         return this->_tabsMgr->getActualTabPanel()->getSceneManager()->boxExist(boxname);
485 }
486
487 //=========================================================================
488
489 void wxGUIEditorGraphicBBS::updateStatusBar(std::string textStatus) {
490         SetStatusText(crea::std2wx(textStatus));
491 }
492
493 //=========================================================================
494
495 void wxGUIEditorGraphicBBS::executeScript(std::string script) {
496         std::string separator =
497                         ConfigurationFile::GetInstance().Get_file_separator();
498         std::string dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
499         std::string filename = dir + separator + "tmp_bbtk.bbs";
500
501         ofstream tempFile;
502         tempFile.open(filename.c_str());
503         tempFile << script;
504         tempFile.close();
505
506         std::string command = "";
507
508 #ifdef WIN32
509         command += "start /b ";
510 #endif
511
512         //              command += "\""+ crea::System::GetExecutablePath();
513         //#ifdef MACOSX
514         //              command += separator + "../../../bbi.app/Contents/MacOS";
515         //#endif
516
517         command += "bbi ";
518         command += "\"" + filename + "\"";
519
520 #ifndef WIN32
521         command += " & ";
522 #endif
523
524         printf("RaC wxGUIEditorGraphicBBS::executeScript %s \n", command.c_str());
525         system(command.c_str());
526 }
527
528 //=========================================================================
529 std::string wxGUIEditorGraphicBBS::CheckExtension(std::string filename, std::string extension) {
530         if(filename.size() > extension.size() ){
531                 if( (filename.compare(filename.size()-extension.size(), extension.size(),extension)) !=0 ){
532                         filename.append(extension);
533                 }
534         }else{
535                 filename.append(extension);
536         }
537         return filename;
538 }
539 //=========================================================================
540 // EVENT HANDLERS
541 //=========================================================================
542
543 void wxGUIEditorGraphicBBS::OnCreateNewTab(wxCommandEvent& event) {
544         _tabsMgr->addNewTab();
545         refreshGUIControls();
546 }
547
548 void wxGUIEditorGraphicBBS::OnUndo(wxCommandEvent& event) {
549         unsigned short disable;
550         std::string stundo;
551         std::string stredo;
552
553         _tabsMgr->loadTempDiagram(0);
554         disable = _tabsMgr->getUndoState(stundo);
555         if(disable == 0)
556         {
557                 std::string name = _tabsMgr->GetNameTabPanel();
558                 if(name[name.length() - 1] == '*')
559                 {
560                         name =name.substr(0,name.length()-1);
561                 }
562                 _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
563         }
564
565         GetToolBar()->EnableTool(ID_UNDO,disable);
566         GetToolBar()->SetToolShortHelp(ID_UNDO,crea::std2wx(stundo));
567         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState(stredo));
568         GetToolBar()->SetToolShortHelp(ID_REDO,crea::std2wx(stredo));
569         refreshGUIControls();
570 }
571
572 //=========================================================================
573
574 void wxGUIEditorGraphicBBS::OnRedo(wxCommandEvent& event) {
575         unsigned short disable;
576         std::string stundo;
577         std::string stredo;
578
579         _tabsMgr->loadTempDiagram(1);
580         std::string name = _tabsMgr->GetNameTabPanel();
581         if(name[name.length() - 1] != '*')
582                 name +="*";
583         _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
584         GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState(stundo));
585         GetToolBar()->SetToolShortHelp(ID_UNDO,crea::std2wx(stundo));
586         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState(stredo));
587         GetToolBar()->SetToolShortHelp(ID_REDO,crea::std2wx(stredo));
588         refreshGUIControls();
589 }
590
591 void wxGUIEditorGraphicBBS::OnChangeName(wxCommandEvent& event){
592 }
593
594 //=========================================================================
595
596 void wxGUIEditorGraphicBBS::OnEditDiagramProperties(wxCommandEvent& event)
597 {
598         wxDiagramPropertiesEditionDialog* dialog =
599                                 new wxDiagramPropertiesEditionDialog(this);
600         dialog->Show();
601 }
602
603 //=========================================================================
604
605 void wxGUIEditorGraphicBBS::OnExecuteActualDiagram(wxCommandEvent& event) {
606         std::string script1 = _tabsMgr->getActualDiagramBBS(true);
607         cout << endl << "RaC wxGUIEditorGraphicBBS::executeActualDiagram SCRIPT"
608                         << endl << endl;
609         cout << script1 << endl;
610
611         std::string script2 = _tabsMgr->getActualDiagramBBS(false);
612         executeScript(script2);
613 }
614
615         
616 //=========================================================================
617 void wxGUIEditorGraphicBBS::OpenBBS(std::string filePathName, std::string fileName) 
618 {
619         _tabsMgr->addNewTab( crea::std2wx(fileName) );
620         bbtk::InterpreterBBS::Pointer I = bbtk::InterpreterBBS::New( this->_tabsMgr->getActualTabPanel()->getSceneManager(), _pkgBrowser->GetFactory());
621         // We tell the interpreter to throw exceptions on error
622         I->SetThrow(true);
623         // Interpret the file supposed to define a box called 'Processing'
624         I->InterpretFile( filePathName );
625         refreshGUIControls();
626 }
627         
628 //=========================================================================
629 // HANDLERS
630 //=========================================================================
631 void wxGUIEditorGraphicBBS::OnOpenBBS(wxCommandEvent& event) 
632 {
633         wxFileDialog * openFileDialog = new wxFileDialog(this, wxT("Open BBS"), wxEmptyString, wxT("NewBBS"), wxT("*.bbs"), wxOPEN);
634
635         if (openFileDialog->ShowModal() == wxID_OK) 
636         {
637                 OpenBBS( crea::wx2std(openFileDialog->GetPath()) , crea::wx2std(openFileDialog->GetFilename())  );
638         }
639         
640 /*EED Borrame   
641         if (openFileDialog->ShowModal() == wxID_OK) 
642         {
643                 wxString fileName = openFileDialog->GetPath();
644                 _tabsMgr->addNewTab(openFileDialog->GetFilename());
645                 bbtk::InterpreterBBS::Pointer I = bbtk::InterpreterBBS::New( this->_tabsMgr->getActualTabPanel()->getSceneManager(), _pkgBrowser->GetFactory());
646                 // We tell the interpreter to throw exceptions on error
647                 I->SetThrow(true);
648                 // Interpret the file supposed to define a box called 'Processing'
649                 I->InterpretFile((const char*) (fileName.mb_str()));
650         }
651         refreshGUIControls();
652 */ 
653 }
654
655 //=========================================================================
656 void wxGUIEditorGraphicBBS::SaveActualBBS(std::string filename) {
657         ofstream file;
658
659         //EED           file.open(fileName.c_str());
660         file.open(filename.c_str());
661         std::string content = "";
662         // writing file header
663         content += "# ----------------------------------\n";
664         content += "# - BBTKGEditor v ";
665         content += _currentBBGversion; //VERSION
666         content += " BBS BlackBox Script\n";
667         content += "# - ";
668         content += filename;
669         content += "\n";
670         content += "# ----------------------------------\n";
671         content += "\n";
672         content += _tabsMgr->getActualDiagramBBS();
673         file << content;
674         file.close();
675 }
676
677 void wxGUIEditorGraphicBBS::OnSaveActualBBS(wxCommandEvent& event) {
678         wxFileDialog * saveFileDialog = new wxFileDialog(this,
679                         wxT("Save  actual BBS"), wxEmptyString, wxT("NewBBS"),
680                         wxT("*.bbs"), wxSAVE | wxOVERWRITE_PROMPT);
681         if (saveFileDialog->ShowModal() == wxID_OK) {
682                 wxString fileName = saveFileDialog->GetPath();
683                 std::string filename = (const char*) (fileName.mb_str());
684                 filename=CheckExtension (filename, ".bbs");
685                 SaveActualBBS(filename);
686         }
687
688 }
689
690 void wxGUIEditorGraphicBBS::AskComplexBoxConfiguration() {
691         std::string cbName = _tabsMgr->GetCbName();
692         std::string paName = _tabsMgr->GetCbPackageName();
693
694         wxTextEntryDialog *cbNameDialog = new wxTextEntryDialog(this,
695                         wxT("Complex Box name"));
696         wxTextEntryDialog *paNameDialog = new wxTextEntryDialog(this,
697                         wxT("Package name"));
698
699         cbNameDialog->SetValue(wxString(cbName.c_str(), wxConvUTF8));
700         paNameDialog->SetValue(wxString(paName.c_str(), wxConvUTF8));
701
702         if (cbNameDialog->ShowModal() == wxID_OK) {
703                 wxString complexboxname = cbNameDialog->GetValue();
704                 cbName = (const char*) (complexboxname.mb_str());
705         }
706
707         if (paNameDialog->ShowModal() == wxID_OK) {
708                 wxString packagename = paNameDialog->GetValue();
709                 paName = (const char*) (packagename.mb_str());
710         }
711
712         _tabsMgr->SetCbName(cbName);
713         _tabsMgr->SetCbPackageName(paName);
714 }
715
716 //=========================================================================
717
718 void wxGUIEditorGraphicBBS::SaveActualComplexBox(std::string filename) {
719         ofstream file;
720         file.open(filename.c_str());
721
722         std::string content = "";
723
724         // writing file header
725         content += "# ----------------------------------\n";
726         content += "# - BBTKGEditor v ";
727         content += _currentBBGversion;//VERSION
728         content += " BBS BlackBox Script (Complex Box)\n";
729         content += "# - ";
730         content += filename;
731         content += "\n";
732         content += "# ----------------------------------\n";
733         content += "\n";
734         content += _tabsMgr->getActualComplexBoxBBS();
735         file << content;
736         file.close();
737 }
738
739 //=========================================================================
740
741 void wxGUIEditorGraphicBBS::OnSaveActualComplexBox(wxCommandEvent& event) {
742         wxFileDialog * saveFileDialog = new wxFileDialog(this,
743                         wxT("Save Complex Box BBS"), wxEmptyString,
744                         _T("ComplexBoxName"), wxT("*.bbs"), wxSAVE
745                                         | wxOVERWRITE_PROMPT);
746         if (saveFileDialog->ShowModal() == wxID_OK) {
747                 wxString fileName = saveFileDialog->GetPath();
748                 std::string filename = (const char*) (fileName.mb_str());
749                 filename=CheckExtension (filename, ".bbs");
750                 SaveActualComplexBox(filename);
751         }
752 }
753
754 //=========================================================================
755 void wxGUIEditorGraphicBBS::SaveActualDiagram(std::string filename) {
756         ofstream file;
757         file.open(filename.c_str());
758
759         std::string content = "";
760
761         // writing file header
762         content += "# ----------------------------------\n";
763         content += "# - BBTKGEditor v ";
764         content += _currentBBGversion; //VERSION
765         content += " BBG BlackBox Diagram file\n";
766         content += "# - ";
767
768         content += filename;
769
770         content += "\n";
771         content += "# ----------------------------------\n";
772         content += "\n";
773         content += "APP_START\n";
774         _tabsMgr->saveActualDiagram(content, filename); //DFCH
775         content += "APP_END\n";
776         file << content;
777         file.close();
778 }
779
780 //=========================================================================
781 void wxGUIEditorGraphicBBS::SaveTempActualDiagram(const std::string &action) 
782 {
783         std::string stundo;
784         std::string stredo;
785         std::string name = _tabsMgr->GetNameTabPanel();
786         if(name[name.length() - 1] != '*')
787                 name +="*";
788         _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
789         _tabsMgr->saveTempActualDiagram(action);
790         GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState(stundo));
791         GetToolBar()->SetToolShortHelp(ID_UNDO,crea::std2wx(stundo));
792         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState(stredo));
793         GetToolBar()->SetToolShortHelp(ID_REDO,crea::std2wx(stredo));
794 }
795
796 //=========================================================================
797
798 //The following method allows to save the current diagram asking the filename
799 //DFCH
800 void wxGUIEditorGraphicBBS::SaveCurrentDiagramAs() {
801         wxFileDialog * saveFileDialog = new wxFileDialog(this,
802                         wxT("Save current diagram"), wxEmptyString, wxT("NewDiagram"),
803                         wxT("*.bbg"), wxSAVE | wxOVERWRITE_PROMPT);
804         if (saveFileDialog->ShowModal() == wxID_OK) {
805                 //_tabsMgr->SetNameTabPanel(saveFileDialog->GetFilename());
806                 wxString pathfileName = saveFileDialog->GetPath();
807                 std::string pathfilename = (const char*) (pathfileName.mb_str());
808                 pathfilename=CheckExtension (pathfilename, ".bbg");
809
810                 //modify the tab name accordingly
811                 wxString fileName = saveFileDialog->GetFilename();
812                 std::string filename = (const char*) (fileName.mb_str());
813                 filename=CheckExtension (filename, ".bbg");
814                 _tabsMgr->SetNameTabPanel( wxString (filename.c_str(), wxConvUTF8) );
815
816                 std::string pathfilenamebbs = pathfilename;
817                 pathfilenamebbs[pathfilenamebbs.length() - 1] = 's';
818                 if (_tabsMgr->isActualDiagramComplexBox() == false) {
819                         SaveActualDiagram(pathfilename);
820                         SaveActualBBS(pathfilenamebbs);
821                 } else {
822                         AskComplexBoxConfiguration();
823                         SaveActualDiagram(pathfilename);
824                         SaveActualComplexBox(pathfilenamebbs);
825                 } //if isActualDiagramComplexBox
826         } // if saveFileDialog
827 }
828 //=========================================================================
829 //DFCH
830 void wxGUIEditorGraphicBBS::OnSaveActualDiagram(wxCommandEvent& event) {
831         std::string pathfilename = _tabsMgr->GetCurrentTabPanelPath();
832         if (pathfilename.empty()) {
833                 SaveCurrentDiagramAs();
834         } else {
835                 std::string pathfilenamebbs = pathfilename;
836                 pathfilenamebbs[pathfilenamebbs.length() - 1] = 's';
837                 
838                 if (_tabsMgr->isActualDiagramComplexBox() == false) {
839                         std::string name = _tabsMgr->GetNameTabPanel();
840                         if(name[name.length() - 1] == '*')
841                         {
842                                 name =name.substr(0,name.length()-1);
843                         }
844                         _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
845                         SaveActualDiagram(pathfilename);
846                         SaveActualBBS(pathfilenamebbs);
847                 } else {
848                         AskComplexBoxConfiguration();
849                         SaveActualDiagram(pathfilename);
850                         SaveActualComplexBox(pathfilenamebbs);
851                 } //if isActualDiagramComplexBox
852         }
853 }
854 //=========================================================================
855 //DFCH
856 void wxGUIEditorGraphicBBS::OnSaveAsActualDiagram(wxCommandEvent& event) {
857         SaveCurrentDiagramAs();
858 }
859
860 //=========================================================================
861 void wxGUIEditorGraphicBBS::OnExportConsoleExecCommand(wxCommandEvent& event)
862 {
863         wxFileDialog * saveFileDialog = new wxFileDialog(this,
864                         wxT("Export console command"), wxEmptyString, wxT("ConsoleApp.txt"),
865                         wxT("*.txt;*.bbs;*.bbg;*.sh;*.bat"), wxSAVE | wxOVERWRITE_PROMPT);
866
867         if (saveFileDialog->ShowModal() == wxID_OK)
868         {
869                 wxString fileName = saveFileDialog->GetPath();
870                 std::string filename = (const char*) (fileName.mb_str());
871                 filename=CheckExtension (filename, ".txt");
872
873                 ofstream file;
874                 file.open(filename.c_str());
875
876                 std::vector<std::string> extInputs = _tabsMgr->getActualTabPanel()->getSceneManager()->GetExternalInputsNames();
877
878                 //TODO PUT IN A METHOD OF A FILES AND TEXT MANAGER CLASS !!!
879                 //std::string inputsInCommentsWin="";
880                 //std::string inputsInCommentsUnix="";
881                 //std::string inputsInOneLineWin="";
882                 std::string inputsInOneLineUnix="";
883                 for (unsigned int i = 0; i < extInputs.size(); ++i)
884                 {
885                         /* No gusto la idea al jefe pero por si acaso ...
886                         * RaC 2012 2nd HackFest
887                         *
888                         inputsInCommentsWin+="REM ";
889                         inputsInCommentsWin+=extInputs[i];
890                         inputsInCommentsWin+="=\"value\"\n";
891                         inputsInCommentsUnix+="# ";
892                         inputsInCommentsUnix+=extInputs[i];
893                         inputsInCommentsUnix+="=\"value\"\n";
894
895                         char buffer [50];
896                         sprintf (buffer, "%d", (i+1));
897                         inputsInOneLineWin+=extInputs[i];
898                         inputsInOneLineWin+="=\"%";
899                         inputsInOneLineWin+=buffer;
900                         inputsInOneLineWin+="\" ";
901                         */
902                         inputsInOneLineUnix+=extInputs[i];
903                         inputsInOneLineUnix+="=\"<value>\" ";
904
905                 }
906
907
908                 std::string content = "";
909                 // writing file header
910                 content += "# ----------------------------------\n";
911                 content += "# - BBTKGEditor v ";
912                 content += _currentBBGversion; //VERSION
913                 content += " Console Commands to execute BBS application\n";
914                 content += "# - ";
915                 content += filename;
916                 content += "\n";
917                 content += "# ----------------------------------\n";
918                 content += "\n";
919                 content += "\n";
920                 content += "# Command to execute bbi\n";
921                 content += "bbi <bbs_path> ";
922                 content += inputsInOneLineUnix;
923
924                 /* No gusto la idea al jefe pero por si acaso ...
925                  * RaC 2012 2nd HackFest
926                  content += "# For Windows (copy the following lines to a .bat file)\n";
927                  content += "REM BBS application\n";
928                  content += "REM Inputs:\n";
929                  content += inputsInCommentsWin;
930                  content += "REM To use previous inputs, put %$inputName% in the corresponding input of the execution bbi line\n";
931                  content += "bbi applicationBBS.bbs ";
932                  content += inputsInOneLineWin;
933                  content += "\n";
934                  content += "\n";
935                  content += "# For Linux or Mac (copy the following lines to a .sh file)\n";
936                  content += "#!/bin/bash\n";
937                  content += "# BBS application\n";
938                  content += "# Inputs:\n";
939                  content += inputsInCommentsUnix;
940                  content += "# To use previous inputs, put $inputName in the corresponding input of the execution bbi line\n";
941                  content += "bbi applicationBBS.bbs ";
942                  content += inputsInOneLineUnix;
943                  content += "\n";
944                 */
945
946                 file << content;
947                 file.close();
948         }
949
950
951 }
952
953 //=========================================================================
954
955 void wxGUIEditorGraphicBBS::refreshGUIControls() {
956         std::string stundo;
957         std::string stredo;
958         //RaC2012 2ndHackFest Next line seems weird ... To keep in mind for other devels
959         wxToolBar* toolbar = GetToolBar();
960         if (_tabsMgr->isActualDiagramComplexBox()) {
961                 toolbar->ToggleTool(ID_BTNCOMPLEXBOX, true);
962                 //toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, true); RaC2012 2nd HackFest
963                 toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT, true);
964
965                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_COMPLEXBOX, true);
966                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_BBS, false);
967                 GetMenuBar()->GetMenu(0)->Enable(ID_EXPORT_CONSOLEAPP, false);
968         } else {
969                 toolbar->ToggleTool(ID_BTNCOMPLEXBOX, false);
970                 //toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, false); RaC2012 2nd HackFest
971                 toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT, false);
972
973                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_COMPLEXBOX, false);
974                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_BBS, true);
975                 GetMenuBar()->GetMenu(0)->Enable(ID_EXPORT_CONSOLEAPP, true);
976         }
977
978         //RaC2012 2ndHackFest
979         GetToolBar()->EnableTool(ID_EDITPROPERTIES,true);
980
981         GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState(stundo));
982         GetToolBar()->SetToolShortHelp(ID_UNDO,crea::std2wx(stundo));
983         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState(stredo));
984         GetToolBar()->SetToolShortHelp(ID_REDO,crea::std2wx(stredo));
985
986         if (_tabsMgr->getNumActualSelectedObjects() > 0) {
987                 GetMenuBar()->GetMenu(1)->Enable(ID_COPY_TO_COMPLEXDIAGRAM, true);
988         } else {
989                 ///TODO Fix the possibility to say from the SceneManager to the GUI to refresh the options
990                 GetMenuBar()->GetMenu(1)->Enable(ID_COPY_TO_COMPLEXDIAGRAM, true);
991         }
992
993 }
994
995 //=========================================================================
996
997 void wxGUIEditorGraphicBBS::OnDeleteAllBoxesActualDiagram(wxCommandEvent& event) 
998 {
999         wxMessageDialog *dial = new wxMessageDialog(NULL,wxT("Are you sure to delete all boxes of the diagram?"),wxT("Delete all boxes"), wxYES_NO | wxNO_DEFAULT| wxICON_QUESTION);
1000         if (dial->ShowModal() == wxID_YES)
1001         {
1002                 _tabsMgr->deleteAllBoxesActualDiagram();
1003         }
1004 }
1005
1006 //=========================================================================
1007
1008 void wxGUIEditorGraphicBBS::OnCenterViewActualDiagram(wxCommandEvent& event) 
1009 {
1010         _tabsMgr->centerViewActualDiagram();
1011 }
1012
1013 //=========================================================================
1014 void wxGUIEditorGraphicBBS::OpenDiagram(std::string filePathName, std::string fileName) 
1015 {
1016         ifstream inputStream;   
1017         inputStream.open(filePathName.c_str());
1018         assert(inputStream.good()); // fails
1019         
1020         if(_tabsMgr->FindTab(filePathName)!=-1) //RaC2012 avoid opening same diagram twice
1021         {
1022                 inputStream.close();
1023                 printf("RaC: Diagram already opened\n");
1024                 return;
1025         }
1026         _tabsMgr->addNewTab( crea::std2wx(fileName) );
1027         _tabsMgr->loadDiagram(inputStream, filePathName);
1028         _tabsMgr->saveTempActualDiagram("load diagram");
1029         GetToolBar()->EnableTool(ID_UNDO, false);
1030         GetToolBar()->EnableTool(ID_UNDO, false);
1031         inputStream.close();
1032         refreshGUIControls();
1033 }
1034         
1035 //=========================================================================
1036
1037 void wxGUIEditorGraphicBBS::OnOpenDiagram(wxCommandEvent& event) 
1038 {
1039         wxFileDialog * openFileDialog = new wxFileDialog(this, wxT("Open diagram"), wxEmptyString, wxT(""), wxT("*.bbg"), wxOPEN | wxFILE_MUST_EXIST);
1040         if (openFileDialog->ShowModal() == wxID_OK) 
1041         {
1042                 wxString filePath = openFileDialog->GetPath();
1043                 wxString fileName = openFileDialog->GetFilename();
1044                 OpenDiagram( crea::wx2std(filePath) , crea::wx2std(fileName) );                                 
1045         }
1046 }
1047
1048 //=========================================================================
1049 void wxGUIEditorGraphicBBS::OnClickBtnBox(wxCommandEvent& event) 
1050 {
1051         BlackBoxDescriptor *bbDes = _pkgBrowser->GetActualSelected();
1052         if (bbDes != NULL) {
1053                 std::string typeName = bbDes->GetTypeName();
1054                 std::string packageName = bbDes->GetPackage()->GetName();
1055                 wxGEditorTabPanel *pnl = this->_tabsMgr->getActualTabPanel();
1056                 if (pnl != NULL) {
1057                         wxVtkSceneManager *scn = pnl->getSceneManager();
1058                         scn->createGBlackBox(50, 50, packageName, typeName);
1059                         scn->refresh();
1060                 }
1061                 SaveTempActualDiagram("New Box");
1062                 // if pnl
1063         }// if bbDes
1064 }
1065
1066 //=========================================================================
1067
1068 void wxGUIEditorGraphicBBS::OnClickBtnExecutableBox(wxCommandEvent& event) 
1069 {
1070         //DFCH
1071         wxGEditorTabPanel *pnl = this->_tabsMgr->getActualTabPanel();
1072
1073         if (pnl != NULL) {
1074                 wxVtkSceneManager *scn = pnl->getSceneManager();
1075                 if( scn->MakeBoxExecutable() )
1076                         std::cout<<"DFCH: wxGUIEditorGraphicBBS::OnClickBtnExecutableBox...successful"<<std::endl;
1077                 else
1078                         std::cout<<"DFCH: wxGUIEditorGraphicBBS::OnClickBtnExecutableBox...Error!!"<<std::endl;
1079         }
1080         //DFCH
1081
1082 }
1083
1084 //=========================================================================
1085 void wxGUIEditorGraphicBBS::OnClickBtnComplexBox(wxCommandEvent& event) 
1086 {
1087         wxToolBar* toolbar = GetToolBar();
1088         bool temp = toolbar->GetToolState(ID_BTNCOMPLEXBOX);
1089         if (temp) {
1090                 enableComplexBox();
1091         } else {
1092                 disableComplexBox();
1093         }
1094 }
1095
1096 //=========================================================================
1097
1098 void wxGUIEditorGraphicBBS::OnAddComplexBoxInput(wxCommandEvent& event) 
1099 {
1100         wxTextEntryDialog *nameDialog = new wxTextEntryDialog(this,
1101                         wxT("Name of input port"));
1102         if (nameDialog->ShowModal() == wxID_OK) {
1103                 wxString fileName = nameDialog->GetValue();
1104                 if (!fileName.IsEmpty()) {
1105                         std::string portName = (const char*) (fileName.mb_str());
1106                         _tabsMgr->addActualDiagramComplexInputPort(portName);
1107                 }
1108         }
1109 }
1110
1111 //=========================================================================
1112 void wxGUIEditorGraphicBBS::OnAddComplexBoxOutput(wxCommandEvent& event) 
1113 {
1114         wxTextEntryDialog *nameDialog = new wxTextEntryDialog(this,
1115                         wxT("Name of output port"));
1116         if (nameDialog->ShowModal() == wxID_OK) {
1117                 wxString fileName = nameDialog->GetValue();
1118                 if (!fileName.IsEmpty()) {
1119                         std::string portName = (const char*) (fileName.mb_str());
1120                         _tabsMgr->addActualDiagramComplexOutputPort(portName);
1121                 }
1122         }
1123 }
1124
1125 //=========================================================================
1126 void wxGUIEditorGraphicBBS::OnCopySelectedToComplexDiagram(
1127                 wxCommandEvent& event) 
1128 {
1129         _tabsMgr->copySelectedBBoxesToComplexDiagram();
1130 }
1131
1132 //=========================================================================
1133 void wxGUIEditorGraphicBBS::OnHelpBBeditor(wxCommandEvent& event) 
1134 {
1135         printf("wxGUIEditorGraphicBBS::OnHelpBBeditor ....not implemented..\n");
1136 }
1137
1138 //=========================================================================
1139 void wxGUIEditorGraphicBBS::OnCreatePackage(wxCommandEvent& event) 
1140 {
1141         bbtkSystemTools::CreatePackage();
1142 }
1143
1144 //=========================================================================
1145 void wxGUIEditorGraphicBBS::OnCreateBlackBox(wxCommandEvent& event) 
1146 {
1147         bbtkSystemTools::CreateBlackBox();
1148 }
1149         
1150 //=========================================================================
1151 void wxGUIEditorGraphicBBS::OnPlugPackage(wxCommandEvent& event) 
1152 {
1153         //DFGO2012 Plug Package functionnalities
1154         long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
1155         wxDirDialog* FD =
1156           new wxDirDialog( 0,
1157                            _T("Select package directory"),
1158                            _T(""),
1159                            style);
1160         long userResponse;
1161         do
1162         {
1163                 userResponse = FD->ShowModal();
1164                 if(userResponse==wxID_OK)
1165                 {
1166                         std::string path = wx2std (FD->GetPath()) ;
1167                         #ifdef WIN32
1168                         std::string fname = path + "\\bbtkPackage";
1169                         #else
1170                         std::string fname = path + "/bbtkPackage";
1171                         #endif
1172                         
1173                         std::cout << "Path chosen = \"" << FD->GetPath() << "\"" << std::endl;
1174                         if ( Utilities::FileExists( fname ) )
1175                         {
1176                                 std::ifstream f;
1177                                 f.open(fname.c_str());
1178                                 std::string pname;
1179                                 f >> pname;
1180                                 while(pname[0] == '#')
1181                                 {
1182                                         getline(f, pname, '\n');
1183                                         f >> pname;
1184                                 }
1185                                 f.close();
1186
1187                                 bbtk::ConfigurationFile::GetInstance().AddPackagePathsAndWrite( path );
1188
1189                                 DoRegeneratePackageDoc(pname);
1190                                 DoRegenerateBoxesLists();
1191
1192                                 _pkgBrowser->RebuildList();
1193
1194                                 wxMessageBox(_T("Package successfully plugged. Please restart bbEditor to see the new package in the package browser."),_T("Plug package"),wxOK | wxICON_INFORMATION);
1195                                 userResponse = wxID_CANCEL;
1196                         }
1197                         else
1198                         {
1199                                 std::string err = "The directory \"" + path + "\" does not contain a 'bbtkPackage' file.";
1200                                 #ifndef WIN32
1201                                         #ifndef MACOSX
1202                                                 err += "\nNote: If you are on Linux please select the folder and click \"Open\", don't enter in it.";
1203                                         #endif
1204                                 #endif
1205
1206                                 wxMessageBox(crea::std2wx(err), _T("Plug package"),wxOK | wxICON_ERROR);
1207                                 // THE FOLLOWING LINE GIVES ERRORS BECAUSE IT IS NECESSARY TO USE std2wx
1208                                 //wxMessageBox( _T( err) , _T("Plug package"),wxOK | wxICON_ERROR);
1209                         }
1210                 }
1211         }while(userResponse != wxID_CANCEL);
1212
1213 }
1214         
1215 //=========================================================================
1216 void wxGUIEditorGraphicBBS::OnEditConfig(wxCommandEvent& event) 
1217 {
1218         std::string commandStr;
1219             std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
1220         #ifdef WIN32
1221                 commandStr = "notepad.exe ";
1222         #else
1223                 commandStr = "gedit ";
1224         #endif
1225                 commandStr = commandStr + configFile;
1226                 std::cout << "system: " << commandStr << std::endl;
1227                 system ( commandStr.c_str() );
1228 }
1229         
1230 //=========================================================================
1231 void wxGUIEditorGraphicBBS::OnShowHTMLDoc(wxCommandEvent& event) 
1232 {
1233         // JGRR & CM WH
1234         if (_helpHtmlBrowser != NULL){
1235              _frameAUIMgr->DetachPane(_helpHtmlBrowser);
1236             _helpHtmlBrowser->Destroy();   
1237         }
1238         initHelpHTMLBrowser();
1239         _frameAUIMgr->AddPane(_helpHtmlBrowser, wxAuiPaneInfo().Right().MinSize(200, 200));
1240         _frameAUIMgr->Update();
1241         refreshGUIControls();
1242         
1243         // EO JGRR    & CM WH       
1244 }
1245         
1246 //=========================================================================
1247 void wxGUIEditorGraphicBBS::OnCreateIndex(wxCommandEvent& event) 
1248 {
1249         printf("EED wxGUIEditorGraphicBBS::OnCreateIndex ... not implemented ..\n");
1250 }
1251
1252         
1253 //=========================================================================
1254 void wxGUIEditorGraphicBBS::OnEditComplexBoxScript(wxCommandEvent& event)
1255 {
1256         BlackBoxDescriptor *bbDes = _pkgBrowser->GetActualSelected();
1257         if (bbDes != NULL) 
1258         {
1259                 std::string packageName = bbDes->GetPackage()->GetName();
1260                 std::string typeName = bbDes->GetTypeName();
1261                 if (!TryToOpenScriptComplexBox(packageName ,typeName))
1262                 {
1263                         wxMessageDialog windowMessage1(NULL,_T("This is not a ComplexBox script."), _T("Alert !"), wxOK);
1264                         windowMessage1.ShowModal();
1265                 }
1266         }// if bbDes
1267 }
1268
1269 //=========================================================================
1270 void wxGUIEditorGraphicBBS::OnExit(wxCommandEvent& event) 
1271 {
1272         Close(true);
1273 }
1274
1275 Factory::Pointer wxGUIEditorGraphicBBS::GetBBTKFactory()
1276 {
1277         if (_pkgBrowser==NULL) 
1278         {
1279                 printf ("EED ERROR! wxGUIEditorGraphicBBS::GetBBTKFactory _pkgBrowser = NULL\n");
1280         }
1281         return _pkgBrowser->GetFactory();
1282 }
1283
1284 //=========================================================================
1285 void wxGUIEditorGraphicBBS::enableComplexBox() 
1286 {
1287         _tabsMgr->setActualDiagramComplexBox(true);
1288         refreshGUIControls();
1289 }
1290
1291 //=========================================================================
1292
1293 void wxGUIEditorGraphicBBS::disableComplexBox() 
1294 {
1295         _tabsMgr->setActualDiagramComplexBox(false);
1296         refreshGUIControls();
1297 }
1298
1299 //=========================================================================
1300 void wxGUIEditorGraphicBBS::setCurrentDiagramDescription(std::string description)
1301 {
1302         _tabsMgr->SetDescription(description);
1303 }
1304
1305 //=========================================================================
1306 void wxGUIEditorGraphicBBS::setCurrentDiagramAuthor(std::string author)
1307 {
1308         _tabsMgr->SetAuthor(author);
1309 }
1310
1311 //=========================================================================
1312 void wxGUIEditorGraphicBBS::setCurrentDiagramCategory(std::string category)
1313 {
1314         _tabsMgr->SetCategory(category);
1315 }
1316
1317 //=========================================================================
1318 std::string wxGUIEditorGraphicBBS::getCurrentDiagramDescription()
1319 {
1320         return _tabsMgr->GetDescription();
1321 }
1322
1323 //=========================================================================
1324 std::string wxGUIEditorGraphicBBS::getCurrentDiagramAuthor()
1325 {
1326         return _tabsMgr->GetAuthor();
1327 }
1328
1329 //=========================================================================
1330 std::string wxGUIEditorGraphicBBS::getCurrentDiagramCategory()
1331 {
1332         return _tabsMgr->GetCategory();
1333 }
1334
1335         
1336 //=========================================================================
1337 bool wxGUIEditorGraphicBBS::isCurrentDiagramComplexBox()
1338 {
1339         return _tabsMgr->isActualDiagramComplexBox();
1340 }
1341
1342
1343 //=========================================================================
1344 void wxGUIEditorGraphicBBS::OpenScript(std::string filePathNameBBS ,std::string boxType)
1345 {       
1346         std::string fileName                                                    = boxType+".bbg";
1347         std::string filePathNameBBG                                             = filePathNameBBS;
1348         filePathNameBBG[ filePathNameBBG.length()-1 ]   = 'g';
1349         
1350         wxMessageDialog windowMessage1(NULL,_T("The system is going to open a bbg or bbs for you. Be careful if you save this file."), _T("Alert !"), wxOK);
1351         windowMessage1.ShowModal();
1352         
1353         if ( Utilities::FileExists( filePathNameBBG ) )
1354         {
1355                 OpenDiagram( filePathNameBBG , fileName );     
1356         } else {
1357                 if ( Utilities::FileExists( filePathNameBBS ) )
1358                 {
1359                         OpenBBS( filePathNameBBS , fileName );     
1360                 } else {
1361                         wxMessageDialog windowMessage1(NULL,_T("..ERROR.. loading script bbg/bbs."), _T("Alert !"), wxOK);
1362                         windowMessage1.ShowModal();
1363                 } // FileExists BBS
1364         } // FieExists BBG
1365 } //OpenScript
1366         
1367         
1368         
1369 //=========================================================================
1370 bool wxGUIEditorGraphicBBS::TryToOpenScriptApplication(std::string packageName ,std::string boxType)
1371 {       
1372         return TryToOpenScript(packageName ,boxType, true, false);
1373 }               
1374
1375 //=========================================================================
1376 bool wxGUIEditorGraphicBBS::TryToOpenScriptComplexBox(std::string packageName ,std::string boxType)
1377 {       
1378         return TryToOpenScript(packageName ,boxType, false, true);
1379 }               
1380         
1381         
1382 //=========================================================================
1383 bool wxGUIEditorGraphicBBS::TryToOpenScript(std::string packageName ,std::string boxType,bool scriptApplication, bool scriptComplexBox)
1384 {       
1385         Package::Pointer k;
1386         std::map< std::string, BlackBoxDescriptor::Pointer> mapDesc;
1387         BlackBoxDescriptor::Pointer descriptor;
1388         k                       = GetBBTKFactory()->GetPackage(packageName);
1389         mapDesc         = k->GetDescriptorMap();
1390         descriptor      = mapDesc[boxType];
1391
1392         bool result=false;
1393         
1394         if (
1395                 ((descriptor->IsTypeOfScript_Application()==true) && (scriptApplication==true))
1396                 ||
1397                 ((descriptor->IsTypeOfScript_ComplexBox()==true) && (scriptComplexBox==true))           
1398                 )
1399         {
1400                 result=true;
1401                 OpenScript(descriptor->GetScriptFileName(),boxType);
1402         }
1403         
1404         return result;
1405 }       
1406         
1407 } // EO namespace bbtk
1408
1409 // EOF
1410