2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
26 #ifndef DateTimeEditElement_h
27 #define DateTimeEditElement_h
29 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
30 #include "core/html/forms/StepRange.h"
31 #include "core/html/shadow/DateTimeFieldElement.h"
32 #include "platform/DateComponents.h"
36 class DateTimeFieldsState;
42 // DateTimeEditElement class contains numberic field and symbolc field for
43 // representing date and time, such as
44 // - Year, Month, Day Of Month
45 // - Hour, Minute, Second, Millisecond, AM/PM
46 class DateTimeEditElement FINAL : public HTMLDivElement, public DateTimeFieldElement::FieldOwner {
47 WTF_MAKE_NONCOPYABLE(DateTimeEditElement);
50 // EditControlOwner implementer must call removeEditControlOwner when
51 // it doesn't handle event, e.g. at destruction.
52 class EditControlOwner {
54 virtual ~EditControlOwner();
55 virtual void didBlurFromControl() = 0;
56 virtual void didFocusOnControl() = 0;
57 virtual void editControlValueChanged() = 0;
58 virtual String formatDateTimeFieldsState(const DateTimeFieldsState&) const = 0;
59 virtual bool isEditControlOwnerDisabled() const = 0;
60 virtual bool isEditControlOwnerReadOnly() const = 0;
61 virtual AtomicString localeIdentifier() const = 0;
64 struct LayoutParameters {
65 String dateTimeFormat;
66 String fallbackDateTimeFormat;
68 const StepRange stepRange;
69 DateComponents minimum;
70 DateComponents maximum;
71 String placeholderForDay;
72 String placeholderForMonth;
73 String placeholderForYear;
75 LayoutParameters(Locale& locale, const StepRange& stepRange)
77 , stepRange(stepRange)
82 static PassRefPtr<DateTimeEditElement> create(Document&, EditControlOwner&);
84 virtual ~DateTimeEditElement();
85 void addField(PassRefPtr<DateTimeFieldElement>);
86 bool anyEditableFieldsHaveValues() const;
88 virtual void defaultEventHandler(Event*) OVERRIDE;
89 void disabledStateChanged();
90 Element* fieldsWrapperElement() const;
91 void focusIfNoFocus();
92 // If oldFocusedNode is one of sub-fields, focus on it. Otherwise focus on
93 // the first sub-field.
94 void focusByOwner(Element* oldFocusedElement = 0);
95 bool hasFocusedField();
96 void readOnlyStateChanged();
97 void removeEditControlOwner() { m_editControlOwner = 0; }
99 void setEmptyValue(const LayoutParameters&, const DateComponents& dateForReadOnlyField);
100 void setValueAsDate(const LayoutParameters&, const DateComponents&);
101 void setValueAsDateTimeFieldsState(const DateTimeFieldsState&);
102 void setOnlyYearMonthDay(const DateComponents&);
105 String value() const;
106 DateTimeFieldsState valueAsDateTimeFieldsState() const;
109 static const size_t invalidFieldIndex = static_cast<size_t>(-1);
111 // Datetime can be represent at most five fields, such as
120 static const int maximumNumberOfFields = 8;
122 DateTimeEditElement(Document&, EditControlOwner&);
124 DateTimeFieldElement* fieldAt(size_t) const;
125 size_t fieldIndexOf(const DateTimeFieldElement&) const;
126 DateTimeFieldElement* focusedField() const;
127 size_t focusedFieldIndex() const;
128 bool focusOnNextFocusableField(size_t startIndex);
129 bool isDisabled() const;
130 bool isReadOnly() const;
131 void layout(const LayoutParameters&, const DateComponents&);
132 void updateUIState();
135 virtual PassRefPtr<RenderStyle> customStyleForRenderer() OVERRIDE;
136 virtual bool isDateTimeEditElement() const OVERRIDE;
138 // DateTimeFieldElement::FieldOwner functions.
139 virtual void didBlurFromField() OVERRIDE;
140 virtual void didFocusOnField() OVERRIDE;
141 virtual void fieldValueChanged() OVERRIDE;
142 virtual bool focusOnNextField(const DateTimeFieldElement&) OVERRIDE;
143 virtual bool focusOnPreviousField(const DateTimeFieldElement&) OVERRIDE;
144 virtual bool isFieldOwnerDisabled() const OVERRIDE;
145 virtual bool isFieldOwnerReadOnly() const OVERRIDE;
146 virtual AtomicString localeIdentifier() const OVERRIDE;
148 Vector<DateTimeFieldElement*, maximumNumberOfFields> m_fields;
149 EditControlOwner* m_editControlOwner;
152 DEFINE_TYPE_CASTS(DateTimeEditElement, Element, element, element->isDateTimeEditElement(), element.isDateTimeEditElement());
154 } // namespace WebCore