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, const wxString& message,
52 const wxString& defaultDir, const wxString& defaultFileName,
53 const wxString& wildCard, long style, const wxPoint& pos)
54 : wxFileDialogBase(parent, message, defaultDir, defaultFileName,
57 wxTopLevelWindows.Append(this);
59 wxASSERT(CreateBase(parent,wxID_ANY,pos,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
62 parent->AddChild(this);
64 m_cocoaNSWindow = nil;
67 //Init the wildcard array
68 m_wildcards = [[NSMutableArray alloc] initWithCapacity:0];
70 //If the user requests to save - use a NSSavePanel
71 //else use a NSOpenPanel
72 if (m_dialogStyle & wxFD_SAVE)
74 SetNSPanel([NSSavePanel savePanel]);
76 [GetNSSavePanel() setTitle:wxNSStringWithWxString(message)];
78 [GetNSSavePanel() setPrompt:@"Save"];
79 [GetNSSavePanel() setTreatsFilePackagesAsDirectories:YES];
80 [GetNSSavePanel() setCanSelectHiddenExtension:YES];
82 // Cached as per-app in obj-c
83 // [GetNSSavePanel() setExtensionHidden:YES];
86 // NB: Note that only Panther supports wildcards
87 // with save dialogs - not that wildcards in save
88 // dialogs are all that useful, anyway :)
91 else //m_dialogStyle & wxFD_OPEN
93 SetNSPanel([NSOpenPanel openPanel]);
94 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(message)];
96 [(NSOpenPanel*)m_cocoaNSWindow setAllowsMultipleSelection:(m_dialogStyle & wxFD_MULTIPLE)];
97 [(NSOpenPanel*)m_cocoaNSWindow setResolvesAliases:YES];
98 [(NSOpenPanel*)m_cocoaNSWindow setCanChooseFiles:YES];
99 [(NSOpenPanel*)m_cocoaNSWindow setCanChooseDirectories:NO];
100 [GetNSSavePanel() setPrompt:@"Open"];
102 //convert wildcards - open panel only takes file extensions -
103 //no actual wildcards here :)
104 size_t lastwcpos = 0;
105 bool bDescription = true;
107 for(i = wildCard.find('|');
109 i = wildCard.find('|', lastwcpos+1))
115 bDescription = !bDescription;
117 //work backwards looking for a period
118 while(i != lastwcpos && wildCard[--i] != '.') {}
122 //no extension - can't use this wildcard
127 [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))];
130 bDescription = !bDescription;
137 size_t oldi = wildCard.length();
140 //work backwards looking for a period
141 while(i != lastwcpos && wildCard[--i] != '.') {}
144 [m_wildcards addObject:wxNSStringWithWxString(wildCard.substr(i+1, oldi-i-1))];
147 if ([m_wildcards count] == 0)
149 [m_wildcards release];
155 wxFileDialog::~wxFileDialog()
157 [m_wildcards release];
160 void wxFileDialog::GetPaths(wxArrayString& paths) const
165 if ( m_dir.Last() != _T('\\') )
168 size_t count = m_fileNames.GetCount();
169 for ( size_t n = 0; n < count; n++ )
171 if (wxFileName(m_fileNames[n]).IsAbsolute())
172 paths.Add(m_fileNames[n]);
174 paths.Add(dir + m_fileNames[n]);
178 void wxFileDialog::GetFilenames(wxArrayString& files) const
183 void wxFileDialog::SetPath(const wxString& path)
186 wxSplitPath(path, &m_dir, &m_fileName, &ext);
188 m_fileName << _T('.') << ext;
191 int wxFileDialog::ShowModal()
193 wxAutoNSAutoreleasePool thePool;
199 if (m_dialogStyle & wxFD_SAVE)
201 nResult = [GetNSSavePanel()
202 runModalForDirectory:wxNSStringWithWxString(m_dir)
203 file:wxNSStringWithWxString(m_fileName)];
205 if (nResult == NSOKButton)
207 m_fileNames.Add(wxStringWithNSString([GetNSSavePanel() filename]));
208 m_path = m_fileNames[0];
211 else //m_dialogStyle & wxFD_OPEN
213 nResult = [(NSOpenPanel*)m_cocoaNSWindow
214 runModalForDirectory:wxNSStringWithWxString(m_dir)
215 file:wxNSStringWithWxString(m_fileName)
218 if (nResult == NSOKButton)
220 for(unsigned i = 0; i < [[(NSOpenPanel*)m_cocoaNSWindow filenames] count]; ++i)
222 m_fileNames.Add(wxStringWithNSString([[(NSOpenPanel*)m_cocoaNSWindow filenames] objectAtIndex:(i)]));
225 m_path = m_fileNames[0];
229 return nResult == NSOKButton ? wxID_OK : wxID_CANCEL;
232 #endif // wxUSE_FILEDLG