Patch #2162 2007-08-28 12:16

olipfei

Patch for Feature Request #3547
Download
2162-Patch_for_Feat.patch (7.1 KB)
Category
Application::FeatureAdd
Status
Accepted
Close date
2009-08-24 07:33
Assigned to
mortenmacfly
Index: src/include/editormanager.h
===================================================================
--- src/include/editormanager.h    (Revision 5294)
+++ src/include/editormanager.h    (Arbeitskopie)
@@ -136,6 +136,8 @@
         void OnSwapHeaderSource(wxCommandEvent& event);
         void OnTabPosition(wxCommandEvent& event);
         void OnProperties(wxCommandEvent& event);
+        void OnAddFileToProject(wxCommandEvent& event);
+        void OnRemoveFileFromProject(wxCommandEvent& event);
         void OnAppDoneStartup(wxCommandEvent& event);
         void OnAppStartShutdown(wxCommandEvent& event);
         void OnUpdateUI(wxUpdateUIEvent& event);
Index: src/sdk/cbeditor.cpp
===================================================================
--- src/sdk/cbeditor.cpp    (Revision 5294)
+++ src/sdk/cbeditor.cpp    (Arbeitskopie)
@@ -398,6 +398,8 @@
 const int idUnsplit = wxNewId();
 const int idConfigureEditor = wxNewId();
 const int idProperties = wxNewId();
+const int idAddFileToProject = wxNewId();
+const int idRemoveFileFromProject = wxNewId();
 
 const int idBookmarkAdd = wxNewId();
 const int idBookmarkRemove = wxNewId();
@@ -431,6 +433,8 @@
     EVT_MENU(idFoldingToggleCurrent, cbEditor::OnContextMenuEntry)
     EVT_MENU(idConfigureEditor, cbEditor::OnContextMenuEntry)
     EVT_MENU(idProperties, cbEditor::OnContextMenuEntry)
+    EVT_MENU(idAddFileToProject, cbEditor::OnContextMenuEntry)
+    EVT_MENU(idRemoveFileFromProject, cbEditor::OnContextMenuEntry)
     EVT_MENU(idBookmarkAdd, cbEditor::OnContextMenuEntry)
     EVT_MENU(idBookmarkRemove, cbEditor::OnContextMenuEntry)
     EVT_MENU(idBreakpointAdd, cbEditor::OnContextMenuEntry)
@@ -662,13 +666,13 @@
         else
             m_Shortname = m_pProjectFile->file.GetFullName();
         SetEditorTitle(m_Shortname);
+
+        if (!wxFileExists(m_Filename))
+            m_pProjectFile->SetFileState(fvsMissing);
+        else if (!wxFile::Access(m_Filename.c_str(), wxFile::write)) // readonly
+            m_pProjectFile->SetFileState(fvsReadOnly);
     }
 
-    if (!wxFileExists(m_Filename))
-        m_pProjectFile->SetFileState(fvsMissing);
-    else if (!wxFile::Access(m_Filename.c_str(), wxFile::write)) // readonly
-        m_pProjectFile->SetFileState(fvsReadOnly);
-
 #if 0
     wxString dbg;
     dbg << _T("[ed] Filename: ") << GetFilename() << _T('\n');
@@ -1418,6 +1422,9 @@
     fname.Assign(m_Filename);
     m_Shortname = fname.GetFullName();
     SetEditorTitle(m_Shortname);
+    // invalidate m_pProjectFile, because if kept, it would point to the ProjectFile with old name and
+    // cause ProjectManager::RemoveFileFromProject called via context menu to crash
+    SetProjectFile(0); 
     //Manager::Get()->GetLogManager()->Log(mltDevDebug, "Filename=%s\nShort=%s", m_Filename.c_str(), m_Shortname.c_str());
     m_IsOK = true;
     SetModified(true);
@@ -2239,6 +2246,15 @@
             popup->Append(idConfigureEditor, _("Configure editor..."));
         popup->Append(idProperties, _("Properties..."));
 
+        if (Manager::Get()->GetProjectManager()->GetActiveProject()) // project must be open
+        {
+            popup->AppendSeparator();
+
+            if (m_pProjectFile)
+                popup->Append(idRemoveFileFromProject, _("Remove file from project"));
+            else
+                popup->Append(idAddFileToProject, _("Add file to active project"));
+        }
         // remove "Insert/Empty" if more than one entry
         wxMenu* insert = 0;
         wxMenuItem* insertitem = popup->FindItem(idInsert);
@@ -2467,6 +2483,27 @@
             dlg.ShowModal();
         }
     }
+    else if (id == idAddFileToProject)
+    {
+        cbProject *prj = Manager::Get()->GetProjectManager()->GetActiveProject();
+
+        wxArrayInt targets;
+        if (Manager::Get()->GetProjectManager()->AddFileToProject(m_Filename, prj, targets) != 0)
+        {
+            ProjectFile* pf = prj->GetFileByFilename(m_Filename, false);
+            SetProjectFile(pf);
+            Manager::Get()->GetProjectManager()->RebuildTree();
+        }
+    }
+    else if (id == idRemoveFileFromProject)
+    {
+        if (m_pProjectFile)
+        {
+            cbProject *prj = m_pProjectFile->GetParentProject();
+            Manager::Get()->GetProjectManager()->RemoveFileFromProject(m_pProjectFile, prj);
+            Manager::Get()->GetProjectManager()->RebuildTree();
+        }
+    }
     else if (id == idBreakpointAdd)
         AddBreakpoint(m_pData->m_LastMarginMenuLine);
     else if (id == idBreakpointEdit)
Index: src/sdk/editormanager.cpp
===================================================================
--- src/sdk/editormanager.cpp    (Revision 5294)
+++ src/sdk/editormanager.cpp    (Arbeitskopie)
@@ -105,6 +105,8 @@
 static const int idNBTabTop = wxNewId();
 static const int idNBTabBottom = wxNewId();
 static const int idNBProperties = wxNewId();
+static const int idNBAddFileToProject = wxNewId();
+static const int idNBRemoveFileFromProj
download for full patch...
olipfei 2007-08-28 12:28

New item 'Add file to active project' in EditorManager and cbEditor popup menus. I actually also added 'Remove file from project' functionality to these menus, but to this purpose I had to insert a function to the public interface of class ProjectManager which I don't know if I'm allowed to do so as 'external' developer. It would be fine if someone could give me a hint how to implement a more suitable and generic version of ProjectManager::RemoveFileFromProject, compared with AddFileToProject.

olipfei 2007-08-28 12:33

Forgot to remove two comments from my other experiments, thus uploaded corrected patch.

rhf 2008-05-01 16:51

I just became aware of this patch and would like to experiment with it. So far I have not been able to apply the patch. Maybe my procedure is wrong:

Open View Raw Patch above and save file in src directory, with .patch extension.

Right-click file->TortoiseSVN->Apply patch....

This gives error "An unknown line type was found in line 109!".

Any suggestions?

Thanks.

olipfei 2008-05-01 18:29

The procedure seems fine (though I have no experience with TortoiseSVN), but the patch is definitely too old. I wrote it with respect to revision 4403, now CB is at revision >5000. I will soon upload an adapted patch, but cannot do so before next week.

rhf 2008-05-02 00:44

Great. I look forward to experimenting with your new patch. This is a MSVC feature that I have really missed in CodeBlocks.

olipfei 2008-05-28 18:57

Sorry for the (expectable ;-) delay! I have adapted the patch to revision 5081. Can you please crosscheck if it works now (applying the patch and compiling)?

rhf 2008-05-29 02:16

I just noticed your updated patch. I will definitely try it out, but it will probably be this weekend before I can get to it. Thanks.

rhf 2008-05-31 15:30

I applied the patch (Windows XP) and it appears to work as you intended. Good work!

However, it does not do quite what I had hoped. I often have a project file, say a header, open in the editor and decide I need another file. I do a File->Save as and enter the new file name, which opens in the editor. I then want to add this to the project, but the context menu continues with the option "Remove file from project" . This new file is not yet in the project and two attempts to remove it will cause CB to crash. If you can fix this, I think this would be an excellent additional feature for CB.

olipfei 2008-06-11 20:26

Uploaded corrected patch which now also fixes a minor flaw in cbEditor::SaveAs that causes another part of the patch to crash. Thanks to 'rhf' for drawing my attention to this issue.

olipfei 2008-07-03 21:04

Cleaned up the patch a little bit and incorporated a further suggestion due to 'rhf'. I would not mind if it were accepted now ;-)

rhf 2008-07-06 16:48

Excellent! I really like this feature and hope that the patch will be incorporated into CodeBlocks. Good work, Olipfei.

rhf 2008-08-01 21:51

I have been using this excellent patch for nearly a month (on Windows XP) with absolutely no problems. Today SVN Rev 5174 caused a conflict with the patched file 'trunk/src/sdk/projectmanager.cpp'. I really don't know how to fix the conflict so I have reverted to an earlier Rev. However, it would really be nice if this feature could be incorporated into the main program. If I could help a developer in any way, perhaps with testing, I would be happy to do so.

Thanks.

olipfei 2008-09-01 20:47

Uploaded updated patch to match revision 5200, due to rhf's request.

rhf 2008-09-02 01:05

Thanks very much for the updated patch. It would be great if it were incorporated into the trunk.

mortenmacfly 2008-10-25 18:05

I have applied the patch partially.

For the rest (to add the menu entry):

I am not satisfied with it. The menu entry is just below "Switch header / source" which is *really* often used. I for myself chose "Remove file from project" a lot by mistake because it's on the "Switch..." things original position. this is very annoying as you really have to add the file again. A message box warning about the file to be removed would be really welcome here. In addition: Probably you can make this menu entry to appear on the very bottom of the menu?

Anyways: It would be nice if you could update the patch so it matches trunk again.

olipfei 2008-10-27 20:07

Thanks for pointing me to this, I also had some concerns about the 'ideal' place for the menu entry. Usually I'm a keen keyboard user, I press F11 to switch between source and header, and so I didn't observe that the old position caused confusion. But are you sure you want to have another dialog popping up? The 'Remove file' procedure in the project management pane which was already there before my patch doesn't warn before it acts either. And you have to admit that some UI parts of CB could bear some streamlining...

I placed my entries at the bottom of the menu now (after the 'Properties...' menu entry, internally) and synced the code with rev 5294. Note that my patch also includes two minor fixes for problems (possibly crashes) of CB in cbEditor::SetProjectFile and cbEditor::SaveAs (see conversation below).

olipfei 2008-10-27 20:12

Oops, forgot checkbox to upload revised patch.

rhf 2008-10-28 01:05

Guys, I applied olipfei's updated patch of Oct 27 to rev 5294.

It seems to work fine for me on Windows XP. Also, I like the menu entry at the bottom; it makes it easier to find. Thanks.

mortenmacfly 2009-01-16 09:54

Just for the record: I am still on this... it's not forgotten. :-)

rhf 2009-01-18 18:54

Thanks, mortenmacfly. If I can help in any way, perhaps with testing, I would be glad to do so.