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