Add wxTEST_DIALOG for testing of modal dialogs.
[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 #include "wx/testing.h"
35
36 #include "wx/cocoa/autorelease.h"
37 #include "wx/cocoa/string.h"
38
39 #import <AppKit/NSOpenPanel.h>
40 #import <AppKit/NSSavePanel.h>
41
42 #import <Foundation/NSArray.h>
43 // ============================================================================
44 // implementation
45 // ============================================================================
46
47 IMPLEMENT_CLASS(wxCocoaDirDialog, wxDialog)
48
49 // ----------------------------------------------------------------------------
50 // wxDirDialog
51 // ----------------------------------------------------------------------------
52
53 wxDirDialog::wxDirDialog(wxWindow *parent, const wxString& message,
54         const wxString& defaultPath, long style, const wxPoint& pos,
55         const wxSize& size, const wxString& name)
56 {
57     wxTopLevelWindows.Append(this);
58
59     m_message = message;
60
61     SetWindowStyle(style);
62     m_parent = parent;
63     m_path = defaultPath;
64
65     wxASSERT(CreateBase(parent,wxID_ANY,pos,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
66
67     if ( parent )
68         parent->AddChild(this);
69
70     m_cocoaNSWindow = nil;
71     m_cocoaNSView = nil;
72
73     //If the user requests to save - use a NSSavePanel
74     //else use a NSOpenPanel
75     if (HasFlag(wxFD_SAVE))
76     {
77         SetNSPanel([NSSavePanel savePanel]);
78
79         [GetNSSavePanel() setTitle:wxNSStringWithWxString(message)];
80
81         [GetNSSavePanel() setPrompt:@"Save"];
82         [GetNSSavePanel() setTreatsFilePackagesAsDirectories:YES];
83         [GetNSSavePanel() setCanSelectHiddenExtension:YES];
84     }
85     else //m_dialogStyle & wxFD_OPEN
86     {
87         SetNSPanel([NSOpenPanel openPanel]);
88         [m_cocoaNSWindow setTitle:wxNSStringWithWxString(message)];
89
90         [(NSOpenPanel*)m_cocoaNSWindow setResolvesAliases:YES];
91         [(NSOpenPanel*)m_cocoaNSWindow setCanChooseFiles:NO];
92         [(NSOpenPanel*)m_cocoaNSWindow setCanChooseDirectories:YES];
93         [GetNSSavePanel() setPrompt:@"Open"];
94     }
95
96     if (HasFlag(wxDD_NEW_DIR_BUTTON)) //m_dialogStyle & wxDD_NEW_DIR_BUTTON
97     {
98         [(NSOpenPanel*)m_cocoaNSWindow setCanCreateDirectories:YES];
99     }
100 }
101
102 wxDirDialog::~wxDirDialog()
103 {
104 }
105
106 int wxDirDialog::ShowModal()
107 {
108     WX_TESTING_SHOW_MODAL_HOOK();
109
110     wxAutoNSAutoreleasePool thePool;
111
112     m_fileNames.Empty();
113
114     int nResult;
115
116     if (HasFlag(wxFD_SAVE))
117     {
118         nResult = [GetNSSavePanel()
119                     runModalForDirectory:wxNSStringWithWxString(m_dir)
120                     file:wxNSStringWithWxString(m_fileName)];
121
122         if (nResult == NSOKButton)
123         {
124             m_fileNames.Add(wxStringWithNSString([GetNSSavePanel() filename]));
125             m_path = m_fileNames[0];
126         }
127     }
128     else //m_dialogStyle & wxFD_OPEN
129     {
130         nResult = [(NSOpenPanel*)m_cocoaNSWindow
131                     runModalForDirectory:wxNSStringWithWxString(m_dir)
132                     file:wxNSStringWithWxString(m_fileName)
133                     types:NULL];
134
135         if (nResult == NSOKButton)
136         {
137             for(unsigned i = 0; i < [[(NSOpenPanel*)m_cocoaNSWindow filenames] count]; ++i)
138             {
139                 m_fileNames.Add(wxStringWithNSString([[(NSOpenPanel*)m_cocoaNSWindow filenames] objectAtIndex:(i)]));
140             }
141
142             m_path = m_fileNames[0];
143         }
144     }
145
146     return nResult == NSOKButton ? wxID_OK : wxID_CANCEL;
147 }
148
149 #endif // wxUSE_DIRDLG