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