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