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// Copyright:   (c) Ryan Norton
8// Licence:     wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#if wxUSE_DIRDLG
23
24#include "wx/dirdlg.h"
25
26#ifndef WX_PRECOMP
27    #include "wx/msgdlg.h"
28    #include "wx/filedlg.h"
29    #include "wx/app.h"
30#endif
31
32#include "wx/filename.h"
33#include "wx/modalhook.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    WX_HOOK_MODAL_DIALOG();
108
109    wxAutoNSAutoreleasePool thePool;
110
111    m_fileNames.Empty();
112
113    int nResult;
114
115    if (HasFlag(wxFD_SAVE))
116    {
117        nResult = [GetNSSavePanel()
118                    runModalForDirectory:wxNSStringWithWxString(m_dir)
119                    file:wxNSStringWithWxString(m_fileName)];
120
121        if (nResult == NSOKButton)
122        {
123            m_fileNames.Add(wxStringWithNSString([GetNSSavePanel() filename]));
124            m_path = m_fileNames[0];
125        }
126    }
127    else //m_dialogStyle & wxFD_OPEN
128    {
129        nResult = [(NSOpenPanel*)m_cocoaNSWindow
130                    runModalForDirectory:wxNSStringWithWxString(m_dir)
131                    file:wxNSStringWithWxString(m_fileName)
132                    types:NULL];
133
134        if (nResult == NSOKButton)
135        {
136            for(unsigned i = 0; i < [[(NSOpenPanel*)m_cocoaNSWindow filenames] count]; ++i)
137            {
138                m_fileNames.Add(wxStringWithNSString([[(NSOpenPanel*)m_cocoaNSWindow filenames] objectAtIndex:(i)]));
139            }
140
141            m_path = m_fileNames[0];
142        }
143    }
144
145    return nResult == NSOKButton ? wxID_OK : wxID_CANCEL;
146}
147
148#endif // wxUSE_DIRDLG
149