]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/dirdlg.mm
69112c5bc8c170f41c4167de4987467b5cbbcd0b
[wxWidgets.git] / src / cocoa / dirdlg.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dirdlg.mm
3 // Purpose: wxDirDialog for wxCocoa
4 // Author: Ryan Norton
5 // Modified by: Hiroyuki Nakamura(maloninc)
6 // Created: 2006-01-10
7 // RCS-ID: $Id$
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #if wxUSE_DIRDLG
24
25 #include "wx/dirdlg.h"
26
27 #ifndef WX_PRECOMP
28 #include "wx/msgdlg.h"
29 #include "wx/filedlg.h"
30 #include "wx/app.h"
31 #endif
32
33 #include "wx/filename.h"
34
35 #include "wx/cocoa/autorelease.h"
36 #include "wx/cocoa/string.h"
37
38 #import <AppKit/NSOpenPanel.h>
39 #import <AppKit/NSSavePanel.h>
40
41 #import <Foundation/NSArray.h>
42 // ============================================================================
43 // implementation
44 // ============================================================================
45
46 IMPLEMENT_CLASS(wxCocoaDirDialog, wxDialog)
47
48 // ----------------------------------------------------------------------------
49 // wxDirDialog
50 // ----------------------------------------------------------------------------
51
52 wxDirDialog::wxDirDialog(wxWindow *parent, const wxString& message,
53 const wxString& defaultPath, long style, const wxPoint& pos,
54 const wxSize& size, const wxString& name)
55 {
56 wxTopLevelWindows.Append(this);
57
58 m_message = message;
59 m_dialogStyle = style;
60 m_parent = parent;
61 m_path = defaultPath;
62
63 wxASSERT(CreateBase(parent,wxID_ANY,pos,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
64
65 if ( parent )
66 parent->AddChild(this);
67
68 m_cocoaNSWindow = nil;
69 m_cocoaNSView = nil;
70
71 //If the user requests to save - use a NSSavePanel
72 //else use a NSOpenPanel
73 if (m_dialogStyle & wxFD_SAVE)
74 {
75 SetNSPanel([NSSavePanel savePanel]);
76
77 [GetNSSavePanel() setTitle:wxNSStringWithWxString(message)];
78
79 [GetNSSavePanel() setPrompt:@"Save"];
80 [GetNSSavePanel() setTreatsFilePackagesAsDirectories:YES];
81 [GetNSSavePanel() setCanSelectHiddenExtension:YES];
82 }
83 else //m_dialogStyle & wxFD_OPEN
84 {
85 SetNSPanel([NSOpenPanel openPanel]);
86 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(message)];
87
88 [(NSOpenPanel*)m_cocoaNSWindow setResolvesAliases:YES];
89 [(NSOpenPanel*)m_cocoaNSWindow setCanChooseFiles:NO];
90 [(NSOpenPanel*)m_cocoaNSWindow setCanChooseDirectories:YES];
91 [GetNSSavePanel() setPrompt:@"Open"];
92 }
93
94 if (m_dialogStyle & wxDD_NEW_DIR_BUTTON) //m_dialogStyle & wxDD_NEW_DIR_BUTTON
95 {
96 [(NSOpenPanel*)m_cocoaNSWindow setCanCreateDirectories:YES];
97 }
98 }
99
100 wxDirDialog::~wxDirDialog()
101 {
102 }
103
104 int wxDirDialog::ShowModal()
105 {
106 wxAutoNSAutoreleasePool thePool;
107
108 m_fileNames.Empty();
109
110 int nResult;
111
112 if (m_dialogStyle & wxFD_SAVE)
113 {
114 nResult = [GetNSSavePanel()
115 runModalForDirectory:wxNSStringWithWxString(m_dir)
116 file:wxNSStringWithWxString(m_fileName)];
117
118 if (nResult == NSOKButton)
119 {
120 m_fileNames.Add(wxStringWithNSString([GetNSSavePanel() filename]));
121 m_path = m_fileNames[0];
122 }
123 }
124 else //m_dialogStyle & wxFD_OPEN
125 {
126 nResult = [(NSOpenPanel*)m_cocoaNSWindow
127 runModalForDirectory:wxNSStringWithWxString(m_dir)
128 file:wxNSStringWithWxString(m_fileName)
129 types:NULL];
130
131 if (nResult == NSOKButton)
132 {
133 for(unsigned i = 0; i < [[(NSOpenPanel*)m_cocoaNSWindow filenames] count]; ++i)
134 {
135 m_fileNames.Add(wxStringWithNSString([[(NSOpenPanel*)m_cocoaNSWindow filenames] objectAtIndex:(i)]));
136 }
137
138 m_path = m_fileNames[0];
139 }
140 }
141
142 return nResult == NSOKButton ? wxID_OK : wxID_CANCEL;
143 }
144
145 #endif // wxUSE_DIRDLG