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