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/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/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/resource/resource_bundle.h" |
17 #include "ui/base/ui_base_switches.h" | 18 #include "ui/base/ui_base_switches.h" |
18 #include "ui/base/ui_base_switches_util.h" | 19 #include "ui/base/ui_base_switches_util.h" |
19 #include "ui/gfx/insets.h" | 20 #include "ui/gfx/insets.h" |
20 #include "ui/gfx/selection_model.h" | 21 #include "ui/gfx/selection_model.h" |
21 #include "ui/native_theme/native_theme.h" | 22 #include "ui/native_theme/native_theme.h" |
22 #include "ui/views/controls/native/native_view_host.h" | 23 #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/native_textfield_views.h" |
24 #include "ui/views/controls/textfield/native_textfield_wrapper.h" | 25 #include "ui/views/controls/textfield/native_textfield_wrapper.h" |
25 #include "ui/views/controls/textfield/textfield_controller.h" | 26 #include "ui/views/controls/textfield/textfield_controller.h" |
26 #include "ui/views/views_delegate.h" | 27 #include "ui/views/views_delegate.h" |
27 #include "ui/views/widget/widget.h" | 28 #include "ui/views/widget/widget.h" |
28 | 29 |
29 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
30 #include "base/win/metro.h" | 31 #include "base/win/metro.h" |
31 #include "base/win/win_util.h" | 32 #include "base/win/win_util.h" |
32 // TODO(beng): this should be removed when the OS_WIN hack from | 33 // TODO(beng): this should be removed when the OS_WIN hack from |
33 // ViewHierarchyChanged is removed. | 34 // ViewHierarchyChanged is removed. |
34 #include "ui/views/controls/textfield/native_textfield_win.h" | 35 #include "ui/views/controls/textfield/native_textfield_win.h" |
35 #endif | 36 #endif |
36 | 37 |
37 namespace { | 38 namespace { |
38 | 39 |
39 // Default placeholder text color. | 40 // Default placeholder text color. |
40 const SkColor kDefaultPlaceholderTextColor = SK_ColorLTGRAY; | 41 const SkColor kDefaultPlaceholderTextColor = SK_ColorLTGRAY; |
41 | 42 |
| 43 gfx::FontList GetDefaultFontList() { |
| 44 return ResourceBundle::GetSharedInstance().GetFontList( |
| 45 ResourceBundle::BaseFont); |
| 46 } |
| 47 |
42 } // namespace | 48 } // namespace |
43 | 49 |
44 namespace views { | 50 namespace views { |
45 | 51 |
46 ///////////////////////////////////////////////////////////////////////////// | 52 ///////////////////////////////////////////////////////////////////////////// |
47 // Textfield | 53 // Textfield |
48 | 54 |
49 // static | 55 // static |
50 const char Textfield::kViewClassName[] = "Textfield"; | 56 const char Textfield::kViewClassName[] = "Textfield"; |
51 | 57 |
(...skipping 20 matching lines...) Expand all Loading... |
72 if (!loaded_msftedit_dll) | 78 if (!loaded_msftedit_dll) |
73 return true; | 79 return true; |
74 #endif | 80 #endif |
75 return true; | 81 return true; |
76 } | 82 } |
77 | 83 |
78 Textfield::Textfield() | 84 Textfield::Textfield() |
79 : native_wrapper_(NULL), | 85 : native_wrapper_(NULL), |
80 controller_(NULL), | 86 controller_(NULL), |
81 style_(STYLE_DEFAULT), | 87 style_(STYLE_DEFAULT), |
| 88 font_list_(GetDefaultFontList()), |
82 read_only_(false), | 89 read_only_(false), |
83 default_width_in_chars_(0), | 90 default_width_in_chars_(0), |
84 draw_border_(true), | 91 draw_border_(true), |
85 text_color_(SK_ColorBLACK), | 92 text_color_(SK_ColorBLACK), |
86 use_default_text_color_(true), | 93 use_default_text_color_(true), |
87 background_color_(SK_ColorWHITE), | 94 background_color_(SK_ColorWHITE), |
88 use_default_background_color_(true), | 95 use_default_background_color_(true), |
89 horizontal_margins_were_set_(false), | 96 horizontal_margins_were_set_(false), |
90 vertical_margins_were_set_(false), | 97 vertical_margins_were_set_(false), |
91 vertical_alignment_(gfx::ALIGN_VCENTER), | 98 vertical_alignment_(gfx::ALIGN_VCENTER), |
92 placeholder_text_color_(kDefaultPlaceholderTextColor), | 99 placeholder_text_color_(kDefaultPlaceholderTextColor), |
93 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT) { | 100 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT) { |
94 set_focusable(true); | 101 set_focusable(true); |
95 | 102 |
96 if (ViewsDelegate::views_delegate) { | 103 if (ViewsDelegate::views_delegate) { |
97 obscured_reveal_duration_ = ViewsDelegate::views_delegate-> | 104 obscured_reveal_duration_ = ViewsDelegate::views_delegate-> |
98 GetDefaultTextfieldObscuredRevealDuration(); | 105 GetDefaultTextfieldObscuredRevealDuration(); |
99 } | 106 } |
100 } | 107 } |
101 | 108 |
102 Textfield::Textfield(StyleFlags style) | 109 Textfield::Textfield(StyleFlags style) |
103 : native_wrapper_(NULL), | 110 : native_wrapper_(NULL), |
104 controller_(NULL), | 111 controller_(NULL), |
105 style_(style), | 112 style_(style), |
| 113 font_list_(GetDefaultFontList()), |
106 read_only_(false), | 114 read_only_(false), |
107 default_width_in_chars_(0), | 115 default_width_in_chars_(0), |
108 draw_border_(true), | 116 draw_border_(true), |
109 text_color_(SK_ColorBLACK), | 117 text_color_(SK_ColorBLACK), |
110 use_default_text_color_(true), | 118 use_default_text_color_(true), |
111 background_color_(SK_ColorWHITE), | 119 background_color_(SK_ColorWHITE), |
112 use_default_background_color_(true), | 120 use_default_background_color_(true), |
113 horizontal_margins_were_set_(false), | 121 horizontal_margins_were_set_(false), |
114 vertical_margins_were_set_(false), | 122 vertical_margins_were_set_(false), |
115 vertical_alignment_(gfx::ALIGN_VCENTER), | 123 vertical_alignment_(gfx::ALIGN_VCENTER), |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 | 271 |
264 bool Textfield::GetCursorEnabled() const { | 272 bool Textfield::GetCursorEnabled() const { |
265 return native_wrapper_ && native_wrapper_->GetCursorEnabled(); | 273 return native_wrapper_ && native_wrapper_->GetCursorEnabled(); |
266 } | 274 } |
267 | 275 |
268 void Textfield::SetCursorEnabled(bool enabled) { | 276 void Textfield::SetCursorEnabled(bool enabled) { |
269 if (native_wrapper_) | 277 if (native_wrapper_) |
270 native_wrapper_->SetCursorEnabled(enabled); | 278 native_wrapper_->SetCursorEnabled(enabled); |
271 } | 279 } |
272 | 280 |
273 void Textfield::SetFont(const gfx::Font& font) { | 281 void Textfield::SetFontList(const gfx::FontList& font_list) { |
274 font_ = font; | 282 font_list_ = font_list; |
275 if (native_wrapper_) | 283 if (native_wrapper_) |
276 native_wrapper_->UpdateFont(); | 284 native_wrapper_->UpdateFont(); |
277 PreferredSizeChanged(); | 285 PreferredSizeChanged(); |
278 } | 286 } |
279 | 287 |
| 288 const gfx::Font& Textfield::GetPrimaryFont() const { |
| 289 return font_list_.GetPrimaryFont(); |
| 290 } |
| 291 |
| 292 void Textfield::SetFont(const gfx::Font& font) { |
| 293 SetFontList(gfx::FontList(font)); |
| 294 } |
| 295 |
280 void Textfield::SetHorizontalMargins(int left, int right) { | 296 void Textfield::SetHorizontalMargins(int left, int right) { |
281 margins_.Set(margins_.top(), left, margins_.bottom(), right); | 297 margins_.Set(margins_.top(), left, margins_.bottom(), right); |
282 horizontal_margins_were_set_ = true; | 298 horizontal_margins_were_set_ = true; |
283 if (native_wrapper_) | 299 if (native_wrapper_) |
284 native_wrapper_->UpdateHorizontalMargins(); | 300 native_wrapper_->UpdateHorizontalMargins(); |
285 PreferredSizeChanged(); | 301 PreferredSizeChanged(); |
286 } | 302 } |
287 | 303 |
288 void Textfield::SetVerticalMargins(int top, int bottom) { | 304 void Textfield::SetVerticalMargins(int top, int bottom) { |
289 margins_.Set(top, margins_.left(), bottom, margins_.right()); | 305 margins_.Set(top, margins_.left(), bottom, margins_.right()); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 void Textfield::Layout() { | 433 void Textfield::Layout() { |
418 if (native_wrapper_) { | 434 if (native_wrapper_) { |
419 native_wrapper_->GetView()->SetBoundsRect(GetContentsBounds()); | 435 native_wrapper_->GetView()->SetBoundsRect(GetContentsBounds()); |
420 native_wrapper_->GetView()->Layout(); | 436 native_wrapper_->GetView()->Layout(); |
421 } | 437 } |
422 } | 438 } |
423 | 439 |
424 int Textfield::GetBaseline() const { | 440 int Textfield::GetBaseline() const { |
425 gfx::Insets insets = GetTextInsets(); | 441 gfx::Insets insets = GetTextInsets(); |
426 const int baseline = native_wrapper_ ? | 442 const int baseline = native_wrapper_ ? |
427 native_wrapper_->GetTextfieldBaseline() : font_.GetBaseline(); | 443 native_wrapper_->GetTextfieldBaseline() : font_list_.GetBaseline(); |
428 return insets.top() + baseline; | 444 return insets.top() + baseline; |
429 } | 445 } |
430 | 446 |
431 gfx::Size Textfield::GetPreferredSize() { | 447 gfx::Size Textfield::GetPreferredSize() { |
432 gfx::Insets insets = GetTextInsets(); | 448 gfx::Insets insets = GetTextInsets(); |
433 | 449 |
434 // For NativeTextfieldViews, we might use a pre-defined font list (defined in | |
435 // IDS_UI_FONT_FAMILY_CROS) as the fonts to render text. The fonts in the | |
436 // list might be different (in name or in size) from |font_|, so we need to | |
437 // use GetFontHeight() to get the height of the first font in the list to | |
438 // guide textfield's height. | |
439 const int font_height = native_wrapper_ ? native_wrapper_->GetFontHeight() : | 450 const int font_height = native_wrapper_ ? native_wrapper_->GetFontHeight() : |
440 font_.GetHeight(); | 451 font_list_.GetHeight(); |
441 return gfx::Size(font_.GetExpectedTextWidth(default_width_in_chars_) + | 452 return gfx::Size( |
442 insets.width(), font_height + insets.height()); | 453 GetPrimaryFont().GetExpectedTextWidth(default_width_in_chars_) |
| 454 + insets.width(), |
| 455 font_height + insets.height()); |
443 } | 456 } |
444 | 457 |
445 void Textfield::AboutToRequestFocusFromTabTraversal(bool reverse) { | 458 void Textfield::AboutToRequestFocusFromTabTraversal(bool reverse) { |
446 SelectAll(false); | 459 SelectAll(false); |
447 } | 460 } |
448 | 461 |
449 bool Textfield::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { | 462 bool Textfield::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { |
450 // Skip any accelerator handling of backspace; textfields handle this key. | 463 // Skip any accelerator handling of backspace; textfields handle this key. |
451 // Also skip processing of [Alt]+<num-pad digit> Unicode alt key codes. | 464 // Also skip processing of [Alt]+<num-pad digit> Unicode alt key codes. |
452 return e.key_code() == ui::VKEY_BACK || e.IsUnicodeKeyCode(); | 465 return e.key_code() == ui::VKEY_BACK || e.IsUnicodeKeyCode(); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( | 569 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( |
557 Textfield* field) { | 570 Textfield* field) { |
558 #if defined(OS_WIN) && !defined(USE_AURA) | 571 #if defined(OS_WIN) && !defined(USE_AURA) |
559 if (!Textfield::IsViewsTextfieldEnabled()) | 572 if (!Textfield::IsViewsTextfieldEnabled()) |
560 return new NativeTextfieldWin(field); | 573 return new NativeTextfieldWin(field); |
561 #endif | 574 #endif |
562 return new NativeTextfieldViews(field); | 575 return new NativeTextfieldViews(field); |
563 } | 576 } |
564 | 577 |
565 } // namespace views | 578 } // namespace views |
OLD | NEW |