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