Fix referencing of cairo_t returned from wxDCImpl::GetCairoContext().
[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 // Copyright: (c) 2007 wxWidgets development team
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #if wxUSE_GTKPRINT
18
19 #include "wx/gtk/print.h"
20
21 #ifndef WX_PRECOMP
22 #include "wx/log.h"
23 #include "wx/dcmemory.h"
24 #include "wx/dcprint.h"
25 #include "wx/icon.h"
26 #include "wx/math.h"
27 #include "wx/image.h"
28 #include "wx/module.h"
29 #include "wx/crt.h"
30 #endif
31
32 #include "wx/fontutil.h"
33 #include "wx/dynlib.h"
34 #include "wx/paper.h"
35 #include "wx/scopeguard.h"
36 #include "wx/modalhook.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 WX_HOOK_MODAL_DIALOG();
627
628 // We need to restore the settings given in the constructor.
629 wxPrintData data = m_printDialogData.GetPrintData();
630 wxGtkPrintNativeData *native =
631 (wxGtkPrintNativeData*) data.GetNativeData();
632 data.ConvertToNative();
633
634 GtkPrintSettings * settings = native->GetPrintConfig();
635
636 // We have to restore pages to print here because they're stored in a wxPrintDialogData and ConvertToNative only works for wxPrintData.
637 int fromPage = m_printDialogData.GetFromPage();
638 int toPage = m_printDialogData.GetToPage();
639 if (m_printDialogData.GetSelection())
640 gtk_print_settings_set_print_pages(settings, GTK_PRINT_PAGES_CURRENT);
641 else if (m_printDialogData.GetAllPages())
642 gtk_print_settings_set_print_pages(settings, GTK_PRINT_PAGES_ALL);
643 else {
644 gtk_print_settings_set_print_pages(settings, GTK_PRINT_PAGES_RANGES);
645 GtkPageRange *range;
646 range = g_new (GtkPageRange, 1);
647 range[0].start = fromPage-1;
648 range[0].end = (toPage >= fromPage) ? toPage-1 : fromPage-1;
649 gtk_print_settings_set_page_ranges (settings, range, 1);
650 }
651
652 GtkPrintOperation * const printOp = native->GetPrintJob();
653
654 // If the settings are OK, we restore it.
655 if (settings != NULL)
656 gtk_print_operation_set_print_settings (printOp, settings);
657 gtk_print_operation_set_default_page_setup (printOp, native->GetPageSetupFromSettings(settings));
658
659 // Show the dialog if needed.
660 GError* gError = NULL;
661 GtkPrintOperationResult response = gtk_print_operation_run
662 (
663 printOp,
664 GetShowDialog()
665 ? GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
666 : GTK_PRINT_OPERATION_ACTION_PRINT,
667 m_parent
668 ? GTK_WINDOW(gtk_widget_get_toplevel(m_parent->m_widget))
669 : NULL,
670 &gError
671 );
672
673 // Does everything went well?
674 if (response == GTK_PRINT_OPERATION_RESULT_CANCEL)
675 {
676 return wxID_CANCEL;
677 }
678 else if (response == GTK_PRINT_OPERATION_RESULT_ERROR)
679 {
680 wxLogError(_("Error while printing: ") + wxString(gError ? gError->message : "???"));
681 g_error_free (gError);
682 return wxID_NO; // We use wxID_NO because there is no wxID_ERROR available
683 }
684
685 // Now get the settings and save it.
686 GtkPrintSettings* newSettings = gtk_print_operation_get_print_settings(printOp);
687 native->SetPrintConfig(newSettings);
688 data.ConvertFromNative();
689
690 // Set PrintDialogData variables
691 m_printDialogData.SetPrintData(data);
692 m_printDialogData.SetCollate(data.GetCollate());
693 m_printDialogData.SetNoCopies(data.GetNoCopies());
694 m_printDialogData.SetPrintToFile(data.GetPrinterName() == "Print to File");
695
696 // Same problem as a few lines before.
697 switch (gtk_print_settings_get_print_pages(newSettings))
698 {
699 case GTK_PRINT_PAGES_CURRENT:
700 m_printDialogData.SetSelection( true );
701 break;
702 case GTK_PRINT_PAGES_RANGES:
703 {// wxWidgets doesn't support multiple ranges, so we can only save the first one even if the user wants to print others.
704 // 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
705 // will hit "OK" button. However we can only save 1-3 in the print data.
706 gint num_ranges = 0;
707 GtkPageRange* range;
708 range = gtk_print_settings_get_page_ranges (newSettings, &num_ranges);
709 if (num_ranges >= 1)
710 {
711 m_printDialogData.SetFromPage( range[0].start );
712 m_printDialogData.SetToPage( range[0].end );
713 }
714 else {
715 m_printDialogData.SetAllPages( true );
716 m_printDialogData.SetFromPage( 0 );
717 m_printDialogData.SetToPage( 9999 );
718 }
719 break;}
720 case GTK_PRINT_PAGES_ALL:
721 default:
722 m_printDialogData.SetAllPages( true );
723 m_printDialogData.SetFromPage( 0 );
724 m_printDialogData.SetToPage( 9999 );
725 break;
726 }
727
728 return wxID_OK;
729 }
730
731 //----------------------------------------------------------------------------
732 // wxGtkPageSetupDialog
733 //----------------------------------------------------------------------------
734
735 IMPLEMENT_CLASS(wxGtkPageSetupDialog, wxPageSetupDialogBase)
736
737 wxGtkPageSetupDialog::wxGtkPageSetupDialog( wxWindow *parent,
738 wxPageSetupDialogData* data )
739 {
740 if (data)
741 m_pageDialogData = *data;
742
743 m_parent = parent;
744 }
745
746 wxGtkPageSetupDialog::~wxGtkPageSetupDialog()
747 {
748 }
749
750 int wxGtkPageSetupDialog::ShowModal()
751 {
752 WX_HOOK_MODAL_DIALOG();
753
754 // Get the config.
755 m_pageDialogData.GetPrintData().ConvertToNative();
756 wxGtkPrintNativeData *native = (wxGtkPrintNativeData*) m_pageDialogData.GetPrintData().GetNativeData();
757 GtkPrintSettings* nativeData = native->GetPrintConfig();
758
759 // We only need the pagesetup data which are part of the settings.
760 GtkPageSetup* oldPageSetup = native->GetPageSetupFromSettings(nativeData);
761
762 // If the user used a custom paper format the last time he printed, we have to restore it too.
763 if (m_pageDialogData.GetPrintData().GetPaperId() == wxPAPER_NONE)
764 {
765 wxSize customPaperSize = m_pageDialogData.GetPaperSize();
766 if (customPaperSize.GetWidth() > 0 && customPaperSize.GetHeight() > 0)
767 {
768 wxString title = _("Custom size");
769 GtkPaperSize* customSize = gtk_paper_size_new_custom ("custom", title.mb_str(), (gdouble) customPaperSize.GetWidth(), (gdouble) customPaperSize.GetHeight(), GTK_UNIT_MM);
770 gtk_page_setup_set_paper_size_and_default_margins (oldPageSetup, customSize);
771 g_object_unref(customSize);
772 }
773 }
774
775
776 // Set selected printer
777 gtk_print_settings_set(nativeData, "format-for-printer",
778 gtk_print_settings_get_printer(nativeData));
779
780 // Create custom dialog
781 wxString title(GetTitle());
782 if ( title.empty() )
783 title = _("Page Setup");
784 GtkWidget *
785 dlg = gtk_page_setup_unix_dialog_new(title.utf8_str(),
786 m_parent
787 ? GTK_WINDOW(m_parent->m_widget)
788 : NULL);
789
790 gtk_page_setup_unix_dialog_set_print_settings(
791 GTK_PAGE_SETUP_UNIX_DIALOG(dlg), nativeData);
792 gtk_page_setup_unix_dialog_set_page_setup(
793 GTK_PAGE_SETUP_UNIX_DIALOG(dlg), oldPageSetup);
794
795 int result = gtk_dialog_run(GTK_DIALOG(dlg));
796 gtk_widget_hide(dlg);
797
798 switch ( result )
799 {
800 case GTK_RESPONSE_OK:
801 case GTK_RESPONSE_APPLY:
802 {
803 // Store Selected Printer Name
804 gtk_print_settings_set_printer
805 (
806 nativeData,
807 gtk_print_settings_get(nativeData, "format-for-printer")
808 );
809
810 wxGtkObject<GtkPageSetup>
811 newPageSetup(gtk_page_setup_unix_dialog_get_page_setup(
812 GTK_PAGE_SETUP_UNIX_DIALOG(dlg)));
813 native->SetPageSetupToSettings(nativeData, newPageSetup);
814
815 m_pageDialogData.GetPrintData().ConvertFromNative();
816
817 // Store custom paper format if any.
818 if ( m_pageDialogData.GetPrintData().GetPaperId() == wxPAPER_NONE )
819 {
820 gdouble ml,mr,mt,mb,pw,ph;
821 ml = gtk_page_setup_get_left_margin (newPageSetup, GTK_UNIT_MM);
822 mr = gtk_page_setup_get_right_margin (newPageSetup, GTK_UNIT_MM);
823 mt = gtk_page_setup_get_top_margin (newPageSetup, GTK_UNIT_MM);
824 mb = gtk_page_setup_get_bottom_margin (newPageSetup, GTK_UNIT_MM);
825
826 pw = gtk_page_setup_get_paper_width (newPageSetup, GTK_UNIT_MM);
827 ph = gtk_page_setup_get_paper_height (newPageSetup, GTK_UNIT_MM);
828
829 m_pageDialogData.SetMarginTopLeft(wxPoint((int)(ml+0.5),
830 (int)(mt+0.5)));
831 m_pageDialogData.SetMarginBottomRight(wxPoint((int)(mr+0.5),
832 (int)(mb+0.5)));
833
834 m_pageDialogData.SetPaperSize(wxSize((int)(pw+0.5),
835 (int)(ph+0.5)));
836 }
837
838 result = wxID_OK;
839 }
840 break;
841
842 default:
843 case GTK_RESPONSE_CANCEL:
844 result = wxID_CANCEL;
845 break;
846 }
847
848 gtk_widget_destroy(dlg);
849
850 return result;
851 }
852
853 //----------------------------------------------------------------------------
854 // wxGtkPrinter
855 //----------------------------------------------------------------------------
856
857 IMPLEMENT_CLASS(wxGtkPrinter, wxPrinterBase)
858
859 wxGtkPrinter::wxGtkPrinter( wxPrintDialogData *data ) :
860 wxPrinterBase( data )
861 {
862 m_gpc = NULL;
863
864 if (data)
865 m_printDialogData = *data;
866 }
867
868 wxGtkPrinter::~wxGtkPrinter()
869 {
870 }
871
872 bool wxGtkPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt )
873 {
874 if (!printout)
875 {
876 sm_lastError = wxPRINTER_ERROR;
877 return false;
878 }
879
880 // Let's correct the PageInfo just in case the app gives wrong values.
881 int fromPage, toPage;
882 int minPage, maxPage;
883 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
884 m_printDialogData.SetAllPages(true);
885
886 if (minPage < 1) minPage = 1;
887 if (maxPage < 1) maxPage = 9999;
888 if (maxPage < minPage) maxPage = minPage;
889
890 m_printDialogData.SetMinPage(minPage);
891 m_printDialogData.SetMaxPage(maxPage);
892 if (fromPage != 0)
893 {
894 if (fromPage < minPage) fromPage = minPage;
895 else if (fromPage > maxPage) fromPage = maxPage;
896 m_printDialogData.SetFromPage(fromPage);
897 }
898 if (toPage != 0)
899 {
900 m_printDialogData.SetToPage(toPage);
901 if (toPage > maxPage) toPage = maxPage;
902 else if (toPage < minPage) toPage = minPage;
903 }
904
905 if (((minPage != fromPage) && fromPage != 0) || ((maxPage != toPage) && toPage != 0)) m_printDialogData.SetAllPages(false);
906
907
908 wxPrintData printdata = GetPrintDialogData().GetPrintData();
909 wxGtkPrintNativeData *native = (wxGtkPrintNativeData*) printdata.GetNativeData();
910
911 wxGtkObject<GtkPrintOperation> printOp(gtk_print_operation_new());
912 native->SetPrintJob(printOp);
913 wxON_BLOCK_EXIT_OBJ1(*native, wxGtkPrintNativeData::SetPrintJob,
914 static_cast<GtkPrintOperation*>(NULL));
915
916 wxPrinterToGtkData dataToSend;
917 dataToSend.printer = this;
918 dataToSend.printout = printout;
919
920 // These Gtk signals are caught here.
921 g_signal_connect (printOp, "begin-print", G_CALLBACK (gtk_begin_print_callback), &dataToSend);
922 g_signal_connect (printOp, "draw-page", G_CALLBACK (gtk_draw_page_print_callback), &dataToSend);
923 g_signal_connect (printOp, "end-print", G_CALLBACK (gtk_end_print_callback), printout);
924
925 // This is used to setup the DC and
926 // show the dialog if desired
927 wxGtkPrintDialog dialog( parent, &m_printDialogData );
928 dialog.SetPrintDC(m_dc);
929 dialog.SetShowDialog(prompt);
930
931 // doesn't necessarily show
932 int ret = dialog.ShowModal();
933 if (ret == wxID_CANCEL)
934 {
935 sm_lastError = wxPRINTER_CANCELLED;
936 }
937 if (ret == wxID_NO)
938 {
939 sm_lastError = wxPRINTER_ERROR;
940 wxFAIL_MSG(_("The print dialog returned an error."));
941 }
942
943 return (sm_lastError == wxPRINTER_NO_ERROR);
944 }
945
946 void wxGtkPrinter::BeginPrint(wxPrintout *printout, GtkPrintOperation *operation, GtkPrintContext *context)
947 {
948 wxPrintData printdata = GetPrintDialogData().GetPrintData();
949 wxGtkPrintNativeData *native = (wxGtkPrintNativeData*) printdata.GetNativeData();
950
951 // We need to update printdata with the new data from the dialog and we
952 // have to do this here because this method needs this new data and we
953 // cannot update it earlier
954 native->SetPrintConfig(gtk_print_operation_get_print_settings(operation));
955 printdata.ConvertFromNative();
956
957 SetPrintContext(context);
958 native->SetPrintContext( context );
959
960 wxPrinterDC *printDC = new wxPrinterDC( printdata );
961 m_dc = printDC;
962
963 if (!m_dc->IsOk())
964 {
965 if (sm_lastError != wxPRINTER_CANCELLED)
966 {
967 sm_lastError = wxPRINTER_ERROR;
968 wxFAIL_MSG(_("The wxGtkPrinterDC cannot be used."));
969 }
970 return;
971 }
972
973 printout->SetPPIScreen(wxGetDisplayPPI());
974 printout->SetPPIPrinter( printDC->GetResolution(),
975 printDC->GetResolution() );
976
977 printout->SetDC(m_dc);
978
979 int w, h;
980 m_dc->GetSize(&w, &h);
981 printout->SetPageSizePixels((int)w, (int)h);
982 printout->SetPaperRectPixels(wxRect(0, 0, w, h));
983 int mw, mh;
984 m_dc->GetSizeMM(&mw, &mh);
985 printout->SetPageSizeMM((int)mw, (int)mh);
986 printout->OnPreparePrinting();
987
988 // Get some parameters from the printout, if defined.
989 int fromPage, toPage;
990 int minPage, maxPage;
991 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
992
993 if (maxPage == 0)
994 {
995 sm_lastError = wxPRINTER_ERROR;
996 wxFAIL_MSG(_("wxPrintout::GetPageInfo gives a null maxPage."));
997 return;
998 }
999
1000 printout->OnBeginPrinting();
1001
1002 int numPages = 0;
1003
1004 // If we're not previewing we need to calculate the number of pages to print.
1005 // If we're previewing, Gtk Print will render every pages without wondering about the page ranges the user may
1006 // have defined in the dialog. So the number of pages is the maximum available.
1007 if (!printout->IsPreview())
1008 {
1009 GtkPrintSettings * settings = gtk_print_operation_get_print_settings (operation);
1010 switch (gtk_print_settings_get_print_pages(settings))
1011 {
1012 case GTK_PRINT_PAGES_CURRENT:
1013 numPages = 1;
1014 break;
1015 case GTK_PRINT_PAGES_RANGES:
1016 {gint num_ranges = 0;
1017 GtkPageRange* range;
1018 int i;
1019 range = gtk_print_settings_get_page_ranges (settings, &num_ranges);
1020 for (i=0; i<num_ranges; i++)
1021 {
1022 if (range[i].end < range[i].start) range[i].end = range[i].start;
1023 if (range[i].start < minPage-1) range[i].start = minPage-1;
1024 if (range[i].end > maxPage-1) range[i].end = maxPage-1;
1025 if (range[i].start > maxPage-1) range[i].start = maxPage-1;
1026 numPages += range[i].end - range[i].start + 1;
1027 }
1028 gtk_print_settings_set_page_ranges (settings, range, 1);
1029 break;}
1030 case GTK_PRINT_PAGES_ALL:
1031 default:
1032 numPages = maxPage - minPage + 1;
1033 break;
1034 }
1035 }
1036 else numPages = maxPage - minPage + 1;
1037
1038 gtk_print_operation_set_n_pages(operation, numPages);
1039 }
1040
1041 void wxGtkPrinter::DrawPage(wxPrintout *printout,
1042 GtkPrintOperation *operation,
1043 GtkPrintContext * WXUNUSED(context),
1044 int page_nr)
1045 {
1046 int fromPage, toPage, minPage, maxPage, startPage, endPage;
1047 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
1048
1049 int numPageToDraw = page_nr + minPage;
1050 if (numPageToDraw < minPage) numPageToDraw = minPage;
1051 if (numPageToDraw > maxPage) numPageToDraw = maxPage;
1052
1053 GtkPrintSettings * settings = gtk_print_operation_get_print_settings (operation);
1054 switch (gtk_print_settings_get_print_pages(settings))
1055 {
1056 case GTK_PRINT_PAGES_CURRENT:
1057 g_object_get_property((GObject*) operation, (const gchar *) "current-page", (GValue*) &startPage);
1058 g_object_get_property((GObject*) operation, (const gchar *) "current-page", (GValue*) &endPage);
1059 break;
1060 case GTK_PRINT_PAGES_RANGES:
1061 {gint num_ranges = 0;
1062 GtkPageRange* range;
1063 range = gtk_print_settings_get_page_ranges (settings, &num_ranges);
1064 // We don't need to verify these values as it has already been done in wxGtkPrinter::BeginPrint.
1065 if (num_ranges >= 1)
1066 {
1067 startPage = range[0].start + 1;
1068 endPage = range[0].end + 1;
1069 }
1070 else {
1071 startPage = minPage;
1072 endPage = maxPage;
1073 }
1074 break;}
1075 case GTK_PRINT_PAGES_ALL:
1076 default:
1077 startPage = minPage;
1078 endPage = maxPage;
1079 break;
1080 }
1081
1082 if(numPageToDraw == startPage)
1083 {
1084 if (!printout->OnBeginDocument(startPage, endPage))
1085 {
1086 wxLogError(_("Could not start printing."));
1087 sm_lastError = wxPRINTER_ERROR;
1088 }
1089 }
1090
1091 // The app can render the page numPageToDraw.
1092 if (printout->HasPage(numPageToDraw))
1093 {
1094 m_dc->StartPage();
1095 printout->OnPrintPage(numPageToDraw);
1096 m_dc->EndPage();
1097 }
1098
1099
1100 if(numPageToDraw == endPage)
1101 {
1102 printout->OnEndDocument();
1103 }
1104 }
1105
1106 wxDC* wxGtkPrinter::PrintDialog( wxWindow *parent )
1107 {
1108 wxGtkPrintDialog dialog( parent, &m_printDialogData );
1109
1110 dialog.SetPrintDC(m_dc);
1111 dialog.SetShowDialog(true);
1112
1113 int ret = dialog.ShowModal();
1114
1115 if (ret == wxID_CANCEL)
1116 {
1117 sm_lastError = wxPRINTER_CANCELLED;
1118 return NULL;
1119 }
1120 if (ret == wxID_NO)
1121 {
1122 sm_lastError = wxPRINTER_ERROR;
1123 wxFAIL_MSG(_("The print dialog returned an error."));
1124 return NULL;
1125 }
1126
1127 m_printDialogData = dialog.GetPrintDialogData();
1128
1129 return new wxPrinterDC( m_printDialogData.GetPrintData() );
1130 }
1131
1132 bool wxGtkPrinter::Setup( wxWindow * WXUNUSED(parent) )
1133 {
1134 // Obsolete, for backward compatibility.
1135 return false;
1136 }
1137
1138 //-----------------------------------------------------------------------------
1139 // wxGtkPrinterDC
1140 //-----------------------------------------------------------------------------
1141
1142 #define wxCAIRO_SCALE 1
1143
1144 #if wxCAIRO_SCALE
1145
1146 #define XLOG2DEV(x) LogicalToDeviceX(x)
1147 #define XLOG2DEVREL(x) LogicalToDeviceXRel(x)
1148 #define YLOG2DEV(x) LogicalToDeviceY(x)
1149 #define YLOG2DEVREL(x) LogicalToDeviceYRel(x)
1150
1151 #else
1152
1153 #define XLOG2DEV(x) ((double)(LogicalToDeviceX(x)) * m_DEV2PS)
1154 #define XLOG2DEVREL(x) ((double)(LogicalToDeviceXRel(x)) * m_DEV2PS)
1155 #define YLOG2DEV(x) ((double)(LogicalToDeviceY(x)) * m_DEV2PS)
1156 #define YLOG2DEVREL(x) ((double)(LogicalToDeviceYRel(x)) * m_DEV2PS)
1157
1158 #endif
1159
1160 IMPLEMENT_ABSTRACT_CLASS(wxGtkPrinterDCImpl, wxDCImpl)
1161
1162 wxGtkPrinterDCImpl::wxGtkPrinterDCImpl(wxPrinterDC *owner, const wxPrintData& data)
1163 : wxDCImpl( owner )
1164 {
1165 m_printData = data;
1166
1167 wxGtkPrintNativeData *native =
1168 (wxGtkPrintNativeData*) m_printData.GetNativeData();
1169
1170 m_gpc = native->GetPrintContext();
1171
1172 // Match print quality to resolution (high = 1200dpi)
1173 m_resolution = m_printData.GetQuality(); // (int) gtk_print_context_get_dpi_x( m_gpc );
1174 if (m_resolution < 0)
1175 m_resolution = (1 << (m_resolution+4)) *150;
1176
1177 m_context = gtk_print_context_create_pango_context( m_gpc );
1178 m_layout = gtk_print_context_create_pango_layout ( m_gpc );
1179 m_fontdesc = pango_font_description_from_string( "Sans 12" );
1180
1181 m_cairo = gtk_print_context_get_cairo_context ( m_gpc );
1182
1183 #if wxCAIRO_SCALE
1184 m_PS2DEV = 1.0;
1185 m_DEV2PS = 1.0;
1186 #else
1187 m_PS2DEV = (double)m_resolution / 72.0;
1188 m_DEV2PS = 72.0 / (double)m_resolution;
1189 #endif
1190
1191 m_currentRed = 0;
1192 m_currentBlue = 0;
1193 m_currentGreen = 0;
1194
1195 m_signX = 1; // default x-axis left to right.
1196 m_signY = 1; // default y-axis bottom up -> top down.
1197 }
1198
1199 wxGtkPrinterDCImpl::~wxGtkPrinterDCImpl()
1200 {
1201 g_object_unref(m_context);
1202 g_object_unref(m_layout);
1203 }
1204
1205 bool wxGtkPrinterDCImpl::IsOk() const
1206 {
1207 return m_gpc != NULL;
1208 }
1209
1210 void* wxGtkPrinterDCImpl::GetCairoContext() const
1211 {
1212 return m_cairo;
1213 }
1214
1215 void* wxGtkPrinterDCImpl::GetHandle() const
1216 {
1217 return GetCairoContext();
1218 }
1219
1220 bool wxGtkPrinterDCImpl::DoFloodFill(wxCoord WXUNUSED(x1),
1221 wxCoord WXUNUSED(y1),
1222 const wxColour& WXUNUSED(col),
1223 wxFloodFillStyle WXUNUSED(style))
1224 {
1225 // We can't access the given coord as a Cairo context is scalable, ie a
1226 // coord doesn't mean anything in this context.
1227 wxFAIL_MSG(_("not implemented"));
1228 return false;
1229 }
1230
1231 void wxGtkPrinterDCImpl::DoGradientFillConcentric(const wxRect& rect, const wxColour& initialColour, const wxColour& destColour, const wxPoint& circleCenter)
1232 {
1233 wxCoord xC = circleCenter.x;
1234 wxCoord yC = circleCenter.y;
1235 wxCoord xR = rect.x;
1236 wxCoord yR = rect.y;
1237 wxCoord w = rect.width;
1238 wxCoord h = rect.height;
1239
1240 const double r2 = (w/2)*(w/2)+(h/2)*(h/2);
1241 double radius = sqrt(r2);
1242
1243 unsigned char redI = initialColour.Red();
1244 unsigned char blueI = initialColour.Blue();
1245 unsigned char greenI = initialColour.Green();
1246 unsigned char alphaI = initialColour.Alpha();
1247 unsigned char redD = destColour.Red();
1248 unsigned char blueD = destColour.Blue();
1249 unsigned char greenD = destColour.Green();
1250 unsigned char alphaD = destColour.Alpha();
1251
1252 double redIPS = (double)(redI) / 255.0;
1253 double blueIPS = (double)(blueI) / 255.0;
1254 double greenIPS = (double)(greenI) / 255.0;
1255 double alphaIPS = (double)(alphaI) / 255.0;
1256 double redDPS = (double)(redD) / 255.0;
1257 double blueDPS = (double)(blueD) / 255.0;
1258 double greenDPS = (double)(greenD) / 255.0;
1259 double alphaDPS = (double)(alphaD) / 255.0;
1260
1261 // Create a pattern with the gradient.
1262 cairo_pattern_t* gradient;
1263 gradient = cairo_pattern_create_radial (XLOG2DEV(xC+xR), YLOG2DEV(yC+yR), 0, XLOG2DEV(xC+xR), YLOG2DEV(yC+yR), radius * m_DEV2PS );
1264 cairo_pattern_add_color_stop_rgba (gradient, 0.0, redIPS, greenIPS, blueIPS, alphaIPS);
1265 cairo_pattern_add_color_stop_rgba (gradient, 1.0, redDPS, greenDPS, blueDPS, alphaDPS);
1266
1267 // Fill the rectangle with this pattern.
1268 cairo_set_source(m_cairo, gradient);
1269 cairo_rectangle (m_cairo, XLOG2DEV(xR), YLOG2DEV(yR), XLOG2DEVREL(w), YLOG2DEVREL(h) );
1270 cairo_fill(m_cairo);
1271
1272 cairo_pattern_destroy(gradient);
1273
1274 CalcBoundingBox(xR, yR);
1275 CalcBoundingBox(xR+w, yR+h);
1276 }
1277
1278 void wxGtkPrinterDCImpl::DoGradientFillLinear(const wxRect& rect, const wxColour& initialColour, const wxColour& destColour, wxDirection nDirection)
1279 {
1280 wxCoord x = rect.x;
1281 wxCoord y = rect.y;
1282 wxCoord w = rect.width;
1283 wxCoord h = rect.height;
1284
1285 unsigned char redI = initialColour.Red();
1286 unsigned char blueI = initialColour.Blue();
1287 unsigned char greenI = initialColour.Green();
1288 unsigned char alphaI = initialColour.Alpha();
1289 unsigned char redD = destColour.Red();
1290 unsigned char blueD = destColour.Blue();
1291 unsigned char greenD = destColour.Green();
1292 unsigned char alphaD = destColour.Alpha();
1293
1294 double redIPS = (double)(redI) / 255.0;
1295 double blueIPS = (double)(blueI) / 255.0;
1296 double greenIPS = (double)(greenI) / 255.0;
1297 double alphaIPS = (double)(alphaI) / 255.0;
1298 double redDPS = (double)(redD) / 255.0;
1299 double blueDPS = (double)(blueD) / 255.0;
1300 double greenDPS = (double)(greenD) / 255.0;
1301 double alphaDPS = (double)(alphaD) / 255.0;
1302
1303 // Create a pattern with the gradient.
1304 cairo_pattern_t* gradient;
1305 gradient = cairo_pattern_create_linear (XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x+w), YLOG2DEV(y));
1306
1307 if (nDirection == wxWEST)
1308 {
1309 cairo_pattern_add_color_stop_rgba (gradient, 0.0, redDPS, greenDPS, blueDPS, alphaDPS);
1310 cairo_pattern_add_color_stop_rgba (gradient, 1.0, redIPS, greenIPS, blueIPS, alphaIPS);
1311 }
1312 else {
1313 cairo_pattern_add_color_stop_rgba (gradient, 0.0, redIPS, greenIPS, blueIPS, alphaIPS);
1314 cairo_pattern_add_color_stop_rgba (gradient, 1.0, redDPS, greenDPS, blueDPS, alphaDPS);
1315 }
1316
1317 // Fill the rectangle with this pattern.
1318 cairo_set_source(m_cairo, gradient);
1319 cairo_rectangle (m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(w), YLOG2DEVREL(h) );
1320 cairo_fill(m_cairo);
1321
1322 cairo_pattern_destroy(gradient);
1323
1324 CalcBoundingBox(x, y);
1325 CalcBoundingBox(x+w, y+h);
1326 }
1327
1328 bool wxGtkPrinterDCImpl::DoGetPixel(wxCoord WXUNUSED(x1),
1329 wxCoord WXUNUSED(y1),
1330 wxColour * WXUNUSED(col)) const
1331 {
1332 wxFAIL_MSG(_("not implemented"));
1333 return false;
1334 }
1335
1336 void wxGtkPrinterDCImpl::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
1337 {
1338 if ( m_pen.IsTransparent() )
1339 return;
1340
1341 SetPen( m_pen );
1342 cairo_move_to ( m_cairo, XLOG2DEV(x1), YLOG2DEV(y1) );
1343 cairo_line_to ( m_cairo, XLOG2DEV(x2), YLOG2DEV(y2) );
1344 cairo_stroke ( m_cairo );
1345
1346 CalcBoundingBox( x1, y1 );
1347 CalcBoundingBox( x2, y2 );
1348 }
1349
1350 void wxGtkPrinterDCImpl::DoCrossHair(wxCoord x, wxCoord y)
1351 {
1352 int w, h;
1353 DoGetSize(&w, &h);
1354
1355 SetPen(m_pen);
1356
1357 cairo_move_to (m_cairo, XLOG2DEV(x), 0);
1358 cairo_line_to (m_cairo, XLOG2DEV(x), YLOG2DEVREL(h));
1359 cairo_move_to (m_cairo, 0, YLOG2DEV(y));
1360 cairo_line_to (m_cairo, XLOG2DEVREL(w), YLOG2DEV(y));
1361
1362 cairo_stroke (m_cairo);
1363 CalcBoundingBox( 0, 0 );
1364 CalcBoundingBox( w, h );
1365 }
1366
1367 void wxGtkPrinterDCImpl::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord xc,wxCoord yc)
1368 {
1369 double dx = x1 - xc;
1370 double dy = y1 - yc;
1371 double radius = sqrt((double)(dx*dx+dy*dy));
1372
1373 double alpha1, alpha2;
1374 if (x1 == x2 && y1 == y2)
1375 {
1376 alpha1 = 0.0;
1377 alpha2 = 360.0;
1378 }
1379 else
1380 if (radius == 0.0)
1381 {
1382 alpha1 = alpha2 = 0.0;
1383 }
1384 else
1385 {
1386 alpha1 = (x1 - xc == 0) ?
1387 (y1 - yc < 0) ? 90.0 : -90.0 :
1388 atan2(double(y1-yc), double(x1-xc)) * RAD2DEG;
1389 alpha2 = (x2 - xc == 0) ?
1390 (y2 - yc < 0) ? 90.0 : -90.0 :
1391 atan2(double(y2-yc), double(x2-xc)) * RAD2DEG;
1392
1393 while (alpha1 <= 0) alpha1 += 360;
1394 while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between.
1395 while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree.
1396 while (alpha2 > 360) alpha2 -= 360;
1397 }
1398
1399 alpha1 *= DEG2RAD;
1400 alpha2 *= DEG2RAD;
1401
1402 cairo_new_path(m_cairo);
1403
1404 cairo_arc_negative ( m_cairo, XLOG2DEV(xc), YLOG2DEV(yc), XLOG2DEVREL((int)radius), alpha1, alpha2);
1405 cairo_line_to(m_cairo, XLOG2DEV(xc), YLOG2DEV(yc));
1406 cairo_close_path (m_cairo);
1407
1408 SetBrush( m_brush );
1409 cairo_fill_preserve( m_cairo );
1410
1411 SetPen (m_pen);
1412 cairo_stroke( m_cairo );
1413
1414 CalcBoundingBox (x1, y1);
1415 CalcBoundingBox (xc, yc);
1416 CalcBoundingBox (x2, y2);
1417 }
1418
1419 void wxGtkPrinterDCImpl::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
1420 {
1421 cairo_save( m_cairo );
1422
1423 cairo_new_path(m_cairo);
1424
1425 cairo_translate( m_cairo, XLOG2DEV((wxCoord) (x + w / 2.)), XLOG2DEV((wxCoord) (y + h / 2.)) );
1426 double scale = (double)YLOG2DEVREL(h) / (double) XLOG2DEVREL(w);
1427 cairo_scale( m_cairo, 1.0, scale );
1428
1429 cairo_arc_negative ( m_cairo, 0, 0, XLOG2DEVREL(w/2), -sa*DEG2RAD, -ea*DEG2RAD);
1430
1431 SetPen (m_pen);
1432 cairo_stroke_preserve( m_cairo );
1433
1434 cairo_line_to(m_cairo, 0,0);
1435
1436 SetBrush( m_brush );
1437 cairo_fill( m_cairo );
1438
1439 cairo_restore( m_cairo );
1440
1441 CalcBoundingBox( x, y);
1442 CalcBoundingBox( x+w, y+h );
1443 }
1444
1445 void wxGtkPrinterDCImpl::DoDrawPoint(wxCoord x, wxCoord y)
1446 {
1447 if ( m_pen.IsTransparent() )
1448 return;
1449
1450 SetPen( m_pen );
1451
1452 cairo_move_to ( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1453 cairo_line_to ( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1454 cairo_stroke ( m_cairo );
1455
1456 CalcBoundingBox( x, y );
1457 }
1458
1459 void wxGtkPrinterDCImpl::DoDrawLines(int n, const wxPoint points[], wxCoord xoffset, wxCoord yoffset)
1460 {
1461 if ( m_pen.IsTransparent() )
1462 return;
1463
1464
1465 if (n <= 0) return;
1466
1467 SetPen (m_pen);
1468
1469 int i;
1470 for ( i =0; i<n ; i++ )
1471 CalcBoundingBox( points[i].x+xoffset, points[i].y+yoffset);
1472
1473 cairo_move_to ( m_cairo, XLOG2DEV(points[0].x+xoffset), YLOG2DEV(points[0].y+yoffset) );
1474
1475 for (i = 1; i < n; i++)
1476 cairo_line_to ( m_cairo, XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset) );
1477
1478 cairo_stroke ( m_cairo);
1479 }
1480
1481 void wxGtkPrinterDCImpl::DoDrawPolygon(int n, const wxPoint points[],
1482 wxCoord xoffset, wxCoord yoffset,
1483 wxPolygonFillMode fillStyle)
1484 {
1485 if (n==0) return;
1486
1487 cairo_save(m_cairo);
1488 if (fillStyle == wxWINDING_RULE)
1489 cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_WINDING);
1490 else
1491 cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_EVEN_ODD);
1492
1493 int x = points[0].x + xoffset;
1494 int y = points[0].y + yoffset;
1495 cairo_new_path(m_cairo);
1496 cairo_move_to( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1497 int i;
1498 for (i = 1; i < n; i++)
1499 {
1500 int xx = points[i].x + xoffset;
1501 int yy = points[i].y + yoffset;
1502 cairo_line_to( m_cairo, XLOG2DEV(xx), YLOG2DEV(yy) );
1503 }
1504 cairo_close_path(m_cairo);
1505
1506 SetBrush( m_brush );
1507 cairo_fill_preserve( m_cairo );
1508
1509 SetPen (m_pen);
1510 cairo_stroke( m_cairo );
1511
1512 CalcBoundingBox( x, y );
1513
1514 cairo_restore(m_cairo);
1515 }
1516
1517 void wxGtkPrinterDCImpl::DoDrawPolyPolygon(int n, const int count[], const wxPoint points[],
1518 wxCoord xoffset, wxCoord yoffset,
1519 wxPolygonFillMode fillStyle)
1520 {
1521 wxDCImpl::DoDrawPolyPolygon( n, count, points, xoffset, yoffset, fillStyle );
1522 }
1523
1524 void wxGtkPrinterDCImpl::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1525 {
1526 width--;
1527 height--;
1528
1529 cairo_new_path(m_cairo);
1530 cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height));
1531
1532 SetBrush( m_brush );
1533 cairo_fill_preserve( m_cairo );
1534
1535 SetPen (m_pen);
1536 cairo_stroke( m_cairo );
1537
1538 CalcBoundingBox( x, y );
1539 CalcBoundingBox( x + width, y + height );
1540 }
1541
1542 void wxGtkPrinterDCImpl::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
1543 {
1544 width--;
1545 height--;
1546
1547 if (radius < 0.0) radius = - radius * ((width < height) ? width : height);
1548
1549 wxCoord dd = 2 * (wxCoord) radius;
1550 if (dd > width) dd = width;
1551 if (dd > height) dd = height;
1552 radius = dd / 2;
1553
1554 wxCoord rad = (wxCoord) radius;
1555
1556 cairo_new_path(m_cairo);
1557 cairo_move_to(m_cairo,XLOG2DEV(x + rad),YLOG2DEV(y));
1558 cairo_curve_to(m_cairo,
1559 XLOG2DEV(x + rad),YLOG2DEV(y),
1560 XLOG2DEV(x),YLOG2DEV(y),
1561 XLOG2DEV(x),YLOG2DEV(y + rad));
1562 cairo_line_to(m_cairo,XLOG2DEV(x),YLOG2DEV(y + height - rad));
1563 cairo_curve_to(m_cairo,
1564 XLOG2DEV(x),YLOG2DEV(y + height - rad),
1565 XLOG2DEV(x),YLOG2DEV(y + height),
1566 XLOG2DEV(x + rad),YLOG2DEV(y + height));
1567 cairo_line_to(m_cairo,XLOG2DEV(x + width - rad),YLOG2DEV(y + height));
1568 cairo_curve_to(m_cairo,
1569 XLOG2DEV(x + width - rad),YLOG2DEV(y + height),
1570 XLOG2DEV(x + width),YLOG2DEV(y + height),
1571 XLOG2DEV(x + width),YLOG2DEV(y + height - rad));
1572 cairo_line_to(m_cairo,XLOG2DEV(x + width),YLOG2DEV(y + rad));
1573 cairo_curve_to(m_cairo,
1574 XLOG2DEV(x + width),YLOG2DEV(y + rad),
1575 XLOG2DEV(x + width),YLOG2DEV(y),
1576 XLOG2DEV(x + width - rad),YLOG2DEV(y));
1577 cairo_line_to(m_cairo,XLOG2DEV(x + rad),YLOG2DEV(y));
1578 cairo_close_path(m_cairo);
1579
1580 SetBrush(m_brush);
1581 cairo_fill_preserve(m_cairo);
1582
1583 SetPen(m_pen);
1584 cairo_stroke(m_cairo);
1585
1586 CalcBoundingBox(x,y);
1587 CalcBoundingBox(x+width,y+height);
1588 }
1589
1590 void wxGtkPrinterDCImpl::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1591 {
1592 width--;
1593 height--;
1594
1595 cairo_save (m_cairo);
1596
1597 cairo_new_path(m_cairo);
1598
1599 cairo_translate (m_cairo, XLOG2DEV((wxCoord) (x + width / 2.)), YLOG2DEV((wxCoord) (y + height / 2.)));
1600 cairo_scale(m_cairo, 1, (double)YLOG2DEVREL(height)/(double)XLOG2DEVREL(width));
1601 cairo_arc ( m_cairo, 0, 0, XLOG2DEVREL(width/2), 0, 2 * M_PI);
1602
1603 SetBrush( m_brush );
1604 cairo_fill_preserve( m_cairo );
1605
1606 SetPen (m_pen);
1607 cairo_stroke( m_cairo );
1608
1609 CalcBoundingBox( x, y );
1610 CalcBoundingBox( x + width, y + height );
1611
1612 cairo_restore (m_cairo);
1613 }
1614
1615 #if wxUSE_SPLINES
1616 void wxGtkPrinterDCImpl::DoDrawSpline(const wxPointList *points)
1617 {
1618 SetPen (m_pen);
1619
1620 double c, d, x1, y1, x2, y2, x3, y3;
1621 wxPoint *p, *q;
1622
1623 wxPointList::compatibility_iterator node = points->GetFirst();
1624 p = node->GetData();
1625 x1 = p->x;
1626 y1 = p->y;
1627
1628 node = node->GetNext();
1629 p = node->GetData();
1630 c = p->x;
1631 d = p->y;
1632 x3 =
1633 (double)(x1 + c) / 2;
1634 y3 =
1635 (double)(y1 + d) / 2;
1636
1637 cairo_new_path( m_cairo );
1638 cairo_move_to( m_cairo, XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1) );
1639 cairo_line_to( m_cairo, XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
1640
1641 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1642 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1643
1644 node = node->GetNext();
1645 while (node)
1646 {
1647 q = node->GetData();
1648
1649 x1 = x3;
1650 y1 = y3;
1651 x2 = c;
1652 y2 = d;
1653 c = q->x;
1654 d = q->y;
1655 x3 = (double)(x2 + c) / 2;
1656 y3 = (double)(y2 + d) / 2;
1657
1658 cairo_curve_to(m_cairo,
1659 XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1),
1660 XLOG2DEV((wxCoord)x2), YLOG2DEV((wxCoord)y2),
1661 XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
1662
1663 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1664 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1665
1666 node = node->GetNext();
1667 }
1668
1669 cairo_line_to ( m_cairo, XLOG2DEV((wxCoord)c), YLOG2DEV((wxCoord)d) );
1670
1671 cairo_stroke( m_cairo );
1672 }
1673 #endif // wxUSE_SPLINES
1674
1675 bool wxGtkPrinterDCImpl::DoBlit(wxCoord xdest, wxCoord ydest,
1676 wxCoord width, wxCoord height,
1677 wxDC *source, wxCoord xsrc, wxCoord ysrc,
1678 wxRasterOperationMode rop, bool useMask,
1679 wxCoord WXUNUSED_UNLESS_DEBUG(xsrcMask),
1680 wxCoord WXUNUSED_UNLESS_DEBUG(ysrcMask))
1681 {
1682 wxASSERT_MSG( xsrcMask == wxDefaultCoord && ysrcMask == wxDefaultCoord,
1683 wxT("mask coordinates are not supported") );
1684
1685 wxCHECK_MSG( source, false, wxT("invalid source dc") );
1686
1687 // Blit into a bitmap.
1688 wxBitmap bitmap( width, height );
1689 wxMemoryDC memDC;
1690 memDC.SelectObject(bitmap);
1691 memDC.Blit(0, 0, width, height, source, xsrc, ysrc, rop);
1692 memDC.SelectObject(wxNullBitmap);
1693
1694 // Draw bitmap. scaling and positioning is done there.
1695 GetOwner()->DrawBitmap( bitmap, xdest, ydest, useMask );
1696
1697 return true;
1698 }
1699
1700 void wxGtkPrinterDCImpl::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
1701 {
1702 DoDrawBitmap( icon, x, y, true );
1703 }
1704
1705 void wxGtkPrinterDCImpl::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool useMask )
1706 {
1707 wxCHECK_RET( bitmap.IsOk(), wxT("Invalid bitmap in wxGtkPrinterDCImpl::DoDrawBitmap"));
1708
1709 x = wxCoord(XLOG2DEV(x));
1710 y = wxCoord(YLOG2DEV(y));
1711 int bw = bitmap.GetWidth();
1712 int bh = bitmap.GetHeight();
1713 #ifndef __WXGTK3__
1714 wxBitmap bmpSource = bitmap; // we need a non-const instance.
1715 if (!useMask && !bitmap.HasPixbuf() && bitmap.GetMask())
1716 bmpSource.SetMask(NULL);
1717 #endif
1718
1719 cairo_save(m_cairo);
1720
1721 // Prepare to draw the image.
1722 cairo_translate(m_cairo, x, y);
1723
1724 // Scale the image
1725 wxDouble scaleX = (wxDouble) XLOG2DEVREL(bw) / (wxDouble) bw;
1726 wxDouble scaleY = (wxDouble) YLOG2DEVREL(bh) / (wxDouble) bh;
1727 cairo_scale(m_cairo, scaleX, scaleY);
1728
1729 #ifdef __WXGTK3__
1730 bitmap.Draw(m_cairo, 0, 0, useMask, &m_textForegroundColour, &m_textBackgroundColour);
1731 #else
1732 gdk_cairo_set_source_pixbuf(m_cairo, bmpSource.GetPixbuf(), 0, 0);
1733 cairo_pattern_set_filter(cairo_get_source(m_cairo), CAIRO_FILTER_NEAREST);
1734 // Use the original size here since the context is scaled already.
1735 cairo_rectangle(m_cairo, 0, 0, bw, bh);
1736 // Fill the rectangle using the pattern.
1737 cairo_fill(m_cairo);
1738 #endif
1739
1740 CalcBoundingBox(0,0);
1741 CalcBoundingBox(bw,bh);
1742
1743 cairo_restore(m_cairo);
1744 }
1745
1746 void wxGtkPrinterDCImpl::DoDrawText(const wxString& text, wxCoord x, wxCoord y )
1747 {
1748 DoDrawRotatedText( text, x, y, 0.0 );
1749 }
1750
1751 void wxGtkPrinterDCImpl::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
1752 {
1753 double xx = XLOG2DEV(x);
1754 double yy = YLOG2DEV(y);
1755
1756 angle = -angle;
1757
1758 const wxScopedCharBuffer data = text.utf8_str();
1759
1760 pango_layout_set_text(m_layout, data, data.length());
1761
1762 const bool setAttrs = m_font.GTKSetPangoAttrs(m_layout);
1763 if (m_textForegroundColour.IsOk())
1764 {
1765 unsigned char red = m_textForegroundColour.Red();
1766 unsigned char blue = m_textForegroundColour.Blue();
1767 unsigned char green = m_textForegroundColour.Green();
1768 unsigned char alpha = m_textForegroundColour.Alpha();
1769
1770 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1771 {
1772 double redPS = (double)(red) / 255.0;
1773 double bluePS = (double)(blue) / 255.0;
1774 double greenPS = (double)(green) / 255.0;
1775 double alphaPS = (double)(alpha) / 255.0;
1776
1777 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1778
1779 m_currentRed = red;
1780 m_currentBlue = blue;
1781 m_currentGreen = green;
1782 m_currentAlpha = alpha;
1783 }
1784 }
1785
1786 // Draw layout.
1787 cairo_move_to (m_cairo, xx, yy);
1788
1789 cairo_save( m_cairo );
1790
1791 if (fabs(angle) > 0.00001)
1792 cairo_rotate( m_cairo, angle*DEG2RAD );
1793
1794 cairo_scale(m_cairo, m_scaleX, m_scaleY);
1795
1796 int w,h;
1797 pango_layout_get_pixel_size( m_layout, &w, &h );
1798
1799 if ( m_backgroundMode == wxBRUSHSTYLE_SOLID )
1800 {
1801 unsigned char red = m_textBackgroundColour.Red();
1802 unsigned char blue = m_textBackgroundColour.Blue();
1803 unsigned char green = m_textBackgroundColour.Green();
1804 unsigned char alpha = m_textBackgroundColour.Alpha();
1805
1806 double redPS = (double)(red) / 255.0;
1807 double bluePS = (double)(blue) / 255.0;
1808 double greenPS = (double)(green) / 255.0;
1809 double alphaPS = (double)(alpha) / 255.0;
1810
1811 cairo_save(m_cairo);
1812 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1813 cairo_rectangle(m_cairo, 0, 0, w, h); // still in cairo units
1814 cairo_fill(m_cairo);
1815 cairo_restore(m_cairo);
1816 }
1817
1818 pango_cairo_update_layout (m_cairo, m_layout);
1819 pango_cairo_show_layout (m_cairo, m_layout);
1820
1821 cairo_restore( m_cairo );
1822
1823 if (setAttrs)
1824 {
1825 // Undo underline attributes setting
1826 pango_layout_set_attributes(m_layout, NULL);
1827 }
1828
1829 // Back to device units:
1830 CalcBoundingBox (x, y);
1831 CalcBoundingBox (x + w, y + h);
1832 }
1833
1834 void wxGtkPrinterDCImpl::Clear()
1835 {
1836 // Clear does nothing for printing, but keep the code
1837 // for later reuse
1838 /*
1839 cairo_save(m_cairo);
1840 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
1841 SetBrush(m_backgroundBrush);
1842 cairo_paint(m_cairo);
1843 cairo_restore(m_cairo);
1844 */
1845 }
1846
1847 void wxGtkPrinterDCImpl::SetFont( const wxFont& font )
1848 {
1849 m_font = font;
1850
1851 if (m_font.IsOk())
1852 {
1853 if (m_fontdesc)
1854 pango_font_description_free( m_fontdesc );
1855
1856 m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description );
1857
1858 float size = pango_font_description_get_size( m_fontdesc );
1859 size = size * GetFontPointSizeAdjustment(72.0);
1860 pango_font_description_set_size( m_fontdesc, (gint)size );
1861
1862 pango_layout_set_font_description( m_layout, m_fontdesc );
1863 }
1864 }
1865
1866 void wxGtkPrinterDCImpl::SetPen( const wxPen& pen )
1867 {
1868 if (!pen.IsOk()) return;
1869
1870 m_pen = pen;
1871
1872 double width;
1873
1874 if (m_pen.GetWidth() <= 0)
1875 width = 0.1;
1876 else
1877 width = (double) m_pen.GetWidth();
1878
1879 cairo_set_line_width( m_cairo, width * m_DEV2PS * m_scaleX );
1880 static const double dotted[] = {2.0, 5.0};
1881 static const double short_dashed[] = {4.0, 4.0};
1882 static const double long_dashed[] = {4.0, 8.0};
1883 static const double dotted_dashed[] = {6.0, 6.0, 2.0, 6.0};
1884
1885 switch (m_pen.GetStyle())
1886 {
1887 case wxPENSTYLE_DOT: cairo_set_dash( m_cairo, dotted, 2, 0 ); break;
1888 case wxPENSTYLE_SHORT_DASH: cairo_set_dash( m_cairo, short_dashed, 2, 0 ); break;
1889 case wxPENSTYLE_LONG_DASH: cairo_set_dash( m_cairo, long_dashed, 2, 0 ); break;
1890 case wxPENSTYLE_DOT_DASH: cairo_set_dash( m_cairo, dotted_dashed, 4, 0 ); break;
1891 case wxPENSTYLE_USER_DASH:
1892 {
1893 wxDash *wx_dashes;
1894 int num = m_pen.GetDashes (&wx_dashes);
1895 gdouble *g_dashes = g_new( gdouble, num );
1896 int i;
1897 for (i = 0; i < num; ++i)
1898 g_dashes[i] = (gdouble) wx_dashes[i];
1899 cairo_set_dash( m_cairo, g_dashes, num, 0);
1900 g_free( g_dashes );
1901 }
1902 break;
1903 case wxPENSTYLE_SOLID:
1904 case wxPENSTYLE_TRANSPARENT:
1905 default: cairo_set_dash( m_cairo, NULL, 0, 0 ); break;
1906 }
1907
1908 switch (m_pen.GetCap())
1909 {
1910 case wxCAP_PROJECTING: cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_SQUARE); break;
1911 case wxCAP_BUTT: cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_BUTT); break;
1912 case wxCAP_ROUND:
1913 default: cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_ROUND); break;
1914 }
1915
1916 switch (m_pen.GetJoin())
1917 {
1918 case wxJOIN_BEVEL: cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_BEVEL); break;
1919 case wxJOIN_MITER: cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_MITER); break;
1920 case wxJOIN_ROUND:
1921 default: cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_ROUND); break;
1922 }
1923
1924 unsigned char red = m_pen.GetColour().Red();
1925 unsigned char blue = m_pen.GetColour().Blue();
1926 unsigned char green = m_pen.GetColour().Green();
1927 unsigned char alpha = m_pen.GetColour().Alpha();
1928
1929 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1930 {
1931 double redPS = (double)(red) / 255.0;
1932 double bluePS = (double)(blue) / 255.0;
1933 double greenPS = (double)(green) / 255.0;
1934 double alphaPS = (double)(alpha) / 255.0;
1935
1936 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1937
1938 m_currentRed = red;
1939 m_currentBlue = blue;
1940 m_currentGreen = green;
1941 m_currentAlpha = alpha;
1942 }
1943 }
1944
1945 void wxGtkPrinterDCImpl::SetBrush( const wxBrush& brush )
1946 {
1947 if (!brush.IsOk()) return;
1948
1949 m_brush = brush;
1950
1951 if (m_brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT)
1952 {
1953 cairo_set_source_rgba( m_cairo, 0, 0, 0, 0 );
1954 m_currentRed = 0;
1955 m_currentBlue = 0;
1956 m_currentGreen = 0;
1957 m_currentAlpha = 0;
1958 return;
1959 }
1960
1961 // Brush colour.
1962 unsigned char red = m_brush.GetColour().Red();
1963 unsigned char blue = m_brush.GetColour().Blue();
1964 unsigned char green = m_brush.GetColour().Green();
1965 unsigned char alpha = m_brush.GetColour().Alpha();
1966
1967 double redPS = (double)(red) / 255.0;
1968 double bluePS = (double)(blue) / 255.0;
1969 double greenPS = (double)(green) / 255.0;
1970 double alphaPS = (double)(alpha) / 255.0;
1971
1972 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1973 {
1974 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1975
1976 m_currentRed = red;
1977 m_currentBlue = blue;
1978 m_currentGreen = green;
1979 m_currentAlpha = alpha;
1980 }
1981
1982 if (m_brush.IsHatch())
1983 {
1984 cairo_t * cr;
1985 cairo_surface_t *surface;
1986 surface = cairo_surface_create_similar(cairo_get_target(m_cairo),CAIRO_CONTENT_COLOR_ALPHA,10,10);
1987 cr = cairo_create(surface);
1988 cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
1989 cairo_set_line_width(cr, 1);
1990 cairo_set_line_join(cr,CAIRO_LINE_JOIN_MITER);
1991
1992 switch (m_brush.GetStyle())
1993 {
1994 case wxBRUSHSTYLE_CROSS_HATCH:
1995 cairo_move_to(cr, 5, 0);
1996 cairo_line_to(cr, 5, 10);
1997 cairo_move_to(cr, 0, 5);
1998 cairo_line_to(cr, 10, 5);
1999 break;
2000 case wxBRUSHSTYLE_BDIAGONAL_HATCH:
2001 cairo_move_to(cr, 0, 10);
2002 cairo_line_to(cr, 10, 0);
2003 break;
2004 case wxBRUSHSTYLE_FDIAGONAL_HATCH:
2005 cairo_move_to(cr, 0, 0);
2006 cairo_line_to(cr, 10, 10);
2007 break;
2008 case wxBRUSHSTYLE_CROSSDIAG_HATCH:
2009 cairo_move_to(cr, 0, 0);
2010 cairo_line_to(cr, 10, 10);
2011 cairo_move_to(cr, 10, 0);
2012 cairo_line_to(cr, 0, 10);
2013 break;
2014 case wxBRUSHSTYLE_HORIZONTAL_HATCH:
2015 cairo_move_to(cr, 0, 5);
2016 cairo_line_to(cr, 10, 5);
2017 break;
2018 case wxBRUSHSTYLE_VERTICAL_HATCH:
2019 cairo_move_to(cr, 5, 0);
2020 cairo_line_to(cr, 5, 10);
2021 break;
2022 default:
2023 wxFAIL_MSG(_("Couldn't get hatch style from wxBrush."));
2024 }
2025
2026 cairo_set_source_rgba(cr, redPS, greenPS, bluePS, alphaPS);
2027 cairo_stroke (cr);
2028
2029 cairo_destroy(cr);
2030 cairo_pattern_t * pattern = cairo_pattern_create_for_surface (surface);
2031 cairo_surface_destroy(surface);
2032 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
2033 cairo_set_source(m_cairo, pattern);
2034 cairo_pattern_destroy(pattern);
2035 }
2036 }
2037
2038 void wxGtkPrinterDCImpl::SetLogicalFunction( wxRasterOperationMode function )
2039 {
2040 if (function == wxCLEAR)
2041 cairo_set_operator (m_cairo, CAIRO_OPERATOR_CLEAR);
2042 else if (function == wxOR)
2043 cairo_set_operator (m_cairo, CAIRO_OPERATOR_OUT);
2044 else if (function == wxNO_OP)
2045 cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST);
2046 else if (function == wxAND)
2047 cairo_set_operator (m_cairo, CAIRO_OPERATOR_ADD);
2048 else if (function == wxSET)
2049 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SATURATE);
2050 else if (function == wxXOR)
2051 cairo_set_operator (m_cairo, CAIRO_OPERATOR_XOR);
2052 else // wxCOPY or anything else.
2053 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
2054 }
2055
2056 void wxGtkPrinterDCImpl::SetBackground( const wxBrush& brush )
2057 {
2058 m_backgroundBrush = brush;
2059 cairo_save(m_cairo);
2060 cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST_OVER);
2061
2062 SetBrush(m_backgroundBrush);
2063 cairo_paint(m_cairo);
2064 cairo_restore(m_cairo);
2065 }
2066
2067 void wxGtkPrinterDCImpl::SetBackgroundMode(int mode)
2068 {
2069 if (mode == wxBRUSHSTYLE_SOLID)
2070 m_backgroundMode = wxBRUSHSTYLE_SOLID;
2071 else
2072 m_backgroundMode = wxBRUSHSTYLE_TRANSPARENT;
2073 }
2074
2075 void wxGtkPrinterDCImpl::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
2076 {
2077 cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height));
2078 cairo_clip(m_cairo);
2079
2080 wxDCImpl::DoSetClippingRegion(x, y, width, height);
2081 }
2082
2083 void wxGtkPrinterDCImpl::DestroyClippingRegion()
2084 {
2085 cairo_reset_clip(m_cairo);
2086 }
2087
2088 bool wxGtkPrinterDCImpl::StartDoc(const wxString& WXUNUSED(message))
2089 {
2090 return true;
2091 }
2092
2093 void wxGtkPrinterDCImpl::EndDoc()
2094 {
2095 return;
2096 }
2097
2098 void wxGtkPrinterDCImpl::StartPage()
2099 {
2100 // Notice that we may change the Cairo transformation matrix only here and
2101 // not before (e.g. in wxGtkPrinterDCImpl ctor as we used to do) in order
2102 // to not affect _gtk_print_context_rotate_according_to_orientation() which
2103 // is used in GTK+ itself and wouldn't work correctly if we applied these
2104 // transformations before it is called.
2105
2106 // By default the origin of the Cairo context is in the upper left
2107 // corner of the printable area. We need to translate it so that it
2108 // is in the upper left corner of the paper (without margins)
2109 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
2110 gdouble ml, mt;
2111 ml = gtk_page_setup_get_left_margin (setup, GTK_UNIT_POINTS);
2112 mt = gtk_page_setup_get_top_margin (setup, GTK_UNIT_POINTS);
2113 cairo_translate(m_cairo, -ml, -mt);
2114
2115 #if wxCAIRO_SCALE
2116 cairo_scale( m_cairo, 72.0 / (double)m_resolution, 72.0 / (double)m_resolution );
2117 #endif
2118 }
2119
2120 void wxGtkPrinterDCImpl::EndPage()
2121 {
2122 return;
2123 }
2124
2125 wxCoord wxGtkPrinterDCImpl::GetCharHeight() const
2126 {
2127 pango_layout_set_text( m_layout, "H", 1 );
2128
2129 int w,h;
2130 pango_layout_get_pixel_size( m_layout, &w, &h );
2131
2132 return wxRound( h * m_PS2DEV );
2133 }
2134
2135 wxCoord wxGtkPrinterDCImpl::GetCharWidth() const
2136 {
2137 pango_layout_set_text( m_layout, "H", 1 );
2138
2139 int w,h;
2140 pango_layout_get_pixel_size( m_layout, &w, &h );
2141
2142 return wxRound( w * m_PS2DEV );
2143 }
2144
2145 void wxGtkPrinterDCImpl::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoord *height,
2146 wxCoord *descent,
2147 wxCoord *externalLeading,
2148 const wxFont *theFont ) const
2149 {
2150 if ( width )
2151 *width = 0;
2152 if ( height )
2153 *height = 0;
2154 if ( descent )
2155 *descent = 0;
2156 if ( externalLeading )
2157 *externalLeading = 0;
2158
2159 if (string.empty())
2160 {
2161 return;
2162 }
2163
2164 cairo_save( m_cairo );
2165 cairo_scale(m_cairo, m_scaleX, m_scaleY);
2166
2167 // Set layout's text
2168 const wxScopedCharBuffer dataUTF8 = string.utf8_str();
2169
2170 gint oldSize=0;
2171 if ( theFont )
2172 {
2173 // scale the font and apply it
2174 PangoFontDescription *desc = theFont->GetNativeFontInfo()->description;
2175 oldSize = pango_font_description_get_size(desc);
2176 const float size = oldSize * GetFontPointSizeAdjustment(72.0);
2177 pango_font_description_set_size(desc, (gint)size);
2178
2179 pango_layout_set_font_description(m_layout, desc);
2180 }
2181
2182 pango_layout_set_text( m_layout, dataUTF8, strlen(dataUTF8) );
2183
2184 int h;
2185 pango_layout_get_pixel_size( m_layout, width, &h );
2186 if ( height )
2187 *height = h;
2188
2189 if (descent)
2190 {
2191 PangoLayoutIter *iter = pango_layout_get_iter(m_layout);
2192 int baseline = pango_layout_iter_get_baseline(iter);
2193 pango_layout_iter_free(iter);
2194 *descent = h - PANGO_PIXELS(baseline);
2195 }
2196
2197 if ( theFont )
2198 {
2199 // restore font and reset font's size back
2200 pango_layout_set_font_description(m_layout, m_fontdesc);
2201
2202 PangoFontDescription *desc = theFont->GetNativeFontInfo()->description;
2203 pango_font_description_set_size(desc, oldSize);
2204 }
2205
2206 cairo_restore( m_cairo );
2207 }
2208
2209 void wxGtkPrinterDCImpl::DoGetSize(int* width, int* height) const
2210 {
2211 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
2212
2213 if (width)
2214 *width = wxRound( (double)gtk_page_setup_get_paper_width( setup, GTK_UNIT_POINTS ) * (double)m_resolution / 72.0 );
2215 if (height)
2216 *height = wxRound( (double)gtk_page_setup_get_paper_height( setup, GTK_UNIT_POINTS ) * (double)m_resolution / 72.0 );
2217 }
2218
2219 void wxGtkPrinterDCImpl::DoGetSizeMM(int *width, int *height) const
2220 {
2221 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
2222
2223 if (width)
2224 *width = wxRound( gtk_page_setup_get_paper_width( setup, GTK_UNIT_MM ) );
2225 if (height)
2226 *height = wxRound( gtk_page_setup_get_paper_height( setup, GTK_UNIT_MM ) );
2227 }
2228
2229 wxSize wxGtkPrinterDCImpl::GetPPI() const
2230 {
2231 return wxSize( (int)m_resolution, (int)m_resolution );
2232 }
2233
2234 void wxGtkPrinterDCImpl::SetPrintData(const wxPrintData& data)
2235 {
2236 m_printData = data;
2237 }
2238
2239 // overridden for wxPrinterDC Impl
2240
2241 wxRect wxGtkPrinterDCImpl::GetPaperRect() const
2242 {
2243 // Does GtkPrint support printer margins?
2244 int w = 0;
2245 int h = 0;
2246 DoGetSize( &w, &h );
2247 return wxRect( 0,0,w,h );
2248 }
2249
2250 int wxGtkPrinterDCImpl::GetResolution() const
2251 {
2252 return m_resolution;
2253 }
2254
2255 // ----------------------------------------------------------------------------
2256 // Print preview
2257 // ----------------------------------------------------------------------------
2258
2259 IMPLEMENT_CLASS(wxGtkPrintPreview, wxPrintPreviewBase)
2260
2261 void wxGtkPrintPreview::Init(wxPrintout * WXUNUSED(printout),
2262 wxPrintout * WXUNUSED(printoutForPrinting),
2263 wxPrintData *data)
2264 {
2265 // convert wxPrintQuality to resolution (input pointer can be NULL)
2266 wxPrintQuality quality = data ? data->GetQuality() : wxPRINT_QUALITY_MEDIUM;
2267 switch ( quality )
2268 {
2269 case wxPRINT_QUALITY_HIGH:
2270 m_resolution = 1200;
2271 break;
2272
2273 case wxPRINT_QUALITY_LOW:
2274 m_resolution = 300;
2275 break;
2276
2277 case wxPRINT_QUALITY_DRAFT:
2278 m_resolution = 150;
2279 break;
2280
2281 default:
2282 if ( quality > 0 )
2283 {
2284 // positive values directly indicate print resolution
2285 m_resolution = quality;
2286 break;
2287 }
2288
2289 wxFAIL_MSG( "unknown print quality" );
2290 // fall through
2291
2292 case wxPRINT_QUALITY_MEDIUM:
2293 m_resolution = 600;
2294 break;
2295
2296 }
2297
2298 DetermineScaling();
2299 }
2300
2301 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout,
2302 wxPrintout *printoutForPrinting,
2303 wxPrintDialogData *data)
2304 : wxPrintPreviewBase(printout, printoutForPrinting, data)
2305 {
2306 Init(printout, printoutForPrinting, data ? &data->GetPrintData() : NULL);
2307 }
2308
2309 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout,
2310 wxPrintout *printoutForPrinting,
2311 wxPrintData *data)
2312 : wxPrintPreviewBase(printout, printoutForPrinting, data)
2313 {
2314 Init(printout, printoutForPrinting, data);
2315 }
2316
2317 wxGtkPrintPreview::~wxGtkPrintPreview()
2318 {
2319 }
2320
2321 bool wxGtkPrintPreview::Print(bool interactive)
2322 {
2323 if (!m_printPrintout)
2324 return false;
2325
2326 wxPrinter printer(& m_printDialogData);
2327 return printer.Print(m_previewFrame, m_printPrintout, interactive);
2328 }
2329
2330 void wxGtkPrintPreview::DetermineScaling()
2331 {
2332 wxPaperSize paperType = m_printDialogData.GetPrintData().GetPaperId();
2333
2334 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
2335 if (!paper)
2336 paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
2337
2338 if (paper)
2339 {
2340 const wxSize screenPPI = wxGetDisplayPPI();
2341 int logPPIScreenX = screenPPI.GetWidth();
2342 int logPPIScreenY = screenPPI.GetHeight();
2343 int logPPIPrinterX = m_resolution;
2344 int logPPIPrinterY = m_resolution;
2345
2346 m_previewPrintout->SetPPIScreen( logPPIScreenX, logPPIScreenY );
2347 m_previewPrintout->SetPPIPrinter( logPPIPrinterX, logPPIPrinterY );
2348
2349 // Get width and height in points (1/72th of an inch)
2350 wxSize sizeDevUnits(paper->GetSizeDeviceUnits());
2351 sizeDevUnits.x = wxRound((double)sizeDevUnits.x * (double)m_resolution / 72.0);
2352 sizeDevUnits.y = wxRound((double)sizeDevUnits.y * (double)m_resolution / 72.0);
2353
2354 wxSize sizeTenthsMM(paper->GetSize());
2355 wxSize sizeMM(sizeTenthsMM.x / 10, sizeTenthsMM.y / 10);
2356
2357 // If in landscape mode, we need to swap the width and height.
2358 if ( m_printDialogData.GetPrintData().GetOrientation() == wxLANDSCAPE )
2359 {
2360 m_pageWidth = sizeDevUnits.y;
2361 m_pageHeight = sizeDevUnits.x;
2362 m_previewPrintout->SetPageSizeMM(sizeMM.y, sizeMM.x);
2363 }
2364 else
2365 {
2366 m_pageWidth = sizeDevUnits.x;
2367 m_pageHeight = sizeDevUnits.y;
2368 m_previewPrintout->SetPageSizeMM(sizeMM.x, sizeMM.y);
2369 }
2370 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
2371 m_previewPrintout->SetPaperRectPixels(wxRect(0, 0, m_pageWidth, m_pageHeight));
2372
2373 // At 100%, the page should look about page-size on the screen.
2374 m_previewScaleX = float(logPPIScreenX) / logPPIPrinterX;
2375 m_previewScaleY = float(logPPIScreenY) / logPPIPrinterY;
2376 }
2377 }
2378
2379 #endif
2380 // wxUSE_GTKPRINT