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 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 return ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_MOVE; | 190 return ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_MOVE; |
191 } | 191 } |
192 | 192 |
193 int NativeTextfieldViews::OnPerformDrop(const DropTargetEvent& event) { | 193 int NativeTextfieldViews::OnPerformDrop(const DropTargetEvent& event) { |
194 DCHECK(CanDrop(event.data())); | 194 DCHECK(CanDrop(event.data())); |
195 DCHECK(!initiating_drag_ || | 195 DCHECK(!initiating_drag_ || |
196 !GetRenderText()->IsPointInSelection(event.location())); | 196 !GetRenderText()->IsPointInSelection(event.location())); |
197 OnBeforeUserAction(); | 197 OnBeforeUserAction(); |
198 skip_input_method_cancel_composition_ = true; | 198 skip_input_method_cancel_composition_ = true; |
199 | 199 |
200 // TODO(msw): Remove final reference to FindCursorPosition. | 200 gfx::SelectionModel drop_destination_model = |
201 gfx::SelectionModel drop_destination = | |
202 GetRenderText()->FindCursorPosition(event.location()); | 201 GetRenderText()->FindCursorPosition(event.location()); |
203 string16 text; | 202 string16 text; |
204 event.data().GetString(&text); | 203 event.data().GetString(&text); |
205 | 204 |
206 // We'll delete the current selection for a drag and drop within this view. | 205 // We'll delete the current selection for a drag and drop within this view. |
207 bool move = initiating_drag_ && !event.IsControlDown() && | 206 bool move = initiating_drag_ && !event.IsControlDown() && |
208 event.source_operations() & ui::DragDropTypes::DRAG_MOVE; | 207 event.source_operations() & ui::DragDropTypes::DRAG_MOVE; |
209 if (move) { | 208 if (move) { |
210 gfx::SelectionModel selected; | 209 gfx::SelectionModel selected; |
211 model_->GetSelectionModel(&selected); | 210 model_->GetSelectionModel(&selected); |
212 // Adjust the drop destination if it is on or after the current selection. | 211 // Adjust the drop destination if it is on or after the current selection. |
213 size_t max_of_selected_range = std::max(selected.selection_start(), | 212 size_t drop_destination = drop_destination_model.caret_pos(); |
214 selected.selection_end()); | 213 drop_destination -= |
215 size_t min_of_selected_range = std::min(selected.selection_start(), | 214 selected.selection().Intersect(ui::Range(0, drop_destination)).length(); |
216 selected.selection_end()); | 215 model_->DeleteSelectionAndInsertTextAt(text, drop_destination); |
217 size_t selected_range_length = max_of_selected_range - | |
218 min_of_selected_range; | |
219 size_t drop_destination_end = drop_destination.selection_end(); | |
220 if (max_of_selected_range <= drop_destination_end) | |
221 drop_destination_end -= selected_range_length; | |
222 else if (min_of_selected_range <= drop_destination_end) | |
223 drop_destination_end = min_of_selected_range; | |
224 model_->DeleteSelectionAndInsertTextAt(text, drop_destination_end); | |
225 } else { | 216 } else { |
226 model_->MoveCursorTo(drop_destination); | 217 model_->MoveCursorTo(drop_destination_model); |
227 // Drop always inserts text even if the textfield is not in insert mode. | 218 // Drop always inserts text even if the textfield is not in insert mode. |
228 model_->InsertText(text); | 219 model_->InsertText(text); |
229 } | 220 } |
230 skip_input_method_cancel_composition_ = false; | 221 skip_input_method_cancel_composition_ = false; |
231 UpdateAfterChange(true, true); | 222 UpdateAfterChange(true, true); |
232 OnAfterUserAction(); | 223 OnAfterUserAction(); |
233 return move ? ui::DragDropTypes::DRAG_MOVE : ui::DragDropTypes::DRAG_COPY; | 224 return move ? ui::DragDropTypes::DRAG_MOVE : ui::DragDropTypes::DRAG_COPY; |
234 } | 225 } |
235 | 226 |
236 void NativeTextfieldViews::OnDragDone() { | 227 void NativeTextfieldViews::OnDragDone() { |
(...skipping 15 matching lines...) Expand all Loading... |
252 | 243 |
253 void NativeTextfieldViews::OnBlur() { | 244 void NativeTextfieldViews::OnBlur() { |
254 NOTREACHED(); | 245 NOTREACHED(); |
255 } | 246 } |
256 | 247 |
257 void NativeTextfieldViews::SelectRect(const gfx::Point& start, | 248 void NativeTextfieldViews::SelectRect(const gfx::Point& start, |
258 const gfx::Point& end) { | 249 const gfx::Point& end) { |
259 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) | 250 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) |
260 return; | 251 return; |
261 | 252 |
262 gfx::SelectionModel start_pos = GetRenderText()->FindCursorPosition(start); | 253 gfx::SelectionModel start_caret = GetRenderText()->FindCursorPosition(start); |
263 gfx::SelectionModel end_pos = GetRenderText()->FindCursorPosition(end); | 254 gfx::SelectionModel end_caret = GetRenderText()->FindCursorPosition(end); |
| 255 gfx::SelectionModel selection( |
| 256 ui::Range(start_caret.caret_pos(), end_caret.caret_pos()), |
| 257 end_caret.caret_affinity()); |
264 | 258 |
265 OnBeforeUserAction(); | 259 OnBeforeUserAction(); |
266 // Merge selection models of "start_pos" and "end_pos" so that | 260 model_->SelectSelectionModel(selection); |
267 // selection start is the value from "start_pos", while selection end, | |
268 // caret position, and caret placement are values from "end_pos". | |
269 if (start_pos.selection_start() == end_pos.selection_end()) | |
270 model_->SelectSelectionModel(end_pos); | |
271 else | |
272 model_->SelectRange(ui::Range(start_pos.selection_start(), | |
273 end_pos.selection_end())); | |
274 | |
275 OnCaretBoundsChanged(); | 261 OnCaretBoundsChanged(); |
276 SchedulePaint(); | 262 SchedulePaint(); |
277 OnAfterUserAction(); | 263 OnAfterUserAction(); |
278 } | 264 } |
279 | 265 |
280 gfx::NativeCursor NativeTextfieldViews::GetCursor(const MouseEvent& event) { | 266 gfx::NativeCursor NativeTextfieldViews::GetCursor(const MouseEvent& event) { |
281 bool in_selection = GetRenderText()->IsPointInSelection(event.location()); | 267 bool in_selection = GetRenderText()->IsPointInSelection(event.location()); |
282 bool drag_event = event.type() == ui::ET_MOUSE_DRAGGED; | 268 bool drag_event = event.type() == ui::ET_MOUSE_DRAGGED; |
283 bool text_cursor = !initiating_drag_ && (drag_event || !in_selection); | 269 bool text_cursor = !initiating_drag_ && (drag_event || !in_selection); |
284 #if defined(USE_AURA) | 270 #if defined(USE_AURA) |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 gfx::NativeView NativeTextfieldViews::GetTestingHandle() const { | 459 gfx::NativeView NativeTextfieldViews::GetTestingHandle() const { |
474 NOTREACHED(); | 460 NOTREACHED(); |
475 return NULL; | 461 return NULL; |
476 } | 462 } |
477 | 463 |
478 bool NativeTextfieldViews::IsIMEComposing() const { | 464 bool NativeTextfieldViews::IsIMEComposing() const { |
479 return model_->HasCompositionText(); | 465 return model_->HasCompositionText(); |
480 } | 466 } |
481 | 467 |
482 void NativeTextfieldViews::GetSelectedRange(ui::Range* range) const { | 468 void NativeTextfieldViews::GetSelectedRange(ui::Range* range) const { |
483 model_->GetSelectedRange(range); | 469 *range = GetRenderText()->selection(); |
484 } | 470 } |
485 | 471 |
486 void NativeTextfieldViews::SelectRange(const ui::Range& range) { | 472 void NativeTextfieldViews::SelectRange(const ui::Range& range) { |
487 model_->SelectRange(range); | 473 model_->SelectRange(range); |
488 OnCaretBoundsChanged(); | 474 OnCaretBoundsChanged(); |
489 SchedulePaint(); | 475 SchedulePaint(); |
490 textfield_->GetWidget()->NotifyAccessibilityEvent( | 476 textfield_->GetWidget()->NotifyAccessibilityEvent( |
491 textfield_, ui::AccessibilityTypes::EVENT_SELECTION_CHANGED, true); | 477 textfield_, ui::AccessibilityTypes::EVENT_SELECTION_CHANGED, true); |
492 } | 478 } |
493 | 479 |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 if (!ImeEditingAllowed()) | 729 if (!ImeEditingAllowed()) |
744 return false; | 730 return false; |
745 | 731 |
746 model_->GetCompositionTextRange(range); | 732 model_->GetCompositionTextRange(range); |
747 return true; | 733 return true; |
748 } | 734 } |
749 | 735 |
750 bool NativeTextfieldViews::GetSelectionRange(ui::Range* range) { | 736 bool NativeTextfieldViews::GetSelectionRange(ui::Range* range) { |
751 if (!ImeEditingAllowed()) | 737 if (!ImeEditingAllowed()) |
752 return false; | 738 return false; |
753 | 739 GetSelectedRange(range); |
754 gfx::SelectionModel sel; | |
755 model_->GetSelectionModel(&sel); | |
756 range->set_start(sel.selection_start()); | |
757 range->set_end(sel.selection_end()); | |
758 return true; | 740 return true; |
759 } | 741 } |
760 | 742 |
761 bool NativeTextfieldViews::SetSelectionRange(const ui::Range& range) { | 743 bool NativeTextfieldViews::SetSelectionRange(const ui::Range& range) { |
762 if (!ImeEditingAllowed() || !range.IsValid()) | 744 if (!ImeEditingAllowed() || !range.IsValid()) |
763 return false; | 745 return false; |
764 | 746 |
765 OnBeforeUserAction(); | 747 OnBeforeUserAction(); |
766 SelectRange(range); | 748 SelectRange(range); |
767 OnAfterUserAction(); | 749 OnAfterUserAction(); |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1139 | 1121 |
1140 #if defined(USE_AURA) | 1122 #if defined(USE_AURA) |
1141 // static | 1123 // static |
1142 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( | 1124 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( |
1143 Textfield* field) { | 1125 Textfield* field) { |
1144 return new NativeTextfieldViews(field); | 1126 return new NativeTextfieldViews(field); |
1145 } | 1127 } |
1146 #endif | 1128 #endif |
1147 | 1129 |
1148 } // namespace views | 1130 } // namespace views |
OLD | NEW |