summaryrefslogtreecommitdiffstats
path: root/libgimp/gimpmenu.c
blob: 7f788d55f7eacb81274c374ee22530b84a4f7b48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
299
300
301
302
303
304
305
306
307
308
309
310
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpmenu.c
 *
 * 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 3 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
 * Library 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, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "gimp.h"

#include "gimppixbuf.h"

#undef GIMP_DISABLE_DEPRECATED
#include "gimpmenu.h"

#include "libgimp-intl.h"


/**
 * SECTION: gimpmenu
 * @title: gimpmenu
 * @short_description: Menus for selecting images, layers, channels
 *                     and drawables.
 *
 * Menus for selecting images, layers, channels and drawables.
 **/


#define MENU_THUMBNAIL_WIDTH   24
#define MENU_THUMBNAIL_HEIGHT  24


/*  local function prototypes  */

static GtkWidget * gimp_menu_make_menu     (GimpMenuCallback    callback,
                                            gpointer            data);
static GtkWidget * gimp_menu_add_item      (GtkWidget          *menu,
                                            const gchar        *image_name,
                                            const gchar        *drawable_name,
                                            gint32              any_ID);
static GtkWidget * gimp_menu_add_empty     (GtkWidget          *menu);
static GtkWidget * gimp_menu_make_preview  (gint32              any_ID,
                                            gboolean            is_image,
                                            gint                width,
                                            gint                height);
static void        gimp_menu_callback      (GtkWidget          *widget,
                                            gpointer            any_ID);


/*  public functions  */

/**
 * gimp_image_menu_new:
 * @constraint:   a function to filter the menu contents
 * @callback:     the callback to call when an image is selected
 * @data:         the callback's user_data
 * @active_image: an image to preselect
 *
 * Deprecated: Use gimp_image_combo_box_new() instead.
 *
 * Returns: the image menu.
 */
GtkWidget *
gimp_image_menu_new (GimpConstraintFunc constraint,
                     GimpMenuCallback   callback,
                     gpointer           data,
                     gint32             active_image)
{
  GtkWidget *menu;
  gchar     *name;
  gchar     *label;
  gint32    *images;
  gint32     image = -1;
  gint       n_images;
  gint       i, k;

  g_return_val_if_fail (callback != NULL, NULL);

  menu = gimp_menu_make_menu (callback, data);

  images = gimp_image_list (&n_images);

  for (i = 0, k = 0; i < n_images; i++)
    if (! constraint || (* constraint) (images[i], -1, data))
      {
        name = gimp_image_get_name (images[i]);
        label = g_strdup_printf ("%s-%d", name, images[i]);
        g_free (name);

        gimp_menu_add_item (menu, label, NULL, images[i]);

        g_free (label);

        if (images[i] == active_image)
          {
            image = active_image;
            gtk_menu_set_active (GTK_MENU (menu), k);
          }
        else if (image == -1)
          {
            image = images[i];
          }

        k += 1;
      }

  if (k == 0)
    gimp_menu_add_empty (menu);

  (* callback) (image, data);

  g_free (images);

  return menu;
}

/**
 * gimp_layer_menu_new:
 * @constraint:   a function to filter the menu contents
 * @callback:     the callback to call when a channel is selected
 * @data:         the callback's user_data
 * @active_layer: a layer to preselect
 *
 * Deprecated: Use gimp_layer_combo_box_new() instead.
 *
 * Returns: the layer menu.
 */
GtkWidget *
gimp_layer_menu_new (GimpConstraintFunc constraint,
                     GimpMenuCallback   callback,
                     gpointer           data,
                     gint32             active_layer)
{
  GtkWidget *menu;
  gchar     *image_label;
  gint32    *images;
  gint32    *layers;
  gint32     layer = -1;
  gint       n_images;
  gint       n_layers;
  gint       i, j, k;

  g_return_val_if_fail (callback != NULL, NULL);

  menu = gimp_menu_make_menu (callback, data);

  images = gimp_image_list (&n_images);

  for (i = 0, k = 0; i < n_images; i++)
    if (! constraint || (* constraint) (images[i], -1, data))
      {
        gchar *name;

        name = gimp_image_get_name (images[i]);
        image_label = g_strdup_printf ("%s-%d", name, images[i]);
        g_free (name);

        layers = gimp_image_get_layers (images[i], &n_layers);

        for (j = 0; j < n_layers; j++)
          if (! constraint || (* constraint) (images[i], layers[j], data))
            {
              name = gimp_item_get_name (layers[j]);
              gimp_menu_add_item (menu, image_label, name, layers[j]);
              g_free (name);

              if (layers[j] == active_layer)
                {
                  layer = active_layer;
                  gtk_menu_set_active (GTK_MENU (menu), k);
                }
              else if (layer == -1)
                {
                  layer = layers[j];
                }

              k += 1;
            }

        g_free (image_label);
        g_free (layers);
      }

  g_free (images);

  if (k == 0)
    gimp_menu_add_empty (menu);

  (* callback) (layer, data);

  return menu;
}

/**
 * gimp_channel_menu_new:
 * @constraint:     a function to filter the menu contents
 * @callback:       the callback to call when a channel is selected
 * @data:           the callback's user_data
 * @active_channel: a channel to preselect
 *
 * Deprecated: Use gimp_channel_combo_box_new() instead.
 *
 * Returns: the channel menu.
 */
GtkWidget *
gimp_channel_menu_new (GimpConstraintFunc constraint,
                       GimpMenuCallback   callback,
                       gpointer           data,
                       gint32             active_channel)
{
  GtkWidget *menu;
  gchar     *image_label;
  gint32    *images;
  gint32    *channels;
  gint32     channel;
  gint       n_images;
  gint       n_channels;
  gint       i, j, k;

  g_return_val_if_fail (callback != NULL, NULL);

  menu = gimp_menu_make_menu (callback, data);

  channel = -1;

  images = gimp_image_list (&n_images);

  for (i = 0, k = 0; i < n_images; i++)
    if (! constraint || (* constraint) (images[i], -1, data))
      {
        gchar *name;

        name = gimp_image_get_name (images[i]);
        image_label = g_strdup_printf ("%s-%d", name, images[i]);
        g_free (name);

        channels = gimp_image_get_channels (images[i], &n_channels);

        for (j = 0; j < n_channels; j++)
          if (! constraint || (* constraint) (images[i], channels[j], data))
            {
              name = gimp_item_get_name (channels[j]);
              gimp_menu_add_item (menu, image_label, name, channels[j]);
              g_free (name);

              if (channels[j] == active_channel)
                {
                  channel = active_channel;
                  gtk_menu_set_active (GTK_MENU (menu), k);
                }
              else if (channel == -1)
                {
                  channel = channels[j];
                }

              k += 1;
            }

        g_free (image_label);
        g_free (channels);
      }

  g_free (images);

  if (k == 0)
    gimp_menu_add_empty (menu);

  (* callback) (channel, data);

  return menu;
}

/**
 * gimp_drawable_menu_new:
 * @constraint:      a function to filter the menu contents
 * @callback:        the callback to call when a channel is selected
 * @data:            the callback's user_data
 * @active_drawable: a drawable to preselect
 *
 * Deprecated: Use gimp_drawable_combo_box_new() instead.
 *
 * Returns: the drawable menu.
 */
GtkWidget *
gimp_drawable_menu_new (GimpConstraintFunc constraint,
                        GimpMenuCallback   callback,
                        gpointer           data,
                        gint32             active_drawable)
{
  GtkWidget *menu;
  gchar     *name;
  gchar     *image_label;
  gint32    *images;
  gint32    *layers;
  gint32    *channels;
  gint32     drawable;
  gint       n_images;
  gint       n_layers;
  gint       n_channels;
  gint       i, j, k;

  menu = gimp_menu_make_menu (callback, data);

  drawable = -1;

  images = gimp_image_list (&n_images);

  for (i = 0, k = 0; i < n_images; i++)
    if (! constraint || (* constraint) (images[i], -1, data))
      {
        name = gimp_image_get_name (images[i]);
        image_label = g_strdup_printf ("%s-%d", name, images[i]);
        g_free (name);

        layers   = gimp_image_get_layers   (images[i], &n_layers);
        channels = gimp_image_get_channels (images[i], &n_channels);

        for (j = 0; j < n_layers; j++)
          if (! constraint || (* constraint) (images[i], layers[j], data))
            {
              name = gimp_item_get_name (layers[j]);
              gimp_menu_add_item (menu, image_label, name, layers[j]);
              g_free (name);

              if (layers[j] == active_drawable)
                {
                  drawable = active_drawable;
                  gtk_menu_set_active (GTK_MENU (menu), k);
                }
              else if (drawable == -1)
                {
                  drawable = layers[j];
                }

              k += 1;
            }

        for (j = 0; j < n_channels; j++)
          if (! constraint || (* constraint) (images[i], channels[j], data))
            {
              name = gimp_item_get_name (channels[j]);
              gimp_menu_add_item (menu, image_label, name, channels[j]);
              g_free (name);

              if (channels[j] == active_drawable)
                {
                  drawable = active_drawable;
                  gtk_menu_set_active (GTK_MENU (menu), k);
                }
              else if (drawable == -1)
                {
                  drawable = channels[j];
                }

              k += 1;
            }

        g_free (image_label);
        g_free (layers);
        g_free (channels);
      }

  g_free (images);

  if (k == 0)
    gimp_menu_add_empty (menu);

  (* callback) (drawable, data);

  return menu;
}


/*  private functions  */

static GtkWidget *
gimp_menu_make_menu (GimpMenuCallback callback,
                     gpointer         data)
{
  GtkWidget *menu;

  menu = gtk_menu_new ();
  g_object_set_data (G_OBJECT (menu), "gimp-menu-callback",      callback);
  g_object_set_data (G_OBJECT (menu), "gimp-menu-callback-data", data);

  return menu;
}

static GtkWidget *
gimp_menu_add_item (GtkWidget   *menu,
                    const gchar *image_name,
                    const gchar *drawable_name,
                    gint32       any_ID)
{
  GtkWidget *menuitem;
  GtkWidget *hbox;
  GtkWidget *vbox;
  GtkWidget *preview;
  GtkWidget *label;
  gchar     *str;

  if (drawable_name)
    str = g_strdup_printf ("%s/%s-%d", image_name, drawable_name, any_ID);
  else
    str = g_strdup (image_name);

  menuitem = gtk_menu_item_new ();
  gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
  gtk_widget_show (menuitem);

  g_signal_connect (menuitem, "activate",
                    G_CALLBACK (gimp_menu_callback),
                    GINT_TO_POINTER (any_ID));

  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
  gtk_container_add (GTK_CONTAINER (menuitem), hbox);
  gtk_widget_show (hbox);

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
  gtk_widget_show (vbox);

  preview = gimp_menu_make_preview (any_ID, drawable_name == NULL,
                                    MENU_THUMBNAIL_WIDTH,
                                    MENU_THUMBNAIL_HEIGHT);
  gtk_box_pack_start (GTK_BOX (vbox), preview, TRUE, TRUE, 0);
  gtk_widget_show (preview);

  label = gtk_label_new (str);
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  g_free (str);

  return menuitem;
}

static GtkWidget *
gimp_menu_add_empty (GtkWidget *menu)
{
  GtkWidget *menuitem;

  menuitem = gtk_menu_item_new_with_label (_("(Empty)"));
  gtk_widget_set_sensitive (menuitem, FALSE);
  gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
  gtk_widget_show (menuitem);

  return menuitem;
}

static GtkWidget *
gimp_menu_make_preview (gint32     any_ID,
                        gboolean   is_image,
                        gint       width,
                        gint       height)
{
  GtkWidget *image;
  GdkPixbuf *pixbuf;

  if (is_image)
    pixbuf = gimp_image_get_thumbnail (any_ID,
                                       width, height,
                                       GIMP_PIXBUF_SMALL_CHECKS);
  else
    pixbuf = gimp_drawable_get_thumbnail (any_ID,
                                          width, height,
                                          GIMP_PIXBUF_SMALL_CHECKS);

  image = gtk_image_new_from_pixbuf (pixbuf);

  g_object_unref (pixbuf);

  return image;
}

static void
gimp_menu_callback (GtkWidget *widget,
                    gpointer   any_ID)
{
  GtkWidget        *parent = gtk_widget_get_parent (widget);
  GimpMenuCallback  callback;
  gpointer          callback_data;

  callback = (GimpMenuCallback) g_object_get_data (G_OBJECT (parent),
                                                   "gimp-menu-callback");
  callback_data = g_object_get_data (G_OBJECT (parent),
                                     "gimp-menu-callback-data");

  (* callback) (GPOINTER_TO_INT (any_ID), callback_data);
}