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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_UI_GTK_GTK_TREE_H_
6 #define CHROME_BROWSER_UI_GTK_GTK_TREE_H_
13 #include "base/basictypes.h"
14 #include "base/compiler_specific.h"
15 #include "chrome/browser/remove_rows_table_model.h"
16 #include "ui/base/models/table_model_observer.h"
17 #include "ui/base/models/tree_model.h"
25 // Get the row number corresponding to |path|.
26 gint GetRowNumForPath(GtkTreePath* path);
28 // Get the row number corresponding to |iter|.
29 gint GetRowNumForIter(GtkTreeModel* model, GtkTreeIter* iter);
31 // Get the row number in the child tree model corresponding to |sort_path| in
32 // the parent tree model.
33 gint GetTreeSortChildRowNumForPath(GtkTreeModel* sort_model,
34 GtkTreePath* sort_path);
36 // Select the given row by number.
37 void SelectAndFocusRowNum(int row, GtkTreeView* tree_view);
39 // Remove the row and all its children from the |tree_store|. If there is a
40 // following row, |iter| will be updated to point to the it and the return value
41 // will be true, otherwise the return will be false and |iter| is no longer
43 bool RemoveRecursively(GtkTreeStore* tree_store, GtkTreeIter* iter);
45 // Writes all the indexes of selected rows into |out|.
46 void GetSelectedIndices(GtkTreeSelection* selection, std::set<int>* out);
48 // A helper class for populating a GtkListStore from a ui::TableModel.
49 class TableAdapter : public ui::TableModelObserver {
64 // Should fill in the column and row.
65 virtual void SetColumnValues(int row, GtkTreeIter* iter) = 0;
67 // Called after any change to the ui::TableModel but before the
68 // corresponding change to the GtkListStore.
69 virtual void OnAnyModelUpdateStart() {}
71 // Called after any change to the ui::TableModel.
72 virtual void OnAnyModelUpdate() {}
74 // When the ui::TableModel has been completely changed, called by
75 // OnModelChanged after clearing the list store. Can be overridden by the
76 // delegate if it needs to do extra initialization before the list store is
78 virtual void OnModelChanged() {}
81 virtual ~Delegate() {}
84 // |table_model| may be NULL.
85 TableAdapter(Delegate* delegate,
86 GtkListStore* list_store,
87 ui::TableModel* table_model);
88 virtual ~TableAdapter() {}
90 // Replace the ui::TableModel with a different one. If the list store
91 // currently has items this would cause weirdness, so this should generally
92 // only be called during the Delegate's OnModelChanged call, or if the adapter
93 // was created with a NULL |table_model|.
94 void SetModel(ui::TableModel* table_model);
96 // Add all model rows corresponding to the given list store indices to |rows|.
97 void MapListStoreIndicesToModelRows(const std::set<int>& list_store_indices,
98 RemoveRowsTableModel::Rows* model_rows);
100 // GtkTreeModel callbacks:
101 // Callback checking whether a row should be drawn as a separator.
102 static gboolean OnCheckRowIsSeparator(GtkTreeModel* model,
106 // Callback checking whether a row may be selected. We use some rows in the
107 // table as headers/separators for the groups, which should not be selectable.
108 static gboolean OnSelectionFilter(GtkTreeSelection* selection,
111 gboolean path_currently_selected,
114 // ui::TableModelObserver implementation.
115 virtual void OnModelChanged() OVERRIDE;
116 virtual void OnItemsChanged(int start, int length) OVERRIDE;
117 virtual void OnItemsAdded(int start, int length) OVERRIDE;
118 virtual void OnItemsRemoved(int start, int length) OVERRIDE;
121 // Return whether the row pointed to by |iter| is a group row, i.e. a group
122 // header, or a separator.
123 bool IsGroupRow(GtkTreeIter* iter) const;
125 // Return the index into the list store for the given model row.
126 int GetListStoreIndexForModelRow(int model_row) const;
128 // Add the values from |row| of the ui::TableModel.
129 void AddNodeToList(int row);
132 GtkListStore* list_store_;
133 ui::TableModel* table_model_;
135 DISALLOW_COPY_AND_ASSIGN(TableAdapter);
138 // A helper class for populating a GtkTreeStore from a TreeModel.
139 // TODO(mattm): support SetRootShown(true)
140 class TreeAdapter : public ui::TreeModelObserver {
142 // Column ids for |tree_store_|.
152 // Called after any change to the TreeModel but before the corresponding
153 // change to the GtkTreeStore.
154 virtual void OnAnyModelUpdateStart() {}
156 // Called after any change to the GtkTreeStore.
157 virtual void OnAnyModelUpdate() {}
160 virtual ~Delegate() {}
163 TreeAdapter(Delegate* delegate, ui::TreeModel* tree_model);
164 virtual ~TreeAdapter();
166 // Populate the tree store from the |tree_model_|.
169 // Return the tree store.
170 GtkTreeStore* tree_store() { return tree_store_; }
172 // Get the TreeModelNode corresponding to iter in the tree store.
173 ui::TreeModelNode* GetNode(GtkTreeIter* iter);
175 // Begin TreeModelObserver implementation.
176 virtual void TreeNodesAdded(ui::TreeModel* model,
177 ui::TreeModelNode* parent,
180 virtual void TreeNodesRemoved(ui::TreeModel* model,
181 ui::TreeModelNode* parent,
184 virtual void TreeNodeChanged(ui::TreeModel* model,
185 ui::TreeModelNode* node) OVERRIDE;
186 // End TreeModelObserver implementation.
189 // Fill the tree store values for a given node.
190 void FillRow(GtkTreeIter* iter, ui::TreeModelNode* node);
192 // Fill the tree store for a row and all its descendants.
193 void Fill(GtkTreeIter* parent_iter, ui::TreeModelNode* parent_node);
195 // Get the GtkTreePath in the tree store for the given node.
196 // The returned path should be freed with gtk_tree_path_free.
197 GtkTreePath* GetTreePath(ui::TreeModelNode* node);
199 // Get the GtkTreeIter in the tree store for the given node.
200 bool GetTreeIter(ui::TreeModelNode* node, GtkTreeIter* iter);
203 GtkTreeStore* tree_store_;
204 ui::TreeModel* tree_model_;
205 std::vector<GdkPixbuf*> pixbufs_;
207 DISALLOW_COPY_AND_ASSIGN(TreeAdapter);
210 } // namespace gtk_tree
212 #endif // CHROME_BROWSER_UI_GTK_GTK_TREE_H_