]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/bbtkwxGUIEditorGraphicBBS.cxx
#3132 bbGEditor Feature New Normal - branch changeWx28to30 compilation with wxWidgets3
[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         //EED02JUIN2010         delete _tabsMgr;
117         _frameAUIMgr->UnInit();
118         // FCY memory leaks
119         delete _frameAUIMgr;
120         delete _notebook;
121         delete _tabsMgr;
122 }
123
124 //=========================================================================
125 void wxGUIEditorGraphicBBS::initToolbar() {
126         wxBitmap bmp_new(new_xpm);
127         wxBitmap bmp_open(open_xpm);
128         wxBitmap bmp_save(save_xpm);
129         wxBitmap bmp_run(run_xpm);
130         wxBitmap bmp_delete(delete_xpm);
131         wxBitmap bmp_centerview(centerview_xpm);
132         wxBitmap bmp_box(box_xpm);
133         wxBitmap bmp_executablebox(executablebox_xpm);
134         wxBitmap bmp_complexbox(complexbox_xpm);
135         wxBitmap bmp_complexinputport(complexinputport_xpm);
136         wxBitmap bmp_complexoutputport(complexoutputport_xpm);
137         wxBitmap bmp_undo(undo_xpm);
138         wxBitmap bmp_redo(redo_xpm);
139         wxBitmap bmp_editProperties(editProperties_xpm);
140         wxBitmap bmp_showTree(showTree_xpm);
141         wxToolBar *toolbar = new wxToolBar(this, wxID_ANY);
142
143         //Adds a tool btn to the toolbar
144         toolbar->AddTool(ID_NEW, _T("New"), bmp_new, wxNullBitmap, wxITEM_NORMAL,
145                         _T("New tab"), _T("Create a new panel tab"));
146         toolbar->AddTool(ID_OPEN, _T("Open diagram"), bmp_open, wxNullBitmap,
147                         wxITEM_NORMAL, _T("Open a diagram"), _T("Open a diagram"));
148         toolbar->AddTool(ID_SAVE_DIAGRAM, _T("Save Diagram"), bmp_save,
149                         wxNullBitmap, wxITEM_NORMAL, _T("Saves the current diagram"),
150                         _T("Saves the current diagram")); //DFCH
151         //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
152         toolbar->AddTool(ID_RUN, _T("Run"), bmp_run, wxNullBitmap, wxITEM_NORMAL,
153                         _T("Execute actual diagram"), _T("Execute actual diagram"));
154         toolbar->AddTool(ID_DELETEALL, _T("Delete all"), bmp_delete, wxNullBitmap,
155                         wxITEM_NORMAL, _T("Delete all boxes"), _T("Delete all boxes"));
156         toolbar->AddTool(ID_CENTERVIEW, _T("Center view"), bmp_centerview,
157                         wxNullBitmap, wxITEM_NORMAL, _T("Center view"), _T("Center view"));
158         toolbar->AddSeparator();
159         toolbar->AddTool(ID_BTNBOX, _T("New Box"), bmp_box, wxNullBitmap,
160                         wxITEM_NORMAL, _T("New Box"), _T("Nex Box"));
161         toolbar->AddSeparator();
162         toolbar->AddTool(ID_BTEXECUTABLEBOX, _T("Select as executable Box"),
163                         bmp_executablebox, wxNullBitmap, wxITEM_NORMAL,
164                         _T("Select as executable Box"), _T("Select as executable Box"));
165         toolbar->AddSeparator();
166         toolbar->AddTool(ID_BTNCOMPLEXBOX, _T("Complex box"), complexbox_xpm,
167                         wxNullBitmap, wxITEM_NORMAL, _T("Complex box"), _T("Complex box"));
168         toolbar->SetToggle(ID_BTNCOMPLEXBOX, true);
169
170         // RaC 2012 2nd HackFest - Now Complex inputs are any external input
171         //              toolbar->AddTool(ID_ADDCOMPLEXBOXINPUT, _T("Add input to Complex box"),
172         //              bmp_complexinputport, wxNullBitmap, wxITEM_NORMAL,
173         //              _T("Add input to Complex box"), _T("Add input to Complex box"));
174         toolbar->AddTool(ID_ADDCOMPLEXBOXINPUT, _T("Add external input (Complex box or console app)"),
175                         bmp_complexinputport, wxNullBitmap, wxITEM_NORMAL,
176                         _T("Add external input (Complex box or console app)"), _T("Add external input (Complex box or console app)"));
177
178         toolbar->AddTool(ID_ADDCOMPLEXBOXOUTPUT, _T("Add output to Complex box"),
179                         bmp_complexoutputport, wxNullBitmap, wxITEM_NORMAL,
180                         _T("Add output Complex box"), _T("Add output Complex box"));
181
182         // toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, false); RaC2012 2nd HackFest
183         toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, true);
184
185         toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT, false);
186         toolbar->AddSeparator();
187         toolbar->AddTool(ID_UNDO, _T("Undo"),bmp_undo, wxNullBitmap, wxITEM_NORMAL,     _T("Undo"), _T("Undo"));
188         toolbar->AddTool(ID_REDO, _T("Redo"),bmp_redo, wxNullBitmap, wxITEM_NORMAL,     _T("Redo"), _T("Redo"));
189         toolbar->AddSeparator();
190         toolbar->AddTool(ID_EDITPROPERTIES, _T("Edit diagram properties"),bmp_editProperties, wxNullBitmap, wxITEM_NORMAL,      _T("Edit diagram properties"), _T("Edit diagram properties"));
191         toolbar->EnableTool(ID_EDITPROPERTIES, false);
192
193         toolbar->AddTool(ID_SHOWTREE, _T("Show tree"),bmp_showTree, wxNullBitmap, wxITEM_NORMAL,        _T("Show tree"), _T("Show tree"));
194         
195         toolbar->EnableTool(ID_UNDO, false);
196         toolbar->EnableTool(ID_REDO, false);
197         toolbar->SetMargins(2, 2);
198         toolbar->Realize();
199         SetToolBar(toolbar);
200
201         
202         // connect command event handlers
203
204         Connect(ID_NEW, wxEVT_COMMAND_TOOL_CLICKED,
205                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCreateNewTab));
206         Connect(ID_OPEN, wxEVT_COMMAND_TOOL_CLICKED,
207                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnOpenDiagram));
208         Connect(ID_SAVE_DIAGRAM, wxEVT_COMMAND_TOOL_CLICKED,
209                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveActualDiagram)); //DFCH
210         Connect(ID_SAVE_AS_DIAGRAM, wxEVT_COMMAND_TOOL_CLICKED,
211                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveAsActualDiagram)); //DFCH
212         Connect(
213                         ID_RUN,
214                         wxEVT_COMMAND_TOOL_CLICKED,
215                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnExecuteActualDiagram));
216         Connect(
217                         ID_DELETEALL,
218                         wxEVT_COMMAND_TOOL_CLICKED,
219                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnDeleteAllBoxesActualDiagram));
220         Connect(
221                         ID_CENTERVIEW,
222                         wxEVT_COMMAND_TOOL_CLICKED,
223                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCenterViewActualDiagram));
224         Connect(ID_BTNBOX, wxEVT_COMMAND_TOOL_CLICKED,
225                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnClickBtnBox));
226         Connect(
227                         ID_BTEXECUTABLEBOX,
228                         wxEVT_COMMAND_TOOL_CLICKED,
229                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnClickBtnExecutableBox));
230         Connect(ID_BTNCOMPLEXBOX, wxEVT_COMMAND_TOOL_CLICKED,
231                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnClickBtnComplexBox));
232         Connect(ID_ADDCOMPLEXBOXINPUT, wxEVT_COMMAND_TOOL_CLICKED,
233                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnAddComplexBoxInput));
234         Connect(ID_ADDCOMPLEXBOXOUTPUT, wxEVT_COMMAND_TOOL_CLICKED,
235                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnAddComplexBoxOutput));
236         Connect(ID_UNDO,wxEVT_COMMAND_TOOL_CLICKED,     wxCommandEventHandler(wxGUIEditorGraphicBBS::OnUndo));
237         Connect(ID_REDO,wxEVT_COMMAND_TOOL_CLICKED,     wxCommandEventHandler(wxGUIEditorGraphicBBS::OnRedo));
238         Connect(ID_CHANGENAME, wxEVT_COMMAND_RIGHT_CLICK, wxCommandEventHandler(wxGUIEditorGraphicBBS::OnChangeName));
239         Connect(ID_EDITPROPERTIES,wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler(wxGUIEditorGraphicBBS::OnEditDiagramProperties));
240         Connect(ID_SHOWTREE,wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler(wxGUIEditorGraphicBBS::OnShowTree));
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
457                 _actualPkgBrowserPkgName = packageName;
458                 _actualPkgBrowserBoxName = boxName;
459
460 //EED 15 oct 2012               BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxName);
461                 
462                 Package::Pointer k;
463 //EED           ComplexBlackBoxDescriptor::Pointer descriptor;
464                 BlackBoxDescriptor::Pointer descriptor;
465
466                 std::map< std::string, BlackBoxDescriptor::Pointer> mapDesc;
467                 k                       = _pkgBrowser->GetFactory()->GetPackage(_actualPkgBrowserPkgName);
468                 mapDesc         = k->GetDescriptorMap();                
469                 descriptor      = mapDesc[_actualPkgBrowserBoxName];
470 /*
471                 tree.treeTour(0);               
472
473                 tree.deleteTree();
474
475                 descriptor->GetBoxesInside(tree, 0);
476
477                 //tree.treeTour(0);
478                 //std::vector<std::string> boxesDiagram = _sceneM->boxesDiagram;
479                 //for(int i=0;i<boxesDiagram.size();i++){
480                 //      std::cout<<"caja: "<<boxesDiagram[i]<<std::endl;
481                 //}             
482                 
483                 _actualPkgBrowserPkgName = "";
484                 _actualPkgBrowserBoxName = "";
485 */
486                 /*
487                 Package::Pointer k;
488                 BlackBoxDescriptor::Pointer descriptor;
489                 std::map< std::string, BlackBoxDescriptor::Pointer> mapDesc;
490                 k                       = _pkgBrowser->GetFactory()->GetPackage(packageName);
491                 std::vector<std::string> temp;
492                 _pkgBrowser->GetFactory()->GetPackagesList(temp);
493                 for(int ii = 0 ; ii < temp.size() ; ii++)
494                 {
495                                 std::cout << " packs "<< temp[ii]<< std::endl;
496                 }
497                 mapDesc         = k->GetDescriptorMap();                
498                 descriptor      = mapDesc[boxName];
499                 */
500                 _pkgBrowser->WxGUIBlackBoxListUserOnSelected(descriptor.get());
501         }
502 }
503
504 //=========================================================================
505
506 void wxGUIEditorGraphicBBS::editBlackBox(GBlackBoxModel *bbmodel) {
507         wxBlackBoxEditionDialog* dialog =
508                         new wxBlackBoxEditionDialog(this, bbmodel);
509         dialog->Show();
510 }
511
512 //=========================================================================
513
514 bool wxGUIEditorGraphicBBS::boxNameExists(std::string boxname) {
515  
516         return this->_tabsMgr->getActualTabPanel()->getSceneManager()->boxExist(boxname);
517 }
518
519 //=========================================================================
520
521 void wxGUIEditorGraphicBBS::updateStatusBar(std::string textStatus) {
522         SetStatusText(crea::std2wx(textStatus));
523 }
524
525 //=========================================================================
526
527 void wxGUIEditorGraphicBBS::executeScript(std::string script) {
528         std::string separator =
529                         ConfigurationFile::GetInstance().Get_file_separator();
530         std::string dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
531         std::string filename = dir + separator + "tmp_bbtk.bbs";
532
533         ofstream tempFile;
534         tempFile.open(filename.c_str());
535         tempFile << script;
536         tempFile.close();
537
538         std::string command = "";
539
540 #ifdef WIN32
541         command += "start /b ";
542 #endif
543
544         //              command += "\""+ crea::System::GetExecutablePath();
545         //#ifdef MACOSX
546         //              command += separator + "../../../bbi.app/Contents/MacOS";
547         //#endif
548
549         command += "bbi ";
550         command += "\"" + filename + "\"";
551
552 #ifndef WIN32
553         command += " & ";
554 #endif
555
556         printf("RaC wxGUIEditorGraphicBBS::executeScript %s \n", command.c_str());
557         system(command.c_str());
558 }
559
560 //=========================================================================
561 std::string wxGUIEditorGraphicBBS::CheckExtension(std::string filename, std::string extension) {
562         if(filename.size() > extension.size() ){
563                 if( (filename.compare(filename.size()-extension.size(), extension.size(),extension)) !=0 ){
564                         filename.append(extension);
565                 }
566         }else{
567                 filename.append(extension);
568         }
569         return filename;
570 }
571 //=========================================================================
572 // EVENT HANDLERS
573 //=========================================================================
574
575 void wxGUIEditorGraphicBBS::OnCreateNewTab(wxCommandEvent& event) {
576         _tabsMgr->addNewTab();
577         refreshGUIControls();
578 }
579
580 void wxGUIEditorGraphicBBS::OnUndo(wxCommandEvent& event) {
581         unsigned short disable;
582         std::string stundo;
583         std::string stredo;
584
585         _tabsMgr->loadTempDiagram(0);
586         disable = _tabsMgr->getUndoState(stundo);
587         if(disable == 0)
588         {
589                 std::string name = _tabsMgr->GetNameTabPanel();
590                 if(name[name.length() - 1] == '*')
591                 {
592                         name =name.substr(0,name.length()-1);
593                 }
594                 _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
595         }
596
597         GetToolBar()->EnableTool(ID_UNDO,disable);
598         GetToolBar()->SetToolShortHelp(ID_UNDO,crea::std2wx(stundo));
599         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState(stredo));
600         GetToolBar()->SetToolShortHelp(ID_REDO,crea::std2wx(stredo));
601         refreshGUIControls();
602 }
603
604 //=========================================================================
605
606 void wxGUIEditorGraphicBBS::OnRedo(wxCommandEvent& event) {
607         unsigned short disable;
608         std::string stundo;
609         std::string stredo;
610
611         _tabsMgr->loadTempDiagram(1);
612         std::string name = _tabsMgr->GetNameTabPanel();
613         if(name[name.length() - 1] != '*')
614                 name +="*";
615         _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
616         GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState(stundo));
617         GetToolBar()->SetToolShortHelp(ID_UNDO,crea::std2wx(stundo));
618         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState(stredo));
619         GetToolBar()->SetToolShortHelp(ID_REDO,crea::std2wx(stredo));
620         refreshGUIControls();
621 }
622
623 void wxGUIEditorGraphicBBS::OnChangeName(wxCommandEvent& event){
624 }
625 //=========================================================================
626
627 void wxGUIEditorGraphicBBS::OnShowTree(wxCommandEvent& event){
628                 std::cout <<  "wxGUIEditorGraphicBBS::OnShowTree"<<std::endl;
629 if (_actualPkgBrowserPkgName !="" || _actualPkgBrowserBoxName != "") 
630         {
631                 Package::Pointer k;
632 //EED           ComplexBlackBoxDescriptor::Pointer descriptor;
633                 BlackBoxDescriptor::Pointer descriptor;
634
635                 std::map< std::string, BlackBoxDescriptor::Pointer> mapDesc;
636                 k                       = _pkgBrowser->GetFactory()->GetPackage(_actualPkgBrowserPkgName);
637                 mapDesc         = k->GetDescriptorMap();                
638                 descriptor      = mapDesc[_actualPkgBrowserBoxName];
639
640                 tree.treeTour(0);               
641
642                 tree.deleteTree();
643
644                 descriptor->GetBoxesInside(tree, 0);
645                 
646                 _actualPkgBrowserPkgName = "";
647                 _actualPkgBrowserBoxName = "";
648         }
649 else
650         {
651                 std::cout<<"Select a Box"<<std::endl;
652         }
653 }
654
655 //=========================================================================
656
657 void wxGUIEditorGraphicBBS::OnEditDiagramProperties(wxCommandEvent& event)
658 {
659         wxDiagramPropertiesEditionDialog* dialog =
660                                 new wxDiagramPropertiesEditionDialog(this, tree);
661         dialog->Show();
662 }
663
664 //=========================================================================
665
666 void wxGUIEditorGraphicBBS::OnExecuteActualDiagram(wxCommandEvent& event) {
667         std::string script1 = _tabsMgr->getActualDiagramBBS(true);
668         cout << endl << "RaC wxGUIEditorGraphicBBS::executeActualDiagram SCRIPT"
669                         << endl << endl;
670         cout << script1 << endl;
671
672         std::string script2 = _tabsMgr->getActualDiagramBBS(false);
673         executeScript(script2);
674 }
675
676         
677 //=========================================================================
678 void wxGUIEditorGraphicBBS::OpenBBS(std::string filePathName, std::string fileName) 
679 {
680         _tabsMgr->addNewTab( crea::std2wx(fileName) );
681         bbtk::InterpreterBBS::Pointer I = bbtk::InterpreterBBS::New( this->_tabsMgr->getActualTabPanel()->getSceneManager(), _pkgBrowser->GetFactory());
682         // We tell the interpreter to throw exceptions on error
683         I->SetThrow(true);
684         // Interpret the file supposed to define a box called 'Processing'
685         I->InterpretFile( filePathName );
686         refreshGUIControls();
687 }
688         
689 //=========================================================================
690 // HANDLERS
691 //=========================================================================
692 void wxGUIEditorGraphicBBS::OnOpenBBS(wxCommandEvent& event) 
693 {
694 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
695 #if wxMAJOR_VERSION <= 2
696         wxFileDialog * openFileDialog = new wxFileDialog(this, wxT("Open BBS"), wxEmptyString, wxT("NewBBS"), wxT("*.bbs"), wxOPEN);
697 #else
698         wxFileDialog * openFileDialog = new wxFileDialog(this, wxT("Open BBS"), wxEmptyString, wxT("NewBBS"), wxT("*.bbs"), wxFD_OPEN);
699 #endif
700
701         if (openFileDialog->ShowModal() == wxID_OK) 
702         {
703                 OpenBBS( crea::wx2std(openFileDialog->GetPath()) , crea::wx2std(openFileDialog->GetFilename())  );
704         }
705         
706 /*EED Borrame   
707         if (openFileDialog->ShowModal() == wxID_OK) 
708         {
709                 wxString fileName = openFileDialog->GetPath();
710                 _tabsMgr->addNewTab(openFileDialog->GetFilename());
711                 bbtk::InterpreterBBS::Pointer I = bbtk::InterpreterBBS::New( this->_tabsMgr->getActualTabPanel()->getSceneManager(), _pkgBrowser->GetFactory());
712                 // We tell the interpreter to throw exceptions on error
713                 I->SetThrow(true);
714                 // Interpret the file supposed to define a box called 'Processing'
715                 I->InterpretFile((const char*) (fileName.mb_str()));
716         }
717         refreshGUIControls();
718 */ 
719 }
720
721 //=========================================================================
722 void wxGUIEditorGraphicBBS::SaveActualBBS(std::string filename) {
723         ofstream file;
724
725         //EED           file.open(fileName.c_str());
726         file.open(filename.c_str());
727         std::string content = "";
728         // writing file header
729         content += "# ----------------------------------\n";
730         content += "# - BBTKGEditor v ";
731         content += _currentBBGversion; //VERSION
732         content += " BBS BlackBox Script\n";
733         content += "# - ";
734         content += filename;
735         content += "\n";
736         content += "# ----------------------------------\n";
737         content += "\n";
738         content += _tabsMgr->getActualDiagramBBS();
739         file << content;
740         file.close();
741 }
742
743 void wxGUIEditorGraphicBBS::OnSaveActualBBS(wxCommandEvent& event) {
744
745 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
746 #if wxMAJOR_VERSION <= 2
747         wxFileDialog * saveFileDialog = new wxFileDialog(this,
748                         wxT("Save  actual BBS"), wxEmptyString, wxT("NewBBS"),
749                         wxT("*.bbs"), wxSAVE | wxOVERWRITE_PROMPT);
750 #else
751         wxFileDialog * saveFileDialog = new wxFileDialog(this,
752                         wxT("Save  actual BBS"), wxEmptyString, wxT("NewBBS"),
753                         wxT("*.bbs"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
754 #endif
755
756         if (saveFileDialog->ShowModal() == wxID_OK) {
757                 wxString fileName = saveFileDialog->GetPath();
758                 std::string filename = (const char*) (fileName.mb_str());
759                 filename=CheckExtension (filename, ".bbs");
760                 SaveActualBBS(filename);
761         }
762
763 }
764
765 void wxGUIEditorGraphicBBS::AskComplexBoxConfiguration() {
766         std::string cbName = _tabsMgr->GetCbName();
767         std::string paName = _tabsMgr->GetCbPackageName();
768
769         wxTextEntryDialog *cbNameDialog = new wxTextEntryDialog(this,
770                         wxT("Complex Box name"));
771         wxTextEntryDialog *paNameDialog = new wxTextEntryDialog(this,
772                         wxT("Package name"));
773
774         cbNameDialog->SetValue(wxString(cbName.c_str(), wxConvUTF8));
775         paNameDialog->SetValue(wxString(paName.c_str(), wxConvUTF8));
776
777         if (cbNameDialog->ShowModal() == wxID_OK) {
778                 wxString complexboxname = cbNameDialog->GetValue();
779                 cbName = (const char*) (complexboxname.mb_str());
780         }
781
782         if (paNameDialog->ShowModal() == wxID_OK) {
783                 wxString packagename = paNameDialog->GetValue();
784                 paName = (const char*) (packagename.mb_str());
785         }
786
787         _tabsMgr->SetCbName(cbName);
788         _tabsMgr->SetCbPackageName(paName);
789 }
790
791 //=========================================================================
792
793 void wxGUIEditorGraphicBBS::SaveActualComplexBox(std::string filename) {
794         ofstream file;
795         file.open(filename.c_str());
796
797         std::string content = "";
798
799         // writing file header
800         content += "# ----------------------------------\n";
801         content += "# - BBTKGEditor v ";
802         content += _currentBBGversion;//VERSION
803         content += " BBS BlackBox Script (Complex Box)\n";
804         content += "# - ";
805         content += filename;
806         content += "\n";
807         content += "# ----------------------------------\n";
808         content += "\n";
809         content += _tabsMgr->getActualComplexBoxBBS();
810         file << content;
811         file.close();
812 }
813
814 //=========================================================================
815
816 void wxGUIEditorGraphicBBS::OnSaveActualComplexBox(wxCommandEvent& event) 
817 {
818 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
819 #if wxMAJOR_VERSION <= 2
820         wxFileDialog * saveFileDialog = new wxFileDialog(this,
821                         wxT("Save Complex Box BBS"), wxEmptyString,
822                         _T("ComplexBoxName"), wxT("*.bbs"), wxSAVE
823                                         | wxOVERWRITE_PROMPT);
824 #else
825         wxFileDialog * saveFileDialog = new wxFileDialog(this,
826                         wxT("Save Complex Box BBS"), wxEmptyString,
827                         _T("ComplexBoxName"), wxT("*.bbs"), wxFD_SAVE
828                                         | wxFD_OVERWRITE_PROMPT);
829 #endif
830
831         if (saveFileDialog->ShowModal() == wxID_OK) {
832                 wxString fileName = saveFileDialog->GetPath();
833                 std::string filename = (const char*) (fileName.mb_str());
834                 filename=CheckExtension (filename, ".bbs");
835                 SaveActualComplexBox(filename);
836         }
837 }
838
839 //=========================================================================
840 void wxGUIEditorGraphicBBS::SaveActualDiagram(std::string filename) {
841         ofstream file;
842         file.open(filename.c_str());
843
844         std::string content = "";
845
846         // writing file header
847         content += "# ----------------------------------\n";
848         content += "# - BBTKGEditor v ";
849         content += _currentBBGversion; //VERSION
850         content += " BBG BlackBox Diagram file\n";
851         content += "# - ";
852
853         content += filename;
854
855         content += "\n";
856         content += "# ----------------------------------\n";
857         content += "\n";
858         content += "APP_START\n";
859         _tabsMgr->saveActualDiagram(content, filename); //DFCH
860         content += "APP_END\n";
861         file << content;
862         file.close();
863 }
864
865 //=========================================================================
866 void wxGUIEditorGraphicBBS::SaveTempActualDiagram(const std::string &action) 
867 {
868         std::string stundo;
869         std::string stredo;
870         std::string name = _tabsMgr->GetNameTabPanel();
871         if(name[name.length() - 1] != '*')
872                 name +="*";
873         _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
874         _tabsMgr->saveTempActualDiagram(action);
875         GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState(stundo));
876         GetToolBar()->SetToolShortHelp(ID_UNDO,crea::std2wx(stundo));
877         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState(stredo));
878         GetToolBar()->SetToolShortHelp(ID_REDO,crea::std2wx(stredo));
879 }
880
881 //=========================================================================
882
883 //The following method allows to save the current diagram asking the filename
884 //DFCH
885 void wxGUIEditorGraphicBBS::SaveCurrentDiagramAs() 
886 {
887
888 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
889 #if wxMAJOR_VERSION <= 2
890         wxFileDialog * saveFileDialog = new wxFileDialog(this,
891                         wxT("Save current diagram"), wxEmptyString, wxT("NewDiagram"),
892                         wxT("*.bbg"), wxSAVE | wxOVERWRITE_PROMPT);
893 #else
894         wxFileDialog * saveFileDialog = new wxFileDialog(this,
895                         wxT("Save current diagram"), wxEmptyString, wxT("NewDiagram"),
896                         wxT("*.bbg"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
897 #endif
898
899         if (saveFileDialog->ShowModal() == wxID_OK) {
900                 //_tabsMgr->SetNameTabPanel(saveFileDialog->GetFilename());
901                 wxString pathfileName = saveFileDialog->GetPath();
902                 std::string pathfilename = (const char*) (pathfileName.mb_str());
903                 pathfilename=CheckExtension (pathfilename, ".bbg");
904
905                 //modify the tab name accordingly
906                 wxString fileName = saveFileDialog->GetFilename();
907                 std::string filename = (const char*) (fileName.mb_str());
908                 filename=CheckExtension (filename, ".bbg");
909                 _tabsMgr->SetNameTabPanel( wxString (filename.c_str(), wxConvUTF8) );
910
911                 std::string pathfilenamebbs = pathfilename;
912                 pathfilenamebbs[pathfilenamebbs.length() - 1] = 's';
913                 if (_tabsMgr->isActualDiagramComplexBox() == false) {
914                         SaveActualDiagram(pathfilename);
915                         SaveActualBBS(pathfilenamebbs);
916                 } else {
917                         AskComplexBoxConfiguration();
918                         SaveActualDiagram(pathfilename);
919                         SaveActualComplexBox(pathfilenamebbs);
920                 } //if isActualDiagramComplexBox
921         } // if saveFileDialog
922 }
923 //=========================================================================
924 //DFCH
925 void wxGUIEditorGraphicBBS::OnSaveActualDiagram(wxCommandEvent& event) {
926         std::string pathfilename = _tabsMgr->GetCurrentTabPanelPath();
927         if (pathfilename.empty()) {
928                 SaveCurrentDiagramAs();
929         } else {
930                 std::string pathfilenamebbs = pathfilename;
931                 pathfilenamebbs[pathfilenamebbs.length() - 1] = 's';
932                 
933                 if (_tabsMgr->isActualDiagramComplexBox() == false) {
934                         std::string name = _tabsMgr->GetNameTabPanel();
935                         if(name[name.length() - 1] == '*')
936                         {
937                                 name =name.substr(0,name.length()-1);
938                         }
939                         _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
940                         SaveActualDiagram(pathfilename);
941                         SaveActualBBS(pathfilenamebbs);
942                 } else {
943                         AskComplexBoxConfiguration();
944                         SaveActualDiagram(pathfilename);
945                         SaveActualComplexBox(pathfilenamebbs);
946                 } //if isActualDiagramComplexBox
947         }
948 }
949 //=========================================================================
950 //DFCH
951 void wxGUIEditorGraphicBBS::OnSaveAsActualDiagram(wxCommandEvent& event) {
952         SaveCurrentDiagramAs();
953 }
954
955 //=========================================================================
956 void wxGUIEditorGraphicBBS::OnExportConsoleExecCommand(wxCommandEvent& event)
957 {
958 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
959 #if wxMAJOR_VERSION <= 2
960         wxFileDialog * saveFileDialog = new wxFileDialog(this,
961                         wxT("Export console command"), wxEmptyString, wxT("ConsoleApp.txt"),
962                         wxT("*.txt;*.bbs;*.bbg;*.sh;*.bat"), wxSAVE | wxOVERWRITE_PROMPT);
963 #else
964         wxFileDialog * saveFileDialog = new wxFileDialog(this,
965                         wxT("Export console command"), wxEmptyString, wxT("ConsoleApp.txt"),
966                         wxT("*.txt;*.bbs;*.bbg;*.sh;*.bat"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
967 #endif
968
969
970         if (saveFileDialog->ShowModal() == wxID_OK)
971         {
972                 wxString fileName = saveFileDialog->GetPath();
973                 std::string filename = (const char*) (fileName.mb_str());
974                 filename=CheckExtension (filename, ".txt");
975
976                 ofstream file;
977                 file.open(filename.c_str());
978
979                 std::vector<std::string> extInputs = _tabsMgr->getActualTabPanel()->getSceneManager()->GetExternalInputsNames();
980
981                 //TODO PUT IN A METHOD OF A FILES AND TEXT MANAGER CLASS !!!
982                 //std::string inputsInCommentsWin="";
983                 //std::string inputsInCommentsUnix="";
984                 //std::string inputsInOneLineWin="";
985                 std::string inputsInOneLineUnix="";
986                 for (unsigned int i = 0; i < extInputs.size(); ++i)
987                 {
988                         /* No gusto la idea al jefe pero por si acaso ...
989                         * RaC 2012 2nd HackFest
990                         *
991                         inputsInCommentsWin+="REM ";
992                         inputsInCommentsWin+=extInputs[i];
993                         inputsInCommentsWin+="=\"value\"\n";
994                         inputsInCommentsUnix+="# ";
995                         inputsInCommentsUnix+=extInputs[i];
996                         inputsInCommentsUnix+="=\"value\"\n";
997
998                         char buffer [50];
999                         sprintf (buffer, "%d", (i+1));
1000                         inputsInOneLineWin+=extInputs[i];
1001                         inputsInOneLineWin+="=\"%";
1002                         inputsInOneLineWin+=buffer;
1003                         inputsInOneLineWin+="\" ";
1004                         */
1005                         inputsInOneLineUnix+=extInputs[i];
1006                         inputsInOneLineUnix+="=\"<value>\" ";
1007
1008                 }
1009
1010
1011                 std::string content = "";
1012                 // writing file header
1013                 content += "# ----------------------------------\n";
1014                 content += "# - BBTKGEditor v ";
1015                 content += _currentBBGversion; //VERSION
1016                 content += " Console Commands to execute BBS application\n";
1017                 content += "# - ";
1018                 content += filename;
1019                 content += "\n";
1020                 content += "# ----------------------------------\n";
1021                 content += "\n";
1022                 content += "\n";
1023                 content += "# Command to execute bbi\n";
1024                 content += "bbi <bbs_path> ";
1025                 content += inputsInOneLineUnix;
1026
1027                 /* No gusto la idea al jefe pero por si acaso ...
1028                  * RaC 2012 2nd HackFest
1029                  content += "# For Windows (copy the following lines to a .bat file)\n";
1030                  content += "REM BBS application\n";
1031                  content += "REM Inputs:\n";
1032                  content += inputsInCommentsWin;
1033                  content += "REM To use previous inputs, put %$inputName% in the corresponding input of the execution bbi line\n";
1034                  content += "bbi applicationBBS.bbs ";
1035                  content += inputsInOneLineWin;
1036                  content += "\n";
1037                  content += "\n";
1038                  content += "# For Linux or Mac (copy the following lines to a .sh file)\n";
1039                  content += "#!/bin/bash\n";
1040                  content += "# BBS application\n";
1041                  content += "# Inputs:\n";
1042                  content += inputsInCommentsUnix;
1043                  content += "# To use previous inputs, put $inputName in the corresponding input of the execution bbi line\n";
1044                  content += "bbi applicationBBS.bbs ";
1045                  content += inputsInOneLineUnix;
1046                  content += "\n";
1047                 */
1048
1049                 file << content;
1050                 file.close();
1051         }
1052
1053
1054 }
1055
1056 //=========================================================================
1057
1058 void wxGUIEditorGraphicBBS::refreshGUIControls() 
1059 {
1060         std::string stundo;
1061         std::string stredo;
1062         //RaC2012 2ndHackFest Next line seems weird ... To keep in mind for other devels
1063         wxToolBar* toolbar = GetToolBar();
1064         if (_tabsMgr->isActualDiagramComplexBox()) 
1065         {
1066                 toolbar->ToggleTool(ID_BTNCOMPLEXBOX, true);
1067                 //toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, true); RaC2012 2nd HackFest
1068                 toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT, true);
1069
1070                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_COMPLEXBOX, true);
1071                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_BBS, false);
1072                 GetMenuBar()->GetMenu(0)->Enable(ID_EXPORT_CONSOLEAPP, false);
1073         } else {
1074                 toolbar->ToggleTool(ID_BTNCOMPLEXBOX, false);
1075                 //toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, false); RaC2012 2nd HackFest
1076                 toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT, false);
1077
1078                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_COMPLEXBOX, false);
1079                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_BBS, true);
1080                 GetMenuBar()->GetMenu(0)->Enable(ID_EXPORT_CONSOLEAPP, true);
1081         }
1082
1083         //RaC2012 2ndHackFest
1084         GetToolBar()->EnableTool(ID_EDITPROPERTIES,true);
1085
1086         GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState(stundo));
1087         GetToolBar()->SetToolShortHelp(ID_UNDO,crea::std2wx(stundo));
1088         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState(stredo));
1089         GetToolBar()->SetToolShortHelp(ID_REDO,crea::std2wx(stredo));
1090
1091         if (_tabsMgr->getNumActualSelectedObjects() > 0) {
1092                 GetMenuBar()->GetMenu(1)->Enable(ID_COPY_TO_COMPLEXDIAGRAM, true);
1093         } else {
1094                 ///TODO Fix the possibility to say from the SceneManager to the GUI to refresh the options
1095                 GetMenuBar()->GetMenu(1)->Enable(ID_COPY_TO_COMPLEXDIAGRAM, true);
1096         }
1097
1098 }
1099
1100 //=========================================================================
1101
1102 void wxGUIEditorGraphicBBS::OnDeleteAllBoxesActualDiagram(wxCommandEvent& event) 
1103 {
1104         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);
1105         if (dial->ShowModal() == wxID_YES)
1106         {
1107                 _tabsMgr->deleteAllBoxesActualDiagram();
1108         }
1109 }
1110
1111 //=========================================================================
1112
1113 void wxGUIEditorGraphicBBS::OnCenterViewActualDiagram(wxCommandEvent& event) 
1114 {
1115         _tabsMgr->centerViewActualDiagram();
1116 }
1117
1118 //=========================================================================
1119 void wxGUIEditorGraphicBBS::OpenDiagram(std::string filePathName, std::string fileName) 
1120 {
1121         ifstream inputStream;   
1122         inputStream.open(filePathName.c_str());
1123         assert(inputStream.good()); // fails
1124         
1125         if(_tabsMgr->FindTab(filePathName)!=-1) //RaC2012 avoid opening same diagram twice
1126         {
1127                 inputStream.close();
1128                 printf("RaC: Diagram already opened\n");
1129                 return;
1130         }
1131         _tabsMgr->addNewTab( crea::std2wx(fileName) );
1132         _tabsMgr->loadDiagram(inputStream, filePathName);
1133
1134         _tabsMgr->saveTempActualDiagram("load diagram");
1135         GetToolBar()->EnableTool(ID_UNDO, false);
1136         GetToolBar()->EnableTool(ID_UNDO, false);
1137         inputStream.close();
1138         refreshGUIControls();
1139 }
1140         
1141 //=========================================================================
1142
1143 void wxGUIEditorGraphicBBS::OnOpenDiagram(wxCommandEvent& event) 
1144 {
1145
1146 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
1147 #if wxMAJOR_VERSION <= 2
1148         wxFileDialog * openFileDialog = new wxFileDialog(this, wxT("Open diagram"), wxEmptyString, wxT(""), wxT("*.bbg"), wxOPEN | wxFILE_MUST_EXIST);
1149 #else
1150         wxFileDialog * openFileDialog = new wxFileDialog(this, wxT("Open diagram"), wxEmptyString, wxT(""), wxT("*.bbg"), wxFD_OPEN | wxFD_FILE_MUST_EXIST);
1151 #endif
1152
1153         if (openFileDialog->ShowModal() == wxID_OK) 
1154         {
1155                 wxString filePath = openFileDialog->GetPath();
1156                 wxString fileName = openFileDialog->GetFilename();
1157                 OpenDiagram( crea::wx2std(filePath) , crea::wx2std(fileName) );                                 
1158         }
1159 }
1160
1161 //=========================================================================
1162 void wxGUIEditorGraphicBBS::OnClickBtnBox(wxCommandEvent& event) 
1163 {
1164         std::cout<<"wxGUIEditorGraphicBBS::OnClickBtnBox"<<std::endl;
1165         BlackBoxDescriptor *bbDes = _pkgBrowser->GetActualSelected();
1166         if (bbDes != NULL) {
1167                 std::string typeName = bbDes->GetTypeName();
1168                 std::string packageName = bbDes->GetPackage()->GetName();
1169                 wxGEditorTabPanel *pnl = this->_tabsMgr->getActualTabPanel();
1170                 if (pnl != NULL) {
1171                         wxVtkSceneManager *scn = pnl->getSceneManager();
1172                         scn->createGBlackBox(50, 50, packageName, typeName);
1173                         scn->refresh();
1174                 }
1175                 SaveTempActualDiagram("New Box");
1176                 // if pnl
1177         }// if bbDes
1178 }
1179
1180 //=========================================================================
1181
1182 void wxGUIEditorGraphicBBS::OnClickBtnExecutableBox(wxCommandEvent& event) 
1183 {
1184         //DFCH
1185         wxGEditorTabPanel *pnl = this->_tabsMgr->getActualTabPanel();
1186
1187         if (pnl != NULL) {
1188                 wxVtkSceneManager *scn = pnl->getSceneManager();
1189                 if( scn->MakeBoxExecutable() )
1190                         std::cout<<"DFCH: wxGUIEditorGraphicBBS::OnClickBtnExecutableBox...successful"<<std::endl;
1191                 else
1192                         std::cout<<"DFCH: wxGUIEditorGraphicBBS::OnClickBtnExecutableBox...Error!!"<<std::endl;
1193         }
1194         //DFCH
1195
1196 }
1197
1198 //=========================================================================
1199 void wxGUIEditorGraphicBBS::OnClickBtnComplexBox(wxCommandEvent& event) 
1200 {
1201         std::cout<<"wxGUIEditorGraphicBBS::OnClickBtnComplexBox"<<std::endl;
1202         wxToolBar* toolbar = GetToolBar();
1203         bool temp = toolbar->GetToolState(ID_BTNCOMPLEXBOX);
1204         if (temp) {
1205                 enableComplexBox();
1206         } else {
1207                 disableComplexBox();
1208         }
1209 }
1210
1211 //=========================================================================
1212 //EED01
1213 void wxGUIEditorGraphicBBS::OnAddComplexBoxInput(wxCommandEvent& event) 
1214 {
1215         wxTextEntryDialog *nameDialog = new wxTextEntryDialog(this,
1216                         wxT("Name of input port"));
1217         if (nameDialog->ShowModal() == wxID_OK) 
1218         {
1219                 wxString fileName = nameDialog->GetValue();
1220                 if (!fileName.IsEmpty()) 
1221                 {
1222                         std::string portName = (const char*) (fileName.mb_str());
1223                         if ( !boxNameExists(portName) )
1224                         {
1225                            _tabsMgr->addActualDiagramComplexInputPort(portName);
1226                         } else {
1227                            wxMessageDialog *dial = new wxMessageDialog(NULL,
1228                                        wxT("Impossible to create Box. The name already exists. Please provide another name"),
1229                                        wxT("Change name: name already exists"), wxOK | wxICON_EXCLAMATION | wxSTAY_ON_TOP);
1230                            dial->ShowModal();
1231                         }
1232                 } // if !fileName
1233         } // if namaDialog
1234 }
1235
1236 //=========================================================================
1237 void wxGUIEditorGraphicBBS::OnAddComplexBoxOutput(wxCommandEvent& event) 
1238 {
1239         wxTextEntryDialog *nameDialog = new wxTextEntryDialog(this,
1240                         wxT("Name of output port"));
1241         if (nameDialog->ShowModal() == wxID_OK) 
1242         {
1243                 wxString fileName = nameDialog->GetValue();
1244                 if (!fileName.IsEmpty()) 
1245                 {
1246                         std::string portName = (const char*) (fileName.mb_str());
1247                         if ( !boxNameExists(portName) )
1248                         {
1249                         _tabsMgr->addActualDiagramComplexOutputPort(portName);
1250                         } else {
1251                            wxMessageDialog *dial = new wxMessageDialog(NULL,
1252                                        wxT("Impossible to create Box. The name already exists. Please provide another name"),
1253                                        wxT("Change name: name already exists"), wxOK | wxICON_EXCLAMATION | wxSTAY_ON_TOP);
1254                            dial->ShowModal();
1255                         }
1256
1257                 } // if !filename
1258         } // namaDialog
1259 }
1260
1261 //=========================================================================
1262 void wxGUIEditorGraphicBBS::OnCopySelectedToComplexDiagram(
1263                 wxCommandEvent& event) 
1264 {
1265         _tabsMgr->copySelectedBBoxesToComplexDiagram();
1266 }
1267
1268 //=========================================================================
1269 void wxGUIEditorGraphicBBS::OnHelpBBeditor(wxCommandEvent& event) 
1270 {
1271         printf("wxGUIEditorGraphicBBS::OnHelpBBeditor ....not implemented..\n");
1272 }
1273
1274 //=========================================================================
1275 void wxGUIEditorGraphicBBS::OnCreatePackage(wxCommandEvent& event) 
1276 {
1277         bbtkSystemTools::CreatePackage();
1278 }
1279
1280 //=========================================================================
1281 void wxGUIEditorGraphicBBS::OnCreateBlackBox(wxCommandEvent& event) 
1282 {
1283         bbtkSystemTools::CreateBlackBox();
1284 }
1285         
1286 //=========================================================================
1287 void wxGUIEditorGraphicBBS::OnPlugPackage(wxCommandEvent& event) 
1288 {
1289         //DFGO2012 Plug Package functionnalities
1290         long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
1291         wxDirDialog* FD =
1292           new wxDirDialog( 0,
1293                            _T("Select package directory"),
1294                            _T(""),
1295                            style);
1296         long userResponse;
1297         do
1298         {
1299                 userResponse = FD->ShowModal();
1300                 if(userResponse==wxID_OK)
1301                 {
1302                         std::string path = wx2std (FD->GetPath()) ;
1303                         #ifdef WIN32
1304                         std::string fname = path + "\\bbtkPackage";
1305                         #else
1306                         std::string fname = path + "/bbtkPackage";
1307                         #endif
1308                         
1309                         std::cout << "Path chosen = \"" << FD->GetPath() << "\"" << std::endl;
1310                         if ( Utilities::FileExists( fname ) )
1311                         {
1312                                 std::ifstream f;
1313                                 f.open(fname.c_str());
1314                                 std::string pname;
1315                                 f >> pname;
1316                                 while(pname[0] == '#')
1317                                 {
1318                                         getline(f, pname, '\n');
1319                                         f >> pname;
1320                                 }
1321                                 f.close();
1322
1323                                 bbtk::ConfigurationFile::GetInstance().AddPackagePathsAndWrite( path );
1324
1325                                 DoRegeneratePackageDoc(pname);
1326                                 DoRegenerateBoxesLists();
1327
1328                                 _pkgBrowser->RebuildList();
1329
1330                                 wxMessageBox(_T("Package successfully plugged. Please restart bbEditor to see the new package in the package browser."),_T("Plug package"),wxOK | wxICON_INFORMATION);
1331                                 userResponse = wxID_CANCEL;
1332                         }
1333                         else
1334                         {
1335                                 std::string err = "The directory \"" + path + "\" does not contain a 'bbtkPackage' file.";
1336                                 #ifndef WIN32
1337                                         #ifndef MACOSX
1338                                                 err += "\nNote: If you are on Linux please select the folder and click \"Open\", don't enter in it.";
1339                                         #endif
1340                                 #endif
1341
1342                                 wxMessageBox(crea::std2wx(err), _T("Plug package"),wxOK | wxICON_ERROR);
1343                                 // THE FOLLOWING LINE GIVES ERRORS BECAUSE IT IS NECESSARY TO USE std2wx
1344                                 //wxMessageBox( _T( err) , _T("Plug package"),wxOK | wxICON_ERROR);
1345                         }
1346                 }
1347         }while(userResponse != wxID_CANCEL);
1348
1349 }
1350         
1351 //=========================================================================
1352 void wxGUIEditorGraphicBBS::OnEditConfig(wxCommandEvent& event) 
1353 {
1354         std::string commandStr;
1355             std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
1356         #ifdef WIN32
1357                 commandStr = "notepad.exe ";
1358         #else
1359                 commandStr = "gedit ";
1360         #endif
1361                 commandStr = commandStr + configFile;
1362                 std::cout << "system: " << commandStr << std::endl;
1363                 system ( commandStr.c_str() );
1364 }
1365         
1366 //=========================================================================
1367 void wxGUIEditorGraphicBBS::OnShowHTMLDoc(wxCommandEvent& event) 
1368 {
1369         // JGRR & CM WH
1370         if (_helpHtmlBrowser != NULL){
1371              _frameAUIMgr->DetachPane(_helpHtmlBrowser);
1372             _helpHtmlBrowser->Destroy();   
1373         }
1374         initHelpHTMLBrowser();
1375         _frameAUIMgr->AddPane(_helpHtmlBrowser, wxAuiPaneInfo().Right().MinSize(200, 200));
1376         _frameAUIMgr->Update();
1377         refreshGUIControls();
1378         
1379         // EO JGRR    & CM WH       
1380 }
1381         
1382 //=========================================================================
1383 void wxGUIEditorGraphicBBS::OnCreateIndex(wxCommandEvent& event) 
1384 {
1385         printf("EED wxGUIEditorGraphicBBS::OnCreateIndex ... not implemented ..\n");
1386 }
1387
1388         
1389 //=========================================================================
1390 void wxGUIEditorGraphicBBS::OnEditComplexBoxScript(wxCommandEvent& event)
1391 {
1392         BlackBoxDescriptor *bbDes = _pkgBrowser->GetActualSelected();
1393         if (bbDes != NULL) 
1394         {
1395                 std::string packageName = bbDes->GetPackage()->GetName();
1396                 std::string typeName = bbDes->GetTypeName();
1397                 if (!TryToOpenScriptComplexBox(packageName ,typeName))
1398                 {
1399                         wxMessageDialog windowMessage1(NULL,_T("This is not a ComplexBox script."), _T("Alert !"), wxOK);
1400                         windowMessage1.ShowModal();
1401                 }
1402         }// if bbDes
1403 }
1404
1405 //=========================================================================
1406 void wxGUIEditorGraphicBBS::OnExit(wxCommandEvent& event) 
1407 {
1408         Close(true);
1409 }
1410
1411 Factory::Pointer wxGUIEditorGraphicBBS::GetBBTKFactory()
1412 {
1413         if (_pkgBrowser==NULL) 
1414         {
1415                 printf ("EED ERROR! wxGUIEditorGraphicBBS::GetBBTKFactory _pkgBrowser = NULL\n");
1416         }
1417         return _pkgBrowser->GetFactory();
1418 }
1419
1420 //=========================================================================
1421 void wxGUIEditorGraphicBBS::enableComplexBox() 
1422 {
1423         _tabsMgr->setActualDiagramComplexBox(true);
1424         refreshGUIControls();
1425 }
1426
1427 //=========================================================================
1428
1429 void wxGUIEditorGraphicBBS::disableComplexBox() 
1430 {
1431         _tabsMgr->setActualDiagramComplexBox(false);
1432         refreshGUIControls();
1433 }
1434
1435 //=========================================================================
1436 void wxGUIEditorGraphicBBS::setCurrentDiagramDescription(std::string description)
1437 {
1438         _tabsMgr->SetDescription(description);
1439 }
1440
1441 //=========================================================================
1442 void wxGUIEditorGraphicBBS::setCurrentDiagramAuthor(std::string author)
1443 {
1444         _tabsMgr->SetAuthor(author);
1445 }
1446
1447 //=========================================================================
1448 void wxGUIEditorGraphicBBS::setCurrentDiagramCategory(std::string category)
1449 {
1450         _tabsMgr->SetCategory(category);
1451 }
1452
1453 //=========================================================================
1454 void wxGUIEditorGraphicBBS::setCurrentDiagramMessageKind(std::string kind){
1455         _tabsMgr->SetMessageKind(kind); 
1456 }
1457
1458 //=========================================================================
1459 void wxGUIEditorGraphicBBS::setCurrentDiagramMessageLevel(std::string level){
1460         _tabsMgr->SetMessageLevel(level);       
1461 }
1462
1463 //=========================================================================
1464 std::string wxGUIEditorGraphicBBS::getCurrentDiagramDescription()
1465 {
1466         return _tabsMgr->GetDescription();
1467 }
1468
1469 //=========================================================================
1470 std::string wxGUIEditorGraphicBBS::getCurrentDiagramAuthor()
1471 {
1472         return _tabsMgr->GetAuthor();
1473 }
1474
1475 //=========================================================================
1476 std::string wxGUIEditorGraphicBBS::getCurrentDiagramCategory()
1477 {
1478         return _tabsMgr->GetCategory();
1479 }
1480
1481 //=========================================================================
1482 std::string wxGUIEditorGraphicBBS::getCurrentDiagramMessageKind(){
1483         return _tabsMgr->GetMessageKind();
1484 }
1485
1486 //=========================================================================
1487 std::string wxGUIEditorGraphicBBS::getCurrentDiagramMessageLevel(){
1488         return _tabsMgr->GetMessageLevel();
1489 }
1490
1491 //=========================================================================
1492 bool wxGUIEditorGraphicBBS::isCurrentDiagramComplexBox()
1493 {
1494         return _tabsMgr->isActualDiagramComplexBox();
1495 }
1496
1497
1498 //=========================================================================
1499 void wxGUIEditorGraphicBBS::OpenScript(std::string filePathNameBBS ,std::string boxType)
1500 {       
1501         std::string fileName                                                    = boxType+".bbg";
1502         std::string filePathNameBBG                                             = filePathNameBBS;
1503         filePathNameBBG[ filePathNameBBG.length()-1 ]   = 'g';
1504         
1505         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);
1506         windowMessage1.ShowModal();
1507         
1508         if ( Utilities::FileExists( filePathNameBBG ) )
1509         {
1510                 OpenDiagram( filePathNameBBG , fileName );     
1511         } else {
1512                 if ( Utilities::FileExists( filePathNameBBS ) )
1513                 {
1514                         OpenBBS( filePathNameBBS , fileName );     
1515                 } else {
1516                         wxMessageDialog windowMessage1(NULL,_T("..ERROR.. loading script bbg/bbs."), _T("Alert !"), wxOK);
1517                         windowMessage1.ShowModal();
1518                 } // FileExists BBS
1519         } // FieExists BBG
1520 } //OpenScript
1521         
1522         
1523         
1524 //=========================================================================
1525 bool wxGUIEditorGraphicBBS::TryToOpenScriptApplication(std::string packageName ,std::string boxType)
1526 {       
1527         return TryToOpenScript(packageName ,boxType, true, false);
1528 }               
1529
1530 //=========================================================================
1531 bool wxGUIEditorGraphicBBS::TryToOpenScriptComplexBox(std::string packageName ,std::string boxType)
1532 {       
1533         return TryToOpenScript(packageName ,boxType, false, true);
1534 }               
1535         
1536         
1537 //=========================================================================
1538 bool wxGUIEditorGraphicBBS::TryToOpenScript(std::string packageName ,std::string boxType,bool scriptApplication, bool scriptComplexBox)
1539 {       
1540         Package::Pointer k;
1541         std::map< std::string, BlackBoxDescriptor::Pointer> mapDesc;
1542         BlackBoxDescriptor::Pointer descriptor;
1543         k                       = GetBBTKFactory()->GetPackage(packageName);
1544         mapDesc         = k->GetDescriptorMap();
1545         descriptor      = mapDesc[boxType];
1546
1547         bool result=false;
1548         
1549         if (
1550                 ((descriptor->IsTypeOfScript_Application()==true) && (scriptApplication==true))
1551                 ||
1552                 ((descriptor->IsTypeOfScript_ComplexBox()==true) && (scriptComplexBox==true))           
1553                 )
1554         {
1555                 result=true;
1556                 OpenScript(descriptor->GetScriptFileName(),boxType);
1557         }
1558         
1559         return result;
1560 }       
1561         
1562 } // EO namespace bbtk
1563
1564 // EOF
1565