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/native_textfield_views.h" | 5 #include "ui/views/controls/textfield/native_textfield_views.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 DCHECK_NE(ui::DragDropTypes::DRAG_NONE, | 309 DCHECK_NE(ui::DragDropTypes::DRAG_NONE, |
310 GetDragOperationsForView(sender, press_pt)); | 310 GetDragOperationsForView(sender, press_pt)); |
311 data->SetString(GetSelectedText()); | 311 data->SetString(GetSelectedText()); |
312 TextfieldController* controller = textfield_->GetController(); | 312 TextfieldController* controller = textfield_->GetController(); |
313 if (controller) | 313 if (controller) |
314 controller->OnWriteDragData(data); | 314 controller->OnWriteDragData(data); |
315 } | 315 } |
316 | 316 |
317 int NativeTextfieldViews::GetDragOperationsForView(views::View* sender, | 317 int NativeTextfieldViews::GetDragOperationsForView(views::View* sender, |
318 const gfx::Point& p) { | 318 const gfx::Point& p) { |
319 if (!textfield_->enabled() || !GetRenderText()->IsPointInSelection(p)) | 319 if (!textfield_->enabled() || textfield_->IsObscured() || |
| 320 !GetRenderText()->IsPointInSelection(p)) |
320 return ui::DragDropTypes::DRAG_NONE; | 321 return ui::DragDropTypes::DRAG_NONE; |
321 if (sender == this && !textfield_->read_only()) | 322 if (sender == this && !textfield_->read_only()) |
322 return ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY; | 323 return ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY; |
323 return ui::DragDropTypes::DRAG_COPY; | 324 return ui::DragDropTypes::DRAG_COPY; |
324 } | 325 } |
325 | 326 |
326 bool NativeTextfieldViews::CanStartDragForView(View* sender, | 327 bool NativeTextfieldViews::CanStartDragForView(View* sender, |
327 const gfx::Point& press_pt, | 328 const gfx::Point& press_pt, |
328 const gfx::Point& p) { | 329 const gfx::Point& p) { |
329 return GetRenderText()->IsPointInSelection(press_pt); | 330 return GetRenderText()->IsPointInSelection(press_pt); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 // For ChromeOS, we support a pre-defined font list per locale. UpdateFont() | 412 // For ChromeOS, we support a pre-defined font list per locale. UpdateFont() |
412 // only changes the font size, not the font family names. | 413 // only changes the font size, not the font family names. |
413 GetRenderText()->SetFontSize(textfield_->font().GetFontSize()); | 414 GetRenderText()->SetFontSize(textfield_->font().GetFontSize()); |
414 #else | 415 #else |
415 GetRenderText()->SetFontList(gfx::FontList(textfield_->font())); | 416 GetRenderText()->SetFontList(gfx::FontList(textfield_->font())); |
416 #endif | 417 #endif |
417 OnCaretBoundsChanged(); | 418 OnCaretBoundsChanged(); |
418 } | 419 } |
419 | 420 |
420 void NativeTextfieldViews::UpdateIsObscured() { | 421 void NativeTextfieldViews::UpdateIsObscured() { |
421 // TODO(benrg): GetRenderText()->SetObscured(textfield_->IsObscured()); | 422 GetRenderText()->SetObscured(textfield_->IsObscured()); |
422 OnCaretBoundsChanged(); | 423 OnCaretBoundsChanged(); |
423 SchedulePaint(); | 424 SchedulePaint(); |
424 OnTextInputTypeChanged(); | 425 OnTextInputTypeChanged(); |
425 } | 426 } |
426 | 427 |
427 void NativeTextfieldViews::UpdateEnabled() { | 428 void NativeTextfieldViews::UpdateEnabled() { |
428 SetEnabled(textfield_->enabled()); | 429 SetEnabled(textfield_->enabled()); |
429 SchedulePaint(); | 430 SchedulePaint(); |
430 OnTextInputTypeChanged(); | 431 OnTextInputTypeChanged(); |
431 } | 432 } |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 | 553 |
553 bool NativeTextfieldViews::IsCommandIdChecked(int command_id) const { | 554 bool NativeTextfieldViews::IsCommandIdChecked(int command_id) const { |
554 return true; | 555 return true; |
555 } | 556 } |
556 | 557 |
557 bool NativeTextfieldViews::IsCommandIdEnabled(int command_id) const { | 558 bool NativeTextfieldViews::IsCommandIdEnabled(int command_id) const { |
558 bool editable = !textfield_->read_only(); | 559 bool editable = !textfield_->read_only(); |
559 string16 result; | 560 string16 result; |
560 switch (command_id) { | 561 switch (command_id) { |
561 case IDS_APP_CUT: | 562 case IDS_APP_CUT: |
562 return editable && model_->HasSelection(); | 563 return editable && model_->HasSelection() && !textfield_->IsObscured(); |
563 case IDS_APP_COPY: | 564 case IDS_APP_COPY: |
564 return model_->HasSelection(); | 565 return model_->HasSelection() && !textfield_->IsObscured(); |
565 case IDS_APP_PASTE: | 566 case IDS_APP_PASTE: |
566 ViewsDelegate::views_delegate->GetClipboard() | 567 ViewsDelegate::views_delegate->GetClipboard() |
567 ->ReadText(ui::Clipboard::BUFFER_STANDARD, &result); | 568 ->ReadText(ui::Clipboard::BUFFER_STANDARD, &result); |
568 return editable && !result.empty(); | 569 return editable && !result.empty(); |
569 case IDS_APP_DELETE: | 570 case IDS_APP_DELETE: |
570 return editable && model_->HasSelection(); | 571 return editable && model_->HasSelection(); |
571 case IDS_APP_SELECT_ALL: | 572 case IDS_APP_SELECT_ALL: |
572 return true; | 573 return true; |
573 default: | 574 default: |
574 return textfield_->GetController()->IsCommandIdEnabled(command_id); | 575 return textfield_->GetController()->IsCommandIdEnabled(command_id); |
575 } | 576 } |
576 } | 577 } |
577 | 578 |
578 bool NativeTextfieldViews::GetAcceleratorForCommandId(int command_id, | 579 bool NativeTextfieldViews::GetAcceleratorForCommandId(int command_id, |
579 ui::Accelerator* accelerator) { | 580 ui::Accelerator* accelerator) { |
580 return false; | 581 return false; |
581 } | 582 } |
582 | 583 |
583 void NativeTextfieldViews::ExecuteCommand(int command_id) { | 584 void NativeTextfieldViews::ExecuteCommand(int command_id) { |
| 585 if (!IsCommandIdEnabled(command_id)) |
| 586 return; |
| 587 |
584 bool text_changed = false; | 588 bool text_changed = false; |
585 bool editable = !textfield_->read_only(); | |
586 OnBeforeUserAction(); | 589 OnBeforeUserAction(); |
587 switch (command_id) { | 590 switch (command_id) { |
588 case IDS_APP_CUT: | 591 case IDS_APP_CUT: |
589 if (editable) | 592 text_changed = Cut(); |
590 text_changed = Cut(); | |
591 break; | 593 break; |
592 case IDS_APP_COPY: | 594 case IDS_APP_COPY: |
593 Copy(); | 595 Copy(); |
594 break; | 596 break; |
595 case IDS_APP_PASTE: | 597 case IDS_APP_PASTE: |
596 if (editable) | 598 text_changed = Paste(); |
597 text_changed = Paste(); | |
598 break; | 599 break; |
599 case IDS_APP_DELETE: | 600 case IDS_APP_DELETE: |
600 if (editable) | 601 text_changed = model_->Delete(); |
601 text_changed = model_->Delete(); | |
602 break; | 602 break; |
603 case IDS_APP_SELECT_ALL: | 603 case IDS_APP_SELECT_ALL: |
604 SelectAll(); | 604 SelectAll(); |
605 break; | 605 break; |
606 default: | 606 default: |
607 textfield_->GetController()->ExecuteCommand(command_id); | 607 textfield_->GetController()->ExecuteCommand(command_id); |
608 break; | 608 break; |
609 } | 609 } |
610 | 610 |
611 // The cursor must have changed if text changed during cut/paste/delete. | 611 // The cursor must have changed if text changed during cut/paste/delete. |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
838 bool NativeTextfieldViews::HandleKeyEvent(const KeyEvent& key_event) { | 838 bool NativeTextfieldViews::HandleKeyEvent(const KeyEvent& key_event) { |
839 // TODO(oshima): Refactor and consolidate with ExecuteCommand. | 839 // TODO(oshima): Refactor and consolidate with ExecuteCommand. |
840 if (key_event.type() == ui::ET_KEY_PRESSED) { | 840 if (key_event.type() == ui::ET_KEY_PRESSED) { |
841 ui::KeyboardCode key_code = key_event.key_code(); | 841 ui::KeyboardCode key_code = key_event.key_code(); |
842 // TODO(oshima): shift-tab does not work. Figure out why and fix. | 842 // TODO(oshima): shift-tab does not work. Figure out why and fix. |
843 if (key_code == ui::VKEY_TAB) | 843 if (key_code == ui::VKEY_TAB) |
844 return false; | 844 return false; |
845 | 845 |
846 OnBeforeUserAction(); | 846 OnBeforeUserAction(); |
847 bool editable = !textfield_->read_only(); | 847 bool editable = !textfield_->read_only(); |
| 848 bool readable = !textfield_->IsObscured(); |
848 bool selection = key_event.IsShiftDown(); | 849 bool selection = key_event.IsShiftDown(); |
849 bool control = key_event.IsControlDown(); | 850 bool control = key_event.IsControlDown(); |
850 bool text_changed = false; | 851 bool text_changed = false; |
851 bool cursor_changed = false; | 852 bool cursor_changed = false; |
852 switch (key_code) { | 853 switch (key_code) { |
853 case ui::VKEY_Z: | 854 case ui::VKEY_Z: |
854 if (control && editable) | 855 if (control && editable) |
855 cursor_changed = text_changed = model_->Undo(); | 856 cursor_changed = text_changed = model_->Undo(); |
856 break; | 857 break; |
857 case ui::VKEY_Y: | 858 case ui::VKEY_Y: |
858 if (control && editable) | 859 if (control && editable) |
859 cursor_changed = text_changed = model_->Redo(); | 860 cursor_changed = text_changed = model_->Redo(); |
860 break; | 861 break; |
861 case ui::VKEY_A: | 862 case ui::VKEY_A: |
862 if (control) { | 863 if (control) { |
863 model_->SelectAll(); | 864 model_->SelectAll(); |
864 cursor_changed = true; | 865 cursor_changed = true; |
865 } | 866 } |
866 break; | 867 break; |
867 case ui::VKEY_X: | 868 case ui::VKEY_X: |
868 if (control && editable) | 869 if (control && editable && readable) |
869 cursor_changed = text_changed = Cut(); | 870 cursor_changed = text_changed = Cut(); |
870 break; | 871 break; |
871 case ui::VKEY_C: | 872 case ui::VKEY_C: |
872 if (control) | 873 if (control && readable) |
873 Copy(); | 874 Copy(); |
874 break; | 875 break; |
875 case ui::VKEY_V: | 876 case ui::VKEY_V: |
876 if (control && editable) | 877 if (control && editable) |
877 cursor_changed = text_changed = Paste(); | 878 cursor_changed = text_changed = Paste(); |
878 break; | 879 break; |
879 case ui::VKEY_RIGHT: | 880 case ui::VKEY_RIGHT: |
880 case ui::VKEY_LEFT: | 881 case ui::VKEY_LEFT: |
881 model_->MoveCursor( | 882 model_->MoveCursor( |
882 control ? gfx::WORD_BREAK : gfx::CHARACTER_BREAK, | 883 control ? gfx::WORD_BREAK : gfx::CHARACTER_BREAK, |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1129 | 1130 |
1130 #if defined(USE_AURA) | 1131 #if defined(USE_AURA) |
1131 // static | 1132 // static |
1132 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( | 1133 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( |
1133 Textfield* field) { | 1134 Textfield* field) { |
1134 return new NativeTextfieldViews(field); | 1135 return new NativeTextfieldViews(field); |
1135 } | 1136 } |
1136 #endif | 1137 #endif |
1137 | 1138 |
1138 } // namespace views | 1139 } // namespace views |
OLD | NEW |