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