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