]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/bbtkwxGUIEditorGraphicBBS.cxx
25c4298571c017fe73111c325ae3b4d81f171053
[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 #include <wx/defs.h>
40
41 #include <InterpreterBBS.h>
42
43 namespace bbtk {
44
45 //=========================================================================
46 wxGUIEditorGraphicBBS::wxGUIEditorGraphicBBS(wxFrame *parent) :
47                         wxFrame(parent, -1, _T("bbtkGEditor"), wxDefaultPosition, wxSize(
48                                         1200, 800)) {
49
50         _frameAUIMgr = new wxAuiManager(this);
51
52         /*std::string datadir( crea::System::GetExecutablePath() );
53          std::string datadir (".");
54          cout<<"RaC //------------------"<<endl;
55          cout<<"RaC //TODO wxGUIEditorGraphicBBS constructor.Initialize datadir with  crea::System::GetExecutablePath() ."<<endl;
56          cout<<"RaC //------------------"<<endl;
57
58          #ifdef LINUX // assume this is OSX
59          datadir=datadir+"/../share/creaContours";
60          #endif // MACOSX
61
62          #ifdef MACOSX // assume this is OSX
63          datadir=datadir+"/../../../../share/creaContours";
64          #endif // MACOSX
65
66          _dataDir = datadir;*/
67
68         initMenu();
69         initToolbar();
70         initTabPanelsManager();
71         initPackageBrowser();
72         // JGGR & CM  initHelpHTMLBrowser();
73         _helpHtmlBrowser = NULL;
74         CreateStatusBar();
75         _frameAUIMgr->Update();
76         _actualPkgBrowserBoxName = "";
77         _actualPkgBrowserPkgName = "";
78         refreshGUIControls();
79 }
80
81 //=========================================================================
82 wxGUIEditorGraphicBBS::~wxGUIEditorGraphicBBS() {
83
84         printf("EED %p ~wxGUIEditorGraphicBBS()\n", this);
85
86         //EED02JUIN2010         delete _tabsMgr;
87
88         _frameAUIMgr->UnInit();
89         // FCY memory leaks
90         delete _frameAUIMgr;
91         delete _notebook;
92         delete _tabsMgr;
93
94 }
95
96 //=========================================================================
97 void wxGUIEditorGraphicBBS::initToolbar() {
98         wxBitmap bmp_new(new_xpm);
99         wxBitmap bmp_open(open_xpm);
100         wxBitmap bmp_save(save_xpm);
101         wxBitmap bmp_run(run_xpm);
102         wxBitmap bmp_delete(delete_xpm);
103         wxBitmap bmp_centerview(centerview_xpm);
104         wxBitmap bmp_box(box_xpm);
105         wxBitmap bmp_executablebox(executablebox_xpm);
106         wxBitmap bmp_complexbox(complexbox_xpm);
107         wxBitmap bmp_complexinputport(complexinputport_xpm);
108         wxBitmap bmp_complexoutputport(complexoutputport_xpm);
109         wxBitmap bmp_undo(undo_xpm);
110         wxBitmap bmp_redo(redo_xpm);
111         wxToolBar *toolbar = new wxToolBar(this, wxID_ANY);
112
113         //Adds a tool btn to the toolbar
114         toolbar->AddTool(ID_NEW, _T("New"), bmp_new, wxNullBitmap, wxITEM_NORMAL,
115                         _T("New tab"), _T("Create a new panel tab"));
116         toolbar->AddTool(ID_OPEN, _T("Open diagram"), bmp_open, wxNullBitmap,
117                         wxITEM_NORMAL, _T("Open a diagram"), _T("Open a diagram"));
118         toolbar->AddTool(ID_SAVE_DIAGRAM, _T("Save Diagram"), bmp_save,
119                         wxNullBitmap, wxITEM_NORMAL, _T("Saves the current diagram"),
120                         _T("Saves the current diagram")); //DFCH
121         //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
122         toolbar->AddTool(ID_RUN, _T("Run"), bmp_run, wxNullBitmap, wxITEM_NORMAL,
123                         _T("Execute actual diagram"), _T("Execute actual diagram"));
124         toolbar->AddTool(ID_DELETEALL, _T("Delete all"), bmp_delete, wxNullBitmap,
125                         wxITEM_NORMAL, _T("Delete all boxes"), _T("Delete all boxes"));
126         toolbar->AddTool(ID_CENTERVIEW, _T("Center view"), bmp_centerview,
127                         wxNullBitmap, wxITEM_NORMAL, _T("Center view"), _T("Center view"));
128         toolbar->AddSeparator();
129         toolbar->AddTool(ID_BTNBOX, _T("New Box"), bmp_box, wxNullBitmap,
130                         wxITEM_NORMAL, _T("New Box"), _T("Nex Box"));
131         toolbar->AddSeparator();
132         toolbar->AddTool(ID_BTEXECUTABLEBOX, _T("Select as executable Box"),
133                         bmp_executablebox, wxNullBitmap, wxITEM_NORMAL,
134                         _T("Select as executable Box"), _T("Select as executable Box"));
135         toolbar->AddSeparator();
136         toolbar->AddTool(ID_BTNCOMPLEXBOX, _T("Complex box"), complexbox_xpm,
137                         wxNullBitmap, wxITEM_NORMAL, _T("Complex box"), _T("Complex box"));
138         toolbar->SetToggle(ID_BTNCOMPLEXBOX, true);
139         toolbar->AddTool(ID_ADDCOMPLEXBOXINPUT, _T("Add input to Complex box"),
140                         bmp_complexinputport, wxNullBitmap, wxITEM_NORMAL,
141                         _T("Add input to Complex box"), _T("Add input to Complex box"));
142         toolbar->AddTool(ID_ADDCOMPLEXBOXOUTPUT, _T("Add output to Complex box"),
143                         bmp_complexoutputport, wxNullBitmap, wxITEM_NORMAL,
144                         _T("Add output Complex box"), _T("Add output Complex box"));
145         toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, false);
146         toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT, false);
147         toolbar->AddSeparator();
148         toolbar->AddTool(ID_UNDO, _T("Undo"),bmp_undo, wxNullBitmap, wxITEM_NORMAL,     _T("Undo"), _T("Undo"));
149         toolbar->AddTool(ID_REDO, _T("Redo"),bmp_redo, wxNullBitmap, wxITEM_NORMAL,     _T("Redo"), _T("Redo"));
150         toolbar->EnableTool(ID_UNDO, false);
151         toolbar->EnableTool(ID_REDO, false);
152         toolbar->SetMargins(2, 2);
153         toolbar->Realize();
154         SetToolBar(toolbar);
155
156         
157         // connect command event handlers
158
159         Connect(ID_NEW, wxEVT_COMMAND_TOOL_CLICKED,
160                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCreateNewTab));
161         Connect(ID_OPEN, wxEVT_COMMAND_TOOL_CLICKED,
162                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnOpenDiagram));
163         Connect(ID_SAVE_DIAGRAM, wxEVT_COMMAND_TOOL_CLICKED,
164                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveActualDiagram)); //DFCH
165         Connect(ID_SAVE_AS_DIAGRAM, wxEVT_COMMAND_TOOL_CLICKED,
166                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveAsActualDiagram)); //DFCH
167         Connect(
168                         ID_RUN,
169                         wxEVT_COMMAND_TOOL_CLICKED,
170                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnExecuteActualDiagram));
171         Connect(
172                         ID_DELETEALL,
173                         wxEVT_COMMAND_TOOL_CLICKED,
174                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnDeleteAllBoxesActualDiagram));
175         Connect(
176                         ID_CENTERVIEW,
177                         wxEVT_COMMAND_TOOL_CLICKED,
178                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCenterViewActualDiagram));
179         Connect(ID_BTNBOX, wxEVT_COMMAND_TOOL_CLICKED,
180                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnClickBtnBox));
181         Connect(
182                         ID_BTEXECUTABLEBOX,
183                         wxEVT_COMMAND_TOOL_CLICKED,
184                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnClickBtnExecutableBox));
185         Connect(ID_BTNCOMPLEXBOX, wxEVT_COMMAND_TOOL_CLICKED,
186                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnClickBtnComplexBox));
187         Connect(ID_ADDCOMPLEXBOXINPUT, wxEVT_COMMAND_TOOL_CLICKED,
188                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnAddComplexBoxInput));
189         Connect(ID_ADDCOMPLEXBOXOUTPUT, wxEVT_COMMAND_TOOL_CLICKED,
190                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnAddComplexBoxOutput));
191         Connect(ID_UNDO,wxEVT_COMMAND_TOOL_CLICKED,     wxCommandEventHandler(wxGUIEditorGraphicBBS::OnUndo));
192         Connect(ID_REDO,wxEVT_COMMAND_TOOL_CLICKED,     wxCommandEventHandler(wxGUIEditorGraphicBBS::OnRedo));
193         Connect(ID_CHANGENAME, wxEVT_COMMAND_RIGHT_CLICK, wxCommandEventHandler(wxGUIEditorGraphicBBS::OnChangeName));
194 }
195 //=========================================================================
196 void wxGUIEditorGraphicBBS::initHelpHTMLBrowser() {
197         // JGRR & CM 
198         _helpHtmlBrowser = new WxGUIHtmlBrowser(this, wxSize(200, 0));
199    
200         // EO JGRR & CM 
201         
202 }
203
204 //=========================================================================
205 void wxGUIEditorGraphicBBS::initMenu() {
206         // Create File menu and its items
207         wxMenu *fileMenu = new wxMenu();
208         fileMenu->Append(ID_NEW, _T("New diagram"), _T("New"));
209         fileMenu->Append(ID_SAVE_DIAGRAM, _T("Save Diagram"), _T("Save Diagram")); //DFCH
210         fileMenu->Append(ID_SAVE_AS_DIAGRAM, _T("Save Diagram As"),
211                         _T("Save Diagram As")); //DFCH
212         fileMenu->Append(ID_OPEN_BBS, _T("Open BBS"), _T("Open BBS"));
213         // CM
214         fileMenu->Append(ID_OPEN, _T("Open a diagram"), _T("Open a diagram"));
215         fileMenu->Append(ID_SAVE_AS_BBS, _T("Save BBS"), _T("Save BBS"));
216         fileMenu->Append(ID_SAVE_AS_COMPLEXBOX, _T("Save complex box"),
217                         _T("Save complex box"));
218         fileMenu->Append(wxID_EXIT, _T("E&xit"), _T("Exit"));
219
220         Connect(ID_NEW, wxEVT_COMMAND_MENU_SELECTED,
221                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCreateNewTab));
222         Connect(ID_SAVE_DIAGRAM, wxEVT_COMMAND_MENU_SELECTED,
223                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveActualDiagram)); //DFCH
224         Connect(ID_SAVE_AS_DIAGRAM, wxEVT_COMMAND_MENU_SELECTED,
225                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveAsActualDiagram));
226         Connect(ID_OPEN_BBS, wxEVT_COMMAND_MENU_SELECTED,
227                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnOpenBBS));
228         Connect(ID_SAVE_AS_BBS, wxEVT_COMMAND_MENU_SELECTED,
229                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveActualBBS));
230         Connect(
231                         ID_SAVE_AS_COMPLEXBOX,
232                         wxEVT_COMMAND_MENU_SELECTED,
233                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnSaveActualComplexBox));
234         Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
235                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnExit));
236
237         // Create Help menu and its items
238         wxMenu *editMenu = new wxMenu;
239         editMenu->Append(ID_COPY_TO_COMPLEXDIAGRAM,
240                         _T("Copy selected to complex box"),
241                         _T("Creates a new complex box diagram with selected boxes"));
242
243         Connect(
244                         ID_COPY_TO_COMPLEXDIAGRAM,
245                         wxEVT_COMMAND_MENU_SELECTED,
246                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCopySelectedToComplexDiagram));
247
248         // Create Help menu and its items
249         wxMenu *helpMenu = new wxMenu;
250         helpMenu->Append(wxID_ABOUT, _T("&About..."), _T("About"));
251         helpMenu->Append(ID_HELP_BBEDITOR, _T("&Help..."), _T("Help"));
252         Connect(ID_HELP_BBEDITOR, wxEVT_COMMAND_MENU_SELECTED,
253                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnHelpBBeditor));
254
255         // Create Tool menu
256         wxMenu *menuTools = new wxMenu;
257         menuTools->Append(ID_bbEditor_Tool_Menu_CreatePackage,
258                         _T("Create &package") );
259         menuTools->Append(ID_bbEditor_Tool_Menu_CreateBlackBox,
260                         _T("Create &blackbox") );
261         menuTools->Append(ID_bbEditor_Tool_Menu_PlugPackage, _T("Plug package") );
262         menuTools->Append(ID_bbEditor_Tool_Menu_EditConfig, _T("&Edit bbtk config") );
263         menuTools->Append(ID_bbEditor_Tool_Menu_ShowHTMLDoc,
264                         _T("Show &HTML documentation") );
265         menuTools->Append(ID_bbEditor_Tool_Menu_CreateIndex, _T("&Generate index") );
266
267         Connect(ID_bbEditor_Tool_Menu_CreatePackage, wxEVT_COMMAND_MENU_SELECTED,
268                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCreatePackage));
269         Connect(ID_bbEditor_Tool_Menu_CreateBlackBox, wxEVT_COMMAND_MENU_SELECTED,
270                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCreateBlackBox));
271         Connect(ID_bbEditor_Tool_Menu_PlugPackage, wxEVT_COMMAND_MENU_SELECTED,
272                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnPlugPackage));
273         Connect(ID_bbEditor_Tool_Menu_EditConfig, wxEVT_COMMAND_MENU_SELECTED,
274                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnEditConfig));
275         Connect(ID_bbEditor_Tool_Menu_ShowHTMLDoc, wxEVT_COMMAND_MENU_SELECTED,
276                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnShowHTMLDoc));
277         Connect(ID_bbEditor_Tool_Menu_CreateIndex, wxEVT_COMMAND_MENU_SELECTED,
278                         wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCreateIndex));
279
280         // Append the created menu to the menu bar
281         wxMenuBar *menuBar = new wxMenuBar();
282         menuBar->Append(fileMenu, _T("&File") );
283         menuBar->Append(editMenu, _T("&Edit") );
284         menuBar->Append(menuTools, _T("&Tool") );
285         menuBar->Append(helpMenu, _T("&Help") );
286
287         // Attach this menu bar to the frame
288         SetMenuBar(menuBar);
289 }
290
291 //=========================================================================
292 void wxGUIEditorGraphicBBS::initTabPanelsManager() {
293         _notebook = new wxAuiNotebook(this, wxID_NOTEBOOK, wxDefaultPosition,
294                         wxDefaultSize, wxAUI_NB_TOP | wxAUI_NB_TAB_MOVE
295                                         | wxAUI_NB_CLOSE_ON_ACTIVE_TAB | wxAUI_NB_TAB_FIXED_WIDTH
296                                         | wxAUI_NB_WINDOWLIST_BUTTON);
297         _tabsMgr = new wxTabPanelsManager(this);
298         _frameAUIMgr->AddPane(_notebook, wxAuiPaneInfo().CenterPane());
299
300 }
301
302 //=========================================================================
303 void wxGUIEditorGraphicBBS::initPackageBrowser() {
304         _pkgBrowser = new WxGUIPackageBrowser2(this);
305         _pkgBrowser->IncludeAll();
306         _frameAUIMgr->AddPane(_pkgBrowser,
307                         wxAuiPaneInfo().Left().MinSize(200, 200).CloseButton(false));
308 }
309
310 //================================================================
311
312 void wxGUIEditorGraphicBBS::DoRegeneratePackageDoc(const std::string& pack) {
313         std::string mess("Regenerating doc for package '");
314         if (pack != "-a") {
315                 mess += pack + "'";
316         } else {
317                 mess = "Regenerating doc for all packages";
318         }
319
320         mess += " ... please wait";
321
322         SetStatusText(std2wx(mess));
323
324         //BBTK_BUSY_CURSOR;
325
326         std::string command;
327 #if defined(WIN32)
328         command = "\"";
329 #endif
330         command += ConfigurationFile::GetInstance().Get_bin_path();
331         command += ConfigurationFile::GetInstance().Get_file_separator();
332         command += "bbRegeneratePackageDoc";
333 #if defined(WIN32)
334         command += "\"";
335 #endif
336
337         command += " " + pack + " -q";
338         bbtkMessage("debug",1,"Executing system command '"<<command<<"'"<<std::endl);
339
340         if (!system(command.c_str())) {
341                 SetStatusText(_T("Done !"));
342                 /*
343                  wxMessageBox(_T("Done !"),_T("Regenerate package '")
344                  +std2wx(pack)+_T("' doc"),
345                  wxOK | wxICON_INFORMATION);
346                  */
347         } else {
348                 SetStatusText(_T("Done !"));
349                 wxString err(_T("An error occured while running '"));
350                 err += bbtk::std2wx(command) + _T("'");
351                 wxMessageBox(err, _T("Regenerate package doc"), wxOK | wxICON_ERROR);
352         }
353 }
354
355 //================================================================
356 void wxGUIEditorGraphicBBS::DoRegenerateBoxesLists() {
357         SetStatusText(_T("Regenerating boxes lists ... please wait"));
358         //BBTK_BUSY_CURSOR ;
359
360         std::string command;
361 #if defined(WIN32)
362         command = "\"";
363 #endif
364         command += ConfigurationFile::GetInstance().Get_bin_path();
365         command += ConfigurationFile::GetInstance().Get_file_separator();
366         command += "bbRegenerateBoxesLists";
367
368 #if defined(WIN32)
369         command += "\"";
370 #endif
371         command += " -q";
372
373         bbtkMessage("debug",1,"Executing system command '"<<command<<"'"<<std::endl);
374
375         if (!system(command.c_str())) {
376                 SetStatusText(_T("Done !"));
377                 /*
378                  wxMessageBox(_T("Done !"),_T("Regenerate boxes lists"),
379                  wxOK | wxICON_INFORMATION);
380                  */
381         } else {
382                 SetStatusText(_T("Done !"));
383                 wxString err(_T("An error occured while running '"));
384                 err += bbtk::std2wx(command) + _T("'");
385                 wxMessageBox(err, _T("Regenerate boxes lists"), wxOK | wxICON_ERROR);
386         }
387 }
388
389 //================================================================
390 void wxGUIEditorGraphicBBS::RegenerateAll() {
391         DoRegeneratePackageDoc("-a");
392         DoRegenerateBoxesLists();
393 }
394
395 //================================================================
396
397 wxAuiNotebook* wxGUIEditorGraphicBBS::getAuiNotebook() {
398         return _notebook;
399 }
400
401 //================================================================
402
403 void wxGUIEditorGraphicBBS::displayBlackBoxInfo(std::string packageName,
404                 std::string boxName) {
405         if (_actualPkgBrowserPkgName != packageName || _actualPkgBrowserBoxName
406                         != boxName) {
407                 _actualPkgBrowserPkgName = packageName;
408                 _actualPkgBrowserBoxName = boxName;
409
410                 BlackBoxDescriptor::Pointer descriptor =
411                                 GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(
412                                                 packageName, boxName);
413                 _pkgBrowser->WxGUIBlackBoxListUserOnSelected(descriptor.get());
414         }
415 }
416
417 //=========================================================================
418
419 void wxGUIEditorGraphicBBS::editBlackBox(GBlackBoxModel *bbmodel) {
420         wxBlackBoxEditionDialog* dialog =
421                         new wxBlackBoxEditionDialog(this, bbmodel);
422         dialog->Show();
423 }
424
425 //=========================================================================
426
427 bool wxGUIEditorGraphicBBS::boxNameExists(std::string boxname) {
428  
429         return this->_tabsMgr->getActualTabPanel()->getSceneManager()->boxExist(boxname);
430 }
431
432 //=========================================================================
433
434 void wxGUIEditorGraphicBBS::updateStatusBar(std::string textStatus) {
435         SetStatusText(crea::std2wx(textStatus));
436 }
437
438 //=========================================================================
439
440 void wxGUIEditorGraphicBBS::executeScript(std::string script) {
441         std::string separator =
442                         ConfigurationFile::GetInstance().Get_file_separator();
443         std::string dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
444         std::string filename = dir + separator + "tmp_bbtk.bbs";
445
446         ofstream tempFile;
447         tempFile.open(filename.c_str());
448         tempFile << script;
449         tempFile.close();
450
451         std::string command = "";
452
453 #ifdef WIN32
454         command += "start /b ";
455 #endif
456
457         //              command += "\""+ crea::System::GetExecutablePath();
458         //#ifdef MACOSX
459         //              command += separator + "../../../bbi.app/Contents/MacOS";
460         //#endif
461
462         command += "bbi ";
463         command += "\"" + filename + "\"";
464
465 #ifndef WIN32
466         command += " & ";
467 #endif
468
469         printf("RaC wxGUIEditorGraphicBBS::executeScript %s \n", command.c_str());
470         system(command.c_str());
471 }
472
473 //=========================================================================
474 std::string wxGUIEditorGraphicBBS::CheckExtension(std::string filename, std::string extension) {
475         if(filename.size() > extension.size() ){
476                 if( (filename.compare(filename.size()-extension.size(), extension.size(),extension)) !=0 ){
477                         filename.append(extension);
478                 }
479         }else{
480                 filename.append(extension);
481         }
482         return filename;
483 }
484 //=========================================================================
485 // EVENT HANDLERS
486 //=========================================================================
487
488 void wxGUIEditorGraphicBBS::OnCreateNewTab(wxCommandEvent& event) {
489         _tabsMgr->addNewTab();
490         refreshGUIControls();
491 }
492
493 void wxGUIEditorGraphicBBS::OnUndo(wxCommandEvent& event) {
494         unsigned short disable;
495         _tabsMgr->loadTempDiagram(0);
496         disable = _tabsMgr->getUndoState();
497         if(disable == 0)
498         {
499                 std::string name = _tabsMgr->GetNameTabPanel();
500                 if(name[name.length() - 1] == '*')
501                 {
502                         name =name.substr(0,name.length()-1);
503                 }
504                 _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
505         }
506
507         GetToolBar()->EnableTool(ID_UNDO,disable);
508         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState());
509         refreshGUIControls();
510 }
511
512 void wxGUIEditorGraphicBBS::OnRedo(wxCommandEvent& event) {
513         unsigned short disable;
514         _tabsMgr->loadTempDiagram(1);
515         GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState());
516         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState());
517         refreshGUIControls();
518 }
519
520 void wxGUIEditorGraphicBBS::OnChangeName(wxCommandEvent& event){
521 }
522 //=========================================================================
523
524 void wxGUIEditorGraphicBBS::OnExecuteActualDiagram(wxCommandEvent& event) {
525         std::string script1 = _tabsMgr->getActualDiagramBBS(true);
526         cout << endl << "RaC wxGUIEditorGraphicBBS::executeActualDiagram SCRIPT"
527                         << endl << endl;
528         cout << script1 << endl;
529
530         std::string script2 = _tabsMgr->getActualDiagramBBS(false);
531         executeScript(script2);
532 }
533
534 //=========================================================================
535 // HANDLERS
536 //=========================================================================
537 void wxGUIEditorGraphicBBS::OnOpenBBS(wxCommandEvent& event) {
538         wxFileDialog * openFileDialog = new wxFileDialog(this, wxT("Open BBS"),
539                         wxEmptyString, wxT("NewBBS"), wxT("*.bbs"), wxOPEN);
540         if (openFileDialog->ShowModal() == wxID_OK) {
541                 wxString fileName = openFileDialog->GetPath();
542
543                 _tabsMgr->addNewTab(openFileDialog->GetFilename());
544
545                 bbtk::InterpreterBBS::Pointer I = bbtk::InterpreterBBS::New(
546                                 this->_tabsMgr->getActualTabPanel()->getSceneManager(),
547                                 _pkgBrowser->GetFactory());
548
549                 // We tell the interpreter to throw exceptions on error
550                 I->SetThrow(true);
551                 // Interpret the file supposed to define a box called 'Processing'
552                 I->InterpretFile((const char*) (fileName.mb_str()));
553         }
554         refreshGUIControls();
555 }
556
557 void wxGUIEditorGraphicBBS::SaveActualBBS(std::string filename) {
558         ofstream file;
559
560         //EED           file.open(fileName.c_str());
561         file.open(filename.c_str());
562         std::string content = "";
563         // writing file header
564         content += "# ----------------------------------\n";
565         content += "# - BBTKGEditor v 1.2 BBS BlackBox Script\n";
566         content += "# - ";
567         content += filename;
568         content += "\n";
569         content += "# ----------------------------------\n";
570         content += "\n";
571         content += _tabsMgr->getActualDiagramBBS();
572         file << content;
573         file.close();
574 }
575
576 void wxGUIEditorGraphicBBS::OnSaveActualBBS(wxCommandEvent& event) {
577         wxFileDialog * saveFileDialog = new wxFileDialog(this,
578                         wxT("Save  actual BBS"), wxEmptyString, wxT("NewBBS"),
579                         wxT("*.bbs"), wxSAVE | wxOVERWRITE_PROMPT);
580         if (saveFileDialog->ShowModal() == wxID_OK) {
581                 wxString fileName = saveFileDialog->GetPath();
582                 std::string filename = (const char*) (fileName.mb_str());
583                 filename=CheckExtension (filename, ".bbs");
584                 SaveActualBBS(filename);
585         }
586
587 }
588
589 void wxGUIEditorGraphicBBS::AskComplexBoxConfiguration() {
590         std::string cbName = _tabsMgr->GetCbName();
591         std::string paName = _tabsMgr->GetCbPackageName();
592
593         wxTextEntryDialog *cbNameDialog = new wxTextEntryDialog(this,
594                         wxT("Complex Box name"));
595         wxTextEntryDialog *paNameDialog = new wxTextEntryDialog(this,
596                         wxT("Package name"));
597
598         cbNameDialog->SetValue(wxString(cbName.c_str(), wxConvUTF8));
599         paNameDialog->SetValue(wxString(paName.c_str(), wxConvUTF8));
600
601         if (cbNameDialog->ShowModal() == wxID_OK) {
602                 wxString complexboxname = cbNameDialog->GetValue();
603                 cbName = (const char*) (complexboxname.mb_str());
604         }
605
606         if (paNameDialog->ShowModal() == wxID_OK) {
607                 wxString packagename = paNameDialog->GetValue();
608                 paName = (const char*) (packagename.mb_str());
609         }
610
611         _tabsMgr->SetCbName(cbName);
612         _tabsMgr->SetCbPackageName(paName);
613 }
614
615 //=========================================================================
616
617 void wxGUIEditorGraphicBBS::SaveActualComplexBox(std::string filename) {
618         ofstream file;
619         file.open(filename.c_str());
620
621         std::string content = "";
622
623         // writing file header
624         content += "# ----------------------------------\n";
625         content += "# - BBTKGEditor v 1.2 BBS BlackBox Script (Complex Box)\n";
626         content += "# - ";
627         content += filename;
628         content += "\n";
629         content += "# ----------------------------------\n";
630         content += "\n";
631         content += _tabsMgr->getActualComplexBoxBBS();
632         file << content;
633         file.close();
634 }
635
636 //=========================================================================
637
638 void wxGUIEditorGraphicBBS::OnSaveActualComplexBox(wxCommandEvent& event) {
639         wxFileDialog * saveFileDialog = new wxFileDialog(this,
640                         wxT("Save Complex Box BBS"), wxEmptyString,
641                         _T("ComplexBoxName"), wxT("*.bbs"), wxSAVE
642                                         | wxOVERWRITE_PROMPT);
643         if (saveFileDialog->ShowModal() == wxID_OK) {
644                 wxString fileName = saveFileDialog->GetPath();
645                 std::string filename = (const char*) (fileName.mb_str());
646                 filename=CheckExtension (filename, ".bbs");
647                 SaveActualComplexBox(filename);
648         }
649 }
650
651 //=========================================================================
652 void wxGUIEditorGraphicBBS::SaveActualDiagram(std::string filename) {
653         ofstream file;
654         file.open(filename.c_str());
655
656         std::string content = "";
657
658         // writing file header
659         content += "# ----------------------------------\n";
660         content += "# - BBTKGEditor v 1.2 BBG BlackBox Diagram file\n";
661         content += "# - ";
662
663         content += filename;
664
665         content += "\n";
666         content += "# ----------------------------------\n";
667         content += "\n";
668         content += "APP_START\n";
669         _tabsMgr->saveActualDiagram(content, filename); //DFCH
670         content += "APP_END\n";
671         file << content;
672         file.close();
673 }
674
675 //=========================================================================
676 void wxGUIEditorGraphicBBS::SaveTempActualDiagram() 
677 {
678         std::string name = _tabsMgr->GetNameTabPanel();
679         if(name[name.length() - 1] != '*')
680                 name +="*";
681         _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
682         _tabsMgr->saveTempActualDiagram();
683 }
684
685 //=========================================================================
686
687 //The following method allows to save the current diagram asking the filename
688 //DFCH
689 void wxGUIEditorGraphicBBS::SaveCurrentDiagramAs() {
690         wxFileDialog * saveFileDialog = new wxFileDialog(this,
691                         wxT("Save current diagram"), wxEmptyString, wxT("NewDiagram"),
692                         wxT("*.bbg"), wxSAVE | wxOVERWRITE_PROMPT);
693         if (saveFileDialog->ShowModal() == wxID_OK) {
694                 //_tabsMgr->SetNameTabPanel(saveFileDialog->GetFilename());
695                 wxString pathfileName = saveFileDialog->GetPath();
696                 std::string pathfilename = (const char*) (pathfileName.mb_str());
697                 pathfilename=CheckExtension (pathfilename, ".bbg");
698
699                 //modify the tab name accordingly
700                 wxString fileName = saveFileDialog->GetFilename();
701                 std::string filename = (const char*) (fileName.mb_str());
702                 filename=CheckExtension (filename, ".bbg");
703                 _tabsMgr->SetNameTabPanel( wxString (filename.c_str(), wxConvUTF8) );
704
705                 std::string pathfilenamebbs = pathfilename;
706                 pathfilenamebbs[pathfilenamebbs.length() - 1] = 's';
707                 if (_tabsMgr->isActualDiagramComplexBox() == false) {
708                         SaveActualDiagram(pathfilename);
709                         SaveActualBBS(pathfilenamebbs);
710                 } else {
711                         AskComplexBoxConfiguration();
712                         SaveActualDiagram(pathfilename);
713                         SaveActualComplexBox(pathfilenamebbs);
714                 } //if isActualDiagramComplexBox
715         } // if saveFileDialog
716 }
717 //=========================================================================
718 //DFCH
719 void wxGUIEditorGraphicBBS::OnSaveActualDiagram(wxCommandEvent& event) {
720         std::string pathfilename = _tabsMgr->GetCurrentTabPanelPath();
721         if (pathfilename.empty()) {
722                 SaveCurrentDiagramAs();
723         } else {
724                 std::string pathfilenamebbs = pathfilename;
725                 pathfilenamebbs[pathfilenamebbs.length() - 1] = 's';
726                 
727                 if (_tabsMgr->isActualDiagramComplexBox() == false) {
728                         SaveActualDiagram(pathfilename);
729                         SaveActualBBS(pathfilenamebbs);
730                 } else {
731                         AskComplexBoxConfiguration();
732                         SaveActualDiagram(pathfilename);
733                         SaveActualComplexBox(pathfilenamebbs);
734                 } //if isActualDiagramComplexBox
735         }
736 }
737 //=========================================================================
738 //DFCH
739 void wxGUIEditorGraphicBBS::OnSaveAsActualDiagram(wxCommandEvent& event) {
740         SaveCurrentDiagramAs();
741 }
742 //=========================================================================
743
744 void wxGUIEditorGraphicBBS::refreshGUIControls() {
745         wxToolBar* toolbar = GetToolBar();
746         if (_tabsMgr->isActualDiagramComplexBox()) {
747                 toolbar->ToggleTool(ID_BTNCOMPLEXBOX, true);
748                 toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, true);
749                 toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT, true);
750
751                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_COMPLEXBOX, true);
752                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_BBS, false);
753         } else {
754                 toolbar->ToggleTool(ID_BTNCOMPLEXBOX, false);
755                 toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, false);
756                 toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT, false);
757
758                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_COMPLEXBOX, false);
759                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_BBS, true);
760         }
761
762         GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState());
763         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState());
764
765         if (_tabsMgr->getNumActualSelectedObjects() > 0) {
766                 GetMenuBar()->GetMenu(1)->Enable(ID_COPY_TO_COMPLEXDIAGRAM, true);
767         } else {
768                 ///TODO Fix the possibility to say from the SceneManager to the GUI to refresh the options
769                 GetMenuBar()->GetMenu(1)->Enable(ID_COPY_TO_COMPLEXDIAGRAM, true);
770         }
771
772 }
773
774 //=========================================================================
775
776 void wxGUIEditorGraphicBBS::OnDeleteAllBoxesActualDiagram(wxCommandEvent& event) {
777         wxMessageDialog *dial = new wxMessageDialog(NULL,
778                         wxT("Are you sure to delete all boxes of the diagram?"),
779                         wxT("Delete all boxes"), wxYES_NO | wxNO_DEFAULT
780                                         | wxICON_QUESTION);
781         if (dial->ShowModal() == wxID_YES) {
782                 _tabsMgr->deleteAllBoxesActualDiagram();
783         }
784 }
785
786 //=========================================================================
787
788 void wxGUIEditorGraphicBBS::OnCenterViewActualDiagram(wxCommandEvent& event) {
789         _tabsMgr->centerViewActualDiagram();
790 }
791
792 //=========================================================================
793
794 void wxGUIEditorGraphicBBS::OnOpenDiagram(wxCommandEvent& event) {
795         wxFileDialog * openFileDialog = new wxFileDialog(this, wxT("Open diagram"),
796                         wxEmptyString, wxT(""), wxT("*.bbg"), wxOPEN | wxFILE_MUST_EXIST);
797         if (openFileDialog->ShowModal() == wxID_OK) {
798                 wxString fileName = openFileDialog->GetPath();
799                 ifstream inputStream;
800                 std::string fName = (const char*) (fileName.mb_str());
801
802                 inputStream.open(fName.c_str());
803                 assert(inputStream.good()); // fails
804                 _tabsMgr->addNewTab(openFileDialog->GetFilename());
805                 _tabsMgr->loadDiagram(inputStream, fName);
806
807                 inputStream.close();
808         }
809         refreshGUIControls();
810 }
811
812 //=========================================================================
813
814
815 void wxGUIEditorGraphicBBS::OnClickBtnBox(wxCommandEvent& event) {
816         BlackBoxDescriptor *bbDes = _pkgBrowser->GetActualSelected();
817         if (bbDes != NULL) {
818                 std::string typeName = bbDes->GetTypeName();
819                 std::string packageName = bbDes->GetPackage()->GetName();
820
821                 wxGEditorTabPanel *pnl = this->_tabsMgr->getActualTabPanel();
822                 
823                 // save empty state!!
824         /*      if (_tabsMgr->getNumActualSelectedObjects() == 0)
825                 {
826                         SaveTempActualDiagram();
827                 }
828                 */
829                 if (pnl != NULL) {
830                         wxVtkSceneManager *scn = pnl->getSceneManager();
831                         scn->createGBlackBox(50, 50, packageName, typeName);
832                         scn->refresh();
833                 }
834                 if (GetToolBar()->GetToolEnabled(ID_UNDO) == false)
835                         GetToolBar()->EnableTool(ID_UNDO,true);
836                 SaveTempActualDiagram();
837                 // if pnl
838         }// if bbDes
839 }
840
841 //=========================================================================
842
843 void wxGUIEditorGraphicBBS::OnClickBtnExecutableBox(wxCommandEvent& event) {
844         //DFCH
845         wxGEditorTabPanel *pnl = this->_tabsMgr->getActualTabPanel();
846
847         if (pnl != NULL) {
848                 wxVtkSceneManager *scn = pnl->getSceneManager();
849                 if( scn->MakeBoxExecutable() )
850                         std::cout<<"DFCH: wxGUIEditorGraphicBBS::OnClickBtnExecutableBox...successful"<<std::endl;
851                 else
852                         std::cout<<"DFCH: wxGUIEditorGraphicBBS::OnClickBtnExecutableBox...Error!!"<<std::endl;
853         }
854         //DFCH
855
856 }
857
858 //=========================================================================
859
860 void wxGUIEditorGraphicBBS::OnClickBtnComplexBox(wxCommandEvent& event) {
861         wxToolBar* toolbar = GetToolBar();
862         bool temp = toolbar->GetToolState(ID_BTNCOMPLEXBOX);
863         if (temp) {
864                 _tabsMgr->setActualDiagramComplexBox(true);
865         } else {
866                 _tabsMgr->setActualDiagramComplexBox(false);
867         }
868         refreshGUIControls();
869 }
870
871 //=========================================================================
872
873 void wxGUIEditorGraphicBBS::OnAddComplexBoxInput(wxCommandEvent& event) {
874         wxTextEntryDialog *nameDialog = new wxTextEntryDialog(this,
875                         wxT("Name of input port"));
876         if (nameDialog->ShowModal() == wxID_OK) {
877                 wxString fileName = nameDialog->GetValue();
878                 if (!fileName.IsEmpty()) {
879                         std::string portName = (const char*) (fileName.mb_str());
880                         _tabsMgr->addActualDiagramComplexInputPort(portName);
881                 }
882         }
883 }
884
885 //=========================================================================
886
887 void wxGUIEditorGraphicBBS::OnAddComplexBoxOutput(wxCommandEvent& event) {
888         wxTextEntryDialog *nameDialog = new wxTextEntryDialog(this,
889                         wxT("Name of output port"));
890         if (nameDialog->ShowModal() == wxID_OK) {
891                 wxString fileName = nameDialog->GetValue();
892                 if (!fileName.IsEmpty()) {
893                         std::string portName = (const char*) (fileName.mb_str());
894                         _tabsMgr->addActualDiagramComplexOutputPort(portName);
895                 }
896         }
897 }
898
899 //=========================================================================
900
901 void wxGUIEditorGraphicBBS::OnCopySelectedToComplexDiagram(
902                 wxCommandEvent& event) {
903         _tabsMgr->copySelectedBBoxesToComplexDiagram();
904 }
905
906 void wxGUIEditorGraphicBBS::OnHelpBBeditor(wxCommandEvent& event) {
907         printf("wxGUIEditorGraphicBBS::OnHelpBBeditor ....not implemented..\n");
908 }
909
910 //=========================================================================
911 void wxGUIEditorGraphicBBS::OnCreatePackage(wxCommandEvent& event) {
912
913         bbtkSystemTools::CreatePackage();
914 }
915
916 //=========================================================================
917 void wxGUIEditorGraphicBBS::OnCreateBlackBox(wxCommandEvent& event) {
918         bbtkSystemTools::CreateBlackBox();
919 }
920 //=========================================================================
921 void wxGUIEditorGraphicBBS::OnPlugPackage(wxCommandEvent& event) {
922         printf("EED wxGUIEditorGraphicBBS::OnPlugPackage ... not implemented ..\n");
923 }
924 //=========================================================================
925 void wxGUIEditorGraphicBBS::OnEditConfig(wxCommandEvent& event) {
926         printf("EED wxGUIEditorGraphicBBS::OnEditConfig ... not implemented ..\n");
927     }
928     //=========================================================================
929
930     void wxGUIEditorGraphicBBS::OnShowHTMLDoc(wxCommandEvent& event) {
931         // JGRR & CM WH
932         if (_helpHtmlBrowser != NULL){
933              _frameAUIMgr->DetachPane(_helpHtmlBrowser);
934             _helpHtmlBrowser->Destroy();   
935         }
936         initHelpHTMLBrowser();
937         _frameAUIMgr->AddPane(_helpHtmlBrowser, wxAuiPaneInfo().Right().MinSize(200, 200));
938         _frameAUIMgr->Update();
939         refreshGUIControls();
940         
941         // EO JGRR    & CM WH       
942 }
943 //=========================================================================
944 void wxGUIEditorGraphicBBS::OnCreateIndex(wxCommandEvent& event) {
945         printf("EED wxGUIEditorGraphicBBS::OnCreateIndex ... not implemented ..\n");
946 }
947
948 //=========================================================================
949
950 void wxGUIEditorGraphicBBS::OnExit(wxCommandEvent& event) {
951         Close(true);
952 }
953
954
955
956 //=========================================================================
957
958 } // EO namespace bbtk
959
960 // EOF
961