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