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