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"
39 #if GTK_CHECK_VERSION(2,14,0)
40 #include <gtk/gtkunixprint.h>
42 #include <gtk/gtkpagesetupunixdialog.h>
46 #if wxUSE_GRAPHICS_CONTEXT
47 #include "wx/graphics.h"
51 wxFORCE_LINK_THIS_MODULE(gtk_print
)
53 #if wxUSE_LIBGNOMEPRINT
54 #include "wx/gtk/gnome/gprint.h"
57 #include "wx/gtk/private/object.h"
59 // Useful to convert angles from/to Rad to/from Deg.
60 static const double RAD2DEG
= 180.0 / M_PI
;
61 static const double DEG2RAD
= M_PI
/ 180.0;
63 //----------------------------------------------------------------------------
65 // Initialized when starting the app : if it successfully load the gtk-print framework,
66 // it uses it. If not, it falls back to gnome print (see /gtk/gnome/gprint.cpp) then
67 // to postscript if gnomeprint is not available.
68 //----------------------------------------------------------------------------
70 class wxGtkPrintModule
: public wxModule
75 #if wxUSE_LIBGNOMEPRINT
76 // This module must be initialized AFTER gnomeprint's one
77 AddDependency(wxCLASSINFO(wxGnomePrintModule
));
84 DECLARE_DYNAMIC_CLASS(wxGtkPrintModule
)
87 bool wxGtkPrintModule::OnInit()
90 if (gtk_check_version(2,10,0) == NULL
)
93 wxPrintFactory::SetPrintFactory( new wxGtkPrintFactory
);
98 IMPLEMENT_DYNAMIC_CLASS(wxGtkPrintModule
, wxModule
)
100 //----------------------------------------------------------------------------
102 //----------------------------------------------------------------------------
104 wxPrinterBase
* wxGtkPrintFactory::CreatePrinter( wxPrintDialogData
*data
)
106 return new wxGtkPrinter( data
);
109 wxPrintPreviewBase
*wxGtkPrintFactory::CreatePrintPreview( wxPrintout
*preview
,
110 wxPrintout
*printout
,
111 wxPrintDialogData
*data
)
113 return new wxGtkPrintPreview( preview
, printout
, data
);
116 wxPrintPreviewBase
*wxGtkPrintFactory::CreatePrintPreview( wxPrintout
*preview
,
117 wxPrintout
*printout
,
120 return new wxGtkPrintPreview( preview
, printout
, data
);
123 wxPrintDialogBase
*wxGtkPrintFactory::CreatePrintDialog( wxWindow
*parent
,
124 wxPrintDialogData
*data
)
126 return new wxGtkPrintDialog( parent
, data
);
129 wxPrintDialogBase
*wxGtkPrintFactory::CreatePrintDialog( wxWindow
*parent
,
132 return new wxGtkPrintDialog( parent
, data
);
135 wxPageSetupDialogBase
*wxGtkPrintFactory::CreatePageSetupDialog( wxWindow
*parent
,
136 wxPageSetupDialogData
* data
)
138 return new wxGtkPageSetupDialog( parent
, data
);
141 bool wxGtkPrintFactory::HasPrintSetupDialog()
147 wxGtkPrintFactory::CreatePrintSetupDialog(wxWindow
* WXUNUSED(parent
),
148 wxPrintData
* WXUNUSED(data
))
153 wxDCImpl
* wxGtkPrintFactory::CreatePrinterDCImpl( wxPrinterDC
*owner
, const wxPrintData
& data
)
155 return new wxGtkPrinterDCImpl( owner
, data
);
158 bool wxGtkPrintFactory::HasOwnPrintToFile()
163 bool wxGtkPrintFactory::HasPrinterLine()
168 wxString
wxGtkPrintFactory::CreatePrinterLine()
171 return wxEmptyString
;
174 bool wxGtkPrintFactory::HasStatusLine()
180 wxString
wxGtkPrintFactory::CreateStatusLine()
183 return wxEmptyString
;
186 wxPrintNativeDataBase
*wxGtkPrintFactory::CreatePrintNativeData()
188 return new wxGtkPrintNativeData
;
191 //----------------------------------------------------------------------------
192 // Callback functions for Gtk Printings.
193 //----------------------------------------------------------------------------
195 // We use it to pass useful objects to GTK printing callback functions.
196 struct wxPrinterToGtkData
198 wxGtkPrinter
* printer
;
199 wxPrintout
* printout
;
204 static void gtk_begin_print_callback (GtkPrintOperation
*operation
, GtkPrintContext
*context
, gpointer user_data
)
206 wxPrinterToGtkData
*data
= (wxPrinterToGtkData
*) user_data
;
208 data
->printer
->BeginPrint(data
->printout
, operation
, context
);
211 static void gtk_draw_page_print_callback (GtkPrintOperation
*operation
, GtkPrintContext
*context
, gint page_nr
, gpointer user_data
)
213 wxPrinterToGtkData
*data
= (wxPrinterToGtkData
*) user_data
;
215 data
->printer
->DrawPage(data
->printout
, operation
, context
, page_nr
);
218 static void gtk_end_print_callback(GtkPrintOperation
* WXUNUSED(operation
),
219 GtkPrintContext
* WXUNUSED(context
),
222 wxPrintout
*printout
= (wxPrintout
*) user_data
;
224 printout
->OnEndPrinting();
228 //----------------------------------------------------------------------------
229 // wxGtkPrintNativeData
230 //----------------------------------------------------------------------------
232 IMPLEMENT_CLASS(wxGtkPrintNativeData
, wxPrintNativeDataBase
)
234 wxGtkPrintNativeData::wxGtkPrintNativeData()
236 m_config
= gtk_print_settings_new();
237 m_job
= gtk_print_operation_new();
241 wxGtkPrintNativeData::~wxGtkPrintNativeData()
243 g_object_unref(m_job
);
244 g_object_unref(m_config
);
247 // Convert datas stored in m_config to a wxPrintData.
248 // Called by wxPrintData::ConvertFromNative().
249 bool wxGtkPrintNativeData::TransferTo( wxPrintData
&data
)
254 int resolution
= gtk_print_settings_get_resolution(m_config
);
255 if ( resolution
> 0 )
257 // if resolution is explicitly set, use it
258 data
.SetQuality(resolution
);
260 else // use more vague "quality"
262 GtkPrintQuality quality
= gtk_print_settings_get_quality(m_config
);
263 if (quality
== GTK_PRINT_QUALITY_HIGH
)
264 data
.SetQuality(wxPRINT_QUALITY_HIGH
);
265 else if (quality
== GTK_PRINT_QUALITY_LOW
)
266 data
.SetQuality(wxPRINT_QUALITY_LOW
);
267 else if (quality
== GTK_PRINT_QUALITY_DRAFT
)
268 data
.SetQuality(wxPRINT_QUALITY_DRAFT
);
270 data
.SetQuality(wxPRINT_QUALITY_MEDIUM
);
273 data
.SetNoCopies(gtk_print_settings_get_n_copies(m_config
));
275 data
.SetColour(gtk_print_settings_get_use_color(m_config
));
277 switch (gtk_print_settings_get_duplex(m_config
))
279 case GTK_PRINT_DUPLEX_SIMPLEX
: data
.SetDuplex (wxDUPLEX_SIMPLEX
);
282 case GTK_PRINT_DUPLEX_HORIZONTAL
: data
.SetDuplex (wxDUPLEX_HORIZONTAL
);
286 case GTK_PRINT_DUPLEX_VERTICAL
: data
.SetDuplex (wxDUPLEX_VERTICAL
);
290 GtkPageOrientation orientation
= gtk_print_settings_get_orientation (m_config
);
291 if (orientation
== GTK_PAGE_ORIENTATION_PORTRAIT
)
293 data
.SetOrientation(wxPORTRAIT
);
294 data
.SetOrientationReversed(false);
296 else if (orientation
== GTK_PAGE_ORIENTATION_LANDSCAPE
)
298 data
.SetOrientation(wxLANDSCAPE
);
299 data
.SetOrientationReversed(false);
301 else if (orientation
== GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT
)
303 data
.SetOrientation(wxPORTRAIT
);
304 data
.SetOrientationReversed(true);
306 else if (orientation
== GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE
)
308 data
.SetOrientation(wxLANDSCAPE
);
309 data
.SetOrientationReversed(true);
312 data
.SetCollate(gtk_print_settings_get_collate (m_config
));
314 // Paper formats : these are the most common paper formats.
315 GtkPaperSize
*paper_size
= gtk_print_settings_get_paper_size (m_config
);
317 data
.SetPaperId(wxPAPER_NONE
);
318 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_A3
)))
319 data
.SetPaperId(wxPAPER_A3
);
320 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_A4
)))
321 data
.SetPaperId(wxPAPER_A4
);
322 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_A5
)))
323 data
.SetPaperId(wxPAPER_A5
);
324 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_B5
)))
325 data
.SetPaperId(wxPAPER_B5
);
326 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_LETTER
)))
327 data
.SetPaperId(wxPAPER_LETTER
);
328 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_LEGAL
)))
329 data
.SetPaperId(wxPAPER_LEGAL
);
330 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_EXECUTIVE
)))
331 data
.SetPaperId(wxPAPER_EXECUTIVE
);
332 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"na_number-10")))
333 data
.SetPaperId(wxPAPER_ENV_10
);
334 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c5")))
335 data
.SetPaperId(wxPAPER_ENV_C5
);
336 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c6")))
337 data
.SetPaperId(wxPAPER_ENV_C6
);
338 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"jis-b5")))
339 data
.SetPaperId(wxPAPER_B5_TRANSVERSE
);
340 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b5")))
341 data
.SetPaperId(wxPAPER_ENV_B5
);
342 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"na_monarch")))
343 data
.SetPaperId(wxPAPER_ENV_MONARCH
);
344 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-c")))
345 data
.SetPaperId( wxPAPER_CSHEET
);
346 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-d")))
347 data
.SetPaperId( wxPAPER_DSHEET
);
348 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-e")))
349 data
.SetPaperId( wxPAPER_ESHEET
);
350 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"letter")))
351 data
.SetPaperId( wxPAPER_LETTERSMALL
);
352 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-b")))
353 data
.SetPaperId( wxPAPER_TABLOID
);
354 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"ledger")))
355 data
.SetPaperId( wxPAPER_LEDGER
);
356 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"statement")))
357 data
.SetPaperId( wxPAPER_STATEMENT
);
358 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( GTK_PAPER_NAME_A4
)))
359 data
.SetPaperId( wxPAPER_A4SMALL
);
360 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b4")))
361 data
.SetPaperId( wxPAPER_B4
);
362 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"folio")))
363 data
.SetPaperId( wxPAPER_FOLIO
);
364 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"quarto")))
365 data
.SetPaperId( wxPAPER_QUARTO
);
366 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"10x14")))
367 data
.SetPaperId( wxPAPER_10X14
);
368 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"ledger")))
369 data
.SetPaperId( wxPAPER_11X17
);
370 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"letter")))
371 data
.SetPaperId( wxPAPER_NOTE
);
372 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"na-number-9-envelope")))
373 data
.SetPaperId( wxPAPER_ENV_9
);
374 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"number-11")))
375 data
.SetPaperId( wxPAPER_ENV_11
);
376 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"number-12")))
377 data
.SetPaperId( wxPAPER_ENV_12
);
378 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"number-14")))
379 data
.SetPaperId( wxPAPER_ENV_14
);
380 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-designated")))
381 data
.SetPaperId( wxPAPER_ENV_DL
);
382 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c3")))
383 data
.SetPaperId( wxPAPER_ENV_C3
);
384 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c4")))
385 data
.SetPaperId( wxPAPER_ENV_C4
);
386 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"c6/c5")))
387 data
.SetPaperId( wxPAPER_ENV_C65
);
388 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b4")))
389 data
.SetPaperId( wxPAPER_ENV_B4
);
390 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b6")))
391 data
.SetPaperId( wxPAPER_ENV_B6
);
392 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"Italian")))
393 data
.SetPaperId( wxPAPER_ENV_ITALY
);
394 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"personal")))
395 data
.SetPaperId( wxPAPER_ENV_PERSONAL
);
396 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"fanfold-us")))
397 data
.SetPaperId( wxPAPER_FANFOLD_US
);
398 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"fanfold-European")))
399 data
.SetPaperId( wxPAPER_FANFOLD_STD_GERMAN
);
400 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"foolscap")))
401 data
.SetPaperId( wxPAPER_FANFOLD_LGL_GERMAN
);
403 data
.SetPaperId(wxPAPER_NONE
);
405 data
.SetPrinterName(gtk_print_settings_get_printer(m_config
));
410 // Put datas given by the wxPrintData into m_config.
411 // Called by wxPrintData::ConvertToNative().
412 bool wxGtkPrintNativeData::TransferFrom( const wxPrintData
&data
)
417 wxPrintQuality quality
= data
.GetQuality();
418 if (quality
== wxPRINT_QUALITY_HIGH
)
419 gtk_print_settings_set_quality (m_config
, GTK_PRINT_QUALITY_HIGH
);
420 else if (quality
== wxPRINT_QUALITY_MEDIUM
)
421 gtk_print_settings_set_quality (m_config
, GTK_PRINT_QUALITY_NORMAL
);
422 else if (quality
== wxPRINT_QUALITY_LOW
)
423 gtk_print_settings_set_quality (m_config
, GTK_PRINT_QUALITY_LOW
);
424 else if (quality
== wxPRINT_QUALITY_DRAFT
)
425 gtk_print_settings_set_quality (m_config
, GTK_PRINT_QUALITY_DRAFT
);
426 else if (quality
> 1)
427 gtk_print_settings_set_resolution (m_config
, quality
);
429 gtk_print_settings_set_quality (m_config
, GTK_PRINT_QUALITY_NORMAL
);
431 gtk_print_settings_set_n_copies(m_config
, data
.GetNoCopies());
433 gtk_print_settings_set_use_color(m_config
, data
.GetColour());
435 switch (data
.GetDuplex())
437 case wxDUPLEX_SIMPLEX
: gtk_print_settings_set_duplex (m_config
, GTK_PRINT_DUPLEX_SIMPLEX
);
440 case wxDUPLEX_HORIZONTAL
: gtk_print_settings_set_duplex (m_config
, GTK_PRINT_DUPLEX_HORIZONTAL
);
444 case wxDUPLEX_VERTICAL
: gtk_print_settings_set_duplex (m_config
, GTK_PRINT_DUPLEX_VERTICAL
);
448 if (!data
.IsOrientationReversed())
450 if (data
.GetOrientation() == wxLANDSCAPE
)
451 gtk_print_settings_set_orientation (m_config
, GTK_PAGE_ORIENTATION_LANDSCAPE
);
453 gtk_print_settings_set_orientation (m_config
, GTK_PAGE_ORIENTATION_PORTRAIT
);
456 if (data
.GetOrientation() == wxLANDSCAPE
)
457 gtk_print_settings_set_orientation (m_config
, GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE
);
459 gtk_print_settings_set_orientation (m_config
, GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT
);
462 gtk_print_settings_set_collate (m_config
, data
.GetCollate());
464 // Paper formats: these are the most common paper formats.
465 switch (data
.GetPaperId())
467 case wxPAPER_A3
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_A3
));
469 case wxPAPER_A4
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_A4
));
471 case wxPAPER_A5
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_A5
));
473 case wxPAPER_B5_TRANSVERSE
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "jis-b5"));
475 case wxPAPER_B5
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_B5
));
477 case wxPAPER_LETTER
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_LETTER
));
479 case wxPAPER_LEGAL
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_LEGAL
));
481 case wxPAPER_EXECUTIVE
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_EXECUTIVE
));
483 case wxPAPER_ENV_10
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "na_number-10"));
485 case wxPAPER_ENV_C5
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-c5"));
487 case wxPAPER_ENV_C6
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-c6"));
489 case wxPAPER_ENV_B5
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-c5b5"));
491 case wxPAPER_ENV_MONARCH
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "na_monarch"));
493 case wxPAPER_CSHEET
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "engineering-c"));
495 case wxPAPER_DSHEET
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "engineering-d"));
497 case wxPAPER_ESHEET
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "engineering-e"));
499 case wxPAPER_LETTERSMALL
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "letter"));
501 case wxPAPER_TABLOID
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "engineering-b"));
503 case wxPAPER_LEDGER
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "ledger"));
505 case wxPAPER_STATEMENT
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "statement"));
507 case wxPAPER_A4SMALL
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_A4
));
509 case wxPAPER_B4
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-b4"));
511 case wxPAPER_FOLIO
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "folio"));
513 case wxPAPER_QUARTO
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "quarto"));
515 case wxPAPER_10X14
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "10x14"));
517 case wxPAPER_11X17
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "ledger"));
519 case wxPAPER_NOTE
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "letter"));
521 case wxPAPER_ENV_9
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "na-number-9-envelope"));
523 case wxPAPER_ENV_11
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "number-11"));
525 case wxPAPER_ENV_12
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "number-12"));
527 case wxPAPER_ENV_14
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "number-14"));
529 case wxPAPER_ENV_DL
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-designated"));
531 case wxPAPER_ENV_C3
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-c3"));
533 case wxPAPER_ENV_C4
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-c4"));
535 case wxPAPER_ENV_C65
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "c6/c5"));
537 case wxPAPER_ENV_B4
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-b4"));
539 case wxPAPER_ENV_B6
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-b6"));
541 case wxPAPER_ENV_ITALY
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "Italian"));
543 case wxPAPER_ENV_PERSONAL
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "personal"));
545 case wxPAPER_FANFOLD_US
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "fanfold-us"));
547 case wxPAPER_FANFOLD_STD_GERMAN
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "fanfold-European"));
549 case wxPAPER_FANFOLD_LGL_GERMAN
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "foolscap"));
555 gtk_print_settings_set_printer(m_config
, data
.GetPrinterName().utf8_str());
560 void wxGtkPrintNativeData::SetPrintConfig( GtkPrintSettings
* config
)
563 m_config
= gtk_print_settings_copy(config
);
566 // Extract page setup from settings.
567 GtkPageSetup
* wxGtkPrintNativeData::GetPageSetupFromSettings(GtkPrintSettings
* settings
)
569 GtkPageSetup
* page_setup
= gtk_page_setup_new();
570 gtk_page_setup_set_orientation (page_setup
, gtk_print_settings_get_orientation (settings
));
572 GtkPaperSize
*paper_size
= gtk_print_settings_get_paper_size (settings
);
573 if (paper_size
!= NULL
)
574 gtk_page_setup_set_paper_size_and_default_margins (page_setup
, paper_size
);
579 // Insert page setup into a given GtkPrintSettings.
580 void wxGtkPrintNativeData::SetPageSetupToSettings(GtkPrintSettings
* settings
, GtkPageSetup
* page_setup
)
582 gtk_print_settings_set_orientation ( settings
, gtk_page_setup_get_orientation (page_setup
));
583 gtk_print_settings_set_paper_size ( settings
, gtk_page_setup_get_paper_size (page_setup
));
586 //----------------------------------------------------------------------------
588 //----------------------------------------------------------------------------
590 IMPLEMENT_CLASS(wxGtkPrintDialog
, wxPrintDialogBase
)
592 wxGtkPrintDialog::wxGtkPrintDialog( wxWindow
*parent
, wxPrintDialogData
*data
)
593 : wxPrintDialogBase(parent
, wxID_ANY
, _("Print"),
594 wxPoint(0, 0), wxSize(600, 600),
595 wxDEFAULT_DIALOG_STYLE
|
599 m_printDialogData
= *data
;
605 wxGtkPrintDialog::wxGtkPrintDialog( wxWindow
*parent
, wxPrintData
*data
)
606 : wxPrintDialogBase(parent
, wxID_ANY
, _("Print"),
607 wxPoint(0, 0), wxSize(600, 600),
608 wxDEFAULT_DIALOG_STYLE
|
612 m_printDialogData
= *data
;
619 wxGtkPrintDialog::~wxGtkPrintDialog()
623 // This is called even if we actually don't want the dialog to appear.
624 int wxGtkPrintDialog::ShowModal()
626 // We need to restore the settings given in the constructor.
627 wxPrintData data
= m_printDialogData
.GetPrintData();
628 wxGtkPrintNativeData
*native
=
629 (wxGtkPrintNativeData
*) data
.GetNativeData();
630 data
.ConvertToNative();
632 GtkPrintSettings
* settings
= native
->GetPrintConfig();
634 // We have to restore pages to print here because they're stored in a wxPrintDialogData and ConvertToNative only works for wxPrintData.
635 int fromPage
= m_printDialogData
.GetFromPage();
636 int toPage
= m_printDialogData
.GetToPage();
637 if (m_printDialogData
.GetSelection())
638 gtk_print_settings_set_print_pages(settings
, GTK_PRINT_PAGES_CURRENT
);
639 else if (m_printDialogData
.GetAllPages())
640 gtk_print_settings_set_print_pages(settings
, GTK_PRINT_PAGES_ALL
);
642 gtk_print_settings_set_print_pages(settings
, GTK_PRINT_PAGES_RANGES
);
644 range
= g_new (GtkPageRange
, 1);
645 range
[0].start
= fromPage
-1;
646 range
[0].end
= (toPage
>= fromPage
) ? toPage
-1 : fromPage
-1;
647 gtk_print_settings_set_page_ranges (settings
, range
, 1);
650 GtkPrintOperation
* const printOp
= native
->GetPrintJob();
652 // If the settings are OK, we restore it.
653 if (settings
!= NULL
)
654 gtk_print_operation_set_print_settings (printOp
, settings
);
655 gtk_print_operation_set_default_page_setup (printOp
, native
->GetPageSetupFromSettings(settings
));
657 // Show the dialog if needed.
658 GError
* gError
= NULL
;
659 GtkPrintOperationResult response
= gtk_print_operation_run
663 ? GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
664 : GTK_PRINT_OPERATION_ACTION_PRINT
,
666 ? GTK_WINDOW(gtk_widget_get_toplevel(m_parent
->m_widget
))
671 // Does everything went well?
672 if (response
== GTK_PRINT_OPERATION_RESULT_CANCEL
)
676 else if (response
== GTK_PRINT_OPERATION_RESULT_ERROR
)
678 wxLogError(_("Error while printing: ") + wxString(gError
? gError
->message
: "???"));
679 g_error_free (gError
);
680 return wxID_NO
; // We use wxID_NO because there is no wxID_ERROR available
683 // Now get the settings and save it.
684 GtkPrintSettings
* newSettings
= gtk_print_operation_get_print_settings(printOp
);
685 native
->SetPrintConfig(newSettings
);
686 data
.ConvertFromNative();
688 // Set PrintDialogData variables
689 m_printDialogData
.SetPrintData(data
);
690 m_printDialogData
.SetCollate(data
.GetCollate());
691 m_printDialogData
.SetNoCopies(data
.GetNoCopies());
692 m_printDialogData
.SetPrintToFile(data
.GetPrinterName() == "Print to File");
694 // Same problem as a few lines before.
695 switch (gtk_print_settings_get_print_pages(newSettings
))
697 case GTK_PRINT_PAGES_CURRENT
:
698 m_printDialogData
.SetSelection( true );
700 case GTK_PRINT_PAGES_RANGES
:
701 {// wxWidgets doesn't support multiple ranges, so we can only save the first one even if the user wants to print others.
702 // 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
703 // will hit "OK" button. However we can only save 1-3 in the print data.
706 range
= gtk_print_settings_get_page_ranges (newSettings
, &num_ranges
);
709 m_printDialogData
.SetFromPage( range
[0].start
);
710 m_printDialogData
.SetToPage( range
[0].end
);
713 m_printDialogData
.SetAllPages( true );
714 m_printDialogData
.SetFromPage( 0 );
715 m_printDialogData
.SetToPage( 9999 );
718 case GTK_PRINT_PAGES_ALL
:
720 m_printDialogData
.SetAllPages( true );
721 m_printDialogData
.SetFromPage( 0 );
722 m_printDialogData
.SetToPage( 9999 );
729 //----------------------------------------------------------------------------
730 // wxGtkPageSetupDialog
731 //----------------------------------------------------------------------------
733 IMPLEMENT_CLASS(wxGtkPageSetupDialog
, wxPageSetupDialogBase
)
735 wxGtkPageSetupDialog::wxGtkPageSetupDialog( wxWindow
*parent
,
736 wxPageSetupDialogData
* data
)
739 m_pageDialogData
= *data
;
744 wxGtkPageSetupDialog::~wxGtkPageSetupDialog()
748 int wxGtkPageSetupDialog::ShowModal()
751 m_pageDialogData
.GetPrintData().ConvertToNative();
752 wxGtkPrintNativeData
*native
= (wxGtkPrintNativeData
*) m_pageDialogData
.GetPrintData().GetNativeData();
753 GtkPrintSettings
* nativeData
= native
->GetPrintConfig();
755 // We only need the pagesetup data which are part of the settings.
756 GtkPageSetup
* oldPageSetup
= native
->GetPageSetupFromSettings(nativeData
);
758 // If the user used a custom paper format the last time he printed, we have to restore it too.
759 if (m_pageDialogData
.GetPrintData().GetPaperId() == wxPAPER_NONE
)
761 wxSize customPaperSize
= m_pageDialogData
.GetPaperSize();
762 if (customPaperSize
.GetWidth() > 0 && customPaperSize
.GetHeight() > 0)
764 wxString title
= _("Custom size");
765 GtkPaperSize
* customSize
= gtk_paper_size_new_custom ("custom", title
.mb_str(), (gdouble
) customPaperSize
.GetWidth(), (gdouble
) customPaperSize
.GetHeight(), GTK_UNIT_MM
);
766 gtk_page_setup_set_paper_size_and_default_margins (oldPageSetup
, customSize
);
767 g_object_unref(customSize
);
772 // Set selected printer
773 gtk_print_settings_set(nativeData
, "format-for-printer",
774 gtk_print_settings_get_printer(nativeData
));
776 // Create custom dialog
777 wxString
title(GetTitle());
779 title
= _("Page Setup");
781 dlg
= gtk_page_setup_unix_dialog_new(title
.utf8_str(),
783 ? GTK_WINDOW(m_parent
->m_widget
)
786 gtk_page_setup_unix_dialog_set_print_settings(
787 GTK_PAGE_SETUP_UNIX_DIALOG(dlg
), nativeData
);
788 gtk_page_setup_unix_dialog_set_page_setup(
789 GTK_PAGE_SETUP_UNIX_DIALOG(dlg
), oldPageSetup
);
791 int result
= gtk_dialog_run(GTK_DIALOG(dlg
));
792 gtk_widget_hide(dlg
);
796 case GTK_RESPONSE_OK
:
797 case GTK_RESPONSE_APPLY
:
799 // Store Selected Printer Name
800 gtk_print_settings_set_printer
803 gtk_print_settings_get(nativeData
, "format-for-printer")
806 wxGtkObject
<GtkPageSetup
>
807 newPageSetup(gtk_page_setup_unix_dialog_get_page_setup(
808 GTK_PAGE_SETUP_UNIX_DIALOG(dlg
)));
809 native
->SetPageSetupToSettings(nativeData
, newPageSetup
);
811 m_pageDialogData
.GetPrintData().ConvertFromNative();
813 // Store custom paper format if any.
814 if ( m_pageDialogData
.GetPrintData().GetPaperId() == wxPAPER_NONE
)
816 gdouble ml
,mr
,mt
,mb
,pw
,ph
;
817 ml
= gtk_page_setup_get_left_margin (newPageSetup
, GTK_UNIT_MM
);
818 mr
= gtk_page_setup_get_right_margin (newPageSetup
, GTK_UNIT_MM
);
819 mt
= gtk_page_setup_get_top_margin (newPageSetup
, GTK_UNIT_MM
);
820 mb
= gtk_page_setup_get_bottom_margin (newPageSetup
, GTK_UNIT_MM
);
822 pw
= gtk_page_setup_get_paper_width (newPageSetup
, GTK_UNIT_MM
);
823 ph
= gtk_page_setup_get_paper_height (newPageSetup
, GTK_UNIT_MM
);
825 m_pageDialogData
.SetMarginTopLeft(wxPoint((int)(ml
+0.5),
827 m_pageDialogData
.SetMarginBottomRight(wxPoint((int)(mr
+0.5),
830 m_pageDialogData
.SetPaperSize(wxSize((int)(pw
+0.5),
839 case GTK_RESPONSE_CANCEL
:
840 result
= wxID_CANCEL
;
844 gtk_widget_destroy(dlg
);
849 //----------------------------------------------------------------------------
851 //----------------------------------------------------------------------------
853 IMPLEMENT_CLASS(wxGtkPrinter
, wxPrinterBase
)
855 wxGtkPrinter::wxGtkPrinter( wxPrintDialogData
*data
) :
856 wxPrinterBase( data
)
861 m_printDialogData
= *data
;
864 wxGtkPrinter::~wxGtkPrinter()
868 bool wxGtkPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
872 sm_lastError
= wxPRINTER_ERROR
;
876 // Let's correct the PageInfo just in case the app gives wrong values.
877 int fromPage
, toPage
;
878 int minPage
, maxPage
;
879 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
880 m_printDialogData
.SetAllPages(true);
882 if (minPage
< 1) minPage
= 1;
883 if (maxPage
< 1) maxPage
= 9999;
884 if (maxPage
< minPage
) maxPage
= minPage
;
886 m_printDialogData
.SetMinPage(minPage
);
887 m_printDialogData
.SetMaxPage(maxPage
);
890 if (fromPage
< minPage
) fromPage
= minPage
;
891 else if (fromPage
> maxPage
) fromPage
= maxPage
;
892 m_printDialogData
.SetFromPage(fromPage
);
896 m_printDialogData
.SetToPage(toPage
);
897 if (toPage
> maxPage
) toPage
= maxPage
;
898 else if (toPage
< minPage
) toPage
= minPage
;
901 if (((minPage
!= fromPage
) && fromPage
!= 0) || ((maxPage
!= toPage
) && toPage
!= 0)) m_printDialogData
.SetAllPages(false);
904 wxPrintData printdata
= GetPrintDialogData().GetPrintData();
905 wxGtkPrintNativeData
*native
= (wxGtkPrintNativeData
*) printdata
.GetNativeData();
907 GtkPrintOperation
* const printOp
= native
->GetPrintJob();
909 wxPrinterToGtkData dataToSend
;
910 dataToSend
.printer
= this;
911 dataToSend
.printout
= printout
;
913 // These Gtk signals are caught here.
914 g_signal_connect (printOp
, "begin-print", G_CALLBACK (gtk_begin_print_callback
), &dataToSend
);
915 g_signal_connect (printOp
, "draw-page", G_CALLBACK (gtk_draw_page_print_callback
), &dataToSend
);
916 g_signal_connect (printOp
, "end-print", G_CALLBACK (gtk_end_print_callback
), printout
);
918 // This is used to setup the DC and
919 // show the dialog if desired
920 wxGtkPrintDialog
dialog( parent
, &m_printDialogData
);
921 dialog
.SetPrintDC(m_dc
);
922 dialog
.SetShowDialog(prompt
);
924 // doesn't necessarily show
925 int ret
= dialog
.ShowModal();
926 if (ret
== wxID_CANCEL
)
928 sm_lastError
= wxPRINTER_CANCELLED
;
932 sm_lastError
= wxPRINTER_ERROR
;
933 wxFAIL_MSG(_("The print dialog returned an error."));
936 return (sm_lastError
== wxPRINTER_NO_ERROR
);
939 void wxGtkPrinter::BeginPrint(wxPrintout
*printout
, GtkPrintOperation
*operation
, GtkPrintContext
*context
)
941 wxPrintData printdata
= GetPrintDialogData().GetPrintData();
942 wxGtkPrintNativeData
*native
= (wxGtkPrintNativeData
*) printdata
.GetNativeData();
944 // We need to update printdata with the new data from the dialog and we
945 // have to do this here because this method needs this new data and we
946 // cannot update it earlier
947 native
->SetPrintConfig(gtk_print_operation_get_print_settings(operation
));
948 printdata
.ConvertFromNative();
950 SetPrintContext(context
);
951 native
->SetPrintContext( context
);
953 wxPrinterDC
*printDC
= new wxPrinterDC( printdata
);
958 if (sm_lastError
!= wxPRINTER_CANCELLED
)
960 sm_lastError
= wxPRINTER_ERROR
;
961 wxFAIL_MSG(_("The wxGtkPrinterDC cannot be used."));
966 printout
->SetPPIScreen(wxGetDisplayPPI());
967 printout
->SetPPIPrinter( printDC
->GetResolution(),
968 printDC
->GetResolution() );
970 printout
->SetDC(m_dc
);
973 m_dc
->GetSize(&w
, &h
);
974 printout
->SetPageSizePixels((int)w
, (int)h
);
975 printout
->SetPaperRectPixels(wxRect(0, 0, w
, h
));
977 m_dc
->GetSizeMM(&mw
, &mh
);
978 printout
->SetPageSizeMM((int)mw
, (int)mh
);
979 printout
->OnPreparePrinting();
981 // Get some parameters from the printout, if defined.
982 int fromPage
, toPage
;
983 int minPage
, maxPage
;
984 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
988 sm_lastError
= wxPRINTER_ERROR
;
989 wxFAIL_MSG(_("wxPrintout::GetPageInfo gives a null maxPage."));
993 printout
->OnBeginPrinting();
997 // If we're not previewing we need to calculate the number of pages to print.
998 // If we're previewing, Gtk Print will render every pages without wondering about the page ranges the user may
999 // have defined in the dialog. So the number of pages is the maximum available.
1000 if (!printout
->IsPreview())
1002 GtkPrintSettings
* settings
= gtk_print_operation_get_print_settings (operation
);
1003 switch (gtk_print_settings_get_print_pages(settings
))
1005 case GTK_PRINT_PAGES_CURRENT
:
1008 case GTK_PRINT_PAGES_RANGES
:
1009 {gint num_ranges
= 0;
1010 GtkPageRange
* range
;
1012 range
= gtk_print_settings_get_page_ranges (settings
, &num_ranges
);
1013 for (i
=0; i
<num_ranges
; i
++)
1015 if (range
[i
].end
< range
[i
].start
) range
[i
].end
= range
[i
].start
;
1016 if (range
[i
].start
< minPage
-1) range
[i
].start
= minPage
-1;
1017 if (range
[i
].end
> maxPage
-1) range
[i
].end
= maxPage
-1;
1018 if (range
[i
].start
> maxPage
-1) range
[i
].start
= maxPage
-1;
1019 numPages
+= range
[i
].end
- range
[i
].start
+ 1;
1021 gtk_print_settings_set_page_ranges (settings
, range
, 1);
1023 case GTK_PRINT_PAGES_ALL
:
1025 numPages
= maxPage
- minPage
+ 1;
1029 else numPages
= maxPage
- minPage
+ 1;
1031 gtk_print_operation_set_n_pages(operation
, numPages
);
1034 void wxGtkPrinter::DrawPage(wxPrintout
*printout
,
1035 GtkPrintOperation
*operation
,
1036 GtkPrintContext
* WXUNUSED(context
),
1039 int fromPage
, toPage
, minPage
, maxPage
, startPage
, endPage
;
1040 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
1042 int numPageToDraw
= page_nr
+ minPage
;
1043 if (numPageToDraw
< minPage
) numPageToDraw
= minPage
;
1044 if (numPageToDraw
> maxPage
) numPageToDraw
= maxPage
;
1046 GtkPrintSettings
* settings
= gtk_print_operation_get_print_settings (operation
);
1047 switch (gtk_print_settings_get_print_pages(settings
))
1049 case GTK_PRINT_PAGES_CURRENT
:
1050 g_object_get_property((GObject
*) operation
, (const gchar
*) "current-page", (GValue
*) &startPage
);
1051 g_object_get_property((GObject
*) operation
, (const gchar
*) "current-page", (GValue
*) &endPage
);
1053 case GTK_PRINT_PAGES_RANGES
:
1054 {gint num_ranges
= 0;
1055 GtkPageRange
* range
;
1056 range
= gtk_print_settings_get_page_ranges (settings
, &num_ranges
);
1057 // We don't need to verify these values as it has already been done in wxGtkPrinter::BeginPrint.
1058 if (num_ranges
>= 1)
1060 startPage
= range
[0].start
+ 1;
1061 endPage
= range
[0].end
+ 1;
1064 startPage
= minPage
;
1068 case GTK_PRINT_PAGES_ALL
:
1070 startPage
= minPage
;
1075 if(numPageToDraw
== startPage
)
1077 if (!printout
->OnBeginDocument(startPage
, endPage
))
1079 wxLogError(_("Could not start printing."));
1080 sm_lastError
= wxPRINTER_ERROR
;
1084 // The app can render the page numPageToDraw.
1085 if (printout
->HasPage(numPageToDraw
))
1088 printout
->OnPrintPage(numPageToDraw
);
1093 if(numPageToDraw
== endPage
)
1095 printout
->OnEndDocument();
1099 wxDC
* wxGtkPrinter::PrintDialog( wxWindow
*parent
)
1101 wxGtkPrintDialog
dialog( parent
, &m_printDialogData
);
1103 dialog
.SetPrintDC(m_dc
);
1104 dialog
.SetShowDialog(true);
1106 int ret
= dialog
.ShowModal();
1108 if (ret
== wxID_CANCEL
)
1110 sm_lastError
= wxPRINTER_CANCELLED
;
1115 sm_lastError
= wxPRINTER_ERROR
;
1116 wxFAIL_MSG(_("The print dialog returned an error."));
1120 m_printDialogData
= dialog
.GetPrintDialogData();
1122 return new wxPrinterDC( m_printDialogData
.GetPrintData() );
1125 bool wxGtkPrinter::Setup( wxWindow
* WXUNUSED(parent
) )
1127 // Obsolete, for backward compatibility.
1131 //-----------------------------------------------------------------------------
1133 //-----------------------------------------------------------------------------
1135 #define wxCAIRO_SCALE 1
1139 #define XLOG2DEV(x) LogicalToDeviceX(x)
1140 #define XLOG2DEVREL(x) LogicalToDeviceXRel(x)
1141 #define YLOG2DEV(x) LogicalToDeviceY(x)
1142 #define YLOG2DEVREL(x) LogicalToDeviceYRel(x)
1146 #define XLOG2DEV(x) ((double)(LogicalToDeviceX(x)) * m_DEV2PS)
1147 #define XLOG2DEVREL(x) ((double)(LogicalToDeviceXRel(x)) * m_DEV2PS)
1148 #define YLOG2DEV(x) ((double)(LogicalToDeviceY(x)) * m_DEV2PS)
1149 #define YLOG2DEVREL(x) ((double)(LogicalToDeviceYRel(x)) * m_DEV2PS)
1153 IMPLEMENT_ABSTRACT_CLASS(wxGtkPrinterDCImpl
, wxDCImpl
)
1155 wxGtkPrinterDCImpl::wxGtkPrinterDCImpl(wxPrinterDC
*owner
, const wxPrintData
& data
)
1160 wxGtkPrintNativeData
*native
=
1161 (wxGtkPrintNativeData
*) m_printData
.GetNativeData();
1163 m_gpc
= native
->GetPrintContext();
1165 // Match print quality to resolution (high = 1200dpi)
1166 m_resolution
= m_printData
.GetQuality(); // (int) gtk_print_context_get_dpi_x( m_gpc );
1167 if (m_resolution
< 0)
1168 m_resolution
= (1 << (m_resolution
+4)) *150;
1170 m_context
= gtk_print_context_create_pango_context( m_gpc
);
1171 m_layout
= gtk_print_context_create_pango_layout ( m_gpc
);
1172 m_fontdesc
= pango_font_description_from_string( "Sans 12" );
1174 m_cairo
= gtk_print_context_get_cairo_context ( m_gpc
);
1180 cairo_scale( m_cairo
, 72.0 / (double)m_resolution
, 72.0 / (double)m_resolution
);
1182 m_PS2DEV
= (double)m_resolution
/ 72.0;
1183 m_DEV2PS
= 72.0 / (double)m_resolution
;
1190 m_signX
= 1; // default x-axis left to right.
1191 m_signY
= 1; // default y-axis bottom up -> top down.
1193 // By default the origin of the Cairo context is in the upper left
1194 // corner of the printable area. We need to translate it so that it
1195 // is in the upper left corner of the paper (without margins)
1196 GtkPageSetup
*setup
= gtk_print_context_get_page_setup( m_gpc
);
1198 ml
= gtk_page_setup_get_left_margin (setup
, GTK_UNIT_POINTS
);
1199 mt
= gtk_page_setup_get_top_margin (setup
, GTK_UNIT_POINTS
);
1200 cairo_translate(m_cairo
, -ml
, -mt
);
1203 wxGtkPrinterDCImpl::~wxGtkPrinterDCImpl()
1205 g_object_unref(m_context
);
1206 g_object_unref(m_layout
);
1209 bool wxGtkPrinterDCImpl::IsOk() const
1211 return m_gpc
!= NULL
;
1214 void* wxGtkPrinterDCImpl::GetCairoContext() const
1216 return (void*) cairo_reference( m_cairo
);
1219 void* wxGtkPrinterDCImpl::GetHandle() const
1221 return GetCairoContext();
1224 bool wxGtkPrinterDCImpl::DoFloodFill(wxCoord
WXUNUSED(x1
),
1225 wxCoord
WXUNUSED(y1
),
1226 const wxColour
& WXUNUSED(col
),
1227 wxFloodFillStyle
WXUNUSED(style
))
1229 // We can't access the given coord as a Cairo context is scalable, ie a
1230 // coord doesn't mean anything in this context.
1231 wxFAIL_MSG(_("not implemented"));
1235 void wxGtkPrinterDCImpl::DoGradientFillConcentric(const wxRect
& rect
, const wxColour
& initialColour
, const wxColour
& destColour
, const wxPoint
& circleCenter
)
1237 wxCoord xC
= circleCenter
.x
;
1238 wxCoord yC
= circleCenter
.y
;
1239 wxCoord xR
= rect
.x
;
1240 wxCoord yR
= rect
.y
;
1241 wxCoord w
= rect
.width
;
1242 wxCoord h
= rect
.height
;
1244 const double r2
= (w
/2)*(w
/2)+(h
/2)*(h
/2);
1245 double radius
= sqrt(r2
);
1247 unsigned char redI
= initialColour
.Red();
1248 unsigned char blueI
= initialColour
.Blue();
1249 unsigned char greenI
= initialColour
.Green();
1250 unsigned char alphaI
= initialColour
.Alpha();
1251 unsigned char redD
= destColour
.Red();
1252 unsigned char blueD
= destColour
.Blue();
1253 unsigned char greenD
= destColour
.Green();
1254 unsigned char alphaD
= destColour
.Alpha();
1256 double redIPS
= (double)(redI
) / 255.0;
1257 double blueIPS
= (double)(blueI
) / 255.0;
1258 double greenIPS
= (double)(greenI
) / 255.0;
1259 double alphaIPS
= (double)(alphaI
) / 255.0;
1260 double redDPS
= (double)(redD
) / 255.0;
1261 double blueDPS
= (double)(blueD
) / 255.0;
1262 double greenDPS
= (double)(greenD
) / 255.0;
1263 double alphaDPS
= (double)(alphaD
) / 255.0;
1265 // Create a pattern with the gradient.
1266 cairo_pattern_t
* gradient
;
1267 gradient
= cairo_pattern_create_radial (XLOG2DEV(xC
+xR
), YLOG2DEV(yC
+yR
), 0, XLOG2DEV(xC
+xR
), YLOG2DEV(yC
+yR
), radius
* m_DEV2PS
);
1268 cairo_pattern_add_color_stop_rgba (gradient
, 0.0, redIPS
, greenIPS
, blueIPS
, alphaIPS
);
1269 cairo_pattern_add_color_stop_rgba (gradient
, 1.0, redDPS
, greenDPS
, blueDPS
, alphaDPS
);
1271 // Fill the rectangle with this pattern.
1272 cairo_set_source(m_cairo
, gradient
);
1273 cairo_rectangle (m_cairo
, XLOG2DEV(xR
), YLOG2DEV(yR
), XLOG2DEVREL(w
), YLOG2DEVREL(h
) );
1274 cairo_fill(m_cairo
);
1276 cairo_pattern_destroy(gradient
);
1278 CalcBoundingBox(xR
, yR
);
1279 CalcBoundingBox(xR
+w
, yR
+h
);
1282 void wxGtkPrinterDCImpl::DoGradientFillLinear(const wxRect
& rect
, const wxColour
& initialColour
, const wxColour
& destColour
, wxDirection nDirection
)
1286 wxCoord w
= rect
.width
;
1287 wxCoord h
= rect
.height
;
1289 unsigned char redI
= initialColour
.Red();
1290 unsigned char blueI
= initialColour
.Blue();
1291 unsigned char greenI
= initialColour
.Green();
1292 unsigned char alphaI
= initialColour
.Alpha();
1293 unsigned char redD
= destColour
.Red();
1294 unsigned char blueD
= destColour
.Blue();
1295 unsigned char greenD
= destColour
.Green();
1296 unsigned char alphaD
= destColour
.Alpha();
1298 double redIPS
= (double)(redI
) / 255.0;
1299 double blueIPS
= (double)(blueI
) / 255.0;
1300 double greenIPS
= (double)(greenI
) / 255.0;
1301 double alphaIPS
= (double)(alphaI
) / 255.0;
1302 double redDPS
= (double)(redD
) / 255.0;
1303 double blueDPS
= (double)(blueD
) / 255.0;
1304 double greenDPS
= (double)(greenD
) / 255.0;
1305 double alphaDPS
= (double)(alphaD
) / 255.0;
1307 // Create a pattern with the gradient.
1308 cairo_pattern_t
* gradient
;
1309 gradient
= cairo_pattern_create_linear (XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x
+w
), YLOG2DEV(y
));
1311 if (nDirection
== wxWEST
)
1313 cairo_pattern_add_color_stop_rgba (gradient
, 0.0, redDPS
, greenDPS
, blueDPS
, alphaDPS
);
1314 cairo_pattern_add_color_stop_rgba (gradient
, 1.0, redIPS
, greenIPS
, blueIPS
, alphaIPS
);
1317 cairo_pattern_add_color_stop_rgba (gradient
, 0.0, redIPS
, greenIPS
, blueIPS
, alphaIPS
);
1318 cairo_pattern_add_color_stop_rgba (gradient
, 1.0, redDPS
, greenDPS
, blueDPS
, alphaDPS
);
1321 // Fill the rectangle with this pattern.
1322 cairo_set_source(m_cairo
, gradient
);
1323 cairo_rectangle (m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEVREL(w
), YLOG2DEVREL(h
) );
1324 cairo_fill(m_cairo
);
1326 cairo_pattern_destroy(gradient
);
1328 CalcBoundingBox(x
, y
);
1329 CalcBoundingBox(x
+w
, y
+h
);
1332 bool wxGtkPrinterDCImpl::DoGetPixel(wxCoord
WXUNUSED(x1
),
1333 wxCoord
WXUNUSED(y1
),
1334 wxColour
* WXUNUSED(col
)) const
1336 wxFAIL_MSG(_("not implemented"));
1340 void wxGtkPrinterDCImpl::DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
1342 if ( m_pen
.IsTransparent() )
1346 cairo_move_to ( m_cairo
, XLOG2DEV(x1
), YLOG2DEV(y1
) );
1347 cairo_line_to ( m_cairo
, XLOG2DEV(x2
), YLOG2DEV(y2
) );
1348 cairo_stroke ( m_cairo
);
1350 CalcBoundingBox( x1
, y1
);
1351 CalcBoundingBox( x2
, y2
);
1354 void wxGtkPrinterDCImpl::DoCrossHair(wxCoord x
, wxCoord y
)
1361 cairo_move_to (m_cairo
, XLOG2DEV(x
), 0);
1362 cairo_line_to (m_cairo
, XLOG2DEV(x
), YLOG2DEVREL(h
));
1363 cairo_move_to (m_cairo
, 0, YLOG2DEV(y
));
1364 cairo_line_to (m_cairo
, XLOG2DEVREL(w
), YLOG2DEV(y
));
1366 cairo_stroke (m_cairo
);
1367 CalcBoundingBox( 0, 0 );
1368 CalcBoundingBox( w
, h
);
1371 void wxGtkPrinterDCImpl::DoDrawArc(wxCoord x1
,wxCoord y1
,wxCoord x2
,wxCoord y2
,wxCoord xc
,wxCoord yc
)
1373 double dx
= x1
- xc
;
1374 double dy
= y1
- yc
;
1375 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
1377 double alpha1
, alpha2
;
1378 if (x1
== x2
&& y1
== y2
)
1386 alpha1
= alpha2
= 0.0;
1390 alpha1
= (x1
- xc
== 0) ?
1391 (y1
- yc
< 0) ? 90.0 : -90.0 :
1392 atan2(double(y1
-yc
), double(x1
-xc
)) * RAD2DEG
;
1393 alpha2
= (x2
- xc
== 0) ?
1394 (y2
- yc
< 0) ? 90.0 : -90.0 :
1395 atan2(double(y2
-yc
), double(x2
-xc
)) * RAD2DEG
;
1397 while (alpha1
<= 0) alpha1
+= 360;
1398 while (alpha2
<= 0) alpha2
+= 360; // adjust angles to be between.
1399 while (alpha1
> 360) alpha1
-= 360; // 0 and 360 degree.
1400 while (alpha2
> 360) alpha2
-= 360;
1406 cairo_new_path(m_cairo
);
1408 cairo_arc_negative ( m_cairo
, XLOG2DEV(xc
), YLOG2DEV(yc
), XLOG2DEVREL((int)radius
), alpha1
, alpha2
);
1409 cairo_line_to(m_cairo
, XLOG2DEV(xc
), YLOG2DEV(yc
));
1410 cairo_close_path (m_cairo
);
1412 SetBrush( m_brush
);
1413 cairo_fill_preserve( m_cairo
);
1416 cairo_stroke( m_cairo
);
1418 CalcBoundingBox (x1
, y1
);
1419 CalcBoundingBox (xc
, yc
);
1420 CalcBoundingBox (x2
, y2
);
1423 void wxGtkPrinterDCImpl::DoDrawEllipticArc(wxCoord x
,wxCoord y
,wxCoord w
,wxCoord h
,double sa
,double ea
)
1425 cairo_save( m_cairo
);
1427 cairo_new_path(m_cairo
);
1429 cairo_translate( m_cairo
, XLOG2DEV((wxCoord
) (x
+ w
/ 2.)), XLOG2DEV((wxCoord
) (y
+ h
/ 2.)) );
1430 double scale
= (double)YLOG2DEVREL(h
) / (double) XLOG2DEVREL(w
);
1431 cairo_scale( m_cairo
, 1.0, scale
);
1433 cairo_arc_negative ( m_cairo
, 0, 0, XLOG2DEVREL(w
/2), -sa
*DEG2RAD
, -ea
*DEG2RAD
);
1436 cairo_stroke_preserve( m_cairo
);
1438 cairo_line_to(m_cairo
, 0,0);
1440 SetBrush( m_brush
);
1441 cairo_fill( m_cairo
);
1443 cairo_restore( m_cairo
);
1445 CalcBoundingBox( x
, y
);
1446 CalcBoundingBox( x
+w
, y
+h
);
1449 void wxGtkPrinterDCImpl::DoDrawPoint(wxCoord x
, wxCoord y
)
1451 if ( m_pen
.IsTransparent() )
1456 cairo_move_to ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
) );
1457 cairo_line_to ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
) );
1458 cairo_stroke ( m_cairo
);
1460 CalcBoundingBox( x
, y
);
1463 void wxGtkPrinterDCImpl::DoDrawLines(int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
1465 if ( m_pen
.IsTransparent() )
1474 for ( i
=0; i
<n
; i
++ )
1475 CalcBoundingBox( points
[i
].x
+xoffset
, points
[i
].y
+yoffset
);
1477 cairo_move_to ( m_cairo
, XLOG2DEV(points
[0].x
+xoffset
), YLOG2DEV(points
[0].y
+yoffset
) );
1479 for (i
= 1; i
< n
; i
++)
1480 cairo_line_to ( m_cairo
, XLOG2DEV(points
[i
].x
+xoffset
), YLOG2DEV(points
[i
].y
+yoffset
) );
1482 cairo_stroke ( m_cairo
);
1485 void wxGtkPrinterDCImpl::DoDrawPolygon(int n
, wxPoint points
[],
1486 wxCoord xoffset
, wxCoord yoffset
,
1487 wxPolygonFillMode fillStyle
)
1491 cairo_save(m_cairo
);
1492 if (fillStyle
== wxWINDING_RULE
)
1493 cairo_set_fill_rule( m_cairo
, CAIRO_FILL_RULE_WINDING
);
1495 cairo_set_fill_rule( m_cairo
, CAIRO_FILL_RULE_EVEN_ODD
);
1497 int x
= points
[0].x
+ xoffset
;
1498 int y
= points
[0].y
+ yoffset
;
1499 cairo_new_path(m_cairo
);
1500 cairo_move_to( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
) );
1502 for (i
= 1; i
< n
; i
++)
1504 int x
= points
[i
].x
+ xoffset
;
1505 int y
= points
[i
].y
+ yoffset
;
1506 cairo_line_to( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
) );
1508 cairo_close_path(m_cairo
);
1510 SetBrush( m_brush
);
1511 cairo_fill_preserve( m_cairo
);
1514 cairo_stroke( m_cairo
);
1516 CalcBoundingBox( x
, y
);
1518 cairo_restore(m_cairo
);
1521 void wxGtkPrinterDCImpl::DoDrawPolyPolygon(int n
, int count
[], wxPoint points
[],
1522 wxCoord xoffset
, wxCoord yoffset
,
1523 wxPolygonFillMode fillStyle
)
1525 wxDCImpl::DoDrawPolyPolygon( n
, count
, points
, xoffset
, yoffset
, fillStyle
);
1528 void wxGtkPrinterDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1533 cairo_new_path(m_cairo
);
1534 cairo_rectangle ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEVREL(width
), YLOG2DEVREL(height
));
1536 SetBrush( m_brush
);
1537 cairo_fill_preserve( m_cairo
);
1540 cairo_stroke( m_cairo
);
1542 CalcBoundingBox( x
, y
);
1543 CalcBoundingBox( x
+ width
, y
+ height
);
1546 void wxGtkPrinterDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
1551 if (radius
< 0.0) radius
= - radius
* ((width
< height
) ? width
: height
);
1553 wxCoord dd
= 2 * (wxCoord
) radius
;
1554 if (dd
> width
) dd
= width
;
1555 if (dd
> height
) dd
= height
;
1558 wxCoord rad
= (wxCoord
) radius
;
1560 cairo_new_path(m_cairo
);
1561 cairo_move_to(m_cairo
,XLOG2DEV(x
+ rad
),YLOG2DEV(y
));
1562 cairo_curve_to(m_cairo
,
1563 XLOG2DEV(x
+ rad
),YLOG2DEV(y
),
1564 XLOG2DEV(x
),YLOG2DEV(y
),
1565 XLOG2DEV(x
),YLOG2DEV(y
+ rad
));
1566 cairo_line_to(m_cairo
,XLOG2DEV(x
),YLOG2DEV(y
+ height
- rad
));
1567 cairo_curve_to(m_cairo
,
1568 XLOG2DEV(x
),YLOG2DEV(y
+ height
- rad
),
1569 XLOG2DEV(x
),YLOG2DEV(y
+ height
),
1570 XLOG2DEV(x
+ rad
),YLOG2DEV(y
+ height
));
1571 cairo_line_to(m_cairo
,XLOG2DEV(x
+ width
- rad
),YLOG2DEV(y
+ height
));
1572 cairo_curve_to(m_cairo
,
1573 XLOG2DEV(x
+ width
- rad
),YLOG2DEV(y
+ height
),
1574 XLOG2DEV(x
+ width
),YLOG2DEV(y
+ height
),
1575 XLOG2DEV(x
+ width
),YLOG2DEV(y
+ height
- rad
));
1576 cairo_line_to(m_cairo
,XLOG2DEV(x
+ width
),YLOG2DEV(y
+ rad
));
1577 cairo_curve_to(m_cairo
,
1578 XLOG2DEV(x
+ width
),YLOG2DEV(y
+ rad
),
1579 XLOG2DEV(x
+ width
),YLOG2DEV(y
),
1580 XLOG2DEV(x
+ width
- rad
),YLOG2DEV(y
));
1581 cairo_line_to(m_cairo
,XLOG2DEV(x
+ rad
),YLOG2DEV(y
));
1582 cairo_close_path(m_cairo
);
1585 cairo_fill_preserve(m_cairo
);
1588 cairo_stroke(m_cairo
);
1590 CalcBoundingBox(x
,y
);
1591 CalcBoundingBox(x
+width
,y
+height
);
1594 void wxGtkPrinterDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1599 cairo_save (m_cairo
);
1601 cairo_new_path(m_cairo
);
1603 cairo_translate (m_cairo
, XLOG2DEV((wxCoord
) (x
+ width
/ 2.)), YLOG2DEV((wxCoord
) (y
+ height
/ 2.)));
1604 cairo_scale(m_cairo
, 1, (double)YLOG2DEVREL(height
)/(double)XLOG2DEVREL(width
));
1605 cairo_arc ( m_cairo
, 0, 0, XLOG2DEVREL(width
/2), 0, 2 * M_PI
);
1607 SetBrush( m_brush
);
1608 cairo_fill_preserve( m_cairo
);
1611 cairo_stroke( m_cairo
);
1613 CalcBoundingBox( x
, y
);
1614 CalcBoundingBox( x
+ width
, y
+ height
);
1616 cairo_restore (m_cairo
);
1620 void wxGtkPrinterDCImpl::DoDrawSpline(const wxPointList
*points
)
1624 double c
, d
, x1
, y1
, x2
, y2
, x3
, y3
;
1627 wxPointList::compatibility_iterator node
= points
->GetFirst();
1628 p
= node
->GetData();
1632 node
= node
->GetNext();
1633 p
= node
->GetData();
1637 (double)(x1
+ c
) / 2;
1639 (double)(y1
+ d
) / 2;
1641 cairo_new_path( m_cairo
);
1642 cairo_move_to( m_cairo
, XLOG2DEV((wxCoord
)x1
), YLOG2DEV((wxCoord
)y1
) );
1643 cairo_line_to( m_cairo
, XLOG2DEV((wxCoord
)x3
), YLOG2DEV((wxCoord
)y3
) );
1645 CalcBoundingBox( (wxCoord
)x1
, (wxCoord
)y1
);
1646 CalcBoundingBox( (wxCoord
)x3
, (wxCoord
)y3
);
1648 node
= node
->GetNext();
1651 q
= node
->GetData();
1659 x3
= (double)(x2
+ c
) / 2;
1660 y3
= (double)(y2
+ d
) / 2;
1662 cairo_curve_to(m_cairo
,
1663 XLOG2DEV((wxCoord
)x1
), YLOG2DEV((wxCoord
)y1
),
1664 XLOG2DEV((wxCoord
)x2
), YLOG2DEV((wxCoord
)y2
),
1665 XLOG2DEV((wxCoord
)x3
), YLOG2DEV((wxCoord
)y3
) );
1667 CalcBoundingBox( (wxCoord
)x1
, (wxCoord
)y1
);
1668 CalcBoundingBox( (wxCoord
)x3
, (wxCoord
)y3
);
1670 node
= node
->GetNext();
1673 cairo_line_to ( m_cairo
, XLOG2DEV((wxCoord
)c
), YLOG2DEV((wxCoord
)d
) );
1675 cairo_stroke( m_cairo
);
1677 #endif // wxUSE_SPLINES
1679 bool wxGtkPrinterDCImpl::DoBlit(wxCoord xdest
, wxCoord ydest
,
1680 wxCoord width
, wxCoord height
,
1681 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
1682 wxRasterOperationMode rop
, bool useMask
,
1683 wxCoord
WXUNUSED_UNLESS_DEBUG(xsrcMask
),
1684 wxCoord
WXUNUSED_UNLESS_DEBUG(ysrcMask
))
1686 wxASSERT_MSG( xsrcMask
== wxDefaultCoord
&& ysrcMask
== wxDefaultCoord
,
1687 wxT("mask coordinates are not supported") );
1689 wxCHECK_MSG( source
, false, wxT("invalid source dc") );
1691 // Blit into a bitmap.
1692 wxBitmap
bitmap( width
, height
);
1694 memDC
.SelectObject(bitmap
);
1695 memDC
.Blit(0, 0, width
, height
, source
, xsrc
, ysrc
, rop
);
1696 memDC
.SelectObject(wxNullBitmap
);
1698 // Draw bitmap. scaling and positioning is done there.
1699 GetOwner()->DrawBitmap( bitmap
, xdest
, ydest
, useMask
);
1704 void wxGtkPrinterDCImpl::DoDrawIcon( const wxIcon
& icon
, wxCoord x
, wxCoord y
)
1706 DoDrawBitmap( icon
, x
, y
, true );
1709 void wxGtkPrinterDCImpl::DoDrawBitmap( const wxBitmap
& bitmap
, wxCoord x
, wxCoord y
, bool useMask
)
1711 wxCHECK_RET( bitmap
.IsOk(), wxT("Invalid bitmap in wxGtkPrinterDCImpl::DoDrawBitmap"));
1713 x
= wxCoord(XLOG2DEV(x
));
1714 y
= wxCoord(YLOG2DEV(y
));
1715 int bw
= bitmap
.GetWidth();
1716 int bh
= bitmap
.GetHeight();
1718 wxBitmap bmpSource
= bitmap
; // we need a non-const instance.
1719 if (!useMask
&& !bitmap
.HasPixbuf() && bitmap
.GetMask())
1720 bmpSource
.SetMask(NULL
);
1723 cairo_save(m_cairo
);
1725 // Prepare to draw the image.
1726 cairo_translate(m_cairo
, x
, y
);
1729 wxDouble scaleX
= (wxDouble
) XLOG2DEVREL(bw
) / (wxDouble
) bw
;
1730 wxDouble scaleY
= (wxDouble
) YLOG2DEVREL(bh
) / (wxDouble
) bh
;
1731 cairo_scale(m_cairo
, scaleX
, scaleY
);
1734 bitmap
.Draw(m_cairo
, 0, 0, useMask
, &m_textForegroundColour
, &m_textBackgroundColour
);
1736 gdk_cairo_set_source_pixbuf(m_cairo
, bmpSource
.GetPixbuf(), 0, 0);
1737 cairo_pattern_set_filter(cairo_get_source(m_cairo
), CAIRO_FILTER_NEAREST
);
1738 // Use the original size here since the context is scaled already.
1739 cairo_rectangle(m_cairo
, 0, 0, bw
, bh
);
1740 // Fill the rectangle using the pattern.
1741 cairo_fill(m_cairo
);
1744 CalcBoundingBox(0,0);
1745 CalcBoundingBox(bw
,bh
);
1747 cairo_restore(m_cairo
);
1750 void wxGtkPrinterDCImpl::DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
)
1752 DoDrawRotatedText( text
, x
, y
, 0.0 );
1755 void wxGtkPrinterDCImpl::DoDrawRotatedText(const wxString
& text
, wxCoord x
, wxCoord y
, double angle
)
1757 double xx
= XLOG2DEV(x
);
1758 double yy
= YLOG2DEV(y
);
1762 const wxScopedCharBuffer data
= text
.utf8_str();
1764 pango_layout_set_text(m_layout
, data
, data
.length());
1766 const bool setAttrs
= m_font
.GTKSetPangoAttrs(m_layout
);
1767 if (m_textForegroundColour
.IsOk())
1769 unsigned char red
= m_textForegroundColour
.Red();
1770 unsigned char blue
= m_textForegroundColour
.Blue();
1771 unsigned char green
= m_textForegroundColour
.Green();
1772 unsigned char alpha
= m_textForegroundColour
.Alpha();
1774 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
&& alpha
== m_currentAlpha
))
1776 double redPS
= (double)(red
) / 255.0;
1777 double bluePS
= (double)(blue
) / 255.0;
1778 double greenPS
= (double)(green
) / 255.0;
1779 double alphaPS
= (double)(alpha
) / 255.0;
1781 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1784 m_currentBlue
= blue
;
1785 m_currentGreen
= green
;
1786 m_currentAlpha
= alpha
;
1791 cairo_move_to (m_cairo
, xx
, yy
);
1793 cairo_save( m_cairo
);
1795 if (fabs(angle
) > 0.00001)
1796 cairo_rotate( m_cairo
, angle
*DEG2RAD
);
1798 cairo_scale(m_cairo
, m_scaleX
, m_scaleY
);
1801 pango_layout_get_pixel_size( m_layout
, &w
, &h
);
1803 if ( m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1805 unsigned char red
= m_textBackgroundColour
.Red();
1806 unsigned char blue
= m_textBackgroundColour
.Blue();
1807 unsigned char green
= m_textBackgroundColour
.Green();
1808 unsigned char alpha
= m_textBackgroundColour
.Alpha();
1810 double redPS
= (double)(red
) / 255.0;
1811 double bluePS
= (double)(blue
) / 255.0;
1812 double greenPS
= (double)(green
) / 255.0;
1813 double alphaPS
= (double)(alpha
) / 255.0;
1815 cairo_save(m_cairo
);
1816 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1817 cairo_rectangle(m_cairo
, 0, 0, w
, h
); // still in cairo units
1818 cairo_fill(m_cairo
);
1819 cairo_restore(m_cairo
);
1822 pango_cairo_update_layout (m_cairo
, m_layout
);
1823 pango_cairo_show_layout (m_cairo
, m_layout
);
1825 cairo_restore( m_cairo
);
1829 // Undo underline attributes setting
1830 pango_layout_set_attributes(m_layout
, NULL
);
1833 // Back to device units:
1834 CalcBoundingBox (x
, y
);
1835 CalcBoundingBox (x
+ w
, y
+ h
);
1838 void wxGtkPrinterDCImpl::Clear()
1840 // Clear does nothing for printing, but keep the code
1843 cairo_save(m_cairo);
1844 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
1845 SetBrush(m_backgroundBrush);
1846 cairo_paint(m_cairo);
1847 cairo_restore(m_cairo);
1851 void wxGtkPrinterDCImpl::SetFont( const wxFont
& font
)
1858 pango_font_description_free( m_fontdesc
);
1860 m_fontdesc
= pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
1862 float size
= pango_font_description_get_size( m_fontdesc
);
1863 size
= size
* GetFontPointSizeAdjustment(72.0);
1864 pango_font_description_set_size( m_fontdesc
, (gint
)size
);
1866 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1870 void wxGtkPrinterDCImpl::SetPen( const wxPen
& pen
)
1872 if (!pen
.IsOk()) return;
1878 if (m_pen
.GetWidth() <= 0)
1881 width
= (double) m_pen
.GetWidth();
1883 cairo_set_line_width( m_cairo
, width
* m_DEV2PS
* m_scaleX
);
1884 static const double dotted
[] = {2.0, 5.0};
1885 static const double short_dashed
[] = {4.0, 4.0};
1886 static const double long_dashed
[] = {4.0, 8.0};
1887 static const double dotted_dashed
[] = {6.0, 6.0, 2.0, 6.0};
1889 switch (m_pen
.GetStyle())
1891 case wxPENSTYLE_DOT
: cairo_set_dash( m_cairo
, dotted
, 2, 0 ); break;
1892 case wxPENSTYLE_SHORT_DASH
: cairo_set_dash( m_cairo
, short_dashed
, 2, 0 ); break;
1893 case wxPENSTYLE_LONG_DASH
: cairo_set_dash( m_cairo
, long_dashed
, 2, 0 ); break;
1894 case wxPENSTYLE_DOT_DASH
: cairo_set_dash( m_cairo
, dotted_dashed
, 4, 0 ); break;
1895 case wxPENSTYLE_USER_DASH
:
1898 int num
= m_pen
.GetDashes (&wx_dashes
);
1899 gdouble
*g_dashes
= g_new( gdouble
, num
);
1901 for (i
= 0; i
< num
; ++i
)
1902 g_dashes
[i
] = (gdouble
) wx_dashes
[i
];
1903 cairo_set_dash( m_cairo
, g_dashes
, num
, 0);
1907 case wxPENSTYLE_SOLID
:
1908 case wxPENSTYLE_TRANSPARENT
:
1909 default: cairo_set_dash( m_cairo
, NULL
, 0, 0 ); break;
1912 switch (m_pen
.GetCap())
1914 case wxCAP_PROJECTING
: cairo_set_line_cap (m_cairo
, CAIRO_LINE_CAP_SQUARE
); break;
1915 case wxCAP_BUTT
: cairo_set_line_cap (m_cairo
, CAIRO_LINE_CAP_BUTT
); break;
1917 default: cairo_set_line_cap (m_cairo
, CAIRO_LINE_CAP_ROUND
); break;
1920 switch (m_pen
.GetJoin())
1922 case wxJOIN_BEVEL
: cairo_set_line_join (m_cairo
, CAIRO_LINE_JOIN_BEVEL
); break;
1923 case wxJOIN_MITER
: cairo_set_line_join (m_cairo
, CAIRO_LINE_JOIN_MITER
); break;
1925 default: cairo_set_line_join (m_cairo
, CAIRO_LINE_JOIN_ROUND
); break;
1928 unsigned char red
= m_pen
.GetColour().Red();
1929 unsigned char blue
= m_pen
.GetColour().Blue();
1930 unsigned char green
= m_pen
.GetColour().Green();
1931 unsigned char alpha
= m_pen
.GetColour().Alpha();
1933 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
&& alpha
== m_currentAlpha
))
1935 double redPS
= (double)(red
) / 255.0;
1936 double bluePS
= (double)(blue
) / 255.0;
1937 double greenPS
= (double)(green
) / 255.0;
1938 double alphaPS
= (double)(alpha
) / 255.0;
1940 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1943 m_currentBlue
= blue
;
1944 m_currentGreen
= green
;
1945 m_currentAlpha
= alpha
;
1949 void wxGtkPrinterDCImpl::SetBrush( const wxBrush
& brush
)
1951 if (!brush
.IsOk()) return;
1955 if (m_brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
1957 cairo_set_source_rgba( m_cairo
, 0, 0, 0, 0 );
1966 unsigned char red
= m_brush
.GetColour().Red();
1967 unsigned char blue
= m_brush
.GetColour().Blue();
1968 unsigned char green
= m_brush
.GetColour().Green();
1969 unsigned char alpha
= m_brush
.GetColour().Alpha();
1971 double redPS
= (double)(red
) / 255.0;
1972 double bluePS
= (double)(blue
) / 255.0;
1973 double greenPS
= (double)(green
) / 255.0;
1974 double alphaPS
= (double)(alpha
) / 255.0;
1976 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
&& alpha
== m_currentAlpha
))
1978 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1981 m_currentBlue
= blue
;
1982 m_currentGreen
= green
;
1983 m_currentAlpha
= alpha
;
1986 if (m_brush
.IsHatch())
1989 cairo_surface_t
*surface
;
1990 surface
= cairo_surface_create_similar(cairo_get_target(m_cairo
),CAIRO_CONTENT_COLOR_ALPHA
,10,10);
1991 cr
= cairo_create(surface
);
1992 cairo_set_line_cap(cr
, CAIRO_LINE_CAP_SQUARE
);
1993 cairo_set_line_width(cr
, 1);
1994 cairo_set_line_join(cr
,CAIRO_LINE_JOIN_MITER
);
1996 switch (m_brush
.GetStyle())
1998 case wxBRUSHSTYLE_CROSS_HATCH
:
1999 cairo_move_to(cr
, 5, 0);
2000 cairo_line_to(cr
, 5, 10);
2001 cairo_move_to(cr
, 0, 5);
2002 cairo_line_to(cr
, 10, 5);
2004 case wxBRUSHSTYLE_BDIAGONAL_HATCH
:
2005 cairo_move_to(cr
, 0, 10);
2006 cairo_line_to(cr
, 10, 0);
2008 case wxBRUSHSTYLE_FDIAGONAL_HATCH
:
2009 cairo_move_to(cr
, 0, 0);
2010 cairo_line_to(cr
, 10, 10);
2012 case wxBRUSHSTYLE_CROSSDIAG_HATCH
:
2013 cairo_move_to(cr
, 0, 0);
2014 cairo_line_to(cr
, 10, 10);
2015 cairo_move_to(cr
, 10, 0);
2016 cairo_line_to(cr
, 0, 10);
2018 case wxBRUSHSTYLE_HORIZONTAL_HATCH
:
2019 cairo_move_to(cr
, 0, 5);
2020 cairo_line_to(cr
, 10, 5);
2022 case wxBRUSHSTYLE_VERTICAL_HATCH
:
2023 cairo_move_to(cr
, 5, 0);
2024 cairo_line_to(cr
, 5, 10);
2027 wxFAIL_MSG(_("Couldn't get hatch style from wxBrush."));
2030 cairo_set_source_rgba(cr
, redPS
, greenPS
, bluePS
, alphaPS
);
2034 cairo_pattern_t
* pattern
= cairo_pattern_create_for_surface (surface
);
2035 cairo_surface_destroy(surface
);
2036 cairo_pattern_set_extend (pattern
, CAIRO_EXTEND_REPEAT
);
2037 cairo_set_source(m_cairo
, pattern
);
2038 cairo_pattern_destroy(pattern
);
2042 void wxGtkPrinterDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
2044 if (function
== wxCLEAR
)
2045 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_CLEAR
);
2046 else if (function
== wxOR
)
2047 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_OUT
);
2048 else if (function
== wxNO_OP
)
2049 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_DEST
);
2050 else if (function
== wxAND
)
2051 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_ADD
);
2052 else if (function
== wxSET
)
2053 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_SATURATE
);
2054 else if (function
== wxXOR
)
2055 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_XOR
);
2056 else // wxCOPY or anything else.
2057 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_SOURCE
);
2060 void wxGtkPrinterDCImpl::SetBackground( const wxBrush
& brush
)
2062 m_backgroundBrush
= brush
;
2063 cairo_save(m_cairo
);
2064 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_DEST_OVER
);
2066 SetBrush(m_backgroundBrush
);
2067 cairo_paint(m_cairo
);
2068 cairo_restore(m_cairo
);
2071 void wxGtkPrinterDCImpl::SetBackgroundMode(int mode
)
2073 if (mode
== wxBRUSHSTYLE_SOLID
)
2074 m_backgroundMode
= wxBRUSHSTYLE_SOLID
;
2076 m_backgroundMode
= wxBRUSHSTYLE_TRANSPARENT
;
2079 void wxGtkPrinterDCImpl::DoSetClippingRegion(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
2081 cairo_rectangle ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEVREL(width
), YLOG2DEVREL(height
));
2082 cairo_clip(m_cairo
);
2085 void wxGtkPrinterDCImpl::DestroyClippingRegion()
2087 cairo_reset_clip(m_cairo
);
2090 bool wxGtkPrinterDCImpl::StartDoc(const wxString
& WXUNUSED(message
))
2095 void wxGtkPrinterDCImpl::EndDoc()
2100 void wxGtkPrinterDCImpl::StartPage()
2105 void wxGtkPrinterDCImpl::EndPage()
2110 wxCoord
wxGtkPrinterDCImpl::GetCharHeight() const
2112 pango_layout_set_text( m_layout
, "H", 1 );
2115 pango_layout_get_pixel_size( m_layout
, &w
, &h
);
2117 return wxRound( h
* m_PS2DEV
);
2120 wxCoord
wxGtkPrinterDCImpl::GetCharWidth() const
2122 pango_layout_set_text( m_layout
, "H", 1 );
2125 pango_layout_get_pixel_size( m_layout
, &w
, &h
);
2127 return wxRound( w
* m_PS2DEV
);
2130 void wxGtkPrinterDCImpl::DoGetTextExtent(const wxString
& string
, wxCoord
*width
, wxCoord
*height
,
2132 wxCoord
*externalLeading
,
2133 const wxFont
*theFont
) const
2141 if ( externalLeading
)
2142 *externalLeading
= 0;
2149 cairo_save( m_cairo
);
2150 cairo_scale(m_cairo
, m_scaleX
, m_scaleY
);
2152 // Set layout's text
2153 const wxScopedCharBuffer dataUTF8
= string
.utf8_str();
2158 // scale the font and apply it
2159 PangoFontDescription
*desc
= theFont
->GetNativeFontInfo()->description
;
2160 oldSize
= pango_font_description_get_size(desc
);
2161 const float size
= oldSize
* GetFontPointSizeAdjustment(72.0);
2162 pango_font_description_set_size(desc
, (gint
)size
);
2164 pango_layout_set_font_description(m_layout
, desc
);
2167 pango_layout_set_text( m_layout
, dataUTF8
, strlen(dataUTF8
) );
2170 pango_layout_get_pixel_size( m_layout
, width
, &h
);
2176 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
2177 int baseline
= pango_layout_iter_get_baseline(iter
);
2178 pango_layout_iter_free(iter
);
2179 *descent
= h
- PANGO_PIXELS(baseline
);
2184 // restore font and reset font's size back
2185 pango_layout_set_font_description(m_layout
, m_fontdesc
);
2187 PangoFontDescription
*desc
= theFont
->GetNativeFontInfo()->description
;
2188 pango_font_description_set_size(desc
, oldSize
);
2191 cairo_restore( m_cairo
);
2194 void wxGtkPrinterDCImpl::DoGetSize(int* width
, int* height
) const
2196 GtkPageSetup
*setup
= gtk_print_context_get_page_setup( m_gpc
);
2199 *width
= wxRound( (double)gtk_page_setup_get_paper_width( setup
, GTK_UNIT_POINTS
) * (double)m_resolution
/ 72.0 );
2201 *height
= wxRound( (double)gtk_page_setup_get_paper_height( setup
, GTK_UNIT_POINTS
) * (double)m_resolution
/ 72.0 );
2204 void wxGtkPrinterDCImpl::DoGetSizeMM(int *width
, int *height
) const
2206 GtkPageSetup
*setup
= gtk_print_context_get_page_setup( m_gpc
);
2209 *width
= wxRound( gtk_page_setup_get_paper_width( setup
, GTK_UNIT_MM
) );
2211 *height
= wxRound( gtk_page_setup_get_paper_height( setup
, GTK_UNIT_MM
) );
2214 wxSize
wxGtkPrinterDCImpl::GetPPI() const
2216 return wxSize( (int)m_resolution
, (int)m_resolution
);
2219 void wxGtkPrinterDCImpl::SetPrintData(const wxPrintData
& data
)
2224 // overridden for wxPrinterDC Impl
2226 wxRect
wxGtkPrinterDCImpl::GetPaperRect() const
2228 // Does GtkPrint support printer margins?
2231 DoGetSize( &w
, &h
);
2232 return wxRect( 0,0,w
,h
);
2235 int wxGtkPrinterDCImpl::GetResolution() const
2237 return m_resolution
;
2240 // ----------------------------------------------------------------------------
2242 // ----------------------------------------------------------------------------
2244 IMPLEMENT_CLASS(wxGtkPrintPreview
, wxPrintPreviewBase
)
2246 void wxGtkPrintPreview::Init(wxPrintout
* WXUNUSED(printout
),
2247 wxPrintout
* WXUNUSED(printoutForPrinting
),
2250 // convert wxPrintQuality to resolution (input pointer can be NULL)
2251 wxPrintQuality quality
= data
? data
->GetQuality() : wxPRINT_QUALITY_MEDIUM
;
2254 case wxPRINT_QUALITY_HIGH
:
2255 m_resolution
= 1200;
2258 case wxPRINT_QUALITY_LOW
:
2262 case wxPRINT_QUALITY_DRAFT
:
2269 // positive values directly indicate print resolution
2270 m_resolution
= quality
;
2274 wxFAIL_MSG( "unknown print quality" );
2277 case wxPRINT_QUALITY_MEDIUM
:
2286 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout
*printout
,
2287 wxPrintout
*printoutForPrinting
,
2288 wxPrintDialogData
*data
)
2289 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
2291 Init(printout
, printoutForPrinting
, data
? &data
->GetPrintData() : NULL
);
2294 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout
*printout
,
2295 wxPrintout
*printoutForPrinting
,
2297 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
2299 Init(printout
, printoutForPrinting
, data
);
2302 wxGtkPrintPreview::~wxGtkPrintPreview()
2306 bool wxGtkPrintPreview::Print(bool interactive
)
2308 if (!m_printPrintout
)
2311 wxPrinter
printer(& m_printDialogData
);
2312 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
2315 void wxGtkPrintPreview::DetermineScaling()
2317 wxPaperSize paperType
= m_printDialogData
.GetPrintData().GetPaperId();
2319 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
2321 paper
= wxThePrintPaperDatabase
->FindPaperType(wxPAPER_A4
);
2325 const wxSize screenPPI
= wxGetDisplayPPI();
2326 int logPPIScreenX
= screenPPI
.GetWidth();
2327 int logPPIScreenY
= screenPPI
.GetHeight();
2328 int logPPIPrinterX
= m_resolution
;
2329 int logPPIPrinterY
= m_resolution
;
2331 m_previewPrintout
->SetPPIScreen( logPPIScreenX
, logPPIScreenY
);
2332 m_previewPrintout
->SetPPIPrinter( logPPIPrinterX
, logPPIPrinterY
);
2334 // Get width and height in points (1/72th of an inch)
2335 wxSize
sizeDevUnits(paper
->GetSizeDeviceUnits());
2336 sizeDevUnits
.x
= wxRound((double)sizeDevUnits
.x
* (double)m_resolution
/ 72.0);
2337 sizeDevUnits
.y
= wxRound((double)sizeDevUnits
.y
* (double)m_resolution
/ 72.0);
2339 wxSize
sizeTenthsMM(paper
->GetSize());
2340 wxSize
sizeMM(sizeTenthsMM
.x
/ 10, sizeTenthsMM
.y
/ 10);
2342 // If in landscape mode, we need to swap the width and height.
2343 if ( m_printDialogData
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
2345 m_pageWidth
= sizeDevUnits
.y
;
2346 m_pageHeight
= sizeDevUnits
.x
;
2347 m_previewPrintout
->SetPageSizeMM(sizeMM
.y
, sizeMM
.x
);
2351 m_pageWidth
= sizeDevUnits
.x
;
2352 m_pageHeight
= sizeDevUnits
.y
;
2353 m_previewPrintout
->SetPageSizeMM(sizeMM
.x
, sizeMM
.y
);
2355 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
2356 m_previewPrintout
->SetPaperRectPixels(wxRect(0, 0, m_pageWidth
, m_pageHeight
));
2358 // At 100%, the page should look about page-size on the screen.
2359 m_previewScaleX
= float(logPPIScreenX
) / logPPIPrinterX
;
2360 m_previewScaleY
= float(logPPIScreenY
) / logPPIPrinterY
;