db9151e4306951827b4f4aa281700dc4be950996
[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     PassRefPtr<FileChooser> fileChooser(new FileChooser(document, uploadControl));
91     return fileChooser;
92 }
93
94 FileChooser::FileChooser(Document* document, RenderFileUploadControl* uploadControl)
95     : m_document(document)
96     , m_icon(0)
97     , m_uploadControl(uploadControl)
98     , m_controller([[OpenPanelController alloc] initWithFileChooser:this])
99 {
100 }
101
102 FileChooser::~FileChooser()
103 {
104     [m_controller release];
105 }
106
107 void FileChooser::openFileChooser()
108 {
109     [m_controller beginSheet];
110 }
111
112 String FileChooser::basenameForWidth(int width) const
113 {
114     if (width <= 0)
115         return String();
116     
117     String strToTruncate;
118     if (m_filename.isEmpty())
119         strToTruncate = fileButtonNoFileSelectedLabel();
120     else
121         strToTruncate = [[NSFileManager defaultManager] displayNameAtPath:m_filename];
122     
123     return [WebCoreStringTruncator centerTruncateString:strToTruncate
124             toWidth:width withFont:m_uploadControl->style()->font().primaryFont()->getNSFont()];
125 }
126
127 void FileChooser::uploadControlDetaching()
128 {
129     if (m_controller)
130         [m_controller fileChooserDetachingSoon];
131 }
132
133 void FileChooser::chooseFile(const String& filename)
134 {
135     if (m_filename == filename)
136         return;
137     
138     m_filename = filename;
139     
140     // Need unsigned 0 here to disambiguate String::operator[] from operator(NSString*, int)[]
141     if (!m_filename.length() || m_filename[0U] != '/')
142         m_icon = 0;
143     else
144         m_icon = Icon::newIconForFile(m_filename);
145     
146     uploadControl()->valueChanged();
147 }
148
149 }