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