5 #define YUILogComponent "gtk" 6 #include <yui/Libyui_config.h> 9 #include <YSelectionWidget.h> 11 #include "YGSelectionStore.h" 13 static inline int getYItemCol (GtkTreeModel *model)
14 {
return gtk_tree_model_get_n_columns (model) - 2; }
16 static inline int getRowIdCol (GtkTreeModel *model)
17 {
return gtk_tree_model_get_n_columns (model) - 1; }
19 YGSelectionStore::YGSelectionStore (
bool tree)
20 : isTree (tree), m_nextRowId (0)
23 YGSelectionStore::~YGSelectionStore()
24 { g_object_unref (G_OBJECT (m_model)); }
26 void YGSelectionStore::createStore (
int cols,
const GType types[])
30 for (
int i = 0; i < cols; i++)
32 _types[cols+0] = G_TYPE_POINTER;
33 _types[cols+1] = G_TYPE_POINTER;
36 m_model = GTK_TREE_MODEL (gtk_tree_store_newv (_cols, _types));
38 m_model = GTK_TREE_MODEL (gtk_list_store_newv (_cols, _types));
41 void YGSelectionStore::addRow (YItem *item, GtkTreeIter *iter, GtkTreeIter *parent)
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);
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);
55 item->setData (m_nextRowId);
56 m_nextRowId = GINT_TO_POINTER (GPOINTER_TO_INT (m_nextRowId) + 1);
59 void YGSelectionStore::setRowText (GtkTreeIter *iter,
int iconCol,
const std::string &icon,
int labelCol,
const std::string &label,
const YSelectionWidget *widget)
61 GdkPixbuf *pixbuf = 0;
63 std::string path (widget->iconFullPath (icon));
64 pixbuf = YGUtils::loadPixbuf (path);
68 gtk_tree_store_set (getTreeStore(), iter, iconCol, pixbuf,
69 labelCol, label.c_str(), -1);
71 gtk_list_store_set (getListStore(), iter, iconCol, pixbuf,
72 labelCol, label.c_str(), -1);
75 void YGSelectionStore::setRowMark (GtkTreeIter *iter,
int markCol,
bool mark)
78 gtk_tree_store_set (getTreeStore(), iter, markCol, mark, -1);
80 gtk_list_store_set (getListStore(), iter, markCol, mark, -1);
83 void YGSelectionStore::doDeleteAllItems()
86 gtk_tree_store_clear (getTreeStore());
88 gtk_list_store_clear (getListStore());
92 YItem *YGSelectionStore::getYItem (GtkTreeIter *iter)
95 gtk_tree_model_get (m_model, iter, getYItemCol (m_model), &ptr, -1);
99 static GtkTreeIter *found_iter;
101 void YGSelectionStore::getTreeIter (
const YItem *item, GtkTreeIter *iter)
104 static gboolean foreach_find (
105 GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer find_id)
108 gtk_tree_model_get (model, iter, getRowIdCol (model), &
id, -1);
110 found_iter = gtk_tree_iter_copy (iter);
118 gtk_tree_model_foreach (m_model, inner::foreach_find, item->data());
120 gtk_tree_iter_free (found_iter);
123 GtkListStore *YGSelectionStore::getListStore()
124 {
return GTK_LIST_STORE (m_model); }
126 GtkTreeStore *YGSelectionStore::getTreeStore()
127 {
return GTK_TREE_STORE (m_model); }
129 bool YGSelectionStore::isEmpty()
132 return !gtk_tree_model_get_iter_first (m_model, &iter);
135 int YGSelectionStore::getTreeDepth()
138 static gboolean foreach_max_depth (
139 GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer _depth)
141 int *depth = (
int *) _depth;
142 *depth = MAX (*depth, gtk_tree_path_get_depth (path));
148 gtk_tree_model_foreach (m_model, inner::foreach_max_depth, &depth);
155 bool YGSelectionStore::findLabel (
int labelCol,
const std::string &label, GtkTreeIter *iter)
158 static gboolean foreach_find (
159 GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer _value)
161 gchar *value = (gchar *) _value;
163 gtk_tree_model_get (model, iter, find_col, &v, -1);
164 if (!strcmp (v, value)) {
165 found_iter = gtk_tree_iter_copy (iter);
174 gtk_tree_model_foreach (m_model, inner::foreach_find, (gpointer) label.c_str());
177 gtk_tree_iter_free (found_iter);
179 return found_iter != NULL;