]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/bbtkwxGUIEditorGraphicBBS.cxx
Bug #1377
[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         std::string name = _tabsMgr->GetNameTabPanel();
516         if(name[name.length() - 1] != '*')
517                 name +="*";
518         _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
519         GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState());
520         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState());
521         refreshGUIControls();
522 }
523
524 void wxGUIEditorGraphicBBS::OnChangeName(wxCommandEvent& event){
525 }
526 //=========================================================================
527
528 void wxGUIEditorGraphicBBS::OnExecuteActualDiagram(wxCommandEvent& event) {
529         std::string script1 = _tabsMgr->getActualDiagramBBS(true);
530         cout << endl << "RaC wxGUIEditorGraphicBBS::executeActualDiagram SCRIPT"
531                         << endl << endl;
532         cout << script1 << endl;
533
534         std::string script2 = _tabsMgr->getActualDiagramBBS(false);
535         executeScript(script2);
536 }
537
538 //=========================================================================
539 // HANDLERS
540 //=========================================================================
541 void wxGUIEditorGraphicBBS::OnOpenBBS(wxCommandEvent& event) {
542         wxFileDialog * openFileDialog = new wxFileDialog(this, wxT("Open BBS"),
543                         wxEmptyString, wxT("NewBBS"), wxT("*.bbs"), wxOPEN);
544         if (openFileDialog->ShowModal() == wxID_OK) {
545                 wxString fileName = openFileDialog->GetPath();
546
547                 _tabsMgr->addNewTab(openFileDialog->GetFilename());
548
549                 bbtk::InterpreterBBS::Pointer I = bbtk::InterpreterBBS::New(
550                                 this->_tabsMgr->getActualTabPanel()->getSceneManager(),
551                                 _pkgBrowser->GetFactory());
552
553                 // We tell the interpreter to throw exceptions on error
554                 I->SetThrow(true);
555                 // Interpret the file supposed to define a box called 'Processing'
556                 I->InterpretFile((const char*) (fileName.mb_str()));
557         }
558         refreshGUIControls();
559 }
560
561 void wxGUIEditorGraphicBBS::SaveActualBBS(std::string filename) {
562         ofstream file;
563
564         //EED           file.open(fileName.c_str());
565         file.open(filename.c_str());
566         std::string content = "";
567         // writing file header
568         content += "# ----------------------------------\n";
569         content += "# - BBTKGEditor v 1.2 BBS BlackBox Script\n";
570         content += "# - ";
571         content += filename;
572         content += "\n";
573         content += "# ----------------------------------\n";
574         content += "\n";
575         content += _tabsMgr->getActualDiagramBBS();
576         file << content;
577         file.close();
578 }
579
580 void wxGUIEditorGraphicBBS::OnSaveActualBBS(wxCommandEvent& event) {
581         wxFileDialog * saveFileDialog = new wxFileDialog(this,
582                         wxT("Save  actual BBS"), wxEmptyString, wxT("NewBBS"),
583                         wxT("*.bbs"), wxSAVE | wxOVERWRITE_PROMPT);
584         if (saveFileDialog->ShowModal() == wxID_OK) {
585                 wxString fileName = saveFileDialog->GetPath();
586                 std::string filename = (const char*) (fileName.mb_str());
587                 filename=CheckExtension (filename, ".bbs");
588                 SaveActualBBS(filename);
589         }
590
591 }
592
593 void wxGUIEditorGraphicBBS::AskComplexBoxConfiguration() {
594         std::string cbName = _tabsMgr->GetCbName();
595         std::string paName = _tabsMgr->GetCbPackageName();
596
597         wxTextEntryDialog *cbNameDialog = new wxTextEntryDialog(this,
598                         wxT("Complex Box name"));
599         wxTextEntryDialog *paNameDialog = new wxTextEntryDialog(this,
600                         wxT("Package name"));
601
602         cbNameDialog->SetValue(wxString(cbName.c_str(), wxConvUTF8));
603         paNameDialog->SetValue(wxString(paName.c_str(), wxConvUTF8));
604
605         if (cbNameDialog->ShowModal() == wxID_OK) {
606                 wxString complexboxname = cbNameDialog->GetValue();
607                 cbName = (const char*) (complexboxname.mb_str());
608         }
609
610         if (paNameDialog->ShowModal() == wxID_OK) {
611                 wxString packagename = paNameDialog->GetValue();
612                 paName = (const char*) (packagename.mb_str());
613         }
614
615         _tabsMgr->SetCbName(cbName);
616         _tabsMgr->SetCbPackageName(paName);
617 }
618
619 //=========================================================================
620
621 void wxGUIEditorGraphicBBS::SaveActualComplexBox(std::string filename) {
622         ofstream file;
623         file.open(filename.c_str());
624
625         std::string content = "";
626
627         // writing file header
628         content += "# ----------------------------------\n";
629         content += "# - BBTKGEditor v 1.2 BBS BlackBox Script (Complex Box)\n";
630         content += "# - ";
631         content += filename;
632         content += "\n";
633         content += "# ----------------------------------\n";
634         content += "\n";
635         content += _tabsMgr->getActualComplexBoxBBS();
636         file << content;
637         file.close();
638 }
639
640 //=========================================================================
641
642 void wxGUIEditorGraphicBBS::OnSaveActualComplexBox(wxCommandEvent& event) {
643         wxFileDialog * saveFileDialog = new wxFileDialog(this,
644                         wxT("Save Complex Box BBS"), wxEmptyString,
645                         _T("ComplexBoxName"), wxT("*.bbs"), wxSAVE
646                                         | wxOVERWRITE_PROMPT);
647         if (saveFileDialog->ShowModal() == wxID_OK) {
648                 wxString fileName = saveFileDialog->GetPath();
649                 std::string filename = (const char*) (fileName.mb_str());
650                 filename=CheckExtension (filename, ".bbs");
651                 SaveActualComplexBox(filename);
652         }
653 }
654
655 //=========================================================================
656 void wxGUIEditorGraphicBBS::SaveActualDiagram(std::string filename) {
657         ofstream file;
658         file.open(filename.c_str());
659
660         std::string content = "";
661
662         // writing file header
663         content += "# ----------------------------------\n";
664         content += "# - BBTKGEditor v 1.2 BBG BlackBox Diagram file\n";
665         content += "# - ";
666
667         content += filename;
668
669         content += "\n";
670         content += "# ----------------------------------\n";
671         content += "\n";
672         content += "APP_START\n";
673         _tabsMgr->saveActualDiagram(content, filename); //DFCH
674         content += "APP_END\n";
675         file << content;
676         file.close();
677 }
678
679 //=========================================================================
680 void wxGUIEditorGraphicBBS::SaveTempActualDiagram() 
681 {
682         std::string name = _tabsMgr->GetNameTabPanel();
683         if(name[name.length() - 1] != '*')
684                 name +="*";
685         _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
686         _tabsMgr->saveTempActualDiagram();
687         GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState());
688         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState());
689 }
690
691 //=========================================================================
692
693 //The following method allows to save the current diagram asking the filename
694 //DFCH
695 void wxGUIEditorGraphicBBS::SaveCurrentDiagramAs() {
696         wxFileDialog * saveFileDialog = new wxFileDialog(this,
697                         wxT("Save current diagram"), wxEmptyString, wxT("NewDiagram"),
698                         wxT("*.bbg"), wxSAVE | wxOVERWRITE_PROMPT);
699         if (saveFileDialog->ShowModal() == wxID_OK) {
700                 //_tabsMgr->SetNameTabPanel(saveFileDialog->GetFilename());
701                 wxString pathfileName = saveFileDialog->GetPath();
702                 std::string pathfilename = (const char*) (pathfileName.mb_str());
703                 pathfilename=CheckExtension (pathfilename, ".bbg");
704
705                 //modify the tab name accordingly
706                 wxString fileName = saveFileDialog->GetFilename();
707                 std::string filename = (const char*) (fileName.mb_str());
708                 filename=CheckExtension (filename, ".bbg");
709                 _tabsMgr->SetNameTabPanel( wxString (filename.c_str(), wxConvUTF8) );
710
711                 std::string pathfilenamebbs = pathfilename;
712                 pathfilenamebbs[pathfilenamebbs.length() - 1] = 's';
713                 if (_tabsMgr->isActualDiagramComplexBox() == false) {
714                         SaveActualDiagram(pathfilename);
715                         SaveActualBBS(pathfilenamebbs);
716                 } else {
717                         AskComplexBoxConfiguration();
718                         SaveActualDiagram(pathfilename);
719                         SaveActualComplexBox(pathfilenamebbs);
720                 } //if isActualDiagramComplexBox
721         } // if saveFileDialog
722 }
723 //=========================================================================
724 //DFCH
725 void wxGUIEditorGraphicBBS::OnSaveActualDiagram(wxCommandEvent& event) {
726         std::string pathfilename = _tabsMgr->GetCurrentTabPanelPath();
727         if (pathfilename.empty()) {
728                 SaveCurrentDiagramAs();
729         } else {
730                 std::string pathfilenamebbs = pathfilename;
731                 pathfilenamebbs[pathfilenamebbs.length() - 1] = 's';
732                 
733                 if (_tabsMgr->isActualDiagramComplexBox() == false) {
734                         std::string name = _tabsMgr->GetNameTabPanel();
735                         if(name[name.length() - 1] == '*')
736                         {
737                                 name =name.substr(0,name.length()-1);
738                         }
739                         _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
740                         SaveActualDiagram(pathfilename);
741                         SaveActualBBS(pathfilenamebbs);
742                 } else {
743                         AskComplexBoxConfiguration();
744                         SaveActualDiagram(pathfilename);
745                         SaveActualComplexBox(pathfilenamebbs);
746                 } //if isActualDiagramComplexBox
747         }
748 }
749 //=========================================================================
750 //DFCH
751 void wxGUIEditorGraphicBBS::OnSaveAsActualDiagram(wxCommandEvent& event) {
752         SaveCurrentDiagramAs();
753 }
754 //=========================================================================
755
756 void wxGUIEditorGraphicBBS::refreshGUIControls() {
757         wxToolBar* toolbar = GetToolBar();
758         if (_tabsMgr->isActualDiagramComplexBox()) {
759                 toolbar->ToggleTool(ID_BTNCOMPLEXBOX, true);
760                 toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, true);
761                 toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT, true);
762
763                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_COMPLEXBOX, true);
764                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_BBS, false);
765         } else {
766                 toolbar->ToggleTool(ID_BTNCOMPLEXBOX, false);
767                 toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, false);
768                 toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT, false);
769
770                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_COMPLEXBOX, false);
771                 GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_BBS, true);
772         }
773
774         GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState());
775         GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState());
776
777         if (_tabsMgr->getNumActualSelectedObjects() > 0) {
778                 GetMenuBar()->GetMenu(1)->Enable(ID_COPY_TO_COMPLEXDIAGRAM, true);
779         } else {
780                 ///TODO Fix the possibility to say from the SceneManager to the GUI to refresh the options
781                 GetMenuBar()->GetMenu(1)->Enable(ID_COPY_TO_COMPLEXDIAGRAM, true);
782         }
783
784 }
785
786 //=========================================================================
787
788 void wxGUIEditorGraphicBBS::OnDeleteAllBoxesActualDiagram(wxCommandEvent& event) {
789         wxMessageDialog *dial = new wxMessageDialog(NULL,
790                         wxT("Are you sure to delete all boxes of the diagram?"),
791                         wxT("Delete all boxes"), wxYES_NO | wxNO_DEFAULT
792                                         | wxICON_QUESTION);
793         if (dial->ShowModal() == wxID_YES) {
794                 _tabsMgr->deleteAllBoxesActualDiagram();
795         }
796 }
797
798 //=========================================================================
799
800 void wxGUIEditorGraphicBBS::OnCenterViewActualDiagram(wxCommandEvent& event) {
801         _tabsMgr->centerViewActualDiagram();
802 }
803
804 //=========================================================================
805
806 void wxGUIEditorGraphicBBS::OnOpenDiagram(wxCommandEvent& event) {
807         wxFileDialog * openFileDialog = new wxFileDialog(this, wxT("Open diagram"),
808                         wxEmptyString, wxT(""), wxT("*.bbg"), wxOPEN | wxFILE_MUST_EXIST);
809         if (openFileDialog->ShowModal() == wxID_OK) {
810                 wxString fileName = openFileDialog->GetPath();
811                 ifstream inputStream;
812                 std::string fName = (const char*) (fileName.mb_str());
813
814                 inputStream.open(fName.c_str());
815                 assert(inputStream.good()); // fails
816                 _tabsMgr->addNewTab(openFileDialog->GetFilename());
817                 _tabsMgr->loadDiagram(inputStream, fName);
818                 _tabsMgr->saveTempActualDiagram();
819                 GetToolBar()->EnableTool(ID_UNDO, false);
820                 GetToolBar()->EnableTool(ID_UNDO, false);
821                 inputStream.close();
822         }
823         refreshGUIControls();
824 }
825
826 //=========================================================================
827
828
829 void wxGUIEditorGraphicBBS::OnClickBtnBox(wxCommandEvent& event) {
830         BlackBoxDescriptor *bbDes = _pkgBrowser->GetActualSelected();
831         if (bbDes != NULL) {
832                 std::string typeName = bbDes->GetTypeName();
833                 std::string packageName = bbDes->GetPackage()->GetName();
834
835                 wxGEditorTabPanel *pnl = this->_tabsMgr->getActualTabPanel();
836                 
837
838                 if (pnl != NULL) {
839                         wxVtkSceneManager *scn = pnl->getSceneManager();
840                         scn->createGBlackBox(50, 50, packageName, typeName);
841                         scn->refresh();
842                 }
843
844                 SaveTempActualDiagram();
845                 // if pnl
846         }// if bbDes
847 }
848
849 //=========================================================================
850
851 void wxGUIEditorGraphicBBS::OnClickBtnExecutableBox(wxCommandEvent& event) {
852         //DFCH
853         wxGEditorTabPanel *pnl = this->_tabsMgr->getActualTabPanel();
854
855         if (pnl != NULL) {
856                 wxVtkSceneManager *scn = pnl->getSceneManager();
857                 if( scn->MakeBoxExecutable() )
858                         std::cout<<"DFCH: wxGUIEditorGraphicBBS::OnClickBtnExecutableBox...successful"<<std::endl;
859                 else
860                         std::cout<<"DFCH: wxGUIEditorGraphicBBS::OnClickBtnExecutableBox...Error!!"<<std::endl;
861         }
862         //DFCH
863
864 }
865
866 //=========================================================================
867
868 void wxGUIEditorGraphicBBS::OnClickBtnComplexBox(wxCommandEvent& event) {
869         wxToolBar* toolbar = GetToolBar();
870         bool temp = toolbar->GetToolState(ID_BTNCOMPLEXBOX);
871         if (temp) {
872                 _tabsMgr->setActualDiagramComplexBox(true);
873         } else {
874                 _tabsMgr->setActualDiagramComplexBox(false);
875         }
876         refreshGUIControls();
877 }
878
879 //=========================================================================
880
881 void wxGUIEditorGraphicBBS::OnAddComplexBoxInput(wxCommandEvent& event) {
882         wxTextEntryDialog *nameDialog = new wxTextEntryDialog(this,
883                         wxT("Name of input port"));
884         if (nameDialog->ShowModal() == wxID_OK) {
885                 wxString fileName = nameDialog->GetValue();
886                 if (!fileName.IsEmpty()) {
887                         std::string portName = (const char*) (fileName.mb_str());
888                         _tabsMgr->addActualDiagramComplexInputPort(portName);
889                 }
890         }
891 }
892
893 //=========================================================================
894
895 void wxGUIEditorGraphicBBS::OnAddComplexBoxOutput(wxCommandEvent& event) {
896         wxTextEntryDialog *nameDialog = new wxTextEntryDialog(this,
897                         wxT("Name of output port"));
898         if (nameDialog->ShowModal() == wxID_OK) {
899                 wxString fileName = nameDialog->GetValue();
900                 if (!fileName.IsEmpty()) {
901                         std::string portName = (const char*) (fileName.mb_str());
902                         _tabsMgr->addActualDiagramComplexOutputPort(portName);
903                 }
904         }
905 }
906
907 //=========================================================================
908
909 void wxGUIEditorGraphicBBS::OnCopySelectedToComplexDiagram(
910                 wxCommandEvent& event) {
911         _tabsMgr->copySelectedBBoxesToComplexDiagram();
912 }
913
914 void wxGUIEditorGraphicBBS::OnHelpBBeditor(wxCommandEvent& event) {
915         printf("wxGUIEditorGraphicBBS::OnHelpBBeditor ....not implemented..\n");
916 }
917
918 //=========================================================================
919 void wxGUIEditorGraphicBBS::OnCreatePackage(wxCommandEvent& event) {
920
921         //bbtkSystemTools::CreatePackage();
922 }
923
924 //=========================================================================
925 void wxGUIEditorGraphicBBS::OnCreateBlackBox(wxCommandEvent& event) {
926         //bbtkSystemTools::CreateBlackBox();
927 }
928 //=========================================================================
929 void wxGUIEditorGraphicBBS::OnPlugPackage(wxCommandEvent& event) {
930         printf("EED wxGUIEditorGraphicBBS::OnPlugPackage ... not implemented ..\n");
931 }
932 //=========================================================================
933 void wxGUIEditorGraphicBBS::OnEditConfig(wxCommandEvent& event) {
934         printf("EED wxGUIEditorGraphicBBS::OnEditConfig ... not implemented ..\n");
935     }
936     //=========================================================================
937
938     void wxGUIEditorGraphicBBS::OnShowHTMLDoc(wxCommandEvent& event) {
939         // JGRR & CM WH
940         if (_helpHtmlBrowser != NULL){
941              _frameAUIMgr->DetachPane(_helpHtmlBrowser);
942             _helpHtmlBrowser->Destroy();   
943         }
944         initHelpHTMLBrowser();
945         _frameAUIMgr->AddPane(_helpHtmlBrowser, wxAuiPaneInfo().Right().MinSize(200, 200));
946         _frameAUIMgr->Update();
947         refreshGUIControls();
948         
949         // EO JGRR    & CM WH       
950 }
951 //=========================================================================
952 void wxGUIEditorGraphicBBS::OnCreateIndex(wxCommandEvent& event) {
953         printf("EED wxGUIEditorGraphicBBS::OnCreateIndex ... not implemented ..\n");
954 }
955
956 //=========================================================================
957
958 void wxGUIEditorGraphicBBS::OnExit(wxCommandEvent& event) {
959         Close(true);
960 }
961
962
963
964 //=========================================================================
965
966 } // EO namespace bbtk
967
968 // EOF
969