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