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/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
10 #include "ui/accessibility/ax_view_state.h" | 10 #include "ui/accessibility/ax_view_state.h" |
11 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 11 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
12 #include "ui/base/cursor/cursor.h" | 12 #include "ui/base/cursor/cursor.h" |
13 #include "ui/base/dragdrop/drag_drop_types.h" | 13 #include "ui/base/dragdrop/drag_drop_types.h" |
14 #include "ui/base/dragdrop/drag_utils.h" | 14 #include "ui/base/dragdrop/drag_utils.h" |
| 15 #include "ui/base/ime/input_method.h" |
15 #include "ui/base/touch/selection_bound.h" | 16 #include "ui/base/touch/selection_bound.h" |
16 #include "ui/base/ui_base_switches_util.h" | 17 #include "ui/base/ui_base_switches_util.h" |
17 #include "ui/compositor/canvas_painter.h" | 18 #include "ui/compositor/canvas_painter.h" |
18 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | 19 #include "ui/compositor/scoped_animation_duration_scale_mode.h" |
19 #include "ui/events/event.h" | 20 #include "ui/events/event.h" |
20 #include "ui/events/keycodes/keyboard_codes.h" | 21 #include "ui/events/keycodes/keyboard_codes.h" |
21 #include "ui/gfx/canvas.h" | 22 #include "ui/gfx/canvas.h" |
22 #include "ui/gfx/display.h" | 23 #include "ui/gfx/display.h" |
23 #include "ui/gfx/geometry/insets.h" | 24 #include "ui/gfx/geometry/insets.h" |
24 #include "ui/gfx/screen.h" | 25 #include "ui/gfx/screen.h" |
25 #include "ui/native_theme/native_theme.h" | 26 #include "ui/native_theme/native_theme.h" |
26 #include "ui/strings/grit/ui_strings.h" | 27 #include "ui/strings/grit/ui_strings.h" |
27 #include "ui/views/background.h" | 28 #include "ui/views/background.h" |
28 #include "ui/views/controls/focusable_border.h" | 29 #include "ui/views/controls/focusable_border.h" |
29 #include "ui/views/controls/label.h" | 30 #include "ui/views/controls/label.h" |
30 #include "ui/views/controls/menu/menu_runner.h" | 31 #include "ui/views/controls/menu/menu_runner.h" |
31 #include "ui/views/controls/native/native_view_host.h" | 32 #include "ui/views/controls/native/native_view_host.h" |
32 #include "ui/views/controls/textfield/textfield_controller.h" | 33 #include "ui/views/controls/textfield/textfield_controller.h" |
33 #include "ui/views/drag_utils.h" | 34 #include "ui/views/drag_utils.h" |
34 #include "ui/views/ime/input_method.h" | |
35 #include "ui/views/metrics.h" | 35 #include "ui/views/metrics.h" |
36 #include "ui/views/native_cursor.h" | 36 #include "ui/views/native_cursor.h" |
37 #include "ui/views/painter.h" | 37 #include "ui/views/painter.h" |
38 #include "ui/views/views_delegate.h" | 38 #include "ui/views/views_delegate.h" |
39 #include "ui/views/widget/widget.h" | 39 #include "ui/views/widget/widget.h" |
40 | 40 |
41 #if defined(OS_WIN) | 41 #if defined(OS_WIN) |
42 #include "base/win/win_util.h" | 42 #include "base/win/win_util.h" |
43 #endif | 43 #endif |
44 | 44 |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 SetBorder(scoped_ptr<Border>(new FocusableBorder())); | 284 SetBorder(scoped_ptr<Border>(new FocusableBorder())); |
285 SetFocusable(true); | 285 SetFocusable(true); |
286 | 286 |
287 if (ViewsDelegate::GetInstance()) { | 287 if (ViewsDelegate::GetInstance()) { |
288 password_reveal_duration_ = | 288 password_reveal_duration_ = |
289 ViewsDelegate::GetInstance() | 289 ViewsDelegate::GetInstance() |
290 ->GetDefaultTextfieldObscuredRevealDuration(); | 290 ->GetDefaultTextfieldObscuredRevealDuration(); |
291 } | 291 } |
292 } | 292 } |
293 | 293 |
294 Textfield::~Textfield() {} | 294 Textfield::~Textfield() { |
| 295 if (GetInputMethod()) { |
| 296 // The textfield should have been blurred before destroy. |
| 297 DCHECK(this != GetInputMethod()->GetTextInputClient()); |
| 298 } |
| 299 } |
295 | 300 |
296 void Textfield::SetReadOnly(bool read_only) { | 301 void Textfield::SetReadOnly(bool read_only) { |
297 // Update read-only without changing the focusable state (or active, etc.). | 302 // Update read-only without changing the focusable state (or active, etc.). |
298 read_only_ = read_only; | 303 read_only_ = read_only; |
299 if (GetInputMethod()) | 304 if (GetInputMethod()) |
300 GetInputMethod()->OnTextInputTypeChanged(this); | 305 GetInputMethod()->OnTextInputTypeChanged(this); |
301 SetColor(GetTextColor()); | 306 SetColor(GetTextColor()); |
302 UpdateBackgroundColor(); | 307 UpdateBackgroundColor(); |
303 } | 308 } |
304 | 309 |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
708 if (edit_command == kNoCommand) | 713 if (edit_command == kNoCommand) |
709 edit_command = GetCommandForKeyEvent(event, HasSelection()); | 714 edit_command = GetCommandForKeyEvent(event, HasSelection()); |
710 | 715 |
711 if (!handled && IsCommandIdEnabled(edit_command)) { | 716 if (!handled && IsCommandIdEnabled(edit_command)) { |
712 ExecuteCommand(edit_command); | 717 ExecuteCommand(edit_command); |
713 handled = true; | 718 handled = true; |
714 } | 719 } |
715 return handled; | 720 return handled; |
716 } | 721 } |
717 | 722 |
718 ui::TextInputClient* Textfield::GetTextInputClient() { | |
719 return this; | |
720 } | |
721 | |
722 void Textfield::OnGestureEvent(ui::GestureEvent* event) { | 723 void Textfield::OnGestureEvent(ui::GestureEvent* event) { |
723 switch (event->type()) { | 724 switch (event->type()) { |
724 case ui::ET_GESTURE_TAP_DOWN: | 725 case ui::ET_GESTURE_TAP_DOWN: |
725 RequestFocus(); | 726 RequestFocus(); |
726 ShowImeIfNeeded(); | 727 ShowImeIfNeeded(); |
727 event->SetHandled(); | 728 event->SetHandled(); |
728 break; | 729 break; |
729 case ui::ET_GESTURE_TAP: | 730 case ui::ET_GESTURE_TAP: |
730 if (event->details().tap_count() == 1) { | 731 if (event->details().tap_count() == 1) { |
731 // If tap is on the selection and touch handles are not present, handles | 732 // If tap is on the selection and touch handles are not present, handles |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
987 void Textfield::OnPaint(gfx::Canvas* canvas) { | 988 void Textfield::OnPaint(gfx::Canvas* canvas) { |
988 OnPaintBackground(canvas); | 989 OnPaintBackground(canvas); |
989 PaintTextAndCursor(canvas); | 990 PaintTextAndCursor(canvas); |
990 OnPaintBorder(canvas); | 991 OnPaintBorder(canvas); |
991 } | 992 } |
992 | 993 |
993 void Textfield::OnFocus() { | 994 void Textfield::OnFocus() { |
994 GetRenderText()->set_focused(true); | 995 GetRenderText()->set_focused(true); |
995 cursor_visible_ = true; | 996 cursor_visible_ = true; |
996 SchedulePaint(); | 997 SchedulePaint(); |
997 GetInputMethod()->OnFocus(); | 998 if (GetInputMethod()) |
| 999 GetInputMethod()->SetFocusedTextInputClient(this); |
998 OnCaretBoundsChanged(); | 1000 OnCaretBoundsChanged(); |
999 | 1001 |
1000 const size_t caret_blink_ms = Textfield::GetCaretBlinkMs(); | 1002 const size_t caret_blink_ms = Textfield::GetCaretBlinkMs(); |
1001 if (caret_blink_ms != 0) { | 1003 if (caret_blink_ms != 0) { |
1002 cursor_repaint_timer_.Start(FROM_HERE, | 1004 cursor_repaint_timer_.Start(FROM_HERE, |
1003 base::TimeDelta::FromMilliseconds(caret_blink_ms), this, | 1005 base::TimeDelta::FromMilliseconds(caret_blink_ms), this, |
1004 &Textfield::UpdateCursor); | 1006 &Textfield::UpdateCursor); |
1005 } | 1007 } |
1006 | 1008 |
1007 View::OnFocus(); | 1009 View::OnFocus(); |
1008 SchedulePaint(); | 1010 SchedulePaint(); |
1009 } | 1011 } |
1010 | 1012 |
1011 void Textfield::OnBlur() { | 1013 void Textfield::OnBlur() { |
1012 GetRenderText()->set_focused(false); | 1014 GetRenderText()->set_focused(false); |
1013 GetInputMethod()->OnBlur(); | 1015 if (GetInputMethod()) |
| 1016 GetInputMethod()->DetachTextInputClient(this); |
1014 cursor_repaint_timer_.Stop(); | 1017 cursor_repaint_timer_.Stop(); |
1015 if (cursor_visible_) { | 1018 if (cursor_visible_) { |
1016 cursor_visible_ = false; | 1019 cursor_visible_ = false; |
1017 RepaintCursor(); | 1020 RepaintCursor(); |
1018 } | 1021 } |
1019 | 1022 |
1020 DestroyTouchSelection(); | 1023 DestroyTouchSelection(); |
1021 | 1024 |
1022 // Border typically draws focus indicator. | 1025 // Border typically draws focus indicator. |
1023 SchedulePaint(); | 1026 SchedulePaint(); |
(...skipping 869 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1893 RequestFocus(); | 1896 RequestFocus(); |
1894 model_->MoveCursorTo(mouse); | 1897 model_->MoveCursorTo(mouse); |
1895 if (!selection_clipboard_text.empty()) { | 1898 if (!selection_clipboard_text.empty()) { |
1896 model_->InsertText(selection_clipboard_text); | 1899 model_->InsertText(selection_clipboard_text); |
1897 UpdateAfterChange(true, true); | 1900 UpdateAfterChange(true, true); |
1898 } | 1901 } |
1899 OnAfterUserAction(); | 1902 OnAfterUserAction(); |
1900 } | 1903 } |
1901 | 1904 |
1902 } // namespace views | 1905 } // namespace views |
OLD | NEW |