gimpitemcombobox.c 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpdrawablecombobox.c
 * Copyright (C) 2004 Sven Neumann <sven@gimp.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * 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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

25 26
#include <stdlib.h>

27 28 29 30 31 32 33 34 35 36
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "gimp.h"

#include "gimpdrawablecombobox.h"
#include "gimppixbuf.h"


37 38
#define THUMBNAIL_SIZE  24
#define WIDTH_REQUEST  200
39 40


41 42 43 44 45 46
static gint  gimp_drawable_combo_box_model_add (GtkListStore               *store,
                                                gint32                      image,
                                                gint                        num_drawables,
                                                gint32                     *drawables,
                                                GimpDrawableConstraintFunc  constraint,
                                                gpointer                    data);
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
static void  gimp_drawable_combo_box_drag_data_received (GtkWidget        *widget,
                                                         GdkDragContext   *context,
                                                         gint              x,
                                                         gint              y,
                                                         GtkSelectionData *selection,
                                                         guint             info,
                                                         guint             time);


static const GtkTargetEntry targets[] =
{
  { "application/x-gimp-channel-id", 0 },
  { "application/x-gimp-layer-id",   0 }
};

63

64 65 66 67 68 69 70 71 72 73
/**
 * 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
74
 * You should use gimp_int_combo_box_connect() to initialize and connect
75 76 77
 * 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.
78 79 80 81 82
 *
 * Return value: a new #GimpIntComboBox.
 *
 * Since: GIMP 2.2
 **/
83 84 85 86 87 88 89 90 91 92 93
GtkWidget *
gimp_drawable_combo_box_new (GimpDrawableConstraintFunc constraint,
                             gpointer                   data)
{
  GtkWidget    *combo_box;
  GtkTreeModel *model;
  GtkTreeIter   iter;
  gint32       *images;
  gint          num_images;
  gint          i;

94
  combo_box = g_object_new (GIMP_TYPE_INT_COMBO_BOX,
95 96
                            "width-request", WIDTH_REQUEST,
                            "ellipsize",     PANGO_ELLIPSIZE_MIDDLE,
97
                            NULL);
98 99 100 101 102 103 104 105 106 107 108

  model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));

  images = gimp_image_list (&num_images);

  for (i = 0; i < num_images; i++)
    {
      gint32 *drawables;
      gint    num_drawables;

      drawables = gimp_image_get_layers (images[i], &num_drawables);
109 110 111 112
      gimp_drawable_combo_box_model_add (GTK_LIST_STORE (model),
                                         images[i],
                                         num_drawables, drawables,
                                         constraint, data);
113 114 115
      g_free (drawables);

      drawables = gimp_image_get_channels (images[i], &num_drawables);
116 117 118 119
      gimp_drawable_combo_box_model_add (GTK_LIST_STORE (model),
                                         images[i],
                                         num_drawables, drawables,
                                         constraint, data);
120 121 122 123 124 125 126 127
      g_free (drawables);
    }

  g_free (images);

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

128 129 130 131 132 133 134 135 136 137 138
  gtk_drag_dest_set (combo_box,
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_DROP,
                     targets, 2,
                     GDK_ACTION_COPY);

  g_signal_connect (combo_box, "drag-data-received",
                    G_CALLBACK (gimp_drawable_combo_box_drag_data_received),
                    NULL);

139 140 141
  return combo_box;
}

142 143 144 145 146 147 148 149 150 151 152 153
/**
 * 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
 * channels. See gimp_drawable_combo_box() for more info.
 *
 * Return value: a new #GimpIntComboBox.
 *
 * Since: GIMP 2.2
 **/
154 155 156 157 158 159 160 161 162 163 164
GtkWidget *
gimp_channel_combo_box_new (GimpDrawableConstraintFunc constraint,
                            gpointer                   data)
{
  GtkWidget    *combo_box;
  GtkTreeModel *model;
  GtkTreeIter   iter;
  gint32       *images;
  gint          num_images;
  gint          i;

165
  combo_box = g_object_new (GIMP_TYPE_INT_COMBO_BOX,
166 167
                            "width-request", WIDTH_REQUEST,
                            "ellipsize",     PANGO_ELLIPSIZE_MIDDLE,
168
                            NULL);
169 170 171 172 173 174 175 176 177 178 179

  model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));

  images = gimp_image_list (&num_images);

  for (i = 0; i < num_images; i++)
    {
      gint32 *drawables;
      gint    num_drawables;

      drawables = gimp_image_get_channels (images[i], &num_drawables);
180 181 182 183
      gimp_drawable_combo_box_model_add (GTK_LIST_STORE (model),
                                         images[i],
                                         num_drawables, drawables,
                                         constraint, data);
184 185 186 187 188 189 190 191
      g_free (drawables);
    }

  g_free (images);

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

192 193 194 195 196 197 198 199 200 201 202
  gtk_drag_dest_set (combo_box,
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_DROP,
                     targets, 1,
                     GDK_ACTION_COPY);

  g_signal_connect (combo_box, "drag-data-received",
                    G_CALLBACK (gimp_drawable_combo_box_drag_data_received),
                    NULL);

203 204 205
  return combo_box;
}

206 207 208 209 210 211 212 213 214 215 216 217
/**
 * 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
 * layers. See gimp_drawable_combo_box() for more info.
 *
 * Return value: a new #GimpIntComboBox.
 *
 * Since: GIMP 2.2
 **/
218 219 220 221 222 223 224 225 226 227 228
GtkWidget *
gimp_layer_combo_box_new (GimpDrawableConstraintFunc constraint,
                          gpointer                   data)
{
  GtkWidget    *combo_box;
  GtkTreeModel *model;
  GtkTreeIter   iter;
  gint32       *images;
  gint          num_images;
  gint          i;

229
  combo_box = g_object_new (GIMP_TYPE_INT_COMBO_BOX,
230 231
                            "width-request", WIDTH_REQUEST,
                            "ellipsize",     PANGO_ELLIPSIZE_MIDDLE,
232
                            NULL);
233 234 235 236 237 238 239 240 241 242 243

  model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));

  images = gimp_image_list (&num_images);

  for (i = 0; i < num_images; i++)
    {
      gint32 *drawables;
      gint    num_drawables;

      drawables = gimp_image_get_layers (images[i], &num_drawables);
244 245 246 247
      gimp_drawable_combo_box_model_add (GTK_LIST_STORE (model),
                                         images[i],
                                         num_drawables, drawables,
                                         constraint, data);
248 249 250 251 252 253 254 255
      g_free (drawables);
    }

  g_free (images);

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

256 257 258 259 260 261 262 263 264 265 266
  gtk_drag_dest_set (combo_box,
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_DROP,
                     targets + 1, 1,
                     GDK_ACTION_COPY);

  g_signal_connect (combo_box, "drag-data-received",
                    G_CALLBACK (gimp_drawable_combo_box_drag_data_received),
                    NULL);

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
  return combo_box;
}

static gint
gimp_drawable_combo_box_model_add (GtkListStore               *store,
                                   gint32                      image,
                                   gint                        num_drawables,
                                   gint32                     *drawables,
                                   GimpDrawableConstraintFunc  constraint,
                                   gpointer                    data)
{
  GtkTreeIter  iter;
  gint         added = 0;
  gint         i;

  for (i = 0; i < num_drawables; i++)
    {
      if (! constraint || (* constraint) (image, drawables[i], data))
        {
          gchar     *image_name    = gimp_image_get_name (image);
          gchar     *drawable_name = gimp_drawable_get_name (drawables[i]);
          gchar     *label;
          GdkPixbuf *thumb;

          label = g_strdup_printf ("%s-%d/%s-%d",
                                   image_name,    image,
                                   drawable_name, drawables[i]);

          g_free (drawable_name);
          g_free (image_name);

          thumb = gimp_drawable_get_thumbnail (drawables[i],
299
                                               THUMBNAIL_SIZE, THUMBNAIL_SIZE,
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
                                               GIMP_PIXBUF_SMALL_CHECKS);

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

          if (thumb)
            g_object_unref (thumb);

          g_free (label);
        }
    }

  return added;
}
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

static void
gimp_drawable_combo_box_drag_data_received (GtkWidget        *widget,
                                            GdkDragContext   *context,
                                            gint              x,
                                            gint              y,
                                            GtkSelectionData *selection,
                                            guint             info,
                                            guint             time)
{
  gchar *id;
  gint   ID;

  if ((selection->format != 8) || (selection->length < 1))
    {
      g_warning ("Received invalid drawable ID data!");
      return;
    }

  id = g_strndup (selection->data, selection->length);
  ID = atoi (id);
  g_free (id);

  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (widget), ID);
}