1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/dirdlg.mm
3 // Purpose: wxDirDialog
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
22 #if wxUSE_DIRDLG && !defined(__WXUNIVERSAL__)
24 #include "wx/dirdlg.h"
27 #include "wx/msgdlg.h"
28 #include "wx/filedlg.h"
32 #include "wx/filename.h"
33 #include "wx/evtloop.h"
34 #include "wx/modalhook.h"
36 #include "wx/osx/private.h"
38 IMPLEMENT_CLASS(wxDirDialog, wxDialog)
40 void wxDirDialog::Init()
42 m_sheetDelegate = nil;
45 void wxDirDialog::Create(wxWindow *parent, const wxString& message,
46 const wxString& defaultPath, long style, const wxPoint& WXUNUSED(pos),
47 const wxSize& WXUNUSED(size), const wxString& WXUNUSED(name))
51 SetMessage( message );
52 SetWindowStyle(style);
54 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
55 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
58 wxDirDialog::~wxDirDialog()
60 [m_sheetDelegate release];
63 WX_NSOpenPanel wxDirDialog::OSXCreatePanel() const
65 NSOpenPanel *oPanel = [NSOpenPanel openPanel];
66 [oPanel setCanChooseDirectories:YES];
67 [oPanel setResolvesAliases:YES];
68 [oPanel setCanChooseFiles:NO];
70 wxCFStringRef cf( m_message );
71 [oPanel setMessage:cf.AsNSString()];
73 if ( !HasFlag(wxDD_DIR_MUST_EXIST) )
74 [oPanel setCanCreateDirectories:YES];
79 void wxDirDialog::ShowWindowModal()
81 wxNonOwnedWindow* parentWindow = NULL;
84 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
86 wxCHECK_RET(parentWindow, "Window modal display requires parent.");
88 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
90 NSOpenPanel *oPanel = OSXCreatePanel();
92 NSWindow* nativeParent = parentWindow->GetWXWindow();
93 wxCFStringRef dir( m_path );
94 [oPanel beginSheetForDirectory:dir.AsNSString() file:nil types: nil
95 modalForWindow: nativeParent modalDelegate: m_sheetDelegate
96 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
100 int wxDirDialog::ShowModal()
102 WX_HOOK_MODAL_DIALOG();
104 wxCFEventLoopPauseIdleEvents pause;
106 NSOpenPanel *oPanel = OSXCreatePanel();
108 wxCFStringRef dir( m_path );
110 m_path = wxEmptyString;
114 returnCode = (NSInteger)[oPanel runModalForDirectory:dir.AsNSString() file:nil types:nil];
115 ModalFinishedCallback(oPanel, returnCode);
117 return GetReturnCode();
120 void wxDirDialog::ModalFinishedCallback(void* panel, int returnCode)
122 int result = wxID_CANCEL;
124 if (returnCode == NSOKButton )
126 NSOpenPanel* oPanel = (NSOpenPanel*)panel;
127 SetPath( wxCFStringRef::AsStringWithNormalizationFormC([[oPanel filenames] objectAtIndex:0]));
130 SetReturnCode(result);
132 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
133 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
136 #endif // wxUSE_DIRDLG