~ubuntu-branches/ubuntu/vivid/gimp/vivid

« back to all changes in this revision

Viewing changes to app/widgets/gimpoverlaydialog.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-08 18:50:03 UTC
  • mto: (1.1.26) (0.5.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 71.
  • Revision ID: package-import@ubuntu.com-20120508185003-tltkvbaysf8d2426
ImportĀ upstreamĀ versionĀ 2.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIMP - The GNU Image Manipulation Program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * gimpoverlaydialog.c
 
5
 * Copyright (C) 2009-2010  Michael Natterer <mitch@gimp.org>
 
6
 *
 
7
 * This program is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <gtk/gtk.h>
 
24
#include <gdk/gdkkeysyms.h>
 
25
 
 
26
#include "libgimpwidgets/gimpwidgets.h"
 
27
 
 
28
#include "widgets-types.h"
 
29
 
 
30
#include "core/gimpmarshal.h"
 
31
#include "core/gimptoolinfo.h"
 
32
 
 
33
#include "gimpoverlaydialog.h"
 
34
 
 
35
 
 
36
enum
 
37
{
 
38
  RESPONSE,
 
39
  CLOSE,
 
40
  LAST_SIGNAL
 
41
};
 
42
 
 
43
 
 
44
typedef struct _ResponseData ResponseData;
 
45
 
 
46
struct _ResponseData
 
47
{
 
48
  gint response_id;
 
49
};
 
50
 
 
51
 
 
52
static void       gimp_overlay_dialog_dispose       (GObject           *object);
 
53
 
 
54
static void       gimp_overlay_dialog_size_request  (GtkWidget         *widget,
 
55
                                                     GtkRequisition    *requisition);
 
56
static void       gimp_overlay_dialog_size_allocate (GtkWidget         *widget,
 
57
                                                     GtkAllocation     *allocation);
 
58
 
 
59
static void       gimp_overlay_dialog_forall        (GtkContainer      *container,
 
60
                                                     gboolean           include_internals,
 
61
                                                     GtkCallback        callback,
 
62
                                                     gpointer           callback_data);
 
63
 
 
64
static void       gimp_overlay_dialog_close         (GimpOverlayDialog *dialog);
 
65
 
 
66
static ResponseData * get_response_data             (GtkWidget         *widget,
 
67
                                                     gboolean          create);
 
68
 
 
69
 
 
70
G_DEFINE_TYPE (GimpOverlayDialog, gimp_overlay_dialog,
 
71
               GIMP_TYPE_OVERLAY_FRAME)
 
72
 
 
73
static guint signals[LAST_SIGNAL] = { 0, };
 
74
 
 
75
#define parent_class gimp_overlay_dialog_parent_class
 
76
 
 
77
 
 
78
static void
 
79
gimp_overlay_dialog_class_init (GimpOverlayDialogClass *klass)
 
80
{
 
81
  GObjectClass      *object_class    = G_OBJECT_CLASS (klass);
 
82
  GtkWidgetClass    *widget_class    = GTK_WIDGET_CLASS (klass);
 
83
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
 
84
 
 
85
  object_class->dispose       = gimp_overlay_dialog_dispose;
 
86
 
 
87
  widget_class->size_request  = gimp_overlay_dialog_size_request;
 
88
  widget_class->size_allocate = gimp_overlay_dialog_size_allocate;
 
89
 
 
90
  container_class->forall     = gimp_overlay_dialog_forall;
 
91
 
 
92
  klass->close                = gimp_overlay_dialog_close;
 
93
 
 
94
  signals[RESPONSE] =
 
95
    g_signal_new ("response",
 
96
                  G_OBJECT_CLASS_TYPE (klass),
 
97
                  G_SIGNAL_RUN_LAST,
 
98
                  G_STRUCT_OFFSET (GimpOverlayDialogClass, response),
 
99
                  NULL, NULL,
 
100
                  gimp_marshal_VOID__INT,
 
101
                  G_TYPE_NONE, 1,
 
102
                  G_TYPE_INT);
 
103
 
 
104
  signals[CLOSE] =
 
105
    g_signal_new ("close",
 
106
                  G_OBJECT_CLASS_TYPE (klass),
 
107
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
 
108
                  G_STRUCT_OFFSET (GimpOverlayDialogClass, close),
 
109
                  NULL, NULL,
 
110
                  gimp_marshal_VOID__VOID,
 
111
                  G_TYPE_NONE, 0);
 
112
 
 
113
  gtk_binding_entry_add_signal (gtk_binding_set_by_class (klass),
 
114
                                GDK_KEY_Escape, 0, "close", 0);
 
115
}
 
116
 
 
117
static void
 
118
gimp_overlay_dialog_init (GimpOverlayDialog *dialog)
 
119
{
 
120
  dialog->action_area = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
 
121
  gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog->action_area),
 
122
                             GTK_BUTTONBOX_END);
 
123
  gtk_widget_set_parent (dialog->action_area, GTK_WIDGET (dialog));
 
124
  gtk_widget_show (dialog->action_area);
 
125
}
 
126
 
 
127
static void
 
128
gimp_overlay_dialog_dispose (GObject *object)
 
129
{
 
130
  GimpOverlayDialog *dialog = GIMP_OVERLAY_DIALOG (object);
 
131
 
 
132
  if (dialog->action_area)
 
133
    {
 
134
      gtk_widget_unparent (dialog->action_area);
 
135
      dialog->action_area = NULL;
 
136
    }
 
137
 
 
138
  G_OBJECT_CLASS (parent_class)->dispose (object);
 
139
}
 
140
 
 
141
static void
 
142
gimp_overlay_dialog_size_request (GtkWidget      *widget,
 
143
                                  GtkRequisition *requisition)
 
144
{
 
145
  GtkContainer      *container = GTK_CONTAINER (widget);
 
146
  GimpOverlayDialog *dialog    = GIMP_OVERLAY_DIALOG (widget);
 
147
  GtkWidget         *child     = gtk_bin_get_child (GTK_BIN (widget));
 
148
  GtkRequisition     child_requisition;
 
149
  GtkRequisition     action_requisition;
 
150
  gint               border_width;
 
151
 
 
152
  border_width = gtk_container_get_border_width (container);
 
153
 
 
154
  requisition->width  = border_width * 2;
 
155
  requisition->height = border_width * 2;
 
156
 
 
157
  if (child && gtk_widget_get_visible (child))
 
158
    {
 
159
      gtk_widget_size_request (child, &child_requisition);
 
160
    }
 
161
  else
 
162
    {
 
163
      child_requisition.width  = 0;
 
164
      child_requisition.height = 0;
 
165
    }
 
166
 
 
167
  gtk_widget_size_request (dialog->action_area, &action_requisition);
 
168
 
 
169
  requisition->width  += MAX (child_requisition.width,
 
170
                              action_requisition.width);
 
171
  requisition->height += (child_requisition.height +
 
172
                          border_width +
 
173
                          action_requisition.height);
 
174
}
 
175
 
 
176
static void
 
177
gimp_overlay_dialog_size_allocate (GtkWidget     *widget,
 
178
                                   GtkAllocation *allocation)
 
179
{
 
180
  GtkContainer      *container = GTK_CONTAINER (widget);
 
181
  GimpOverlayDialog *dialog    = GIMP_OVERLAY_DIALOG (widget);
 
182
  GtkWidget         *child     = gtk_bin_get_child (GTK_BIN (widget));
 
183
  GtkRequisition     action_requisition;
 
184
  GtkAllocation      child_allocation = { 0, };
 
185
  GtkAllocation      action_allocation;
 
186
  gint               border_width;
 
187
 
 
188
  gtk_widget_set_allocation (widget, allocation);
 
189
 
 
190
  border_width = gtk_container_get_border_width (container);
 
191
 
 
192
  gtk_widget_size_request (dialog->action_area, &action_requisition);
 
193
 
 
194
  if (child && gtk_widget_get_visible (child))
 
195
    {
 
196
      child_allocation.x      = allocation->x + border_width;
 
197
      child_allocation.y      = allocation->y + border_width;
 
198
      child_allocation.width  = MAX (allocation->width  - 2 * border_width, 0);
 
199
      child_allocation.height = MAX (allocation->height -
 
200
                                     3 * border_width -
 
201
                                     action_requisition.height, 0);
 
202
 
 
203
      gtk_widget_size_allocate (child, &child_allocation);
 
204
    }
 
205
 
 
206
  action_allocation.x = allocation->x + border_width;
 
207
  action_allocation.y = (child_allocation.y + child_allocation.height +
 
208
                         border_width);
 
209
  action_allocation.width  = MAX (allocation->width  - 2 * border_width, 0);
 
210
  action_allocation.height = MAX (action_requisition.height, 0);
 
211
 
 
212
  gtk_widget_size_allocate (dialog->action_area, &action_allocation);
 
213
}
 
214
 
 
215
static void
 
216
gimp_overlay_dialog_forall (GtkContainer *container,
 
217
                            gboolean      include_internals,
 
218
                            GtkCallback   callback,
 
219
                            gpointer      callback_data)
 
220
{
 
221
  GTK_CONTAINER_CLASS (parent_class)->forall (container, include_internals,
 
222
                                              callback, callback_data);
 
223
 
 
224
  if (include_internals)
 
225
    {
 
226
      GimpOverlayDialog *dialog = GIMP_OVERLAY_DIALOG (container);
 
227
 
 
228
      if (dialog->action_area)
 
229
        (* callback) (dialog->action_area, callback_data);
 
230
    }
 
231
}
 
232
 
 
233
static void
 
234
gimp_overlay_dialog_close (GimpOverlayDialog *dialog)
 
235
{
 
236
  GList        *children;
 
237
  GList        *list;
 
238
  ResponseData *ad = NULL;
 
239
 
 
240
  children = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
 
241
 
 
242
  for (list = children; list; list = g_list_next (list))
 
243
    {
 
244
      GtkWidget *child = list->data;
 
245
 
 
246
      ad = get_response_data (child, FALSE);
 
247
 
 
248
      if (ad->response_id == GTK_RESPONSE_CLOSE ||
 
249
          ad->response_id == GTK_RESPONSE_CANCEL)
 
250
        {
 
251
          break;
 
252
        }
 
253
 
 
254
      ad = NULL;
 
255
    }
 
256
 
 
257
  g_list_free (children);
 
258
 
 
259
  if (ad)
 
260
    gimp_overlay_dialog_response (dialog, ad->response_id);
 
261
}
 
262
 
 
263
GtkWidget *
 
264
gimp_overlay_dialog_new (GimpToolInfo *tool_info,
 
265
                         const gchar  *desc,
 
266
                         ...)
 
267
{
 
268
  GtkWidget   *dialog;
 
269
  /* const gchar *stock_id; */
 
270
  va_list      args;
 
271
 
 
272
  g_return_val_if_fail (GIMP_IS_TOOL_INFO (tool_info), NULL);
 
273
 
 
274
  /* stock_id = gimp_viewable_get_stock_id (GIMP_VIEWABLE (tool_info)); */
 
275
 
 
276
  dialog = g_object_new (GIMP_TYPE_OVERLAY_DIALOG, NULL);
 
277
 
 
278
  va_start (args, desc);
 
279
  gimp_overlay_dialog_add_buttons_valist (GIMP_OVERLAY_DIALOG (dialog), args);
 
280
  va_end (args);
 
281
 
 
282
  return dialog;
 
283
}
 
284
 
 
285
void
 
286
gimp_overlay_dialog_response (GimpOverlayDialog *dialog,
 
287
                              gint               response_id)
 
288
{
 
289
  g_return_if_fail (GIMP_IS_OVERLAY_DIALOG (dialog));
 
290
 
 
291
  g_signal_emit (dialog, signals[RESPONSE], 0,
 
292
                 response_id);
 
293
}
 
294
 
 
295
void
 
296
gimp_overlay_dialog_add_buttons_valist (GimpOverlayDialog *dialog,
 
297
                                        va_list            args)
 
298
{
 
299
  const gchar *button_text;
 
300
  gint         response_id;
 
301
 
 
302
  g_return_if_fail (GIMP_IS_OVERLAY_DIALOG (dialog));
 
303
 
 
304
  while ((button_text = va_arg (args, const gchar *)))
 
305
    {
 
306
      response_id = va_arg (args, gint);
 
307
 
 
308
      gimp_overlay_dialog_add_button (dialog, button_text, response_id);
 
309
    }
 
310
}
 
311
 
 
312
static void
 
313
action_widget_activated (GtkWidget         *widget,
 
314
                         GimpOverlayDialog *dialog)
 
315
{
 
316
  ResponseData *ad = get_response_data (widget, FALSE);
 
317
 
 
318
  gimp_overlay_dialog_response (dialog, ad->response_id);
 
319
}
 
320
 
 
321
GtkWidget *
 
322
gimp_overlay_dialog_add_button (GimpOverlayDialog *dialog,
 
323
                                const gchar       *button_text,
 
324
                                gint               response_id)
 
325
{
 
326
  GtkWidget    *button;
 
327
  ResponseData *ad;
 
328
  guint         signal_id;
 
329
  GClosure     *closure;
 
330
 
 
331
  g_return_val_if_fail (GIMP_IS_OVERLAY_DIALOG (dialog), NULL);
 
332
  g_return_val_if_fail (button_text != NULL, NULL);
 
333
 
 
334
  button = gtk_button_new_from_stock (button_text);
 
335
 
 
336
  gtk_widget_set_can_default (button, TRUE);
 
337
 
 
338
  gtk_widget_show (button);
 
339
 
 
340
  ad = get_response_data (button, TRUE);
 
341
 
 
342
  ad->response_id = response_id;
 
343
 
 
344
  signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
 
345
 
 
346
  closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
 
347
                                   G_OBJECT (dialog));
 
348
  g_signal_connect_closure_by_id (button, signal_id, 0,
 
349
                                  closure, FALSE);
 
350
 
 
351
  gtk_box_pack_end (GTK_BOX (dialog->action_area), button, FALSE, TRUE, 0);
 
352
 
 
353
  if (response_id == GTK_RESPONSE_HELP)
 
354
    gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (dialog->action_area),
 
355
                                        button, TRUE);
 
356
 
 
357
  return button;
 
358
}
 
359
 
 
360
static void
 
361
response_data_free (gpointer data)
 
362
{
 
363
  g_slice_free (ResponseData, data);
 
364
}
 
365
 
 
366
static ResponseData *
 
367
get_response_data (GtkWidget *widget,
 
368
                   gboolean   create)
 
369
{
 
370
  ResponseData *ad = g_object_get_data (G_OBJECT (widget),
 
371
                                        "gimp-overlay-dialog-response-data");
 
372
 
 
373
  if (! ad && create)
 
374
    {
 
375
      ad = g_slice_new (ResponseData);
 
376
 
 
377
      g_object_set_data_full (G_OBJECT (widget),
 
378
                              "gimp-overlay-dialog-response-data",
 
379
                              ad, response_data_free);
 
380
    }
 
381
 
 
382
  return ad;
 
383
}