libyui-gtk  2.49.0
YGSelectionStore.cc
1 /********************************************************************
2  * YaST2-GTK - http://en.opensuse.org/YaST2-GTK *
3  ********************************************************************/
4 
5 #define YUILogComponent "gtk"
6 #include <yui/Libyui_config.h>
7 #include <gtk/gtk.h>
8 #include <YItem.h>
9 #include <YSelectionWidget.h>
10 #include "YGUtils.h"
11 #include "YGSelectionStore.h"
12 
13 static inline int getYItemCol (GtkTreeModel *model)
14 { return gtk_tree_model_get_n_columns (model) - 2; }
15 
16 static inline int getRowIdCol (GtkTreeModel *model)
17 { return gtk_tree_model_get_n_columns (model) - 1; }
18 
19 YGSelectionStore::YGSelectionStore (bool tree)
20 : isTree (tree), m_nextRowId (0)
21 {}
22 
23 YGSelectionStore::~YGSelectionStore()
24 { g_object_unref (G_OBJECT (m_model)); }
25 
26 void YGSelectionStore::createStore (int cols, const GType types[])
27 {
28  int _cols = cols + 2;
29  GType _types[_cols];
30  for (int i = 0; i < cols; i++)
31  _types[i] = types[i];
32  _types[cols+0] = G_TYPE_POINTER; // pointer to YItem
33  _types[cols+1] = G_TYPE_POINTER; // some unique value identifying this row
34 
35  if (isTree)
36  m_model = GTK_TREE_MODEL (gtk_tree_store_newv (_cols, _types));
37  else
38  m_model = GTK_TREE_MODEL (gtk_list_store_newv (_cols, _types));
39 }
40 
41 void YGSelectionStore::addRow (YItem *item, GtkTreeIter *iter, GtkTreeIter *parent)
42 {
43  if (isTree) {
44  GtkTreeStore *store = GTK_TREE_STORE (m_model);
45  gtk_tree_store_append (store, iter, parent);
46  gtk_tree_store_set (store, iter, getYItemCol (m_model), item,
47  getRowIdCol (m_model), m_nextRowId, -1);
48  }
49  else {
50  GtkListStore *store = getListStore();
51  gtk_list_store_append (store, iter);
52  gtk_list_store_set (store, iter, getYItemCol (m_model), item,
53  getRowIdCol (m_model), m_nextRowId, -1);
54  }
55  item->setData (m_nextRowId);
56  m_nextRowId = GINT_TO_POINTER (GPOINTER_TO_INT (m_nextRowId) + 1);
57 }
58 
59 void YGSelectionStore::setRowText (GtkTreeIter *iter, int iconCol, const std::string &icon, int labelCol, const std::string &label, const YSelectionWidget *widget)
60 {
61  GdkPixbuf *pixbuf = 0;
62  if (!icon.empty()) {
63  std::string path (widget->iconFullPath (icon));
64  pixbuf = YGUtils::loadPixbuf (path);
65  }
66 
67  if (isTree)
68  gtk_tree_store_set (getTreeStore(), iter, iconCol, pixbuf,
69  labelCol, label.c_str(), -1);
70  else
71  gtk_list_store_set (getListStore(), iter, iconCol, pixbuf,
72  labelCol, label.c_str(), -1);
73 }
74 
75 void YGSelectionStore::setRowMark (GtkTreeIter *iter, int markCol, bool mark)
76 {
77  if (isTree)
78  gtk_tree_store_set (getTreeStore(), iter, markCol, mark, -1);
79  else
80  gtk_list_store_set (getListStore(), iter, markCol, mark, -1);
81 }
82 
83 void YGSelectionStore::doDeleteAllItems()
84 {
85  if (isTree)
86  gtk_tree_store_clear (getTreeStore());
87  else
88  gtk_list_store_clear (getListStore());
89  m_nextRowId = 0;
90 }
91 
92 YItem *YGSelectionStore::getYItem (GtkTreeIter *iter)
93 {
94  gpointer ptr;
95  gtk_tree_model_get (m_model, iter, getYItemCol (m_model), &ptr, -1);
96  return (YItem *) ptr;
97 }
98 
99 static GtkTreeIter *found_iter;
100 
101 void YGSelectionStore::getTreeIter (const YItem *item, GtkTreeIter *iter)
102 {
103  struct inner {
104  static gboolean foreach_find (
105  GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer find_id)
106  {
107  gpointer id;
108  gtk_tree_model_get (model, iter, getRowIdCol (model), &id, -1);
109  if (id == find_id) {
110  found_iter = gtk_tree_iter_copy (iter);
111  return TRUE;
112  }
113  return FALSE;
114  }
115  };
116 
117  found_iter = NULL;
118  gtk_tree_model_foreach (m_model, inner::foreach_find, item->data());
119  *iter = *found_iter;
120  gtk_tree_iter_free (found_iter);
121 }
122 
123 GtkListStore *YGSelectionStore::getListStore()
124 { return GTK_LIST_STORE (m_model); }
125 
126 GtkTreeStore *YGSelectionStore::getTreeStore()
127 { return GTK_TREE_STORE (m_model); }
128 
129 bool YGSelectionStore::isEmpty()
130 {
131  GtkTreeIter iter;
132  return !gtk_tree_model_get_iter_first (m_model, &iter);
133 }
134 
135 int YGSelectionStore::getTreeDepth()
136 {
137  struct inner {
138  static gboolean foreach_max_depth (
139  GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer _depth)
140  {
141  int *depth = (int *) _depth;
142  *depth = MAX (*depth, gtk_tree_path_get_depth (path));
143  return FALSE;
144  }
145  };
146 
147  int depth = 0;
148  gtk_tree_model_foreach (m_model, inner::foreach_max_depth, &depth);
149  return depth;
150 }
151 
152 #include <string.h>
153 static int find_col;
154 
155 bool YGSelectionStore::findLabel (int labelCol, const std::string &label, GtkTreeIter *iter)
156 {
157  struct inner {
158  static gboolean foreach_find (
159  GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer _value)
160  {
161  gchar *value = (gchar *) _value;
162  gchar *v;
163  gtk_tree_model_get (model, iter, find_col, &v, -1);
164  if (!strcmp (v, value)) {
165  found_iter = gtk_tree_iter_copy (iter);
166  return TRUE;
167  }
168  return FALSE;
169  }
170  };
171 
172  find_col = labelCol;
173  found_iter = NULL;
174  gtk_tree_model_foreach (m_model, inner::foreach_find, (gpointer) label.c_str());
175  if (found_iter) {
176  *iter = *found_iter;
177  gtk_tree_iter_free (found_iter);
178  }
179  return found_iter != NULL;
180 }
181