1 /*
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
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., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21 #include "config.h"
22 #include "RenderFileUploadControl.h"
23
24 #include "Chrome.h"
25 #include "FileList.h"
26 #include "Frame.h"
27 #include "FrameView.h"
28 #include "GraphicsContext.h"
29 #include "HTMLInputElement.h"
30 #include "HTMLNames.h"
31 #include "ShadowElement.h"
32 #include "Icon.h"
33 #include "LocalizedStrings.h"
34 #include "Page.h"
35 #include "PaintInfo.h"
36 #include "RenderButton.h"
37 #include "RenderText.h"
38 #include "RenderTheme.h"
39 #include "RenderView.h"
40 #include "TextRun.h"
41 #include <math.h>
42
43 using namespace std;
44
45 namespace WebCore {
46
47 using namespace HTMLNames;
48
49 const int afterButtonSpacing = 4;
50 const int iconHeight = 16;
51 const int iconWidth = 16;
52 const int iconFilenameSpacing = 2;
53 const int defaultWidthNumChars = 34;
54 const int buttonShadowHeight = 2;
55
RenderFileUploadControl(HTMLInputElement * input)56 RenderFileUploadControl::RenderFileUploadControl(HTMLInputElement* input)
57 : RenderBlock(input)
58 {
59 FileList* list = input->files();
60 Vector<String> filenames;
61 unsigned length = list ? list->length() : 0;
62 for (unsigned i = 0; i < length; ++i)
63 filenames.append(list->item(i)->path());
64 m_fileChooser = FileChooser::create(this, filenames);
65 }
66
~RenderFileUploadControl()67 RenderFileUploadControl::~RenderFileUploadControl()
68 {
69 if (m_button)
70 m_button->detach();
71 m_fileChooser->disconnectClient();
72 }
73
styleDidChange(StyleDifference diff,const RenderStyle * oldStyle)74 void RenderFileUploadControl::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
75 {
76 RenderBlock::styleDidChange(diff, oldStyle);
77 if (m_button)
78 m_button->renderer()->setStyle(createButtonStyle(style()));
79 }
80
valueChanged()81 void RenderFileUploadControl::valueChanged()
82 {
83 // dispatchFormControlChangeEvent may destroy this renderer
84 RefPtr<FileChooser> fileChooser = m_fileChooser;
85
86 HTMLInputElement* inputElement = static_cast<HTMLInputElement*>(node());
87 inputElement->setFileListFromRenderer(fileChooser->filenames());
88 inputElement->dispatchFormControlChangeEvent();
89
90 // only repaint if it doesn't seem we have been destroyed
91 if (!fileChooser->disconnected())
92 repaint();
93 }
94
allowsMultipleFiles()95 bool RenderFileUploadControl::allowsMultipleFiles()
96 {
97 #if ENABLE(DIRECTORY_UPLOAD)
98 if (allowsDirectoryUpload())
99 return true;
100 #endif
101
102 HTMLInputElement* input = static_cast<HTMLInputElement*>(node());
103 return input->fastHasAttribute(multipleAttr);
104 }
105
106 #if ENABLE(DIRECTORY_UPLOAD)
allowsDirectoryUpload()107 bool RenderFileUploadControl::allowsDirectoryUpload()
108 {
109 HTMLInputElement* input = static_cast<HTMLInputElement*>(node());
110 return input->fastHasAttribute(webkitdirectoryAttr);
111 }
112
receiveDropForDirectoryUpload(const Vector<String> & paths)113 void RenderFileUploadControl::receiveDropForDirectoryUpload(const Vector<String>& paths)
114 {
115 if (Chrome* chromePointer = chrome())
116 chromePointer->enumerateChosenDirectory(paths[0], m_fileChooser.get());
117 }
118 #endif
119
acceptTypes()120 String RenderFileUploadControl::acceptTypes()
121 {
122 return static_cast<HTMLInputElement*>(node())->accept();
123 }
124
chooseIconForFiles(FileChooser * chooser,const Vector<String> & filenames)125 void RenderFileUploadControl::chooseIconForFiles(FileChooser* chooser, const Vector<String>& filenames)
126 {
127 if (Chrome* chromePointer = chrome())
128 chromePointer->chooseIconForFiles(filenames, chooser);
129 }
130
click()131 void RenderFileUploadControl::click()
132 {
133 // Requires a user gesture to open the file dialog.
134 if (!frame() || !frame()->loader()->isProcessingUserGesture())
135 return;
136 if (Chrome* chromePointer = chrome())
137 chromePointer->runOpenPanel(frame(), m_fileChooser);
138 }
139
chrome() const140 Chrome* RenderFileUploadControl::chrome() const
141 {
142 Frame* frame = node()->document()->frame();
143 if (!frame)
144 return 0;
145 Page* page = frame->page();
146 if (!page)
147 return 0;
148 return page->chrome();
149 }
150
updateFromElement()151 void RenderFileUploadControl::updateFromElement()
152 {
153 HTMLInputElement* inputElement = static_cast<HTMLInputElement*>(node());
154 ASSERT(inputElement->isFileUpload());
155
156 if (!m_button) {
157 m_button = ShadowInputElement::create(inputElement);
158 m_button->setType("button");
159 m_button->setValue(fileButtonChooseFileLabel());
160 RefPtr<RenderStyle> buttonStyle = createButtonStyle(style());
161 RenderObject* renderer = m_button->createRenderer(renderArena(), buttonStyle.get());
162 m_button->setRenderer(renderer);
163 renderer->setStyle(buttonStyle.release());
164 renderer->updateFromElement();
165 m_button->setAttached();
166 m_button->setInDocument();
167
168 addChild(renderer);
169 }
170
171 m_button->setDisabled(!theme()->isEnabled(this));
172
173 // This only supports clearing out the files, but that's OK because for
174 // security reasons that's the only change the DOM is allowed to make.
175 FileList* files = inputElement->files();
176 ASSERT(files);
177 if (files && files->isEmpty() && !m_fileChooser->filenames().isEmpty()) {
178 m_fileChooser->clear();
179 repaint();
180 }
181 }
182
maxFilenameWidth() const183 int RenderFileUploadControl::maxFilenameWidth() const
184 {
185 return max(0, contentWidth() - m_button->renderBox()->width() - afterButtonSpacing
186 - (m_fileChooser->icon() ? iconWidth + iconFilenameSpacing : 0));
187 }
188
createButtonStyle(const RenderStyle * parentStyle) const189 PassRefPtr<RenderStyle> RenderFileUploadControl::createButtonStyle(const RenderStyle* parentStyle) const
190 {
191 RefPtr<RenderStyle> style = getCachedPseudoStyle(FILE_UPLOAD_BUTTON);
192 if (!style) {
193 style = RenderStyle::create();
194 if (parentStyle)
195 style->inheritFrom(parentStyle);
196 }
197
198 // Button text will wrap on file upload controls with widths smaller than the intrinsic button width
199 // without this setWhiteSpace.
200 style->setWhiteSpace(NOWRAP);
201
202 return style.release();
203 }
204
paintObject(PaintInfo & paintInfo,int tx,int ty)205 void RenderFileUploadControl::paintObject(PaintInfo& paintInfo, int tx, int ty)
206 {
207 if (style()->visibility() != VISIBLE)
208 return;
209 ASSERT(m_fileChooser);
210
211 // Push a clip.
212 GraphicsContextStateSaver stateSaver(*paintInfo.context, false);
213 if (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseChildBlockBackgrounds) {
214 IntRect clipRect(tx + borderLeft(), ty + borderTop(),
215 width() - borderLeft() - borderRight(), height() - borderBottom() - borderTop() + buttonShadowHeight);
216 if (clipRect.isEmpty())
217 return;
218 stateSaver.save();
219 paintInfo.context->clip(clipRect);
220 }
221
222 if (paintInfo.phase == PaintPhaseForeground) {
223 const String& displayedFilename = fileTextValue();
224 unsigned length = displayedFilename.length();
225 const UChar* string = displayedFilename.characters();
226 TextRun textRun(string, length, false, 0, 0, TextRun::AllowTrailingExpansion, style()->direction(), style()->unicodeBidi() == Override);
227
228 // Determine where the filename should be placed
229 int contentLeft = tx + borderLeft() + paddingLeft();
230 int buttonAndIconWidth = m_button->renderBox()->width() + afterButtonSpacing
231 + (m_fileChooser->icon() ? iconWidth + iconFilenameSpacing : 0);
232 int textX;
233 if (style()->isLeftToRightDirection())
234 textX = contentLeft + buttonAndIconWidth;
235 else
236 textX = contentLeft + contentWidth() - buttonAndIconWidth - style()->font().width(textRun);
237 // We want to match the button's baseline
238 RenderButton* buttonRenderer = toRenderButton(m_button->renderer());
239 int textY = buttonRenderer->absoluteBoundingBoxRect().y()
240 + buttonRenderer->marginTop() + buttonRenderer->borderTop() + buttonRenderer->paddingTop()
241 + buttonRenderer->baselinePosition(AlphabeticBaseline, true, HorizontalLine, PositionOnContainingLine);
242
243 paintInfo.context->setFillColor(style()->visitedDependentColor(CSSPropertyColor), style()->colorSpace());
244
245 // Draw the filename
246 paintInfo.context->drawBidiText(style()->font(), textRun, IntPoint(textX, textY));
247
248 if (m_fileChooser->icon()) {
249 // Determine where the icon should be placed
250 int iconY = ty + borderTop() + paddingTop() + (contentHeight() - iconHeight) / 2;
251 int iconX;
252 if (style()->isLeftToRightDirection())
253 iconX = contentLeft + m_button->renderBox()->width() + afterButtonSpacing;
254 else
255 iconX = contentLeft + contentWidth() - m_button->renderBox()->width() - afterButtonSpacing - iconWidth;
256
257 // Draw the file icon
258 m_fileChooser->icon()->paint(paintInfo.context, IntRect(iconX, iconY, iconWidth, iconHeight));
259 }
260 }
261
262 // Paint the children.
263 RenderBlock::paintObject(paintInfo, tx, ty);
264 }
265
computePreferredLogicalWidths()266 void RenderFileUploadControl::computePreferredLogicalWidths()
267 {
268 ASSERT(preferredLogicalWidthsDirty());
269
270 m_minPreferredLogicalWidth = 0;
271 m_maxPreferredLogicalWidth = 0;
272
273 if (style()->width().isFixed() && style()->width().value() > 0)
274 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = computeContentBoxLogicalWidth(style()->width().value());
275 else {
276 // Figure out how big the filename space needs to be for a given number of characters
277 // (using "0" as the nominal character).
278 const UChar ch = '0';
279 float charWidth = style()->font().width(TextRun(&ch, 1, false, 0, 0, TextRun::AllowTrailingExpansion));
280 m_maxPreferredLogicalWidth = (int)ceilf(charWidth * defaultWidthNumChars);
281 }
282
283 if (style()->minWidth().isFixed() && style()->minWidth().value() > 0) {
284 m_maxPreferredLogicalWidth = max(m_maxPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->minWidth().value()));
285 m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->minWidth().value()));
286 } else if (style()->width().isPercent() || (style()->width().isAuto() && style()->height().isPercent()))
287 m_minPreferredLogicalWidth = 0;
288 else
289 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth;
290
291 if (style()->maxWidth().isFixed() && style()->maxWidth().value() != undefinedLength) {
292 m_maxPreferredLogicalWidth = min(m_maxPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->maxWidth().value()));
293 m_minPreferredLogicalWidth = min(m_minPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->maxWidth().value()));
294 }
295
296 int toAdd = borderAndPaddingWidth();
297 m_minPreferredLogicalWidth += toAdd;
298 m_maxPreferredLogicalWidth += toAdd;
299
300 setPreferredLogicalWidthsDirty(false);
301 }
302
positionForPoint(const IntPoint &)303 VisiblePosition RenderFileUploadControl::positionForPoint(const IntPoint&)
304 {
305 return VisiblePosition();
306 }
307
receiveDroppedFiles(const Vector<String> & paths)308 void RenderFileUploadControl::receiveDroppedFiles(const Vector<String>& paths)
309 {
310 #if ENABLE(DIRECTORY_UPLOAD)
311 if (allowsDirectoryUpload()) {
312 receiveDropForDirectoryUpload(paths);
313 return;
314 }
315 #endif
316
317 if (allowsMultipleFiles())
318 m_fileChooser->chooseFiles(paths);
319 else
320 m_fileChooser->chooseFile(paths[0]);
321 }
322
buttonValue()323 String RenderFileUploadControl::buttonValue()
324 {
325 if (!m_button)
326 return String();
327
328 return m_button->value();
329 }
330
fileTextValue() const331 String RenderFileUploadControl::fileTextValue() const
332 {
333 return m_fileChooser->basenameForWidth(style()->font(), maxFilenameWidth());
334 }
335
336 } // namespace WebCore
337