Use cairo_scale() for scaling to full printing resolution, correct transformation...
[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 wxSize ScreenPixels = wxGetDisplaySize();
930 wxSize ScreenMM = wxGetDisplaySizeMM();
931
932 printout->SetPPIScreen( (int) ((ScreenPixels.GetWidth() * 25.4) / ScreenMM.GetWidth()),
933 (int) ((ScreenPixels.GetHeight() * 25.4) / ScreenMM.GetHeight()) );
934 printout->SetPPIPrinter( printDC->GetResolution(),
935 printDC->GetResolution() );
936
937 printout->SetDC(m_dc);
938
939 int w, h;
940 m_dc->GetSize(&w, &h);
941 printout->SetPageSizePixels((int)w, (int)h);
942 printout->SetPaperRectPixels(wxRect(0, 0, w, h));
943 int mw, mh;
944 m_dc->GetSizeMM(&mw, &mh);
945 printout->SetPageSizeMM((int)mw, (int)mh);
946 printout->OnPreparePrinting();
947
948 // Get some parameters from the printout, if defined.
949 int fromPage, toPage;
950 int minPage, maxPage;
951 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
952
953 if (maxPage == 0)
954 {
955 sm_lastError = wxPRINTER_ERROR;
956 wxFAIL_MSG(_("wxPrintout::GetPageInfo gives a null maxPage."));
957 return;
958 }
959
960 printout->OnBeginPrinting();
961
962 int numPages = 0;
963
964 // If we're not previewing we need to calculate the number of pages to print.
965 // If we're previewing, Gtk Print will render every pages without wondering about the page ranges the user may
966 // have defined in the dialog. So the number of pages is the maximum available.
967 if (!printout->IsPreview())
968 {
969 GtkPrintSettings * settings = gtk_print_operation_get_print_settings (operation);
970 switch (gtk_print_settings_get_print_pages(settings))
971 {
972 case GTK_PRINT_PAGES_CURRENT:
973 numPages = 1;
974 break;
975 case GTK_PRINT_PAGES_RANGES:
976 {gint num_ranges = 0;
977 GtkPageRange* range;
978 int i;
979 range = gtk_print_settings_get_page_ranges (settings, &num_ranges);
980 for (i=0; i<num_ranges; i++)
981 {
982 if (range[i].end < range[i].start) range[i].end = range[i].start;
983 if (range[i].start < minPage-1) range[i].start = minPage-1;
984 if (range[i].end > maxPage-1) range[i].end = maxPage-1;
985 if (range[i].start > maxPage-1) range[i].start = maxPage-1;
986 numPages += range[i].end - range[i].start + 1;
987 }
988 gtk_print_settings_set_page_ranges (settings, range, 1);
989 break;}
990 case GTK_PRINT_PAGES_ALL:
991 default:
992 numPages = maxPage - minPage + 1;
993 break;
994 }
995 }
996 else numPages = maxPage - minPage + 1;
997
998 gtk_print_operation_set_n_pages(operation, numPages);
999 }
1000
1001 void wxGtkPrinter::DrawPage(wxPrintout *printout,
1002 GtkPrintOperation *operation,
1003 GtkPrintContext * WXUNUSED(context),
1004 int page_nr)
1005 {
1006 int fromPage, toPage, minPage, maxPage, startPage, endPage;
1007 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
1008
1009 int numPageToDraw = page_nr + minPage;
1010 if (numPageToDraw < minPage) numPageToDraw = minPage;
1011 if (numPageToDraw > maxPage) numPageToDraw = maxPage;
1012
1013 GtkPrintSettings * settings = gtk_print_operation_get_print_settings (operation);
1014 switch (gtk_print_settings_get_print_pages(settings))
1015 {
1016 case GTK_PRINT_PAGES_CURRENT:
1017 g_object_get_property((GObject*) operation, (const gchar *) "current-page", (GValue*) &startPage);
1018 g_object_get_property((GObject*) operation, (const gchar *) "current-page", (GValue*) &endPage);
1019 break;
1020 case GTK_PRINT_PAGES_RANGES:
1021 {gint num_ranges = 0;
1022 GtkPageRange* range;
1023 range = gtk_print_settings_get_page_ranges (settings, &num_ranges);
1024 // We don't need to verify these values as it has already been done in wxGtkPrinter::BeginPrint.
1025 if (num_ranges >= 1)
1026 {
1027 startPage = range[0].start + 1;
1028 endPage = range[0].end + 1;
1029 }
1030 else {
1031 startPage = minPage;
1032 endPage = maxPage;
1033 }
1034 break;}
1035 case GTK_PRINT_PAGES_ALL:
1036 default:
1037 startPage = minPage;
1038 endPage = maxPage;
1039 break;
1040 }
1041
1042 if(numPageToDraw == startPage)
1043 {
1044 if (!printout->OnBeginDocument(startPage, endPage))
1045 {
1046 wxLogError(_("Could not start printing."));
1047 sm_lastError = wxPRINTER_ERROR;
1048 }
1049 }
1050
1051 // The app can render the page numPageToDraw.
1052 if (printout->HasPage(numPageToDraw))
1053 {
1054 m_dc->StartPage();
1055 printout->OnPrintPage(numPageToDraw);
1056 m_dc->EndPage();
1057 }
1058
1059
1060 if(numPageToDraw == endPage)
1061 {
1062 printout->OnEndDocument();
1063 }
1064 }
1065
1066 wxDC* wxGtkPrinter::PrintDialog( wxWindow *parent )
1067 {
1068 wxGtkPrintDialog dialog( parent, &m_printDialogData );
1069
1070 dialog.SetPrintDC(m_dc);
1071 dialog.SetShowDialog(true);
1072
1073 int ret = dialog.ShowModal();
1074
1075 if (ret == wxID_CANCEL)
1076 {
1077 sm_lastError = wxPRINTER_CANCELLED;
1078 return NULL;
1079 }
1080 if (ret == wxID_NO)
1081 {
1082 sm_lastError = wxPRINTER_ERROR;
1083 wxFAIL_MSG(_("The print dialog returned an error."));
1084 return NULL;
1085 }
1086
1087 m_printDialogData = dialog.GetPrintDialogData();
1088
1089 return new wxPrinterDC( m_printDialogData.GetPrintData() );
1090 }
1091
1092 bool wxGtkPrinter::Setup( wxWindow * WXUNUSED(parent) )
1093 {
1094 // Obsolete, for backward compatibility.
1095 return false;
1096 }
1097
1098 //-----------------------------------------------------------------------------
1099 // wxGtkPrinterDC
1100 //-----------------------------------------------------------------------------
1101
1102 #define wxCAIRO_SCALE 1
1103
1104 #if wxCAIRO_SCALE
1105
1106 #define XLOG2DEV(x) LogicalToDeviceX(x)
1107 #define XLOG2DEVREL(x) LogicalToDeviceXRel(x)
1108 #define YLOG2DEV(x) LogicalToDeviceY(x)
1109 #define YLOG2DEVREL(x) LogicalToDeviceYRel(x)
1110
1111 #else
1112
1113 #define XLOG2DEV(x) ((double)(LogicalToDeviceX(x)) * m_DEV2PS)
1114 #define XLOG2DEVREL(x) ((double)(LogicalToDeviceXRel(x)) * m_DEV2PS)
1115 #define YLOG2DEV(x) ((double)(LogicalToDeviceY(x)) * m_DEV2PS)
1116 #define YLOG2DEVREL(x) ((double)(LogicalToDeviceYRel(x)) * m_DEV2PS)
1117
1118 #endif
1119
1120 IMPLEMENT_ABSTRACT_CLASS(wxGtkPrinterDCImpl, wxDCImpl)
1121
1122 wxGtkPrinterDCImpl::wxGtkPrinterDCImpl(wxPrinterDC *owner, const wxPrintData& data)
1123 : wxDCImpl( owner )
1124 {
1125 m_printData = data;
1126
1127 wxGtkPrintNativeData *native =
1128 (wxGtkPrintNativeData*) m_printData.GetNativeData();
1129
1130 m_gpc = native->GetPrintContext();
1131
1132 // Match print quality to resolution (high = 1200dpi)
1133 m_resolution = m_printData.GetQuality(); // (int) gtk_print_context_get_dpi_x( m_gpc );
1134 if (m_resolution < 0)
1135 m_resolution = (1 << (m_resolution+4)) *150;
1136
1137 m_context = gtk_print_context_create_pango_context( m_gpc );
1138 m_layout = gtk_print_context_create_pango_layout ( m_gpc );
1139 m_fontdesc = pango_font_description_from_string( "Sans 12" );
1140
1141 m_cairo = gtk_print_context_get_cairo_context ( m_gpc );
1142
1143 #if wxCAIRO_SCALE
1144 m_PS2DEV = 1.0;
1145 m_DEV2PS = 1.0;
1146
1147 gs_cairo->cairo_scale( m_cairo, 72.0 / (double)m_resolution, 72.0 / (double)m_resolution );
1148 #else
1149 m_PS2DEV = (double)m_resolution / 72.0;
1150 m_DEV2PS = 72.0 / (double)m_resolution;
1151 #endif
1152
1153 m_currentRed = 0;
1154 m_currentBlue = 0;
1155 m_currentGreen = 0;
1156
1157 m_signX = 1; // default x-axis left to right.
1158 m_signY = 1; // default y-axis bottom up -> top down.
1159
1160 // By default the origin of the Cairo context is in the upper left
1161 // corner of the printable area. We need to translate it so that it
1162 // is in the upper left corner of the paper (without margins)
1163 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
1164 gdouble ml, mt;
1165 ml = gtk_page_setup_get_left_margin (setup, GTK_UNIT_POINTS);
1166 mt = gtk_page_setup_get_top_margin (setup, GTK_UNIT_POINTS);
1167 gs_cairo->cairo_translate(m_cairo, -ml, -mt);
1168 }
1169
1170 wxGtkPrinterDCImpl::~wxGtkPrinterDCImpl()
1171 {
1172 g_object_unref(m_context);
1173 g_object_unref(m_layout);
1174 }
1175
1176 bool wxGtkPrinterDCImpl::IsOk() const
1177 {
1178 return m_gpc != NULL;
1179 }
1180
1181 void* wxGtkPrinterDCImpl::GetCairoContext() const
1182 {
1183 return (void*) cairo_reference( m_cairo );
1184 }
1185
1186 bool wxGtkPrinterDCImpl::DoFloodFill(wxCoord WXUNUSED(x1),
1187 wxCoord WXUNUSED(y1),
1188 const wxColour& WXUNUSED(col),
1189 int WXUNUSED(style))
1190 {
1191 // We can't access the given coord as a Cairo context is scalable, ie a
1192 // coord doesn't mean anything in this context.
1193 wxFAIL_MSG(_("not implemented"));
1194 return false;
1195 }
1196
1197 void wxGtkPrinterDCImpl::DoGradientFillConcentric(const wxRect& rect, const wxColour& initialColour, const wxColour& destColour, const wxPoint& circleCenter)
1198 {
1199 wxCoord xC = circleCenter.x;
1200 wxCoord yC = circleCenter.y;
1201 wxCoord xR = rect.x;
1202 wxCoord yR = rect.y;
1203 wxCoord w = rect.width;
1204 wxCoord h = rect.height;
1205
1206 double radius = sqrt((w/2)*(w/2)+(h/2)*(h/2));
1207
1208 unsigned char redI = initialColour.Red();
1209 unsigned char blueI = initialColour.Blue();
1210 unsigned char greenI = initialColour.Green();
1211 unsigned char alphaI = initialColour.Alpha();
1212 unsigned char redD = destColour.Red();
1213 unsigned char blueD = destColour.Blue();
1214 unsigned char greenD = destColour.Green();
1215 unsigned char alphaD = destColour.Alpha();
1216
1217 double redIPS = (double)(redI) / 255.0;
1218 double blueIPS = (double)(blueI) / 255.0;
1219 double greenIPS = (double)(greenI) / 255.0;
1220 double alphaIPS = (double)(alphaI) / 255.0;
1221 double redDPS = (double)(redD) / 255.0;
1222 double blueDPS = (double)(blueD) / 255.0;
1223 double greenDPS = (double)(greenD) / 255.0;
1224 double alphaDPS = (double)(alphaD) / 255.0;
1225
1226 // Create a pattern with the gradient.
1227 cairo_pattern_t* gradient;
1228 gradient = gs_cairo->cairo_pattern_create_radial (XLOG2DEV(xC+xR), YLOG2DEV(yC+yR), 0, XLOG2DEV(xC+xR), YLOG2DEV(yC+yR), radius * m_DEV2PS );
1229 gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 0.0, redIPS, greenIPS, blueIPS, alphaIPS);
1230 gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 1.0, redDPS, greenDPS, blueDPS, alphaDPS);
1231
1232 // Fill the rectangle with this pattern.
1233 gs_cairo->cairo_set_source(m_cairo, gradient);
1234 gs_cairo->cairo_rectangle (m_cairo, XLOG2DEV(xR), YLOG2DEV(yR), XLOG2DEVREL(w), YLOG2DEVREL(h) );
1235 gs_cairo->cairo_fill(m_cairo);
1236
1237 gs_cairo->cairo_pattern_destroy(gradient);
1238
1239 CalcBoundingBox(xR, yR);
1240 CalcBoundingBox(xR+w, yR+h);
1241 }
1242
1243 void wxGtkPrinterDCImpl::DoGradientFillLinear(const wxRect& rect, const wxColour& initialColour, const wxColour& destColour, wxDirection nDirection)
1244 {
1245 wxCoord x = rect.x;
1246 wxCoord y = rect.y;
1247 wxCoord w = rect.width;
1248 wxCoord h = rect.height;
1249
1250 unsigned char redI = initialColour.Red();
1251 unsigned char blueI = initialColour.Blue();
1252 unsigned char greenI = initialColour.Green();
1253 unsigned char alphaI = initialColour.Alpha();
1254 unsigned char redD = destColour.Red();
1255 unsigned char blueD = destColour.Blue();
1256 unsigned char greenD = destColour.Green();
1257 unsigned char alphaD = destColour.Alpha();
1258
1259 double redIPS = (double)(redI) / 255.0;
1260 double blueIPS = (double)(blueI) / 255.0;
1261 double greenIPS = (double)(greenI) / 255.0;
1262 double alphaIPS = (double)(alphaI) / 255.0;
1263 double redDPS = (double)(redD) / 255.0;
1264 double blueDPS = (double)(blueD) / 255.0;
1265 double greenDPS = (double)(greenD) / 255.0;
1266 double alphaDPS = (double)(alphaD) / 255.0;
1267
1268 // Create a pattern with the gradient.
1269 cairo_pattern_t* gradient;
1270 gradient = gs_cairo->cairo_pattern_create_linear (XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x+w), YLOG2DEV(y));
1271
1272 if (nDirection == wxWEST)
1273 {
1274 gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 0.0, redDPS, greenDPS, blueDPS, alphaDPS);
1275 gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 1.0, redIPS, greenIPS, blueIPS, alphaIPS);
1276 }
1277 else {
1278 gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 0.0, redIPS, greenIPS, blueIPS, alphaIPS);
1279 gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 1.0, redDPS, greenDPS, blueDPS, alphaDPS);
1280 }
1281
1282 // Fill the rectangle with this pattern.
1283 gs_cairo->cairo_set_source(m_cairo, gradient);
1284 gs_cairo->cairo_rectangle (m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(w), YLOG2DEVREL(h) );
1285 gs_cairo->cairo_fill(m_cairo);
1286
1287 gs_cairo->cairo_pattern_destroy(gradient);
1288
1289 CalcBoundingBox(x, y);
1290 CalcBoundingBox(x+w, y+h);
1291 }
1292
1293 bool wxGtkPrinterDCImpl::DoGetPixel(wxCoord WXUNUSED(x1),
1294 wxCoord WXUNUSED(y1),
1295 wxColour * WXUNUSED(col)) const
1296 {
1297 wxFAIL_MSG(_("not implemented"));
1298 return false;
1299 }
1300
1301 void wxGtkPrinterDCImpl::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
1302 {
1303 if (m_pen.GetStyle() == wxPENSTYLE_TRANSPARENT) return;
1304
1305 SetPen( m_pen );
1306 gs_cairo->cairo_move_to ( m_cairo, XLOG2DEV(x1), YLOG2DEV(y1) );
1307 gs_cairo->cairo_line_to ( m_cairo, XLOG2DEV(x2), YLOG2DEV(y2) );
1308 gs_cairo->cairo_stroke ( m_cairo );
1309
1310 CalcBoundingBox( x1, y1 );
1311 CalcBoundingBox( x2, y2 );
1312 }
1313
1314 void wxGtkPrinterDCImpl::DoCrossHair(wxCoord x, wxCoord y)
1315 {
1316 int w, h;
1317 DoGetSize(&w, &h);
1318
1319 SetPen(m_pen);
1320
1321 gs_cairo->cairo_move_to (m_cairo, XLOG2DEV(x), 0);
1322 gs_cairo->cairo_line_to (m_cairo, XLOG2DEV(x), YLOG2DEVREL(h));
1323 gs_cairo->cairo_move_to (m_cairo, 0, YLOG2DEV(y));
1324 gs_cairo->cairo_line_to (m_cairo, XLOG2DEVREL(w), YLOG2DEV(y));
1325
1326 gs_cairo->cairo_stroke (m_cairo);
1327 CalcBoundingBox( 0, 0 );
1328 CalcBoundingBox( w, h );
1329 }
1330
1331 void wxGtkPrinterDCImpl::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord xc,wxCoord yc)
1332 {
1333 double dx = x1 - xc;
1334 double dy = y1 - yc;
1335 double radius = sqrt((double)(dx*dx+dy*dy));
1336
1337 double alpha1, alpha2;
1338 if (x1 == x2 && y1 == y2)
1339 {
1340 alpha1 = 0.0;
1341 alpha2 = 360.0;
1342 }
1343 else
1344 if (radius == 0.0)
1345 {
1346 alpha1 = alpha2 = 0.0;
1347 }
1348 else
1349 {
1350 alpha1 = (x1 - xc == 0) ?
1351 (y1 - yc < 0) ? 90.0 : -90.0 :
1352 atan2(double(y1-yc), double(x1-xc)) * RAD2DEG;
1353 alpha2 = (x2 - xc == 0) ?
1354 (y2 - yc < 0) ? 90.0 : -90.0 :
1355 atan2(double(y2-yc), double(x2-xc)) * RAD2DEG;
1356
1357 while (alpha1 <= 0) alpha1 += 360;
1358 while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between.
1359 while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree.
1360 while (alpha2 > 360) alpha2 -= 360;
1361 }
1362
1363 alpha1 *= DEG2RAD;
1364 alpha2 *= DEG2RAD;
1365
1366 gs_cairo->cairo_new_path(m_cairo);
1367
1368 gs_cairo->cairo_arc_negative ( m_cairo, XLOG2DEV(xc), YLOG2DEV(yc), XLOG2DEVREL((int)radius), alpha1, alpha2);
1369 gs_cairo->cairo_line_to(m_cairo, XLOG2DEV(xc), YLOG2DEV(yc));
1370 gs_cairo->cairo_close_path (m_cairo);
1371
1372 SetBrush( m_brush );
1373 gs_cairo->cairo_fill_preserve( m_cairo );
1374
1375 SetPen (m_pen);
1376 gs_cairo->cairo_stroke( m_cairo );
1377
1378 CalcBoundingBox (x1, y1);
1379 CalcBoundingBox (xc, yc);
1380 CalcBoundingBox (x2, y2);
1381 }
1382
1383 void wxGtkPrinterDCImpl::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
1384 {
1385 gs_cairo->cairo_save( m_cairo );
1386
1387 gs_cairo->cairo_new_path(m_cairo);
1388
1389 gs_cairo->cairo_translate( m_cairo, XLOG2DEV((wxCoord) (x + w / 2.)), XLOG2DEV((wxCoord) (y + h / 2.)) );
1390 double scale = (double)YLOG2DEVREL(h) / (double) XLOG2DEVREL(w);
1391 gs_cairo->cairo_scale( m_cairo, 1.0, scale );
1392
1393 gs_cairo->cairo_arc_negative ( m_cairo, 0, 0, XLOG2DEVREL(w/2), -sa*DEG2RAD, -ea*DEG2RAD);
1394
1395 SetPen (m_pen);
1396 gs_cairo->cairo_stroke_preserve( m_cairo );
1397
1398 gs_cairo->cairo_line_to(m_cairo, 0,0);
1399
1400 SetBrush( m_brush );
1401 gs_cairo->cairo_fill( m_cairo );
1402
1403 gs_cairo->cairo_restore( m_cairo );
1404
1405 CalcBoundingBox( x, y);
1406 CalcBoundingBox( x+w, y+h );
1407 }
1408
1409 void wxGtkPrinterDCImpl::DoDrawPoint(wxCoord x, wxCoord y)
1410 {
1411 if (m_pen.GetStyle() == wxPENSTYLE_TRANSPARENT) return;
1412
1413 SetPen( m_pen );
1414
1415 gs_cairo->cairo_move_to ( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1416 gs_cairo->cairo_line_to ( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1417 gs_cairo->cairo_stroke ( m_cairo );
1418
1419 CalcBoundingBox( x, y );
1420 }
1421
1422 void wxGtkPrinterDCImpl::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
1423 {
1424 if (m_pen.GetStyle() == wxPENSTYLE_TRANSPARENT) return;
1425
1426 if (n <= 0) return;
1427
1428 SetPen (m_pen);
1429
1430 int i;
1431 for ( i =0; i<n ; i++ )
1432 CalcBoundingBox( points[i].x+xoffset, points[i].y+yoffset);
1433
1434 gs_cairo->cairo_move_to ( m_cairo, XLOG2DEV(points[0].x+xoffset), YLOG2DEV(points[0].y+yoffset) );
1435
1436 for (i = 1; i < n; i++)
1437 gs_cairo->cairo_line_to ( m_cairo, XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset) );
1438
1439 gs_cairo->cairo_stroke ( m_cairo);
1440 }
1441
1442 void wxGtkPrinterDCImpl::DoDrawPolygon(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
1443 {
1444 if (n==0) return;
1445
1446 gs_cairo->cairo_save(m_cairo);
1447 if (fillStyle == wxWINDING_RULE)
1448 gs_cairo->cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_WINDING);
1449 else
1450 gs_cairo->cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_EVEN_ODD);
1451
1452 int x = points[0].x + xoffset;
1453 int y = points[0].y + yoffset;
1454 gs_cairo->cairo_new_path(m_cairo);
1455 gs_cairo->cairo_move_to( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1456 int i;
1457 for (i = 1; i < n; i++)
1458 {
1459 int x = points[i].x + xoffset;
1460 int y = points[i].y + yoffset;
1461 gs_cairo->cairo_line_to( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1462 }
1463 gs_cairo->cairo_close_path(m_cairo);
1464
1465 SetBrush( m_brush );
1466 gs_cairo->cairo_fill_preserve( m_cairo );
1467
1468 SetPen (m_pen);
1469 gs_cairo->cairo_stroke( m_cairo );
1470
1471 CalcBoundingBox( x, y );
1472
1473 gs_cairo->cairo_restore(m_cairo);
1474 }
1475
1476 void wxGtkPrinterDCImpl::DoDrawPolyPolygon(int n, int count[], wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
1477 {
1478 wxDCImpl::DoDrawPolyPolygon( n, count, points, xoffset, yoffset, fillStyle );
1479 }
1480
1481 void wxGtkPrinterDCImpl::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1482 {
1483 width--;
1484 height--;
1485
1486 gs_cairo->cairo_new_path(m_cairo);
1487 gs_cairo->cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height));
1488
1489 SetBrush( m_brush );
1490 gs_cairo->cairo_fill_preserve( m_cairo );
1491
1492 SetPen (m_pen);
1493 gs_cairo->cairo_stroke( m_cairo );
1494
1495 CalcBoundingBox( x, y );
1496 CalcBoundingBox( x + width, y + height );
1497 }
1498
1499 void wxGtkPrinterDCImpl::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
1500 {
1501 width--;
1502 height--;
1503
1504 if (radius < 0.0) radius = - radius * ((width < height) ? width : height);
1505
1506 wxCoord dd = 2 * (wxCoord) radius;
1507 if (dd > width) dd = width;
1508 if (dd > height) dd = height;
1509 radius = dd / 2;
1510
1511 wxCoord rad = (wxCoord) radius;
1512
1513 gs_cairo->cairo_new_path(m_cairo);
1514 gs_cairo->cairo_move_to(m_cairo,XLOG2DEV(x + rad),YLOG2DEV(y));
1515 gs_cairo->cairo_curve_to(m_cairo,
1516 XLOG2DEV(x + rad),YLOG2DEV(y),
1517 XLOG2DEV(x),YLOG2DEV(y),
1518 XLOG2DEV(x),YLOG2DEV(y + rad));
1519 gs_cairo->cairo_line_to(m_cairo,XLOG2DEV(x),YLOG2DEV(y + height - rad));
1520 gs_cairo->cairo_curve_to(m_cairo,
1521 XLOG2DEV(x),YLOG2DEV(y + height - rad),
1522 XLOG2DEV(x),YLOG2DEV(y + height),
1523 XLOG2DEV(x + rad),YLOG2DEV(y + height));
1524 gs_cairo->cairo_line_to(m_cairo,XLOG2DEV(x + width - rad),YLOG2DEV(y + height));
1525 gs_cairo->cairo_curve_to(m_cairo,
1526 XLOG2DEV(x + width - rad),YLOG2DEV(y + height),
1527 XLOG2DEV(x + width),YLOG2DEV(y + height),
1528 XLOG2DEV(x + width),YLOG2DEV(y + height - rad));
1529 gs_cairo->cairo_line_to(m_cairo,XLOG2DEV(x + width),YLOG2DEV(y + rad));
1530 gs_cairo->cairo_curve_to(m_cairo,
1531 XLOG2DEV(x + width),YLOG2DEV(y + rad),
1532 XLOG2DEV(x + width),YLOG2DEV(y),
1533 XLOG2DEV(x + width - rad),YLOG2DEV(y));
1534 gs_cairo->cairo_line_to(m_cairo,XLOG2DEV(x + rad),YLOG2DEV(y));
1535 gs_cairo->cairo_close_path(m_cairo);
1536
1537 SetBrush(m_brush);
1538 gs_cairo->cairo_fill_preserve(m_cairo);
1539
1540 SetPen(m_pen);
1541 gs_cairo->cairo_stroke(m_cairo);
1542
1543 CalcBoundingBox(x,y);
1544 CalcBoundingBox(x+width,y+height);
1545 }
1546
1547 void wxGtkPrinterDCImpl::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1548 {
1549 width--;
1550 height--;
1551
1552 gs_cairo->cairo_save (m_cairo);
1553
1554 gs_cairo->cairo_new_path(m_cairo);
1555
1556 gs_cairo->cairo_translate (m_cairo, XLOG2DEV((wxCoord) (x + width / 2.)), YLOG2DEV((wxCoord) (y + height / 2.)));
1557 gs_cairo->cairo_scale(m_cairo, 1, (double)YLOG2DEVREL(height)/(double)XLOG2DEVREL(width));
1558 gs_cairo->cairo_arc ( m_cairo, 0, 0, XLOG2DEVREL(width/2), 0, 2 * M_PI);
1559
1560 SetBrush( m_brush );
1561 gs_cairo->cairo_fill_preserve( m_cairo );
1562
1563 SetPen (m_pen);
1564 gs_cairo->cairo_stroke( m_cairo );
1565
1566 CalcBoundingBox( x, y );
1567 CalcBoundingBox( x + width, y + height );
1568
1569 gs_cairo->cairo_restore (m_cairo);
1570 }
1571
1572 #if wxUSE_SPLINES
1573 void wxGtkPrinterDCImpl::DoDrawSpline(const wxPointList *points)
1574 {
1575 SetPen (m_pen);
1576
1577 double c, d, x1, y1, x2, y2, x3, y3;
1578 wxPoint *p, *q;
1579
1580 wxPointList::compatibility_iterator node = points->GetFirst();
1581 p = node->GetData();
1582 x1 = p->x;
1583 y1 = p->y;
1584
1585 node = node->GetNext();
1586 p = node->GetData();
1587 c = p->x;
1588 d = p->y;
1589 x3 =
1590 (double)(x1 + c) / 2;
1591 y3 =
1592 (double)(y1 + d) / 2;
1593
1594 gs_cairo->cairo_new_path( m_cairo );
1595 gs_cairo->cairo_move_to( m_cairo, XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1) );
1596 gs_cairo->cairo_line_to( m_cairo, XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
1597
1598 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1599 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1600
1601 node = node->GetNext();
1602 while (node)
1603 {
1604 q = node->GetData();
1605
1606 x1 = x3;
1607 y1 = y3;
1608 x2 = c;
1609 y2 = d;
1610 c = q->x;
1611 d = q->y;
1612 x3 = (double)(x2 + c) / 2;
1613 y3 = (double)(y2 + d) / 2;
1614
1615 gs_cairo->cairo_curve_to(m_cairo,
1616 XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1),
1617 XLOG2DEV((wxCoord)x2), YLOG2DEV((wxCoord)y2),
1618 XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
1619
1620 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1621 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1622
1623 node = node->GetNext();
1624 }
1625
1626 gs_cairo->cairo_line_to ( m_cairo, XLOG2DEV((wxCoord)c), YLOG2DEV((wxCoord)d) );
1627
1628 gs_cairo->cairo_stroke( m_cairo );
1629 }
1630 #endif // wxUSE_SPLINES
1631
1632 bool wxGtkPrinterDCImpl::DoBlit(wxCoord xdest, wxCoord ydest,
1633 wxCoord width, wxCoord height,
1634 wxDC *source, wxCoord xsrc, wxCoord ysrc,
1635 int rop, bool useMask,
1636 wxCoord WXUNUSED_UNLESS_DEBUG(xsrcMask),
1637 wxCoord WXUNUSED_UNLESS_DEBUG(ysrcMask))
1638 {
1639 wxASSERT_MSG( xsrcMask == wxDefaultCoord && ysrcMask == wxDefaultCoord,
1640 wxT("mask coordinates are not supported") );
1641
1642 wxCHECK_MSG( source, false, wxT("invalid source dc") );
1643
1644 // Blit into a bitmap.
1645 wxBitmap bitmap( width, height );
1646 wxMemoryDC memDC;
1647 memDC.SelectObject(bitmap);
1648 memDC.Blit(0, 0, width, height, source, xsrc, ysrc, rop);
1649 memDC.SelectObject(wxNullBitmap);
1650
1651 // Draw bitmap. scaling and positioning is done there.
1652 GetOwner()->DrawBitmap( bitmap, xdest, ydest, useMask );
1653
1654 return true;
1655 }
1656
1657 void wxGtkPrinterDCImpl::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
1658 {
1659 DoDrawBitmap( icon, x, y, true );
1660 }
1661
1662 void wxGtkPrinterDCImpl::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool useMask )
1663 {
1664 wxCHECK_RET( bitmap.IsOk(), wxT("Invalid bitmap in wxGtkPrinterDCImpl::DoDrawBitmap"));
1665
1666 cairo_surface_t* surface;
1667 x = wxCoord(XLOG2DEV(x));
1668 y = wxCoord(YLOG2DEV(y));
1669 int bw = bitmap.GetWidth();
1670 int bh = bitmap.GetHeight();
1671 wxBitmap bmpSource = bitmap; // we need a non-const instance.
1672 unsigned char* buffer = new unsigned char[bw*bh*4];
1673 wxUint32* data = (wxUint32*)buffer;
1674
1675 wxMask *mask = NULL;
1676 if (useMask) mask = bmpSource.GetMask();
1677
1678 // Create a surface object and copy the bitmap pixel data to it. If the image has alpha (or a mask represented as alpha)
1679 // then we'll use a different format and iterator than if it doesn't.
1680 if (bmpSource.HasAlpha() || mask)
1681 {
1682 surface = gs_cairo->cairo_image_surface_create_for_data(
1683 buffer, CAIRO_FORMAT_ARGB32, bw, bh, bw*4);
1684 wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1685 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1686
1687 wxAlphaPixelData::Iterator p(pixData);
1688 int y, x;
1689 for (y=0; y<bh; y++)
1690 {
1691 wxAlphaPixelData::Iterator rowStart = p;
1692 for (x=0; x<bw; x++)
1693 {
1694 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1695 // with alpha in the upper 8 bits, then red, then green, then
1696 // blue. The 32-bit quantities are stored native-endian.
1697 // Pre-multiplied alpha is used.
1698 unsigned char alpha = p.Alpha();
1699
1700 if (!bmpSource.HasAlpha() && mask)
1701 alpha = 255;
1702
1703 if (alpha == 0)
1704 *data = 0;
1705 else
1706 *data = ( alpha << 24
1707 | (p.Red() * alpha/255) << 16
1708 | (p.Green() * alpha/255) << 8
1709 | (p.Blue() * alpha/255) );
1710 ++data;
1711 ++p;
1712 }
1713 p = rowStart;
1714 p.OffsetY(pixData, 1);
1715 }
1716 }
1717 else // no alpha
1718 {
1719 surface = gs_cairo->cairo_image_surface_create_for_data(
1720 buffer, CAIRO_FORMAT_RGB24, bw, bh, bw*4);
1721 wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1722 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1723
1724 wxNativePixelData::Iterator p(pixData);
1725 int y, x;
1726 for (y=0; y<bh; y++)
1727 {
1728 wxNativePixelData::Iterator rowStart = p;
1729 for (x=0; x<bw; x++)
1730 {
1731 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1732 // the upper 8 bits unused. Red, Green, and Blue are stored in
1733 // the remaining 24 bits in that order. The 32-bit quantities
1734 // are stored native-endian.
1735 *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() );
1736 ++data;
1737 ++p;
1738 }
1739 p = rowStart;
1740 p.OffsetY(pixData, 1);
1741 }
1742 }
1743
1744
1745 gs_cairo->cairo_save(m_cairo);
1746
1747 // Prepare to draw the image.
1748 gs_cairo->cairo_translate(m_cairo, x, y);
1749
1750 // Scale the image
1751 cairo_filter_t filter = CAIRO_FILTER_BILINEAR;
1752 cairo_pattern_t* pattern = cairo_pattern_create_for_surface(surface);
1753 cairo_pattern_set_filter(pattern,filter);
1754 wxDouble scaleX = (wxDouble) XLOG2DEVREL(bw) / (wxDouble) bw;
1755 wxDouble scaleY = (wxDouble) YLOG2DEVREL(bh) / (wxDouble) bh;
1756 cairo_scale(m_cairo, scaleX, scaleY);
1757
1758 gs_cairo->cairo_set_source(m_cairo, pattern);
1759 // Use the original size here since the context is scaled already.
1760 gs_cairo->cairo_rectangle(m_cairo, 0, 0, bw, bh);
1761 // Fill the rectangle using the pattern.
1762 gs_cairo->cairo_fill(m_cairo);
1763
1764 // Clean up.
1765 gs_cairo->cairo_pattern_destroy(pattern);
1766 gs_cairo->cairo_surface_destroy(surface);
1767 delete [] buffer;
1768
1769 CalcBoundingBox(0,0);
1770 CalcBoundingBox(bw,bh);
1771
1772 gs_cairo->cairo_restore(m_cairo);
1773 }
1774
1775 void wxGtkPrinterDCImpl::DoDrawText(const wxString& text, wxCoord x, wxCoord y )
1776 {
1777 DoDrawRotatedText( text, x, y, 0.0 );
1778 }
1779
1780 void wxGtkPrinterDCImpl::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
1781 {
1782 double xx = XLOG2DEV(x);
1783 double yy = YLOG2DEV(y);
1784
1785 angle = -angle;
1786
1787 bool underlined = m_font.Ok() && m_font.GetUnderlined();
1788
1789 const wxUTF8Buf data = text.utf8_str();
1790
1791 size_t datalen = strlen(data);
1792 pango_layout_set_text( m_layout, data, datalen);
1793
1794 if (underlined)
1795 {
1796 PangoAttrList *attrs = pango_attr_list_new();
1797 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
1798 a->start_index = 0;
1799 a->end_index = datalen;
1800 pango_attr_list_insert(attrs, a);
1801 pango_layout_set_attributes(m_layout, attrs);
1802 pango_attr_list_unref(attrs);
1803 }
1804
1805 if (m_textForegroundColour.Ok())
1806 {
1807 unsigned char red = m_textForegroundColour.Red();
1808 unsigned char blue = m_textForegroundColour.Blue();
1809 unsigned char green = m_textForegroundColour.Green();
1810 unsigned char alpha = m_textForegroundColour.Alpha();
1811
1812 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1813 {
1814 double redPS = (double)(red) / 255.0;
1815 double bluePS = (double)(blue) / 255.0;
1816 double greenPS = (double)(green) / 255.0;
1817 double alphaPS = (double)(alpha) / 255.0;
1818
1819 gs_cairo->cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1820
1821 m_currentRed = red;
1822 m_currentBlue = blue;
1823 m_currentGreen = green;
1824 m_currentAlpha = alpha;
1825 }
1826 }
1827
1828 int w,h;
1829
1830 // Scale font description.
1831 gint oldSize = pango_font_description_get_size( m_fontdesc );
1832 double size = oldSize;
1833 size = size * m_scaleX;
1834 pango_font_description_set_size( m_fontdesc, (gint)size );
1835
1836 // Actually apply scaled font.
1837 pango_layout_set_font_description( m_layout, m_fontdesc );
1838
1839 pango_layout_get_pixel_size( m_layout, &w, &h );
1840
1841 if ( m_backgroundMode == wxBRUSHSTYLE_SOLID )
1842 {
1843 unsigned char red = m_textBackgroundColour.Red();
1844 unsigned char blue = m_textBackgroundColour.Blue();
1845 unsigned char green = m_textBackgroundColour.Green();
1846 unsigned char alpha = m_textBackgroundColour.Alpha();
1847
1848 double redPS = (double)(red) / 255.0;
1849 double bluePS = (double)(blue) / 255.0;
1850 double greenPS = (double)(green) / 255.0;
1851 double alphaPS = (double)(alpha) / 255.0;
1852
1853 gs_cairo->cairo_save(m_cairo);
1854 gs_cairo->cairo_translate(m_cairo, xx, yy);
1855 gs_cairo->cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1856 gs_cairo->cairo_rotate(m_cairo,angle*DEG2RAD);
1857 gs_cairo->cairo_rectangle(m_cairo, 0, 0, w, h); // still in cairo units
1858 gs_cairo->cairo_fill(m_cairo);
1859 gs_cairo->cairo_restore(m_cairo);
1860 }
1861
1862 // Draw layout.
1863 gs_cairo->cairo_move_to (m_cairo, xx, yy);
1864
1865 gs_cairo->cairo_save( m_cairo );
1866
1867 if (fabs(angle) > 0.00001)
1868 gs_cairo->cairo_rotate( m_cairo, angle*DEG2RAD );
1869
1870 gs_cairo->pango_cairo_update_layout (m_cairo, m_layout);
1871 gs_cairo->pango_cairo_show_layout (m_cairo, m_layout);
1872
1873 gs_cairo->cairo_restore( m_cairo );
1874
1875 if (underlined)
1876 {
1877 // Undo underline attributes setting
1878 pango_layout_set_attributes(m_layout, NULL);
1879 }
1880
1881 // Reset unscaled size.
1882 pango_font_description_set_size( m_fontdesc, oldSize );
1883
1884 // Actually apply unscaled font.
1885 pango_layout_set_font_description( m_layout, m_fontdesc );
1886
1887 // Back to device units:
1888 CalcBoundingBox (x, y);
1889 CalcBoundingBox (x + w, y + h);
1890 }
1891
1892 void wxGtkPrinterDCImpl::Clear()
1893 {
1894 // Clear does nothing for printing, but keep the code
1895 // for later reuse
1896 /*
1897 gs_cairo->cairo_save(m_cairo);
1898 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
1899 SetBrush(m_backgroundBrush);
1900 gs_cairo->cairo_paint(m_cairo);
1901 gs_cairo->cairo_restore(m_cairo);
1902 */
1903 }
1904
1905 void wxGtkPrinterDCImpl::SetFont( const wxFont& font )
1906 {
1907 m_font = font;
1908
1909 if (m_font.Ok())
1910 {
1911 if (m_fontdesc)
1912 pango_font_description_free( m_fontdesc );
1913
1914 m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description ); // m_fontdesc is now set to device units
1915
1916 // Scale font description from device units to pango units
1917 gint oldSize = pango_font_description_get_size( m_fontdesc );
1918 double size = oldSize *m_DEV2PS; // scale to cairo units
1919 pango_font_description_set_size( m_fontdesc, (gint)size ); // apply to description
1920
1921 // Actually apply scaled font.
1922 pango_layout_set_font_description( m_layout, m_fontdesc );
1923 }
1924 }
1925
1926 void wxGtkPrinterDCImpl::SetPen( const wxPen& pen )
1927 {
1928 if (!pen.Ok()) return;
1929
1930 m_pen = pen;
1931
1932 double width;
1933
1934 if (m_pen.GetWidth() <= 0)
1935 width = 0.1;
1936 else
1937 width = (double) m_pen.GetWidth();
1938
1939 gs_cairo->cairo_set_line_width( m_cairo, width * m_DEV2PS * m_scaleX );
1940 static const double dotted[] = {2.0, 5.0};
1941 static const double short_dashed[] = {4.0, 4.0};
1942 static const double long_dashed[] = {4.0, 8.0};
1943 static const double dotted_dashed[] = {6.0, 6.0, 2.0, 6.0};
1944
1945 switch (m_pen.GetStyle())
1946 {
1947 case wxPENSTYLE_DOT: gs_cairo->cairo_set_dash( m_cairo, dotted, 2, 0 ); break;
1948 case wxPENSTYLE_SHORT_DASH: gs_cairo->cairo_set_dash( m_cairo, short_dashed, 2, 0 ); break;
1949 case wxPENSTYLE_LONG_DASH: gs_cairo->cairo_set_dash( m_cairo, long_dashed, 2, 0 ); break;
1950 case wxPENSTYLE_DOT_DASH: gs_cairo->cairo_set_dash( m_cairo, dotted_dashed, 4, 0 ); break;
1951 case wxPENSTYLE_USER_DASH:
1952 {
1953 wxDash *wx_dashes;
1954 int num = m_pen.GetDashes (&wx_dashes);
1955 gdouble *g_dashes = g_new( gdouble, num );
1956 int i;
1957 for (i = 0; i < num; ++i)
1958 g_dashes[i] = (gdouble) wx_dashes[i];
1959 gs_cairo->cairo_set_dash( m_cairo, g_dashes, num, 0);
1960 g_free( g_dashes );
1961 }
1962 break;
1963 case wxPENSTYLE_SOLID:
1964 case wxPENSTYLE_TRANSPARENT:
1965 default: gs_cairo->cairo_set_dash( m_cairo, NULL, 0, 0 ); break;
1966 }
1967
1968 switch (m_pen.GetCap())
1969 {
1970 case wxCAP_PROJECTING: gs_cairo->cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_SQUARE); break;
1971 case wxCAP_BUTT: gs_cairo->cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_BUTT); break;
1972 case wxCAP_ROUND:
1973 default: gs_cairo->cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_ROUND); break;
1974 }
1975
1976 switch (m_pen.GetJoin())
1977 {
1978 case wxJOIN_BEVEL: gs_cairo->cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_BEVEL); break;
1979 case wxJOIN_MITER: gs_cairo->cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_MITER); break;
1980 case wxJOIN_ROUND:
1981 default: gs_cairo->cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_ROUND); break;
1982 }
1983
1984 unsigned char red = m_pen.GetColour().Red();
1985 unsigned char blue = m_pen.GetColour().Blue();
1986 unsigned char green = m_pen.GetColour().Green();
1987 unsigned char alpha = m_pen.GetColour().Alpha();
1988
1989 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1990 {
1991 double redPS = (double)(red) / 255.0;
1992 double bluePS = (double)(blue) / 255.0;
1993 double greenPS = (double)(green) / 255.0;
1994 double alphaPS = (double)(alpha) / 255.0;
1995
1996 gs_cairo->cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1997
1998 m_currentRed = red;
1999 m_currentBlue = blue;
2000 m_currentGreen = green;
2001 m_currentAlpha = alpha;
2002 }
2003 }
2004
2005 void wxGtkPrinterDCImpl::SetBrush( const wxBrush& brush )
2006 {
2007 if (!brush.Ok()) return;
2008
2009 m_brush = brush;
2010
2011 if (m_brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT)
2012 {
2013 gs_cairo->cairo_set_source_rgba( m_cairo, 0, 0, 0, 0 );
2014 m_currentRed = 0;
2015 m_currentBlue = 0;
2016 m_currentGreen = 0;
2017 m_currentAlpha = 0;
2018 return;
2019 }
2020
2021 // Brush colour.
2022 unsigned char red = m_brush.GetColour().Red();
2023 unsigned char blue = m_brush.GetColour().Blue();
2024 unsigned char green = m_brush.GetColour().Green();
2025 unsigned char alpha = m_brush.GetColour().Alpha();
2026
2027 double redPS = (double)(red) / 255.0;
2028 double bluePS = (double)(blue) / 255.0;
2029 double greenPS = (double)(green) / 255.0;
2030 double alphaPS = (double)(alpha) / 255.0;
2031
2032 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
2033 {
2034 gs_cairo->cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
2035
2036 m_currentRed = red;
2037 m_currentBlue = blue;
2038 m_currentGreen = green;
2039 m_currentAlpha = alpha;
2040 }
2041
2042 if (m_brush.IsHatch())
2043 {
2044 cairo_t * cr;
2045 cairo_surface_t *surface;
2046 surface = gs_cairo->cairo_surface_create_similar(gs_cairo->cairo_get_target(m_cairo),CAIRO_CONTENT_COLOR_ALPHA,10,10);
2047 cr = gs_cairo->cairo_create(surface);
2048 gs_cairo->cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
2049 gs_cairo->cairo_set_line_width(cr, 1);
2050 gs_cairo->cairo_set_line_join(cr,CAIRO_LINE_JOIN_MITER);
2051
2052 switch (m_brush.GetStyle())
2053 {
2054 case wxBRUSHSTYLE_CROSS_HATCH:
2055 gs_cairo->cairo_move_to(cr, 5, 0);
2056 gs_cairo->cairo_line_to(cr, 5, 10);
2057 gs_cairo->cairo_move_to(cr, 0, 5);
2058 gs_cairo->cairo_line_to(cr, 10, 5);
2059 break;
2060 case wxBRUSHSTYLE_BDIAGONAL_HATCH:
2061 gs_cairo->cairo_move_to(cr, 0, 10);
2062 gs_cairo->cairo_line_to(cr, 10, 0);
2063 break;
2064 case wxBRUSHSTYLE_FDIAGONAL_HATCH:
2065 gs_cairo->cairo_move_to(cr, 0, 0);
2066 gs_cairo->cairo_line_to(cr, 10, 10);
2067 break;
2068 case wxBRUSHSTYLE_CROSSDIAG_HATCH:
2069 gs_cairo->cairo_move_to(cr, 0, 0);
2070 gs_cairo->cairo_line_to(cr, 10, 10);
2071 gs_cairo->cairo_move_to(cr, 10, 0);
2072 gs_cairo->cairo_line_to(cr, 0, 10);
2073 break;
2074 case wxBRUSHSTYLE_HORIZONTAL_HATCH:
2075 gs_cairo->cairo_move_to(cr, 0, 5);
2076 gs_cairo->cairo_line_to(cr, 10, 5);
2077 break;
2078 case wxBRUSHSTYLE_VERTICAL_HATCH:
2079 gs_cairo->cairo_move_to(cr, 5, 0);
2080 gs_cairo->cairo_line_to(cr, 5, 10);
2081 break;
2082 default:
2083 wxFAIL_MSG(_("Couldn't get hatch style from wxBrush."));
2084 }
2085
2086 gs_cairo->cairo_set_source_rgba(cr, redPS, greenPS, bluePS, alphaPS);
2087 gs_cairo->cairo_stroke (cr);
2088
2089 gs_cairo->cairo_destroy(cr);
2090 cairo_pattern_t * pattern = gs_cairo->cairo_pattern_create_for_surface (surface);
2091 gs_cairo->cairo_surface_destroy(surface);
2092 gs_cairo->cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
2093 gs_cairo->cairo_set_source(m_cairo, pattern);
2094 gs_cairo->cairo_pattern_destroy(pattern);
2095 }
2096 }
2097
2098 void wxGtkPrinterDCImpl::SetLogicalFunction( int function )
2099 {
2100 if (function == wxCLEAR)
2101 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_CLEAR);
2102 else if (function == wxOR)
2103 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_OUT);
2104 else if (function == wxNO_OP)
2105 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST);
2106 else if (function == wxAND)
2107 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_ADD);
2108 else if (function == wxSET)
2109 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_SATURATE);
2110 else if (function == wxXOR)
2111 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_XOR);
2112 else // wxCOPY or anything else.
2113 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
2114 }
2115
2116 void wxGtkPrinterDCImpl::SetBackground( const wxBrush& brush )
2117 {
2118 m_backgroundBrush = brush;
2119 gs_cairo->cairo_save(m_cairo);
2120 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST_OVER);
2121
2122 SetBrush(m_backgroundBrush);
2123 gs_cairo->cairo_paint(m_cairo);
2124 gs_cairo->cairo_restore(m_cairo);
2125 }
2126
2127 void wxGtkPrinterDCImpl::SetBackgroundMode(int mode)
2128 {
2129 if (mode == wxBRUSHSTYLE_SOLID)
2130 m_backgroundMode = wxBRUSHSTYLE_SOLID;
2131 else
2132 m_backgroundMode = wxBRUSHSTYLE_TRANSPARENT;
2133 }
2134
2135 void wxGtkPrinterDCImpl::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
2136 {
2137 gs_cairo->cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height));
2138 gs_cairo->cairo_clip(m_cairo);
2139 }
2140
2141 void wxGtkPrinterDCImpl::DestroyClippingRegion()
2142 {
2143 gs_cairo->cairo_reset_clip(m_cairo);
2144 }
2145
2146 bool wxGtkPrinterDCImpl::StartDoc(const wxString& WXUNUSED(message))
2147 {
2148 return true;
2149 }
2150
2151 void wxGtkPrinterDCImpl::EndDoc()
2152 {
2153 return;
2154 }
2155
2156 void wxGtkPrinterDCImpl::StartPage()
2157 {
2158 return;
2159 }
2160
2161 void wxGtkPrinterDCImpl::EndPage()
2162 {
2163 return;
2164 }
2165
2166 wxCoord wxGtkPrinterDCImpl::GetCharHeight() const
2167 {
2168 pango_layout_set_text( m_layout, "H", 1 );
2169
2170 int w,h;
2171 pango_layout_get_pixel_size( m_layout, &w, &h );
2172
2173 return wxRound( h * m_PS2DEV );
2174 }
2175
2176 wxCoord wxGtkPrinterDCImpl::GetCharWidth() const
2177 {
2178 pango_layout_set_text( m_layout, "H", 1 );
2179
2180 int w,h;
2181 pango_layout_get_pixel_size( m_layout, &w, &h );
2182
2183 return wxRound( w * m_PS2DEV );
2184 }
2185
2186 void wxGtkPrinterDCImpl::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoord *height,
2187 wxCoord *descent,
2188 wxCoord *externalLeading,
2189 const wxFont *theFont ) const
2190 {
2191 if ( width )
2192 *width = 0;
2193 if ( height )
2194 *height = 0;
2195 if ( descent )
2196 *descent = 0;
2197 if ( externalLeading )
2198 *externalLeading = 0;
2199
2200 if (string.empty())
2201 {
2202 return;
2203 }
2204
2205 // Set layout's text
2206 const wxUTF8Buf dataUTF8 = string.utf8_str();
2207
2208 PangoFontDescription *desc = m_fontdesc;
2209 if (theFont) desc = theFont->GetNativeFontInfo()->description;
2210
2211 gint oldSize = pango_font_description_get_size( desc );
2212 double size = oldSize;
2213 size = size * m_scaleY;
2214 pango_font_description_set_size( desc, (gint)size );
2215
2216 // apply scaled font
2217 pango_layout_set_font_description( m_layout, desc );
2218
2219 pango_layout_set_text( m_layout, dataUTF8, strlen(dataUTF8) );
2220
2221 int w, h;
2222 pango_layout_get_pixel_size( m_layout, &w, &h );
2223
2224 if (width)
2225 *width = wxRound( (double)w / m_scaleX * m_PS2DEV );
2226 if (height)
2227 *height = wxRound( (double)h / m_scaleY * m_PS2DEV );
2228
2229 if (descent)
2230 {
2231 PangoLayoutIter *iter = pango_layout_get_iter(m_layout);
2232 int baseline = pango_layout_iter_get_baseline(iter);
2233 pango_layout_iter_free(iter);
2234 *descent = wxRound( (h - PANGO_PIXELS(baseline)) * m_PS2DEV );
2235 }
2236
2237 // Reset unscaled size.
2238 pango_font_description_set_size( desc, oldSize );
2239
2240 // Reset unscaled font.
2241 pango_layout_set_font_description( m_layout, m_fontdesc );
2242 }
2243
2244 void wxGtkPrinterDCImpl::DoGetSize(int* width, int* height) const
2245 {
2246 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
2247
2248 if (width)
2249 *width = wxRound( (double)gtk_page_setup_get_paper_width( setup, GTK_UNIT_POINTS ) * (double)m_resolution / 72.0 );
2250 if (height)
2251 *height = wxRound( (double)gtk_page_setup_get_paper_height( setup, GTK_UNIT_POINTS ) * (double)m_resolution / 72.0 );
2252 }
2253
2254 void wxGtkPrinterDCImpl::DoGetSizeMM(int *width, int *height) const
2255 {
2256 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
2257
2258 if (width)
2259 *width = wxRound( gtk_page_setup_get_paper_width( setup, GTK_UNIT_MM ) );
2260 if (height)
2261 *height = wxRound( gtk_page_setup_get_paper_height( setup, GTK_UNIT_MM ) );
2262 }
2263
2264 wxSize wxGtkPrinterDCImpl::GetPPI() const
2265 {
2266 return wxSize( (int)m_resolution, (int)m_resolution );
2267 }
2268
2269 void wxGtkPrinterDCImpl::SetPrintData(const wxPrintData& data)
2270 {
2271 m_printData = data;
2272 }
2273
2274 // overriden for wxPrinterDC Impl
2275
2276 wxRect wxGtkPrinterDCImpl::GetPaperRect()
2277 {
2278 // Does GtkPrint support printer margins?
2279 int w = 0;
2280 int h = 0;
2281 DoGetSize( &w, &h );
2282 return wxRect( 0,0,w,h );
2283 }
2284
2285 int wxGtkPrinterDCImpl::GetResolution()
2286 {
2287 return m_resolution;
2288 }
2289
2290 // ----------------------------------------------------------------------------
2291 // Print preview
2292 // ----------------------------------------------------------------------------
2293
2294 IMPLEMENT_CLASS(wxGtkPrintPreview, wxPrintPreviewBase)
2295
2296 void wxGtkPrintPreview::Init(wxPrintout * WXUNUSED(printout),
2297 wxPrintout * WXUNUSED(printoutForPrinting),
2298 wxPrintData *data)
2299 {
2300 // convert wxPrintQuality to resolution (input pointer can be NULL)
2301 wxPrintQuality quality = data ? data->GetQuality() : wxPRINT_QUALITY_MEDIUM;
2302 switch ( quality )
2303 {
2304 case wxPRINT_QUALITY_HIGH:
2305 m_resolution = 1200;
2306 break;
2307
2308 case wxPRINT_QUALITY_LOW:
2309 m_resolution = 300;
2310 break;
2311
2312 case wxPRINT_QUALITY_DRAFT:
2313 m_resolution = 150;
2314 break;
2315
2316 default:
2317 if ( quality > 0 )
2318 {
2319 // positive values directly indicate print resolution
2320 m_resolution = quality;
2321 break;
2322 }
2323
2324 wxFAIL_MSG( "unknown print quality" );
2325 // fall through
2326
2327 case wxPRINT_QUALITY_MEDIUM:
2328 m_resolution = 600;
2329 break;
2330
2331 }
2332
2333 DetermineScaling();
2334 }
2335
2336 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout,
2337 wxPrintout *printoutForPrinting,
2338 wxPrintDialogData *data)
2339 : wxPrintPreviewBase(printout, printoutForPrinting, data)
2340 {
2341 Init(printout, printoutForPrinting, data ? &data->GetPrintData() : NULL);
2342 }
2343
2344 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout,
2345 wxPrintout *printoutForPrinting,
2346 wxPrintData *data)
2347 : wxPrintPreviewBase(printout, printoutForPrinting, data)
2348 {
2349 Init(printout, printoutForPrinting, data);
2350 }
2351
2352 wxGtkPrintPreview::~wxGtkPrintPreview()
2353 {
2354 }
2355
2356 bool wxGtkPrintPreview::Print(bool interactive)
2357 {
2358 if (!m_printPrintout)
2359 return false;
2360
2361 wxPrinter printer(& m_printDialogData);
2362 return printer.Print(m_previewFrame, m_printPrintout, interactive);
2363 }
2364
2365 void wxGtkPrintPreview::DetermineScaling()
2366 {
2367 wxPaperSize paperType = m_printDialogData.GetPrintData().GetPaperId();
2368
2369 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
2370 if (!paper)
2371 paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
2372
2373 if (paper)
2374 {
2375 wxSize ScreenPixels = wxGetDisplaySize();
2376 wxSize ScreenMM = wxGetDisplaySizeMM();
2377
2378 m_previewPrintout->SetPPIScreen( (int) ((ScreenPixels.GetWidth() * 25.4) / ScreenMM.GetWidth()),
2379 (int) ((ScreenPixels.GetHeight() * 25.4) / ScreenMM.GetHeight()) );
2380
2381 m_previewPrintout->SetPPIPrinter( m_resolution, m_resolution );
2382
2383 // Get width and height in points (1/72th of an inch)
2384 wxSize sizeDevUnits(paper->GetSizeDeviceUnits());
2385 sizeDevUnits.x = wxRound((double)sizeDevUnits.x * (double)m_resolution / 72.0);
2386 sizeDevUnits.y = wxRound((double)sizeDevUnits.y * (double)m_resolution / 72.0);
2387
2388 wxSize sizeTenthsMM(paper->GetSize());
2389 wxSize sizeMM(sizeTenthsMM.x / 10, sizeTenthsMM.y / 10);
2390
2391 // If in landscape mode, we need to swap the width and height.
2392 if ( m_printDialogData.GetPrintData().GetOrientation() == wxLANDSCAPE )
2393 {
2394 m_pageWidth = sizeDevUnits.y;
2395 m_pageHeight = sizeDevUnits.x;
2396 m_previewPrintout->SetPageSizeMM(sizeMM.y, sizeMM.x);
2397 }
2398 else
2399 {
2400 m_pageWidth = sizeDevUnits.x;
2401 m_pageHeight = sizeDevUnits.y;
2402 m_previewPrintout->SetPageSizeMM(sizeMM.x, sizeMM.y);
2403 }
2404 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
2405 m_previewPrintout->SetPaperRectPixels(wxRect(0, 0, m_pageWidth, m_pageHeight));
2406
2407 // At 100%, the page should look about page-size on the screen.
2408 m_previewScaleX = 0.8 * 72.0 / (double)m_resolution;
2409 m_previewScaleY = m_previewScaleX;
2410 }
2411 }
2412
2413 #endif
2414 // wxUSE_GTKPRINT