2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
23 #include "ImageInputType.h"
25 #include "FormDataList.h"
26 #include "HTMLFormElement.h"
27 #include "HTMLImageLoader.h"
28 #include "HTMLInputElement.h"
29 #include "MouseEvent.h"
30 #include "RenderImage.h"
31 #include <wtf/PassOwnPtr.h>
35 inline ImageInputType::ImageInputType(HTMLInputElement* element)
36 : BaseButtonInputType(element)
40 PassOwnPtr<InputType> ImageInputType::create(HTMLInputElement* element)
42 return adoptPtr(new ImageInputType(element));
45 const AtomicString& ImageInputType::formControlType() const
47 return InputTypeNames::image();
50 bool ImageInputType::isFormDataAppendable() const
55 bool ImageInputType::appendFormData(FormDataList& encoding, bool) const
57 if (!element()->isActivatedSubmit())
59 const AtomicString& name = element()->name();
61 encoding.appendData("x", m_clickLocation.x());
62 encoding.appendData("y", m_clickLocation.y());
66 DEFINE_STATIC_LOCAL(String, dotXString, (".x"));
67 DEFINE_STATIC_LOCAL(String, dotYString, (".y"));
68 encoding.appendData(name + dotXString, m_clickLocation.x());
69 encoding.appendData(name + dotYString, m_clickLocation.y());
71 if (!element()->value().isEmpty())
72 encoding.appendData(name, element()->value());
76 bool ImageInputType::supportsValidation() const
81 void ImageInputType::handleDOMActivateEvent(Event* event)
83 RefPtr<HTMLInputElement> element = this->element();
84 if (element->disabled() || !element->form())
86 element->setActivatedSubmit(true);
87 if (event->underlyingEvent() && event->underlyingEvent()->isMouseEvent()) {
88 MouseEvent* mouseEvent = static_cast<MouseEvent*>(event->underlyingEvent());
89 m_clickLocation = IntPoint(mouseEvent->offsetX(), mouseEvent->offsetY());
91 m_clickLocation = IntPoint();
92 element->form()->prepareForSubmission(event); // Event handlers can run.
93 element->setActivatedSubmit(false);
94 event->setDefaultHandled();
97 RenderObject* ImageInputType::createRenderer(RenderArena* arena, RenderStyle*) const
99 RenderImage* image = new (arena) RenderImage(element());
100 image->setImageResource(RenderImageResource::create());
104 void ImageInputType::altAttributeChanged()
106 RenderImage* image = toRenderImage(element()->renderer());
109 image->updateAltText();
112 void ImageInputType::srcAttributeChanged()
114 if (!element()->renderer())
117 m_imageLoader = adoptPtr(new HTMLImageLoader(element()));
118 m_imageLoader->updateFromElementIgnoringPreviousError();
121 void ImageInputType::attach()
123 BaseButtonInputType::attach();
126 m_imageLoader = adoptPtr(new HTMLImageLoader(element()));
127 m_imageLoader->updateFromElement();
129 RenderImage* renderer = toRenderImage(element()->renderer());
133 if (!m_imageLoader->haveFiredBeforeLoadEvent())
136 RenderImageResource* imageResource = renderer->imageResource();
137 imageResource->setCachedImage(m_imageLoader->image());
139 // If we have no image at all because we have no src attribute, set
140 // image height and width for the alt text instead.
141 if (!m_imageLoader->image() && !imageResource->cachedImage())
142 renderer->setImageSizeForAltText();
145 void ImageInputType::willMoveToNewOwnerDocument()
147 BaseButtonInputType::willMoveToNewOwnerDocument();
149 m_imageLoader->elementWillMoveToNewOwnerDocument();
152 bool ImageInputType::shouldRespectAlignAttribute()
157 bool ImageInputType::canBeSuccessfulSubmitButton()
162 bool ImageInputType::isImageButton() const
167 bool ImageInputType::isEnumeratable()
172 bool ImageInputType::shouldRespectHeightAndWidthAttributes()
177 } // namespace WebCore