1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/print.cpp
3 // Author: Anthony Bretaudeau
4 // Purpose: GTK printing support
7 // Copyright: (c) 2007 wxWidgets development team
8 // Licence: wxWindows Licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/gtk/print.h"
24 #include "wx/dcmemory.h"
25 #include "wx/dcprint.h"
29 #include "wx/module.h"
33 #include "wx/fontutil.h"
34 #include "wx/dynlib.h"
36 #include "wx/scopeguard.h"
37 #include "wx/testing.h"
41 #if GTK_CHECK_VERSION(2,14,0)
42 #include <gtk/gtkunixprint.h>
44 #include <gtk/gtkpagesetupunixdialog.h>
48 #if wxUSE_GRAPHICS_CONTEXT
49 #include "wx/graphics.h"
53 wxFORCE_LINK_THIS_MODULE(gtk_print
)
55 #if wxUSE_LIBGNOMEPRINT
56 #include "wx/gtk/gnome/gprint.h"
59 #include "wx/gtk/private/object.h"
61 // Useful to convert angles from/to Rad to/from Deg.
62 static const double RAD2DEG
= 180.0 / M_PI
;
63 static const double DEG2RAD
= M_PI
/ 180.0;
65 //----------------------------------------------------------------------------
67 // Initialized when starting the app : if it successfully load the gtk-print framework,
68 // it uses it. If not, it falls back to gnome print (see /gtk/gnome/gprint.cpp) then
69 // to postscript if gnomeprint is not available.
70 //----------------------------------------------------------------------------
72 class wxGtkPrintModule
: public wxModule
77 #if wxUSE_LIBGNOMEPRINT
78 // This module must be initialized AFTER gnomeprint's one
79 AddDependency(wxCLASSINFO(wxGnomePrintModule
));
86 DECLARE_DYNAMIC_CLASS(wxGtkPrintModule
)
89 bool wxGtkPrintModule::OnInit()
92 if (gtk_check_version(2,10,0) == NULL
)
95 wxPrintFactory::SetPrintFactory( new wxGtkPrintFactory
);
100 IMPLEMENT_DYNAMIC_CLASS(wxGtkPrintModule
, wxModule
)
102 //----------------------------------------------------------------------------
104 //----------------------------------------------------------------------------
106 wxPrinterBase
* wxGtkPrintFactory::CreatePrinter( wxPrintDialogData
*data
)
108 return new wxGtkPrinter( data
);
111 wxPrintPreviewBase
*wxGtkPrintFactory::CreatePrintPreview( wxPrintout
*preview
,
112 wxPrintout
*printout
,
113 wxPrintDialogData
*data
)
115 return new wxGtkPrintPreview( preview
, printout
, data
);
118 wxPrintPreviewBase
*wxGtkPrintFactory::CreatePrintPreview( wxPrintout
*preview
,
119 wxPrintout
*printout
,
122 return new wxGtkPrintPreview( preview
, printout
, data
);
125 wxPrintDialogBase
*wxGtkPrintFactory::CreatePrintDialog( wxWindow
*parent
,
126 wxPrintDialogData
*data
)
128 return new wxGtkPrintDialog( parent
, data
);
131 wxPrintDialogBase
*wxGtkPrintFactory::CreatePrintDialog( wxWindow
*parent
,
134 return new wxGtkPrintDialog( parent
, data
);
137 wxPageSetupDialogBase
*wxGtkPrintFactory::CreatePageSetupDialog( wxWindow
*parent
,
138 wxPageSetupDialogData
* data
)
140 return new wxGtkPageSetupDialog( parent
, data
);
143 bool wxGtkPrintFactory::HasPrintSetupDialog()
149 wxGtkPrintFactory::CreatePrintSetupDialog(wxWindow
* WXUNUSED(parent
),
150 wxPrintData
* WXUNUSED(data
))
155 wxDCImpl
* wxGtkPrintFactory::CreatePrinterDCImpl( wxPrinterDC
*owner
, const wxPrintData
& data
)
157 return new wxGtkPrinterDCImpl( owner
, data
);
160 bool wxGtkPrintFactory::HasOwnPrintToFile()
165 bool wxGtkPrintFactory::HasPrinterLine()
170 wxString
wxGtkPrintFactory::CreatePrinterLine()
173 return wxEmptyString
;
176 bool wxGtkPrintFactory::HasStatusLine()
182 wxString
wxGtkPrintFactory::CreateStatusLine()
185 return wxEmptyString
;
188 wxPrintNativeDataBase
*wxGtkPrintFactory::CreatePrintNativeData()
190 return new wxGtkPrintNativeData
;
193 //----------------------------------------------------------------------------
194 // Callback functions for Gtk Printings.
195 //----------------------------------------------------------------------------
197 // We use it to pass useful objects to GTK printing callback functions.
198 struct wxPrinterToGtkData
200 wxGtkPrinter
* printer
;
201 wxPrintout
* printout
;
206 static void gtk_begin_print_callback (GtkPrintOperation
*operation
, GtkPrintContext
*context
, gpointer user_data
)
208 wxPrinterToGtkData
*data
= (wxPrinterToGtkData
*) user_data
;
210 data
->printer
->BeginPrint(data
->printout
, operation
, context
);
213 static void gtk_draw_page_print_callback (GtkPrintOperation
*operation
, GtkPrintContext
*context
, gint page_nr
, gpointer user_data
)
215 wxPrinterToGtkData
*data
= (wxPrinterToGtkData
*) user_data
;
217 data
->printer
->DrawPage(data
->printout
, operation
, context
, page_nr
);
220 static void gtk_end_print_callback(GtkPrintOperation
* WXUNUSED(operation
),
221 GtkPrintContext
* WXUNUSED(context
),
224 wxPrintout
*printout
= (wxPrintout
*) user_data
;
226 printout
->OnEndPrinting();
230 //----------------------------------------------------------------------------
231 // wxGtkPrintNativeData
232 //----------------------------------------------------------------------------
234 IMPLEMENT_CLASS(wxGtkPrintNativeData
, wxPrintNativeDataBase
)
236 wxGtkPrintNativeData::wxGtkPrintNativeData()
238 m_config
= gtk_print_settings_new();
243 wxGtkPrintNativeData::~wxGtkPrintNativeData()
245 g_object_unref(m_config
);
248 // Convert datas stored in m_config to a wxPrintData.
249 // Called by wxPrintData::ConvertFromNative().
250 bool wxGtkPrintNativeData::TransferTo( wxPrintData
&data
)
255 int resolution
= gtk_print_settings_get_resolution(m_config
);
256 if ( resolution
> 0 )
258 // if resolution is explicitly set, use it
259 data
.SetQuality(resolution
);
261 else // use more vague "quality"
263 GtkPrintQuality quality
= gtk_print_settings_get_quality(m_config
);
264 if (quality
== GTK_PRINT_QUALITY_HIGH
)
265 data
.SetQuality(wxPRINT_QUALITY_HIGH
);
266 else if (quality
== GTK_PRINT_QUALITY_LOW
)
267 data
.SetQuality(wxPRINT_QUALITY_LOW
);
268 else if (quality
== GTK_PRINT_QUALITY_DRAFT
)
269 data
.SetQuality(wxPRINT_QUALITY_DRAFT
);
271 data
.SetQuality(wxPRINT_QUALITY_MEDIUM
);
274 data
.SetNoCopies(gtk_print_settings_get_n_copies(m_config
));
276 data
.SetColour(gtk_print_settings_get_use_color(m_config
));
278 switch (gtk_print_settings_get_duplex(m_config
))
280 case GTK_PRINT_DUPLEX_SIMPLEX
: data
.SetDuplex (wxDUPLEX_SIMPLEX
);
283 case GTK_PRINT_DUPLEX_HORIZONTAL
: data
.SetDuplex (wxDUPLEX_HORIZONTAL
);
287 case GTK_PRINT_DUPLEX_VERTICAL
: data
.SetDuplex (wxDUPLEX_VERTICAL
);
291 GtkPageOrientation orientation
= gtk_print_settings_get_orientation (m_config
);
292 if (orientation
== GTK_PAGE_ORIENTATION_PORTRAIT
)
294 data
.SetOrientation(wxPORTRAIT
);
295 data
.SetOrientationReversed(false);
297 else if (orientation
== GTK_PAGE_ORIENTATION_LANDSCAPE
)
299 data
.SetOrientation(wxLANDSCAPE
);
300 data
.SetOrientationReversed(false);
302 else if (orientation
== GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT
)
304 data
.SetOrientation(wxPORTRAIT
);
305 data
.SetOrientationReversed(true);
307 else if (orientation
== GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE
)
309 data
.SetOrientation(wxLANDSCAPE
);
310 data
.SetOrientationReversed(true);
313 data
.SetCollate(gtk_print_settings_get_collate (m_config
));
315 // Paper formats : these are the most common paper formats.
316 GtkPaperSize
*paper_size
= gtk_print_settings_get_paper_size (m_config
);
318 data
.SetPaperId(wxPAPER_NONE
);
319 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_A3
)))
320 data
.SetPaperId(wxPAPER_A3
);
321 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_A4
)))
322 data
.SetPaperId(wxPAPER_A4
);
323 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_A5
)))
324 data
.SetPaperId(wxPAPER_A5
);
325 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_B5
)))
326 data
.SetPaperId(wxPAPER_B5
);
327 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_LETTER
)))
328 data
.SetPaperId(wxPAPER_LETTER
);
329 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_LEGAL
)))
330 data
.SetPaperId(wxPAPER_LEGAL
);
331 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_EXECUTIVE
)))
332 data
.SetPaperId(wxPAPER_EXECUTIVE
);
333 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"na_number-10")))
334 data
.SetPaperId(wxPAPER_ENV_10
);
335 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c5")))
336 data
.SetPaperId(wxPAPER_ENV_C5
);
337 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c6")))
338 data
.SetPaperId(wxPAPER_ENV_C6
);
339 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"jis-b5")))
340 data
.SetPaperId(wxPAPER_B5_TRANSVERSE
);
341 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b5")))
342 data
.SetPaperId(wxPAPER_ENV_B5
);
343 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"na_monarch")))
344 data
.SetPaperId(wxPAPER_ENV_MONARCH
);
345 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-c")))
346 data
.SetPaperId( wxPAPER_CSHEET
);
347 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-d")))
348 data
.SetPaperId( wxPAPER_DSHEET
);
349 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-e")))
350 data
.SetPaperId( wxPAPER_ESHEET
);
351 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"letter")))
352 data
.SetPaperId( wxPAPER_LETTERSMALL
);
353 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-b")))
354 data
.SetPaperId( wxPAPER_TABLOID
);
355 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"ledger")))
356 data
.SetPaperId( wxPAPER_LEDGER
);
357 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"statement")))
358 data
.SetPaperId( wxPAPER_STATEMENT
);
359 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( GTK_PAPER_NAME_A4
)))
360 data
.SetPaperId( wxPAPER_A4SMALL
);
361 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b4")))
362 data
.SetPaperId( wxPAPER_B4
);
363 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"folio")))
364 data
.SetPaperId( wxPAPER_FOLIO
);
365 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"quarto")))
366 data
.SetPaperId( wxPAPER_QUARTO
);
367 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"10x14")))
368 data
.SetPaperId( wxPAPER_10X14
);
369 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"ledger")))
370 data
.SetPaperId( wxPAPER_11X17
);
371 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"letter")))
372 data
.SetPaperId( wxPAPER_NOTE
);
373 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"na-number-9-envelope")))
374 data
.SetPaperId( wxPAPER_ENV_9
);
375 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"number-11")))
376 data
.SetPaperId( wxPAPER_ENV_11
);
377 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"number-12")))
378 data
.SetPaperId( wxPAPER_ENV_12
);
379 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"number-14")))
380 data
.SetPaperId( wxPAPER_ENV_14
);
381 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-designated")))
382 data
.SetPaperId( wxPAPER_ENV_DL
);
383 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c3")))
384 data
.SetPaperId( wxPAPER_ENV_C3
);
385 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c4")))
386 data
.SetPaperId( wxPAPER_ENV_C4
);
387 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"c6/c5")))
388 data
.SetPaperId( wxPAPER_ENV_C65
);
389 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b4")))
390 data
.SetPaperId( wxPAPER_ENV_B4
);
391 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b6")))
392 data
.SetPaperId( wxPAPER_ENV_B6
);
393 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"Italian")))
394 data
.SetPaperId( wxPAPER_ENV_ITALY
);
395 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"personal")))
396 data
.SetPaperId( wxPAPER_ENV_PERSONAL
);
397 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"fanfold-us")))
398 data
.SetPaperId( wxPAPER_FANFOLD_US
);
399 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"fanfold-European")))
400 data
.SetPaperId( wxPAPER_FANFOLD_STD_GERMAN
);
401 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"foolscap")))
402 data
.SetPaperId( wxPAPER_FANFOLD_LGL_GERMAN
);
404 data
.SetPaperId(wxPAPER_NONE
);
406 data
.SetPrinterName(gtk_print_settings_get_printer(m_config
));
411 // Put datas given by the wxPrintData into m_config.
412 // Called by wxPrintData::ConvertToNative().
413 bool wxGtkPrintNativeData::TransferFrom( const wxPrintData
&data
)
418 wxPrintQuality quality
= data
.GetQuality();
419 if (quality
== wxPRINT_QUALITY_HIGH
)
420 gtk_print_settings_set_quality (m_config
, GTK_PRINT_QUALITY_HIGH
);
421 else if (quality
== wxPRINT_QUALITY_MEDIUM
)
422 gtk_print_settings_set_quality (m_config
, GTK_PRINT_QUALITY_NORMAL
);
423 else if (quality
== wxPRINT_QUALITY_LOW
)
424 gtk_print_settings_set_quality (m_config
, GTK_PRINT_QUALITY_LOW
);
425 else if (quality
== wxPRINT_QUALITY_DRAFT
)
426 gtk_print_settings_set_quality (m_config
, GTK_PRINT_QUALITY_DRAFT
);
427 else if (quality
> 1)
428 gtk_print_settings_set_resolution (m_config
, quality
);
430 gtk_print_settings_set_quality (m_config
, GTK_PRINT_QUALITY_NORMAL
);
432 gtk_print_settings_set_n_copies(m_config
, data
.GetNoCopies());
434 gtk_print_settings_set_use_color(m_config
, data
.GetColour());
436 switch (data
.GetDuplex())
438 case wxDUPLEX_SIMPLEX
: gtk_print_settings_set_duplex (m_config
, GTK_PRINT_DUPLEX_SIMPLEX
);
441 case wxDUPLEX_HORIZONTAL
: gtk_print_settings_set_duplex (m_config
, GTK_PRINT_DUPLEX_HORIZONTAL
);
445 case wxDUPLEX_VERTICAL
: gtk_print_settings_set_duplex (m_config
, GTK_PRINT_DUPLEX_VERTICAL
);
449 if (!data
.IsOrientationReversed())
451 if (data
.GetOrientation() == wxLANDSCAPE
)
452 gtk_print_settings_set_orientation (m_config
, GTK_PAGE_ORIENTATION_LANDSCAPE
);
454 gtk_print_settings_set_orientation (m_config
, GTK_PAGE_ORIENTATION_PORTRAIT
);
457 if (data
.GetOrientation() == wxLANDSCAPE
)
458 gtk_print_settings_set_orientation (m_config
, GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE
);
460 gtk_print_settings_set_orientation (m_config
, GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT
);
463 gtk_print_settings_set_collate (m_config
, data
.GetCollate());
465 // Paper formats: these are the most common paper formats.
466 switch (data
.GetPaperId())
468 case wxPAPER_A3
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_A3
));
470 case wxPAPER_A4
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_A4
));
472 case wxPAPER_A5
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_A5
));
474 case wxPAPER_B5_TRANSVERSE
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "jis-b5"));
476 case wxPAPER_B5
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_B5
));
478 case wxPAPER_LETTER
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_LETTER
));
480 case wxPAPER_LEGAL
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_LEGAL
));
482 case wxPAPER_EXECUTIVE
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_EXECUTIVE
));
484 case wxPAPER_ENV_10
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "na_number-10"));
486 case wxPAPER_ENV_C5
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-c5"));
488 case wxPAPER_ENV_C6
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-c6"));
490 case wxPAPER_ENV_B5
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-c5b5"));
492 case wxPAPER_ENV_MONARCH
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "na_monarch"));
494 case wxPAPER_CSHEET
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "engineering-c"));
496 case wxPAPER_DSHEET
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "engineering-d"));
498 case wxPAPER_ESHEET
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "engineering-e"));
500 case wxPAPER_LETTERSMALL
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "letter"));
502 case wxPAPER_TABLOID
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "engineering-b"));
504 case wxPAPER_LEDGER
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "ledger"));
506 case wxPAPER_STATEMENT
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "statement"));
508 case wxPAPER_A4SMALL
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_A4
));
510 case wxPAPER_B4
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-b4"));
512 case wxPAPER_FOLIO
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "folio"));
514 case wxPAPER_QUARTO
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "quarto"));
516 case wxPAPER_10X14
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "10x14"));
518 case wxPAPER_11X17
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "ledger"));
520 case wxPAPER_NOTE
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "letter"));
522 case wxPAPER_ENV_9
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "na-number-9-envelope"));
524 case wxPAPER_ENV_11
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "number-11"));
526 case wxPAPER_ENV_12
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "number-12"));
528 case wxPAPER_ENV_14
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "number-14"));
530 case wxPAPER_ENV_DL
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-designated"));
532 case wxPAPER_ENV_C3
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-c3"));
534 case wxPAPER_ENV_C4
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-c4"));
536 case wxPAPER_ENV_C65
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "c6/c5"));
538 case wxPAPER_ENV_B4
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-b4"));
540 case wxPAPER_ENV_B6
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-b6"));
542 case wxPAPER_ENV_ITALY
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "Italian"));
544 case wxPAPER_ENV_PERSONAL
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "personal"));
546 case wxPAPER_FANFOLD_US
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "fanfold-us"));
548 case wxPAPER_FANFOLD_STD_GERMAN
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "fanfold-European"));
550 case wxPAPER_FANFOLD_LGL_GERMAN
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "foolscap"));
556 gtk_print_settings_set_printer(m_config
, data
.GetPrinterName().utf8_str());
561 void wxGtkPrintNativeData::SetPrintConfig( GtkPrintSettings
* config
)
564 m_config
= gtk_print_settings_copy(config
);
567 // Extract page setup from settings.
568 GtkPageSetup
* wxGtkPrintNativeData::GetPageSetupFromSettings(GtkPrintSettings
* settings
)
570 GtkPageSetup
* page_setup
= gtk_page_setup_new();
571 gtk_page_setup_set_orientation (page_setup
, gtk_print_settings_get_orientation (settings
));
573 GtkPaperSize
*paper_size
= gtk_print_settings_get_paper_size (settings
);
574 if (paper_size
!= NULL
)
575 gtk_page_setup_set_paper_size_and_default_margins (page_setup
, paper_size
);
580 // Insert page setup into a given GtkPrintSettings.
581 void wxGtkPrintNativeData::SetPageSetupToSettings(GtkPrintSettings
* settings
, GtkPageSetup
* page_setup
)
583 gtk_print_settings_set_orientation ( settings
, gtk_page_setup_get_orientation (page_setup
));
584 gtk_print_settings_set_paper_size ( settings
, gtk_page_setup_get_paper_size (page_setup
));
587 //----------------------------------------------------------------------------
589 //----------------------------------------------------------------------------
591 IMPLEMENT_CLASS(wxGtkPrintDialog
, wxPrintDialogBase
)
593 wxGtkPrintDialog::wxGtkPrintDialog( wxWindow
*parent
, wxPrintDialogData
*data
)
594 : wxPrintDialogBase(parent
, wxID_ANY
, _("Print"),
595 wxPoint(0, 0), wxSize(600, 600),
596 wxDEFAULT_DIALOG_STYLE
|
600 m_printDialogData
= *data
;
606 wxGtkPrintDialog::wxGtkPrintDialog( wxWindow
*parent
, wxPrintData
*data
)
607 : wxPrintDialogBase(parent
, wxID_ANY
, _("Print"),
608 wxPoint(0, 0), wxSize(600, 600),
609 wxDEFAULT_DIALOG_STYLE
|
613 m_printDialogData
= *data
;
620 wxGtkPrintDialog::~wxGtkPrintDialog()
624 // This is called even if we actually don't want the dialog to appear.
625 int wxGtkPrintDialog::ShowModal()
627 WX_TESTING_SHOW_MODAL_HOOK();
629 // We need to restore the settings given in the constructor.
630 wxPrintData data
= m_printDialogData
.GetPrintData();
631 wxGtkPrintNativeData
*native
=
632 (wxGtkPrintNativeData
*) data
.GetNativeData();
633 data
.ConvertToNative();
635 GtkPrintSettings
* settings
= native
->GetPrintConfig();
637 // We have to restore pages to print here because they're stored in a wxPrintDialogData and ConvertToNative only works for wxPrintData.
638 int fromPage
= m_printDialogData
.GetFromPage();
639 int toPage
= m_printDialogData
.GetToPage();
640 if (m_printDialogData
.GetSelection())
641 gtk_print_settings_set_print_pages(settings
, GTK_PRINT_PAGES_CURRENT
);
642 else if (m_printDialogData
.GetAllPages())
643 gtk_print_settings_set_print_pages(settings
, GTK_PRINT_PAGES_ALL
);
645 gtk_print_settings_set_print_pages(settings
, GTK_PRINT_PAGES_RANGES
);
647 range
= g_new (GtkPageRange
, 1);
648 range
[0].start
= fromPage
-1;
649 range
[0].end
= (toPage
>= fromPage
) ? toPage
-1 : fromPage
-1;
650 gtk_print_settings_set_page_ranges (settings
, range
, 1);
653 GtkPrintOperation
* const printOp
= native
->GetPrintJob();
655 // If the settings are OK, we restore it.
656 if (settings
!= NULL
)
657 gtk_print_operation_set_print_settings (printOp
, settings
);
658 gtk_print_operation_set_default_page_setup (printOp
, native
->GetPageSetupFromSettings(settings
));
660 // Show the dialog if needed.
661 GError
* gError
= NULL
;
662 GtkPrintOperationResult response
= gtk_print_operation_run
666 ? GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
667 : GTK_PRINT_OPERATION_ACTION_PRINT
,
669 ? GTK_WINDOW(gtk_widget_get_toplevel(m_parent
->m_widget
))
674 // Does everything went well?
675 if (response
== GTK_PRINT_OPERATION_RESULT_CANCEL
)
679 else if (response
== GTK_PRINT_OPERATION_RESULT_ERROR
)
681 wxLogError(_("Error while printing: ") + wxString(gError
? gError
->message
: "???"));
682 g_error_free (gError
);
683 return wxID_NO
; // We use wxID_NO because there is no wxID_ERROR available
686 // Now get the settings and save it.
687 GtkPrintSettings
* newSettings
= gtk_print_operation_get_print_settings(printOp
);
688 native
->SetPrintConfig(newSettings
);
689 data
.ConvertFromNative();
691 // Set PrintDialogData variables
692 m_printDialogData
.SetPrintData(data
);
693 m_printDialogData
.SetCollate(data
.GetCollate());
694 m_printDialogData
.SetNoCopies(data
.GetNoCopies());
695 m_printDialogData
.SetPrintToFile(data
.GetPrinterName() == "Print to File");
697 // Same problem as a few lines before.
698 switch (gtk_print_settings_get_print_pages(newSettings
))
700 case GTK_PRINT_PAGES_CURRENT
:
701 m_printDialogData
.SetSelection( true );
703 case GTK_PRINT_PAGES_RANGES
:
704 {// wxWidgets doesn't support multiple ranges, so we can only save the first one even if the user wants to print others.
705 // 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
706 // will hit "OK" button. However we can only save 1-3 in the print data.
709 range
= gtk_print_settings_get_page_ranges (newSettings
, &num_ranges
);
712 m_printDialogData
.SetFromPage( range
[0].start
);
713 m_printDialogData
.SetToPage( range
[0].end
);
716 m_printDialogData
.SetAllPages( true );
717 m_printDialogData
.SetFromPage( 0 );
718 m_printDialogData
.SetToPage( 9999 );
721 case GTK_PRINT_PAGES_ALL
:
723 m_printDialogData
.SetAllPages( true );
724 m_printDialogData
.SetFromPage( 0 );
725 m_printDialogData
.SetToPage( 9999 );
732 //----------------------------------------------------------------------------
733 // wxGtkPageSetupDialog
734 //----------------------------------------------------------------------------
736 IMPLEMENT_CLASS(wxGtkPageSetupDialog
, wxPageSetupDialogBase
)
738 wxGtkPageSetupDialog::wxGtkPageSetupDialog( wxWindow
*parent
,
739 wxPageSetupDialogData
* data
)
742 m_pageDialogData
= *data
;
747 wxGtkPageSetupDialog::~wxGtkPageSetupDialog()
751 int wxGtkPageSetupDialog::ShowModal()
753 WX_TESTING_SHOW_MODAL_HOOK();
756 m_pageDialogData
.GetPrintData().ConvertToNative();
757 wxGtkPrintNativeData
*native
= (wxGtkPrintNativeData
*) m_pageDialogData
.GetPrintData().GetNativeData();
758 GtkPrintSettings
* nativeData
= native
->GetPrintConfig();
760 // We only need the pagesetup data which are part of the settings.
761 GtkPageSetup
* oldPageSetup
= native
->GetPageSetupFromSettings(nativeData
);
763 // If the user used a custom paper format the last time he printed, we have to restore it too.
764 if (m_pageDialogData
.GetPrintData().GetPaperId() == wxPAPER_NONE
)
766 wxSize customPaperSize
= m_pageDialogData
.GetPaperSize();
767 if (customPaperSize
.GetWidth() > 0 && customPaperSize
.GetHeight() > 0)
769 wxString title
= _("Custom size");
770 GtkPaperSize
* customSize
= gtk_paper_size_new_custom ("custom", title
.mb_str(), (gdouble
) customPaperSize
.GetWidth(), (gdouble
) customPaperSize
.GetHeight(), GTK_UNIT_MM
);
771 gtk_page_setup_set_paper_size_and_default_margins (oldPageSetup
, customSize
);
772 g_object_unref(customSize
);
777 // Set selected printer
778 gtk_print_settings_set(nativeData
, "format-for-printer",
779 gtk_print_settings_get_printer(nativeData
));
781 // Create custom dialog
782 wxString
title(GetTitle());
784 title
= _("Page Setup");
786 dlg
= gtk_page_setup_unix_dialog_new(title
.utf8_str(),
788 ? GTK_WINDOW(m_parent
->m_widget
)
791 gtk_page_setup_unix_dialog_set_print_settings(
792 GTK_PAGE_SETUP_UNIX_DIALOG(dlg
), nativeData
);
793 gtk_page_setup_unix_dialog_set_page_setup(
794 GTK_PAGE_SETUP_UNIX_DIALOG(dlg
), oldPageSetup
);
796 int result
= gtk_dialog_run(GTK_DIALOG(dlg
));
797 gtk_widget_hide(dlg
);
801 case GTK_RESPONSE_OK
:
802 case GTK_RESPONSE_APPLY
:
804 // Store Selected Printer Name
805 gtk_print_settings_set_printer
808 gtk_print_settings_get(nativeData
, "format-for-printer")
811 wxGtkObject
<GtkPageSetup
>
812 newPageSetup(gtk_page_setup_unix_dialog_get_page_setup(
813 GTK_PAGE_SETUP_UNIX_DIALOG(dlg
)));
814 native
->SetPageSetupToSettings(nativeData
, newPageSetup
);
816 m_pageDialogData
.GetPrintData().ConvertFromNative();
818 // Store custom paper format if any.
819 if ( m_pageDialogData
.GetPrintData().GetPaperId() == wxPAPER_NONE
)
821 gdouble ml
,mr
,mt
,mb
,pw
,ph
;
822 ml
= gtk_page_setup_get_left_margin (newPageSetup
, GTK_UNIT_MM
);
823 mr
= gtk_page_setup_get_right_margin (newPageSetup
, GTK_UNIT_MM
);
824 mt
= gtk_page_setup_get_top_margin (newPageSetup
, GTK_UNIT_MM
);
825 mb
= gtk_page_setup_get_bottom_margin (newPageSetup
, GTK_UNIT_MM
);
827 pw
= gtk_page_setup_get_paper_width (newPageSetup
, GTK_UNIT_MM
);
828 ph
= gtk_page_setup_get_paper_height (newPageSetup
, GTK_UNIT_MM
);
830 m_pageDialogData
.SetMarginTopLeft(wxPoint((int)(ml
+0.5),
832 m_pageDialogData
.SetMarginBottomRight(wxPoint((int)(mr
+0.5),
835 m_pageDialogData
.SetPaperSize(wxSize((int)(pw
+0.5),
844 case GTK_RESPONSE_CANCEL
:
845 result
= wxID_CANCEL
;
849 gtk_widget_destroy(dlg
);
854 //----------------------------------------------------------------------------
856 //----------------------------------------------------------------------------
858 IMPLEMENT_CLASS(wxGtkPrinter
, wxPrinterBase
)
860 wxGtkPrinter::wxGtkPrinter( wxPrintDialogData
*data
) :
861 wxPrinterBase( data
)
866 m_printDialogData
= *data
;
869 wxGtkPrinter::~wxGtkPrinter()
873 bool wxGtkPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
877 sm_lastError
= wxPRINTER_ERROR
;
881 // Let's correct the PageInfo just in case the app gives wrong values.
882 int fromPage
, toPage
;
883 int minPage
, maxPage
;
884 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
885 m_printDialogData
.SetAllPages(true);
887 if (minPage
< 1) minPage
= 1;
888 if (maxPage
< 1) maxPage
= 9999;
889 if (maxPage
< minPage
) maxPage
= minPage
;
891 m_printDialogData
.SetMinPage(minPage
);
892 m_printDialogData
.SetMaxPage(maxPage
);
895 if (fromPage
< minPage
) fromPage
= minPage
;
896 else if (fromPage
> maxPage
) fromPage
= maxPage
;
897 m_printDialogData
.SetFromPage(fromPage
);
901 m_printDialogData
.SetToPage(toPage
);
902 if (toPage
> maxPage
) toPage
= maxPage
;
903 else if (toPage
< minPage
) toPage
= minPage
;
906 if (((minPage
!= fromPage
) && fromPage
!= 0) || ((maxPage
!= toPage
) && toPage
!= 0)) m_printDialogData
.SetAllPages(false);
909 wxPrintData printdata
= GetPrintDialogData().GetPrintData();
910 wxGtkPrintNativeData
*native
= (wxGtkPrintNativeData
*) printdata
.GetNativeData();
912 wxGtkObject
<GtkPrintOperation
> printOp(gtk_print_operation_new());
913 native
->SetPrintJob(printOp
);
914 wxON_BLOCK_EXIT_OBJ1(*native
, wxGtkPrintNativeData::SetPrintJob
,
915 static_cast<GtkPrintOperation
*>(NULL
));
917 wxPrinterToGtkData dataToSend
;
918 dataToSend
.printer
= this;
919 dataToSend
.printout
= printout
;
921 // These Gtk signals are caught here.
922 g_signal_connect (printOp
, "begin-print", G_CALLBACK (gtk_begin_print_callback
), &dataToSend
);
923 g_signal_connect (printOp
, "draw-page", G_CALLBACK (gtk_draw_page_print_callback
), &dataToSend
);
924 g_signal_connect (printOp
, "end-print", G_CALLBACK (gtk_end_print_callback
), printout
);
926 // This is used to setup the DC and
927 // show the dialog if desired
928 wxGtkPrintDialog
dialog( parent
, &m_printDialogData
);
929 dialog
.SetPrintDC(m_dc
);
930 dialog
.SetShowDialog(prompt
);
932 // doesn't necessarily show
933 int ret
= dialog
.ShowModal();
934 if (ret
== wxID_CANCEL
)
936 sm_lastError
= wxPRINTER_CANCELLED
;
940 sm_lastError
= wxPRINTER_ERROR
;
941 wxFAIL_MSG(_("The print dialog returned an error."));
944 return (sm_lastError
== wxPRINTER_NO_ERROR
);
947 void wxGtkPrinter::BeginPrint(wxPrintout
*printout
, GtkPrintOperation
*operation
, GtkPrintContext
*context
)
949 wxPrintData printdata
= GetPrintDialogData().GetPrintData();
950 wxGtkPrintNativeData
*native
= (wxGtkPrintNativeData
*) printdata
.GetNativeData();
952 // We need to update printdata with the new data from the dialog and we
953 // have to do this here because this method needs this new data and we
954 // cannot update it earlier
955 native
->SetPrintConfig(gtk_print_operation_get_print_settings(operation
));
956 printdata
.ConvertFromNative();
958 SetPrintContext(context
);
959 native
->SetPrintContext( context
);
961 wxPrinterDC
*printDC
= new wxPrinterDC( printdata
);
966 if (sm_lastError
!= wxPRINTER_CANCELLED
)
968 sm_lastError
= wxPRINTER_ERROR
;
969 wxFAIL_MSG(_("The wxGtkPrinterDC cannot be used."));
974 printout
->SetPPIScreen(wxGetDisplayPPI());
975 printout
->SetPPIPrinter( printDC
->GetResolution(),
976 printDC
->GetResolution() );
978 printout
->SetDC(m_dc
);
981 m_dc
->GetSize(&w
, &h
);
982 printout
->SetPageSizePixels((int)w
, (int)h
);
983 printout
->SetPaperRectPixels(wxRect(0, 0, w
, h
));
985 m_dc
->GetSizeMM(&mw
, &mh
);
986 printout
->SetPageSizeMM((int)mw
, (int)mh
);
987 printout
->OnPreparePrinting();
989 // Get some parameters from the printout, if defined.
990 int fromPage
, toPage
;
991 int minPage
, maxPage
;
992 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
996 sm_lastError
= wxPRINTER_ERROR
;
997 wxFAIL_MSG(_("wxPrintout::GetPageInfo gives a null maxPage."));
1001 printout
->OnBeginPrinting();
1005 // If we're not previewing we need to calculate the number of pages to print.
1006 // If we're previewing, Gtk Print will render every pages without wondering about the page ranges the user may
1007 // have defined in the dialog. So the number of pages is the maximum available.
1008 if (!printout
->IsPreview())
1010 GtkPrintSettings
* settings
= gtk_print_operation_get_print_settings (operation
);
1011 switch (gtk_print_settings_get_print_pages(settings
))
1013 case GTK_PRINT_PAGES_CURRENT
:
1016 case GTK_PRINT_PAGES_RANGES
:
1017 {gint num_ranges
= 0;
1018 GtkPageRange
* range
;
1020 range
= gtk_print_settings_get_page_ranges (settings
, &num_ranges
);
1021 for (i
=0; i
<num_ranges
; i
++)
1023 if (range
[i
].end
< range
[i
].start
) range
[i
].end
= range
[i
].start
;
1024 if (range
[i
].start
< minPage
-1) range
[i
].start
= minPage
-1;
1025 if (range
[i
].end
> maxPage
-1) range
[i
].end
= maxPage
-1;
1026 if (range
[i
].start
> maxPage
-1) range
[i
].start
= maxPage
-1;
1027 numPages
+= range
[i
].end
- range
[i
].start
+ 1;
1029 gtk_print_settings_set_page_ranges (settings
, range
, 1);
1031 case GTK_PRINT_PAGES_ALL
:
1033 numPages
= maxPage
- minPage
+ 1;
1037 else numPages
= maxPage
- minPage
+ 1;
1039 gtk_print_operation_set_n_pages(operation
, numPages
);
1042 void wxGtkPrinter::DrawPage(wxPrintout
*printout
,
1043 GtkPrintOperation
*operation
,
1044 GtkPrintContext
* WXUNUSED(context
),
1047 int fromPage
, toPage
, minPage
, maxPage
, startPage
, endPage
;
1048 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
1050 int numPageToDraw
= page_nr
+ minPage
;
1051 if (numPageToDraw
< minPage
) numPageToDraw
= minPage
;
1052 if (numPageToDraw
> maxPage
) numPageToDraw
= maxPage
;
1054 GtkPrintSettings
* settings
= gtk_print_operation_get_print_settings (operation
);
1055 switch (gtk_print_settings_get_print_pages(settings
))
1057 case GTK_PRINT_PAGES_CURRENT
:
1058 g_object_get_property((GObject
*) operation
, (const gchar
*) "current-page", (GValue
*) &startPage
);
1059 g_object_get_property((GObject
*) operation
, (const gchar
*) "current-page", (GValue
*) &endPage
);
1061 case GTK_PRINT_PAGES_RANGES
:
1062 {gint num_ranges
= 0;
1063 GtkPageRange
* range
;
1064 range
= gtk_print_settings_get_page_ranges (settings
, &num_ranges
);
1065 // We don't need to verify these values as it has already been done in wxGtkPrinter::BeginPrint.
1066 if (num_ranges
>= 1)
1068 startPage
= range
[0].start
+ 1;
1069 endPage
= range
[0].end
+ 1;
1072 startPage
= minPage
;
1076 case GTK_PRINT_PAGES_ALL
:
1078 startPage
= minPage
;
1083 if(numPageToDraw
== startPage
)
1085 if (!printout
->OnBeginDocument(startPage
, endPage
))
1087 wxLogError(_("Could not start printing."));
1088 sm_lastError
= wxPRINTER_ERROR
;
1092 // The app can render the page numPageToDraw.
1093 if (printout
->HasPage(numPageToDraw
))
1096 printout
->OnPrintPage(numPageToDraw
);
1101 if(numPageToDraw
== endPage
)
1103 printout
->OnEndDocument();
1107 wxDC
* wxGtkPrinter::PrintDialog( wxWindow
*parent
)
1109 wxGtkPrintDialog
dialog( parent
, &m_printDialogData
);
1111 dialog
.SetPrintDC(m_dc
);
1112 dialog
.SetShowDialog(true);
1114 int ret
= dialog
.ShowModal();
1116 if (ret
== wxID_CANCEL
)
1118 sm_lastError
= wxPRINTER_CANCELLED
;
1123 sm_lastError
= wxPRINTER_ERROR
;
1124 wxFAIL_MSG(_("The print dialog returned an error."));
1128 m_printDialogData
= dialog
.GetPrintDialogData();
1130 return new wxPrinterDC( m_printDialogData
.GetPrintData() );
1133 bool wxGtkPrinter::Setup( wxWindow
* WXUNUSED(parent
) )
1135 // Obsolete, for backward compatibility.
1139 //-----------------------------------------------------------------------------
1141 //-----------------------------------------------------------------------------
1143 #define wxCAIRO_SCALE 1
1147 #define XLOG2DEV(x) LogicalToDeviceX(x)
1148 #define XLOG2DEVREL(x) LogicalToDeviceXRel(x)
1149 #define YLOG2DEV(x) LogicalToDeviceY(x)
1150 #define YLOG2DEVREL(x) LogicalToDeviceYRel(x)
1154 #define XLOG2DEV(x) ((double)(LogicalToDeviceX(x)) * m_DEV2PS)
1155 #define XLOG2DEVREL(x) ((double)(LogicalToDeviceXRel(x)) * m_DEV2PS)
1156 #define YLOG2DEV(x) ((double)(LogicalToDeviceY(x)) * m_DEV2PS)
1157 #define YLOG2DEVREL(x) ((double)(LogicalToDeviceYRel(x)) * m_DEV2PS)
1161 IMPLEMENT_ABSTRACT_CLASS(wxGtkPrinterDCImpl
, wxDCImpl
)
1163 wxGtkPrinterDCImpl::wxGtkPrinterDCImpl(wxPrinterDC
*owner
, const wxPrintData
& data
)
1168 wxGtkPrintNativeData
*native
=
1169 (wxGtkPrintNativeData
*) m_printData
.GetNativeData();
1171 m_gpc
= native
->GetPrintContext();
1173 // Match print quality to resolution (high = 1200dpi)
1174 m_resolution
= m_printData
.GetQuality(); // (int) gtk_print_context_get_dpi_x( m_gpc );
1175 if (m_resolution
< 0)
1176 m_resolution
= (1 << (m_resolution
+4)) *150;
1178 m_context
= gtk_print_context_create_pango_context( m_gpc
);
1179 m_layout
= gtk_print_context_create_pango_layout ( m_gpc
);
1180 m_fontdesc
= pango_font_description_from_string( "Sans 12" );
1182 m_cairo
= gtk_print_context_get_cairo_context ( m_gpc
);
1188 m_PS2DEV
= (double)m_resolution
/ 72.0;
1189 m_DEV2PS
= 72.0 / (double)m_resolution
;
1196 m_signX
= 1; // default x-axis left to right.
1197 m_signY
= 1; // default y-axis bottom up -> top down.
1200 wxGtkPrinterDCImpl::~wxGtkPrinterDCImpl()
1202 g_object_unref(m_context
);
1203 g_object_unref(m_layout
);
1206 bool wxGtkPrinterDCImpl::IsOk() const
1208 return m_gpc
!= NULL
;
1211 void* wxGtkPrinterDCImpl::GetCairoContext() const
1213 return (void*) cairo_reference( m_cairo
);
1216 void* wxGtkPrinterDCImpl::GetHandle() const
1218 return GetCairoContext();
1221 bool wxGtkPrinterDCImpl::DoFloodFill(wxCoord
WXUNUSED(x1
),
1222 wxCoord
WXUNUSED(y1
),
1223 const wxColour
& WXUNUSED(col
),
1224 wxFloodFillStyle
WXUNUSED(style
))
1226 // We can't access the given coord as a Cairo context is scalable, ie a
1227 // coord doesn't mean anything in this context.
1228 wxFAIL_MSG(_("not implemented"));
1232 void wxGtkPrinterDCImpl::DoGradientFillConcentric(const wxRect
& rect
, const wxColour
& initialColour
, const wxColour
& destColour
, const wxPoint
& circleCenter
)
1234 wxCoord xC
= circleCenter
.x
;
1235 wxCoord yC
= circleCenter
.y
;
1236 wxCoord xR
= rect
.x
;
1237 wxCoord yR
= rect
.y
;
1238 wxCoord w
= rect
.width
;
1239 wxCoord h
= rect
.height
;
1241 const double r2
= (w
/2)*(w
/2)+(h
/2)*(h
/2);
1242 double radius
= sqrt(r2
);
1244 unsigned char redI
= initialColour
.Red();
1245 unsigned char blueI
= initialColour
.Blue();
1246 unsigned char greenI
= initialColour
.Green();
1247 unsigned char alphaI
= initialColour
.Alpha();
1248 unsigned char redD
= destColour
.Red();
1249 unsigned char blueD
= destColour
.Blue();
1250 unsigned char greenD
= destColour
.Green();
1251 unsigned char alphaD
= destColour
.Alpha();
1253 double redIPS
= (double)(redI
) / 255.0;
1254 double blueIPS
= (double)(blueI
) / 255.0;
1255 double greenIPS
= (double)(greenI
) / 255.0;
1256 double alphaIPS
= (double)(alphaI
) / 255.0;
1257 double redDPS
= (double)(redD
) / 255.0;
1258 double blueDPS
= (double)(blueD
) / 255.0;
1259 double greenDPS
= (double)(greenD
) / 255.0;
1260 double alphaDPS
= (double)(alphaD
) / 255.0;
1262 // Create a pattern with the gradient.
1263 cairo_pattern_t
* gradient
;
1264 gradient
= cairo_pattern_create_radial (XLOG2DEV(xC
+xR
), YLOG2DEV(yC
+yR
), 0, XLOG2DEV(xC
+xR
), YLOG2DEV(yC
+yR
), radius
* m_DEV2PS
);
1265 cairo_pattern_add_color_stop_rgba (gradient
, 0.0, redIPS
, greenIPS
, blueIPS
, alphaIPS
);
1266 cairo_pattern_add_color_stop_rgba (gradient
, 1.0, redDPS
, greenDPS
, blueDPS
, alphaDPS
);
1268 // Fill the rectangle with this pattern.
1269 cairo_set_source(m_cairo
, gradient
);
1270 cairo_rectangle (m_cairo
, XLOG2DEV(xR
), YLOG2DEV(yR
), XLOG2DEVREL(w
), YLOG2DEVREL(h
) );
1271 cairo_fill(m_cairo
);
1273 cairo_pattern_destroy(gradient
);
1275 CalcBoundingBox(xR
, yR
);
1276 CalcBoundingBox(xR
+w
, yR
+h
);
1279 void wxGtkPrinterDCImpl::DoGradientFillLinear(const wxRect
& rect
, const wxColour
& initialColour
, const wxColour
& destColour
, wxDirection nDirection
)
1283 wxCoord w
= rect
.width
;
1284 wxCoord h
= rect
.height
;
1286 unsigned char redI
= initialColour
.Red();
1287 unsigned char blueI
= initialColour
.Blue();
1288 unsigned char greenI
= initialColour
.Green();
1289 unsigned char alphaI
= initialColour
.Alpha();
1290 unsigned char redD
= destColour
.Red();
1291 unsigned char blueD
= destColour
.Blue();
1292 unsigned char greenD
= destColour
.Green();
1293 unsigned char alphaD
= destColour
.Alpha();
1295 double redIPS
= (double)(redI
) / 255.0;
1296 double blueIPS
= (double)(blueI
) / 255.0;
1297 double greenIPS
= (double)(greenI
) / 255.0;
1298 double alphaIPS
= (double)(alphaI
) / 255.0;
1299 double redDPS
= (double)(redD
) / 255.0;
1300 double blueDPS
= (double)(blueD
) / 255.0;
1301 double greenDPS
= (double)(greenD
) / 255.0;
1302 double alphaDPS
= (double)(alphaD
) / 255.0;
1304 // Create a pattern with the gradient.
1305 cairo_pattern_t
* gradient
;
1306 gradient
= cairo_pattern_create_linear (XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x
+w
), YLOG2DEV(y
));
1308 if (nDirection
== wxWEST
)
1310 cairo_pattern_add_color_stop_rgba (gradient
, 0.0, redDPS
, greenDPS
, blueDPS
, alphaDPS
);
1311 cairo_pattern_add_color_stop_rgba (gradient
, 1.0, redIPS
, greenIPS
, blueIPS
, alphaIPS
);
1314 cairo_pattern_add_color_stop_rgba (gradient
, 0.0, redIPS
, greenIPS
, blueIPS
, alphaIPS
);
1315 cairo_pattern_add_color_stop_rgba (gradient
, 1.0, redDPS
, greenDPS
, blueDPS
, alphaDPS
);
1318 // Fill the rectangle with this pattern.
1319 cairo_set_source(m_cairo
, gradient
);
1320 cairo_rectangle (m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEVREL(w
), YLOG2DEVREL(h
) );
1321 cairo_fill(m_cairo
);
1323 cairo_pattern_destroy(gradient
);
1325 CalcBoundingBox(x
, y
);
1326 CalcBoundingBox(x
+w
, y
+h
);
1329 bool wxGtkPrinterDCImpl::DoGetPixel(wxCoord
WXUNUSED(x1
),
1330 wxCoord
WXUNUSED(y1
),
1331 wxColour
* WXUNUSED(col
)) const
1333 wxFAIL_MSG(_("not implemented"));
1337 void wxGtkPrinterDCImpl::DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
1339 if ( m_pen
.IsTransparent() )
1343 cairo_move_to ( m_cairo
, XLOG2DEV(x1
), YLOG2DEV(y1
) );
1344 cairo_line_to ( m_cairo
, XLOG2DEV(x2
), YLOG2DEV(y2
) );
1345 cairo_stroke ( m_cairo
);
1347 CalcBoundingBox( x1
, y1
);
1348 CalcBoundingBox( x2
, y2
);
1351 void wxGtkPrinterDCImpl::DoCrossHair(wxCoord x
, wxCoord y
)
1358 cairo_move_to (m_cairo
, XLOG2DEV(x
), 0);
1359 cairo_line_to (m_cairo
, XLOG2DEV(x
), YLOG2DEVREL(h
));
1360 cairo_move_to (m_cairo
, 0, YLOG2DEV(y
));
1361 cairo_line_to (m_cairo
, XLOG2DEVREL(w
), YLOG2DEV(y
));
1363 cairo_stroke (m_cairo
);
1364 CalcBoundingBox( 0, 0 );
1365 CalcBoundingBox( w
, h
);
1368 void wxGtkPrinterDCImpl::DoDrawArc(wxCoord x1
,wxCoord y1
,wxCoord x2
,wxCoord y2
,wxCoord xc
,wxCoord yc
)
1370 double dx
= x1
- xc
;
1371 double dy
= y1
- yc
;
1372 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
1374 double alpha1
, alpha2
;
1375 if (x1
== x2
&& y1
== y2
)
1383 alpha1
= alpha2
= 0.0;
1387 alpha1
= (x1
- xc
== 0) ?
1388 (y1
- yc
< 0) ? 90.0 : -90.0 :
1389 atan2(double(y1
-yc
), double(x1
-xc
)) * RAD2DEG
;
1390 alpha2
= (x2
- xc
== 0) ?
1391 (y2
- yc
< 0) ? 90.0 : -90.0 :
1392 atan2(double(y2
-yc
), double(x2
-xc
)) * RAD2DEG
;
1394 while (alpha1
<= 0) alpha1
+= 360;
1395 while (alpha2
<= 0) alpha2
+= 360; // adjust angles to be between.
1396 while (alpha1
> 360) alpha1
-= 360; // 0 and 360 degree.
1397 while (alpha2
> 360) alpha2
-= 360;
1403 cairo_new_path(m_cairo
);
1405 cairo_arc_negative ( m_cairo
, XLOG2DEV(xc
), YLOG2DEV(yc
), XLOG2DEVREL((int)radius
), alpha1
, alpha2
);
1406 cairo_line_to(m_cairo
, XLOG2DEV(xc
), YLOG2DEV(yc
));
1407 cairo_close_path (m_cairo
);
1409 SetBrush( m_brush
);
1410 cairo_fill_preserve( m_cairo
);
1413 cairo_stroke( m_cairo
);
1415 CalcBoundingBox (x1
, y1
);
1416 CalcBoundingBox (xc
, yc
);
1417 CalcBoundingBox (x2
, y2
);
1420 void wxGtkPrinterDCImpl::DoDrawEllipticArc(wxCoord x
,wxCoord y
,wxCoord w
,wxCoord h
,double sa
,double ea
)
1422 cairo_save( m_cairo
);
1424 cairo_new_path(m_cairo
);
1426 cairo_translate( m_cairo
, XLOG2DEV((wxCoord
) (x
+ w
/ 2.)), XLOG2DEV((wxCoord
) (y
+ h
/ 2.)) );
1427 double scale
= (double)YLOG2DEVREL(h
) / (double) XLOG2DEVREL(w
);
1428 cairo_scale( m_cairo
, 1.0, scale
);
1430 cairo_arc_negative ( m_cairo
, 0, 0, XLOG2DEVREL(w
/2), -sa
*DEG2RAD
, -ea
*DEG2RAD
);
1433 cairo_stroke_preserve( m_cairo
);
1435 cairo_line_to(m_cairo
, 0,0);
1437 SetBrush( m_brush
);
1438 cairo_fill( m_cairo
);
1440 cairo_restore( m_cairo
);
1442 CalcBoundingBox( x
, y
);
1443 CalcBoundingBox( x
+w
, y
+h
);
1446 void wxGtkPrinterDCImpl::DoDrawPoint(wxCoord x
, wxCoord y
)
1448 if ( m_pen
.IsTransparent() )
1453 cairo_move_to ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
) );
1454 cairo_line_to ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
) );
1455 cairo_stroke ( m_cairo
);
1457 CalcBoundingBox( x
, y
);
1460 void wxGtkPrinterDCImpl::DoDrawLines(int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
1462 if ( m_pen
.IsTransparent() )
1471 for ( i
=0; i
<n
; i
++ )
1472 CalcBoundingBox( points
[i
].x
+xoffset
, points
[i
].y
+yoffset
);
1474 cairo_move_to ( m_cairo
, XLOG2DEV(points
[0].x
+xoffset
), YLOG2DEV(points
[0].y
+yoffset
) );
1476 for (i
= 1; i
< n
; i
++)
1477 cairo_line_to ( m_cairo
, XLOG2DEV(points
[i
].x
+xoffset
), YLOG2DEV(points
[i
].y
+yoffset
) );
1479 cairo_stroke ( m_cairo
);
1482 void wxGtkPrinterDCImpl::DoDrawPolygon(int n
, wxPoint points
[],
1483 wxCoord xoffset
, wxCoord yoffset
,
1484 wxPolygonFillMode fillStyle
)
1488 cairo_save(m_cairo
);
1489 if (fillStyle
== wxWINDING_RULE
)
1490 cairo_set_fill_rule( m_cairo
, CAIRO_FILL_RULE_WINDING
);
1492 cairo_set_fill_rule( m_cairo
, CAIRO_FILL_RULE_EVEN_ODD
);
1494 int x
= points
[0].x
+ xoffset
;
1495 int y
= points
[0].y
+ yoffset
;
1496 cairo_new_path(m_cairo
);
1497 cairo_move_to( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
) );
1499 for (i
= 1; i
< n
; i
++)
1501 int xx
= points
[i
].x
+ xoffset
;
1502 int yy
= points
[i
].y
+ yoffset
;
1503 cairo_line_to( m_cairo
, XLOG2DEV(xx
), YLOG2DEV(yy
) );
1505 cairo_close_path(m_cairo
);
1507 SetBrush( m_brush
);
1508 cairo_fill_preserve( m_cairo
);
1511 cairo_stroke( m_cairo
);
1513 CalcBoundingBox( x
, y
);
1515 cairo_restore(m_cairo
);
1518 void wxGtkPrinterDCImpl::DoDrawPolyPolygon(int n
, int count
[], wxPoint points
[],
1519 wxCoord xoffset
, wxCoord yoffset
,
1520 wxPolygonFillMode fillStyle
)
1522 wxDCImpl::DoDrawPolyPolygon( n
, count
, points
, xoffset
, yoffset
, fillStyle
);
1525 void wxGtkPrinterDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1530 cairo_new_path(m_cairo
);
1531 cairo_rectangle ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEVREL(width
), YLOG2DEVREL(height
));
1533 SetBrush( m_brush
);
1534 cairo_fill_preserve( m_cairo
);
1537 cairo_stroke( m_cairo
);
1539 CalcBoundingBox( x
, y
);
1540 CalcBoundingBox( x
+ width
, y
+ height
);
1543 void wxGtkPrinterDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
1548 if (radius
< 0.0) radius
= - radius
* ((width
< height
) ? width
: height
);
1550 wxCoord dd
= 2 * (wxCoord
) radius
;
1551 if (dd
> width
) dd
= width
;
1552 if (dd
> height
) dd
= height
;
1555 wxCoord rad
= (wxCoord
) radius
;
1557 cairo_new_path(m_cairo
);
1558 cairo_move_to(m_cairo
,XLOG2DEV(x
+ rad
),YLOG2DEV(y
));
1559 cairo_curve_to(m_cairo
,
1560 XLOG2DEV(x
+ rad
),YLOG2DEV(y
),
1561 XLOG2DEV(x
),YLOG2DEV(y
),
1562 XLOG2DEV(x
),YLOG2DEV(y
+ rad
));
1563 cairo_line_to(m_cairo
,XLOG2DEV(x
),YLOG2DEV(y
+ height
- rad
));
1564 cairo_curve_to(m_cairo
,
1565 XLOG2DEV(x
),YLOG2DEV(y
+ height
- rad
),
1566 XLOG2DEV(x
),YLOG2DEV(y
+ height
),
1567 XLOG2DEV(x
+ rad
),YLOG2DEV(y
+ height
));
1568 cairo_line_to(m_cairo
,XLOG2DEV(x
+ width
- rad
),YLOG2DEV(y
+ height
));
1569 cairo_curve_to(m_cairo
,
1570 XLOG2DEV(x
+ width
- rad
),YLOG2DEV(y
+ height
),
1571 XLOG2DEV(x
+ width
),YLOG2DEV(y
+ height
),
1572 XLOG2DEV(x
+ width
),YLOG2DEV(y
+ height
- rad
));
1573 cairo_line_to(m_cairo
,XLOG2DEV(x
+ width
),YLOG2DEV(y
+ rad
));
1574 cairo_curve_to(m_cairo
,
1575 XLOG2DEV(x
+ width
),YLOG2DEV(y
+ rad
),
1576 XLOG2DEV(x
+ width
),YLOG2DEV(y
),
1577 XLOG2DEV(x
+ width
- rad
),YLOG2DEV(y
));
1578 cairo_line_to(m_cairo
,XLOG2DEV(x
+ rad
),YLOG2DEV(y
));
1579 cairo_close_path(m_cairo
);
1582 cairo_fill_preserve(m_cairo
);
1585 cairo_stroke(m_cairo
);
1587 CalcBoundingBox(x
,y
);
1588 CalcBoundingBox(x
+width
,y
+height
);
1591 void wxGtkPrinterDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1596 cairo_save (m_cairo
);
1598 cairo_new_path(m_cairo
);
1600 cairo_translate (m_cairo
, XLOG2DEV((wxCoord
) (x
+ width
/ 2.)), YLOG2DEV((wxCoord
) (y
+ height
/ 2.)));
1601 cairo_scale(m_cairo
, 1, (double)YLOG2DEVREL(height
)/(double)XLOG2DEVREL(width
));
1602 cairo_arc ( m_cairo
, 0, 0, XLOG2DEVREL(width
/2), 0, 2 * M_PI
);
1604 SetBrush( m_brush
);
1605 cairo_fill_preserve( m_cairo
);
1608 cairo_stroke( m_cairo
);
1610 CalcBoundingBox( x
, y
);
1611 CalcBoundingBox( x
+ width
, y
+ height
);
1613 cairo_restore (m_cairo
);
1617 void wxGtkPrinterDCImpl::DoDrawSpline(const wxPointList
*points
)
1621 double c
, d
, x1
, y1
, x2
, y2
, x3
, y3
;
1624 wxPointList::compatibility_iterator node
= points
->GetFirst();
1625 p
= node
->GetData();
1629 node
= node
->GetNext();
1630 p
= node
->GetData();
1634 (double)(x1
+ c
) / 2;
1636 (double)(y1
+ d
) / 2;
1638 cairo_new_path( m_cairo
);
1639 cairo_move_to( m_cairo
, XLOG2DEV((wxCoord
)x1
), YLOG2DEV((wxCoord
)y1
) );
1640 cairo_line_to( m_cairo
, XLOG2DEV((wxCoord
)x3
), YLOG2DEV((wxCoord
)y3
) );
1642 CalcBoundingBox( (wxCoord
)x1
, (wxCoord
)y1
);
1643 CalcBoundingBox( (wxCoord
)x3
, (wxCoord
)y3
);
1645 node
= node
->GetNext();
1648 q
= node
->GetData();
1656 x3
= (double)(x2
+ c
) / 2;
1657 y3
= (double)(y2
+ d
) / 2;
1659 cairo_curve_to(m_cairo
,
1660 XLOG2DEV((wxCoord
)x1
), YLOG2DEV((wxCoord
)y1
),
1661 XLOG2DEV((wxCoord
)x2
), YLOG2DEV((wxCoord
)y2
),
1662 XLOG2DEV((wxCoord
)x3
), YLOG2DEV((wxCoord
)y3
) );
1664 CalcBoundingBox( (wxCoord
)x1
, (wxCoord
)y1
);
1665 CalcBoundingBox( (wxCoord
)x3
, (wxCoord
)y3
);
1667 node
= node
->GetNext();
1670 cairo_line_to ( m_cairo
, XLOG2DEV((wxCoord
)c
), YLOG2DEV((wxCoord
)d
) );
1672 cairo_stroke( m_cairo
);
1674 #endif // wxUSE_SPLINES
1676 bool wxGtkPrinterDCImpl::DoBlit(wxCoord xdest
, wxCoord ydest
,
1677 wxCoord width
, wxCoord height
,
1678 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
1679 wxRasterOperationMode rop
, bool useMask
,
1680 wxCoord
WXUNUSED_UNLESS_DEBUG(xsrcMask
),
1681 wxCoord
WXUNUSED_UNLESS_DEBUG(ysrcMask
))
1683 wxASSERT_MSG( xsrcMask
== wxDefaultCoord
&& ysrcMask
== wxDefaultCoord
,
1684 wxT("mask coordinates are not supported") );
1686 wxCHECK_MSG( source
, false, wxT("invalid source dc") );
1688 // Blit into a bitmap.
1689 wxBitmap
bitmap( width
, height
);
1691 memDC
.SelectObject(bitmap
);
1692 memDC
.Blit(0, 0, width
, height
, source
, xsrc
, ysrc
, rop
);
1693 memDC
.SelectObject(wxNullBitmap
);
1695 // Draw bitmap. scaling and positioning is done there.
1696 GetOwner()->DrawBitmap( bitmap
, xdest
, ydest
, useMask
);
1701 void wxGtkPrinterDCImpl::DoDrawIcon( const wxIcon
& icon
, wxCoord x
, wxCoord y
)
1703 DoDrawBitmap( icon
, x
, y
, true );
1706 void wxGtkPrinterDCImpl::DoDrawBitmap( const wxBitmap
& bitmap
, wxCoord x
, wxCoord y
, bool useMask
)
1708 wxCHECK_RET( bitmap
.IsOk(), wxT("Invalid bitmap in wxGtkPrinterDCImpl::DoDrawBitmap"));
1710 x
= wxCoord(XLOG2DEV(x
));
1711 y
= wxCoord(YLOG2DEV(y
));
1712 int bw
= bitmap
.GetWidth();
1713 int bh
= bitmap
.GetHeight();
1715 wxBitmap bmpSource
= bitmap
; // we need a non-const instance.
1716 if (!useMask
&& !bitmap
.HasPixbuf() && bitmap
.GetMask())
1717 bmpSource
.SetMask(NULL
);
1720 cairo_save(m_cairo
);
1722 // Prepare to draw the image.
1723 cairo_translate(m_cairo
, x
, y
);
1726 wxDouble scaleX
= (wxDouble
) XLOG2DEVREL(bw
) / (wxDouble
) bw
;
1727 wxDouble scaleY
= (wxDouble
) YLOG2DEVREL(bh
) / (wxDouble
) bh
;
1728 cairo_scale(m_cairo
, scaleX
, scaleY
);
1731 bitmap
.Draw(m_cairo
, 0, 0, useMask
, &m_textForegroundColour
, &m_textBackgroundColour
);
1733 gdk_cairo_set_source_pixbuf(m_cairo
, bmpSource
.GetPixbuf(), 0, 0);
1734 cairo_pattern_set_filter(cairo_get_source(m_cairo
), CAIRO_FILTER_NEAREST
);
1735 // Use the original size here since the context is scaled already.
1736 cairo_rectangle(m_cairo
, 0, 0, bw
, bh
);
1737 // Fill the rectangle using the pattern.
1738 cairo_fill(m_cairo
);
1741 CalcBoundingBox(0,0);
1742 CalcBoundingBox(bw
,bh
);
1744 cairo_restore(m_cairo
);
1747 void wxGtkPrinterDCImpl::DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
)
1749 DoDrawRotatedText( text
, x
, y
, 0.0 );
1752 void wxGtkPrinterDCImpl::DoDrawRotatedText(const wxString
& text
, wxCoord x
, wxCoord y
, double angle
)
1754 double xx
= XLOG2DEV(x
);
1755 double yy
= YLOG2DEV(y
);
1759 const wxScopedCharBuffer data
= text
.utf8_str();
1761 pango_layout_set_text(m_layout
, data
, data
.length());
1763 const bool setAttrs
= m_font
.GTKSetPangoAttrs(m_layout
);
1764 if (m_textForegroundColour
.IsOk())
1766 unsigned char red
= m_textForegroundColour
.Red();
1767 unsigned char blue
= m_textForegroundColour
.Blue();
1768 unsigned char green
= m_textForegroundColour
.Green();
1769 unsigned char alpha
= m_textForegroundColour
.Alpha();
1771 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
&& alpha
== m_currentAlpha
))
1773 double redPS
= (double)(red
) / 255.0;
1774 double bluePS
= (double)(blue
) / 255.0;
1775 double greenPS
= (double)(green
) / 255.0;
1776 double alphaPS
= (double)(alpha
) / 255.0;
1778 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1781 m_currentBlue
= blue
;
1782 m_currentGreen
= green
;
1783 m_currentAlpha
= alpha
;
1788 cairo_move_to (m_cairo
, xx
, yy
);
1790 cairo_save( m_cairo
);
1792 if (fabs(angle
) > 0.00001)
1793 cairo_rotate( m_cairo
, angle
*DEG2RAD
);
1795 cairo_scale(m_cairo
, m_scaleX
, m_scaleY
);
1798 pango_layout_get_pixel_size( m_layout
, &w
, &h
);
1800 if ( m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1802 unsigned char red
= m_textBackgroundColour
.Red();
1803 unsigned char blue
= m_textBackgroundColour
.Blue();
1804 unsigned char green
= m_textBackgroundColour
.Green();
1805 unsigned char alpha
= m_textBackgroundColour
.Alpha();
1807 double redPS
= (double)(red
) / 255.0;
1808 double bluePS
= (double)(blue
) / 255.0;
1809 double greenPS
= (double)(green
) / 255.0;
1810 double alphaPS
= (double)(alpha
) / 255.0;
1812 cairo_save(m_cairo
);
1813 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1814 cairo_rectangle(m_cairo
, 0, 0, w
, h
); // still in cairo units
1815 cairo_fill(m_cairo
);
1816 cairo_restore(m_cairo
);
1819 pango_cairo_update_layout (m_cairo
, m_layout
);
1820 pango_cairo_show_layout (m_cairo
, m_layout
);
1822 cairo_restore( m_cairo
);
1826 // Undo underline attributes setting
1827 pango_layout_set_attributes(m_layout
, NULL
);
1830 // Back to device units:
1831 CalcBoundingBox (x
, y
);
1832 CalcBoundingBox (x
+ w
, y
+ h
);
1835 void wxGtkPrinterDCImpl::Clear()
1837 // Clear does nothing for printing, but keep the code
1840 cairo_save(m_cairo);
1841 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
1842 SetBrush(m_backgroundBrush);
1843 cairo_paint(m_cairo);
1844 cairo_restore(m_cairo);
1848 void wxGtkPrinterDCImpl::SetFont( const wxFont
& font
)
1855 pango_font_description_free( m_fontdesc
);
1857 m_fontdesc
= pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
1859 float size
= pango_font_description_get_size( m_fontdesc
);
1860 size
= size
* GetFontPointSizeAdjustment(72.0);
1861 pango_font_description_set_size( m_fontdesc
, (gint
)size
);
1863 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1867 void wxGtkPrinterDCImpl::SetPen( const wxPen
& pen
)
1869 if (!pen
.IsOk()) return;
1875 if (m_pen
.GetWidth() <= 0)
1878 width
= (double) m_pen
.GetWidth();
1880 cairo_set_line_width( m_cairo
, width
* m_DEV2PS
* m_scaleX
);
1881 static const double dotted
[] = {2.0, 5.0};
1882 static const double short_dashed
[] = {4.0, 4.0};
1883 static const double long_dashed
[] = {4.0, 8.0};
1884 static const double dotted_dashed
[] = {6.0, 6.0, 2.0, 6.0};
1886 switch (m_pen
.GetStyle())
1888 case wxPENSTYLE_DOT
: cairo_set_dash( m_cairo
, dotted
, 2, 0 ); break;
1889 case wxPENSTYLE_SHORT_DASH
: cairo_set_dash( m_cairo
, short_dashed
, 2, 0 ); break;
1890 case wxPENSTYLE_LONG_DASH
: cairo_set_dash( m_cairo
, long_dashed
, 2, 0 ); break;
1891 case wxPENSTYLE_DOT_DASH
: cairo_set_dash( m_cairo
, dotted_dashed
, 4, 0 ); break;
1892 case wxPENSTYLE_USER_DASH
:
1895 int num
= m_pen
.GetDashes (&wx_dashes
);
1896 gdouble
*g_dashes
= g_new( gdouble
, num
);
1898 for (i
= 0; i
< num
; ++i
)
1899 g_dashes
[i
] = (gdouble
) wx_dashes
[i
];
1900 cairo_set_dash( m_cairo
, g_dashes
, num
, 0);
1904 case wxPENSTYLE_SOLID
:
1905 case wxPENSTYLE_TRANSPARENT
:
1906 default: cairo_set_dash( m_cairo
, NULL
, 0, 0 ); break;
1909 switch (m_pen
.GetCap())
1911 case wxCAP_PROJECTING
: cairo_set_line_cap (m_cairo
, CAIRO_LINE_CAP_SQUARE
); break;
1912 case wxCAP_BUTT
: cairo_set_line_cap (m_cairo
, CAIRO_LINE_CAP_BUTT
); break;
1914 default: cairo_set_line_cap (m_cairo
, CAIRO_LINE_CAP_ROUND
); break;
1917 switch (m_pen
.GetJoin())
1919 case wxJOIN_BEVEL
: cairo_set_line_join (m_cairo
, CAIRO_LINE_JOIN_BEVEL
); break;
1920 case wxJOIN_MITER
: cairo_set_line_join (m_cairo
, CAIRO_LINE_JOIN_MITER
); break;
1922 default: cairo_set_line_join (m_cairo
, CAIRO_LINE_JOIN_ROUND
); break;
1925 unsigned char red
= m_pen
.GetColour().Red();
1926 unsigned char blue
= m_pen
.GetColour().Blue();
1927 unsigned char green
= m_pen
.GetColour().Green();
1928 unsigned char alpha
= m_pen
.GetColour().Alpha();
1930 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
&& alpha
== m_currentAlpha
))
1932 double redPS
= (double)(red
) / 255.0;
1933 double bluePS
= (double)(blue
) / 255.0;
1934 double greenPS
= (double)(green
) / 255.0;
1935 double alphaPS
= (double)(alpha
) / 255.0;
1937 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1940 m_currentBlue
= blue
;
1941 m_currentGreen
= green
;
1942 m_currentAlpha
= alpha
;
1946 void wxGtkPrinterDCImpl::SetBrush( const wxBrush
& brush
)
1948 if (!brush
.IsOk()) return;
1952 if (m_brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
1954 cairo_set_source_rgba( m_cairo
, 0, 0, 0, 0 );
1963 unsigned char red
= m_brush
.GetColour().Red();
1964 unsigned char blue
= m_brush
.GetColour().Blue();
1965 unsigned char green
= m_brush
.GetColour().Green();
1966 unsigned char alpha
= m_brush
.GetColour().Alpha();
1968 double redPS
= (double)(red
) / 255.0;
1969 double bluePS
= (double)(blue
) / 255.0;
1970 double greenPS
= (double)(green
) / 255.0;
1971 double alphaPS
= (double)(alpha
) / 255.0;
1973 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
&& alpha
== m_currentAlpha
))
1975 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1978 m_currentBlue
= blue
;
1979 m_currentGreen
= green
;
1980 m_currentAlpha
= alpha
;
1983 if (m_brush
.IsHatch())
1986 cairo_surface_t
*surface
;
1987 surface
= cairo_surface_create_similar(cairo_get_target(m_cairo
),CAIRO_CONTENT_COLOR_ALPHA
,10,10);
1988 cr
= cairo_create(surface
);
1989 cairo_set_line_cap(cr
, CAIRO_LINE_CAP_SQUARE
);
1990 cairo_set_line_width(cr
, 1);
1991 cairo_set_line_join(cr
,CAIRO_LINE_JOIN_MITER
);
1993 switch (m_brush
.GetStyle())
1995 case wxBRUSHSTYLE_CROSS_HATCH
:
1996 cairo_move_to(cr
, 5, 0);
1997 cairo_line_to(cr
, 5, 10);
1998 cairo_move_to(cr
, 0, 5);
1999 cairo_line_to(cr
, 10, 5);
2001 case wxBRUSHSTYLE_BDIAGONAL_HATCH
:
2002 cairo_move_to(cr
, 0, 10);
2003 cairo_line_to(cr
, 10, 0);
2005 case wxBRUSHSTYLE_FDIAGONAL_HATCH
:
2006 cairo_move_to(cr
, 0, 0);
2007 cairo_line_to(cr
, 10, 10);
2009 case wxBRUSHSTYLE_CROSSDIAG_HATCH
:
2010 cairo_move_to(cr
, 0, 0);
2011 cairo_line_to(cr
, 10, 10);
2012 cairo_move_to(cr
, 10, 0);
2013 cairo_line_to(cr
, 0, 10);
2015 case wxBRUSHSTYLE_HORIZONTAL_HATCH
:
2016 cairo_move_to(cr
, 0, 5);
2017 cairo_line_to(cr
, 10, 5);
2019 case wxBRUSHSTYLE_VERTICAL_HATCH
:
2020 cairo_move_to(cr
, 5, 0);
2021 cairo_line_to(cr
, 5, 10);
2024 wxFAIL_MSG(_("Couldn't get hatch style from wxBrush."));
2027 cairo_set_source_rgba(cr
, redPS
, greenPS
, bluePS
, alphaPS
);
2031 cairo_pattern_t
* pattern
= cairo_pattern_create_for_surface (surface
);
2032 cairo_surface_destroy(surface
);
2033 cairo_pattern_set_extend (pattern
, CAIRO_EXTEND_REPEAT
);
2034 cairo_set_source(m_cairo
, pattern
);
2035 cairo_pattern_destroy(pattern
);
2039 void wxGtkPrinterDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
2041 if (function
== wxCLEAR
)
2042 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_CLEAR
);
2043 else if (function
== wxOR
)
2044 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_OUT
);
2045 else if (function
== wxNO_OP
)
2046 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_DEST
);
2047 else if (function
== wxAND
)
2048 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_ADD
);
2049 else if (function
== wxSET
)
2050 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_SATURATE
);
2051 else if (function
== wxXOR
)
2052 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_XOR
);
2053 else // wxCOPY or anything else.
2054 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_SOURCE
);
2057 void wxGtkPrinterDCImpl::SetBackground( const wxBrush
& brush
)
2059 m_backgroundBrush
= brush
;
2060 cairo_save(m_cairo
);
2061 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_DEST_OVER
);
2063 SetBrush(m_backgroundBrush
);
2064 cairo_paint(m_cairo
);
2065 cairo_restore(m_cairo
);
2068 void wxGtkPrinterDCImpl::SetBackgroundMode(int mode
)
2070 if (mode
== wxBRUSHSTYLE_SOLID
)
2071 m_backgroundMode
= wxBRUSHSTYLE_SOLID
;
2073 m_backgroundMode
= wxBRUSHSTYLE_TRANSPARENT
;
2076 void wxGtkPrinterDCImpl::DoSetClippingRegion(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
2078 cairo_rectangle ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEVREL(width
), YLOG2DEVREL(height
));
2079 cairo_clip(m_cairo
);
2081 wxDCImpl::DoSetClippingRegion(x
, y
, width
, height
);
2084 void wxGtkPrinterDCImpl::DestroyClippingRegion()
2086 cairo_reset_clip(m_cairo
);
2089 bool wxGtkPrinterDCImpl::StartDoc(const wxString
& WXUNUSED(message
))
2094 void wxGtkPrinterDCImpl::EndDoc()
2099 void wxGtkPrinterDCImpl::StartPage()
2101 // Notice that we may change the Cairo transformation matrix only here and
2102 // not before (e.g. in wxGtkPrinterDCImpl ctor as we used to do) in order
2103 // to not affect _gtk_print_context_rotate_according_to_orientation() which
2104 // is used in GTK+ itself and wouldn't work correctly if we applied these
2105 // transformations before it is called.
2107 // By default the origin of the Cairo context is in the upper left
2108 // corner of the printable area. We need to translate it so that it
2109 // is in the upper left corner of the paper (without margins)
2110 GtkPageSetup
*setup
= gtk_print_context_get_page_setup( m_gpc
);
2112 ml
= gtk_page_setup_get_left_margin (setup
, GTK_UNIT_POINTS
);
2113 mt
= gtk_page_setup_get_top_margin (setup
, GTK_UNIT_POINTS
);
2114 cairo_translate(m_cairo
, -ml
, -mt
);
2117 cairo_scale( m_cairo
, 72.0 / (double)m_resolution
, 72.0 / (double)m_resolution
);
2121 void wxGtkPrinterDCImpl::EndPage()
2126 wxCoord
wxGtkPrinterDCImpl::GetCharHeight() const
2128 pango_layout_set_text( m_layout
, "H", 1 );
2131 pango_layout_get_pixel_size( m_layout
, &w
, &h
);
2133 return wxRound( h
* m_PS2DEV
);
2136 wxCoord
wxGtkPrinterDCImpl::GetCharWidth() const
2138 pango_layout_set_text( m_layout
, "H", 1 );
2141 pango_layout_get_pixel_size( m_layout
, &w
, &h
);
2143 return wxRound( w
* m_PS2DEV
);
2146 void wxGtkPrinterDCImpl::DoGetTextExtent(const wxString
& string
, wxCoord
*width
, wxCoord
*height
,
2148 wxCoord
*externalLeading
,
2149 const wxFont
*theFont
) const
2157 if ( externalLeading
)
2158 *externalLeading
= 0;
2165 cairo_save( m_cairo
);
2166 cairo_scale(m_cairo
, m_scaleX
, m_scaleY
);
2168 // Set layout's text
2169 const wxScopedCharBuffer dataUTF8
= string
.utf8_str();
2174 // scale the font and apply it
2175 PangoFontDescription
*desc
= theFont
->GetNativeFontInfo()->description
;
2176 oldSize
= pango_font_description_get_size(desc
);
2177 const float size
= oldSize
* GetFontPointSizeAdjustment(72.0);
2178 pango_font_description_set_size(desc
, (gint
)size
);
2180 pango_layout_set_font_description(m_layout
, desc
);
2183 pango_layout_set_text( m_layout
, dataUTF8
, strlen(dataUTF8
) );
2186 pango_layout_get_pixel_size( m_layout
, width
, &h
);
2192 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
2193 int baseline
= pango_layout_iter_get_baseline(iter
);
2194 pango_layout_iter_free(iter
);
2195 *descent
= h
- PANGO_PIXELS(baseline
);
2200 // restore font and reset font's size back
2201 pango_layout_set_font_description(m_layout
, m_fontdesc
);
2203 PangoFontDescription
*desc
= theFont
->GetNativeFontInfo()->description
;
2204 pango_font_description_set_size(desc
, oldSize
);
2207 cairo_restore( m_cairo
);
2210 void wxGtkPrinterDCImpl::DoGetSize(int* width
, int* height
) const
2212 GtkPageSetup
*setup
= gtk_print_context_get_page_setup( m_gpc
);
2215 *width
= wxRound( (double)gtk_page_setup_get_paper_width( setup
, GTK_UNIT_POINTS
) * (double)m_resolution
/ 72.0 );
2217 *height
= wxRound( (double)gtk_page_setup_get_paper_height( setup
, GTK_UNIT_POINTS
) * (double)m_resolution
/ 72.0 );
2220 void wxGtkPrinterDCImpl::DoGetSizeMM(int *width
, int *height
) const
2222 GtkPageSetup
*setup
= gtk_print_context_get_page_setup( m_gpc
);
2225 *width
= wxRound( gtk_page_setup_get_paper_width( setup
, GTK_UNIT_MM
) );
2227 *height
= wxRound( gtk_page_setup_get_paper_height( setup
, GTK_UNIT_MM
) );
2230 wxSize
wxGtkPrinterDCImpl::GetPPI() const
2232 return wxSize( (int)m_resolution
, (int)m_resolution
);
2235 void wxGtkPrinterDCImpl::SetPrintData(const wxPrintData
& data
)
2240 // overridden for wxPrinterDC Impl
2242 wxRect
wxGtkPrinterDCImpl::GetPaperRect() const
2244 // Does GtkPrint support printer margins?
2247 DoGetSize( &w
, &h
);
2248 return wxRect( 0,0,w
,h
);
2251 int wxGtkPrinterDCImpl::GetResolution() const
2253 return m_resolution
;
2256 // ----------------------------------------------------------------------------
2258 // ----------------------------------------------------------------------------
2260 IMPLEMENT_CLASS(wxGtkPrintPreview
, wxPrintPreviewBase
)
2262 void wxGtkPrintPreview::Init(wxPrintout
* WXUNUSED(printout
),
2263 wxPrintout
* WXUNUSED(printoutForPrinting
),
2266 // convert wxPrintQuality to resolution (input pointer can be NULL)
2267 wxPrintQuality quality
= data
? data
->GetQuality() : wxPRINT_QUALITY_MEDIUM
;
2270 case wxPRINT_QUALITY_HIGH
:
2271 m_resolution
= 1200;
2274 case wxPRINT_QUALITY_LOW
:
2278 case wxPRINT_QUALITY_DRAFT
:
2285 // positive values directly indicate print resolution
2286 m_resolution
= quality
;
2290 wxFAIL_MSG( "unknown print quality" );
2293 case wxPRINT_QUALITY_MEDIUM
:
2302 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout
*printout
,
2303 wxPrintout
*printoutForPrinting
,
2304 wxPrintDialogData
*data
)
2305 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
2307 Init(printout
, printoutForPrinting
, data
? &data
->GetPrintData() : NULL
);
2310 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout
*printout
,
2311 wxPrintout
*printoutForPrinting
,
2313 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
2315 Init(printout
, printoutForPrinting
, data
);
2318 wxGtkPrintPreview::~wxGtkPrintPreview()
2322 bool wxGtkPrintPreview::Print(bool interactive
)
2324 if (!m_printPrintout
)
2327 wxPrinter
printer(& m_printDialogData
);
2328 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
2331 void wxGtkPrintPreview::DetermineScaling()
2333 wxPaperSize paperType
= m_printDialogData
.GetPrintData().GetPaperId();
2335 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
2337 paper
= wxThePrintPaperDatabase
->FindPaperType(wxPAPER_A4
);
2341 const wxSize screenPPI
= wxGetDisplayPPI();
2342 int logPPIScreenX
= screenPPI
.GetWidth();
2343 int logPPIScreenY
= screenPPI
.GetHeight();
2344 int logPPIPrinterX
= m_resolution
;
2345 int logPPIPrinterY
= m_resolution
;
2347 m_previewPrintout
->SetPPIScreen( logPPIScreenX
, logPPIScreenY
);
2348 m_previewPrintout
->SetPPIPrinter( logPPIPrinterX
, logPPIPrinterY
);
2350 // Get width and height in points (1/72th of an inch)
2351 wxSize
sizeDevUnits(paper
->GetSizeDeviceUnits());
2352 sizeDevUnits
.x
= wxRound((double)sizeDevUnits
.x
* (double)m_resolution
/ 72.0);
2353 sizeDevUnits
.y
= wxRound((double)sizeDevUnits
.y
* (double)m_resolution
/ 72.0);
2355 wxSize
sizeTenthsMM(paper
->GetSize());
2356 wxSize
sizeMM(sizeTenthsMM
.x
/ 10, sizeTenthsMM
.y
/ 10);
2358 // If in landscape mode, we need to swap the width and height.
2359 if ( m_printDialogData
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
2361 m_pageWidth
= sizeDevUnits
.y
;
2362 m_pageHeight
= sizeDevUnits
.x
;
2363 m_previewPrintout
->SetPageSizeMM(sizeMM
.y
, sizeMM
.x
);
2367 m_pageWidth
= sizeDevUnits
.x
;
2368 m_pageHeight
= sizeDevUnits
.y
;
2369 m_previewPrintout
->SetPageSizeMM(sizeMM
.x
, sizeMM
.y
);
2371 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
2372 m_previewPrintout
->SetPaperRectPixels(wxRect(0, 0, m_pageWidth
, m_pageHeight
));
2374 // At 100%, the page should look about page-size on the screen.
2375 m_previewScaleX
= float(logPPIScreenX
) / logPPIPrinterX
;
2376 m_previewScaleY
= float(logPPIScreenY
) / logPPIPrinterY
;