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

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

Issue 8044004: Clean up of SelectionModel (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: fix SelectionModel ctor usage in TextfieldViewsModel::MoveCursorTo Created 9 years, 2 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "views/controls/textfield/native_textfield_views.h" 5 #include "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 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 if (move) { 219 if (move) {
220 gfx::SelectionModel selected; 220 gfx::SelectionModel selected;
221 model_->GetSelectionModel(&selected); 221 model_->GetSelectionModel(&selected);
222 // Adjust the drop destination if it is on or after the current selection. 222 // Adjust the drop destination if it is on or after the current selection.
223 size_t max_of_selected_range = std::max(selected.selection_start(), 223 size_t max_of_selected_range = std::max(selected.selection_start(),
224 selected.selection_end()); 224 selected.selection_end());
225 size_t min_of_selected_range = std::min(selected.selection_start(), 225 size_t min_of_selected_range = std::min(selected.selection_start(),
226 selected.selection_end()); 226 selected.selection_end());
227 size_t selected_range_length = max_of_selected_range - 227 size_t selected_range_length = max_of_selected_range -
228 min_of_selected_range; 228 min_of_selected_range;
229 if (max_of_selected_range <= drop_destination.selection_end()) 229 size_t drop_destination_end = drop_destination.selection_end();
230 drop_destination.set_selection_end( 230 if (max_of_selected_range <= drop_destination_end)
231 drop_destination.selection_end() - selected_range_length); 231 drop_destination_end -= selected_range_length;
232 else if (min_of_selected_range <= drop_destination.selection_end()) 232 else if (min_of_selected_range <= drop_destination_end)
233 drop_destination.set_selection_end(min_of_selected_range); 233 drop_destination_end = min_of_selected_range;
234 model_->DeleteSelectionAndInsertTextAt(text, 234 model_->DeleteSelectionAndInsertTextAt(text, drop_destination_end);
235 drop_destination.selection_end());
236 } else { 235 } else {
237 drop_destination.set_selection_start(drop_destination.selection_end());
238 model_->MoveCursorTo(drop_destination); 236 model_->MoveCursorTo(drop_destination);
239 // Drop always inserts text even if the textfield is not in insert mode. 237 // Drop always inserts text even if the textfield is not in insert mode.
240 model_->InsertText(text); 238 model_->InsertText(text);
241 } 239 }
242 skip_input_method_cancel_composition_ = false; 240 skip_input_method_cancel_composition_ = false;
243 UpdateAfterChange(true, true); 241 UpdateAfterChange(true, true);
244 OnAfterUserAction(); 242 OnAfterUserAction();
245 return move ? ui::DragDropTypes::DRAG_MOVE : ui::DragDropTypes::DRAG_COPY; 243 return move ? ui::DragDropTypes::DRAG_MOVE : ui::DragDropTypes::DRAG_COPY;
246 } 244 }
247 245
(...skipping 23 matching lines...) Expand all
271 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) 269 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE)
272 return; 270 return;
273 271
274 gfx::SelectionModel start_pos = GetRenderText()->FindCursorPosition(start); 272 gfx::SelectionModel start_pos = GetRenderText()->FindCursorPosition(start);
275 gfx::SelectionModel end_pos = GetRenderText()->FindCursorPosition(end); 273 gfx::SelectionModel end_pos = GetRenderText()->FindCursorPosition(end);
276 274
277 OnBeforeUserAction(); 275 OnBeforeUserAction();
278 // Merge selection models of "start_pos" and "end_pos" so that 276 // Merge selection models of "start_pos" and "end_pos" so that
279 // selection start is the value from "start_pos", while selection end, 277 // selection start is the value from "start_pos", while selection end,
280 // caret position, and caret placement are values from "end_pos". 278 // caret position, and caret placement are values from "end_pos".
281 gfx::SelectionModel sel(end_pos); 279 if (start_pos.selection_start() == end_pos.selection_end())
msw 2011/10/07 20:54:20 Is it always safe to just use the SelectRange stat
xji 2011/10/07 21:42:04 I think you are asking about line 282. When there
282 sel.set_selection_start(start_pos.selection_start()); 280 model_->SelectSelectionModel(end_pos);
283 model_->SelectSelectionModel(sel); 281 else
282 model_->SelectRange(ui::Range(start_pos.selection_start(),
283 end_pos.selection_end()));
284 284
285 OnCaretBoundsChanged(); 285 OnCaretBoundsChanged();
286 SchedulePaint(); 286 SchedulePaint();
287 OnAfterUserAction(); 287 OnAfterUserAction();
288 } 288 }
289 289
290 gfx::NativeCursor NativeTextfieldViews::GetCursor(const MouseEvent& event) { 290 gfx::NativeCursor NativeTextfieldViews::GetCursor(const MouseEvent& event) {
291 bool in_selection = GetRenderText()->IsPointInSelection(event.location()); 291 bool in_selection = GetRenderText()->IsPointInSelection(event.location());
292 bool drag_event = event.type() == ui::ET_MOUSE_DRAGGED; 292 bool drag_event = event.type() == ui::ET_MOUSE_DRAGGED;
293 bool text_cursor = !initiating_drag_ && (drag_event || !in_selection); 293 bool text_cursor = !initiating_drag_ && (drag_event || !in_selection);
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 483
484 gfx::NativeView NativeTextfieldViews::GetTestingHandle() const { 484 gfx::NativeView NativeTextfieldViews::GetTestingHandle() const {
485 NOTREACHED(); 485 NOTREACHED();
486 return NULL; 486 return NULL;
487 } 487 }
488 488
489 bool NativeTextfieldViews::IsIMEComposing() const { 489 bool NativeTextfieldViews::IsIMEComposing() const {
490 return model_->HasCompositionText(); 490 return model_->HasCompositionText();
491 } 491 }
492 492
493 void NativeTextfieldViews::GetSelectedRange(ui::Range* range) const {
494 model_->GetSelectedRange(range);
495 }
496
497 void NativeTextfieldViews::SelectRange(const ui::Range& range) {
498 model_->SelectRange(range);
499 OnCaretBoundsChanged();
500 SchedulePaint();
501 }
502
493 void NativeTextfieldViews::GetSelectionModel(gfx::SelectionModel* sel) const { 503 void NativeTextfieldViews::GetSelectionModel(gfx::SelectionModel* sel) const {
494 model_->GetSelectionModel(sel); 504 model_->GetSelectionModel(sel);
495 } 505 }
496 506
497 void NativeTextfieldViews::SelectSelectionModel( 507 void NativeTextfieldViews::SelectSelectionModel(
498 const gfx::SelectionModel& sel) { 508 const gfx::SelectionModel& sel) {
499 model_->SelectSelectionModel(sel); 509 model_->SelectSelectionModel(sel);
500 OnCaretBoundsChanged(); 510 OnCaretBoundsChanged();
501 SchedulePaint(); 511 SchedulePaint();
502 } 512 }
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 range->set_start(sel.selection_start()); 761 range->set_start(sel.selection_start());
752 range->set_end(sel.selection_end()); 762 range->set_end(sel.selection_end());
753 return true; 763 return true;
754 } 764 }
755 765
756 bool NativeTextfieldViews::SetSelectionRange(const ui::Range& range) { 766 bool NativeTextfieldViews::SetSelectionRange(const ui::Range& range) {
757 if (GetTextInputType() != ui::TEXT_INPUT_TYPE_TEXT || !range.IsValid()) 767 if (GetTextInputType() != ui::TEXT_INPUT_TYPE_TEXT || !range.IsValid())
758 return false; 768 return false;
759 769
760 OnBeforeUserAction(); 770 OnBeforeUserAction();
761 SelectSelectionModel(gfx::SelectionModel(range.start(), range.end())); 771 SelectRange(range);
762 OnAfterUserAction(); 772 OnAfterUserAction();
763 return true; 773 return true;
764 } 774 }
765 775
766 bool NativeTextfieldViews::DeleteRange(const ui::Range& range) { 776 bool NativeTextfieldViews::DeleteRange(const ui::Range& range) {
767 if (GetTextInputType() != ui::TEXT_INPUT_TYPE_TEXT || range.is_empty()) 777 if (GetTextInputType() != ui::TEXT_INPUT_TYPE_TEXT || range.is_empty())
768 return false; 778 return false;
769 779
770 OnBeforeUserAction(); 780 OnBeforeUserAction();
771 gfx::SelectionModel selection(range.start(), range.end()); 781 model_->SelectRange(range);
772 model_->SelectSelectionModel(selection);
773 if (model_->HasSelection()) { 782 if (model_->HasSelection()) {
774 model_->DeleteSelection(); 783 model_->DeleteSelection();
775 UpdateAfterChange(true, true); 784 UpdateAfterChange(true, true);
776 } 785 }
777 OnAfterUserAction(); 786 OnAfterUserAction();
778 return true; 787 return true;
779 } 788 }
780 789
781 bool NativeTextfieldViews::GetTextFromRange( 790 bool NativeTextfieldViews::GetTextFromRange(
782 const ui::Range& range, 791 const ui::Range& range,
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
1063 1072
1064 #if defined(USE_AURA) 1073 #if defined(USE_AURA)
1065 // static 1074 // static
1066 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( 1075 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper(
1067 Textfield* field) { 1076 Textfield* field) {
1068 return new NativeTextfieldViews(field); 1077 return new NativeTextfieldViews(field);
1069 } 1078 }
1070 #endif 1079 #endif
1071 1080
1072 } // namespace views 1081 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698