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

#include "config.h"

25
#include <stdlib.h>
26
#include <string.h>
27

28
#include <gegl.h>
29 30 31 32 33 34
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "gimp.h"

Manish Singh's avatar
Manish Singh committed
35
#include "gimpuitypes.h"
Simon Budig's avatar
Simon Budig committed
36
#include "gimpitemcombobox.h"
37 38 39
#include "gimppixbuf.h"


40 41 42 43 44 45 46 47 48 49
/**
 * SECTION: gimpitemcombobox
 * @title: GimpItemComboBox
 * @short_description: Widgets providing popup menus of items.
 *
 * Widgets providing popup menus of items (layers, channels,
 * drawables, vectors).
 **/


50 51
#define THUMBNAIL_SIZE  24
#define WIDTH_REQUEST  200
52

Simon Budig's avatar
Simon Budig committed
53

54
#define GET_PRIVATE(obj) (g_object_get_data (G_OBJECT (obj), "gimp-item-combo-box-private"))
55 56


57
typedef struct _GimpItemComboBoxPrivate   GimpItemComboBoxPrivate;
58

59
struct _GimpItemComboBoxPrivate
60 61 62 63 64
{
  GimpItemConstraintFunc  constraint;
  gpointer                data;
};

65 66 67 68
typedef struct _GimpDrawableComboBoxClass GimpDrawableComboBoxClass;
typedef struct _GimpChannelComboBoxClass  GimpChannelComboBoxClass;
typedef struct _GimpLayerComboBoxClass    GimpLayerComboBoxClass;
typedef struct _GimpVectorsComboBoxClass  GimpVectorsComboBoxClass;
69

Manish Singh's avatar
Manish Singh committed
70 71
struct _GimpDrawableComboBox
{
72
  GimpIntComboBox  parent_instance;
Manish Singh's avatar
Manish Singh committed
73 74 75 76
};

struct _GimpDrawableComboBoxClass
{
77
  GimpIntComboBoxClass  parent_class;
Manish Singh's avatar
Manish Singh committed
78 79 80 81
};

struct _GimpChannelComboBox
{
82
  GimpIntComboBox  parent_instance;
Manish Singh's avatar
Manish Singh committed
83 84 85 86
};

struct _GimpChannelComboBoxClass
{
87
  GimpIntComboBoxClass  parent_class;
Manish Singh's avatar
Manish Singh committed
88 89 90 91
};

struct _GimpLayerComboBox
{
92
  GimpIntComboBox  parent_instance;
Manish Singh's avatar
Manish Singh committed
93 94 95 96
};

struct _GimpLayerComboBoxClass
{
97
  GimpIntComboBoxClass  parent_class;
Manish Singh's avatar
Manish Singh committed
98 99
};

Simon Budig's avatar
Simon Budig committed
100 101
struct _GimpVectorsComboBox
{
102
  GimpIntComboBox  parent_instance;
Simon Budig's avatar
Simon Budig committed
103 104 105 106
};

struct _GimpVectorsComboBoxClass
{
107
  GimpIntComboBoxClass  parent_class;
Simon Budig's avatar
Simon Budig committed
108 109
};

Manish Singh's avatar
Manish Singh committed
110

111
static GtkWidget * gimp_item_combo_box_new (GType                       type,
Simon Budig's avatar
Simon Budig committed
112 113
                                            GimpItemConstraintFunc      constraint,
                                            gpointer                    data);
114

115 116
static void  gimp_item_combo_box_populate  (GimpIntComboBox            *combo_box);
static void  gimp_item_combo_box_model_add (GimpIntComboBox            *combo_box,
117
                                            GtkListStore               *store,
Simon Budig's avatar
Simon Budig committed
118 119 120
                                            gint32                      image,
                                            gint                        num_items,
                                            gint32                     *items,
121
                                            gint                        tree_level);
Simon Budig's avatar
Simon Budig committed
122 123 124 125 126 127 128 129

static void  gimp_item_combo_box_drag_data_received (GtkWidget         *widget,
                                                     GdkDragContext    *context,
                                                     gint               x,
                                                     gint               y,
                                                     GtkSelectionData  *selection,
                                                     guint              info,
                                                     guint              time);
130

131
static void  gimp_item_combo_box_changed   (GimpIntComboBox *combo_box);
132

133 134 135 136

static const GtkTargetEntry targets[] =
{
  { "application/x-gimp-channel-id", 0 },
Simon Budig's avatar
Simon Budig committed
137 138
  { "application/x-gimp-layer-id",   0 },
  { "application/x-gimp-vectors-id", 0 }
139 140
};

141

142
G_DEFINE_TYPE (GimpDrawableComboBox, gimp_drawable_combo_box,
143 144
               GIMP_TYPE_INT_COMBO_BOX)

Manish Singh's avatar
Manish Singh committed
145
static void
146
gimp_drawable_combo_box_class_init (GimpDrawableComboBoxClass *klass)
Manish Singh's avatar
Manish Singh committed
147
{
148
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
Manish Singh's avatar
Manish Singh committed
149

Simon Budig's avatar
Simon Budig committed
150
  widget_class->drag_data_received = gimp_item_combo_box_drag_data_received;
Manish Singh's avatar
Manish Singh committed
151 152 153 154 155 156 157 158 159 160 161
}

static void
gimp_drawable_combo_box_init (GimpDrawableComboBox *combo_box)
{
  gtk_drag_dest_set (GTK_WIDGET (combo_box),
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_DROP,
                     targets, 2,
                     GDK_ACTION_COPY);
162 163 164 165

  g_object_set_data_full (G_OBJECT (combo_box), "gimp-item-combo-box-private",
                          g_new0 (GimpItemComboBoxPrivate, 1),
                          (GDestroyNotify) g_free);
Manish Singh's avatar
Manish Singh committed
166 167
}

168 169 170 171 172 173 174 175 176 177
/**
 * gimp_drawable_combo_box_new:
 * @constraint: a #GimpDrawableConstraintFunc or %NULL
 * @data:       a pointer that is passed to @constraint
 *
 * Creates a new #GimpIntComboBox filled with all currently opened
 * drawables. If a @constraint function is specified, it is called for
 * each drawable and only if the function returns %TRUE, the drawable
 * is added to the combobox.
 *
Sven Neumann's avatar
Sven Neumann committed
178
 * You should use gimp_int_combo_box_connect() to initialize and connect
179 180 181
 * the combo.  Use gimp_int_combo_box_set_active() to get the active
 * drawable ID and gimp_int_combo_box_get_active() to retrieve the ID
 * of the selected drawable.
182 183 184
 *
 * Return value: a new #GimpIntComboBox.
 *
185
 * Since: 2.2
186
 **/
187 188 189 190
GtkWidget *
gimp_drawable_combo_box_new (GimpDrawableConstraintFunc constraint,
                             gpointer                   data)
{
191 192
  return gimp_item_combo_box_new (GIMP_TYPE_DRAWABLE_COMBO_BOX,
                                  constraint, data);
Manish Singh's avatar
Manish Singh committed
193 194 195
}


196
G_DEFINE_TYPE (GimpChannelComboBox, gimp_channel_combo_box,
197
               GIMP_TYPE_INT_COMBO_BOX)
Manish Singh's avatar
Manish Singh committed
198 199 200 201

static void
gimp_channel_combo_box_class_init (GimpChannelComboBoxClass *klass)
{
202 203 204
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  widget_class->drag_data_received = gimp_item_combo_box_drag_data_received;
Manish Singh's avatar
Manish Singh committed
205 206 207 208 209 210
}

static void
gimp_channel_combo_box_init (GimpChannelComboBox *combo_box)
{
  gtk_drag_dest_set (GTK_WIDGET (combo_box),
211 212 213
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_DROP,
Manish Singh's avatar
Manish Singh committed
214
                     targets, 1,
215
                     GDK_ACTION_COPY);
216 217 218 219

  g_object_set_data_full (G_OBJECT (combo_box), "gimp-item-combo-box-private",
                          g_new0 (GimpItemComboBoxPrivate, 1),
                          (GDestroyNotify) g_free);
220 221
}

222 223 224 225 226 227
/**
 * gimp_channel_combo_box_new:
 * @constraint: a #GimpDrawableConstraintFunc or %NULL
 * @data:       a pointer that is passed to @constraint
 *
 * Creates a new #GimpIntComboBox filled with all currently opened
Sven Neumann's avatar
Sven Neumann committed
228
 * channels. See gimp_drawable_combo_box_new() for more information.
229 230 231
 *
 * Return value: a new #GimpIntComboBox.
 *
232
 * Since: 2.2
233
 **/
234 235 236 237
GtkWidget *
gimp_channel_combo_box_new (GimpDrawableConstraintFunc constraint,
                            gpointer                   data)
{
238 239
  return gimp_item_combo_box_new (GIMP_TYPE_CHANNEL_COMBO_BOX,
                                  constraint, data);
Manish Singh's avatar
Manish Singh committed
240 241 242
}


243
G_DEFINE_TYPE (GimpLayerComboBox, gimp_layer_combo_box,
244
               GIMP_TYPE_INT_COMBO_BOX)
Manish Singh's avatar
Manish Singh committed
245 246 247 248

static void
gimp_layer_combo_box_class_init (GimpLayerComboBoxClass *klass)
{
249 250 251
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  widget_class->drag_data_received = gimp_item_combo_box_drag_data_received;
Manish Singh's avatar
Manish Singh committed
252 253 254 255 256 257
}

static void
gimp_layer_combo_box_init (GimpLayerComboBox *combo_box)
{
  gtk_drag_dest_set (GTK_WIDGET (combo_box),
258 259 260
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_DROP,
Manish Singh's avatar
Manish Singh committed
261
                     targets + 1, 1,
262
                     GDK_ACTION_COPY);
263 264 265 266

  g_object_set_data_full (G_OBJECT (combo_box), "gimp-item-combo-box-private",
                          g_new0 (GimpItemComboBoxPrivate, 1),
                          (GDestroyNotify) g_free);
267 268
}

269 270 271 272 273 274
/**
 * gimp_layer_combo_box_new:
 * @constraint: a #GimpDrawableConstraintFunc or %NULL
 * @data:       a pointer that is passed to @constraint
 *
 * Creates a new #GimpIntComboBox filled with all currently opened
Sven Neumann's avatar
Sven Neumann committed
275
 * layers. See gimp_drawable_combo_box_new() for more information.
276 277 278
 *
 * Return value: a new #GimpIntComboBox.
 *
279
 * Since: 2.2
280
 **/
281 282 283 284
GtkWidget *
gimp_layer_combo_box_new (GimpDrawableConstraintFunc constraint,
                          gpointer                   data)
{
285 286
  return gimp_item_combo_box_new (GIMP_TYPE_LAYER_COMBO_BOX,
                                  constraint, data);
Simon Budig's avatar
Simon Budig committed
287 288 289 290
}


G_DEFINE_TYPE (GimpVectorsComboBox, gimp_vectors_combo_box,
291
               GIMP_TYPE_INT_COMBO_BOX)
Simon Budig's avatar
Simon Budig committed
292 293 294 295

static void
gimp_vectors_combo_box_class_init (GimpVectorsComboBoxClass *klass)
{
296 297 298
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  widget_class->drag_data_received = gimp_item_combo_box_drag_data_received;
Simon Budig's avatar
Simon Budig committed
299 300 301 302 303 304 305 306 307 308 309
}

static void
gimp_vectors_combo_box_init (GimpVectorsComboBox *combo_box)
{
  gtk_drag_dest_set (GTK_WIDGET (combo_box),
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_DROP,
                     targets + 2, 1,
                     GDK_ACTION_COPY);
310 311 312 313

  g_object_set_data_full (G_OBJECT (combo_box), "gimp-item-combo-box-private",
                          g_new0 (GimpItemComboBoxPrivate, 1),
                          (GDestroyNotify) g_free);
Simon Budig's avatar
Simon Budig committed
314 315
}

316

Simon Budig's avatar
Simon Budig committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
/**
 * gimp_vectors_combo_box_new:
 * @constraint: a #GimpVectorsConstraintFunc or %NULL
 * @data:       a pointer that is passed to @constraint
 *
 * Creates a new #GimpIntComboBox filled with all currently opened
 * vectors objects. If a @constraint function is specified, it is called for
 * each vectors object and only if the function returns %TRUE, the vectors
 * object is added to the combobox.
 *
 * You should use gimp_int_combo_box_connect() to initialize and connect
 * the combo.  Use gimp_int_combo_box_set_active() to set the active
 * vectors ID and gimp_int_combo_box_get_active() to retrieve the ID
 * of the selected vectors object.
 *
 * Return value: a new #GimpIntComboBox.
 *
334
 * Since: 2.4
Simon Budig's avatar
Simon Budig committed
335 336 337 338 339
 **/
GtkWidget *
gimp_vectors_combo_box_new (GimpVectorsConstraintFunc constraint,
                            gpointer                  data)
{
340 341
  return gimp_item_combo_box_new (GIMP_TYPE_VECTORS_COMBO_BOX,
                                  constraint, data);
Simon Budig's avatar
Simon Budig committed
342 343 344 345
}


static GtkWidget *
346
gimp_item_combo_box_new (GType                  type,
Simon Budig's avatar
Simon Budig committed
347 348 349
                         GimpItemConstraintFunc constraint,
                         gpointer               data)
{
350 351
  GimpIntComboBox         *combo_box;
  GimpItemComboBoxPrivate *private;
352 353 354 355 356 357

  combo_box = g_object_new (type,
                            "width-request", WIDTH_REQUEST,
                            "ellipsize",     PANGO_ELLIPSIZE_MIDDLE,
                            NULL);

358 359 360 361
  private = GET_PRIVATE (combo_box);

  private->constraint = constraint;
  private->data       = data;
362 363 364 365 366 367 368 369 370 371 372

  gimp_item_combo_box_populate (combo_box);

  g_signal_connect (combo_box, "changed",
                    G_CALLBACK (gimp_item_combo_box_changed),
                    NULL);

  return GTK_WIDGET (combo_box);
}

static void
373
gimp_item_combo_box_populate (GimpIntComboBox *combo_box)
374
{
375 376 377 378 379 380 381 382 383 384 385 386
  GtkTreeModel *model;
  GtkTreeIter   iter;
  gint32       *images;
  gint          num_images;
  gint          i;

  model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));

  images = gimp_image_list (&num_images);

  for (i = 0; i < num_images; i++)
    {
Simon Budig's avatar
Simon Budig committed
387 388 389
      gint32 *items;
      gint    num_items;

390 391
      if (GIMP_IS_DRAWABLE_COMBO_BOX (combo_box) ||
          GIMP_IS_LAYER_COMBO_BOX (combo_box))
Simon Budig's avatar
Simon Budig committed
392 393
        {
          items = gimp_image_get_layers (images[i], &num_items);
394
          gimp_item_combo_box_model_add (combo_box, GTK_LIST_STORE (model),
Simon Budig's avatar
Simon Budig committed
395
                                         images[i],
396
                                         num_items, items, 0);
Simon Budig's avatar
Simon Budig committed
397 398
          g_free (items);
        }
399

400 401
      if (GIMP_IS_DRAWABLE_COMBO_BOX (combo_box) ||
          GIMP_IS_CHANNEL_COMBO_BOX (combo_box))
Simon Budig's avatar
Simon Budig committed
402 403
        {
          items = gimp_image_get_channels (images[i], &num_items);
404
          gimp_item_combo_box_model_add (combo_box, GTK_LIST_STORE (model),
405
                                         images[i],
406
                                         num_items, items, 0);
Simon Budig's avatar
Simon Budig committed
407 408 409
          g_free (items);
        }

410
      if (GIMP_IS_VECTORS_COMBO_BOX (combo_box))
Simon Budig's avatar
Simon Budig committed
411 412
        {
          items = gimp_image_get_vectors (images[i], &num_items);
413
          gimp_item_combo_box_model_add (combo_box, GTK_LIST_STORE (model),
Simon Budig's avatar
Simon Budig committed
414
                                         images[i],
415
                                         num_items, items, 0);
Simon Budig's avatar
Simon Budig committed
416 417
          g_free (items);
        }
418 419 420 421 422 423 424 425
    }

  g_free (images);

  if (gtk_tree_model_get_iter_first (model, &iter))
    gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &iter);
}

Manish Singh's avatar
Manish Singh committed
426
static void
427 428 429 430 431 432
gimp_item_combo_box_model_add (GimpIntComboBox *combo_box,
                               GtkListStore    *store,
                               gint32           image,
                               gint             num_items,
                               gint32          *items,
                               gint             tree_level)
433
{
434 435 436 437
  GimpItemComboBoxPrivate *private = GET_PRIVATE (combo_box);
  GtkTreeIter              iter;
  gint                     i;
  gchar                   *indent;
438 439 440 441 442 443 444 445 446 447 448 449

  if (tree_level > 0)
    {
      indent = g_new (gchar, tree_level + 2);
      memset (indent, '-', tree_level);
      indent[tree_level] = ' ';
      indent[tree_level + 1] = '\0';
    }
  else
    {
      indent = g_strdup ("");
    }
450

Simon Budig's avatar
Simon Budig committed
451
  for (i = 0; i < num_items; i++)
452
    {
453 454
      if (! private->constraint ||
          (* private->constraint) (image, items[i], private->data))
455
        {
Simon Budig's avatar
Simon Budig committed
456
          gchar     *image_name = gimp_image_get_name (image);
457
          gchar     *item_name  = gimp_item_get_name (items[i]);
458 459 460
          gchar     *label;
          GdkPixbuf *thumb;

461 462
          label = g_strdup_printf ("%s%s-%d / %s-%d",
                                   indent, image_name, image,
Simon Budig's avatar
Simon Budig committed
463
                                   item_name, items[i]);
464

Simon Budig's avatar
Simon Budig committed
465
          g_free (item_name);
466 467
          g_free (image_name);

468
          if (GIMP_IS_VECTORS_COMBO_BOX (combo_box))
Simon Budig's avatar
Simon Budig committed
469 470 471 472 473
            thumb = NULL;
          else
            thumb = gimp_drawable_get_thumbnail (items[i],
                                                 THUMBNAIL_SIZE, THUMBNAIL_SIZE,
                                                 GIMP_PIXBUF_SMALL_CHECKS);
474 475 476

          gtk_list_store_append (store, &iter);
          gtk_list_store_set (store, &iter,
Simon Budig's avatar
Simon Budig committed
477
                              GIMP_INT_STORE_VALUE,  items[i],
478 479 480 481 482 483 484 485
                              GIMP_INT_STORE_LABEL,  label,
                              GIMP_INT_STORE_PIXBUF, thumb,
                              -1);

          if (thumb)
            g_object_unref (thumb);

          g_free (label);
486
        }
487

488 489 490 491 492 493 494 495 496 497 498
      if (gimp_item_is_group (items[i]))
        {
          gint32 *children;
          gint    n_children;

          children = gimp_item_get_children (items[i], &n_children);
          gimp_item_combo_box_model_add (combo_box, store,
                                         image,
                                         n_children, children,
                                         tree_level + 1);
          g_free (children);
499 500
        }
    }
501 502

  g_free (indent);
503
}
504 505

static void
Simon Budig's avatar
Simon Budig committed
506 507 508 509 510 511 512
gimp_item_combo_box_drag_data_received (GtkWidget        *widget,
                                        GdkDragContext   *context,
                                        gint              x,
                                        gint              y,
                                        GtkSelectionData *selection,
                                        guint             info,
                                        guint             time)
513
{
514
  gint   length = gtk_selection_data_get_length (selection);
515
  gchar *str;
516

517
  if (gtk_selection_data_get_format (selection) != 8 || length < 1)
518
    {
519
      g_warning ("%s: received invalid item ID data", G_STRFUNC);
520 521 522
      return;
    }

523 524
  str = g_strndup ((const gchar *) gtk_selection_data_get_data (selection),
                   length);
525 526 527 528 529 530 531 532 533 534 535 536

  if (g_utf8_validate (str, -1, NULL))
    {
      gint pid;
      gint ID;

      if (sscanf (str, "%i:%i", &pid, &ID) == 2 &&
          pid == gimp_getpid ())
        {
          gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (widget), ID);
        }
    }
537

538
  g_free (str);
539
}
540

541 542 543 544 545 546
static gboolean
gimp_item_combo_box_remove_items (GtkTreeModel *model,
                                  GtkTreePath  *path,
                                  GtkTreeIter  *iter,
                                  gpointer      data)
{
547 548
  gint    item_ID;
  GList **remove = data;
549 550 551 552 553 554

  gtk_tree_model_get (model, iter,
                      GIMP_INT_STORE_VALUE, &item_ID,
                      -1);

  if (item_ID > 0)
555
    *remove = g_list_prepend (*remove, g_memdup (iter, sizeof (GtkTreeIter)));
556 557 558 559

  return FALSE;
}

560
static void
561
gimp_item_combo_box_changed (GimpIntComboBox *combo_box)
562 563 564
{
  gint item_ID;

565
  if (gimp_int_combo_box_get_active (combo_box, &item_ID))
566
    {
567
      if (item_ID > 0 && ! gimp_item_is_valid (item_ID))
568 569
        {
          GtkTreeModel *model;
570 571
          GList        *remove = NULL;
          GList        *list;
572 573 574 575 576

          model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));

          g_signal_stop_emission_by_name (combo_box, "changed");

577 578
          gtk_tree_model_foreach (model,
                                  gimp_item_combo_box_remove_items,
579 580 581 582 583 584 585
                                  &remove);

          for (list = remove; list; list = g_list_next (list))
            gtk_list_store_remove (GTK_LIST_STORE (model), list->data);

          g_list_free_full (remove, (GDestroyNotify) g_free);

586 587 588 589
          gimp_item_combo_box_populate (combo_box);
        }
    }
}