Chromium Code Reviews| 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/textfield.h" | 5 #include "ui/views/controls/textfield/textfield.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 read_only_(false), | 231 read_only_(false), |
| 232 default_width_in_chars_(0), | 232 default_width_in_chars_(0), |
| 233 use_default_text_color_(true), | 233 use_default_text_color_(true), |
| 234 use_default_background_color_(true), | 234 use_default_background_color_(true), |
| 235 use_default_selection_text_color_(true), | 235 use_default_selection_text_color_(true), |
| 236 use_default_selection_background_color_(true), | 236 use_default_selection_background_color_(true), |
| 237 text_color_(SK_ColorBLACK), | 237 text_color_(SK_ColorBLACK), |
| 238 background_color_(SK_ColorWHITE), | 238 background_color_(SK_ColorWHITE), |
| 239 selection_text_color_(SK_ColorWHITE), | 239 selection_text_color_(SK_ColorWHITE), |
| 240 selection_background_color_(SK_ColorBLUE), | 240 selection_background_color_(SK_ColorBLUE), |
| 241 focus_manager_(nullptr), | |
| 241 placeholder_text_color_(kDefaultPlaceholderTextColor), | 242 placeholder_text_color_(kDefaultPlaceholderTextColor), |
| 242 invalid_(false), | 243 invalid_(false), |
| 243 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT), | 244 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT), |
| 244 text_input_flags_(0), | 245 text_input_flags_(0), |
| 245 performing_user_action_(false), | 246 performing_user_action_(false), |
| 246 skip_input_method_cancel_composition_(false), | 247 skip_input_method_cancel_composition_(false), |
| 247 drop_cursor_visible_(false), | 248 drop_cursor_visible_(false), |
| 248 initiating_drag_(false), | 249 initiating_drag_(false), |
| 249 selection_controller_(this), | 250 selection_controller_(this), |
| 250 drag_start_display_offset_(0), | 251 drag_start_display_offset_(0), |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 268 AddAccelerator(ui::Accelerator(ui::VKEY_X, ui::EF_CONTROL_DOWN)); | 269 AddAccelerator(ui::Accelerator(ui::VKEY_X, ui::EF_CONTROL_DOWN)); |
| 269 AddAccelerator(ui::Accelerator(ui::VKEY_C, ui::EF_CONTROL_DOWN)); | 270 AddAccelerator(ui::Accelerator(ui::VKEY_C, ui::EF_CONTROL_DOWN)); |
| 270 AddAccelerator(ui::Accelerator(ui::VKEY_V, ui::EF_CONTROL_DOWN)); | 271 AddAccelerator(ui::Accelerator(ui::VKEY_V, ui::EF_CONTROL_DOWN)); |
| 271 } | 272 } |
| 272 | 273 |
| 273 Textfield::~Textfield() { | 274 Textfield::~Textfield() { |
| 274 if (GetInputMethod()) { | 275 if (GetInputMethod()) { |
| 275 // The textfield should have been blurred before destroy. | 276 // The textfield should have been blurred before destroy. |
| 276 DCHECK(this != GetInputMethod()->GetTextInputClient()); | 277 DCHECK(this != GetInputMethod()->GetTextInputClient()); |
| 277 } | 278 } |
| 279 DCHECK(!focus_manager_); | |
| 278 } | 280 } |
| 279 | 281 |
| 280 void Textfield::SetReadOnly(bool read_only) { | 282 void Textfield::SetReadOnly(bool read_only) { |
| 281 // Update read-only without changing the focusable state (or active, etc.). | 283 // Update read-only without changing the focusable state (or active, etc.). |
| 282 read_only_ = read_only; | 284 read_only_ = read_only; |
| 283 if (GetInputMethod()) | 285 if (GetInputMethod()) |
| 284 GetInputMethod()->OnTextInputTypeChanged(this); | 286 GetInputMethod()->OnTextInputTypeChanged(this); |
| 285 SetColor(GetTextColor()); | 287 SetColor(GetTextColor()); |
| 286 UpdateBackgroundColor(); | 288 UpdateBackgroundColor(); |
| 287 } | 289 } |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 415 SkColor Textfield::GetSelectionBackgroundColor() const { | 417 SkColor Textfield::GetSelectionBackgroundColor() const { |
| 416 return use_default_selection_background_color_ ? | 418 return use_default_selection_background_color_ ? |
| 417 GetNativeTheme()->GetSystemColor( | 419 GetNativeTheme()->GetSystemColor( |
| 418 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused) : | 420 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused) : |
| 419 selection_background_color_; | 421 selection_background_color_; |
| 420 } | 422 } |
| 421 | 423 |
| 422 void Textfield::SetSelectionBackgroundColor(SkColor color) { | 424 void Textfield::SetSelectionBackgroundColor(SkColor color) { |
| 423 selection_background_color_ = color; | 425 selection_background_color_ = color; |
| 424 use_default_selection_background_color_ = false; | 426 use_default_selection_background_color_ = false; |
| 425 GetRenderText()->set_selection_background_focused_color( | 427 GetRenderText()->set_selection_background_color( |
| 426 GetSelectionBackgroundColor()); | 428 GetSelectionBackgroundColor()); |
| 427 SchedulePaint(); | 429 SchedulePaint(); |
| 428 } | 430 } |
| 429 | 431 |
| 430 void Textfield::UseDefaultSelectionBackgroundColor() { | 432 void Textfield::UseDefaultSelectionBackgroundColor() { |
| 431 use_default_selection_background_color_ = true; | 433 use_default_selection_background_color_ = true; |
| 432 GetRenderText()->set_selection_background_focused_color( | 434 GetRenderText()->set_selection_background_color( |
| 433 GetSelectionBackgroundColor()); | 435 GetSelectionBackgroundColor()); |
| 434 SchedulePaint(); | 436 SchedulePaint(); |
| 435 } | 437 } |
| 436 | 438 |
| 439 SkColor Textfield::GetUnfocusedSelectionBackgroundColor() const { | |
| 440 return GetNativeTheme()->GetSystemColor( | |
| 441 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundUnfocused); | |
| 442 } | |
| 443 | |
| 437 bool Textfield::GetCursorEnabled() const { | 444 bool Textfield::GetCursorEnabled() const { |
| 438 return GetRenderText()->cursor_enabled(); | 445 return GetRenderText()->cursor_enabled(); |
| 439 } | 446 } |
| 440 | 447 |
| 441 void Textfield::SetCursorEnabled(bool enabled) { | 448 void Textfield::SetCursorEnabled(bool enabled) { |
| 442 GetRenderText()->SetCursorEnabled(enabled); | 449 GetRenderText()->SetCursorEnabled(enabled); |
| 443 } | 450 } |
| 444 | 451 |
| 445 const gfx::FontList& Textfield::GetFontList() const { | 452 const gfx::FontList& Textfield::GetFontList() const { |
| 446 return GetRenderText()->font_list(); | 453 return GetRenderText()->font_list(); |
| (...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 920 touch_selection_controller_->SelectionChanged(); | 927 touch_selection_controller_->SelectionChanged(); |
| 921 } | 928 } |
| 922 | 929 |
| 923 void Textfield::OnEnabledChanged() { | 930 void Textfield::OnEnabledChanged() { |
| 924 View::OnEnabledChanged(); | 931 View::OnEnabledChanged(); |
| 925 if (GetInputMethod()) | 932 if (GetInputMethod()) |
| 926 GetInputMethod()->OnTextInputTypeChanged(this); | 933 GetInputMethod()->OnTextInputTypeChanged(this); |
| 927 SchedulePaint(); | 934 SchedulePaint(); |
| 928 } | 935 } |
| 929 | 936 |
| 937 void Textfield::ViewHierarchyChanged( | |
| 938 const ViewHierarchyChangedDetails& details) { | |
| 939 // Textfields only care about focus changes if the entire Widget has lost | |
| 940 // focus, so don't bother listening if there is no Widget. | |
| 941 if (!GetWidget()) | |
| 942 return; | |
| 943 | |
| 944 if (details.parent->Contains(this) && details.move_view == nullptr) | |
| 945 ObserveWidgetFocusChanges(details.is_add); | |
| 946 } | |
| 947 | |
| 948 void Textfield::NativeViewHierarchyChanged() { | |
| 949 ObserveWidgetFocusChanges(true); | |
| 950 } | |
| 951 | |
| 930 void Textfield::OnPaint(gfx::Canvas* canvas) { | 952 void Textfield::OnPaint(gfx::Canvas* canvas) { |
| 931 OnPaintBackground(canvas); | 953 OnPaintBackground(canvas); |
| 932 PaintTextAndCursor(canvas); | 954 PaintTextAndCursor(canvas); |
| 933 OnPaintBorder(canvas); | 955 OnPaintBorder(canvas); |
| 934 } | 956 } |
| 935 | 957 |
| 936 void Textfield::OnFocus() { | 958 void Textfield::OnFocus() { |
| 937 GetRenderText()->set_focused(true); | 959 GetRenderText()->set_focused(true); |
| 938 if (ShouldShowCursor()) | 960 if (ShouldShowCursor()) |
| 939 GetRenderText()->set_cursor_visible(true); | 961 GetRenderText()->set_cursor_visible(true); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 973 gfx::Point Textfield::GetKeyboardContextMenuLocation() { | 995 gfx::Point Textfield::GetKeyboardContextMenuLocation() { |
| 974 return GetCaretBounds().bottom_right(); | 996 return GetCaretBounds().bottom_right(); |
| 975 } | 997 } |
| 976 | 998 |
| 977 void Textfield::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 999 void Textfield::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 978 gfx::RenderText* render_text = GetRenderText(); | 1000 gfx::RenderText* render_text = GetRenderText(); |
| 979 render_text->SetColor(GetTextColor()); | 1001 render_text->SetColor(GetTextColor()); |
| 980 UpdateBackgroundColor(); | 1002 UpdateBackgroundColor(); |
| 981 render_text->set_cursor_color(GetTextColor()); | 1003 render_text->set_cursor_color(GetTextColor()); |
| 982 render_text->set_selection_color(GetSelectionTextColor()); | 1004 render_text->set_selection_color(GetSelectionTextColor()); |
| 983 render_text->set_selection_background_focused_color( | 1005 if (HasFocus()) { |
| 984 GetSelectionBackgroundColor()); | 1006 render_text->set_selection_background_color(GetSelectionBackgroundColor()); |
| 1007 } else { | |
| 1008 render_text->set_selection_background_color( | |
| 1009 GetUnfocusedSelectionBackgroundColor()); | |
| 1010 } | |
| 985 } | 1011 } |
| 986 | 1012 |
| 987 //////////////////////////////////////////////////////////////////////////////// | 1013 //////////////////////////////////////////////////////////////////////////////// |
| 988 // Textfield, TextfieldModel::Delegate overrides: | 1014 // Textfield, TextfieldModel::Delegate overrides: |
| 989 | 1015 |
| 990 void Textfield::OnCompositionTextConfirmedOrCleared() { | 1016 void Textfield::OnCompositionTextConfirmedOrCleared() { |
| 991 if (!skip_input_method_cancel_composition_) | 1017 if (!skip_input_method_cancel_composition_) |
| 992 GetInputMethod()->CancelComposition(this); | 1018 GetInputMethod()->CancelComposition(this); |
| 993 } | 1019 } |
| 994 | 1020 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1051 return drag_operations; | 1077 return drag_operations; |
| 1052 } | 1078 } |
| 1053 | 1079 |
| 1054 bool Textfield::CanStartDragForView(View* sender, | 1080 bool Textfield::CanStartDragForView(View* sender, |
| 1055 const gfx::Point& press_pt, | 1081 const gfx::Point& press_pt, |
| 1056 const gfx::Point& p) { | 1082 const gfx::Point& p) { |
| 1057 return initiating_drag_ && GetRenderText()->IsPointInSelection(press_pt); | 1083 return initiating_drag_ && GetRenderText()->IsPointInSelection(press_pt); |
| 1058 } | 1084 } |
| 1059 | 1085 |
| 1060 //////////////////////////////////////////////////////////////////////////////// | 1086 //////////////////////////////////////////////////////////////////////////////// |
| 1087 // Textfield, FocusChangeListener overrides: | |
| 1088 | |
| 1089 void Textfield::OnWillChangeFocus(View* focus_before, View* focus_after) { | |
| 1090 if (focus_before != this && focus_after != this) | |
| 1091 return; | |
| 1092 | |
| 1093 SkColor selection_bg_color = focus_after | |
| 1094 ? GetSelectionBackgroundColor() | |
| 1095 : GetUnfocusedSelectionBackgroundColor(); | |
| 1096 | |
| 1097 // Selection is drawn if |this| has focus, or the Widget loses activation, but | |
| 1098 // not if another View in this Widget is gaining focus. | |
| 1099 GetRenderText()->set_draw_text_selection(focus_after == this || !focus_after); | |
| 1100 GetRenderText()->set_selection_background_color(selection_bg_color); | |
| 1101 } | |
| 1102 | |
| 1103 void Textfield::OnDidChangeFocus(View* focused_before, View* focused_now) {} | |
| 1104 | |
| 1105 //////////////////////////////////////////////////////////////////////////////// | |
| 1061 // Textfield, WordLookupClient overrides: | 1106 // Textfield, WordLookupClient overrides: |
| 1062 | 1107 |
| 1063 bool Textfield::GetDecoratedWordAtPoint(const gfx::Point& point, | 1108 bool Textfield::GetDecoratedWordAtPoint(const gfx::Point& point, |
| 1064 gfx::DecoratedText* decorated_word, | 1109 gfx::DecoratedText* decorated_word, |
| 1065 gfx::Point* baseline_point) { | 1110 gfx::Point* baseline_point) { |
| 1066 return GetRenderText()->GetDecoratedWordAtPoint(point, decorated_word, | 1111 return GetRenderText()->GetDecoratedWordAtPoint(point, decorated_word, |
| 1067 baseline_point); | 1112 baseline_point); |
| 1068 } | 1113 } |
| 1069 | 1114 |
| 1070 //////////////////////////////////////////////////////////////////////////////// | 1115 //////////////////////////////////////////////////////////////////////////////// |
| (...skipping 746 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1817 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 1862 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 1818 if (text_input_type_ != ui::TEXT_INPUT_TYPE_PASSWORD) { | 1863 if (text_input_type_ != ui::TEXT_INPUT_TYPE_PASSWORD) { |
| 1819 ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_SELECTION) | 1864 ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_SELECTION) |
| 1820 .WriteText(GetSelectedText()); | 1865 .WriteText(GetSelectedText()); |
| 1821 if (controller_) | 1866 if (controller_) |
| 1822 controller_->OnAfterCutOrCopy(ui::CLIPBOARD_TYPE_SELECTION); | 1867 controller_->OnAfterCutOrCopy(ui::CLIPBOARD_TYPE_SELECTION); |
| 1823 } | 1868 } |
| 1824 #endif | 1869 #endif |
| 1825 } | 1870 } |
| 1826 | 1871 |
| 1872 void Textfield::ObserveWidgetFocusChanges(bool add_as_listener) { | |
| 1873 if (focus_manager_) { | |
| 1874 focus_manager_->RemoveFocusChangeListener(this); | |
| 1875 focus_manager_ = nullptr; | |
| 1876 } | |
| 1877 | |
| 1878 if (add_as_listener) { | |
| 1879 focus_manager_ = GetFocusManager(); | |
| 1880 if (!focus_manager_) { | |
| 1881 // If the Widget this Textfield is attached to has started deleting | |
| 1882 // itself, GetWidget() and thus GetFocusManager() will both return null. | |
| 1883 // NativeViewHierarchyChanged() will be called after this, which should | |
| 1884 // make this Textfield start listening to the new FocusManager. | |
|
tapted
2016/11/07 06:14:50
This isn't quite right. I think we're after someth
Patti Lor
2016/11/08 01:47:31
Done.
| |
| 1885 return; | |
| 1886 } | |
| 1887 focus_manager_->AddFocusChangeListener(this); | |
| 1888 } | |
| 1889 } | |
| 1890 | |
| 1827 void Textfield::AccessibilitySetValue(const base::string16& new_value, | 1891 void Textfield::AccessibilitySetValue(const base::string16& new_value, |
| 1828 bool clear_first) { | 1892 bool clear_first) { |
| 1829 if (read_only()) | 1893 if (read_only()) |
| 1830 return; | 1894 return; |
| 1831 if (!clear_first) | 1895 if (!clear_first) |
| 1832 InsertOrReplaceText(new_value); | 1896 InsertOrReplaceText(new_value); |
| 1833 else | 1897 else |
| 1834 SetText(new_value); | 1898 SetText(new_value); |
| 1835 ClearSelection(); | 1899 ClearSelection(); |
| 1836 } | 1900 } |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2045 } | 2109 } |
| 2046 | 2110 |
| 2047 void Textfield::OnCursorBlinkTimerFired() { | 2111 void Textfield::OnCursorBlinkTimerFired() { |
| 2048 DCHECK(ShouldBlinkCursor()); | 2112 DCHECK(ShouldBlinkCursor()); |
| 2049 gfx::RenderText* render_text = GetRenderText(); | 2113 gfx::RenderText* render_text = GetRenderText(); |
| 2050 render_text->set_cursor_visible(!render_text->cursor_visible()); | 2114 render_text->set_cursor_visible(!render_text->cursor_visible()); |
| 2051 RepaintCursor(); | 2115 RepaintCursor(); |
| 2052 } | 2116 } |
| 2053 | 2117 |
| 2054 } // namespace views | 2118 } // namespace views |
| OLD | NEW |