Patch #985 2006-04-08 18:12

dermeister

Feature Request #2119 (swap header/source enchancement)
Download
985-Feature_Request.patch (2.7 KB)
Category
Application::FeatureAdd
Status
Accepted
Close date
2006-05-09 10:41
Assigned to
 
Index: src/sdk/editormanager.cpp
===================================================================
--- src/sdk/editormanager.cpp    (revision 2321)
+++ src/sdk/editormanager.cpp    (working copy)
@@ -585,12 +585,12 @@
     }
 }
 
-cbEditor* EditorManager::New()
+cbEditor* EditorManager::New(const wxString& newFileName)
 {
     SANITY_CHECK(0L);
 
     wxString old_title = Manager::Get()->GetAppWindow()->GetTitle(); // Fix for Bug #1389450
-    cbEditor* ed = new cbEditor(m_pNotebook, wxEmptyString);
+    cbEditor* ed = new cbEditor(m_pNotebook, newFileName);
     if (!ed->SaveAs())
     {
         //DeletePage(ed->GetPageIndex());
@@ -1104,6 +1104,41 @@
         //    newEd->SetProjectFile(ed->GetProjectFile());
         return newEd;
     }
+
+    // We couldn't find the file, maybe it does not exist. Ask the user if we
+    // should create it:
+    if (cbMessageBox(_("The file does not exist. Do you want to create it?"),
+                _("Error"), wxICON_QUESTION | wxYES_NO) == wxID_YES)
+    {
+        cbProject* project = Manager::Get()->GetProjectManager()->GetActiveProject();
+        if (project)
+            wxSetWorkingDirectory(project->GetBasePath());
+
+        // Create a suggestion for the new file name:
+        if (ft == ftHeader)
+            fn.SetExt(FileFilters::CPP_EXT);
+        else if (ft == ftSource)
+            fn.SetExt(FileFilters::H_EXT);
+        // else? Well, if the filename is not changed we could possibly
+        // overwrite an existing file with our suggestion.
+
+        cbEditor* newEd = New(fn.GetFullPath());
+        if (cbMessageBox(_("Do you want to add this new file in the active project?"),
+                    _("Add file to project"),
+                    wxYES_NO | wxICON_QUESTION) == wxID_YES)
+        {
+            wxArrayInt targets;
+            if (Manager::Get()->GetProjectManager()->AddFileToProject(newEd->GetFilename(), project, targets) != 0)
+            {
+                ProjectFile* pf = project->GetFileByFilename(newEd->GetFilename(), false);
+                newEd->SetProjectFile(pf);
+                Manager::Get()->GetProjectManager()->RebuildTree();
+            }
+        }
+        // verify that the open files are still in sync
+        // the new file might have overwritten an existing one)
+        Manager::Get()->GetEditorManager()->CheckForExternallyModifiedFiles();
+    }
     return 0L;
 }
 
Index: src/sdk/editormanager.h
===================================================================
--- src/sdk/editormanager.h    (revision 2321)
+++ src/sdk/editormanager.h    (working copy)
@@ -72,7 +72,7 @@
         void SetActiveEditor(EditorBase* ed);
         EditorColorSet* GetColorSet(){ return (this==NULL) ? 0 : m_Theme; }
         void SetColorSet(EditorColorSet* theme);
-        cbEditor* New();
+        cbEditor* New(const wxString& newFileName = wxEmptyString);
 
         // these are used *only* for custom editors
         void AddCustomEditor(EditorBase* eb);
mandrav 2006-04-10 09:27

I don't get it...

What does this have to do with ftr.req. 2119?

This just adds a parameter in EditorManager::New().

dermeister 2006-04-10 10:33

If you edit a .cpp-file and click on 'swap header/source' but the headerfile does not exist, nothing happens. This is the behaviour of Code::Blocks. But (as requested in ftr. req. 2119) it would probably better to ask the user if he wants to create this missing file (as he wants to access it he either assumes that it is there or at least he wants it to be there). This patch implements exactly this. If the file cannot be found it asks the user if he wants to create a new file. I manily used the code the is executed by clicking 'File->New' to get this, thus the behaviour is the same.

I only made one change: Normaly the suggested filename for a new file is 'Untitled'. Thats OK but in this case we could give a better suggestion: The file that we were looking for. To achive this I added this parameter to EditorManager::New(). This doens't really change the interface of EditorManager because this parameter is optional and if you don't provide it you get exactly the old behaviour.