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#if ENABLE(INPUT_SPEECH) 33#include "RenderInputSpeech.h" 34 35#include "GraphicsContext.h" 36#include "HTMLNames.h" 37#include "PaintInfo.h" 38#include "RenderBox.h" 39#include "TextControlInnerElements.h" 40 41namespace WebCore { 42 43static const float defaultControlFontPixelSize = 13; 44static const float defaultSpeechButtonSize = 16; 45static const float minSpeechButtonSize = 8; 46static const float maxSpeechButtonSize = 40; 47 48void RenderInputSpeech::adjustInputFieldSpeechButtonStyle(StyleResolver*, RenderStyle* style, Element*) 49{ 50 // Scale the button size based on the font size. 51 float fontScale = style->fontSize() / defaultControlFontPixelSize; 52 int speechButtonSize = lroundf(std::min(std::max(minSpeechButtonSize, defaultSpeechButtonSize * fontScale), maxSpeechButtonSize)); 53 style->setWidth(Length(speechButtonSize, Fixed)); 54 style->setHeight(Length(speechButtonSize, Fixed)); 55} 56 57bool RenderInputSpeech::paintInputFieldSpeechButton(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect) 58{ 59 Element* element = object->node()->isElementNode() ? toElement(object->node()) : 0; 60 if (!element || !element->isInputFieldSpeechButtonElement()) 61 return false; 62 63 // Get the renderer of <input> element. 64 Node* input = object->node()->shadowHost(); 65 if (!input->renderer()->isBox()) 66 return false; 67 RenderBox* inputRenderBox = toRenderBox(input->renderer()); 68 LayoutRect inputContentBox = inputRenderBox->contentBoxRect(); 69 70 // Make sure the scaled button stays square and will fit in its parent's box. 71 LayoutUnit buttonSize = std::min(inputContentBox.width(), std::min<LayoutUnit>(inputContentBox.height(), rect.height())); 72 // Calculate button's coordinates relative to the input element. 73 // Center the button vertically. Round up though, so if it has to be one pixel off-center, it will 74 // be one pixel closer to the bottom of the field. This tends to look better with the text. 75 LayoutRect buttonRect(object->offsetFromAncestorContainer(inputRenderBox).width(), 76 inputContentBox.y() + (inputContentBox.height() - buttonSize + 1) / 2, 77 buttonSize, buttonSize); 78 79 // Compute an offset between the part renderer and the input renderer. 80 LayoutSize offsetFromInputRenderer = -(object->offsetFromAncestorContainer(inputRenderBox)); 81 // Move the rect into partRenderer's coords. 82 buttonRect.move(offsetFromInputRenderer); 83 // Account for the local drawing offset. 84 buttonRect.moveBy(rect.location()); 85 86 DEPRECATED_DEFINE_STATIC_LOCAL(RefPtr<Image>, imageStateNormal, (Image::loadPlatformResource("inputSpeech"))); 87 DEPRECATED_DEFINE_STATIC_LOCAL(RefPtr<Image>, imageStateRecording, (Image::loadPlatformResource("inputSpeechRecording"))); 88 DEPRECATED_DEFINE_STATIC_LOCAL(RefPtr<Image>, imageStateWaiting, (Image::loadPlatformResource("inputSpeechWaiting"))); 89 90 InputFieldSpeechButtonElement* speechButton = toInputFieldSpeechButtonElement(element); 91 Image* image = imageStateNormal.get(); 92 if (speechButton->state() == InputFieldSpeechButtonElement::Recording) 93 image = imageStateRecording.get(); 94 else if (speechButton->state() == InputFieldSpeechButtonElement::Recognizing) 95 image = imageStateWaiting.get(); 96 paintInfo.context->drawImage(image, object->style().colorSpace(), pixelSnappedIntRect(buttonRect)); 97 98 return false; 99} 100 101} // namespace WebCore 102 103#endif // ENABLE(INPUT_SPEECH) 104