Rubberstamped by Anders.
[WebKit-https.git] / WebCore / platform / mac / FileChooserMac.mm
1 /*
2  * Copyright (C) 2006 Apple Computer, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  */
20
21 #import "config.h"
22 #import "FileChooser.h"
23
24 #import <AppKit/NSOpenPanel.h>
25 #import "Document.h"
26 #import "FontData.h"
27 #import "FrameMac.h"
28 #import "Icon.h"
29 #import "LocalizedStrings.h"
30 #import "RenderFileUploadControl.h"
31 #import "WebCoreFrameBridge.h"
32 #import "WebCoreStringTruncator.h"
33
34 using namespace WebCore;
35
36 @interface OpenPanelController : NSObject <WebCoreOpenPanelResultListener> {
37     FileChooser *_fileChooser;
38     WebCoreFrameBridge *_bridge;
39 }
40 - (id)initWithFileChooser:(FileChooser *)fileChooser;
41 - (void)fileChooserDetachingSoon;
42 - (void)beginSheet;
43 @end
44
45 @implementation OpenPanelController
46
47 - (id)initWithFileChooser:(FileChooser *)fileChooser
48 {
49     self = [super init];
50     if (!self)
51         return nil;
52     
53     _fileChooser = fileChooser;
54     return self;
55 }
56
57 - (void)fileChooserDetachingSoon
58 {
59     _fileChooser = 0;
60 }
61
62 - (void)beginSheet
63 {
64     if (!_fileChooser)
65         return;
66     
67     _bridge = Mac(_fileChooser->document()->frame())->bridge();
68     [_bridge retain];
69     [_bridge runOpenPanelForFileButtonWithResultListener:self];
70 }
71
72 - (void)chooseFilename:(NSString *)filename
73 {
74     if (_fileChooser)
75         _fileChooser->chooseFile(filename);
76     [_bridge release];
77 }
78
79 - (void)cancel
80 {
81     [_bridge release];
82 }
83
84 @end
85
86 namespace WebCore {
87     
88 PassRefPtr<FileChooser> FileChooser::create(Document* document, RenderFileUploadControl* uploadControl)
89 {
90     return new FileChooser(document, uploadControl);
91 }
92
93 FileChooser::FileChooser(Document* document, RenderFileUploadControl* uploadControl)
94     : m_document(document)
95     , m_icon(0)
96     , m_uploadControl(uploadControl)
97     , m_controller([[OpenPanelController alloc] initWithFileChooser:this])
98 {
99 }
100
101 FileChooser::~FileChooser()
102 {
103     [m_controller release];
104 }
105
106 void FileChooser::openFileChooser()
107 {
108     [m_controller beginSheet];
109 }
110
111 String FileChooser::basenameForWidth(int width) const
112 {
113     if (width <= 0)
114         return String();
115     
116     String strToTruncate;
117     if (m_filename.isEmpty())
118         strToTruncate = fileButtonNoFileSelectedLabel();
119     else
120         strToTruncate = [[NSFileManager defaultManager] displayNameAtPath:m_filename];
121     
122     return [WebCoreStringTruncator centerTruncateString:strToTruncate
123             toWidth:width withFont:m_uploadControl->style()->font().primaryFont()->getNSFont()];
124 }
125
126 void FileChooser::uploadControlDetaching()
127 {
128     if (m_controller)
129         [m_controller fileChooserDetachingSoon];
130 }
131
132 void FileChooser::chooseFile(const String& filename)
133 {
134     if (m_filename == filename)
135         return;
136     
137     m_filename = filename;
138     
139     // Need unsigned 0 here to disambiguate String::operator[] from operator(NSString*, int)[]
140     if (!m_filename.length() || m_filename[0U] != '/')
141         m_icon = 0;
142     else
143         m_icon = Icon::newIconForFile(m_filename);
144     
145     uploadControl()->valueChanged();
146 }
147
148 }