OLD | NEW |
---|---|
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/native_textfield_views.h" | 5 #include "ui/views/controls/textfield/native_textfield_views.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/debug/trace_event.h" | 11 #include "base/debug/trace_event.h" |
12 #include "base/i18n/case_conversion.h" | |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
14 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
15 #include "grit/app_locale_settings.h" | 16 #include "grit/app_locale_settings.h" |
16 #include "grit/ui_strings.h" | 17 #include "grit/ui_strings.h" |
17 #include "third_party/skia/include/core/SkColor.h" | 18 #include "third_party/skia/include/core/SkColor.h" |
18 #include "ui/base/clipboard/clipboard.h" | 19 #include "ui/base/clipboard/clipboard.h" |
19 #include "ui/base/dragdrop/drag_drop_types.h" | 20 #include "ui/base/dragdrop/drag_drop_types.h" |
20 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
21 #include "ui/base/range/range.h" | 22 #include "ui/base/range/range.h" |
22 #include "ui/gfx/canvas.h" | 23 #include "ui/gfx/canvas.h" |
23 #include "ui/gfx/compositor/layer.h" | 24 #include "ui/gfx/compositor/layer.h" |
24 #include "ui/gfx/insets.h" | 25 #include "ui/gfx/insets.h" |
25 #include "ui/gfx/render_text.h" | 26 #include "ui/gfx/render_text.h" |
26 #include "ui/views/background.h" | 27 #include "ui/views/background.h" |
27 #include "ui/views/border.h" | 28 #include "ui/views/border.h" |
28 #include "ui/views/controls/focusable_border.h" | 29 #include "ui/views/controls/focusable_border.h" |
29 #include "ui/views/controls/menu/menu_item_view.h" | 30 #include "ui/views/controls/menu/menu_item_view.h" |
30 #include "ui/views/controls/menu/menu_model_adapter.h" | 31 #include "ui/views/controls/menu/menu_model_adapter.h" |
31 #include "ui/views/controls/menu/menu_runner.h" | 32 #include "ui/views/controls/menu/menu_runner.h" |
32 #include "ui/views/controls/textfield/textfield.h" | 33 #include "ui/views/controls/textfield/textfield.h" |
33 #include "ui/views/controls/textfield/textfield_controller.h" | 34 #include "ui/views/controls/textfield/textfield_controller.h" |
34 #include "ui/views/controls/textfield/textfield_views_model.h" | 35 #include "ui/views/controls/textfield/textfield_views_model.h" |
35 #include "ui/views/events/event.h" | 36 #include "ui/views/events/event.h" |
36 #include "ui/views/ime/input_method.h" | 37 #include "ui/views/ime/input_method.h" |
37 #include "ui/views/metrics.h" | 38 #include "ui/views/metrics.h" |
38 #include "ui/views/views_delegate.h" | 39 #include "ui/views/views_delegate.h" |
39 #include "ui/views/widget/widget.h" | 40 #include "ui/views/widget/widget.h" |
41 #include "unicode/uchar.h" | |
40 | 42 |
41 #if defined(USE_AURA) | 43 #if defined(USE_AURA) |
42 #include "ui/base/cursor/cursor.h" | 44 #include "ui/base/cursor/cursor.h" |
43 #endif | 45 #endif |
44 | 46 |
45 namespace { | 47 namespace { |
46 | 48 |
47 // Text color for read only. | 49 // Text color for read only. |
48 const SkColor kReadonlyTextColor = SK_ColorDKGRAY; | 50 const SkColor kReadonlyTextColor = SK_ColorDKGRAY; |
49 | 51 |
(...skipping 20 matching lines...) Expand all Loading... | |
70 skip_input_method_cancel_composition_(false), | 72 skip_input_method_cancel_composition_(false), |
71 initiating_drag_(false), | 73 initiating_drag_(false), |
72 ALLOW_THIS_IN_INITIALIZER_LIST(cursor_timer_(this)), | 74 ALLOW_THIS_IN_INITIALIZER_LIST(cursor_timer_(this)), |
73 aggregated_clicks_(0), | 75 aggregated_clicks_(0), |
74 last_click_time_(), | 76 last_click_time_(), |
75 last_click_location_(), | 77 last_click_location_(), |
76 ALLOW_THIS_IN_INITIALIZER_LIST(touch_selection_controller_( | 78 ALLOW_THIS_IN_INITIALIZER_LIST(touch_selection_controller_( |
77 TouchSelectionController::create(this))) { | 79 TouchSelectionController::create(this))) { |
78 set_border(text_border_); | 80 set_border(text_border_); |
79 | 81 |
80 // Lowercase is not supported. | |
81 DCHECK_NE(parent->style(), Textfield::STYLE_LOWERCASE); | |
82 | |
83 #if defined(OS_CHROMEOS) | 82 #if defined(OS_CHROMEOS) |
84 GetRenderText()->SetFontList(gfx::FontList(l10n_util::GetStringUTF8( | 83 GetRenderText()->SetFontList(gfx::FontList(l10n_util::GetStringUTF8( |
85 IDS_UI_FONT_FAMILY_CROS))); | 84 IDS_UI_FONT_FAMILY_CROS))); |
86 #else | 85 #else |
87 GetRenderText()->SetFontList(gfx::FontList(textfield_->font())); | 86 GetRenderText()->SetFontList(gfx::FontList(textfield_->font())); |
88 #endif | 87 #endif |
89 // Set the default text style. | 88 // Set the default text style. |
90 gfx::StyleRange default_style; | 89 gfx::StyleRange default_style; |
91 default_style.foreground = textfield_->text_color(); | 90 default_style.foreground = textfield_->text_color(); |
92 GetRenderText()->set_default_style(default_style); | 91 GetRenderText()->set_default_style(default_style); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
191 DCHECK(CanDrop(event.data())); | 190 DCHECK(CanDrop(event.data())); |
192 DCHECK(!initiating_drag_ || | 191 DCHECK(!initiating_drag_ || |
193 !GetRenderText()->IsPointInSelection(event.location())); | 192 !GetRenderText()->IsPointInSelection(event.location())); |
194 OnBeforeUserAction(); | 193 OnBeforeUserAction(); |
195 skip_input_method_cancel_composition_ = true; | 194 skip_input_method_cancel_composition_ = true; |
196 | 195 |
197 gfx::SelectionModel drop_destination_model = | 196 gfx::SelectionModel drop_destination_model = |
198 GetRenderText()->FindCursorPosition(event.location()); | 197 GetRenderText()->FindCursorPosition(event.location()); |
199 string16 text; | 198 string16 text; |
200 event.data().GetString(&text); | 199 event.data().GetString(&text); |
200 if (textfield_->style() & Textfield::STYLE_LOWERCASE) | |
201 text = base::i18n::ToLower(text); | |
201 | 202 |
202 // We'll delete the current selection for a drag and drop within this view. | 203 // We'll delete the current selection for a drag and drop within this view. |
203 bool move = initiating_drag_ && !event.IsControlDown() && | 204 bool move = initiating_drag_ && !event.IsControlDown() && |
204 event.source_operations() & ui::DragDropTypes::DRAG_MOVE; | 205 event.source_operations() & ui::DragDropTypes::DRAG_MOVE; |
205 if (move) { | 206 if (move) { |
206 gfx::SelectionModel selected; | 207 gfx::SelectionModel selected; |
207 model_->GetSelectionModel(&selected); | 208 model_->GetSelectionModel(&selected); |
208 // Adjust the drop destination if it is on or after the current selection. | 209 // Adjust the drop destination if it is on or after the current selection. |
209 size_t drop_destination = drop_destination_model.caret_pos(); | 210 size_t drop_destination = drop_destination_model.caret_pos(); |
210 drop_destination -= | 211 drop_destination -= |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 } | 315 } |
315 | 316 |
316 ///////////////////////////////////////////////////////////////// | 317 ///////////////////////////////////////////////////////////////// |
317 // NativeTextfieldViews, NativeTextifieldWrapper overrides: | 318 // NativeTextfieldViews, NativeTextifieldWrapper overrides: |
318 | 319 |
319 string16 NativeTextfieldViews::GetText() const { | 320 string16 NativeTextfieldViews::GetText() const { |
320 return model_->GetText(); | 321 return model_->GetText(); |
321 } | 322 } |
322 | 323 |
323 void NativeTextfieldViews::UpdateText() { | 324 void NativeTextfieldViews::UpdateText() { |
324 model_->SetText(textfield_->text()); | 325 string16 text = textfield_->text(); |
326 if (textfield_->style() & Textfield::STYLE_LOWERCASE) | |
327 text = base::i18n::ToLower(text); | |
328 model_->SetText(text); | |
325 OnCaretBoundsChanged(); | 329 OnCaretBoundsChanged(); |
326 SchedulePaint(); | 330 SchedulePaint(); |
327 textfield_->GetWidget()->NotifyAccessibilityEvent( | 331 textfield_->GetWidget()->NotifyAccessibilityEvent( |
328 textfield_, ui::AccessibilityTypes::EVENT_TEXT_CHANGED, true); | 332 textfield_, ui::AccessibilityTypes::EVENT_TEXT_CHANGED, true); |
329 } | 333 } |
330 | 334 |
331 void NativeTextfieldViews::AppendText(const string16& text) { | 335 void NativeTextfieldViews::AppendText(const string16& text) { |
332 if (text.empty()) | 336 if (text.empty()) |
333 return; | 337 return; |
334 model_->Append(text); | 338 if (textfield_->style() & Textfield::STYLE_LOWERCASE) |
339 model_->Append(base::i18n::ToLower(text)); | |
340 else | |
341 model_->Append(text); | |
335 OnCaretBoundsChanged(); | 342 OnCaretBoundsChanged(); |
336 SchedulePaint(); | 343 SchedulePaint(); |
337 } | 344 } |
338 | 345 |
339 string16 NativeTextfieldViews::GetSelectedText() const { | 346 string16 NativeTextfieldViews::GetSelectedText() const { |
340 return model_->GetSelectedText(); | 347 return model_->GetSelectedText(); |
341 } | 348 } |
342 | 349 |
343 void NativeTextfieldViews::SelectAll() { | 350 void NativeTextfieldViews::SelectAll() { |
344 OnBeforeUserAction(); | 351 OnBeforeUserAction(); |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
661 OnAfterUserAction(); | 668 OnAfterUserAction(); |
662 } | 669 } |
663 | 670 |
664 void NativeTextfieldViews::InsertText(const string16& text) { | 671 void NativeTextfieldViews::InsertText(const string16& text) { |
665 // TODO(suzhe): Filter invalid characters. | 672 // TODO(suzhe): Filter invalid characters. |
666 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || text.empty()) | 673 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || text.empty()) |
667 return; | 674 return; |
668 | 675 |
669 OnBeforeUserAction(); | 676 OnBeforeUserAction(); |
670 skip_input_method_cancel_composition_ = true; | 677 skip_input_method_cancel_composition_ = true; |
678 | |
679 string16 new_text = text; | |
680 if (textfield_->style() & Textfield::STYLE_LOWERCASE) | |
681 new_text = base::i18n::ToLower(new_text); | |
682 | |
671 if (GetRenderText()->insert_mode()) | 683 if (GetRenderText()->insert_mode()) |
672 model_->InsertText(text); | 684 model_->InsertText(new_text); |
673 else | 685 else |
674 model_->ReplaceText(text); | 686 model_->ReplaceText(new_text); |
675 skip_input_method_cancel_composition_ = false; | 687 skip_input_method_cancel_composition_ = false; |
676 UpdateAfterChange(true, true); | 688 UpdateAfterChange(true, true); |
677 OnAfterUserAction(); | 689 OnAfterUserAction(); |
678 } | 690 } |
679 | 691 |
680 void NativeTextfieldViews::InsertChar(char16 ch, int flags) { | 692 void NativeTextfieldViews::InsertChar(char16 ch, int flags) { |
681 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || | 693 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || |
682 !ShouldInsertChar(ch, flags)) { | 694 !ShouldInsertChar(ch, flags)) { |
683 return; | 695 return; |
684 } | 696 } |
685 | 697 |
686 OnBeforeUserAction(); | 698 OnBeforeUserAction(); |
687 skip_input_method_cancel_composition_ = true; | 699 skip_input_method_cancel_composition_ = true; |
688 if (GetRenderText()->insert_mode()) | 700 if (GetRenderText()->insert_mode()) |
689 model_->InsertChar(ch); | 701 model_->InsertChar(ch); |
690 else | 702 else |
691 model_->ReplaceChar(ch); | 703 model_->ReplaceChar(ch); |
692 skip_input_method_cancel_composition_ = false; | 704 skip_input_method_cancel_composition_ = false; |
705 | |
706 if (textfield_->style() & Textfield::STYLE_LOWERCASE) | |
707 model_->SetText(base::i18n::ToLower(GetText())); | |
708 | |
693 UpdateAfterChange(true, true); | 709 UpdateAfterChange(true, true); |
694 OnAfterUserAction(); | 710 OnAfterUserAction(); |
695 } | 711 } |
696 | 712 |
697 ui::TextInputType NativeTextfieldViews::GetTextInputType() const { | 713 ui::TextInputType NativeTextfieldViews::GetTextInputType() const { |
698 return textfield_->GetTextInputType(); | 714 return textfield_->GetTextInputType(); |
699 } | 715 } |
700 | 716 |
701 bool NativeTextfieldViews::CanComposeInline() const { | 717 bool NativeTextfieldViews::CanComposeInline() const { |
702 return true; | 718 return true; |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1042 if (controller) | 1058 if (controller) |
1043 controller->OnAfterCutOrCopy(); | 1059 controller->OnAfterCutOrCopy(); |
1044 return true; | 1060 return true; |
1045 } | 1061 } |
1046 return false; | 1062 return false; |
1047 } | 1063 } |
1048 | 1064 |
1049 bool NativeTextfieldViews::Paste() { | 1065 bool NativeTextfieldViews::Paste() { |
1050 const bool success = model_->Paste(); | 1066 const bool success = model_->Paste(); |
1051 | 1067 |
1052 // Calls TextfieldController::ContentsChanged() explicitly if the paste action | 1068 if (success) { |
1053 // did not change the content at all. See http://crbug.com/79002 | 1069 // As Paste is handled in model_->Paste(), the RenderText may contain |
1054 if (success && GetText() == textfield_->text()) { | 1070 // upper case characters. This is not consistent with other places |
1055 TextfieldController* controller = textfield_->GetController(); | 1071 // which keeps RenderText only containing lower case characters. |
1056 if (controller) | 1072 if (textfield_->style() & Textfield::STYLE_LOWERCASE) |
1057 controller->ContentsChanged(textfield_, textfield_->text()); | 1073 model_->SetText(base::i18n::ToLower(GetText())); |
1074 | |
1075 // Calls TextfieldController::ContentsChanged() explicitly if the paste | |
1076 // action did not change the content at all. See http://crbug.com/79002 | |
1077 if (GetText() == base::i18n::ToLower(textfield_->text())) { | |
xji
2012/04/11 04:06:35
ToLower() here should only be done for STYLE_LOWER
kochi
2012/04/11 04:49:27
Oops... Thanks!
| |
1078 TextfieldController* controller = textfield_->GetController(); | |
1079 if (controller) | |
1080 controller->ContentsChanged(textfield_, textfield_->text()); | |
1081 } | |
1058 } | 1082 } |
1059 return success; | 1083 return success; |
1060 } | 1084 } |
1061 | 1085 |
1062 void NativeTextfieldViews::TrackMouseClicks(const MouseEvent& event) { | 1086 void NativeTextfieldViews::TrackMouseClicks(const MouseEvent& event) { |
1063 if (event.IsOnlyLeftMouseButton()) { | 1087 if (event.IsOnlyLeftMouseButton()) { |
1064 base::TimeDelta time_delta = event.time_stamp() - last_click_time_; | 1088 base::TimeDelta time_delta = event.time_stamp() - last_click_time_; |
1065 if (time_delta.InMilliseconds() <= GetDoubleClickInterval() && | 1089 if (time_delta.InMilliseconds() <= GetDoubleClickInterval() && |
1066 !ExceededDragThresholdFromLastClickLocation(event)) { | 1090 !ExceededDragThresholdFromLastClickLocation(event)) { |
1067 aggregated_clicks_ = (aggregated_clicks_ + 1) % 3; | 1091 aggregated_clicks_ = (aggregated_clicks_ + 1) % 3; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1122 | 1146 |
1123 #if defined(USE_AURA) | 1147 #if defined(USE_AURA) |
1124 // static | 1148 // static |
1125 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( | 1149 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( |
1126 Textfield* field) { | 1150 Textfield* field) { |
1127 return new NativeTextfieldViews(field); | 1151 return new NativeTextfieldViews(field); |
1128 } | 1152 } |
1129 #endif | 1153 #endif |
1130 | 1154 |
1131 } // namespace views | 1155 } // namespace views |
OLD | NEW |