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