1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/filedlg.mm
3 // Purpose: wxFileDialog for wxCocoa
7 // Copyright: (c) Ryan Norton
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
24 #include "wx/filedlg.h"
27 #include "wx/msgdlg.h"
31 #include "wx/filename.h"
33 #include "wx/cocoa/autorelease.h"
34 #include "wx/cocoa/string.h"
35 #include "wx/modalhook.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 WX_HOOK_MODAL_DIALOG();
201 wxAutoNSAutoreleasePool thePool;
207 if (HasFlag(wxFD_SAVE))
209 nResult = [GetNSSavePanel()
210 runModalForDirectory:wxNSStringWithWxString(m_dir)
211 file:wxNSStringWithWxString(m_fileName)];
213 if (nResult == NSOKButton)
215 m_fileNames.Add(wxStringWithNSString([GetNSSavePanel() filename]));
216 m_path = m_fileNames[0];
219 else //m_dialogStyle & wxFD_OPEN
221 nResult = [(NSOpenPanel*)m_cocoaNSWindow
222 runModalForDirectory:wxNSStringWithWxString(m_dir)
223 file:wxNSStringWithWxString(m_fileName)
226 if (nResult == NSOKButton)
228 for(unsigned i = 0; i < [[(NSOpenPanel*)m_cocoaNSWindow filenames] count]; ++i)
230 m_fileNames.Add(wxStringWithNSString([[(NSOpenPanel*)m_cocoaNSWindow filenames] objectAtIndex:(i)]));
233 m_path = m_fileNames[0];
237 return nResult == NSOKButton ? wxID_OK : wxID_CANCEL;
240 #endif // wxUSE_FILEDLG