Add wxActivateEvent::GetActivationReason().
[wxWidgets.git] / src / cocoa / dirdlg.mm
CommitLineData
937f314d
VZ
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
937f314d
VZ
7// Copyright: (c) Ryan Norton
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#if wxUSE_DIRDLG
23
1ab440bc
WS
24#include "wx/dirdlg.h"
25
937f314d
VZ
26#ifndef WX_PRECOMP
27 #include "wx/msgdlg.h"
28 #include "wx/filedlg.h"
937f314d
VZ
29 #include "wx/app.h"
30#endif
e031f1df 31
937f314d 32#include "wx/filename.h"
691745ab 33#include "wx/modalhook.h"
937f314d
VZ
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
46IMPLEMENT_CLASS(wxCocoaDirDialog, wxDialog)
47
48// ----------------------------------------------------------------------------
49// wxDirDialog
50// ----------------------------------------------------------------------------
51
52wxDirDialog::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);
e031f1df
WS
57
58 m_message = message;
747592e7
SC
59
60 SetWindowStyle(style);
937f314d
VZ
61 m_parent = parent;
62 m_path = defaultPath;
63
64 wxASSERT(CreateBase(parent,wxID_ANY,pos,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
65
66 if ( parent )
67 parent->AddChild(this);
68
69 m_cocoaNSWindow = nil;
70 m_cocoaNSView = nil;
71
72 //If the user requests to save - use a NSSavePanel
73 //else use a NSOpenPanel
747592e7 74 if (HasFlag(wxFD_SAVE))
937f314d
VZ
75 {
76 SetNSPanel([NSSavePanel savePanel]);
77
78 [GetNSSavePanel() setTitle:wxNSStringWithWxString(message)];
79
80 [GetNSSavePanel() setPrompt:@"Save"];
81 [GetNSSavePanel() setTreatsFilePackagesAsDirectories:YES];
82 [GetNSSavePanel() setCanSelectHiddenExtension:YES];
83 }
e031f1df 84 else //m_dialogStyle & wxFD_OPEN
937f314d
VZ
85 {
86 SetNSPanel([NSOpenPanel openPanel]);
87 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(message)];
88
89 [(NSOpenPanel*)m_cocoaNSWindow setResolvesAliases:YES];
90 [(NSOpenPanel*)m_cocoaNSWindow setCanChooseFiles:NO];
91 [(NSOpenPanel*)m_cocoaNSWindow setCanChooseDirectories:YES];
92 [GetNSSavePanel() setPrompt:@"Open"];
93 }
94
747592e7 95 if (HasFlag(wxDD_NEW_DIR_BUTTON)) //m_dialogStyle & wxDD_NEW_DIR_BUTTON
e031f1df 96 {
937f314d 97 [(NSOpenPanel*)m_cocoaNSWindow setCanCreateDirectories:YES];
e031f1df 98 }
937f314d
VZ
99}
100
101wxDirDialog::~wxDirDialog()
102{
103}
104
105int wxDirDialog::ShowModal()
106{
691745ab 107 WX_HOOK_MODAL_DIALOG();
643e9cf9 108
937f314d
VZ
109 wxAutoNSAutoreleasePool thePool;
110
111 m_fileNames.Empty();
112
113 int nResult;
114
747592e7 115 if (HasFlag(wxFD_SAVE))
937f314d
VZ
116 {
117 nResult = [GetNSSavePanel()
118 runModalForDirectory:wxNSStringWithWxString(m_dir)
119 file:wxNSStringWithWxString(m_fileName)];
120
121 if (nResult == NSOKButton)
122 {
123 m_fileNames.Add(wxStringWithNSString([GetNSSavePanel() filename]));
124 m_path = m_fileNames[0];
125 }
126 }
e031f1df 127 else //m_dialogStyle & wxFD_OPEN
937f314d
VZ
128 {
129 nResult = [(NSOpenPanel*)m_cocoaNSWindow
130 runModalForDirectory:wxNSStringWithWxString(m_dir)
131 file:wxNSStringWithWxString(m_fileName)
132 types:NULL];
133
134 if (nResult == NSOKButton)
135 {
136 for(unsigned i = 0; i < [[(NSOpenPanel*)m_cocoaNSWindow filenames] count]; ++i)
137 {
138 m_fileNames.Add(wxStringWithNSString([[(NSOpenPanel*)m_cocoaNSWindow filenames] objectAtIndex:(i)]));
139 }
140
141 m_path = m_fileNames[0];
142 }
143 }
144
145 return nResult == NSOKButton ? wxID_OK : wxID_CANCEL;
146}
147
148#endif // wxUSE_DIRDLG