]> Pileus Git - ~andy/gtk/blob - gtk/gtkprintoperation-unix.c
Match parameter names with the headers.
[~andy/gtk] / gtk / gtkprintoperation-unix.c
1 /* GTK - The GIMP Toolkit
2  * gtkprintoperation-unix.c: Print Operation Details for Unix and Unix like platforms
3  * Copyright (C) 2006, Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <string.h>
28
29 #include "gtkprintoperation-private.h"
30 #include "gtkmarshal.h"
31 #include "gtkmessagedialog.h"
32
33 #include "gtkprintunixdialog.h"
34 #include "gtkpagesetupunixdialog.h"
35 #include "gtkprintbackend.h"
36 #include "gtkprinter.h"
37 #include "gtkprintjob.h"
38 #include "gtkalias.h"
39
40 typedef struct {
41   GtkPrintJob *job;         /* the job we are sending to the printer */
42   gulong job_status_changed_tag;
43   GtkWindow *parent;        /* parent window just in case we need to throw error dialogs */
44 } GtkPrintOperationUnix;
45
46 static void
47 unix_start_page (GtkPrintOperation *op,
48                  GtkPrintContext *print_context,
49                  GtkPageSetup *page_setup)
50 {
51
52 }
53
54 static void
55 unix_end_page (GtkPrintOperation *op,
56                GtkPrintContext *print_context)
57 {
58   cairo_t *cr;
59
60   cr = gtk_print_context_get_cairo (print_context);
61   cairo_show_page (cr);
62 }
63
64 static void
65 op_unix_free (GtkPrintOperationUnix *op_unix)
66 {
67   if (op_unix->job)
68     {
69       g_signal_handler_disconnect (op_unix->job,
70                                    op_unix->job_status_changed_tag);
71       g_object_unref (op_unix->job);
72     }
73
74   g_free (op_unix);
75 }
76
77 static void
78 unix_finish_send  (GtkPrintJob *job,
79                    void *user_data, 
80                    GError *error)
81 {
82   GtkPrintOperationUnix *op_unix;
83   GtkWindow *parent;
84
85   op_unix = (GtkPrintOperationUnix *) user_data;
86
87   parent = op_unix->parent;
88
89   if (error != NULL)
90     {
91       GtkWidget *edialog;
92       edialog = gtk_message_dialog_new (parent, 
93                                         GTK_DIALOG_DESTROY_WITH_PARENT,
94                                         GTK_MESSAGE_ERROR,
95                                         GTK_BUTTONS_CLOSE,
96                                         "Error printing: %s",
97                                         error->message);
98
99       gtk_dialog_run (GTK_DIALOG (edialog));
100       gtk_widget_destroy (edialog);
101     }
102 }
103
104 static void
105 unix_end_run (GtkPrintOperation *op)
106 {
107   GtkPrintOperationUnix *op_unix = op->priv->platform_data;
108  
109   /* TODO: Check for error */
110   gtk_print_job_send (op_unix->job,
111                       unix_finish_send, 
112                       op_unix, NULL,
113                       NULL);
114 }
115
116 static void
117 job_status_changed_cb (GtkPrintJob *job, GtkPrintOperation *op)
118 {
119   _gtk_print_operation_set_status (op, gtk_print_job_get_status (job), NULL);
120 }
121
122 GtkPrintOperationResult
123 _gtk_print_operation_platform_backend_run_dialog (GtkPrintOperation *op,
124                                                   GtkWindow *parent,
125                                                   gboolean *do_print,
126                                                   GError **error)
127 {
128   GtkWidget *pd;
129   GtkPrintOperationResult result;
130   GtkPageSetup *page_setup;
131   
132   result = GTK_PRINT_OPERATION_RESULT_CANCEL;
133
134   if (op->priv->default_page_setup)
135     page_setup = gtk_page_setup_copy (op->priv->default_page_setup);
136   else
137     page_setup = gtk_page_setup_new ();
138
139   pd = gtk_print_unix_dialog_new (NULL, parent);
140
141   if (op->priv->print_settings)
142     gtk_print_unix_dialog_set_settings (GTK_PRINT_UNIX_DIALOG (pd),
143                                         op->priv->print_settings);
144
145   gtk_print_unix_dialog_set_page_setup (GTK_PRINT_UNIX_DIALOG (pd), page_setup);
146   
147   *do_print = FALSE; 
148   if (gtk_dialog_run (GTK_DIALOG (pd)) == GTK_RESPONSE_OK)
149     {
150       GtkPrintOperationUnix *op_unix;
151       GtkPrinter *printer;
152       GtkPrintSettings *settings;
153
154       result = GTK_PRINT_OPERATION_RESULT_APPLY;
155       
156       printer = gtk_print_unix_dialog_get_selected_printer (GTK_PRINT_UNIX_DIALOG (pd));
157       if (printer == NULL)
158         goto out;
159       
160       *do_print = TRUE;
161
162       settings = gtk_print_unix_dialog_get_settings (GTK_PRINT_UNIX_DIALOG (pd));
163       gtk_print_operation_set_print_settings (op, settings);
164
165       op_unix = g_new0 (GtkPrintOperationUnix, 1);
166       op_unix->job = gtk_print_job_new (op->priv->job_name,
167                                         printer,
168                                         settings,
169                                         page_setup);
170       g_object_unref (settings);
171
172       op->priv->surface = gtk_print_job_get_surface (op_unix->job, error);
173       if (op->priv->surface == NULL)
174         {
175           *do_print = FALSE;
176           op_unix_free (op_unix);
177           result = GTK_PRINT_OPERATION_RESULT_ERROR;
178           goto out;
179         }
180
181       _gtk_print_operation_set_status (op, gtk_print_job_get_status (op_unix->job), NULL);
182       op_unix->job_status_changed_tag =
183         g_signal_connect (op_unix->job, "status_changed",
184                           G_CALLBACK (job_status_changed_cb), op);
185       
186       op_unix->parent = parent;
187
188       op->priv->dpi_x = 72;
189       op->priv->dpi_y = 72;
190  
191       op->priv->platform_data = op_unix;
192       op->priv->free_platform_data = (GDestroyNotify) op_unix_free;
193
194       op->priv->print_pages = op_unix->job->print_pages;
195       op->priv->page_ranges = op_unix->job->page_ranges;
196       op->priv->num_page_ranges = op_unix->job->num_page_ranges;
197   
198       op->priv->manual_num_copies = op_unix->job->num_copies;
199       op->priv->manual_collation = op_unix->job->collate;
200       op->priv->manual_reverse = op_unix->job->reverse;
201       op->priv->manual_page_set = op_unix->job->page_set;
202       op->priv->manual_scale = op_unix->job->scale;
203       op->priv->manual_orientation = op_unix->job->rotate_to_orientation;
204     } 
205
206   op->priv->start_page = unix_start_page;
207   op->priv->end_page = unix_end_page;
208   op->priv->end_run = unix_end_run;
209
210  out:
211   g_object_unref (page_setup); 
212   
213   gtk_widget_destroy (pd);
214
215   return result;
216 }
217
218 /**
219  * gtk_print_run_page_setup_dialog:
220  * @parent: transient parent, or %NULL
221  * @page_setup: an existing #GtkPageSetup, or %NULL
222  * @settings: a #GtkPrintSettings
223  * 
224  * Runs a page setup dialog, letting the user modify 
225  * the values from @page_setup. If the user cancels
226  * the dialog, the returned #GtkPageSetup is identical
227  * to the passed in @page_setup, otherwise it contains
228  * the modifications done in the dialog.
229  * 
230  * Return value: a new #GtkPageSetup
231  *
232  * Since: 2.10
233  */
234 GtkPageSetup *
235 gtk_print_run_page_setup_dialog (GtkWindow        *parent,
236                                  GtkPageSetup     *page_setup,
237                                  GtkPrintSettings *settings)
238 {
239   GtkWidget *dialog;
240   GtkPageSetup *new_page_setup;
241   
242   dialog = gtk_page_setup_unix_dialog_new (NULL, parent);
243   if (page_setup)
244     gtk_page_setup_unix_dialog_set_page_setup (GTK_PAGE_SETUP_UNIX_DIALOG (dialog),
245                                                page_setup);
246   gtk_page_setup_unix_dialog_set_print_settings (GTK_PAGE_SETUP_UNIX_DIALOG (dialog),
247                                                  settings);
248   gtk_dialog_run (GTK_DIALOG (dialog));
249
250   new_page_setup = gtk_page_setup_unix_dialog_get_page_setup (GTK_PAGE_SETUP_UNIX_DIALOG (dialog));
251
252   gtk_widget_destroy (dialog);
253
254   return new_page_setup;
255 }
256
257
258 #define __GTK_PRINT_OPERATION_UNIX_C__
259 #include "gtkaliasdef.c"