| 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 #include "ui/gfx/selection_bound.h" | 35 #include "ui/gfx/selection_bound.h" |
| 36 #include "ui/native_theme/native_theme.h" | 36 #include "ui/native_theme/native_theme.h" |
| 37 #include "ui/strings/grit/ui_strings.h" | 37 #include "ui/strings/grit/ui_strings.h" |
| 38 #include "ui/views/background.h" | 38 #include "ui/views/background.h" |
| 39 #include "ui/views/controls/focus_ring.h" | 39 #include "ui/views/controls/focus_ring.h" |
| 40 #include "ui/views/controls/focusable_border.h" | 40 #include "ui/views/controls/focusable_border.h" |
| 41 #include "ui/views/controls/label.h" | 41 #include "ui/views/controls/label.h" |
| 42 #include "ui/views/controls/menu/menu_runner.h" | 42 #include "ui/views/controls/menu/menu_runner.h" |
| 43 #include "ui/views/controls/native/native_view_host.h" | 43 #include "ui/views/controls/native/native_view_host.h" |
| 44 #include "ui/views/controls/textfield/textfield_controller.h" | 44 #include "ui/views/controls/textfield/textfield_controller.h" |
| 45 #include "ui/views/controls/views_text_services_context_menu.h" | |
| 46 #include "ui/views/drag_utils.h" | 45 #include "ui/views/drag_utils.h" |
| 47 #include "ui/views/native_cursor.h" | 46 #include "ui/views/native_cursor.h" |
| 48 #include "ui/views/painter.h" | 47 #include "ui/views/painter.h" |
| 49 #include "ui/views/style/platform_style.h" | 48 #include "ui/views/style/platform_style.h" |
| 50 #include "ui/views/views_delegate.h" | 49 #include "ui/views/views_delegate.h" |
| 51 #include "ui/views/widget/widget.h" | 50 #include "ui/views/widget/widget.h" |
| 52 | 51 |
| 53 #if defined(OS_WIN) | 52 #if defined(OS_WIN) |
| 54 #include "base/win/win_util.h" | 53 #include "base/win/win_util.h" |
| 55 #include "ui/base/win/osk_display_manager.h" | 54 #include "ui/base/win/osk_display_manager.h" |
| (...skipping 1026 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1082 | 1081 |
| 1083 bool Textfield::CanStartDragForView(View* sender, | 1082 bool Textfield::CanStartDragForView(View* sender, |
| 1084 const gfx::Point& press_pt, | 1083 const gfx::Point& press_pt, |
| 1085 const gfx::Point& p) { | 1084 const gfx::Point& p) { |
| 1086 return initiating_drag_ && GetRenderText()->IsPointInSelection(press_pt); | 1085 return initiating_drag_ && GetRenderText()->IsPointInSelection(press_pt); |
| 1087 } | 1086 } |
| 1088 | 1087 |
| 1089 //////////////////////////////////////////////////////////////////////////////// | 1088 //////////////////////////////////////////////////////////////////////////////// |
| 1090 // Textfield, WordLookupClient overrides: | 1089 // Textfield, WordLookupClient overrides: |
| 1091 | 1090 |
| 1092 bool Textfield::GetDecoratedWordAndBaselineAtPoint( | 1091 bool Textfield::GetDecoratedWordAtPoint(const gfx::Point& point, |
| 1093 const gfx::Point& point, | 1092 gfx::DecoratedText* decorated_word, |
| 1094 gfx::DecoratedText* decorated_word, | 1093 gfx::Point* baseline_point) { |
| 1095 gfx::Point* baseline_point) { | 1094 return GetRenderText()->GetDecoratedWordAtPoint(point, decorated_word, |
| 1096 return GetRenderText()->GetDecoratedWordAndBaselineAtPoint( | 1095 baseline_point); |
| 1097 point, decorated_word, baseline_point); | |
| 1098 } | |
| 1099 | |
| 1100 bool Textfield::GetDecoratedTextAndBaselineFromSelection( | |
| 1101 gfx::DecoratedText* decorated_text, | |
| 1102 gfx::Point* baseline_point) { | |
| 1103 return GetRenderText()->GetDecoratedTextAndBaselineForRange( | |
| 1104 GetRenderText()->selection(), decorated_text, baseline_point); | |
| 1105 } | 1096 } |
| 1106 | 1097 |
| 1107 //////////////////////////////////////////////////////////////////////////////// | 1098 //////////////////////////////////////////////////////////////////////////////// |
| 1108 // Textfield, SelectionControllerDelegate overrides: | 1099 // Textfield, SelectionControllerDelegate overrides: |
| 1109 | 1100 |
| 1110 bool Textfield::HasTextBeingDragged() const { | 1101 bool Textfield::HasTextBeingDragged() const { |
| 1111 return initiating_drag_; | 1102 return initiating_drag_; |
| 1112 } | 1103 } |
| 1113 | 1104 |
| 1114 //////////////////////////////////////////////////////////////////////////////// | 1105 //////////////////////////////////////////////////////////////////////////////// |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1191 } | 1182 } |
| 1192 | 1183 |
| 1193 void Textfield::DestroyTouchSelection() { | 1184 void Textfield::DestroyTouchSelection() { |
| 1194 touch_selection_controller_.reset(); | 1185 touch_selection_controller_.reset(); |
| 1195 } | 1186 } |
| 1196 | 1187 |
| 1197 //////////////////////////////////////////////////////////////////////////////// | 1188 //////////////////////////////////////////////////////////////////////////////// |
| 1198 // Textfield, ui::SimpleMenuModel::Delegate overrides: | 1189 // Textfield, ui::SimpleMenuModel::Delegate overrides: |
| 1199 | 1190 |
| 1200 bool Textfield::IsCommandIdChecked(int command_id) const { | 1191 bool Textfield::IsCommandIdChecked(int command_id) const { |
| 1201 if (text_services_context_menu_ && | |
| 1202 text_services_context_menu_->HandlesCommandId(command_id)) { | |
| 1203 return text_services_context_menu_->IsCommandIdChecked(command_id); | |
| 1204 } | |
| 1205 | |
| 1206 return true; | 1192 return true; |
| 1207 } | 1193 } |
| 1208 | 1194 |
| 1209 bool Textfield::IsCommandIdEnabled(int command_id) const { | 1195 bool Textfield::IsCommandIdEnabled(int command_id) const { |
| 1210 if (text_services_context_menu_ && | |
| 1211 text_services_context_menu_->HandlesCommandId(command_id)) { | |
| 1212 return text_services_context_menu_->IsCommandIdEnabled(command_id); | |
| 1213 } | |
| 1214 | |
| 1215 return Textfield::IsTextEditCommandEnabled( | 1196 return Textfield::IsTextEditCommandEnabled( |
| 1216 GetTextEditCommandFromMenuCommand(command_id, HasSelection())); | 1197 GetTextEditCommandFromMenuCommand(command_id, HasSelection())); |
| 1217 } | 1198 } |
| 1218 | 1199 |
| 1219 bool Textfield::GetAcceleratorForCommandId(int command_id, | 1200 bool Textfield::GetAcceleratorForCommandId(int command_id, |
| 1220 ui::Accelerator* accelerator) const { | 1201 ui::Accelerator* accelerator) const { |
| 1221 switch (command_id) { | 1202 switch (command_id) { |
| 1222 case IDS_APP_UNDO: | 1203 case IDS_APP_UNDO: |
| 1223 *accelerator = ui::Accelerator(ui::VKEY_Z, kPlatformModifier); | 1204 *accelerator = ui::Accelerator(ui::VKEY_Z, kPlatformModifier); |
| 1224 return true; | 1205 return true; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1238 case IDS_APP_SELECT_ALL: | 1219 case IDS_APP_SELECT_ALL: |
| 1239 *accelerator = ui::Accelerator(ui::VKEY_A, kPlatformModifier); | 1220 *accelerator = ui::Accelerator(ui::VKEY_A, kPlatformModifier); |
| 1240 return true; | 1221 return true; |
| 1241 | 1222 |
| 1242 default: | 1223 default: |
| 1243 return false; | 1224 return false; |
| 1244 } | 1225 } |
| 1245 } | 1226 } |
| 1246 | 1227 |
| 1247 void Textfield::ExecuteCommand(int command_id, int event_flags) { | 1228 void Textfield::ExecuteCommand(int command_id, int event_flags) { |
| 1248 if (text_services_context_menu_ && | |
| 1249 text_services_context_menu_->HandlesCommandId(command_id)) { | |
| 1250 text_services_context_menu_->ExecuteCommand(command_id, event_flags); | |
| 1251 return; | |
| 1252 } | |
| 1253 | |
| 1254 Textfield::ExecuteTextEditCommand( | 1229 Textfield::ExecuteTextEditCommand( |
| 1255 GetTextEditCommandFromMenuCommand(command_id, HasSelection())); | 1230 GetTextEditCommandFromMenuCommand(command_id, HasSelection())); |
| 1256 } | 1231 } |
| 1257 | 1232 |
| 1258 //////////////////////////////////////////////////////////////////////////////// | 1233 //////////////////////////////////////////////////////////////////////////////// |
| 1259 // Textfield, ui::TextInputClient overrides: | 1234 // Textfield, ui::TextInputClient overrides: |
| 1260 | 1235 |
| 1261 void Textfield::SetCompositionText(const ui::CompositionText& composition) { | 1236 void Textfield::SetCompositionText(const ui::CompositionText& composition) { |
| 1262 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) | 1237 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) |
| 1263 return; | 1238 return; |
| (...skipping 694 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1958 void Textfield::MoveCursorTo(const gfx::Point& point, bool select) { | 1933 void Textfield::MoveCursorTo(const gfx::Point& point, bool select) { |
| 1959 if (model_->MoveCursorTo(point, select)) | 1934 if (model_->MoveCursorTo(point, select)) |
| 1960 UpdateAfterChange(false, true); | 1935 UpdateAfterChange(false, true); |
| 1961 } | 1936 } |
| 1962 | 1937 |
| 1963 void Textfield::OnCaretBoundsChanged() { | 1938 void Textfield::OnCaretBoundsChanged() { |
| 1964 if (GetInputMethod()) | 1939 if (GetInputMethod()) |
| 1965 GetInputMethod()->OnCaretBoundsChanged(this); | 1940 GetInputMethod()->OnCaretBoundsChanged(this); |
| 1966 if (touch_selection_controller_) | 1941 if (touch_selection_controller_) |
| 1967 touch_selection_controller_->SelectionChanged(); | 1942 touch_selection_controller_->SelectionChanged(); |
| 1968 | |
| 1969 // On Mac, the context menu contains a look up item which displays the | |
| 1970 // selected text. As such, the menu needs to be updated if the selection has | |
| 1971 // changed. | |
| 1972 context_menu_contents_.reset(); | |
| 1973 } | 1943 } |
| 1974 | 1944 |
| 1975 void Textfield::OnBeforeUserAction() { | 1945 void Textfield::OnBeforeUserAction() { |
| 1976 DCHECK(!performing_user_action_); | 1946 DCHECK(!performing_user_action_); |
| 1977 performing_user_action_ = true; | 1947 performing_user_action_ = true; |
| 1978 if (controller_) | 1948 if (controller_) |
| 1979 controller_->OnBeforeUserAction(this); | 1949 controller_->OnBeforeUserAction(this); |
| 1980 } | 1950 } |
| 1981 | 1951 |
| 1982 void Textfield::OnAfterUserAction() { | 1952 void Textfield::OnAfterUserAction() { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2024 context_menu_contents_->AddItemWithStringId(IDS_APP_PASTE, IDS_APP_PASTE); | 1994 context_menu_contents_->AddItemWithStringId(IDS_APP_PASTE, IDS_APP_PASTE); |
| 2025 context_menu_contents_->AddItemWithStringId(IDS_APP_DELETE, IDS_APP_DELETE); | 1995 context_menu_contents_->AddItemWithStringId(IDS_APP_DELETE, IDS_APP_DELETE); |
| 2026 context_menu_contents_->AddSeparator(ui::NORMAL_SEPARATOR); | 1996 context_menu_contents_->AddSeparator(ui::NORMAL_SEPARATOR); |
| 2027 context_menu_contents_->AddItemWithStringId(IDS_APP_SELECT_ALL, | 1997 context_menu_contents_->AddItemWithStringId(IDS_APP_SELECT_ALL, |
| 2028 IDS_APP_SELECT_ALL); | 1998 IDS_APP_SELECT_ALL); |
| 2029 | 1999 |
| 2030 // If the controller adds menu commands, also override ExecuteCommand() and | 2000 // If the controller adds menu commands, also override ExecuteCommand() and |
| 2031 // IsCommandIdEnabled() as appropriate, for the commands added. | 2001 // IsCommandIdEnabled() as appropriate, for the commands added. |
| 2032 if (controller_) | 2002 if (controller_) |
| 2033 controller_->UpdateContextMenu(context_menu_contents_.get()); | 2003 controller_->UpdateContextMenu(context_menu_contents_.get()); |
| 2034 | |
| 2035 text_services_context_menu_ = ViewsTextServicesContextMenu::Create( | |
| 2036 context_menu_contents_.get(), this); | |
| 2037 } | 2004 } |
| 2038 | |
| 2039 context_menu_runner_.reset(new MenuRunner(context_menu_contents_.get(), | 2005 context_menu_runner_.reset(new MenuRunner(context_menu_contents_.get(), |
| 2040 MenuRunner::HAS_MNEMONICS | | 2006 MenuRunner::HAS_MNEMONICS | |
| 2041 MenuRunner::CONTEXT_MENU | | 2007 MenuRunner::CONTEXT_MENU | |
| 2042 MenuRunner::ASYNC)); | 2008 MenuRunner::ASYNC)); |
| 2043 } | 2009 } |
| 2044 | 2010 |
| 2045 bool Textfield::ImeEditingAllowed() const { | 2011 bool Textfield::ImeEditingAllowed() const { |
| 2046 // Disallow input method editing of password fields. | 2012 // Disallow input method editing of password fields. |
| 2047 ui::TextInputType t = GetTextInputType(); | 2013 ui::TextInputType t = GetTextInputType(); |
| 2048 return (t != ui::TEXT_INPUT_TYPE_NONE && t != ui::TEXT_INPUT_TYPE_PASSWORD); | 2014 return (t != ui::TEXT_INPUT_TYPE_NONE && t != ui::TEXT_INPUT_TYPE_PASSWORD); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2096 } | 2062 } |
| 2097 | 2063 |
| 2098 void Textfield::OnCursorBlinkTimerFired() { | 2064 void Textfield::OnCursorBlinkTimerFired() { |
| 2099 DCHECK(ShouldBlinkCursor()); | 2065 DCHECK(ShouldBlinkCursor()); |
| 2100 gfx::RenderText* render_text = GetRenderText(); | 2066 gfx::RenderText* render_text = GetRenderText(); |
| 2101 render_text->set_cursor_visible(!render_text->cursor_visible()); | 2067 render_text->set_cursor_visible(!render_text->cursor_visible()); |
| 2102 RepaintCursor(); | 2068 RepaintCursor(); |
| 2103 } | 2069 } |
| 2104 | 2070 |
| 2105 } // namespace views | 2071 } // namespace views |
| OLD | NEW |