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