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