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 size_t start_pos = GetRenderText()->FindCursorPosition(start).caret_pos(); |
252 gfx::SelectionModel end_pos = GetRenderText()->FindCursorPosition(end); | 243 gfx::SelectionModel end_model = GetRenderText()->FindCursorPosition(end); |
| 244 size_t end_pos = end_model.caret_pos(); |
253 | 245 |
254 OnBeforeUserAction(); | 246 OnBeforeUserAction(); |
255 // Merge selection models of "start_pos" and "end_pos" so that | 247 // Merge selection models of "start_pos" and "end_pos" so that |
256 // selection start is the value from "start_pos", while selection end, | 248 // selection start is the value from "start_pos", while selection end, |
257 // caret position, and caret placement are values from "end_pos". | 249 // caret position, and caret placement are values from "end_pos". |
258 if (start_pos.selection_start() == end_pos.selection_end()) | 250 if (start_pos == end_pos) |
259 model_->SelectSelectionModel(end_pos); | 251 model_->SelectSelectionModel(end_model); |
260 else | 252 else |
261 model_->SelectRange(ui::Range(start_pos.selection_start(), | 253 model_->SelectRange(ui::Range(start_pos, end_pos)); |
262 end_pos.selection_end())); | |
263 | 254 |
264 OnCaretBoundsChanged(); | 255 OnCaretBoundsChanged(); |
265 SchedulePaint(); | 256 SchedulePaint(); |
266 OnAfterUserAction(); | 257 OnAfterUserAction(); |
267 } | 258 } |
268 | 259 |
269 gfx::NativeCursor NativeTextfieldViews::GetCursor(const MouseEvent& event) { | 260 gfx::NativeCursor NativeTextfieldViews::GetCursor(const MouseEvent& event) { |
270 bool in_selection = GetRenderText()->IsPointInSelection(event.location()); | 261 bool in_selection = GetRenderText()->IsPointInSelection(event.location()); |
271 bool drag_event = event.type() == ui::ET_MOUSE_DRAGGED; | 262 bool drag_event = event.type() == ui::ET_MOUSE_DRAGGED; |
272 bool text_cursor = !initiating_drag_ && (drag_event || !in_selection); | 263 bool text_cursor = !initiating_drag_ && (drag_event || !in_selection); |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 model_->GetCompositionTextRange(range); | 722 model_->GetCompositionTextRange(range); |
732 return true; | 723 return true; |
733 } | 724 } |
734 | 725 |
735 bool NativeTextfieldViews::GetSelectionRange(ui::Range* range) { | 726 bool NativeTextfieldViews::GetSelectionRange(ui::Range* range) { |
736 if (!ImeEditingAllowed()) | 727 if (!ImeEditingAllowed()) |
737 return false; | 728 return false; |
738 | 729 |
739 gfx::SelectionModel sel; | 730 gfx::SelectionModel sel; |
740 model_->GetSelectionModel(&sel); | 731 model_->GetSelectionModel(&sel); |
741 range->set_start(sel.selection_start()); | 732 *range = sel.selection(); |
742 range->set_end(sel.selection_end()); | |
743 return true; | 733 return true; |
744 } | 734 } |
745 | 735 |
746 bool NativeTextfieldViews::SetSelectionRange(const ui::Range& range) { | 736 bool NativeTextfieldViews::SetSelectionRange(const ui::Range& range) { |
747 if (!ImeEditingAllowed() || !range.IsValid()) | 737 if (!ImeEditingAllowed() || !range.IsValid()) |
748 return false; | 738 return false; |
749 | 739 |
750 OnBeforeUserAction(); | 740 OnBeforeUserAction(); |
751 SelectRange(range); | 741 SelectRange(range); |
752 OnAfterUserAction(); | 742 OnAfterUserAction(); |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1117 | 1107 |
1118 #if defined(USE_AURA) | 1108 #if defined(USE_AURA) |
1119 // static | 1109 // static |
1120 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( | 1110 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( |
1121 Textfield* field) { | 1111 Textfield* field) { |
1122 return new NativeTextfieldViews(field); | 1112 return new NativeTextfieldViews(field); |
1123 } | 1113 } |
1124 #endif | 1114 #endif |
1125 | 1115 |
1126 } // namespace views | 1116 } // namespace views |
OLD | NEW |