]>
Commit | Line | Data |
---|---|---|
0f9b48d1 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/cocoa/filedlg.mm | |
3 | // Purpose: wxFileDialog for wxCocoa | |
4 | // Author: Ryan Norton | |
5 | // Modified by: | |
6 | // Created: 2004-10-02 | |
7 | // RCS-ID: $Id: filedlg.mm 40007 2006-07-05 13:10:46Z SC $ | |
8 | // Copyright: (c) Ryan Norton | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #if wxUSE_FILEDLG | |
24 | ||
25 | #include "wx/filedlg.h" | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/msgdlg.h" | |
29 | #include "wx/app.h" | |
30 | #endif | |
31 | ||
32 | #include "wx/filename.h" | |
33 | ||
34 | #include "wx/osx/private.h" | |
35 | ||
36 | // ============================================================================ | |
37 | // implementation | |
38 | // ============================================================================ | |
39 | ||
4dd9fdf8 SC |
40 | // Open Items: |
41 | // - support for old style MacOS creator / type combos | |
42 | // - parameter support for descending into packages as directories (setTreatsFilePackagesAsDirectories) | |
43 | ||
0f9b48d1 SC |
44 | IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase) |
45 | ||
46 | wxFileDialog::wxFileDialog( | |
47 | wxWindow *parent, const wxString& message, | |
48 | const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard, | |
49 | long style, const wxPoint& pos, const wxSize& sz, const wxString& name) | |
50 | : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name) | |
51 | { | |
52 | } | |
53 | ||
54 | ||
55 | NSArray* GetTypesFromFilter( const wxString filter ) | |
56 | { | |
57 | NSMutableArray* types = nil; | |
58 | if ( !filter.empty() ) | |
59 | { | |
60 | wxArrayString names ; | |
61 | wxArrayString extensions; | |
bd365871 | 62 | |
0f9b48d1 SC |
63 | wxString filter2(filter) ; |
64 | int filterIndex = 0; | |
65 | bool isName = true ; | |
66 | wxString current ; | |
67 | ||
68 | for ( unsigned int i = 0; i < filter2.length() ; i++ ) | |
69 | { | |
70 | if ( filter2.GetChar(i) == wxT('|') ) | |
71 | { | |
72 | if ( isName ) | |
73 | { | |
74 | names.Add( current ) ; | |
75 | } | |
76 | else | |
77 | { | |
78 | extensions.Add( current ) ; | |
79 | ++filterIndex ; | |
80 | } | |
81 | ||
82 | isName = !isName ; | |
83 | current = wxEmptyString ; | |
84 | } | |
85 | else | |
86 | { | |
87 | current += filter2.GetChar(i) ; | |
88 | } | |
89 | } | |
90 | // we allow for compatibility reason to have a single filter expression (like *.*) without | |
91 | // an explanatory text, in that case the first part is name and extension at the same time | |
92 | ||
93 | wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ; | |
94 | if ( current.empty() ) | |
95 | extensions.Add( names[filterIndex] ) ; | |
96 | else | |
97 | extensions.Add( current ) ; | |
98 | if ( filterIndex == 0 || isName ) | |
99 | names.Add( current ) ; | |
100 | ||
101 | ++filterIndex ; | |
102 | ||
103 | const size_t extCount = extensions.GetCount(); | |
104 | for ( size_t i = 0 ; i < extCount; i++ ) | |
105 | { | |
106 | wxString extension = extensions[i]; | |
107 | ||
108 | // Remove leading '*' | |
109 | if (extension.length() && (extension.GetChar(0) == '*')) | |
110 | extension = extension.Mid( 1 ); | |
111 | ||
112 | // Remove leading '.' | |
113 | if (extension.length() && (extension.GetChar(0) == '.')) | |
114 | extension = extension.Mid( 1 ); | |
115 | ||
bd365871 | 116 | if ( extension.IsEmpty() ) |
0f9b48d1 SC |
117 | { |
118 | if ( types != nil ) | |
119 | [types release]; | |
120 | return nil; | |
121 | } | |
bd365871 FM |
122 | |
123 | ||
0f9b48d1 SC |
124 | if ( types == nil ) |
125 | types = [[NSMutableArray alloc] init]; | |
bd365871 | 126 | |
0f9b48d1 SC |
127 | wxCFStringRef cfext(extension); |
128 | [types addObject: (NSString*)cfext.AsNSString() ]; | |
bd365871 | 129 | #if 0 |
4dd9fdf8 | 130 | // add support for classic fileType / creator here |
0f9b48d1 | 131 | wxUint32 fileType, creator; |
0f9b48d1 SC |
132 | // extension -> mactypes |
133 | #endif | |
134 | } | |
135 | } | |
136 | return types; | |
137 | } | |
138 | ||
139 | int wxFileDialog::ShowModal() | |
140 | { | |
141 | int result = wxID_CANCEL; | |
bd365871 | 142 | |
0f9b48d1 | 143 | NSSavePanel *panel = nil; |
bd365871 | 144 | |
0f9b48d1 | 145 | wxCFStringRef cf( m_message ); |
bd365871 | 146 | |
1b447793 | 147 | wxCFStringRef dir( m_dir ); |
0f9b48d1 | 148 | wxCFStringRef file( m_fileName ); |
bd365871 | 149 | |
0f9b48d1 SC |
150 | m_path = wxEmptyString; |
151 | m_fileNames.Clear(); | |
bd365871 | 152 | |
0f9b48d1 SC |
153 | if (HasFlag(wxFD_SAVE)) |
154 | { | |
155 | NSSavePanel* sPanel = [NSSavePanel savePanel]; | |
156 | // makes things more convenient: | |
157 | [sPanel setCanCreateDirectories:YES]; | |
158 | [sPanel setMessage:cf.AsNSString()]; | |
4dd9fdf8 SC |
159 | // if we should be able to descend into pacakges we must somehow |
160 | // be able to pass this in | |
0f9b48d1 | 161 | [sPanel setTreatsFilePackagesAsDirectories:NO]; |
4dd9fdf8 | 162 | [sPanel setCanSelectHiddenExtension:YES]; |
bd365871 | 163 | |
0f9b48d1 SC |
164 | if ( HasFlag(wxFD_OVERWRITE_PROMPT) ) |
165 | { | |
166 | } | |
167 | ||
168 | if ( [sPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() ] == NSOKButton ) | |
169 | { | |
170 | panel = sPanel; | |
171 | result = wxID_OK; | |
bd365871 | 172 | |
0f9b48d1 SC |
173 | wxCFStringRef filename( [[sPanel filename] retain] ); |
174 | ||
175 | m_path = filename.AsString(); | |
176 | m_fileName = wxFileNameFromPath(m_path); | |
177 | m_dir = wxPathOnly( m_path ); | |
178 | } | |
179 | } | |
180 | else | |
181 | { | |
182 | NSArray* types = GetTypesFromFilter( m_wildCard ) ; | |
183 | NSOpenPanel* oPanel = [NSOpenPanel openPanel]; | |
184 | [oPanel setTreatsFilePackagesAsDirectories:NO]; | |
185 | [oPanel setCanChooseDirectories:NO]; | |
186 | [oPanel setResolvesAliases:YES]; | |
187 | [oPanel setCanChooseFiles:YES]; | |
188 | [oPanel setMessage:cf.AsNSString()]; | |
bd365871 | 189 | |
0f9b48d1 SC |
190 | if ( [oPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() types:types] == NSOKButton ) |
191 | { | |
192 | panel = oPanel; | |
193 | result = wxID_OK; | |
194 | NSArray* filenames = [oPanel filenames]; | |
195 | for ( size_t i = 0 ; i < [filenames count] ; ++ i ) | |
196 | { | |
197 | wxCFStringRef filename( [(NSString*) [filenames objectAtIndex:i] retain] ); | |
198 | wxString fnstr = filename.AsString(); | |
199 | m_paths.Add( fnstr ); | |
200 | m_fileNames.Add( wxFileNameFromPath(fnstr) ); | |
201 | if ( i == 0 ) | |
202 | { | |
203 | m_path = fnstr; | |
204 | m_fileName = wxFileNameFromPath(fnstr); | |
205 | m_dir = wxPathOnly( fnstr ); | |
206 | } | |
207 | } | |
208 | } | |
209 | if ( types != nil ) | |
210 | [types release]; | |
211 | } | |
bd365871 | 212 | |
0f9b48d1 SC |
213 | return result; |
214 | } | |
215 | ||
0f9b48d1 | 216 | #endif // wxUSE_FILEDLG |