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() && GetTextInputClient()) | |
James Su
2015/06/12 07:58:50
GetTextInputClient() returns 'this', so no need to
Shu Chen
2015/06/12 08:51:31
yukawa@'s comment pointed out the derived class of
| |
296 GetInputMethod()->DetachTextInputClient(GetTextInputClient()); | |
James Su
2015/06/12 07:58:50
use 'this' directly?
Shu Chen
2015/06/12 08:51:31
Done.
| |
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() && GetTextInputClient()) |
James Su
2015/06/12 07:58:50
ditto
Shu Chen
2015/06/12 08:51:31
Done.
| |
300 GetInputMethod()->OnTextInputTypeChanged(this); | 303 GetInputMethod()->OnTextInputTypeChanged(GetTextInputClient()); |
James Su
2015/06/12 07:58:50
ditto
Shu Chen
2015/06/12 08:51:31
Done.
| |
301 SetColor(GetTextColor()); | 304 SetColor(GetTextColor()); |
302 UpdateBackgroundColor(); | 305 UpdateBackgroundColor(); |
303 } | 306 } |
304 | 307 |
305 void Textfield::SetTextInputType(ui::TextInputType type) { | 308 void Textfield::SetTextInputType(ui::TextInputType type) { |
306 GetRenderText()->SetObscured(type == ui::TEXT_INPUT_TYPE_PASSWORD); | 309 GetRenderText()->SetObscured(type == ui::TEXT_INPUT_TYPE_PASSWORD); |
307 text_input_type_ = type; | 310 text_input_type_ = type; |
308 OnCaretBoundsChanged(); | 311 OnCaretBoundsChanged(); |
309 if (GetInputMethod()) | 312 if (GetInputMethod() && GetTextInputClient()) |
James Su
2015/06/12 07:58:50
ditto
Shu Chen
2015/06/12 08:51:31
Done.
| |
310 GetInputMethod()->OnTextInputTypeChanged(this); | 313 GetInputMethod()->OnTextInputTypeChanged(GetTextInputClient()); |
James Su
2015/06/12 07:58:50
ditto
Shu Chen
2015/06/12 08:51:31
Done.
| |
311 SchedulePaint(); | 314 SchedulePaint(); |
312 } | 315 } |
313 | 316 |
314 void Textfield::SetTextInputFlags(int flags) { | 317 void Textfield::SetTextInputFlags(int flags) { |
315 text_input_flags_ = flags; | 318 text_input_flags_ = flags; |
316 } | 319 } |
317 | 320 |
318 void Textfield::SetText(const base::string16& new_text) { | 321 void Textfield::SetText(const base::string16& new_text) { |
319 model_->SetText(new_text); | 322 model_->SetText(new_text); |
320 OnCaretBoundsChanged(); | 323 OnCaretBoundsChanged(); |
(...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
972 return true; | 975 return true; |
973 } | 976 } |
974 | 977 |
975 void Textfield::OnVisibleBoundsChanged() { | 978 void Textfield::OnVisibleBoundsChanged() { |
976 if (touch_selection_controller_) | 979 if (touch_selection_controller_) |
977 touch_selection_controller_->SelectionChanged(); | 980 touch_selection_controller_->SelectionChanged(); |
978 } | 981 } |
979 | 982 |
980 void Textfield::OnEnabledChanged() { | 983 void Textfield::OnEnabledChanged() { |
981 View::OnEnabledChanged(); | 984 View::OnEnabledChanged(); |
982 if (GetInputMethod()) | 985 if (GetInputMethod() && GetTextInputClient()) |
983 GetInputMethod()->OnTextInputTypeChanged(this); | 986 GetInputMethod()->OnTextInputTypeChanged(GetTextInputClient()); |
James Su
2015/06/12 07:58:50
ditto
Shu Chen
2015/06/12 08:51:31
Done.
| |
984 SchedulePaint(); | 987 SchedulePaint(); |
985 } | 988 } |
986 | 989 |
987 void Textfield::OnPaint(gfx::Canvas* canvas) { | 990 void Textfield::OnPaint(gfx::Canvas* canvas) { |
988 OnPaintBackground(canvas); | 991 OnPaintBackground(canvas); |
989 PaintTextAndCursor(canvas); | 992 PaintTextAndCursor(canvas); |
990 OnPaintBorder(canvas); | 993 OnPaintBorder(canvas); |
991 } | 994 } |
992 | 995 |
993 void Textfield::OnFocus() { | 996 void Textfield::OnFocus() { |
994 GetRenderText()->set_focused(true); | 997 GetRenderText()->set_focused(true); |
995 cursor_visible_ = true; | 998 cursor_visible_ = true; |
996 SchedulePaint(); | 999 SchedulePaint(); |
997 GetInputMethod()->OnFocus(); | 1000 if (GetInputMethod() && GetTextInputClient()) |
1001 GetInputMethod()->SetFocusedTextInputClient(GetTextInputClient()); | |
James Su
2015/06/12 07:58:50
ditto
Shu Chen
2015/06/12 08:51:31
Done.
| |
998 OnCaretBoundsChanged(); | 1002 OnCaretBoundsChanged(); |
999 | 1003 |
1000 const size_t caret_blink_ms = Textfield::GetCaretBlinkMs(); | 1004 const size_t caret_blink_ms = Textfield::GetCaretBlinkMs(); |
1001 if (caret_blink_ms != 0) { | 1005 if (caret_blink_ms != 0) { |
1002 cursor_repaint_timer_.Start(FROM_HERE, | 1006 cursor_repaint_timer_.Start(FROM_HERE, |
1003 base::TimeDelta::FromMilliseconds(caret_blink_ms), this, | 1007 base::TimeDelta::FromMilliseconds(caret_blink_ms), this, |
1004 &Textfield::UpdateCursor); | 1008 &Textfield::UpdateCursor); |
1005 } | 1009 } |
1006 | 1010 |
1007 View::OnFocus(); | 1011 View::OnFocus(); |
1008 SchedulePaint(); | 1012 SchedulePaint(); |
1009 } | 1013 } |
1010 | 1014 |
1011 void Textfield::OnBlur() { | 1015 void Textfield::OnBlur() { |
1012 GetRenderText()->set_focused(false); | 1016 GetRenderText()->set_focused(false); |
1013 GetInputMethod()->OnBlur(); | 1017 if (GetInputMethod()) |
1018 GetInputMethod()->DetachTextInputClient(this); | |
1014 cursor_repaint_timer_.Stop(); | 1019 cursor_repaint_timer_.Stop(); |
1015 if (cursor_visible_) { | 1020 if (cursor_visible_) { |
1016 cursor_visible_ = false; | 1021 cursor_visible_ = false; |
1017 RepaintCursor(); | 1022 RepaintCursor(); |
1018 } | 1023 } |
1019 | 1024 |
1020 DestroyTouchSelection(); | 1025 DestroyTouchSelection(); |
1021 | 1026 |
1022 // Border typically draws focus indicator. | 1027 // Border typically draws focus indicator. |
1023 SchedulePaint(); | 1028 SchedulePaint(); |
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1452 DoInsertChar(ch); | 1457 DoInsertChar(ch); |
1453 | 1458 |
1454 if (text_input_type_ == ui::TEXT_INPUT_TYPE_PASSWORD && | 1459 if (text_input_type_ == ui::TEXT_INPUT_TYPE_PASSWORD && |
1455 password_reveal_duration_ != base::TimeDelta()) { | 1460 password_reveal_duration_ != base::TimeDelta()) { |
1456 const size_t change_offset = model_->GetCursorPosition(); | 1461 const size_t change_offset = model_->GetCursorPosition(); |
1457 DCHECK_GT(change_offset, 0u); | 1462 DCHECK_GT(change_offset, 0u); |
1458 RevealPasswordChar(change_offset - 1); | 1463 RevealPasswordChar(change_offset - 1); |
1459 } | 1464 } |
1460 } | 1465 } |
1461 | 1466 |
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 { | 1467 ui::TextInputType Textfield::GetTextInputType() const { |
1478 if (read_only() || !enabled()) | 1468 if (read_only() || !enabled()) |
1479 return ui::TEXT_INPUT_TYPE_NONE; | 1469 return ui::TEXT_INPUT_TYPE_NONE; |
1480 return text_input_type_; | 1470 return text_input_type_; |
1481 } | 1471 } |
1482 | 1472 |
1483 ui::TextInputMode Textfield::GetTextInputMode() const { | 1473 ui::TextInputMode Textfield::GetTextInputMode() const { |
1484 return ui::TEXT_INPUT_MODE_DEFAULT; | 1474 return ui::TEXT_INPUT_MODE_DEFAULT; |
1485 } | 1475 } |
1486 | 1476 |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1750 const bool reversed = selection.is_reversed(); | 1740 const bool reversed = selection.is_reversed(); |
1751 selection.set_start(reversed ? max : min); | 1741 selection.set_start(reversed ? max : min); |
1752 selection.set_end(reversed ? min : max); | 1742 selection.set_end(reversed ? min : max); |
1753 model_->SelectRange(selection); | 1743 model_->SelectRange(selection); |
1754 } | 1744 } |
1755 UpdateAfterChange(false, true); | 1745 UpdateAfterChange(false, true); |
1756 OnAfterUserAction(); | 1746 OnAfterUserAction(); |
1757 } | 1747 } |
1758 | 1748 |
1759 void Textfield::OnCaretBoundsChanged() { | 1749 void Textfield::OnCaretBoundsChanged() { |
1760 if (GetInputMethod()) | 1750 if (GetInputMethod() && GetTextInputClient()) |
1761 GetInputMethod()->OnCaretBoundsChanged(this); | 1751 GetInputMethod()->OnCaretBoundsChanged(GetTextInputClient()); |
James Su
2015/06/12 07:58:50
ditto
Shu Chen
2015/06/12 08:51:31
Done.
| |
1762 if (touch_selection_controller_) | 1752 if (touch_selection_controller_) |
1763 touch_selection_controller_->SelectionChanged(); | 1753 touch_selection_controller_->SelectionChanged(); |
1764 } | 1754 } |
1765 | 1755 |
1766 void Textfield::OnBeforeUserAction() { | 1756 void Textfield::OnBeforeUserAction() { |
1767 DCHECK(!performing_user_action_); | 1757 DCHECK(!performing_user_action_); |
1768 performing_user_action_ = true; | 1758 performing_user_action_ = true; |
1769 if (controller_) | 1759 if (controller_) |
1770 controller_->OnBeforeUserAction(this); | 1760 controller_->OnBeforeUserAction(this); |
1771 } | 1761 } |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1893 RequestFocus(); | 1883 RequestFocus(); |
1894 model_->MoveCursorTo(mouse); | 1884 model_->MoveCursorTo(mouse); |
1895 if (!selection_clipboard_text.empty()) { | 1885 if (!selection_clipboard_text.empty()) { |
1896 model_->InsertText(selection_clipboard_text); | 1886 model_->InsertText(selection_clipboard_text); |
1897 UpdateAfterChange(true, true); | 1887 UpdateAfterChange(true, true); |
1898 } | 1888 } |
1899 OnAfterUserAction(); | 1889 OnAfterUserAction(); |
1900 } | 1890 } |
1901 | 1891 |
1902 } // namespace views | 1892 } // namespace views |
OLD | NEW |