1/*
2 * Copyright (C) 2012 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
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'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#ifndef DateTimeNumericFieldElement_h
27#define DateTimeNumericFieldElement_h
28
29#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
30#include "core/html/shadow/DateTimeFieldElement.h"
31
32#include "wtf/text/StringBuilder.h"
33#include "wtf/text/WTFString.h"
34
35namespace blink {
36
37// DateTimeNumericFieldElement represents numeric field of date time format,
38// such as:
39//  - hour
40//  - minute
41//  - millisecond
42//  - second
43//  - year
44class DateTimeNumericFieldElement : public DateTimeFieldElement {
45    WTF_MAKE_NONCOPYABLE(DateTimeNumericFieldElement);
46
47public:
48    struct Step {
49        Step(int step = 1, int stepBase = 0) : step(step), stepBase(stepBase) { }
50        int step;
51        int stepBase;
52    };
53
54    struct Range {
55        Range(int minimum, int maximum) : minimum(minimum), maximum(maximum) { }
56        int clampValue(int) const;
57        bool isInRange(int) const;
58        bool isSingleton() const { return minimum == maximum; }
59
60        int minimum;
61        int maximum;
62    };
63
64protected:
65    DateTimeNumericFieldElement(Document&, FieldOwner&, const Range&, const Range& hardLimits, const String& placeholder, const Step& = Step());
66
67    int clampValue(int value) const { return m_range.clampValue(value); }
68    virtual int defaultValueForStepDown() const;
69    virtual int defaultValueForStepUp() const;
70    const Range& range() const { return m_range; }
71
72    // DateTimeFieldElement functions.
73    virtual bool hasValue() const OVERRIDE FINAL;
74    void initialize(const AtomicString& pseudo, const String& axHelpText);
75    int maximum() const;
76    virtual void setEmptyValue(EventBehavior = DispatchNoEvent) OVERRIDE FINAL;
77    virtual void setValueAsInteger(int, EventBehavior = DispatchNoEvent) OVERRIDE;
78    virtual int valueAsInteger() const OVERRIDE FINAL;
79    virtual String visibleValue() const OVERRIDE FINAL;
80
81private:
82    // DateTimeFieldElement functions.
83    virtual void handleKeyboardEvent(KeyboardEvent*) OVERRIDE FINAL;
84    virtual float maximumWidth(const Font&) OVERRIDE;
85    virtual void stepDown() OVERRIDE FINAL;
86    virtual void stepUp() OVERRIDE FINAL;
87    virtual String value() const OVERRIDE FINAL;
88
89    // Node functions.
90    virtual void setFocus(bool) OVERRIDE FINAL;
91
92    String formatValue(int) const;
93    int roundUp(int) const;
94    int roundDown(int) const;
95    int typeAheadValue() const;
96
97    const String m_placeholder;
98    const Range m_range;
99    const Range m_hardLimits;
100    const Step m_step;
101    int m_value;
102    bool m_hasValue;
103    mutable StringBuilder m_typeAheadBuffer;
104};
105
106} // namespace blink
107
108#endif
109#endif
110