Index: src/sdk/editormanager.cpp
===================================================================
--- src/sdk/editormanager.cpp	(revision 5986)
+++ src/sdk/editormanager.cpp	(working copy)
@@ -953,21 +953,70 @@
     m_isCheckingForExternallyModifiedFiles = false;
 }
 
+bool EditorManager::IsHeaderSource(const wxFileName& testedFileName, const wxFileName& activeFileName, FileType ftActive)
+{
+    if (testedFileName.GetName() == activeFileName.GetName())
+    {
+        FileType ftTested = FileTypeOf(testedFileName.GetFullName());
+        if (    ((ftActive == ftHeader) && (ftTested == ftSource))
+             || ((ftActive == ftSource) && (ftTested == ftHeader)) )
+        {
+            if (testedFileName.FileExists())
+            {
+                return true;
+            }
+        }
+    }
+    return false;
+}
+
+wxFileName EditorManager::FindHeaderSource(const wxArrayString& fileArray, const wxFileName& activeFileName, bool& isCandidate)
+{
+    wxFileName candidateName;
+    FileType ftActive = FileTypeOf(activeFileName.GetFullName());
+    // because ftActive == ftHeader || ftSource, the extension has at least 1 character
+    bool extStartsWithCapital = wxIsupper(activeFileName.GetExt()[0]);
+
+    for (unsigned i = 0; i < fileArray.GetCount(); ++i)
+    {
+        wxFileName testedFile(fileArray[i]);
+
+        if (IsHeaderSource(testedFile, activeFileName, ftActive))
+        {
+            bool isUpper = wxIsupper(testedFile.GetExt()[0]);
+            if (isUpper == extStartsWithCapital)
+            {
+                // we found the header/source we were searching for
+                isCandidate = false;
+                return testedFile;
+            }
+            else
+            {
+                // the header/source has a different capitalization of its extension
+                // use this if nothing better is found
+                candidateName = testedFile;
+            }
+        }
+    }
+    isCandidate = true;
+    // may be invalid (empty) file name
+    return candidateName;
+}
+
 bool EditorManager::SwapActiveHeaderSource()
 {
     cbEditor* ed = GetBuiltinEditor(GetActiveEditor());
     if (!ed)
         return false;
 
+    ProjectManager *pm = Manager::Get()->GetProjectManager();
+    if (!pm)
+        return false;
+
     FileType ft = FileTypeOf(ed->GetFilename());
     if (ft != ftHeader && ft != ftSource)
         return false;
 
-    // because ft == ftHeader || ftSource, the extension has at least 1 character
-    bool extStartsWithCapital = wxIsupper(wxFileName(ed->GetFilename()).GetExt()[0]);
-
-    // create a list of search dirs
-    wxArrayString dirs;
     cbProject* project = 0;
 
     // if the file in question belongs to a different open project,
@@ -980,94 +1029,91 @@
 
     // if we didn't get a valid project, try the active one
     if (!project)
-        project = Manager::Get()->GetProjectManager()->GetActiveProject();
+        project = pm->GetActiveProject();
 
-    if (project)
+    wxFileName fn(ed->GetFilename());
+    wxFileName candidateFile;
+    bool isCandidate;
+    wxArrayString fileArray;
+
+    // find all files with the same name as the active file, but with possibly different extension
+    // search in the directory of the active file
+    wxDir::GetAllFiles(fn.GetPath(wxPATH_GET_VOLUME), &fileArray, fn.GetName() + _T(".*"), wxDIR_FILES | wxDIR_HIDDEN);
+    // try to find the header/source in the list
+    wxFileName foundFile = FindHeaderSource(fileArray, fn, isCandidate);
+
+    if (isCandidate)
     {
-        // get project's include dirs
-        dirs = project->GetIncludeDirs();
+        candidateFile = foundFile;
+    }
+    else if (foundFile.IsOk())
+    {
+        cbEditor* newEd = Open(foundFile.GetFullPath());
+        if (newEd!=0L) // we found and were able to open it
+            return true; // --> RETURN;
+    }
 
-        if (opf)
-        {
-            wxString const &activeName = opf->file.GetName();
+    // try to find the file among the opened files
 
-            // first try to find the file among the opened files
-            for (int i = 0; i < GetEditorsCount(); ++i)
-            {
-                cbEditor* edit = GetBuiltinEditor(GetEditor(i));
-                if (!edit)
-                    continue;
+    // build a list of opened files
+    fileArray.Clear();
+    for (int i = 0; i < GetEditorsCount(); ++i)
+    {
+        cbEditor* edit = GetBuiltinEditor(GetEditor(i));
+        if (!edit)
+            continue;
 
-                ProjectFile* pf = edit->GetProjectFile();
-                if (!pf)
-                    continue;
+        ProjectFile* pf = edit->GetProjectFile();
+        if (!pf)
+            continue;
 
-                if (pf->file.GetName() == activeName)
-                {
-                    wxFileName const & fname = pf->file;
-                    FileType ft_other = FileTypeOf(fname.GetFullName());
-                    if (   (    ((ft == ftHeader) && (ft_other == ftSource))
-                             || ((ft == ftSource) && (ft_other == ftHeader)) )
-                        && (wxIsupper(fname.GetExt()[0]) == extStartsWithCapital) )
-                    {
-                        if (fname.FileExists())
-                        {
-                            cbEditor* newEd = Open(fname.GetFullPath());
-                            if (newEd!=0L) // we found and were able to open it
-                                return true; // --> RETURN
-                        }
-                    }
-                }
-            }
+        fileArray.Add(pf->file.GetFullPath());
+    }
 
-            // second try to find in the project files - at the same time
-            // build the directory list for further searching if not
-            // successful now
-            for (int i = 0; i < project->GetFilesCount(); ++i)
-            {
-                ProjectFile* pf = project->GetFile(i);
-                if (!pf)
-                    continue;
+    // try to find the header/source in the list
+    foundFile = FindHeaderSource(fileArray, fn, isCandidate);
 
-                wxString dir = pf->file.GetPath(wxPATH_GET_VOLUME);
-                if (dirs.Index(dir) == wxNOT_FOUND)
-                    dirs.Add(dir);
+    if (!isCandidate && foundFile.IsOk())
+    {
+        cbEditor* newEd = Open(foundFile.GetFullPath());
+        if (newEd!=0L) // we found and were able to open it
+            return true; // --> RETURN;
+    }
 
-                if (pf->file.GetName() == activeName)
-                {
-                    wxFileName const & fname = pf->file;
-                    FileType ft_other = FileTypeOf(fname.GetFullName());
-                    if (   (    ((ft == ftHeader) && (ft_other == ftSource))
-                             || ((ft == ftSource) && (ft_other == ftHeader)) )
-                        && (wxIsupper(fname.GetExt()[0]) == extStartsWithCapital) )
-                    {
-                        if (fname.FileExists())
-                        {
-                            cbEditor* newEd = Open(fname.GetFullPath());
-                            if (newEd!=0L) // we found and were able to open it
-                                return true; // --> RETURN
-                        }
-                    }
-                }
-            }
-        }
-        else // no opf
+    if (project)
+    {
+        // try to find in the project files
+
+        // build a list of project files
+        fileArray.Clear();
+        for (int i = 0; i < project->GetFilesCount(); ++i)
         {
-            // build the directory list for further searching if opf not available
-            for (int i = 0; i < project->GetFilesCount(); ++i)
-            {
-                ProjectFile* pf = project->GetFile(i);
-                if (!pf)
-                    continue;
+            ProjectFile* pf = project->GetFile(i);
+            if (!pf)
+                continue;
 
-                wxString dir = pf->file.GetPath(wxPATH_GET_VOLUME);
-                if (dirs.Index(dir) == wxNOT_FOUND)
-                    dirs.Add(dir);
-            }
+            fileArray.Add(pf->file.GetFullPath());
         }
 
-        // if not found, continue building the list of directories for further searching
+        // try to find the header/source in the list
+        foundFile = FindHeaderSource(fileArray, fn, isCandidate);
 
+        if (isCandidate && !candidateFile.IsOk())
+        {
+            candidateFile = foundFile;
+        }
+        else if (foundFile.IsOk())
+        {
+            cbEditor* newEd = Open(foundFile.GetFullPath());
+            if (newEd!=0L) // we found and were able to open it
+                return true; // --> RETURN;
+        }
+
+        // if not found, build the list of directories for further searching
+
+        // get project's include dirs
+        wxArrayString dirs = project->GetIncludeDirs();
+
         // get targets include dirs
         for (int i = 0; i < project->GetBuildTargetsCount(); ++i)
         {
@@ -1076,51 +1122,50 @@
             {
                 for (unsigned int ti = 0; ti < target->GetIncludeDirs().GetCount(); ++ti)
                 {
-                    // TODO (Morten#5#): target include dirs might override project include dirs, take append/prepend option into account
                     wxString dir = target->GetIncludeDirs()[ti];
                     if (dirs.Index(dir) == wxNOT_FOUND)
                         dirs.Add(dir);
                 }
             }
         }
-    } // project
 
-    wxFileName fname;
-    wxFileName fn(ed->GetFilename());
-    dirs.Insert(fn.GetPath(wxPATH_GET_VOLUME), 0); // add file's dir
+        // go through the directories and try to find the header/source there
+        for (unsigned int i = 0; i < dirs.GetCount(); ++i)
+        {
+            wxString dir = dirs[i]; // might contain macros -> replace them
+            Manager::Get()->GetMacrosManager()->ReplaceMacros(dir);
 
-    wxString HeaderSource;
+            wxFileName dname(dir);
+            if (!dname.IsAbsolute())
+            {
+                dname.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, project->GetBasePath());
+    //            Manager::Get()->GetLogManager()->DebugLog(F(_T("Normalizing dir to '%s'."), dname.GetFullPath().c_str()));
+            }
 
-    for (unsigned int i = 0; i < dirs.GetCount(); ++i)
-    {
-        ProjectManager *pm = Manager::Get()->GetProjectManager();
-        if ( !pm )
-            break;
+            fileArray.Clear();
+            // find all files inside the directory with the same name as the active file, but with possibly different extension
+            wxDir::GetAllFiles(dname.GetPath(), &fileArray, fn.GetName() + _T(".*"), wxDIR_FILES | wxDIR_HIDDEN);
+            // try to find the header/source in the list
+            foundFile = FindHeaderSource(fileArray, fn, isCandidate);
 
-        wxString dir = dirs[i]; // might contain macros -> replace them
-        Manager::Get()->GetMacrosManager()->ReplaceMacros(dir);
-
-        fname.Assign(dir + wxFileName::GetPathSeparator() + fn.GetFullName());
-//        Manager::Get()->GetLogManager()->DebugLog(F(_T("Looking for '%s', dir='%s'."), fname.GetFullPath().c_str(), dir.c_str()));
-        if (!fname.IsAbsolute() && project)
-        {
-            fname.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, project->GetBasePath());
-//            Manager::Get()->GetLogManager()->DebugLog(F(_T("Normalizing dir to '%s'."), fname.GetFullPath().c_str()));
+            if (isCandidate)
+            {
+                candidateFile = foundFile;
+            }
+            else if (foundFile.IsOk())
+            {
+                cbEditor* newEd = Open(foundFile.GetFullPath());
+                if (newEd!=0L) // we found and were able to open it
+                    return true; // --> RETURN;
+            }
         }
-
-        HeaderSource = pm->GetHeaderSource(fname);
-        if (!HeaderSource.IsEmpty())
-        {
-            fname.SetFullName(HeaderSource);
-//            Manager::Get()->GetLogManager()->DebugLog(F(_T("Located '%s'."), fname.GetFullPath().c_str()));
-            break;
-        }
     }
 
-    if (!HeaderSource.IsEmpty() && fname.FileExists())
+    // candidateFile is header/source whose extension does not match the capitalization of the active file
+    // - open it if nothing better is found
+    if (candidateFile.IsOk())
     {
-        //Manager::Get()->GetLogManager()->DebugLog("ed=%s, pair=%s", ed->GetFilename().c_str(), pair.c_str());
-        cbEditor* newEd = Open(fname.GetFullPath());
+        cbEditor* newEd = Open(candidateFile.GetFullPath());
         if (newEd!=0L) // we found and were able to open it
             return true; // --> RETURN;
     }
@@ -1128,7 +1173,7 @@
     // We couldn't find the file, maybe it does not exist. Ask the user if we
     // should create it:
     if (cbMessageBox(_("The file seems not to exist. Do you want to create it?"),
-                _("Error"), wxICON_QUESTION | wxYES_NO) == wxID_YES)
+                _("Error"), wxICON_QUESTION | wxYES_NO | wxNO_DEFAULT) == wxID_YES)
     {
         cbProject* project = Manager::Get()->GetProjectManager()->GetActiveProject();
         if (project)
Index: src/include/editormanager.h
===================================================================
--- src/include/editormanager.h	(revision 5986)
+++ src/include/editormanager.h	(working copy)
@@ -170,6 +170,8 @@
         int FindInFiles(cbFindReplaceData* data);
         int Replace(cbStyledTextCtrl* control, cbFindReplaceData* data);
         int ReplaceInFiles(cbFindReplaceData* data);
+        bool IsHeaderSource(const wxFileName& testedFileName, const wxFileName& activeFileName, FileType ftActive);
+        wxFileName FindHeaderSource(const wxArrayString& fileArray, const wxFileName& activeFileName, bool& isCandidate);
 
         wxAuiNotebook* m_pNotebook;
         cbFindReplaceData* m_LastFindReplaceData;
Index: src/include/projectmanager.h
===================================================================
--- src/include/projectmanager.h	(revision 5986)
+++ src/include/projectmanager.h	(working copy)
@@ -65,11 +65,6 @@
         static void CreateMenu(wxMenuBar* menuBar);
         /// Application menu removal. Called by the application only.
         void ReleaseMenu(wxMenuBar* menuBar);
-        /** Try to locate the header for a given source file and vice versa
-          * @param fname The file to locate
-          * @return The path to the file in question; wxEmptyString if not found
-          */
-        wxString GetHeaderSource(const wxFileName &fname);
         /** Retrieve the default path for new projects.
           * @return The default path for new projects. Contains trailing path separator.
           * @note This might be empty if not configured before...
Index: src/sdk/projectmanager.cpp
===================================================================
--- src/sdk/projectmanager.cpp	(revision 5986)
+++ src/sdk/projectmanager.cpp	(working copy)
@@ -432,69 +432,6 @@
 {
 }
 
-wxString ProjectManager::GetHeaderSource(const wxFileName &fname)
-{
-    const FilesGroupsAndMasks* fg = GetFilesGroupsAndMasks();
-    if ( !fg )
-        return wxEmptyString;
-
-    FileType ft = FileTypeOf(fname.GetFullName());
-
-    // If header file provided, return source (if found)
-    if (ft == ftHeader)
-    {
-        for ( unsigned int i = 0; i != fg->GetGroupsCount(); ++i )
-        {
-            if ( fg->GetGroupName(i) == _("Sources") )
-            {
-                wxStringTokenizer tkz( fg->GetFileMasks(i), _T(";") );
-                while ( tkz.HasMoreTokens() )
-                {
-                    wxString token = tkz.GetNextToken();
-                    wxString ext;
-                    if ( token.StartsWith( _("*."), &ext ) )
-                    {
-                        wxFileName fn(fname);
-                        fn.SetExt(ext);
-//                        Manager::Get()->GetLogManager()->DebugLog(F(_T("Trying to locate '%s'."), fn.GetFullPath().c_str()));
-                        if (fn.FileExists())
-                            return fn.GetFullPath();
-                    }
-                }
-                break;
-            }
-        }
-    }
-    // If source file provided, return header (if found)
-    else if (ft == ftSource)
-    {
-        for ( unsigned int i = 0; i != fg->GetGroupsCount(); ++i )
-        {
-            if ( fg->GetGroupName(i) == _("Headers") )
-            {
-                wxStringTokenizer tkz( fg->GetFileMasks(i), _T(";") );
-                while ( tkz.HasMoreTokens() )
-                {
-                    wxString token = tkz.GetNextToken();
-                    wxString ext;
-                    if ( token.StartsWith( _("*."), &ext ) )
-                    {
-                        wxFileName fn(fname);
-                        fn.SetExt(ext);
-//                        Manager::Get()->GetLogManager()->DebugLog(F(_T("Trying to locate '%s'."), fn.GetFullPath().c_str()));
-                        if (fn.FileExists())
-                            return fn.GetFullPath();
-                    }
-                }
-                break;
-            }
-        }
-    }
-
-//    Manager::Get()->GetLogManager()->DebugLog(F(_T("Cannot locate opposite of '%s'."), fname.GetFullPath().c_str()));
-    return wxEmptyString;
-}
-
 wxString ProjectManager::GetDefaultPath()
 {
     wxString path = Manager::Get()->GetConfigManager(_T("project_manager"))->Read(_T("default_path"), wxEmptyString);
