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 start_caret = GetRenderText()->FindCursorPosition(start); |
252 gfx::SelectionModel end_pos = GetRenderText()->FindCursorPosition(end); | 243 gfx::SelectionModel end_caret = GetRenderText()->FindCursorPosition(end); |
244 gfx::SelectionModel selection( | |
245 ui::Range(start_caret.caret_pos(), end_caret.caret_pos()), | |
246 end_caret.caret_affinity()); | |
253 | 247 |
254 OnBeforeUserAction(); | 248 OnBeforeUserAction(); |
255 // Merge selection models of "start_pos" and "end_pos" so that | 249 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(); | 250 OnCaretBoundsChanged(); |
265 SchedulePaint(); | 251 SchedulePaint(); |
266 OnAfterUserAction(); | 252 OnAfterUserAction(); |
267 } | 253 } |
268 | 254 |
269 gfx::NativeCursor NativeTextfieldViews::GetCursor(const MouseEvent& event) { | 255 gfx::NativeCursor NativeTextfieldViews::GetCursor(const MouseEvent& event) { |
270 bool in_selection = GetRenderText()->IsPointInSelection(event.location()); | 256 bool in_selection = GetRenderText()->IsPointInSelection(event.location()); |
271 bool drag_event = event.type() == ui::ET_MOUSE_DRAGGED; | 257 bool drag_event = event.type() == ui::ET_MOUSE_DRAGGED; |
272 bool text_cursor = !initiating_drag_ && (drag_event || !in_selection); | 258 bool text_cursor = !initiating_drag_ && (drag_event || !in_selection); |
273 #if defined(USE_AURA) | 259 #if defined(USE_AURA) |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
457 gfx::NativeView NativeTextfieldViews::GetTestingHandle() const { | 443 gfx::NativeView NativeTextfieldViews::GetTestingHandle() const { |
458 NOTREACHED(); | 444 NOTREACHED(); |
459 return NULL; | 445 return NULL; |
460 } | 446 } |
461 | 447 |
462 bool NativeTextfieldViews::IsIMEComposing() const { | 448 bool NativeTextfieldViews::IsIMEComposing() const { |
463 return model_->HasCompositionText(); | 449 return model_->HasCompositionText(); |
464 } | 450 } |
465 | 451 |
466 void NativeTextfieldViews::GetSelectedRange(ui::Range* range) const { | 452 void NativeTextfieldViews::GetSelectedRange(ui::Range* range) const { |
467 model_->GetSelectedRange(range); | 453 *range = GetRenderText()->selection(); |
468 } | 454 } |
469 | 455 |
470 void NativeTextfieldViews::SelectRange(const ui::Range& range) { | 456 void NativeTextfieldViews::SelectRange(const ui::Range& range) { |
471 model_->SelectRange(range); | 457 model_->SelectRange(range); |
472 OnCaretBoundsChanged(); | 458 OnCaretBoundsChanged(); |
473 SchedulePaint(); | 459 SchedulePaint(); |
474 textfield_->GetWidget()->NotifyAccessibilityEvent( | 460 textfield_->GetWidget()->NotifyAccessibilityEvent( |
475 textfield_, ui::AccessibilityTypes::EVENT_SELECTION_CHANGED, true); | 461 textfield_, ui::AccessibilityTypes::EVENT_SELECTION_CHANGED, true); |
476 } | 462 } |
477 | 463 |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
731 model_->GetCompositionTextRange(range); | 717 model_->GetCompositionTextRange(range); |
732 return true; | 718 return true; |
733 } | 719 } |
734 | 720 |
735 bool NativeTextfieldViews::GetSelectionRange(ui::Range* range) { | 721 bool NativeTextfieldViews::GetSelectionRange(ui::Range* range) { |
736 if (!ImeEditingAllowed()) | 722 if (!ImeEditingAllowed()) |
737 return false; | 723 return false; |
738 | 724 |
739 gfx::SelectionModel sel; | 725 gfx::SelectionModel sel; |
740 model_->GetSelectionModel(&sel); | 726 model_->GetSelectionModel(&sel); |
741 range->set_start(sel.selection_start()); | 727 *range = sel.selection(); |
msw
2012/02/28 22:51:32
nit: Just call GetSelectedRange(range); nix |sel|/
benrg
2012/03/07 01:04:44
Done.
| |
742 range->set_end(sel.selection_end()); | |
743 return true; | 728 return true; |
744 } | 729 } |
745 | 730 |
746 bool NativeTextfieldViews::SetSelectionRange(const ui::Range& range) { | 731 bool NativeTextfieldViews::SetSelectionRange(const ui::Range& range) { |
747 if (!ImeEditingAllowed() || !range.IsValid()) | 732 if (!ImeEditingAllowed() || !range.IsValid()) |
748 return false; | 733 return false; |
749 | 734 |
750 OnBeforeUserAction(); | 735 OnBeforeUserAction(); |
751 SelectRange(range); | 736 SelectRange(range); |
752 OnAfterUserAction(); | 737 OnAfterUserAction(); |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1117 | 1102 |
1118 #if defined(USE_AURA) | 1103 #if defined(USE_AURA) |
1119 // static | 1104 // static |
1120 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( | 1105 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( |
1121 Textfield* field) { | 1106 Textfield* field) { |
1122 return new NativeTextfieldViews(field); | 1107 return new NativeTextfieldViews(field); |
1123 } | 1108 } |
1124 #endif | 1109 #endif |
1125 | 1110 |
1126 } // namespace views | 1111 } // namespace views |
OLD | NEW |