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