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