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 text = GetTextForDisplay(text); | |
201 | 201 |
202 // We'll delete the current selection for a drag and drop within this view. | 202 // We'll delete the current selection for a drag and drop within this view. |
203 bool move = initiating_drag_ && !event.IsControlDown() && | 203 bool move = initiating_drag_ && !event.IsControlDown() && |
204 event.source_operations() & ui::DragDropTypes::DRAG_MOVE; | 204 event.source_operations() & ui::DragDropTypes::DRAG_MOVE; |
205 if (move) { | 205 if (move) { |
206 gfx::SelectionModel selected; | 206 gfx::SelectionModel selected; |
207 model_->GetSelectionModel(&selected); | 207 model_->GetSelectionModel(&selected); |
208 // Adjust the drop destination if it is on or after the current selection. | 208 // Adjust the drop destination if it is on or after the current selection. |
209 size_t drop_destination = drop_destination_model.caret_pos(); | 209 size_t drop_destination = drop_destination_model.caret_pos(); |
210 drop_destination -= | 210 drop_destination -= |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 } | 314 } |
315 | 315 |
316 ///////////////////////////////////////////////////////////////// | 316 ///////////////////////////////////////////////////////////////// |
317 // NativeTextfieldViews, NativeTextifieldWrapper overrides: | 317 // NativeTextfieldViews, NativeTextifieldWrapper overrides: |
318 | 318 |
319 string16 NativeTextfieldViews::GetText() const { | 319 string16 NativeTextfieldViews::GetText() const { |
320 return model_->GetText(); | 320 return model_->GetText(); |
321 } | 321 } |
322 | 322 |
323 void NativeTextfieldViews::UpdateText() { | 323 void NativeTextfieldViews::UpdateText() { |
324 model_->SetText(textfield_->text()); | 324 string16 text = textfield_->text(); |
325 model_->SetText(GetTextForDisplay(text)); | |
msw
2012/04/13 18:13:25
nit: model_->SetText(GetTextForDisplay(textfield_-
kochi
2012/04/16 04:54:18
Done.
| |
325 OnCaretBoundsChanged(); | 326 OnCaretBoundsChanged(); |
326 SchedulePaint(); | 327 SchedulePaint(); |
327 textfield_->GetWidget()->NotifyAccessibilityEvent( | 328 textfield_->GetWidget()->NotifyAccessibilityEvent( |
328 textfield_, ui::AccessibilityTypes::EVENT_TEXT_CHANGED, true); | 329 textfield_, ui::AccessibilityTypes::EVENT_TEXT_CHANGED, true); |
329 } | 330 } |
330 | 331 |
331 void NativeTextfieldViews::AppendText(const string16& text) { | 332 void NativeTextfieldViews::AppendText(const string16& text) { |
332 if (text.empty()) | 333 if (text.empty()) |
333 return; | 334 return; |
334 model_->Append(text); | 335 model_->Append(GetTextForDisplay(text)); |
335 OnCaretBoundsChanged(); | 336 OnCaretBoundsChanged(); |
336 SchedulePaint(); | 337 SchedulePaint(); |
337 } | 338 } |
338 | 339 |
339 string16 NativeTextfieldViews::GetSelectedText() const { | 340 string16 NativeTextfieldViews::GetSelectedText() const { |
340 return model_->GetSelectedText(); | 341 return model_->GetSelectedText(); |
341 } | 342 } |
342 | 343 |
343 void NativeTextfieldViews::SelectAll() { | 344 void NativeTextfieldViews::SelectAll() { |
344 OnBeforeUserAction(); | 345 OnBeforeUserAction(); |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
661 OnAfterUserAction(); | 662 OnAfterUserAction(); |
662 } | 663 } |
663 | 664 |
664 void NativeTextfieldViews::InsertText(const string16& text) { | 665 void NativeTextfieldViews::InsertText(const string16& text) { |
665 // TODO(suzhe): Filter invalid characters. | 666 // TODO(suzhe): Filter invalid characters. |
666 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || text.empty()) | 667 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || text.empty()) |
667 return; | 668 return; |
668 | 669 |
669 OnBeforeUserAction(); | 670 OnBeforeUserAction(); |
670 skip_input_method_cancel_composition_ = true; | 671 skip_input_method_cancel_composition_ = true; |
672 | |
671 if (GetRenderText()->insert_mode()) | 673 if (GetRenderText()->insert_mode()) |
672 model_->InsertText(text); | 674 model_->InsertText(GetTextForDisplay(text)); |
673 else | 675 else |
674 model_->ReplaceText(text); | 676 model_->ReplaceText(GetTextForDisplay(text)); |
675 skip_input_method_cancel_composition_ = false; | 677 skip_input_method_cancel_composition_ = false; |
676 UpdateAfterChange(true, true); | 678 UpdateAfterChange(true, true); |
677 OnAfterUserAction(); | 679 OnAfterUserAction(); |
678 } | 680 } |
679 | 681 |
680 void NativeTextfieldViews::InsertChar(char16 ch, int flags) { | 682 void NativeTextfieldViews::InsertChar(char16 ch, int flags) { |
681 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || | 683 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || |
682 !ShouldInsertChar(ch, flags)) { | 684 !ShouldInsertChar(ch, flags)) { |
683 return; | 685 return; |
684 } | 686 } |
685 | 687 |
686 OnBeforeUserAction(); | 688 OnBeforeUserAction(); |
687 skip_input_method_cancel_composition_ = true; | 689 skip_input_method_cancel_composition_ = true; |
688 if (GetRenderText()->insert_mode()) | 690 if (GetRenderText()->insert_mode()) |
689 model_->InsertChar(ch); | 691 model_->InsertChar(ch); |
690 else | 692 else |
691 model_->ReplaceChar(ch); | 693 model_->ReplaceChar(ch); |
692 skip_input_method_cancel_composition_ = false; | 694 skip_input_method_cancel_composition_ = false; |
695 | |
696 if (textfield_->style() & Textfield::STYLE_LOWERCASE) | |
697 model_->SetText(base::i18n::ToLower(GetText())); | |
msw
2012/04/13 18:13:25
Should this be an unconditional model_->SetText(Ge
kochi
2012/04/16 04:54:18
Done.
| |
698 | |
693 UpdateAfterChange(true, true); | 699 UpdateAfterChange(true, true); |
694 OnAfterUserAction(); | 700 OnAfterUserAction(); |
695 } | 701 } |
696 | 702 |
697 ui::TextInputType NativeTextfieldViews::GetTextInputType() const { | 703 ui::TextInputType NativeTextfieldViews::GetTextInputType() const { |
698 return textfield_->GetTextInputType(); | 704 return textfield_->GetTextInputType(); |
699 } | 705 } |
700 | 706 |
701 bool NativeTextfieldViews::CanComposeInline() const { | 707 bool NativeTextfieldViews::CanComposeInline() const { |
702 return true; | 708 return true; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
785 if (skip_input_method_cancel_composition_) | 791 if (skip_input_method_cancel_composition_) |
786 return; | 792 return; |
787 DCHECK(textfield_->GetInputMethod()); | 793 DCHECK(textfield_->GetInputMethod()); |
788 textfield_->GetInputMethod()->CancelComposition(textfield_); | 794 textfield_->GetInputMethod()->CancelComposition(textfield_); |
789 } | 795 } |
790 | 796 |
791 gfx::RenderText* NativeTextfieldViews::GetRenderText() const { | 797 gfx::RenderText* NativeTextfieldViews::GetRenderText() const { |
792 return model_->render_text(); | 798 return model_->render_text(); |
793 } | 799 } |
794 | 800 |
801 string16 NativeTextfieldViews::GetTextForDisplay(const string16& text) { | |
802 return textfield_->style() & Textfield::STYLE_LOWERCASE ? | |
803 base::i18n::ToLower(text) : text; | |
804 } | |
805 | |
795 void NativeTextfieldViews::UpdateCursor() { | 806 void NativeTextfieldViews::UpdateCursor() { |
796 is_cursor_visible_ = !is_cursor_visible_; | 807 is_cursor_visible_ = !is_cursor_visible_; |
797 RepaintCursor(); | 808 RepaintCursor(); |
798 MessageLoop::current()->PostDelayedTask( | 809 MessageLoop::current()->PostDelayedTask( |
799 FROM_HERE, | 810 FROM_HERE, |
800 base::Bind(&NativeTextfieldViews::UpdateCursor, | 811 base::Bind(&NativeTextfieldViews::UpdateCursor, |
801 cursor_timer_.GetWeakPtr()), | 812 cursor_timer_.GetWeakPtr()), |
802 is_cursor_visible_ ? kCursorVisibleTimeMs : kCursorInvisibleTimeMs); | 813 is_cursor_visible_ ? kCursorVisibleTimeMs : kCursorInvisibleTimeMs); |
803 } | 814 } |
804 | 815 |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1039 bool NativeTextfieldViews::Copy() { | 1050 bool NativeTextfieldViews::Copy() { |
1040 if (model_->Copy()) { | 1051 if (model_->Copy()) { |
1041 TextfieldController* controller = textfield_->GetController(); | 1052 TextfieldController* controller = textfield_->GetController(); |
1042 if (controller) | 1053 if (controller) |
1043 controller->OnAfterCutOrCopy(); | 1054 controller->OnAfterCutOrCopy(); |
1044 return true; | 1055 return true; |
1045 } | 1056 } |
1046 return false; | 1057 return false; |
1047 } | 1058 } |
1048 | 1059 |
1049 bool NativeTextfieldViews::Paste() { | 1060 bool NativeTextfieldViews::Paste() { |
msw
2012/04/13 18:13:25
Gah, I think textfield_->text() versus GetText() (
kochi
2012/04/16 04:54:18
I mostly applied your comment.
I felt that calling
| |
1050 const bool success = model_->Paste(); | 1061 const bool success = model_->Paste(); |
1051 | 1062 |
1052 // Calls TextfieldController::ContentsChanged() explicitly if the paste action | 1063 if (success) { |
1053 // did not change the content at all. See http://crbug.com/79002 | 1064 string16 textfield_text = textfield_->text(); |
1054 if (success && GetText() == textfield_->text()) { | 1065 // As Paste is handled in model_->Paste(), the RenderText may contain |
1055 TextfieldController* controller = textfield_->GetController(); | 1066 // upper case characters. This is not consistent with other places |
1056 if (controller) | 1067 // which keeps RenderText only containing lower case characters. |
1057 controller->ContentsChanged(textfield_, textfield_->text()); | 1068 if (textfield_->style() & Textfield::STYLE_LOWERCASE) { |
1069 model_->SetText(base::i18n::ToLower(GetText())); | |
1070 textfield_text = base::i18n::ToLower(textfield_text); | |
1071 } | |
1072 // Calls TextfieldController::ContentsChanged() explicitly if the paste | |
1073 // action did not change the content at all. See http://crbug.com/79002 | |
1074 if (GetText() == textfield_text) { | |
1075 TextfieldController* controller = textfield_->GetController(); | |
1076 if (controller) | |
1077 controller->ContentsChanged(textfield_, textfield_->text()); | |
1078 } | |
1058 } | 1079 } |
1059 return success; | 1080 return success; |
1060 } | 1081 } |
1061 | 1082 |
1062 void NativeTextfieldViews::TrackMouseClicks(const MouseEvent& event) { | 1083 void NativeTextfieldViews::TrackMouseClicks(const MouseEvent& event) { |
1063 if (event.IsOnlyLeftMouseButton()) { | 1084 if (event.IsOnlyLeftMouseButton()) { |
1064 base::TimeDelta time_delta = event.time_stamp() - last_click_time_; | 1085 base::TimeDelta time_delta = event.time_stamp() - last_click_time_; |
1065 if (time_delta.InMilliseconds() <= GetDoubleClickInterval() && | 1086 if (time_delta.InMilliseconds() <= GetDoubleClickInterval() && |
1066 !ExceededDragThresholdFromLastClickLocation(event)) { | 1087 !ExceededDragThresholdFromLastClickLocation(event)) { |
1067 aggregated_clicks_ = (aggregated_clicks_ + 1) % 3; | 1088 aggregated_clicks_ = (aggregated_clicks_ + 1) % 3; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1122 | 1143 |
1123 #if defined(USE_AURA) | 1144 #if defined(USE_AURA) |
1124 // static | 1145 // static |
1125 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( | 1146 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( |
1126 Textfield* field) { | 1147 Textfield* field) { |
1127 return new NativeTextfieldViews(field); | 1148 return new NativeTextfieldViews(field); |
1128 } | 1149 } |
1129 #endif | 1150 #endif |
1130 | 1151 |
1131 } // namespace views | 1152 } // namespace views |
OLD | NEW |