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