1/////////////////////////////////////////////////////////////////////////////
2// Name:        src/cocoa/dirdlg.mm
3// Purpose:     wxDirDialog for wxCocoa
4// Author:      Ryan Norton
5// Modified by: Hiroyuki Nakamura(maloninc)
6// Created:     2006-01-10
7// RCS-ID:      $Id: dirdlg.mm 40007 2006-07-05 13:10:46Z SC $
8// Copyright:   (c) Ryan Norton
9// Licence:     wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#if wxUSE_DIRDLG
24
25#include "wx/dirdlg.h"
26
27#ifndef WX_PRECOMP
28    #include "wx/msgdlg.h"
29    #include "wx/filedlg.h"
30    #include "wx/app.h"
31#endif
32
33#include "wx/filename.h"
34
35#include "wx/cocoa/autorelease.h"
36#include "wx/cocoa/string.h"
37
38#import <AppKit/NSOpenPanel.h>
39#import <AppKit/NSSavePanel.h>
40
41#import <Foundation/NSArray.h>
42// ============================================================================
43// implementation
44// ============================================================================
45
46IMPLEMENT_CLASS(wxCocoaDirDialog, wxDialog)
47
48// ----------------------------------------------------------------------------
49// wxDirDialog
50// ----------------------------------------------------------------------------
51
52wxDirDialog::wxDirDialog(wxWindow *parent, const wxString& message,
53        const wxString& defaultPath, long style, const wxPoint& pos,
54        const wxSize& size, const wxString& name)
55{
56    wxTopLevelWindows.Append(this);
57
58    m_message = message;
59
60    SetWindowStyle(style);
61    m_parent = parent;
62    m_path = defaultPath;
63
64    wxASSERT(CreateBase(parent,wxID_ANY,pos,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
65
66    if ( parent )
67        parent->AddChild(this);
68
69    m_cocoaNSWindow = nil;
70    m_cocoaNSView = nil;
71
72    //If the user requests to save - use a NSSavePanel
73    //else use a NSOpenPanel
74    if (HasFlag(wxFD_SAVE))
75    {
76        SetNSPanel([NSSavePanel savePanel]);
77
78        [GetNSSavePanel() setTitle:wxNSStringWithWxString(message)];
79
80        [GetNSSavePanel() setPrompt:@"Save"];
81        [GetNSSavePanel() setTreatsFilePackagesAsDirectories:YES];
82        [GetNSSavePanel() setCanSelectHiddenExtension:YES];
83    }
84    else //m_dialogStyle & wxFD_OPEN
85    {
86        SetNSPanel([NSOpenPanel openPanel]);
87        [m_cocoaNSWindow setTitle:wxNSStringWithWxString(message)];
88
89        [(NSOpenPanel*)m_cocoaNSWindow setResolvesAliases:YES];
90        [(NSOpenPanel*)m_cocoaNSWindow setCanChooseFiles:NO];
91        [(NSOpenPanel*)m_cocoaNSWindow setCanChooseDirectories:YES];
92        [GetNSSavePanel() setPrompt:@"Open"];
93    }
94
95    if (HasFlag(wxDD_NEW_DIR_BUTTON)) //m_dialogStyle & wxDD_NEW_DIR_BUTTON
96    {
97        [(NSOpenPanel*)m_cocoaNSWindow setCanCreateDirectories:YES];
98    }
99}
100
101wxDirDialog::~wxDirDialog()
102{
103}
104
105int wxDirDialog::ShowModal()
106{
107    wxAutoNSAutoreleasePool thePool;
108
109    m_fileNames.Empty();
110
111    int nResult;
112
113    if (HasFlag(wxFD_SAVE))
114    {
115        nResult = [GetNSSavePanel()
116                    runModalForDirectory:wxNSStringWithWxString(m_dir)
117                    file:wxNSStringWithWxString(m_fileName)];
118
119        if (nResult == NSOKButton)
120        {
121            m_fileNames.Add(wxStringWithNSString([GetNSSavePanel() filename]));
122            m_path = m_fileNames[0];
123        }
124    }
125    else //m_dialogStyle & wxFD_OPEN
126    {
127        nResult = [(NSOpenPanel*)m_cocoaNSWindow
128                    runModalForDirectory:wxNSStringWithWxString(m_dir)
129                    file:wxNSStringWithWxString(m_fileName)
130                    types:NULL];
131
132        if (nResult == NSOKButton)
133        {
134            for(unsigned i = 0; i < [[(NSOpenPanel*)m_cocoaNSWindow filenames] count]; ++i)
135            {
136                m_fileNames.Add(wxStringWithNSString([[(NSOpenPanel*)m_cocoaNSWindow filenames] objectAtIndex:(i)]));
137            }
138
139            m_path = m_fileNames[0];
140        }
141    }
142
143    return nResult == NSOKButton ? wxID_OK : wxID_CANCEL;
144}
145
146#endif // wxUSE_DIRDLG
147