1 /**********************************************************************
2 
3   Audacity: A Digital Audio Editor
4 
5   FileDialog.cpp
6 
7   Leland Lucius
8 
9 *******************************************************************//**
10 
11 \class FileDialog
12 \brief Dialog used to present platform specific "Save As" dialog with
13 custom controls.
14 
15 *//*******************************************************************/
16 
17 #include "FileDialog.h"
18 
FileDialogBase()19 FileDialogBase::FileDialogBase()
20 {
21    m_creator = NULL;
22    m_userdata = 0;
23 }
24 
HasUserPaneCreator() const25 bool FileDialogBase::HasUserPaneCreator() const
26 {
27    return m_creator != NULL;
28 }
29 
SetUserPaneCreator(UserPaneCreatorFunction creator,wxUIntPtr userdata)30 void FileDialogBase::SetUserPaneCreator(UserPaneCreatorFunction creator, wxUIntPtr userdata)
31 {
32    m_creator = creator;
33    m_userdata = userdata;
34 }
35 
CreateUserPane(wxWindow * parent)36 void FileDialogBase::CreateUserPane(wxWindow *parent)
37 {
38    if (m_creator)
39    {
40       (*m_creator)(parent, m_userdata);
41    }
42 }
43 
44 //
45 // Copied from wx 3.0.2 and modified to support additional features
46 //
47 /////////////////////////////////////////////////////////////////////////////
48 // Name:        src/common/fldlgcmn.cpp
49 // Purpose:     wxFileDialog common functions
50 // Author:      John Labenski
51 // Modified by: Leland Lucius
52 // Created:     14.06.03 (extracted from src/*/filedlg.cpp)
53 // Copyright:   (c) Robert Roebling
54 // Licence:     wxWindows licence
55 /////////////////////////////////////////////////////////////////////////////
56 
57 //----------------------------------------------------------------------------
58 // FileDialog convenience functions
59 //----------------------------------------------------------------------------
60 
FileSelector(const wxString & title,const wxString & defaultDir,const wxString & defaultFileName,const wxString & defaultExtension,const wxString & filter,int flags,wxWindow * parent,int x,int y)61 wxString FileSelector(const wxString& title,
62                       const wxString& defaultDir,
63                       const wxString& defaultFileName,
64                       const wxString& defaultExtension,
65                       const wxString& filter,
66                       int flags,
67                       wxWindow *parent,
68                       int x, int y)
69 {
70     // The defaultExtension, if non-empty, is
71     // appended to the filename if the user fails to type an extension. The new
72     // implementation (taken from FileSelectorEx) appends the extension
73     // automatically, by looking at the filter specification. In fact this
74     // should be better than the native Microsoft implementation because
75     // Windows only allows *one* default extension, whereas here we do the
76     // right thing depending on the filter the user has chosen.
77 
78     // If there's a default extension specified but no filter, we create a
79     // suitable filter.
80 
81     wxString filter2;
82     if ( !defaultExtension.empty() && filter.empty() )
83         filter2 = wxString(wxT("*.")) + defaultExtension;
84     else if ( !filter.empty() )
85         filter2 = filter;
86 
87     FileDialog fileDialog(parent, title, defaultDir,
88                             defaultFileName, filter2,
89                             flags, wxPoint(x, y));
90 
91     // if filter is of form "All files (*)|*|..." set correct filter index
92     if ( !defaultExtension.empty() && filter2.find(wxT('|')) != wxString::npos )
93     {
94         int filterIndex = 0;
95 
96         wxArrayString descriptions, filters;
97         // don't care about errors, handled already by FileDialog
98         (void)wxParseCommonDialogsFilter(filter2, descriptions, filters);
99         for (size_t n=0; n<filters.GetCount(); n++)
100         {
101             if (filters[n].Contains(defaultExtension))
102             {
103                 filterIndex = n;
104                 break;
105             }
106         }
107 
108         if (filterIndex > 0)
109             fileDialog.SetFilterIndex(filterIndex);
110     }
111 
112     wxString filename;
113     if ( fileDialog.ShowModal() == wxID_OK )
114     {
115         filename = fileDialog.GetPath();
116     }
117 
118     return filename;
119 }
120 
121 //----------------------------------------------------------------------------
122 // FileSelectorEx
123 //----------------------------------------------------------------------------
124 
FileSelectorEx(const wxString & title,const wxString & defaultDir,const wxString & defaultFileName,int * defaultFilterIndex,const wxString & filter,int flags,wxWindow * parent,int x,int y)125 wxString FileSelectorEx(const wxString& title,
126                         const wxString& defaultDir,
127                         const wxString& defaultFileName,
128                         int*            defaultFilterIndex,
129                         const wxString& filter,
130                         int             flags,
131                         wxWindow*       parent,
132                         int             x,
133                         int             y)
134 
135 {
136     FileDialog fileDialog(parent,
137                             title,
138                             defaultDir,
139                             defaultFileName,
140                             filter,
141                             flags, wxPoint(x, y));
142 
143     wxString filename;
144     if ( fileDialog.ShowModal() == wxID_OK )
145     {
146         if ( defaultFilterIndex )
147             *defaultFilterIndex = fileDialog.GetFilterIndex();
148 
149         filename = fileDialog.GetPath();
150     }
151 
152     return filename;
153 }
154 
155