wxFD_* constants without 2.6 compatibility (heavily extended and modified patch ...
[wxWidgets.git] / src / cocoa / filedlg.mm
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$
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 #ifndef WX_PRECOMP
26     #include "wx/msgdlg.h"
27     #include "wx/filedlg.h"
28     #include "wx/app.h"
29 #endif
30
31 #include "wx/filename.h"
32
33 #include "wx/cocoa/autorelease.h"
34 #include "wx/cocoa/string.h"
35
36 #import <AppKit/NSOpenPanel.h>
37 #import <AppKit/NSSavePanel.h>
38
39 #import <Foundation/NSArray.h>
40 // ============================================================================
41 // implementation
42 // ============================================================================
43
44 IMPLEMENT_CLASS(wxCocoaFileDialog, wxFileDialogBase)
45
46 // ----------------------------------------------------------------------------
47 // wxFileDialog
48 // ----------------------------------------------------------------------------
49
50 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
51         const wxString& defaultDir, const wxString& defaultFileName,
52         const wxString& wildCard, long style, const wxPoint& pos)
53 :   wxFileDialogBase(parent, message, defaultDir, defaultFileName,
54         wildCard, style, pos)
55 {
56     wxTopLevelWindows.Append(this);
57
58     wxASSERT(CreateBase(parent,wxID_ANY,pos,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
59
60     if ( parent )
61         parent->AddChild(this);
62
63     m_cocoaNSWindow = nil;
64     m_cocoaNSView = nil;
65
66     //Init the wildcard array
67     m_wildcards = [[NSMutableArray alloc] initWithCapacity:0];
68
69     //If the user requests to save - use a NSSavePanel
70     //else use a NSOpenPanel
71     if (m_dialogStyle & wxFD_SAVE)
72     {
73         SetNSPanel([NSSavePanel savePanel]);
74
75         [GetNSSavePanel() setTitle:wxNSStringWithWxString(message)];
76
77         [GetNSSavePanel() setPrompt:@"Save"];
78         [GetNSSavePanel() setTreatsFilePackagesAsDirectories:YES];
79         [GetNSSavePanel() setCanSelectHiddenExtension:YES];
80
81 //        Cached as per-app in obj-c
82 //        [GetNSSavePanel() setExtensionHidden:YES];
83
84         //
85         // NB:  Note that only Panther supports wildcards
86         // with save dialogs - not that wildcards in save
87         // dialogs are all that useful, anyway :)
88         //
89     }
90     else //m_dialogStyle & wxFD_OPEN
91     {
92         SetNSPanel([NSOpenPanel openPanel]);
93         [m_cocoaNSWindow setTitle:wxNSStringWithWxString(message)];
94
95         [(NSOpenPanel*)m_cocoaNSWindow setAllowsMultipleSelection:(m_dialogStyle & wxFD_MULTIPLE)];
96         [(NSOpenPanel*)m_cocoaNSWindow setResolvesAliases:YES];
97         [(NSOpenPanel*)m_cocoaNSWindow setCanChooseFiles:YES];
98         [(NSOpenPanel*)m_cocoaNSWindow setCanChooseDirectories:NO];
99         [GetNSSavePanel() setPrompt:@"Open"];
100
101         //convert wildcards - open panel only takes file extensions -
102         //no actual wildcards here :)
103         size_t lastwcpos = 0;
104         bool bDescription = true;
105         size_t i;
106         for(i = wildCard.find('|');
107                 i != wxString::npos;
108                 i = wildCard.find('|', lastwcpos+1))
109         {
110             size_t oldi = i;
111
112             if(!bDescription)
113             {
114                 bDescription = !bDescription;
115
116                 //work backwards looking for a period
117                 while(i != lastwcpos && wildCard[--i] != '.') {}
118
119                 if(i == lastwcpos)
120                 {
121                     //no extension - can't use this wildcard
122                     lastwcpos = oldi;
123                     continue;
124                 }
125
126                 [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))];
127             }
128             else
129                 bDescription = !bDescription;
130             lastwcpos = oldi;
131         }
132
133         if (!bDescription)
134         {
135             //get last wildcard
136             size_t oldi = wildCard.length();
137             i = oldi;
138
139             //work backwards looking for a period
140             while(i != lastwcpos && wildCard[--i] != '.') {}
141
142             if(i != lastwcpos)
143                 [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))];
144         }
145
146         if ([m_wildcards count] == 0)
147         {
148             [m_wildcards release];
149             m_wildcards = nil;
150         }
151     }
152 }
153
154 wxFileDialog::~wxFileDialog()
155 {
156     [m_wildcards release];
157 }
158
159 void wxFileDialog::GetPaths(wxArrayString& paths) const
160 {
161     paths.Empty();
162
163     wxString dir(m_dir);
164     if ( m_dir.Last() != _T('\\') )
165         dir += _T('\\');
166
167     size_t count = m_fileNames.GetCount();
168     for ( size_t n = 0; n < count; n++ )
169     {
170         if (wxFileName(m_fileNames[n]).IsAbsolute())
171             paths.Add(m_fileNames[n]);
172         else
173             paths.Add(dir + m_fileNames[n]);
174     }
175 }
176
177 void wxFileDialog::GetFilenames(wxArrayString& files) const
178 {
179     files = m_fileNames;
180 }
181
182 void wxFileDialog::SetPath(const wxString& path)
183 {
184     wxString ext;
185     wxSplitPath(path, &m_dir, &m_fileName, &ext);
186     if ( !ext.empty() )
187         m_fileName << _T('.') << ext;
188 }
189
190 int wxFileDialog::ShowModal()
191 {
192     wxAutoNSAutoreleasePool thePool;
193
194     m_fileNames.Empty();
195
196     int nResult;
197
198     if (m_dialogStyle & wxFD_SAVE)
199     {
200         nResult = [GetNSSavePanel()
201                     runModalForDirectory:wxNSStringWithWxString(m_dir)
202                     file:wxNSStringWithWxString(m_fileName)];
203
204         if (nResult == NSOKButton)
205         {
206             m_fileNames.Add(wxStringWithNSString([GetNSSavePanel() filename]));
207             m_path = m_fileNames[0];
208         }
209     }
210     else //m_dialogStyle & wxFD_OPEN
211     {
212         nResult = [(NSOpenPanel*)m_cocoaNSWindow
213                     runModalForDirectory:wxNSStringWithWxString(m_dir)
214                     file:wxNSStringWithWxString(m_fileName)
215                     types:m_wildcards];
216
217         if (nResult == NSOKButton)
218         {
219             for(unsigned i = 0; i < [[(NSOpenPanel*)m_cocoaNSWindow filenames] count]; ++i)
220             {
221                 m_fileNames.Add(wxStringWithNSString([[(NSOpenPanel*)m_cocoaNSWindow filenames] objectAtIndex:(i)]));
222             }
223
224             m_path = m_fileNames[0];
225         }
226     }
227
228     return nResult == NSOKButton ? wxID_OK : wxID_CANCEL;
229 }
230
231 #endif // wxUSE_FILEDLG