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

Side by Side Diff: ui/views/controls/tree/tree_view.cc

Issue 2379863002: Fix object ownership in ui/base/models. (Closed)
Patch Set: fix Created 4 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
« no previous file with comments | « ui/views/controls/tree/tree_view.h ('k') | ui/views/controls/tree/tree_view_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/tree/tree_view.h" 5 #include "ui/views/controls/tree/tree_view.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/i18n/rtl.h" 9 #include "base/i18n/rtl.h"
10 #include "base/memory/ptr_util.h"
10 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
11 #include "ui/accessibility/ax_view_state.h" 12 #include "ui/accessibility/ax_view_state.h"
12 #include "ui/base/ime/input_method.h" 13 #include "ui/base/ime/input_method.h"
13 #include "ui/base/resource/resource_bundle.h" 14 #include "ui/base/resource/resource_bundle.h"
14 #include "ui/events/event.h" 15 #include "ui/events/event.h"
15 #include "ui/events/keycodes/keyboard_codes.h" 16 #include "ui/events/keycodes/keyboard_codes.h"
16 #include "ui/gfx/canvas.h" 17 #include "ui/gfx/canvas.h"
17 #include "ui/gfx/geometry/rect.h" 18 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gfx/geometry/rect_conversions.h" 19 #include "ui/gfx/geometry/rect_conversions.h"
19 #include "ui/gfx/image/image.h" 20 #include "ui/gfx/image/image.h"
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 130
130 CancelEdit(); 131 CancelEdit();
131 132
132 model_ = model; 133 model_ = model;
133 selected_node_ = NULL; 134 selected_node_ = NULL;
134 icons_.clear(); 135 icons_.clear();
135 if (model_) { 136 if (model_) {
136 model_->AddObserver(this); 137 model_->AddObserver(this);
137 model_->GetIcons(&icons_); 138 model_->GetIcons(&icons_);
138 139
139 root_.RemoveAll(); 140 root_.DeleteAll();
140 ConfigureInternalNode(model_->GetRoot(), &root_); 141 ConfigureInternalNode(model_->GetRoot(), &root_);
141 LoadChildren(&root_); 142 LoadChildren(&root_);
142 root_.set_is_expanded(true); 143 root_.set_is_expanded(true);
143 if (root_shown_) 144 if (root_shown_)
144 selected_node_ = &root_; 145 selected_node_ = &root_;
145 else if (root_.child_count()) 146 else if (root_.child_count())
146 selected_node_ = root_.GetChild(0); 147 selected_node_ = root_.GetChild(0);
147 } 148 }
148 DrawnNodesChanged(); 149 DrawnNodesChanged();
149 } 150 }
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 423
423 void TreeView::TreeNodesAdded(TreeModel* model, 424 void TreeView::TreeNodesAdded(TreeModel* model,
424 TreeModelNode* parent, 425 TreeModelNode* parent,
425 int start, 426 int start,
426 int count) { 427 int count) {
427 InternalNode* parent_node = 428 InternalNode* parent_node =
428 GetInternalNodeForModelNode(parent, DONT_CREATE_IF_NOT_LOADED); 429 GetInternalNodeForModelNode(parent, DONT_CREATE_IF_NOT_LOADED);
429 if (!parent_node || !parent_node->loaded_children()) 430 if (!parent_node || !parent_node->loaded_children())
430 return; 431 return;
431 for (int i = 0; i < count; ++i) { 432 for (int i = 0; i < count; ++i) {
432 InternalNode* child = new InternalNode; 433 std::unique_ptr<InternalNode> child = base::MakeUnique<InternalNode>();
433 ConfigureInternalNode(model_->GetChild(parent, start + i), child); 434 ConfigureInternalNode(model_->GetChild(parent, start + i), child.get());
434 parent_node->Add(child, start + i); 435 parent_node->Add(std::move(child), start + i);
435 } 436 }
436 if (IsExpanded(parent)) 437 if (IsExpanded(parent))
437 DrawnNodesChanged(); 438 DrawnNodesChanged();
438 } 439 }
439 440
440 void TreeView::TreeNodesRemoved(TreeModel* model, 441 void TreeView::TreeNodesRemoved(TreeModel* model,
441 TreeModelNode* parent, 442 TreeModelNode* parent,
442 int start, 443 int start,
443 int count) { 444 int count) {
444 InternalNode* parent_node = 445 InternalNode* parent_node =
445 GetInternalNodeForModelNode(parent, DONT_CREATE_IF_NOT_LOADED); 446 GetInternalNodeForModelNode(parent, DONT_CREATE_IF_NOT_LOADED);
446 if (!parent_node || !parent_node->loaded_children()) 447 if (!parent_node || !parent_node->loaded_children())
447 return; 448 return;
448 bool reset_selection = false; 449 bool reset_selection = false;
449 for (int i = 0; i < count; ++i) { 450 for (int i = 0; i < count; ++i) {
450 InternalNode* child_removing = parent_node->GetChild(start); 451 InternalNode* child_removing = parent_node->GetChild(start);
451 if (selected_node_ && selected_node_->HasAncestor(child_removing)) 452 if (selected_node_ && selected_node_->HasAncestor(child_removing))
452 reset_selection = true; 453 reset_selection = true;
453 delete parent_node->Remove(child_removing); 454 parent_node->Remove(child_removing);
454 } 455 }
455 if (reset_selection) { 456 if (reset_selection) {
456 // selected_node_ is no longer valid (at the time we enter this function 457 // selected_node_ is no longer valid (at the time we enter this function
457 // its model_node() is likely deleted). Explicitly NULL out the field 458 // its model_node() is likely deleted). Explicitly NULL out the field
458 // rather than invoking SetSelectedNode() otherwise, we'll try and use a 459 // rather than invoking SetSelectedNode() otherwise, we'll try and use a
459 // deleted value. 460 // deleted value.
460 selected_node_ = NULL; 461 selected_node_ = NULL;
461 TreeModelNode* to_select = parent; 462 TreeModelNode* to_select = parent;
462 if (parent == root_.model_node() && !root_shown_) { 463 if (parent == root_.model_node() && !root_shown_) {
463 to_select = model_->GetChildCount(parent) > 0 ? 464 to_select = model_->GetChildCount(parent) > 0 ?
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
660 } 661 }
661 return true; 662 return true;
662 } 663 }
663 664
664 void TreeView::LoadChildren(InternalNode* node) { 665 void TreeView::LoadChildren(InternalNode* node) {
665 DCHECK_EQ(0, node->child_count()); 666 DCHECK_EQ(0, node->child_count());
666 DCHECK(!node->loaded_children()); 667 DCHECK(!node->loaded_children());
667 node->set_loaded_children(true); 668 node->set_loaded_children(true);
668 for (int i = 0, child_count = model_->GetChildCount(node->model_node()); 669 for (int i = 0, child_count = model_->GetChildCount(node->model_node());
669 i < child_count; ++i) { 670 i < child_count; ++i) {
670 InternalNode* child = new InternalNode; 671 std::unique_ptr<InternalNode> child = base::MakeUnique<InternalNode>();
671 ConfigureInternalNode(model_->GetChild(node->model_node(), i), child); 672 ConfigureInternalNode(model_->GetChild(node->model_node(), i), child.get());
672 node->Add(child, node->child_count()); 673 node->Add(std::move(child), node->child_count());
673 } 674 }
674 } 675 }
675 676
676 void TreeView::ConfigureInternalNode(TreeModelNode* model_node, 677 void TreeView::ConfigureInternalNode(TreeModelNode* model_node,
677 InternalNode* node) { 678 InternalNode* node) {
678 node->Reset(model_node); 679 node->Reset(model_node);
679 UpdateNodeTextWidth(node); 680 UpdateNodeTextWidth(node);
680 } 681 }
681 682
682 void TreeView::UpdateNodeTextWidth(InternalNode* node) { 683 void TreeView::UpdateNodeTextWidth(InternalNode* node) {
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
1089 if (!is_expanded_) 1090 if (!is_expanded_)
1090 return max_width; 1091 return max_width;
1091 for (int i = 0; i < child_count(); ++i) { 1092 for (int i = 0; i < child_count(); ++i) {
1092 max_width = std::max(max_width, 1093 max_width = std::max(max_width,
1093 GetChild(i)->GetMaxWidth(indent, depth + 1)); 1094 GetChild(i)->GetMaxWidth(indent, depth + 1));
1094 } 1095 }
1095 return max_width; 1096 return max_width;
1096 } 1097 }
1097 1098
1098 } // namespace views 1099 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/tree/tree_view.h ('k') | ui/views/controls/tree/tree_view_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698