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 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 #include "ui/views/metrics.h" | 40 #include "ui/views/metrics.h" |
41 #include "ui/views/widget/widget.h" | 41 #include "ui/views/widget/widget.h" |
42 #include "unicode/uchar.h" | 42 #include "unicode/uchar.h" |
43 | 43 |
44 #if defined(USE_AURA) | 44 #if defined(USE_AURA) |
45 #include "ui/base/cursor/cursor.h" | 45 #include "ui/base/cursor/cursor.h" |
46 #endif | 46 #endif |
47 | 47 |
48 namespace { | 48 namespace { |
49 | 49 |
50 // Text color for read only. | |
51 const SkColor kReadonlyTextColor = SK_ColorDKGRAY; | |
52 | |
53 // Default "system" color for text cursor. | 50 // Default "system" color for text cursor. |
54 const SkColor kDefaultCursorColor = SK_ColorBLACK; | 51 const SkColor kDefaultCursorColor = SK_ColorBLACK; |
55 | 52 |
56 // Parameters to control cursor blinking. | 53 // Parameters to control cursor blinking. |
57 const int kCursorVisibleTimeMs = 800; | 54 const int kCursorVisibleTimeMs = 800; |
58 const int kCursorInvisibleTimeMs = 500; | 55 const int kCursorInvisibleTimeMs = 500; |
59 | 56 |
60 } // namespace | 57 } // namespace |
61 | 58 |
62 namespace views { | 59 namespace views { |
(...skipping 14 matching lines...) Expand all Loading... |
77 ALLOW_THIS_IN_INITIALIZER_LIST(touch_selection_controller_( | 74 ALLOW_THIS_IN_INITIALIZER_LIST(touch_selection_controller_( |
78 TouchSelectionController::create(this))) { | 75 TouchSelectionController::create(this))) { |
79 set_border(text_border_); | 76 set_border(text_border_); |
80 | 77 |
81 #if defined(OS_CHROMEOS) | 78 #if defined(OS_CHROMEOS) |
82 GetRenderText()->SetFontList(gfx::FontList(l10n_util::GetStringUTF8( | 79 GetRenderText()->SetFontList(gfx::FontList(l10n_util::GetStringUTF8( |
83 IDS_UI_FONT_FAMILY_CROS))); | 80 IDS_UI_FONT_FAMILY_CROS))); |
84 #else | 81 #else |
85 GetRenderText()->SetFont(textfield_->font()); | 82 GetRenderText()->SetFont(textfield_->font()); |
86 #endif | 83 #endif |
87 // Set the default text style. | |
88 gfx::StyleRange default_style; | |
89 default_style.foreground = textfield_->text_color(); | |
90 GetRenderText()->set_default_style(default_style); | |
91 GetRenderText()->ApplyDefaultStyle(); | |
92 | 84 |
| 85 UpdateColorsFromTheme(GetNativeTheme()); |
93 set_context_menu_controller(this); | 86 set_context_menu_controller(this); |
94 set_drag_controller(this); | 87 set_drag_controller(this); |
95 } | 88 } |
96 | 89 |
97 NativeTextfieldViews::~NativeTextfieldViews() { | 90 NativeTextfieldViews::~NativeTextfieldViews() { |
98 } | 91 } |
99 | 92 |
100 //////////////////////////////////////////////////////////////////////////////// | 93 //////////////////////////////////////////////////////////////////////////////// |
101 // NativeTextfieldViews, View overrides: | 94 // NativeTextfieldViews, View overrides: |
102 | 95 |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 | 262 |
270 void NativeTextfieldViews::OnFocus() { | 263 void NativeTextfieldViews::OnFocus() { |
271 NOTREACHED(); | 264 NOTREACHED(); |
272 } | 265 } |
273 | 266 |
274 void NativeTextfieldViews::OnBlur() { | 267 void NativeTextfieldViews::OnBlur() { |
275 NOTREACHED(); | 268 NOTREACHED(); |
276 } | 269 } |
277 | 270 |
278 void NativeTextfieldViews::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 271 void NativeTextfieldViews::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
279 gfx::RenderText* render_text = GetRenderText(); | 272 UpdateColorsFromTheme(theme); |
280 render_text->set_selection_color( | |
281 theme->GetSystemColor(ui::NativeTheme::kColorId_TextfieldSelectionColor)); | |
282 render_text->set_selection_background_focused_color( | |
283 theme->GetSystemColor( | |
284 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)); | |
285 render_text->set_selection_background_unfocused_color( | |
286 theme->GetSystemColor( | |
287 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundUnfocused)); | |
288 } | 273 } |
289 | 274 |
290 void NativeTextfieldViews::SelectRect(const gfx::Point& start, | 275 void NativeTextfieldViews::SelectRect(const gfx::Point& start, |
291 const gfx::Point& end) { | 276 const gfx::Point& end) { |
292 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) | 277 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) |
293 return; | 278 return; |
294 | 279 |
295 gfx::SelectionModel start_caret = GetRenderText()->FindCursorPosition(start); | 280 gfx::SelectionModel start_caret = GetRenderText()->FindCursorPosition(start); |
296 gfx::SelectionModel end_caret = GetRenderText()->FindCursorPosition(end); | 281 gfx::SelectionModel end_caret = GetRenderText()->FindCursorPosition(end); |
297 gfx::SelectionModel selection( | 282 gfx::SelectionModel selection( |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 gfx::Insets insets = GetInsets(); | 404 gfx::Insets insets = GetInsets(); |
420 textfield_->SetHorizontalMargins(insets.left(), insets.right()); | 405 textfield_->SetHorizontalMargins(insets.left(), insets.right()); |
421 textfield_->SetVerticalMargins(insets.top(), insets.bottom()); | 406 textfield_->SetVerticalMargins(insets.top(), insets.bottom()); |
422 } else { | 407 } else { |
423 textfield_->SetHorizontalMargins(0, 0); | 408 textfield_->SetHorizontalMargins(0, 0); |
424 textfield_->SetVerticalMargins(0, 0); | 409 textfield_->SetVerticalMargins(0, 0); |
425 } | 410 } |
426 } | 411 } |
427 | 412 |
428 void NativeTextfieldViews::UpdateTextColor() { | 413 void NativeTextfieldViews::UpdateTextColor() { |
| 414 gfx::StyleRange default_style(GetRenderText()->default_style()); |
| 415 default_style.foreground = textfield_->GetTextColor(); |
| 416 GetRenderText()->set_default_style(default_style); |
| 417 GetRenderText()->ApplyDefaultStyle(); |
429 SchedulePaint(); | 418 SchedulePaint(); |
430 } | 419 } |
431 | 420 |
432 void NativeTextfieldViews::UpdateBackgroundColor() { | 421 void NativeTextfieldViews::UpdateBackgroundColor() { |
433 // TODO(oshima): Background has to match the border's shape. | 422 const SkColor color = textfield_->GetBackgroundColor(); |
434 set_background( | 423 set_background(Background::CreateSolidBackground(color)); |
435 Background::CreateSolidBackground(textfield_->background_color())); | 424 GetRenderText()->set_background_is_transparent(SkColorGetA(color) != 0xFF); |
436 SchedulePaint(); | |
437 } | |
438 | |
439 void NativeTextfieldViews::UpdateCursorColor() { | |
440 SchedulePaint(); | 425 SchedulePaint(); |
441 } | 426 } |
442 | 427 |
443 void NativeTextfieldViews::UpdateReadOnly() { | 428 void NativeTextfieldViews::UpdateReadOnly() { |
444 // Update the default text style. | |
445 gfx::StyleRange default_style(GetRenderText()->default_style()); | |
446 default_style.foreground = textfield_->read_only() ? kReadonlyTextColor : | |
447 textfield_->text_color(); | |
448 GetRenderText()->set_default_style(default_style); | |
449 GetRenderText()->ApplyDefaultStyle(); | |
450 | |
451 SchedulePaint(); | |
452 OnTextInputTypeChanged(); | 429 OnTextInputTypeChanged(); |
453 } | 430 } |
454 | 431 |
455 void NativeTextfieldViews::UpdateFont() { | 432 void NativeTextfieldViews::UpdateFont() { |
456 #if defined(OS_CHROMEOS) | 433 #if defined(OS_CHROMEOS) |
457 // For ChromeOS, we support a pre-defined font list per locale. UpdateFont() | 434 // For ChromeOS, we support a pre-defined font list per locale. UpdateFont() |
458 // only changes the font size, not the font family names. | 435 // only changes the font size, not the font family names. |
459 GetRenderText()->SetFontSize(textfield_->font().GetFontSize()); | 436 GetRenderText()->SetFontSize(textfield_->font().GetFontSize()); |
460 #else | 437 #else |
461 GetRenderText()->SetFont(textfield_->font()); | 438 GetRenderText()->SetFont(textfield_->font()); |
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
909 | 886 |
910 gfx::RenderText* NativeTextfieldViews::GetRenderText() const { | 887 gfx::RenderText* NativeTextfieldViews::GetRenderText() const { |
911 return model_->render_text(); | 888 return model_->render_text(); |
912 } | 889 } |
913 | 890 |
914 string16 NativeTextfieldViews::GetTextForDisplay(const string16& text) { | 891 string16 NativeTextfieldViews::GetTextForDisplay(const string16& text) { |
915 return textfield_->style() & Textfield::STYLE_LOWERCASE ? | 892 return textfield_->style() & Textfield::STYLE_LOWERCASE ? |
916 base::i18n::ToLower(text) : text; | 893 base::i18n::ToLower(text) : text; |
917 } | 894 } |
918 | 895 |
| 896 void NativeTextfieldViews::UpdateColorsFromTheme(const ui::NativeTheme* theme) { |
| 897 UpdateTextColor(); |
| 898 UpdateBackgroundColor(); |
| 899 gfx::RenderText* render_text = GetRenderText(); |
| 900 render_text->set_cursor_color(kDefaultCursorColor); |
| 901 render_text->set_selection_color(theme->GetSystemColor( |
| 902 ui::NativeTheme::kColorId_TextfieldSelectionColor)); |
| 903 render_text->set_selection_background_focused_color(theme->GetSystemColor( |
| 904 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)); |
| 905 render_text->set_selection_background_unfocused_color(theme->GetSystemColor( |
| 906 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundUnfocused)); |
| 907 } |
| 908 |
919 void NativeTextfieldViews::UpdateCursor() { | 909 void NativeTextfieldViews::UpdateCursor() { |
920 is_cursor_visible_ = !is_cursor_visible_; | 910 is_cursor_visible_ = !is_cursor_visible_; |
921 RepaintCursor(); | 911 RepaintCursor(); |
922 MessageLoop::current()->PostDelayedTask( | 912 MessageLoop::current()->PostDelayedTask( |
923 FROM_HERE, | 913 FROM_HERE, |
924 base::Bind(&NativeTextfieldViews::UpdateCursor, | 914 base::Bind(&NativeTextfieldViews::UpdateCursor, |
925 cursor_timer_.GetWeakPtr()), | 915 cursor_timer_.GetWeakPtr()), |
926 base::TimeDelta::FromMilliseconds( | 916 base::TimeDelta::FromMilliseconds( |
927 is_cursor_visible_ ? kCursorVisibleTimeMs : kCursorInvisibleTimeMs)); | 917 is_cursor_visible_ ? kCursorVisibleTimeMs : kCursorInvisibleTimeMs)); |
928 } | 918 } |
929 | 919 |
930 void NativeTextfieldViews::RepaintCursor() { | 920 void NativeTextfieldViews::RepaintCursor() { |
931 gfx::Rect r(GetCaretBounds()); | 921 gfx::Rect r(GetCaretBounds()); |
932 r.Inset(-1, -1, -1, -1); | 922 r.Inset(-1, -1, -1, -1); |
933 SchedulePaintInRect(r); | 923 SchedulePaintInRect(r); |
934 } | 924 } |
935 | 925 |
936 void NativeTextfieldViews::PaintTextAndCursor(gfx::Canvas* canvas) { | 926 void NativeTextfieldViews::PaintTextAndCursor(gfx::Canvas* canvas) { |
937 TRACE_EVENT0("views", "NativeTextfieldViews::PaintTextAndCursor"); | 927 TRACE_EVENT0("views", "NativeTextfieldViews::PaintTextAndCursor"); |
938 canvas->Save(); | 928 canvas->Save(); |
939 GetRenderText()->set_background_is_transparent( | |
940 !textfield_->use_default_background_color() && | |
941 SkColorGetA(textfield_->background_color()) != 0xFF); | |
942 GetRenderText()->set_cursor_visible(is_drop_cursor_visible_ || | 929 GetRenderText()->set_cursor_visible(is_drop_cursor_visible_ || |
943 (is_cursor_visible_ && !model_->HasSelection())); | 930 (is_cursor_visible_ && !model_->HasSelection())); |
944 GetRenderText()->set_cursor_color( | |
945 textfield_->use_default_cursor_color() ? | |
946 kDefaultCursorColor : | |
947 textfield_->cursor_color()); | |
948 // Draw the text, cursor, and selection. | 931 // Draw the text, cursor, and selection. |
949 GetRenderText()->Draw(canvas); | 932 GetRenderText()->Draw(canvas); |
950 | 933 |
951 // Draw placeholder text if needed. | 934 // Draw placeholder text if needed. |
952 if (model_->GetText().empty() && | 935 if (model_->GetText().empty() && |
953 !textfield_->placeholder_text().empty()) { | 936 !textfield_->placeholder_text().empty()) { |
954 canvas->DrawStringInt( | 937 canvas->DrawStringInt( |
955 textfield_->placeholder_text(), | 938 textfield_->placeholder_text(), |
956 GetRenderText()->GetFont(), | 939 GetRenderText()->GetFont(), |
957 textfield_->placeholder_text_color(), | 940 textfield_->placeholder_text_color(), |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1265 // Filter out all control characters, including tab and new line characters, | 1248 // Filter out all control characters, including tab and new line characters, |
1266 // and all characters with Alt modifier. But we need to allow characters with | 1249 // and all characters with Alt modifier. But we need to allow characters with |
1267 // AltGr modifier. | 1250 // AltGr modifier. |
1268 // On Windows AltGr is represented by Alt+Ctrl, and on Linux it's a different | 1251 // On Windows AltGr is represented by Alt+Ctrl, and on Linux it's a different |
1269 // flag that we don't care about. | 1252 // flag that we don't care about. |
1270 return ((ch >= 0x20 && ch < 0x7F) || ch > 0x9F) && | 1253 return ((ch >= 0x20 && ch < 0x7F) || ch > 0x9F) && |
1271 (flags & ~(ui::EF_SHIFT_DOWN | ui::EF_CAPS_LOCK_DOWN)) != ui::EF_ALT_DOWN; | 1254 (flags & ~(ui::EF_SHIFT_DOWN | ui::EF_CAPS_LOCK_DOWN)) != ui::EF_ALT_DOWN; |
1272 } | 1255 } |
1273 | 1256 |
1274 } // namespace views | 1257 } // namespace views |
OLD | NEW |