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