]> Creatis software - crea.git/blobdiff - lib/creaDevManagerLib/wxCDMMainFrame.cpp
Merge remote-tracking branch 'refs/remotes/origin/master' into creaDevManagerCMake
[crea.git] / lib / creaDevManagerLib / wxCDMMainFrame.cpp
index 9565b8a37858d75e7e8b0713d573e1c0fabab5c4..761a4f98c2028b5fb6c59d4974e1036c745b3788 100755 (executable)
@@ -38,7 +38,9 @@
 #include "wx/tooltip.h"
 #include "wx/wxhtml.h"
 #include "wx/statline.h"
+#include "wx/config.h"
 #include "CDMUtilities.h"
+#include "images/CIcon64.xpm"
 
 #include "creaDevManagerIds.h"
 #include "wxCDMMainDescriptionPanel.h"
 #include "wxCDMFileDescriptionPanel.h"
 #include "wxCDMPackageManagerPanel.h"
 
+#include "wxCDMSettingsDialog.h"
+
 #include "wxCDMProjectActionsPanel.h"
 #include "wxCDMNewProjectDialog.h"
 
 
 
 BEGIN_EVENT_TABLE(wxCDMMainFrame, wxFrame)
+EVT_MENU_OPEN(wxCDMMainFrame::OnMenuBarOpen)
 EVT_MENU(ID_MENU_NEW_PROJECT, wxCDMMainFrame::OnMenuNewProject)
 EVT_MENU(ID_MENU_OPEN_PROJECT, wxCDMMainFrame::OnMenuOpenProject)
+EVT_MENU(ID_MENU_OPEN_RECENT1, wxCDMMainFrame::OnMenuOpenRecent)
+EVT_MENU(ID_MENU_OPEN_RECENT2, wxCDMMainFrame::OnMenuOpenRecent)
+EVT_MENU(ID_MENU_OPEN_RECENT3, wxCDMMainFrame::OnMenuOpenRecent)
+EVT_MENU(ID_MENU_OPEN_RECENT4, wxCDMMainFrame::OnMenuOpenRecent)
+EVT_MENU(ID_MENU_OPEN_RECENT5, wxCDMMainFrame::OnMenuOpenRecent)
 EVT_MENU(ID_MENU_CLOSE_PROJECT, wxCDMMainFrame::OnMenuCloseProject)
 EVT_MENU(ID_MENU_EXPORT_HIERARCHY, wxCDMMainFrame::OnMenuExportHierarchy)
 EVT_MENU(ID_MENU_EXIT, wxCDMMainFrame::OnMenuExit)
 EVT_MENU(ID_MENU_REFRESH_PROJECT, wxCDMMainFrame::OnMenuRefreshProject)
+EVT_MENU(ID_MENU_SETTINGS, wxCDMMainFrame::OnMenuSettings)
 EVT_MENU(ID_MENU_BBTK_GRAPHICAL_EDITOR, wxCDMMainFrame::OnMenuBBTKGraphicalEditor)
 EVT_MENU(ID_MENU_MINITOOLS, wxCDMMainFrame::OnMenuMiniTools)
 EVT_MENU(ID_MENU_CODE_EDITOR, wxCDMMainFrame::OnMenuCodeEditor)
@@ -110,6 +121,9 @@ wxCDMMainFrame::wxCDMMainFrame(
 
 wxCDMMainFrame::~wxCDMMainFrame()
 {
+  wxConfigBase* pConfig = wxConfigBase::Get();
+  pConfig->Write(wxT("HELP"), this->help);
+
   auiManager.UnInit();
 }
 
@@ -123,10 +137,16 @@ bool wxCDMMainFrame::Create(
 )
 {
   wxFrame::Create(parent, id, caption, pos, size, style);
-  this->model = new modelCDMMain();
   this->help = true;
+
+  wxConfigBase* pConfig = wxConfigBase::Get();
+  this->help = pConfig->Read(wxT("HELP"), this->help) != 0;
+
+  this->model = new modelCDMMain();
+
   CreateMenus();
   CreateControls();
+  SetIcon(wxIcon(CIcon64));
   return TRUE;
 }
 
@@ -152,16 +172,48 @@ void wxCDMMainFrame::RefreshProject()
   this->model->RefreshProject(result);
   std::cout << "rebuilding project tree" << std::endl;
   this->tree_Projects->BuildTree(this->model->GetModelElements(), this->model->GetProject());
+  this->tree_Projects->Unselect();
+  this->actualTreeItem.Unset();
 }
 
 void wxCDMMainFrame::CreateMenus()
 {
   wxMenuBar* menuBar = new wxMenuBar;
 
+  //Recently opened projects
+  menu_Recent = new wxMenu();
+  wxConfigBase* pConfig = wxConfigBase::Get();
+  std::string rp = crea::wx2std(pConfig->Read(wxT("RECENT1"), wxT("")));
+  if(rp != "")
+    {
+      menu_Recent->Append(ID_MENU_OPEN_RECENT1, crea::std2wx(rp));
+      rp = crea::wx2std(pConfig->Read(wxT("RECENT2"), wxT("")));
+      if(rp != "")
+        {
+          menu_Recent->Append(ID_MENU_OPEN_RECENT2, crea::std2wx(rp));
+          rp = crea::wx2std(pConfig->Read(wxT("RECENT3"), wxT("")));
+          if(rp != "")
+            {
+              menu_Recent->Append(ID_MENU_OPEN_RECENT3, crea::std2wx(rp));
+              rp = crea::wx2std(pConfig->Read(wxT("RECENT4"), wxT("")));
+              if(rp != "")
+                {
+                  menu_Recent->Append(ID_MENU_OPEN_RECENT4, crea::std2wx(rp));
+                  rp = crea::wx2std(pConfig->Read(wxT("RECENT5"), wxT("")));
+                  if(rp != "")
+                    {
+                      menu_Recent->Append(ID_MENU_OPEN_RECENT5, crea::std2wx(rp));
+                    }
+                }
+            }
+        }
+    }
+
   //FileMenu
   menu_File = new wxMenu();
   menu_File->Append(ID_MENU_NEW_PROJECT, wxT("&New Project..."));
   menu_File->Append(ID_MENU_OPEN_PROJECT, wxT("&Open Project..."));
+  menu_File->AppendSubMenu(menu_Recent,wxT("Open &Recent"),wxT("Open a recently opened project."));
   menu_File->AppendSeparator();
   menu_File->Append(ID_MENU_CLOSE_PROJECT, wxT("&Close Project"));
   menu_File->AppendSeparator();
@@ -174,6 +226,7 @@ void wxCDMMainFrame::CreateMenus()
   //EditMenu
   menu_Edit = new wxMenu();
   menu_Edit->Append(ID_MENU_REFRESH_PROJECT, wxT("&Refresh Project"));
+  menu_Edit->Append(ID_MENU_SETTINGS, wxT("&Settings"));
 
   menuBar->Append(menu_Edit, wxT("&Edit"));
 
@@ -231,7 +284,7 @@ void wxCDMMainFrame::CreateControls()
   );
 
   auiManager.AddPane(panel_Properties, wxAuiPaneInfo().BestSize(600,400).CenterPane().Name(wxT("panel_Properties")).Caption(wxT("")).CloseButton(false));
-  auiManager.AddPane(tree_Projects, wxAuiPaneInfo().Right().MinSize(300,300).BestSize(300,400).CloseButton(false).Name(wxT("tree_Projects")).Caption(wxT("Project Tree")).CloseButton(false));
+  auiManager.AddPane(tree_Projects, wxAuiPaneInfo().Left().MinSize(250,300).BestSize(250,400).CloseButton(false).Name(wxT("tree_Projects")).Caption(wxT("Project Tree")).CloseButton(false));
   auiManager.Update();
   //auiManager.LoadPerspective(pers,true);
   wxToolTip::Enable(true);
@@ -239,6 +292,43 @@ void wxCDMMainFrame::CreateControls()
 }
 
 //Event Handlers
+
+void wxCDMMainFrame::OnMenuBarOpen(wxMenuEvent& event)
+{
+  //clean recent menu
+  int tam = menu_Recent->GetMenuItemCount();
+  for (int i = 0; i < tam; ++i) {
+    menu_Recent->Delete(menu_Recent->FindItemByPosition(0));
+  }
+  //populate recent menu
+  wxConfigBase* pConfig = wxConfigBase::Get();
+  std::string rp = crea::wx2std(pConfig->Read(wxT("RECENT1"), wxT("")));
+  if(rp != "")
+    {
+      menu_Recent->Append(ID_MENU_OPEN_RECENT1, crea::std2wx(rp));
+      rp = crea::wx2std(pConfig->Read(wxT("RECENT2"), wxT("")));
+      if(rp != "")
+        {
+          menu_Recent->Append(ID_MENU_OPEN_RECENT2, crea::std2wx(rp));
+          rp = crea::wx2std(pConfig->Read(wxT("RECENT3"), wxT("")));
+          if(rp != "")
+            {
+              menu_Recent->Append(ID_MENU_OPEN_RECENT3, crea::std2wx(rp));
+              rp = crea::wx2std(pConfig->Read(wxT("RECENT4"), wxT("")));
+              if(rp != "")
+                {
+                  menu_Recent->Append(ID_MENU_OPEN_RECENT4, crea::std2wx(rp));
+                  rp = crea::wx2std(pConfig->Read(wxT("RECENT5"), wxT("")));
+                  if(rp != "")
+                    {
+                      menu_Recent->Append(ID_MENU_OPEN_RECENT5, crea::std2wx(rp));
+                    }
+                }
+            }
+        }
+    }
+}
+
 //File menu
 void wxCDMMainFrame::OnMenuNewProject(wxCommandEvent& event)
 {
@@ -250,7 +340,7 @@ void wxCDMMainFrame::OnMenuNewProject(wxCommandEvent& event)
 
   if(userResponse == wxID_FORWARD)
     {
-      //create project
+      //close open project
       if(this->model->GetProject() != NULL)
         {
           if(!this->model->CloseProject(result))
@@ -260,9 +350,21 @@ void wxCDMMainFrame::OnMenuNewProject(wxCommandEvent& event)
               event.Skip();
               return;
             }
-        }
 
+          if(this->panel_Properties != NULL)
+            {
+              auiManager.DetachPane(this->panel_Properties);
+              this->panel_Properties->Hide();
+            }
+          if(this->panel_ProjectActions != NULL)
+            {
+              auiManager.DetachPane(this->panel_ProjectActions);
+              this->panel_ProjectActions->Hide();    
+            }
+
+        }
 
+      //create project
       if(!this->model->CreateProject(
           crea::wx2std(dialog->GetProjectName()),
           crea::wx2std(dialog->GetProjectLocation()),
@@ -277,6 +379,18 @@ void wxCDMMainFrame::OnMenuNewProject(wxCommandEvent& event)
           return;
         }
       
+      //update recently open projects
+      wxConfigBase* pConfig = wxConfigBase::Get();
+      if(pConfig->Read(wxT("RECENT1"),wxT("")) != crea::std2wx(this->model->GetProject()->GetPath()))
+        {
+          pConfig->Write(wxT("RECENT5"), pConfig->Read(wxT("RECENT4"),wxT("")));
+          pConfig->Write(wxT("RECENT4"), pConfig->Read(wxT("RECENT3"),wxT("")));
+          pConfig->Write(wxT("RECENT3"), pConfig->Read(wxT("RECENT2"),wxT("")));
+          pConfig->Write(wxT("RECENT2"), pConfig->Read(wxT("RECENT1"),wxT("")));
+          pConfig->Write(wxT("RECENT1"), crea::std2wx(this->model->GetProject()->GetPath()));
+        }
+
+
       //show project actions panel
       if(this->panel_ProjectActions != NULL)
         {
@@ -299,12 +413,110 @@ void wxCDMMainFrame::OnMenuNewProject(wxCommandEvent& event)
       auiManager.Update();
 
       //populate tree control
+         tree_Projects->BuildTree(this->model->GetModelElements(),this->model->GetProject());
       tree_Projects->Unselect();
          this->actualTreeItem.Unset();
-         tree_Projects->BuildTree(this->model->GetModelElements(),this->model->GetProject());
-         tree_Projects->SelectItem(this->model->GetProject()->GetId().GetWxId());
-         wxMessageBox(wxT("ProjectSelected") ,_T("New Project - Success!"),wxOK | wxICON_ERROR);
+         tree_Projects->SelectItem(this->model->GetProject()->GetId().GetWxId(), true);
+         //wxMessageBox(wxT("ProjectSelected") ,_T("New Project - Success!"),wxOK | wxICON_ERROR);
+    }
+}
+void wxCDMMainFrame::OnMenuOpenRecent(wxCommandEvent& event)
+{
+  std::string* result;
+
+  //((wxMenuItem*)(event.GetEventObject()))->GetItemLabel();
+
+  std::string path = "";
+  wxConfigBase* pConfig = wxConfigBase::Get();
+  if(event.GetId() == ID_MENU_OPEN_RECENT1)
+    path = crea::wx2std (pConfig->Read(wxT("RECENT1"),wxT("")));
+  else if(event.GetId() == ID_MENU_OPEN_RECENT2)
+    path = crea::wx2std (pConfig->Read(wxT("RECENT2"),wxT("")));
+  else if(event.GetId() == ID_MENU_OPEN_RECENT3)
+    path = crea::wx2std (pConfig->Read(wxT("RECENT3"),wxT("")));
+  else if(event.GetId() == ID_MENU_OPEN_RECENT4)
+    path = crea::wx2std (pConfig->Read(wxT("RECENT4"),wxT("")));
+  else if(event.GetId() == ID_MENU_OPEN_RECENT5)
+    path = crea::wx2std (pConfig->Read(wxT("RECENT5"),wxT("")));
+
+  std::cout << "Selection to open: " << path << std::endl;
+  std::cout.flush();
+
+
+  //populate model
+  if(this->model->GetProject() != NULL)
+    {
+      std::cout << "Project not null, closing it" << std::endl;
+      if(!this->model->CloseProject(result))
+        {
+          std::cout << "error closing project: " << *result << std::endl;
+          wxMessageBox(crea::std2wx(result->c_str()),_T("New Project - Error!"),wxOK | wxICON_ERROR);
+          event.Skip();
+          return;
+        }
+
+      if(this->panel_Properties != NULL)
+        {
+          auiManager.DetachPane(this->panel_Properties);
+          this->panel_Properties->Hide();
+        }
+      if(this->panel_ProjectActions != NULL)
+        {
+          auiManager.DetachPane(this->panel_ProjectActions);
+          this->panel_ProjectActions->Hide();
+        }
+    }
+
+  if (!this->model->OpenProject(path, result))
+    {
+      std::cout << "error opening project: " << *result << std::endl;
+      wxMessageBox( crea::std2wx(result->c_str()), wxT("Open Project - Error"), wxICON_ERROR);
+      event.Skip();
+      return;
+    };
+
+  //update recently open projects
+  if(pConfig->Read(wxT("RECENT1"),wxT("")) != crea::std2wx(this->model->GetProject()->GetPath()))
+    {
+      pConfig->Write(wxT("RECENT5"), pConfig->Read(wxT("RECENT4"),wxT("")));
+      pConfig->Write(wxT("RECENT4"), pConfig->Read(wxT("RECENT3"),wxT("")));
+      pConfig->Write(wxT("RECENT3"), pConfig->Read(wxT("RECENT2"),wxT("")));
+      pConfig->Write(wxT("RECENT2"), pConfig->Read(wxT("RECENT1"),wxT("")));
+      pConfig->Write(wxT("RECENT1"), crea::std2wx(this->model->GetProject()->GetPath()));
+    }
+
+  std::cout << "building ui" << std::endl;
+
+  //populate tree control
+  tree_Projects->BuildTree(this->model->GetModelElements(), this->model->GetProject());
+  tree_Projects->Unselect();
+  this->actualTreeItem.Unset();
+      tree_Projects->SelectItem(this->model->GetProject()->GetId().GetWxId(), true);
+
+
+  //change project's actions panel
+  if(this->panel_ProjectActions!= NULL)
+    {
+      auiManager.DetachPane(this->panel_ProjectActions);
+      this->panel_ProjectActions->Destroy();
+      this->panel_ProjectActions = NULL;
     }
+  panel_ProjectActions = new wxCDMProjectActionsPanel(
+      this,
+      this->model->GetProject(),
+      ID_WINDOW_PROJ_ACTIONS,
+      wxT("Project Actions Panel"),
+      wxDefaultPosition,
+      wxSize(800,200),
+      0
+  );
+  panel_ProjectActions->SetMinSize(wxSize(500, 100));
+
+
+  auiManager.AddPane(panel_ProjectActions, wxAuiPaneInfo().Bottom().MinSize(800,50).Name(wxT("panel_ProjectActions")).Caption(wxT("General Project Actions")).BestSize(800,70).CloseButton(false));
+
+  auiManager.Update();
+
 }
 void wxCDMMainFrame::OnMenuOpenProject(wxCommandEvent& event)
 {
@@ -333,6 +545,17 @@ void wxCDMMainFrame::OnMenuOpenProject(wxCommandEvent& event)
               event.Skip();
               return;
             }
+
+          if(this->panel_Properties != NULL)
+            {
+              auiManager.DetachPane(this->panel_Properties);
+              this->panel_Properties->Hide();
+            }
+          if(this->panel_ProjectActions != NULL)
+            {
+              auiManager.DetachPane(this->panel_ProjectActions);
+              this->panel_ProjectActions->Hide();    
+            }
         }
 
       if (!this->model->OpenProject(path, result))
@@ -343,12 +566,23 @@ void wxCDMMainFrame::OnMenuOpenProject(wxCommandEvent& event)
           return;
         };
 
+      //update recently open projects
+      wxConfigBase* pConfig = wxConfigBase::Get();
+      if(pConfig->Read(wxT("RECENT1"),wxT("")) != crea::std2wx(this->model->GetProject()->GetPath()))
+        {
+          pConfig->Write(wxT("RECENT5"), pConfig->Read(wxT("RECENT4"),wxT("")));
+          pConfig->Write(wxT("RECENT4"), pConfig->Read(wxT("RECENT3"),wxT("")));
+          pConfig->Write(wxT("RECENT3"), pConfig->Read(wxT("RECENT2"),wxT("")));
+          pConfig->Write(wxT("RECENT2"), pConfig->Read(wxT("RECENT1"),wxT("")));
+          pConfig->Write(wxT("RECENT1"), crea::std2wx(this->model->GetProject()->GetPath()));
+        }
+
       std::cout << "building ui" << std::endl;
 
       //populate tree control
-         tree_Projects->Unselect();
-         this->actualTreeItem.Unset();
       tree_Projects->BuildTree(this->model->GetModelElements(), this->model->GetProject());
+      tree_Projects->Unselect();
+      this->actualTreeItem.Unset();
          tree_Projects->SelectItem(this->model->GetProject()->GetId().GetWxId(), true);
 
 
@@ -467,11 +701,25 @@ void wxCDMMainFrame::OnMenuRefreshProject(wxCommandEvent& event)
       this->auiManager.Update();
 
       this->tree_Projects->Unselect();
+         this->actualTreeItem.Unset();
       this->tree_Projects->SelectItem(this->model->GetProject()->GetId().GetWxId(), true);
     }
   event.Skip();
 }
 
+void wxCDMMainFrame::OnMenuSettings(wxCommandEvent& event)
+{
+  wxCDMSettingsDialog* settingsDialog = new wxCDMSettingsDialog(this, -1);
+  settingsDialog->SetHelpEnabled(this->help);
+
+  int res = settingsDialog->ShowModal();
+  if(res == wxID_OK)
+    {
+      this->help = settingsDialog->IsHelpEnabled();
+      this->menu_Help->Check(ID_MENU_TOGGLE_HELP, this->help);
+    }
+}
+
 void wxCDMMainFrame::OnMenuBBTKGraphicalEditor(wxCommandEvent& event)
 {
   std::cerr << "Event OnMenuBBTKGraphicalEditor not implemented" << std::endl;
@@ -587,11 +835,12 @@ void wxCDMMainFrame::OnTreeSelectionChanged(wxTreeEvent& event)
 
   //get selected element
   wxTreeItemId elementId = event.GetItem();
+  //std::cout << "Tree Selection id: " << elementId.m_pItem << this->actualTreeItem.m_pItem << std::endl;
   //elementId.IsOk() && this->tree_Projects->IsSelected(elementId)
   if(elementId.IsOk() && this->actualTreeItem != elementId)
     {
       
-      std::cout << "Tree Selection id: " << elementId.m_pItem << std::endl;
+      //std::cout << "Valid tree selection id: " << elementId.m_pItem << std::endl;
       //get element from model
       modelCDMIProjectTreeNode* element = this->model->GetModelElements()[elementId];
 
@@ -843,7 +1092,7 @@ void wxCDMMainFrame::OnChangeView(wxCommandEvent& event)
         //this->tree_Projects->Expand(this->model->GetProject()->GetId());
         //this->tree_Projects->Unselect();
         this->actualTreeItem.Unset();
-                 description = new wxCDMPackageManagerPanel(
+       description = new wxCDMPackageManagerPanel(
             this,
             this->model->GetProject(),
             ID_WINDOW_PROPERTIES,
@@ -868,7 +1117,8 @@ void wxCDMMainFrame::OnChangeView(wxCommandEvent& event)
     else if(event.GetString() == wxT("blackbox"))
       {
         modelCDMBlackBox* bb = (modelCDMBlackBox*)event.GetClientData();
-               this->actualTreeItem.Unset();
+        this->actualTreeItem.Unset();
+        this->tree_Projects->SelectItem(bb->GetHeaderFile()->GetId().GetWxId());
         description = new wxCDMBlackBoxDescriptionPanel(
             this,
             bb,
@@ -878,6 +1128,7 @@ void wxCDMMainFrame::OnChangeView(wxCommandEvent& event)
             wxSize(600, 400),
             0
         );
+
                
       }
 
@@ -895,7 +1146,8 @@ void wxCDMMainFrame::OnChangeView(wxCommandEvent& event)
     auiManager.Update();
     break;
   default:
-         event.Skip();
+    event.Skip();
+    break;
   }
   
 }