1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/gtk/print.cpp
3 // Author: Anthony Bretaudeau
4 // Purpose: GTK printing support
6 // RCS-ID: $Id: print.cpp,v 1 2007-08-25 05:44:44 PC Exp $
7 // Copyright: (c) 2007 wxWidgets development team
8 // Licence: wxWindows Licence
9 /////////////////////////////////////////////////////////////////////////////
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/gtk/private.h"
35 #include "wx/dynlib.h"
40 #if wxUSE_GRAPHICS_CONTEXT
41 #include "wx/graphics.h"
45 wxFORCE_LINK_THIS_MODULE(gtk_print
)
47 #if wxUSE_LIBGNOMEPRINT
48 #include "wx/gtk/gnome/gprint.h"
51 // Usefull to convert angles from/to Rad to/from Deg.
52 static const double RAD2DEG
= 180.0 / M_PI
;
53 static const double DEG2RAD
= M_PI
/ 180.0;
55 //----------------------------------------------------------------------------
57 // Initialized when starting the app : if it successfully load the gtk-print framework,
58 // it uses it. If not, it falls back to gnome print (see /gtk/gnome/gprint.cpp) then
59 // to postscript if gnomeprint is not available.
60 //----------------------------------------------------------------------------
62 class wxGtkPrintModule
: public wxModule
67 #if wxUSE_LIBGNOMEPRINT
68 // This module must be initialized AFTER gnomeprint's one
69 AddDependency(CLASSINFO(wxGnomePrintModule
));
76 DECLARE_DYNAMIC_CLASS(wxGtkPrintModule
)
79 bool wxGtkPrintModule::OnInit()
81 if (gtk_check_version(2,10,0) == NULL
)
82 wxPrintFactory::SetPrintFactory( new wxGtkPrintFactory
);
86 IMPLEMENT_DYNAMIC_CLASS(wxGtkPrintModule
, wxModule
)
88 //----------------------------------------------------------------------------
90 //----------------------------------------------------------------------------
92 wxPrinterBase
* wxGtkPrintFactory::CreatePrinter( wxPrintDialogData
*data
)
94 return new wxGtkPrinter( data
);
97 wxPrintPreviewBase
*wxGtkPrintFactory::CreatePrintPreview( wxPrintout
*preview
,
99 wxPrintDialogData
*data
)
101 return new wxGtkPrintPreview( preview
, printout
, data
);
104 wxPrintPreviewBase
*wxGtkPrintFactory::CreatePrintPreview( wxPrintout
*preview
,
105 wxPrintout
*printout
,
108 return new wxGtkPrintPreview( preview
, printout
, data
);
111 wxPrintDialogBase
*wxGtkPrintFactory::CreatePrintDialog( wxWindow
*parent
,
112 wxPrintDialogData
*data
)
114 return new wxGtkPrintDialog( parent
, data
);
117 wxPrintDialogBase
*wxGtkPrintFactory::CreatePrintDialog( wxWindow
*parent
,
120 return new wxGtkPrintDialog( parent
, data
);
123 wxPageSetupDialogBase
*wxGtkPrintFactory::CreatePageSetupDialog( wxWindow
*parent
,
124 wxPageSetupDialogData
* data
)
126 return new wxGtkPageSetupDialog( parent
, data
);
129 bool wxGtkPrintFactory::HasPrintSetupDialog()
135 wxGtkPrintFactory::CreatePrintSetupDialog(wxWindow
* WXUNUSED(parent
),
136 wxPrintData
* WXUNUSED(data
))
141 wxDCImpl
* wxGtkPrintFactory::CreatePrinterDCImpl( wxPrinterDC
*owner
, const wxPrintData
& data
)
143 return new wxGtkPrinterDCImpl( owner
, data
);
146 bool wxGtkPrintFactory::HasOwnPrintToFile()
151 bool wxGtkPrintFactory::HasPrinterLine()
156 wxString
wxGtkPrintFactory::CreatePrinterLine()
159 return wxEmptyString
;
162 bool wxGtkPrintFactory::HasStatusLine()
168 wxString
wxGtkPrintFactory::CreateStatusLine()
171 return wxEmptyString
;
174 wxPrintNativeDataBase
*wxGtkPrintFactory::CreatePrintNativeData()
176 return new wxGtkPrintNativeData
;
179 //----------------------------------------------------------------------------
180 // Callback functions for Gtk Printings.
181 //----------------------------------------------------------------------------
183 // We use it to pass useful objects to GTK printing callback functions.
184 struct wxPrinterToGtkData
186 wxGtkPrinter
* printer
;
187 wxPrintout
* printout
;
192 static void gtk_begin_print_callback (GtkPrintOperation
*operation
, GtkPrintContext
*context
, gpointer user_data
)
194 wxPrinterToGtkData
*data
= (wxPrinterToGtkData
*) user_data
;
196 data
->printer
->BeginPrint(data
->printout
, operation
, context
);
199 static void gtk_draw_page_print_callback (GtkPrintOperation
*operation
, GtkPrintContext
*context
, gint page_nr
, gpointer user_data
)
201 wxPrinterToGtkData
*data
= (wxPrinterToGtkData
*) user_data
;
203 data
->printer
->DrawPage(data
->printout
, operation
, context
, page_nr
);
206 static void gtk_end_print_callback(GtkPrintOperation
* WXUNUSED(operation
),
207 GtkPrintContext
* WXUNUSED(context
),
210 wxPrintout
*printout
= (wxPrintout
*) user_data
;
212 printout
->OnEndPrinting();
216 gtk_preview_print_callback(GtkPrintOperation
* WXUNUSED(operation
),
217 GtkPrintOperationPreview
* WXUNUSED(preview
),
218 GtkPrintContext
*context
,
222 wxPrintout
*printout
= (wxPrintout
*) user_data
;
224 printout
->SetIsPreview(true);
226 /* We create a Cairo context with 72dpi resolution. This resolution is
227 * only used for positioning. */
228 cairo_t
*cairo
= gdk_cairo_create(GTK_WIDGET(parent
)->window
);
229 gtk_print_context_set_cairo_context(context
, cairo
, 72, 72);
235 //----------------------------------------------------------------------------
236 // wxGtkPrintNativeData
237 //----------------------------------------------------------------------------
239 IMPLEMENT_CLASS(wxGtkPrintNativeData
, wxPrintNativeDataBase
)
241 wxGtkPrintNativeData::wxGtkPrintNativeData()
243 m_config
= gtk_print_settings_new();
246 wxGtkPrintNativeData::~wxGtkPrintNativeData()
248 g_object_unref (m_config
);
251 // Convert datas stored in m_config to a wxPrintData.
252 // Called by wxPrintData::ConvertFromNative().
253 bool wxGtkPrintNativeData::TransferTo( wxPrintData
&data
)
258 int resolution
= gtk_print_settings_get_resolution(m_config
);
259 if ( resolution
> 0 )
261 // if resolution is explicitly set, use it
262 data
.SetQuality(resolution
);
264 else // use more vague "quality"
266 GtkPrintQuality quality
= gtk_print_settings_get_quality(m_config
);
267 if (quality
== GTK_PRINT_QUALITY_HIGH
)
268 data
.SetQuality(wxPRINT_QUALITY_HIGH
);
269 else if (quality
== GTK_PRINT_QUALITY_LOW
)
270 data
.SetQuality(wxPRINT_QUALITY_LOW
);
271 else if (quality
== GTK_PRINT_QUALITY_DRAFT
)
272 data
.SetQuality(wxPRINT_QUALITY_DRAFT
);
274 data
.SetQuality(wxPRINT_QUALITY_MEDIUM
);
277 data
.SetNoCopies(gtk_print_settings_get_n_copies(m_config
));
279 data
.SetColour(gtk_print_settings_get_use_color(m_config
));
281 switch (gtk_print_settings_get_duplex(m_config
))
283 case GTK_PRINT_DUPLEX_SIMPLEX
: data
.SetDuplex (wxDUPLEX_SIMPLEX
);
286 case GTK_PRINT_DUPLEX_HORIZONTAL
: data
.SetDuplex (wxDUPLEX_HORIZONTAL
);
290 case GTK_PRINT_DUPLEX_VERTICAL
: data
.SetDuplex (wxDUPLEX_VERTICAL
);
294 GtkPageOrientation orientation
= gtk_print_settings_get_orientation (m_config
);
295 if (orientation
== GTK_PAGE_ORIENTATION_PORTRAIT
)
297 data
.SetOrientation(wxPORTRAIT
);
298 data
.SetOrientationReversed(false);
300 else if (orientation
== GTK_PAGE_ORIENTATION_LANDSCAPE
)
302 data
.SetOrientation(wxLANDSCAPE
);
303 data
.SetOrientationReversed(false);
305 else if (orientation
== GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT
)
307 data
.SetOrientation(wxPORTRAIT
);
308 data
.SetOrientationReversed(true);
310 else if (orientation
== GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE
)
312 data
.SetOrientation(wxLANDSCAPE
);
313 data
.SetOrientationReversed(true);
316 data
.SetCollate(gtk_print_settings_get_collate (m_config
));
318 // Paper formats : these are the most common paper formats.
319 GtkPaperSize
*paper_size
= gtk_print_settings_get_paper_size (m_config
);
321 data
.SetPaperId(wxPAPER_NONE
);
322 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_A3
)))
323 data
.SetPaperId(wxPAPER_A3
);
324 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_A4
)))
325 data
.SetPaperId(wxPAPER_A4
);
326 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_A5
)))
327 data
.SetPaperId(wxPAPER_A5
);
328 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_B5
)))
329 data
.SetPaperId(wxPAPER_B5
);
330 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_LETTER
)))
331 data
.SetPaperId(wxPAPER_LETTER
);
332 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_LEGAL
)))
333 data
.SetPaperId(wxPAPER_LEGAL
);
334 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_EXECUTIVE
)))
335 data
.SetPaperId(wxPAPER_EXECUTIVE
);
336 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"na_number-10")))
337 data
.SetPaperId(wxPAPER_ENV_10
);
338 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c5")))
339 data
.SetPaperId(wxPAPER_ENV_C5
);
340 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c6")))
341 data
.SetPaperId(wxPAPER_ENV_C6
);
342 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"jis-b5")))
343 data
.SetPaperId(wxPAPER_B5_TRANSVERSE
);
344 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b5")))
345 data
.SetPaperId(wxPAPER_ENV_B5
);
346 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"na_monarch")))
347 data
.SetPaperId(wxPAPER_ENV_MONARCH
);
348 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-c")))
349 data
.SetPaperId( wxPAPER_CSHEET
);
350 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-d")))
351 data
.SetPaperId( wxPAPER_DSHEET
);
352 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-e")))
353 data
.SetPaperId( wxPAPER_ESHEET
);
354 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"letter")))
355 data
.SetPaperId( wxPAPER_LETTERSMALL
);
356 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-b")))
357 data
.SetPaperId( wxPAPER_TABLOID
);
358 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"ledger")))
359 data
.SetPaperId( wxPAPER_LEDGER
);
360 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"statement")))
361 data
.SetPaperId( wxPAPER_STATEMENT
);
362 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( GTK_PAPER_NAME_A4
)))
363 data
.SetPaperId( wxPAPER_A4SMALL
);
364 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b4")))
365 data
.SetPaperId( wxPAPER_B4
);
366 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"folio")))
367 data
.SetPaperId( wxPAPER_FOLIO
);
368 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"quarto")))
369 data
.SetPaperId( wxPAPER_QUARTO
);
370 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"10x14")))
371 data
.SetPaperId( wxPAPER_10X14
);
372 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"ledger")))
373 data
.SetPaperId( wxPAPER_11X17
);
374 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"letter")))
375 data
.SetPaperId( wxPAPER_NOTE
);
376 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"na-number-9-envelope")))
377 data
.SetPaperId( wxPAPER_ENV_9
);
378 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"number-11")))
379 data
.SetPaperId( wxPAPER_ENV_11
);
380 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"number-12")))
381 data
.SetPaperId( wxPAPER_ENV_12
);
382 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"number-14")))
383 data
.SetPaperId( wxPAPER_ENV_14
);
384 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-designated")))
385 data
.SetPaperId( wxPAPER_ENV_DL
);
386 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c3")))
387 data
.SetPaperId( wxPAPER_ENV_C3
);
388 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c4")))
389 data
.SetPaperId( wxPAPER_ENV_C4
);
390 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"c6/c5")))
391 data
.SetPaperId( wxPAPER_ENV_C65
);
392 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b4")))
393 data
.SetPaperId( wxPAPER_ENV_B4
);
394 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b6")))
395 data
.SetPaperId( wxPAPER_ENV_B6
);
396 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"Italian")))
397 data
.SetPaperId( wxPAPER_ENV_ITALY
);
398 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"personal")))
399 data
.SetPaperId( wxPAPER_ENV_PERSONAL
);
400 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"fanfold-us")))
401 data
.SetPaperId( wxPAPER_FANFOLD_US
);
402 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"fanfold-European")))
403 data
.SetPaperId( wxPAPER_FANFOLD_STD_GERMAN
);
404 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"foolscap")))
405 data
.SetPaperId( wxPAPER_FANFOLD_LGL_GERMAN
);
407 data
.SetPaperId(wxPAPER_NONE
);
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"));
559 void wxGtkPrintNativeData::SetPrintConfig( GtkPrintSettings
* config
)
562 m_config
= gtk_print_settings_copy(config
);
565 // Extract page setup from settings.
566 GtkPageSetup
* wxGtkPrintNativeData::GetPageSetupFromSettings(GtkPrintSettings
* settings
)
568 GtkPageSetup
* page_setup
= gtk_page_setup_new();
569 gtk_page_setup_set_orientation (page_setup
, gtk_print_settings_get_orientation (settings
));
571 GtkPaperSize
*paper_size
= gtk_print_settings_get_paper_size (settings
);
572 if (paper_size
!= NULL
)
573 gtk_page_setup_set_paper_size_and_default_margins (page_setup
, paper_size
);
578 // Insert page setup into a given GtkPrintSettings.
579 void wxGtkPrintNativeData::SetPageSetupToSettings(GtkPrintSettings
* settings
, GtkPageSetup
* page_setup
)
581 gtk_print_settings_set_orientation ( settings
, gtk_page_setup_get_orientation (page_setup
));
582 gtk_print_settings_set_paper_size ( settings
, gtk_page_setup_get_paper_size (page_setup
));
585 //----------------------------------------------------------------------------
587 //----------------------------------------------------------------------------
589 IMPLEMENT_CLASS(wxGtkPrintDialog
, wxPrintDialogBase
)
591 wxGtkPrintDialog::wxGtkPrintDialog( wxWindow
*parent
, wxPrintDialogData
*data
)
592 : wxPrintDialogBase(parent
, wxID_ANY
, _("Print"),
593 wxPoint(0, 0), wxSize(600, 600),
594 wxDEFAULT_DIALOG_STYLE
|
598 m_printDialogData
= *data
;
604 wxGtkPrintDialog::wxGtkPrintDialog( wxWindow
*parent
, wxPrintData
*data
)
605 : wxPrintDialogBase(parent
, wxID_ANY
, _("Print"),
606 wxPoint(0, 0), wxSize(600, 600),
607 wxDEFAULT_DIALOG_STYLE
|
611 m_printDialogData
= *data
;
618 wxGtkPrintDialog::~wxGtkPrintDialog()
622 // This is called even if we actually don't want the dialog to appear.
623 int wxGtkPrintDialog::ShowModal()
625 GtkPrintOperationResult response
;
627 // We need to restore the settings given in the constructor.
628 wxPrintData data
= m_printDialogData
.GetPrintData();
629 wxGtkPrintNativeData
*native
=
630 (wxGtkPrintNativeData
*) data
.GetNativeData();
631 data
.ConvertToNative();
633 GtkPrintSettings
* settings
= native
->GetPrintConfig();
635 // We have to restore pages to print here because they're stored in a wxPrintDialogData and ConvertToNative only works for wxPrintData.
636 int fromPage
= m_printDialogData
.GetFromPage();
637 int toPage
= m_printDialogData
.GetToPage();
638 if (m_printDialogData
.GetSelection())
639 gtk_print_settings_set_print_pages(settings
, GTK_PRINT_PAGES_CURRENT
);
640 else if (m_printDialogData
.GetAllPages())
641 gtk_print_settings_set_print_pages(settings
, GTK_PRINT_PAGES_ALL
);
643 gtk_print_settings_set_print_pages(settings
, GTK_PRINT_PAGES_RANGES
);
645 range
= g_new (GtkPageRange
, 1);
646 range
[0].start
= fromPage
-1;
647 range
[0].end
= (toPage
>= fromPage
) ? toPage
-1 : fromPage
-1;
648 gtk_print_settings_set_page_ranges (settings
, range
, 1);
651 // If the settings are OK, we restore it.
652 if (settings
!= NULL
)
653 gtk_print_operation_set_print_settings (native
->GetPrintJob(), settings
);
654 gtk_print_operation_set_default_page_setup (native
->GetPrintJob(), native
->GetPageSetupFromSettings(settings
));
656 // Show the dialog if needed.
657 GError
* gError
= NULL
;
659 response
= gtk_print_operation_run (native
->GetPrintJob(), GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
, GTK_WINDOW(gtk_widget_get_toplevel(m_parent
->m_widget
) ), &gError
);
661 response
= gtk_print_operation_run (native
->GetPrintJob(), GTK_PRINT_OPERATION_ACTION_PRINT
, GTK_WINDOW(gtk_widget_get_toplevel(m_parent
->m_widget
)), &gError
);
663 // Does everything went well?
664 if (response
== GTK_PRINT_OPERATION_RESULT_CANCEL
)
668 else if (response
== GTK_PRINT_OPERATION_RESULT_ERROR
)
670 g_error_free (gError
);
671 wxLogError(_("Error while printing: ") + wxString::Format(_("%s"), gError
->message
));
672 return wxID_NO
; // We use wxID_NO because there is no wxID_ERROR available
675 // Now get the settings and save it.
676 GtkPrintSettings
* newSettings
= gtk_print_operation_get_print_settings (native
->GetPrintJob());
677 native
->SetPrintConfig(newSettings
);
678 data
.ConvertFromNative();
680 // Same problem as a few lines before.
681 switch (gtk_print_settings_get_print_pages(newSettings
))
683 case GTK_PRINT_PAGES_CURRENT
:
684 m_printDialogData
.SetSelection( true );
686 case GTK_PRINT_PAGES_RANGES
:
687 {// wxWidgets doesn't support multiple ranges, so we can only save the first one even if the user wants to print others.
688 // 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
689 // will hit "OK" button. However we can only save 1-3 in the print data.
692 range
= gtk_print_settings_get_page_ranges (newSettings
, &num_ranges
);
695 m_printDialogData
.SetFromPage( range
[0].start
);
696 m_printDialogData
.SetToPage( range
[0].end
);
699 m_printDialogData
.SetAllPages( true );
700 m_printDialogData
.SetFromPage( 0 );
701 m_printDialogData
.SetToPage( 9999 );
704 case GTK_PRINT_PAGES_ALL
:
706 m_printDialogData
.SetAllPages( true );
707 m_printDialogData
.SetFromPage( 0 );
708 m_printDialogData
.SetToPage( 9999 );
715 //----------------------------------------------------------------------------
716 // wxGtkPageSetupDialog
717 //----------------------------------------------------------------------------
719 IMPLEMENT_CLASS(wxGtkPageSetupDialog
, wxPageSetupDialogBase
)
721 wxGtkPageSetupDialog::wxGtkPageSetupDialog( wxWindow
*parent
,
722 wxPageSetupDialogData
* data
)
725 m_pageDialogData
= *data
;
730 wxGtkPageSetupDialog::~wxGtkPageSetupDialog()
734 int wxGtkPageSetupDialog::ShowModal()
737 m_pageDialogData
.GetPrintData().ConvertToNative();
738 wxGtkPrintNativeData
*native
= (wxGtkPrintNativeData
*) m_pageDialogData
.GetPrintData().GetNativeData();
739 GtkPrintSettings
* nativeData
= native
->GetPrintConfig();
741 // We only need the pagesetup data which are part of the settings.
742 GtkPageSetup
* oldPageSetup
= native
->GetPageSetupFromSettings(nativeData
);
744 // If the user used a custom paper format the last time he printed, we have to restore it too.
745 if (m_pageDialogData
.GetPrintData().GetPaperId() == wxPAPER_NONE
)
747 wxSize customPaperSize
= m_pageDialogData
.GetPaperSize();
748 if (customPaperSize
.GetWidth() > 0 && customPaperSize
.GetHeight() > 0)
750 wxString title
= _("Custom size");
751 GtkPaperSize
* customSize
= gtk_paper_size_new_custom ("custom", title
.mb_str(), (gdouble
) customPaperSize
.GetWidth(), (gdouble
) customPaperSize
.GetHeight(), GTK_UNIT_MM
);
752 gtk_page_setup_set_paper_size_and_default_margins (oldPageSetup
, customSize
);
753 g_object_unref(customSize
);
757 // Now show the dialog.
758 GtkPageSetup
* newPageSetup
= gtk_print_run_page_setup_dialog (GTK_WINDOW(m_parent
->m_widget
),
763 if (newPageSetup
!= oldPageSetup
)
765 native
->SetPageSetupToSettings(nativeData
, newPageSetup
);
766 m_pageDialogData
.GetPrintData().ConvertFromNative();
768 // Store custom paper format if any.
769 if (m_pageDialogData
.GetPrintData().GetPaperId() == wxPAPER_NONE
)
771 gdouble ml
,mr
,mt
,mb
,pw
,ph
;
772 ml
= gtk_page_setup_get_left_margin (newPageSetup
, GTK_UNIT_MM
);
773 mr
= gtk_page_setup_get_right_margin (newPageSetup
, GTK_UNIT_MM
);
774 mt
= gtk_page_setup_get_top_margin (newPageSetup
, GTK_UNIT_MM
);
775 mb
= gtk_page_setup_get_bottom_margin (newPageSetup
, GTK_UNIT_MM
);
777 pw
= gtk_page_setup_get_paper_width (newPageSetup
, GTK_UNIT_MM
);
778 ph
= gtk_page_setup_get_paper_height (newPageSetup
, GTK_UNIT_MM
);
780 m_pageDialogData
.SetMarginTopLeft( wxPoint( (int)(ml
+0.5), (int)(mt
+0.5)) );
781 m_pageDialogData
.SetMarginBottomRight( wxPoint( (int)(mr
+0.5), (int)(mb
+0.5)) );
783 m_pageDialogData
.SetPaperSize( wxSize( (int)(pw
+0.5), (int)(ph
+0.5) ) );
796 //----------------------------------------------------------------------------
798 //----------------------------------------------------------------------------
800 IMPLEMENT_CLASS(wxGtkPrinter
, wxPrinterBase
)
802 wxGtkPrinter::wxGtkPrinter( wxPrintDialogData
*data
) :
803 wxPrinterBase( data
)
808 m_printDialogData
= *data
;
811 wxGtkPrinter::~wxGtkPrinter()
815 bool wxGtkPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
819 sm_lastError
= wxPRINTER_ERROR
;
823 // Let's correct the PageInfo just in case the app gives wrong values.
824 int fromPage
, toPage
;
825 int minPage
, maxPage
;
826 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
827 m_printDialogData
.SetAllPages(true);
829 if (minPage
< 1) minPage
= 1;
830 if (maxPage
< 1) maxPage
= 9999;
831 if (maxPage
< minPage
) maxPage
= minPage
;
833 m_printDialogData
.SetMinPage(minPage
);
834 m_printDialogData
.SetMaxPage(maxPage
);
837 if (fromPage
< minPage
) fromPage
= minPage
;
838 else if (fromPage
> maxPage
) fromPage
= maxPage
;
839 m_printDialogData
.SetFromPage(fromPage
);
843 m_printDialogData
.SetToPage(toPage
);
844 if (toPage
> maxPage
) toPage
= maxPage
;
845 else if (toPage
< minPage
) toPage
= minPage
;
848 if (((minPage
!= fromPage
) && fromPage
!= 0) || ((maxPage
!= toPage
) && toPage
!= 0)) m_printDialogData
.SetAllPages(false);
851 wxPrintData printdata
= GetPrintDialogData().GetPrintData();
852 wxGtkPrintNativeData
*native
= (wxGtkPrintNativeData
*) printdata
.GetNativeData();
854 GtkPrintOperation
*printOp
= gtk_print_operation_new ();
856 native
->SetPrintJob( printOp
);
858 printout
->SetIsPreview(false);
860 wxPrinterToGtkData dataToSend
;
861 dataToSend
.printer
= this;
862 dataToSend
.printout
= printout
;
864 // These Gtk signals are caught here.
865 g_signal_connect (printOp
, "begin-print", G_CALLBACK (gtk_begin_print_callback
), &dataToSend
);
866 g_signal_connect (printOp
, "draw-page", G_CALLBACK (gtk_draw_page_print_callback
), &dataToSend
);
867 g_signal_connect (printOp
, "end-print", G_CALLBACK (gtk_end_print_callback
), printout
);
868 g_signal_connect (printOp
, "preview", G_CALLBACK (gtk_preview_print_callback
), printout
);
870 // This is used to setup the DC and
871 // show the dialog if desired
872 wxGtkPrintDialog
dialog( parent
, &m_printDialogData
);
873 dialog
.SetPrintDC(m_dc
);
874 dialog
.SetShowDialog(prompt
);
876 // doesn't necessarily show
877 int ret
= dialog
.ShowModal();
878 if (ret
== wxID_CANCEL
)
880 sm_lastError
= wxPRINTER_CANCELLED
;
884 sm_lastError
= wxPRINTER_ERROR
;
885 wxFAIL_MSG(_("The print dialog returned an error."));
888 g_object_unref (printOp
);
890 return (sm_lastError
== wxPRINTER_NO_ERROR
);
893 void wxGtkPrinter::BeginPrint(wxPrintout
*printout
, GtkPrintOperation
*operation
, GtkPrintContext
*context
)
895 wxPrintData printdata
= GetPrintDialogData().GetPrintData();
896 wxGtkPrintNativeData
*native
= (wxGtkPrintNativeData
*) printdata
.GetNativeData();
898 // We need to update printdata with the new data from the dialog and we
899 // have to do this here because this method needs this new data and we
900 // cannot update it earlier
901 native
->SetPrintConfig(gtk_print_operation_get_print_settings(operation
));
902 printdata
.ConvertFromNative();
904 SetPrintContext(context
);
905 native
->SetPrintContext( context
);
907 wxPrinterDC
*printDC
= new wxPrinterDC( printdata
);
912 if (sm_lastError
!= wxPRINTER_CANCELLED
)
914 sm_lastError
= wxPRINTER_ERROR
;
915 wxFAIL_MSG(_("The wxGtkPrinterDC cannot be used."));
920 printout
->SetPPIScreen(wxGetDisplayPPI());
921 printout
->SetPPIPrinter( printDC
->GetResolution(),
922 printDC
->GetResolution() );
924 printout
->SetDC(m_dc
);
927 m_dc
->GetSize(&w
, &h
);
928 printout
->SetPageSizePixels((int)w
, (int)h
);
929 printout
->SetPaperRectPixels(wxRect(0, 0, w
, h
));
931 m_dc
->GetSizeMM(&mw
, &mh
);
932 printout
->SetPageSizeMM((int)mw
, (int)mh
);
933 printout
->OnPreparePrinting();
935 // Get some parameters from the printout, if defined.
936 int fromPage
, toPage
;
937 int minPage
, maxPage
;
938 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
942 sm_lastError
= wxPRINTER_ERROR
;
943 wxFAIL_MSG(_("wxPrintout::GetPageInfo gives a null maxPage."));
947 printout
->OnBeginPrinting();
951 // If we're not previewing we need to calculate the number of pages to print.
952 // If we're previewing, Gtk Print will render every pages without wondering about the page ranges the user may
953 // have defined in the dialog. So the number of pages is the maximum available.
954 if (!printout
->IsPreview())
956 GtkPrintSettings
* settings
= gtk_print_operation_get_print_settings (operation
);
957 switch (gtk_print_settings_get_print_pages(settings
))
959 case GTK_PRINT_PAGES_CURRENT
:
962 case GTK_PRINT_PAGES_RANGES
:
963 {gint num_ranges
= 0;
966 range
= gtk_print_settings_get_page_ranges (settings
, &num_ranges
);
967 for (i
=0; i
<num_ranges
; i
++)
969 if (range
[i
].end
< range
[i
].start
) range
[i
].end
= range
[i
].start
;
970 if (range
[i
].start
< minPage
-1) range
[i
].start
= minPage
-1;
971 if (range
[i
].end
> maxPage
-1) range
[i
].end
= maxPage
-1;
972 if (range
[i
].start
> maxPage
-1) range
[i
].start
= maxPage
-1;
973 numPages
+= range
[i
].end
- range
[i
].start
+ 1;
975 gtk_print_settings_set_page_ranges (settings
, range
, 1);
977 case GTK_PRINT_PAGES_ALL
:
979 numPages
= maxPage
- minPage
+ 1;
983 else numPages
= maxPage
- minPage
+ 1;
985 gtk_print_operation_set_n_pages(operation
, numPages
);
988 void wxGtkPrinter::DrawPage(wxPrintout
*printout
,
989 GtkPrintOperation
*operation
,
990 GtkPrintContext
* WXUNUSED(context
),
993 int fromPage
, toPage
, minPage
, maxPage
, startPage
, endPage
;
994 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
996 int numPageToDraw
= page_nr
+ minPage
;
997 if (numPageToDraw
< minPage
) numPageToDraw
= minPage
;
998 if (numPageToDraw
> maxPage
) numPageToDraw
= maxPage
;
1000 GtkPrintSettings
* settings
= gtk_print_operation_get_print_settings (operation
);
1001 switch (gtk_print_settings_get_print_pages(settings
))
1003 case GTK_PRINT_PAGES_CURRENT
:
1004 g_object_get_property((GObject
*) operation
, (const gchar
*) "current-page", (GValue
*) &startPage
);
1005 g_object_get_property((GObject
*) operation
, (const gchar
*) "current-page", (GValue
*) &endPage
);
1007 case GTK_PRINT_PAGES_RANGES
:
1008 {gint num_ranges
= 0;
1009 GtkPageRange
* range
;
1010 range
= gtk_print_settings_get_page_ranges (settings
, &num_ranges
);
1011 // We don't need to verify these values as it has already been done in wxGtkPrinter::BeginPrint.
1012 if (num_ranges
>= 1)
1014 startPage
= range
[0].start
+ 1;
1015 endPage
= range
[0].end
+ 1;
1018 startPage
= minPage
;
1022 case GTK_PRINT_PAGES_ALL
:
1024 startPage
= minPage
;
1029 if(numPageToDraw
== startPage
)
1031 if (!printout
->OnBeginDocument(startPage
, endPage
))
1033 wxLogError(_("Could not start printing."));
1034 sm_lastError
= wxPRINTER_ERROR
;
1038 // The app can render the page numPageToDraw.
1039 if (printout
->HasPage(numPageToDraw
))
1042 printout
->OnPrintPage(numPageToDraw
);
1047 if(numPageToDraw
== endPage
)
1049 printout
->OnEndDocument();
1053 wxDC
* wxGtkPrinter::PrintDialog( wxWindow
*parent
)
1055 wxGtkPrintDialog
dialog( parent
, &m_printDialogData
);
1057 dialog
.SetPrintDC(m_dc
);
1058 dialog
.SetShowDialog(true);
1060 int ret
= dialog
.ShowModal();
1062 if (ret
== wxID_CANCEL
)
1064 sm_lastError
= wxPRINTER_CANCELLED
;
1069 sm_lastError
= wxPRINTER_ERROR
;
1070 wxFAIL_MSG(_("The print dialog returned an error."));
1074 m_printDialogData
= dialog
.GetPrintDialogData();
1076 return new wxPrinterDC( m_printDialogData
.GetPrintData() );
1079 bool wxGtkPrinter::Setup( wxWindow
* WXUNUSED(parent
) )
1081 // Obsolete, for backward compatibility.
1085 //-----------------------------------------------------------------------------
1087 //-----------------------------------------------------------------------------
1089 #define wxCAIRO_SCALE 1
1093 #define XLOG2DEV(x) LogicalToDeviceX(x)
1094 #define XLOG2DEVREL(x) LogicalToDeviceXRel(x)
1095 #define YLOG2DEV(x) LogicalToDeviceY(x)
1096 #define YLOG2DEVREL(x) LogicalToDeviceYRel(x)
1100 #define XLOG2DEV(x) ((double)(LogicalToDeviceX(x)) * m_DEV2PS)
1101 #define XLOG2DEVREL(x) ((double)(LogicalToDeviceXRel(x)) * m_DEV2PS)
1102 #define YLOG2DEV(x) ((double)(LogicalToDeviceY(x)) * m_DEV2PS)
1103 #define YLOG2DEVREL(x) ((double)(LogicalToDeviceYRel(x)) * m_DEV2PS)
1107 IMPLEMENT_ABSTRACT_CLASS(wxGtkPrinterDCImpl
, wxDCImpl
)
1109 wxGtkPrinterDCImpl::wxGtkPrinterDCImpl(wxPrinterDC
*owner
, const wxPrintData
& data
)
1114 wxGtkPrintNativeData
*native
=
1115 (wxGtkPrintNativeData
*) m_printData
.GetNativeData();
1117 m_gpc
= native
->GetPrintContext();
1119 // Match print quality to resolution (high = 1200dpi)
1120 m_resolution
= m_printData
.GetQuality(); // (int) gtk_print_context_get_dpi_x( m_gpc );
1121 if (m_resolution
< 0)
1122 m_resolution
= (1 << (m_resolution
+4)) *150;
1124 m_context
= gtk_print_context_create_pango_context( m_gpc
);
1125 m_layout
= gtk_print_context_create_pango_layout ( m_gpc
);
1126 m_fontdesc
= pango_font_description_from_string( "Sans 12" );
1128 m_cairo
= gtk_print_context_get_cairo_context ( m_gpc
);
1134 cairo_scale( m_cairo
, 72.0 / (double)m_resolution
, 72.0 / (double)m_resolution
);
1136 m_PS2DEV
= (double)m_resolution
/ 72.0;
1137 m_DEV2PS
= 72.0 / (double)m_resolution
;
1144 m_signX
= 1; // default x-axis left to right.
1145 m_signY
= 1; // default y-axis bottom up -> top down.
1147 // By default the origin of the Cairo context is in the upper left
1148 // corner of the printable area. We need to translate it so that it
1149 // is in the upper left corner of the paper (without margins)
1150 GtkPageSetup
*setup
= gtk_print_context_get_page_setup( m_gpc
);
1152 ml
= gtk_page_setup_get_left_margin (setup
, GTK_UNIT_POINTS
);
1153 mt
= gtk_page_setup_get_top_margin (setup
, GTK_UNIT_POINTS
);
1154 cairo_translate(m_cairo
, -ml
, -mt
);
1157 wxGtkPrinterDCImpl::~wxGtkPrinterDCImpl()
1159 g_object_unref(m_context
);
1160 g_object_unref(m_layout
);
1163 bool wxGtkPrinterDCImpl::IsOk() const
1165 return m_gpc
!= NULL
;
1168 void* wxGtkPrinterDCImpl::GetCairoContext() const
1170 return (void*) cairo_reference( m_cairo
);
1173 bool wxGtkPrinterDCImpl::DoFloodFill(wxCoord
WXUNUSED(x1
),
1174 wxCoord
WXUNUSED(y1
),
1175 const wxColour
& WXUNUSED(col
),
1176 wxFloodFillStyle
WXUNUSED(style
))
1178 // We can't access the given coord as a Cairo context is scalable, ie a
1179 // coord doesn't mean anything in this context.
1180 wxFAIL_MSG(_("not implemented"));
1184 void wxGtkPrinterDCImpl::DoGradientFillConcentric(const wxRect
& rect
, const wxColour
& initialColour
, const wxColour
& destColour
, const wxPoint
& circleCenter
)
1186 wxCoord xC
= circleCenter
.x
;
1187 wxCoord yC
= circleCenter
.y
;
1188 wxCoord xR
= rect
.x
;
1189 wxCoord yR
= rect
.y
;
1190 wxCoord w
= rect
.width
;
1191 wxCoord h
= rect
.height
;
1193 const double r2
= (w
/2)*(w
/2)+(h
/2)*(h
/2);
1194 double radius
= sqrt(r2
);
1196 unsigned char redI
= initialColour
.Red();
1197 unsigned char blueI
= initialColour
.Blue();
1198 unsigned char greenI
= initialColour
.Green();
1199 unsigned char alphaI
= initialColour
.Alpha();
1200 unsigned char redD
= destColour
.Red();
1201 unsigned char blueD
= destColour
.Blue();
1202 unsigned char greenD
= destColour
.Green();
1203 unsigned char alphaD
= destColour
.Alpha();
1205 double redIPS
= (double)(redI
) / 255.0;
1206 double blueIPS
= (double)(blueI
) / 255.0;
1207 double greenIPS
= (double)(greenI
) / 255.0;
1208 double alphaIPS
= (double)(alphaI
) / 255.0;
1209 double redDPS
= (double)(redD
) / 255.0;
1210 double blueDPS
= (double)(blueD
) / 255.0;
1211 double greenDPS
= (double)(greenD
) / 255.0;
1212 double alphaDPS
= (double)(alphaD
) / 255.0;
1214 // Create a pattern with the gradient.
1215 cairo_pattern_t
* gradient
;
1216 gradient
= cairo_pattern_create_radial (XLOG2DEV(xC
+xR
), YLOG2DEV(yC
+yR
), 0, XLOG2DEV(xC
+xR
), YLOG2DEV(yC
+yR
), radius
* m_DEV2PS
);
1217 cairo_pattern_add_color_stop_rgba (gradient
, 0.0, redIPS
, greenIPS
, blueIPS
, alphaIPS
);
1218 cairo_pattern_add_color_stop_rgba (gradient
, 1.0, redDPS
, greenDPS
, blueDPS
, alphaDPS
);
1220 // Fill the rectangle with this pattern.
1221 cairo_set_source(m_cairo
, gradient
);
1222 cairo_rectangle (m_cairo
, XLOG2DEV(xR
), YLOG2DEV(yR
), XLOG2DEVREL(w
), YLOG2DEVREL(h
) );
1223 cairo_fill(m_cairo
);
1225 cairo_pattern_destroy(gradient
);
1227 CalcBoundingBox(xR
, yR
);
1228 CalcBoundingBox(xR
+w
, yR
+h
);
1231 void wxGtkPrinterDCImpl::DoGradientFillLinear(const wxRect
& rect
, const wxColour
& initialColour
, const wxColour
& destColour
, wxDirection nDirection
)
1235 wxCoord w
= rect
.width
;
1236 wxCoord h
= rect
.height
;
1238 unsigned char redI
= initialColour
.Red();
1239 unsigned char blueI
= initialColour
.Blue();
1240 unsigned char greenI
= initialColour
.Green();
1241 unsigned char alphaI
= initialColour
.Alpha();
1242 unsigned char redD
= destColour
.Red();
1243 unsigned char blueD
= destColour
.Blue();
1244 unsigned char greenD
= destColour
.Green();
1245 unsigned char alphaD
= destColour
.Alpha();
1247 double redIPS
= (double)(redI
) / 255.0;
1248 double blueIPS
= (double)(blueI
) / 255.0;
1249 double greenIPS
= (double)(greenI
) / 255.0;
1250 double alphaIPS
= (double)(alphaI
) / 255.0;
1251 double redDPS
= (double)(redD
) / 255.0;
1252 double blueDPS
= (double)(blueD
) / 255.0;
1253 double greenDPS
= (double)(greenD
) / 255.0;
1254 double alphaDPS
= (double)(alphaD
) / 255.0;
1256 // Create a pattern with the gradient.
1257 cairo_pattern_t
* gradient
;
1258 gradient
= cairo_pattern_create_linear (XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x
+w
), YLOG2DEV(y
));
1260 if (nDirection
== wxWEST
)
1262 cairo_pattern_add_color_stop_rgba (gradient
, 0.0, redDPS
, greenDPS
, blueDPS
, alphaDPS
);
1263 cairo_pattern_add_color_stop_rgba (gradient
, 1.0, redIPS
, greenIPS
, blueIPS
, alphaIPS
);
1266 cairo_pattern_add_color_stop_rgba (gradient
, 0.0, redIPS
, greenIPS
, blueIPS
, alphaIPS
);
1267 cairo_pattern_add_color_stop_rgba (gradient
, 1.0, redDPS
, greenDPS
, blueDPS
, alphaDPS
);
1270 // Fill the rectangle with this pattern.
1271 cairo_set_source(m_cairo
, gradient
);
1272 cairo_rectangle (m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEVREL(w
), YLOG2DEVREL(h
) );
1273 cairo_fill(m_cairo
);
1275 cairo_pattern_destroy(gradient
);
1277 CalcBoundingBox(x
, y
);
1278 CalcBoundingBox(x
+w
, y
+h
);
1281 bool wxGtkPrinterDCImpl::DoGetPixel(wxCoord
WXUNUSED(x1
),
1282 wxCoord
WXUNUSED(y1
),
1283 wxColour
* WXUNUSED(col
)) const
1285 wxFAIL_MSG(_("not implemented"));
1289 void wxGtkPrinterDCImpl::DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
1291 if (m_pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
) return;
1294 cairo_move_to ( m_cairo
, XLOG2DEV(x1
), YLOG2DEV(y1
) );
1295 cairo_line_to ( m_cairo
, XLOG2DEV(x2
), YLOG2DEV(y2
) );
1296 cairo_stroke ( m_cairo
);
1298 CalcBoundingBox( x1
, y1
);
1299 CalcBoundingBox( x2
, y2
);
1302 void wxGtkPrinterDCImpl::DoCrossHair(wxCoord x
, wxCoord y
)
1309 cairo_move_to (m_cairo
, XLOG2DEV(x
), 0);
1310 cairo_line_to (m_cairo
, XLOG2DEV(x
), YLOG2DEVREL(h
));
1311 cairo_move_to (m_cairo
, 0, YLOG2DEV(y
));
1312 cairo_line_to (m_cairo
, XLOG2DEVREL(w
), YLOG2DEV(y
));
1314 cairo_stroke (m_cairo
);
1315 CalcBoundingBox( 0, 0 );
1316 CalcBoundingBox( w
, h
);
1319 void wxGtkPrinterDCImpl::DoDrawArc(wxCoord x1
,wxCoord y1
,wxCoord x2
,wxCoord y2
,wxCoord xc
,wxCoord yc
)
1321 double dx
= x1
- xc
;
1322 double dy
= y1
- yc
;
1323 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
1325 double alpha1
, alpha2
;
1326 if (x1
== x2
&& y1
== y2
)
1334 alpha1
= alpha2
= 0.0;
1338 alpha1
= (x1
- xc
== 0) ?
1339 (y1
- yc
< 0) ? 90.0 : -90.0 :
1340 atan2(double(y1
-yc
), double(x1
-xc
)) * RAD2DEG
;
1341 alpha2
= (x2
- xc
== 0) ?
1342 (y2
- yc
< 0) ? 90.0 : -90.0 :
1343 atan2(double(y2
-yc
), double(x2
-xc
)) * RAD2DEG
;
1345 while (alpha1
<= 0) alpha1
+= 360;
1346 while (alpha2
<= 0) alpha2
+= 360; // adjust angles to be between.
1347 while (alpha1
> 360) alpha1
-= 360; // 0 and 360 degree.
1348 while (alpha2
> 360) alpha2
-= 360;
1354 cairo_new_path(m_cairo
);
1356 cairo_arc_negative ( m_cairo
, XLOG2DEV(xc
), YLOG2DEV(yc
), XLOG2DEVREL((int)radius
), alpha1
, alpha2
);
1357 cairo_line_to(m_cairo
, XLOG2DEV(xc
), YLOG2DEV(yc
));
1358 cairo_close_path (m_cairo
);
1360 SetBrush( m_brush
);
1361 cairo_fill_preserve( m_cairo
);
1364 cairo_stroke( m_cairo
);
1366 CalcBoundingBox (x1
, y1
);
1367 CalcBoundingBox (xc
, yc
);
1368 CalcBoundingBox (x2
, y2
);
1371 void wxGtkPrinterDCImpl::DoDrawEllipticArc(wxCoord x
,wxCoord y
,wxCoord w
,wxCoord h
,double sa
,double ea
)
1373 cairo_save( m_cairo
);
1375 cairo_new_path(m_cairo
);
1377 cairo_translate( m_cairo
, XLOG2DEV((wxCoord
) (x
+ w
/ 2.)), XLOG2DEV((wxCoord
) (y
+ h
/ 2.)) );
1378 double scale
= (double)YLOG2DEVREL(h
) / (double) XLOG2DEVREL(w
);
1379 cairo_scale( m_cairo
, 1.0, scale
);
1381 cairo_arc_negative ( m_cairo
, 0, 0, XLOG2DEVREL(w
/2), -sa
*DEG2RAD
, -ea
*DEG2RAD
);
1384 cairo_stroke_preserve( m_cairo
);
1386 cairo_line_to(m_cairo
, 0,0);
1388 SetBrush( m_brush
);
1389 cairo_fill( m_cairo
);
1391 cairo_restore( m_cairo
);
1393 CalcBoundingBox( x
, y
);
1394 CalcBoundingBox( x
+w
, y
+h
);
1397 void wxGtkPrinterDCImpl::DoDrawPoint(wxCoord x
, wxCoord y
)
1399 if (m_pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
) return;
1403 cairo_move_to ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
) );
1404 cairo_line_to ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
) );
1405 cairo_stroke ( m_cairo
);
1407 CalcBoundingBox( x
, y
);
1410 void wxGtkPrinterDCImpl::DoDrawLines(int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
1412 if (m_pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
) return;
1419 for ( i
=0; i
<n
; i
++ )
1420 CalcBoundingBox( points
[i
].x
+xoffset
, points
[i
].y
+yoffset
);
1422 cairo_move_to ( m_cairo
, XLOG2DEV(points
[0].x
+xoffset
), YLOG2DEV(points
[0].y
+yoffset
) );
1424 for (i
= 1; i
< n
; i
++)
1425 cairo_line_to ( m_cairo
, XLOG2DEV(points
[i
].x
+xoffset
), YLOG2DEV(points
[i
].y
+yoffset
) );
1427 cairo_stroke ( m_cairo
);
1430 void wxGtkPrinterDCImpl::DoDrawPolygon(int n
, wxPoint points
[],
1431 wxCoord xoffset
, wxCoord yoffset
,
1432 wxPolygonFillMode fillStyle
)
1436 cairo_save(m_cairo
);
1437 if (fillStyle
== wxWINDING_RULE
)
1438 cairo_set_fill_rule( m_cairo
, CAIRO_FILL_RULE_WINDING
);
1440 cairo_set_fill_rule( m_cairo
, CAIRO_FILL_RULE_EVEN_ODD
);
1442 int x
= points
[0].x
+ xoffset
;
1443 int y
= points
[0].y
+ yoffset
;
1444 cairo_new_path(m_cairo
);
1445 cairo_move_to( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
) );
1447 for (i
= 1; i
< n
; i
++)
1449 int x
= points
[i
].x
+ xoffset
;
1450 int y
= points
[i
].y
+ yoffset
;
1451 cairo_line_to( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
) );
1453 cairo_close_path(m_cairo
);
1455 SetBrush( m_brush
);
1456 cairo_fill_preserve( m_cairo
);
1459 cairo_stroke( m_cairo
);
1461 CalcBoundingBox( x
, y
);
1463 cairo_restore(m_cairo
);
1466 void wxGtkPrinterDCImpl::DoDrawPolyPolygon(int n
, int count
[], wxPoint points
[],
1467 wxCoord xoffset
, wxCoord yoffset
,
1468 wxPolygonFillMode fillStyle
)
1470 wxDCImpl::DoDrawPolyPolygon( n
, count
, points
, xoffset
, yoffset
, fillStyle
);
1473 void wxGtkPrinterDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1478 cairo_new_path(m_cairo
);
1479 cairo_rectangle ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEVREL(width
), YLOG2DEVREL(height
));
1481 SetBrush( m_brush
);
1482 cairo_fill_preserve( m_cairo
);
1485 cairo_stroke( m_cairo
);
1487 CalcBoundingBox( x
, y
);
1488 CalcBoundingBox( x
+ width
, y
+ height
);
1491 void wxGtkPrinterDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
1496 if (radius
< 0.0) radius
= - radius
* ((width
< height
) ? width
: height
);
1498 wxCoord dd
= 2 * (wxCoord
) radius
;
1499 if (dd
> width
) dd
= width
;
1500 if (dd
> height
) dd
= height
;
1503 wxCoord rad
= (wxCoord
) radius
;
1505 cairo_new_path(m_cairo
);
1506 cairo_move_to(m_cairo
,XLOG2DEV(x
+ rad
),YLOG2DEV(y
));
1507 cairo_curve_to(m_cairo
,
1508 XLOG2DEV(x
+ rad
),YLOG2DEV(y
),
1509 XLOG2DEV(x
),YLOG2DEV(y
),
1510 XLOG2DEV(x
),YLOG2DEV(y
+ rad
));
1511 cairo_line_to(m_cairo
,XLOG2DEV(x
),YLOG2DEV(y
+ height
- rad
));
1512 cairo_curve_to(m_cairo
,
1513 XLOG2DEV(x
),YLOG2DEV(y
+ height
- rad
),
1514 XLOG2DEV(x
),YLOG2DEV(y
+ height
),
1515 XLOG2DEV(x
+ rad
),YLOG2DEV(y
+ height
));
1516 cairo_line_to(m_cairo
,XLOG2DEV(x
+ width
- rad
),YLOG2DEV(y
+ height
));
1517 cairo_curve_to(m_cairo
,
1518 XLOG2DEV(x
+ width
- rad
),YLOG2DEV(y
+ height
),
1519 XLOG2DEV(x
+ width
),YLOG2DEV(y
+ height
),
1520 XLOG2DEV(x
+ width
),YLOG2DEV(y
+ height
- rad
));
1521 cairo_line_to(m_cairo
,XLOG2DEV(x
+ width
),YLOG2DEV(y
+ rad
));
1522 cairo_curve_to(m_cairo
,
1523 XLOG2DEV(x
+ width
),YLOG2DEV(y
+ rad
),
1524 XLOG2DEV(x
+ width
),YLOG2DEV(y
),
1525 XLOG2DEV(x
+ width
- rad
),YLOG2DEV(y
));
1526 cairo_line_to(m_cairo
,XLOG2DEV(x
+ rad
),YLOG2DEV(y
));
1527 cairo_close_path(m_cairo
);
1530 cairo_fill_preserve(m_cairo
);
1533 cairo_stroke(m_cairo
);
1535 CalcBoundingBox(x
,y
);
1536 CalcBoundingBox(x
+width
,y
+height
);
1539 void wxGtkPrinterDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1544 cairo_save (m_cairo
);
1546 cairo_new_path(m_cairo
);
1548 cairo_translate (m_cairo
, XLOG2DEV((wxCoord
) (x
+ width
/ 2.)), YLOG2DEV((wxCoord
) (y
+ height
/ 2.)));
1549 cairo_scale(m_cairo
, 1, (double)YLOG2DEVREL(height
)/(double)XLOG2DEVREL(width
));
1550 cairo_arc ( m_cairo
, 0, 0, XLOG2DEVREL(width
/2), 0, 2 * M_PI
);
1552 SetBrush( m_brush
);
1553 cairo_fill_preserve( m_cairo
);
1556 cairo_stroke( m_cairo
);
1558 CalcBoundingBox( x
, y
);
1559 CalcBoundingBox( x
+ width
, y
+ height
);
1561 cairo_restore (m_cairo
);
1565 void wxGtkPrinterDCImpl::DoDrawSpline(const wxPointList
*points
)
1569 double c
, d
, x1
, y1
, x2
, y2
, x3
, y3
;
1572 wxPointList::compatibility_iterator node
= points
->GetFirst();
1573 p
= node
->GetData();
1577 node
= node
->GetNext();
1578 p
= node
->GetData();
1582 (double)(x1
+ c
) / 2;
1584 (double)(y1
+ d
) / 2;
1586 cairo_new_path( m_cairo
);
1587 cairo_move_to( m_cairo
, XLOG2DEV((wxCoord
)x1
), YLOG2DEV((wxCoord
)y1
) );
1588 cairo_line_to( m_cairo
, XLOG2DEV((wxCoord
)x3
), YLOG2DEV((wxCoord
)y3
) );
1590 CalcBoundingBox( (wxCoord
)x1
, (wxCoord
)y1
);
1591 CalcBoundingBox( (wxCoord
)x3
, (wxCoord
)y3
);
1593 node
= node
->GetNext();
1596 q
= node
->GetData();
1604 x3
= (double)(x2
+ c
) / 2;
1605 y3
= (double)(y2
+ d
) / 2;
1607 cairo_curve_to(m_cairo
,
1608 XLOG2DEV((wxCoord
)x1
), YLOG2DEV((wxCoord
)y1
),
1609 XLOG2DEV((wxCoord
)x2
), YLOG2DEV((wxCoord
)y2
),
1610 XLOG2DEV((wxCoord
)x3
), YLOG2DEV((wxCoord
)y3
) );
1612 CalcBoundingBox( (wxCoord
)x1
, (wxCoord
)y1
);
1613 CalcBoundingBox( (wxCoord
)x3
, (wxCoord
)y3
);
1615 node
= node
->GetNext();
1618 cairo_line_to ( m_cairo
, XLOG2DEV((wxCoord
)c
), YLOG2DEV((wxCoord
)d
) );
1620 cairo_stroke( m_cairo
);
1622 #endif // wxUSE_SPLINES
1624 bool wxGtkPrinterDCImpl::DoBlit(wxCoord xdest
, wxCoord ydest
,
1625 wxCoord width
, wxCoord height
,
1626 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
1627 wxRasterOperationMode rop
, bool useMask
,
1628 wxCoord
WXUNUSED_UNLESS_DEBUG(xsrcMask
),
1629 wxCoord
WXUNUSED_UNLESS_DEBUG(ysrcMask
))
1631 wxASSERT_MSG( xsrcMask
== wxDefaultCoord
&& ysrcMask
== wxDefaultCoord
,
1632 wxT("mask coordinates are not supported") );
1634 wxCHECK_MSG( source
, false, wxT("invalid source dc") );
1636 // Blit into a bitmap.
1637 wxBitmap
bitmap( width
, height
);
1639 memDC
.SelectObject(bitmap
);
1640 memDC
.Blit(0, 0, width
, height
, source
, xsrc
, ysrc
, rop
);
1641 memDC
.SelectObject(wxNullBitmap
);
1643 // Draw bitmap. scaling and positioning is done there.
1644 GetOwner()->DrawBitmap( bitmap
, xdest
, ydest
, useMask
);
1649 void wxGtkPrinterDCImpl::DoDrawIcon( const wxIcon
& icon
, wxCoord x
, wxCoord y
)
1651 DoDrawBitmap( icon
, x
, y
, true );
1654 void wxGtkPrinterDCImpl::DoDrawBitmap( const wxBitmap
& bitmap
, wxCoord x
, wxCoord y
, bool useMask
)
1656 wxCHECK_RET( bitmap
.IsOk(), wxT("Invalid bitmap in wxGtkPrinterDCImpl::DoDrawBitmap"));
1658 x
= wxCoord(XLOG2DEV(x
));
1659 y
= wxCoord(YLOG2DEV(y
));
1660 int bw
= bitmap
.GetWidth();
1661 int bh
= bitmap
.GetHeight();
1662 wxBitmap bmpSource
= bitmap
; // we need a non-const instance.
1663 if (!useMask
&& !bitmap
.HasPixbuf() && bitmap
.GetMask())
1664 bmpSource
.SetMask(NULL
);
1666 cairo_save(m_cairo
);
1668 // Prepare to draw the image.
1669 cairo_translate(m_cairo
, x
, y
);
1672 wxDouble scaleX
= (wxDouble
) XLOG2DEVREL(bw
) / (wxDouble
) bw
;
1673 wxDouble scaleY
= (wxDouble
) YLOG2DEVREL(bh
) / (wxDouble
) bh
;
1674 cairo_scale(m_cairo
, scaleX
, scaleY
);
1676 gdk_cairo_set_source_pixbuf(m_cairo
, bmpSource
.GetPixbuf(), 0, 0);
1677 cairo_pattern_set_filter(cairo_get_source(m_cairo
), CAIRO_FILTER_NEAREST
);
1678 // Use the original size here since the context is scaled already.
1679 cairo_rectangle(m_cairo
, 0, 0, bw
, bh
);
1680 // Fill the rectangle using the pattern.
1681 cairo_fill(m_cairo
);
1683 CalcBoundingBox(0,0);
1684 CalcBoundingBox(bw
,bh
);
1686 cairo_restore(m_cairo
);
1689 void wxGtkPrinterDCImpl::DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
)
1691 DoDrawRotatedText( text
, x
, y
, 0.0 );
1694 void wxGtkPrinterDCImpl::DoDrawRotatedText(const wxString
& text
, wxCoord x
, wxCoord y
, double angle
)
1696 double xx
= XLOG2DEV(x
);
1697 double yy
= YLOG2DEV(y
);
1701 bool underlined
= m_font
.Ok() && m_font
.GetUnderlined();
1703 const wxUTF8Buf data
= text
.utf8_str();
1705 size_t datalen
= strlen(data
);
1706 pango_layout_set_text( m_layout
, data
, datalen
);
1710 PangoAttrList
*attrs
= pango_attr_list_new();
1711 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1713 a
->end_index
= datalen
;
1714 pango_attr_list_insert(attrs
, a
);
1715 pango_layout_set_attributes(m_layout
, attrs
);
1716 pango_attr_list_unref(attrs
);
1719 if (m_textForegroundColour
.Ok())
1721 unsigned char red
= m_textForegroundColour
.Red();
1722 unsigned char blue
= m_textForegroundColour
.Blue();
1723 unsigned char green
= m_textForegroundColour
.Green();
1724 unsigned char alpha
= m_textForegroundColour
.Alpha();
1726 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
&& alpha
== m_currentAlpha
))
1728 double redPS
= (double)(red
) / 255.0;
1729 double bluePS
= (double)(blue
) / 255.0;
1730 double greenPS
= (double)(green
) / 255.0;
1731 double alphaPS
= (double)(alpha
) / 255.0;
1733 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1736 m_currentBlue
= blue
;
1737 m_currentGreen
= green
;
1738 m_currentAlpha
= alpha
;
1743 cairo_move_to (m_cairo
, xx
, yy
);
1745 cairo_save( m_cairo
);
1747 if (fabs(angle
) > 0.00001)
1748 cairo_rotate( m_cairo
, angle
*DEG2RAD
);
1750 cairo_scale(m_cairo
, m_scaleX
, m_scaleY
);
1753 pango_layout_get_pixel_size( m_layout
, &w
, &h
);
1755 if ( m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1757 unsigned char red
= m_textBackgroundColour
.Red();
1758 unsigned char blue
= m_textBackgroundColour
.Blue();
1759 unsigned char green
= m_textBackgroundColour
.Green();
1760 unsigned char alpha
= m_textBackgroundColour
.Alpha();
1762 double redPS
= (double)(red
) / 255.0;
1763 double bluePS
= (double)(blue
) / 255.0;
1764 double greenPS
= (double)(green
) / 255.0;
1765 double alphaPS
= (double)(alpha
) / 255.0;
1767 cairo_save(m_cairo
);
1768 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1769 cairo_rectangle(m_cairo
, 0, 0, w
, h
); // still in cairo units
1770 cairo_fill(m_cairo
);
1771 cairo_restore(m_cairo
);
1774 pango_cairo_update_layout (m_cairo
, m_layout
);
1775 pango_cairo_show_layout (m_cairo
, m_layout
);
1777 cairo_restore( m_cairo
);
1781 // Undo underline attributes setting
1782 pango_layout_set_attributes(m_layout
, NULL
);
1785 // Back to device units:
1786 CalcBoundingBox (x
, y
);
1787 CalcBoundingBox (x
+ w
, y
+ h
);
1790 void wxGtkPrinterDCImpl::Clear()
1792 // Clear does nothing for printing, but keep the code
1795 cairo_save(m_cairo);
1796 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
1797 SetBrush(m_backgroundBrush);
1798 cairo_paint(m_cairo);
1799 cairo_restore(m_cairo);
1803 void wxGtkPrinterDCImpl::SetFont( const wxFont
& font
)
1810 pango_font_description_free( m_fontdesc
);
1812 m_fontdesc
= pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
1814 float size
= pango_font_description_get_size( m_fontdesc
);
1815 size
= size
* GetFontPointSizeAdjustment(72.0);
1816 pango_font_description_set_size( m_fontdesc
, (gint
)size
);
1818 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1822 void wxGtkPrinterDCImpl::SetPen( const wxPen
& pen
)
1824 if (!pen
.Ok()) return;
1830 if (m_pen
.GetWidth() <= 0)
1833 width
= (double) m_pen
.GetWidth();
1835 cairo_set_line_width( m_cairo
, width
* m_DEV2PS
* m_scaleX
);
1836 static const double dotted
[] = {2.0, 5.0};
1837 static const double short_dashed
[] = {4.0, 4.0};
1838 static const double long_dashed
[] = {4.0, 8.0};
1839 static const double dotted_dashed
[] = {6.0, 6.0, 2.0, 6.0};
1841 switch (m_pen
.GetStyle())
1843 case wxPENSTYLE_DOT
: cairo_set_dash( m_cairo
, dotted
, 2, 0 ); break;
1844 case wxPENSTYLE_SHORT_DASH
: cairo_set_dash( m_cairo
, short_dashed
, 2, 0 ); break;
1845 case wxPENSTYLE_LONG_DASH
: cairo_set_dash( m_cairo
, long_dashed
, 2, 0 ); break;
1846 case wxPENSTYLE_DOT_DASH
: cairo_set_dash( m_cairo
, dotted_dashed
, 4, 0 ); break;
1847 case wxPENSTYLE_USER_DASH
:
1850 int num
= m_pen
.GetDashes (&wx_dashes
);
1851 gdouble
*g_dashes
= g_new( gdouble
, num
);
1853 for (i
= 0; i
< num
; ++i
)
1854 g_dashes
[i
] = (gdouble
) wx_dashes
[i
];
1855 cairo_set_dash( m_cairo
, g_dashes
, num
, 0);
1859 case wxPENSTYLE_SOLID
:
1860 case wxPENSTYLE_TRANSPARENT
:
1861 default: cairo_set_dash( m_cairo
, NULL
, 0, 0 ); break;
1864 switch (m_pen
.GetCap())
1866 case wxCAP_PROJECTING
: cairo_set_line_cap (m_cairo
, CAIRO_LINE_CAP_SQUARE
); break;
1867 case wxCAP_BUTT
: cairo_set_line_cap (m_cairo
, CAIRO_LINE_CAP_BUTT
); break;
1869 default: cairo_set_line_cap (m_cairo
, CAIRO_LINE_CAP_ROUND
); break;
1872 switch (m_pen
.GetJoin())
1874 case wxJOIN_BEVEL
: cairo_set_line_join (m_cairo
, CAIRO_LINE_JOIN_BEVEL
); break;
1875 case wxJOIN_MITER
: cairo_set_line_join (m_cairo
, CAIRO_LINE_JOIN_MITER
); break;
1877 default: cairo_set_line_join (m_cairo
, CAIRO_LINE_JOIN_ROUND
); break;
1880 unsigned char red
= m_pen
.GetColour().Red();
1881 unsigned char blue
= m_pen
.GetColour().Blue();
1882 unsigned char green
= m_pen
.GetColour().Green();
1883 unsigned char alpha
= m_pen
.GetColour().Alpha();
1885 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
&& alpha
== m_currentAlpha
))
1887 double redPS
= (double)(red
) / 255.0;
1888 double bluePS
= (double)(blue
) / 255.0;
1889 double greenPS
= (double)(green
) / 255.0;
1890 double alphaPS
= (double)(alpha
) / 255.0;
1892 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1895 m_currentBlue
= blue
;
1896 m_currentGreen
= green
;
1897 m_currentAlpha
= alpha
;
1901 void wxGtkPrinterDCImpl::SetBrush( const wxBrush
& brush
)
1903 if (!brush
.Ok()) return;
1907 if (m_brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
1909 cairo_set_source_rgba( m_cairo
, 0, 0, 0, 0 );
1918 unsigned char red
= m_brush
.GetColour().Red();
1919 unsigned char blue
= m_brush
.GetColour().Blue();
1920 unsigned char green
= m_brush
.GetColour().Green();
1921 unsigned char alpha
= m_brush
.GetColour().Alpha();
1923 double redPS
= (double)(red
) / 255.0;
1924 double bluePS
= (double)(blue
) / 255.0;
1925 double greenPS
= (double)(green
) / 255.0;
1926 double alphaPS
= (double)(alpha
) / 255.0;
1928 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
&& alpha
== m_currentAlpha
))
1930 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1933 m_currentBlue
= blue
;
1934 m_currentGreen
= green
;
1935 m_currentAlpha
= alpha
;
1938 if (m_brush
.IsHatch())
1941 cairo_surface_t
*surface
;
1942 surface
= cairo_surface_create_similar(cairo_get_target(m_cairo
),CAIRO_CONTENT_COLOR_ALPHA
,10,10);
1943 cr
= cairo_create(surface
);
1944 cairo_set_line_cap(cr
, CAIRO_LINE_CAP_SQUARE
);
1945 cairo_set_line_width(cr
, 1);
1946 cairo_set_line_join(cr
,CAIRO_LINE_JOIN_MITER
);
1948 switch (m_brush
.GetStyle())
1950 case wxBRUSHSTYLE_CROSS_HATCH
:
1951 cairo_move_to(cr
, 5, 0);
1952 cairo_line_to(cr
, 5, 10);
1953 cairo_move_to(cr
, 0, 5);
1954 cairo_line_to(cr
, 10, 5);
1956 case wxBRUSHSTYLE_BDIAGONAL_HATCH
:
1957 cairo_move_to(cr
, 0, 10);
1958 cairo_line_to(cr
, 10, 0);
1960 case wxBRUSHSTYLE_FDIAGONAL_HATCH
:
1961 cairo_move_to(cr
, 0, 0);
1962 cairo_line_to(cr
, 10, 10);
1964 case wxBRUSHSTYLE_CROSSDIAG_HATCH
:
1965 cairo_move_to(cr
, 0, 0);
1966 cairo_line_to(cr
, 10, 10);
1967 cairo_move_to(cr
, 10, 0);
1968 cairo_line_to(cr
, 0, 10);
1970 case wxBRUSHSTYLE_HORIZONTAL_HATCH
:
1971 cairo_move_to(cr
, 0, 5);
1972 cairo_line_to(cr
, 10, 5);
1974 case wxBRUSHSTYLE_VERTICAL_HATCH
:
1975 cairo_move_to(cr
, 5, 0);
1976 cairo_line_to(cr
, 5, 10);
1979 wxFAIL_MSG(_("Couldn't get hatch style from wxBrush."));
1982 cairo_set_source_rgba(cr
, redPS
, greenPS
, bluePS
, alphaPS
);
1986 cairo_pattern_t
* pattern
= cairo_pattern_create_for_surface (surface
);
1987 cairo_surface_destroy(surface
);
1988 cairo_pattern_set_extend (pattern
, CAIRO_EXTEND_REPEAT
);
1989 cairo_set_source(m_cairo
, pattern
);
1990 cairo_pattern_destroy(pattern
);
1994 void wxGtkPrinterDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
1996 if (function
== wxCLEAR
)
1997 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_CLEAR
);
1998 else if (function
== wxOR
)
1999 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_OUT
);
2000 else if (function
== wxNO_OP
)
2001 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_DEST
);
2002 else if (function
== wxAND
)
2003 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_ADD
);
2004 else if (function
== wxSET
)
2005 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_SATURATE
);
2006 else if (function
== wxXOR
)
2007 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_XOR
);
2008 else // wxCOPY or anything else.
2009 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_SOURCE
);
2012 void wxGtkPrinterDCImpl::SetBackground( const wxBrush
& brush
)
2014 m_backgroundBrush
= brush
;
2015 cairo_save(m_cairo
);
2016 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_DEST_OVER
);
2018 SetBrush(m_backgroundBrush
);
2019 cairo_paint(m_cairo
);
2020 cairo_restore(m_cairo
);
2023 void wxGtkPrinterDCImpl::SetBackgroundMode(int mode
)
2025 if (mode
== wxBRUSHSTYLE_SOLID
)
2026 m_backgroundMode
= wxBRUSHSTYLE_SOLID
;
2028 m_backgroundMode
= wxBRUSHSTYLE_TRANSPARENT
;
2031 void wxGtkPrinterDCImpl::DoSetClippingRegion(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
2033 cairo_rectangle ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEVREL(width
), YLOG2DEVREL(height
));
2034 cairo_clip(m_cairo
);
2037 void wxGtkPrinterDCImpl::DestroyClippingRegion()
2039 cairo_reset_clip(m_cairo
);
2042 bool wxGtkPrinterDCImpl::StartDoc(const wxString
& WXUNUSED(message
))
2047 void wxGtkPrinterDCImpl::EndDoc()
2052 void wxGtkPrinterDCImpl::StartPage()
2057 void wxGtkPrinterDCImpl::EndPage()
2062 wxCoord
wxGtkPrinterDCImpl::GetCharHeight() const
2064 pango_layout_set_text( m_layout
, "H", 1 );
2067 pango_layout_get_pixel_size( m_layout
, &w
, &h
);
2069 return wxRound( h
* m_PS2DEV
);
2072 wxCoord
wxGtkPrinterDCImpl::GetCharWidth() const
2074 pango_layout_set_text( m_layout
, "H", 1 );
2077 pango_layout_get_pixel_size( m_layout
, &w
, &h
);
2079 return wxRound( w
* m_PS2DEV
);
2082 void wxGtkPrinterDCImpl::DoGetTextExtent(const wxString
& string
, wxCoord
*width
, wxCoord
*height
,
2084 wxCoord
*externalLeading
,
2085 const wxFont
*theFont
) const
2093 if ( externalLeading
)
2094 *externalLeading
= 0;
2101 cairo_save( m_cairo
);
2102 cairo_scale(m_cairo
, m_scaleX
, m_scaleY
);
2104 // Set layout's text
2105 const wxUTF8Buf dataUTF8
= string
.utf8_str();
2110 // scale the font and apply it
2111 PangoFontDescription
*desc
= theFont
->GetNativeFontInfo()->description
;
2112 float size
= pango_font_description_get_size(desc
);
2113 size
= size
* GetFontPointSizeAdjustment(72.0);
2114 pango_font_description_set_size(desc
, (gint
)size
);
2116 pango_layout_set_font_description(m_layout
, desc
);
2119 pango_layout_set_text( m_layout
, dataUTF8
, strlen(dataUTF8
) );
2122 pango_layout_get_pixel_size( m_layout
, width
, &h
);
2128 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
2129 int baseline
= pango_layout_iter_get_baseline(iter
);
2130 pango_layout_iter_free(iter
);
2131 *descent
= h
- PANGO_PIXELS(baseline
);
2136 // restore font and reset font's size back
2137 pango_layout_set_font_description(m_layout
, m_fontdesc
);
2139 PangoFontDescription
*desc
= theFont
->GetNativeFontInfo()->description
;
2140 pango_font_description_set_size(desc
, oldSize
);
2143 cairo_restore( m_cairo
);
2146 void wxGtkPrinterDCImpl::DoGetSize(int* width
, int* height
) const
2148 GtkPageSetup
*setup
= gtk_print_context_get_page_setup( m_gpc
);
2151 *width
= wxRound( (double)gtk_page_setup_get_paper_width( setup
, GTK_UNIT_POINTS
) * (double)m_resolution
/ 72.0 );
2153 *height
= wxRound( (double)gtk_page_setup_get_paper_height( setup
, GTK_UNIT_POINTS
) * (double)m_resolution
/ 72.0 );
2156 void wxGtkPrinterDCImpl::DoGetSizeMM(int *width
, int *height
) const
2158 GtkPageSetup
*setup
= gtk_print_context_get_page_setup( m_gpc
);
2161 *width
= wxRound( gtk_page_setup_get_paper_width( setup
, GTK_UNIT_MM
) );
2163 *height
= wxRound( gtk_page_setup_get_paper_height( setup
, GTK_UNIT_MM
) );
2166 wxSize
wxGtkPrinterDCImpl::GetPPI() const
2168 return wxSize( (int)m_resolution
, (int)m_resolution
);
2171 void wxGtkPrinterDCImpl::SetPrintData(const wxPrintData
& data
)
2176 // overriden for wxPrinterDC Impl
2178 wxRect
wxGtkPrinterDCImpl::GetPaperRect() const
2180 // Does GtkPrint support printer margins?
2183 DoGetSize( &w
, &h
);
2184 return wxRect( 0,0,w
,h
);
2187 int wxGtkPrinterDCImpl::GetResolution() const
2189 return m_resolution
;
2192 // ----------------------------------------------------------------------------
2194 // ----------------------------------------------------------------------------
2196 IMPLEMENT_CLASS(wxGtkPrintPreview
, wxPrintPreviewBase
)
2198 void wxGtkPrintPreview::Init(wxPrintout
* WXUNUSED(printout
),
2199 wxPrintout
* WXUNUSED(printoutForPrinting
),
2202 // convert wxPrintQuality to resolution (input pointer can be NULL)
2203 wxPrintQuality quality
= data
? data
->GetQuality() : wxPRINT_QUALITY_MEDIUM
;
2206 case wxPRINT_QUALITY_HIGH
:
2207 m_resolution
= 1200;
2210 case wxPRINT_QUALITY_LOW
:
2214 case wxPRINT_QUALITY_DRAFT
:
2221 // positive values directly indicate print resolution
2222 m_resolution
= quality
;
2226 wxFAIL_MSG( "unknown print quality" );
2229 case wxPRINT_QUALITY_MEDIUM
:
2238 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout
*printout
,
2239 wxPrintout
*printoutForPrinting
,
2240 wxPrintDialogData
*data
)
2241 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
2243 Init(printout
, printoutForPrinting
, data
? &data
->GetPrintData() : NULL
);
2246 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout
*printout
,
2247 wxPrintout
*printoutForPrinting
,
2249 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
2251 Init(printout
, printoutForPrinting
, data
);
2254 wxGtkPrintPreview::~wxGtkPrintPreview()
2258 bool wxGtkPrintPreview::Print(bool interactive
)
2260 if (!m_printPrintout
)
2263 wxPrinter
printer(& m_printDialogData
);
2264 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
2267 void wxGtkPrintPreview::DetermineScaling()
2269 wxPaperSize paperType
= m_printDialogData
.GetPrintData().GetPaperId();
2271 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
2273 paper
= wxThePrintPaperDatabase
->FindPaperType(wxPAPER_A4
);
2277 m_previewPrintout
->SetPPIScreen(wxGetDisplayPPI());
2278 m_previewPrintout
->SetPPIPrinter( m_resolution
, m_resolution
);
2280 // Get width and height in points (1/72th of an inch)
2281 wxSize
sizeDevUnits(paper
->GetSizeDeviceUnits());
2282 sizeDevUnits
.x
= wxRound((double)sizeDevUnits
.x
* (double)m_resolution
/ 72.0);
2283 sizeDevUnits
.y
= wxRound((double)sizeDevUnits
.y
* (double)m_resolution
/ 72.0);
2285 wxSize
sizeTenthsMM(paper
->GetSize());
2286 wxSize
sizeMM(sizeTenthsMM
.x
/ 10, sizeTenthsMM
.y
/ 10);
2288 // If in landscape mode, we need to swap the width and height.
2289 if ( m_printDialogData
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
2291 m_pageWidth
= sizeDevUnits
.y
;
2292 m_pageHeight
= sizeDevUnits
.x
;
2293 m_previewPrintout
->SetPageSizeMM(sizeMM
.y
, sizeMM
.x
);
2297 m_pageWidth
= sizeDevUnits
.x
;
2298 m_pageHeight
= sizeDevUnits
.y
;
2299 m_previewPrintout
->SetPageSizeMM(sizeMM
.x
, sizeMM
.y
);
2301 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
2302 m_previewPrintout
->SetPaperRectPixels(wxRect(0, 0, m_pageWidth
, m_pageHeight
));
2304 // At 100%, the page should look about page-size on the screen.
2305 m_previewScaleX
= 0.8 * 72.0 / (double)m_resolution
;
2306 m_previewScaleY
= m_previewScaleX
;