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"
26 #include "wx/msgdlg.h"
27 #include "wx/filedlg.h"
31 #include "wx/filename.h"
33 #include "wx/cocoa/autorelease.h"
34 #include "wx/cocoa/string.h"
36 #import <AppKit/NSOpenPanel.h>
37 #import <AppKit/NSSavePanel.h>
39 #import <Foundation/NSArray.h>
40 // ============================================================================
42 // ============================================================================
44 IMPLEMENT_CLASS(wxCocoaFileDialog, wxFileDialogBase)
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
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,
56 wxTopLevelWindows.Append(this);
58 wxASSERT(CreateBase(parent,wxID_ANY,pos,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
61 parent->AddChild(this);
63 m_cocoaNSWindow = nil;
66 //Init the wildcard array
67 m_wildcards = [[NSMutableArray alloc] initWithCapacity:0];
69 //If the user requests to save - use a NSSavePanel
70 //else use a NSOpenPanel
71 if (m_dialogStyle & wxFD_SAVE)
73 SetNSPanel([NSSavePanel savePanel]);
75 [GetNSSavePanel() setTitle:wxNSStringWithWxString(message)];
77 [GetNSSavePanel() setPrompt:@"Save"];
78 [GetNSSavePanel() setTreatsFilePackagesAsDirectories:YES];
79 [GetNSSavePanel() setCanSelectHiddenExtension:YES];
81 // Cached as per-app in obj-c
82 // [GetNSSavePanel() setExtensionHidden:YES];
85 // NB: Note that only Panther supports wildcards
86 // with save dialogs - not that wildcards in save
87 // dialogs are all that useful, anyway :)
90 else //m_dialogStyle & wxFD_OPEN
92 SetNSPanel([NSOpenPanel openPanel]);
93 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(message)];
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"];
101 //convert wildcards - open panel only takes file extensions -
102 //no actual wildcards here :)
103 size_t lastwcpos = 0;
104 bool bDescription = true;
106 for(i = wildCard.find('|');
108 i = wildCard.find('|', lastwcpos+1))
114 bDescription = !bDescription;
116 //work backwards looking for a period
117 while(i != lastwcpos && wildCard[--i] != '.') {}
121 //no extension - can't use this wildcard
126 [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))];
129 bDescription = !bDescription;
136 size_t oldi = wildCard.length();
139 //work backwards looking for a period
140 while(i != lastwcpos && wildCard[--i] != '.') {}
143 [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))];
146 if ([m_wildcards count] == 0)
148 [m_wildcards release];
154 wxFileDialog::~wxFileDialog()
156 [m_wildcards release];
159 void wxFileDialog::GetPaths(wxArrayString& paths) const
164 if ( m_dir.Last() != _T('\\') )
167 size_t count = m_fileNames.GetCount();
168 for ( size_t n = 0; n < count; n++ )
170 if (wxFileName(m_fileNames[n]).IsAbsolute())
171 paths.Add(m_fileNames[n]);
173 paths.Add(dir + m_fileNames[n]);
177 void wxFileDialog::GetFilenames(wxArrayString& files) const
182 void wxFileDialog::SetPath(const wxString& path)
185 wxSplitPath(path, &m_dir, &m_fileName, &ext);
187 m_fileName << _T('.') << ext;
190 int wxFileDialog::ShowModal()
192 wxAutoNSAutoreleasePool thePool;
198 if (m_dialogStyle & wxFD_SAVE)
200 nResult = [GetNSSavePanel()
201 runModalForDirectory:wxNSStringWithWxString(m_dir)
202 file:wxNSStringWithWxString(m_fileName)];
204 if (nResult == NSOKButton)
206 m_fileNames.Add(wxStringWithNSString([GetNSSavePanel() filename]));
207 m_path = m_fileNames[0];
210 else //m_dialogStyle & wxFD_OPEN
212 nResult = [(NSOpenPanel*)m_cocoaNSWindow
213 runModalForDirectory:wxNSStringWithWxString(m_dir)
214 file:wxNSStringWithWxString(m_fileName)
217 if (nResult == NSOKButton)
219 for(unsigned i = 0; i < [[(NSOpenPanel*)m_cocoaNSWindow filenames] count]; ++i)
221 m_fileNames.Add(wxStringWithNSString([[(NSOpenPanel*)m_cocoaNSWindow filenames] objectAtIndex:(i)]));
224 m_path = m_fileNames[0];
228 return nResult == NSOKButton ? wxID_OK : wxID_CANCEL;
231 #endif // wxUSE_FILEDLG