1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/filedlg.mm
3 // Purpose: wxFileDialog for wxCocoa
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
25 #include "wx/filedlg.h"
28 #include "wx/msgdlg.h"
32 #include "wx/filename.h"
34 #include "wx/cocoa/autorelease.h"
35 #include "wx/cocoa/string.h"
37 #import <AppKit/NSOpenPanel.h>
38 #import <AppKit/NSSavePanel.h>
40 #import <Foundation/NSArray.h>
41 // ============================================================================
43 // ============================================================================
45 IMPLEMENT_CLASS(wxCocoaFileDialog, wxFileDialogBase)
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 wxFileDialog::wxFileDialog(wxWindow *parent,
52 const wxString& message,
53 const wxString& defaultDir,
54 const wxString& defaultFileName,
55 const wxString& wildCard,
60 : wxFileDialogBase(parent, message, defaultDir, defaultFileName,
61 wildCard, style, pos, sz, name)
63 wxTopLevelWindows.Append(this);
65 wxASSERT(CreateBase(parent,wxID_ANY,pos,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
68 parent->AddChild(this);
70 m_cocoaNSWindow = nil;
73 //Init the wildcard array
74 m_wildcards = [[NSMutableArray alloc] initWithCapacity:0];
76 //If the user requests to save - use a NSSavePanel
77 //else use a NSOpenPanel
78 if (HasFlag(wxFD_SAVE))
80 SetNSPanel([NSSavePanel savePanel]);
82 [GetNSSavePanel() setTitle:wxNSStringWithWxString(message)];
84 [GetNSSavePanel() setPrompt:@"Save"];
85 [GetNSSavePanel() setTreatsFilePackagesAsDirectories:YES];
86 [GetNSSavePanel() setCanSelectHiddenExtension:YES];
88 // Cached as per-app in obj-c
89 // [GetNSSavePanel() setExtensionHidden:YES];
92 // NB: Note that only Panther supports wildcards
93 // with save dialogs - not that wildcards in save
94 // dialogs are all that useful, anyway :)
97 else //m_dialogStyle & wxFD_OPEN
99 SetNSPanel([NSOpenPanel openPanel]);
100 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(message)];
102 [(NSOpenPanel*)m_cocoaNSWindow setAllowsMultipleSelection:(HasFlag(wxFD_MULTIPLE))];
103 [(NSOpenPanel*)m_cocoaNSWindow setResolvesAliases:YES];
104 [(NSOpenPanel*)m_cocoaNSWindow setCanChooseFiles:YES];
105 [(NSOpenPanel*)m_cocoaNSWindow setCanChooseDirectories:NO];
106 [GetNSSavePanel() setPrompt:@"Open"];
108 //convert wildcards - open panel only takes file extensions -
109 //no actual wildcards here :)
110 size_t lastwcpos = 0;
111 bool bDescription = true;
113 for(i = wildCard.find('|');
115 i = wildCard.find('|', lastwcpos+1))
121 bDescription = !bDescription;
123 //work backwards looking for a period
124 while(i != lastwcpos && wildCard[--i] != '.') {}
128 //no extension - can't use this wildcard
133 [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))];
136 bDescription = !bDescription;
143 size_t oldi = wildCard.length();
146 //work backwards looking for a period
147 while(i != lastwcpos && wildCard[--i] != '.') {}
150 [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))];
153 if ([m_wildcards count] == 0)
155 [m_wildcards release];
161 wxFileDialog::~wxFileDialog()
163 [m_wildcards release];
166 void wxFileDialog::GetPaths(wxArrayString& paths) const
171 if ( m_dir.Last() != wxT('\\') )
174 size_t count = m_fileNames.GetCount();
175 for ( size_t n = 0; n < count; n++ )
177 if (wxFileName(m_fileNames[n]).IsAbsolute())
178 paths.Add(m_fileNames[n]);
180 paths.Add(dir + m_fileNames[n]);
184 void wxFileDialog::GetFilenames(wxArrayString& files) const
189 void wxFileDialog::SetPath(const wxString& path)
192 wxFileName::SplitPath(path, &m_dir, &m_fileName, &ext);
194 m_fileName << wxT('.') << ext;
197 int wxFileDialog::ShowModal()
199 wxAutoNSAutoreleasePool thePool;
205 if (HasFlag(wxFD_SAVE))
207 nResult = [GetNSSavePanel()
208 runModalForDirectory:wxNSStringWithWxString(m_dir)
209 file:wxNSStringWithWxString(m_fileName)];
211 if (nResult == NSOKButton)
213 m_fileNames.Add(wxStringWithNSString([GetNSSavePanel() filename]));
214 m_path = m_fileNames[0];
217 else //m_dialogStyle & wxFD_OPEN
219 nResult = [(NSOpenPanel*)m_cocoaNSWindow
220 runModalForDirectory:wxNSStringWithWxString(m_dir)
221 file:wxNSStringWithWxString(m_fileName)
224 if (nResult == NSOKButton)
226 for(unsigned i = 0; i < [[(NSOpenPanel*)m_cocoaNSWindow filenames] count]; ++i)
228 m_fileNames.Add(wxStringWithNSString([[(NSOpenPanel*)m_cocoaNSWindow filenames] objectAtIndex:(i)]));
231 m_path = m_fileNames[0];
235 return nResult == NSOKButton ? wxID_OK : wxID_CANCEL;
238 #endif // wxUSE_FILEDLG