gimpaction.c 10.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
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpaction.c
 * Copyright (C) 2004 Michael Natterer <mitch@gimp.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

24
25
#include <string.h>

26
27
#include <gtk/gtk.h>

28
#include "libgimpbase/gimpbase.h"
29
30
31
32
33
34
#include "libgimpcolor/gimpcolor.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

#include "core/gimpmarshal.h"
35
#include "core/gimpimagefile.h"  /* eek */
36
37
38
#include "core/gimpviewable.h"

#include "gimpaction.h"
39
#include "gimpview.h"
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


enum
{
  PROP_0,
  PROP_COLOR,
  PROP_VIEWABLE
};


static void   gimp_action_init          (GimpAction      *action);
static void   gimp_action_class_init    (GimpActionClass *klass);

static void   gimp_action_finalize      (GObject         *object);
static void   gimp_action_set_property  (GObject         *object,
                                         guint            prop_id,
                                         const GValue    *value,
                                         GParamSpec      *pspec);
static void   gimp_action_get_property  (GObject         *object,
                                         guint            prop_id,
                                         GValue          *value,
                                         GParamSpec      *pspec);
static void   gimp_action_connect_proxy (GtkAction       *action,
                                         GtkWidget       *proxy);
static void   gimp_action_set_proxy     (GimpAction      *action,
                                         GtkWidget       *proxy);


static GtkActionClass *parent_class = NULL;


GType
gimp_action_get_type (void)
{
  static GType type = 0;

  if (!type)
    {
      static const GTypeInfo type_info =
      {
        sizeof (GimpActionClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gimp_action_class_init,
        (GClassFinalizeFunc) NULL,
        NULL,
        sizeof (GimpAction),
        0, /* n_preallocs */
        (GInstanceInitFunc) gimp_action_init,
      };

      type = g_type_register_static (GTK_TYPE_ACTION,
                                     "GimpAction",
                                     &type_info, 0);
    }

  return type;
}

static void
gimp_action_class_init (GimpActionClass *klass)
{
  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
  GtkActionClass *action_class = GTK_ACTION_CLASS (klass);
  GimpRGB         black;

  parent_class = g_type_class_peek_parent (klass);

  object_class->finalize      = gimp_action_finalize;
  object_class->set_property  = gimp_action_set_property;
  object_class->get_property  = gimp_action_get_property;

  action_class->connect_proxy = gimp_action_connect_proxy;

  gimp_rgba_set (&black, 0.0, 0.0, 0.0, GIMP_OPACITY_OPAQUE);

  g_object_class_install_property (object_class, PROP_COLOR,
Michael Natterer's avatar
Michael Natterer committed
117
118
119
120
                                   gimp_param_spec_rgb ("color",
                                                        NULL, NULL,
                                                        &black,
                                                        G_PARAM_READWRITE));
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

  g_object_class_install_property (object_class, PROP_VIEWABLE,
                                   g_param_spec_object ("viewable",
                                                        NULL, NULL,
                                                        GIMP_TYPE_VIEWABLE,
                                                        G_PARAM_READWRITE));
}

static void
gimp_action_init (GimpAction *action)
{
  action->color    = NULL;
  action->viewable = NULL;
}

static void
gimp_action_finalize (GObject *object)
{
  GimpAction *action = GIMP_ACTION (object);

  if (action->color)
    {
      g_free (action->color);
      action->color = NULL;
    }

  if (action->viewable)
    {
      g_object_unref (action->viewable);
      action->viewable = NULL;
    }

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

static void
gimp_action_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  GimpAction *action = GIMP_ACTION (object);

  switch (prop_id)
    {
    case PROP_COLOR:
      g_value_set_boxed (value, action->color);
      break;
    case PROP_VIEWABLE:
      g_value_set_object (value, action->viewable);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gimp_action_set_property (GObject      *object,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  GimpAction *action    = GIMP_ACTION (object);
  gboolean    set_proxy = FALSE;

  switch (prop_id)
    {
    case PROP_COLOR:
      if (action->color)
        g_free (action->color);
      action->color = g_value_dup_boxed (value);
      set_proxy = TRUE;
      break;
    case PROP_VIEWABLE:
      if (action->viewable)
        g_object_unref  (action->viewable);
      action->viewable = (GimpViewable *) g_value_dup_object (value);
      set_proxy = TRUE;
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }

  if (set_proxy)
    {
      GSList *list;

      for (list = gtk_action_get_proxies (GTK_ACTION (action));
           list;
           list = g_slist_next (list))
        {
          gimp_action_set_proxy (action, list->data);
        }
    }
}

static void
gimp_action_connect_proxy (GtkAction *action,
                           GtkWidget *proxy)
{
  GTK_ACTION_CLASS (parent_class)->connect_proxy (action, proxy);

  gimp_action_set_proxy (GIMP_ACTION (action), proxy);
}


/*  public functions  */

GimpAction *
gimp_action_new (const gchar *name,
                 const gchar *label,
                 const gchar *tooltip,
                 const gchar *stock_id)
{
  return g_object_new (GIMP_TYPE_ACTION,
                       "name",     name,
                       "label",    label,
                       "tooltip",  tooltip,
241
                       "stock-id", stock_id,
242
243
244
                       NULL);
}

245
246
247
248
249
250
251
252
gint
gimp_action_name_compare (GimpAction  *action1,
                          GimpAction  *action2)
{
  return strcmp (gtk_action_get_name ((GtkAction *) action1),
                 gtk_action_get_name ((GtkAction *) action2));
}

253
254
255
256
257
258
259
260
261
262

/*  private functions  */

static void
gimp_action_set_proxy (GimpAction *action,
                       GtkWidget  *proxy)
{
  if (! GTK_IS_IMAGE_MENU_ITEM (proxy))
    return;

263
264
265
266
267
268
269
270
271
272
273
274
275
276
  /*  This is not quite the correct check, but works fine to enable
   *  tooltips only for the "Open Recent" menu items, since they are
   *  the only ones having both a viewable and a tooltip. --mitch
   */
  if (action->viewable)
    {
      gchar *tooltip = NULL;

      g_object_get (action, "tooltip", &tooltip, NULL);

      if (tooltip)
        {
          const gchar *help_id;

Sven Neumann's avatar
Sven Neumann committed
277
          help_id = g_object_get_qdata (G_OBJECT (proxy), GIMP_HELP_ID);
278
279
280
281
282
283

          gimp_help_set_help_data (proxy, tooltip, help_id);
          g_free (tooltip);
        }
    }

284
285
286
287
288
289
290
291
292
293
294
295
296
297
  if (action->color)
    {
      GtkWidget *area;

      area = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));

      if (area && ! GIMP_IS_COLOR_AREA (area))
        {
          gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
          area = NULL;
        }

      if (! area)
        {
298
          GtkSettings *settings = gtk_widget_get_settings (proxy);
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
          gint         width, height;

          area = gimp_color_area_new (action->color,
                                      GIMP_COLOR_AREA_SMALL_CHECKS, 0);
          gimp_color_area_set_draw_border (GIMP_COLOR_AREA (area), TRUE);

          gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
                                             &width, &height);

          gtk_widget_set_size_request (area, width, height);
          gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), area);
          gtk_widget_show (area);
        }
      else
        {
          gimp_color_area_set_color (GIMP_COLOR_AREA (area), action->color);
        }
    }
  else if (action->viewable)
    {
319
      GtkWidget *view;
320

321
      view = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
322

323
      if (view && ! GIMP_IS_VIEW (view))
324
325
        {
          gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
326
          view = NULL;
327
328
        }

329
      if (! view)
330
        {
331
          GtkSettings *settings = gtk_widget_get_settings (proxy);
332
333
334
          GtkIconSize  size;
          gint         width, height;
          gint         border_width;
335

336
337
338
339
340
341
342
343
344
345
          if (GIMP_IS_IMAGEFILE (action->viewable))
            {
              size         = GTK_ICON_SIZE_LARGE_TOOLBAR;
              border_width = 0;
            }
          else
            {
              size         = GTK_ICON_SIZE_MENU;
              border_width = 1;
            }
346

347
          gtk_icon_size_lookup_for_settings (settings, size, &width, &height);
348

349
          view = gimp_view_new_full (action->viewable,
350
                                     width, height, border_width,
351
                                     FALSE, FALSE, FALSE);
352
353
          gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), view);
          gtk_widget_show (view);
354
355
356
        }
      else
        {
357
          gimp_view_set_viewable (GIMP_VIEW (view), action->viewable);
358
359
360
361
362
363
364
365
        }
    }
  else
    {
      GtkWidget *image;

      image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));

366
      if (image && (GIMP_IS_VIEW (image) || GIMP_IS_COLOR_AREA (image)))
367
368
369
370
371
372
        {
          gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
          g_object_notify (G_OBJECT (action), "stock-id");
        }
    }
}