| 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 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "ui/base/accessibility/accessible_view_state.h" | 12 #include "ui/base/accessibility/accessible_view_state.h" |
| 13 #include "ui/base/events/event.h" | 13 #include "ui/base/events/event.h" |
| 14 #include "ui/base/ime/text_input_type.h" | 14 #include "ui/base/ime/text_input_type.h" |
| 15 #include "ui/base/keycodes/keyboard_codes.h" | 15 #include "ui/base/keycodes/keyboard_codes.h" |
| 16 #include "ui/base/range/range.h" | 16 #include "ui/base/range/range.h" |
| 17 #include "ui/base/ui_base_switches.h" | 17 #include "ui/base/ui_base_switches.h" |
| 18 #include "ui/gfx/insets.h" | 18 #include "ui/gfx/insets.h" |
| 19 #include "ui/gfx/selection_model.h" | 19 #include "ui/gfx/selection_model.h" |
| 20 #include "ui/native_theme/native_theme.h" |
| 20 #include "ui/views/controls/native/native_view_host.h" | 21 #include "ui/views/controls/native/native_view_host.h" |
| 21 #include "ui/views/controls/textfield/native_textfield_views.h" | 22 #include "ui/views/controls/textfield/native_textfield_views.h" |
| 22 #include "ui/views/controls/textfield/native_textfield_wrapper.h" | 23 #include "ui/views/controls/textfield/native_textfield_wrapper.h" |
| 23 #include "ui/views/controls/textfield/textfield_controller.h" | 24 #include "ui/views/controls/textfield/textfield_controller.h" |
| 24 #include "ui/views/widget/widget.h" | 25 #include "ui/views/widget/widget.h" |
| 25 | 26 |
| 26 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
| 27 #include "base/win/win_util.h" | 28 #include "base/win/win_util.h" |
| 28 // TODO(beng): this should be removed when the OS_WIN hack from | 29 // TODO(beng): this should be removed when the OS_WIN hack from |
| 29 // ViewHierarchyChanged is removed. | 30 // ViewHierarchyChanged is removed. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 58 : native_wrapper_(NULL), | 59 : native_wrapper_(NULL), |
| 59 controller_(NULL), | 60 controller_(NULL), |
| 60 style_(STYLE_DEFAULT), | 61 style_(STYLE_DEFAULT), |
| 61 read_only_(false), | 62 read_only_(false), |
| 62 default_width_in_chars_(0), | 63 default_width_in_chars_(0), |
| 63 draw_border_(true), | 64 draw_border_(true), |
| 64 text_color_(SK_ColorBLACK), | 65 text_color_(SK_ColorBLACK), |
| 65 use_default_text_color_(true), | 66 use_default_text_color_(true), |
| 66 background_color_(SK_ColorWHITE), | 67 background_color_(SK_ColorWHITE), |
| 67 use_default_background_color_(true), | 68 use_default_background_color_(true), |
| 68 cursor_color_(SK_ColorBLACK), | |
| 69 use_default_cursor_color_(true), | |
| 70 initialized_(false), | 69 initialized_(false), |
| 71 horizontal_margins_were_set_(false), | 70 horizontal_margins_were_set_(false), |
| 72 vertical_margins_were_set_(false), | 71 vertical_margins_were_set_(false), |
| 73 placeholder_text_color_(kDefaultPlaceholderTextColor), | 72 placeholder_text_color_(kDefaultPlaceholderTextColor), |
| 74 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT) { | 73 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT) { |
| 75 set_focusable(true); | 74 set_focusable(true); |
| 76 } | 75 } |
| 77 | 76 |
| 78 Textfield::Textfield(StyleFlags style) | 77 Textfield::Textfield(StyleFlags style) |
| 79 : native_wrapper_(NULL), | 78 : native_wrapper_(NULL), |
| 80 controller_(NULL), | 79 controller_(NULL), |
| 81 style_(style), | 80 style_(style), |
| 82 read_only_(false), | 81 read_only_(false), |
| 83 default_width_in_chars_(0), | 82 default_width_in_chars_(0), |
| 84 draw_border_(true), | 83 draw_border_(true), |
| 85 text_color_(SK_ColorBLACK), | 84 text_color_(SK_ColorBLACK), |
| 86 use_default_text_color_(true), | 85 use_default_text_color_(true), |
| 87 background_color_(SK_ColorWHITE), | 86 background_color_(SK_ColorWHITE), |
| 88 use_default_background_color_(true), | 87 use_default_background_color_(true), |
| 89 cursor_color_(SK_ColorBLACK), | |
| 90 use_default_cursor_color_(true), | |
| 91 initialized_(false), | 88 initialized_(false), |
| 92 horizontal_margins_were_set_(false), | 89 horizontal_margins_were_set_(false), |
| 93 vertical_margins_were_set_(false), | 90 vertical_margins_were_set_(false), |
| 94 placeholder_text_color_(kDefaultPlaceholderTextColor), | 91 placeholder_text_color_(kDefaultPlaceholderTextColor), |
| 95 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT) { | 92 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT) { |
| 96 set_focusable(true); | 93 set_focusable(true); |
| 97 if (IsObscured()) | 94 if (IsObscured()) |
| 98 SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); | 95 SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); |
| 99 } | 96 } |
| 100 | 97 |
| 101 Textfield::~Textfield() { | 98 Textfield::~Textfield() { |
| 102 } | 99 } |
| 103 | 100 |
| 104 void Textfield::SetController(TextfieldController* controller) { | 101 void Textfield::SetController(TextfieldController* controller) { |
| 105 controller_ = controller; | 102 controller_ = controller; |
| 106 } | 103 } |
| 107 | 104 |
| 108 TextfieldController* Textfield::GetController() const { | 105 TextfieldController* Textfield::GetController() const { |
| 109 return controller_; | 106 return controller_; |
| 110 } | 107 } |
| 111 | 108 |
| 112 void Textfield::SetReadOnly(bool read_only) { | 109 void Textfield::SetReadOnly(bool read_only) { |
| 113 read_only_ = read_only; | 110 read_only_ = read_only; |
| 114 set_focusable(!read_only); | 111 set_focusable(!read_only); |
| 115 if (native_wrapper_) { | 112 if (native_wrapper_) { |
| 116 native_wrapper_->UpdateReadOnly(); | 113 native_wrapper_->UpdateReadOnly(); |
| 117 native_wrapper_->UpdateTextColor(); | 114 native_wrapper_->UpdateTextColor(); |
| 118 native_wrapper_->UpdateBackgroundColor(); | 115 native_wrapper_->UpdateBackgroundColor(); |
| 119 native_wrapper_->UpdateCursorColor(); | |
| 120 } | 116 } |
| 121 } | 117 } |
| 122 | 118 |
| 123 bool Textfield::IsObscured() const { | 119 bool Textfield::IsObscured() const { |
| 124 return style_ & STYLE_OBSCURED; | 120 return style_ & STYLE_OBSCURED; |
| 125 } | 121 } |
| 126 | 122 |
| 127 void Textfield::SetObscured(bool obscured) { | 123 void Textfield::SetObscured(bool obscured) { |
| 128 if (obscured) { | 124 if (obscured) { |
| 129 style_ = static_cast<StyleFlags>(style_ | STYLE_OBSCURED); | 125 style_ = static_cast<StyleFlags>(style_ | STYLE_OBSCURED); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 native_wrapper_->ClearSelection(); | 186 native_wrapper_->ClearSelection(); |
| 191 } | 187 } |
| 192 | 188 |
| 193 bool Textfield::HasSelection() const { | 189 bool Textfield::HasSelection() const { |
| 194 ui::Range range; | 190 ui::Range range; |
| 195 if (native_wrapper_) | 191 if (native_wrapper_) |
| 196 native_wrapper_->GetSelectedRange(&range); | 192 native_wrapper_->GetSelectedRange(&range); |
| 197 return !range.is_empty(); | 193 return !range.is_empty(); |
| 198 } | 194 } |
| 199 | 195 |
| 196 SkColor Textfield::GetTextColor() const { |
| 197 if (!use_default_text_color_) |
| 198 return text_color_; |
| 199 |
| 200 return GetNativeTheme()->GetSystemColor(read_only() ? |
| 201 ui::NativeTheme::kColorId_TextfieldReadOnlyColor : |
| 202 ui::NativeTheme::kColorId_TextfieldDefaultColor); |
| 203 } |
| 204 |
| 200 void Textfield::SetTextColor(SkColor color) { | 205 void Textfield::SetTextColor(SkColor color) { |
| 201 text_color_ = color; | 206 text_color_ = color; |
| 202 use_default_text_color_ = false; | 207 use_default_text_color_ = false; |
| 203 if (native_wrapper_) | 208 if (native_wrapper_) |
| 204 native_wrapper_->UpdateTextColor(); | 209 native_wrapper_->UpdateTextColor(); |
| 205 } | 210 } |
| 206 | 211 |
| 207 void Textfield::UseDefaultTextColor() { | 212 void Textfield::UseDefaultTextColor() { |
| 208 use_default_text_color_ = true; | 213 use_default_text_color_ = true; |
| 209 if (native_wrapper_) | 214 if (native_wrapper_) |
| 210 native_wrapper_->UpdateTextColor(); | 215 native_wrapper_->UpdateTextColor(); |
| 211 } | 216 } |
| 212 | 217 |
| 218 SkColor Textfield::GetBackgroundColor() const { |
| 219 if (!use_default_background_color_) |
| 220 return background_color_; |
| 221 |
| 222 return GetNativeTheme()->GetSystemColor(read_only() ? |
| 223 ui::NativeTheme::kColorId_TextfieldReadOnlyBackground : |
| 224 ui::NativeTheme::kColorId_TextfieldDefaultBackground); |
| 225 } |
| 226 |
| 213 void Textfield::SetBackgroundColor(SkColor color) { | 227 void Textfield::SetBackgroundColor(SkColor color) { |
| 214 background_color_ = color; | 228 background_color_ = color; |
| 215 use_default_background_color_ = false; | 229 use_default_background_color_ = false; |
| 216 if (native_wrapper_) | 230 if (native_wrapper_) |
| 217 native_wrapper_->UpdateBackgroundColor(); | 231 native_wrapper_->UpdateBackgroundColor(); |
| 218 } | 232 } |
| 219 | 233 |
| 220 void Textfield::UseDefaultBackgroundColor() { | 234 void Textfield::UseDefaultBackgroundColor() { |
| 221 use_default_background_color_ = true; | 235 use_default_background_color_ = true; |
| 222 if (native_wrapper_) | 236 if (native_wrapper_) |
| 223 native_wrapper_->UpdateBackgroundColor(); | 237 native_wrapper_->UpdateBackgroundColor(); |
| 224 } | 238 } |
| 225 | 239 |
| 226 void Textfield::SetCursorColor(SkColor color) { | 240 bool Textfield::GetCursorEnabled() const { |
| 227 cursor_color_ = color; | 241 return native_wrapper_ && native_wrapper_->GetCursorEnabled(); |
| 228 use_default_cursor_color_ = false; | |
| 229 if (native_wrapper_) | |
| 230 native_wrapper_->UpdateCursorColor(); | |
| 231 } | 242 } |
| 232 | 243 |
| 233 void Textfield::UseDefaultCursorColor() { | 244 void Textfield::SetCursorEnabled(bool enabled) { |
| 234 use_default_cursor_color_ = true; | |
| 235 if (native_wrapper_) | 245 if (native_wrapper_) |
| 236 native_wrapper_->UpdateCursorColor(); | 246 native_wrapper_->SetCursorEnabled(enabled); |
| 237 } | 247 } |
| 238 | 248 |
| 239 void Textfield::SetFont(const gfx::Font& font) { | 249 void Textfield::SetFont(const gfx::Font& font) { |
| 240 font_ = font; | 250 font_ = font; |
| 241 if (native_wrapper_) | 251 if (native_wrapper_) |
| 242 native_wrapper_->UpdateFont(); | 252 native_wrapper_->UpdateFont(); |
| 243 PreferredSizeChanged(); | 253 PreferredSizeChanged(); |
| 244 } | 254 } |
| 245 | 255 |
| 246 void Textfield::SetHorizontalMargins(int left, int right) { | 256 void Textfield::SetHorizontalMargins(int left, int right) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 *top = margins_.top(); | 292 *top = margins_.top(); |
| 283 *bottom = margins_.bottom(); | 293 *bottom = margins_.bottom(); |
| 284 return true; | 294 return true; |
| 285 } | 295 } |
| 286 | 296 |
| 287 void Textfield::UpdateAllProperties() { | 297 void Textfield::UpdateAllProperties() { |
| 288 if (native_wrapper_) { | 298 if (native_wrapper_) { |
| 289 native_wrapper_->UpdateText(); | 299 native_wrapper_->UpdateText(); |
| 290 native_wrapper_->UpdateTextColor(); | 300 native_wrapper_->UpdateTextColor(); |
| 291 native_wrapper_->UpdateBackgroundColor(); | 301 native_wrapper_->UpdateBackgroundColor(); |
| 292 native_wrapper_->UpdateCursorColor(); | |
| 293 native_wrapper_->UpdateReadOnly(); | 302 native_wrapper_->UpdateReadOnly(); |
| 294 native_wrapper_->UpdateFont(); | 303 native_wrapper_->UpdateFont(); |
| 295 native_wrapper_->UpdateEnabled(); | 304 native_wrapper_->UpdateEnabled(); |
| 296 native_wrapper_->UpdateBorder(); | 305 native_wrapper_->UpdateBorder(); |
| 297 native_wrapper_->UpdateIsObscured(); | 306 native_wrapper_->UpdateIsObscured(); |
| 298 native_wrapper_->UpdateHorizontalMargins(); | 307 native_wrapper_->UpdateHorizontalMargins(); |
| 299 native_wrapper_->UpdateVerticalMargins(); | 308 native_wrapper_->UpdateVerticalMargins(); |
| 300 } | 309 } |
| 301 } | 310 } |
| 302 | 311 |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( | 528 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( |
| 520 Textfield* field) { | 529 Textfield* field) { |
| 521 #if defined(OS_WIN) && !defined(USE_AURA) | 530 #if defined(OS_WIN) && !defined(USE_AURA) |
| 522 if (!Textfield::IsViewsTextfieldEnabled()) | 531 if (!Textfield::IsViewsTextfieldEnabled()) |
| 523 return new NativeTextfieldWin(field); | 532 return new NativeTextfieldWin(field); |
| 524 #endif | 533 #endif |
| 525 return new NativeTextfieldViews(field); | 534 return new NativeTextfieldViews(field); |
| 526 } | 535 } |
| 527 | 536 |
| 528 } // namespace views | 537 } // namespace views |
| OLD | NEW |