OLD | NEW |
| (Empty) |
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 #include "core/html/shadow/DateTimeSymbolicFieldElement.h" | |
27 | |
28 #include "core/events/KeyboardEvent.h" | |
29 #include "platform/fonts/Font.h" | |
30 #include "platform/text/TextBreakIterator.h" | |
31 #include "platform/text/TextRun.h" | |
32 #include "platform/wtf/text/StringBuilder.h" | |
33 #include "platform/wtf/text/Unicode.h" | |
34 | |
35 namespace blink { | |
36 | |
37 static AtomicString MakeVisibleEmptyValue(const Vector<String>& symbols) { | |
38 unsigned maximum_length = 0; | |
39 for (unsigned index = 0; index < symbols.size(); ++index) | |
40 maximum_length = | |
41 std::max(maximum_length, NumGraphemeClusters(symbols[index])); | |
42 StringBuilder builder; | |
43 builder.ReserveCapacity(maximum_length); | |
44 for (unsigned length = 0; length < maximum_length; ++length) | |
45 builder.Append('-'); | |
46 return builder.ToAtomicString(); | |
47 } | |
48 | |
49 DateTimeSymbolicFieldElement::DateTimeSymbolicFieldElement( | |
50 Document& document, | |
51 FieldOwner& field_owner, | |
52 const Vector<String>& symbols, | |
53 int minimum, | |
54 int maximum) | |
55 : DateTimeFieldElement(document, field_owner), | |
56 symbols_(symbols), | |
57 visible_empty_value_(MakeVisibleEmptyValue(symbols)), | |
58 selected_index_(-1), | |
59 type_ahead_(this), | |
60 minimum_index_(minimum), | |
61 maximum_index_(maximum) { | |
62 DCHECK(!symbols.IsEmpty()); | |
63 DCHECK_GE(minimum_index_, 0); | |
64 SECURITY_DCHECK(maximum_index_ < static_cast<int>(symbols_.size())); | |
65 DCHECK_LE(minimum_index_, maximum_index_); | |
66 } | |
67 | |
68 float DateTimeSymbolicFieldElement::MaximumWidth(const ComputedStyle& style) { | |
69 float maximum_width = ComputeTextWidth(style, VisibleEmptyValue()); | |
70 for (unsigned index = 0; index < symbols_.size(); ++index) | |
71 maximum_width = | |
72 std::max(maximum_width, ComputeTextWidth(style, symbols_[index])); | |
73 return maximum_width + DateTimeFieldElement::MaximumWidth(style); | |
74 } | |
75 | |
76 void DateTimeSymbolicFieldElement::HandleKeyboardEvent( | |
77 KeyboardEvent* keyboard_event) { | |
78 if (keyboard_event->type() != EventTypeNames::keypress) | |
79 return; | |
80 | |
81 const UChar char_code = WTF::Unicode::ToLower(keyboard_event->charCode()); | |
82 if (char_code < ' ') | |
83 return; | |
84 | |
85 keyboard_event->SetDefaultHandled(); | |
86 | |
87 int index = type_ahead_.HandleEvent( | |
88 keyboard_event, TypeAhead::kMatchPrefix | TypeAhead::kCycleFirstChar | | |
89 TypeAhead::kMatchIndex); | |
90 if (index < 0) | |
91 return; | |
92 SetValueAsInteger(index, kDispatchEvent); | |
93 } | |
94 | |
95 bool DateTimeSymbolicFieldElement::HasValue() const { | |
96 return selected_index_ >= 0; | |
97 } | |
98 | |
99 void DateTimeSymbolicFieldElement::Initialize(const AtomicString& pseudo, | |
100 const String& ax_help_text) { | |
101 // The minimum and maximum below are exposed to users, and 1-based numbers | |
102 // are natural for symbolic fields. For example, the minimum value of a | |
103 // month field should be 1, not 0. | |
104 DateTimeFieldElement::Initialize(pseudo, ax_help_text, minimum_index_ + 1, | |
105 maximum_index_ + 1); | |
106 } | |
107 | |
108 void DateTimeSymbolicFieldElement::SetEmptyValue(EventBehavior event_behavior) { | |
109 if (IsDisabled()) | |
110 return; | |
111 selected_index_ = kInvalidIndex; | |
112 UpdateVisibleValue(event_behavior); | |
113 } | |
114 | |
115 void DateTimeSymbolicFieldElement::SetValueAsInteger( | |
116 int new_selected_index, | |
117 EventBehavior event_behavior) { | |
118 selected_index_ = std::max( | |
119 0, std::min(new_selected_index, static_cast<int>(symbols_.size() - 1))); | |
120 UpdateVisibleValue(event_behavior); | |
121 } | |
122 | |
123 void DateTimeSymbolicFieldElement::StepDown() { | |
124 if (HasValue()) { | |
125 if (!IndexIsInRange(--selected_index_)) | |
126 selected_index_ = maximum_index_; | |
127 } else { | |
128 selected_index_ = maximum_index_; | |
129 } | |
130 UpdateVisibleValue(kDispatchEvent); | |
131 } | |
132 | |
133 void DateTimeSymbolicFieldElement::StepUp() { | |
134 if (HasValue()) { | |
135 if (!IndexIsInRange(++selected_index_)) | |
136 selected_index_ = minimum_index_; | |
137 } else { | |
138 selected_index_ = minimum_index_; | |
139 } | |
140 UpdateVisibleValue(kDispatchEvent); | |
141 } | |
142 | |
143 String DateTimeSymbolicFieldElement::Value() const { | |
144 return HasValue() ? symbols_[selected_index_] : g_empty_string; | |
145 } | |
146 | |
147 int DateTimeSymbolicFieldElement::ValueAsInteger() const { | |
148 return selected_index_; | |
149 } | |
150 | |
151 int DateTimeSymbolicFieldElement::ValueForARIAValueNow() const { | |
152 // Synchronize with minimum/maximum adjustment in initialize(). | |
153 return selected_index_ + 1; | |
154 } | |
155 | |
156 String DateTimeSymbolicFieldElement::VisibleEmptyValue() const { | |
157 return visible_empty_value_; | |
158 } | |
159 | |
160 String DateTimeSymbolicFieldElement::VisibleValue() const { | |
161 return HasValue() ? symbols_[selected_index_] : VisibleEmptyValue(); | |
162 } | |
163 | |
164 int DateTimeSymbolicFieldElement::IndexOfSelectedOption() const { | |
165 return selected_index_; | |
166 } | |
167 | |
168 int DateTimeSymbolicFieldElement::OptionCount() const { | |
169 return symbols_.size(); | |
170 } | |
171 | |
172 String DateTimeSymbolicFieldElement::OptionAtIndex(int index) const { | |
173 return symbols_[index]; | |
174 } | |
175 | |
176 } // namespace blink | |
OLD | NEW |