| 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 } // namespace | 53 } // namespace |
| 57 | 54 |
| 58 namespace views { | 55 namespace views { |
| 59 | 56 |
| 60 const char NativeTextfieldViews::kViewClassName[] = | 57 const char NativeTextfieldViews::kViewClassName[] = |
| 61 "views/NativeTextfieldViews"; | 58 "views/NativeTextfieldViews"; |
| 62 | 59 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 75 ALLOW_THIS_IN_INITIALIZER_LIST(touch_selection_controller_( | 72 ALLOW_THIS_IN_INITIALIZER_LIST(touch_selection_controller_( |
| 76 TouchSelectionController::create(this))) { | 73 TouchSelectionController::create(this))) { |
| 77 set_border(text_border_); | 74 set_border(text_border_); |
| 78 | 75 |
| 79 #if defined(OS_CHROMEOS) | 76 #if defined(OS_CHROMEOS) |
| 80 GetRenderText()->SetFontList(gfx::FontList(l10n_util::GetStringUTF8( | 77 GetRenderText()->SetFontList(gfx::FontList(l10n_util::GetStringUTF8( |
| 81 IDS_UI_FONT_FAMILY_CROS))); | 78 IDS_UI_FONT_FAMILY_CROS))); |
| 82 #else | 79 #else |
| 83 GetRenderText()->SetFont(textfield_->font()); | 80 GetRenderText()->SetFont(textfield_->font()); |
| 84 #endif | 81 #endif |
| 85 // Set the default text style. | |
| 86 gfx::StyleRange default_style; | |
| 87 default_style.foreground = textfield_->text_color(); | |
| 88 GetRenderText()->set_default_style(default_style); | |
| 89 GetRenderText()->ApplyDefaultStyle(); | |
| 90 | 82 |
| 83 UpdateColorsFromTheme(GetNativeTheme()); |
| 91 set_context_menu_controller(this); | 84 set_context_menu_controller(this); |
| 92 set_drag_controller(this); | 85 set_drag_controller(this); |
| 93 } | 86 } |
| 94 | 87 |
| 95 NativeTextfieldViews::~NativeTextfieldViews() { | 88 NativeTextfieldViews::~NativeTextfieldViews() { |
| 96 } | 89 } |
| 97 | 90 |
| 98 //////////////////////////////////////////////////////////////////////////////// | 91 //////////////////////////////////////////////////////////////////////////////// |
| 99 // NativeTextfieldViews, View overrides: | 92 // NativeTextfieldViews, View overrides: |
| 100 | 93 |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 | 278 |
| 286 void NativeTextfieldViews::OnFocus() { | 279 void NativeTextfieldViews::OnFocus() { |
| 287 NOTREACHED(); | 280 NOTREACHED(); |
| 288 } | 281 } |
| 289 | 282 |
| 290 void NativeTextfieldViews::OnBlur() { | 283 void NativeTextfieldViews::OnBlur() { |
| 291 NOTREACHED(); | 284 NOTREACHED(); |
| 292 } | 285 } |
| 293 | 286 |
| 294 void NativeTextfieldViews::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 287 void NativeTextfieldViews::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 295 gfx::RenderText* render_text = GetRenderText(); | 288 UpdateColorsFromTheme(theme); |
| 296 render_text->set_selection_color( | |
| 297 theme->GetSystemColor(ui::NativeTheme::kColorId_TextfieldSelectionColor)); | |
| 298 render_text->set_selection_background_focused_color( | |
| 299 theme->GetSystemColor( | |
| 300 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)); | |
| 301 render_text->set_selection_background_unfocused_color( | |
| 302 theme->GetSystemColor( | |
| 303 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundUnfocused)); | |
| 304 } | 289 } |
| 305 | 290 |
| 306 void NativeTextfieldViews::SelectRect(const gfx::Point& start, | 291 void NativeTextfieldViews::SelectRect(const gfx::Point& start, |
| 307 const gfx::Point& end) { | 292 const gfx::Point& end) { |
| 308 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) | 293 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) |
| 309 return; | 294 return; |
| 310 | 295 |
| 311 gfx::SelectionModel start_caret = GetRenderText()->FindCursorPosition(start); | 296 gfx::SelectionModel start_caret = GetRenderText()->FindCursorPosition(start); |
| 312 gfx::SelectionModel end_caret = GetRenderText()->FindCursorPosition(end); | 297 gfx::SelectionModel end_caret = GetRenderText()->FindCursorPosition(end); |
| 313 gfx::SelectionModel selection( | 298 gfx::SelectionModel selection( |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 gfx::Insets insets = GetInsets(); | 420 gfx::Insets insets = GetInsets(); |
| 436 textfield_->SetHorizontalMargins(insets.left(), insets.right()); | 421 textfield_->SetHorizontalMargins(insets.left(), insets.right()); |
| 437 textfield_->SetVerticalMargins(insets.top(), insets.bottom()); | 422 textfield_->SetVerticalMargins(insets.top(), insets.bottom()); |
| 438 } else { | 423 } else { |
| 439 textfield_->SetHorizontalMargins(0, 0); | 424 textfield_->SetHorizontalMargins(0, 0); |
| 440 textfield_->SetVerticalMargins(0, 0); | 425 textfield_->SetVerticalMargins(0, 0); |
| 441 } | 426 } |
| 442 } | 427 } |
| 443 | 428 |
| 444 void NativeTextfieldViews::UpdateTextColor() { | 429 void NativeTextfieldViews::UpdateTextColor() { |
| 430 gfx::StyleRange default_style(GetRenderText()->default_style()); |
| 431 default_style.foreground = textfield_->GetTextColor(); |
| 432 GetRenderText()->set_default_style(default_style); |
| 433 GetRenderText()->ApplyDefaultStyle(); |
| 445 SchedulePaint(); | 434 SchedulePaint(); |
| 446 } | 435 } |
| 447 | 436 |
| 448 void NativeTextfieldViews::UpdateBackgroundColor() { | 437 void NativeTextfieldViews::UpdateBackgroundColor() { |
| 449 // TODO(oshima): Background has to match the border's shape. | 438 const SkColor color = textfield_->GetBackgroundColor(); |
| 450 set_background( | 439 set_background(Background::CreateSolidBackground(color)); |
| 451 Background::CreateSolidBackground(textfield_->background_color())); | 440 GetRenderText()->set_background_is_transparent(SkColorGetA(color) != 0xFF); |
| 452 SchedulePaint(); | |
| 453 } | |
| 454 | |
| 455 void NativeTextfieldViews::UpdateCursorColor() { | |
| 456 SchedulePaint(); | 441 SchedulePaint(); |
| 457 } | 442 } |
| 458 | 443 |
| 459 void NativeTextfieldViews::UpdateReadOnly() { | 444 void NativeTextfieldViews::UpdateReadOnly() { |
| 460 // Update the default text style. | |
| 461 gfx::StyleRange default_style(GetRenderText()->default_style()); | |
| 462 default_style.foreground = textfield_->read_only() ? kReadonlyTextColor : | |
| 463 textfield_->text_color(); | |
| 464 GetRenderText()->set_default_style(default_style); | |
| 465 GetRenderText()->ApplyDefaultStyle(); | |
| 466 | |
| 467 SchedulePaint(); | |
| 468 OnTextInputTypeChanged(); | 445 OnTextInputTypeChanged(); |
| 469 } | 446 } |
| 470 | 447 |
| 471 void NativeTextfieldViews::UpdateFont() { | 448 void NativeTextfieldViews::UpdateFont() { |
| 472 #if defined(OS_CHROMEOS) | 449 #if defined(OS_CHROMEOS) |
| 473 // For ChromeOS, we support a pre-defined font list per locale. UpdateFont() | 450 // For ChromeOS, we support a pre-defined font list per locale. UpdateFont() |
| 474 // only changes the font size, not the font family names. | 451 // only changes the font size, not the font family names. |
| 475 GetRenderText()->SetFontSize(textfield_->font().GetFontSize()); | 452 GetRenderText()->SetFontSize(textfield_->font().GetFontSize()); |
| 476 #else | 453 #else |
| 477 GetRenderText()->SetFont(textfield_->font()); | 454 GetRenderText()->SetFont(textfield_->font()); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 const gfx::SelectionModel& sel) { | 529 const gfx::SelectionModel& sel) { |
| 553 model_->SelectSelectionModel(sel); | 530 model_->SelectSelectionModel(sel); |
| 554 OnCaretBoundsChanged(); | 531 OnCaretBoundsChanged(); |
| 555 SchedulePaint(); | 532 SchedulePaint(); |
| 556 } | 533 } |
| 557 | 534 |
| 558 size_t NativeTextfieldViews::GetCursorPosition() const { | 535 size_t NativeTextfieldViews::GetCursorPosition() const { |
| 559 return model_->GetCursorPosition(); | 536 return model_->GetCursorPosition(); |
| 560 } | 537 } |
| 561 | 538 |
| 539 bool NativeTextfieldViews::GetCursorEnabled() const { |
| 540 return GetRenderText()->cursor_enabled(); |
| 541 } |
| 542 |
| 543 void NativeTextfieldViews::SetCursorEnabled(bool enabled) { |
| 544 GetRenderText()->SetCursorEnabled(enabled); |
| 545 } |
| 546 |
| 562 bool NativeTextfieldViews::HandleKeyPressed(const ui::KeyEvent& e) { | 547 bool NativeTextfieldViews::HandleKeyPressed(const ui::KeyEvent& e) { |
| 563 TextfieldController* controller = textfield_->GetController(); | 548 TextfieldController* controller = textfield_->GetController(); |
| 564 bool handled = false; | 549 bool handled = false; |
| 565 if (controller) | 550 if (controller) |
| 566 handled = controller->HandleKeyEvent(textfield_, e); | 551 handled = controller->HandleKeyEvent(textfield_, e); |
| 567 return handled || HandleKeyEvent(e); | 552 return handled || HandleKeyEvent(e); |
| 568 } | 553 } |
| 569 | 554 |
| 570 bool NativeTextfieldViews::HandleKeyReleased(const ui::KeyEvent& e) { | 555 bool NativeTextfieldViews::HandleKeyReleased(const ui::KeyEvent& e) { |
| 571 return false; // crbug.com/127520 | 556 return false; // crbug.com/127520 |
| (...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 925 | 910 |
| 926 gfx::RenderText* NativeTextfieldViews::GetRenderText() const { | 911 gfx::RenderText* NativeTextfieldViews::GetRenderText() const { |
| 927 return model_->render_text(); | 912 return model_->render_text(); |
| 928 } | 913 } |
| 929 | 914 |
| 930 string16 NativeTextfieldViews::GetTextForDisplay(const string16& text) { | 915 string16 NativeTextfieldViews::GetTextForDisplay(const string16& text) { |
| 931 return textfield_->style() & Textfield::STYLE_LOWERCASE ? | 916 return textfield_->style() & Textfield::STYLE_LOWERCASE ? |
| 932 base::i18n::ToLower(text) : text; | 917 base::i18n::ToLower(text) : text; |
| 933 } | 918 } |
| 934 | 919 |
| 920 void NativeTextfieldViews::UpdateColorsFromTheme(const ui::NativeTheme* theme) { |
| 921 UpdateTextColor(); |
| 922 UpdateBackgroundColor(); |
| 923 gfx::RenderText* render_text = GetRenderText(); |
| 924 render_text->set_cursor_color(kDefaultCursorColor); |
| 925 render_text->set_selection_color(theme->GetSystemColor( |
| 926 ui::NativeTheme::kColorId_TextfieldSelectionColor)); |
| 927 render_text->set_selection_background_focused_color(theme->GetSystemColor( |
| 928 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)); |
| 929 render_text->set_selection_background_unfocused_color(theme->GetSystemColor( |
| 930 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundUnfocused)); |
| 931 } |
| 932 |
| 935 void NativeTextfieldViews::UpdateCursor() { | 933 void NativeTextfieldViews::UpdateCursor() { |
| 936 is_cursor_visible_ = !is_cursor_visible_; | 934 is_cursor_visible_ = !is_cursor_visible_; |
| 937 RepaintCursor(); | 935 RepaintCursor(); |
| 938 MessageLoop::current()->PostDelayedTask( | 936 MessageLoop::current()->PostDelayedTask( |
| 939 FROM_HERE, | 937 FROM_HERE, |
| 940 base::Bind(&NativeTextfieldViews::UpdateCursor, | 938 base::Bind(&NativeTextfieldViews::UpdateCursor, |
| 941 cursor_timer_.GetWeakPtr()), | 939 cursor_timer_.GetWeakPtr()), |
| 942 base::TimeDelta::FromMilliseconds(kCursorBlinkCycleMs / 2)); | 940 base::TimeDelta::FromMilliseconds(kCursorBlinkCycleMs / 2)); |
| 943 } | 941 } |
| 944 | 942 |
| 945 void NativeTextfieldViews::RepaintCursor() { | 943 void NativeTextfieldViews::RepaintCursor() { |
| 946 gfx::Rect r(GetCaretBounds()); | 944 gfx::Rect r(GetCaretBounds()); |
| 947 r.Inset(-1, -1, -1, -1); | 945 r.Inset(-1, -1, -1, -1); |
| 948 SchedulePaintInRect(r); | 946 SchedulePaintInRect(r); |
| 949 } | 947 } |
| 950 | 948 |
| 951 void NativeTextfieldViews::PaintTextAndCursor(gfx::Canvas* canvas) { | 949 void NativeTextfieldViews::PaintTextAndCursor(gfx::Canvas* canvas) { |
| 952 TRACE_EVENT0("views", "NativeTextfieldViews::PaintTextAndCursor"); | 950 TRACE_EVENT0("views", "NativeTextfieldViews::PaintTextAndCursor"); |
| 953 canvas->Save(); | 951 canvas->Save(); |
| 954 GetRenderText()->set_background_is_transparent( | |
| 955 !textfield_->use_default_background_color() && | |
| 956 SkColorGetA(textfield_->background_color()) != 0xFF); | |
| 957 GetRenderText()->set_cursor_visible(is_drop_cursor_visible_ || | 952 GetRenderText()->set_cursor_visible(is_drop_cursor_visible_ || |
| 958 (is_cursor_visible_ && !model_->HasSelection())); | 953 (is_cursor_visible_ && !model_->HasSelection())); |
| 959 GetRenderText()->set_cursor_color( | |
| 960 textfield_->use_default_cursor_color() ? | |
| 961 kDefaultCursorColor : | |
| 962 textfield_->cursor_color()); | |
| 963 // Draw the text, cursor, and selection. | 954 // Draw the text, cursor, and selection. |
| 964 GetRenderText()->Draw(canvas); | 955 GetRenderText()->Draw(canvas); |
| 965 | 956 |
| 966 // Draw placeholder text if needed. | 957 // Draw placeholder text if needed. |
| 967 if (model_->GetText().empty() && | 958 if (model_->GetText().empty() && |
| 968 !textfield_->placeholder_text().empty()) { | 959 !textfield_->placeholder_text().empty()) { |
| 969 canvas->DrawStringInt( | 960 canvas->DrawStringInt( |
| 970 textfield_->placeholder_text(), | 961 textfield_->placeholder_text(), |
| 971 GetRenderText()->GetFont(), | 962 GetRenderText()->GetFont(), |
| 972 textfield_->placeholder_text_color(), | 963 textfield_->placeholder_text_color(), |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1280 // Filter out all control characters, including tab and new line characters, | 1271 // Filter out all control characters, including tab and new line characters, |
| 1281 // and all characters with Alt modifier. But we need to allow characters with | 1272 // and all characters with Alt modifier. But we need to allow characters with |
| 1282 // AltGr modifier. | 1273 // AltGr modifier. |
| 1283 // On Windows AltGr is represented by Alt+Ctrl, and on Linux it's a different | 1274 // On Windows AltGr is represented by Alt+Ctrl, and on Linux it's a different |
| 1284 // flag that we don't care about. | 1275 // flag that we don't care about. |
| 1285 return ((ch >= 0x20 && ch < 0x7F) || ch > 0x9F) && | 1276 return ((ch >= 0x20 && ch < 0x7F) || ch > 0x9F) && |
| 1286 (flags & ~(ui::EF_SHIFT_DOWN | ui::EF_CAPS_LOCK_DOWN)) != ui::EF_ALT_DOWN; | 1277 (flags & ~(ui::EF_SHIFT_DOWN | ui::EF_CAPS_LOCK_DOWN)) != ui::EF_ALT_DOWN; |
| 1287 } | 1278 } |
| 1288 | 1279 |
| 1289 } // namespace views | 1280 } // namespace views |
| OLD | NEW |