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#if ENABLE(PDFKIT_PLUGIN)
27
28#import "config.h"
29#import "PDFPluginChoiceAnnotation.h"
30
31#import "PDFKitImports.h"
32#import "PDFLayerControllerDetails.h"
33#import <PDFKit/PDFKit.h>
34#import <WebCore/CSSPrimitiveValue.h>
35#import <WebCore/CSSPropertyNames.h>
36#import <WebCore/ColorMac.h>
37#import <WebCore/HTMLElement.h>
38#import <WebCore/HTMLNames.h>
39#import <WebCore/HTMLOptionElement.h>
40#import <WebCore/HTMLSelectElement.h>
41#import <WebCore/Page.h>
42
43using namespace WebCore;
44
45namespace WebKit {
46
47using namespace HTMLNames;
48
49PassRefPtr<PDFPluginChoiceAnnotation> PDFPluginChoiceAnnotation::create(PDFAnnotation *annotation, PDFLayerController *pdfLayerController, PDFPlugin* plugin)
50{
51    return adoptRef(new PDFPluginChoiceAnnotation(annotation, pdfLayerController, plugin));
52}
53
54void PDFPluginChoiceAnnotation::updateGeometry()
55{
56    PDFPluginAnnotation::updateGeometry();
57
58    StyledElement* styledElement = static_cast<StyledElement*>(element());
59    styledElement->setInlineStyleProperty(CSSPropertyFontSize, choiceAnnotation().font.pointSize * pdfLayerController().contentScaleFactor, CSSPrimitiveValue::CSS_PX);
60}
61
62void PDFPluginChoiceAnnotation::commit()
63{
64    choiceAnnotation().stringValue = static_cast<HTMLSelectElement*>(element())->value();
65
66    PDFPluginAnnotation::commit();
67}
68
69PassRefPtr<Element> PDFPluginChoiceAnnotation::createAnnotationElement()
70{
71    Document* document = parent()->ownerDocument();
72    PDFAnnotationChoiceWidget *choiceAnnotation = this->choiceAnnotation();
73
74    RefPtr<Element> element = document->createElement(selectTag, false);
75
76    StyledElement* styledElement = static_cast<StyledElement*>(element.get());
77
78    // FIXME: Match font weight and style as well?
79    styledElement->setInlineStyleProperty(CSSPropertyColor, colorFromNSColor(choiceAnnotation.fontColor).serialized());
80    styledElement->setInlineStyleProperty(CSSPropertyFontFamily, choiceAnnotation.font.familyName);
81
82    NSArray *choices = choiceAnnotation.choices;
83    NSString *selectedChoice = choiceAnnotation.stringValue;
84
85    for (NSString *choice in choices) {
86        RefPtr<Element> choiceOption = document->createElement(optionTag, false);
87        choiceOption->setAttribute(valueAttr, choice);
88        choiceOption->setTextContent(choice, ASSERT_NO_EXCEPTION);
89
90        if (choice == selectedChoice)
91            choiceOption->setAttribute(selectedAttr, "selected");
92
93        styledElement->appendChild(choiceOption);
94    }
95
96    return element;
97}
98
99} // namespace WebKit
100
101#endif // ENABLE(PDFKIT_PLUGIN)
102