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"
 
  35 #include "wx/modalhook.h"
 
  37 #include "wx/osx/private.h"
 
  39 IMPLEMENT_CLASS(wxDirDialog, wxDialog)
 
  41 void wxDirDialog::Init()
 
  43     m_sheetDelegate = nil;
 
  46 void wxDirDialog::Create(wxWindow *parent, const wxString& message,
 
  47         const wxString& defaultPath, long style, const wxPoint& WXUNUSED(pos),
 
  48         const wxSize& WXUNUSED(size), const wxString& WXUNUSED(name))
 
  52     SetMessage( message );
 
  53     SetWindowStyle(style);
 
  55     m_sheetDelegate = [[ModalDialogDelegate alloc] init];
 
  56     [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
 
  59 wxDirDialog::~wxDirDialog()
 
  61     [m_sheetDelegate release];
 
  64 WX_NSOpenPanel wxDirDialog::OSXCreatePanel() const
 
  66     NSOpenPanel *oPanel = [NSOpenPanel openPanel];
 
  67     [oPanel setCanChooseDirectories:YES];
 
  68     [oPanel setResolvesAliases:YES];
 
  69     [oPanel setCanChooseFiles:NO];
 
  71     wxCFStringRef cf( m_message );
 
  72     [oPanel setMessage:cf.AsNSString()];
 
  74     if ( !HasFlag(wxDD_DIR_MUST_EXIST) )
 
  75         [oPanel setCanCreateDirectories:YES];
 
  80 void wxDirDialog::ShowWindowModal()
 
  82     wxNonOwnedWindow* parentWindow = NULL;
 
  85         parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
 
  87     wxCHECK_RET(parentWindow, "Window modal display requires parent.");
 
  89     m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
 
  91     NSOpenPanel *oPanel = OSXCreatePanel();
 
  93     NSWindow* nativeParent = parentWindow->GetWXWindow();
 
  94     wxCFStringRef dir( m_path );
 
  95     [oPanel beginSheetForDirectory:dir.AsNSString() file:nil types: nil
 
  96         modalForWindow: nativeParent modalDelegate: m_sheetDelegate
 
  97         didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
 
 101 int wxDirDialog::ShowModal()
 
 103     WX_HOOK_MODAL_DIALOG();
 
 105     wxCFEventLoopPauseIdleEvents pause;
 
 107     NSOpenPanel *oPanel = OSXCreatePanel();
 
 109     wxCFStringRef dir( m_path );
 
 111     m_path = wxEmptyString;
 
 115     returnCode = (NSInteger)[oPanel runModalForDirectory:dir.AsNSString() file:nil types:nil];
 
 116     ModalFinishedCallback(oPanel, returnCode);
 
 118     return GetReturnCode();
 
 121 void wxDirDialog::ModalFinishedCallback(void* panel, int returnCode)
 
 123     int result = wxID_CANCEL;
 
 125     if (returnCode == NSOKButton )
 
 127         NSOpenPanel* oPanel = (NSOpenPanel*)panel;
 
 128         SetPath( wxCFStringRef::AsStringWithNormalizationFormC([[oPanel filenames] objectAtIndex:0]));
 
 131     SetReturnCode(result);
 
 133     if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
 
 134         SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED  );
 
 137 #endif // wxUSE_DIRDLG