From 992c625bf2d6dfaaa46cf5b45cdcd7de68e7817e Mon Sep 17 00:00:00 2001 From: "phajdan.jr@chromium.org" Date: Wed, 13 May 2009 18:54:20 +0000 Subject: Move tree-related classes that Linux code depends on from views/ to app/ TEST=If it compiles and unit_tests pass, it's ok. Just moving files around. http://crbug.com/11066 Review URL: http://codereview.chromium.org/115185 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15982 0039d316-1c4b-4281-b951-d872f2087c98 --- app/app.vcproj | 12 + app/tree_model.h | 87 +++++++ app/tree_node_iterator.h | 70 +++++ app/tree_node_iterator_unittest.cc | 41 +++ app/tree_node_model.h | 279 ++++++++++++++++++++ chrome/browser/automation/automation_provider.h | 2 + .../bookmarks/bookmark_folder_tree_model.cc | 8 +- .../browser/bookmarks/bookmark_folder_tree_model.h | 16 +- .../bookmark_folder_tree_model_unittest.cc | 24 +- chrome/browser/bookmarks/bookmark_model.h | 4 +- .../browser/bookmarks/bookmark_model_unittest.cc | 4 +- chrome/browser/bookmarks/bookmark_utils.cc | 10 +- chrome/browser/history/starred_url_database.h | 4 +- chrome/browser/views/bookmark_editor_view.cc | 3 +- chrome/browser/views/bookmark_editor_view.h | 11 +- chrome/browser/views/bookmark_folder_tree_view.cc | 4 +- chrome/browser/views/bookmark_manager_view.cc | 2 +- chrome/chrome.gyp | 10 +- chrome/test/unit/unittests.vcproj | 8 +- views/controls/tree/tree_model.h | 91 ------- views/controls/tree/tree_node_iterator.h | 74 ------ views/controls/tree/tree_node_iterator_unittest.cc | 41 --- views/controls/tree/tree_node_model.h | 283 --------------------- views/controls/tree/tree_view.h | 2 +- views/views.vcproj | 12 - 25 files changed, 546 insertions(+), 556 deletions(-) create mode 100644 app/tree_model.h create mode 100644 app/tree_node_iterator.h create mode 100644 app/tree_node_iterator_unittest.cc create mode 100644 app/tree_node_model.h delete mode 100644 views/controls/tree/tree_model.h delete mode 100644 views/controls/tree/tree_node_iterator.h delete mode 100644 views/controls/tree/tree_node_iterator_unittest.cc delete mode 100644 views/controls/tree/tree_node_model.h diff --git a/app/app.vcproj b/app/app.vcproj index d940bf9..64b07d87 100644 --- a/app/app.vcproj +++ b/app/app.vcproj @@ -262,6 +262,18 @@ > + + + + + + diff --git a/app/tree_model.h b/app/tree_model.h new file mode 100644 index 0000000..e7b93ee --- /dev/null +++ b/app/tree_model.h @@ -0,0 +1,87 @@ +// Copyright (c) 2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef APP_TREE_MODEL_H_ +#define APP_TREE_MODEL_H_ + +#include + +#include "base/logging.h" + +class SkBitmap; + +class TreeModel; + +// TreeModelNode -------------------------------------------------------------- + +// Type of class returned from the model. +class TreeModelNode { + public: + // Returns the title for the node. + virtual std::wstring GetTitle() = 0; +}; + +// Observer for the TreeModel. Notified of significant events to the model. +class TreeModelObserver { + public: + // Notification that nodes were added to the specified parent. + virtual void TreeNodesAdded(TreeModel* model, + TreeModelNode* parent, + int start, + int count) = 0; + + // Notification that nodes were removed from the specified parent. + virtual void TreeNodesRemoved(TreeModel* model, + TreeModelNode* parent, + int start, + int count) = 0; + + // Notification the children of |parent| have been reordered. Note, only + // the direct children of |parent| have been reordered, not descendants. + virtual void TreeNodeChildrenReordered(TreeModel* model, + TreeModelNode* parent) = 0; + + // Notification that the contents of a node has changed. + virtual void TreeNodeChanged(TreeModel* model, TreeModelNode* node) = 0; +}; + +// TreeModel ------------------------------------------------------------------ + +// The model for TreeView. +class TreeModel { + public: + // Returns the root of the tree. This may or may not be shown in the tree, + // see SetRootShown for details. + virtual TreeModelNode* GetRoot() = 0; + + // Returns the number of children in the specified node. + virtual int GetChildCount(TreeModelNode* parent) = 0; + + // Returns the child node at the specified index. + virtual TreeModelNode* GetChild(TreeModelNode* parent, int index) = 0; + + // Returns the parent of a node, or NULL if node is the root. + virtual TreeModelNode* GetParent(TreeModelNode* node) = 0; + + // Sets the observer of the model. + virtual void SetObserver(TreeModelObserver* observer) = 0; + + // Sets the title of the specified node. + // This is only invoked if the node is editable and the user edits a node. + virtual void SetTitle(TreeModelNode* node, + const std::wstring& title) { + NOTREACHED(); + } + + // Returns the set of icons for the nodes in the tree. You only need override + // this if you don't want to use the default folder icons. + virtual void GetIcons(std::vector* icons) {} + + // Returns the index of the icon to use for |node|. Return -1 to use the + // default icon. The index is relative to the list of icons returned from + // GetIcons. + virtual int GetIconIndex(TreeModelNode* node) { return -1; } +}; + +#endif // APP_TREE_TREE_MODEL_H_ diff --git a/app/tree_node_iterator.h b/app/tree_node_iterator.h new file mode 100644 index 0000000..7e7398c --- /dev/null +++ b/app/tree_node_iterator.h @@ -0,0 +1,70 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef APP_TREE_NODE_ITERATOR_H_ +#define APP_TREE_NODE_ITERATOR_H_ + +#include + +#include "base/basictypes.h" +#include "base/logging.h" + +// Iterator that iterates over the descendants of a node. The iteration does +// not include the node itself, only the descendants. The following illustrates +// typical usage: +// while (iterator.has_next()) { +// Node* node = iterator.Next(); +// // do something with node. +// } +template +class TreeNodeIterator { + public: + explicit TreeNodeIterator(NodeType* node) { + if (node->GetChildCount() > 0) + positions_.push(Position(node, 0)); + } + + // Returns true if there are more descendants. + bool has_next() const { return !positions_.empty(); } + + // Returns the next descendant. + NodeType* Next() { + if (!has_next()) { + NOTREACHED(); + return NULL; + } + + NodeType* result = positions_.top().node->GetChild(positions_.top().index); + + // Make sure we don't attempt to visit result again. + positions_.top().index++; + + // Iterate over result's children. + positions_.push(Position(result, 0)); + + // Advance to next position. + while (!positions_.empty() && positions_.top().index >= + positions_.top().node->GetChildCount()) { + positions_.pop(); + } + + return result; + } + + private: + template + struct Position { + Position(PositionNodeType* node, int index) : node(node), index(index) {} + Position() : node(NULL), index(-1) {} + + PositionNodeType* node; + int index; + }; + + std::stack > positions_; + + DISALLOW_COPY_AND_ASSIGN(TreeNodeIterator); +}; + +#endif // APP_TREE_NODE_ITERATOR_H_ diff --git a/app/tree_node_iterator_unittest.cc b/app/tree_node_iterator_unittest.cc new file mode 100644 index 0000000..c20babe --- /dev/null +++ b/app/tree_node_iterator_unittest.cc @@ -0,0 +1,41 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "testing/gtest/include/gtest/gtest.h" + +#include "app/tree_node_iterator.h" +#include "app/tree_node_model.h" + +namespace { + +TEST(TreeNodeIteratorTest, Test) { + TreeNodeWithValue root; + root.Add(0, new TreeNodeWithValue(1)); + root.Add(1, new TreeNodeWithValue(2)); + TreeNodeWithValue* f3 = new TreeNodeWithValue(3); + root.Add(2, f3); + TreeNodeWithValue* f4 = new TreeNodeWithValue(4); + f3->Add(0, f4); + f4->Add(0, new TreeNodeWithValue(5)); + + TreeNodeIterator > iterator(&root); + ASSERT_TRUE(iterator.has_next()); + ASSERT_EQ(root.GetChild(0), iterator.Next()); + + ASSERT_TRUE(iterator.has_next()); + ASSERT_EQ(root.GetChild(1), iterator.Next()); + + ASSERT_TRUE(iterator.has_next()); + ASSERT_EQ(root.GetChild(2), iterator.Next()); + + ASSERT_TRUE(iterator.has_next()); + ASSERT_EQ(f4, iterator.Next()); + + ASSERT_TRUE(iterator.has_next()); + ASSERT_EQ(f4->GetChild(0), iterator.Next()); + + ASSERT_FALSE(iterator.has_next()); +} + +} // namespace diff --git a/app/tree_node_model.h b/app/tree_node_model.h new file mode 100644 index 0000000..69d5c8d --- /dev/null +++ b/app/tree_node_model.h @@ -0,0 +1,279 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef APP_TREE_NODE_MODEL_H_ +#define APP_TREE_NODE_MODEL_H_ + +#include +#include + +#include "app/tree_model.h" +#include "base/basictypes.h" +#include "base/scoped_ptr.h" +#include "base/scoped_vector.h" +#include "base/stl_util-inl.h" + +// TreeNodeModel and TreeNodes provide an implementation of TreeModel around +// TreeNodes. TreeNodes form a directed acyclic graph. +// +// TreeNodes own their children, so that deleting a node deletes all +// descendants. +// +// TreeNodes do NOT maintain a pointer back to the model. As such, if you +// are using TreeNodes with a TreeNodeModel you will need to notify the observer +// yourself any time you make any change directly to the TreeNodes. For example, +// if you directly invoke SetTitle on a node it does not notify the +// observer, you will need to do it yourself. This includes the following +// methods: SetTitle, Remove and Add. TreeNodeModel provides cover +// methods that mutate the TreeNodes and notify the observer. If you are using +// TreeNodes with a TreeNodeModel use the cover methods to save yourself the +// headache. +// +// The following example creates a TreeNode with two children and then +// creates a TreeNodeModel from it: +// +// TreeNodeWithValue root = new TreeNodeWithValue(0, L"root"); +// root.add(new TreeNodeWithValue(1, L"child 1")); +// root.add(new TreeNodeWithValue(1, L"child 2")); +// TreeNodeModel>* model = +// new TreeNodeModel>(root); +// +// Two variants of TreeNode are provided here: +// +// . TreeNode itself is intended for subclassing. It has one type parameter +// that corresponds to the type of the node. When subclassing use your class +// name as the type parameter, eg: +// class MyTreeNode : public TreeNode . +// . TreeNodeWithValue is a trivial subclass of TreeNode that has one type +// type parameter: a value type that is associated with the node. +// +// Which you use depends upon the situation. If you want to subclass and add +// methods, then use TreeNode. If you don't need any extra methods and just +// want to associate a value with each node, then use TreeNodeWithValue. +// +// Regardless of which TreeNode you use, if you are using the nodes with a +// TreeView take care to notify the observer when mutating the nodes. + +template +class TreeNodeModel; + +// TreeNode ------------------------------------------------------------------- + +template +class TreeNode : public TreeModelNode { + public: + TreeNode() : parent_(NULL) { } + + explicit TreeNode(const std::wstring& title) + : title_(title), parent_(NULL) {} + + virtual ~TreeNode() { + } + + // Adds the specified child node. + virtual void Add(int index, NodeType* child) { + DCHECK(child && index >= 0 && index <= GetChildCount()); + // If the node has a parent, remove it from its parent. + NodeType* node_parent = child->GetParent(); + if (node_parent) + node_parent->Remove(node_parent->IndexOfChild(child)); + child->parent_ = static_cast(this); + children_->insert(children_->begin() + index, child); + } + + // Removes the node by index. This does NOT delete the specified node, it is + // up to the caller to delete it when done. + virtual NodeType* Remove(int index) { + DCHECK(index >= 0 && index < GetChildCount()); + NodeType* node = GetChild(index); + node->parent_ = NULL; + children_->erase(index + children_->begin()); + return node; + } + + // Removes all the children from this node. This does NOT delete the nodes. + void RemoveAll() { + for (size_t i = 0; i < children_->size(); ++i) + children_[i]->parent_ = NULL; + children_->clear(); + } + + // Returns the number of children. + int GetChildCount() { + return static_cast(children_->size()); + } + + // Returns a child by index. + NodeType* GetChild(int index) { + DCHECK(index >= 0 && index < GetChildCount()); + return children_[index]; + } + + // Returns the parent. + NodeType* GetParent() { + return parent_; + } + + // Returns the index of the specified child, or -1 if node is a not a child. + int IndexOfChild(const NodeType* node) { + DCHECK(node); + typename std::vector::iterator i = + std::find(children_->begin(), children_->end(), node); + if (i != children_->end()) + return static_cast(i - children_->begin()); + return -1; + } + + // Sets the title of the node. + void SetTitle(const std::wstring& string) { + title_ = string; + } + + // Returns the title of the node. + std::wstring GetTitle() { + return title_; + } + + // Returns true if this is the root. + bool IsRoot() { return (parent_ == NULL); } + + // Returns true if this == ancestor, or one of this nodes parents is + // ancestor. + bool HasAncestor(NodeType* ancestor) const { + if (ancestor == this) + return true; + if (!ancestor) + return false; + return parent_ ? parent_->HasAncestor(ancestor) : false; + } + + protected: + std::vector& children() { return children_.get(); } + + private: + // Title displayed in the tree. + std::wstring title_; + + NodeType* parent_; + + // Children. + ScopedVector children_; + + DISALLOW_COPY_AND_ASSIGN(TreeNode); +}; + +// TreeNodeWithValue ---------------------------------------------------------- + +template +class TreeNodeWithValue : public TreeNode< TreeNodeWithValue > { + private: + typedef TreeNode< TreeNodeWithValue > ParentType; + + public: + TreeNodeWithValue() { } + + TreeNodeWithValue(const ValueType& value) + : ParentType(std::wstring()), value(value) { } + + TreeNodeWithValue(const std::wstring& title, const ValueType& value) + : ParentType(title), value(value) { } + + ValueType value; + + private: + DISALLOW_COPY_AND_ASSIGN(TreeNodeWithValue); +}; + +// TreeNodeModel -------------------------------------------------------------- + +// TreeModel implementation intended to be used with TreeNodes. +template +class TreeNodeModel : public TreeModel { + public: + // Creates a TreeNodeModel with the specified root node. The root is owned + // by the TreeNodeModel. + explicit TreeNodeModel(NodeType* root) + : root_(root), + observer_(NULL) { + } + + virtual ~TreeNodeModel() {} + + virtual void SetObserver(TreeModelObserver* observer) { + observer_ = observer; + } + + TreeModelObserver* GetObserver() { + return observer_; + } + + // TreeModel methods, all forward to the nodes. + virtual NodeType* GetRoot() { return root_.get(); } + + virtual int GetChildCount(TreeModelNode* parent) { + DCHECK(parent); + return AsNode(parent)->GetChildCount(); + } + + virtual NodeType* GetChild(TreeModelNode* parent, int index) { + DCHECK(parent); + return AsNode(parent)->GetChild(index); + } + + virtual TreeModelNode* GetParent(TreeModelNode* node) { + DCHECK(node); + return AsNode(node)->GetParent(); + } + + NodeType* AsNode(TreeModelNode* model_node) { + return reinterpret_cast(model_node); + } + + // Sets the title of the specified node. + virtual void SetTitle(TreeModelNode* node, + const std::wstring& title) { + DCHECK(node); + AsNode(node)->SetTitle(title); + NotifyObserverTreeNodeChanged(node); + } + + void Add(NodeType* parent, int index, NodeType* child) { + DCHECK(parent && child); + parent->Add(index, child); + NotifyObserverTreeNodesAdded(parent, index, 1); + } + + NodeType* Remove(NodeType* parent, int index) { + DCHECK(parent); + NodeType* child = parent->Remove(index); + NotifyObserverTreeNodesRemoved(parent, index, 1); + return child; + } + + void NotifyObserverTreeNodesAdded(NodeType* parent, int start, int count) { + if (observer_) + observer_->TreeNodesAdded(this, parent, start, count); + } + + void NotifyObserverTreeNodesRemoved(NodeType* parent, int start, int count) { + if (observer_) + observer_->TreeNodesRemoved(this, parent, start, count); + } + + virtual void NotifyObserverTreeNodeChanged(TreeModelNode* node) { + if (observer_) + observer_->TreeNodeChanged(this, node); + } + + private: + // The root. + scoped_ptr root_; + + // The observer. + TreeModelObserver* observer_; + + DISALLOW_COPY_AND_ASSIGN(TreeNodeModel); +}; + +#endif // APP_TREE_NODE_MODEL_H_ diff --git a/chrome/browser/automation/automation_provider.h b/chrome/browser/automation/automation_provider.h index de84e86..f7aa80f 100644 --- a/chrome/browser/automation/automation_provider.h +++ b/chrome/browser/automation/automation_provider.h @@ -27,7 +27,9 @@ #include "chrome/common/ipc_sync_channel.h" #include "chrome/common/notification_observer.h" #include "chrome/test/automation/automation_messages.h" +#if defined(OS_WIN) #include "views/event.h" +#endif // defined(OS_WIN) #if defined(OS_WIN) // TODO(port): enable these. diff --git a/chrome/browser/bookmarks/bookmark_folder_tree_model.cc b/chrome/browser/bookmarks/bookmark_folder_tree_model.cc index 3f1530f..ad03b61 100644 --- a/chrome/browser/bookmarks/bookmark_folder_tree_model.cc +++ b/chrome/browser/bookmarks/bookmark_folder_tree_model.cc @@ -10,7 +10,7 @@ #include "grit/theme_resources.h" BookmarkFolderTreeModel::BookmarkFolderTreeModel(BookmarkModel* model) - : views::TreeNodeModel(new FolderNode(NULL)), + : TreeNodeModel(new FolderNode(NULL)), model_(model), recently_bookmarked_node_(new FolderNode(NULL)), search_node_(new FolderNode(NULL)){ @@ -29,7 +29,7 @@ BookmarkFolderTreeModel::~BookmarkFolderTreeModel() { } BookmarkFolderTreeModel::NodeType BookmarkFolderTreeModel::GetNodeType( - views::TreeModelNode* node) { + TreeModelNode* node) { if (node == recently_bookmarked_node_) return RECENTLY_BOOKMARKED; if (node == search_node_) @@ -48,7 +48,7 @@ FolderNode* BookmarkFolderTreeModel::GetFolderNodeForBookmarkNode( } BookmarkNode* BookmarkFolderTreeModel::TreeNodeAsBookmarkNode( - views::TreeModelNode* node) { + TreeModelNode* node) { if (GetNodeType(node) != BOOKMARK) return NULL; return AsNode(node)->value; @@ -170,7 +170,7 @@ void BookmarkFolderTreeModel::GetIcons(std::vector* icons) { icons->push_back(*rb.GetBitmapNamed(IDR_BOOKMARK_MANAGER_SEARCH_ICON)); } -int BookmarkFolderTreeModel::GetIconIndex(views::TreeModelNode* node) { +int BookmarkFolderTreeModel::GetIconIndex(TreeModelNode* node) { if (node == recently_bookmarked_node_) return 0; if (node == search_node_) diff --git a/chrome/browser/bookmarks/bookmark_folder_tree_model.h b/chrome/browser/bookmarks/bookmark_folder_tree_model.h index ce62ed0..4faff4e 100644 --- a/chrome/browser/bookmarks/bookmark_folder_tree_model.h +++ b/chrome/browser/bookmarks/bookmark_folder_tree_model.h @@ -5,16 +5,18 @@ #ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_FOLDER_TREE_MODEL_H_ #define CHROME_BROWSER_BOOKMARKS_BOOKMARK_FOLDER_TREE_MODEL_H_ +#include + +#include "app/tree_node_model.h" #include "chrome/browser/bookmarks/bookmark_model.h" -#include "views/controls/tree/tree_node_model.h" // The type of nodes created by BookmarkFolderTreeModel. -typedef views::TreeNodeWithValue FolderNode; +typedef TreeNodeWithValue FolderNode; // TreeModel implementation that shows the folders from the BookmarkModel. // The root node contains the following nodes: // bookmark bar, other folders, recently bookmarked and search. -class BookmarkFolderTreeModel : public views::TreeNodeModel, +class BookmarkFolderTreeModel : public TreeNodeModel, public BookmarkModelObserver { public: // Type of the node. @@ -30,19 +32,19 @@ class BookmarkFolderTreeModel : public views::TreeNodeModel, ~BookmarkFolderTreeModel(); // The tree is not editable. - virtual void SetTitle(views::TreeModelNode* node, const std::wstring& title) { + virtual void SetTitle(TreeModelNode* node, const std::wstring& title) { NOTREACHED(); } // Returns the type of the specified node. - NodeType GetNodeType(views::TreeModelNode* node); + NodeType GetNodeType(TreeModelNode* node); // Returns the FolderNode for the specified BookmarkNode. FolderNode* GetFolderNodeForBookmarkNode(BookmarkNode* node); // Converts the tree node into a BookmarkNode. Returns NULL if |node| is NULL // or not of NodeType::BOOKMARK. - BookmarkNode* TreeNodeAsBookmarkNode(views::TreeModelNode* node); + BookmarkNode* TreeNodeAsBookmarkNode(TreeModelNode* node); // Returns the search node. FolderNode* search_node() const { return search_node_; } @@ -73,7 +75,7 @@ class BookmarkFolderTreeModel : public views::TreeNodeModel, // The following are overriden to return custom icons for the recently // bookmarked and search nodes. virtual void GetIcons(std::vector* icons); - virtual int GetIconIndex(views::TreeModelNode* node); + virtual int GetIconIndex(TreeModelNode* node); private: // Invoked from the constructor to create the children of the root node. diff --git a/chrome/browser/bookmarks/bookmark_folder_tree_model_unittest.cc b/chrome/browser/bookmarks/bookmark_folder_tree_model_unittest.cc index 0fe6ce9..ce50564 100644 --- a/chrome/browser/bookmarks/bookmark_folder_tree_model_unittest.cc +++ b/chrome/browser/bookmarks/bookmark_folder_tree_model_unittest.cc @@ -22,7 +22,7 @@ // a1 // g1 class BookmarkFolderTreeModelTest : public testing::Test, - public views::TreeModelObserver { + public TreeModelObserver { public: BookmarkFolderTreeModelTest() : url1_("http://1"), @@ -61,27 +61,27 @@ class BookmarkFolderTreeModelTest : public testing::Test, return profile_->GetBookmarkModel(); } - virtual void TreeNodesAdded(views::TreeModel* model, - views::TreeModelNode* parent, + virtual void TreeNodesAdded(TreeModel* model, + TreeModelNode* parent, int start, int count) { added_count_++; } - virtual void TreeNodesRemoved(views::TreeModel* model, - views::TreeModelNode* parent, + virtual void TreeNodesRemoved(TreeModel* model, + TreeModelNode* parent, int start, int count) { removed_count_++; } - virtual void TreeNodeChanged(views::TreeModel* model, - views::TreeModelNode* node) { + virtual void TreeNodeChanged(TreeModel* model, + TreeModelNode* node) { changed_count_++; } - virtual void TreeNodeChildrenReordered(views::TreeModel* model, - views::TreeModelNode* parent) { + virtual void TreeNodeChildrenReordered(TreeModel* model, + TreeModelNode* parent) { reordered_count_++; } @@ -118,7 +118,7 @@ class BookmarkFolderTreeModelTest : public testing::Test, // and other folders matches the initial state. TEST_F(BookmarkFolderTreeModelTest, InitialState) { // Verify the first 4 nodes. - views::TreeModelNode* root = model_->GetRoot(); + TreeModelNode* root = model_->GetRoot(); ASSERT_EQ(4, model_->GetChildCount(root)); EXPECT_EQ(BookmarkFolderTreeModel::BOOKMARK, model_->GetNodeType(model_->GetChild(root, 0))); @@ -130,7 +130,7 @@ TEST_F(BookmarkFolderTreeModelTest, InitialState) { model_->GetNodeType(model_->GetChild(root, 3))); // Verify the contents of the bookmark bar node. - views::TreeModelNode* bb_node = model_->GetChild(root, 0); + TreeModelNode* bb_node = model_->GetChild(root, 0); EXPECT_TRUE(model_->TreeNodeAsBookmarkNode(bb_node) == bookmark_model()->GetBookmarkBarNode()); ASSERT_EQ(1, model_->GetChildCount(bb_node)); @@ -138,7 +138,7 @@ TEST_F(BookmarkFolderTreeModelTest, InitialState) { bookmark_model()->GetBookmarkBarNode()->GetChild(1)); // Verify the contents of the other folders node. - views::TreeModelNode* other_node = model_->GetChild(root, 1); + TreeModelNode* other_node = model_->GetChild(root, 1); EXPECT_TRUE(model_->TreeNodeAsBookmarkNode(other_node) == bookmark_model()->other_node()); ASSERT_EQ(1, model_->GetChildCount(other_node)); diff --git a/chrome/browser/bookmarks/bookmark_model.h b/chrome/browser/bookmarks/bookmark_model.h index ac32fc0..f605dec 100644 --- a/chrome/browser/bookmarks/bookmark_model.h +++ b/chrome/browser/bookmarks/bookmark_model.h @@ -10,6 +10,7 @@ #include #include +#include "app/tree_node_model.h" #include "base/lock.h" #include "base/observer_list.h" #include "base/waitable_event.h" @@ -22,7 +23,6 @@ #include "googleurl/src/gurl.h" #include "third_party/skia/include/core/SkBitmap.h" #include "testing/gtest/include/gtest/gtest_prod.h" -#include "views/controls/tree/tree_node_model.h" class BookmarkEditorView; class BookmarkModel; @@ -38,7 +38,7 @@ class StarredURLDatabase; // BookmarkNode contains information about a starred entry: title, URL, favicon, // star id and type. BookmarkNodes are returned from a BookmarkModel. // -class BookmarkNode : public views::TreeNode { +class BookmarkNode : public TreeNode { friend class BookmarkModel; friend class BookmarkCodec; friend class history::StarredURLDatabase; diff --git a/chrome/browser/bookmarks/bookmark_model_unittest.cc b/chrome/browser/bookmarks/bookmark_model_unittest.cc index 8c1b7c2..e2f8571 100644 --- a/chrome/browser/bookmarks/bookmark_model_unittest.cc +++ b/chrome/browser/bookmarks/bookmark_model_unittest.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "app/tree_node_model.h" #include "base/string_util.h" #include "chrome/browser/bookmarks/bookmark_codec.h" #include "chrome/browser/bookmarks/bookmark_model.h" @@ -12,7 +13,6 @@ #include "chrome/common/notification_service.h" #include "chrome/test/testing_profile.h" #include "testing/gtest/include/gtest/gtest.h" -#include "views/controls/tree/tree_node_model.h" using base::Time; using base::TimeDelta; @@ -467,7 +467,7 @@ TEST_F(BookmarkModelTest, NotifyURLsStarred) { namespace { // See comment in PopulateNodeFromString. -typedef views::TreeNodeWithValue TestNode; +typedef TreeNodeWithValue TestNode; // Does the work of PopulateNodeFromString. index gives the index of the current // element in description to process. diff --git a/chrome/browser/bookmarks/bookmark_utils.cc b/chrome/browser/bookmarks/bookmark_utils.cc index e21e230..8082f16 100644 --- a/chrome/browser/bookmarks/bookmark_utils.cc +++ b/chrome/browser/bookmarks/bookmark_utils.cc @@ -12,6 +12,7 @@ #else #include "chrome/common/temp_scaffolding_stubs.h" #endif +#include "app/tree_node_iterator.h" #include "base/basictypes.h" #include "base/string_util.h" #include "base/time.h" @@ -28,7 +29,6 @@ #include "chrome/common/pref_service.h" #include "grit/chromium_strings.h" #include "grit/generated_resources.h" -#include "views/controls/tree/tree_node_iterator.h" #include "views/event.h" using base::Time; @@ -417,7 +417,7 @@ std::vector GetMostRecentlyModifiedGroups( BookmarkModel* model, size_t max_count) { std::vector nodes; - views::TreeNodeIterator iterator(model->root_node()); + TreeNodeIterator iterator(model->root_node()); while (iterator.has_next()) { BookmarkNode* parent = iterator.Next(); if (parent->is_folder() && parent->date_group_modified() > base::Time()) { @@ -455,7 +455,7 @@ std::vector GetMostRecentlyModifiedGroups( void GetMostRecentlyAddedEntries(BookmarkModel* model, size_t count, std::vector* nodes) { - views::TreeNodeIterator iterator(model->root_node()); + TreeNodeIterator iterator(model->root_node()); while (iterator.has_next()) { BookmarkNode* node = iterator.Next(); if (node->is_url()) { @@ -481,7 +481,7 @@ void GetBookmarksMatchingText(BookmarkModel* model, if (query_nodes.empty()) return; - views::TreeNodeIterator iterator(model->root_node()); + TreeNodeIterator iterator(model->root_node()); Snippet::MatchPositions match_position; while (iterator.has_next()) { BookmarkNode* node = iterator.Next(); @@ -511,7 +511,7 @@ void GetBookmarksContainingText(BookmarkModel* model, if (words.empty()) return; - views::TreeNodeIterator iterator(model->root_node()); + TreeNodeIterator iterator(model->root_node()); while (iterator.has_next()) { BookmarkNode* node = iterator.Next(); if (node->is_url() && DoesBookmarkContainWords(node, words)) { diff --git a/chrome/browser/history/starred_url_database.h b/chrome/browser/history/starred_url_database.h index 6b91d69..7c2925c 100644 --- a/chrome/browser/history/starred_url_database.h +++ b/chrome/browser/history/starred_url_database.h @@ -8,12 +8,12 @@ #include #include +#include "app/tree_node_model.h" #include "base/basictypes.h" #include "base/file_path.h" #include "chrome/browser/history/history_types.h" #include "chrome/browser/history/url_database.h" #include "testing/gtest/include/gtest/gtest_prod.h" -#include "views/controls/tree/tree_node_model.h" struct sqlite3; class SqliteStatementCache; @@ -115,7 +115,7 @@ class StarredURLDatabase : public URLDatabase { StarID CreateStarredEntry(StarredEntry* entry); // Used when checking integrity of starred table. - typedef views::TreeNodeWithValue StarredNode; + typedef TreeNodeWithValue StarredNode; // Returns the max group id, or 0 if there is an error. UIStarID GetMaxGroupID(); diff --git a/chrome/browser/views/bookmark_editor_view.cc b/chrome/browser/views/bookmark_editor_view.cc index 692f72d..0aee8bc 100644 --- a/chrome/browser/views/bookmark_editor_view.cc +++ b/chrome/browser/views/bookmark_editor_view.cc @@ -31,7 +31,6 @@ using views::GridLayout; using views::Label; using views::NativeButton; using views::TextField; -using views::TreeNode; // Background color of text field when URL is invalid. static const SkColor kErrorColor = SkColorSetRGB(0xFF, 0xBC, 0xBC); @@ -162,7 +161,7 @@ void BookmarkEditorView::OnTreeViewSelectionChanged( } bool BookmarkEditorView::CanEdit(views::TreeView* tree_view, - views::TreeModelNode* node) { + TreeModelNode* node) { // Only allow editting of children of the bookmark bar node and other node. EditorNode* bb_node = tree_model_->AsNode(node); return (bb_node->GetParent() && bb_node->GetParent()->GetParent()); diff --git a/chrome/browser/views/bookmark_editor_view.h b/chrome/browser/views/bookmark_editor_view.h index e7cba63..4ca3fde 100644 --- a/chrome/browser/views/bookmark_editor_view.h +++ b/chrome/browser/views/bookmark_editor_view.h @@ -7,12 +7,12 @@ #include +#include "app/tree_node_model.h" #include "chrome/browser/bookmarks/bookmark_editor.h" #include "chrome/browser/bookmarks/bookmark_model.h" #include "views/controls/button/button.h" #include "views/controls/menu/menu.h" #include "views/controls/text_field.h" -#include "views/controls/tree/tree_node_model.h" #include "views/controls/tree/tree_view.h" #include "views/window/dialog_delegate.h" @@ -78,8 +78,7 @@ class BookmarkEditorView : public BookmarkEditor, // TreeViewObserver methods. virtual void OnTreeViewSelectionChanged(views::TreeView* tree_view); - virtual bool CanEdit(views::TreeView* tree_view, - views::TreeModelNode* node); + virtual bool CanEdit(views::TreeView* tree_view, TreeModelNode* node); // TextField::Controller methods. virtual void ContentsChanged(views::TextField* sender, @@ -113,16 +112,16 @@ class BookmarkEditorView : public BookmarkEditor, private: // Type of node in the tree. - typedef views::TreeNodeWithValue EditorNode; + typedef TreeNodeWithValue EditorNode; // Model for the TreeView. Trivial subclass that doesn't allow titles with // empty strings. - class EditorTreeModel : public views::TreeNodeModel { + class EditorTreeModel : public TreeNodeModel { public: explicit EditorTreeModel(EditorNode* root) : TreeNodeModel(root) {} - virtual void SetTitle(views::TreeModelNode* node, + virtual void SetTitle(TreeModelNode* node, const std::wstring& title) { if (!title.empty()) TreeNodeModel::SetTitle(node, title); diff --git a/chrome/browser/views/bookmark_folder_tree_view.cc b/chrome/browser/views/bookmark_folder_tree_view.cc index e092670..d71071b 100644 --- a/chrome/browser/views/bookmark_folder_tree_view.cc +++ b/chrome/browser/views/bookmark_folder_tree_view.cc @@ -77,7 +77,7 @@ int BookmarkFolderTreeView::OnPerformDrop(const views::DropTargetEvent& event) { } BookmarkNode* BookmarkFolderTreeView::GetSelectedBookmarkNode() { - views::TreeModelNode* selected_node = GetSelectedNode(); + TreeModelNode* selected_node = GetSelectedNode(); if (!selected_node) return NULL; return TreeNodeAsBookmarkNode(folder_model()->AsNode(selected_node)); @@ -141,7 +141,7 @@ BookmarkFolderTreeView::DropPosition BookmarkFolderTreeView:: RECT bounds; TreeView_GetItemRect(hwnd, item, &bounds, TRUE); if (y < bounds.bottom) { - views::TreeModelNode* model_node = GetNodeForTreeItem(item); + TreeModelNode* model_node = GetNodeForTreeItem(item); if (folder_model()->GetNodeType(model_node) != BookmarkFolderTreeModel::BOOKMARK) { // Only allow drops on bookmark nodes. diff --git a/chrome/browser/views/bookmark_manager_view.cc b/chrome/browser/views/bookmark_manager_view.cc index d635932..ed8774b 100644 --- a/chrome/browser/views/bookmark_manager_view.cc +++ b/chrome/browser/views/bookmark_manager_view.cc @@ -388,7 +388,7 @@ void BookmarkManagerView::OnKeyDown(unsigned short virtual_keycode) { void BookmarkManagerView::OnTreeViewSelectionChanged( views::TreeView* tree_view) { - views::TreeModelNode* node = tree_view_->GetSelectedNode(); + TreeModelNode* node = tree_view_->GetSelectedNode(); BookmarkTableModel* new_table_model = NULL; BookmarkNode* table_parent_node = NULL; diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index 5f5bab8..3dba3a5 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -215,7 +215,7 @@ '../app/throb_animation.cc', '../app/throb_animation.h', '../app/win_util.cc', - '../app/win_util.h', + '../app/win_util.h', ], 'direct_dependent_settings': { 'include_dirs': [ @@ -1897,6 +1897,9 @@ 'app/keystone_glue.h', 'app/keystone_glue.m', 'app/scoped_ole_initializer.h', + 'app/tree_model.h', + 'app/tree_node_iterator.h', + 'app/tree_node_model.h', ], 'mac_bundle_resources': [ 'app/nibs/en.lproj/BrowserWindow.xib', @@ -2798,6 +2801,7 @@ 'common/pref_service_unittest.cc', 'common/property_bag_unittest.cc', 'common/resource_dispatcher_unittest.cc', + '../app/tree_node_iterator_unittest.cc', 'common/time_format_unittest.cc', 'common/unzip_unittest.cc', '../app/win_util_unittest.cc', @@ -2823,7 +2827,6 @@ 'test/v8_unit_test.h', '../views/controls/label_unittest.cc', '../views/controls/table/table_view_unittest.cc', - '../views/controls/tree/tree_node_iterator_unittest.cc', '../views/focus/focus_manager_unittest.cc', '../views/grid_layout_unittest.cc', '../views/view_unittest.cc', @@ -3235,9 +3238,6 @@ '../views/controls/text_field.h', '../views/controls/throbber.cc', '../views/controls/throbber.h', - '../views/controls/tree/tree_model.h', - '../views/controls/tree/tree_node_iterator.h', - '../views/controls/tree/tree_node_model.h', '../views/controls/tree/tree_view.cc', '../views/controls/tree/tree_view.h', '../views/drag_utils.cc', diff --git a/chrome/test/unit/unittests.vcproj b/chrome/test/unit/unittests.vcproj index b3fa491..025ce8b 100644 --- a/chrome/test/unit/unittests.vcproj +++ b/chrome/test/unit/unittests.vcproj @@ -1076,10 +1076,6 @@ > - - @@ -1132,6 +1128,10 @@ > + + - -#include "base/logging.h" - -class SkBitmap; - -namespace views { - -class TreeModel; - -// TreeModelNode -------------------------------------------------------------- - -// Type of class returned from the model. -class TreeModelNode { - public: - // Returns the title for the node. - virtual std::wstring GetTitle() = 0; -}; - -// Observer for the TreeModel. Notified of significant events to the model. -class TreeModelObserver { - public: - // Notification that nodes were added to the specified parent. - virtual void TreeNodesAdded(TreeModel* model, - TreeModelNode* parent, - int start, - int count) = 0; - - // Notification that nodes were removed from the specified parent. - virtual void TreeNodesRemoved(TreeModel* model, - TreeModelNode* parent, - int start, - int count) = 0; - - // Notification the children of |parent| have been reordered. Note, only - // the direct children of |parent| have been reordered, not descendants. - virtual void TreeNodeChildrenReordered(TreeModel* model, - TreeModelNode* parent) = 0; - - // Notification that the contents of a node has changed. - virtual void TreeNodeChanged(TreeModel* model, TreeModelNode* node) = 0; -}; - -// TreeModel ------------------------------------------------------------------ - -// The model for TreeView. -class TreeModel { - public: - // Returns the root of the tree. This may or may not be shown in the tree, - // see SetRootShown for details. - virtual TreeModelNode* GetRoot() = 0; - - // Returns the number of children in the specified node. - virtual int GetChildCount(TreeModelNode* parent) = 0; - - // Returns the child node at the specified index. - virtual TreeModelNode* GetChild(TreeModelNode* parent, int index) = 0; - - // Returns the parent of a node, or NULL if node is the root. - virtual TreeModelNode* GetParent(TreeModelNode* node) = 0; - - // Sets the observer of the model. - virtual void SetObserver(TreeModelObserver* observer) = 0; - - // Sets the title of the specified node. - // This is only invoked if the node is editable and the user edits a node. - virtual void SetTitle(TreeModelNode* node, - const std::wstring& title) { - NOTREACHED(); - } - - // Returns the set of icons for the nodes in the tree. You only need override - // this if you don't want to use the default folder icons. - virtual void GetIcons(std::vector* icons) {} - - // Returns the index of the icon to use for |node|. Return -1 to use the - // default icon. The index is relative to the list of icons returned from - // GetIcons. - virtual int GetIconIndex(TreeModelNode* node) { return -1; } -}; - -} // namespace views - -#endif // VIEWS_CONTROLS_TREE_TREE_MODEL_H_ diff --git a/views/controls/tree/tree_node_iterator.h b/views/controls/tree/tree_node_iterator.h deleted file mode 100644 index 76618e7..0000000 --- a/views/controls/tree/tree_node_iterator.h +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef VIEWS_CONTROLS_TREE_TREE_NODE_ITERATOR_H_ -#define VIEWS_CONTROLS_TREE_TREE_NODE_ITERATOR_H_ - -#include - -#include "base/basictypes.h" -#include "base/logging.h" - -namespace views { - -// Iterator that iterates over the descendants of a node. The iteration does -// not include the node itself, only the descendants. The following illustrates -// typical usage: -// while (iterator.has_next()) { -// Node* node = iterator.Next(); -// // do something with node. -// } -template -class TreeNodeIterator { - public: - explicit TreeNodeIterator(NodeType* node) { - if (node->GetChildCount() > 0) - positions_.push(Position(node, 0)); - } - - // Returns true if there are more descendants. - bool has_next() const { return !positions_.empty(); } - - // Returns the next descendant. - NodeType* Next() { - if (!has_next()) { - NOTREACHED(); - return NULL; - } - - NodeType* result = positions_.top().node->GetChild(positions_.top().index); - - // Make sure we don't attempt to visit result again. - positions_.top().index++; - - // Iterate over result's children. - positions_.push(Position(result, 0)); - - // Advance to next position. - while (!positions_.empty() && positions_.top().index >= - positions_.top().node->GetChildCount()) { - positions_.pop(); - } - - return result; - } - - private: - template - struct Position { - Position(PositionNodeType* node, int index) : node(node), index(index) {} - Position() : node(NULL), index(-1) {} - - PositionNodeType* node; - int index; - }; - - std::stack > positions_; - - DISALLOW_COPY_AND_ASSIGN(TreeNodeIterator); -}; - -} // namespace views - -#endif // VIEWS_CONTROLS_TREE_TREE_NODE_ITERATOR_H_ diff --git a/views/controls/tree/tree_node_iterator_unittest.cc b/views/controls/tree/tree_node_iterator_unittest.cc deleted file mode 100644 index e5353fc..0000000 --- a/views/controls/tree/tree_node_iterator_unittest.cc +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "testing/gtest/include/gtest/gtest.h" - -#include "views/controls/tree/tree_node_iterator.h" -#include "views/controls/tree/tree_node_model.h" - -typedef testing::Test TreeNodeIteratorTest; - -using views::TreeNodeWithValue; - -TEST_F(TreeNodeIteratorTest, Test) { - TreeNodeWithValue root; - root.Add(0, new TreeNodeWithValue(1)); - root.Add(1, new TreeNodeWithValue(2)); - TreeNodeWithValue* f3 = new TreeNodeWithValue(3); - root.Add(2, f3); - TreeNodeWithValue* f4 = new TreeNodeWithValue(4); - f3->Add(0, f4); - f4->Add(0, new TreeNodeWithValue(5)); - - views::TreeNodeIterator > iterator(&root); - ASSERT_TRUE(iterator.has_next()); - ASSERT_EQ(root.GetChild(0), iterator.Next()); - - ASSERT_TRUE(iterator.has_next()); - ASSERT_EQ(root.GetChild(1), iterator.Next()); - - ASSERT_TRUE(iterator.has_next()); - ASSERT_EQ(root.GetChild(2), iterator.Next()); - - ASSERT_TRUE(iterator.has_next()); - ASSERT_EQ(f4, iterator.Next()); - - ASSERT_TRUE(iterator.has_next()); - ASSERT_EQ(f4->GetChild(0), iterator.Next()); - - ASSERT_FALSE(iterator.has_next()); -} diff --git a/views/controls/tree/tree_node_model.h b/views/controls/tree/tree_node_model.h deleted file mode 100644 index f1e1c16..0000000 --- a/views/controls/tree/tree_node_model.h +++ /dev/null @@ -1,283 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef VIEWS_CONTROLS_TREE_TREE_NODE_MODEL_H_ -#define VIEWS_CONTROLS_TREE_TREE_NODE_MODEL_H_ - -#include -#include - -#include "base/basictypes.h" -#include "base/scoped_ptr.h" -#include "base/scoped_vector.h" -#include "base/stl_util-inl.h" -#include "views/controls/tree/tree_model.h" - -namespace views { - -// TreeNodeModel and TreeNodes provide an implementation of TreeModel around -// TreeNodes. TreeNodes form a directed acyclic graph. -// -// TreeNodes own their children, so that deleting a node deletes all -// descendants. -// -// TreeNodes do NOT maintain a pointer back to the model. As such, if you -// are using TreeNodes with a TreeNodeModel you will need to notify the observer -// yourself any time you make any change directly to the TreeNodes. For example, -// if you directly invoke SetTitle on a node it does not notify the -// observer, you will need to do it yourself. This includes the following -// methods: SetTitle, Remove and Add. TreeNodeModel provides cover -// methods that mutate the TreeNodes and notify the observer. If you are using -// TreeNodes with a TreeNodeModel use the cover methods to save yourself the -// headache. -// -// The following example creates a TreeNode with two children and then -// creates a TreeNodeModel from it: -// -// TreeNodeWithValue root = new TreeNodeWithValue(0, L"root"); -// root.add(new TreeNodeWithValue(1, L"child 1")); -// root.add(new TreeNodeWithValue(1, L"child 2")); -// TreeNodeModel>* model = -// new TreeNodeModel>(root); -// -// Two variants of TreeNode are provided here: -// -// . TreeNode itself is intended for subclassing. It has one type parameter -// that corresponds to the type of the node. When subclassing use your class -// name as the type parameter, eg: -// class MyTreeNode : public TreeNode . -// . TreeNodeWithValue is a trivial subclass of TreeNode that has one type -// type parameter: a value type that is associated with the node. -// -// Which you use depends upon the situation. If you want to subclass and add -// methods, then use TreeNode. If you don't need any extra methods and just -// want to associate a value with each node, then use TreeNodeWithValue. -// -// Regardless of which TreeNode you use, if you are using the nodes with a -// TreeView take care to notify the observer when mutating the nodes. - -template -class TreeNodeModel; - -// TreeNode ------------------------------------------------------------------- - -template -class TreeNode : public TreeModelNode { - public: - TreeNode() : parent_(NULL) { } - - explicit TreeNode(const std::wstring& title) - : title_(title), parent_(NULL) {} - - virtual ~TreeNode() { - } - - // Adds the specified child node. - virtual void Add(int index, NodeType* child) { - DCHECK(child && index >= 0 && index <= GetChildCount()); - // If the node has a parent, remove it from its parent. - NodeType* node_parent = child->GetParent(); - if (node_parent) - node_parent->Remove(node_parent->IndexOfChild(child)); - child->parent_ = static_cast(this); - children_->insert(children_->begin() + index, child); - } - - // Removes the node by index. This does NOT delete the specified node, it is - // up to the caller to delete it when done. - virtual NodeType* Remove(int index) { - DCHECK(index >= 0 && index < GetChildCount()); - NodeType* node = GetChild(index); - node->parent_ = NULL; - children_->erase(index + children_->begin()); - return node; - } - - // Removes all the children from this node. This does NOT delete the nodes. - void RemoveAll() { - for (size_t i = 0; i < children_->size(); ++i) - children_[i]->parent_ = NULL; - children_->clear(); - } - - // Returns the number of children. - int GetChildCount() { - return static_cast(children_->size()); - } - - // Returns a child by index. - NodeType* GetChild(int index) { - DCHECK(index >= 0 && index < GetChildCount()); - return children_[index]; - } - - // Returns the parent. - NodeType* GetParent() { - return parent_; - } - - // Returns the index of the specified child, or -1 if node is a not a child. - int IndexOfChild(const NodeType* node) { - DCHECK(node); - typename std::vector::iterator i = - std::find(children_->begin(), children_->end(), node); - if (i != children_->end()) - return static_cast(i - children_->begin()); - return -1; - } - - // Sets the title of the node. - void SetTitle(const std::wstring& string) { - title_ = string; - } - - // Returns the title of the node. - std::wstring GetTitle() { - return title_; - } - - // Returns true if this is the root. - bool IsRoot() { return (parent_ == NULL); } - - // Returns true if this == ancestor, or one of this nodes parents is - // ancestor. - bool HasAncestor(NodeType* ancestor) const { - if (ancestor == this) - return true; - if (!ancestor) - return false; - return parent_ ? parent_->HasAncestor(ancestor) : false; - } - - protected: - std::vector& children() { return children_.get(); } - - private: - // Title displayed in the tree. - std::wstring title_; - - NodeType* parent_; - - // Children. - ScopedVector children_; - - DISALLOW_COPY_AND_ASSIGN(TreeNode); -}; - -// TreeNodeWithValue ---------------------------------------------------------- - -template -class TreeNodeWithValue : public TreeNode< TreeNodeWithValue > { - private: - typedef TreeNode< TreeNodeWithValue > ParentType; - - public: - TreeNodeWithValue() { } - - TreeNodeWithValue(const ValueType& value) - : ParentType(std::wstring()), value(value) { } - - TreeNodeWithValue(const std::wstring& title, const ValueType& value) - : ParentType(title), value(value) { } - - ValueType value; - - private: - DISALLOW_COPY_AND_ASSIGN(TreeNodeWithValue); -}; - -// TreeNodeModel -------------------------------------------------------------- - -// TreeModel implementation intended to be used with TreeNodes. -template -class TreeNodeModel : public TreeModel { - public: - // Creates a TreeNodeModel with the specified root node. The root is owned - // by the TreeNodeModel. - explicit TreeNodeModel(NodeType* root) - : root_(root), - observer_(NULL) { - } - - virtual ~TreeNodeModel() {} - - virtual void SetObserver(TreeModelObserver* observer) { - observer_ = observer; - } - - TreeModelObserver* GetObserver() { - return observer_; - } - - // TreeModel methods, all forward to the nodes. - virtual NodeType* GetRoot() { return root_.get(); } - - virtual int GetChildCount(TreeModelNode* parent) { - DCHECK(parent); - return AsNode(parent)->GetChildCount(); - } - - virtual NodeType* GetChild(TreeModelNode* parent, int index) { - DCHECK(parent); - return AsNode(parent)->GetChild(index); - } - - virtual TreeModelNode* GetParent(TreeModelNode* node) { - DCHECK(node); - return AsNode(node)->GetParent(); - } - - NodeType* AsNode(TreeModelNode* model_node) { - return reinterpret_cast(model_node); - } - - // Sets the title of the specified node. - virtual void SetTitle(TreeModelNode* node, - const std::wstring& title) { - DCHECK(node); - AsNode(node)->SetTitle(title); - NotifyObserverTreeNodeChanged(node); - } - - void Add(NodeType* parent, int index, NodeType* child) { - DCHECK(parent && child); - parent->Add(index, child); - NotifyObserverTreeNodesAdded(parent, index, 1); - } - - NodeType* Remove(NodeType* parent, int index) { - DCHECK(parent); - NodeType* child = parent->Remove(index); - NotifyObserverTreeNodesRemoved(parent, index, 1); - return child; - } - - void NotifyObserverTreeNodesAdded(NodeType* parent, int start, int count) { - if (observer_) - observer_->TreeNodesAdded(this, parent, start, count); - } - - void NotifyObserverTreeNodesRemoved(NodeType* parent, int start, int count) { - if (observer_) - observer_->TreeNodesRemoved(this, parent, start, count); - } - - virtual void NotifyObserverTreeNodeChanged(TreeModelNode* node) { - if (observer_) - observer_->TreeNodeChanged(this, node); - } - - private: - // The root. - scoped_ptr root_; - - // The observer. - TreeModelObserver* observer_; - - DISALLOW_COPY_AND_ASSIGN(TreeNodeModel); -}; - -} // namespace views - -#endif // VIEWS_CONTROLS_TREE_TREE_NODE_MODEL_H_ diff --git a/views/controls/tree/tree_view.h b/views/controls/tree/tree_view.h index 1ded058..4523916 100644 --- a/views/controls/tree/tree_view.h +++ b/views/controls/tree/tree_view.h @@ -7,10 +7,10 @@ #include +#include "app/tree_model.h" #include "base/basictypes.h" #include "base/logging.h" #include "views/controls/native_control.h" -#include "views/controls/tree/tree_model.h" namespace views { diff --git a/views/views.vcproj b/views/views.vcproj index 096e269..0c336a6 100644 --- a/views/views.vcproj +++ b/views/views.vcproj @@ -716,18 +716,6 @@ Name="tree" > - - - - - - -- cgit v1.1