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