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