initial import
[vuplus_webkit] / Source / WebCore / html / ImageInputType.cpp
1 /*
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.
4  *
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.
9  *
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.
14  *
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.
19  *
20  */
21
22 #include "config.h"
23 #include "ImageInputType.h"
24
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>
32
33 namespace WebCore {
34
35 inline ImageInputType::ImageInputType(HTMLInputElement* element)
36     : BaseButtonInputType(element)
37 {
38 }
39
40 PassOwnPtr<InputType> ImageInputType::create(HTMLInputElement* element)
41 {
42     return adoptPtr(new ImageInputType(element));
43 }
44
45 const AtomicString& ImageInputType::formControlType() const
46 {
47     return InputTypeNames::image();
48 }
49
50 bool ImageInputType::isFormDataAppendable() const
51 {
52     return true;
53 }
54
55 bool ImageInputType::appendFormData(FormDataList& encoding, bool) const
56 {
57     if (!element()->isActivatedSubmit())
58         return false;
59     const AtomicString& name = element()->name();
60     if (name.isEmpty()) {
61         encoding.appendData("x", m_clickLocation.x());
62         encoding.appendData("y", m_clickLocation.y());
63         return true;
64     }
65
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());
70
71     if (!element()->value().isEmpty())
72         encoding.appendData(name, element()->value());
73     return true;
74 }
75
76 bool ImageInputType::supportsValidation() const
77 {
78     return false;
79 }
80
81 void ImageInputType::handleDOMActivateEvent(Event* event)
82 {
83     RefPtr<HTMLInputElement> element = this->element();
84     if (element->disabled() || !element->form())
85         return;
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());
90     } else
91         m_clickLocation = IntPoint();
92     element->form()->prepareForSubmission(event); // Event handlers can run.
93     element->setActivatedSubmit(false);
94     event->setDefaultHandled();
95 }
96
97 RenderObject* ImageInputType::createRenderer(RenderArena* arena, RenderStyle*) const
98 {
99     RenderImage* image = new (arena) RenderImage(element());
100     image->setImageResource(RenderImageResource::create());
101     return image;
102 }
103
104 void ImageInputType::altAttributeChanged()
105 {
106     RenderImage* image = toRenderImage(element()->renderer());
107     if (!image)
108         return;
109     image->updateAltText();
110 }
111
112 void ImageInputType::srcAttributeChanged()
113 {
114     if (!element()->renderer())
115         return;
116     if (!m_imageLoader)
117         m_imageLoader = adoptPtr(new HTMLImageLoader(element()));
118     m_imageLoader->updateFromElementIgnoringPreviousError();
119 }
120
121 void ImageInputType::attach()
122 {
123     BaseButtonInputType::attach();
124
125     if (!m_imageLoader)
126         m_imageLoader = adoptPtr(new HTMLImageLoader(element()));
127     m_imageLoader->updateFromElement();
128
129     RenderImage* renderer = toRenderImage(element()->renderer());
130     if (!renderer)
131         return;
132
133     if (!m_imageLoader->haveFiredBeforeLoadEvent())
134         return;
135
136     RenderImageResource* imageResource = renderer->imageResource();
137     imageResource->setCachedImage(m_imageLoader->image()); 
138
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();
143 }
144
145 void ImageInputType::willMoveToNewOwnerDocument()
146 {
147     BaseButtonInputType::willMoveToNewOwnerDocument();
148     if (m_imageLoader)
149         m_imageLoader->elementWillMoveToNewOwnerDocument();
150 }
151
152 bool ImageInputType::shouldRespectAlignAttribute()
153 {
154     return true;
155 }
156
157 bool ImageInputType::canBeSuccessfulSubmitButton()
158 {
159     return true;
160 }
161
162 bool ImageInputType::isImageButton() const
163 {
164     return true;
165 }
166
167 bool ImageInputType::isEnumeratable()
168 {
169     return false;
170 }
171
172 bool ImageInputType::shouldRespectHeightAndWidthAttributes()
173 {
174     return true;
175 }
176
177 } // namespace WebCore