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