]> Creatis software - bbtkGEditor.git/blobdiff - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/bbtkwxGUIEditorGraphicBBS.cxx
Bug #1658
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsWxGUIEditorGraphic / bbtkwxGUIEditorGraphicBBS.cxx
index e76f8bcecb98b8bf41302e20bafa3bc4df48d14c..3695c51df9740e03f766335372bfa999459ef199 100644 (file)
@@ -36,6 +36,9 @@
 #include "bbtkwxGUIEditorGraphicBBS.h"
 #include "creaWx.h"
 #include "creaSystem.h"
+//must go #include "bbtkUtilities.h"
+//must go #include "bbtkConfigurationFile.h"
+#include <wx/defs.h>
 
 #include <InterpreterBBS.h>
 
@@ -68,8 +71,8 @@ wxGUIEditorGraphicBBS::wxGUIEditorGraphicBBS(wxFrame *parent) :
        initToolbar();
        initTabPanelsManager();
        initPackageBrowser();
-       initHelpHTMLBrowser();
-
+        // JGGR & CM  initHelpHTMLBrowser();
+        _helpHtmlBrowser = NULL;
        CreateStatusBar();
        _frameAUIMgr->Update();
        _actualPkgBrowserBoxName = "";
@@ -79,6 +82,7 @@ wxGUIEditorGraphicBBS::wxGUIEditorGraphicBBS(wxFrame *parent) :
 
 //=========================================================================
 wxGUIEditorGraphicBBS::~wxGUIEditorGraphicBBS() {
+
        printf("EED %p ~wxGUIEditorGraphicBBS()\n", this);
 
        //EED02JUIN2010         delete _tabsMgr;
@@ -104,7 +108,8 @@ void wxGUIEditorGraphicBBS::initToolbar() {
        wxBitmap bmp_complexbox(complexbox_xpm);
        wxBitmap bmp_complexinputport(complexinputport_xpm);
        wxBitmap bmp_complexoutputport(complexoutputport_xpm);
-
+       wxBitmap bmp_undo(undo_xpm);
+       wxBitmap bmp_redo(redo_xpm);
        wxToolBar *toolbar = new wxToolBar(this, wxID_ANY);
 
        //Adds a tool btn to the toolbar
@@ -141,12 +146,19 @@ void wxGUIEditorGraphicBBS::initToolbar() {
                        _T("Add output Complex box"), _T("Add output Complex box"));
        toolbar->EnableTool(ID_ADDCOMPLEXBOXINPUT, false);
        toolbar->EnableTool(ID_ADDCOMPLEXBOXOUTPUT, false);
-
+       toolbar->AddSeparator();
+       toolbar->AddTool(ID_UNDO, _T("Undo"),bmp_undo, wxNullBitmap, wxITEM_NORMAL,     _T("Undo"), _T("Undo"));
+       toolbar->AddTool(ID_REDO, _T("Redo"),bmp_redo, wxNullBitmap, wxITEM_NORMAL,     _T("Redo"), _T("Redo"));
+       
+       toolbar->EnableTool(ID_UNDO, false);
+       toolbar->EnableTool(ID_REDO, false);
        toolbar->SetMargins(2, 2);
        toolbar->Realize();
        SetToolBar(toolbar);
 
+       
        // connect command event handlers
+
        Connect(ID_NEW, wxEVT_COMMAND_TOOL_CLICKED,
                        wxCommandEventHandler(wxGUIEditorGraphicBBS::OnCreateNewTab));
        Connect(ID_OPEN, wxEVT_COMMAND_TOOL_CLICKED,
@@ -179,15 +191,17 @@ void wxGUIEditorGraphicBBS::initToolbar() {
                        wxCommandEventHandler(wxGUIEditorGraphicBBS::OnAddComplexBoxInput));
        Connect(ID_ADDCOMPLEXBOXOUTPUT, wxEVT_COMMAND_TOOL_CLICKED,
                        wxCommandEventHandler(wxGUIEditorGraphicBBS::OnAddComplexBoxOutput));
-
+       Connect(ID_UNDO,wxEVT_COMMAND_TOOL_CLICKED,     wxCommandEventHandler(wxGUIEditorGraphicBBS::OnUndo));
+       Connect(ID_REDO,wxEVT_COMMAND_TOOL_CLICKED,     wxCommandEventHandler(wxGUIEditorGraphicBBS::OnRedo));
+       Connect(ID_CHANGENAME, wxEVT_COMMAND_RIGHT_CLICK, wxCommandEventHandler(wxGUIEditorGraphicBBS::OnChangeName));
 }
 //=========================================================================
 void wxGUIEditorGraphicBBS::initHelpHTMLBrowser() {
-       //TO FIX THE PATH OF BBTK BIN  IN ORDER TO USE REGENERATE EXECUTABLES
-       //RegenerateAll();
+       // JGRR & CM 
        _helpHtmlBrowser = new WxGUIHtmlBrowser(this, wxSize(200, 0));
-       _frameAUIMgr->AddPane(_helpHtmlBrowser, wxAuiPaneInfo().Right().MinSize(
-                       200, 200));
+   
+       // EO JGRR & CM 
+       
 }
 
 //=========================================================================
@@ -199,7 +213,9 @@ void wxGUIEditorGraphicBBS::initMenu() {
        fileMenu->Append(ID_SAVE_AS_DIAGRAM, _T("Save Diagram As"),
                        _T("Save Diagram As")); //DFCH
        fileMenu->Append(ID_OPEN_BBS, _T("Open BBS"), _T("Open BBS"));
-       fileMenu->Append(ID_SAVE_AS_BBS, _T("Save BBS"), _T("Save BBS"));
+        // CM
+       fileMenu->Append(ID_OPEN, _T("Open a diagram"), _T("Open a diagram"));
+        fileMenu->Append(ID_SAVE_AS_BBS, _T("Save BBS"), _T("Save BBS"));
        fileMenu->Append(ID_SAVE_AS_COMPLEXBOX, _T("Save complex box"),
                        _T("Save complex box"));
        fileMenu->Append(wxID_EXIT, _T("E&xit"), _T("Exit"));
@@ -276,27 +292,28 @@ void wxGUIEditorGraphicBBS::initMenu() {
 }
 
 //=========================================================================
-void wxGUIEditorGraphicBBS::initTabPanelsManager() {
+void wxGUIEditorGraphicBBS::initTabPanelsManager() 
+{
        _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);
        _tabsMgr = new wxTabPanelsManager(this);
        _frameAUIMgr->AddPane(_notebook, wxAuiPaneInfo().CenterPane());
-
 }
 
 //=========================================================================
-void wxGUIEditorGraphicBBS::initPackageBrowser() {
+void wxGUIEditorGraphicBBS::initPackageBrowser() 
+{
        _pkgBrowser = new WxGUIPackageBrowser2(this);
        _pkgBrowser->IncludeAll();
-       _frameAUIMgr->AddPane(_pkgBrowser,
-                       wxAuiPaneInfo().Left().MinSize(200, 200).CloseButton(false));
+       _frameAUIMgr->AddPane(_pkgBrowser, wxAuiPaneInfo().Left().MinSize(30, 500).BestSize(750,500).CloseButton(false));
 }
 
 //================================================================
 
-void wxGUIEditorGraphicBBS::DoRegeneratePackageDoc(const std::string& pack) {
+void wxGUIEditorGraphicBBS::DoRegeneratePackageDoc(const std::string& pack) 
+{
        std::string mess("Regenerating doc for package '");
        if (pack != "-a") {
                mess += pack + "'";
@@ -411,6 +428,13 @@ void wxGUIEditorGraphicBBS::editBlackBox(GBlackBoxModel *bbmodel) {
 
 //=========================================================================
 
+bool wxGUIEditorGraphicBBS::boxNameExists(std::string boxname) {
+       return this->_tabsMgr->getActualTabPanel()->getSceneManager()->boxExist(boxname);
+}
+
+//=========================================================================
+
 void wxGUIEditorGraphicBBS::updateStatusBar(std::string textStatus) {
        SetStatusText(crea::std2wx(textStatus));
 }
@@ -450,6 +474,17 @@ void wxGUIEditorGraphicBBS::executeScript(std::string script) {
        system(command.c_str());
 }
 
+//=========================================================================
+std::string wxGUIEditorGraphicBBS::CheckExtension(std::string filename, std::string extension) {
+        if(filename.size() > extension.size() ){
+                if( (filename.compare(filename.size()-extension.size(), extension.size(),extension)) !=0 ){
+                        filename.append(extension);
+                }
+        }else{
+                filename.append(extension);
+        }
+       return filename;
+}
 //=========================================================================
 // EVENT HANDLERS
 //=========================================================================
@@ -459,6 +494,49 @@ void wxGUIEditorGraphicBBS::OnCreateNewTab(wxCommandEvent& event) {
        refreshGUIControls();
 }
 
+void wxGUIEditorGraphicBBS::OnUndo(wxCommandEvent& event) {
+       unsigned short disable;
+       std::string stundo;
+       std::string stredo;
+
+       _tabsMgr->loadTempDiagram(0);
+       disable = _tabsMgr->getUndoState(stundo);
+       if(disable == 0)
+       {
+               std::string name = _tabsMgr->GetNameTabPanel();
+               if(name[name.length() - 1] == '*')
+               {
+                       name =name.substr(0,name.length()-1);
+               }
+               _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
+       }
+
+       GetToolBar()->EnableTool(ID_UNDO,disable);
+       GetToolBar()->SetToolShortHelp(ID_UNDO,crea::std2wx(stundo));
+       GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState(stredo));
+       GetToolBar()->SetToolShortHelp(ID_REDO,crea::std2wx(stredo));
+       refreshGUIControls();
+}
+
+void wxGUIEditorGraphicBBS::OnRedo(wxCommandEvent& event) {
+       unsigned short disable;
+       std::string stundo;
+       std::string stredo;
+
+       _tabsMgr->loadTempDiagram(1);
+       std::string name = _tabsMgr->GetNameTabPanel();
+       if(name[name.length() - 1] != '*')
+               name +="*";
+       _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
+       GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState(stundo));
+       GetToolBar()->SetToolShortHelp(ID_UNDO,crea::std2wx(stundo));
+       GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState(stredo));
+       GetToolBar()->SetToolShortHelp(ID_REDO,crea::std2wx(stredo));
+       refreshGUIControls();
+}
+
+void wxGUIEditorGraphicBBS::OnChangeName(wxCommandEvent& event){
+}
 //=========================================================================
 
 void wxGUIEditorGraphicBBS::OnExecuteActualDiagram(wxCommandEvent& event) {
@@ -502,7 +580,7 @@ void wxGUIEditorGraphicBBS::SaveActualBBS(std::string filename) {
        std::string content = "";
        // writing file header
        content += "# ----------------------------------\n";
-       content += "# - BBTKGEditor v 1.2 BBS BlackBox Script\n";
+       content += "# - BBTKGEditor v 1.3 BBS BlackBox Script\n";
        content += "# - ";
        content += filename;
        content += "\n";
@@ -519,7 +597,9 @@ void wxGUIEditorGraphicBBS::OnSaveActualBBS(wxCommandEvent& event) {
                        wxT("*.bbs"), wxSAVE | wxOVERWRITE_PROMPT);
        if (saveFileDialog->ShowModal() == wxID_OK) {
                wxString fileName = saveFileDialog->GetPath();
-               SaveActualBBS((const char*) (fileName.mb_str()));
+               std::string filename = (const char*) (fileName.mb_str());
+               filename=CheckExtension (filename, ".bbs");
+               SaveActualBBS(filename);
        }
 
 }
@@ -560,7 +640,7 @@ void wxGUIEditorGraphicBBS::SaveActualComplexBox(std::string filename) {
 
        // writing file header
        content += "# ----------------------------------\n";
-       content += "# - BBTKGEditor v 1.2 BBS BlackBox Script (Complex Box)\n";
+       content += "# - BBTKGEditor v 1.3 BBS BlackBox Script (Complex Box)\n";
        content += "# - ";
        content += filename;
        content += "\n";
@@ -580,7 +660,9 @@ void wxGUIEditorGraphicBBS::OnSaveActualComplexBox(wxCommandEvent& event) {
                                        | wxOVERWRITE_PROMPT);
        if (saveFileDialog->ShowModal() == wxID_OK) {
                wxString fileName = saveFileDialog->GetPath();
-               SaveActualComplexBox((const char*) (fileName.mb_str()));
+               std::string filename = (const char*) (fileName.mb_str());
+               filename=CheckExtension (filename, ".bbs");
+               SaveActualComplexBox(filename);
        }
 }
 
@@ -593,7 +675,7 @@ void wxGUIEditorGraphicBBS::SaveActualDiagram(std::string filename) {
 
        // writing file header
        content += "# ----------------------------------\n";
-       content += "# - BBTKGEditor v 1.2 BBG BlackBox Diagram file\n";
+       content += "# - BBTKGEditor v 1.3 BBG BlackBox Diagram file\n";
        content += "# - ";
 
        content += filename;
@@ -608,6 +690,22 @@ void wxGUIEditorGraphicBBS::SaveActualDiagram(std::string filename) {
        file.close();
 }
 
+//=========================================================================
+void wxGUIEditorGraphicBBS::SaveTempActualDiagram(const std::string &action) 
+{
+       std::string stundo;
+       std::string stredo;
+       std::string name = _tabsMgr->GetNameTabPanel();
+       if(name[name.length() - 1] != '*')
+               name +="*";
+       _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
+       _tabsMgr->saveTempActualDiagram(action);
+       GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState(stundo));
+       GetToolBar()->SetToolShortHelp(ID_UNDO,crea::std2wx(stundo));
+       GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState(stredo));
+       GetToolBar()->SetToolShortHelp(ID_REDO,crea::std2wx(stredo));
+}
+
 //=========================================================================
 
 //The following method allows to save the current diagram asking the filename
@@ -617,9 +715,17 @@ void wxGUIEditorGraphicBBS::SaveCurrentDiagramAs() {
                        wxT("Save current diagram"), wxEmptyString, wxT("NewDiagram"),
                        wxT("*.bbg"), wxSAVE | wxOVERWRITE_PROMPT);
        if (saveFileDialog->ShowModal() == wxID_OK) {
-               _tabsMgr->SetNameTabPanel(saveFileDialog->GetFilename());
+               //_tabsMgr->SetNameTabPanel(saveFileDialog->GetFilename());
                wxString pathfileName = saveFileDialog->GetPath();
                std::string pathfilename = (const char*) (pathfileName.mb_str());
+               pathfilename=CheckExtension (pathfilename, ".bbg");
+
+               //modify the tab name accordingly
+               wxString fileName = saveFileDialog->GetFilename();
+               std::string filename = (const char*) (fileName.mb_str());
+               filename=CheckExtension (filename, ".bbg");
+               _tabsMgr->SetNameTabPanel( wxString (filename.c_str(), wxConvUTF8) );
+
                std::string pathfilenamebbs = pathfilename;
                pathfilenamebbs[pathfilenamebbs.length() - 1] = 's';
                if (_tabsMgr->isActualDiagramComplexBox() == false) {
@@ -641,8 +747,14 @@ void wxGUIEditorGraphicBBS::OnSaveActualDiagram(wxCommandEvent& event) {
        } else {
                std::string pathfilenamebbs = pathfilename;
                pathfilenamebbs[pathfilenamebbs.length() - 1] = 's';
-
+               
                if (_tabsMgr->isActualDiagramComplexBox() == false) {
+                       std::string name = _tabsMgr->GetNameTabPanel();
+                       if(name[name.length() - 1] == '*')
+                       {
+                               name =name.substr(0,name.length()-1);
+                       }
+                       _tabsMgr->SetNameTabPanel(wxString (name.c_str(), wxConvUTF8));
                        SaveActualDiagram(pathfilename);
                        SaveActualBBS(pathfilenamebbs);
                } else {
@@ -660,6 +772,8 @@ void wxGUIEditorGraphicBBS::OnSaveAsActualDiagram(wxCommandEvent& event) {
 //=========================================================================
 
 void wxGUIEditorGraphicBBS::refreshGUIControls() {
+       std::string stundo;
+       std::string stredo;
        wxToolBar* toolbar = GetToolBar();
        if (_tabsMgr->isActualDiagramComplexBox()) {
                toolbar->ToggleTool(ID_BTNCOMPLEXBOX, true);
@@ -677,6 +791,11 @@ void wxGUIEditorGraphicBBS::refreshGUIControls() {
                GetMenuBar()->GetMenu(0)->Enable(ID_SAVE_AS_BBS, true);
        }
 
+       GetToolBar()->EnableTool(ID_UNDO,_tabsMgr->getUndoState(stundo));
+       GetToolBar()->SetToolShortHelp(ID_UNDO,crea::std2wx(stundo));
+       GetToolBar()->EnableTool(ID_REDO,_tabsMgr->getRedoState(stredo));
+       GetToolBar()->SetToolShortHelp(ID_REDO,crea::std2wx(stredo));
+
        if (_tabsMgr->getNumActualSelectedObjects() > 0) {
                GetMenuBar()->GetMenu(1)->Enable(ID_COPY_TO_COMPLEXDIAGRAM, true);
        } else {
@@ -709,15 +828,19 @@ void wxGUIEditorGraphicBBS::OnCenterViewActualDiagram(wxCommandEvent& event) {
 void wxGUIEditorGraphicBBS::OnOpenDiagram(wxCommandEvent& event) {
        wxFileDialog * openFileDialog = new wxFileDialog(this, wxT("Open diagram"),
                        wxEmptyString, wxT(""), wxT("*.bbg"), wxOPEN | wxFILE_MUST_EXIST);
+       printf("SCP: wxGUIEditorGraphicBBS::OnOpenDiagram  \n");
        if (openFileDialog->ShowModal() == wxID_OK) {
                wxString fileName = openFileDialog->GetPath();
                ifstream inputStream;
                std::string fName = (const char*) (fileName.mb_str());
+
                inputStream.open(fName.c_str());
+               assert(inputStream.good()); // fails
                _tabsMgr->addNewTab(openFileDialog->GetFilename());
-
                _tabsMgr->loadDiagram(inputStream, fName);
-
+               _tabsMgr->saveTempActualDiagram("load diagram");
+               GetToolBar()->EnableTool(ID_UNDO, false);
+               GetToolBar()->EnableTool(ID_UNDO, false);
                inputStream.close();
        }
        refreshGUIControls();
@@ -733,12 +856,16 @@ void wxGUIEditorGraphicBBS::OnClickBtnBox(wxCommandEvent& event) {
                std::string packageName = bbDes->GetPackage()->GetName();
 
                wxGEditorTabPanel *pnl = this->_tabsMgr->getActualTabPanel();
+               
 
                if (pnl != NULL) {
                        wxVtkSceneManager *scn = pnl->getSceneManager();
                        scn->createGBlackBox(50, 50, packageName, typeName);
                        scn->refresh();
-               } // if pnl
+               }
+
+               SaveTempActualDiagram("New Box");
+               // if pnl
        }// if bbDes
 }
 
@@ -824,14 +951,71 @@ void wxGUIEditorGraphicBBS::OnCreateBlackBox(wxCommandEvent& event) {
 //=========================================================================
 void wxGUIEditorGraphicBBS::OnPlugPackage(wxCommandEvent& event) {
        printf("EED wxGUIEditorGraphicBBS::OnPlugPackage ... not implemented ..\n");
+/* this is the right code but it's left to include the corresponding commented libraries
+       long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
+       wxDirDialog* FD =
+         new wxDirDialog( 0,
+                          _T("Select package directory"),
+                          _T(""),
+                          style);
+       long userResponse;
+       do
+       {
+               userResponse = FD->ShowModal();
+               if(userResponse==wxID_OK)
+               {
+                       std::string path = wx2std (FD->GetPath()) ;
+                       std::string fname = path + "/bbtkPackage";
+                       std::cout << "Path chosen = \"" << FD->GetPath() << "\"" << std::endl;
+                       if ( Utilities::FileExists( fname ) )
+                       {
+                               std::ifstream f;
+                               f.open(fname.c_str());
+                               std::string pname;
+                               f >> pname;
+                               f.close();
+
+                               bbtk::ConfigurationFile::GetInstance().AddPackagePathsAndWrite( path );
+
+                               DoRegeneratePackageDoc(pname);
+                               DoRegenerateBoxesLists();
+                               wxMessageBox(_T("Package successfully plugged"),_T("Plug package"),wxOK | wxICON_INFORMATION);
+                               userResponse = wxID_CANCEL;
+                       }
+                       else
+                       {
+                               std::string err = "The directory \"" + path + "\" does not contain a 'bbtkPackage' file.";
+                               #ifndef WIN32
+                                       #ifndef MACOSX
+                                               err += "\nNote: If you are on Linux please select the folder and click \"Open\", don't enter in it.";
+                                       #endif
+                               #endif
+
+
+                               wxMessageBox(_T(err),_T("Plug package"),wxOK | wxICON_ERROR);
+                       }
+               }
+       }while(userResponse != wxID_CANCEL);
+*/
 }
 //=========================================================================
 void wxGUIEditorGraphicBBS::OnEditConfig(wxCommandEvent& event) {
        printf("EED wxGUIEditorGraphicBBS::OnEditConfig ... not implemented ..\n");
-}
-//=========================================================================
-void wxGUIEditorGraphicBBS::OnShowHTMLDoc(wxCommandEvent& event) {
-       printf("EED wxGUIEditorGraphicBBS::OnShowHTMLDoc ... not implemented ..\n");
+    }
+    //=========================================================================
+
+    void wxGUIEditorGraphicBBS::OnShowHTMLDoc(wxCommandEvent& event) {
+        // JGRR & CM WH
+        if (_helpHtmlBrowser != NULL){
+             _frameAUIMgr->DetachPane(_helpHtmlBrowser);
+            _helpHtmlBrowser->Destroy();   
+        }
+        initHelpHTMLBrowser();
+        _frameAUIMgr->AddPane(_helpHtmlBrowser, wxAuiPaneInfo().Right().MinSize(200, 200));
+        _frameAUIMgr->Update();
+        refreshGUIControls();
+        
+        // EO JGRR    & CM WH       
 }
 //=========================================================================
 void wxGUIEditorGraphicBBS::OnCreateIndex(wxCommandEvent& event) {
@@ -844,6 +1028,8 @@ void wxGUIEditorGraphicBBS::OnExit(wxCommandEvent& event) {
        Close(true);
 }
 
+
+
 //=========================================================================
 
 } // EO namespace bbtk