Chromium Code Reviews| 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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 return ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_MOVE; | 179 return ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_MOVE; |
| 180 } | 180 } |
| 181 | 181 |
| 182 int NativeTextfieldViews::OnPerformDrop(const DropTargetEvent& event) { | 182 int NativeTextfieldViews::OnPerformDrop(const DropTargetEvent& event) { |
| 183 DCHECK(CanDrop(event.data())); | 183 DCHECK(CanDrop(event.data())); |
| 184 DCHECK(!initiating_drag_ || | 184 DCHECK(!initiating_drag_ || |
| 185 !GetRenderText()->IsPointInSelection(event.location())); | 185 !GetRenderText()->IsPointInSelection(event.location())); |
| 186 OnBeforeUserAction(); | 186 OnBeforeUserAction(); |
| 187 skip_input_method_cancel_composition_ = true; | 187 skip_input_method_cancel_composition_ = true; |
| 188 | 188 |
| 189 // TODO(msw): Remove final reference to FindCursorPosition. | 189 gfx::SelectionModel drop_destination_model = |
| 190 gfx::SelectionModel drop_destination = | |
| 191 GetRenderText()->FindCursorPosition(event.location()); | 190 GetRenderText()->FindCursorPosition(event.location()); |
| 192 string16 text; | 191 string16 text; |
| 193 event.data().GetString(&text); | 192 event.data().GetString(&text); |
| 194 | 193 |
| 195 // We'll delete the current selection for a drag and drop within this view. | 194 // We'll delete the current selection for a drag and drop within this view. |
| 196 bool move = initiating_drag_ && !event.IsControlDown() && | 195 bool move = initiating_drag_ && !event.IsControlDown() && |
| 197 event.source_operations() & ui::DragDropTypes::DRAG_MOVE; | 196 event.source_operations() & ui::DragDropTypes::DRAG_MOVE; |
| 198 if (move) { | 197 if (move) { |
| 199 gfx::SelectionModel selected; | 198 gfx::SelectionModel selected; |
| 200 model_->GetSelectionModel(&selected); | 199 model_->GetSelectionModel(&selected); |
| 201 // Adjust the drop destination if it is on or after the current selection. | 200 // Adjust the drop destination if it is on or after the current selection. |
| 202 size_t max_of_selected_range = std::max(selected.selection_start(), | 201 size_t drop_destination = drop_destination_model.caret_pos(); |
| 203 selected.selection_end()); | 202 drop_destination -= |
| 204 size_t min_of_selected_range = std::min(selected.selection_start(), | 203 selected.selection().Intersect(ui::Range(0, drop_destination)).length(); |
| 205 selected.selection_end()); | 204 model_->DeleteSelectionAndInsertTextAt(text, drop_destination); |
| 206 size_t selected_range_length = max_of_selected_range - | |
| 207 min_of_selected_range; | |
| 208 size_t drop_destination_end = drop_destination.selection_end(); | |
| 209 if (max_of_selected_range <= drop_destination_end) | |
| 210 drop_destination_end -= selected_range_length; | |
| 211 else if (min_of_selected_range <= drop_destination_end) | |
| 212 drop_destination_end = min_of_selected_range; | |
| 213 model_->DeleteSelectionAndInsertTextAt(text, drop_destination_end); | |
| 214 } else { | 205 } else { |
| 215 model_->MoveCursorTo(drop_destination); | 206 model_->MoveCursorTo(drop_destination_model); |
| 216 // Drop always inserts text even if the textfield is not in insert mode. | 207 // Drop always inserts text even if the textfield is not in insert mode. |
| 217 model_->InsertText(text); | 208 model_->InsertText(text); |
| 218 } | 209 } |
| 219 skip_input_method_cancel_composition_ = false; | 210 skip_input_method_cancel_composition_ = false; |
| 220 UpdateAfterChange(true, true); | 211 UpdateAfterChange(true, true); |
| 221 OnAfterUserAction(); | 212 OnAfterUserAction(); |
| 222 return move ? ui::DragDropTypes::DRAG_MOVE : ui::DragDropTypes::DRAG_COPY; | 213 return move ? ui::DragDropTypes::DRAG_MOVE : ui::DragDropTypes::DRAG_COPY; |
| 223 } | 214 } |
| 224 | 215 |
| 225 void NativeTextfieldViews::OnDragDone() { | 216 void NativeTextfieldViews::OnDragDone() { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 241 | 232 |
| 242 void NativeTextfieldViews::OnBlur() { | 233 void NativeTextfieldViews::OnBlur() { |
| 243 NOTREACHED(); | 234 NOTREACHED(); |
| 244 } | 235 } |
| 245 | 236 |
| 246 void NativeTextfieldViews::SelectRect(const gfx::Point& start, | 237 void NativeTextfieldViews::SelectRect(const gfx::Point& start, |
| 247 const gfx::Point& end) { | 238 const gfx::Point& end) { |
| 248 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) | 239 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) |
| 249 return; | 240 return; |
| 250 | 241 |
| 251 gfx::SelectionModel start_pos = GetRenderText()->FindCursorPosition(start); | 242 gfx::SelectionModel selection = GetRenderText()->FindCursorPosition(end); |
| 252 gfx::SelectionModel end_pos = GetRenderText()->FindCursorPosition(end); | 243 selection.set_selection_start( |
|
msw
2012/02/21 21:13:39
I agree with xji's reasoning for keeping Selection
benrg
2012/02/21 23:21:45
I rewrote it to no longer use set_selection_start,
| |
| 244 GetRenderText()->FindCursorPosition(start).caret_pos()); | |
| 253 | 245 |
| 254 OnBeforeUserAction(); | 246 OnBeforeUserAction(); |
| 255 // Merge selection models of "start_pos" and "end_pos" so that | 247 model_->SelectSelectionModel(selection); |
| 256 // selection start is the value from "start_pos", while selection end, | |
| 257 // caret position, and caret placement are values from "end_pos". | |
| 258 if (start_pos.selection_start() == end_pos.selection_end()) | |
| 259 model_->SelectSelectionModel(end_pos); | |
| 260 else | |
| 261 model_->SelectRange(ui::Range(start_pos.selection_start(), | |
| 262 end_pos.selection_end())); | |
| 263 | |
| 264 OnCaretBoundsChanged(); | 248 OnCaretBoundsChanged(); |
| 265 SchedulePaint(); | 249 SchedulePaint(); |
| 266 OnAfterUserAction(); | 250 OnAfterUserAction(); |
| 267 } | 251 } |
| 268 | 252 |
| 269 gfx::NativeCursor NativeTextfieldViews::GetCursor(const MouseEvent& event) { | 253 gfx::NativeCursor NativeTextfieldViews::GetCursor(const MouseEvent& event) { |
| 270 bool in_selection = GetRenderText()->IsPointInSelection(event.location()); | 254 bool in_selection = GetRenderText()->IsPointInSelection(event.location()); |
| 271 bool drag_event = event.type() == ui::ET_MOUSE_DRAGGED; | 255 bool drag_event = event.type() == ui::ET_MOUSE_DRAGGED; |
| 272 bool text_cursor = !initiating_drag_ && (drag_event || !in_selection); | 256 bool text_cursor = !initiating_drag_ && (drag_event || !in_selection); |
| 273 #if defined(USE_AURA) | 257 #if defined(USE_AURA) |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 457 gfx::NativeView NativeTextfieldViews::GetTestingHandle() const { | 441 gfx::NativeView NativeTextfieldViews::GetTestingHandle() const { |
| 458 NOTREACHED(); | 442 NOTREACHED(); |
| 459 return NULL; | 443 return NULL; |
| 460 } | 444 } |
| 461 | 445 |
| 462 bool NativeTextfieldViews::IsIMEComposing() const { | 446 bool NativeTextfieldViews::IsIMEComposing() const { |
| 463 return model_->HasCompositionText(); | 447 return model_->HasCompositionText(); |
| 464 } | 448 } |
| 465 | 449 |
| 466 void NativeTextfieldViews::GetSelectedRange(ui::Range* range) const { | 450 void NativeTextfieldViews::GetSelectedRange(ui::Range* range) const { |
| 467 model_->GetSelectedRange(range); | 451 *range = GetRenderText()->selection(); |
| 468 } | 452 } |
| 469 | 453 |
| 470 void NativeTextfieldViews::SelectRange(const ui::Range& range) { | 454 void NativeTextfieldViews::SelectRange(const ui::Range& range) { |
| 471 model_->SelectRange(range); | 455 model_->SelectRange(range); |
| 472 OnCaretBoundsChanged(); | 456 OnCaretBoundsChanged(); |
| 473 SchedulePaint(); | 457 SchedulePaint(); |
| 474 textfield_->GetWidget()->NotifyAccessibilityEvent( | 458 textfield_->GetWidget()->NotifyAccessibilityEvent( |
| 475 textfield_, ui::AccessibilityTypes::EVENT_SELECTION_CHANGED, true); | 459 textfield_, ui::AccessibilityTypes::EVENT_SELECTION_CHANGED, true); |
| 476 } | 460 } |
| 477 | 461 |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 731 model_->GetCompositionTextRange(range); | 715 model_->GetCompositionTextRange(range); |
| 732 return true; | 716 return true; |
| 733 } | 717 } |
| 734 | 718 |
| 735 bool NativeTextfieldViews::GetSelectionRange(ui::Range* range) { | 719 bool NativeTextfieldViews::GetSelectionRange(ui::Range* range) { |
| 736 if (!ImeEditingAllowed()) | 720 if (!ImeEditingAllowed()) |
| 737 return false; | 721 return false; |
| 738 | 722 |
| 739 gfx::SelectionModel sel; | 723 gfx::SelectionModel sel; |
| 740 model_->GetSelectionModel(&sel); | 724 model_->GetSelectionModel(&sel); |
| 741 range->set_start(sel.selection_start()); | 725 *range = sel.selection(); |
| 742 range->set_end(sel.selection_end()); | |
| 743 return true; | 726 return true; |
| 744 } | 727 } |
| 745 | 728 |
| 746 bool NativeTextfieldViews::SetSelectionRange(const ui::Range& range) { | 729 bool NativeTextfieldViews::SetSelectionRange(const ui::Range& range) { |
| 747 if (!ImeEditingAllowed() || !range.IsValid()) | 730 if (!ImeEditingAllowed() || !range.IsValid()) |
| 748 return false; | 731 return false; |
| 749 | 732 |
| 750 OnBeforeUserAction(); | 733 OnBeforeUserAction(); |
| 751 SelectRange(range); | 734 SelectRange(range); |
| 752 OnAfterUserAction(); | 735 OnAfterUserAction(); |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1117 | 1100 |
| 1118 #if defined(USE_AURA) | 1101 #if defined(USE_AURA) |
| 1119 // static | 1102 // static |
| 1120 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( | 1103 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( |
| 1121 Textfield* field) { | 1104 Textfield* field) { |
| 1122 return new NativeTextfieldViews(field); | 1105 return new NativeTextfieldViews(field); |
| 1123 } | 1106 } |
| 1124 #endif | 1107 #endif |
| 1125 | 1108 |
| 1126 } // namespace views | 1109 } // namespace views |
| OLD | NEW |