~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/print/print.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2008-10-06 13:30:41 UTC
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: james.westby@ubuntu.com-20081006133041-3panbkcanaymfsmp
Tags: upstream-2.6.0
ImportĀ upstreamĀ versionĀ 2.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include "print.h"
27
27
#include "print-settings.h"
28
28
#include "print-page-layout.h"
 
29
#include "print-page-setup.h"
29
30
#include "print-draw-page.h"
30
31
 
31
32
#include "libgimp/stdplugins-intl.h"
32
33
 
33
34
 
34
 
#define PRINT_PROC_NAME  "file-print-gtk"
35
 
#define PLUG_IN_BINARY   "print"
 
35
#define PLUG_IN_BINARY        "print"
 
36
 
 
37
#define PRINT_PROC_NAME       "file-print-gtk"
 
38
#define PAGE_SETUP_PROC_NAME  "file-print-gtk-page-setup"
 
39
#define PRINT_TEMP_PROC_NAME  "file-print-gtk-page-setup-notify-temp"
36
40
 
37
41
 
38
42
static void        query (void);
42
46
                          gint              *nreturn_vals,
43
47
                          GimpParam        **return_vals);
44
48
 
45
 
static gboolean    print_image              (gint32             image_ID,
46
 
                                             gboolean           interactive);
 
49
static GimpPDBStatusType  print_image       (gint32             image_ID,
 
50
                                             gboolean           interactive,
 
51
                                             GError           **error);
 
52
static GimpPDBStatusType  page_setup        (gint32             image_ID);
47
53
 
48
 
static void        print_show_error         (const gchar       *message,
49
 
                                             gboolean           interactive);
 
54
static void        print_show_error         (const gchar       *message);
50
55
static void        print_operation_set_name (GtkPrintOperation *operation,
51
56
                                             gint               image_ID);
52
57
 
55
60
                                             PrintData         *data);
56
61
static void        end_print                (GtkPrintOperation *operation,
57
62
                                             GtkPrintContext   *context,
58
 
                                             gint32            *image_ID);
 
63
                                             gint32            *layer_ID);
59
64
static void        draw_page                (GtkPrintOperation *print,
60
65
                                             GtkPrintContext   *context,
61
66
                                             gint               page_nr,
64
69
static GtkWidget * create_custom_widget     (GtkPrintOperation *operation,
65
70
                                             PrintData         *data);
66
71
 
 
72
static gchar     * print_temp_proc_name     (gint32             image_ID);
 
73
static gchar     * print_temp_proc_install  (gint32             image_ID);
 
74
 
 
75
 
 
76
/*  Keep a reference to the current GtkPrintOperation
 
77
 *  for access by the temporary procedure.
 
78
 */
 
79
static GtkPrintOperation *print_operation = NULL;
67
80
 
68
81
 
69
82
const GimpPlugInInfo PLUG_IN_INFO =
89
102
                          N_("Print the image"),
90
103
                          "Print the image using the GTK+ Print API.",
91
104
                          "Bill Skaggs, Sven Neumann, Stefan Rƶllin",
92
 
                          "Bill Skaggs  <weskaggs@primate.ucdavis.edu>",
 
105
                          "Bill Skaggs <weskaggs@primate.ucdavis.edu>",
93
106
                          "2006, 2007",
94
107
                          N_("_Print..."),
95
108
                          "*",
101
114
  gimp_plugin_icon_register (PRINT_PROC_NAME, GIMP_ICON_TYPE_STOCK_ID,
102
115
                             (const guint8 *) GTK_STOCK_PRINT);
103
116
 
 
117
  gimp_install_procedure (PAGE_SETUP_PROC_NAME,
 
118
                          N_("Adjust page size and orientation for printing"),
 
119
                          "Adjust page size and orientation for printing the "
 
120
                          "image using the GTK+ Print API.",
 
121
                          "Bill Skaggs, Sven Neumann, Stefan Rƶllin",
 
122
                          "Sven Neumann <sven@gimp.org>",
 
123
                          "2008",
 
124
                          N_("Page Set_up"),
 
125
                          "*",
 
126
                          GIMP_PLUGIN,
 
127
                          G_N_ELEMENTS (print_args), 0,
 
128
                          print_args, NULL);
 
129
 
 
130
  gimp_plugin_menu_register (PAGE_SETUP_PROC_NAME, "<Image>/File/Send");
 
131
 
 
132
#if GTK_CHECK_VERSION(2,13,0)
 
133
  gimp_plugin_icon_register (PAGE_SETUP_PROC_NAME, GIMP_ICON_TYPE_STOCK_ID,
 
134
                             (const guint8 *) GTK_STOCK_PAGE_SETUP);
 
135
#endif
104
136
}
105
137
 
106
138
static void
110
142
     gint             *nreturn_vals,
111
143
     GimpParam       **return_vals)
112
144
{
113
 
  static GimpParam  values[2];
114
 
  GimpRunMode       run_mode;
115
 
  GimpPDBStatusType status = GIMP_PDB_SUCCESS;
116
 
  gint32            image_ID;
 
145
  static GimpParam   values[2];
 
146
  GimpRunMode        run_mode;
 
147
  GimpPDBStatusType  status;
 
148
  gint32             image_ID;
 
149
  GError            *error = NULL;
117
150
 
118
151
  run_mode = param[0].data.d_int32;
119
152
 
122
155
  *nreturn_vals = 1;
123
156
  *return_vals  = values;
124
157
 
 
158
  g_thread_init (NULL);
 
159
 
125
160
  values[0].type          = GIMP_PDB_STATUS;
126
161
  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
127
162
 
129
164
 
130
165
  if (strcmp (name, PRINT_PROC_NAME) == 0)
131
166
    {
132
 
      g_thread_init (NULL);
133
 
 
134
 
      gimp_ui_init (PLUG_IN_BINARY, FALSE);
135
 
 
136
 
      if (! print_image (image_ID, run_mode == GIMP_RUN_INTERACTIVE))
137
 
        {
138
 
          status = GIMP_PDB_EXECUTION_ERROR;
 
167
      status = print_image (image_ID, run_mode == GIMP_RUN_INTERACTIVE, &error);
 
168
 
 
169
      if (error && run_mode == GIMP_RUN_INTERACTIVE)
 
170
        {
 
171
          print_show_error (error->message);
 
172
          g_clear_error (&error);
 
173
        }
 
174
    }
 
175
  else if (strcmp (name, PAGE_SETUP_PROC_NAME) == 0)
 
176
    {
 
177
      if (run_mode == GIMP_RUN_INTERACTIVE)
 
178
        {
 
179
          status = page_setup (image_ID);
 
180
        }
 
181
      else
 
182
        {
 
183
          status = GIMP_PDB_CALLING_ERROR;
139
184
        }
140
185
    }
141
186
  else
143
188
      status = GIMP_PDB_CALLING_ERROR;
144
189
    }
145
190
 
 
191
  if (status != GIMP_PDB_SUCCESS && error)
 
192
    {
 
193
      *nreturn_vals = 2;
 
194
      values[1].type          = GIMP_PDB_STRING;
 
195
      values[1].data.d_string = error->message;
 
196
    }
 
197
 
146
198
  values[0].data.d_status = status;
147
199
}
148
200
 
149
 
static gboolean
150
 
print_image (gint32    image_ID,
151
 
             gboolean  interactive)
 
201
static GimpPDBStatusType
 
202
print_image (gint32     image_ID,
 
203
             gboolean   interactive,
 
204
             GError   **error)
152
205
{
153
 
  GtkPrintOperation *operation;
154
 
  GError            *error         = NULL;
155
 
  gint32             orig_image_ID = image_ID;
156
 
  gint32             drawable_ID   = gimp_image_get_active_drawable (image_ID);
157
 
  PrintData          data;
158
 
  GimpExportReturn   export;
159
 
 
160
 
  /* export the image */
161
 
  export = gimp_export_image (&image_ID, &drawable_ID, NULL,
162
 
                              GIMP_EXPORT_CAN_HANDLE_RGB   |
163
 
                              GIMP_EXPORT_CAN_HANDLE_ALPHA);
164
 
 
165
 
  if (export == GIMP_EXPORT_CANCEL)
166
 
    return FALSE;
 
206
  GtkPrintOperation       *operation;
 
207
  GtkPrintOperationResult  result;
 
208
  gchar                   *temp_proc;
 
209
  gint32                   layer;
 
210
  PrintData                data;
 
211
 
 
212
  /*  create a print layer from the projection  */
 
213
  layer = gimp_layer_new_from_visible (image_ID, image_ID, PRINT_PROC_NAME);
167
214
 
168
215
  operation = gtk_print_operation_new ();
169
216
 
170
 
  print_operation_set_name (operation, orig_image_ID);
 
217
  gtk_print_operation_set_n_pages (operation, 1);
 
218
  print_operation_set_name (operation, image_ID);
 
219
 
 
220
  print_page_setup_load (operation, image_ID);
171
221
 
172
222
  /* fill in the PrintData struct */
173
 
  data.num_pages     = 1;
174
 
  data.image_id      = orig_image_ID;
175
 
  data.drawable_id   = drawable_ID;
 
223
  data.image_id      = image_ID;
 
224
  data.drawable_id   = layer;
176
225
  data.unit          = gimp_get_default_unit ();
177
226
  data.image_unit    = gimp_image_get_unit (image_ID);
178
227
  data.offset_x      = 0;
183
232
 
184
233
  gimp_image_get_resolution (image_ID, &data.xres, &data.yres);
185
234
 
186
 
  load_print_settings (&data);
187
 
 
188
 
  if (export != GIMP_EXPORT_EXPORT)
189
 
    image_ID = -1;
 
235
  print_settings_load (&data);
190
236
 
191
237
  gtk_print_operation_set_unit (operation, GTK_UNIT_POINTS);
192
238
 
200
246
                    G_CALLBACK (end_print),
201
247
                    &image_ID);
202
248
 
 
249
  print_operation = operation;
 
250
  temp_proc = print_temp_proc_install (image_ID);
 
251
  gimp_extension_enable ();
 
252
 
203
253
  if (interactive)
204
254
    {
 
255
      gimp_ui_init (PLUG_IN_BINARY, FALSE);
 
256
 
205
257
      g_signal_connect_swapped (operation, "end-print",
206
 
                                G_CALLBACK (save_print_settings),
 
258
                                G_CALLBACK (print_settings_save),
207
259
                                &data);
208
260
 
209
261
      g_signal_connect (operation, "create-custom-widget",
212
264
 
213
265
      gtk_print_operation_set_custom_tab_label (operation, _("Image Settings"));
214
266
 
215
 
      gtk_print_operation_run (operation,
216
 
                               GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
217
 
                               NULL, &error);
 
267
      result = gtk_print_operation_run (operation,
 
268
                                        GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
 
269
                                        NULL, error);
218
270
    }
219
271
  else
220
272
    {
221
 
      gtk_print_operation_run (operation,
222
 
                               GTK_PRINT_OPERATION_ACTION_PRINT,
223
 
                               NULL, &error);
 
273
      result = gtk_print_operation_run (operation,
 
274
                                        GTK_PRINT_OPERATION_ACTION_PRINT,
 
275
                                        NULL, error);
224
276
    }
225
277
 
 
278
  gimp_uninstall_temp_proc (temp_proc);
 
279
  g_free (temp_proc);
 
280
  print_operation = NULL;
 
281
 
226
282
  g_object_unref (operation);
227
283
 
228
 
  if (gimp_image_is_valid (image_ID))
229
 
    gimp_image_delete (image_ID);
 
284
  if (gimp_drawable_is_valid (layer))
 
285
    gimp_drawable_delete (layer);
230
286
 
231
 
  if (error)
 
287
  switch (result)
232
288
    {
233
 
      print_show_error (error->message, interactive);
234
 
      g_error_free (error);
 
289
    case GTK_PRINT_OPERATION_RESULT_APPLY:
 
290
    case GTK_PRINT_OPERATION_RESULT_IN_PROGRESS:
 
291
      return GIMP_PDB_SUCCESS;
 
292
 
 
293
    case GTK_PRINT_OPERATION_RESULT_CANCEL:
 
294
      return GIMP_PDB_CANCEL;
 
295
 
 
296
    case GTK_PRINT_OPERATION_RESULT_ERROR:
 
297
      return GIMP_PDB_EXECUTION_ERROR;
235
298
    }
236
299
 
237
 
  return TRUE;
 
300
  return GIMP_PDB_EXECUTION_ERROR;
 
301
}
 
302
 
 
303
static GimpPDBStatusType
 
304
page_setup (gint32 image_ID)
 
305
{
 
306
  GtkPrintOperation  *operation;
 
307
  GimpParam          *return_vals;
 
308
  gchar              *name;
 
309
  gint                n_return_vals;
 
310
 
 
311
  gimp_ui_init (PLUG_IN_BINARY, FALSE);
 
312
 
 
313
  operation = gtk_print_operation_new ();
 
314
 
 
315
  print_page_setup_load (operation, image_ID);
 
316
  print_page_setup_dialog (operation);
 
317
  print_page_setup_save (operation, image_ID);
 
318
 
 
319
  g_object_unref (operation);
 
320
 
 
321
  /* now notify a running print procedure about this change */
 
322
  name = print_temp_proc_name (image_ID);
 
323
 
 
324
  /* we don't want the core to show an error message if the
 
325
   * temporary procedure does not exist
 
326
   */
 
327
  gimp_plugin_set_pdb_error_handler (GIMP_PDB_ERROR_HANDLER_PLUGIN);
 
328
 
 
329
  return_vals = gimp_run_procedure (name,
 
330
                                    &n_return_vals,
 
331
                                    GIMP_PDB_IMAGE, image_ID,
 
332
                                    GIMP_PDB_END);
 
333
  gimp_destroy_params (return_vals, n_return_vals);
 
334
 
 
335
  g_free (name);
 
336
 
 
337
  return GIMP_PDB_SUCCESS;
238
338
}
239
339
 
240
340
static void
241
 
print_show_error (const gchar *message,
242
 
                  gboolean     interactive)
 
341
print_show_error (const gchar *message)
243
342
{
244
 
  g_printerr ("Print: %s\n", message);
245
 
 
246
 
  if (interactive)
247
 
    {
248
 
      GtkWidget *dialog;
249
 
 
250
 
      dialog = gtk_message_dialog_new (NULL, 0,
251
 
                                       GTK_MESSAGE_ERROR,
252
 
                                       GTK_BUTTONS_OK,
253
 
                                       _("An error occurred "
254
 
                                         "while trying to print:"));
255
 
 
256
 
      gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
257
 
                                                message);
258
 
 
259
 
      gtk_dialog_run (GTK_DIALOG (dialog));
260
 
      gtk_widget_destroy (dialog);
261
 
    }
 
343
  GtkWidget *dialog;
 
344
 
 
345
  dialog = gtk_message_dialog_new (NULL, 0,
 
346
                                   GTK_MESSAGE_ERROR,
 
347
                                   GTK_BUTTONS_OK,
 
348
                                   _("An error occurred while trying to print:"));
 
349
 
 
350
  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
 
351
                                            message);
 
352
 
 
353
  gtk_dialog_run (GTK_DIALOG (dialog));
 
354
  gtk_widget_destroy (dialog);
262
355
}
263
356
 
264
357
static void
277
370
             GtkPrintContext   *context,
278
371
             PrintData         *data)
279
372
{
280
 
  gtk_print_operation_set_n_pages (operation, data->num_pages);
281
373
  gtk_print_operation_set_use_full_page (operation, data->use_full_page);
282
374
 
283
375
  gimp_progress_init (_("Printing"));
286
378
static void
287
379
end_print (GtkPrintOperation *operation,
288
380
           GtkPrintContext   *context,
289
 
           gint32            *image_ID)
 
381
           gint32            *layer_ID)
290
382
{
291
 
  /* we don't need the export image any longer, delete it */
292
 
  if (gimp_image_is_valid (*image_ID))
 
383
  /* we don't need the print layer any longer, delete it */
 
384
  if (gimp_drawable_is_valid (*layer_ID))
293
385
    {
294
 
      gimp_image_delete (*image_ID);
295
 
      *image_ID = -1;
 
386
      gimp_drawable_delete (*layer_ID);
 
387
      *layer_ID = -1;
296
388
    }
297
389
 
298
390
  gimp_progress_end ();
299
391
 
300
392
  /* generate events to solve the problems described in bug #466928 */
301
 
  g_timeout_add (1000, (GSourceFunc) gtk_true, NULL);
 
393
  g_timeout_add_seconds (1, (GSourceFunc) gtk_true, NULL);
302
394
}
303
395
 
304
396
static void
307
399
           gint               page_nr,
308
400
           PrintData         *data)
309
401
{
310
 
  draw_page_cairo (context, data);
 
402
  print_draw_page (context, data);
 
403
 
 
404
  gimp_progress_update (1.0);
311
405
}
312
406
 
313
407
/*
318
412
create_custom_widget (GtkPrintOperation *operation,
319
413
                      PrintData         *data)
320
414
{
321
 
  return print_page_layout_gui (data);
 
415
  return print_page_layout_gui (data, PRINT_PROC_NAME);
 
416
}
 
417
 
 
418
static void
 
419
print_temp_proc_run (const gchar      *name,
 
420
                     gint              nparams,
 
421
                     const GimpParam  *param,
 
422
                     gint             *nreturn_vals,
 
423
                     GimpParam       **return_vals)
 
424
{
 
425
  static GimpParam  values[1];
 
426
 
 
427
  values[0].type          = GIMP_PDB_STATUS;
 
428
  values[0].data.d_status = GIMP_PDB_SUCCESS;
 
429
 
 
430
  *nreturn_vals = 1;
 
431
  *return_vals  = values;
 
432
 
 
433
  if (print_operation && nparams == 1)
 
434
    print_page_setup_load (print_operation, param[0].data.d_int32);
 
435
}
 
436
 
 
437
static gchar *
 
438
print_temp_proc_name (gint32 image_ID)
 
439
{
 
440
  return g_strdup_printf (PRINT_TEMP_PROC_NAME "-%d", image_ID);
 
441
}
 
442
 
 
443
static gchar *
 
444
print_temp_proc_install (gint32  image_ID)
 
445
{
 
446
  static const GimpParamDef args[] =
 
447
  {
 
448
    { GIMP_PDB_IMAGE, "image", "Image to print" }
 
449
  };
 
450
 
 
451
  gchar *name = print_temp_proc_name (image_ID);
 
452
 
 
453
  gimp_install_temp_proc (name,
 
454
                          "DON'T USE THIS ONE",
 
455
                          "Temporary procedure to notify the Print plug-in "
 
456
                          "about changes to the Page Setup.",
 
457
                          "Sven Neumann",
 
458
                          "Sven Neumann",
 
459
                          "2008",
 
460
                          NULL,
 
461
                          "",
 
462
                          GIMP_TEMPORARY,
 
463
                          G_N_ELEMENTS (args), 0, args, NULL,
 
464
                          print_temp_proc_run);
 
465
 
 
466
  return name;
322
467
}