1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/dirdlg.mm
3 // Purpose: wxDirDialog
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if wxUSE_DIRDLG && !defined(__WXUNIVERSAL__)
25 #include "wx/dirdlg.h"
28 #include "wx/msgdlg.h"
29 #include "wx/filedlg.h"
33 #include "wx/filename.h"
34 #include "wx/evtloop.h"
36 #include "wx/osx/private.h"
38 IMPLEMENT_CLASS(wxDirDialog, wxDialog)
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))
46 SetMessage( message );
47 SetWindowStyle(style);
49 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
50 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
53 wxDirDialog::~wxDirDialog()
55 [m_sheetDelegate release];
58 WX_NSOpenPanel wxDirDialog::OSXCreatePanel() const
60 NSOpenPanel *oPanel = [NSOpenPanel openPanel];
61 [oPanel setCanChooseDirectories:YES];
62 [oPanel setResolvesAliases:YES];
63 [oPanel setCanChooseFiles:NO];
65 wxCFStringRef cf( m_message );
66 [oPanel setMessage:cf.AsNSString()];
68 if ( !HasFlag(wxDD_DIR_MUST_EXIST) )
69 [oPanel setCanCreateDirectories:YES];
74 void wxDirDialog::ShowWindowModal()
76 wxNonOwnedWindow* parentWindow = NULL;
79 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
81 wxCHECK_RET(parentWindow, "Window modal display requires parent.");
83 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
85 NSOpenPanel *oPanel = OSXCreatePanel();
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:)
95 int wxDirDialog::ShowModal()
97 wxCFEventLoopPauseIdleEvents pause;
99 NSOpenPanel *oPanel = OSXCreatePanel();
101 wxCFStringRef dir( m_path );
103 m_path = wxEmptyString;
107 returnCode = (NSInteger)[oPanel runModalForDirectory:dir.AsNSString() file:nil types:nil];
108 ModalFinishedCallback(oPanel, returnCode);
110 return GetReturnCode();
113 void wxDirDialog::ModalFinishedCallback(void* panel, int returnCode)
115 int result = wxID_CANCEL;
117 if (returnCode == NSOKButton )
119 NSOpenPanel* oPanel = (NSOpenPanel*)panel;
120 SetPath( wxCFStringRef::AsString([[oPanel filenames] objectAtIndex:0]));
123 SetReturnCode(result);
125 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
126 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
129 #endif // wxUSE_DIRDLG