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