gimpintstore.c 10.4 KB
Newer Older
1 2
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3 4
 *
 * gimpintstore.c
5
 * Copyright (C) 2004-2007  Sven Neumann <sven@gimp.org>
6
 *
7
 * This library is free software: you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 3 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
19
 * <https://www.gnu.org/licenses/>.
20 21 22 23
 */

#include "config.h"

24 25
#include <string.h>

26 27 28 29 30 31
#include <gtk/gtk.h>

#include "gimpwidgetstypes.h"

#include "gimpintstore.h"

32 33 34
#include "libgimp/libgimp-intl.h"


35 36 37 38 39 40 41 42 43 44
/**
 * SECTION: gimpintstore
 * @title: GimpIntStore
 * @short_description: A model for integer based name-value pairs
 *                     (e.g. enums)
 *
 * A model for integer based name-value pairs (e.g. enums)
 **/


45 46 47 48 49 50
enum
{
  PROP_0,
  PROP_USER_DATA_TYPE
};

51 52

struct _GimpIntStorePrivate
53
{
54 55
  GtkTreeIter *empty_iter;
  GType        user_data_type;
56 57 58
};

#define GET_PRIVATE(obj) (((GimpIntStore *) (obj))->priv)
59 60


61
static void  gimp_int_store_tree_model_init (GtkTreeModelIface *iface);
62

63
static void  gimp_int_store_constructed     (GObject           *object);
64
static void  gimp_int_store_finalize        (GObject           *object);
65 66 67 68 69 70 71 72 73
static void  gimp_int_store_set_property    (GObject           *object,
                                             guint              property_id,
                                             const GValue      *value,
                                             GParamSpec        *pspec);
static void  gimp_int_store_get_property    (GObject           *object,
                                             guint              property_id,
                                             GValue            *value,
                                             GParamSpec        *pspec);

74 75 76
static void  gimp_int_store_row_inserted    (GtkTreeModel      *model,
                                             GtkTreePath       *path,
                                             GtkTreeIter       *iter);
77 78
static void  gimp_int_store_row_deleted     (GtkTreeModel      *model,
                                             GtkTreePath       *path);
79
static void  gimp_int_store_add_empty       (GimpIntStore      *store);
80

81

82
G_DEFINE_TYPE_WITH_CODE (GimpIntStore, gimp_int_store, GTK_TYPE_LIST_STORE,
83
                         G_ADD_PRIVATE (GimpIntStore)
84
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
85
                                                gimp_int_store_tree_model_init))
86

87
#define parent_class gimp_int_store_parent_class
88

89
static GtkTreeModelIface *parent_iface = NULL;
90 91


92 93 94 95 96
static void
gimp_int_store_class_init (GimpIntStoreClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

97
  object_class->constructed  = gimp_int_store_constructed;
98 99 100 101 102 103 104
  object_class->finalize     = gimp_int_store_finalize;
  object_class->set_property = gimp_int_store_set_property;
  object_class->get_property = gimp_int_store_get_property;

  /**
   * GimpIntStore:user-data-type:
   *
105
   * Sets the #GType for the GIMP_INT_STORE_USER_DATA column.
106 107 108 109 110
   *
   * You need to set this property when constructing the store if you want
   * to use the GIMP_INT_STORE_USER_DATA column and want to have the store
   * handle ref-counting of your user data.
   *
111
   * Since: 2.4
112 113 114 115
   */
  g_object_class_install_property (object_class,
                                   PROP_USER_DATA_TYPE,
                                   g_param_spec_gtype ("user-data-type",
116 117
                                                       "User Data Type",
                                                       "The GType of the user_data column",
118 119 120
                                                       G_TYPE_NONE,
                                                       G_PARAM_CONSTRUCT_ONLY |
                                                       GIMP_PARAM_READWRITE));
121 122
}

123 124 125 126 127 128
static void
gimp_int_store_tree_model_init (GtkTreeModelIface *iface)
{
  parent_iface = g_type_interface_peek_parent (iface);

  iface->row_inserted = gimp_int_store_row_inserted;
129
  iface->row_deleted  = gimp_int_store_row_deleted;
130 131
}

132 133 134
static void
gimp_int_store_init (GimpIntStore *store)
{
135
  store->priv = gimp_int_store_get_instance_private (store);
136 137
}

138 139
static void
gimp_int_store_constructed (GObject *object)
140
{
141
  GimpIntStore        *store = GIMP_INT_STORE (object);
142
  GimpIntStorePrivate *priv  = GET_PRIVATE (store);
143 144
  GType                types[GIMP_INT_STORE_NUM_COLUMNS];

145
  G_OBJECT_CLASS (parent_class)->constructed (object);
146 147 148

  types[GIMP_INT_STORE_VALUE]     = G_TYPE_INT;
  types[GIMP_INT_STORE_LABEL]     = G_TYPE_STRING;
149
  types[GIMP_INT_STORE_ICON_NAME] = G_TYPE_STRING;
150
  types[GIMP_INT_STORE_PIXBUF]    = GDK_TYPE_PIXBUF;
151 152
  types[GIMP_INT_STORE_USER_DATA] = (priv->user_data_type != G_TYPE_NONE ?
                                     priv->user_data_type : G_TYPE_POINTER);
153
  types[GIMP_INT_STORE_ABBREV]    = G_TYPE_STRING;
154

155 156
  gtk_list_store_set_column_types (GTK_LIST_STORE (store),
                                   GIMP_INT_STORE_NUM_COLUMNS, types);
157 158

  gimp_int_store_add_empty (store);
159 160
}

161 162 163
static void
gimp_int_store_finalize (GObject *object)
{
164
  GimpIntStorePrivate *priv = GET_PRIVATE (object);
165

166
  g_clear_pointer (&priv->empty_iter, gtk_tree_iter_free);
167 168 169 170

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

171 172 173 174 175 176
static void
gimp_int_store_set_property (GObject      *object,
                             guint         property_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
177
  GimpIntStorePrivate *priv = GET_PRIVATE (object);
178 179 180 181 182 183

  switch (property_id)
    {
    case PROP_USER_DATA_TYPE:
      priv->user_data_type = g_value_get_gtype (value);
      break;
184

185 186 187 188 189 190 191 192 193 194 195 196
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_int_store_get_property (GObject    *object,
                             guint       property_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
197
  GimpIntStorePrivate *priv = GET_PRIVATE (object);
198 199 200 201 202 203

  switch (property_id)
    {
    case PROP_USER_DATA_TYPE:
      g_value_set_gtype (value, priv->user_data_type);
      break;
204

205 206 207 208 209 210
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

211 212
static void
gimp_int_store_row_inserted (GtkTreeModel *model,
Sven Neumann's avatar
Sven Neumann committed
213 214
                             GtkTreePath  *path,
                             GtkTreeIter  *iter)
215
{
216 217
  GimpIntStore        *store = GIMP_INT_STORE (model);
  GimpIntStorePrivate *priv  = GET_PRIVATE (store);
218 219 220 221

  if (parent_iface->row_inserted)
    parent_iface->row_inserted (model, path, iter);

222 223
  if (priv->empty_iter &&
      memcmp (iter, priv->empty_iter, sizeof (GtkTreeIter)))
224
    {
225 226 227
      gtk_list_store_remove (GTK_LIST_STORE (store), priv->empty_iter);
      gtk_tree_iter_free (priv->empty_iter);
      priv->empty_iter = NULL;
228 229 230 231 232 233 234 235 236
    }
}

static void
gimp_int_store_row_deleted (GtkTreeModel *model,
                            GtkTreePath  *path)
{
  if (parent_iface->row_deleted)
    parent_iface->row_deleted (model, path);
237 238 239 240 241
}

static void
gimp_int_store_add_empty (GimpIntStore *store)
{
242 243
  GimpIntStorePrivate *priv = GET_PRIVATE (store);
  GtkTreeIter          iter = { 0, };
244

245
  g_return_if_fail (priv->empty_iter == NULL);
246

247 248
  gtk_list_store_prepend (GTK_LIST_STORE (store), &iter);
  gtk_list_store_set (GTK_LIST_STORE (store), &iter,
249
                      GIMP_INT_STORE_VALUE, -1,
250 251 252
                      /* This string appears in an empty menu as in
                       * "nothing selected and nothing to select"
                       */
253 254
                      GIMP_INT_STORE_LABEL, (_("(Empty)")),
                      -1);
255

256
  priv->empty_iter = gtk_tree_iter_copy (&iter);
257
}
258 259 260 261 262 263 264 265 266 267

/**
 * gimp_int_store_new:
 *
 * Creates a #GtkListStore with a number of useful columns.
 * #GimpIntStore is especially useful if the items you want to store
 * are identified using an integer value.
 *
 * Return value: a new #GimpIntStore.
 *
268
 * Since: 2.2
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
 **/
GtkListStore *
gimp_int_store_new (void)
{
  return g_object_new (GIMP_TYPE_INT_STORE, NULL);
}

/**
 * gimp_int_store_lookup_by_value:
 * @model: a #GimpIntStore
 * @value: an integer value to lookup in the @model
 * @iter:  return location for the iter of the given @value
 *
 * Iterate over the @model looking for @value.
 *
 * Return value: %TRUE if the value has been located and @iter is
 *               valid, %FALSE otherwise.
286
 *
287
 * Since: 2.2
288 289 290
 **/
gboolean
gimp_int_store_lookup_by_value (GtkTreeModel *model,
291 292
                                gint          value,
                                GtkTreeIter  *iter)
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
{
  gboolean  iter_valid;

  g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
  g_return_val_if_fail (iter != NULL, FALSE);

  for (iter_valid = gtk_tree_model_get_iter_first (model, iter);
       iter_valid;
       iter_valid = gtk_tree_model_iter_next (model, iter))
    {
      gint  this;

      gtk_tree_model_get (model, iter,
                          GIMP_INT_STORE_VALUE, &this,
                          -1);
      if (this == value)
        break;
    }

  return iter_valid;
}
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

/**
 * gimp_int_store_lookup_by_user_data:
 * @model: a #GimpIntStore
 * @user_data: a gpointer "user-data" to lookup in the @model
 * @iter:  return location for the iter of the given @user_data
 *
 * Iterate over the @model looking for @user_data.
 *
 * Return value: %TRUE if the user-data has been located and @iter is
 *               valid, %FALSE otherwise.
 *
 * Since: 2.10
 **/
gboolean
gimp_int_store_lookup_by_user_data (GtkTreeModel *model,
                                    gpointer      user_data,
                                    GtkTreeIter  *iter)
{
  gboolean iter_valid = FALSE;

  g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
  g_return_val_if_fail (iter != NULL, FALSE);

  for (iter_valid = gtk_tree_model_get_iter_first (model, iter);
       iter_valid;
       iter_valid = gtk_tree_model_iter_next (model, iter))
    {
      gpointer this;

      gtk_tree_model_get (model, iter,
                          GIMP_INT_STORE_USER_DATA, &this,
                          -1);
      if (this == user_data)
        break;
    }

  return (gboolean) iter_valid;
}