~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to app/widgets/gimppdbdialog.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * gimppdbdialog.c
 
5
 * Copyright (C) 2004 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 2 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, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <string.h>
 
25
 
 
26
#include <gtk/gtk.h>
 
27
 
 
28
#include "libgimpwidgets/gimpwidgets.h"
 
29
 
 
30
#include "widgets-types.h"
 
31
 
 
32
#include "core/gimpcontext.h"
 
33
 
 
34
#include "pdb/procedural_db.h"
 
35
 
 
36
#include "gimpmenufactory.h"
 
37
#include "gimppdbdialog.h"
 
38
 
 
39
#include "gimp-intl.h"
 
40
 
 
41
 
 
42
enum
 
43
{
 
44
  PROP_0,
 
45
  PROP_CONTEXT,
 
46
  PROP_SELECT_TYPE,
 
47
  PROP_INITIAL_OBJECT,
 
48
  PROP_CALLBACK_NAME,
 
49
  PROP_MENU_FACTORY
 
50
};
 
51
 
 
52
 
 
53
static void      gimp_pdb_dialog_class_init     (GimpPdbDialogClass *klass);
 
54
static void      gimp_pdb_dialog_init           (GimpPdbDialog      *dialog,
 
55
                                                 GimpPdbDialogClass *klass);
 
56
 
 
57
static GObject * gimp_pdb_dialog_constructor    (GType               type,
 
58
                                                 guint               n_params,
 
59
                                                 GObjectConstructParam *params);
 
60
static void      gimp_pdb_dialog_dispose        (GObject            *object);
 
61
static void      gimp_pdb_dialog_set_property   (GObject            *object,
 
62
                                                 guint               property_id,
 
63
                                                 const GValue       *value,
 
64
                                                 GParamSpec         *pspec);
 
65
 
 
66
static void      gimp_pdb_dialog_destroy        (GtkObject          *object);
 
67
 
 
68
static void      gimp_pdb_dialog_response       (GtkDialog          *dialog,
 
69
                                                 gint                response_id);
 
70
 
 
71
static void     gimp_pdb_dialog_context_changed (GimpContext        *context,
 
72
                                                 GimpObject         *object,
 
73
                                                 GimpPdbDialog      *dialog);
 
74
 
 
75
 
 
76
static GimpDialogClass *parent_class = NULL;
 
77
 
 
78
 
 
79
GType
 
80
gimp_pdb_dialog_get_type (void)
 
81
{
 
82
  static GType dialog_type = 0;
 
83
 
 
84
  if (! dialog_type)
 
85
    {
 
86
      static const GTypeInfo dialog_info =
 
87
      {
 
88
        sizeof (GimpPdbDialogClass),
 
89
        (GBaseInitFunc) NULL,
 
90
        (GBaseFinalizeFunc) NULL,
 
91
        (GClassInitFunc) gimp_pdb_dialog_class_init,
 
92
        NULL,           /* class_finalize */
 
93
        NULL,           /* class_data     */
 
94
        sizeof (GimpPdbDialog),
 
95
        0,              /* n_preallocs    */
 
96
        (GInstanceInitFunc) gimp_pdb_dialog_init,
 
97
      };
 
98
 
 
99
      dialog_type = g_type_register_static (GIMP_TYPE_DIALOG,
 
100
                                            "GimpPdbDialog",
 
101
                                            &dialog_info,
 
102
                                            G_TYPE_FLAG_ABSTRACT);
 
103
    }
 
104
 
 
105
  return dialog_type;
 
106
}
 
107
 
 
108
static void
 
109
gimp_pdb_dialog_class_init (GimpPdbDialogClass *klass)
 
110
{
 
111
  GObjectClass   *object_class     = G_OBJECT_CLASS (klass);
 
112
  GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (klass);
 
113
  GtkDialogClass *dialog_class     = GTK_DIALOG_CLASS (klass);
 
114
 
 
115
  parent_class = g_type_class_peek_parent (klass);
 
116
 
 
117
  object_class->constructor  = gimp_pdb_dialog_constructor;
 
118
  object_class->dispose      = gimp_pdb_dialog_dispose;
 
119
  object_class->set_property = gimp_pdb_dialog_set_property;
 
120
  object_class->set_property = gimp_pdb_dialog_set_property;
 
121
 
 
122
  gtk_object_class->destroy  = gimp_pdb_dialog_destroy;
 
123
 
 
124
  dialog_class->response     = gimp_pdb_dialog_response;
 
125
 
 
126
  klass->run_callback        = NULL;
 
127
 
 
128
  g_object_class_install_property (object_class, PROP_CONTEXT,
 
129
                                   g_param_spec_object ("context", NULL, NULL,
 
130
                                                        GIMP_TYPE_CONTEXT,
 
131
                                                        G_PARAM_WRITABLE |
 
132
                                                        G_PARAM_CONSTRUCT_ONLY));
 
133
 
 
134
  g_object_class_install_property (object_class, PROP_SELECT_TYPE,
 
135
                                   g_param_spec_pointer ("select-type",
 
136
                                                         NULL, NULL,
 
137
                                                         G_PARAM_WRITABLE |
 
138
                                                         G_PARAM_CONSTRUCT_ONLY));
 
139
 
 
140
  g_object_class_install_property (object_class, PROP_INITIAL_OBJECT,
 
141
                                   g_param_spec_object ("initial-object",
 
142
                                                        NULL, NULL,
 
143
                                                        GIMP_TYPE_OBJECT,
 
144
                                                        G_PARAM_WRITABLE |
 
145
                                                        G_PARAM_CONSTRUCT_ONLY));
 
146
 
 
147
  g_object_class_install_property (object_class, PROP_CALLBACK_NAME,
 
148
                                   g_param_spec_string ("callback-name",
 
149
                                                        NULL, NULL,
 
150
                                                        NULL,
 
151
                                                        G_PARAM_WRITABLE |
 
152
                                                        G_PARAM_CONSTRUCT_ONLY));
 
153
 
 
154
  g_object_class_install_property (object_class, PROP_MENU_FACTORY,
 
155
                                   g_param_spec_object ("menu-factory",
 
156
                                                        NULL, NULL,
 
157
                                                        GIMP_TYPE_MENU_FACTORY,
 
158
                                                        G_PARAM_WRITABLE |
 
159
                                                        G_PARAM_CONSTRUCT_ONLY));
 
160
}
 
161
 
 
162
static void
 
163
gimp_pdb_dialog_init (GimpPdbDialog      *dialog,
 
164
                      GimpPdbDialogClass *klass)
 
165
{
 
166
  klass->dialogs = g_list_prepend (klass->dialogs, dialog);
 
167
 
 
168
  gtk_dialog_add_button (GTK_DIALOG (dialog),
 
169
                         GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
 
170
}
 
171
 
 
172
static GObject *
 
173
gimp_pdb_dialog_constructor (GType                  type,
 
174
                             guint                  n_params,
 
175
                             GObjectConstructParam *params)
 
176
{
 
177
  GObject       *object;
 
178
  GimpPdbDialog *dialog;
 
179
  const gchar   *signal_name;
 
180
 
 
181
  object = G_OBJECT_CLASS (parent_class)->constructor (type, n_params, params);
 
182
 
 
183
  dialog = GIMP_PDB_DIALOG (object);
 
184
 
 
185
  g_assert (GIMP_IS_CONTEXT (dialog->caller_context));
 
186
  g_assert (g_type_is_a (dialog->select_type, GIMP_TYPE_OBJECT));
 
187
 
 
188
  dialog->context = gimp_context_new (dialog->caller_context->gimp,
 
189
                                      g_type_name (type),
 
190
                                      NULL);
 
191
 
 
192
  gimp_context_set_by_type (dialog->context, dialog->select_type,
 
193
                            dialog->initial_object);
 
194
 
 
195
  dialog->initial_object = NULL;
 
196
 
 
197
  signal_name = gimp_context_type_to_signal_name (dialog->select_type);
 
198
 
 
199
  g_signal_connect_object (dialog->context, signal_name,
 
200
                           G_CALLBACK (gimp_pdb_dialog_context_changed),
 
201
                           dialog, 0);
 
202
 
 
203
  return object;
 
204
}
 
205
 
 
206
static void
 
207
gimp_pdb_dialog_dispose (GObject *object)
 
208
{
 
209
  GimpPdbDialogClass *klass = GIMP_PDB_DIALOG_GET_CLASS (object);
 
210
 
 
211
  klass->dialogs = g_list_remove (klass->dialogs, object);
 
212
 
 
213
  G_OBJECT_CLASS (parent_class)->dispose (object);
 
214
}
 
215
 
 
216
static void
 
217
gimp_pdb_dialog_set_property (GObject      *object,
 
218
                              guint         property_id,
 
219
                              const GValue *value,
 
220
                              GParamSpec   *pspec)
 
221
{
 
222
  GimpPdbDialog *dialog = GIMP_PDB_DIALOG (object);
 
223
 
 
224
  switch (property_id)
 
225
    {
 
226
    case PROP_CONTEXT:
 
227
      dialog->caller_context = GIMP_CONTEXT (g_value_dup_object (value));
 
228
      break;
 
229
    case PROP_SELECT_TYPE:
 
230
      dialog->select_type = (GType) g_value_get_pointer (value);
 
231
      break;
 
232
    case PROP_INITIAL_OBJECT:
 
233
      /* don't ref, see constructor */
 
234
      dialog->initial_object = g_value_get_object (value);
 
235
      break;
 
236
    case PROP_CALLBACK_NAME:
 
237
      if (dialog->callback_name)
 
238
        g_free (dialog->callback_name);
 
239
      dialog->callback_name = g_value_dup_string (value);
 
240
      break;
 
241
    case PROP_MENU_FACTORY:
 
242
      dialog->menu_factory = (GimpMenuFactory *) g_value_dup_object (value);
 
243
      break;
 
244
    default:
 
245
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
246
      break;
 
247
    }
 
248
}
 
249
 
 
250
static void
 
251
gimp_pdb_dialog_destroy (GtkObject *object)
 
252
{
 
253
  GimpPdbDialog *dialog = GIMP_PDB_DIALOG (object);
 
254
 
 
255
  if (dialog->caller_context)
 
256
    {
 
257
      g_object_unref (dialog->caller_context);
 
258
      dialog->caller_context = NULL;
 
259
    }
 
260
 
 
261
  if (dialog->context)
 
262
    {
 
263
      g_object_unref (dialog->context);
 
264
      dialog->context = NULL;
 
265
    }
 
266
 
 
267
  if (dialog->callback_name)
 
268
    {
 
269
      g_free (dialog->callback_name);
 
270
      dialog->callback_name = NULL;
 
271
    }
 
272
 
 
273
  if (dialog->menu_factory)
 
274
    {
 
275
      g_object_unref (dialog->menu_factory);
 
276
      dialog->menu_factory = NULL;
 
277
    }
 
278
 
 
279
  GTK_OBJECT_CLASS (parent_class)->destroy (object);
 
280
}
 
281
 
 
282
static void
 
283
gimp_pdb_dialog_response (GtkDialog *gtk_dialog,
 
284
                          gint       response_id)
 
285
{
 
286
  GimpPdbDialog *dialog = GIMP_PDB_DIALOG (gtk_dialog);
 
287
 
 
288
  gimp_pdb_dialog_run_callback (dialog, TRUE);
 
289
  gtk_widget_destroy (GTK_WIDGET (dialog));
 
290
}
 
291
 
 
292
void
 
293
gimp_pdb_dialog_run_callback (GimpPdbDialog *dialog,
 
294
                              gboolean       closing)
 
295
{
 
296
  GimpPdbDialogClass *klass = GIMP_PDB_DIALOG_GET_CLASS (dialog);
 
297
  GimpObject         *object;
 
298
 
 
299
  object = gimp_context_get_by_type (dialog->context, dialog->select_type);
 
300
 
 
301
  if (object                &&
 
302
      klass->run_callback   &&
 
303
      dialog->callback_name &&
 
304
      ! dialog->callback_busy)
 
305
    {
 
306
      dialog->callback_busy = TRUE;
 
307
 
 
308
      if (procedural_db_lookup (dialog->caller_context->gimp,
 
309
                                dialog->callback_name))
 
310
        {
 
311
          Argument *return_vals;
 
312
          gint      n_return_vals;
 
313
 
 
314
          return_vals = klass->run_callback (dialog, object, closing,
 
315
                                             &n_return_vals);
 
316
 
 
317
          if (! return_vals ||
 
318
              return_vals[0].value.pdb_int != GIMP_PDB_SUCCESS)
 
319
            {
 
320
              g_message (_("Unable to run %s callback. "
 
321
                           "The corresponding plug-in may have crashed."),
 
322
                         g_type_name (G_TYPE_FROM_INSTANCE (dialog)));
 
323
            }
 
324
 
 
325
          if (return_vals)
 
326
            procedural_db_destroy_args (return_vals, n_return_vals);
 
327
        }
 
328
 
 
329
      dialog->callback_busy = FALSE;
 
330
    }
 
331
}
 
332
 
 
333
GimpPdbDialog *
 
334
gimp_pdb_dialog_get_by_callback (GimpPdbDialogClass *klass,
 
335
                                 const gchar        *callback_name)
 
336
{
 
337
  GList *list;
 
338
 
 
339
  g_return_val_if_fail (GIMP_IS_PDB_DIALOG_CLASS (klass), NULL);
 
340
  g_return_val_if_fail (callback_name != NULL, NULL);
 
341
 
 
342
  for (list = klass->dialogs; list; list = g_list_next (list))
 
343
    {
 
344
      GimpPdbDialog *dialog = list->data;
 
345
 
 
346
      if (dialog->callback_name &&
 
347
          ! strcmp (callback_name, dialog->callback_name))
 
348
        return dialog;
 
349
    }
 
350
 
 
351
  return NULL;
 
352
}
 
353
 
 
354
void
 
355
gimp_pdb_dialogs_check_callback (GimpPdbDialogClass *klass)
 
356
{
 
357
  GList *list;
 
358
 
 
359
  g_return_if_fail (GIMP_IS_PDB_DIALOG_CLASS (klass));
 
360
 
 
361
  list = klass->dialogs;
 
362
 
 
363
  while (list)
 
364
    {
 
365
      GimpPdbDialog *dialog = list->data;
 
366
 
 
367
      list = g_list_next (list);
 
368
 
 
369
      if (dialog->caller_context && dialog->callback_name)
 
370
        {
 
371
          if (! procedural_db_lookup (dialog->caller_context->gimp,
 
372
                                      dialog->callback_name))
 
373
            {
 
374
              gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE);
 
375
            }
 
376
        }
 
377
    }
 
378
}
 
379
 
 
380
 
 
381
/*  private functions  */
 
382
 
 
383
static void
 
384
gimp_pdb_dialog_context_changed (GimpContext   *context,
 
385
                                 GimpObject    *object,
 
386
                                 GimpPdbDialog *dialog)
 
387
{
 
388
  if (object)
 
389
    gimp_pdb_dialog_run_callback (dialog, FALSE);
 
390
}