gtkprintoperation-unix.c 12.5 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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* GTK - The GIMP Toolkit
 * gtkprintoperation-unix.c: Print Operation Details for Unix and Unix like platforms
 * Copyright (C) 2006, Red Hat, Inc.
 *
 * 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 2 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
 * Lesser 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

#include "gtkprintoperation-private.h"
#include "gtkmarshal.h"
#include "gtkmessagedialog.h"

#include "gtkprintunixdialog.h"
#include "gtkpagesetupunixdialog.h"
#include "gtkprintbackend.h"
#include "gtkprinter.h"
#include "gtkprintjob.h"
#include "gtkalias.h"
39
#include "gtkintl.h"
40
41
42
43

typedef struct {
  GtkPrintJob *job;         /* the job we are sending to the printer */
  gulong job_status_changed_tag;
Matthias Clasen's avatar
Matthias Clasen committed
44
  GtkWindow *parent;        /* just in case we need to throw error dialogs */
45
46
47
48
} GtkPrintOperationUnix;

static void
unix_start_page (GtkPrintOperation *op,
Matthias Clasen's avatar
Matthias Clasen committed
49
50
		 GtkPrintContext   *print_context,
		 GtkPageSetup      *page_setup)
51
52
53
54
55
56
{

}

static void
unix_end_page (GtkPrintOperation *op,
Matthias Clasen's avatar
Matthias Clasen committed
57
	       GtkPrintContext   *print_context)
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{
  cairo_t *cr;

  cr = gtk_print_context_get_cairo (print_context);
  cairo_show_page (cr);
}

static void
op_unix_free (GtkPrintOperationUnix *op_unix)
{
  if (op_unix->job)
    {
      g_signal_handler_disconnect (op_unix->job,
				   op_unix->job_status_changed_tag);
      g_object_unref (op_unix->job);
    }

  g_free (op_unix);
}

static void
unix_finish_send  (GtkPrintJob *job,
Matthias Clasen's avatar
Matthias Clasen committed
80
81
                   void        *user_data, 
                   GError      *error)
82
83
84
85
86
87
88
89
{
  GtkPrintOperationUnix *op_unix;

  op_unix = (GtkPrintOperationUnix *) user_data;

  if (error != NULL)
    {
      GtkWidget *edialog;
90
      edialog = gtk_message_dialog_new (op_unix->parent, 
91
92
93
                                        GTK_DIALOG_DESTROY_WITH_PARENT,
                                        GTK_MESSAGE_ERROR,
                                        GTK_BUTTONS_CLOSE,
94
95
96
97
98
99
100
101
                                        _("Error printing") /* FIXME better text */);
      gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (edialog),
                                                "%s", error->message);
      gtk_window_set_modal (GTK_WINDOW (edialog), TRUE);
      g_signal_connect (edialog, "response",
                        G_CALLBACK (gtk_widget_destroy), NULL);

      gtk_window_present (GTK_WINDOW (edialog));
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    }
}

static void
unix_end_run (GtkPrintOperation *op)
{
  GtkPrintOperationUnix *op_unix = op->priv->platform_data;
 
  /* TODO: Check for error */
  gtk_print_job_send (op_unix->job,
                      unix_finish_send, 
                      op_unix, NULL,
		      NULL);
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
118
119
job_status_changed_cb (GtkPrintJob       *job, 
		       GtkPrintOperation *op)
120
121
122
123
{
  _gtk_print_operation_set_status (op, gtk_print_job_get_status (job), NULL);
}

124
125
126
127

static GtkWidget *
get_print_dialog (GtkPrintOperation *op,
                  GtkWindow         *parent)
128
{
129
  GtkPrintOperationPrivate *priv = op->priv;
130
131
132
133
134
  GtkWidget *pd;
  GtkPageSetup *page_setup;

  pd = gtk_print_unix_dialog_new (NULL, parent);

135
  if (priv->print_settings)
136
    gtk_print_unix_dialog_set_settings (GTK_PRINT_UNIX_DIALOG (pd),
137
138
139
					priv->print_settings);
  if (priv->default_page_setup)
    page_setup = gtk_page_setup_copy (priv->default_page_setup);
140
141
142
143
144
145
  else
    page_setup = gtk_page_setup_new ();

  gtk_print_unix_dialog_set_page_setup (GTK_PRINT_UNIX_DIALOG (pd), 
                                        page_setup);
  g_object_unref (page_setup);
146

147
148
  return pd;
}
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
typedef struct {
  GtkPrintOperation           *op;
  gboolean                     do_print;
  GError                     **error;
  GtkPrintOperationResult      result;
  GtkPrintOperationPrintFunc   print_cb;
  GDestroyNotify               destroy;
} PrintResponseData;

static void
print_response_data_free (gpointer data)
{
  PrintResponseData *rdata = data;

  g_object_unref (rdata->op);
  g_free (rdata);
}

static void
handle_print_response (GtkWidget *dialog,
		       gint       response,
		       gpointer   data)
{
  GtkPrintUnixDialog *pd = GTK_PRINT_UNIX_DIALOG (dialog);
  PrintResponseData *rdata = data;
  GtkPrintOperation *op = rdata->op;
176
  GtkPrintOperationPrivate *priv = op->priv;
177
178

  if (response == GTK_RESPONSE_OK)
179
180
181
182
    {
      GtkPrintOperationUnix *op_unix;
      GtkPrinter *printer;
      GtkPrintSettings *settings;
183
184
185
      GtkPageSetup *page_setup;

      rdata->result = GTK_PRINT_OPERATION_RESULT_APPLY;
186
187
188
189
190

      printer = gtk_print_unix_dialog_get_selected_printer (GTK_PRINT_UNIX_DIALOG (pd));
      if (printer == NULL)
	goto out;
      
191
      rdata->do_print = TRUE;
192
193

      settings = gtk_print_unix_dialog_get_settings (GTK_PRINT_UNIX_DIALOG (pd));
194
195
      page_setup = gtk_print_unix_dialog_get_page_setup (GTK_PRINT_UNIX_DIALOG (pd));

196
197
198
      gtk_print_operation_set_print_settings (op, settings);

      op_unix = g_new0 (GtkPrintOperationUnix, 1);
199
      op_unix->job = gtk_print_job_new (priv->job_name,
200
201
202
203
					printer,
					settings,
					page_setup);
      g_object_unref (settings);
204
205
  
      rdata->op->priv->surface = gtk_print_job_get_surface (op_unix->job, rdata->error);
206
207
      if (op->priv->surface == NULL)
        {
208
	  rdata->do_print = FALSE;
209
	  op_unix_free (op_unix);
210
	  rdata->result = GTK_PRINT_OPERATION_RESULT_ERROR;
211
212
213
214
215
216
217
218
	  goto out;
	}

      _gtk_print_operation_set_status (op, gtk_print_job_get_status (op_unix->job), NULL);
      op_unix->job_status_changed_tag =
	g_signal_connect (op_unix->job, "status_changed",
			  G_CALLBACK (job_status_changed_cb), op);
      
219
      op_unix->parent = gtk_window_get_transient_for (GTK_WINDOW (pd));
220

221
222
      priv->dpi_x = 72;
      priv->dpi_y = 72;
223
 
224
225
      priv->platform_data = op_unix;
      priv->free_platform_data = (GDestroyNotify) op_unix_free;
226

227
228
229
      priv->print_pages = op_unix->job->print_pages;
      priv->page_ranges = op_unix->job->page_ranges;
      priv->num_page_ranges = op_unix->job->num_page_ranges;
230
  
231
232
233
234
235
236
      priv->manual_num_copies = op_unix->job->num_copies;
      priv->manual_collation = op_unix->job->collate;
      priv->manual_reverse = op_unix->job->reverse;
      priv->manual_page_set = op_unix->job->page_set;
      priv->manual_scale = op_unix->job->scale;
      priv->manual_orientation = op_unix->job->rotate_to_orientation;
237
238
    } 

239
240
241
  priv->start_page = unix_start_page;
  priv->end_page = unix_end_page;
  priv->end_run = unix_end_run;
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
 out:  
  gtk_widget_destroy (GTK_WIDGET (pd));

  if (rdata->print_cb)
    {
      if (rdata->do_print)
        rdata->print_cb (op); 
      else
       _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL); 
    }

  if (rdata->destroy)
    rdata->destroy (rdata);
}

void
_gtk_print_operation_platform_backend_run_dialog_async (GtkPrintOperation          *op,
                                                        GtkWindow                  *parent,
							GtkPrintOperationPrintFunc  print_cb)
{
  GtkWidget *pd;
  PrintResponseData *rdata;

  rdata = g_new (PrintResponseData, 1);
  rdata->op = g_object_ref (op);
  rdata->do_print = FALSE;
  rdata->result = GTK_PRINT_OPERATION_RESULT_CANCEL;
  rdata->error = NULL;
  rdata->print_cb = print_cb;
  rdata->destroy = print_response_data_free;
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
  pd = get_print_dialog (op, parent);
  gtk_window_set_modal (GTK_WINDOW (pd), TRUE);

  g_signal_connect (pd, "response", 
		    G_CALLBACK (handle_print_response), rdata);

  gtk_window_present (GTK_WINDOW (pd));
}

GtkPrintOperationResult
_gtk_print_operation_platform_backend_run_dialog (GtkPrintOperation *op,
						  GtkWindow         *parent,
						  gboolean          *do_print,
						  GError           **error)
 {
  GtkWidget *pd;
  PrintResponseData rdata;
  gint response;  
   
  rdata.op = op;
  rdata.do_print = FALSE;
  rdata.result = GTK_PRINT_OPERATION_RESULT_CANCEL;
  rdata.error = error;
  rdata.print_cb = NULL;
  rdata.destroy = NULL;

  pd = get_print_dialog (op, parent);

  response = gtk_dialog_run (GTK_DIALOG (pd));
  handle_print_response (pd, response, &rdata);

  *do_print = rdata.do_print;

  return rdata.result;
}


typedef struct {
312
313
314
315
  GtkPageSetup         *page_setup;
  GtkPageSetupDoneFunc  done_cb;
  gpointer              data;
  GDestroyNotify        destroy;
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
} PageSetupResponseData;

static void
page_setup_data_free (gpointer data)
{
  PageSetupResponseData *rdata = data;

  g_object_unref (rdata->page_setup);
  g_free (rdata);
}

static void
handle_page_setup_response (GtkWidget *dialog,
			    gint       response,
			    gpointer   data)
{
  GtkPageSetupUnixDialog *psd;
  PageSetupResponseData *rdata = data;
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
  psd = GTK_PAGE_SETUP_UNIX_DIALOG (dialog);
  if (response == GTK_RESPONSE_OK)
    rdata->page_setup = gtk_page_setup_unix_dialog_get_page_setup (psd);

  gtk_widget_destroy (dialog);

  if (rdata->done_cb)
    rdata->done_cb (rdata->page_setup, rdata->data);

  if (rdata->destroy)
    rdata->destroy (rdata);
}

static GtkWidget *
get_page_setup_dialog (GtkWindow        *parent,
		       GtkPageSetup     *page_setup,
		       GtkPrintSettings *settings)
{
  GtkWidget *dialog;

  dialog = gtk_page_setup_unix_dialog_new (NULL, parent);
  if (page_setup)
    gtk_page_setup_unix_dialog_set_page_setup (GTK_PAGE_SETUP_UNIX_DIALOG (dialog),
					       page_setup);
  gtk_page_setup_unix_dialog_set_print_settings (GTK_PAGE_SETUP_UNIX_DIALOG (dialog),
						 settings);

  return dialog;
363
364
}

365
366
367
368
369
370
/**
 * gtk_print_run_page_setup_dialog:
 * @parent: transient parent, or %NULL
 * @page_setup: an existing #GtkPageSetup, or %NULL
 * @settings: a #GtkPrintSettings
 * 
371
372
373
374
375
376
377
378
 * Runs a page setup dialog, letting the user modify the values from 
 * @page_setup. If the user cancels the dialog, the returned #GtkPageSetup 
 * is identical to the passed in @page_setup, otherwise it contains the 
 * modifications done in the dialog.
 *
 * Note that this function may use a recursive mainloop to show the page
 * setup dialog. See gtk_print_run_page_setup_dialog_async() if this is 
 * a problem.
379
380
381
382
383
 * 
 * Return value: a new #GtkPageSetup
 *
 * Since: 2.10
 */
384
385
386
387
388
389
GtkPageSetup *
gtk_print_run_page_setup_dialog (GtkWindow        *parent,
				 GtkPageSetup     *page_setup,
				 GtkPrintSettings *settings)
{
  GtkWidget *dialog;
390
391
  gint response;
  PageSetupResponseData rdata;  
392
  
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
  rdata.page_setup = NULL;
  rdata.done_cb = NULL;
  rdata.data = NULL;
  rdata.destroy = NULL;

  dialog = get_page_setup_dialog (parent, page_setup, settings);
  response = gtk_dialog_run (GTK_DIALOG (dialog));
  handle_page_setup_response (dialog, response, &rdata);
 
  if (rdata.page_setup)
    return rdata.page_setup;
  else if (page_setup)
    return gtk_page_setup_copy (page_setup);
  else
    return gtk_page_setup_new ();
408
409
}

410
411
412
413
414
415
416
417
/**
 * gtk_print_run_page_setup_dialog_async:
 * @parent: transient parent, or %NULL
 * @page_setup: an existing #GtkPageSetup, or %NULL
 * @settings: a #GtkPrintSettings
 * @done_cb: a function to call when the user saves the modified page setup
 * @data: user data to pass to @done_cb
 * 
Matthias Clasen's avatar
Matthias Clasen committed
418
 * Runs a page setup dialog, letting the user modify the values from @page_setup. 
419
 *
Matthias Clasen's avatar
Matthias Clasen committed
420
421
422
 * In contrast to gtk_print_run_page_setup_dialog(), this function  returns after 
 * showing the page setup dialog on platforms that support this, and calls @done_cb 
 * from a signal handler for the ::response signal of the dialog.
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
 *
 * Since: 2.10
 */
void
gtk_print_run_page_setup_dialog_async (GtkWindow            *parent,
				       GtkPageSetup         *page_setup,
				       GtkPrintSettings     *settings,
				       GtkPageSetupDoneFunc  done_cb,
				       gpointer              data)
{
  GtkWidget *dialog;
  PageSetupResponseData *rdata;
  
  dialog = get_page_setup_dialog (parent, page_setup, settings);
  gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
  
  rdata = g_new (PageSetupResponseData, 1);
  rdata->page_setup = NULL;
  rdata->done_cb = done_cb;
  rdata->data = data;
  rdata->destroy = page_setup_data_free;

  g_signal_connect (dialog, "response",
		    G_CALLBACK (handle_page_setup_response), rdata);
 
  gtk_window_present (GTK_WINDOW (dialog));
 }

451
452
453

#define __GTK_PRINT_OPERATION_UNIX_C__
#include "gtkaliasdef.c"