Improve wxGTK print and page setup dialogs.
[wxWidgets.git] / src / gtk / print.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/gtk/print.cpp
3 // Author: Anthony Bretaudeau
4 // Purpose: GTK printing support
5 // Created: 2007-08-25
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 wxWidgets development team
8 // Licence: wxWindows Licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_GTKPRINT
19
20 #include "wx/gtk/print.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/log.h"
24 #include "wx/dcmemory.h"
25 #include "wx/dcprint.h"
26 #include "wx/icon.h"
27 #include "wx/math.h"
28 #include "wx/image.h"
29 #include "wx/module.h"
30 #include "wx/crt.h"
31 #endif
32
33 #include "wx/fontutil.h"
34 #include "wx/gtk/private.h"
35 #include "wx/dynlib.h"
36 #include "wx/paper.h"
37
38 #include <gtk/gtk.h>
39 #include <gtk/gtkpagesetupunixdialog.h>
40
41 #if wxUSE_GRAPHICS_CONTEXT
42 #include "wx/graphics.h"
43 #endif
44
45 #include "wx/link.h"
46 wxFORCE_LINK_THIS_MODULE(gtk_print)
47
48 #if wxUSE_LIBGNOMEPRINT
49 #include "wx/gtk/gnome/gprint.h"
50 #endif
51
52 #include "wx/gtk/private/object.h"
53
54 // Usefull to convert angles from/to Rad to/from Deg.
55 static const double RAD2DEG = 180.0 / M_PI;
56 static const double DEG2RAD = M_PI / 180.0;
57
58 //----------------------------------------------------------------------------
59 // wxGtkPrintModule
60 // Initialized when starting the app : if it successfully load the gtk-print framework,
61 // it uses it. If not, it falls back to gnome print (see /gtk/gnome/gprint.cpp) then
62 // to postscript if gnomeprint is not available.
63 //----------------------------------------------------------------------------
64
65 class wxGtkPrintModule: public wxModule
66 {
67 public:
68 wxGtkPrintModule()
69 {
70 #if wxUSE_LIBGNOMEPRINT
71 // This module must be initialized AFTER gnomeprint's one
72 AddDependency(CLASSINFO(wxGnomePrintModule));
73 #endif
74 }
75 bool OnInit();
76 void OnExit() {}
77
78 private:
79 DECLARE_DYNAMIC_CLASS(wxGtkPrintModule)
80 };
81
82 bool wxGtkPrintModule::OnInit()
83 {
84 if (gtk_check_version(2,10,0) == NULL)
85 wxPrintFactory::SetPrintFactory( new wxGtkPrintFactory );
86 return true;
87 }
88
89 IMPLEMENT_DYNAMIC_CLASS(wxGtkPrintModule, wxModule)
90
91 //----------------------------------------------------------------------------
92 // wxGtkPrintFactory
93 //----------------------------------------------------------------------------
94
95 wxPrinterBase* wxGtkPrintFactory::CreatePrinter( wxPrintDialogData *data )
96 {
97 return new wxGtkPrinter( data );
98 }
99
100 wxPrintPreviewBase *wxGtkPrintFactory::CreatePrintPreview( wxPrintout *preview,
101 wxPrintout *printout,
102 wxPrintDialogData *data )
103 {
104 return new wxGtkPrintPreview( preview, printout, data );
105 }
106
107 wxPrintPreviewBase *wxGtkPrintFactory::CreatePrintPreview( wxPrintout *preview,
108 wxPrintout *printout,
109 wxPrintData *data )
110 {
111 return new wxGtkPrintPreview( preview, printout, data );
112 }
113
114 wxPrintDialogBase *wxGtkPrintFactory::CreatePrintDialog( wxWindow *parent,
115 wxPrintDialogData *data )
116 {
117 return new wxGtkPrintDialog( parent, data );
118 }
119
120 wxPrintDialogBase *wxGtkPrintFactory::CreatePrintDialog( wxWindow *parent,
121 wxPrintData *data )
122 {
123 return new wxGtkPrintDialog( parent, data );
124 }
125
126 wxPageSetupDialogBase *wxGtkPrintFactory::CreatePageSetupDialog( wxWindow *parent,
127 wxPageSetupDialogData * data )
128 {
129 return new wxGtkPageSetupDialog( parent, data );
130 }
131
132 bool wxGtkPrintFactory::HasPrintSetupDialog()
133 {
134 return false;
135 }
136
137 wxDialog *
138 wxGtkPrintFactory::CreatePrintSetupDialog(wxWindow * WXUNUSED(parent),
139 wxPrintData * WXUNUSED(data))
140 {
141 return NULL;
142 }
143
144 wxDCImpl* wxGtkPrintFactory::CreatePrinterDCImpl( wxPrinterDC *owner, const wxPrintData& data )
145 {
146 return new wxGtkPrinterDCImpl( owner, data );
147 }
148
149 bool wxGtkPrintFactory::HasOwnPrintToFile()
150 {
151 return true;
152 }
153
154 bool wxGtkPrintFactory::HasPrinterLine()
155 {
156 return true;
157 }
158
159 wxString wxGtkPrintFactory::CreatePrinterLine()
160 {
161 // redundant now
162 return wxEmptyString;
163 }
164
165 bool wxGtkPrintFactory::HasStatusLine()
166 {
167 // redundant now
168 return true;
169 }
170
171 wxString wxGtkPrintFactory::CreateStatusLine()
172 {
173 // redundant now
174 return wxEmptyString;
175 }
176
177 wxPrintNativeDataBase *wxGtkPrintFactory::CreatePrintNativeData()
178 {
179 return new wxGtkPrintNativeData;
180 }
181
182 //----------------------------------------------------------------------------
183 // Callback functions for Gtk Printings.
184 //----------------------------------------------------------------------------
185
186 // We use it to pass useful objects to GTK printing callback functions.
187 struct wxPrinterToGtkData
188 {
189 wxGtkPrinter * printer;
190 wxPrintout * printout;
191 };
192
193 extern "C"
194 {
195 static void gtk_begin_print_callback (GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data)
196 {
197 wxPrinterToGtkData *data = (wxPrinterToGtkData *) user_data;
198
199 data->printer->BeginPrint(data->printout, operation, context);
200 }
201
202 static void gtk_draw_page_print_callback (GtkPrintOperation *operation, GtkPrintContext *context, gint page_nr, gpointer user_data)
203 {
204 wxPrinterToGtkData *data = (wxPrinterToGtkData *) user_data;
205
206 data->printer->DrawPage(data->printout, operation, context, page_nr);
207 }
208
209 static void gtk_end_print_callback(GtkPrintOperation * WXUNUSED(operation),
210 GtkPrintContext * WXUNUSED(context),
211 gpointer user_data)
212 {
213 wxPrintout *printout = (wxPrintout *) user_data;
214
215 printout->OnEndPrinting();
216 }
217 }
218
219 //----------------------------------------------------------------------------
220 // wxGtkPrintNativeData
221 //----------------------------------------------------------------------------
222
223 IMPLEMENT_CLASS(wxGtkPrintNativeData, wxPrintNativeDataBase)
224
225 wxGtkPrintNativeData::wxGtkPrintNativeData()
226 {
227 m_config = gtk_print_settings_new();
228 m_job = gtk_print_operation_new();
229 m_context = NULL;
230 }
231
232 wxGtkPrintNativeData::~wxGtkPrintNativeData()
233 {
234 g_object_unref(m_job);
235 g_object_unref(m_config);
236 }
237
238 // Convert datas stored in m_config to a wxPrintData.
239 // Called by wxPrintData::ConvertFromNative().
240 bool wxGtkPrintNativeData::TransferTo( wxPrintData &data )
241 {
242 if(!m_config)
243 return false;
244
245 int resolution = gtk_print_settings_get_resolution(m_config);
246 if ( resolution > 0 )
247 {
248 // if resolution is explicitly set, use it
249 data.SetQuality(resolution);
250 }
251 else // use more vague "quality"
252 {
253 GtkPrintQuality quality = gtk_print_settings_get_quality(m_config);
254 if (quality == GTK_PRINT_QUALITY_HIGH)
255 data.SetQuality(wxPRINT_QUALITY_HIGH);
256 else if (quality == GTK_PRINT_QUALITY_LOW)
257 data.SetQuality(wxPRINT_QUALITY_LOW);
258 else if (quality == GTK_PRINT_QUALITY_DRAFT)
259 data.SetQuality(wxPRINT_QUALITY_DRAFT);
260 else
261 data.SetQuality(wxPRINT_QUALITY_MEDIUM);
262 }
263
264 data.SetNoCopies(gtk_print_settings_get_n_copies(m_config));
265
266 data.SetColour(gtk_print_settings_get_use_color(m_config));
267
268 switch (gtk_print_settings_get_duplex(m_config))
269 {
270 case GTK_PRINT_DUPLEX_SIMPLEX: data.SetDuplex (wxDUPLEX_SIMPLEX);
271 break;
272
273 case GTK_PRINT_DUPLEX_HORIZONTAL: data.SetDuplex (wxDUPLEX_HORIZONTAL);
274 break;
275
276 default:
277 case GTK_PRINT_DUPLEX_VERTICAL: data.SetDuplex (wxDUPLEX_VERTICAL);
278 break;
279 }
280
281 GtkPageOrientation orientation = gtk_print_settings_get_orientation (m_config);
282 if (orientation == GTK_PAGE_ORIENTATION_PORTRAIT)
283 {
284 data.SetOrientation(wxPORTRAIT);
285 data.SetOrientationReversed(false);
286 }
287 else if (orientation == GTK_PAGE_ORIENTATION_LANDSCAPE)
288 {
289 data.SetOrientation(wxLANDSCAPE);
290 data.SetOrientationReversed(false);
291 }
292 else if (orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
293 {
294 data.SetOrientation(wxPORTRAIT);
295 data.SetOrientationReversed(true);
296 }
297 else if (orientation == GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE)
298 {
299 data.SetOrientation(wxLANDSCAPE);
300 data.SetOrientationReversed(true);
301 }
302
303 data.SetCollate(gtk_print_settings_get_collate (m_config));
304
305 // Paper formats : these are the most common paper formats.
306 GtkPaperSize *paper_size = gtk_print_settings_get_paper_size (m_config);
307 if (!paper_size)
308 data.SetPaperId(wxPAPER_NONE);
309 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new (GTK_PAPER_NAME_A3)))
310 data.SetPaperId(wxPAPER_A3);
311 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new (GTK_PAPER_NAME_A4)))
312 data.SetPaperId(wxPAPER_A4);
313 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new (GTK_PAPER_NAME_A5)))
314 data.SetPaperId(wxPAPER_A5);
315 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new (GTK_PAPER_NAME_B5)))
316 data.SetPaperId(wxPAPER_B5);
317 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new (GTK_PAPER_NAME_LETTER)))
318 data.SetPaperId(wxPAPER_LETTER);
319 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new (GTK_PAPER_NAME_LEGAL)))
320 data.SetPaperId(wxPAPER_LEGAL);
321 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new (GTK_PAPER_NAME_EXECUTIVE)))
322 data.SetPaperId(wxPAPER_EXECUTIVE);
323 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"na_number-10")))
324 data.SetPaperId(wxPAPER_ENV_10);
325 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"iso-c5")))
326 data.SetPaperId(wxPAPER_ENV_C5);
327 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"iso-c6")))
328 data.SetPaperId(wxPAPER_ENV_C6);
329 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"jis-b5")))
330 data.SetPaperId(wxPAPER_B5_TRANSVERSE);
331 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"iso-b5")))
332 data.SetPaperId(wxPAPER_ENV_B5);
333 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"na_monarch")))
334 data.SetPaperId(wxPAPER_ENV_MONARCH);
335 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"engineering-c")))
336 data.SetPaperId( wxPAPER_CSHEET);
337 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"engineering-d")))
338 data.SetPaperId( wxPAPER_DSHEET);
339 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"engineering-e")))
340 data.SetPaperId( wxPAPER_ESHEET);
341 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"letter")))
342 data.SetPaperId( wxPAPER_LETTERSMALL);
343 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"engineering-b")))
344 data.SetPaperId( wxPAPER_TABLOID);
345 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"ledger")))
346 data.SetPaperId( wxPAPER_LEDGER);
347 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"statement")))
348 data.SetPaperId( wxPAPER_STATEMENT);
349 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( GTK_PAPER_NAME_A4 )))
350 data.SetPaperId( wxPAPER_A4SMALL);
351 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"iso-b4")))
352 data.SetPaperId( wxPAPER_B4);
353 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"folio")))
354 data.SetPaperId( wxPAPER_FOLIO);
355 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"quarto")))
356 data.SetPaperId( wxPAPER_QUARTO);
357 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"10x14")))
358 data.SetPaperId( wxPAPER_10X14);
359 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"ledger")))
360 data.SetPaperId( wxPAPER_11X17);
361 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"letter")))
362 data.SetPaperId( wxPAPER_NOTE);
363 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"na-number-9-envelope")))
364 data.SetPaperId( wxPAPER_ENV_9);
365 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"number-11")))
366 data.SetPaperId( wxPAPER_ENV_11);
367 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"number-12")))
368 data.SetPaperId( wxPAPER_ENV_12);
369 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"number-14")))
370 data.SetPaperId( wxPAPER_ENV_14);
371 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"iso-designated")))
372 data.SetPaperId( wxPAPER_ENV_DL);
373 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"iso-c3")))
374 data.SetPaperId( wxPAPER_ENV_C3);
375 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"iso-c4")))
376 data.SetPaperId( wxPAPER_ENV_C4);
377 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"c6/c5")))
378 data.SetPaperId( wxPAPER_ENV_C65);
379 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"iso-b4")))
380 data.SetPaperId( wxPAPER_ENV_B4);
381 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"iso-b6")))
382 data.SetPaperId( wxPAPER_ENV_B6);
383 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"Italian")))
384 data.SetPaperId( wxPAPER_ENV_ITALY);
385 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"personal")))
386 data.SetPaperId( wxPAPER_ENV_PERSONAL);
387 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"fanfold-us")))
388 data.SetPaperId( wxPAPER_FANFOLD_US);
389 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"fanfold-European")))
390 data.SetPaperId( wxPAPER_FANFOLD_STD_GERMAN);
391 else if (gtk_paper_size_is_equal(paper_size,gtk_paper_size_new ( (const gchar*)"foolscap")))
392 data.SetPaperId( wxPAPER_FANFOLD_LGL_GERMAN);
393 else
394 data.SetPaperId(wxPAPER_NONE);
395
396 data.SetPrinterName(gtk_print_settings_get_printer(m_config));
397
398 return true;
399 }
400
401 // Put datas given by the wxPrintData into m_config.
402 // Called by wxPrintData::ConvertToNative().
403 bool wxGtkPrintNativeData::TransferFrom( const wxPrintData &data )
404 {
405 if(!m_config)
406 return false;
407
408 wxPrintQuality quality = data.GetQuality();
409 if (quality == wxPRINT_QUALITY_HIGH)
410 gtk_print_settings_set_quality (m_config, GTK_PRINT_QUALITY_HIGH);
411 else if (quality == wxPRINT_QUALITY_MEDIUM)
412 gtk_print_settings_set_quality (m_config, GTK_PRINT_QUALITY_NORMAL);
413 else if (quality == wxPRINT_QUALITY_LOW)
414 gtk_print_settings_set_quality (m_config, GTK_PRINT_QUALITY_LOW);
415 else if (quality == wxPRINT_QUALITY_DRAFT)
416 gtk_print_settings_set_quality (m_config, GTK_PRINT_QUALITY_DRAFT);
417 else if (quality > 1)
418 gtk_print_settings_set_resolution (m_config, quality);
419 else
420 gtk_print_settings_set_quality (m_config, GTK_PRINT_QUALITY_NORMAL);
421
422 gtk_print_settings_set_n_copies(m_config, data.GetNoCopies());
423
424 gtk_print_settings_set_use_color(m_config, data.GetColour());
425
426 switch (data.GetDuplex())
427 {
428 case wxDUPLEX_SIMPLEX: gtk_print_settings_set_duplex (m_config, GTK_PRINT_DUPLEX_SIMPLEX);
429 break;
430
431 case wxDUPLEX_HORIZONTAL: gtk_print_settings_set_duplex (m_config, GTK_PRINT_DUPLEX_HORIZONTAL);
432 break;
433
434 default:
435 case wxDUPLEX_VERTICAL: gtk_print_settings_set_duplex (m_config, GTK_PRINT_DUPLEX_VERTICAL);
436 break;
437 }
438
439 if (!data.IsOrientationReversed())
440 {
441 if (data.GetOrientation() == wxLANDSCAPE)
442 gtk_print_settings_set_orientation (m_config, GTK_PAGE_ORIENTATION_LANDSCAPE);
443 else
444 gtk_print_settings_set_orientation (m_config, GTK_PAGE_ORIENTATION_PORTRAIT);
445 }
446 else {
447 if (data.GetOrientation() == wxLANDSCAPE)
448 gtk_print_settings_set_orientation (m_config, GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE);
449 else
450 gtk_print_settings_set_orientation (m_config, GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT);
451 }
452
453 gtk_print_settings_set_collate (m_config, data.GetCollate());
454
455 // Paper formats: these are the most common paper formats.
456 switch (data.GetPaperId())
457 {
458 case wxPAPER_A3: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new (GTK_PAPER_NAME_A3));
459 break;
460 case wxPAPER_A4: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new (GTK_PAPER_NAME_A4));
461 break;
462 case wxPAPER_A5: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new (GTK_PAPER_NAME_A5));
463 break;
464 case wxPAPER_B5_TRANSVERSE: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "jis-b5"));
465 break;
466 case wxPAPER_B5: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new (GTK_PAPER_NAME_B5));
467 break;
468 case wxPAPER_LETTER: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new (GTK_PAPER_NAME_LETTER));
469 break;
470 case wxPAPER_LEGAL: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new (GTK_PAPER_NAME_LEGAL));
471 break;
472 case wxPAPER_EXECUTIVE: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new (GTK_PAPER_NAME_EXECUTIVE));
473 break;
474 case wxPAPER_ENV_10: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "na_number-10"));
475 break;
476 case wxPAPER_ENV_C5: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "iso-c5"));
477 break;
478 case wxPAPER_ENV_C6: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "iso-c6"));
479 break;
480 case wxPAPER_ENV_B5: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "iso-c5b5"));
481 break;
482 case wxPAPER_ENV_MONARCH: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "na_monarch"));
483 break;
484 case wxPAPER_CSHEET: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "engineering-c"));
485 break;
486 case wxPAPER_DSHEET: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "engineering-d"));
487 break;
488 case wxPAPER_ESHEET: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "engineering-e"));
489 break;
490 case wxPAPER_LETTERSMALL: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "letter"));
491 break;
492 case wxPAPER_TABLOID: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "engineering-b"));
493 break;
494 case wxPAPER_LEDGER: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "ledger"));
495 break;
496 case wxPAPER_STATEMENT: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "statement"));
497 break;
498 case wxPAPER_A4SMALL: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new (GTK_PAPER_NAME_A4));
499 break;
500 case wxPAPER_B4: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "iso-b4"));
501 break;
502 case wxPAPER_FOLIO: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "folio"));
503 break;
504 case wxPAPER_QUARTO: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "quarto"));
505 break;
506 case wxPAPER_10X14: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "10x14"));
507 break;
508 case wxPAPER_11X17: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "ledger"));
509 break;
510 case wxPAPER_NOTE: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "letter"));
511 break;
512 case wxPAPER_ENV_9: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "na-number-9-envelope"));
513 break;
514 case wxPAPER_ENV_11: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "number-11"));
515 break;
516 case wxPAPER_ENV_12: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "number-12"));
517 break;
518 case wxPAPER_ENV_14: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "number-14"));
519 break;
520 case wxPAPER_ENV_DL: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "iso-designated"));
521 break;
522 case wxPAPER_ENV_C3: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "iso-c3"));
523 break;
524 case wxPAPER_ENV_C4: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "iso-c4"));
525 break;
526 case wxPAPER_ENV_C65: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "c6/c5"));
527 break;
528 case wxPAPER_ENV_B4: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "iso-b4"));
529 break;
530 case wxPAPER_ENV_B6: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "iso-b6"));
531 break;
532 case wxPAPER_ENV_ITALY: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "Italian"));
533 break;
534 case wxPAPER_ENV_PERSONAL: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "personal"));
535 break;
536 case wxPAPER_FANFOLD_US: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "fanfold-us"));
537 break;
538 case wxPAPER_FANFOLD_STD_GERMAN: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "fanfold-European"));
539 break;
540 case wxPAPER_FANFOLD_LGL_GERMAN: gtk_print_settings_set_paper_size(m_config, gtk_paper_size_new ((const gchar*) "foolscap"));
541 break;
542 case wxPAPER_NONE:
543 default: break;
544 }
545
546 gtk_print_settings_set_printer(m_config, data.GetPrinterName());
547
548 return true;
549 }
550
551 void wxGtkPrintNativeData::SetPrintConfig( GtkPrintSettings * config )
552 {
553 if (config)
554 m_config = gtk_print_settings_copy(config);
555 }
556
557 // Extract page setup from settings.
558 GtkPageSetup* wxGtkPrintNativeData::GetPageSetupFromSettings(GtkPrintSettings* settings)
559 {
560 GtkPageSetup* page_setup = gtk_page_setup_new();
561 gtk_page_setup_set_orientation (page_setup, gtk_print_settings_get_orientation (settings));
562
563 GtkPaperSize *paper_size = gtk_print_settings_get_paper_size (settings);
564 if (paper_size != NULL)
565 gtk_page_setup_set_paper_size_and_default_margins (page_setup, paper_size);
566
567 return page_setup;
568 }
569
570 // Insert page setup into a given GtkPrintSettings.
571 void wxGtkPrintNativeData::SetPageSetupToSettings(GtkPrintSettings* settings, GtkPageSetup* page_setup)
572 {
573 gtk_print_settings_set_orientation ( settings, gtk_page_setup_get_orientation (page_setup));
574 gtk_print_settings_set_paper_size ( settings, gtk_page_setup_get_paper_size (page_setup));
575 }
576
577 //----------------------------------------------------------------------------
578 // wxGtkPrintDialog
579 //----------------------------------------------------------------------------
580
581 IMPLEMENT_CLASS(wxGtkPrintDialog, wxPrintDialogBase)
582
583 wxGtkPrintDialog::wxGtkPrintDialog( wxWindow *parent, wxPrintDialogData *data )
584 : wxPrintDialogBase(parent, wxID_ANY, _("Print"),
585 wxPoint(0, 0), wxSize(600, 600),
586 wxDEFAULT_DIALOG_STYLE |
587 wxTAB_TRAVERSAL)
588 {
589 if (data)
590 m_printDialogData = *data;
591
592 m_parent = parent;
593 SetShowDialog(true);
594 }
595
596 wxGtkPrintDialog::wxGtkPrintDialog( wxWindow *parent, wxPrintData *data )
597 : wxPrintDialogBase(parent, wxID_ANY, _("Print"),
598 wxPoint(0, 0), wxSize(600, 600),
599 wxDEFAULT_DIALOG_STYLE |
600 wxTAB_TRAVERSAL)
601 {
602 if (data)
603 m_printDialogData = *data;
604
605 m_parent = parent;
606 SetShowDialog(true);
607 }
608
609
610 wxGtkPrintDialog::~wxGtkPrintDialog()
611 {
612 }
613
614 // This is called even if we actually don't want the dialog to appear.
615 int wxGtkPrintDialog::ShowModal()
616 {
617 GtkPrintOperationResult response;
618
619 // We need to restore the settings given in the constructor.
620 wxPrintData data = m_printDialogData.GetPrintData();
621 wxGtkPrintNativeData *native =
622 (wxGtkPrintNativeData*) data.GetNativeData();
623 data.ConvertToNative();
624
625 GtkPrintSettings * settings = native->GetPrintConfig();
626
627 // We have to restore pages to print here because they're stored in a wxPrintDialogData and ConvertToNative only works for wxPrintData.
628 int fromPage = m_printDialogData.GetFromPage();
629 int toPage = m_printDialogData.GetToPage();
630 if (m_printDialogData.GetSelection())
631 gtk_print_settings_set_print_pages(settings, GTK_PRINT_PAGES_CURRENT);
632 else if (m_printDialogData.GetAllPages())
633 gtk_print_settings_set_print_pages(settings, GTK_PRINT_PAGES_ALL);
634 else {
635 gtk_print_settings_set_print_pages(settings, GTK_PRINT_PAGES_RANGES);
636 GtkPageRange *range;
637 range = g_new (GtkPageRange, 1);
638 range[0].start = fromPage-1;
639 range[0].end = (toPage >= fromPage) ? toPage-1 : fromPage-1;
640 gtk_print_settings_set_page_ranges (settings, range, 1);
641 }
642
643 GtkPrintOperation * const printOp = native->GetPrintJob();
644
645 // If the settings are OK, we restore it.
646 if (settings != NULL)
647 gtk_print_operation_set_print_settings (printOp, settings);
648 gtk_print_operation_set_default_page_setup (printOp, native->GetPageSetupFromSettings(settings));
649
650 // Show the dialog if needed.
651 GError* gError = NULL;
652 if (GetShowDialog())
653 response = gtk_print_operation_run (printOp, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, GTK_WINDOW(gtk_widget_get_toplevel(m_parent->m_widget) ), &gError);
654 else
655 response = gtk_print_operation_run (printOp, GTK_PRINT_OPERATION_ACTION_PRINT, GTK_WINDOW(gtk_widget_get_toplevel(m_parent->m_widget)), &gError);
656
657 // Does everything went well?
658 if (response == GTK_PRINT_OPERATION_RESULT_CANCEL)
659 {
660 return wxID_CANCEL;
661 }
662 else if (response == GTK_PRINT_OPERATION_RESULT_ERROR)
663 {
664 g_error_free (gError);
665 wxLogError(_("Error while printing: ") + wxString::Format(_("%s"), gError->message));
666 return wxID_NO; // We use wxID_NO because there is no wxID_ERROR available
667 }
668
669 // Now get the settings and save it.
670 GtkPrintSettings* newSettings = gtk_print_operation_get_print_settings(printOp);
671 native->SetPrintConfig(newSettings);
672 data.ConvertFromNative();
673
674 // Set PrintDialogData variables
675 m_printDialogData.SetPrintData(data);
676 m_printDialogData.SetCollate(data.GetCollate());
677 m_printDialogData.SetNoCopies(data.GetNoCopies());
678 m_printDialogData.SetPrintToFile(data.GetPrinterName() == "Print to File");
679
680 // Same problem as a few lines before.
681 switch (gtk_print_settings_get_print_pages(newSettings))
682 {
683 case GTK_PRINT_PAGES_CURRENT:
684 m_printDialogData.SetSelection( true );
685 break;
686 case GTK_PRINT_PAGES_RANGES:
687 {// wxWidgets doesn't support multiple ranges, so we can only save the first one even if the user wants to print others.
688 // For example, the user enters "1-3;5-7" in the dialog: pages 1-3 and 5-7 will be correctly printed when the user
689 // will hit "OK" button. However we can only save 1-3 in the print data.
690 gint num_ranges = 0;
691 GtkPageRange* range;
692 range = gtk_print_settings_get_page_ranges (newSettings, &num_ranges);
693 if (num_ranges >= 1)
694 {
695 m_printDialogData.SetFromPage( range[0].start );
696 m_printDialogData.SetToPage( range[0].end );
697 }
698 else {
699 m_printDialogData.SetAllPages( true );
700 m_printDialogData.SetFromPage( 0 );
701 m_printDialogData.SetToPage( 9999 );
702 }
703 break;}
704 case GTK_PRINT_PAGES_ALL:
705 default:
706 m_printDialogData.SetAllPages( true );
707 m_printDialogData.SetFromPage( 0 );
708 m_printDialogData.SetToPage( 9999 );
709 break;
710 }
711
712 return wxID_OK;
713 }
714
715 //----------------------------------------------------------------------------
716 // wxGtkPageSetupDialog
717 //----------------------------------------------------------------------------
718
719 IMPLEMENT_CLASS(wxGtkPageSetupDialog, wxPageSetupDialogBase)
720
721 wxGtkPageSetupDialog::wxGtkPageSetupDialog( wxWindow *parent,
722 wxPageSetupDialogData* data )
723 {
724 if (data)
725 m_pageDialogData = *data;
726
727 m_parent = parent;
728 }
729
730 wxGtkPageSetupDialog::~wxGtkPageSetupDialog()
731 {
732 }
733
734 int wxGtkPageSetupDialog::ShowModal()
735 {
736 // Get the config.
737 m_pageDialogData.GetPrintData().ConvertToNative();
738 wxGtkPrintNativeData *native = (wxGtkPrintNativeData*) m_pageDialogData.GetPrintData().GetNativeData();
739 GtkPrintSettings* nativeData = native->GetPrintConfig();
740
741 // We only need the pagesetup data which are part of the settings.
742 GtkPageSetup* oldPageSetup = native->GetPageSetupFromSettings(nativeData);
743
744 // If the user used a custom paper format the last time he printed, we have to restore it too.
745 if (m_pageDialogData.GetPrintData().GetPaperId() == wxPAPER_NONE)
746 {
747 wxSize customPaperSize = m_pageDialogData.GetPaperSize();
748 if (customPaperSize.GetWidth() > 0 && customPaperSize.GetHeight() > 0)
749 {
750 wxString title = _("Custom size");
751 GtkPaperSize* customSize = gtk_paper_size_new_custom ("custom", title.mb_str(), (gdouble) customPaperSize.GetWidth(), (gdouble) customPaperSize.GetHeight(), GTK_UNIT_MM);
752 gtk_page_setup_set_paper_size_and_default_margins (oldPageSetup, customSize);
753 g_object_unref(customSize);
754 }
755 }
756
757
758 // Set selected printer
759 gtk_print_settings_set(nativeData, "format-for-printer",
760 gtk_print_settings_get_printer(nativeData));
761
762 // Create custom dialog
763 wxString title(GetTitle());
764 if ( title.empty() )
765 title = _("Page Setup");
766 GtkWidget *dlg = gtk_page_setup_unix_dialog_new(title, GTK_WINDOW(m_parent->m_widget));
767
768 gtk_page_setup_unix_dialog_set_print_settings(
769 GTK_PAGE_SETUP_UNIX_DIALOG(dlg), nativeData);
770 gtk_page_setup_unix_dialog_set_page_setup(
771 GTK_PAGE_SETUP_UNIX_DIALOG(dlg), oldPageSetup);
772
773 int result = gtk_dialog_run(GTK_DIALOG(dlg));
774 gtk_widget_hide(dlg);
775
776 switch ( result )
777 {
778 case GTK_RESPONSE_OK:
779 case GTK_RESPONSE_APPLY:
780 {
781 // Store Selected Printer Name
782 gtk_print_settings_set_printer
783 (
784 nativeData,
785 gtk_print_settings_get(nativeData, "format-for-printer")
786 );
787
788 wxGtkObject<GtkPageSetup>
789 newPageSetup(gtk_page_setup_unix_dialog_get_page_setup(
790 GTK_PAGE_SETUP_UNIX_DIALOG(dlg)));
791 native->SetPageSetupToSettings(nativeData, newPageSetup);
792
793 m_pageDialogData.GetPrintData().ConvertFromNative();
794
795 // Store custom paper format if any.
796 if ( m_pageDialogData.GetPrintData().GetPaperId() == wxPAPER_NONE )
797 {
798 gdouble ml,mr,mt,mb,pw,ph;
799 ml = gtk_page_setup_get_left_margin (newPageSetup, GTK_UNIT_MM);
800 mr = gtk_page_setup_get_right_margin (newPageSetup, GTK_UNIT_MM);
801 mt = gtk_page_setup_get_top_margin (newPageSetup, GTK_UNIT_MM);
802 mb = gtk_page_setup_get_bottom_margin (newPageSetup, GTK_UNIT_MM);
803
804 pw = gtk_page_setup_get_paper_width (newPageSetup, GTK_UNIT_MM);
805 ph = gtk_page_setup_get_paper_height (newPageSetup, GTK_UNIT_MM);
806
807 m_pageDialogData.SetMarginTopLeft(wxPoint((int)(ml+0.5),
808 (int)(mt+0.5)));
809 m_pageDialogData.SetMarginBottomRight(wxPoint((int)(mr+0.5),
810 (int)(mb+0.5)));
811
812 m_pageDialogData.SetPaperSize(wxSize((int)(pw+0.5),
813 (int)(ph+0.5)));
814 }
815
816 result = wxID_OK;
817 }
818 break;
819
820 default:
821 case GTK_RESPONSE_CANCEL:
822 result = wxID_CANCEL;
823 break;
824 }
825
826 gtk_widget_destroy(dlg);
827
828 return result;
829 }
830
831 //----------------------------------------------------------------------------
832 // wxGtkPrinter
833 //----------------------------------------------------------------------------
834
835 IMPLEMENT_CLASS(wxGtkPrinter, wxPrinterBase)
836
837 wxGtkPrinter::wxGtkPrinter( wxPrintDialogData *data ) :
838 wxPrinterBase( data )
839 {
840 m_gpc = NULL;
841
842 if (data)
843 m_printDialogData = *data;
844 }
845
846 wxGtkPrinter::~wxGtkPrinter()
847 {
848 }
849
850 bool wxGtkPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt )
851 {
852 if (!printout)
853 {
854 sm_lastError = wxPRINTER_ERROR;
855 return false;
856 }
857
858 // Let's correct the PageInfo just in case the app gives wrong values.
859 int fromPage, toPage;
860 int minPage, maxPage;
861 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
862 m_printDialogData.SetAllPages(true);
863
864 if (minPage < 1) minPage = 1;
865 if (maxPage < 1) maxPage = 9999;
866 if (maxPage < minPage) maxPage = minPage;
867
868 m_printDialogData.SetMinPage(minPage);
869 m_printDialogData.SetMaxPage(maxPage);
870 if (fromPage != 0)
871 {
872 if (fromPage < minPage) fromPage = minPage;
873 else if (fromPage > maxPage) fromPage = maxPage;
874 m_printDialogData.SetFromPage(fromPage);
875 }
876 if (toPage != 0)
877 {
878 m_printDialogData.SetToPage(toPage);
879 if (toPage > maxPage) toPage = maxPage;
880 else if (toPage < minPage) toPage = minPage;
881 }
882
883 if (((minPage != fromPage) && fromPage != 0) || ((maxPage != toPage) && toPage != 0)) m_printDialogData.SetAllPages(false);
884
885
886 wxPrintData printdata = GetPrintDialogData().GetPrintData();
887 wxGtkPrintNativeData *native = (wxGtkPrintNativeData*) printdata.GetNativeData();
888
889 GtkPrintOperation * const printOp = native->GetPrintJob();
890
891 wxPrinterToGtkData dataToSend;
892 dataToSend.printer = this;
893 dataToSend.printout = printout;
894
895 // These Gtk signals are caught here.
896 g_signal_connect (printOp, "begin-print", G_CALLBACK (gtk_begin_print_callback), &dataToSend);
897 g_signal_connect (printOp, "draw-page", G_CALLBACK (gtk_draw_page_print_callback), &dataToSend);
898 g_signal_connect (printOp, "end-print", G_CALLBACK (gtk_end_print_callback), printout);
899
900 // This is used to setup the DC and
901 // show the dialog if desired
902 wxGtkPrintDialog dialog( parent, &m_printDialogData );
903 dialog.SetPrintDC(m_dc);
904 dialog.SetShowDialog(prompt);
905
906 // doesn't necessarily show
907 int ret = dialog.ShowModal();
908 if (ret == wxID_CANCEL)
909 {
910 sm_lastError = wxPRINTER_CANCELLED;
911 }
912 if (ret == wxID_NO)
913 {
914 sm_lastError = wxPRINTER_ERROR;
915 wxFAIL_MSG(_("The print dialog returned an error."));
916 }
917
918 return (sm_lastError == wxPRINTER_NO_ERROR);
919 }
920
921 void wxGtkPrinter::BeginPrint(wxPrintout *printout, GtkPrintOperation *operation, GtkPrintContext *context)
922 {
923 wxPrintData printdata = GetPrintDialogData().GetPrintData();
924 wxGtkPrintNativeData *native = (wxGtkPrintNativeData*) printdata.GetNativeData();
925
926 // We need to update printdata with the new data from the dialog and we
927 // have to do this here because this method needs this new data and we
928 // cannot update it earlier
929 native->SetPrintConfig(gtk_print_operation_get_print_settings(operation));
930 printdata.ConvertFromNative();
931
932 SetPrintContext(context);
933 native->SetPrintContext( context );
934
935 wxPrinterDC *printDC = new wxPrinterDC( printdata );
936 m_dc = printDC;
937
938 if (!m_dc->IsOk())
939 {
940 if (sm_lastError != wxPRINTER_CANCELLED)
941 {
942 sm_lastError = wxPRINTER_ERROR;
943 wxFAIL_MSG(_("The wxGtkPrinterDC cannot be used."));
944 }
945 return;
946 }
947
948 printout->SetPPIScreen(wxGetDisplayPPI());
949 printout->SetPPIPrinter( printDC->GetResolution(),
950 printDC->GetResolution() );
951
952 printout->SetDC(m_dc);
953
954 int w, h;
955 m_dc->GetSize(&w, &h);
956 printout->SetPageSizePixels((int)w, (int)h);
957 printout->SetPaperRectPixels(wxRect(0, 0, w, h));
958 int mw, mh;
959 m_dc->GetSizeMM(&mw, &mh);
960 printout->SetPageSizeMM((int)mw, (int)mh);
961 printout->OnPreparePrinting();
962
963 // Get some parameters from the printout, if defined.
964 int fromPage, toPage;
965 int minPage, maxPage;
966 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
967
968 if (maxPage == 0)
969 {
970 sm_lastError = wxPRINTER_ERROR;
971 wxFAIL_MSG(_("wxPrintout::GetPageInfo gives a null maxPage."));
972 return;
973 }
974
975 printout->OnBeginPrinting();
976
977 int numPages = 0;
978
979 // If we're not previewing we need to calculate the number of pages to print.
980 // If we're previewing, Gtk Print will render every pages without wondering about the page ranges the user may
981 // have defined in the dialog. So the number of pages is the maximum available.
982 if (!printout->IsPreview())
983 {
984 GtkPrintSettings * settings = gtk_print_operation_get_print_settings (operation);
985 switch (gtk_print_settings_get_print_pages(settings))
986 {
987 case GTK_PRINT_PAGES_CURRENT:
988 numPages = 1;
989 break;
990 case GTK_PRINT_PAGES_RANGES:
991 {gint num_ranges = 0;
992 GtkPageRange* range;
993 int i;
994 range = gtk_print_settings_get_page_ranges (settings, &num_ranges);
995 for (i=0; i<num_ranges; i++)
996 {
997 if (range[i].end < range[i].start) range[i].end = range[i].start;
998 if (range[i].start < minPage-1) range[i].start = minPage-1;
999 if (range[i].end > maxPage-1) range[i].end = maxPage-1;
1000 if (range[i].start > maxPage-1) range[i].start = maxPage-1;
1001 numPages += range[i].end - range[i].start + 1;
1002 }
1003 gtk_print_settings_set_page_ranges (settings, range, 1);
1004 break;}
1005 case GTK_PRINT_PAGES_ALL:
1006 default:
1007 numPages = maxPage - minPage + 1;
1008 break;
1009 }
1010 }
1011 else numPages = maxPage - minPage + 1;
1012
1013 gtk_print_operation_set_n_pages(operation, numPages);
1014 }
1015
1016 void wxGtkPrinter::DrawPage(wxPrintout *printout,
1017 GtkPrintOperation *operation,
1018 GtkPrintContext * WXUNUSED(context),
1019 int page_nr)
1020 {
1021 int fromPage, toPage, minPage, maxPage, startPage, endPage;
1022 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
1023
1024 int numPageToDraw = page_nr + minPage;
1025 if (numPageToDraw < minPage) numPageToDraw = minPage;
1026 if (numPageToDraw > maxPage) numPageToDraw = maxPage;
1027
1028 GtkPrintSettings * settings = gtk_print_operation_get_print_settings (operation);
1029 switch (gtk_print_settings_get_print_pages(settings))
1030 {
1031 case GTK_PRINT_PAGES_CURRENT:
1032 g_object_get_property((GObject*) operation, (const gchar *) "current-page", (GValue*) &startPage);
1033 g_object_get_property((GObject*) operation, (const gchar *) "current-page", (GValue*) &endPage);
1034 break;
1035 case GTK_PRINT_PAGES_RANGES:
1036 {gint num_ranges = 0;
1037 GtkPageRange* range;
1038 range = gtk_print_settings_get_page_ranges (settings, &num_ranges);
1039 // We don't need to verify these values as it has already been done in wxGtkPrinter::BeginPrint.
1040 if (num_ranges >= 1)
1041 {
1042 startPage = range[0].start + 1;
1043 endPage = range[0].end + 1;
1044 }
1045 else {
1046 startPage = minPage;
1047 endPage = maxPage;
1048 }
1049 break;}
1050 case GTK_PRINT_PAGES_ALL:
1051 default:
1052 startPage = minPage;
1053 endPage = maxPage;
1054 break;
1055 }
1056
1057 if(numPageToDraw == startPage)
1058 {
1059 if (!printout->OnBeginDocument(startPage, endPage))
1060 {
1061 wxLogError(_("Could not start printing."));
1062 sm_lastError = wxPRINTER_ERROR;
1063 }
1064 }
1065
1066 // The app can render the page numPageToDraw.
1067 if (printout->HasPage(numPageToDraw))
1068 {
1069 m_dc->StartPage();
1070 printout->OnPrintPage(numPageToDraw);
1071 m_dc->EndPage();
1072 }
1073
1074
1075 if(numPageToDraw == endPage)
1076 {
1077 printout->OnEndDocument();
1078 }
1079 }
1080
1081 wxDC* wxGtkPrinter::PrintDialog( wxWindow *parent )
1082 {
1083 wxGtkPrintDialog dialog( parent, &m_printDialogData );
1084
1085 dialog.SetPrintDC(m_dc);
1086 dialog.SetShowDialog(true);
1087
1088 int ret = dialog.ShowModal();
1089
1090 if (ret == wxID_CANCEL)
1091 {
1092 sm_lastError = wxPRINTER_CANCELLED;
1093 return NULL;
1094 }
1095 if (ret == wxID_NO)
1096 {
1097 sm_lastError = wxPRINTER_ERROR;
1098 wxFAIL_MSG(_("The print dialog returned an error."));
1099 return NULL;
1100 }
1101
1102 m_printDialogData = dialog.GetPrintDialogData();
1103
1104 return new wxPrinterDC( m_printDialogData.GetPrintData() );
1105 }
1106
1107 bool wxGtkPrinter::Setup( wxWindow * WXUNUSED(parent) )
1108 {
1109 // Obsolete, for backward compatibility.
1110 return false;
1111 }
1112
1113 //-----------------------------------------------------------------------------
1114 // wxGtkPrinterDC
1115 //-----------------------------------------------------------------------------
1116
1117 #define wxCAIRO_SCALE 1
1118
1119 #if wxCAIRO_SCALE
1120
1121 #define XLOG2DEV(x) LogicalToDeviceX(x)
1122 #define XLOG2DEVREL(x) LogicalToDeviceXRel(x)
1123 #define YLOG2DEV(x) LogicalToDeviceY(x)
1124 #define YLOG2DEVREL(x) LogicalToDeviceYRel(x)
1125
1126 #else
1127
1128 #define XLOG2DEV(x) ((double)(LogicalToDeviceX(x)) * m_DEV2PS)
1129 #define XLOG2DEVREL(x) ((double)(LogicalToDeviceXRel(x)) * m_DEV2PS)
1130 #define YLOG2DEV(x) ((double)(LogicalToDeviceY(x)) * m_DEV2PS)
1131 #define YLOG2DEVREL(x) ((double)(LogicalToDeviceYRel(x)) * m_DEV2PS)
1132
1133 #endif
1134
1135 IMPLEMENT_ABSTRACT_CLASS(wxGtkPrinterDCImpl, wxDCImpl)
1136
1137 wxGtkPrinterDCImpl::wxGtkPrinterDCImpl(wxPrinterDC *owner, const wxPrintData& data)
1138 : wxDCImpl( owner )
1139 {
1140 m_printData = data;
1141
1142 wxGtkPrintNativeData *native =
1143 (wxGtkPrintNativeData*) m_printData.GetNativeData();
1144
1145 m_gpc = native->GetPrintContext();
1146
1147 // Match print quality to resolution (high = 1200dpi)
1148 m_resolution = m_printData.GetQuality(); // (int) gtk_print_context_get_dpi_x( m_gpc );
1149 if (m_resolution < 0)
1150 m_resolution = (1 << (m_resolution+4)) *150;
1151
1152 m_context = gtk_print_context_create_pango_context( m_gpc );
1153 m_layout = gtk_print_context_create_pango_layout ( m_gpc );
1154 m_fontdesc = pango_font_description_from_string( "Sans 12" );
1155
1156 m_cairo = gtk_print_context_get_cairo_context ( m_gpc );
1157
1158 #if wxCAIRO_SCALE
1159 m_PS2DEV = 1.0;
1160 m_DEV2PS = 1.0;
1161
1162 cairo_scale( m_cairo, 72.0 / (double)m_resolution, 72.0 / (double)m_resolution );
1163 #else
1164 m_PS2DEV = (double)m_resolution / 72.0;
1165 m_DEV2PS = 72.0 / (double)m_resolution;
1166 #endif
1167
1168 m_currentRed = 0;
1169 m_currentBlue = 0;
1170 m_currentGreen = 0;
1171
1172 m_signX = 1; // default x-axis left to right.
1173 m_signY = 1; // default y-axis bottom up -> top down.
1174
1175 // By default the origin of the Cairo context is in the upper left
1176 // corner of the printable area. We need to translate it so that it
1177 // is in the upper left corner of the paper (without margins)
1178 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
1179 gdouble ml, mt;
1180 ml = gtk_page_setup_get_left_margin (setup, GTK_UNIT_POINTS);
1181 mt = gtk_page_setup_get_top_margin (setup, GTK_UNIT_POINTS);
1182 cairo_translate(m_cairo, -ml, -mt);
1183 }
1184
1185 wxGtkPrinterDCImpl::~wxGtkPrinterDCImpl()
1186 {
1187 g_object_unref(m_context);
1188 g_object_unref(m_layout);
1189 }
1190
1191 bool wxGtkPrinterDCImpl::IsOk() const
1192 {
1193 return m_gpc != NULL;
1194 }
1195
1196 void* wxGtkPrinterDCImpl::GetCairoContext() const
1197 {
1198 return (void*) cairo_reference( m_cairo );
1199 }
1200
1201 bool wxGtkPrinterDCImpl::DoFloodFill(wxCoord WXUNUSED(x1),
1202 wxCoord WXUNUSED(y1),
1203 const wxColour& WXUNUSED(col),
1204 wxFloodFillStyle WXUNUSED(style))
1205 {
1206 // We can't access the given coord as a Cairo context is scalable, ie a
1207 // coord doesn't mean anything in this context.
1208 wxFAIL_MSG(_("not implemented"));
1209 return false;
1210 }
1211
1212 void wxGtkPrinterDCImpl::DoGradientFillConcentric(const wxRect& rect, const wxColour& initialColour, const wxColour& destColour, const wxPoint& circleCenter)
1213 {
1214 wxCoord xC = circleCenter.x;
1215 wxCoord yC = circleCenter.y;
1216 wxCoord xR = rect.x;
1217 wxCoord yR = rect.y;
1218 wxCoord w = rect.width;
1219 wxCoord h = rect.height;
1220
1221 const double r2 = (w/2)*(w/2)+(h/2)*(h/2);
1222 double radius = sqrt(r2);
1223
1224 unsigned char redI = initialColour.Red();
1225 unsigned char blueI = initialColour.Blue();
1226 unsigned char greenI = initialColour.Green();
1227 unsigned char alphaI = initialColour.Alpha();
1228 unsigned char redD = destColour.Red();
1229 unsigned char blueD = destColour.Blue();
1230 unsigned char greenD = destColour.Green();
1231 unsigned char alphaD = destColour.Alpha();
1232
1233 double redIPS = (double)(redI) / 255.0;
1234 double blueIPS = (double)(blueI) / 255.0;
1235 double greenIPS = (double)(greenI) / 255.0;
1236 double alphaIPS = (double)(alphaI) / 255.0;
1237 double redDPS = (double)(redD) / 255.0;
1238 double blueDPS = (double)(blueD) / 255.0;
1239 double greenDPS = (double)(greenD) / 255.0;
1240 double alphaDPS = (double)(alphaD) / 255.0;
1241
1242 // Create a pattern with the gradient.
1243 cairo_pattern_t* gradient;
1244 gradient = cairo_pattern_create_radial (XLOG2DEV(xC+xR), YLOG2DEV(yC+yR), 0, XLOG2DEV(xC+xR), YLOG2DEV(yC+yR), radius * m_DEV2PS );
1245 cairo_pattern_add_color_stop_rgba (gradient, 0.0, redIPS, greenIPS, blueIPS, alphaIPS);
1246 cairo_pattern_add_color_stop_rgba (gradient, 1.0, redDPS, greenDPS, blueDPS, alphaDPS);
1247
1248 // Fill the rectangle with this pattern.
1249 cairo_set_source(m_cairo, gradient);
1250 cairo_rectangle (m_cairo, XLOG2DEV(xR), YLOG2DEV(yR), XLOG2DEVREL(w), YLOG2DEVREL(h) );
1251 cairo_fill(m_cairo);
1252
1253 cairo_pattern_destroy(gradient);
1254
1255 CalcBoundingBox(xR, yR);
1256 CalcBoundingBox(xR+w, yR+h);
1257 }
1258
1259 void wxGtkPrinterDCImpl::DoGradientFillLinear(const wxRect& rect, const wxColour& initialColour, const wxColour& destColour, wxDirection nDirection)
1260 {
1261 wxCoord x = rect.x;
1262 wxCoord y = rect.y;
1263 wxCoord w = rect.width;
1264 wxCoord h = rect.height;
1265
1266 unsigned char redI = initialColour.Red();
1267 unsigned char blueI = initialColour.Blue();
1268 unsigned char greenI = initialColour.Green();
1269 unsigned char alphaI = initialColour.Alpha();
1270 unsigned char redD = destColour.Red();
1271 unsigned char blueD = destColour.Blue();
1272 unsigned char greenD = destColour.Green();
1273 unsigned char alphaD = destColour.Alpha();
1274
1275 double redIPS = (double)(redI) / 255.0;
1276 double blueIPS = (double)(blueI) / 255.0;
1277 double greenIPS = (double)(greenI) / 255.0;
1278 double alphaIPS = (double)(alphaI) / 255.0;
1279 double redDPS = (double)(redD) / 255.0;
1280 double blueDPS = (double)(blueD) / 255.0;
1281 double greenDPS = (double)(greenD) / 255.0;
1282 double alphaDPS = (double)(alphaD) / 255.0;
1283
1284 // Create a pattern with the gradient.
1285 cairo_pattern_t* gradient;
1286 gradient = cairo_pattern_create_linear (XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x+w), YLOG2DEV(y));
1287
1288 if (nDirection == wxWEST)
1289 {
1290 cairo_pattern_add_color_stop_rgba (gradient, 0.0, redDPS, greenDPS, blueDPS, alphaDPS);
1291 cairo_pattern_add_color_stop_rgba (gradient, 1.0, redIPS, greenIPS, blueIPS, alphaIPS);
1292 }
1293 else {
1294 cairo_pattern_add_color_stop_rgba (gradient, 0.0, redIPS, greenIPS, blueIPS, alphaIPS);
1295 cairo_pattern_add_color_stop_rgba (gradient, 1.0, redDPS, greenDPS, blueDPS, alphaDPS);
1296 }
1297
1298 // Fill the rectangle with this pattern.
1299 cairo_set_source(m_cairo, gradient);
1300 cairo_rectangle (m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(w), YLOG2DEVREL(h) );
1301 cairo_fill(m_cairo);
1302
1303 cairo_pattern_destroy(gradient);
1304
1305 CalcBoundingBox(x, y);
1306 CalcBoundingBox(x+w, y+h);
1307 }
1308
1309 bool wxGtkPrinterDCImpl::DoGetPixel(wxCoord WXUNUSED(x1),
1310 wxCoord WXUNUSED(y1),
1311 wxColour * WXUNUSED(col)) const
1312 {
1313 wxFAIL_MSG(_("not implemented"));
1314 return false;
1315 }
1316
1317 void wxGtkPrinterDCImpl::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
1318 {
1319 if ( m_pen.IsTransparent() )
1320 return;
1321
1322 SetPen( m_pen );
1323 cairo_move_to ( m_cairo, XLOG2DEV(x1), YLOG2DEV(y1) );
1324 cairo_line_to ( m_cairo, XLOG2DEV(x2), YLOG2DEV(y2) );
1325 cairo_stroke ( m_cairo );
1326
1327 CalcBoundingBox( x1, y1 );
1328 CalcBoundingBox( x2, y2 );
1329 }
1330
1331 void wxGtkPrinterDCImpl::DoCrossHair(wxCoord x, wxCoord y)
1332 {
1333 int w, h;
1334 DoGetSize(&w, &h);
1335
1336 SetPen(m_pen);
1337
1338 cairo_move_to (m_cairo, XLOG2DEV(x), 0);
1339 cairo_line_to (m_cairo, XLOG2DEV(x), YLOG2DEVREL(h));
1340 cairo_move_to (m_cairo, 0, YLOG2DEV(y));
1341 cairo_line_to (m_cairo, XLOG2DEVREL(w), YLOG2DEV(y));
1342
1343 cairo_stroke (m_cairo);
1344 CalcBoundingBox( 0, 0 );
1345 CalcBoundingBox( w, h );
1346 }
1347
1348 void wxGtkPrinterDCImpl::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord xc,wxCoord yc)
1349 {
1350 double dx = x1 - xc;
1351 double dy = y1 - yc;
1352 double radius = sqrt((double)(dx*dx+dy*dy));
1353
1354 double alpha1, alpha2;
1355 if (x1 == x2 && y1 == y2)
1356 {
1357 alpha1 = 0.0;
1358 alpha2 = 360.0;
1359 }
1360 else
1361 if (radius == 0.0)
1362 {
1363 alpha1 = alpha2 = 0.0;
1364 }
1365 else
1366 {
1367 alpha1 = (x1 - xc == 0) ?
1368 (y1 - yc < 0) ? 90.0 : -90.0 :
1369 atan2(double(y1-yc), double(x1-xc)) * RAD2DEG;
1370 alpha2 = (x2 - xc == 0) ?
1371 (y2 - yc < 0) ? 90.0 : -90.0 :
1372 atan2(double(y2-yc), double(x2-xc)) * RAD2DEG;
1373
1374 while (alpha1 <= 0) alpha1 += 360;
1375 while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between.
1376 while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree.
1377 while (alpha2 > 360) alpha2 -= 360;
1378 }
1379
1380 alpha1 *= DEG2RAD;
1381 alpha2 *= DEG2RAD;
1382
1383 cairo_new_path(m_cairo);
1384
1385 cairo_arc_negative ( m_cairo, XLOG2DEV(xc), YLOG2DEV(yc), XLOG2DEVREL((int)radius), alpha1, alpha2);
1386 cairo_line_to(m_cairo, XLOG2DEV(xc), YLOG2DEV(yc));
1387 cairo_close_path (m_cairo);
1388
1389 SetBrush( m_brush );
1390 cairo_fill_preserve( m_cairo );
1391
1392 SetPen (m_pen);
1393 cairo_stroke( m_cairo );
1394
1395 CalcBoundingBox (x1, y1);
1396 CalcBoundingBox (xc, yc);
1397 CalcBoundingBox (x2, y2);
1398 }
1399
1400 void wxGtkPrinterDCImpl::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
1401 {
1402 cairo_save( m_cairo );
1403
1404 cairo_new_path(m_cairo);
1405
1406 cairo_translate( m_cairo, XLOG2DEV((wxCoord) (x + w / 2.)), XLOG2DEV((wxCoord) (y + h / 2.)) );
1407 double scale = (double)YLOG2DEVREL(h) / (double) XLOG2DEVREL(w);
1408 cairo_scale( m_cairo, 1.0, scale );
1409
1410 cairo_arc_negative ( m_cairo, 0, 0, XLOG2DEVREL(w/2), -sa*DEG2RAD, -ea*DEG2RAD);
1411
1412 SetPen (m_pen);
1413 cairo_stroke_preserve( m_cairo );
1414
1415 cairo_line_to(m_cairo, 0,0);
1416
1417 SetBrush( m_brush );
1418 cairo_fill( m_cairo );
1419
1420 cairo_restore( m_cairo );
1421
1422 CalcBoundingBox( x, y);
1423 CalcBoundingBox( x+w, y+h );
1424 }
1425
1426 void wxGtkPrinterDCImpl::DoDrawPoint(wxCoord x, wxCoord y)
1427 {
1428 if ( m_pen.IsTransparent() )
1429 return;
1430
1431 SetPen( m_pen );
1432
1433 cairo_move_to ( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1434 cairo_line_to ( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1435 cairo_stroke ( m_cairo );
1436
1437 CalcBoundingBox( x, y );
1438 }
1439
1440 void wxGtkPrinterDCImpl::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
1441 {
1442 if ( m_pen.IsTransparent() )
1443 return;
1444
1445
1446 if (n <= 0) return;
1447
1448 SetPen (m_pen);
1449
1450 int i;
1451 for ( i =0; i<n ; i++ )
1452 CalcBoundingBox( points[i].x+xoffset, points[i].y+yoffset);
1453
1454 cairo_move_to ( m_cairo, XLOG2DEV(points[0].x+xoffset), YLOG2DEV(points[0].y+yoffset) );
1455
1456 for (i = 1; i < n; i++)
1457 cairo_line_to ( m_cairo, XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset) );
1458
1459 cairo_stroke ( m_cairo);
1460 }
1461
1462 void wxGtkPrinterDCImpl::DoDrawPolygon(int n, wxPoint points[],
1463 wxCoord xoffset, wxCoord yoffset,
1464 wxPolygonFillMode fillStyle)
1465 {
1466 if (n==0) return;
1467
1468 cairo_save(m_cairo);
1469 if (fillStyle == wxWINDING_RULE)
1470 cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_WINDING);
1471 else
1472 cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_EVEN_ODD);
1473
1474 int x = points[0].x + xoffset;
1475 int y = points[0].y + yoffset;
1476 cairo_new_path(m_cairo);
1477 cairo_move_to( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1478 int i;
1479 for (i = 1; i < n; i++)
1480 {
1481 int x = points[i].x + xoffset;
1482 int y = points[i].y + yoffset;
1483 cairo_line_to( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1484 }
1485 cairo_close_path(m_cairo);
1486
1487 SetBrush( m_brush );
1488 cairo_fill_preserve( m_cairo );
1489
1490 SetPen (m_pen);
1491 cairo_stroke( m_cairo );
1492
1493 CalcBoundingBox( x, y );
1494
1495 cairo_restore(m_cairo);
1496 }
1497
1498 void wxGtkPrinterDCImpl::DoDrawPolyPolygon(int n, int count[], wxPoint points[],
1499 wxCoord xoffset, wxCoord yoffset,
1500 wxPolygonFillMode fillStyle)
1501 {
1502 wxDCImpl::DoDrawPolyPolygon( n, count, points, xoffset, yoffset, fillStyle );
1503 }
1504
1505 void wxGtkPrinterDCImpl::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1506 {
1507 width--;
1508 height--;
1509
1510 cairo_new_path(m_cairo);
1511 cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height));
1512
1513 SetBrush( m_brush );
1514 cairo_fill_preserve( m_cairo );
1515
1516 SetPen (m_pen);
1517 cairo_stroke( m_cairo );
1518
1519 CalcBoundingBox( x, y );
1520 CalcBoundingBox( x + width, y + height );
1521 }
1522
1523 void wxGtkPrinterDCImpl::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
1524 {
1525 width--;
1526 height--;
1527
1528 if (radius < 0.0) radius = - radius * ((width < height) ? width : height);
1529
1530 wxCoord dd = 2 * (wxCoord) radius;
1531 if (dd > width) dd = width;
1532 if (dd > height) dd = height;
1533 radius = dd / 2;
1534
1535 wxCoord rad = (wxCoord) radius;
1536
1537 cairo_new_path(m_cairo);
1538 cairo_move_to(m_cairo,XLOG2DEV(x + rad),YLOG2DEV(y));
1539 cairo_curve_to(m_cairo,
1540 XLOG2DEV(x + rad),YLOG2DEV(y),
1541 XLOG2DEV(x),YLOG2DEV(y),
1542 XLOG2DEV(x),YLOG2DEV(y + rad));
1543 cairo_line_to(m_cairo,XLOG2DEV(x),YLOG2DEV(y + height - rad));
1544 cairo_curve_to(m_cairo,
1545 XLOG2DEV(x),YLOG2DEV(y + height - rad),
1546 XLOG2DEV(x),YLOG2DEV(y + height),
1547 XLOG2DEV(x + rad),YLOG2DEV(y + height));
1548 cairo_line_to(m_cairo,XLOG2DEV(x + width - rad),YLOG2DEV(y + height));
1549 cairo_curve_to(m_cairo,
1550 XLOG2DEV(x + width - rad),YLOG2DEV(y + height),
1551 XLOG2DEV(x + width),YLOG2DEV(y + height),
1552 XLOG2DEV(x + width),YLOG2DEV(y + height - rad));
1553 cairo_line_to(m_cairo,XLOG2DEV(x + width),YLOG2DEV(y + rad));
1554 cairo_curve_to(m_cairo,
1555 XLOG2DEV(x + width),YLOG2DEV(y + rad),
1556 XLOG2DEV(x + width),YLOG2DEV(y),
1557 XLOG2DEV(x + width - rad),YLOG2DEV(y));
1558 cairo_line_to(m_cairo,XLOG2DEV(x + rad),YLOG2DEV(y));
1559 cairo_close_path(m_cairo);
1560
1561 SetBrush(m_brush);
1562 cairo_fill_preserve(m_cairo);
1563
1564 SetPen(m_pen);
1565 cairo_stroke(m_cairo);
1566
1567 CalcBoundingBox(x,y);
1568 CalcBoundingBox(x+width,y+height);
1569 }
1570
1571 void wxGtkPrinterDCImpl::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1572 {
1573 width--;
1574 height--;
1575
1576 cairo_save (m_cairo);
1577
1578 cairo_new_path(m_cairo);
1579
1580 cairo_translate (m_cairo, XLOG2DEV((wxCoord) (x + width / 2.)), YLOG2DEV((wxCoord) (y + height / 2.)));
1581 cairo_scale(m_cairo, 1, (double)YLOG2DEVREL(height)/(double)XLOG2DEVREL(width));
1582 cairo_arc ( m_cairo, 0, 0, XLOG2DEVREL(width/2), 0, 2 * M_PI);
1583
1584 SetBrush( m_brush );
1585 cairo_fill_preserve( m_cairo );
1586
1587 SetPen (m_pen);
1588 cairo_stroke( m_cairo );
1589
1590 CalcBoundingBox( x, y );
1591 CalcBoundingBox( x + width, y + height );
1592
1593 cairo_restore (m_cairo);
1594 }
1595
1596 #if wxUSE_SPLINES
1597 void wxGtkPrinterDCImpl::DoDrawSpline(const wxPointList *points)
1598 {
1599 SetPen (m_pen);
1600
1601 double c, d, x1, y1, x2, y2, x3, y3;
1602 wxPoint *p, *q;
1603
1604 wxPointList::compatibility_iterator node = points->GetFirst();
1605 p = node->GetData();
1606 x1 = p->x;
1607 y1 = p->y;
1608
1609 node = node->GetNext();
1610 p = node->GetData();
1611 c = p->x;
1612 d = p->y;
1613 x3 =
1614 (double)(x1 + c) / 2;
1615 y3 =
1616 (double)(y1 + d) / 2;
1617
1618 cairo_new_path( m_cairo );
1619 cairo_move_to( m_cairo, XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1) );
1620 cairo_line_to( m_cairo, XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
1621
1622 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1623 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1624
1625 node = node->GetNext();
1626 while (node)
1627 {
1628 q = node->GetData();
1629
1630 x1 = x3;
1631 y1 = y3;
1632 x2 = c;
1633 y2 = d;
1634 c = q->x;
1635 d = q->y;
1636 x3 = (double)(x2 + c) / 2;
1637 y3 = (double)(y2 + d) / 2;
1638
1639 cairo_curve_to(m_cairo,
1640 XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1),
1641 XLOG2DEV((wxCoord)x2), YLOG2DEV((wxCoord)y2),
1642 XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
1643
1644 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1645 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1646
1647 node = node->GetNext();
1648 }
1649
1650 cairo_line_to ( m_cairo, XLOG2DEV((wxCoord)c), YLOG2DEV((wxCoord)d) );
1651
1652 cairo_stroke( m_cairo );
1653 }
1654 #endif // wxUSE_SPLINES
1655
1656 bool wxGtkPrinterDCImpl::DoBlit(wxCoord xdest, wxCoord ydest,
1657 wxCoord width, wxCoord height,
1658 wxDC *source, wxCoord xsrc, wxCoord ysrc,
1659 wxRasterOperationMode rop, bool useMask,
1660 wxCoord WXUNUSED_UNLESS_DEBUG(xsrcMask),
1661 wxCoord WXUNUSED_UNLESS_DEBUG(ysrcMask))
1662 {
1663 wxASSERT_MSG( xsrcMask == wxDefaultCoord && ysrcMask == wxDefaultCoord,
1664 wxT("mask coordinates are not supported") );
1665
1666 wxCHECK_MSG( source, false, wxT("invalid source dc") );
1667
1668 // Blit into a bitmap.
1669 wxBitmap bitmap( width, height );
1670 wxMemoryDC memDC;
1671 memDC.SelectObject(bitmap);
1672 memDC.Blit(0, 0, width, height, source, xsrc, ysrc, rop);
1673 memDC.SelectObject(wxNullBitmap);
1674
1675 // Draw bitmap. scaling and positioning is done there.
1676 GetOwner()->DrawBitmap( bitmap, xdest, ydest, useMask );
1677
1678 return true;
1679 }
1680
1681 void wxGtkPrinterDCImpl::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
1682 {
1683 DoDrawBitmap( icon, x, y, true );
1684 }
1685
1686 void wxGtkPrinterDCImpl::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool useMask )
1687 {
1688 wxCHECK_RET( bitmap.IsOk(), wxT("Invalid bitmap in wxGtkPrinterDCImpl::DoDrawBitmap"));
1689
1690 x = wxCoord(XLOG2DEV(x));
1691 y = wxCoord(YLOG2DEV(y));
1692 int bw = bitmap.GetWidth();
1693 int bh = bitmap.GetHeight();
1694 wxBitmap bmpSource = bitmap; // we need a non-const instance.
1695 if (!useMask && !bitmap.HasPixbuf() && bitmap.GetMask())
1696 bmpSource.SetMask(NULL);
1697
1698 cairo_save(m_cairo);
1699
1700 // Prepare to draw the image.
1701 cairo_translate(m_cairo, x, y);
1702
1703 // Scale the image
1704 wxDouble scaleX = (wxDouble) XLOG2DEVREL(bw) / (wxDouble) bw;
1705 wxDouble scaleY = (wxDouble) YLOG2DEVREL(bh) / (wxDouble) bh;
1706 cairo_scale(m_cairo, scaleX, scaleY);
1707
1708 gdk_cairo_set_source_pixbuf(m_cairo, bmpSource.GetPixbuf(), 0, 0);
1709 cairo_pattern_set_filter(cairo_get_source(m_cairo), CAIRO_FILTER_NEAREST);
1710 // Use the original size here since the context is scaled already.
1711 cairo_rectangle(m_cairo, 0, 0, bw, bh);
1712 // Fill the rectangle using the pattern.
1713 cairo_fill(m_cairo);
1714
1715 CalcBoundingBox(0,0);
1716 CalcBoundingBox(bw,bh);
1717
1718 cairo_restore(m_cairo);
1719 }
1720
1721 void wxGtkPrinterDCImpl::DoDrawText(const wxString& text, wxCoord x, wxCoord y )
1722 {
1723 DoDrawRotatedText( text, x, y, 0.0 );
1724 }
1725
1726 void wxGtkPrinterDCImpl::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
1727 {
1728 double xx = XLOG2DEV(x);
1729 double yy = YLOG2DEV(y);
1730
1731 angle = -angle;
1732
1733 bool underlined = m_font.Ok() && m_font.GetUnderlined();
1734
1735 const wxScopedCharBuffer data = text.utf8_str();
1736
1737 size_t datalen = strlen(data);
1738 pango_layout_set_text( m_layout, data, datalen);
1739
1740 if (underlined)
1741 {
1742 PangoAttrList *attrs = pango_attr_list_new();
1743 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
1744 a->start_index = 0;
1745 a->end_index = datalen;
1746 pango_attr_list_insert(attrs, a);
1747 pango_layout_set_attributes(m_layout, attrs);
1748 pango_attr_list_unref(attrs);
1749 }
1750
1751 if (m_textForegroundColour.Ok())
1752 {
1753 unsigned char red = m_textForegroundColour.Red();
1754 unsigned char blue = m_textForegroundColour.Blue();
1755 unsigned char green = m_textForegroundColour.Green();
1756 unsigned char alpha = m_textForegroundColour.Alpha();
1757
1758 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1759 {
1760 double redPS = (double)(red) / 255.0;
1761 double bluePS = (double)(blue) / 255.0;
1762 double greenPS = (double)(green) / 255.0;
1763 double alphaPS = (double)(alpha) / 255.0;
1764
1765 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1766
1767 m_currentRed = red;
1768 m_currentBlue = blue;
1769 m_currentGreen = green;
1770 m_currentAlpha = alpha;
1771 }
1772 }
1773
1774 // Draw layout.
1775 cairo_move_to (m_cairo, xx, yy);
1776
1777 cairo_save( m_cairo );
1778
1779 if (fabs(angle) > 0.00001)
1780 cairo_rotate( m_cairo, angle*DEG2RAD );
1781
1782 cairo_scale(m_cairo, m_scaleX, m_scaleY);
1783
1784 int w,h;
1785 pango_layout_get_pixel_size( m_layout, &w, &h );
1786
1787 if ( m_backgroundMode == wxBRUSHSTYLE_SOLID )
1788 {
1789 unsigned char red = m_textBackgroundColour.Red();
1790 unsigned char blue = m_textBackgroundColour.Blue();
1791 unsigned char green = m_textBackgroundColour.Green();
1792 unsigned char alpha = m_textBackgroundColour.Alpha();
1793
1794 double redPS = (double)(red) / 255.0;
1795 double bluePS = (double)(blue) / 255.0;
1796 double greenPS = (double)(green) / 255.0;
1797 double alphaPS = (double)(alpha) / 255.0;
1798
1799 cairo_save(m_cairo);
1800 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1801 cairo_rectangle(m_cairo, 0, 0, w, h); // still in cairo units
1802 cairo_fill(m_cairo);
1803 cairo_restore(m_cairo);
1804 }
1805
1806 pango_cairo_update_layout (m_cairo, m_layout);
1807 pango_cairo_show_layout (m_cairo, m_layout);
1808
1809 cairo_restore( m_cairo );
1810
1811 if (underlined)
1812 {
1813 // Undo underline attributes setting
1814 pango_layout_set_attributes(m_layout, NULL);
1815 }
1816
1817 // Back to device units:
1818 CalcBoundingBox (x, y);
1819 CalcBoundingBox (x + w, y + h);
1820 }
1821
1822 void wxGtkPrinterDCImpl::Clear()
1823 {
1824 // Clear does nothing for printing, but keep the code
1825 // for later reuse
1826 /*
1827 cairo_save(m_cairo);
1828 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
1829 SetBrush(m_backgroundBrush);
1830 cairo_paint(m_cairo);
1831 cairo_restore(m_cairo);
1832 */
1833 }
1834
1835 void wxGtkPrinterDCImpl::SetFont( const wxFont& font )
1836 {
1837 m_font = font;
1838
1839 if (m_font.Ok())
1840 {
1841 if (m_fontdesc)
1842 pango_font_description_free( m_fontdesc );
1843
1844 m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description );
1845
1846 float size = pango_font_description_get_size( m_fontdesc );
1847 size = size * GetFontPointSizeAdjustment(72.0);
1848 pango_font_description_set_size( m_fontdesc, (gint)size );
1849
1850 pango_layout_set_font_description( m_layout, m_fontdesc );
1851 }
1852 }
1853
1854 void wxGtkPrinterDCImpl::SetPen( const wxPen& pen )
1855 {
1856 if (!pen.Ok()) return;
1857
1858 m_pen = pen;
1859
1860 double width;
1861
1862 if (m_pen.GetWidth() <= 0)
1863 width = 0.1;
1864 else
1865 width = (double) m_pen.GetWidth();
1866
1867 cairo_set_line_width( m_cairo, width * m_DEV2PS * m_scaleX );
1868 static const double dotted[] = {2.0, 5.0};
1869 static const double short_dashed[] = {4.0, 4.0};
1870 static const double long_dashed[] = {4.0, 8.0};
1871 static const double dotted_dashed[] = {6.0, 6.0, 2.0, 6.0};
1872
1873 switch (m_pen.GetStyle())
1874 {
1875 case wxPENSTYLE_DOT: cairo_set_dash( m_cairo, dotted, 2, 0 ); break;
1876 case wxPENSTYLE_SHORT_DASH: cairo_set_dash( m_cairo, short_dashed, 2, 0 ); break;
1877 case wxPENSTYLE_LONG_DASH: cairo_set_dash( m_cairo, long_dashed, 2, 0 ); break;
1878 case wxPENSTYLE_DOT_DASH: cairo_set_dash( m_cairo, dotted_dashed, 4, 0 ); break;
1879 case wxPENSTYLE_USER_DASH:
1880 {
1881 wxDash *wx_dashes;
1882 int num = m_pen.GetDashes (&wx_dashes);
1883 gdouble *g_dashes = g_new( gdouble, num );
1884 int i;
1885 for (i = 0; i < num; ++i)
1886 g_dashes[i] = (gdouble) wx_dashes[i];
1887 cairo_set_dash( m_cairo, g_dashes, num, 0);
1888 g_free( g_dashes );
1889 }
1890 break;
1891 case wxPENSTYLE_SOLID:
1892 case wxPENSTYLE_TRANSPARENT:
1893 default: cairo_set_dash( m_cairo, NULL, 0, 0 ); break;
1894 }
1895
1896 switch (m_pen.GetCap())
1897 {
1898 case wxCAP_PROJECTING: cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_SQUARE); break;
1899 case wxCAP_BUTT: cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_BUTT); break;
1900 case wxCAP_ROUND:
1901 default: cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_ROUND); break;
1902 }
1903
1904 switch (m_pen.GetJoin())
1905 {
1906 case wxJOIN_BEVEL: cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_BEVEL); break;
1907 case wxJOIN_MITER: cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_MITER); break;
1908 case wxJOIN_ROUND:
1909 default: cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_ROUND); break;
1910 }
1911
1912 unsigned char red = m_pen.GetColour().Red();
1913 unsigned char blue = m_pen.GetColour().Blue();
1914 unsigned char green = m_pen.GetColour().Green();
1915 unsigned char alpha = m_pen.GetColour().Alpha();
1916
1917 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1918 {
1919 double redPS = (double)(red) / 255.0;
1920 double bluePS = (double)(blue) / 255.0;
1921 double greenPS = (double)(green) / 255.0;
1922 double alphaPS = (double)(alpha) / 255.0;
1923
1924 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1925
1926 m_currentRed = red;
1927 m_currentBlue = blue;
1928 m_currentGreen = green;
1929 m_currentAlpha = alpha;
1930 }
1931 }
1932
1933 void wxGtkPrinterDCImpl::SetBrush( const wxBrush& brush )
1934 {
1935 if (!brush.Ok()) return;
1936
1937 m_brush = brush;
1938
1939 if (m_brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT)
1940 {
1941 cairo_set_source_rgba( m_cairo, 0, 0, 0, 0 );
1942 m_currentRed = 0;
1943 m_currentBlue = 0;
1944 m_currentGreen = 0;
1945 m_currentAlpha = 0;
1946 return;
1947 }
1948
1949 // Brush colour.
1950 unsigned char red = m_brush.GetColour().Red();
1951 unsigned char blue = m_brush.GetColour().Blue();
1952 unsigned char green = m_brush.GetColour().Green();
1953 unsigned char alpha = m_brush.GetColour().Alpha();
1954
1955 double redPS = (double)(red) / 255.0;
1956 double bluePS = (double)(blue) / 255.0;
1957 double greenPS = (double)(green) / 255.0;
1958 double alphaPS = (double)(alpha) / 255.0;
1959
1960 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1961 {
1962 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1963
1964 m_currentRed = red;
1965 m_currentBlue = blue;
1966 m_currentGreen = green;
1967 m_currentAlpha = alpha;
1968 }
1969
1970 if (m_brush.IsHatch())
1971 {
1972 cairo_t * cr;
1973 cairo_surface_t *surface;
1974 surface = cairo_surface_create_similar(cairo_get_target(m_cairo),CAIRO_CONTENT_COLOR_ALPHA,10,10);
1975 cr = cairo_create(surface);
1976 cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
1977 cairo_set_line_width(cr, 1);
1978 cairo_set_line_join(cr,CAIRO_LINE_JOIN_MITER);
1979
1980 switch (m_brush.GetStyle())
1981 {
1982 case wxBRUSHSTYLE_CROSS_HATCH:
1983 cairo_move_to(cr, 5, 0);
1984 cairo_line_to(cr, 5, 10);
1985 cairo_move_to(cr, 0, 5);
1986 cairo_line_to(cr, 10, 5);
1987 break;
1988 case wxBRUSHSTYLE_BDIAGONAL_HATCH:
1989 cairo_move_to(cr, 0, 10);
1990 cairo_line_to(cr, 10, 0);
1991 break;
1992 case wxBRUSHSTYLE_FDIAGONAL_HATCH:
1993 cairo_move_to(cr, 0, 0);
1994 cairo_line_to(cr, 10, 10);
1995 break;
1996 case wxBRUSHSTYLE_CROSSDIAG_HATCH:
1997 cairo_move_to(cr, 0, 0);
1998 cairo_line_to(cr, 10, 10);
1999 cairo_move_to(cr, 10, 0);
2000 cairo_line_to(cr, 0, 10);
2001 break;
2002 case wxBRUSHSTYLE_HORIZONTAL_HATCH:
2003 cairo_move_to(cr, 0, 5);
2004 cairo_line_to(cr, 10, 5);
2005 break;
2006 case wxBRUSHSTYLE_VERTICAL_HATCH:
2007 cairo_move_to(cr, 5, 0);
2008 cairo_line_to(cr, 5, 10);
2009 break;
2010 default:
2011 wxFAIL_MSG(_("Couldn't get hatch style from wxBrush."));
2012 }
2013
2014 cairo_set_source_rgba(cr, redPS, greenPS, bluePS, alphaPS);
2015 cairo_stroke (cr);
2016
2017 cairo_destroy(cr);
2018 cairo_pattern_t * pattern = cairo_pattern_create_for_surface (surface);
2019 cairo_surface_destroy(surface);
2020 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
2021 cairo_set_source(m_cairo, pattern);
2022 cairo_pattern_destroy(pattern);
2023 }
2024 }
2025
2026 void wxGtkPrinterDCImpl::SetLogicalFunction( wxRasterOperationMode function )
2027 {
2028 if (function == wxCLEAR)
2029 cairo_set_operator (m_cairo, CAIRO_OPERATOR_CLEAR);
2030 else if (function == wxOR)
2031 cairo_set_operator (m_cairo, CAIRO_OPERATOR_OUT);
2032 else if (function == wxNO_OP)
2033 cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST);
2034 else if (function == wxAND)
2035 cairo_set_operator (m_cairo, CAIRO_OPERATOR_ADD);
2036 else if (function == wxSET)
2037 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SATURATE);
2038 else if (function == wxXOR)
2039 cairo_set_operator (m_cairo, CAIRO_OPERATOR_XOR);
2040 else // wxCOPY or anything else.
2041 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
2042 }
2043
2044 void wxGtkPrinterDCImpl::SetBackground( const wxBrush& brush )
2045 {
2046 m_backgroundBrush = brush;
2047 cairo_save(m_cairo);
2048 cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST_OVER);
2049
2050 SetBrush(m_backgroundBrush);
2051 cairo_paint(m_cairo);
2052 cairo_restore(m_cairo);
2053 }
2054
2055 void wxGtkPrinterDCImpl::SetBackgroundMode(int mode)
2056 {
2057 if (mode == wxBRUSHSTYLE_SOLID)
2058 m_backgroundMode = wxBRUSHSTYLE_SOLID;
2059 else
2060 m_backgroundMode = wxBRUSHSTYLE_TRANSPARENT;
2061 }
2062
2063 void wxGtkPrinterDCImpl::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
2064 {
2065 cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height));
2066 cairo_clip(m_cairo);
2067 }
2068
2069 void wxGtkPrinterDCImpl::DestroyClippingRegion()
2070 {
2071 cairo_reset_clip(m_cairo);
2072 }
2073
2074 bool wxGtkPrinterDCImpl::StartDoc(const wxString& WXUNUSED(message))
2075 {
2076 return true;
2077 }
2078
2079 void wxGtkPrinterDCImpl::EndDoc()
2080 {
2081 return;
2082 }
2083
2084 void wxGtkPrinterDCImpl::StartPage()
2085 {
2086 return;
2087 }
2088
2089 void wxGtkPrinterDCImpl::EndPage()
2090 {
2091 return;
2092 }
2093
2094 wxCoord wxGtkPrinterDCImpl::GetCharHeight() const
2095 {
2096 pango_layout_set_text( m_layout, "H", 1 );
2097
2098 int w,h;
2099 pango_layout_get_pixel_size( m_layout, &w, &h );
2100
2101 return wxRound( h * m_PS2DEV );
2102 }
2103
2104 wxCoord wxGtkPrinterDCImpl::GetCharWidth() const
2105 {
2106 pango_layout_set_text( m_layout, "H", 1 );
2107
2108 int w,h;
2109 pango_layout_get_pixel_size( m_layout, &w, &h );
2110
2111 return wxRound( w * m_PS2DEV );
2112 }
2113
2114 void wxGtkPrinterDCImpl::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoord *height,
2115 wxCoord *descent,
2116 wxCoord *externalLeading,
2117 const wxFont *theFont ) const
2118 {
2119 if ( width )
2120 *width = 0;
2121 if ( height )
2122 *height = 0;
2123 if ( descent )
2124 *descent = 0;
2125 if ( externalLeading )
2126 *externalLeading = 0;
2127
2128 if (string.empty())
2129 {
2130 return;
2131 }
2132
2133 cairo_save( m_cairo );
2134 cairo_scale(m_cairo, m_scaleX, m_scaleY);
2135
2136 // Set layout's text
2137 const wxScopedCharBuffer dataUTF8 = string.utf8_str();
2138
2139 gint oldSize=0;
2140 if ( theFont )
2141 {
2142 // scale the font and apply it
2143 PangoFontDescription *desc = theFont->GetNativeFontInfo()->description;
2144 float size = pango_font_description_get_size(desc);
2145 size = size * GetFontPointSizeAdjustment(72.0);
2146 pango_font_description_set_size(desc, (gint)size);
2147
2148 pango_layout_set_font_description(m_layout, desc);
2149 }
2150
2151 pango_layout_set_text( m_layout, dataUTF8, strlen(dataUTF8) );
2152
2153 int h;
2154 pango_layout_get_pixel_size( m_layout, width, &h );
2155 if ( height )
2156 *height = h;
2157
2158 if (descent)
2159 {
2160 PangoLayoutIter *iter = pango_layout_get_iter(m_layout);
2161 int baseline = pango_layout_iter_get_baseline(iter);
2162 pango_layout_iter_free(iter);
2163 *descent = h - PANGO_PIXELS(baseline);
2164 }
2165
2166 if ( theFont )
2167 {
2168 // restore font and reset font's size back
2169 pango_layout_set_font_description(m_layout, m_fontdesc);
2170
2171 PangoFontDescription *desc = theFont->GetNativeFontInfo()->description;
2172 pango_font_description_set_size(desc, oldSize);
2173 }
2174
2175 cairo_restore( m_cairo );
2176 }
2177
2178 void wxGtkPrinterDCImpl::DoGetSize(int* width, int* height) const
2179 {
2180 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
2181
2182 if (width)
2183 *width = wxRound( (double)gtk_page_setup_get_paper_width( setup, GTK_UNIT_POINTS ) * (double)m_resolution / 72.0 );
2184 if (height)
2185 *height = wxRound( (double)gtk_page_setup_get_paper_height( setup, GTK_UNIT_POINTS ) * (double)m_resolution / 72.0 );
2186 }
2187
2188 void wxGtkPrinterDCImpl::DoGetSizeMM(int *width, int *height) const
2189 {
2190 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
2191
2192 if (width)
2193 *width = wxRound( gtk_page_setup_get_paper_width( setup, GTK_UNIT_MM ) );
2194 if (height)
2195 *height = wxRound( gtk_page_setup_get_paper_height( setup, GTK_UNIT_MM ) );
2196 }
2197
2198 wxSize wxGtkPrinterDCImpl::GetPPI() const
2199 {
2200 return wxSize( (int)m_resolution, (int)m_resolution );
2201 }
2202
2203 void wxGtkPrinterDCImpl::SetPrintData(const wxPrintData& data)
2204 {
2205 m_printData = data;
2206 }
2207
2208 // overriden for wxPrinterDC Impl
2209
2210 wxRect wxGtkPrinterDCImpl::GetPaperRect() const
2211 {
2212 // Does GtkPrint support printer margins?
2213 int w = 0;
2214 int h = 0;
2215 DoGetSize( &w, &h );
2216 return wxRect( 0,0,w,h );
2217 }
2218
2219 int wxGtkPrinterDCImpl::GetResolution() const
2220 {
2221 return m_resolution;
2222 }
2223
2224 // ----------------------------------------------------------------------------
2225 // Print preview
2226 // ----------------------------------------------------------------------------
2227
2228 IMPLEMENT_CLASS(wxGtkPrintPreview, wxPrintPreviewBase)
2229
2230 void wxGtkPrintPreview::Init(wxPrintout * WXUNUSED(printout),
2231 wxPrintout * WXUNUSED(printoutForPrinting),
2232 wxPrintData *data)
2233 {
2234 // convert wxPrintQuality to resolution (input pointer can be NULL)
2235 wxPrintQuality quality = data ? data->GetQuality() : wxPRINT_QUALITY_MEDIUM;
2236 switch ( quality )
2237 {
2238 case wxPRINT_QUALITY_HIGH:
2239 m_resolution = 1200;
2240 break;
2241
2242 case wxPRINT_QUALITY_LOW:
2243 m_resolution = 300;
2244 break;
2245
2246 case wxPRINT_QUALITY_DRAFT:
2247 m_resolution = 150;
2248 break;
2249
2250 default:
2251 if ( quality > 0 )
2252 {
2253 // positive values directly indicate print resolution
2254 m_resolution = quality;
2255 break;
2256 }
2257
2258 wxFAIL_MSG( "unknown print quality" );
2259 // fall through
2260
2261 case wxPRINT_QUALITY_MEDIUM:
2262 m_resolution = 600;
2263 break;
2264
2265 }
2266
2267 DetermineScaling();
2268 }
2269
2270 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout,
2271 wxPrintout *printoutForPrinting,
2272 wxPrintDialogData *data)
2273 : wxPrintPreviewBase(printout, printoutForPrinting, data)
2274 {
2275 Init(printout, printoutForPrinting, data ? &data->GetPrintData() : NULL);
2276 }
2277
2278 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout,
2279 wxPrintout *printoutForPrinting,
2280 wxPrintData *data)
2281 : wxPrintPreviewBase(printout, printoutForPrinting, data)
2282 {
2283 Init(printout, printoutForPrinting, data);
2284 }
2285
2286 wxGtkPrintPreview::~wxGtkPrintPreview()
2287 {
2288 }
2289
2290 bool wxGtkPrintPreview::Print(bool interactive)
2291 {
2292 if (!m_printPrintout)
2293 return false;
2294
2295 wxPrinter printer(& m_printDialogData);
2296 return printer.Print(m_previewFrame, m_printPrintout, interactive);
2297 }
2298
2299 void wxGtkPrintPreview::DetermineScaling()
2300 {
2301 wxPaperSize paperType = m_printDialogData.GetPrintData().GetPaperId();
2302
2303 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
2304 if (!paper)
2305 paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
2306
2307 if (paper)
2308 {
2309 m_previewPrintout->SetPPIScreen(wxGetDisplayPPI());
2310 m_previewPrintout->SetPPIPrinter( m_resolution, m_resolution );
2311
2312 // Get width and height in points (1/72th of an inch)
2313 wxSize sizeDevUnits(paper->GetSizeDeviceUnits());
2314 sizeDevUnits.x = wxRound((double)sizeDevUnits.x * (double)m_resolution / 72.0);
2315 sizeDevUnits.y = wxRound((double)sizeDevUnits.y * (double)m_resolution / 72.0);
2316
2317 wxSize sizeTenthsMM(paper->GetSize());
2318 wxSize sizeMM(sizeTenthsMM.x / 10, sizeTenthsMM.y / 10);
2319
2320 // If in landscape mode, we need to swap the width and height.
2321 if ( m_printDialogData.GetPrintData().GetOrientation() == wxLANDSCAPE )
2322 {
2323 m_pageWidth = sizeDevUnits.y;
2324 m_pageHeight = sizeDevUnits.x;
2325 m_previewPrintout->SetPageSizeMM(sizeMM.y, sizeMM.x);
2326 }
2327 else
2328 {
2329 m_pageWidth = sizeDevUnits.x;
2330 m_pageHeight = sizeDevUnits.y;
2331 m_previewPrintout->SetPageSizeMM(sizeMM.x, sizeMM.y);
2332 }
2333 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
2334 m_previewPrintout->SetPaperRectPixels(wxRect(0, 0, m_pageWidth, m_pageHeight));
2335
2336 // At 100%, the page should look about page-size on the screen.
2337 m_previewScaleX = 0.8 * 72.0 / (double)m_resolution;
2338 m_previewScaleY = m_previewScaleX;
2339 }
2340 }
2341
2342 #endif
2343 // wxUSE_GTKPRINT