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()) | |
yukawa
2015/06/16 02:45:03
Is there any chance to move this logic to TextInpu
Shu Chen
2015/06/16 05:15:27
I would prefer not to do that. Currently TextInput
yukawa
2015/06/16 05:34:10
It may make sense but how do you guarantee that Ge
Shu Chen
2015/06/16 06:53:59
GetInputMethod() returns NULL only when the View i
| |
296 GetInputMethod()->DetachTextInputClient(this); | |
297 } | |
295 | 298 |
296 void Textfield::SetReadOnly(bool read_only) { | 299 void Textfield::SetReadOnly(bool read_only) { |
297 // Update read-only without changing the focusable state (or active, etc.). | 300 // Update read-only without changing the focusable state (or active, etc.). |
298 read_only_ = read_only; | 301 read_only_ = read_only; |
299 if (GetInputMethod()) | 302 if (GetInputMethod()) |
300 GetInputMethod()->OnTextInputTypeChanged(this); | 303 GetInputMethod()->OnTextInputTypeChanged(this); |
301 SetColor(GetTextColor()); | 304 SetColor(GetTextColor()); |
302 UpdateBackgroundColor(); | 305 UpdateBackgroundColor(); |
303 } | 306 } |
304 | 307 |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
708 if (edit_command == kNoCommand) | 711 if (edit_command == kNoCommand) |
709 edit_command = GetCommandForKeyEvent(event, HasSelection()); | 712 edit_command = GetCommandForKeyEvent(event, HasSelection()); |
710 | 713 |
711 if (!handled && IsCommandIdEnabled(edit_command)) { | 714 if (!handled && IsCommandIdEnabled(edit_command)) { |
712 ExecuteCommand(edit_command); | 715 ExecuteCommand(edit_command); |
713 handled = true; | 716 handled = true; |
714 } | 717 } |
715 return handled; | 718 return handled; |
716 } | 719 } |
717 | 720 |
718 ui::TextInputClient* Textfield::GetTextInputClient() { | |
719 return this; | |
720 } | |
721 | |
722 void Textfield::OnGestureEvent(ui::GestureEvent* event) { | 721 void Textfield::OnGestureEvent(ui::GestureEvent* event) { |
723 switch (event->type()) { | 722 switch (event->type()) { |
724 case ui::ET_GESTURE_TAP_DOWN: | 723 case ui::ET_GESTURE_TAP_DOWN: |
725 RequestFocus(); | 724 RequestFocus(); |
726 ShowImeIfNeeded(); | 725 ShowImeIfNeeded(); |
727 event->SetHandled(); | 726 event->SetHandled(); |
728 break; | 727 break; |
729 case ui::ET_GESTURE_TAP: | 728 case ui::ET_GESTURE_TAP: |
730 if (event->details().tap_count() == 1) { | 729 if (event->details().tap_count() == 1) { |
731 // If tap is on the selection and touch handles are not present, handles | 730 // 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) { | 986 void Textfield::OnPaint(gfx::Canvas* canvas) { |
988 OnPaintBackground(canvas); | 987 OnPaintBackground(canvas); |
989 PaintTextAndCursor(canvas); | 988 PaintTextAndCursor(canvas); |
990 OnPaintBorder(canvas); | 989 OnPaintBorder(canvas); |
991 } | 990 } |
992 | 991 |
993 void Textfield::OnFocus() { | 992 void Textfield::OnFocus() { |
994 GetRenderText()->set_focused(true); | 993 GetRenderText()->set_focused(true); |
995 cursor_visible_ = true; | 994 cursor_visible_ = true; |
996 SchedulePaint(); | 995 SchedulePaint(); |
997 GetInputMethod()->OnFocus(); | 996 if (GetInputMethod()) |
997 GetInputMethod()->SetFocusedTextInputClient(this); | |
998 OnCaretBoundsChanged(); | 998 OnCaretBoundsChanged(); |
999 | 999 |
1000 const size_t caret_blink_ms = Textfield::GetCaretBlinkMs(); | 1000 const size_t caret_blink_ms = Textfield::GetCaretBlinkMs(); |
1001 if (caret_blink_ms != 0) { | 1001 if (caret_blink_ms != 0) { |
1002 cursor_repaint_timer_.Start(FROM_HERE, | 1002 cursor_repaint_timer_.Start(FROM_HERE, |
1003 base::TimeDelta::FromMilliseconds(caret_blink_ms), this, | 1003 base::TimeDelta::FromMilliseconds(caret_blink_ms), this, |
1004 &Textfield::UpdateCursor); | 1004 &Textfield::UpdateCursor); |
1005 } | 1005 } |
1006 | 1006 |
1007 View::OnFocus(); | 1007 View::OnFocus(); |
1008 SchedulePaint(); | 1008 SchedulePaint(); |
1009 } | 1009 } |
1010 | 1010 |
1011 void Textfield::OnBlur() { | 1011 void Textfield::OnBlur() { |
1012 GetRenderText()->set_focused(false); | 1012 GetRenderText()->set_focused(false); |
1013 GetInputMethod()->OnBlur(); | 1013 if (GetInputMethod()) |
1014 GetInputMethod()->DetachTextInputClient(this); | |
1014 cursor_repaint_timer_.Stop(); | 1015 cursor_repaint_timer_.Stop(); |
1015 if (cursor_visible_) { | 1016 if (cursor_visible_) { |
1016 cursor_visible_ = false; | 1017 cursor_visible_ = false; |
1017 RepaintCursor(); | 1018 RepaintCursor(); |
1018 } | 1019 } |
1019 | 1020 |
1020 DestroyTouchSelection(); | 1021 DestroyTouchSelection(); |
1021 | 1022 |
1022 // Border typically draws focus indicator. | 1023 // Border typically draws focus indicator. |
1023 SchedulePaint(); | 1024 SchedulePaint(); |
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1452 DoInsertChar(ch); | 1453 DoInsertChar(ch); |
1453 | 1454 |
1454 if (text_input_type_ == ui::TEXT_INPUT_TYPE_PASSWORD && | 1455 if (text_input_type_ == ui::TEXT_INPUT_TYPE_PASSWORD && |
1455 password_reveal_duration_ != base::TimeDelta()) { | 1456 password_reveal_duration_ != base::TimeDelta()) { |
1456 const size_t change_offset = model_->GetCursorPosition(); | 1457 const size_t change_offset = model_->GetCursorPosition(); |
1457 DCHECK_GT(change_offset, 0u); | 1458 DCHECK_GT(change_offset, 0u); |
1458 RevealPasswordChar(change_offset - 1); | 1459 RevealPasswordChar(change_offset - 1); |
1459 } | 1460 } |
1460 } | 1461 } |
1461 | 1462 |
1462 gfx::NativeWindow Textfield::GetAttachedWindow() const { | |
1463 // Imagine the following hierarchy. | |
1464 // [NativeWidget A] - FocusManager | |
1465 // [View] | |
1466 // [NativeWidget B] | |
1467 // [View] | |
1468 // [View X] | |
1469 // An important thing is that [NativeWidget A] owns Win32 input focus even | |
1470 // when [View X] is logically focused by FocusManager. As a result, an Win32 | |
1471 // IME may want to interact with the native view of [NativeWidget A] rather | |
1472 // than that of [NativeWidget B]. This is why we need to call | |
1473 // GetTopLevelWidget() here. | |
1474 return GetWidget()->GetTopLevelWidget()->GetNativeWindow(); | |
1475 } | |
1476 | |
1477 ui::TextInputType Textfield::GetTextInputType() const { | 1463 ui::TextInputType Textfield::GetTextInputType() const { |
1478 if (read_only() || !enabled()) | 1464 if (read_only() || !enabled()) |
1479 return ui::TEXT_INPUT_TYPE_NONE; | 1465 return ui::TEXT_INPUT_TYPE_NONE; |
1480 return text_input_type_; | 1466 return text_input_type_; |
1481 } | 1467 } |
1482 | 1468 |
1483 ui::TextInputMode Textfield::GetTextInputMode() const { | 1469 ui::TextInputMode Textfield::GetTextInputMode() const { |
1484 return ui::TEXT_INPUT_MODE_DEFAULT; | 1470 return ui::TEXT_INPUT_MODE_DEFAULT; |
1485 } | 1471 } |
1486 | 1472 |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1893 RequestFocus(); | 1879 RequestFocus(); |
1894 model_->MoveCursorTo(mouse); | 1880 model_->MoveCursorTo(mouse); |
1895 if (!selection_clipboard_text.empty()) { | 1881 if (!selection_clipboard_text.empty()) { |
1896 model_->InsertText(selection_clipboard_text); | 1882 model_->InsertText(selection_clipboard_text); |
1897 UpdateAfterChange(true, true); | 1883 UpdateAfterChange(true, true); |
1898 } | 1884 } |
1899 OnAfterUserAction(); | 1885 OnAfterUserAction(); |
1900 } | 1886 } |
1901 | 1887 |
1902 } // namespace views | 1888 } // namespace views |
OLD | NEW |