1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "RenderInputSpeech.h"
33
34#if ENABLE(INPUT_SPEECH)
35
36#include "GraphicsContext.h"
37#include "HTMLNames.h"
38#include "PaintInfo.h"
39#include "RenderBox.h"
40#include "TextControlInnerElements.h"
41
42namespace WebCore {
43
44static const float defaultControlFontPixelSize = 13;
45static const float defaultSpeechButtonSize = 16;
46static const float minSpeechButtonSize = 8;
47static const float maxSpeechButtonSize = 40;
48
49void RenderInputSpeech::adjustInputFieldSpeechButtonStyle(CSSStyleSelector*, RenderStyle* style, Element*)
50{
51    // Scale the button size based on the font size.
52    float fontScale = style->fontSize() / defaultControlFontPixelSize;
53    int speechButtonSize = lroundf(std::min(std::max(minSpeechButtonSize, defaultSpeechButtonSize * fontScale), maxSpeechButtonSize));
54    style->setWidth(Length(speechButtonSize, Fixed));
55    style->setHeight(Length(speechButtonSize, Fixed));
56}
57
58bool RenderInputSpeech::paintInputFieldSpeechButton(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
59{
60    Element* element = object->node()->isElementNode() ? toElement(object->node()) : 0;
61    if (!element || !element->isInputFieldSpeechButtonElement())
62        return false;
63
64    // Get the renderer of <input> element.
65    Node* input = object->node()->shadowAncestorNode();
66    if (!input->renderer()->isBox())
67        return false;
68    RenderBox* inputRenderBox = toRenderBox(input->renderer());
69    IntRect inputContentBox = inputRenderBox->contentBoxRect();
70
71    // Make sure the scaled button stays square and will fit in its parent's box.
72    int buttonSize = std::min(inputContentBox.width(), std::min(inputContentBox.height(), rect.height()));
73    // Calculate button's coordinates relative to the input element.
74    // Center the button vertically.  Round up though, so if it has to be one pixel off-center, it will
75    // be one pixel closer to the bottom of the field.  This tends to look better with the text.
76    IntRect buttonRect(object->offsetFromAncestorContainer(inputRenderBox).width(),
77                       inputContentBox.y() + (inputContentBox.height() - buttonSize + 1) / 2,
78                       buttonSize, buttonSize);
79
80    // Compute an offset between the part renderer and the input renderer.
81    IntSize offsetFromInputRenderer = -(object->offsetFromAncestorContainer(inputRenderBox));
82    // Move the rect into partRenderer's coords.
83    buttonRect.move(offsetFromInputRenderer);
84    // Account for the local drawing offset.
85    buttonRect.move(rect.x(), rect.y());
86
87    DEFINE_STATIC_LOCAL(RefPtr<Image>, imageStateNormal, (Image::loadPlatformResource("inputSpeech")));
88    DEFINE_STATIC_LOCAL(RefPtr<Image>, imageStateRecording, (Image::loadPlatformResource("inputSpeechRecording")));
89    DEFINE_STATIC_LOCAL(RefPtr<Image>, imageStateWaiting, (Image::loadPlatformResource("inputSpeechWaiting")));
90
91    InputFieldSpeechButtonElement* speechButton = toInputFieldSpeechButtonElement(element);
92    Image* image = imageStateNormal.get();
93    if (speechButton->state() == InputFieldSpeechButtonElement::Recording)
94        image = imageStateRecording.get();
95    else if (speechButton->state() == InputFieldSpeechButtonElement::Recognizing)
96        image = imageStateWaiting.get();
97    paintInfo.context->drawImage(image, object->style()->colorSpace(), buttonRect);
98
99    return false;
100}
101
102} // namespace WebCore
103
104#endif // ENABLE(INPUT_SPEECH)
105