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