1/*
2 * Copyright (C) 2012 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#import "config.h"
27#import "PDFPluginTextAnnotation.h"
28
29#if ENABLE(PDFKIT_PLUGIN)
30
31#import "PDFAnnotationTextWidgetDetails.h"
32#import "PDFKitImports.h"
33#import "PDFLayerControllerDetails.h"
34#import "PDFPlugin.h"
35#import <PDFKit/PDFKit.h>
36#import <WebCore/CSSPrimitiveValue.h>
37#import <WebCore/CSSPropertyNames.h>
38#import <WebCore/ColorMac.h>
39#import <WebCore/Event.h>
40#import <WebCore/HTMLElement.h>
41#import <WebCore/HTMLInputElement.h>
42#import <WebCore/HTMLNames.h>
43#import <WebCore/HTMLTextAreaElement.h>
44#import <WebCore/KeyboardEvent.h>
45#import <WebCore/Page.h>
46
47using namespace WebCore;
48
49namespace WebKit {
50
51using namespace HTMLNames;
52
53static const String cssAlignmentValueForNSTextAlignment(NSTextAlignment alignment)
54{
55    switch (alignment) {
56    case NSLeftTextAlignment:
57        return "left";
58    case NSRightTextAlignment:
59        return "right";
60    case NSCenterTextAlignment:
61        return "center";
62    case NSJustifiedTextAlignment:
63        return "justify";
64    case NSNaturalTextAlignment:
65        return "-webkit-start";
66    }
67
68    ASSERT_NOT_REACHED();
69    return String();
70}
71
72PassRefPtr<PDFPluginTextAnnotation> PDFPluginTextAnnotation::create(PDFAnnotation *annotation, PDFLayerController *pdfLayerController, PDFPlugin* plugin)
73{
74    return adoptRef(new PDFPluginTextAnnotation(annotation, pdfLayerController, plugin));
75}
76
77PDFPluginTextAnnotation::~PDFPluginTextAnnotation()
78{
79    element()->removeEventListener(eventNames().keydownEvent, eventListener(), false);
80}
81
82PassRefPtr<Element> PDFPluginTextAnnotation::createAnnotationElement()
83{
84    RefPtr<Element> element;
85
86    Document& document = parent()->document();
87    PDFAnnotationTextWidget *textAnnotation = this->textAnnotation();
88    bool isMultiline = textAnnotation.isMultiline;
89
90    if (isMultiline)
91        element = document.createElement(textareaTag, false);
92    else
93        element = document.createElement(inputTag, false);
94
95    element->addEventListener(eventNames().keydownEvent, eventListener(), false);
96
97    StyledElement* styledElement = static_cast<StyledElement*>(element.get());
98
99    if (!textAnnotation)
100        return element;
101
102    // FIXME: Match font weight and style as well?
103    styledElement->setInlineStyleProperty(CSSPropertyColor, colorFromNSColor(textAnnotation.fontColor).serialized());
104    styledElement->setInlineStyleProperty(CSSPropertyFontFamily, textAnnotation.font.familyName);
105    styledElement->setInlineStyleProperty(CSSPropertyTextAlign, cssAlignmentValueForNSTextAlignment(textAnnotation.alignment));
106
107    if (isMultiline)
108        toHTMLTextAreaElement(styledElement)->setValue(textAnnotation.stringValue);
109    else
110        toHTMLInputElement(styledElement)->setValue(textAnnotation.stringValue);
111
112    return element;
113}
114
115void PDFPluginTextAnnotation::updateGeometry()
116{
117    PDFPluginAnnotation::updateGeometry();
118
119    StyledElement* styledElement = static_cast<StyledElement*>(element());
120    styledElement->setInlineStyleProperty(CSSPropertyFontSize, textAnnotation().font.pointSize * pdfLayerController().contentScaleFactor, CSSPrimitiveValue::CSS_PX);
121}
122
123void PDFPluginTextAnnotation::commit()
124{
125    textAnnotation().stringValue = value();
126    PDFPluginAnnotation::commit();
127}
128
129String PDFPluginTextAnnotation::value() const
130{
131    return toHTMLTextFormControlElement(element())->value();
132}
133
134bool PDFPluginTextAnnotation::handleEvent(Event* event)
135{
136    if (PDFPluginAnnotation::handleEvent(event))
137        return true;
138
139    if (event->isKeyboardEvent() && event->type() == eventNames().keydownEvent) {
140        KeyboardEvent* keyboardEvent = static_cast<KeyboardEvent*>(event);
141
142        if (keyboardEvent->keyIdentifier() == "U+0009") {
143            if (keyboardEvent->ctrlKey() || keyboardEvent->metaKey() || keyboardEvent->altGraphKey())
144                return false;
145
146            if (keyboardEvent->shiftKey())
147                plugin()->focusPreviousAnnotation();
148            else
149                plugin()->focusNextAnnotation();
150
151            event->preventDefault();
152            return true;
153        }
154    }
155
156    return false;
157}
158
159} // namespace WebKit
160
161#endif // ENABLE(PDFKIT_PLUGIN)
162