From daf3246330c8ae7b4536cd1f6deef733a405cecd Mon Sep 17 00:00:00 2001
From: =?utf8?q?W=C5=82odzimierz=20Skiba?= <abx@abx.art.pl>
Date: Tue, 22 Jun 2004 16:58:27 +0000
Subject: [PATCH] wxParseWildcard renamed to wxParseCommonDialogsFilter. Its
 usage is extended from generic dialogs to MSW, Motif and other incoming
 ports.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@27941 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/changes.txt            |  5 +--
 docs/latex/wx/function.tex  |  8 +++--
 include/wx/filefn.h         |  2 +-
 samples/dialogs/dialogs.cpp |  8 ++---
 src/common/filefn.cpp       | 64 ++++++++++++++++++++++++++++++++++---
 src/common/fldlgcmn.cpp     |  4 +--
 src/generic/dirctrlg.cpp    |  6 ++--
 src/generic/filedlgg.cpp    |  2 +-
 src/msw/filedlg.cpp         | 35 ++++++--------------
 wxPython/src/_functions.i   |  2 +-
 10 files changed, 90 insertions(+), 46 deletions(-)

diff --git a/docs/changes.txt b/docs/changes.txt
index cb0f3d7627..8c06d25629 100644
--- a/docs/changes.txt
+++ b/docs/changes.txt
@@ -55,8 +55,9 @@ INCOMPATIBLE CHANGES SINCE 2.4.x
   simply switch to using const methods.
 - EVT_XXX macros are now type-safe; code that uses wrong type for event
   handler's argument will no longer compile.
-- Identical functionality of wxFileDialog::ParseWildcard and 
-  wxGenericDirCtrl::ParseFilter is now accessible in ::wxParseWildcard
+- Identical functionality of wxFileDialog::ParseWildcard,
+  wxGenericDirCtrl::ParseFilter, Motif and MSW parsing native dialogs
+  is now accessible in ::wxParseCommonDialogsFilter
 
 
 
diff --git a/docs/latex/wx/function.tex b/docs/latex/wx/function.tex
index 6a95f696cc..d31df54da0 100644
--- a/docs/latex/wx/function.tex
+++ b/docs/latex/wx/function.tex
@@ -181,7 +181,7 @@ the corresponding topic.
 \helpref{wxNow}{wxnow}\\
 \helpref{wxOnAssert}{wxonassert}\\
 \helpref{wxOpenClipboard}{wxopenclipboard}\\
-\helpref{wxParseWildcard}{wxparsewildcard}\\
+\helpref{wxParseCommonDialogsFilter}{wxparsecommondialogsfilter}\\
 \helpref{wxPathOnly}{wxpathonly}\\
 \helpref{wxPostDelete}{wxpostdelete}\\
 \helpref{wxPostEvent}{wxpostevent}\\
@@ -1094,13 +1094,15 @@ Makes the directory \arg{dir}, returning true if successful.
 supported (Unix) and doesn't have effect for the other ones.
 
 
-\membersection{::wxParseWildcard}\label{wxparsewildcard}
+\membersection{::wxParseCommonDialogsFilter}\label{wxparsecommondialogsfilter}
 
-\func{int}{wxParseWildcard}{\param{const wxString\& }{wildCard}, \param{wxArrayString\& }{descriptions}, \param{wxArrayString\& }{filters}}
+\func{int}{wxParseCommonDialogsFilter}{\param{const wxString\& }{wildCard}, \param{wxArrayString\& }{descriptions}, \param{wxArrayString\& }{filters}}
 
 Parses the \arg{wildCard}, returning the number of filters.
 Returns 0 if none or if there's a problem.
 The arrays will contain an equal number of items found before the error.
+On platforms where native dialogs handle only one filter per entry,
+entries in arrays are automatically adjusted.
 \arg{wildCard} is in the form:
 \begin{verbatim}
  "All files (*)|*|Image Files (*.jpeg *.png)|*.jpg;*.png"
diff --git a/include/wx/filefn.h b/include/wx/filefn.h
index 03fa8300d2..0c1bfcbe37 100644
--- a/include/wx/filefn.h
+++ b/include/wx/filefn.h
@@ -359,7 +359,7 @@ WXDLLIMPEXP_BASE time_t wxFileModificationTime(const wxString& filename);
 // The arrays will contain an equal number of items found before the error.
 // wildCard is in the form:
 // "All files (*)|*|Image Files (*.jpeg *.png)|*.jpg;*.png"
-WXDLLIMPEXP_BASE int wxParseWildcard(const wxString& wildCard, wxArrayString& descriptions, wxArrayString& filters);
+WXDLLIMPEXP_BASE int wxParseCommonDialogsFilter(const wxString& wildCard, wxArrayString& descriptions, wxArrayString& filters);
 
 // ----------------------------------------------------------------------------
 // classes
diff --git a/samples/dialogs/dialogs.cpp b/samples/dialogs/dialogs.cpp
index d0e3f37835..598fe7f515 100644
--- a/samples/dialogs/dialogs.cpp
+++ b/samples/dialogs/dialogs.cpp
@@ -647,7 +647,7 @@ void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
 #ifdef __WXMOTIF__
                     _T("C++ files (*.cpp)|*.cpp")
 #else
-                    _T("C++ files (*.h;*.cpp)|*.h;*.cpp")
+                    _T("C++ files (*.cpp;*.h)|*.cpp;*.h")
 #endif
                  );
 
@@ -698,7 +698,7 @@ void MyFrame::FilesOpen(wxCommandEvent& WXUNUSED(event) )
 #ifdef __WXMOTIF__
                     _T("C++ files (*.cpp)|*.cpp");
 #else
-                    _T("All files (*.*)|*.*|C++ files (*.h;*.cpp)|*.h;*.cpp");
+                    _T("All files (*.*)|*.*|C++ files (*.cpp;*.h)|*.cpp;*.h");
 #endif
     wxFileDialog dialog(this, _T("Testing open multiple file dialog"),
                         wxEmptyString, wxEmptyString, wildcards,
@@ -756,7 +756,7 @@ void MyFrame::FileOpenGeneric(wxCommandEvent& WXUNUSED(event) )
                     _T("Testing open file dialog"),
                     wxEmptyString,
                     wxEmptyString,
-                    _T("C++ files (*.h;*.cpp)|*.h;*.cpp")
+                    _T("C++ files (*.cpp;*.h)|*.cpp;*.h")
                  );
 
     dialog.SetDirectory(wxGetHomeDir());
@@ -777,7 +777,7 @@ void MyFrame::FileOpenGeneric(wxCommandEvent& WXUNUSED(event) )
 
 void MyFrame::FilesOpenGeneric(wxCommandEvent& WXUNUSED(event) )
 {
-    wxString wildcards = _T("All files (*.*)|*.*|C++ files (*.h;*.cpp)|*.h;*.cpp");
+    wxString wildcards = _T("All files (*.*)|*.*|C++ files (*.cpp;*.h)|*.cpp;*.h");
     wxGenericFileDialog dialog(this, _T("Testing open multiple file dialog"),
                         wxEmptyString, wxEmptyString, wildcards,
                         wxMULTIPLE);
diff --git a/src/common/filefn.cpp b/src/common/filefn.cpp
index c98cb1d890..3c0a2f5285 100644
--- a/src/common/filefn.cpp
+++ b/src/common/filefn.cpp
@@ -1838,9 +1838,9 @@ time_t WXDLLEXPORT wxFileModificationTime(const wxString& filename)
 
 // Parses the filterStr, returning the number of filters.
 // Returns 0 if none or if there's a problem.
-// filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
+// filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpeg"
 
-int WXDLLEXPORT wxParseWildcard(const wxString& filterStr, wxArrayString& descriptions, wxArrayString& filters)
+int WXDLLEXPORT wxParseCommonDialogsFilter(const wxString& filterStr, wxArrayString& descriptions, wxArrayString& filters)
 {
     descriptions.Clear();
     filters.Clear();
@@ -1855,10 +1855,10 @@ int WXDLLEXPORT wxParseWildcard(const wxString& filterStr, wxArrayString& descri
         if ( pos == wxNOT_FOUND )
         {
             // if there are no '|'s at all in the string just take the entire
-            // string as filter
+            // string as filter and make description empty for later autocompletion
             if ( filters.IsEmpty() )
             {
-                descriptions.Add(filterStr);
+                descriptions.Add(wxEmptyString);
                 filters.Add(filterStr);
             }
             else
@@ -1886,6 +1886,62 @@ int WXDLLEXPORT wxParseWildcard(const wxString& filterStr, wxArrayString& descri
         filters.Add(filter);
     }
 
+#if defined(__WXMOTIF__)
+    // split it so there is one wildcard per entry
+    for( size_t i = 0 ; i < descriptions.GetCount() ; i++ )
+    {
+        pos = filters[i].Find(wxT(';'));
+        if (pos != wxNOT_FOUND)
+        {
+            // first split only filters
+            descriptions.Insert(descriptions[i],i+1);
+            filters.Insert(filters[i].Mid(pos+1),i+1);
+            filters[i]=filters[i].Left(pos);
+
+            // autoreplace new filter in description with pattern:
+            //     C/C++ Files(*.cpp;*.c;*.h)|*.cpp;*.c;*.h
+            // cause split into:
+            //     C/C++ Files(*.cpp)|*.cpp
+            //     C/C++ Files(*.c;*.h)|*.c;*.h
+            // and next iteration cause another split into:
+            //     C/C++ Files(*.cpp)|*.cpp
+            //     C/C++ Files(*.c)|*.c
+            //     C/C++ Files(*.h)|*.h
+            for ( size_t k=i;k<i+2;k++ )
+            {
+                pos = descriptions[k].Find(filters[k]);
+                if (pos != wxNOT_FOUND)
+                {
+                    wxString before = descriptions[k].Left(pos);
+                    wxString after = descriptions[k].Mid(pos+filters[k].Len());
+                    pos = before.Find(_T('('),true);
+                    if (pos>before.Find(_T(')'),true))
+                    {
+                        before = before.Left(pos+1);
+                        before << filters[k];
+                        pos = after.Find(_T(')'));
+                        int pos1 = after.Find(_T('('));
+                        if (pos != wxNOT_FOUND && (pos<pos1 || pos1==wxNOT_FOUND))
+                        {
+                            before << after.Mid(pos);
+                            descriptions[k] = before;
+                        }
+                    }
+                }
+            }
+        }
+    }
+#endif
+
+    // autocompletion
+    for( size_t j = 0 ; j < descriptions.GetCount() ; j++ )
+    {
+        if ( descriptions[j] == wxEmptyString && filters[j] != wxEmptyString )
+        {
+            descriptions[j].Printf(_("Files (%s)"), filters[j].c_str());
+        }
+    }
+
     return filters.GetCount();
 }
 
diff --git a/src/common/fldlgcmn.cpp b/src/common/fldlgcmn.cpp
index df96a83102..119665674f 100644
--- a/src/common/fldlgcmn.cpp
+++ b/src/common/fldlgcmn.cpp
@@ -88,7 +88,7 @@ int wxFileDialogBase::ParseWildcard(const wxString& filterStr,
                                     wxArrayString& descriptions,
                                     wxArrayString& filters)
 {
-    return ::wxParseWildcard(filterStr, descriptions, filters);
+    return ::wxParseCommonDialogsFilter(filterStr, descriptions, filters);
 }
 #endif // WXWIN_COMPATIBILITY_2_4
 
@@ -175,7 +175,7 @@ wxString wxFileSelector(const wxChar *title,
 
         wxArrayString descriptions, filters;
         // don't care about errors, handled already by wxFileDialog
-        (void)wxParseWildcard(filter2, descriptions, filters);
+        (void)wxParseCommonDialogsFilter(filter2, descriptions, filters);
         for (size_t n=0; n<filters.GetCount(); n++)
                 {
             if (filters[n].Contains(defaultExtension))
diff --git a/src/generic/dirctrlg.cpp b/src/generic/dirctrlg.cpp
index 22f0e418a8..32c1cdc692 100644
--- a/src/generic/dirctrlg.cpp
+++ b/src/generic/dirctrlg.cpp
@@ -1137,7 +1137,7 @@ void wxGenericDirCtrl::SetFilter(const wxString& filter)
 bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description)
 {
     wxArrayString filters, descriptions;
-    int count = wxParseWildcard(filterStr, filters, descriptions);
+    int count = wxParseCommonDialogsFilter(filterStr, descriptions, filters);
     if (count > 0 && n < count)
     {
         filter = filters[n];
@@ -1154,7 +1154,7 @@ bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxStrin
 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
 int wxGenericDirCtrl::ParseFilter(const wxString& filterStr, wxArrayString& filters, wxArrayString& descriptions)
 {
-    return wxParseWildcard(filterStr, descriptions, filters );
+    return wxParseCommonDialogsFilter(filterStr, descriptions, filters );
 }
 #endif // WXWIN_COMPATIBILITY_2_4
 
@@ -1266,7 +1266,7 @@ void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilt
 {
     Clear();
     wxArrayString descriptions, filters;
-    size_t n = (size_t) wxParseWildcard(filter, filters, descriptions);
+    size_t n = (size_t) wxParseCommonDialogsFilter(filter, filters, descriptions);
 
     if (n > 0 && defaultFilter < (int) n)
     {
diff --git a/src/generic/filedlgg.cpp b/src/generic/filedlgg.cpp
index c457e404e7..e3af623719 100644
--- a/src/generic/filedlgg.cpp
+++ b/src/generic/filedlgg.cpp
@@ -901,7 +901,7 @@ wxGenericFileDialog::wxGenericFileDialog(wxWindow *parent,
 
     // interpret wildcards
     wxArrayString wildDescriptions, wildFilters;
-    if ( !wxParseWildcard(m_wildCard, wildDescriptions, wildFilters) )
+    if ( !wxParseCommonDialogsFilter(m_wildCard, wildDescriptions, wildFilters) )
     {
         wxFAIL_MSG( wxT("Wrong file type description") );
     }
diff --git a/src/msw/filedlg.cpp b/src/msw/filedlg.cpp
index 775affb082..de26b29ef6 100644
--- a/src/msw/filedlg.cpp
+++ b/src/msw/filedlg.cpp
@@ -230,37 +230,22 @@ int wxFileDialog::ShowModal()
 
     of.Flags             = msw_flags;
 
+    wxArrayString wildDescriptions, wildFilters;
 
-    //=== Like Alejandro Sierra's wildcard modification >>===================
-    /*
-       In wxFileSelector you can put, instead of a single wild_card,
-       pairs of strings separated by '|'.
-       The first string is a description, and the
-       second is the wild card. You can put any number of pairs.
+    size_t items = wxParseCommonDialogsFilter(m_wildCard, wildDescriptions, wildFilters);
 
-       eg.  "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
+    wxASSERT_MSG( items > 0 , _T("empty wildcard list") );
 
-       If you put a single wild card, it works as before the modification.
-     */
-    //=======================================================================
-
-    wxString theFilter;
-    if ( wxStrlen(m_wildCard) == 0 )
-        theFilter = wxString(wxT("*.*"));
-    else
-        theFilter = m_wildCard ;
     wxString filterBuffer;
 
-    if ( !wxStrchr( theFilter, wxT('|') ) ) {    // only one filter ==> default text
-        filterBuffer.Printf(_("Files (%s)|%s"),
-                            theFilter.c_str(), theFilter.c_str());
-    }
-    else {                                // more then one filter
-        filterBuffer = theFilter;
-
+    for (i = 0; i < items ; i++)
+    {
+        filterBuffer += wildDescriptions[i];
+        filterBuffer += wxT("|");
+        filterBuffer += wildFilters[i];
+        filterBuffer += wxT("|");
     }
 
-    filterBuffer += wxT("|");
     // Replace | with \0
     for (i = 0; i < filterBuffer.Len(); i++ ) {
         if ( filterBuffer.GetChar(i) == wxT('|') ) {
@@ -268,7 +253,7 @@ int wxFileDialog::ShowModal()
         }
     }
 
-    of.lpstrFilter  = (LPTSTR)(const wxChar *)filterBuffer;
+    of.lpstrFilter  = (LPTSTR)filterBuffer.c_str();
     of.nFilterIndex = m_filterIndex + 1;
 
     //=== Setting defaultFileName >>=========================================
diff --git a/wxPython/src/_functions.i b/wxPython/src/_functions.i
index 60285dafde..efc60338f3 100644
--- a/wxPython/src/_functions.i
+++ b/wxPython/src/_functions.i
@@ -57,7 +57,7 @@ wxString wxGetOsDescription();
 // // The arrays will contain an equal number of items found before the error.
 // // wildCard is in the form:
 // // "All files (*)|*|Image Files (*.jpeg *.png)|*.jpg;*.png"
-// int wxParseWildcard(const wxString& wildCard, wxArrayString& descriptions, wxArrayString& filters);
+// int wxParseCommonDialogsFilter(const wxString& wildCard, wxArrayString& descriptions, wxArrayString& filters);
 
 #if defined(__WXMSW__) || defined(__WXMAC__)
 long wxGetFreeMemory();
-- 
2.50.0

