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" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 if (!loaded_msftedit_dll) | 73 if (!loaded_msftedit_dll) |
73 return true; | 74 return true; |
74 #endif | 75 #endif |
75 return true; | 76 return true; |
76 } | 77 } |
77 | 78 |
78 Textfield::Textfield() | 79 Textfield::Textfield() |
79 : native_wrapper_(NULL), | 80 : native_wrapper_(NULL), |
80 controller_(NULL), | 81 controller_(NULL), |
81 style_(STYLE_DEFAULT), | 82 style_(STYLE_DEFAULT), |
83 font_list_(GetDefaultFontList()), | |
82 read_only_(false), | 84 read_only_(false), |
83 default_width_in_chars_(0), | 85 default_width_in_chars_(0), |
84 draw_border_(true), | 86 draw_border_(true), |
85 text_color_(SK_ColorBLACK), | 87 text_color_(SK_ColorBLACK), |
86 use_default_text_color_(true), | 88 use_default_text_color_(true), |
87 background_color_(SK_ColorWHITE), | 89 background_color_(SK_ColorWHITE), |
88 use_default_background_color_(true), | 90 use_default_background_color_(true), |
89 horizontal_margins_were_set_(false), | 91 horizontal_margins_were_set_(false), |
90 vertical_margins_were_set_(false), | 92 vertical_margins_were_set_(false), |
91 vertical_alignment_(gfx::ALIGN_VCENTER), | 93 vertical_alignment_(gfx::ALIGN_VCENTER), |
92 placeholder_text_color_(kDefaultPlaceholderTextColor), | 94 placeholder_text_color_(kDefaultPlaceholderTextColor), |
93 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT) { | 95 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT) { |
94 set_focusable(true); | 96 set_focusable(true); |
95 | 97 |
96 if (ViewsDelegate::views_delegate) { | 98 if (ViewsDelegate::views_delegate) { |
97 obscured_reveal_duration_ = ViewsDelegate::views_delegate-> | 99 obscured_reveal_duration_ = ViewsDelegate::views_delegate-> |
98 GetDefaultTextfieldObscuredRevealDuration(); | 100 GetDefaultTextfieldObscuredRevealDuration(); |
99 } | 101 } |
100 } | 102 } |
101 | 103 |
102 Textfield::Textfield(StyleFlags style) | 104 Textfield::Textfield(StyleFlags style) |
103 : native_wrapper_(NULL), | 105 : native_wrapper_(NULL), |
104 controller_(NULL), | 106 controller_(NULL), |
105 style_(style), | 107 style_(style), |
108 font_list_(GetDefaultFontList()), | |
106 read_only_(false), | 109 read_only_(false), |
107 default_width_in_chars_(0), | 110 default_width_in_chars_(0), |
108 draw_border_(true), | 111 draw_border_(true), |
109 text_color_(SK_ColorBLACK), | 112 text_color_(SK_ColorBLACK), |
110 use_default_text_color_(true), | 113 use_default_text_color_(true), |
111 background_color_(SK_ColorWHITE), | 114 background_color_(SK_ColorWHITE), |
112 use_default_background_color_(true), | 115 use_default_background_color_(true), |
113 horizontal_margins_were_set_(false), | 116 horizontal_margins_were_set_(false), |
114 vertical_margins_were_set_(false), | 117 vertical_margins_were_set_(false), |
115 vertical_alignment_(gfx::ALIGN_VCENTER), | 118 vertical_alignment_(gfx::ALIGN_VCENTER), |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 | 266 |
264 bool Textfield::GetCursorEnabled() const { | 267 bool Textfield::GetCursorEnabled() const { |
265 return native_wrapper_ && native_wrapper_->GetCursorEnabled(); | 268 return native_wrapper_ && native_wrapper_->GetCursorEnabled(); |
266 } | 269 } |
267 | 270 |
268 void Textfield::SetCursorEnabled(bool enabled) { | 271 void Textfield::SetCursorEnabled(bool enabled) { |
269 if (native_wrapper_) | 272 if (native_wrapper_) |
270 native_wrapper_->SetCursorEnabled(enabled); | 273 native_wrapper_->SetCursorEnabled(enabled); |
271 } | 274 } |
272 | 275 |
273 void Textfield::SetFont(const gfx::Font& font) { | 276 void Textfield::SetFontList(const gfx::FontList& font_list) { |
274 font_ = font; | 277 font_list_ = font_list; |
275 if (native_wrapper_) | 278 if (native_wrapper_) |
276 native_wrapper_->UpdateFont(); | 279 native_wrapper_->UpdateFont(); |
277 PreferredSizeChanged(); | 280 PreferredSizeChanged(); |
278 } | 281 } |
279 | 282 |
283 const gfx::Font& Textfield::GetPrimaryFont() const { | |
284 return font_list_.GetPrimaryFont(); | |
285 } | |
286 | |
287 void Textfield::SetFont(const gfx::Font& font) { | |
288 SetFontList(gfx::FontList(font)); | |
289 } | |
290 | |
280 void Textfield::SetHorizontalMargins(int left, int right) { | 291 void Textfield::SetHorizontalMargins(int left, int right) { |
281 margins_.Set(margins_.top(), left, margins_.bottom(), right); | 292 margins_.Set(margins_.top(), left, margins_.bottom(), right); |
282 horizontal_margins_were_set_ = true; | 293 horizontal_margins_were_set_ = true; |
283 if (native_wrapper_) | 294 if (native_wrapper_) |
284 native_wrapper_->UpdateHorizontalMargins(); | 295 native_wrapper_->UpdateHorizontalMargins(); |
285 PreferredSizeChanged(); | 296 PreferredSizeChanged(); |
286 } | 297 } |
287 | 298 |
288 void Textfield::SetVerticalMargins(int top, int bottom) { | 299 void Textfield::SetVerticalMargins(int top, int bottom) { |
289 margins_.Set(top, margins_.left(), bottom, margins_.right()); | 300 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() { | 428 void Textfield::Layout() { |
418 if (native_wrapper_) { | 429 if (native_wrapper_) { |
419 native_wrapper_->GetView()->SetBoundsRect(GetContentsBounds()); | 430 native_wrapper_->GetView()->SetBoundsRect(GetContentsBounds()); |
420 native_wrapper_->GetView()->Layout(); | 431 native_wrapper_->GetView()->Layout(); |
421 } | 432 } |
422 } | 433 } |
423 | 434 |
424 int Textfield::GetBaseline() const { | 435 int Textfield::GetBaseline() const { |
425 gfx::Insets insets = GetTextInsets(); | 436 gfx::Insets insets = GetTextInsets(); |
426 const int baseline = native_wrapper_ ? | 437 const int baseline = native_wrapper_ ? |
427 native_wrapper_->GetTextfieldBaseline() : font_.GetBaseline(); | 438 native_wrapper_->GetTextfieldBaseline() : font_list_.GetBaseline(); |
428 return insets.top() + baseline; | 439 return insets.top() + baseline; |
429 } | 440 } |
430 | 441 |
431 gfx::Size Textfield::GetPreferredSize() { | 442 gfx::Size Textfield::GetPreferredSize() { |
432 gfx::Insets insets = GetTextInsets(); | 443 gfx::Insets insets = GetTextInsets(); |
433 | 444 |
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() : | 445 const int font_height = native_wrapper_ ? native_wrapper_->GetFontHeight() : |
440 font_.GetHeight(); | 446 font_list_.GetHeight(); |
441 return gfx::Size(font_.GetExpectedTextWidth(default_width_in_chars_) + | 447 return gfx::Size( |
442 insets.width(), font_height + insets.height()); | 448 GetPrimaryFont().GetExpectedTextWidth(default_width_in_chars_) |
449 + insets.width(), | |
450 font_height + insets.height()); | |
443 } | 451 } |
444 | 452 |
445 void Textfield::AboutToRequestFocusFromTabTraversal(bool reverse) { | 453 void Textfield::AboutToRequestFocusFromTabTraversal(bool reverse) { |
446 SelectAll(false); | 454 SelectAll(false); |
447 } | 455 } |
448 | 456 |
449 bool Textfield::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { | 457 bool Textfield::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { |
450 // Skip any accelerator handling of backspace; textfields handle this key. | 458 // Skip any accelerator handling of backspace; textfields handle this key. |
451 // Also skip processing of [Alt]+<num-pad digit> Unicode alt key codes. | 459 // Also skip processing of [Alt]+<num-pad digit> Unicode alt key codes. |
452 return e.key_code() == ui::VKEY_BACK || e.IsUnicodeKeyCode(); | 460 return e.key_code() == ui::VKEY_BACK || e.IsUnicodeKeyCode(); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
535 if (!IsViewsTextfieldEnabled()) | 543 if (!IsViewsTextfieldEnabled()) |
536 static_cast<NativeTextfieldWin*>(native_wrapper_)->AttachHack(); | 544 static_cast<NativeTextfieldWin*>(native_wrapper_)->AttachHack(); |
537 #endif | 545 #endif |
538 } | 546 } |
539 } | 547 } |
540 | 548 |
541 const char* Textfield::GetClassName() const { | 549 const char* Textfield::GetClassName() const { |
542 return kViewClassName; | 550 return kViewClassName; |
543 } | 551 } |
544 | 552 |
553 // static | |
554 gfx::FontList Textfield::GetDefaultFontList() { | |
msw
2013/07/30 21:32:27
This is only used by the Textfield ctors above. In
Yuki
2013/07/31 05:38:43
Done.
| |
555 return ResourceBundle::GetSharedInstance().GetFontList( | |
556 ResourceBundle::BaseFont); | |
557 } | |
558 | |
545 gfx::Insets Textfield::GetTextInsets() const { | 559 gfx::Insets Textfield::GetTextInsets() const { |
546 gfx::Insets insets = GetInsets(); | 560 gfx::Insets insets = GetInsets(); |
547 if (draw_border_ && native_wrapper_) | 561 if (draw_border_ && native_wrapper_) |
548 insets += native_wrapper_->CalculateInsets(); | 562 insets += native_wrapper_->CalculateInsets(); |
549 return insets; | 563 return insets; |
550 } | 564 } |
551 | 565 |
552 //////////////////////////////////////////////////////////////////////////////// | 566 //////////////////////////////////////////////////////////////////////////////// |
553 // NativeTextfieldWrapper, public: | 567 // NativeTextfieldWrapper, public: |
554 | 568 |
555 // static | 569 // static |
556 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( | 570 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( |
557 Textfield* field) { | 571 Textfield* field) { |
558 #if defined(OS_WIN) && !defined(USE_AURA) | 572 #if defined(OS_WIN) && !defined(USE_AURA) |
559 if (!Textfield::IsViewsTextfieldEnabled()) | 573 if (!Textfield::IsViewsTextfieldEnabled()) |
560 return new NativeTextfieldWin(field); | 574 return new NativeTextfieldWin(field); |
561 #endif | 575 #endif |
562 return new NativeTextfieldViews(field); | 576 return new NativeTextfieldViews(field); |
563 } | 577 } |
564 | 578 |
565 } // namespace views | 579 } // namespace views |
OLD | NEW |