| 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 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 return ui::TextEditCommand::INVALID_COMMAND; | 268 return ui::TextEditCommand::INVALID_COMMAND; |
| 269 } | 269 } |
| 270 #endif | 270 #endif |
| 271 | 271 |
| 272 const gfx::FontList& GetDefaultFontList() { | 272 const gfx::FontList& GetDefaultFontList() { |
| 273 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 273 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 274 return rb.GetFontListWithDelta(ui::kLabelFontSizeDelta); | 274 return rb.GetFontListWithDelta(ui::kLabelFontSizeDelta); |
| 275 } | 275 } |
| 276 | 276 |
| 277 // Returns the ui::TextEditCommand corresponding to the |command_id| menu | 277 // Returns the ui::TextEditCommand corresponding to the |command_id| menu |
| 278 // action. Keep in sync with UpdateContextMenu. | 278 // action. |has_selection| is true if the textfield has an active selection. |
| 279 ui::TextEditCommand GetTextEditCommandFromMenuCommand(int command_id) { | 279 // Keep in sync with UpdateContextMenu. |
| 280 ui::TextEditCommand GetTextEditCommandFromMenuCommand(int command_id, |
| 281 bool has_selection) { |
| 280 switch (command_id) { | 282 switch (command_id) { |
| 281 case IDS_APP_UNDO: | 283 case IDS_APP_UNDO: |
| 282 return ui::TextEditCommand::UNDO; | 284 return ui::TextEditCommand::UNDO; |
| 283 case IDS_APP_CUT: | 285 case IDS_APP_CUT: |
| 284 return ui::TextEditCommand::CUT; | 286 return ui::TextEditCommand::CUT; |
| 285 case IDS_APP_COPY: | 287 case IDS_APP_COPY: |
| 286 return ui::TextEditCommand::COPY; | 288 return ui::TextEditCommand::COPY; |
| 287 case IDS_APP_PASTE: | 289 case IDS_APP_PASTE: |
| 288 return ui::TextEditCommand::PASTE; | 290 return ui::TextEditCommand::PASTE; |
| 289 case IDS_APP_DELETE: | 291 case IDS_APP_DELETE: |
| 290 return ui::TextEditCommand::DELETE_SELECTION; | 292 // The DELETE menu action only works in case of an active selection. |
| 293 if (has_selection) |
| 294 return ui::TextEditCommand::DELETE_FORWARD; |
| 295 break; |
| 291 case IDS_APP_SELECT_ALL: | 296 case IDS_APP_SELECT_ALL: |
| 292 return ui::TextEditCommand::SELECT_ALL; | 297 return ui::TextEditCommand::SELECT_ALL; |
| 293 default: | |
| 294 return ui::TextEditCommand::INVALID_COMMAND; | |
| 295 } | 298 } |
| 299 return ui::TextEditCommand::INVALID_COMMAND; |
| 296 } | 300 } |
| 297 | 301 |
| 298 } // namespace | 302 } // namespace |
| 299 | 303 |
| 300 // static | 304 // static |
| 301 const char Textfield::kViewClassName[] = "Textfield"; | 305 const char Textfield::kViewClassName[] = "Textfield"; |
| 302 const int Textfield::kTextPadding = 3; | 306 const int Textfield::kTextPadding = 3; |
| 303 | 307 |
| 304 // static | 308 // static |
| 305 size_t Textfield::GetCaretBlinkMs() { | 309 size_t Textfield::GetCaretBlinkMs() { |
| (...skipping 959 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1265 | 1269 |
| 1266 //////////////////////////////////////////////////////////////////////////////// | 1270 //////////////////////////////////////////////////////////////////////////////// |
| 1267 // Textfield, ui::SimpleMenuModel::Delegate overrides: | 1271 // Textfield, ui::SimpleMenuModel::Delegate overrides: |
| 1268 | 1272 |
| 1269 bool Textfield::IsCommandIdChecked(int command_id) const { | 1273 bool Textfield::IsCommandIdChecked(int command_id) const { |
| 1270 return true; | 1274 return true; |
| 1271 } | 1275 } |
| 1272 | 1276 |
| 1273 bool Textfield::IsCommandIdEnabled(int command_id) const { | 1277 bool Textfield::IsCommandIdEnabled(int command_id) const { |
| 1274 return Textfield::IsTextEditCommandEnabled( | 1278 return Textfield::IsTextEditCommandEnabled( |
| 1275 GetTextEditCommandFromMenuCommand(command_id)); | 1279 GetTextEditCommandFromMenuCommand(command_id, HasSelection())); |
| 1276 } | 1280 } |
| 1277 | 1281 |
| 1278 bool Textfield::GetAcceleratorForCommandId(int command_id, | 1282 bool Textfield::GetAcceleratorForCommandId(int command_id, |
| 1279 ui::Accelerator* accelerator) { | 1283 ui::Accelerator* accelerator) { |
| 1280 switch (command_id) { | 1284 switch (command_id) { |
| 1281 case IDS_APP_UNDO: | 1285 case IDS_APP_UNDO: |
| 1282 *accelerator = ui::Accelerator(ui::VKEY_Z, ui::EF_CONTROL_DOWN); | 1286 *accelerator = ui::Accelerator(ui::VKEY_Z, ui::EF_CONTROL_DOWN); |
| 1283 return true; | 1287 return true; |
| 1284 | 1288 |
| 1285 case IDS_APP_CUT: | 1289 case IDS_APP_CUT: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1298 *accelerator = ui::Accelerator(ui::VKEY_A, ui::EF_CONTROL_DOWN); | 1302 *accelerator = ui::Accelerator(ui::VKEY_A, ui::EF_CONTROL_DOWN); |
| 1299 return true; | 1303 return true; |
| 1300 | 1304 |
| 1301 default: | 1305 default: |
| 1302 return false; | 1306 return false; |
| 1303 } | 1307 } |
| 1304 } | 1308 } |
| 1305 | 1309 |
| 1306 void Textfield::ExecuteCommand(int command_id, int event_flags) { | 1310 void Textfield::ExecuteCommand(int command_id, int event_flags) { |
| 1307 Textfield::ExecuteTextEditCommand( | 1311 Textfield::ExecuteTextEditCommand( |
| 1308 GetTextEditCommandFromMenuCommand(command_id)); | 1312 GetTextEditCommandFromMenuCommand(command_id, HasSelection())); |
| 1309 } | 1313 } |
| 1310 | 1314 |
| 1311 //////////////////////////////////////////////////////////////////////////////// | 1315 //////////////////////////////////////////////////////////////////////////////// |
| 1312 // Textfield, ui::TextInputClient overrides: | 1316 // Textfield, ui::TextInputClient overrides: |
| 1313 | 1317 |
| 1314 void Textfield::SetCompositionText(const ui::CompositionText& composition) { | 1318 void Textfield::SetCompositionText(const ui::CompositionText& composition) { |
| 1315 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) | 1319 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) |
| 1316 return; | 1320 return; |
| 1317 | 1321 |
| 1318 OnBeforeUserAction(); | 1322 OnBeforeUserAction(); |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1539 case ui::TextEditCommand::REDO: | 1543 case ui::TextEditCommand::REDO: |
| 1540 return editable && model_->CanRedo(); | 1544 return editable && model_->CanRedo(); |
| 1541 case ui::TextEditCommand::CUT: | 1545 case ui::TextEditCommand::CUT: |
| 1542 return editable && readable && model_->HasSelection(); | 1546 return editable && readable && model_->HasSelection(); |
| 1543 case ui::TextEditCommand::COPY: | 1547 case ui::TextEditCommand::COPY: |
| 1544 return readable && model_->HasSelection(); | 1548 return readable && model_->HasSelection(); |
| 1545 case ui::TextEditCommand::PASTE: | 1549 case ui::TextEditCommand::PASTE: |
| 1546 ui::Clipboard::GetForCurrentThread()->ReadText( | 1550 ui::Clipboard::GetForCurrentThread()->ReadText( |
| 1547 ui::CLIPBOARD_TYPE_COPY_PASTE, &result); | 1551 ui::CLIPBOARD_TYPE_COPY_PASTE, &result); |
| 1548 return editable && !result.empty(); | 1552 return editable && !result.empty(); |
| 1549 case ui::TextEditCommand::DELETE_SELECTION: | |
| 1550 return editable && model_->HasSelection(); | |
| 1551 case ui::TextEditCommand::SELECT_ALL: | 1553 case ui::TextEditCommand::SELECT_ALL: |
| 1552 return !text().empty(); | 1554 return !text().empty(); |
| 1553 case ui::TextEditCommand::DELETE_FORWARD: | 1555 case ui::TextEditCommand::DELETE_FORWARD: |
| 1554 case ui::TextEditCommand::DELETE_BACKWARD: | 1556 case ui::TextEditCommand::DELETE_BACKWARD: |
| 1555 case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_LINE: | 1557 case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_LINE: |
| 1556 case ui::TextEditCommand::DELETE_TO_END_OF_LINE: | 1558 case ui::TextEditCommand::DELETE_TO_END_OF_LINE: |
| 1557 case ui::TextEditCommand::DELETE_WORD_BACKWARD: | 1559 case ui::TextEditCommand::DELETE_WORD_BACKWARD: |
| 1558 case ui::TextEditCommand::DELETE_WORD_FORWARD: | 1560 case ui::TextEditCommand::DELETE_WORD_FORWARD: |
| 1559 return editable; | 1561 return editable; |
| 1560 case ui::TextEditCommand::MOVE_LEFT: | 1562 case ui::TextEditCommand::MOVE_LEFT: |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1652 break; | 1654 break; |
| 1653 case ui::TextEditCommand::CUT: | 1655 case ui::TextEditCommand::CUT: |
| 1654 text_changed = cursor_changed = Cut(); | 1656 text_changed = cursor_changed = Cut(); |
| 1655 break; | 1657 break; |
| 1656 case ui::TextEditCommand::COPY: | 1658 case ui::TextEditCommand::COPY: |
| 1657 Copy(); | 1659 Copy(); |
| 1658 break; | 1660 break; |
| 1659 case ui::TextEditCommand::PASTE: | 1661 case ui::TextEditCommand::PASTE: |
| 1660 text_changed = cursor_changed = Paste(); | 1662 text_changed = cursor_changed = Paste(); |
| 1661 break; | 1663 break; |
| 1662 case ui::TextEditCommand::DELETE_SELECTION: | |
| 1663 text_changed = cursor_changed = model_->Delete(); | |
| 1664 break; | |
| 1665 case ui::TextEditCommand::SELECT_ALL: | 1664 case ui::TextEditCommand::SELECT_ALL: |
| 1666 SelectAll(false); | 1665 SelectAll(false); |
| 1667 break; | 1666 break; |
| 1668 case ui::TextEditCommand::DELETE_BACKWARD: | 1667 case ui::TextEditCommand::DELETE_BACKWARD: |
| 1669 text_changed = cursor_changed = model_->Backspace(); | 1668 text_changed = cursor_changed = model_->Backspace(); |
| 1670 break; | 1669 break; |
| 1671 case ui::TextEditCommand::DELETE_FORWARD: | 1670 case ui::TextEditCommand::DELETE_FORWARD: |
| 1672 text_changed = cursor_changed = model_->Delete(); | 1671 text_changed = cursor_changed = model_->Delete(); |
| 1673 break; | 1672 break; |
| 1674 case ui::TextEditCommand::DELETE_TO_END_OF_LINE: | 1673 case ui::TextEditCommand::DELETE_TO_END_OF_LINE: |
| (...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1992 RequestFocus(); | 1991 RequestFocus(); |
| 1993 model_->MoveCursorTo(mouse); | 1992 model_->MoveCursorTo(mouse); |
| 1994 if (!selection_clipboard_text.empty()) { | 1993 if (!selection_clipboard_text.empty()) { |
| 1995 model_->InsertText(selection_clipboard_text); | 1994 model_->InsertText(selection_clipboard_text); |
| 1996 UpdateAfterChange(true, true); | 1995 UpdateAfterChange(true, true); |
| 1997 } | 1996 } |
| 1998 OnAfterUserAction(); | 1997 OnAfterUserAction(); |
| 1999 } | 1998 } |
| 2000 | 1999 |
| 2001 } // namespace views | 2000 } // namespace views |
| OLD | NEW |