Home | History | Annotate | Download | only in gtk
      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 
      8 #include <gtk/gtk.h>
      9 
     10 #include <set>
     11 #include <vector>
     12 
     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"
     18 
     19 namespace ui {
     20 class TableModel;
     21 }
     22 
     23 namespace gtk_tree {
     24 
     25 // Get the row number corresponding to |path|.
     26 gint GetRowNumForPath(GtkTreePath* path);
     27 
     28 // Get the row number corresponding to |iter|.
     29 gint GetRowNumForIter(GtkTreeModel* model, GtkTreeIter* iter);
     30 
     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);
     35 
     36 // Select the given row by number.
     37 void SelectAndFocusRowNum(int row, GtkTreeView* tree_view);
     38 
     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
     42 // valid.
     43 bool RemoveRecursively(GtkTreeStore* tree_store, GtkTreeIter* iter);
     44 
     45 // Writes all the indexes of selected rows into |out|.
     46 void GetSelectedIndices(GtkTreeSelection* selection, std::set<int>* out);
     47 
     48 // A helper class for populating a GtkListStore from a ui::TableModel.
     49 class TableAdapter : public ui::TableModelObserver {
     50  public:
     51 
     52   enum ColumnID {
     53     COL_TITLE = 0,
     54     COL_IS_HEADER,
     55     COL_IS_SEPARATOR,
     56     COL_GROUP_ID,
     57     COL_WEIGHT,
     58     COL_WEIGHT_SET,
     59     COL_LAST_ID
     60   };
     61 
     62   class Delegate {
     63    public:
     64     // Should fill in the column and row.
     65     virtual void SetColumnValues(int row, GtkTreeIter* iter) = 0;
     66 
     67     // Called after any change to the ui::TableModel but before the
     68     // corresponding change to the GtkListStore.
     69     virtual void OnAnyModelUpdateStart() {}
     70 
     71     // Called after any change to the ui::TableModel.
     72     virtual void OnAnyModelUpdate() {}
     73 
     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
     77     // populated.
     78     virtual void OnModelChanged() {}
     79 
     80    protected:
     81     virtual ~Delegate() {}
     82   };
     83 
     84   // |table_model| may be NULL.
     85   TableAdapter(Delegate* delegate,
     86                GtkListStore* list_store,
     87                ui::TableModel* table_model);
     88   virtual ~TableAdapter() {}
     89 
     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);
     95 
     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);
     99 
    100   // GtkTreeModel callbacks:
    101   // Callback checking whether a row should be drawn as a separator.
    102   static gboolean OnCheckRowIsSeparator(GtkTreeModel* model,
    103                                         GtkTreeIter* iter,
    104                                         gpointer user_data);
    105 
    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,
    109                                     GtkTreeModel* model,
    110                                     GtkTreePath* path,
    111                                     gboolean path_currently_selected,
    112                                     gpointer user_data);
    113 
    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;
    119 
    120  private:
    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;
    124 
    125   // Return the index into the list store for the given model row.
    126   int GetListStoreIndexForModelRow(int model_row) const;
    127 
    128   // Add the values from |row| of the ui::TableModel.
    129   void AddNodeToList(int row);
    130 
    131   Delegate* delegate_;
    132   GtkListStore* list_store_;
    133   ui::TableModel* table_model_;
    134 
    135   DISALLOW_COPY_AND_ASSIGN(TableAdapter);
    136 };
    137 
    138 // A helper class for populating a GtkTreeStore from a TreeModel.
    139 // TODO(mattm): support SetRootShown(true)
    140 class TreeAdapter : public ui::TreeModelObserver {
    141  public:
    142   // Column ids for |tree_store_|.
    143   enum {
    144     COL_ICON,
    145     COL_TITLE,
    146     COL_NODE_PTR,
    147     COL_COUNT,
    148   };
    149 
    150   class Delegate {
    151    public:
    152     // Called after any change to the TreeModel but before the corresponding
    153     // change to the GtkTreeStore.
    154     virtual void OnAnyModelUpdateStart() {}
    155 
    156     // Called after any change to the GtkTreeStore.
    157     virtual void OnAnyModelUpdate() {}
    158 
    159    protected:
    160     virtual ~Delegate() {}
    161   };
    162 
    163   TreeAdapter(Delegate* delegate, ui::TreeModel* tree_model);
    164   virtual ~TreeAdapter();
    165 
    166   // Populate the tree store from the |tree_model_|.
    167   void Init();
    168 
    169   // Return the tree store.
    170   GtkTreeStore* tree_store() { return tree_store_; }
    171 
    172   // Get the TreeModelNode corresponding to iter in the tree store.
    173   ui::TreeModelNode* GetNode(GtkTreeIter* iter);
    174 
    175   // Begin TreeModelObserver implementation.
    176   virtual void TreeNodesAdded(ui::TreeModel* model,
    177                               ui::TreeModelNode* parent,
    178                               int start,
    179                               int count) OVERRIDE;
    180   virtual void TreeNodesRemoved(ui::TreeModel* model,
    181                                 ui::TreeModelNode* parent,
    182                                 int start,
    183                                 int count) OVERRIDE;
    184   virtual void TreeNodeChanged(ui::TreeModel* model,
    185                                ui::TreeModelNode* node) OVERRIDE;
    186   // End TreeModelObserver implementation.
    187 
    188  private:
    189   // Fill the tree store values for a given node.
    190   void FillRow(GtkTreeIter* iter, ui::TreeModelNode* node);
    191 
    192   // Fill the tree store for a row and all its descendants.
    193   void Fill(GtkTreeIter* parent_iter, ui::TreeModelNode* parent_node);
    194 
    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);
    198 
    199   // Get the GtkTreeIter in the tree store for the given node.
    200   bool GetTreeIter(ui::TreeModelNode* node, GtkTreeIter* iter);
    201 
    202   Delegate* delegate_;
    203   GtkTreeStore* tree_store_;
    204   ui::TreeModel* tree_model_;
    205   std::vector<GdkPixbuf*> pixbufs_;
    206 
    207   DISALLOW_COPY_AND_ASSIGN(TreeAdapter);
    208 };
    209 
    210 }  // namespace gtk_tree
    211 
    212 #endif  // CHROME_BROWSER_UI_GTK_GTK_TREE_H_
    213