OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 return drag_operation; | 311 return drag_operation; |
312 } | 312 } |
313 | 313 |
314 DCHECK(!initiating_drag_ || | 314 DCHECK(!initiating_drag_ || |
315 !GetRenderText()->IsPointInSelection(event.location())); | 315 !GetRenderText()->IsPointInSelection(event.location())); |
316 OnBeforeUserAction(); | 316 OnBeforeUserAction(); |
317 skip_input_method_cancel_composition_ = true; | 317 skip_input_method_cancel_composition_ = true; |
318 | 318 |
319 gfx::SelectionModel drop_destination_model = | 319 gfx::SelectionModel drop_destination_model = |
320 GetRenderText()->FindCursorPosition(event.location()); | 320 GetRenderText()->FindCursorPosition(event.location()); |
321 string16 text; | 321 base::string16 text; |
322 event.data().GetString(&text); | 322 event.data().GetString(&text); |
323 text = GetTextForDisplay(text); | 323 text = GetTextForDisplay(text); |
324 | 324 |
325 // Delete the current selection for a drag and drop within this view. | 325 // Delete the current selection for a drag and drop within this view. |
326 const bool move = initiating_drag_ && !event.IsControlDown() && | 326 const bool move = initiating_drag_ && !event.IsControlDown() && |
327 event.source_operations() & ui::DragDropTypes::DRAG_MOVE; | 327 event.source_operations() & ui::DragDropTypes::DRAG_MOVE; |
328 if (move) { | 328 if (move) { |
329 // Adjust the drop destination if it is on or after the current selection. | 329 // Adjust the drop destination if it is on or after the current selection. |
330 size_t drop = drop_destination_model.caret_pos(); | 330 size_t drop = drop_destination_model.caret_pos(); |
331 drop -= GetSelectedRange().Intersect(gfx::Range(0, drop)).length(); | 331 drop -= GetSelectedRange().Intersect(gfx::Range(0, drop)).length(); |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 | 486 |
487 bool NativeTextfieldViews::CanStartDragForView(View* sender, | 487 bool NativeTextfieldViews::CanStartDragForView(View* sender, |
488 const gfx::Point& press_pt, | 488 const gfx::Point& press_pt, |
489 const gfx::Point& p) { | 489 const gfx::Point& p) { |
490 return initiating_drag_ && GetRenderText()->IsPointInSelection(press_pt); | 490 return initiating_drag_ && GetRenderText()->IsPointInSelection(press_pt); |
491 } | 491 } |
492 | 492 |
493 ///////////////////////////////////////////////////////////////// | 493 ///////////////////////////////////////////////////////////////// |
494 // NativeTextfieldViews, NativeTextifieldWrapper overrides: | 494 // NativeTextfieldViews, NativeTextifieldWrapper overrides: |
495 | 495 |
496 string16 NativeTextfieldViews::GetText() const { | 496 base::string16 NativeTextfieldViews::GetText() const { |
497 return model_->GetText(); | 497 return model_->GetText(); |
498 } | 498 } |
499 | 499 |
500 void NativeTextfieldViews::UpdateText() { | 500 void NativeTextfieldViews::UpdateText() { |
501 model_->SetText(GetTextForDisplay(textfield_->text())); | 501 model_->SetText(GetTextForDisplay(textfield_->text())); |
502 OnCaretBoundsChanged(); | 502 OnCaretBoundsChanged(); |
503 SchedulePaint(); | 503 SchedulePaint(); |
504 textfield_->NotifyAccessibilityEvent( | 504 textfield_->NotifyAccessibilityEvent( |
505 ui::AccessibilityTypes::EVENT_TEXT_CHANGED, true); | 505 ui::AccessibilityTypes::EVENT_TEXT_CHANGED, true); |
506 } | 506 } |
507 | 507 |
508 void NativeTextfieldViews::AppendText(const string16& text) { | 508 void NativeTextfieldViews::AppendText(const base::string16& text) { |
509 if (text.empty()) | 509 if (text.empty()) |
510 return; | 510 return; |
511 model_->Append(GetTextForDisplay(text)); | 511 model_->Append(GetTextForDisplay(text)); |
512 OnCaretBoundsChanged(); | 512 OnCaretBoundsChanged(); |
513 SchedulePaint(); | 513 SchedulePaint(); |
514 } | 514 } |
515 | 515 |
516 void NativeTextfieldViews::InsertOrReplaceText(const string16& text) { | 516 void NativeTextfieldViews::InsertOrReplaceText(const base::string16& text) { |
517 if (text.empty()) | 517 if (text.empty()) |
518 return; | 518 return; |
519 model_->InsertText(text); | 519 model_->InsertText(text); |
520 OnCaretBoundsChanged(); | 520 OnCaretBoundsChanged(); |
521 SchedulePaint(); | 521 SchedulePaint(); |
522 } | 522 } |
523 | 523 |
524 base::i18n::TextDirection NativeTextfieldViews::GetTextDirection() const { | 524 base::i18n::TextDirection NativeTextfieldViews::GetTextDirection() const { |
525 return GetRenderText()->GetTextDirection(); | 525 return GetRenderText()->GetTextDirection(); |
526 } | 526 } |
527 | 527 |
528 string16 NativeTextfieldViews::GetSelectedText() const { | 528 base::string16 NativeTextfieldViews::GetSelectedText() const { |
529 return model_->GetSelectedText(); | 529 return model_->GetSelectedText(); |
530 } | 530 } |
531 | 531 |
532 void NativeTextfieldViews::SelectAll(bool reversed) { | 532 void NativeTextfieldViews::SelectAll(bool reversed) { |
533 model_->SelectAll(reversed); | 533 model_->SelectAll(reversed); |
534 OnCaretBoundsChanged(); | 534 OnCaretBoundsChanged(); |
535 SchedulePaint(); | 535 SchedulePaint(); |
536 } | 536 } |
537 | 537 |
538 void NativeTextfieldViews::ClearSelection() { | 538 void NativeTextfieldViews::ClearSelection() { |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
742 bool NativeTextfieldViews::IsCommandIdChecked(int command_id) const { | 742 bool NativeTextfieldViews::IsCommandIdChecked(int command_id) const { |
743 return true; | 743 return true; |
744 } | 744 } |
745 | 745 |
746 bool NativeTextfieldViews::IsCommandIdEnabled(int command_id) const { | 746 bool NativeTextfieldViews::IsCommandIdEnabled(int command_id) const { |
747 TextfieldController* controller = textfield_->GetController(); | 747 TextfieldController* controller = textfield_->GetController(); |
748 if (controller && controller->HandlesCommand(command_id)) | 748 if (controller && controller->HandlesCommand(command_id)) |
749 return controller->IsCommandIdEnabled(command_id); | 749 return controller->IsCommandIdEnabled(command_id); |
750 | 750 |
751 bool editable = !textfield_->read_only(); | 751 bool editable = !textfield_->read_only(); |
752 string16 result; | 752 base::string16 result; |
753 switch (command_id) { | 753 switch (command_id) { |
754 case IDS_APP_UNDO: | 754 case IDS_APP_UNDO: |
755 return editable && model_->CanUndo(); | 755 return editable && model_->CanUndo(); |
756 case IDS_APP_CUT: | 756 case IDS_APP_CUT: |
757 return editable && model_->HasSelection() && !textfield_->IsObscured(); | 757 return editable && model_->HasSelection() && !textfield_->IsObscured(); |
758 case IDS_APP_COPY: | 758 case IDS_APP_COPY: |
759 return model_->HasSelection() && !textfield_->IsObscured(); | 759 return model_->HasSelection() && !textfield_->IsObscured(); |
760 case IDS_APP_PASTE: | 760 case IDS_APP_PASTE: |
761 ui::Clipboard::GetForCurrentThread()->ReadText( | 761 ui::Clipboard::GetForCurrentThread()->ReadText( |
762 ui::CLIPBOARD_TYPE_COPY_PASTE, &result); | 762 ui::CLIPBOARD_TYPE_COPY_PASTE, &result); |
(...skipping 10 matching lines...) Expand all Loading... |
773 bool NativeTextfieldViews::GetAcceleratorForCommandId(int command_id, | 773 bool NativeTextfieldViews::GetAcceleratorForCommandId(int command_id, |
774 ui::Accelerator* accelerator) { | 774 ui::Accelerator* accelerator) { |
775 return false; | 775 return false; |
776 } | 776 } |
777 | 777 |
778 bool NativeTextfieldViews::IsItemForCommandIdDynamic(int command_id) const { | 778 bool NativeTextfieldViews::IsItemForCommandIdDynamic(int command_id) const { |
779 const TextfieldController* controller = textfield_->GetController(); | 779 const TextfieldController* controller = textfield_->GetController(); |
780 return controller && controller->IsItemForCommandIdDynamic(command_id); | 780 return controller && controller->IsItemForCommandIdDynamic(command_id); |
781 } | 781 } |
782 | 782 |
783 string16 NativeTextfieldViews::GetLabelForCommandId(int command_id) const { | 783 base::string16 NativeTextfieldViews::GetLabelForCommandId( |
| 784 int command_id) const { |
784 const TextfieldController* controller = textfield_->GetController(); | 785 const TextfieldController* controller = textfield_->GetController(); |
785 return controller ? controller->GetLabelForCommandId(command_id) : string16(); | 786 return controller ? controller->GetLabelForCommandId(command_id) : |
| 787 base::string16(); |
786 } | 788 } |
787 | 789 |
788 void NativeTextfieldViews::ExecuteCommand(int command_id, int event_flags) { | 790 void NativeTextfieldViews::ExecuteCommand(int command_id, int event_flags) { |
789 touch_selection_controller_.reset(); | 791 touch_selection_controller_.reset(); |
790 if (!IsCommandIdEnabled(command_id)) | 792 if (!IsCommandIdEnabled(command_id)) |
791 return; | 793 return; |
792 | 794 |
793 TextfieldController* controller = textfield_->GetController(); | 795 TextfieldController* controller = textfield_->GetController(); |
794 if (controller && controller->HandlesCommand(command_id)) { | 796 if (controller && controller->HandlesCommand(command_id)) { |
795 controller->ExecuteCommand(command_id, 0); | 797 controller->ExecuteCommand(command_id, 0); |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
904 return; | 906 return; |
905 | 907 |
906 OnBeforeUserAction(); | 908 OnBeforeUserAction(); |
907 skip_input_method_cancel_composition_ = true; | 909 skip_input_method_cancel_composition_ = true; |
908 model_->CancelCompositionText(); | 910 model_->CancelCompositionText(); |
909 skip_input_method_cancel_composition_ = false; | 911 skip_input_method_cancel_composition_ = false; |
910 UpdateAfterChange(true, true); | 912 UpdateAfterChange(true, true); |
911 OnAfterUserAction(); | 913 OnAfterUserAction(); |
912 } | 914 } |
913 | 915 |
914 void NativeTextfieldViews::InsertText(const string16& text) { | 916 void NativeTextfieldViews::InsertText(const base::string16& text) { |
915 // TODO(suzhe): Filter invalid characters. | 917 // TODO(suzhe): Filter invalid characters. |
916 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || text.empty()) | 918 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || text.empty()) |
917 return; | 919 return; |
918 | 920 |
919 OnBeforeUserAction(); | 921 OnBeforeUserAction(); |
920 skip_input_method_cancel_composition_ = true; | 922 skip_input_method_cancel_composition_ = true; |
921 if (GetRenderText()->insert_mode()) | 923 if (GetRenderText()->insert_mode()) |
922 model_->InsertText(GetTextForDisplay(text)); | 924 model_->InsertText(GetTextForDisplay(text)); |
923 else | 925 else |
924 model_->ReplaceText(GetTextForDisplay(text)); | 926 model_->ReplaceText(GetTextForDisplay(text)); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1063 if (model_->HasSelection()) { | 1065 if (model_->HasSelection()) { |
1064 model_->DeleteSelection(); | 1066 model_->DeleteSelection(); |
1065 UpdateAfterChange(true, true); | 1067 UpdateAfterChange(true, true); |
1066 } | 1068 } |
1067 OnAfterUserAction(); | 1069 OnAfterUserAction(); |
1068 return true; | 1070 return true; |
1069 } | 1071 } |
1070 | 1072 |
1071 bool NativeTextfieldViews::GetTextFromRange( | 1073 bool NativeTextfieldViews::GetTextFromRange( |
1072 const gfx::Range& range, | 1074 const gfx::Range& range, |
1073 string16* text) const { | 1075 base::string16* text) const { |
1074 if (!ImeEditingAllowed() || !range.IsValid()) | 1076 if (!ImeEditingAllowed() || !range.IsValid()) |
1075 return false; | 1077 return false; |
1076 | 1078 |
1077 gfx::Range text_range; | 1079 gfx::Range text_range; |
1078 if (!GetTextRange(&text_range) || !text_range.Contains(range)) | 1080 if (!GetTextRange(&text_range) || !text_range.Contains(range)) |
1079 return false; | 1081 return false; |
1080 | 1082 |
1081 *text = model_->GetTextFromRange(range); | 1083 *text = model_->GetTextFromRange(range); |
1082 return true; | 1084 return true; |
1083 } | 1085 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1130 if (skip_input_method_cancel_composition_) | 1132 if (skip_input_method_cancel_composition_) |
1131 return; | 1133 return; |
1132 DCHECK(textfield_->GetInputMethod()); | 1134 DCHECK(textfield_->GetInputMethod()); |
1133 textfield_->GetInputMethod()->CancelComposition(textfield_); | 1135 textfield_->GetInputMethod()->CancelComposition(textfield_); |
1134 } | 1136 } |
1135 | 1137 |
1136 gfx::RenderText* NativeTextfieldViews::GetRenderText() const { | 1138 gfx::RenderText* NativeTextfieldViews::GetRenderText() const { |
1137 return model_->render_text(); | 1139 return model_->render_text(); |
1138 } | 1140 } |
1139 | 1141 |
1140 string16 NativeTextfieldViews::GetTextForDisplay(const string16& text) { | 1142 base::string16 NativeTextfieldViews::GetTextForDisplay( |
| 1143 const base::string16& text) { |
1141 return textfield_->style() & Textfield::STYLE_LOWERCASE ? | 1144 return textfield_->style() & Textfield::STYLE_LOWERCASE ? |
1142 base::i18n::ToLower(text) : text; | 1145 base::i18n::ToLower(text) : text; |
1143 } | 1146 } |
1144 | 1147 |
1145 void NativeTextfieldViews::UpdateColorsFromTheme(const ui::NativeTheme* theme) { | 1148 void NativeTextfieldViews::UpdateColorsFromTheme(const ui::NativeTheme* theme) { |
1146 UpdateTextColor(); | 1149 UpdateTextColor(); |
1147 UpdateBackgroundColor(); | 1150 UpdateBackgroundColor(); |
1148 gfx::RenderText* render_text = GetRenderText(); | 1151 gfx::RenderText* render_text = GetRenderText(); |
1149 render_text->set_cursor_color(textfield_->GetTextColor()); | 1152 render_text->set_cursor_color(textfield_->GetTextColor()); |
1150 render_text->set_selection_color(theme->GetSystemColor( | 1153 render_text->set_selection_color(theme->GetSystemColor( |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1419 controller->OnAfterCutOrCopy(); | 1422 controller->OnAfterCutOrCopy(); |
1420 return true; | 1423 return true; |
1421 } | 1424 } |
1422 return false; | 1425 return false; |
1423 } | 1426 } |
1424 | 1427 |
1425 bool NativeTextfieldViews::Paste() { | 1428 bool NativeTextfieldViews::Paste() { |
1426 if (textfield_->read_only()) | 1429 if (textfield_->read_only()) |
1427 return false; | 1430 return false; |
1428 | 1431 |
1429 const string16 original_text = GetText(); | 1432 const base::string16 original_text = GetText(); |
1430 const bool success = model_->Paste(); | 1433 const bool success = model_->Paste(); |
1431 | 1434 |
1432 if (success) { | 1435 if (success) { |
1433 // As Paste is handled in model_->Paste(), the RenderText may contain | 1436 // As Paste is handled in model_->Paste(), the RenderText may contain |
1434 // upper case characters. This is not consistent with other places | 1437 // upper case characters. This is not consistent with other places |
1435 // which keeps RenderText only containing lower case characters. | 1438 // which keeps RenderText only containing lower case characters. |
1436 string16 new_text = GetTextForDisplay(GetText()); | 1439 base::string16 new_text = GetTextForDisplay(GetText()); |
1437 model_->SetText(new_text); | 1440 model_->SetText(new_text); |
1438 | 1441 |
1439 TextfieldController* controller = textfield_->GetController(); | 1442 TextfieldController* controller = textfield_->GetController(); |
1440 if (controller) | 1443 if (controller) |
1441 controller->OnAfterPaste(); | 1444 controller->OnAfterPaste(); |
1442 } | 1445 } |
1443 return success; | 1446 return success; |
1444 } | 1447 } |
1445 | 1448 |
1446 void NativeTextfieldViews::TrackMouseClicks(const ui::MouseEvent& event) { | 1449 void NativeTextfieldViews::TrackMouseClicks(const ui::MouseEvent& event) { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1536 if (index != -1) { | 1539 if (index != -1) { |
1537 obscured_reveal_timer_.Start( | 1540 obscured_reveal_timer_.Start( |
1538 FROM_HERE, | 1541 FROM_HERE, |
1539 duration, | 1542 duration, |
1540 base::Bind(&NativeTextfieldViews::RevealObscuredChar, | 1543 base::Bind(&NativeTextfieldViews::RevealObscuredChar, |
1541 base::Unretained(this), -1, base::TimeDelta())); | 1544 base::Unretained(this), -1, base::TimeDelta())); |
1542 } | 1545 } |
1543 } | 1546 } |
1544 | 1547 |
1545 } // namespace views | 1548 } // namespace views |
OLD | NEW |