]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/dirdlg.mm
No changes, just avoid code duplication in wxOSX wxDirDialog.
[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
36 #include "wx/osx/private.h"
37
38 IMPLEMENT_CLASS(wxDirDialog, wxDialog)
39
40 wxDirDialog::wxDirDialog(wxWindow *parent, const wxString& message,
41 const wxString& defaultPath, long style, const wxPoint& WXUNUSED(pos),
42 const wxSize& WXUNUSED(size), const wxString& WXUNUSED(name))
43 {
44 m_parent = parent;
45
46 SetMessage( message );
47 SetWindowStyle(style);
48 SetPath(defaultPath);
49 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
50 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
51 }
52
53 wxDirDialog::~wxDirDialog()
54 {
55 [m_sheetDelegate release];
56 }
57
58 WX_NSOpenPanel wxDirDialog::OSXCreatePanel() const
59 {
60 NSOpenPanel *oPanel = [NSOpenPanel openPanel];
61 [oPanel setCanChooseDirectories:YES];
62 [oPanel setResolvesAliases:YES];
63 [oPanel setCanChooseFiles:NO];
64
65 wxCFStringRef cf( m_message );
66 [oPanel setMessage:cf.AsNSString()];
67
68 if ( HasFlag(wxDD_NEW_DIR_BUTTON) )
69 [oPanel setCanCreateDirectories:YES];
70
71 return oPanel;
72 }
73
74 void wxDirDialog::ShowWindowModal()
75 {
76 wxNonOwnedWindow* parentWindow = NULL;
77
78 if (GetParent())
79 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
80
81 wxCHECK_RET(parentWindow, "Window modal display requires parent.");
82
83 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
84
85 NSOpenPanel *oPanel = OSXCreatePanel();
86
87 NSWindow* nativeParent = parentWindow->GetWXWindow();
88 wxCFStringRef dir( m_path );
89 [oPanel beginSheetForDirectory:dir.AsNSString() file:nil types: nil
90 modalForWindow: nativeParent modalDelegate: m_sheetDelegate
91 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
92 contextInfo: nil];
93 }
94
95 int wxDirDialog::ShowModal()
96 {
97 wxCFEventLoopPauseIdleEvents pause;
98
99 NSOpenPanel *oPanel = OSXCreatePanel();
100
101 wxCFStringRef dir( m_path );
102
103 m_path = wxEmptyString;
104
105 int returnCode = -1;
106
107 returnCode = (NSInteger)[oPanel runModalForDirectory:dir.AsNSString() file:nil types:nil];
108 ModalFinishedCallback(oPanel, returnCode);
109
110 return GetReturnCode();
111 }
112
113 void wxDirDialog::ModalFinishedCallback(void* panel, int returnCode)
114 {
115 int result = wxID_CANCEL;
116
117 if (returnCode == NSOKButton )
118 {
119 NSOpenPanel* oPanel = (NSOpenPanel*)panel;
120 SetPath( wxCFStringRef::AsString([[oPanel filenames] objectAtIndex:0]));
121 result = wxID_OK;
122 }
123 SetReturnCode(result);
124
125 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
126 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
127 }
128
129 #endif // wxUSE_DIRDLG