+// 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)|*.jpeg"
+
+int WXDLLEXPORT wxParseCommonDialogsFilter(const wxString& filterStr, wxArrayString& descriptions, wxArrayString& filters)
+{
+ descriptions.Clear();
+ filters.Clear();
+
+ wxString str(filterStr);
+
+ wxString description, filter;
+ int pos = 0;
+ while( pos != wxNOT_FOUND )
+ {
+ pos = str.Find(wxT('|'));
+ if ( pos == wxNOT_FOUND )
+ {
+ // if there are no '|'s at all in the string just take the entire
+ // string as filter and make description empty for later autocompletion
+ if ( filters.IsEmpty() )
+ {
+ descriptions.Add(wxEmptyString);
+ filters.Add(filterStr);
+ }
+ else
+ {
+ wxFAIL_MSG( _T("missing '|' in the wildcard string!") );
+ }
+
+ break;
+ }
+
+ description = str.Left(pos);
+ str = str.Mid(pos + 1);
+ pos = str.Find(wxT('|'));
+ if ( pos == wxNOT_FOUND )
+ {
+ filter = str;
+ }
+ else
+ {
+ filter = str.Left(pos);
+ str = str.Mid(pos + 1);
+ }
+
+ descriptions.Add(description);
+ 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].empty() && !filters[j].empty() )
+ {
+ descriptions[j].Printf(_("Files (%s)"), filters[j].c_str());
+ }
+ }
+
+ return filters.GetCount();
+}
+
+