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"
35 #include "wx/osx/private.h"
37 IMPLEMENT_CLASS(wxDirDialog, wxDialog)
39 wxDirDialog::wxDirDialog(wxWindow *parent, const wxString& message,
40 const wxString& defaultPath, long style, const wxPoint& WXUNUSED(pos),
41 const wxSize& WXUNUSED(size), const wxString& WXUNUSED(name))
45 SetMessage( message );
46 SetWindowStyle(style);
48 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
49 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
52 wxDirDialog::~wxDirDialog()
54 [m_sheetDelegate release];
57 void wxDirDialog::ShowWindowModal()
59 wxCFStringRef dir( m_path );
61 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
63 NSOpenPanel *oPanel = [NSOpenPanel openPanel];
64 [oPanel setCanChooseDirectories:YES];
65 [oPanel setResolvesAliases:YES];
66 [oPanel setCanChooseFiles:NO];
68 wxCFStringRef cf( m_message );
69 [oPanel setMessage:cf.AsNSString()];
71 if ( HasFlag(wxDD_NEW_DIR_BUTTON) )
72 [oPanel setCanCreateDirectories:YES];
74 wxNonOwnedWindow* parentWindow = NULL;
77 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
79 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
83 NSWindow* nativeParent = parentWindow->GetWXWindow();
84 [oPanel beginSheetForDirectory:dir.AsNSString() file:nil types: nil
85 modalForWindow: nativeParent modalDelegate: m_sheetDelegate
86 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
91 int wxDirDialog::ShowModal()
93 NSOpenPanel *oPanel = [NSOpenPanel openPanel];
94 [oPanel setCanChooseDirectories:YES];
95 [oPanel setResolvesAliases:YES];
96 [oPanel setCanChooseFiles:NO];
98 wxCFStringRef cf( m_message );
99 [oPanel setMessage:cf.AsNSString()];
101 if ( HasFlag(wxDD_NEW_DIR_BUTTON) )
102 [oPanel setCanCreateDirectories:YES];
104 wxCFStringRef dir( m_path );
106 m_path = wxEmptyString;
110 returnCode = (NSInteger)[oPanel runModalForDirectory:dir.AsNSString() file:nil types:nil];
111 ModalFinishedCallback(oPanel, returnCode);
113 return GetReturnCode();
116 void wxDirDialog::ModalFinishedCallback(void* panel, int returnCode)
118 int result = wxID_CANCEL;
120 if (returnCode == NSOKButton )
122 NSOpenPanel* oPanel = (NSOpenPanel*)panel;
123 SetPath( wxCFStringRef::AsString([[oPanel filenames] objectAtIndex:0]));
126 SetReturnCode(result);
128 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
129 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
132 #endif // wxUSE_DIRDLG