summaryrefslogtreecommitdiffstats
path: root/chrome/common/gtk_tree.h
blob: 4e74efafc9ee173878e04b6be33646882de6e636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2009 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 CHROME_COMMON_GTK_TREE_H_
#define CHROME_COMMON_GTK_TREE_H_

#include <gtk/gtk.h>

#include "app/table_model_observer.h"
#include "base/basictypes.h"

class TableModel;

namespace gtk_tree {

// Get the row number corresponding to |path|.
gint GetRowNumForPath(GtkTreePath* path);

// Get the row number corresponding to |iter|.
gint GetRowNumForIter(GtkTreeModel* model, GtkTreeIter* iter);

// Get the row number in the child tree model corresponding to |sort_path| in
// the parent tree model.
gint GetTreeSortChildRowNumForPath(GtkTreeModel* sort_model,
                                   GtkTreePath* sort_path);

// Select the given row by number.
void SelectAndFocusRowNum(int row, GtkTreeView* tree_view);

// A helper class for populating a GtkListStore from a TableModel.
class ModelAdapter : public TableModelObserver {
 public:
  class Delegate {
   public:
    // Should fill in the column and row.
    virtual void SetColumnValues(int row, GtkTreeIter* iter) = 0;

    // Called after any change to the TableModel.  Overriding optional.
    virtual void OnAnyModelUpdate() {}

    // When the TableModel has been completely changed, called by OnModelChanged
    // after clearing the list store.  Can be overriden by the delegate if it
    // needs to do extra initialization before the list store is populated.
    virtual void OnModelChanged() {}

   protected:
    ~Delegate() {}
  };

  // |table_model| may be NULL.
  explicit ModelAdapter(Delegate* delegate, GtkListStore* list_store,
                        TableModel* table_model);
  virtual ~ModelAdapter() {}

  // Replace the TableModel with a different one.  If the list store currenty
  // has items this would cause weirdness, so this should generally only be
  // called during the Delegate's OnModelChanged call, or if the adapter was
  // created with a NULL |table_model|.
  void SetModel(TableModel* table_model);

  // TableModelObserver implementation.
  virtual void OnModelChanged();
  virtual void OnItemsChanged(int start, int length);
  virtual void OnItemsAdded(int start, int length);
  virtual void OnItemsRemoved(int start, int length);

 private:
  // Add the values from |row| of the TableModel.
  void AddNodeToList(int row);

  Delegate* delegate_;
  GtkListStore* list_store_;
  TableModel* table_model_;

  DISALLOW_COPY_AND_ASSIGN(ModelAdapter);
};

}  // namespace gtk_tree

#endif  // CHROME_COMMON_GTK_TREE_H_