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