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 void wxDirDialog::ShowWindowModal()
60 wxCFStringRef dir( m_path );
62 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
64 NSOpenPanel *oPanel = [NSOpenPanel openPanel];
65 [oPanel setCanChooseDirectories:YES];
66 [oPanel setResolvesAliases:YES];
67 [oPanel setCanChooseFiles:NO];
69 wxCFStringRef cf( m_message );
70 [oPanel setMessage:cf.AsNSString()];
72 if ( HasFlag(wxDD_NEW_DIR_BUTTON) )
73 [oPanel setCanCreateDirectories:YES];
75 wxNonOwnedWindow* parentWindow = NULL;
78 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
80 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
84 NSWindow* nativeParent = parentWindow->GetWXWindow();
85 [oPanel beginSheetForDirectory:dir.AsNSString() file:nil types: nil
86 modalForWindow: nativeParent modalDelegate: m_sheetDelegate
87 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
92 int wxDirDialog::ShowModal()
94 wxCFEventLoopPauseIdleEvents pause;
96 NSOpenPanel *oPanel = [NSOpenPanel openPanel];
97 [oPanel setCanChooseDirectories:YES];
98 [oPanel setResolvesAliases:YES];
99 [oPanel setCanChooseFiles:NO];
101 wxCFStringRef cf( m_message );
102 [oPanel setMessage:cf.AsNSString()];
104 if ( HasFlag(wxDD_NEW_DIR_BUTTON) )
105 [oPanel setCanCreateDirectories:YES];
107 wxCFStringRef dir( m_path );
109 m_path = wxEmptyString;
113 returnCode = (NSInteger)[oPanel runModalForDirectory:dir.AsNSString() file:nil types:nil];
114 ModalFinishedCallback(oPanel, returnCode);
116 return GetReturnCode();
119 void wxDirDialog::ModalFinishedCallback(void* panel, int returnCode)
121 int result = wxID_CANCEL;
123 if (returnCode == NSOKButton )
125 NSOpenPanel* oPanel = (NSOpenPanel*)panel;
126 SetPath( wxCFStringRef::AsString([[oPanel filenames] objectAtIndex:0]));
129 SetReturnCode(result);
131 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
132 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
135 #endif // wxUSE_DIRDLG