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