]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/bbtkwxGUIEditorGraphicBBS.cxx
#3084 bbGEditor Bug New Normal - Color refresh for inputs and outputs
[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         wxFileDialog * openFileDialog = new wxFileDialog(this, wxT("Open BBS"), wxEmptyString, wxT("NewBBS"), wxT("*.bbs"), wxOPEN);
695
696         if (openFileDialog->ShowModal() == wxID_OK) 
697         {
698                 OpenBBS( crea::wx2std(openFileDialog->GetPath()) , crea::wx2std(openFileDialog->GetFilename())  );
699         }
700         
701 /*EED Borrame   
702         if (openFileDialog->ShowModal() == wxID_OK) 
703         {
704                 wxString fileName = openFileDialog->GetPath();
705                 _tabsMgr->addNewTab(openFileDialog->GetFilename());
706                 bbtk::InterpreterBBS::Pointer I = bbtk::InterpreterBBS::New( this->_tabsMgr->getActualTabPanel()->getSceneManager(), _pkgBrowser->GetFactory());
707                 // We tell the interpreter to throw exceptions on error
708                 I->SetThrow(true);
709                 // Interpret the file supposed to define a box called 'Processing'
710                 I->InterpretFile((const char*) (fileName.mb_str()));
711         }
712         refreshGUIControls();
713 */ 
714 }
715
716 //=========================================================================
717 void wxGUIEditorGraphicBBS::SaveActualBBS(std::string filename) {
718         ofstream file;
719
720         //EED           file.open(fileName.c_str());
721         file.open(filename.c_str());
722         std::string content = "";
723         // writing file header
724         content += "# ----------------------------------\n";
725         content += "# - BBTKGEditor v ";
726         content += _currentBBGversion; //VERSION
727         content += " BBS BlackBox Script\n";
728         content += "# - ";
729         content += filename;
730         content += "\n";
731         content += "# ----------------------------------\n";
732         content += "\n";
733         content += _tabsMgr->getActualDiagramBBS();
734         file << content;
735         file.close();
736 }
737
738 void wxGUIEditorGraphicBBS::OnSaveActualBBS(wxCommandEvent& event) {
739         wxFileDialog * saveFileDialog = new wxFileDialog(this,
740                         wxT("Save  actual BBS"), wxEmptyString, wxT("NewBBS"),
741                         wxT("*.bbs"), wxSAVE | wxOVERWRITE_PROMPT);
742         if (saveFileDialog->ShowModal() == wxID_OK) {
743                 wxString fileName = saveFileDialog->GetPath();
744                 std::string filename = (const char*) (fileName.mb_str());
745                 filename=CheckExtension (filename, ".bbs");
746                 SaveActualBBS(filename);
747         }
748
749 }
750
751 void wxGUIEditorGraphicBBS::AskComplexBoxConfiguration() {
752         std::string cbName = _tabsMgr->GetCbName();
753         std::string paName = _tabsMgr->GetCbPackageName();
754
755         wxTextEntryDialog *cbNameDialog = new wxTextEntryDialog(this,
756                         wxT("Complex Box name"));
757         wxTextEntryDialog *paNameDialog = new wxTextEntryDialog(this,
758                         wxT("Package name"));
759
760         cbNameDialog->SetValue(wxString(cbName.c_str(), wxConvUTF8));
761         paNameDialog->SetValue(wxString(paName.c_str(), wxConvUTF8));
762
763         if (cbNameDialog->ShowModal() == wxID_OK) {
764                 wxString complexboxname = cbNameDialog->GetValue();
765                 cbName = (const char*) (complexboxname.mb_str());
766         }
767
768         if (paNameDialog->ShowModal() == wxID_OK) {
769                 wxString packagename = paNameDialog->GetValue();
770                 paName = (const char*) (packagename.mb_str());
771         }
772
773         _tabsMgr->SetCbName(cbName);
774         _tabsMgr->SetCbPackageName(paName);
775 }
776
777 //=========================================================================
778
779 void wxGUIEditorGraphicBBS::SaveActualComplexBox(std::string filename) {
780         ofstream file;
781         file.open(filename.c_str());
782
783         std::string content = "";
784
785         // writing file header
786         content += "# ----------------------------------\n";
787         content += "# - BBTKGEditor v ";
788         content += _currentBBGversion;//VERSION
789         content += " BBS BlackBox Script (Complex Box)\n";
790         content += "# - ";
791         content += filename;
792         content += "\n";
793         content += "# ----------------------------------\n";
794         content += "\n";
795         content += _tabsMgr->getActualComplexBoxBBS();
796         file << content;
797         file.close();
798 }
799
800 //=========================================================================
801
802 void wxGUIEditorGraphicBBS::OnSaveActualComplexBox(wxCommandEvent& event) {
803         wxFileDialog * saveFileDialog = new wxFileDialog(this,
804                         wxT("Save Complex Box BBS"), wxEmptyString,
805                         _T("ComplexBoxName"), wxT("*.bbs"), wxSAVE
806                                         | wxOVERWRITE_PROMPT);
807         if (saveFileDialog->ShowModal() == wxID_OK) {
808                 wxString fileName = saveFileDialog->GetPath();
809                 std::string filename = (const char*) (fileName.mb_str());
810                 filename=CheckExtension (filename, ".bbs");
811                 SaveActualComplexBox(filename);
812         }
813 }
814
815 //=========================================================================
816 void wxGUIEditorGraphicBBS::SaveActualDiagram(std::string filename) {
817         ofstream file;
818         file.open(filename.c_str());
819
820         std::string content = "";
821
822         // writing file header
823         content += "# ----------------------------------\n";
824         content += "# - BBTKGEditor v ";
825         content += _currentBBGversion; //VERSION
826         content += " BBG BlackBox Diagram file\n";
827         content += "# - ";
828
829         content += filename;
830
831         content += "\n";
832         content += "# ----------------------------------\n";
833         content += "\n";
834         content += "APP_START\n";
835         _tabsMgr->saveActualDiagram(content, filename); //DFCH
836         content += "APP_END\n";
837         file << content;
838         file.close();
839 }
840
841 //=========================================================================
842 void wxGUIEditorGraphicBBS::SaveTempActualDiagram(const std::string &action) 
843 {
844         std::string stundo;
845         std::string stredo;
846         std::string name = _tabsMgr->GetNameTabPanel();
847         if(name[name.length() - 1] != '*')
848                 name +="*";
849         _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
850         _tabsMgr->saveTempActualDiagram(action);
851         GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState(stundo));
852         GetToolBar()->SetToolShortHelp(ID_UNDO,crea::std2wx(stundo));
853         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState(stredo));
854         GetToolBar()->SetToolShortHelp(ID_REDO,crea::std2wx(stredo));
855 }
856
857 //=========================================================================
858
859 //The following method allows to save the current diagram asking the filename
860 //DFCH
861 void wxGUIEditorGraphicBBS::SaveCurrentDiagramAs() {
862         wxFileDialog * saveFileDialog = new wxFileDialog(this,
863                         wxT("Save current diagram"), wxEmptyString, wxT("NewDiagram"),
864                         wxT("*.bbg"), wxSAVE | wxOVERWRITE_PROMPT);
865         if (saveFileDialog->ShowModal() == wxID_OK) {
866                 //_tabsMgr->SetNameTabPanel(saveFileDialog->GetFilename());
867                 wxString pathfileName = saveFileDialog->GetPath();
868                 std::string pathfilename = (const char*) (pathfileName.mb_str());
869                 pathfilename=CheckExtension (pathfilename, ".bbg");
870
871                 //modify the tab name accordingly
872                 wxString fileName = saveFileDialog->GetFilename();
873                 std::string filename = (const char*) (fileName.mb_str());
874                 filename=CheckExtension (filename, ".bbg");
875                 _tabsMgr->SetNameTabPanel( wxString (filename.c_str(), wxConvUTF8) );
876
877                 std::string pathfilenamebbs = pathfilename;
878                 pathfilenamebbs[pathfilenamebbs.length() - 1] = 's';
879                 if (_tabsMgr->isActualDiagramComplexBox() == false) {
880                         SaveActualDiagram(pathfilename);
881                         SaveActualBBS(pathfilenamebbs);
882                 } else {
883                         AskComplexBoxConfiguration();
884                         SaveActualDiagram(pathfilename);
885                         SaveActualComplexBox(pathfilenamebbs);
886                 } //if isActualDiagramComplexBox
887         } // if saveFileDialog
888 }
889 //=========================================================================
890 //DFCH
891 void wxGUIEditorGraphicBBS::OnSaveActualDiagram(wxCommandEvent& event) {
892         std::string pathfilename = _tabsMgr->GetCurrentTabPanelPath();
893         if (pathfilename.empty()) {
894                 SaveCurrentDiagramAs();
895         } else {
896                 std::string pathfilenamebbs = pathfilename;
897                 pathfilenamebbs[pathfilenamebbs.length() - 1] = 's';
898                 
899                 if (_tabsMgr->isActualDiagramComplexBox() == false) {
900                         std::string name = _tabsMgr->GetNameTabPanel();
901                         if(name[name.length() - 1] == '*')
902                         {
903                                 name =name.substr(0,name.length()-1);
904                         }
905                         _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
906                         SaveActualDiagram(pathfilename);
907                         SaveActualBBS(pathfilenamebbs);
908                 } else {
909                         AskComplexBoxConfiguration();
910                         SaveActualDiagram(pathfilename);
911                         SaveActualComplexBox(pathfilenamebbs);
912                 } //if isActualDiagramComplexBox
913         }
914 }
915 //=========================================================================
916 //DFCH
917 void wxGUIEditorGraphicBBS::OnSaveAsActualDiagram(wxCommandEvent& event) {
918         SaveCurrentDiagramAs();
919 }
920
921 //=========================================================================
922 void wxGUIEditorGraphicBBS::OnExportConsoleExecCommand(wxCommandEvent& event)
923 {
924         wxFileDialog * saveFileDialog = new wxFileDialog(this,
925                         wxT("Export console command"), wxEmptyString, wxT("ConsoleApp.txt"),
926                         wxT("*.txt;*.bbs;*.bbg;*.sh;*.bat"), wxSAVE | wxOVERWRITE_PROMPT);
927
928         if (saveFileDialog->ShowModal() == wxID_OK)
929         {
930                 wxString fileName = saveFileDialog->GetPath();
931                 std::string filename = (const char*) (fileName.mb_str());
932                 filename=CheckExtension (filename, ".txt");
933
934                 ofstream file;
935                 file.open(filename.c_str());
936
937                 std::vector<std::string> extInputs = _tabsMgr->getActualTabPanel()->getSceneManager()->GetExternalInputsNames();
938
939                 //TODO PUT IN A METHOD OF A FILES AND TEXT MANAGER CLASS !!!
940                 //std::string inputsInCommentsWin="";
941                 //std::string inputsInCommentsUnix="";
942                 //std::string inputsInOneLineWin="";
943                 std::string inputsInOneLineUnix="";
944                 for (unsigned int i = 0; i < extInputs.size(); ++i)
945                 {
946                         /* No gusto la idea al jefe pero por si acaso ...
947                         * RaC 2012 2nd HackFest
948                         *
949                         inputsInCommentsWin+="REM ";
950                         inputsInCommentsWin+=extInputs[i];
951                         inputsInCommentsWin+="=\"value\"\n";
952                         inputsInCommentsUnix+="# ";
953                         inputsInCommentsUnix+=extInputs[i];
954                         inputsInCommentsUnix+="=\"value\"\n";
955
956                         char buffer [50];
957                         sprintf (buffer, "%d", (i+1));
958                         inputsInOneLineWin+=extInputs[i];
959                         inputsInOneLineWin+="=\"%";
960                         inputsInOneLineWin+=buffer;
961                         inputsInOneLineWin+="\" ";
962                         */
963                         inputsInOneLineUnix+=extInputs[i];
964                         inputsInOneLineUnix+="=\"<value>\" ";
965
966                 }
967
968
969                 std::string content = "";
970                 // writing file header
971                 content += "# ----------------------------------\n";
972                 content += "# - BBTKGEditor v ";
973                 content += _currentBBGversion; //VERSION
974                 content += " Console Commands to execute BBS application\n";
975                 content += "# - ";
976                 content += filename;
977                 content += "\n";
978                 content += "# ----------------------------------\n";
979                 content += "\n";
980                 content += "\n";
981                 content += "# Command to execute bbi\n";
982                 content += "bbi <bbs_path> ";
983                 content += inputsInOneLineUnix;
984
985                 /* No gusto la idea al jefe pero por si acaso ...
986                  * RaC 2012 2nd HackFest
987                  content += "# For Windows (copy the following lines to a .bat file)\n";
988                  content += "REM BBS application\n";
989                  content += "REM Inputs:\n";
990                  content += inputsInCommentsWin;
991                  content += "REM To use previous inputs, put %$inputName% in the corresponding input of the execution bbi line\n";
992                  content += "bbi applicationBBS.bbs ";
993                  content += inputsInOneLineWin;
994                  content += "\n";
995                  content += "\n";
996                  content += "# For Linux or Mac (copy the following lines to a .sh file)\n";
997                  content += "#!/bin/bash\n";
998                  content += "# BBS application\n";
999                  content += "# Inputs:\n";
1000                  content += inputsInCommentsUnix;
1001                  content += "# To use previous inputs, put $inputName in the corresponding input of the execution bbi line\n";
1002                  content += "bbi applicationBBS.bbs ";
1003                  content += inputsInOneLineUnix;
1004                  content += "\n";
1005                 */
1006
1007                 file << content;
1008                 file.close();
1009         }
1010
1011
1012 }
1013
1014 //=========================================================================
1015
1016 void wxGUIEditorGraphicBBS::refreshGUIControls() 
1017 {
1018         std::string stundo;
1019         std::string stredo;
1020         //RaC2012 2ndHackFest Next line seems weird ... To keep in mind for other devels
1021         wxToolBar* toolbar = GetToolBar();
1022         if (_tabsMgr->isActualDiagramComplexBox()) 
1023         {
1024                 toolbar->ToggleTool(ID_BTNCOMPLEXBOX, true);
1025                 //toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, true); RaC2012 2nd HackFest
1026                 toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT, true);
1027
1028                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_COMPLEXBOX, true);
1029                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_BBS, false);
1030                 GetMenuBar()->GetMenu(0)->Enable(ID_EXPORT_CONSOLEAPP, false);
1031         } else {
1032                 toolbar->ToggleTool(ID_BTNCOMPLEXBOX, false);
1033                 //toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, false); RaC2012 2nd HackFest
1034                 toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT, false);
1035
1036                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_COMPLEXBOX, false);
1037                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_BBS, true);
1038                 GetMenuBar()->GetMenu(0)->Enable(ID_EXPORT_CONSOLEAPP, true);
1039         }
1040
1041         //RaC2012 2ndHackFest
1042         GetToolBar()->EnableTool(ID_EDITPROPERTIES,true);
1043
1044         GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState(stundo));
1045         GetToolBar()->SetToolShortHelp(ID_UNDO,crea::std2wx(stundo));
1046         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState(stredo));
1047         GetToolBar()->SetToolShortHelp(ID_REDO,crea::std2wx(stredo));
1048
1049         if (_tabsMgr->getNumActualSelectedObjects() > 0) {
1050                 GetMenuBar()->GetMenu(1)->Enable(ID_COPY_TO_COMPLEXDIAGRAM, true);
1051         } else {
1052                 ///TODO Fix the possibility to say from the SceneManager to the GUI to refresh the options
1053                 GetMenuBar()->GetMenu(1)->Enable(ID_COPY_TO_COMPLEXDIAGRAM, true);
1054         }
1055
1056 }
1057
1058 //=========================================================================
1059
1060 void wxGUIEditorGraphicBBS::OnDeleteAllBoxesActualDiagram(wxCommandEvent& event) 
1061 {
1062         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);
1063         if (dial->ShowModal() == wxID_YES)
1064         {
1065                 _tabsMgr->deleteAllBoxesActualDiagram();
1066         }
1067 }
1068
1069 //=========================================================================
1070
1071 void wxGUIEditorGraphicBBS::OnCenterViewActualDiagram(wxCommandEvent& event) 
1072 {
1073         _tabsMgr->centerViewActualDiagram();
1074 }
1075
1076 //=========================================================================
1077 void wxGUIEditorGraphicBBS::OpenDiagram(std::string filePathName, std::string fileName) 
1078 {
1079         ifstream inputStream;   
1080         inputStream.open(filePathName.c_str());
1081         assert(inputStream.good()); // fails
1082         
1083         if(_tabsMgr->FindTab(filePathName)!=-1) //RaC2012 avoid opening same diagram twice
1084         {
1085                 inputStream.close();
1086                 printf("RaC: Diagram already opened\n");
1087                 return;
1088         }
1089         _tabsMgr->addNewTab( crea::std2wx(fileName) );
1090         _tabsMgr->loadDiagram(inputStream, filePathName);
1091
1092         _tabsMgr->saveTempActualDiagram("load diagram");
1093         GetToolBar()->EnableTool(ID_UNDO, false);
1094         GetToolBar()->EnableTool(ID_UNDO, false);
1095         inputStream.close();
1096         refreshGUIControls();
1097 }
1098         
1099 //=========================================================================
1100
1101 void wxGUIEditorGraphicBBS::OnOpenDiagram(wxCommandEvent& event) 
1102 {
1103         wxFileDialog * openFileDialog = new wxFileDialog(this, wxT("Open diagram"), wxEmptyString, wxT(""), wxT("*.bbg"), wxOPEN | wxFILE_MUST_EXIST);
1104         if (openFileDialog->ShowModal() == wxID_OK) 
1105         {
1106                 wxString filePath = openFileDialog->GetPath();
1107                 wxString fileName = openFileDialog->GetFilename();
1108                 OpenDiagram( crea::wx2std(filePath) , crea::wx2std(fileName) );                                 
1109         }
1110 }
1111
1112 //=========================================================================
1113 void wxGUIEditorGraphicBBS::OnClickBtnBox(wxCommandEvent& event) 
1114 {
1115         std::cout<<"wxGUIEditorGraphicBBS::OnClickBtnBox"<<std::endl;
1116         BlackBoxDescriptor *bbDes = _pkgBrowser->GetActualSelected();
1117         if (bbDes != NULL) {
1118                 std::string typeName = bbDes->GetTypeName();
1119                 std::string packageName = bbDes->GetPackage()->GetName();
1120                 wxGEditorTabPanel *pnl = this->_tabsMgr->getActualTabPanel();
1121                 if (pnl != NULL) {
1122                         wxVtkSceneManager *scn = pnl->getSceneManager();
1123                         scn->createGBlackBox(50, 50, packageName, typeName);
1124                         scn->refresh();
1125                 }
1126                 SaveTempActualDiagram("New Box");
1127                 // if pnl
1128         }// if bbDes
1129 }
1130
1131 //=========================================================================
1132
1133 void wxGUIEditorGraphicBBS::OnClickBtnExecutableBox(wxCommandEvent& event) 
1134 {
1135         //DFCH
1136         wxGEditorTabPanel *pnl = this->_tabsMgr->getActualTabPanel();
1137
1138         if (pnl != NULL) {
1139                 wxVtkSceneManager *scn = pnl->getSceneManager();
1140                 if( scn->MakeBoxExecutable() )
1141                         std::cout<<"DFCH: wxGUIEditorGraphicBBS::OnClickBtnExecutableBox...successful"<<std::endl;
1142                 else
1143                         std::cout<<"DFCH: wxGUIEditorGraphicBBS::OnClickBtnExecutableBox...Error!!"<<std::endl;
1144         }
1145         //DFCH
1146
1147 }
1148
1149 //=========================================================================
1150 void wxGUIEditorGraphicBBS::OnClickBtnComplexBox(wxCommandEvent& event) 
1151 {
1152         std::cout<<"wxGUIEditorGraphicBBS::OnClickBtnComplexBox"<<std::endl;
1153         wxToolBar* toolbar = GetToolBar();
1154         bool temp = toolbar->GetToolState(ID_BTNCOMPLEXBOX);
1155         if (temp) {
1156                 enableComplexBox();
1157         } else {
1158                 disableComplexBox();
1159         }
1160 }
1161
1162 //=========================================================================
1163 //EED01
1164 void wxGUIEditorGraphicBBS::OnAddComplexBoxInput(wxCommandEvent& event) 
1165 {
1166         wxTextEntryDialog *nameDialog = new wxTextEntryDialog(this,
1167                         wxT("Name of input port"));
1168         if (nameDialog->ShowModal() == wxID_OK) 
1169         {
1170                 wxString fileName = nameDialog->GetValue();
1171                 if (!fileName.IsEmpty()) 
1172                 {
1173                         std::string portName = (const char*) (fileName.mb_str());
1174                         if ( !boxNameExists(portName) )
1175                         {
1176                            _tabsMgr->addActualDiagramComplexInputPort(portName);
1177                         } else {
1178                            wxMessageDialog *dial = new wxMessageDialog(NULL,
1179                                        wxT("Impossible to create Box. The name already exists. Please provide another name"),
1180                                        wxT("Change name: name already exists"), wxOK | wxICON_EXCLAMATION | wxSTAY_ON_TOP);
1181                            dial->ShowModal();
1182                         }
1183                 } // if !fileName
1184         } // if namaDialog
1185 }
1186
1187 //=========================================================================
1188 void wxGUIEditorGraphicBBS::OnAddComplexBoxOutput(wxCommandEvent& event) 
1189 {
1190         wxTextEntryDialog *nameDialog = new wxTextEntryDialog(this,
1191                         wxT("Name of output port"));
1192         if (nameDialog->ShowModal() == wxID_OK) 
1193         {
1194                 wxString fileName = nameDialog->GetValue();
1195                 if (!fileName.IsEmpty()) 
1196                 {
1197                         std::string portName = (const char*) (fileName.mb_str());
1198                         if ( !boxNameExists(portName) )
1199                         {
1200                         _tabsMgr->addActualDiagramComplexOutputPort(portName);
1201                         } else {
1202                            wxMessageDialog *dial = new wxMessageDialog(NULL,
1203                                        wxT("Impossible to create Box. The name already exists. Please provide another name"),
1204                                        wxT("Change name: name already exists"), wxOK | wxICON_EXCLAMATION | wxSTAY_ON_TOP);
1205                            dial->ShowModal();
1206                         }
1207
1208                 } // if !filename
1209         } // namaDialog
1210 }
1211
1212 //=========================================================================
1213 void wxGUIEditorGraphicBBS::OnCopySelectedToComplexDiagram(
1214                 wxCommandEvent& event) 
1215 {
1216         _tabsMgr->copySelectedBBoxesToComplexDiagram();
1217 }
1218
1219 //=========================================================================
1220 void wxGUIEditorGraphicBBS::OnHelpBBeditor(wxCommandEvent& event) 
1221 {
1222         printf("wxGUIEditorGraphicBBS::OnHelpBBeditor ....not implemented..\n");
1223 }
1224
1225 //=========================================================================
1226 void wxGUIEditorGraphicBBS::OnCreatePackage(wxCommandEvent& event) 
1227 {
1228         bbtkSystemTools::CreatePackage();
1229 }
1230
1231 //=========================================================================
1232 void wxGUIEditorGraphicBBS::OnCreateBlackBox(wxCommandEvent& event) 
1233 {
1234         bbtkSystemTools::CreateBlackBox();
1235 }
1236         
1237 //=========================================================================
1238 void wxGUIEditorGraphicBBS::OnPlugPackage(wxCommandEvent& event) 
1239 {
1240         //DFGO2012 Plug Package functionnalities
1241         long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
1242         wxDirDialog* FD =
1243           new wxDirDialog( 0,
1244                            _T("Select package directory"),
1245                            _T(""),
1246                            style);
1247         long userResponse;
1248         do
1249         {
1250                 userResponse = FD->ShowModal();
1251                 if(userResponse==wxID_OK)
1252                 {
1253                         std::string path = wx2std (FD->GetPath()) ;
1254                         #ifdef WIN32
1255                         std::string fname = path + "\\bbtkPackage";
1256                         #else
1257                         std::string fname = path + "/bbtkPackage";
1258                         #endif
1259                         
1260                         std::cout << "Path chosen = \"" << FD->GetPath() << "\"" << std::endl;
1261                         if ( Utilities::FileExists( fname ) )
1262                         {
1263                                 std::ifstream f;
1264                                 f.open(fname.c_str());
1265                                 std::string pname;
1266                                 f >> pname;
1267                                 while(pname[0] == '#')
1268                                 {
1269                                         getline(f, pname, '\n');
1270                                         f >> pname;
1271                                 }
1272                                 f.close();
1273
1274                                 bbtk::ConfigurationFile::GetInstance().AddPackagePathsAndWrite( path );
1275
1276                                 DoRegeneratePackageDoc(pname);
1277                                 DoRegenerateBoxesLists();
1278
1279                                 _pkgBrowser->RebuildList();
1280
1281                                 wxMessageBox(_T("Package successfully plugged. Please restart bbEditor to see the new package in the package browser."),_T("Plug package"),wxOK | wxICON_INFORMATION);
1282                                 userResponse = wxID_CANCEL;
1283                         }
1284                         else
1285                         {
1286                                 std::string err = "The directory \"" + path + "\" does not contain a 'bbtkPackage' file.";
1287                                 #ifndef WIN32
1288                                         #ifndef MACOSX
1289                                                 err += "\nNote: If you are on Linux please select the folder and click \"Open\", don't enter in it.";
1290                                         #endif
1291                                 #endif
1292
1293                                 wxMessageBox(crea::std2wx(err), _T("Plug package"),wxOK | wxICON_ERROR);
1294                                 // THE FOLLOWING LINE GIVES ERRORS BECAUSE IT IS NECESSARY TO USE std2wx
1295                                 //wxMessageBox( _T( err) , _T("Plug package"),wxOK | wxICON_ERROR);
1296                         }
1297                 }
1298         }while(userResponse != wxID_CANCEL);
1299
1300 }
1301         
1302 //=========================================================================
1303 void wxGUIEditorGraphicBBS::OnEditConfig(wxCommandEvent& event) 
1304 {
1305         std::string commandStr;
1306             std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
1307         #ifdef WIN32
1308                 commandStr = "notepad.exe ";
1309         #else
1310                 commandStr = "gedit ";
1311         #endif
1312                 commandStr = commandStr + configFile;
1313                 std::cout << "system: " << commandStr << std::endl;
1314                 system ( commandStr.c_str() );
1315 }
1316         
1317 //=========================================================================
1318 void wxGUIEditorGraphicBBS::OnShowHTMLDoc(wxCommandEvent& event) 
1319 {
1320         // JGRR & CM WH
1321         if (_helpHtmlBrowser != NULL){
1322              _frameAUIMgr->DetachPane(_helpHtmlBrowser);
1323             _helpHtmlBrowser->Destroy();   
1324         }
1325         initHelpHTMLBrowser();
1326         _frameAUIMgr->AddPane(_helpHtmlBrowser, wxAuiPaneInfo().Right().MinSize(200, 200));
1327         _frameAUIMgr->Update();
1328         refreshGUIControls();
1329         
1330         // EO JGRR    & CM WH       
1331 }
1332         
1333 //=========================================================================
1334 void wxGUIEditorGraphicBBS::OnCreateIndex(wxCommandEvent& event) 
1335 {
1336         printf("EED wxGUIEditorGraphicBBS::OnCreateIndex ... not implemented ..\n");
1337 }
1338
1339         
1340 //=========================================================================
1341 void wxGUIEditorGraphicBBS::OnEditComplexBoxScript(wxCommandEvent& event)
1342 {
1343         BlackBoxDescriptor *bbDes = _pkgBrowser->GetActualSelected();
1344         if (bbDes != NULL) 
1345         {
1346                 std::string packageName = bbDes->GetPackage()->GetName();
1347                 std::string typeName = bbDes->GetTypeName();
1348                 if (!TryToOpenScriptComplexBox(packageName ,typeName))
1349                 {
1350                         wxMessageDialog windowMessage1(NULL,_T("This is not a ComplexBox script."), _T("Alert !"), wxOK);
1351                         windowMessage1.ShowModal();
1352                 }
1353         }// if bbDes
1354 }
1355
1356 //=========================================================================
1357 void wxGUIEditorGraphicBBS::OnExit(wxCommandEvent& event) 
1358 {
1359         Close(true);
1360 }
1361
1362 Factory::Pointer wxGUIEditorGraphicBBS::GetBBTKFactory()
1363 {
1364         if (_pkgBrowser==NULL) 
1365         {
1366                 printf ("EED ERROR! wxGUIEditorGraphicBBS::GetBBTKFactory _pkgBrowser = NULL\n");
1367         }
1368         return _pkgBrowser->GetFactory();
1369 }
1370
1371 //=========================================================================
1372 void wxGUIEditorGraphicBBS::enableComplexBox() 
1373 {
1374         _tabsMgr->setActualDiagramComplexBox(true);
1375         refreshGUIControls();
1376 }
1377
1378 //=========================================================================
1379
1380 void wxGUIEditorGraphicBBS::disableComplexBox() 
1381 {
1382         _tabsMgr->setActualDiagramComplexBox(false);
1383         refreshGUIControls();
1384 }
1385
1386 //=========================================================================
1387 void wxGUIEditorGraphicBBS::setCurrentDiagramDescription(std::string description)
1388 {
1389         _tabsMgr->SetDescription(description);
1390 }
1391
1392 //=========================================================================
1393 void wxGUIEditorGraphicBBS::setCurrentDiagramAuthor(std::string author)
1394 {
1395         _tabsMgr->SetAuthor(author);
1396 }
1397
1398 //=========================================================================
1399 void wxGUIEditorGraphicBBS::setCurrentDiagramCategory(std::string category)
1400 {
1401         _tabsMgr->SetCategory(category);
1402 }
1403
1404 //=========================================================================
1405 void wxGUIEditorGraphicBBS::setCurrentDiagramMessageKind(std::string kind){
1406         _tabsMgr->SetMessageKind(kind); 
1407 }
1408
1409 //=========================================================================
1410 void wxGUIEditorGraphicBBS::setCurrentDiagramMessageLevel(std::string level){
1411         _tabsMgr->SetMessageLevel(level);       
1412 }
1413
1414 //=========================================================================
1415 std::string wxGUIEditorGraphicBBS::getCurrentDiagramDescription()
1416 {
1417         return _tabsMgr->GetDescription();
1418 }
1419
1420 //=========================================================================
1421 std::string wxGUIEditorGraphicBBS::getCurrentDiagramAuthor()
1422 {
1423         return _tabsMgr->GetAuthor();
1424 }
1425
1426 //=========================================================================
1427 std::string wxGUIEditorGraphicBBS::getCurrentDiagramCategory()
1428 {
1429         return _tabsMgr->GetCategory();
1430 }
1431
1432 //=========================================================================
1433 std::string wxGUIEditorGraphicBBS::getCurrentDiagramMessageKind(){
1434         return _tabsMgr->GetMessageKind();
1435 }
1436
1437 //=========================================================================
1438 std::string wxGUIEditorGraphicBBS::getCurrentDiagramMessageLevel(){
1439         return _tabsMgr->GetMessageLevel();
1440 }
1441
1442 //=========================================================================
1443 bool wxGUIEditorGraphicBBS::isCurrentDiagramComplexBox()
1444 {
1445         return _tabsMgr->isActualDiagramComplexBox();
1446 }
1447
1448
1449 //=========================================================================
1450 void wxGUIEditorGraphicBBS::OpenScript(std::string filePathNameBBS ,std::string boxType)
1451 {       
1452         std::string fileName                                                    = boxType+".bbg";
1453         std::string filePathNameBBG                                             = filePathNameBBS;
1454         filePathNameBBG[ filePathNameBBG.length()-1 ]   = 'g';
1455         
1456         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);
1457         windowMessage1.ShowModal();
1458         
1459         if ( Utilities::FileExists( filePathNameBBG ) )
1460         {
1461                 OpenDiagram( filePathNameBBG , fileName );     
1462         } else {
1463                 if ( Utilities::FileExists( filePathNameBBS ) )
1464                 {
1465                         OpenBBS( filePathNameBBS , fileName );     
1466                 } else {
1467                         wxMessageDialog windowMessage1(NULL,_T("..ERROR.. loading script bbg/bbs."), _T("Alert !"), wxOK);
1468                         windowMessage1.ShowModal();
1469                 } // FileExists BBS
1470         } // FieExists BBG
1471 } //OpenScript
1472         
1473         
1474         
1475 //=========================================================================
1476 bool wxGUIEditorGraphicBBS::TryToOpenScriptApplication(std::string packageName ,std::string boxType)
1477 {       
1478         return TryToOpenScript(packageName ,boxType, true, false);
1479 }               
1480
1481 //=========================================================================
1482 bool wxGUIEditorGraphicBBS::TryToOpenScriptComplexBox(std::string packageName ,std::string boxType)
1483 {       
1484         return TryToOpenScript(packageName ,boxType, false, true);
1485 }               
1486         
1487         
1488 //=========================================================================
1489 bool wxGUIEditorGraphicBBS::TryToOpenScript(std::string packageName ,std::string boxType,bool scriptApplication, bool scriptComplexBox)
1490 {       
1491         Package::Pointer k;
1492         std::map< std::string, BlackBoxDescriptor::Pointer> mapDesc;
1493         BlackBoxDescriptor::Pointer descriptor;
1494         k                       = GetBBTKFactory()->GetPackage(packageName);
1495         mapDesc         = k->GetDescriptorMap();
1496         descriptor      = mapDesc[boxType];
1497
1498         bool result=false;
1499         
1500         if (
1501                 ((descriptor->IsTypeOfScript_Application()==true) && (scriptApplication==true))
1502                 ||
1503                 ((descriptor->IsTypeOfScript_ComplexBox()==true) && (scriptComplexBox==true))           
1504                 )
1505         {
1506                 result=true;
1507                 OpenScript(descriptor->GetScriptFileName(),boxType);
1508         }
1509         
1510         return result;
1511 }       
1512         
1513 } // EO namespace bbtk
1514
1515 // EOF
1516