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