Add default ctors and Create() to wxDirDialog and wxFileDialog in wxOSX.
[wxWidgets.git] / src / osx / cocoa / dirdlg.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/osx/cocoa/dirdlg.mm
3 // Purpose:     wxDirDialog
4 // Author:      Stefan Csomor
5 // Modified by:
6 // Created:     2008-08-30
7 // RCS-ID:      $Id$
8 // Copyright:   (c) Stefan Csomor
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 && !defined(__WXUNIVERSAL__)
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/evtloop.h"
35 #include "wx/modalhook.h"
36
37 #include "wx/osx/private.h"
38
39 IMPLEMENT_CLASS(wxDirDialog, wxDialog)
40
41 void wxDirDialog::Init()
42 {
43     m_sheetDelegate = nil;
44 }
45
46 void wxDirDialog::Create(wxWindow *parent, const wxString& message,
47         const wxString& defaultPath, long style, const wxPoint& WXUNUSED(pos),
48         const wxSize& WXUNUSED(size), const wxString& WXUNUSED(name))
49 {
50     m_parent = parent;
51
52     SetMessage( message );
53     SetWindowStyle(style);
54     SetPath(defaultPath);
55     m_sheetDelegate = [[ModalDialogDelegate alloc] init];
56     [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
57 }
58
59 wxDirDialog::~wxDirDialog()
60 {
61     [m_sheetDelegate release];
62 }
63
64 WX_NSOpenPanel wxDirDialog::OSXCreatePanel() const
65 {
66     NSOpenPanel *oPanel = [NSOpenPanel openPanel];
67     [oPanel setCanChooseDirectories:YES];
68     [oPanel setResolvesAliases:YES];
69     [oPanel setCanChooseFiles:NO];
70
71     wxCFStringRef cf( m_message );
72     [oPanel setMessage:cf.AsNSString()];
73
74     if ( !HasFlag(wxDD_DIR_MUST_EXIST) )
75         [oPanel setCanCreateDirectories:YES];
76
77     return oPanel;
78 }
79
80 void wxDirDialog::ShowWindowModal()
81 {
82     wxNonOwnedWindow* parentWindow = NULL;
83
84     if (GetParent())
85         parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
86
87     wxCHECK_RET(parentWindow, "Window modal display requires parent.");
88
89     m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
90
91     NSOpenPanel *oPanel = OSXCreatePanel();
92
93     NSWindow* nativeParent = parentWindow->GetWXWindow();
94     wxCFStringRef dir( m_path );
95     [oPanel beginSheetForDirectory:dir.AsNSString() file:nil types: nil
96         modalForWindow: nativeParent modalDelegate: m_sheetDelegate
97         didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
98         contextInfo: nil];
99 }
100
101 int wxDirDialog::ShowModal()
102 {
103     WX_HOOK_MODAL_DIALOG();
104
105     wxCFEventLoopPauseIdleEvents pause;
106
107     NSOpenPanel *oPanel = OSXCreatePanel();
108
109     wxCFStringRef dir( m_path );
110
111     m_path = wxEmptyString;
112
113     int returnCode = -1;
114
115     returnCode = (NSInteger)[oPanel runModalForDirectory:dir.AsNSString() file:nil types:nil];
116     ModalFinishedCallback(oPanel, returnCode);
117
118     return GetReturnCode();
119 }
120
121 void wxDirDialog::ModalFinishedCallback(void* panel, int returnCode)
122 {
123     int result = wxID_CANCEL;
124
125     if (returnCode == NSOKButton )
126     {
127         NSOpenPanel* oPanel = (NSOpenPanel*)panel;
128         SetPath( wxCFStringRef::AsStringWithNormalizationFormC([[oPanel filenames] objectAtIndex:0]));
129         result = wxID_OK;
130     }
131     SetReturnCode(result);
132
133     if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
134         SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED  );
135 }
136
137 #endif // wxUSE_DIRDLG