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. 4 5 #ifndef CHROME_BROWSER_UI_GTK_GTK_TREE_H_ 6 #define CHROME_BROWSER_UI_GTK_GTK_TREE_H_ 7 #pragma once 8 9 #include <gtk/gtk.h> 10 #include <set> 11 #include <vector> 12 13 #include "base/basictypes.h" 14 #include "chrome/browser/remove_rows_table_model.h" 15 #include "ui/base/models/table_model_observer.h" 16 #include "ui/base/models/tree_model.h" 17 18 namespace ui { 19 class TableModel; 20 } 21 22 namespace gtk_tree { 23 24 // Get the row number corresponding to |path|. 25 gint GetRowNumForPath(GtkTreePath* path); 26 27 // Get the row number corresponding to |iter|. 28 gint GetRowNumForIter(GtkTreeModel* model, GtkTreeIter* iter); 29 30 // Get the row number in the child tree model corresponding to |sort_path| in 31 // the parent tree model. 32 gint GetTreeSortChildRowNumForPath(GtkTreeModel* sort_model, 33 GtkTreePath* sort_path); 34 35 // Select the given row by number. 36 void SelectAndFocusRowNum(int row, GtkTreeView* tree_view); 37 38 // Remove the row and all its children from the |tree_store|. If there is a 39 // following row, |iter| will be updated to point to the it and the return value 40 // will be true, otherwise the return will be false and |iter| is no longer 41 // valid. 42 bool RemoveRecursively(GtkTreeStore* tree_store, GtkTreeIter* iter); 43 44 // Writes all the indexes of selected rows into |out|. 45 void GetSelectedIndices(GtkTreeSelection* selection, std::set<int>* out); 46 47 // A helper class for populating a GtkListStore from a ui::TableModel. 48 class TableAdapter : public ui::TableModelObserver { 49 public: 50 51 enum ColumnID { 52 COL_TITLE = 0, 53 COL_IS_HEADER, 54 COL_IS_SEPARATOR, 55 COL_GROUP_ID, 56 COL_WEIGHT, 57 COL_WEIGHT_SET, 58 COL_LAST_ID 59 }; 60 61 class Delegate { 62 public: 63 // Should fill in the column and row. 64 virtual void SetColumnValues(int row, GtkTreeIter* iter) = 0; 65 66 // Called after any change to the ui::TableModel but before the 67 // corresponding change to the GtkListStore. 68 virtual void OnAnyModelUpdateStart() {} 69 70 // Called after any change to the ui::TableModel. 71 virtual void OnAnyModelUpdate() {} 72 73 // When the ui::TableModel has been completely changed, called by 74 // OnModelChanged after clearing the list store. Can be overridden by the 75 // delegate if it needs to do extra initialization before the list store is 76 // populated. 77 virtual void OnModelChanged() {} 78 79 protected: 80 virtual ~Delegate() {} 81 }; 82 83 // |table_model| may be NULL. 84 TableAdapter(Delegate* delegate, 85 GtkListStore* list_store, 86 ui::TableModel* table_model); 87 virtual ~TableAdapter() {} 88 89 // Replace the ui::TableModel with a different one. If the list store 90 // currently has items this would cause weirdness, so this should generally 91 // only be called during the Delegate's OnModelChanged call, or if the adapter 92 // was created with a NULL |table_model|. 93 void SetModel(ui::TableModel* table_model); 94 95 // Add all model rows corresponding to the given list store indices to |rows|. 96 void MapListStoreIndicesToModelRows(const std::set<int>& list_store_indices, 97 RemoveRowsTableModel::Rows* model_rows); 98 99 // GtkTreeModel callbacks: 100 // Callback checking whether a row should be drawn as a separator. 101 static gboolean OnCheckRowIsSeparator(GtkTreeModel* model, 102 GtkTreeIter* iter, 103 gpointer user_data); 104 105 // Callback checking whether a row may be selected. We use some rows in the 106 // table as headers/separators for the groups, which should not be selectable. 107 static gboolean OnSelectionFilter(GtkTreeSelection* selection, 108 GtkTreeModel* model, 109 GtkTreePath* path, 110 gboolean path_currently_selected, 111 gpointer user_data); 112 113 // ui::TableModelObserver implementation. 114 virtual void OnModelChanged(); 115 virtual void OnItemsChanged(int start, int length); 116 virtual void OnItemsAdded(int start, int length); 117 virtual void OnItemsRemoved(int start, int length); 118 119 private: 120 // Return whether the row pointed to by |iter| is a group row, i.e. a group 121 // header, or a separator. 122 bool IsGroupRow(GtkTreeIter* iter) const; 123 124 // Return the index into the list store for the given model row. 125 int GetListStoreIndexForModelRow(int model_row) const; 126 127 // Add the values from |row| of the ui::TableModel. 128 void AddNodeToList(int row); 129 130 Delegate* delegate_; 131 GtkListStore* list_store_; 132 ui::TableModel* table_model_; 133 134 DISALLOW_COPY_AND_ASSIGN(TableAdapter); 135 }; 136 137 // A helper class for populating a GtkTreeStore from a TreeModel. 138 // TODO(mattm): support SetRootShown(true) 139 class TreeAdapter : public ui::TreeModelObserver { 140 public: 141 // Column ids for |tree_store_|. 142 enum { 143 COL_ICON, 144 COL_TITLE, 145 COL_NODE_PTR, 146 COL_COUNT, 147 }; 148 149 class Delegate { 150 public: 151 // Called after any change to the TreeModel but before the corresponding 152 // change to the GtkTreeStore. 153 virtual void OnAnyModelUpdateStart() {} 154 155 // Called after any change to the GtkTreeStore. 156 virtual void OnAnyModelUpdate() {} 157 158 protected: 159 virtual ~Delegate() {} 160 }; 161 162 TreeAdapter(Delegate* delegate, ui::TreeModel* tree_model); 163 virtual ~TreeAdapter(); 164 165 // Populate the tree store from the |tree_model_|. 166 void Init(); 167 168 // Return the tree store. 169 GtkTreeStore* tree_store() { return tree_store_; } 170 171 // Get the TreeModelNode corresponding to iter in the tree store. 172 ui::TreeModelNode* GetNode(GtkTreeIter* iter); 173 174 // Begin TreeModelObserver implementation. 175 virtual void TreeNodesAdded(ui::TreeModel* model, 176 ui::TreeModelNode* parent, 177 int start, 178 int count); 179 virtual void TreeNodesRemoved(ui::TreeModel* model, 180 ui::TreeModelNode* parent, 181 int start, 182 int count); 183 virtual void TreeNodeChanged(ui::TreeModel* model, ui::TreeModelNode* node); 184 // End TreeModelObserver implementation. 185 186 private: 187 // Fill the tree store values for a given node. 188 void FillRow(GtkTreeIter* iter, ui::TreeModelNode* node); 189 190 // Fill the tree store for a row and all its descendants. 191 void Fill(GtkTreeIter* parent_iter, ui::TreeModelNode* parent_node); 192 193 // Get the GtkTreePath in the tree store for the given node. 194 // The returned path should be freed with gtk_tree_path_free. 195 GtkTreePath* GetTreePath(ui::TreeModelNode* node); 196 197 // Get the GtkTreeIter in the tree store for the given node. 198 bool GetTreeIter(ui::TreeModelNode* node, GtkTreeIter* iter); 199 200 Delegate* delegate_; 201 GtkTreeStore* tree_store_; 202 ui::TreeModel* tree_model_; 203 std::vector<GdkPixbuf*> pixbufs_; 204 205 DISALLOW_COPY_AND_ASSIGN(TreeAdapter); 206 }; 207 208 } // namespace gtk_tree 209 210 #endif // CHROME_BROWSER_UI_GTK_GTK_TREE_H_ 211