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"
36 #include "wx/testing.h"
38 #import <AppKit/NSOpenPanel.h>
39 #import <AppKit/NSSavePanel.h>
41 #import <Foundation/NSArray.h>
42 // ============================================================================
44 // ============================================================================
46 IMPLEMENT_CLASS(wxCocoaFileDialog, wxFileDialogBase)
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 wxFileDialog::wxFileDialog(wxWindow *parent,
53 const wxString& message,
54 const wxString& defaultDir,
55 const wxString& defaultFileName,
56 const wxString& wildCard,
61 : wxFileDialogBase(parent, message, defaultDir, defaultFileName,
62 wildCard, style, pos, sz, name)
64 wxTopLevelWindows.Append(this);
66 wxASSERT(CreateBase(parent,wxID_ANY,pos,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
69 parent->AddChild(this);
71 m_cocoaNSWindow = nil;
74 //Init the wildcard array
75 m_wildcards = [[NSMutableArray alloc] initWithCapacity:0];
77 //If the user requests to save - use a NSSavePanel
78 //else use a NSOpenPanel
79 if (HasFlag(wxFD_SAVE))
81 SetNSPanel([NSSavePanel savePanel]);
83 [GetNSSavePanel() setTitle:wxNSStringWithWxString(message)];
85 [GetNSSavePanel() setPrompt:@"Save"];
86 [GetNSSavePanel() setTreatsFilePackagesAsDirectories:YES];
87 [GetNSSavePanel() setCanSelectHiddenExtension:YES];
89 // Cached as per-app in obj-c
90 // [GetNSSavePanel() setExtensionHidden:YES];
93 // NB: Note that only Panther supports wildcards
94 // with save dialogs - not that wildcards in save
95 // dialogs are all that useful, anyway :)
98 else //m_dialogStyle & wxFD_OPEN
100 SetNSPanel([NSOpenPanel openPanel]);
101 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(message)];
103 [(NSOpenPanel*)m_cocoaNSWindow setAllowsMultipleSelection:(HasFlag(wxFD_MULTIPLE))];
104 [(NSOpenPanel*)m_cocoaNSWindow setResolvesAliases:YES];
105 [(NSOpenPanel*)m_cocoaNSWindow setCanChooseFiles:YES];
106 [(NSOpenPanel*)m_cocoaNSWindow setCanChooseDirectories:NO];
107 [GetNSSavePanel() setPrompt:@"Open"];
109 //convert wildcards - open panel only takes file extensions -
110 //no actual wildcards here :)
111 size_t lastwcpos = 0;
112 bool bDescription = true;
114 for(i = wildCard.find('|');
116 i = wildCard.find('|', lastwcpos+1))
122 bDescription = !bDescription;
124 //work backwards looking for a period
125 while(i != lastwcpos && wildCard[--i] != '.') {}
129 //no extension - can't use this wildcard
134 [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))];
137 bDescription = !bDescription;
144 size_t oldi = wildCard.length();
147 //work backwards looking for a period
148 while(i != lastwcpos && wildCard[--i] != '.') {}
151 [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))];
154 if ([m_wildcards count] == 0)
156 [m_wildcards release];
162 wxFileDialog::~wxFileDialog()
164 [m_wildcards release];
167 void wxFileDialog::GetPaths(wxArrayString& paths) const
172 if ( m_dir.Last() != wxT('\\') )
175 size_t count = m_fileNames.GetCount();
176 for ( size_t n = 0; n < count; n++ )
178 if (wxFileName(m_fileNames[n]).IsAbsolute())
179 paths.Add(m_fileNames[n]);
181 paths.Add(dir + m_fileNames[n]);
185 void wxFileDialog::GetFilenames(wxArrayString& files) const
190 void wxFileDialog::SetPath(const wxString& path)
193 wxFileName::SplitPath(path, &m_dir, &m_fileName, &ext);
195 m_fileName << wxT('.') << ext;
198 int wxFileDialog::ShowModal()
200 WX_TESTING_SHOW_MODAL_HOOK();
202 wxAutoNSAutoreleasePool thePool;
208 if (HasFlag(wxFD_SAVE))
210 nResult = [GetNSSavePanel()
211 runModalForDirectory:wxNSStringWithWxString(m_dir)
212 file:wxNSStringWithWxString(m_fileName)];
214 if (nResult == NSOKButton)
216 m_fileNames.Add(wxStringWithNSString([GetNSSavePanel() filename]));
217 m_path = m_fileNames[0];
220 else //m_dialogStyle & wxFD_OPEN
222 nResult = [(NSOpenPanel*)m_cocoaNSWindow
223 runModalForDirectory:wxNSStringWithWxString(m_dir)
224 file:wxNSStringWithWxString(m_fileName)
227 if (nResult == NSOKButton)
229 for(unsigned i = 0; i < [[(NSOpenPanel*)m_cocoaNSWindow filenames] count]; ++i)
231 m_fileNames.Add(wxStringWithNSString([[(NSOpenPanel*)m_cocoaNSWindow filenames] objectAtIndex:(i)]));
234 m_path = m_fileNames[0];
238 return nResult == NSOKButton ? wxID_OK : wxID_CANCEL;
241 #endif // wxUSE_FILEDLG