| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "views/controls/textfield/native_textfield_views.h" | 5 #include "views/controls/textfield/native_textfield_views.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 } | 322 } |
| 323 | 323 |
| 324 ///////////////////////////////////////////////////////////////// | 324 ///////////////////////////////////////////////////////////////// |
| 325 // NativeTextfieldViews, ui::SimpleMenuModel::Delegate overrides: | 325 // NativeTextfieldViews, ui::SimpleMenuModel::Delegate overrides: |
| 326 | 326 |
| 327 bool NativeTextfieldViews::IsCommandIdChecked(int command_id) const { | 327 bool NativeTextfieldViews::IsCommandIdChecked(int command_id) const { |
| 328 return true; | 328 return true; |
| 329 } | 329 } |
| 330 | 330 |
| 331 bool NativeTextfieldViews::IsCommandIdEnabled(int command_id) const { | 331 bool NativeTextfieldViews::IsCommandIdEnabled(int command_id) const { |
| 332 bool editable = !textfield_->read_only(); |
| 332 string16 result; | 333 string16 result; |
| 333 switch (command_id) { | 334 switch (command_id) { |
| 334 case IDS_APP_CUT: | 335 case IDS_APP_CUT: |
| 335 return model_->HasSelection(); | 336 return editable && model_->HasSelection(); |
| 336 case IDS_APP_COPY: | 337 case IDS_APP_COPY: |
| 337 return model_->HasSelection(); | 338 return model_->HasSelection(); |
| 338 case IDS_APP_PASTE: | 339 case IDS_APP_PASTE: |
| 339 views::ViewsDelegate::views_delegate->GetClipboard() | 340 views::ViewsDelegate::views_delegate->GetClipboard() |
| 340 ->ReadText(ui::Clipboard::BUFFER_STANDARD, &result); | 341 ->ReadText(ui::Clipboard::BUFFER_STANDARD, &result); |
| 341 return !result.empty(); | 342 return editable && !result.empty(); |
| 342 case IDS_APP_DELETE: | 343 case IDS_APP_DELETE: |
| 343 return model_->HasSelection(); | 344 return editable && model_->HasSelection(); |
| 344 case IDS_APP_SELECT_ALL: | 345 case IDS_APP_SELECT_ALL: |
| 345 return true; | 346 return true; |
| 346 default: | 347 default: |
| 347 NOTREACHED(); | 348 NOTREACHED(); |
| 348 return false; | 349 return false; |
| 349 } | 350 } |
| 350 } | 351 } |
| 351 | 352 |
| 352 bool NativeTextfieldViews::GetAcceleratorForCommandId(int command_id, | 353 bool NativeTextfieldViews::GetAcceleratorForCommandId(int command_id, |
| 353 ui::Accelerator* accelerator) { | 354 ui::Accelerator* accelerator) { |
| 354 return false; | 355 return false; |
| 355 } | 356 } |
| 356 | 357 |
| 357 void NativeTextfieldViews::ExecuteCommand(int command_id) { | 358 void NativeTextfieldViews::ExecuteCommand(int command_id) { |
| 358 bool text_changed = false; | 359 bool text_changed = false; |
| 360 bool editable = !textfield_->read_only(); |
| 359 switch (command_id) { | 361 switch (command_id) { |
| 360 case IDS_APP_CUT: | 362 case IDS_APP_CUT: |
| 361 text_changed = model_->Cut(); | 363 if (editable) |
| 364 text_changed = model_->Cut(); |
| 362 break; | 365 break; |
| 363 case IDS_APP_COPY: | 366 case IDS_APP_COPY: |
| 364 model_->Copy(); | 367 model_->Copy(); |
| 365 break; | 368 break; |
| 366 case IDS_APP_PASTE: | 369 case IDS_APP_PASTE: |
| 367 text_changed = model_->Paste(); | 370 if (editable) |
| 371 text_changed = model_->Paste(); |
| 368 break; | 372 break; |
| 369 case IDS_APP_DELETE: | 373 case IDS_APP_DELETE: |
| 370 text_changed = model_->Delete(); | 374 if (editable) |
| 375 text_changed = model_->Delete(); |
| 371 break; | 376 break; |
| 372 case IDS_APP_SELECT_ALL: | 377 case IDS_APP_SELECT_ALL: |
| 373 SelectAll(); | 378 SelectAll(); |
| 374 break; | 379 break; |
| 375 default: | 380 default: |
| 376 NOTREACHED() << "unknown command: " << command_id; | 381 NOTREACHED() << "unknown command: " << command_id; |
| 377 break; | 382 break; |
| 378 } | 383 } |
| 379 if (text_changed) | 384 if (text_changed) |
| 380 PropagateTextChange(); | 385 PropagateTextChange(); |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 } | 517 } |
| 513 } | 518 } |
| 514 | 519 |
| 515 bool NativeTextfieldViews::HandleKeyEvent(const KeyEvent& key_event) { | 520 bool NativeTextfieldViews::HandleKeyEvent(const KeyEvent& key_event) { |
| 516 // TODO(oshima): handle IME. | 521 // TODO(oshima): handle IME. |
| 517 if (key_event.GetType() == views::Event::ET_KEY_PRESSED) { | 522 if (key_event.GetType() == views::Event::ET_KEY_PRESSED) { |
| 518 ui::KeyboardCode key_code = key_event.GetKeyCode(); | 523 ui::KeyboardCode key_code = key_event.GetKeyCode(); |
| 519 // TODO(oshima): shift-tab does not work. Figure out why and fix. | 524 // TODO(oshima): shift-tab does not work. Figure out why and fix. |
| 520 if (key_code == ui::VKEY_TAB) | 525 if (key_code == ui::VKEY_TAB) |
| 521 return false; | 526 return false; |
| 527 bool editable = !textfield_->read_only(); |
| 522 bool selection = key_event.IsShiftDown(); | 528 bool selection = key_event.IsShiftDown(); |
| 523 bool control = key_event.IsControlDown(); | 529 bool control = key_event.IsControlDown(); |
| 524 bool text_changed = false; | 530 bool text_changed = false; |
| 525 bool cursor_changed = false; | 531 bool cursor_changed = false; |
| 526 switch (key_code) { | 532 switch (key_code) { |
| 527 case ui::VKEY_A: | 533 case ui::VKEY_A: |
| 528 if (control) { | 534 if (control) { |
| 529 model_->SelectAll(); | 535 model_->SelectAll(); |
| 530 cursor_changed = true; | 536 cursor_changed = true; |
| 531 } | 537 } |
| 532 break; | 538 break; |
| 533 case ui::VKEY_X: | 539 case ui::VKEY_X: |
| 534 if (control) | 540 if (control && editable) |
| 535 text_changed = model_->Cut(); | 541 text_changed = model_->Cut(); |
| 536 break; | 542 break; |
| 537 case ui::VKEY_C: | 543 case ui::VKEY_C: |
| 538 if (control) | 544 if (control) |
| 539 model_->Copy(); | 545 model_->Copy(); |
| 540 break; | 546 break; |
| 541 case ui::VKEY_V: | 547 case ui::VKEY_V: |
| 542 if (control) | 548 if (control && editable) |
| 543 text_changed = model_->Paste(); | 549 text_changed = model_->Paste(); |
| 544 break; | 550 break; |
| 545 case ui::VKEY_RIGHT: | 551 case ui::VKEY_RIGHT: |
| 546 control ? model_->MoveCursorToNextWord(selection) | 552 control ? model_->MoveCursorToNextWord(selection) |
| 547 : model_->MoveCursorRight(selection); | 553 : model_->MoveCursorRight(selection); |
| 548 cursor_changed = true; | 554 cursor_changed = true; |
| 549 break; | 555 break; |
| 550 case ui::VKEY_LEFT: | 556 case ui::VKEY_LEFT: |
| 551 control ? model_->MoveCursorToPreviousWord(selection) | 557 control ? model_->MoveCursorToPreviousWord(selection) |
| 552 : model_->MoveCursorLeft(selection); | 558 : model_->MoveCursorLeft(selection); |
| 553 cursor_changed = true; | 559 cursor_changed = true; |
| 554 break; | 560 break; |
| 555 case ui::VKEY_END: | 561 case ui::VKEY_END: |
| 556 model_->MoveCursorToEnd(selection); | 562 model_->MoveCursorToEnd(selection); |
| 557 cursor_changed = true; | 563 cursor_changed = true; |
| 558 break; | 564 break; |
| 559 case ui::VKEY_HOME: | 565 case ui::VKEY_HOME: |
| 560 model_->MoveCursorToStart(selection); | 566 model_->MoveCursorToStart(selection); |
| 561 cursor_changed = true; | 567 cursor_changed = true; |
| 562 break; | 568 break; |
| 563 case ui::VKEY_BACK: | 569 case ui::VKEY_BACK: |
| 570 if (!editable) |
| 571 break; |
| 564 if (!model_->HasSelection()) { | 572 if (!model_->HasSelection()) { |
| 565 if (selection && control) { | 573 if (selection && control) { |
| 566 // If both shift and control are pressed, then erase upto the | 574 // If both shift and control are pressed, then erase upto the |
| 567 // beginning of the buffer in ChromeOS. In windows, do nothing. | 575 // beginning of the buffer in ChromeOS. In windows, do nothing. |
| 568 #if defined(OS_WIN) | 576 #if defined(OS_WIN) |
| 569 break; | 577 break; |
| 570 #else | 578 #else |
| 571 model_->MoveCursorToStart(true); | 579 model_->MoveCursorToStart(true); |
| 572 #endif | 580 #endif |
| 573 } else if (control) { | 581 } else if (control) { |
| 574 // If only control is pressed, then erase the previous word. | 582 // If only control is pressed, then erase the previous word. |
| 575 model_->MoveCursorToPreviousWord(true); | 583 model_->MoveCursorToPreviousWord(true); |
| 576 } | 584 } |
| 577 } | 585 } |
| 578 text_changed = model_->Backspace(); | 586 text_changed = model_->Backspace(); |
| 579 cursor_changed = true; | 587 cursor_changed = true; |
| 580 break; | 588 break; |
| 581 case ui::VKEY_DELETE: | 589 case ui::VKEY_DELETE: |
| 590 if (!editable) |
| 591 break; |
| 582 if (!model_->HasSelection()) { | 592 if (!model_->HasSelection()) { |
| 583 if (selection && control) { | 593 if (selection && control) { |
| 584 // If both shift and control are pressed, then erase upto the | 594 // If both shift and control are pressed, then erase upto the |
| 585 // end of the buffer in ChromeOS. In windows, do nothing. | 595 // end of the buffer in ChromeOS. In windows, do nothing. |
| 586 #if defined(OS_WIN) | 596 #if defined(OS_WIN) |
| 587 break; | 597 break; |
| 588 #else | 598 #else |
| 589 model_->MoveCursorToEnd(true); | 599 model_->MoveCursorToEnd(true); |
| 590 #endif | 600 #endif |
| 591 } else if (control) { | 601 } else if (control) { |
| 592 // If only control is pressed, then erase the next word. | 602 // If only control is pressed, then erase the next word. |
| 593 model_->MoveCursorToNextWord(true); | 603 model_->MoveCursorToNextWord(true); |
| 594 } | 604 } |
| 595 } | 605 } |
| 596 text_changed = model_->Delete(); | 606 text_changed = model_->Delete(); |
| 597 break; | 607 break; |
| 598 case ui::VKEY_INSERT: | 608 case ui::VKEY_INSERT: |
| 599 insert_ = !insert_; | 609 insert_ = !insert_; |
| 600 cursor_changed = true; | 610 cursor_changed = true; |
| 601 break; | 611 break; |
| 602 default: | 612 default: |
| 603 break; | 613 break; |
| 604 } | 614 } |
| 605 char16 print_char = GetPrintableChar(key_event); | 615 char16 print_char = GetPrintableChar(key_event); |
| 606 if (!control && print_char) { | 616 if (!control && print_char && editable) { |
| 607 if (insert_) | 617 if (insert_) |
| 608 model_->Insert(print_char); | 618 model_->Insert(print_char); |
| 609 else | 619 else |
| 610 model_->Replace(print_char); | 620 model_->Replace(print_char); |
| 611 text_changed = true; | 621 text_changed = true; |
| 612 } | 622 } |
| 613 if (text_changed) | 623 if (text_changed) |
| 614 PropagateTextChange(); | 624 PropagateTextChange(); |
| 615 if (cursor_changed) { | 625 if (cursor_changed) { |
| 616 is_cursor_visible_ = true; | 626 is_cursor_visible_ = true; |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 843 } | 853 } |
| 844 | 854 |
| 845 void NativeTextfieldViews::TextfieldBorder::SetInsets(int top, | 855 void NativeTextfieldViews::TextfieldBorder::SetInsets(int top, |
| 846 int left, | 856 int left, |
| 847 int bottom, | 857 int bottom, |
| 848 int right) { | 858 int right) { |
| 849 insets_.Set(top, left, bottom, right); | 859 insets_.Set(top, left, bottom, right); |
| 850 } | 860 } |
| 851 | 861 |
| 852 } // namespace views | 862 } // namespace views |
| OLD | NEW |