| 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/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 switch (ui::ScopedAnimationDurationScaleMode::duration_scale_mode()) { | 76 switch (ui::ScopedAnimationDurationScaleMode::duration_scale_mode()) { |
| 77 case ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION: return 100; | 77 case ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION: return 100; |
| 78 case ui::ScopedAnimationDurationScaleMode::FAST_DURATION: return 25; | 78 case ui::ScopedAnimationDurationScaleMode::FAST_DURATION: return 25; |
| 79 case ui::ScopedAnimationDurationScaleMode::SLOW_DURATION: return 400; | 79 case ui::ScopedAnimationDurationScaleMode::SLOW_DURATION: return 400; |
| 80 case ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION: return 1; | 80 case ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION: return 1; |
| 81 case ui::ScopedAnimationDurationScaleMode::ZERO_DURATION: return 0; | 81 case ui::ScopedAnimationDurationScaleMode::ZERO_DURATION: return 0; |
| 82 } | 82 } |
| 83 return 100; | 83 return 100; |
| 84 } | 84 } |
| 85 | 85 |
| 86 // Get the default command for a given key |event| and selection state. | 86 // Get the default command for a given key |event|. |
| 87 int GetCommandForKeyEvent(const ui::KeyEvent& event, bool has_selection) { | 87 int GetCommandForKeyEvent(const ui::KeyEvent& event) { |
| 88 if (event.type() != ui::ET_KEY_PRESSED || event.IsUnicodeKeyCode()) | 88 if (event.type() != ui::ET_KEY_PRESSED || event.IsUnicodeKeyCode()) |
| 89 return kNoCommand; | 89 return kNoCommand; |
| 90 | 90 |
| 91 const bool shift = event.IsShiftDown(); | 91 const bool shift = event.IsShiftDown(); |
| 92 const bool control = event.IsControlDown(); | 92 const bool control = event.IsControlDown(); |
| 93 const bool alt = event.IsAltDown() || event.IsAltGrDown(); | 93 const bool alt = event.IsAltDown() || event.IsAltGrDown(); |
| 94 switch (event.key_code()) { | 94 switch (event.key_code()) { |
| 95 case ui::VKEY_Z: | 95 case ui::VKEY_Z: |
| 96 if (control && !shift && !alt) | 96 if (control && !shift && !alt) |
| 97 return IDS_APP_UNDO; | 97 return IDS_APP_UNDO; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 122 return control ? IDS_MOVE_WORD_LEFT : IDS_MOVE_LEFT; | 122 return control ? IDS_MOVE_WORD_LEFT : IDS_MOVE_LEFT; |
| 123 return control ? IDS_MOVE_WORD_LEFT_AND_MODIFY_SELECTION : | 123 return control ? IDS_MOVE_WORD_LEFT_AND_MODIFY_SELECTION : |
| 124 IDS_MOVE_LEFT_AND_MODIFY_SELECTION; | 124 IDS_MOVE_LEFT_AND_MODIFY_SELECTION; |
| 125 case ui::VKEY_HOME: | 125 case ui::VKEY_HOME: |
| 126 return shift ? IDS_MOVE_TO_BEGINNING_OF_LINE_AND_MODIFY_SELECTION : | 126 return shift ? IDS_MOVE_TO_BEGINNING_OF_LINE_AND_MODIFY_SELECTION : |
| 127 IDS_MOVE_TO_BEGINNING_OF_LINE; | 127 IDS_MOVE_TO_BEGINNING_OF_LINE; |
| 128 case ui::VKEY_END: | 128 case ui::VKEY_END: |
| 129 return shift ? IDS_MOVE_TO_END_OF_LINE_AND_MODIFY_SELECTION : | 129 return shift ? IDS_MOVE_TO_END_OF_LINE_AND_MODIFY_SELECTION : |
| 130 IDS_MOVE_TO_END_OF_LINE; | 130 IDS_MOVE_TO_END_OF_LINE; |
| 131 case ui::VKEY_BACK: | 131 case ui::VKEY_BACK: |
| 132 if (!control || has_selection) | 132 if (!control) |
| 133 return IDS_DELETE_BACKWARD; | 133 return IDS_DELETE_BACKWARD; |
| 134 #if defined(OS_LINUX) | 134 #if defined(OS_LINUX) |
| 135 // Only erase by line break on Linux and ChromeOS. | 135 // Only erase by line break on Linux and ChromeOS. |
| 136 if (shift) | 136 if (shift) |
| 137 return IDS_DELETE_TO_BEGINNING_OF_LINE; | 137 return IDS_DELETE_TO_BEGINNING_OF_LINE; |
| 138 #endif | 138 #endif |
| 139 return IDS_DELETE_WORD_BACKWARD; | 139 return IDS_DELETE_WORD_BACKWARD; |
| 140 case ui::VKEY_DELETE: | 140 case ui::VKEY_DELETE: |
| 141 if (!control || has_selection) | |
| 142 return (shift && has_selection) ? IDS_APP_CUT : IDS_DELETE_FORWARD; | |
| 143 #if defined(OS_LINUX) | 141 #if defined(OS_LINUX) |
| 144 // Only erase by line break on Linux and ChromeOS. | 142 // Only erase by line break on Linux and ChromeOS. |
| 145 if (shift) | 143 if (shift && control) |
| 146 return IDS_DELETE_TO_END_OF_LINE; | 144 return IDS_DELETE_TO_END_OF_LINE; |
| 147 #endif | 145 #endif |
| 148 return IDS_DELETE_WORD_FORWARD; | 146 if (control) |
| 147 return IDS_DELETE_WORD_FORWARD; |
| 148 return shift ? IDS_APP_CUT : IDS_DELETE_FORWARD; |
| 149 case ui::VKEY_INSERT: | 149 case ui::VKEY_INSERT: |
| 150 if (control && !shift) | 150 if (control && !shift) |
| 151 return IDS_APP_COPY; | 151 return IDS_APP_COPY; |
| 152 return (shift && !control) ? IDS_APP_PASTE : kNoCommand; | 152 return (shift && !control) ? IDS_APP_PASTE : kNoCommand; |
| 153 default: | 153 default: |
| 154 return kNoCommand; | 154 return kNoCommand; |
| 155 } | 155 } |
| 156 } | 156 } |
| 157 | 157 |
| 158 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 158 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| (...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 723 if (IsCommandIdEnabled(command)) { | 723 if (IsCommandIdEnabled(command)) { |
| 724 ExecuteCommand(command); | 724 ExecuteCommand(command); |
| 725 handled = true; | 725 handled = true; |
| 726 } | 726 } |
| 727 } | 727 } |
| 728 return handled; | 728 return handled; |
| 729 } | 729 } |
| 730 #endif | 730 #endif |
| 731 | 731 |
| 732 if (edit_command == kNoCommand) | 732 if (edit_command == kNoCommand) |
| 733 edit_command = GetCommandForKeyEvent(event, HasSelection()); | 733 edit_command = GetCommandForKeyEvent(event); |
| 734 | 734 |
| 735 if (!handled && IsCommandIdEnabled(edit_command)) { | 735 if (!handled && IsCommandIdEnabled(edit_command)) { |
| 736 ExecuteCommand(edit_command); | 736 ExecuteCommand(edit_command); |
| 737 handled = true; | 737 handled = true; |
| 738 } | 738 } |
| 739 return handled; | 739 return handled; |
| 740 } | 740 } |
| 741 | 741 |
| 742 void Textfield::OnGestureEvent(ui::GestureEvent* event) { | 742 void Textfield::OnGestureEvent(ui::GestureEvent* event) { |
| 743 switch (event->type()) { | 743 switch (event->type()) { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 827 break; | 827 break; |
| 828 default: | 828 default: |
| 829 return; | 829 return; |
| 830 } | 830 } |
| 831 } | 831 } |
| 832 | 832 |
| 833 // This function is called by BrowserView to execute clipboard commands. | 833 // This function is called by BrowserView to execute clipboard commands. |
| 834 bool Textfield::AcceleratorPressed(const ui::Accelerator& accelerator) { | 834 bool Textfield::AcceleratorPressed(const ui::Accelerator& accelerator) { |
| 835 ui::KeyEvent event(accelerator.type(), accelerator.key_code(), | 835 ui::KeyEvent event(accelerator.type(), accelerator.key_code(), |
| 836 accelerator.modifiers()); | 836 accelerator.modifiers()); |
| 837 ExecuteCommand(GetCommandForKeyEvent(event, HasSelection())); | 837 ExecuteCommand(GetCommandForKeyEvent(event)); |
| 838 return true; | 838 return true; |
| 839 } | 839 } |
| 840 | 840 |
| 841 bool Textfield::CanHandleAccelerators() const { | 841 bool Textfield::CanHandleAccelerators() const { |
| 842 return GetRenderText()->focused() && View::CanHandleAccelerators(); | 842 return GetRenderText()->focused() && View::CanHandleAccelerators(); |
| 843 } | 843 } |
| 844 | 844 |
| 845 void Textfield::AboutToRequestFocusFromTabTraversal(bool reverse) { | 845 void Textfield::AboutToRequestFocusFromTabTraversal(bool reverse) { |
| 846 SelectAll(false); | 846 SelectAll(false); |
| 847 } | 847 } |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1296 *accelerator = ui::Accelerator(ui::VKEY_A, ui::EF_CONTROL_DOWN); | 1296 *accelerator = ui::Accelerator(ui::VKEY_A, ui::EF_CONTROL_DOWN); |
| 1297 return true; | 1297 return true; |
| 1298 | 1298 |
| 1299 default: | 1299 default: |
| 1300 return false; | 1300 return false; |
| 1301 } | 1301 } |
| 1302 } | 1302 } |
| 1303 | 1303 |
| 1304 void Textfield::ExecuteCommand(int command_id, int event_flags) { | 1304 void Textfield::ExecuteCommand(int command_id, int event_flags) { |
| 1305 DestroyTouchSelection(); | 1305 DestroyTouchSelection(); |
| 1306 |
| 1307 // Some codepaths may bypass GetCommandForKeyEvent, so any selection-dependent |
| 1308 // modifications of the command should happen here. |
| 1309 if (HasSelection()) { |
| 1310 switch (command_id) { |
| 1311 case IDS_DELETE_WORD_BACKWARD: |
| 1312 case IDS_DELETE_TO_BEGINNING_OF_LINE: |
| 1313 command_id = IDS_DELETE_BACKWARD; |
| 1314 break; |
| 1315 case IDS_DELETE_WORD_FORWARD: |
| 1316 case IDS_DELETE_TO_END_OF_LINE: |
| 1317 command_id = IDS_DELETE_FORWARD; |
| 1318 break; |
| 1319 } |
| 1320 } |
| 1321 |
| 1306 if (!IsCommandIdEnabled(command_id)) | 1322 if (!IsCommandIdEnabled(command_id)) |
| 1307 return; | 1323 return; |
| 1308 | 1324 |
| 1309 bool text_changed = false; | 1325 bool text_changed = false; |
| 1310 bool cursor_changed = false; | 1326 bool cursor_changed = false; |
| 1311 bool rtl = GetTextDirection() == base::i18n::RIGHT_TO_LEFT; | 1327 bool rtl = GetTextDirection() == base::i18n::RIGHT_TO_LEFT; |
| 1312 gfx::VisualCursorDirection begin = rtl ? gfx::CURSOR_RIGHT : gfx::CURSOR_LEFT; | 1328 gfx::VisualCursorDirection begin = rtl ? gfx::CURSOR_RIGHT : gfx::CURSOR_LEFT; |
| 1313 gfx::VisualCursorDirection end = rtl ? gfx::CURSOR_LEFT : gfx::CURSOR_RIGHT; | 1329 gfx::VisualCursorDirection end = rtl ? gfx::CURSOR_LEFT : gfx::CURSOR_RIGHT; |
| 1314 gfx::SelectionModel selection_model = GetSelectionModel(); | 1330 gfx::SelectionModel selection_model = GetSelectionModel(); |
| 1315 | 1331 |
| (...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1913 RequestFocus(); | 1929 RequestFocus(); |
| 1914 model_->MoveCursorTo(mouse); | 1930 model_->MoveCursorTo(mouse); |
| 1915 if (!selection_clipboard_text.empty()) { | 1931 if (!selection_clipboard_text.empty()) { |
| 1916 model_->InsertText(selection_clipboard_text); | 1932 model_->InsertText(selection_clipboard_text); |
| 1917 UpdateAfterChange(true, true); | 1933 UpdateAfterChange(true, true); |
| 1918 } | 1934 } |
| 1919 OnAfterUserAction(); | 1935 OnAfterUserAction(); |
| 1920 } | 1936 } |
| 1921 | 1937 |
| 1922 } // namespace views | 1938 } // namespace views |
| OLD | NEW |