Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: ui/views/controls/textfield/textfield.cc

Issue 120503005: Merge NativeTextfieldViews into views::Textfield. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Simplify Textfield border code. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/views/controls/textfield/textfield.h" 5 #include "ui/views/controls/textfield/textfield.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/command_line.h" 9 #include "base/debug/trace_event.h"
10 #include "base/strings/string_util.h" 10 #include "base/i18n/case_conversion.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "grit/ui_strings.h"
12 #include "ui/base/accessibility/accessible_view_state.h" 12 #include "ui/base/accessibility/accessible_view_state.h"
13 #include "ui/base/ime/text_input_type.h" 13 #include "ui/base/dragdrop/drag_drop_types.h"
14 #include "ui/base/dragdrop/drag_utils.h"
14 #include "ui/base/resource/resource_bundle.h" 15 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/base/ui_base_switches.h" 16 #include "ui/base/ui_base_switches_util.h"
16 #include "ui/events/event.h" 17 #include "ui/events/event.h"
17 #include "ui/events/keycodes/keyboard_codes.h" 18 #include "ui/events/keycodes/keyboard_codes.h"
19 #include "ui/gfx/canvas.h"
18 #include "ui/gfx/insets.h" 20 #include "ui/gfx/insets.h"
19 #include "ui/gfx/range/range.h"
20 #include "ui/gfx/selection_model.h"
21 #include "ui/native_theme/native_theme.h" 21 #include "ui/native_theme/native_theme.h"
22 #include "ui/views/background.h"
23 #include "ui/views/controls/focusable_border.h"
24 #include "ui/views/controls/menu/menu_item_view.h"
25 #include "ui/views/controls/menu/menu_model_adapter.h"
26 #include "ui/views/controls/menu/menu_runner.h"
22 #include "ui/views/controls/native/native_view_host.h" 27 #include "ui/views/controls/native/native_view_host.h"
23 #include "ui/views/controls/textfield/native_textfield_views.h"
24 #include "ui/views/controls/textfield/textfield_controller.h" 28 #include "ui/views/controls/textfield/textfield_controller.h"
29 #include "ui/views/drag_utils.h"
30 #include "ui/views/ime/input_method.h"
31 #include "ui/views/metrics.h"
25 #include "ui/views/painter.h" 32 #include "ui/views/painter.h"
26 #include "ui/views/views_delegate.h" 33 #include "ui/views/views_delegate.h"
27 #include "ui/views/widget/widget.h" 34 #include "ui/views/widget/widget.h"
28 35
36 #if defined(USE_AURA)
37 #include "ui/base/cursor/cursor.h"
38 #endif
39
40 #if defined(OS_WIN) && defined(USE_AURA)
41 #include "base/win/win_util.h"
42 #endif
43
29 namespace { 44 namespace {
30 45
31 // Default placeholder text color. 46 // Default placeholder text color.
32 const SkColor kDefaultPlaceholderTextColor = SK_ColorLTGRAY; 47 const SkColor kDefaultPlaceholderTextColor = SK_ColorLTGRAY;
33 48
34 gfx::FontList GetDefaultFontList() { 49 void ConvertRectToScreen(const views::View* src, gfx::Rect* r) {
35 return ResourceBundle::GetSharedInstance().GetFontList( 50 DCHECK(src);
36 ResourceBundle::BaseFont); 51
52 gfx::Point new_origin = r->origin();
53 views::View::ConvertPointToScreen(src, &new_origin);
54 r->set_origin(new_origin);
37 } 55 }
38 56
39 } // namespace 57 } // namespace
40 58
41 namespace views { 59 namespace views {
42 60
43 // static 61 // static
44 const char Textfield::kViewClassName[] = "Textfield"; 62 const char Textfield::kViewClassName[] = "Textfield";
45 63
46 // static 64 // static
47 size_t Textfield::GetCaretBlinkMs() { 65 size_t Textfield::GetCaretBlinkMs() {
48 static const size_t default_value = 500; 66 static const size_t default_value = 500;
49 #if defined(OS_WIN) 67 #if defined(OS_WIN)
50 static const size_t system_value = ::GetCaretBlinkTime(); 68 static const size_t system_value = ::GetCaretBlinkTime();
51 if (system_value != 0) 69 if (system_value != 0)
52 return (system_value == INFINITE) ? 0 : system_value; 70 return (system_value == INFINITE) ? 0 : system_value;
53 #endif 71 #endif
54 return default_value; 72 return default_value;
55 } 73 }
56 74
57 Textfield::Textfield() 75 Textfield::Textfield()
58 : textfield_view_(NULL), 76 : model_(new TextfieldViewsModel(this)),
59 controller_(NULL), 77 controller_(NULL),
60 style_(STYLE_DEFAULT), 78 style_(STYLE_DEFAULT),
61 font_list_(GetDefaultFontList()),
62 read_only_(false), 79 read_only_(false),
63 default_width_in_chars_(0), 80 default_width_in_chars_(0),
64 draw_border_(true),
65 text_color_(SK_ColorBLACK), 81 text_color_(SK_ColorBLACK),
66 use_default_text_color_(true), 82 use_default_text_color_(true),
67 background_color_(SK_ColorWHITE), 83 background_color_(SK_ColorWHITE),
68 use_default_background_color_(true), 84 use_default_background_color_(true),
69 horizontal_margins_were_set_(false),
70 vertical_margins_were_set_(false),
71 placeholder_text_color_(kDefaultPlaceholderTextColor), 85 placeholder_text_color_(kDefaultPlaceholderTextColor),
72 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT), 86 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT),
87 skip_input_method_cancel_composition_(false),
88 is_cursor_visible_(false),
89 is_drop_cursor_visible_(false),
90 initiating_drag_(false),
91 aggregated_clicks_(0),
73 weak_ptr_factory_(this) { 92 weak_ptr_factory_(this) {
93 set_context_menu_controller(this);
94 set_drag_controller(this);
95 set_border(new FocusableBorder());
74 SetFocusable(true); 96 SetFocusable(true);
75 97
76 if (ViewsDelegate::views_delegate) { 98 if (ViewsDelegate::views_delegate) {
77 obscured_reveal_duration_ = ViewsDelegate::views_delegate-> 99 obscured_reveal_duration_ = ViewsDelegate::views_delegate->
78 GetDefaultTextfieldObscuredRevealDuration(); 100 GetDefaultTextfieldObscuredRevealDuration();
79 } 101 }
80 102
81 if (NativeViewHost::kRenderNativeControlFocus) 103 if (NativeViewHost::kRenderNativeControlFocus)
82 focus_painter_ = Painter::CreateDashedFocusPainter(); 104 focus_painter_ = Painter::CreateDashedFocusPainter();
83 } 105 }
84 106
85 Textfield::Textfield(StyleFlags style) 107 Textfield::Textfield(StyleFlags style)
86 : textfield_view_(NULL), 108 : model_(new TextfieldViewsModel(this)),
87 controller_(NULL), 109 controller_(NULL),
88 style_(style), 110 style_(style),
89 font_list_(GetDefaultFontList()),
90 read_only_(false), 111 read_only_(false),
91 default_width_in_chars_(0), 112 default_width_in_chars_(0),
92 draw_border_(true),
93 text_color_(SK_ColorBLACK), 113 text_color_(SK_ColorBLACK),
94 use_default_text_color_(true), 114 use_default_text_color_(true),
95 background_color_(SK_ColorWHITE), 115 background_color_(SK_ColorWHITE),
96 use_default_background_color_(true), 116 use_default_background_color_(true),
97 horizontal_margins_were_set_(false),
98 vertical_margins_were_set_(false),
99 placeholder_text_color_(kDefaultPlaceholderTextColor), 117 placeholder_text_color_(kDefaultPlaceholderTextColor),
100 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT), 118 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT),
119 skip_input_method_cancel_composition_(false),
120 is_cursor_visible_(false),
121 is_drop_cursor_visible_(false),
122 initiating_drag_(false),
123 aggregated_clicks_(0),
101 weak_ptr_factory_(this) { 124 weak_ptr_factory_(this) {
125 set_context_menu_controller(this);
126 set_drag_controller(this);
127 set_border(new FocusableBorder());
102 SetFocusable(true); 128 SetFocusable(true);
129
103 if (IsObscured()) 130 if (IsObscured())
104 SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); 131 SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
105 132
106 if (ViewsDelegate::views_delegate) { 133 if (ViewsDelegate::views_delegate) {
107 obscured_reveal_duration_ = ViewsDelegate::views_delegate-> 134 obscured_reveal_duration_ = ViewsDelegate::views_delegate->
108 GetDefaultTextfieldObscuredRevealDuration(); 135 GetDefaultTextfieldObscuredRevealDuration();
109 } 136 }
110 137
111 if (NativeViewHost::kRenderNativeControlFocus) 138 if (NativeViewHost::kRenderNativeControlFocus)
112 focus_painter_ = Painter::CreateDashedFocusPainter(); 139 focus_painter_ = Painter::CreateDashedFocusPainter();
113 } 140 }
114 141
115 Textfield::~Textfield() { 142 Textfield::~Textfield() {
116 } 143 }
117 144
118 void Textfield::SetController(TextfieldController* controller) { 145 void Textfield::SetController(TextfieldController* controller) {
119 controller_ = controller; 146 controller_ = controller;
120 } 147 }
121 148
122 TextfieldController* Textfield::GetController() const { 149 TextfieldController* Textfield::GetController() const {
123 return controller_; 150 return controller_;
124 } 151 }
125 152
126 void Textfield::SetReadOnly(bool read_only) { 153 void Textfield::SetReadOnly(bool read_only) {
127 // Update read-only without changing the focusable state (or active, etc.). 154 // Update read-only without changing the focusable state (or active, etc.).
128 read_only_ = read_only; 155 read_only_ = read_only;
129 if (textfield_view_) { 156 if (GetInputMethod())
130 textfield_view_->UpdateReadOnly(); 157 GetInputMethod()->OnTextInputTypeChanged(this);
131 textfield_view_->UpdateTextColor(); 158 SetColor(GetTextColor());
132 textfield_view_->UpdateBackgroundColor(); 159 UpdateBackgroundColor();
133 }
134 } 160 }
135 161
136 bool Textfield::IsObscured() const { 162 bool Textfield::IsObscured() const {
137 return style_ & STYLE_OBSCURED; 163 return style_ & STYLE_OBSCURED;
138 } 164 }
139 165
140 void Textfield::SetObscured(bool obscured) { 166 void Textfield::SetObscured(bool obscured) {
141 if (obscured) { 167 if (obscured) {
142 style_ = static_cast<StyleFlags>(style_ | STYLE_OBSCURED); 168 style_ = static_cast<StyleFlags>(style_ | STYLE_OBSCURED);
143 SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); 169 SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
144 } else { 170 } else {
145 style_ = static_cast<StyleFlags>(style_ & ~STYLE_OBSCURED); 171 style_ = static_cast<StyleFlags>(style_ & ~STYLE_OBSCURED);
146 SetTextInputType(ui::TEXT_INPUT_TYPE_TEXT); 172 SetTextInputType(ui::TEXT_INPUT_TYPE_TEXT);
147 } 173 }
148 if (textfield_view_) 174 GetRenderText()->SetObscured(obscured);
149 textfield_view_->UpdateIsObscured(); 175 OnCaretBoundsChanged();
150 } 176 if (GetInputMethod())
151 177 GetInputMethod()->OnTextInputTypeChanged(this);
152 ui::TextInputType Textfield::GetTextInputType() const { 178 SchedulePaint();
153 if (read_only() || !enabled())
154 return ui::TEXT_INPUT_TYPE_NONE;
155 return text_input_type_;
156 } 179 }
157 180
158 void Textfield::SetTextInputType(ui::TextInputType type) { 181 void Textfield::SetTextInputType(ui::TextInputType type) {
159 text_input_type_ = type; 182 text_input_type_ = type;
160 bool should_be_obscured = type == ui::TEXT_INPUT_TYPE_PASSWORD; 183 bool should_be_obscured = type == ui::TEXT_INPUT_TYPE_PASSWORD;
161 if (IsObscured() != should_be_obscured) 184 if (IsObscured() != should_be_obscured)
162 SetObscured(should_be_obscured); 185 SetObscured(should_be_obscured);
163 } 186 }
164 187
165 void Textfield::SetText(const base::string16& text) { 188 void Textfield::SetText(const base::string16& new_text) {
166 text_ = text; 189 model_->SetText(GetTextForDisplay(new_text));
167 if (textfield_view_) 190 OnCaretBoundsChanged();
168 textfield_view_->UpdateText(); 191 SchedulePaint();
192 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_TEXT_CHANGED, true);
169 } 193 }
170 194
171 void Textfield::AppendText(const base::string16& text) { 195 void Textfield::AppendText(const base::string16& new_text) {
172 text_ += text; 196 if (new_text.empty())
173 if (textfield_view_) 197 return;
174 textfield_view_->AppendText(text); 198 model_->Append(GetTextForDisplay(new_text));
199 OnCaretBoundsChanged();
200 SchedulePaint();
175 } 201 }
176 202
177 void Textfield::InsertOrReplaceText(const base::string16& text) { 203 void Textfield::InsertOrReplaceText(const base::string16& new_text) {
178 if (textfield_view_) { 204 if (new_text.empty())
179 textfield_view_->InsertOrReplaceText(text); 205 return;
180 text_ = textfield_view_->GetText(); 206 model_->InsertText(new_text);
181 } 207 OnCaretBoundsChanged();
208 SchedulePaint();
182 } 209 }
183 210
184 base::i18n::TextDirection Textfield::GetTextDirection() const { 211 base::i18n::TextDirection Textfield::GetTextDirection() const {
185 return textfield_view_ ? 212 return GetRenderText()->GetTextDirection();
186 textfield_view_->GetTextDirection() : base::i18n::UNKNOWN_DIRECTION;
187 } 213 }
188 214
189 void Textfield::SelectAll(bool reversed) { 215 void Textfield::SelectAll(bool reversed) {
190 if (textfield_view_) 216 model_->SelectAll(reversed);
191 textfield_view_->SelectAll(reversed); 217 OnCaretBoundsChanged();
218 SchedulePaint();
192 } 219 }
193 220
194 base::string16 Textfield::GetSelectedText() const { 221 base::string16 Textfield::GetSelectedText() const {
195 return textfield_view_ ? textfield_view_->GetSelectedText() : 222 return model_->GetSelectedText();
196 base::string16();
197 } 223 }
198 224
199 void Textfield::ClearSelection() const { 225 void Textfield::ClearSelection() {
200 if (textfield_view_) 226 model_->ClearSelection();
201 textfield_view_->ClearSelection(); 227 OnCaretBoundsChanged();
228 SchedulePaint();
202 } 229 }
203 230
204 bool Textfield::HasSelection() const { 231 bool Textfield::HasSelection() const {
205 return textfield_view_ && !textfield_view_->GetSelectedRange().is_empty(); 232 return !GetSelectedRange().is_empty();
206 } 233 }
207 234
208 SkColor Textfield::GetTextColor() const { 235 SkColor Textfield::GetTextColor() const {
209 if (!use_default_text_color_) 236 if (!use_default_text_color_)
210 return text_color_; 237 return text_color_;
211 238
212 return GetNativeTheme()->GetSystemColor(read_only() ? 239 return GetNativeTheme()->GetSystemColor(read_only() ?
213 ui::NativeTheme::kColorId_TextfieldReadOnlyColor : 240 ui::NativeTheme::kColorId_TextfieldReadOnlyColor :
214 ui::NativeTheme::kColorId_TextfieldDefaultColor); 241 ui::NativeTheme::kColorId_TextfieldDefaultColor);
215 } 242 }
216 243
217 void Textfield::SetTextColor(SkColor color) { 244 void Textfield::SetTextColor(SkColor color) {
218 text_color_ = color; 245 text_color_ = color;
219 use_default_text_color_ = false; 246 use_default_text_color_ = false;
220 if (textfield_view_) 247 SetColor(color);
221 textfield_view_->UpdateTextColor();
222 } 248 }
223 249
224 void Textfield::UseDefaultTextColor() { 250 void Textfield::UseDefaultTextColor() {
225 use_default_text_color_ = true; 251 use_default_text_color_ = true;
226 if (textfield_view_) 252 SetColor(GetTextColor());
227 textfield_view_->UpdateTextColor();
228 } 253 }
229 254
230 SkColor Textfield::GetBackgroundColor() const { 255 SkColor Textfield::GetBackgroundColor() const {
231 if (!use_default_background_color_) 256 if (!use_default_background_color_)
232 return background_color_; 257 return background_color_;
233 258
234 return GetNativeTheme()->GetSystemColor(read_only() ? 259 return GetNativeTheme()->GetSystemColor(read_only() ?
235 ui::NativeTheme::kColorId_TextfieldReadOnlyBackground : 260 ui::NativeTheme::kColorId_TextfieldReadOnlyBackground :
236 ui::NativeTheme::kColorId_TextfieldDefaultBackground); 261 ui::NativeTheme::kColorId_TextfieldDefaultBackground);
237 } 262 }
238 263
239 void Textfield::SetBackgroundColor(SkColor color) { 264 void Textfield::SetBackgroundColor(SkColor color) {
240 background_color_ = color; 265 background_color_ = color;
241 use_default_background_color_ = false; 266 use_default_background_color_ = false;
242 if (textfield_view_) 267 UpdateBackgroundColor();
243 textfield_view_->UpdateBackgroundColor();
244 } 268 }
245 269
246 void Textfield::UseDefaultBackgroundColor() { 270 void Textfield::UseDefaultBackgroundColor() {
247 use_default_background_color_ = true; 271 use_default_background_color_ = true;
248 if (textfield_view_) 272 UpdateBackgroundColor();
249 textfield_view_->UpdateBackgroundColor();
250 } 273 }
251 274
252 bool Textfield::GetCursorEnabled() const { 275 bool Textfield::GetCursorEnabled() const {
253 return textfield_view_ && textfield_view_->GetCursorEnabled(); 276 return GetRenderText()->cursor_enabled();
254 } 277 }
255 278
256 void Textfield::SetCursorEnabled(bool enabled) { 279 void Textfield::SetCursorEnabled(bool enabled) {
257 if (textfield_view_) 280 GetRenderText()->SetCursorEnabled(enabled);
258 textfield_view_->SetCursorEnabled(enabled); 281 }
282
283 const gfx::FontList& Textfield::GetFontList() const {
284 return GetRenderText()->font_list();
259 } 285 }
260 286
261 void Textfield::SetFontList(const gfx::FontList& font_list) { 287 void Textfield::SetFontList(const gfx::FontList& font_list) {
262 font_list_ = font_list; 288 GetRenderText()->SetFontList(font_list);
263 if (textfield_view_) 289 OnCaretBoundsChanged();
264 textfield_view_->UpdateFont();
265 PreferredSizeChanged(); 290 PreferredSizeChanged();
266 } 291 }
267 292
268 const gfx::Font& Textfield::GetPrimaryFont() const {
269 return font_list_.GetPrimaryFont();
270 }
271
272 void Textfield::SetFont(const gfx::Font& font) {
273 SetFontList(gfx::FontList(font));
274 }
275
276 void Textfield::SetHorizontalMargins(int left, int right) {
277 if (horizontal_margins_were_set_ &&
278 left == margins_.left() && right == margins_.right()) {
279 return;
280 }
281 margins_.Set(margins_.top(), left, margins_.bottom(), right);
282 horizontal_margins_were_set_ = true;
283 if (textfield_view_)
284 textfield_view_->UpdateHorizontalMargins();
285 PreferredSizeChanged();
286 }
287
288 void Textfield::SetVerticalMargins(int top, int bottom) {
289 if (vertical_margins_were_set_ &&
290 top == margins_.top() && bottom == margins_.bottom()) {
291 return;
292 }
293 margins_.Set(top, margins_.left(), bottom, margins_.right());
294 vertical_margins_were_set_ = true;
295 if (textfield_view_)
296 textfield_view_->UpdateVerticalMargins();
297 PreferredSizeChanged();
298 }
299
300 void Textfield::RemoveBorder() {
301 if (!draw_border_)
302 return;
303
304 draw_border_ = false;
305 if (textfield_view_)
306 textfield_view_->UpdateBorder();
307 }
308
309 base::string16 Textfield::GetPlaceholderText() const { 293 base::string16 Textfield::GetPlaceholderText() const {
310 return placeholder_text_; 294 return placeholder_text_;
311 } 295 }
312 296
313 bool Textfield::GetHorizontalMargins(int* left, int* right) {
314 if (!horizontal_margins_were_set_)
315 return false;
316
317 *left = margins_.left();
318 *right = margins_.right();
319 return true;
320 }
321
322 bool Textfield::GetVerticalMargins(int* top, int* bottom) {
323 if (!vertical_margins_were_set_)
324 return false;
325
326 *top = margins_.top();
327 *bottom = margins_.bottom();
328 return true;
329 }
330
331 void Textfield::UpdateAllProperties() {
332 if (textfield_view_) {
333 textfield_view_->UpdateText();
334 textfield_view_->UpdateTextColor();
335 textfield_view_->UpdateBackgroundColor();
336 textfield_view_->UpdateReadOnly();
337 textfield_view_->UpdateFont();
338 textfield_view_->UpdateEnabled();
339 textfield_view_->UpdateBorder();
340 textfield_view_->UpdateIsObscured();
341 textfield_view_->UpdateHorizontalMargins();
342 textfield_view_->UpdateVerticalMargins();
343 }
344 }
345
346 void Textfield::SyncText() {
347 if (textfield_view_) {
348 base::string16 new_text = textfield_view_->GetText();
349 if (new_text != text_) {
350 text_ = new_text;
351 if (controller_)
352 controller_->ContentsChanged(this, text_);
353 }
354 }
355 }
356
357 bool Textfield::IsIMEComposing() const { 297 bool Textfield::IsIMEComposing() const {
358 return textfield_view_ && textfield_view_->IsIMEComposing(); 298 return model_->HasCompositionText();
359 } 299 }
360 300
361 const gfx::Range& Textfield::GetSelectedRange() const { 301 const gfx::Range& Textfield::GetSelectedRange() const {
362 return textfield_view_->GetSelectedRange(); 302 return GetRenderText()->selection();
363 } 303 }
364 304
365 void Textfield::SelectRange(const gfx::Range& range) { 305 void Textfield::SelectRange(const gfx::Range& range) {
366 textfield_view_->SelectRange(range); 306 model_->SelectRange(range);
307 OnCaretBoundsChanged();
308 SchedulePaint();
309 NotifyAccessibilityEvent(
310 ui::AccessibilityTypes::EVENT_SELECTION_CHANGED, true);
367 } 311 }
368 312
369 const gfx::SelectionModel& Textfield::GetSelectionModel() const { 313 const gfx::SelectionModel& Textfield::GetSelectionModel() const {
370 return textfield_view_->GetSelectionModel(); 314 return GetRenderText()->selection_model();
371 } 315 }
372 316
373 void Textfield::SelectSelectionModel(const gfx::SelectionModel& sel) { 317 void Textfield::SelectSelectionModel(const gfx::SelectionModel& sel) {
374 textfield_view_->SelectSelectionModel(sel); 318 model_->SelectSelectionModel(sel);
319 OnCaretBoundsChanged();
320 SchedulePaint();
375 } 321 }
376 322
377 size_t Textfield::GetCursorPosition() const { 323 size_t Textfield::GetCursorPosition() const {
378 return textfield_view_->GetCursorPosition(); 324 return model_->GetCursorPosition();
379 } 325 }
380 326
381 void Textfield::SetColor(SkColor value) { 327 void Textfield::SetColor(SkColor value) {
382 return textfield_view_->SetColor(value); 328 GetRenderText()->SetColor(value);
329 SchedulePaint();
383 } 330 }
384 331
385 void Textfield::ApplyColor(SkColor value, const gfx::Range& range) { 332 void Textfield::ApplyColor(SkColor value, const gfx::Range& range) {
386 return textfield_view_->ApplyColor(value, range); 333 GetRenderText()->ApplyColor(value, range);
334 SchedulePaint();
387 } 335 }
388 336
389 void Textfield::SetStyle(gfx::TextStyle style, bool value) { 337 void Textfield::SetStyle(gfx::TextStyle style, bool value) {
390 return textfield_view_->SetStyle(style, value); 338 GetRenderText()->SetStyle(style, value);
339 SchedulePaint();
391 } 340 }
392 341
393 void Textfield::ApplyStyle(gfx::TextStyle style, 342 void Textfield::ApplyStyle(gfx::TextStyle style,
394 bool value, 343 bool value,
395 const gfx::Range& range) { 344 const gfx::Range& range) {
396 return textfield_view_->ApplyStyle(style, value, range); 345 GetRenderText()->ApplyStyle(style, value, range);
346 SchedulePaint();
397 } 347 }
398 348
399 void Textfield::ClearEditHistory() { 349 void Textfield::ClearEditHistory() {
400 textfield_view_->ClearEditHistory(); 350 model_->ClearEditHistory();
401 } 351 }
402 352
403 void Textfield::SetAccessibleName(const base::string16& name) { 353 void Textfield::SetAccessibleName(const base::string16& name) {
404 accessible_name_ = name; 354 accessible_name_ = name;
405 } 355 }
406 356
407 void Textfield::ExecuteCommand(int command_id) { 357 void Textfield::ExecuteCommand(int command_id) {
408 textfield_view_->ExecuteCommand(command_id, ui::EF_NONE); 358 ExecuteCommand(command_id, ui::EF_NONE);
409 } 359 }
410 360
411 void Textfield::SetFocusPainter(scoped_ptr<Painter> focus_painter) { 361 void Textfield::SetFocusPainter(scoped_ptr<Painter> focus_painter) {
412 focus_painter_ = focus_painter.Pass(); 362 focus_painter_ = focus_painter.Pass();
413 } 363 }
414 364
415 bool Textfield::HasTextBeingDragged() { 365 bool Textfield::HasTextBeingDragged() {
416 return textfield_view_->HasTextBeingDragged(); 366 return initiating_drag_;
417 } 367 }
418 368
419 //////////////////////////////////////////////////////////////////////////////// 369 ////////////////////////////////////////////////////////////////////////////////
420 // Textfield, View overrides: 370 // Textfield, View overrides:
421 371
422 void Textfield::Layout() {
423 if (textfield_view_) {
424 textfield_view_->SetBoundsRect(GetContentsBounds());
425 textfield_view_->Layout();
426 }
427 }
428
429 int Textfield::GetBaseline() const { 372 int Textfield::GetBaseline() const {
430 gfx::Insets insets = GetTextInsets(); 373 return GetInsets().top() + GetRenderText()->GetBaseline();
431 const int baseline = textfield_view_ ?
432 textfield_view_->GetTextfieldBaseline() : font_list_.GetBaseline();
433 return insets.top() + baseline;
434 } 374 }
435 375
436 gfx::Size Textfield::GetPreferredSize() { 376 gfx::Size Textfield::GetPreferredSize() {
437 gfx::Insets insets = GetTextInsets(); 377 const gfx::Insets& insets = GetInsets();
438 378 return gfx::Size(GetFontList().GetExpectedTextWidth(default_width_in_chars_) +
439 const int font_height = textfield_view_ ? textfield_view_->GetFontHeight() : 379 insets.width(), GetFontList().GetHeight() + insets.height());
440 font_list_.GetHeight();
441 return gfx::Size(
442 GetPrimaryFont().GetExpectedTextWidth(default_width_in_chars_)
443 + insets.width(),
444 font_height + insets.height());
445 } 380 }
446 381
447 void Textfield::AboutToRequestFocusFromTabTraversal(bool reverse) { 382 void Textfield::AboutToRequestFocusFromTabTraversal(bool reverse) {
448 SelectAll(false); 383 SelectAll(false);
449 } 384 }
450 385
451 bool Textfield::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { 386 bool Textfield::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) {
452 // Skip any accelerator handling of backspace; textfields handle this key. 387 // Skip any accelerator handling of backspace; textfields handle this key.
453 // Also skip processing of [Alt]+<num-pad digit> Unicode alt key codes. 388 // Also skip processing of [Alt]+<num-pad digit> Unicode alt key codes.
454 return e.key_code() == ui::VKEY_BACK || e.IsUnicodeKeyCode(); 389 return e.key_code() == ui::VKEY_BACK || e.IsUnicodeKeyCode();
455 } 390 }
456 391
457 void Textfield::OnPaint(gfx::Canvas* canvas) { 392 void Textfield::OnPaint(gfx::Canvas* canvas) {
458 View::OnPaint(canvas); 393 OnPaintBackground(canvas);
394 PaintTextAndCursor(canvas);
395 OnPaintBorder(canvas);
459 if (NativeViewHost::kRenderNativeControlFocus) 396 if (NativeViewHost::kRenderNativeControlFocus)
460 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); 397 Painter::PaintFocusPainter(this, canvas, focus_painter_.get());
461 } 398 }
462 399
463 bool Textfield::OnKeyPressed(const ui::KeyEvent& e) { 400 bool Textfield::OnKeyPressed(const ui::KeyEvent& event) {
464 return textfield_view_ && textfield_view_->HandleKeyPressed(e); 401 bool handled = controller_ && controller_->HandleKeyEvent(this, event);
465 } 402 touch_selection_controller_.reset();
466 403 if (handled)
467 bool Textfield::OnKeyReleased(const ui::KeyEvent& e) { 404 return true;
468 return textfield_view_ && textfield_view_->HandleKeyReleased(e); 405
469 } 406 // TODO(oshima): Refactor and consolidate with ExecuteCommand.
470 407 if (event.type() == ui::ET_KEY_PRESSED) {
471 bool Textfield::OnMouseDragged(const ui::MouseEvent& e) { 408 ui::KeyboardCode key_code = event.key_code();
472 if (!e.IsOnlyRightMouseButton()) 409 if (key_code == ui::VKEY_TAB || event.IsUnicodeKeyCode())
473 return View::OnMouseDragged(e); 410 return false;
411
412 gfx::RenderText* render_text = GetRenderText();
413 const bool editable = !read_only();
414 const bool readable = !IsObscured();
415 const bool shift = event.IsShiftDown();
416 const bool control = event.IsControlDown();
417 const bool alt = event.IsAltDown() || event.IsAltGrDown();
418 bool text_changed = false;
419 bool cursor_changed = false;
420
421 OnBeforeUserAction();
422 switch (key_code) {
423 case ui::VKEY_Z:
424 if (control && !shift && !alt && editable)
425 cursor_changed = text_changed = model_->Undo();
426 else if (control && shift && !alt && editable)
427 cursor_changed = text_changed = model_->Redo();
428 break;
429 case ui::VKEY_Y:
430 if (control && !alt && editable)
431 cursor_changed = text_changed = model_->Redo();
432 break;
433 case ui::VKEY_A:
434 if (control && !alt) {
435 model_->SelectAll(false);
436 cursor_changed = true;
437 }
438 break;
439 case ui::VKEY_X:
440 if (control && !alt && editable && readable)
441 cursor_changed = text_changed = Cut();
442 break;
443 case ui::VKEY_C:
444 if (control && !alt && readable)
445 Copy();
446 break;
447 case ui::VKEY_V:
448 if (control && !alt && editable)
449 cursor_changed = text_changed = Paste();
450 break;
451 case ui::VKEY_RIGHT:
452 case ui::VKEY_LEFT: {
453 // We should ignore the alt-left/right keys because alt key doesn't make
454 // any special effects for them and they can be shortcut keys such like
455 // forward/back of the browser history.
456 if (alt)
457 break;
458 const gfx::Range selection_range = render_text->selection();
459 model_->MoveCursor(
460 control ? gfx::WORD_BREAK : gfx::CHARACTER_BREAK,
461 (key_code == ui::VKEY_RIGHT) ? gfx::CURSOR_RIGHT : gfx::CURSOR_LEFT,
462 shift);
463 cursor_changed = render_text->selection() != selection_range;
464 break;
465 }
466 case ui::VKEY_END:
467 case ui::VKEY_HOME:
468 if ((key_code == ui::VKEY_HOME) ==
469 (render_text->GetTextDirection() == base::i18n::RIGHT_TO_LEFT))
470 model_->MoveCursor(gfx::LINE_BREAK, gfx::CURSOR_RIGHT, shift);
471 else
472 model_->MoveCursor(gfx::LINE_BREAK, gfx::CURSOR_LEFT, shift);
473 cursor_changed = true;
474 break;
475 case ui::VKEY_BACK:
476 case ui::VKEY_DELETE:
477 if (!editable)
478 break;
479 if (!model_->HasSelection()) {
480 gfx::VisualCursorDirection direction = (key_code == ui::VKEY_DELETE) ?
481 gfx::CURSOR_RIGHT : gfx::CURSOR_LEFT;
482 if (shift && control) {
483 // If both shift and control are pressed, then erase up to the
484 // beginning/end of the buffer in ChromeOS. Otherwise, do nothing.
485 model_->MoveCursor(gfx::LINE_BREAK, direction, true);
486 #if !defined(OS_CHROMEOS)
487 break;
488 #endif
489 } else if (control) {
490 // If only control is pressed, then erase the previous/next word.
491 model_->MoveCursor(gfx::WORD_BREAK, direction, true);
492 }
493 }
494 if (key_code == ui::VKEY_BACK)
495 model_->Backspace();
496 else if (shift && model_->HasSelection() && readable)
497 Cut();
498 else
499 model_->Delete();
500
501 // Consume backspace and delete keys even if the edit did nothing. This
502 // prevents potential unintended side-effects of further event handling.
503 text_changed = true;
504 break;
505 case ui::VKEY_INSERT:
506 if (control && !shift && readable)
507 Copy();
508 else if (shift && !control && editable)
509 cursor_changed = text_changed = Paste();
510 break;
511 default:
512 break;
513 }
514
515 // We must have input method in order to support text input.
516 DCHECK(GetInputMethod());
517 UpdateAfterChange(text_changed, cursor_changed);
518 OnAfterUserAction();
519 return (text_changed || cursor_changed);
520 }
521 return false;
522 }
523
524 bool Textfield::OnMousePressed(const ui::MouseEvent& event) {
525 OnBeforeUserAction();
526 TrackMouseClicks(event);
527
528 if (!controller_ || !controller_->HandleMouseEvent(this, event)) {
529 if (event.IsOnlyLeftMouseButton() || event.IsOnlyRightMouseButton())
530 RequestFocus();
531
532 if (event.IsOnlyLeftMouseButton()) {
533 initiating_drag_ = false;
534 bool can_drag = true;
535
536 switch (aggregated_clicks_) {
537 case 0:
538 if (can_drag &&
539 GetRenderText()->IsPointInSelection(event.location())) {
540 initiating_drag_ = true;
541 } else {
542 MoveCursorTo(event.location(), event.IsShiftDown());
543 }
544 break;
545 case 1:
546 MoveCursorTo(event.location(), false);
547 model_->SelectWord();
548 double_click_word_ = GetRenderText()->selection();
549 OnCaretBoundsChanged();
550 break;
551 case 2:
552 model_->SelectAll(false);
553 OnCaretBoundsChanged();
554 break;
555 default:
556 NOTREACHED();
557 }
558 }
559 SchedulePaint();
560 }
561
562 OnAfterUserAction();
563 touch_selection_controller_.reset();
474 return true; 564 return true;
475 } 565 }
476 566
567 bool Textfield::OnMouseDragged(const ui::MouseEvent& event) {
568 // Don't adjust the cursor on a potential drag and drop, or if the mouse
569 // movement from the last mouse click does not exceed the drag threshold.
570 if (initiating_drag_ || !event.IsOnlyLeftMouseButton() ||
571 !ExceededDragThreshold(event.location() - last_click_location_)) {
572 return true;
573 }
574
575 if (!event.IsOnlyRightMouseButton()) {
576 OnBeforeUserAction();
577 MoveCursorTo(event.location(), true);
578 if (aggregated_clicks_ == 1) {
579 model_->SelectWord();
580 // Expand the selection so the initially selected word remains selected.
581 gfx::Range selection = GetRenderText()->selection();
582 const size_t min = std::min(selection.GetMin(),
583 double_click_word_.GetMin());
584 const size_t max = std::max(selection.GetMax(),
585 double_click_word_.GetMax());
586 const bool reversed = selection.is_reversed();
587 selection.set_start(reversed ? max : min);
588 selection.set_end(reversed ? min : max);
589 model_->SelectRange(selection);
590 }
591 SchedulePaint();
592 OnAfterUserAction();
593 }
594 return true;
595 }
596
597 void Textfield::OnMouseReleased(const ui::MouseEvent& event) {
598 OnBeforeUserAction();
599 // Cancel suspected drag initiations, the user was clicking in the selection.
600 if (initiating_drag_ && MoveCursorTo(event.location(), false))
601 SchedulePaint();
602 initiating_drag_ = false;
603 OnAfterUserAction();
604 }
605
477 void Textfield::OnFocus() { 606 void Textfield::OnFocus() {
478 if (textfield_view_) 607 GetRenderText()->set_focused(true);
479 textfield_view_->HandleFocus(); 608 is_cursor_visible_ = true;
609 SchedulePaint();
610 GetInputMethod()->OnFocus();
611 OnCaretBoundsChanged();
612
613 const size_t caret_blink_ms = Textfield::GetCaretBlinkMs();
614 if (caret_blink_ms != 0) {
615 cursor_repaint_timer_.Start(FROM_HERE,
616 base::TimeDelta::FromMilliseconds(caret_blink_ms), this,
617 &Textfield::UpdateCursor);
618 }
619
480 View::OnFocus(); 620 View::OnFocus();
481 SchedulePaint(); 621 SchedulePaint();
482 } 622 }
483 623
484 void Textfield::OnBlur() { 624 void Textfield::OnBlur() {
485 if (textfield_view_) 625 GetRenderText()->set_focused(false);
486 textfield_view_->HandleBlur(); 626 GetInputMethod()->OnBlur();
627 cursor_repaint_timer_.Stop();
628 if (is_cursor_visible_) {
629 is_cursor_visible_ = false;
630 RepaintCursor();
631 }
632
633 touch_selection_controller_.reset();
487 634
488 // Border typically draws focus indicator. 635 // Border typically draws focus indicator.
489 SchedulePaint(); 636 SchedulePaint();
490 } 637 }
491 638
492 void Textfield::GetAccessibleState(ui::AccessibleViewState* state) { 639 void Textfield::GetAccessibleState(ui::AccessibleViewState* state) {
493 state->role = ui::AccessibilityTypes::ROLE_TEXT; 640 state->role = ui::AccessibilityTypes::ROLE_TEXT;
494 state->name = accessible_name_; 641 state->name = accessible_name_;
495 if (read_only()) 642 if (read_only())
496 state->state |= ui::AccessibilityTypes::STATE_READONLY; 643 state->state |= ui::AccessibilityTypes::STATE_READONLY;
497 if (IsObscured()) 644 if (IsObscured())
498 state->state |= ui::AccessibilityTypes::STATE_PROTECTED; 645 state->state |= ui::AccessibilityTypes::STATE_PROTECTED;
499 state->value = text_; 646 state->value = text();
500 647
501 const gfx::Range range = textfield_view_->GetSelectedRange(); 648 const gfx::Range range = GetSelectedRange();
502 state->selection_start = range.start(); 649 state->selection_start = range.start();
503 state->selection_end = range.end(); 650 state->selection_end = range.end();
504 651
505 if (!read_only()) { 652 if (!read_only()) {
506 state->set_value_callback = 653 state->set_value_callback =
507 base::Bind(&Textfield::AccessibilitySetValue, 654 base::Bind(&Textfield::AccessibilitySetValue,
508 weak_ptr_factory_.GetWeakPtr()); 655 weak_ptr_factory_.GetWeakPtr());
509 } 656 }
510 } 657 }
511 658
512 ui::TextInputClient* Textfield::GetTextInputClient() { 659 ui::TextInputClient* Textfield::GetTextInputClient() {
513 return textfield_view_ ? textfield_view_->GetTextInputClient() : NULL; 660 return read_only_ ? NULL : this;
514 } 661 }
515 662
516 gfx::Point Textfield::GetKeyboardContextMenuLocation() { 663 gfx::Point Textfield::GetKeyboardContextMenuLocation() {
517 return textfield_view_ ? textfield_view_->GetContextMenuLocation() : 664 return GetCaretBounds().bottom_right();
518 View::GetKeyboardContextMenuLocation(); 665 }
666
667 void Textfield::OnNativeThemeChanged(const ui::NativeTheme* theme) {
668 UpdateColorsFromTheme(theme);
519 } 669 }
520 670
521 void Textfield::OnEnabledChanged() { 671 void Textfield::OnEnabledChanged() {
522 View::OnEnabledChanged(); 672 View::OnEnabledChanged();
523 if (textfield_view_) 673 if (GetInputMethod())
524 textfield_view_->UpdateEnabled(); 674 GetInputMethod()->OnTextInputTypeChanged(this);
525 } 675 SchedulePaint();
526
527 void Textfield::ViewHierarchyChanged(
528 const ViewHierarchyChangedDetails& details) {
529 if (details.is_add && !textfield_view_ && GetWidget()) {
530 // The textfield view's lifetime is managed by the view hierarchy.
531 textfield_view_ = new NativeTextfieldViews(this);
532 AddChildViewAt(textfield_view_, 0);
533 Layout();
534 UpdateAllProperties();
535 }
536 } 676 }
537 677
538 const char* Textfield::GetClassName() const { 678 const char* Textfield::GetClassName() const {
539 return kViewClassName; 679 return kViewClassName;
540 } 680 }
541 681
682 gfx::NativeCursor Textfield::GetCursor(const ui::MouseEvent& event) {
683 bool in_selection = GetRenderText()->IsPointInSelection(event.location());
684 bool drag_event = event.type() == ui::ET_MOUSE_DRAGGED;
685 bool text_cursor = !initiating_drag_ && (drag_event || !in_selection);
686 #if defined(USE_AURA)
687 return text_cursor ? ui::kCursorIBeam : ui::kCursorNull;
688 #elif defined(OS_WIN)
689 static HCURSOR ibeam = LoadCursor(NULL, IDC_IBEAM);
690 static HCURSOR arrow = LoadCursor(NULL, IDC_ARROW);
691 return text_cursor ? ibeam : arrow;
692 #endif
693 }
694
695 void Textfield::OnGestureEvent(ui::GestureEvent* event) {
696 switch (event->type()) {
697 case ui::ET_GESTURE_TAP_DOWN:
698 OnBeforeUserAction();
699 RequestFocus();
700 // We don't deselect if the point is in the selection
701 // because TAP_DOWN may turn into a LONG_PRESS.
702 if (!GetRenderText()->IsPointInSelection(event->location()) &&
703 MoveCursorTo(event->location(), false))
704 SchedulePaint();
705 OnAfterUserAction();
706 event->SetHandled();
707 break;
708 case ui::ET_GESTURE_SCROLL_UPDATE:
709 OnBeforeUserAction();
710 if (MoveCursorTo(event->location(), true))
711 SchedulePaint();
712 OnAfterUserAction();
713 event->SetHandled();
714 break;
715 case ui::ET_GESTURE_SCROLL_END:
716 case ui::ET_SCROLL_FLING_START:
717 CreateTouchSelectionControllerAndNotifyIt();
718 event->SetHandled();
719 break;
720 case ui::ET_GESTURE_TAP:
721 if (event->details().tap_count() == 1) {
722 CreateTouchSelectionControllerAndNotifyIt();
723 } else {
724 OnBeforeUserAction();
725 SelectAll(false);
726 OnAfterUserAction();
727 event->SetHandled();
728 }
729 #if defined(OS_WIN) && defined(USE_AURA)
730 if (!read_only())
731 base::win::DisplayVirtualKeyboard();
732 #endif
733 break;
734 case ui::ET_GESTURE_LONG_PRESS:
735 // If long press happens outside selection, select word and show context
736 // menu (If touch selection is enabled, context menu is shown by the
737 // |touch_selection_controller_|, hence we mark the event handled.
738 // Otherwise, the regular context menu will be shown by views).
739 // If long press happens in selected text and touch drag drop is enabled,
740 // we will turn off touch selection (if one exists) and let views do drag
741 // drop.
742 if (!GetRenderText()->IsPointInSelection(event->location())) {
743 OnBeforeUserAction();
744 model_->SelectWord();
745 touch_selection_controller_.reset(
746 ui::TouchSelectionController::create(this));
747 OnCaretBoundsChanged();
748 SchedulePaint();
749 OnAfterUserAction();
750 if (touch_selection_controller_)
751 event->SetHandled();
752 } else if (switches::IsTouchDragDropEnabled()) {
753 initiating_drag_ = true;
754 touch_selection_controller_.reset();
755 } else {
756 if (!touch_selection_controller_)
757 CreateTouchSelectionControllerAndNotifyIt();
758 if (touch_selection_controller_)
759 event->SetHandled();
760 }
761 return;
762 case ui::ET_GESTURE_LONG_TAP:
763 if (!touch_selection_controller_)
764 CreateTouchSelectionControllerAndNotifyIt();
765
766 // If touch selection is enabled, the context menu on long tap will be
767 // shown by the |touch_selection_controller_|, hence we mark the event
768 // handled so views does not try to show context menu on it.
769 if (touch_selection_controller_)
770 event->SetHandled();
771 break;
772 default:
773 return;
774 }
775 }
776
777 bool Textfield::GetDropFormats(
778 int* formats,
779 std::set<OSExchangeData::CustomFormat>* custom_formats) {
780 if (!enabled() || read_only())
781 return false;
782 // TODO(msw): Can we support URL, FILENAME, etc.?
783 *formats = ui::OSExchangeData::STRING;
784 if (controller_)
785 controller_->AppendDropFormats(formats, custom_formats);
786 return true;
787 }
788
789 bool Textfield::CanDrop(const OSExchangeData& data) {
790 int formats;
791 std::set<OSExchangeData::CustomFormat> custom_formats;
792 GetDropFormats(&formats, &custom_formats);
793 return enabled() && !read_only() &&
794 data.HasAnyFormat(formats, custom_formats);
795 }
796
797 int Textfield::OnDragUpdated(const ui::DropTargetEvent& event) {
798 DCHECK(CanDrop(event.data()));
799 gfx::RenderText* render_text = GetRenderText();
800 const gfx::Range& selection = render_text->selection();
801 drop_cursor_position_ = render_text->FindCursorPosition(event.location());
802 bool in_selection = !selection.is_empty() &&
803 selection.Contains(gfx::Range(drop_cursor_position_.caret_pos()));
804 is_drop_cursor_visible_ = !in_selection;
805 // TODO(msw): Pan over text when the user drags to the visible text edge.
806 OnCaretBoundsChanged();
807 SchedulePaint();
808
809 if (initiating_drag_) {
810 if (in_selection)
811 return ui::DragDropTypes::DRAG_NONE;
812 return event.IsControlDown() ? ui::DragDropTypes::DRAG_COPY :
813 ui::DragDropTypes::DRAG_MOVE;
814 }
815 return ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_MOVE;
816 }
817
818 void Textfield::OnDragExited() {
819 is_drop_cursor_visible_ = false;
820 SchedulePaint();
821 }
822
823 int Textfield::OnPerformDrop(const ui::DropTargetEvent& event) {
824 DCHECK(CanDrop(event.data()));
825 is_drop_cursor_visible_ = false;
826
827 if (controller_) {
828 int drag_operation = controller_->OnDrop(event.data());
829 if (drag_operation != ui::DragDropTypes::DRAG_NONE)
830 return drag_operation;
831 }
832
833 gfx::RenderText* render_text = GetRenderText();
834 DCHECK(!initiating_drag_ ||
835 !render_text->IsPointInSelection(event.location()));
836 OnBeforeUserAction();
837 skip_input_method_cancel_composition_ = true;
838
839 gfx::SelectionModel drop_destination_model =
840 render_text->FindCursorPosition(event.location());
841 base::string16 new_text;
842 event.data().GetString(&new_text);
843 new_text = GetTextForDisplay(new_text);
844
845 // Delete the current selection for a drag and drop within this view.
846 const bool move = initiating_drag_ && !event.IsControlDown() &&
847 event.source_operations() & ui::DragDropTypes::DRAG_MOVE;
848 if (move) {
849 // Adjust the drop destination if it is on or after the current selection.
850 size_t pos = drop_destination_model.caret_pos();
851 pos -= render_text->selection().Intersect(gfx::Range(0, pos)).length();
852 model_->DeleteSelectionAndInsertTextAt(new_text, pos);
853 } else {
854 model_->MoveCursorTo(drop_destination_model);
855 // Drop always inserts text even if the textfield is not in insert mode.
856 model_->InsertText(new_text);
857 }
858 skip_input_method_cancel_composition_ = false;
859 UpdateAfterChange(true, true);
860 OnAfterUserAction();
861 return move ? ui::DragDropTypes::DRAG_MOVE : ui::DragDropTypes::DRAG_COPY;
862 }
863
864 void Textfield::OnDragDone() {
865 initiating_drag_ = false;
866 is_drop_cursor_visible_ = false;
867 }
868
869 void Textfield::OnBoundsChanged(const gfx::Rect& previous_bounds) {
870 GetRenderText()->SetDisplayRect(GetContentsBounds());
871 OnCaretBoundsChanged();
872 }
873
874 ////////////////////////////////////////////////////////////////////////////////
875 // Textfield, TextfieldViewsModel::Delegate overrides:
876
877 void Textfield::OnCompositionTextConfirmedOrCleared() {
878 if (!skip_input_method_cancel_composition_)
879 GetInputMethod()->CancelComposition(this);
880 }
881
882 ////////////////////////////////////////////////////////////////////////////////
883 // Textfield, ContextMenuController overrides:
884
885 void Textfield::ShowContextMenuForView(
886 View* source,
887 const gfx::Point& point,
888 ui::MenuSourceType source_type) {
889 UpdateContextMenu();
890 if (context_menu_runner_->RunMenuAt(GetWidget(), NULL,
891 gfx::Rect(point, gfx::Size()), views::MenuItemView::TOPLEFT,
892 source_type,
893 MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU) ==
894 MenuRunner::MENU_DELETED)
895 return;
896 }
897
898 ////////////////////////////////////////////////////////////////////////////////
899 // Textfield, views::DragController overrides:
900
901 void Textfield::WriteDragDataForView(views::View* sender,
902 const gfx::Point& press_pt,
903 OSExchangeData* data) {
904 DCHECK_NE(ui::DragDropTypes::DRAG_NONE,
905 GetDragOperationsForView(sender, press_pt));
906 data->SetString(model_->GetSelectedText());
907 scoped_ptr<gfx::Canvas> canvas(
908 views::GetCanvasForDragImage(GetWidget(), size()));
909 GetRenderText()->DrawSelectedTextForDrag(canvas.get());
910 drag_utils::SetDragImageOnDataObject(*canvas, size(),
911 press_pt.OffsetFromOrigin(),
912 data);
913 if (controller_)
914 controller_->OnWriteDragData(data);
915 }
916
917 int Textfield::GetDragOperationsForView(views::View* sender,
918 const gfx::Point& p) {
919 int drag_operations = ui::DragDropTypes::DRAG_COPY;
920 if (!enabled() || IsObscured() || !GetRenderText()->IsPointInSelection(p))
921 drag_operations = ui::DragDropTypes::DRAG_NONE;
922 else if (sender == this && !read_only())
923 drag_operations =
924 ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY;
925 if (controller_)
926 controller_->OnGetDragOperationsForTextfield(&drag_operations);
927 return drag_operations;
928 }
929
930 bool Textfield::CanStartDragForView(View* sender,
931 const gfx::Point& press_pt,
932 const gfx::Point& p) {
933 return initiating_drag_ && GetRenderText()->IsPointInSelection(press_pt);
934 }
935
936 ////////////////////////////////////////////////////////////////////////////////
937 // Textfield, ui::TouchEditable overrides:
938
939 void Textfield::SelectRect(const gfx::Point& start, const gfx::Point& end) {
940 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE)
941 return;
942
943 gfx::SelectionModel start_caret = GetRenderText()->FindCursorPosition(start);
944 gfx::SelectionModel end_caret = GetRenderText()->FindCursorPosition(end);
945 gfx::SelectionModel selection(
946 gfx::Range(start_caret.caret_pos(), end_caret.caret_pos()),
947 end_caret.caret_affinity());
948
949 OnBeforeUserAction();
950 model_->SelectSelectionModel(selection);
951 OnCaretBoundsChanged();
952 SchedulePaint();
953 OnAfterUserAction();
954 }
955
956 void Textfield::MoveCaretTo(const gfx::Point& point) {
957 SelectRect(point, point);
958 }
959
960 void Textfield::GetSelectionEndPoints(gfx::Rect* p1, gfx::Rect* p2) {
961 gfx::RenderText* render_text = GetRenderText();
962 const gfx::SelectionModel& sel = render_text->selection_model();
963 gfx::SelectionModel start_sel =
964 render_text->GetSelectionModelForSelectionStart();
965 *p1 = render_text->GetCursorBounds(start_sel, true);
966 *p2 = render_text->GetCursorBounds(sel, true);
967 }
968
969 gfx::Rect Textfield::GetBounds() {
970 return bounds();
971 }
972
973 gfx::NativeView Textfield::GetNativeView() const {
974 return GetWidget()->GetNativeView();
975 }
976
977 void Textfield::ConvertPointToScreen(gfx::Point* point) {
978 View::ConvertPointToScreen(this, point);
979 }
980
981 void Textfield::ConvertPointFromScreen(gfx::Point* point) {
982 View::ConvertPointFromScreen(this, point);
983 }
984
985 bool Textfield::DrawsHandles() {
986 return false;
987 }
988
989 void Textfield::OpenContextMenu(const gfx::Point& anchor) {
990 touch_selection_controller_.reset();
991 ShowContextMenu(anchor, ui::MENU_SOURCE_TOUCH_EDIT_MENU);
992 }
993
994 ////////////////////////////////////////////////////////////////////////////////
995 // Textfield, ui::SimpleMenuModel::Delegate overrides:
996
997 bool Textfield::IsCommandIdChecked(int command_id) const {
998 return true;
999 }
1000
1001 bool Textfield::IsCommandIdEnabled(int command_id) const {
1002 base::string16 result;
1003 bool editable = !read_only();
1004 switch (command_id) {
1005 case IDS_APP_UNDO:
1006 return editable && model_->CanUndo();
1007 case IDS_APP_CUT:
1008 return editable && model_->HasSelection() && !IsObscured();
1009 case IDS_APP_COPY:
1010 return model_->HasSelection() && !IsObscured();
1011 case IDS_APP_PASTE:
1012 ui::Clipboard::GetForCurrentThread()->ReadText(
1013 ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1014 return editable && !result.empty();
1015 case IDS_APP_DELETE:
1016 return editable && model_->HasSelection();
1017 case IDS_APP_SELECT_ALL:
1018 return !text().empty();
1019 default:
1020 return false;
1021 }
1022 }
1023
1024 bool Textfield::GetAcceleratorForCommandId(int command_id,
1025 ui::Accelerator* accelerator) {
1026 return false;
1027 }
1028
1029 void Textfield::ExecuteCommand(int command_id, int event_flags) {
1030 touch_selection_controller_.reset();
1031 if (!IsCommandIdEnabled(command_id))
1032 return;
1033
1034 bool text_changed = false;
1035 switch (command_id) {
1036 case IDS_APP_UNDO:
1037 OnBeforeUserAction();
1038 text_changed = model_->Undo();
1039 UpdateAfterChange(text_changed, text_changed);
1040 OnAfterUserAction();
1041 break;
1042 case IDS_APP_CUT:
1043 OnBeforeUserAction();
1044 text_changed = Cut();
1045 UpdateAfterChange(text_changed, text_changed);
1046 OnAfterUserAction();
1047 break;
1048 case IDS_APP_COPY:
1049 OnBeforeUserAction();
1050 Copy();
1051 OnAfterUserAction();
1052 break;
1053 case IDS_APP_PASTE:
1054 OnBeforeUserAction();
1055 text_changed = Paste();
1056 UpdateAfterChange(text_changed, text_changed);
1057 OnAfterUserAction();
1058 break;
1059 case IDS_APP_DELETE:
1060 OnBeforeUserAction();
1061 text_changed = model_->Delete();
1062 UpdateAfterChange(text_changed, text_changed);
1063 OnAfterUserAction();
1064 break;
1065 case IDS_APP_SELECT_ALL:
1066 OnBeforeUserAction();
1067 SelectAll(false);
1068 UpdateAfterChange(false, true);
1069 OnAfterUserAction();
1070 break;
1071 default:
1072 NOTREACHED();
1073 break;
1074 }
1075 }
1076
1077 ////////////////////////////////////////////////////////////////////////////////
1078 // Textfield, ui::TextInputClient overrides:
1079
1080 void Textfield::SetCompositionText(const ui::CompositionText& composition) {
1081 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE)
1082 return;
1083
1084 OnBeforeUserAction();
1085 skip_input_method_cancel_composition_ = true;
1086 model_->SetCompositionText(composition);
1087 skip_input_method_cancel_composition_ = false;
1088 UpdateAfterChange(true, true);
1089 OnAfterUserAction();
1090 }
1091
1092 void Textfield::ConfirmCompositionText() {
1093 if (!model_->HasCompositionText())
1094 return;
1095
1096 OnBeforeUserAction();
1097 skip_input_method_cancel_composition_ = true;
1098 model_->ConfirmCompositionText();
1099 skip_input_method_cancel_composition_ = false;
1100 UpdateAfterChange(true, true);
1101 OnAfterUserAction();
1102 }
1103
1104 void Textfield::ClearCompositionText() {
1105 if (!model_->HasCompositionText())
1106 return;
1107
1108 OnBeforeUserAction();
1109 skip_input_method_cancel_composition_ = true;
1110 model_->CancelCompositionText();
1111 skip_input_method_cancel_composition_ = false;
1112 UpdateAfterChange(true, true);
1113 OnAfterUserAction();
1114 }
1115
1116 void Textfield::InsertText(const base::string16& new_text) {
1117 // TODO(suzhe): Filter invalid characters.
1118 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || new_text.empty())
1119 return;
1120
1121 OnBeforeUserAction();
1122 skip_input_method_cancel_composition_ = true;
1123 if (GetRenderText()->insert_mode())
1124 model_->InsertText(GetTextForDisplay(new_text));
1125 else
1126 model_->ReplaceText(GetTextForDisplay(new_text));
1127 skip_input_method_cancel_composition_ = false;
1128 UpdateAfterChange(true, true);
1129 OnAfterUserAction();
1130 }
1131
1132 void Textfield::InsertChar(base::char16 ch, int flags) {
1133 // Filter out all control characters, including tab and new line characters,
1134 // and all characters with Alt modifier. But allow characters with the AltGr
1135 // modifier. On Windows AltGr is represented by Alt+Ctrl, and on Linux it's a
1136 // different flag that we don't care about.
1137 const bool should_insert_char = ((ch >= 0x20 && ch < 0x7F) || ch > 0x9F) &&
1138 (flags & ~(ui::EF_SHIFT_DOWN | ui::EF_CAPS_LOCK_DOWN)) != ui::EF_ALT_DOWN;
1139 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || !should_insert_char)
1140 return;
1141
1142 OnBeforeUserAction();
1143 skip_input_method_cancel_composition_ = true;
1144 if (GetRenderText()->insert_mode())
1145 model_->InsertChar(ch);
1146 else
1147 model_->ReplaceChar(ch);
1148 skip_input_method_cancel_composition_ = false;
1149
1150 model_->SetText(GetTextForDisplay(text()));
1151
1152 UpdateAfterChange(true, true);
1153 OnAfterUserAction();
1154
1155 if (IsObscured() && obscured_reveal_duration_ != base::TimeDelta()) {
1156 const size_t change_offset = model_->GetCursorPosition();
1157 DCHECK_GT(change_offset, 0u);
1158 RevealObscuredChar(change_offset - 1, obscured_reveal_duration_);
1159 }
1160 }
1161
1162 gfx::NativeWindow Textfield::GetAttachedWindow() const {
1163 // Imagine the following hierarchy.
1164 // [NativeWidget A] - FocusManager
1165 // [View]
1166 // [NativeWidget B]
1167 // [View]
1168 // [View X]
1169 // An important thing is that [NativeWidget A] owns Win32 input focus even
1170 // when [View X] is logically focused by FocusManager. As a result, an Win32
1171 // IME may want to interact with the native view of [NativeWidget A] rather
1172 // than that of [NativeWidget B]. This is why we need to call
1173 // GetTopLevelWidget() here.
1174 return GetWidget()->GetTopLevelWidget()->GetNativeView();
1175 }
1176
1177 ui::TextInputType Textfield::GetTextInputType() const {
1178 if (read_only() || !enabled())
1179 return ui::TEXT_INPUT_TYPE_NONE;
1180 return text_input_type_;
1181 }
1182
1183 ui::TextInputMode Textfield::GetTextInputMode() const {
1184 return ui::TEXT_INPUT_MODE_DEFAULT;
1185 }
1186
1187 bool Textfield::CanComposeInline() const {
1188 return true;
1189 }
1190
1191 gfx::Rect Textfield::GetCaretBounds() const {
1192 gfx::Rect rect = GetRenderText()->GetUpdatedCursorBounds();
1193 ConvertRectToScreen(this, &rect);
1194 return rect;
1195 }
1196
1197 bool Textfield::GetCompositionCharacterBounds(uint32 index,
1198 gfx::Rect* rect) const {
1199 DCHECK(rect);
1200 if (!HasCompositionText())
1201 return false;
1202 gfx::RenderText* render_text = GetRenderText();
1203 const gfx::Range& composition_range = render_text->GetCompositionRange();
1204 DCHECK(!composition_range.is_empty());
1205
1206 size_t text_index = composition_range.start() + index;
1207 if (composition_range.end() <= text_index)
1208 return false;
1209 if (!render_text->IsCursorablePosition(text_index)) {
1210 text_index = render_text->IndexOfAdjacentGrapheme(
1211 text_index, gfx::CURSOR_BACKWARD);
1212 }
1213 if (text_index < composition_range.start())
1214 return false;
1215 const gfx::SelectionModel caret(text_index, gfx::CURSOR_BACKWARD);
1216 *rect = render_text->GetCursorBounds(caret, false);
1217 ConvertRectToScreen(this, rect);
1218 return true;
1219 }
1220
1221 bool Textfield::HasCompositionText() const {
1222 return model_->HasCompositionText();
1223 }
1224
1225 bool Textfield::GetTextRange(gfx::Range* range) const {
1226 if (!ImeEditingAllowed())
1227 return false;
1228
1229 model_->GetTextRange(range);
1230 return true;
1231 }
1232
1233 bool Textfield::GetCompositionTextRange(gfx::Range* range) const {
1234 if (!ImeEditingAllowed())
1235 return false;
1236
1237 model_->GetCompositionTextRange(range);
1238 return true;
1239 }
1240
1241 bool Textfield::GetSelectionRange(gfx::Range* range) const {
1242 if (!ImeEditingAllowed())
1243 return false;
1244 *range = GetRenderText()->selection();
1245 return true;
1246 }
1247
1248 bool Textfield::SetSelectionRange(const gfx::Range& range) {
1249 if (!ImeEditingAllowed() || !range.IsValid())
1250 return false;
1251
1252 OnBeforeUserAction();
1253 SelectRange(range);
1254 OnAfterUserAction();
1255 return true;
1256 }
1257
1258 bool Textfield::DeleteRange(const gfx::Range& range) {
1259 if (!ImeEditingAllowed() || range.is_empty())
1260 return false;
1261
1262 OnBeforeUserAction();
1263 model_->SelectRange(range);
1264 if (model_->HasSelection()) {
1265 model_->DeleteSelection();
1266 UpdateAfterChange(true, true);
1267 }
1268 OnAfterUserAction();
1269 return true;
1270 }
1271
1272 bool Textfield::GetTextFromRange(const gfx::Range& range,
1273 base::string16* range_text) const {
1274 if (!ImeEditingAllowed() || !range.IsValid())
1275 return false;
1276
1277 gfx::Range text_range;
1278 if (!GetTextRange(&text_range) || !text_range.Contains(range))
1279 return false;
1280
1281 *range_text = model_->GetTextFromRange(range);
1282 return true;
1283 }
1284
1285 void Textfield::OnInputMethodChanged() {}
1286
1287 bool Textfield::ChangeTextDirectionAndLayoutAlignment(
1288 base::i18n::TextDirection direction) {
1289 // Restore text directionality mode when the indicated direction matches the
1290 // current forced mode; otherwise, force the mode indicated. This helps users
1291 // manage BiDi text layout without getting stuck in forced LTR or RTL modes.
1292 const gfx::DirectionalityMode mode = direction == base::i18n::RIGHT_TO_LEFT ?
1293 gfx::DIRECTIONALITY_FORCE_RTL : gfx::DIRECTIONALITY_FORCE_LTR;
1294 if (mode == GetRenderText()->directionality_mode())
1295 GetRenderText()->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_TEXT);
1296 else
1297 GetRenderText()->SetDirectionalityMode(mode);
1298 SchedulePaint();
1299 return true;
1300 }
1301
1302 void Textfield::ExtendSelectionAndDelete(size_t before, size_t after) {
1303 gfx::Range range = GetRenderText()->selection();
1304 DCHECK_GE(range.start(), before);
1305
1306 range.set_start(range.start() - before);
1307 range.set_end(range.end() + after);
1308 gfx::Range text_range;
1309 if (GetTextRange(&text_range) && text_range.Contains(range))
1310 DeleteRange(range);
1311 }
1312
1313 void Textfield::EnsureCaretInRect(const gfx::Rect& rect) {}
1314
1315 void Textfield::OnCandidateWindowShown() {}
1316
1317 void Textfield::OnCandidateWindowUpdated() {}
1318
1319 void Textfield::OnCandidateWindowHidden() {}
1320
1321 ////////////////////////////////////////////////////////////////////////////////
1322 // Textfield, protected:
1323
1324 gfx::RenderText* Textfield::GetRenderText() const {
1325 return model_->render_text();
1326 }
1327
542 //////////////////////////////////////////////////////////////////////////////// 1328 ////////////////////////////////////////////////////////////////////////////////
543 // Textfield, private: 1329 // Textfield, private:
544 1330
545 gfx::Insets Textfield::GetTextInsets() const { 1331 base::string16 Textfield::GetTextForDisplay(const base::string16& raw) {
546 gfx::Insets insets = GetInsets(); 1332 return style_ & Textfield::STYLE_LOWERCASE ? base::i18n::ToLower(raw) : raw;
547 if (draw_border_ && textfield_view_)
548 insets += textfield_view_->GetInsets();
549 return insets;
550 } 1333 }
551 1334
552 void Textfield::AccessibilitySetValue(const base::string16& new_value) { 1335 void Textfield::AccessibilitySetValue(const base::string16& new_value) {
553 if (!read_only()) { 1336 if (!read_only()) {
554 SetText(new_value); 1337 SetText(new_value);
555 ClearSelection(); 1338 ClearSelection();
556 } 1339 }
557 } 1340 }
558 1341
1342 void Textfield::UpdateBackgroundColor() {
1343 const SkColor color = GetBackgroundColor();
1344 set_background(Background::CreateSolidBackground(color));
1345 GetRenderText()->set_background_is_transparent(SkColorGetA(color) != 0xFF);
1346 SchedulePaint();
1347 }
1348
1349 void Textfield::UpdateColorsFromTheme(const ui::NativeTheme* theme) {
1350 gfx::RenderText* render_text = GetRenderText();
1351 render_text->SetColor(GetTextColor());
1352 UpdateBackgroundColor();
1353 render_text->set_cursor_color(GetTextColor());
1354 render_text->set_selection_color(theme->GetSystemColor(
1355 ui::NativeTheme::kColorId_TextfieldSelectionColor));
1356 render_text->set_selection_background_focused_color(theme->GetSystemColor(
1357 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused));
1358 }
1359
1360 void Textfield::UpdateAfterChange(bool text_changed, bool cursor_changed) {
1361 if (text_changed) {
1362 if (controller_)
1363 controller_->ContentsChanged(this, text());
1364 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_TEXT_CHANGED, true);
1365 }
1366 if (cursor_changed) {
1367 is_cursor_visible_ = true;
1368 RepaintCursor();
1369 if (cursor_repaint_timer_.IsRunning())
1370 cursor_repaint_timer_.Reset();
1371 if (!text_changed) {
1372 // TEXT_CHANGED implies SELECTION_CHANGED, so we only need to fire
1373 // this if only the selection changed.
1374 NotifyAccessibilityEvent(
1375 ui::AccessibilityTypes::EVENT_SELECTION_CHANGED, true);
1376 }
1377 }
1378 if (text_changed || cursor_changed) {
1379 OnCaretBoundsChanged();
1380 SchedulePaint();
1381 }
1382 }
1383
1384 void Textfield::UpdateCursor() {
1385 const size_t caret_blink_ms = Textfield::GetCaretBlinkMs();
1386 is_cursor_visible_ = !is_cursor_visible_ || (caret_blink_ms == 0);
1387 RepaintCursor();
1388 }
1389
1390 void Textfield::RepaintCursor() {
1391 gfx::Rect r(GetRenderText()->GetUpdatedCursorBounds());
1392 r.Inset(-1, -1, -1, -1);
1393 SchedulePaintInRect(r);
1394 }
1395
1396 void Textfield::PaintTextAndCursor(gfx::Canvas* canvas) {
1397 TRACE_EVENT0("views", "Textfield::PaintTextAndCursor");
1398 canvas->Save();
1399 gfx::RenderText* render_text = GetRenderText();
1400 render_text->set_cursor_visible(!is_drop_cursor_visible_ &&
1401 is_cursor_visible_ && !model_->HasSelection());
1402 // Draw the text, cursor, and selection.
1403 render_text->Draw(canvas);
1404
1405 // Draw the detached drop cursor that marks where the text will be dropped.
1406 if (is_drop_cursor_visible_)
1407 render_text->DrawCursor(canvas, drop_cursor_position_);
1408
1409 // Draw placeholder text if needed.
1410 if (text().empty() && !GetPlaceholderText().empty()) {
1411 canvas->DrawStringRect(GetPlaceholderText(), GetFontList(),
1412 placeholder_text_color(), render_text->display_rect());
1413 }
1414 canvas->Restore();
1415 }
1416
1417 bool Textfield::MoveCursorTo(const gfx::Point& point, bool select) {
1418 if (!model_->MoveCursorTo(point, select))
1419 return false;
1420 OnCaretBoundsChanged();
1421 return true;
1422 }
1423
1424 void Textfield::OnCaretBoundsChanged() {
1425 if (GetInputMethod())
1426 GetInputMethod()->OnCaretBoundsChanged(this);
1427 if (touch_selection_controller_)
1428 touch_selection_controller_->SelectionChanged();
1429 }
1430
1431 void Textfield::OnBeforeUserAction() {
1432 if (controller_)
1433 controller_->OnBeforeUserAction(this);
1434 }
1435
1436 void Textfield::OnAfterUserAction() {
1437 if (controller_)
1438 controller_->OnAfterUserAction(this);
1439 }
1440
1441 bool Textfield::Cut() {
1442 if (!read_only() && !IsObscured() && model_->Cut()) {
1443 if (controller_)
1444 controller_->OnAfterCutOrCopy();
1445 return true;
1446 }
1447 return false;
1448 }
1449
1450 bool Textfield::Copy() {
1451 if (!IsObscured() && model_->Copy()) {
1452 if (controller_)
1453 controller_->OnAfterCutOrCopy();
1454 return true;
1455 }
1456 return false;
1457 }
1458
1459 bool Textfield::Paste() {
1460 if (read_only())
1461 return false;
1462
1463 const base::string16 original_text = text();
1464 if (model_->Paste()) {
1465 // As Paste is handled in model_->Paste(), the RenderText may contain
1466 // upper case characters. This is not consistent with other places
1467 // which keeps RenderText only containing lower case characters.
1468 base::string16 new_text = GetTextForDisplay(text());
1469 model_->SetText(new_text);
1470 if (controller_)
1471 controller_->OnAfterPaste();
1472 return true;
1473 }
1474 return false;
1475 }
1476
1477 void Textfield::UpdateContextMenu() {
1478 if (!context_menu_contents_.get()) {
1479 context_menu_contents_.reset(new ui::SimpleMenuModel(this));
1480 context_menu_contents_->AddItemWithStringId(IDS_APP_UNDO, IDS_APP_UNDO);
1481 context_menu_contents_->AddSeparator(ui::NORMAL_SEPARATOR);
1482 context_menu_contents_->AddItemWithStringId(IDS_APP_CUT, IDS_APP_CUT);
1483 context_menu_contents_->AddItemWithStringId(IDS_APP_COPY, IDS_APP_COPY);
1484 context_menu_contents_->AddItemWithStringId(IDS_APP_PASTE, IDS_APP_PASTE);
1485 context_menu_contents_->AddItemWithStringId(IDS_APP_DELETE, IDS_APP_DELETE);
1486 context_menu_contents_->AddSeparator(ui::NORMAL_SEPARATOR);
1487 context_menu_contents_->AddItemWithStringId(IDS_APP_SELECT_ALL,
1488 IDS_APP_SELECT_ALL);
1489 if (controller_)
1490 controller_->UpdateContextMenu(context_menu_contents_.get());
1491 context_menu_runner_.reset(new MenuRunner(context_menu_contents_.get()));
1492 }
1493 }
1494
1495 void Textfield::TrackMouseClicks(const ui::MouseEvent& event) {
1496 if (event.IsOnlyLeftMouseButton()) {
1497 base::TimeDelta time_delta = event.time_stamp() - last_click_time_;
1498 if (time_delta.InMilliseconds() <= GetDoubleClickInterval() &&
1499 !ExceededDragThreshold(event.location() - last_click_location_)) {
1500 // Upon clicking after a triple click, the count should go back to double
1501 // click and alternate between double and triple. This assignment maps
1502 // 0 to 1, 1 to 2, 2 to 1.
1503 aggregated_clicks_ = (aggregated_clicks_ % 2) + 1;
1504 } else {
1505 aggregated_clicks_ = 0;
1506 }
1507 last_click_time_ = event.time_stamp();
1508 last_click_location_ = event.location();
1509 }
1510 }
1511
1512 bool Textfield::ImeEditingAllowed() const {
1513 // Disallow input method editing of password fields.
1514 ui::TextInputType t = GetTextInputType();
1515 return (t != ui::TEXT_INPUT_TYPE_NONE && t != ui::TEXT_INPUT_TYPE_PASSWORD);
1516 }
1517
1518 void Textfield::RevealObscuredChar(int index, const base::TimeDelta& duration) {
1519 GetRenderText()->SetObscuredRevealIndex(index);
1520 SchedulePaint();
1521
1522 if (index != -1) {
1523 obscured_reveal_timer_.Start(FROM_HERE, duration,
1524 base::Bind(&Textfield::RevealObscuredChar, base::Unretained(this),
1525 -1, base::TimeDelta()));
1526 }
1527 }
1528
1529 void Textfield::CreateTouchSelectionControllerAndNotifyIt() {
1530 if (!touch_selection_controller_) {
1531 touch_selection_controller_.reset(
1532 ui::TouchSelectionController::create(this));
1533 }
1534 if (touch_selection_controller_)
1535 touch_selection_controller_->SelectionChanged();
1536 }
1537
559 } // namespace views 1538 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/textfield/textfield.h ('k') | ui/views/controls/textfield/textfield_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698