Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: ui/views/controls/textfield/native_textfield_views.cc

Issue 9390022: Simplify handling of BiDi cursor movement (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix merge breakage Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 gfx::NativeView NativeTextfieldViews::GetTestingHandle() const { 457 gfx::NativeView NativeTextfieldViews::GetTestingHandle() const {
472 NOTREACHED(); 458 NOTREACHED();
473 return NULL; 459 return NULL;
474 } 460 }
475 461
476 bool NativeTextfieldViews::IsIMEComposing() const { 462 bool NativeTextfieldViews::IsIMEComposing() const {
477 return model_->HasCompositionText(); 463 return model_->HasCompositionText();
478 } 464 }
479 465
480 void NativeTextfieldViews::GetSelectedRange(ui::Range* range) const { 466 void NativeTextfieldViews::GetSelectedRange(ui::Range* range) const {
481 model_->GetSelectedRange(range); 467 *range = GetRenderText()->selection();
482 } 468 }
483 469
484 void NativeTextfieldViews::SelectRange(const ui::Range& range) { 470 void NativeTextfieldViews::SelectRange(const ui::Range& range) {
485 model_->SelectRange(range); 471 model_->SelectRange(range);
486 OnCaretBoundsChanged(); 472 OnCaretBoundsChanged();
487 SchedulePaint(); 473 SchedulePaint();
488 textfield_->GetWidget()->NotifyAccessibilityEvent( 474 textfield_->GetWidget()->NotifyAccessibilityEvent(
489 textfield_, ui::AccessibilityTypes::EVENT_SELECTION_CHANGED, true); 475 textfield_, ui::AccessibilityTypes::EVENT_SELECTION_CHANGED, true);
490 } 476 }
491 477
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
741 if (!ImeEditingAllowed()) 727 if (!ImeEditingAllowed())
742 return false; 728 return false;
743 729
744 model_->GetCompositionTextRange(range); 730 model_->GetCompositionTextRange(range);
745 return true; 731 return true;
746 } 732 }
747 733
748 bool NativeTextfieldViews::GetSelectionRange(ui::Range* range) { 734 bool NativeTextfieldViews::GetSelectionRange(ui::Range* range) {
749 if (!ImeEditingAllowed()) 735 if (!ImeEditingAllowed())
750 return false; 736 return false;
751 737 GetSelectedRange(range);
752 gfx::SelectionModel sel;
753 model_->GetSelectionModel(&sel);
754 range->set_start(sel.selection_start());
755 range->set_end(sel.selection_end());
756 return true; 738 return true;
757 } 739 }
758 740
759 bool NativeTextfieldViews::SetSelectionRange(const ui::Range& range) { 741 bool NativeTextfieldViews::SetSelectionRange(const ui::Range& range) {
760 if (!ImeEditingAllowed() || !range.IsValid()) 742 if (!ImeEditingAllowed() || !range.IsValid())
761 return false; 743 return false;
762 744
763 OnBeforeUserAction(); 745 OnBeforeUserAction();
764 SelectRange(range); 746 SelectRange(range);
765 OnAfterUserAction(); 747 OnAfterUserAction();
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
1137 1119
1138 #if defined(USE_AURA) 1120 #if defined(USE_AURA)
1139 // static 1121 // static
1140 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( 1122 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper(
1141 Textfield* field) { 1123 Textfield* field) {
1142 return new NativeTextfieldViews(field); 1124 return new NativeTextfieldViews(field);
1143 } 1125 }
1144 #endif 1126 #endif
1145 1127
1146 } // namespace views 1128 } // namespace views
OLDNEW
« no previous file with comments | « ui/gfx/selection_model.cc ('k') | ui/views/controls/textfield/native_textfield_views_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698