ide: add recent files to workspace for the recent files menu when a workspace is...
authorRejean Loyer <redj@ecere.com>
Tue, 15 Mar 2016 19:52:26 +0000 (15:52 -0400)
committerJerome St-Louis <jerome@ecere.com>
Thu, 28 Jul 2016 22:23:34 +0000 (18:23 -0400)
ide/src/IDESettings.ec
ide/src/designer/CodeEditor.ec
ide/src/dialogs/GlobalSettingsDialog.ec
ide/src/ide.ec
ide/src/project/Project.ec
ide/src/project/Workspace.ec

index c7dbf1b..1fedfa4 100644 (file)
@@ -354,8 +354,8 @@ private:
                for(c : oldSettings.compilerConfigs)
                   data.compilerConfigs.Add(c.Copy());
 
-               for(s : oldSettings.recentFiles) data.recentFiles.Add(CopyString(s));
-               for(s : oldSettings.recentProjects) data.recentProjects.Add(CopyString(s));
+               for(s : oldSettings.recentFiles) data.recentFiles.Add(s);
+               for(s : oldSettings.recentProjects) data.recentProjects.Add(s);
 
                data.docDir = oldSettings.docDir;
                data.ideFileDialogLocation = oldSettings.ideFileDialogLocation;
@@ -452,8 +452,8 @@ class IDESettings : GlobalSettingsData
 {
 public:
    List<CompilerConfig> compilerConfigs { };
-   Array<String> recentFiles { };
-   Array<String> recentProjects { };
+   RecentPaths recentFiles { };
+   RecentPaths recentProjects { };
    property const char * docDir
    {
       set { delete docDir; if(value && value[0]) docDir = CopyString(value); }
@@ -551,10 +551,8 @@ private:
    {
       compilerConfigs.Free();
       delete compilerConfigs;
-      recentFiles.Free();
-      delete recentFiles;
-      recentProjects.Free();
-      delete recentProjects;
+      if(recentProjects) { recentFiles.Free(); delete recentFiles; }
+      if(recentProjects) { recentProjects.Free(); delete recentProjects; }
       delete docDir;
 
       delete projectDefaultTargetDir;
@@ -606,24 +604,8 @@ private:
             }
          }
       }
-      if(recentFiles && recentFiles.count)
-      {
-         int c;
-         for(c = 0; c < recentFiles.count; c++)
-         {
-            if(recentFiles[c] && recentFiles[c][0])
-               ChangeCh(recentFiles[c], from, to);
-         }
-      }
-      if(recentProjects && recentProjects.count)
-      {
-         int c;
-         for(c = 0; c < recentProjects.count; c++)
-         {
-            if(recentProjects[c] && recentProjects[c][0])
-               ChangeCh(recentProjects[c], from, to);
-         }
-      }
+      recentFiles.changeChar(from, to);
+      recentProjects.changeChar(from, to);
       if(docDir && docDir[0])
          ChangeCh(docDir, from, to);
       if(ideFileDialogLocation && ideFileDialogLocation[0])
@@ -725,43 +707,55 @@ private:
       }
       return output;
    }
+}
 
-   void AddRecentFile(const char * fileName)
+class RecentPaths : Array<String>
+{
+   IteratorPointer Add(T value)
    {
       int c;
-      char * filePath = CopyString(fileName);
+      char * filePath = (char *)value;
       ChangeCh(filePath, '\\', '/');
-      for(c = 0; c<recentFiles.count; c++)
+      for(c = 0; c < count; c++)
       {
-         if(recentFiles[c] && !fstrcmp(recentFiles[c], filePath))
+         if(this[c] && !fstrcmp(this[c], filePath))
          {
-            recentFiles.Delete((void *)&recentFiles[c]);
+            Delete((void *)&this[c]);
             c--;
          }
       }
-      while(recentFiles.count >= MaxRecent)
-         recentFiles.Delete(recentFiles.GetLast());
-      recentFiles.Insert(null, filePath);
-      //UpdateRecentMenus(owner);
+      return Array::Add((T)filePath);
    }
 
-   void AddRecentProject(const char * projectName)
+   IteratorPointer addRecent(T value)
    {
       int c;
-      char * filePath = CopyString(projectName);
+      char * filePath = (char *)value;
       ChangeCh(filePath, '\\', '/');
-      for(c = 0; c<recentProjects.count; c++)
+      for(c = 0; c < count; c++)
       {
-         if(recentProjects[c] && !fstrcmp(recentProjects[c], filePath))
+         if(this[c] && !fstrcmp(this[c], filePath))
          {
-            recentProjects.Delete((void *)&recentProjects[c]);
+            Delete((void *)&this[c]);
             c--;
          }
       }
-      while(recentProjects.count >= MaxRecent)
-         recentProjects.Delete(recentProjects.GetLast());
-      recentProjects.Insert(null, filePath);
-      //UpdateRecentMenus(owner);
+      while(count >= MaxRecent)
+         Delete(GetLast());
+      return Insert(null, filePath);
+   }
+
+   void changeChar(char from, char to)
+   {
+      if(this && count)
+      {
+         int c;
+         for(c = 0; c < count; c++)
+         {
+            if(this[c] && this[c][0])
+               ChangeCh(this[c], from, to);
+         }
+      }
    }
 }
 
index 1b89990..a24e939 100644 (file)
@@ -2101,7 +2101,7 @@ class CodeEditor : Window
       if(!parentClosing)
       {
          if(ide.workspace && fileName)
-            ide.workspace.UpdateOpenedFileInfo(fileName, closed);
+            ide.workspace.UpdateOpenedFileInfo(fileName, closed, false);
          if(inUseDebug && !debugClosing)
          {
             debugClosing = true;
index cc389a5..bea4dcd 100644 (file)
@@ -93,7 +93,6 @@ class GlobalSettingsDialog : Window
                {
                   ideSettings.compilerConfigs.Add(compiler.Copy());
                }
-
                compilerSettingsChanged = true;
             }
 
index 2ad63ca..cb08cd7 100644 (file)
@@ -734,8 +734,8 @@ class IDEWorkSpace : Window
          }
       };
       MenuDivider { fileMenu };
-      Menu recentFiles { fileMenu, $"Recent Files", r };
-      Menu recentProjects { fileMenu, $"Recent Projects", p };
+      Menu recentFilesMenu { fileMenu, $"Recent Files", r };
+      Menu recentProjectsMenu { fileMenu, $"Recent Projects", p };
       MenuDivider { fileMenu };
       MenuItem exitItem
       {
@@ -751,7 +751,8 @@ class IDEWorkSpace : Window
       bool FileRecentFile(MenuItem selection, Modifiers mods)
       {
          int id = 0;
-         for(file : ideSettings.recentFiles)
+         RecentPaths recentFiles = workspace ? workspace.recentFiles : ideSettings.recentFiles;
+         for(file : recentFiles)
          {
             if(id == selection.id)
             {
@@ -821,8 +822,8 @@ class IDEWorkSpace : Window
                      newProjectDialog.CreateNewProject();
                      if(projectView)
                      {
-                        ideSettings.AddRecentProject(projectView.fileName);
-                        ide.UpdateRecentMenus();
+                        ideSettings.recentProjects.addRecent(CopyString(projectView.fileName));
+                        ide.updateRecentProjectsMenu();
                         settingsContainer.Save();
                      }
                   }
@@ -1723,6 +1724,7 @@ class IDEWorkSpace : Window
       ideMainFrame.SetText("%s - %s", project.topNode.name, titleECEREIDE);
 
       AdjustMenus();
+      updateRecentMenus();
 
       ide.breakpointsView.LoadFromWorkspace();
       ide.watchesView.LoadFromWorkspace();
@@ -1754,6 +1756,7 @@ class IDEWorkSpace : Window
          outputView.visible = false;
          ideMainFrame.text = titleECEREIDE;
          ide.AdjustMenus();
+         ide.updateRecentMenus();
          return true;
       }
       return false;
@@ -1820,8 +1823,8 @@ class IDEWorkSpace : Window
 
    void DocumentSaved(Window document, const char * fileName)
    {
-      ideSettings.AddRecentFile(fileName);
-      ide.UpdateRecentMenus();
+      ideSettings.recentFiles.addRecent(CopyString(fileName));
+      ide.updateRecentFilesMenu();
       ide.AdjustFileMenus();
       settingsContainer.Save();
    }
@@ -2537,7 +2540,7 @@ class IDEWorkSpace : Window
          {
             document.fileName = filePath;
             if(workspace && !workspace.holdTracking)
-               workspace.UpdateOpenedFileInfo(filePath, opened);
+               workspace.UpdateOpenedFileInfo(filePath, opened, true);
          }
       }
 
@@ -2566,11 +2569,11 @@ class IDEWorkSpace : Window
             document.state = maximized;
 
          if(isProject)
-            ideSettings.AddRecentProject(document.fileName);
-         else
-            ideSettings.AddRecentFile(document.fileName);
-         ide.UpdateRecentMenus();
+            ideSettings.recentProjects.addRecent(CopyString(document.fileName));
+         else if(!workspace)
+            ideSettings.recentFiles.addRecent(CopyString(document.fileName));
          ide.AdjustFileMenus();
+         ide.updateRecentFilesMenu();
          settingsContainer.Save();
 
          return document;
@@ -2583,19 +2586,19 @@ class IDEWorkSpace : Window
    /*bool Window::GenericDocumentOnClose(bool parentClosing)
    {
       if(!parentClosing && ide.workspace)
-         ide.workspace.UpdateOpenedFileInfo(fileName, unknown);
+         ide.workspace.UpdateOpenedFileInfo(fileName, unknown, false);
       return true;
    }*/
    bool ModelView::ModelViewOnClose(bool parentClosing)
    {
       if(!parentClosing && ide.workspace)
-         ide.workspace.UpdateOpenedFileInfo(fileName, unknown);
+         ide.workspace.UpdateOpenedFileInfo(fileName, unknown, false);
       return true;
    }
    bool PictureEdit::PictureEditOnClose(bool parentClosing)
    {
       if(!parentClosing && ide.workspace)
-         ide.workspace.UpdateOpenedFileInfo(fileName, unknown);
+         ide.workspace.UpdateOpenedFileInfo(fileName, unknown, false);
       return true;
    }
 
@@ -3166,8 +3169,8 @@ class IDEWorkSpace : Window
                   newProjectDialog.Modal();
                   if(projectView)
                   {
-                     ideSettings.AddRecentProject(projectView.fileName);
-                     ide.UpdateRecentMenus();
+                     ideSettings.recentProjects.addRecent(CopyString(projectView.fileName));
+                     ide.updateRecentMenus();
                      settingsContainer.Save();
                   }
                   delete newProjectDialog;
@@ -3459,38 +3462,46 @@ class IDEWorkSpace : Window
       return true;
    }
 
-   void UpdateRecentMenus()
+   void updateRecentMenus()
    {
-      int c;
-      Menu fileMenu = menu.FindMenu($"File");
-      Menu recentFiles = fileMenu.FindMenu($"Recent Files");
-      Menu recentProjects = fileMenu.FindMenu($"Recent Projects");
+      updateRecentFilesMenu();
+      updateRecentProjectsMenu();
+   }
+
+   void updateRecentFilesMenu()
+   {
+      int c = 0;
       char * itemPath = new char[MAX_LOCATION];
       char * itemName = new char[MAX_LOCATION+4];
-
-      recentFiles.Clear();
-      c = 0;
-
-      for(recent : ideSettings.recentFiles)
+      Workspace ws = workspace;
+      RecentPaths recentFiles = ws ? ws.recentFiles : ideSettings.recentFiles;
+      recentFilesMenu.Clear();
+      for(recent : recentFiles)
       {
          strncpy(itemPath, recent, MAX_LOCATION); itemPath[MAX_LOCATION-1] = '\0';
          MakeSystemPath(itemPath);
          snprintf(itemName, MAX_LOCATION+4, "%d %s", 1 + c, itemPath); itemName[MAX_LOCATION+4-1] = '\0';
-         recentFiles.AddDynamic(MenuItem { copyText = true, text = itemName, (Key)k1 + c, id = c, NotifySelect = ide.FileRecentFile }, ide, true);
+         recentFilesMenu.AddDynamic(MenuItem { copyText = true, text = itemName, (Key)k1 + c, id = c, NotifySelect = ide.FileRecentFile }, ide, true);
          c++;
       }
+      delete itemPath;
+      delete itemName;
+   }
 
-      recentProjects.Clear();
-      c = 0;
+   void updateRecentProjectsMenu()
+   {
+      int c = 0;
+      char * itemPath = new char[MAX_LOCATION];
+      char * itemName = new char[MAX_LOCATION+4];
+      recentProjectsMenu.Clear();
       for(recent : ideSettings.recentProjects)
       {
          strncpy(itemPath, recent, MAX_LOCATION); itemPath[MAX_LOCATION-1] = '\0';
          MakeSystemPath(itemPath);
          snprintf(itemName, MAX_LOCATION+4, "%d %s", 1 + c, itemPath); itemName[MAX_LOCATION+4-1] = '\0';
-         recentProjects.AddDynamic(MenuItem { copyText = true, text = itemName, (Key)k1 + c, id = c, NotifySelect = ide.FileRecentProject }, ide, true);
+         recentProjectsMenu.AddDynamic(MenuItem { copyText = true, text = itemName, (Key)k1 + c, id = c, NotifySelect = ide.FileRecentProject }, ide, true);
          c++;
       }
-
       delete itemPath;
       delete itemName;
    }
index 867fa05..4b03347 100644 (file)
@@ -45,7 +45,7 @@ IDESettingsContainer settingsContainer
 #ifndef MAKEFILE_GENERATOR
       IDESettings settings = (IDESettings)data;
       globalSettingsDialog.ideSettings = settings;
-      ide.UpdateRecentMenus();
+      ide.updateRecentMenus();
       ide.UpdateCompilerConfigs(true);
 #endif
    }
index 1c368c3..0ba89ae 100644 (file)
@@ -205,6 +205,7 @@ public:
    property List<Breakpoint> breakpoints { set { breakpoints = value; } get { return breakpoints; } isset { return breakpoints && breakpoints.count; } }
    property List<Watch> watches { set { watches = value; } get { return watches; } isset { return watches && watches.count; } }
    property List<OpenedFileInfo> openedFiles { set { openedFiles = value; } get { return openedFiles; } isset { return openedFiles && openedFiles.count; } }
+   property RecentPaths recentFiles { set { recentFiles = value; } get { return recentFiles; } isset { return recentFiles && recentFiles.count; } }
 
    bool useValgrind;
    ValgrindLeakCheck vgLeakCheck;
@@ -223,6 +224,7 @@ private:
    List<Breakpoint> breakpoints;
    List<Watch> watches;
    List<OpenedFileInfo> openedFiles;
+   RecentPaths recentFiles;
 
    char * workspaceFile;
    char * workspaceDir;
@@ -534,20 +536,26 @@ public:
       return node;
    }
 
-   OpenedFileInfo UpdateOpenedFileInfo(const char * fileName, OpenedFileState state)
+   OpenedFileInfo UpdateOpenedFileInfo(const char * fileName, OpenedFileState state, bool fileOpen)
    {
+      bool insert = false;
       char absolutePath[MAX_LOCATION];
       char relativePath[MAX_LOCATION];
       OpenedFileInfo ofi;
       GetSlashPathBuffer(absolutePath, fileName);
       MakeRelativePath(relativePath, fileName);
       ofi = FindOpenedFileInfo(relativePath, absolutePath);
+      if(fileOpen && ofi)
+      {
+         openedFiles.Remove(openedFiles.Find(ofi));
+         insert = true;
+      }
       if(state)
       {
          if(!ofi)
          {
             ofi = OpenedFileInfo { path = CopyString(relativePath) };
-            openedFiles.Add(ofi);
+            insert = true;
          }
          ofi.state = state;
          ofi.modified = GetLocalTimeStamp();
@@ -562,6 +570,8 @@ public:
          if(!holdTracking)
             modified = true;
       }
+      if(insert)
+         openedFiles.Insert(null, ofi);
       return ofi;
    }
 
@@ -641,7 +651,7 @@ public:
 
    void RestorePreviouslyOpenedFileState(CodeEditor editor)
    {
-      if((editor.openedFileInfo = UpdateOpenedFileInfo(editor.fileName, opened)))
+      if((editor.openedFileInfo = UpdateOpenedFileInfo(editor.fileName, opened, true)))
          editor.openedFileInfo.SetCodeEditorState(editor);
    }
 
@@ -931,6 +941,7 @@ public:
       if(!breakpoints) breakpoints = { };
       if(!watches) watches = { };
       if(!openedFiles) openedFiles = { };
+      if(!recentFiles) recentFiles = { };
    }
 
    void Free()
@@ -950,6 +961,7 @@ public:
       if(breakpoints) { breakpoints.Free(); delete breakpoints; }
       if(watches) { watches.Free(); delete watches; }
       if(openedFiles) { openedFiles.Free(); delete openedFiles; }
+      if(recentFiles) { recentFiles.Free(); delete recentFiles; }
 
       projects.Free();
    }
@@ -976,7 +988,6 @@ public:
       SetSourceDirs(null);
       Free();
    }
-
 }
 
 Workspace LoadWorkspace(const char * filePath, const char * fromProjectFile)
@@ -1076,6 +1087,21 @@ Workspace LoadWorkspace(const char * filePath, const char * fromProjectFile)
 
       if(workspace)
       {
+         if(!workspace.recentFiles || !workspace.recentFiles.count)
+         {
+            int c;
+            if(!workspace.recentFiles) workspace.recentFiles = { };
+            if(workspace.openedFiles && workspace.openedFiles.count)
+            {
+               for(c = workspace.openedFiles.count - 1; c >= 0; c--)
+               {
+                 char path[MAX_LOCATION];
+                 strcpy(path, workspace.workspaceDir);
+                 PathCatSlash(path, workspace.openedFiles[c].path);
+                 workspace.recentFiles.addRecent(CopyString(path));
+               }
+            }
+         }
          workspace.Init();
          if(!workspace.projects.first)
          {