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 DateTimeEditElement_h 27 #define DateTimeEditElement_h 28 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" 33 34 namespace WebCore { 35 36 class DateTimeFieldsState; 37 class KeyboardEvent; 38 class Locale; 39 class MouseEvent; 40 class StepRange; 41 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); 48 49 public: 50 // EditControlOwner implementer must call removeEditControlOwner when 51 // it doesn't handle event, e.g. at destruction. 52 class EditControlOwner { 53 public: 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; 62 }; 63 64 struct LayoutParameters { 65 String dateTimeFormat; 66 String fallbackDateTimeFormat; 67 Locale& locale; 68 const StepRange stepRange; 69 DateComponents minimum; 70 DateComponents maximum; 71 String placeholderForDay; 72 String placeholderForMonth; 73 String placeholderForYear; 74 LayoutParametersLayoutParameters75 LayoutParameters(Locale& locale, const StepRange& stepRange) 76 : locale(locale) 77 , stepRange(stepRange) 78 { 79 } 80 }; 81 82 static PassRefPtr<DateTimeEditElement> create(Document&, EditControlOwner&); 83 84 virtual ~DateTimeEditElement(); 85 void addField(PassRefPtr<DateTimeFieldElement>); 86 bool anyEditableFieldsHaveValues() const; 87 void blurByOwner(); 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(); removeEditControlOwner()97 void removeEditControlOwner() { m_editControlOwner = 0; } 98 void resetFields(); 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&); 103 void stepDown(); 104 void stepUp(); 105 String value() const; 106 DateTimeFieldsState valueAsDateTimeFieldsState() const; 107 108 private: 109 static const size_t invalidFieldIndex = static_cast<size_t>(-1); 110 111 // Datetime can be represent at most five fields, such as 112 // 1. year 113 // 2. month 114 // 3. day-of-month 115 // 4. hour 116 // 5. minute 117 // 6. second 118 // 7. millisecond 119 // 8. AM/PM 120 static const int maximumNumberOfFields = 8; 121 122 DateTimeEditElement(Document&, EditControlOwner&); 123 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(); 133 134 // Element function. 135 virtual PassRefPtr<RenderStyle> customStyleForRenderer() OVERRIDE; 136 virtual bool isDateTimeEditElement() const OVERRIDE; 137 138 // DateTimeFieldElement::FieldOwner functions. 139 virtual void didBlurFromField() OVERRIDE FINAL; 140 virtual void didFocusOnField() OVERRIDE FINAL; 141 virtual void fieldValueChanged() OVERRIDE FINAL; 142 virtual bool focusOnNextField(const DateTimeFieldElement&) OVERRIDE FINAL; 143 virtual bool focusOnPreviousField(const DateTimeFieldElement&) OVERRIDE FINAL; 144 virtual bool isFieldOwnerDisabled() const OVERRIDE FINAL; 145 virtual bool isFieldOwnerReadOnly() const OVERRIDE FINAL; 146 virtual AtomicString localeIdentifier() const OVERRIDE FINAL; 147 148 Vector<DateTimeFieldElement*, maximumNumberOfFields> m_fields; 149 EditControlOwner* m_editControlOwner; 150 }; 151 152 DEFINE_TYPE_CASTS(DateTimeEditElement, Element, element, element->isDateTimeEditElement(), element.isDateTimeEditElement()); 153 154 } // namespace WebCore 155 156 #endif 157 #endif 158