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"
30 #include "wx/filename.h"
32 #include "wx/cocoa/autorelease.h"
33 #include "wx/cocoa/string.h"
35 #import <AppKit/NSOpenPanel.h>
36 #import <AppKit/NSSavePanel.h>
38 #import <Foundation/NSArray.h>
39 // ============================================================================
41 // ============================================================================
43 IMPLEMENT_CLASS(wxCocoaFileDialog, wxFileDialogBase)
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 wxFileDialog::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,
55 wxTopLevelWindows.Append(this);
57 wxASSERT(CreateBase(parent,wxID_ANY,pos,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
60 parent->AddChild(this);
62 m_cocoaNSWindow = nil;
65 //Init the wildcard array
66 m_wildcards = [[NSMutableArray alloc] initWithCapacity:0];
68 //If the user requests to save - use a NSSavePanel
69 //else use a NSOpenPanel
70 if (m_dialogStyle & wxSAVE)
72 SetNSPanel([NSSavePanel savePanel]);
74 [GetNSSavePanel() setTitle:wxNSStringWithWxString(message)];
76 [GetNSSavePanel() setPrompt:@"Save"];
77 [GetNSSavePanel() setTreatsFilePackagesAsDirectories:YES];
78 [GetNSSavePanel() setCanSelectHiddenExtension:YES];
80 // Cached as per-app in obj-c
81 // [GetNSSavePanel() setExtensionHidden:YES];
84 // NB: Note that only Panther supports wildcards
85 // with save dialogs - not that wildcards in save
86 // dialogs are all that useful, anyway :)
89 else //m_dialogStyle & wxOPEN
91 SetNSPanel([NSOpenPanel openPanel]);
92 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(message)];
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"];
100 //convert wildcards - open panel only takes file extensions -
101 //no actual wildcards here :)
102 size_t lastwcpos = 0;
103 bool bDescription = true;
105 for(i = wildCard.find('|');
107 i = wildCard.find('|', lastwcpos+1))
113 bDescription = !bDescription;
115 //work backwards looking for a period
116 while(i != lastwcpos && wildCard[--i] != '.') {}
120 //no extension - can't use this wildcard
125 [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))];
128 bDescription = !bDescription;
135 size_t oldi = wildCard.length();
138 //work backwards looking for a period
139 while(i != lastwcpos && wildCard[--i] != '.') {}
142 [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))];
145 if ([m_wildcards count] == 0)
147 [m_wildcards release];
153 wxFileDialog::~wxFileDialog()
155 [m_wildcards release];
158 void wxFileDialog::GetPaths(wxArrayString& paths) const
163 if ( m_dir.Last() != _T('\\') )
166 size_t count = m_fileNames.GetCount();
167 for ( size_t n = 0; n < count; n++ )
169 if (wxFileName(m_fileNames[n]).IsAbsolute())
170 paths.Add(m_fileNames[n]);
172 paths.Add(dir + m_fileNames[n]);
176 void wxFileDialog::GetFilenames(wxArrayString& files) const
181 void wxFileDialog::SetPath(const wxString& path)
184 wxSplitPath(path, &m_dir, &m_fileName, &ext);
186 m_fileName << _T('.') << ext;
189 int wxFileDialog::ShowModal()
191 wxAutoNSAutoreleasePool thePool;
197 if (m_dialogStyle & wxSAVE)
199 nResult = [GetNSSavePanel()
200 runModalForDirectory:wxNSStringWithWxString(m_dir)
201 file:wxNSStringWithWxString(m_fileName)];
203 if (nResult == NSOKButton)
205 m_fileNames.Add(wxStringWithNSString([GetNSSavePanel() filename]));
206 m_path = m_fileNames[0];
209 else //m_dialogStyle & wxOPEN
211 nResult = [(NSOpenPanel*)m_cocoaNSWindow
212 runModalForDirectory:wxNSStringWithWxString(m_dir)
213 file:wxNSStringWithWxString(m_fileName)
216 if (nResult == NSOKButton)
218 for(unsigned i = 0; i < [[(NSOpenPanel*)m_cocoaNSWindow filenames] count]; ++i)
220 m_fileNames.Add(wxStringWithNSString([[(NSOpenPanel*)m_cocoaNSWindow filenames] objectAtIndex:(i)]));
223 m_path = m_fileNames[0];
227 return nResult == NSOKButton ? wxID_OK : wxID_CANCEL;
230 #endif // wxUSE_FILEDLG