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