gimpimagecombobox.c 11.2 KB
Newer Older
1 2 3 4 5 6
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpimagecombobox.c
 * Copyright (C) 2004 Sven Neumann <sven@gimp.org>
 *
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 13 14 15 16 17
 *
 * 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
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 <stdlib.h>

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

#include "libgimpwidgets/gimpwidgets.h"

#include "gimp.h"

33
#include "gimpuitypes.h"
34 35 36 37
#include "gimpimagecombobox.h"
#include "gimppixbuf.h"


38 39 40 41 42 43 44 45 46
/**
 * SECTION: gimpimagecombobox
 * @title: GimpImageComboBox
 * @short_description: A widget providing a popup menu of images.
 *
 * A widget providing a popup menu of images.
 **/


47 48
#define THUMBNAIL_SIZE   24
#define WIDTH_REQUEST   200
49 50


51 52 53 54
typedef struct _GimpImageComboBoxClass GimpImageComboBoxClass;

struct _GimpImageComboBox
{
55 56 57 58
  GimpIntComboBox          parent_instance;

  GimpImageConstraintFunc  constraint;
  gpointer                 data;
59
  GDestroyNotify           data_destroy;
60 61

  GimpImageConstraintDeprecatedFunc  constraint_d;
62 63 64 65 66 67 68 69
};

struct _GimpImageComboBoxClass
{
  GimpIntComboBoxClass  parent_class;
};


70 71
static void  gimp_image_combo_box_finalize  (GObject                 *object);

72
static void  gimp_image_combo_box_populate  (GimpImageComboBox       *combo_box);
73 74 75 76
static void  gimp_image_combo_box_model_add (GtkListStore            *store,
                                             gint                     num_images,
                                             gint32                  *images,
                                             GimpImageConstraintFunc  constraint,
77 78
                                             GimpImageConstraintDeprecatedFunc
                                                                      constraint_d,
79
                                             gpointer                 data);
80

81 82 83 84 85 86 87 88
static void  gimp_image_combo_box_drag_data_received (GtkWidget        *widget,
                                                      GdkDragContext   *context,
                                                      gint              x,
                                                      gint              y,
                                                      GtkSelectionData *selection,
                                                      guint             info,
                                                      guint             time);

89 90
static void  gimp_image_combo_box_changed   (GimpImageComboBox *combo_box);

91 92 93

static const GtkTargetEntry target = { "application/x-gimp-image-id", 0 };

94

95 96 97 98
G_DEFINE_TYPE (GimpImageComboBox, gimp_image_combo_box,
               GIMP_TYPE_INT_COMBO_BOX)

#define parent_class gimp_image_combo_box_parent_class
99

100 101 102 103

static void
gimp_image_combo_box_class_init (GimpImageComboBoxClass *klass)
{
104
  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
Michael Natterer's avatar
Michael Natterer committed
105
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
106

107 108
  object_class->finalize           = gimp_image_combo_box_finalize;

109 110 111 112 113 114 115 116 117 118 119 120 121 122
  widget_class->drag_data_received = gimp_image_combo_box_drag_data_received;
}

static void
gimp_image_combo_box_init (GimpImageComboBox *combo_box)
{
  gtk_drag_dest_set (GTK_WIDGET (combo_box),
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_DROP,
                     &target, 1,
                     GDK_ACTION_COPY);
}

123 124 125 126 127 128 129 130 131 132 133
static void
gimp_image_combo_box_finalize (GObject *object)
{
  GimpImageComboBox *combo = GIMP_IMAGE_COMBO_BOX (object);

  if (combo->data_destroy)
    combo->data_destroy (combo->data);

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

134 135
/**
 * gimp_image_combo_box_new:
136 137 138
 * @constraint:   a #GimpImageConstraintFunc or %NULL
 * @data:         a pointer that is passed to @constraint
 * @data_destroy: Destroy function for @data.
139 140 141 142 143 144
 *
 * Creates a new #GimpIntComboBox filled with all currently opened
 * images. If a @constraint function is specified, it is called for
 * each image and only if the function returns %TRUE, the image is
 * added to the combobox.
 *
145 146 147 148
 * You should use gimp_int_combo_box_connect() to initialize and
 * connect the combo. Use gimp_int_combo_box_set_active() to get the
 * active image ID and gimp_int_combo_box_get_active() to retrieve the
 * ID of the selected image.
149
 *
150
 * Returns: a new #GimpIntComboBox.
151
 *
152
 * Since: 2.2
153
 **/
154 155
GtkWidget *
gimp_image_combo_box_new (GimpImageConstraintFunc constraint,
156 157
                          gpointer                data,
                          GDestroyNotify          data_destroy)
158
{
159
  GimpImageComboBox *combo_box;
160

161
  combo_box = g_object_new (GIMP_TYPE_IMAGE_COMBO_BOX,
162 163
                            "width-request", WIDTH_REQUEST,
                            "ellipsize",     PANGO_ELLIPSIZE_MIDDLE,
164
                            NULL);
165

166 167 168
  combo_box->constraint   = constraint;
  combo_box->data         = data;
  combo_box->data_destroy = data_destroy;
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

  gimp_image_combo_box_populate (combo_box);

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

  return GTK_WIDGET (combo_box);
}

static void
gimp_image_combo_box_populate (GimpImageComboBox *combo_box)
{
  GtkTreeModel *model;
  GtkTreeIter   iter;
  gint32       *images;
  gint          num_images;

187 188 189 190
  model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));

  images = gimp_image_list (&num_images);

191 192
  gimp_image_combo_box_model_add (GTK_LIST_STORE (model),
                                  num_images, images,
193
                                  combo_box->constraint,
194
                                  combo_box->constraint_d,
195 196
                                  combo_box->data);

197 198 199 200 201 202 203 204 205 206 207
  g_free (images);

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

static void
gimp_image_combo_box_model_add (GtkListStore            *store,
                                gint                     num_images,
                                gint32                  *images,
                                GimpImageConstraintFunc  constraint,
208 209
                                GimpImageConstraintDeprecatedFunc
                                                         constraint_d,
210 211 212 213 214 215 216
                                gpointer                 data)
{
  GtkTreeIter  iter;
  gint         i;

  for (i = 0; i < num_images; i++)
    {
217 218
      GimpImage *image;

219
      image = gimp_image_new_by_id (images[i]);
220 221 222
      if ((! constraint && ! constraint_d)             ||
          (constraint && (* constraint) (image, data)) ||
          (constraint_d && (* constraint_d) (images[i], data)))
223
        {
224
          gchar     *image_name = gimp_image_get_name (image);
225 226 227 228 229 230 231
          gchar     *label;
          GdkPixbuf *thumb;

          label = g_strdup_printf ("%s-%d", image_name, images[i]);

          g_free (image_name);

232
          thumb = gimp_image_get_thumbnail (image,
233
                                            THUMBNAIL_SIZE, THUMBNAIL_SIZE,
234 235 236 237 238 239 240 241 242 243 244 245 246 247
                                            GIMP_PIXBUF_SMALL_CHECKS);

          gtk_list_store_append (store, &iter);
          gtk_list_store_set (store, &iter,
                              GIMP_INT_STORE_VALUE,  images[i],
                              GIMP_INT_STORE_LABEL,  label,
                              GIMP_INT_STORE_PIXBUF, thumb,
                              -1);

          if (thumb)
            g_object_unref (thumb);

          g_free (label);
        }
248
      g_object_unref (image);
249 250
    }
}
251 252 253 254 255 256 257 258 259 260

static void
gimp_image_combo_box_drag_data_received (GtkWidget        *widget,
                                         GdkDragContext   *context,
                                         gint              x,
                                         gint              y,
                                         GtkSelectionData *selection,
                                         guint             info,
                                         guint             time)
{
261
  gint   length = gtk_selection_data_get_length (selection);
Michael Natterer's avatar
Michael Natterer committed
262
  gchar *str;
263

264
  if (gtk_selection_data_get_format (selection) != 8 || length < 1)
265
    {
266
      g_warning ("%s: received invalid image ID data", G_STRFUNC);
267 268 269
      return;
    }

270 271
  str = g_strndup ((const gchar *) gtk_selection_data_get_data (selection),
                   length);
Michael Natterer's avatar
Michael Natterer committed
272 273 274 275 276 277 278 279 280 281 282 283

  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);
        }
    }
284

Michael Natterer's avatar
Michael Natterer committed
285
  g_free (str);
286
}
287 288 289 290 291 292 293 294 295

static void
gimp_image_combo_box_changed (GimpImageComboBox *combo_box)
{
  gint image_ID;

  if (gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (combo_box),
                                     &image_ID))
    {
296 297
      GimpImage *image;

298
      image = gimp_image_new_by_id (image_ID);
299
      if (! gimp_image_is_valid (image))
300 301 302 303 304 305 306 307 308 309
        {
          GtkTreeModel *model;

          model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));

          g_signal_stop_emission_by_name (combo_box, "changed");

          gtk_list_store_clear (GTK_LIST_STORE (model));
          gimp_image_combo_box_populate (combo_box);
        }
310
      g_object_unref (image);
311 312
    }
}
313 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 353 354 355 356 357 358 359 360 361


/* Deprecated API. */


/**
 * gimp_image_combo_box_new_deprecated: (skip)
 * @constraint:   a #GimpImageConstraintDeprecatedFunc or %NULL
 * @data:         a pointer that is passed to @constraint
 * @data_destroy: Destroy function for @data.
 *
 * Creates a new #GimpIntComboBox filled with all currently opened
 * images. If a @constraint function is specified, it is called for
 * each image and only if the function returns %TRUE, the image 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 get the
 * active image ID and gimp_int_combo_box_get_active() to retrieve the
 * ID of the selected image.
 *
 * Returns: a new #GimpIntComboBox.
 *
 * Since: 2.2
 **/
GtkWidget *
gimp_image_combo_box_new_deprecated (GimpImageConstraintDeprecatedFunc constraint,
                                     gpointer                          data,
                                     GDestroyNotify                    data_destroy)
{
  GimpImageComboBox *combo_box;

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

  combo_box->constraint_d = constraint;
  combo_box->data         = data;
  combo_box->data_destroy = data_destroy;

  gimp_image_combo_box_populate (combo_box);

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

  return GTK_WIDGET (combo_box);
}