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/gtk/private.h"
35 #include "wx/dynlib.h"
40 #if GTK_CHECK_VERSION(2,14,0)
41 #include <gtk/gtkunixprint.h>
43 #include <gtk/gtkpagesetupunixdialog.h>
47 #if wxUSE_GRAPHICS_CONTEXT
48 #include "wx/graphics.h"
52 wxFORCE_LINK_THIS_MODULE(gtk_print
)
54 #if wxUSE_LIBGNOMEPRINT
55 #include "wx/gtk/gnome/gprint.h"
58 #include "wx/gtk/private/object.h"
60 // Usefull to convert angles from/to Rad to/from Deg.
61 static const double RAD2DEG
= 180.0 / M_PI
;
62 static const double DEG2RAD
= M_PI
/ 180.0;
64 //----------------------------------------------------------------------------
66 // Initialized when starting the app : if it successfully load the gtk-print framework,
67 // it uses it. If not, it falls back to gnome print (see /gtk/gnome/gprint.cpp) then
68 // to postscript if gnomeprint is not available.
69 //----------------------------------------------------------------------------
71 class wxGtkPrintModule
: public wxModule
76 #if wxUSE_LIBGNOMEPRINT
77 // This module must be initialized AFTER gnomeprint's one
78 AddDependency(CLASSINFO(wxGnomePrintModule
));
85 DECLARE_DYNAMIC_CLASS(wxGtkPrintModule
)
88 bool wxGtkPrintModule::OnInit()
90 if (gtk_check_version(2,10,0) == NULL
)
91 wxPrintFactory::SetPrintFactory( new wxGtkPrintFactory
);
95 IMPLEMENT_DYNAMIC_CLASS(wxGtkPrintModule
, wxModule
)
97 //----------------------------------------------------------------------------
99 //----------------------------------------------------------------------------
101 wxPrinterBase
* wxGtkPrintFactory::CreatePrinter( wxPrintDialogData
*data
)
103 return new wxGtkPrinter( data
);
106 wxPrintPreviewBase
*wxGtkPrintFactory::CreatePrintPreview( wxPrintout
*preview
,
107 wxPrintout
*printout
,
108 wxPrintDialogData
*data
)
110 return new wxGtkPrintPreview( preview
, printout
, data
);
113 wxPrintPreviewBase
*wxGtkPrintFactory::CreatePrintPreview( wxPrintout
*preview
,
114 wxPrintout
*printout
,
117 return new wxGtkPrintPreview( preview
, printout
, data
);
120 wxPrintDialogBase
*wxGtkPrintFactory::CreatePrintDialog( wxWindow
*parent
,
121 wxPrintDialogData
*data
)
123 return new wxGtkPrintDialog( parent
, data
);
126 wxPrintDialogBase
*wxGtkPrintFactory::CreatePrintDialog( wxWindow
*parent
,
129 return new wxGtkPrintDialog( parent
, data
);
132 wxPageSetupDialogBase
*wxGtkPrintFactory::CreatePageSetupDialog( wxWindow
*parent
,
133 wxPageSetupDialogData
* data
)
135 return new wxGtkPageSetupDialog( parent
, data
);
138 bool wxGtkPrintFactory::HasPrintSetupDialog()
144 wxGtkPrintFactory::CreatePrintSetupDialog(wxWindow
* WXUNUSED(parent
),
145 wxPrintData
* WXUNUSED(data
))
150 wxDCImpl
* wxGtkPrintFactory::CreatePrinterDCImpl( wxPrinterDC
*owner
, const wxPrintData
& data
)
152 return new wxGtkPrinterDCImpl( owner
, data
);
155 bool wxGtkPrintFactory::HasOwnPrintToFile()
160 bool wxGtkPrintFactory::HasPrinterLine()
165 wxString
wxGtkPrintFactory::CreatePrinterLine()
168 return wxEmptyString
;
171 bool wxGtkPrintFactory::HasStatusLine()
177 wxString
wxGtkPrintFactory::CreateStatusLine()
180 return wxEmptyString
;
183 wxPrintNativeDataBase
*wxGtkPrintFactory::CreatePrintNativeData()
185 return new wxGtkPrintNativeData
;
188 //----------------------------------------------------------------------------
189 // Callback functions for Gtk Printings.
190 //----------------------------------------------------------------------------
192 // We use it to pass useful objects to GTK printing callback functions.
193 struct wxPrinterToGtkData
195 wxGtkPrinter
* printer
;
196 wxPrintout
* printout
;
201 static void gtk_begin_print_callback (GtkPrintOperation
*operation
, GtkPrintContext
*context
, gpointer user_data
)
203 wxPrinterToGtkData
*data
= (wxPrinterToGtkData
*) user_data
;
205 data
->printer
->BeginPrint(data
->printout
, operation
, context
);
208 static void gtk_draw_page_print_callback (GtkPrintOperation
*operation
, GtkPrintContext
*context
, gint page_nr
, gpointer user_data
)
210 wxPrinterToGtkData
*data
= (wxPrinterToGtkData
*) user_data
;
212 data
->printer
->DrawPage(data
->printout
, operation
, context
, page_nr
);
215 static void gtk_end_print_callback(GtkPrintOperation
* WXUNUSED(operation
),
216 GtkPrintContext
* WXUNUSED(context
),
219 wxPrintout
*printout
= (wxPrintout
*) user_data
;
221 printout
->OnEndPrinting();
225 //----------------------------------------------------------------------------
226 // wxGtkPrintNativeData
227 //----------------------------------------------------------------------------
229 IMPLEMENT_CLASS(wxGtkPrintNativeData
, wxPrintNativeDataBase
)
231 wxGtkPrintNativeData::wxGtkPrintNativeData()
233 m_config
= gtk_print_settings_new();
234 m_job
= gtk_print_operation_new();
238 wxGtkPrintNativeData::~wxGtkPrintNativeData()
240 g_object_unref(m_job
);
241 g_object_unref(m_config
);
244 // Convert datas stored in m_config to a wxPrintData.
245 // Called by wxPrintData::ConvertFromNative().
246 bool wxGtkPrintNativeData::TransferTo( wxPrintData
&data
)
251 int resolution
= gtk_print_settings_get_resolution(m_config
);
252 if ( resolution
> 0 )
254 // if resolution is explicitly set, use it
255 data
.SetQuality(resolution
);
257 else // use more vague "quality"
259 GtkPrintQuality quality
= gtk_print_settings_get_quality(m_config
);
260 if (quality
== GTK_PRINT_QUALITY_HIGH
)
261 data
.SetQuality(wxPRINT_QUALITY_HIGH
);
262 else if (quality
== GTK_PRINT_QUALITY_LOW
)
263 data
.SetQuality(wxPRINT_QUALITY_LOW
);
264 else if (quality
== GTK_PRINT_QUALITY_DRAFT
)
265 data
.SetQuality(wxPRINT_QUALITY_DRAFT
);
267 data
.SetQuality(wxPRINT_QUALITY_MEDIUM
);
270 data
.SetNoCopies(gtk_print_settings_get_n_copies(m_config
));
272 data
.SetColour(gtk_print_settings_get_use_color(m_config
));
274 switch (gtk_print_settings_get_duplex(m_config
))
276 case GTK_PRINT_DUPLEX_SIMPLEX
: data
.SetDuplex (wxDUPLEX_SIMPLEX
);
279 case GTK_PRINT_DUPLEX_HORIZONTAL
: data
.SetDuplex (wxDUPLEX_HORIZONTAL
);
283 case GTK_PRINT_DUPLEX_VERTICAL
: data
.SetDuplex (wxDUPLEX_VERTICAL
);
287 GtkPageOrientation orientation
= gtk_print_settings_get_orientation (m_config
);
288 if (orientation
== GTK_PAGE_ORIENTATION_PORTRAIT
)
290 data
.SetOrientation(wxPORTRAIT
);
291 data
.SetOrientationReversed(false);
293 else if (orientation
== GTK_PAGE_ORIENTATION_LANDSCAPE
)
295 data
.SetOrientation(wxLANDSCAPE
);
296 data
.SetOrientationReversed(false);
298 else if (orientation
== GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT
)
300 data
.SetOrientation(wxPORTRAIT
);
301 data
.SetOrientationReversed(true);
303 else if (orientation
== GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE
)
305 data
.SetOrientation(wxLANDSCAPE
);
306 data
.SetOrientationReversed(true);
309 data
.SetCollate(gtk_print_settings_get_collate (m_config
));
311 // Paper formats : these are the most common paper formats.
312 GtkPaperSize
*paper_size
= gtk_print_settings_get_paper_size (m_config
);
314 data
.SetPaperId(wxPAPER_NONE
);
315 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_A3
)))
316 data
.SetPaperId(wxPAPER_A3
);
317 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_A4
)))
318 data
.SetPaperId(wxPAPER_A4
);
319 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_A5
)))
320 data
.SetPaperId(wxPAPER_A5
);
321 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_B5
)))
322 data
.SetPaperId(wxPAPER_B5
);
323 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_LETTER
)))
324 data
.SetPaperId(wxPAPER_LETTER
);
325 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_LEGAL
)))
326 data
.SetPaperId(wxPAPER_LEGAL
);
327 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new (GTK_PAPER_NAME_EXECUTIVE
)))
328 data
.SetPaperId(wxPAPER_EXECUTIVE
);
329 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"na_number-10")))
330 data
.SetPaperId(wxPAPER_ENV_10
);
331 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c5")))
332 data
.SetPaperId(wxPAPER_ENV_C5
);
333 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c6")))
334 data
.SetPaperId(wxPAPER_ENV_C6
);
335 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"jis-b5")))
336 data
.SetPaperId(wxPAPER_B5_TRANSVERSE
);
337 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b5")))
338 data
.SetPaperId(wxPAPER_ENV_B5
);
339 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"na_monarch")))
340 data
.SetPaperId(wxPAPER_ENV_MONARCH
);
341 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-c")))
342 data
.SetPaperId( wxPAPER_CSHEET
);
343 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-d")))
344 data
.SetPaperId( wxPAPER_DSHEET
);
345 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-e")))
346 data
.SetPaperId( wxPAPER_ESHEET
);
347 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"letter")))
348 data
.SetPaperId( wxPAPER_LETTERSMALL
);
349 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"engineering-b")))
350 data
.SetPaperId( wxPAPER_TABLOID
);
351 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"ledger")))
352 data
.SetPaperId( wxPAPER_LEDGER
);
353 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"statement")))
354 data
.SetPaperId( wxPAPER_STATEMENT
);
355 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( GTK_PAPER_NAME_A4
)))
356 data
.SetPaperId( wxPAPER_A4SMALL
);
357 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b4")))
358 data
.SetPaperId( wxPAPER_B4
);
359 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"folio")))
360 data
.SetPaperId( wxPAPER_FOLIO
);
361 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"quarto")))
362 data
.SetPaperId( wxPAPER_QUARTO
);
363 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"10x14")))
364 data
.SetPaperId( wxPAPER_10X14
);
365 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"ledger")))
366 data
.SetPaperId( wxPAPER_11X17
);
367 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"letter")))
368 data
.SetPaperId( wxPAPER_NOTE
);
369 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"na-number-9-envelope")))
370 data
.SetPaperId( wxPAPER_ENV_9
);
371 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"number-11")))
372 data
.SetPaperId( wxPAPER_ENV_11
);
373 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"number-12")))
374 data
.SetPaperId( wxPAPER_ENV_12
);
375 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"number-14")))
376 data
.SetPaperId( wxPAPER_ENV_14
);
377 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-designated")))
378 data
.SetPaperId( wxPAPER_ENV_DL
);
379 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c3")))
380 data
.SetPaperId( wxPAPER_ENV_C3
);
381 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-c4")))
382 data
.SetPaperId( wxPAPER_ENV_C4
);
383 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"c6/c5")))
384 data
.SetPaperId( wxPAPER_ENV_C65
);
385 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b4")))
386 data
.SetPaperId( wxPAPER_ENV_B4
);
387 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"iso-b6")))
388 data
.SetPaperId( wxPAPER_ENV_B6
);
389 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"Italian")))
390 data
.SetPaperId( wxPAPER_ENV_ITALY
);
391 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"personal")))
392 data
.SetPaperId( wxPAPER_ENV_PERSONAL
);
393 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"fanfold-us")))
394 data
.SetPaperId( wxPAPER_FANFOLD_US
);
395 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"fanfold-European")))
396 data
.SetPaperId( wxPAPER_FANFOLD_STD_GERMAN
);
397 else if (gtk_paper_size_is_equal(paper_size
,gtk_paper_size_new ( (const gchar
*)"foolscap")))
398 data
.SetPaperId( wxPAPER_FANFOLD_LGL_GERMAN
);
400 data
.SetPaperId(wxPAPER_NONE
);
402 data
.SetPrinterName(gtk_print_settings_get_printer(m_config
));
407 // Put datas given by the wxPrintData into m_config.
408 // Called by wxPrintData::ConvertToNative().
409 bool wxGtkPrintNativeData::TransferFrom( const wxPrintData
&data
)
414 wxPrintQuality quality
= data
.GetQuality();
415 if (quality
== wxPRINT_QUALITY_HIGH
)
416 gtk_print_settings_set_quality (m_config
, GTK_PRINT_QUALITY_HIGH
);
417 else if (quality
== wxPRINT_QUALITY_MEDIUM
)
418 gtk_print_settings_set_quality (m_config
, GTK_PRINT_QUALITY_NORMAL
);
419 else if (quality
== wxPRINT_QUALITY_LOW
)
420 gtk_print_settings_set_quality (m_config
, GTK_PRINT_QUALITY_LOW
);
421 else if (quality
== wxPRINT_QUALITY_DRAFT
)
422 gtk_print_settings_set_quality (m_config
, GTK_PRINT_QUALITY_DRAFT
);
423 else if (quality
> 1)
424 gtk_print_settings_set_resolution (m_config
, quality
);
426 gtk_print_settings_set_quality (m_config
, GTK_PRINT_QUALITY_NORMAL
);
428 gtk_print_settings_set_n_copies(m_config
, data
.GetNoCopies());
430 gtk_print_settings_set_use_color(m_config
, data
.GetColour());
432 switch (data
.GetDuplex())
434 case wxDUPLEX_SIMPLEX
: gtk_print_settings_set_duplex (m_config
, GTK_PRINT_DUPLEX_SIMPLEX
);
437 case wxDUPLEX_HORIZONTAL
: gtk_print_settings_set_duplex (m_config
, GTK_PRINT_DUPLEX_HORIZONTAL
);
441 case wxDUPLEX_VERTICAL
: gtk_print_settings_set_duplex (m_config
, GTK_PRINT_DUPLEX_VERTICAL
);
445 if (!data
.IsOrientationReversed())
447 if (data
.GetOrientation() == wxLANDSCAPE
)
448 gtk_print_settings_set_orientation (m_config
, GTK_PAGE_ORIENTATION_LANDSCAPE
);
450 gtk_print_settings_set_orientation (m_config
, GTK_PAGE_ORIENTATION_PORTRAIT
);
453 if (data
.GetOrientation() == wxLANDSCAPE
)
454 gtk_print_settings_set_orientation (m_config
, GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE
);
456 gtk_print_settings_set_orientation (m_config
, GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT
);
459 gtk_print_settings_set_collate (m_config
, data
.GetCollate());
461 // Paper formats: these are the most common paper formats.
462 switch (data
.GetPaperId())
464 case wxPAPER_A3
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_A3
));
466 case wxPAPER_A4
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_A4
));
468 case wxPAPER_A5
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_A5
));
470 case wxPAPER_B5_TRANSVERSE
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "jis-b5"));
472 case wxPAPER_B5
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_B5
));
474 case wxPAPER_LETTER
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_LETTER
));
476 case wxPAPER_LEGAL
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_LEGAL
));
478 case wxPAPER_EXECUTIVE
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_EXECUTIVE
));
480 case wxPAPER_ENV_10
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "na_number-10"));
482 case wxPAPER_ENV_C5
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-c5"));
484 case wxPAPER_ENV_C6
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-c6"));
486 case wxPAPER_ENV_B5
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-c5b5"));
488 case wxPAPER_ENV_MONARCH
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "na_monarch"));
490 case wxPAPER_CSHEET
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "engineering-c"));
492 case wxPAPER_DSHEET
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "engineering-d"));
494 case wxPAPER_ESHEET
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "engineering-e"));
496 case wxPAPER_LETTERSMALL
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "letter"));
498 case wxPAPER_TABLOID
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "engineering-b"));
500 case wxPAPER_LEDGER
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "ledger"));
502 case wxPAPER_STATEMENT
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "statement"));
504 case wxPAPER_A4SMALL
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new (GTK_PAPER_NAME_A4
));
506 case wxPAPER_B4
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-b4"));
508 case wxPAPER_FOLIO
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "folio"));
510 case wxPAPER_QUARTO
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "quarto"));
512 case wxPAPER_10X14
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "10x14"));
514 case wxPAPER_11X17
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "ledger"));
516 case wxPAPER_NOTE
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "letter"));
518 case wxPAPER_ENV_9
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "na-number-9-envelope"));
520 case wxPAPER_ENV_11
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "number-11"));
522 case wxPAPER_ENV_12
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "number-12"));
524 case wxPAPER_ENV_14
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "number-14"));
526 case wxPAPER_ENV_DL
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-designated"));
528 case wxPAPER_ENV_C3
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-c3"));
530 case wxPAPER_ENV_C4
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-c4"));
532 case wxPAPER_ENV_C65
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "c6/c5"));
534 case wxPAPER_ENV_B4
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-b4"));
536 case wxPAPER_ENV_B6
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "iso-b6"));
538 case wxPAPER_ENV_ITALY
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "Italian"));
540 case wxPAPER_ENV_PERSONAL
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "personal"));
542 case wxPAPER_FANFOLD_US
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "fanfold-us"));
544 case wxPAPER_FANFOLD_STD_GERMAN
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "fanfold-European"));
546 case wxPAPER_FANFOLD_LGL_GERMAN
: gtk_print_settings_set_paper_size(m_config
, gtk_paper_size_new ((const gchar
*) "foolscap"));
552 gtk_print_settings_set_printer(m_config
, data
.GetPrinterName().utf8_str());
557 void wxGtkPrintNativeData::SetPrintConfig( GtkPrintSettings
* config
)
560 m_config
= gtk_print_settings_copy(config
);
563 // Extract page setup from settings.
564 GtkPageSetup
* wxGtkPrintNativeData::GetPageSetupFromSettings(GtkPrintSettings
* settings
)
566 GtkPageSetup
* page_setup
= gtk_page_setup_new();
567 gtk_page_setup_set_orientation (page_setup
, gtk_print_settings_get_orientation (settings
));
569 GtkPaperSize
*paper_size
= gtk_print_settings_get_paper_size (settings
);
570 if (paper_size
!= NULL
)
571 gtk_page_setup_set_paper_size_and_default_margins (page_setup
, paper_size
);
576 // Insert page setup into a given GtkPrintSettings.
577 void wxGtkPrintNativeData::SetPageSetupToSettings(GtkPrintSettings
* settings
, GtkPageSetup
* page_setup
)
579 gtk_print_settings_set_orientation ( settings
, gtk_page_setup_get_orientation (page_setup
));
580 gtk_print_settings_set_paper_size ( settings
, gtk_page_setup_get_paper_size (page_setup
));
583 //----------------------------------------------------------------------------
585 //----------------------------------------------------------------------------
587 IMPLEMENT_CLASS(wxGtkPrintDialog
, wxPrintDialogBase
)
589 wxGtkPrintDialog::wxGtkPrintDialog( wxWindow
*parent
, wxPrintDialogData
*data
)
590 : wxPrintDialogBase(parent
, wxID_ANY
, _("Print"),
591 wxPoint(0, 0), wxSize(600, 600),
592 wxDEFAULT_DIALOG_STYLE
|
596 m_printDialogData
= *data
;
602 wxGtkPrintDialog::wxGtkPrintDialog( wxWindow
*parent
, wxPrintData
*data
)
603 : wxPrintDialogBase(parent
, wxID_ANY
, _("Print"),
604 wxPoint(0, 0), wxSize(600, 600),
605 wxDEFAULT_DIALOG_STYLE
|
609 m_printDialogData
= *data
;
616 wxGtkPrintDialog::~wxGtkPrintDialog()
620 // This is called even if we actually don't want the dialog to appear.
621 int wxGtkPrintDialog::ShowModal()
623 GtkPrintOperationResult response
;
625 // We need to restore the settings given in the constructor.
626 wxPrintData data
= m_printDialogData
.GetPrintData();
627 wxGtkPrintNativeData
*native
=
628 (wxGtkPrintNativeData
*) data
.GetNativeData();
629 data
.ConvertToNative();
631 GtkPrintSettings
* settings
= native
->GetPrintConfig();
633 // We have to restore pages to print here because they're stored in a wxPrintDialogData and ConvertToNative only works for wxPrintData.
634 int fromPage
= m_printDialogData
.GetFromPage();
635 int toPage
= m_printDialogData
.GetToPage();
636 if (m_printDialogData
.GetSelection())
637 gtk_print_settings_set_print_pages(settings
, GTK_PRINT_PAGES_CURRENT
);
638 else if (m_printDialogData
.GetAllPages())
639 gtk_print_settings_set_print_pages(settings
, GTK_PRINT_PAGES_ALL
);
641 gtk_print_settings_set_print_pages(settings
, GTK_PRINT_PAGES_RANGES
);
643 range
= g_new (GtkPageRange
, 1);
644 range
[0].start
= fromPage
-1;
645 range
[0].end
= (toPage
>= fromPage
) ? toPage
-1 : fromPage
-1;
646 gtk_print_settings_set_page_ranges (settings
, range
, 1);
649 GtkPrintOperation
* const printOp
= native
->GetPrintJob();
651 // If the settings are OK, we restore it.
652 if (settings
!= NULL
)
653 gtk_print_operation_set_print_settings (printOp
, settings
);
654 gtk_print_operation_set_default_page_setup (printOp
, native
->GetPageSetupFromSettings(settings
));
656 // Show the dialog if needed.
657 GError
* gError
= NULL
;
659 response
= gtk_print_operation_run (printOp
, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
, GTK_WINDOW(gtk_widget_get_toplevel(m_parent
->m_widget
) ), &gError
);
661 response
= gtk_print_operation_run (printOp
, 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 wxLogError(_("Error while printing: ") + wxString(gError
? gError
->message
: "???"));
671 g_error_free (gError
);
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(printOp
);
677 native
->SetPrintConfig(newSettings
);
678 data
.ConvertFromNative();
680 // Set PrintDialogData variables
681 m_printDialogData
.SetPrintData(data
);
682 m_printDialogData
.SetCollate(data
.GetCollate());
683 m_printDialogData
.SetNoCopies(data
.GetNoCopies());
684 m_printDialogData
.SetPrintToFile(data
.GetPrinterName() == "Print to File");
686 // Same problem as a few lines before.
687 switch (gtk_print_settings_get_print_pages(newSettings
))
689 case GTK_PRINT_PAGES_CURRENT
:
690 m_printDialogData
.SetSelection( true );
692 case GTK_PRINT_PAGES_RANGES
:
693 {// wxWidgets doesn't support multiple ranges, so we can only save the first one even if the user wants to print others.
694 // For example, the user enters "1-3;5-7" in the dialog: pages 1-3 and 5-7 will be correctly printed when the user
695 // will hit "OK" button. However we can only save 1-3 in the print data.
698 range
= gtk_print_settings_get_page_ranges (newSettings
, &num_ranges
);
701 m_printDialogData
.SetFromPage( range
[0].start
);
702 m_printDialogData
.SetToPage( range
[0].end
);
705 m_printDialogData
.SetAllPages( true );
706 m_printDialogData
.SetFromPage( 0 );
707 m_printDialogData
.SetToPage( 9999 );
710 case GTK_PRINT_PAGES_ALL
:
712 m_printDialogData
.SetAllPages( true );
713 m_printDialogData
.SetFromPage( 0 );
714 m_printDialogData
.SetToPage( 9999 );
721 //----------------------------------------------------------------------------
722 // wxGtkPageSetupDialog
723 //----------------------------------------------------------------------------
725 IMPLEMENT_CLASS(wxGtkPageSetupDialog
, wxPageSetupDialogBase
)
727 wxGtkPageSetupDialog::wxGtkPageSetupDialog( wxWindow
*parent
,
728 wxPageSetupDialogData
* data
)
731 m_pageDialogData
= *data
;
736 wxGtkPageSetupDialog::~wxGtkPageSetupDialog()
740 int wxGtkPageSetupDialog::ShowModal()
743 m_pageDialogData
.GetPrintData().ConvertToNative();
744 wxGtkPrintNativeData
*native
= (wxGtkPrintNativeData
*) m_pageDialogData
.GetPrintData().GetNativeData();
745 GtkPrintSettings
* nativeData
= native
->GetPrintConfig();
747 // We only need the pagesetup data which are part of the settings.
748 GtkPageSetup
* oldPageSetup
= native
->GetPageSetupFromSettings(nativeData
);
750 // If the user used a custom paper format the last time he printed, we have to restore it too.
751 if (m_pageDialogData
.GetPrintData().GetPaperId() == wxPAPER_NONE
)
753 wxSize customPaperSize
= m_pageDialogData
.GetPaperSize();
754 if (customPaperSize
.GetWidth() > 0 && customPaperSize
.GetHeight() > 0)
756 wxString title
= _("Custom size");
757 GtkPaperSize
* customSize
= gtk_paper_size_new_custom ("custom", title
.mb_str(), (gdouble
) customPaperSize
.GetWidth(), (gdouble
) customPaperSize
.GetHeight(), GTK_UNIT_MM
);
758 gtk_page_setup_set_paper_size_and_default_margins (oldPageSetup
, customSize
);
759 g_object_unref(customSize
);
764 // Set selected printer
765 gtk_print_settings_set(nativeData
, "format-for-printer",
766 gtk_print_settings_get_printer(nativeData
));
768 // Create custom dialog
769 wxString
title(GetTitle());
771 title
= _("Page Setup");
773 dlg
= gtk_page_setup_unix_dialog_new(title
.utf8_str(),
774 GTK_WINDOW(m_parent
->m_widget
));
776 gtk_page_setup_unix_dialog_set_print_settings(
777 GTK_PAGE_SETUP_UNIX_DIALOG(dlg
), nativeData
);
778 gtk_page_setup_unix_dialog_set_page_setup(
779 GTK_PAGE_SETUP_UNIX_DIALOG(dlg
), oldPageSetup
);
781 int result
= gtk_dialog_run(GTK_DIALOG(dlg
));
782 gtk_widget_hide(dlg
);
786 case GTK_RESPONSE_OK
:
787 case GTK_RESPONSE_APPLY
:
789 // Store Selected Printer Name
790 gtk_print_settings_set_printer
793 gtk_print_settings_get(nativeData
, "format-for-printer")
796 wxGtkObject
<GtkPageSetup
>
797 newPageSetup(gtk_page_setup_unix_dialog_get_page_setup(
798 GTK_PAGE_SETUP_UNIX_DIALOG(dlg
)));
799 native
->SetPageSetupToSettings(nativeData
, newPageSetup
);
801 m_pageDialogData
.GetPrintData().ConvertFromNative();
803 // Store custom paper format if any.
804 if ( m_pageDialogData
.GetPrintData().GetPaperId() == wxPAPER_NONE
)
806 gdouble ml
,mr
,mt
,mb
,pw
,ph
;
807 ml
= gtk_page_setup_get_left_margin (newPageSetup
, GTK_UNIT_MM
);
808 mr
= gtk_page_setup_get_right_margin (newPageSetup
, GTK_UNIT_MM
);
809 mt
= gtk_page_setup_get_top_margin (newPageSetup
, GTK_UNIT_MM
);
810 mb
= gtk_page_setup_get_bottom_margin (newPageSetup
, GTK_UNIT_MM
);
812 pw
= gtk_page_setup_get_paper_width (newPageSetup
, GTK_UNIT_MM
);
813 ph
= gtk_page_setup_get_paper_height (newPageSetup
, GTK_UNIT_MM
);
815 m_pageDialogData
.SetMarginTopLeft(wxPoint((int)(ml
+0.5),
817 m_pageDialogData
.SetMarginBottomRight(wxPoint((int)(mr
+0.5),
820 m_pageDialogData
.SetPaperSize(wxSize((int)(pw
+0.5),
829 case GTK_RESPONSE_CANCEL
:
830 result
= wxID_CANCEL
;
834 gtk_widget_destroy(dlg
);
839 //----------------------------------------------------------------------------
841 //----------------------------------------------------------------------------
843 IMPLEMENT_CLASS(wxGtkPrinter
, wxPrinterBase
)
845 wxGtkPrinter::wxGtkPrinter( wxPrintDialogData
*data
) :
846 wxPrinterBase( data
)
851 m_printDialogData
= *data
;
854 wxGtkPrinter::~wxGtkPrinter()
858 bool wxGtkPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
862 sm_lastError
= wxPRINTER_ERROR
;
866 // Let's correct the PageInfo just in case the app gives wrong values.
867 int fromPage
, toPage
;
868 int minPage
, maxPage
;
869 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
870 m_printDialogData
.SetAllPages(true);
872 if (minPage
< 1) minPage
= 1;
873 if (maxPage
< 1) maxPage
= 9999;
874 if (maxPage
< minPage
) maxPage
= minPage
;
876 m_printDialogData
.SetMinPage(minPage
);
877 m_printDialogData
.SetMaxPage(maxPage
);
880 if (fromPage
< minPage
) fromPage
= minPage
;
881 else if (fromPage
> maxPage
) fromPage
= maxPage
;
882 m_printDialogData
.SetFromPage(fromPage
);
886 m_printDialogData
.SetToPage(toPage
);
887 if (toPage
> maxPage
) toPage
= maxPage
;
888 else if (toPage
< minPage
) toPage
= minPage
;
891 if (((minPage
!= fromPage
) && fromPage
!= 0) || ((maxPage
!= toPage
) && toPage
!= 0)) m_printDialogData
.SetAllPages(false);
894 wxPrintData printdata
= GetPrintDialogData().GetPrintData();
895 wxGtkPrintNativeData
*native
= (wxGtkPrintNativeData
*) printdata
.GetNativeData();
897 GtkPrintOperation
* const printOp
= native
->GetPrintJob();
899 wxPrinterToGtkData dataToSend
;
900 dataToSend
.printer
= this;
901 dataToSend
.printout
= printout
;
903 // These Gtk signals are caught here.
904 g_signal_connect (printOp
, "begin-print", G_CALLBACK (gtk_begin_print_callback
), &dataToSend
);
905 g_signal_connect (printOp
, "draw-page", G_CALLBACK (gtk_draw_page_print_callback
), &dataToSend
);
906 g_signal_connect (printOp
, "end-print", G_CALLBACK (gtk_end_print_callback
), printout
);
908 // This is used to setup the DC and
909 // show the dialog if desired
910 wxGtkPrintDialog
dialog( parent
, &m_printDialogData
);
911 dialog
.SetPrintDC(m_dc
);
912 dialog
.SetShowDialog(prompt
);
914 // doesn't necessarily show
915 int ret
= dialog
.ShowModal();
916 if (ret
== wxID_CANCEL
)
918 sm_lastError
= wxPRINTER_CANCELLED
;
922 sm_lastError
= wxPRINTER_ERROR
;
923 wxFAIL_MSG(_("The print dialog returned an error."));
926 return (sm_lastError
== wxPRINTER_NO_ERROR
);
929 void wxGtkPrinter::BeginPrint(wxPrintout
*printout
, GtkPrintOperation
*operation
, GtkPrintContext
*context
)
931 wxPrintData printdata
= GetPrintDialogData().GetPrintData();
932 wxGtkPrintNativeData
*native
= (wxGtkPrintNativeData
*) printdata
.GetNativeData();
934 // We need to update printdata with the new data from the dialog and we
935 // have to do this here because this method needs this new data and we
936 // cannot update it earlier
937 native
->SetPrintConfig(gtk_print_operation_get_print_settings(operation
));
938 printdata
.ConvertFromNative();
940 SetPrintContext(context
);
941 native
->SetPrintContext( context
);
943 wxPrinterDC
*printDC
= new wxPrinterDC( printdata
);
948 if (sm_lastError
!= wxPRINTER_CANCELLED
)
950 sm_lastError
= wxPRINTER_ERROR
;
951 wxFAIL_MSG(_("The wxGtkPrinterDC cannot be used."));
956 printout
->SetPPIScreen(wxGetDisplayPPI());
957 printout
->SetPPIPrinter( printDC
->GetResolution(),
958 printDC
->GetResolution() );
960 printout
->SetDC(m_dc
);
963 m_dc
->GetSize(&w
, &h
);
964 printout
->SetPageSizePixels((int)w
, (int)h
);
965 printout
->SetPaperRectPixels(wxRect(0, 0, w
, h
));
967 m_dc
->GetSizeMM(&mw
, &mh
);
968 printout
->SetPageSizeMM((int)mw
, (int)mh
);
969 printout
->OnPreparePrinting();
971 // Get some parameters from the printout, if defined.
972 int fromPage
, toPage
;
973 int minPage
, maxPage
;
974 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
978 sm_lastError
= wxPRINTER_ERROR
;
979 wxFAIL_MSG(_("wxPrintout::GetPageInfo gives a null maxPage."));
983 printout
->OnBeginPrinting();
987 // If we're not previewing we need to calculate the number of pages to print.
988 // If we're previewing, Gtk Print will render every pages without wondering about the page ranges the user may
989 // have defined in the dialog. So the number of pages is the maximum available.
990 if (!printout
->IsPreview())
992 GtkPrintSettings
* settings
= gtk_print_operation_get_print_settings (operation
);
993 switch (gtk_print_settings_get_print_pages(settings
))
995 case GTK_PRINT_PAGES_CURRENT
:
998 case GTK_PRINT_PAGES_RANGES
:
999 {gint num_ranges
= 0;
1000 GtkPageRange
* range
;
1002 range
= gtk_print_settings_get_page_ranges (settings
, &num_ranges
);
1003 for (i
=0; i
<num_ranges
; i
++)
1005 if (range
[i
].end
< range
[i
].start
) range
[i
].end
= range
[i
].start
;
1006 if (range
[i
].start
< minPage
-1) range
[i
].start
= minPage
-1;
1007 if (range
[i
].end
> maxPage
-1) range
[i
].end
= maxPage
-1;
1008 if (range
[i
].start
> maxPage
-1) range
[i
].start
= maxPage
-1;
1009 numPages
+= range
[i
].end
- range
[i
].start
+ 1;
1011 gtk_print_settings_set_page_ranges (settings
, range
, 1);
1013 case GTK_PRINT_PAGES_ALL
:
1015 numPages
= maxPage
- minPage
+ 1;
1019 else numPages
= maxPage
- minPage
+ 1;
1021 gtk_print_operation_set_n_pages(operation
, numPages
);
1024 void wxGtkPrinter::DrawPage(wxPrintout
*printout
,
1025 GtkPrintOperation
*operation
,
1026 GtkPrintContext
* WXUNUSED(context
),
1029 int fromPage
, toPage
, minPage
, maxPage
, startPage
, endPage
;
1030 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
1032 int numPageToDraw
= page_nr
+ minPage
;
1033 if (numPageToDraw
< minPage
) numPageToDraw
= minPage
;
1034 if (numPageToDraw
> maxPage
) numPageToDraw
= maxPage
;
1036 GtkPrintSettings
* settings
= gtk_print_operation_get_print_settings (operation
);
1037 switch (gtk_print_settings_get_print_pages(settings
))
1039 case GTK_PRINT_PAGES_CURRENT
:
1040 g_object_get_property((GObject
*) operation
, (const gchar
*) "current-page", (GValue
*) &startPage
);
1041 g_object_get_property((GObject
*) operation
, (const gchar
*) "current-page", (GValue
*) &endPage
);
1043 case GTK_PRINT_PAGES_RANGES
:
1044 {gint num_ranges
= 0;
1045 GtkPageRange
* range
;
1046 range
= gtk_print_settings_get_page_ranges (settings
, &num_ranges
);
1047 // We don't need to verify these values as it has already been done in wxGtkPrinter::BeginPrint.
1048 if (num_ranges
>= 1)
1050 startPage
= range
[0].start
+ 1;
1051 endPage
= range
[0].end
+ 1;
1054 startPage
= minPage
;
1058 case GTK_PRINT_PAGES_ALL
:
1060 startPage
= minPage
;
1065 if(numPageToDraw
== startPage
)
1067 if (!printout
->OnBeginDocument(startPage
, endPage
))
1069 wxLogError(_("Could not start printing."));
1070 sm_lastError
= wxPRINTER_ERROR
;
1074 // The app can render the page numPageToDraw.
1075 if (printout
->HasPage(numPageToDraw
))
1078 printout
->OnPrintPage(numPageToDraw
);
1083 if(numPageToDraw
== endPage
)
1085 printout
->OnEndDocument();
1089 wxDC
* wxGtkPrinter::PrintDialog( wxWindow
*parent
)
1091 wxGtkPrintDialog
dialog( parent
, &m_printDialogData
);
1093 dialog
.SetPrintDC(m_dc
);
1094 dialog
.SetShowDialog(true);
1096 int ret
= dialog
.ShowModal();
1098 if (ret
== wxID_CANCEL
)
1100 sm_lastError
= wxPRINTER_CANCELLED
;
1105 sm_lastError
= wxPRINTER_ERROR
;
1106 wxFAIL_MSG(_("The print dialog returned an error."));
1110 m_printDialogData
= dialog
.GetPrintDialogData();
1112 return new wxPrinterDC( m_printDialogData
.GetPrintData() );
1115 bool wxGtkPrinter::Setup( wxWindow
* WXUNUSED(parent
) )
1117 // Obsolete, for backward compatibility.
1121 //-----------------------------------------------------------------------------
1123 //-----------------------------------------------------------------------------
1125 #define wxCAIRO_SCALE 1
1129 #define XLOG2DEV(x) LogicalToDeviceX(x)
1130 #define XLOG2DEVREL(x) LogicalToDeviceXRel(x)
1131 #define YLOG2DEV(x) LogicalToDeviceY(x)
1132 #define YLOG2DEVREL(x) LogicalToDeviceYRel(x)
1136 #define XLOG2DEV(x) ((double)(LogicalToDeviceX(x)) * m_DEV2PS)
1137 #define XLOG2DEVREL(x) ((double)(LogicalToDeviceXRel(x)) * m_DEV2PS)
1138 #define YLOG2DEV(x) ((double)(LogicalToDeviceY(x)) * m_DEV2PS)
1139 #define YLOG2DEVREL(x) ((double)(LogicalToDeviceYRel(x)) * m_DEV2PS)
1143 IMPLEMENT_ABSTRACT_CLASS(wxGtkPrinterDCImpl
, wxDCImpl
)
1145 wxGtkPrinterDCImpl::wxGtkPrinterDCImpl(wxPrinterDC
*owner
, const wxPrintData
& data
)
1150 wxGtkPrintNativeData
*native
=
1151 (wxGtkPrintNativeData
*) m_printData
.GetNativeData();
1153 m_gpc
= native
->GetPrintContext();
1155 // Match print quality to resolution (high = 1200dpi)
1156 m_resolution
= m_printData
.GetQuality(); // (int) gtk_print_context_get_dpi_x( m_gpc );
1157 if (m_resolution
< 0)
1158 m_resolution
= (1 << (m_resolution
+4)) *150;
1160 m_context
= gtk_print_context_create_pango_context( m_gpc
);
1161 m_layout
= gtk_print_context_create_pango_layout ( m_gpc
);
1162 m_fontdesc
= pango_font_description_from_string( "Sans 12" );
1164 m_cairo
= gtk_print_context_get_cairo_context ( m_gpc
);
1170 cairo_scale( m_cairo
, 72.0 / (double)m_resolution
, 72.0 / (double)m_resolution
);
1172 m_PS2DEV
= (double)m_resolution
/ 72.0;
1173 m_DEV2PS
= 72.0 / (double)m_resolution
;
1180 m_signX
= 1; // default x-axis left to right.
1181 m_signY
= 1; // default y-axis bottom up -> top down.
1183 // By default the origin of the Cairo context is in the upper left
1184 // corner of the printable area. We need to translate it so that it
1185 // is in the upper left corner of the paper (without margins)
1186 GtkPageSetup
*setup
= gtk_print_context_get_page_setup( m_gpc
);
1188 ml
= gtk_page_setup_get_left_margin (setup
, GTK_UNIT_POINTS
);
1189 mt
= gtk_page_setup_get_top_margin (setup
, GTK_UNIT_POINTS
);
1190 cairo_translate(m_cairo
, -ml
, -mt
);
1193 wxGtkPrinterDCImpl::~wxGtkPrinterDCImpl()
1195 g_object_unref(m_context
);
1196 g_object_unref(m_layout
);
1199 bool wxGtkPrinterDCImpl::IsOk() const
1201 return m_gpc
!= NULL
;
1204 void* wxGtkPrinterDCImpl::GetCairoContext() const
1206 return (void*) cairo_reference( m_cairo
);
1209 bool wxGtkPrinterDCImpl::DoFloodFill(wxCoord
WXUNUSED(x1
),
1210 wxCoord
WXUNUSED(y1
),
1211 const wxColour
& WXUNUSED(col
),
1212 wxFloodFillStyle
WXUNUSED(style
))
1214 // We can't access the given coord as a Cairo context is scalable, ie a
1215 // coord doesn't mean anything in this context.
1216 wxFAIL_MSG(_("not implemented"));
1220 void wxGtkPrinterDCImpl::DoGradientFillConcentric(const wxRect
& rect
, const wxColour
& initialColour
, const wxColour
& destColour
, const wxPoint
& circleCenter
)
1222 wxCoord xC
= circleCenter
.x
;
1223 wxCoord yC
= circleCenter
.y
;
1224 wxCoord xR
= rect
.x
;
1225 wxCoord yR
= rect
.y
;
1226 wxCoord w
= rect
.width
;
1227 wxCoord h
= rect
.height
;
1229 const double r2
= (w
/2)*(w
/2)+(h
/2)*(h
/2);
1230 double radius
= sqrt(r2
);
1232 unsigned char redI
= initialColour
.Red();
1233 unsigned char blueI
= initialColour
.Blue();
1234 unsigned char greenI
= initialColour
.Green();
1235 unsigned char alphaI
= initialColour
.Alpha();
1236 unsigned char redD
= destColour
.Red();
1237 unsigned char blueD
= destColour
.Blue();
1238 unsigned char greenD
= destColour
.Green();
1239 unsigned char alphaD
= destColour
.Alpha();
1241 double redIPS
= (double)(redI
) / 255.0;
1242 double blueIPS
= (double)(blueI
) / 255.0;
1243 double greenIPS
= (double)(greenI
) / 255.0;
1244 double alphaIPS
= (double)(alphaI
) / 255.0;
1245 double redDPS
= (double)(redD
) / 255.0;
1246 double blueDPS
= (double)(blueD
) / 255.0;
1247 double greenDPS
= (double)(greenD
) / 255.0;
1248 double alphaDPS
= (double)(alphaD
) / 255.0;
1250 // Create a pattern with the gradient.
1251 cairo_pattern_t
* gradient
;
1252 gradient
= cairo_pattern_create_radial (XLOG2DEV(xC
+xR
), YLOG2DEV(yC
+yR
), 0, XLOG2DEV(xC
+xR
), YLOG2DEV(yC
+yR
), radius
* m_DEV2PS
);
1253 cairo_pattern_add_color_stop_rgba (gradient
, 0.0, redIPS
, greenIPS
, blueIPS
, alphaIPS
);
1254 cairo_pattern_add_color_stop_rgba (gradient
, 1.0, redDPS
, greenDPS
, blueDPS
, alphaDPS
);
1256 // Fill the rectangle with this pattern.
1257 cairo_set_source(m_cairo
, gradient
);
1258 cairo_rectangle (m_cairo
, XLOG2DEV(xR
), YLOG2DEV(yR
), XLOG2DEVREL(w
), YLOG2DEVREL(h
) );
1259 cairo_fill(m_cairo
);
1261 cairo_pattern_destroy(gradient
);
1263 CalcBoundingBox(xR
, yR
);
1264 CalcBoundingBox(xR
+w
, yR
+h
);
1267 void wxGtkPrinterDCImpl::DoGradientFillLinear(const wxRect
& rect
, const wxColour
& initialColour
, const wxColour
& destColour
, wxDirection nDirection
)
1271 wxCoord w
= rect
.width
;
1272 wxCoord h
= rect
.height
;
1274 unsigned char redI
= initialColour
.Red();
1275 unsigned char blueI
= initialColour
.Blue();
1276 unsigned char greenI
= initialColour
.Green();
1277 unsigned char alphaI
= initialColour
.Alpha();
1278 unsigned char redD
= destColour
.Red();
1279 unsigned char blueD
= destColour
.Blue();
1280 unsigned char greenD
= destColour
.Green();
1281 unsigned char alphaD
= destColour
.Alpha();
1283 double redIPS
= (double)(redI
) / 255.0;
1284 double blueIPS
= (double)(blueI
) / 255.0;
1285 double greenIPS
= (double)(greenI
) / 255.0;
1286 double alphaIPS
= (double)(alphaI
) / 255.0;
1287 double redDPS
= (double)(redD
) / 255.0;
1288 double blueDPS
= (double)(blueD
) / 255.0;
1289 double greenDPS
= (double)(greenD
) / 255.0;
1290 double alphaDPS
= (double)(alphaD
) / 255.0;
1292 // Create a pattern with the gradient.
1293 cairo_pattern_t
* gradient
;
1294 gradient
= cairo_pattern_create_linear (XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x
+w
), YLOG2DEV(y
));
1296 if (nDirection
== wxWEST
)
1298 cairo_pattern_add_color_stop_rgba (gradient
, 0.0, redDPS
, greenDPS
, blueDPS
, alphaDPS
);
1299 cairo_pattern_add_color_stop_rgba (gradient
, 1.0, redIPS
, greenIPS
, blueIPS
, alphaIPS
);
1302 cairo_pattern_add_color_stop_rgba (gradient
, 0.0, redIPS
, greenIPS
, blueIPS
, alphaIPS
);
1303 cairo_pattern_add_color_stop_rgba (gradient
, 1.0, redDPS
, greenDPS
, blueDPS
, alphaDPS
);
1306 // Fill the rectangle with this pattern.
1307 cairo_set_source(m_cairo
, gradient
);
1308 cairo_rectangle (m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEVREL(w
), YLOG2DEVREL(h
) );
1309 cairo_fill(m_cairo
);
1311 cairo_pattern_destroy(gradient
);
1313 CalcBoundingBox(x
, y
);
1314 CalcBoundingBox(x
+w
, y
+h
);
1317 bool wxGtkPrinterDCImpl::DoGetPixel(wxCoord
WXUNUSED(x1
),
1318 wxCoord
WXUNUSED(y1
),
1319 wxColour
* WXUNUSED(col
)) const
1321 wxFAIL_MSG(_("not implemented"));
1325 void wxGtkPrinterDCImpl::DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
1327 if ( m_pen
.IsTransparent() )
1331 cairo_move_to ( m_cairo
, XLOG2DEV(x1
), YLOG2DEV(y1
) );
1332 cairo_line_to ( m_cairo
, XLOG2DEV(x2
), YLOG2DEV(y2
) );
1333 cairo_stroke ( m_cairo
);
1335 CalcBoundingBox( x1
, y1
);
1336 CalcBoundingBox( x2
, y2
);
1339 void wxGtkPrinterDCImpl::DoCrossHair(wxCoord x
, wxCoord y
)
1346 cairo_move_to (m_cairo
, XLOG2DEV(x
), 0);
1347 cairo_line_to (m_cairo
, XLOG2DEV(x
), YLOG2DEVREL(h
));
1348 cairo_move_to (m_cairo
, 0, YLOG2DEV(y
));
1349 cairo_line_to (m_cairo
, XLOG2DEVREL(w
), YLOG2DEV(y
));
1351 cairo_stroke (m_cairo
);
1352 CalcBoundingBox( 0, 0 );
1353 CalcBoundingBox( w
, h
);
1356 void wxGtkPrinterDCImpl::DoDrawArc(wxCoord x1
,wxCoord y1
,wxCoord x2
,wxCoord y2
,wxCoord xc
,wxCoord yc
)
1358 double dx
= x1
- xc
;
1359 double dy
= y1
- yc
;
1360 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
1362 double alpha1
, alpha2
;
1363 if (x1
== x2
&& y1
== y2
)
1371 alpha1
= alpha2
= 0.0;
1375 alpha1
= (x1
- xc
== 0) ?
1376 (y1
- yc
< 0) ? 90.0 : -90.0 :
1377 atan2(double(y1
-yc
), double(x1
-xc
)) * RAD2DEG
;
1378 alpha2
= (x2
- xc
== 0) ?
1379 (y2
- yc
< 0) ? 90.0 : -90.0 :
1380 atan2(double(y2
-yc
), double(x2
-xc
)) * RAD2DEG
;
1382 while (alpha1
<= 0) alpha1
+= 360;
1383 while (alpha2
<= 0) alpha2
+= 360; // adjust angles to be between.
1384 while (alpha1
> 360) alpha1
-= 360; // 0 and 360 degree.
1385 while (alpha2
> 360) alpha2
-= 360;
1391 cairo_new_path(m_cairo
);
1393 cairo_arc_negative ( m_cairo
, XLOG2DEV(xc
), YLOG2DEV(yc
), XLOG2DEVREL((int)radius
), alpha1
, alpha2
);
1394 cairo_line_to(m_cairo
, XLOG2DEV(xc
), YLOG2DEV(yc
));
1395 cairo_close_path (m_cairo
);
1397 SetBrush( m_brush
);
1398 cairo_fill_preserve( m_cairo
);
1401 cairo_stroke( m_cairo
);
1403 CalcBoundingBox (x1
, y1
);
1404 CalcBoundingBox (xc
, yc
);
1405 CalcBoundingBox (x2
, y2
);
1408 void wxGtkPrinterDCImpl::DoDrawEllipticArc(wxCoord x
,wxCoord y
,wxCoord w
,wxCoord h
,double sa
,double ea
)
1410 cairo_save( m_cairo
);
1412 cairo_new_path(m_cairo
);
1414 cairo_translate( m_cairo
, XLOG2DEV((wxCoord
) (x
+ w
/ 2.)), XLOG2DEV((wxCoord
) (y
+ h
/ 2.)) );
1415 double scale
= (double)YLOG2DEVREL(h
) / (double) XLOG2DEVREL(w
);
1416 cairo_scale( m_cairo
, 1.0, scale
);
1418 cairo_arc_negative ( m_cairo
, 0, 0, XLOG2DEVREL(w
/2), -sa
*DEG2RAD
, -ea
*DEG2RAD
);
1421 cairo_stroke_preserve( m_cairo
);
1423 cairo_line_to(m_cairo
, 0,0);
1425 SetBrush( m_brush
);
1426 cairo_fill( m_cairo
);
1428 cairo_restore( m_cairo
);
1430 CalcBoundingBox( x
, y
);
1431 CalcBoundingBox( x
+w
, y
+h
);
1434 void wxGtkPrinterDCImpl::DoDrawPoint(wxCoord x
, wxCoord y
)
1436 if ( m_pen
.IsTransparent() )
1441 cairo_move_to ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
) );
1442 cairo_line_to ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
) );
1443 cairo_stroke ( m_cairo
);
1445 CalcBoundingBox( x
, y
);
1448 void wxGtkPrinterDCImpl::DoDrawLines(int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
1450 if ( m_pen
.IsTransparent() )
1459 for ( i
=0; i
<n
; i
++ )
1460 CalcBoundingBox( points
[i
].x
+xoffset
, points
[i
].y
+yoffset
);
1462 cairo_move_to ( m_cairo
, XLOG2DEV(points
[0].x
+xoffset
), YLOG2DEV(points
[0].y
+yoffset
) );
1464 for (i
= 1; i
< n
; i
++)
1465 cairo_line_to ( m_cairo
, XLOG2DEV(points
[i
].x
+xoffset
), YLOG2DEV(points
[i
].y
+yoffset
) );
1467 cairo_stroke ( m_cairo
);
1470 void wxGtkPrinterDCImpl::DoDrawPolygon(int n
, wxPoint points
[],
1471 wxCoord xoffset
, wxCoord yoffset
,
1472 wxPolygonFillMode fillStyle
)
1476 cairo_save(m_cairo
);
1477 if (fillStyle
== wxWINDING_RULE
)
1478 cairo_set_fill_rule( m_cairo
, CAIRO_FILL_RULE_WINDING
);
1480 cairo_set_fill_rule( m_cairo
, CAIRO_FILL_RULE_EVEN_ODD
);
1482 int x
= points
[0].x
+ xoffset
;
1483 int y
= points
[0].y
+ yoffset
;
1484 cairo_new_path(m_cairo
);
1485 cairo_move_to( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
) );
1487 for (i
= 1; i
< n
; i
++)
1489 int x
= points
[i
].x
+ xoffset
;
1490 int y
= points
[i
].y
+ yoffset
;
1491 cairo_line_to( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
) );
1493 cairo_close_path(m_cairo
);
1495 SetBrush( m_brush
);
1496 cairo_fill_preserve( m_cairo
);
1499 cairo_stroke( m_cairo
);
1501 CalcBoundingBox( x
, y
);
1503 cairo_restore(m_cairo
);
1506 void wxGtkPrinterDCImpl::DoDrawPolyPolygon(int n
, int count
[], wxPoint points
[],
1507 wxCoord xoffset
, wxCoord yoffset
,
1508 wxPolygonFillMode fillStyle
)
1510 wxDCImpl::DoDrawPolyPolygon( n
, count
, points
, xoffset
, yoffset
, fillStyle
);
1513 void wxGtkPrinterDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1518 cairo_new_path(m_cairo
);
1519 cairo_rectangle ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEVREL(width
), YLOG2DEVREL(height
));
1521 SetBrush( m_brush
);
1522 cairo_fill_preserve( m_cairo
);
1525 cairo_stroke( m_cairo
);
1527 CalcBoundingBox( x
, y
);
1528 CalcBoundingBox( x
+ width
, y
+ height
);
1531 void wxGtkPrinterDCImpl::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
1536 if (radius
< 0.0) radius
= - radius
* ((width
< height
) ? width
: height
);
1538 wxCoord dd
= 2 * (wxCoord
) radius
;
1539 if (dd
> width
) dd
= width
;
1540 if (dd
> height
) dd
= height
;
1543 wxCoord rad
= (wxCoord
) radius
;
1545 cairo_new_path(m_cairo
);
1546 cairo_move_to(m_cairo
,XLOG2DEV(x
+ rad
),YLOG2DEV(y
));
1547 cairo_curve_to(m_cairo
,
1548 XLOG2DEV(x
+ rad
),YLOG2DEV(y
),
1549 XLOG2DEV(x
),YLOG2DEV(y
),
1550 XLOG2DEV(x
),YLOG2DEV(y
+ rad
));
1551 cairo_line_to(m_cairo
,XLOG2DEV(x
),YLOG2DEV(y
+ height
- rad
));
1552 cairo_curve_to(m_cairo
,
1553 XLOG2DEV(x
),YLOG2DEV(y
+ height
- rad
),
1554 XLOG2DEV(x
),YLOG2DEV(y
+ height
),
1555 XLOG2DEV(x
+ rad
),YLOG2DEV(y
+ height
));
1556 cairo_line_to(m_cairo
,XLOG2DEV(x
+ width
- rad
),YLOG2DEV(y
+ height
));
1557 cairo_curve_to(m_cairo
,
1558 XLOG2DEV(x
+ width
- rad
),YLOG2DEV(y
+ height
),
1559 XLOG2DEV(x
+ width
),YLOG2DEV(y
+ height
),
1560 XLOG2DEV(x
+ width
),YLOG2DEV(y
+ height
- rad
));
1561 cairo_line_to(m_cairo
,XLOG2DEV(x
+ width
),YLOG2DEV(y
+ rad
));
1562 cairo_curve_to(m_cairo
,
1563 XLOG2DEV(x
+ width
),YLOG2DEV(y
+ rad
),
1564 XLOG2DEV(x
+ width
),YLOG2DEV(y
),
1565 XLOG2DEV(x
+ width
- rad
),YLOG2DEV(y
));
1566 cairo_line_to(m_cairo
,XLOG2DEV(x
+ rad
),YLOG2DEV(y
));
1567 cairo_close_path(m_cairo
);
1570 cairo_fill_preserve(m_cairo
);
1573 cairo_stroke(m_cairo
);
1575 CalcBoundingBox(x
,y
);
1576 CalcBoundingBox(x
+width
,y
+height
);
1579 void wxGtkPrinterDCImpl::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1584 cairo_save (m_cairo
);
1586 cairo_new_path(m_cairo
);
1588 cairo_translate (m_cairo
, XLOG2DEV((wxCoord
) (x
+ width
/ 2.)), YLOG2DEV((wxCoord
) (y
+ height
/ 2.)));
1589 cairo_scale(m_cairo
, 1, (double)YLOG2DEVREL(height
)/(double)XLOG2DEVREL(width
));
1590 cairo_arc ( m_cairo
, 0, 0, XLOG2DEVREL(width
/2), 0, 2 * M_PI
);
1592 SetBrush( m_brush
);
1593 cairo_fill_preserve( m_cairo
);
1596 cairo_stroke( m_cairo
);
1598 CalcBoundingBox( x
, y
);
1599 CalcBoundingBox( x
+ width
, y
+ height
);
1601 cairo_restore (m_cairo
);
1605 void wxGtkPrinterDCImpl::DoDrawSpline(const wxPointList
*points
)
1609 double c
, d
, x1
, y1
, x2
, y2
, x3
, y3
;
1612 wxPointList::compatibility_iterator node
= points
->GetFirst();
1613 p
= node
->GetData();
1617 node
= node
->GetNext();
1618 p
= node
->GetData();
1622 (double)(x1
+ c
) / 2;
1624 (double)(y1
+ d
) / 2;
1626 cairo_new_path( m_cairo
);
1627 cairo_move_to( m_cairo
, XLOG2DEV((wxCoord
)x1
), YLOG2DEV((wxCoord
)y1
) );
1628 cairo_line_to( m_cairo
, XLOG2DEV((wxCoord
)x3
), YLOG2DEV((wxCoord
)y3
) );
1630 CalcBoundingBox( (wxCoord
)x1
, (wxCoord
)y1
);
1631 CalcBoundingBox( (wxCoord
)x3
, (wxCoord
)y3
);
1633 node
= node
->GetNext();
1636 q
= node
->GetData();
1644 x3
= (double)(x2
+ c
) / 2;
1645 y3
= (double)(y2
+ d
) / 2;
1647 cairo_curve_to(m_cairo
,
1648 XLOG2DEV((wxCoord
)x1
), YLOG2DEV((wxCoord
)y1
),
1649 XLOG2DEV((wxCoord
)x2
), YLOG2DEV((wxCoord
)y2
),
1650 XLOG2DEV((wxCoord
)x3
), YLOG2DEV((wxCoord
)y3
) );
1652 CalcBoundingBox( (wxCoord
)x1
, (wxCoord
)y1
);
1653 CalcBoundingBox( (wxCoord
)x3
, (wxCoord
)y3
);
1655 node
= node
->GetNext();
1658 cairo_line_to ( m_cairo
, XLOG2DEV((wxCoord
)c
), YLOG2DEV((wxCoord
)d
) );
1660 cairo_stroke( m_cairo
);
1662 #endif // wxUSE_SPLINES
1664 bool wxGtkPrinterDCImpl::DoBlit(wxCoord xdest
, wxCoord ydest
,
1665 wxCoord width
, wxCoord height
,
1666 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
1667 wxRasterOperationMode rop
, bool useMask
,
1668 wxCoord
WXUNUSED_UNLESS_DEBUG(xsrcMask
),
1669 wxCoord
WXUNUSED_UNLESS_DEBUG(ysrcMask
))
1671 wxASSERT_MSG( xsrcMask
== wxDefaultCoord
&& ysrcMask
== wxDefaultCoord
,
1672 wxT("mask coordinates are not supported") );
1674 wxCHECK_MSG( source
, false, wxT("invalid source dc") );
1676 // Blit into a bitmap.
1677 wxBitmap
bitmap( width
, height
);
1679 memDC
.SelectObject(bitmap
);
1680 memDC
.Blit(0, 0, width
, height
, source
, xsrc
, ysrc
, rop
);
1681 memDC
.SelectObject(wxNullBitmap
);
1683 // Draw bitmap. scaling and positioning is done there.
1684 GetOwner()->DrawBitmap( bitmap
, xdest
, ydest
, useMask
);
1689 void wxGtkPrinterDCImpl::DoDrawIcon( const wxIcon
& icon
, wxCoord x
, wxCoord y
)
1691 DoDrawBitmap( icon
, x
, y
, true );
1694 void wxGtkPrinterDCImpl::DoDrawBitmap( const wxBitmap
& bitmap
, wxCoord x
, wxCoord y
, bool useMask
)
1696 wxCHECK_RET( bitmap
.IsOk(), wxT("Invalid bitmap in wxGtkPrinterDCImpl::DoDrawBitmap"));
1698 x
= wxCoord(XLOG2DEV(x
));
1699 y
= wxCoord(YLOG2DEV(y
));
1700 int bw
= bitmap
.GetWidth();
1701 int bh
= bitmap
.GetHeight();
1702 wxBitmap bmpSource
= bitmap
; // we need a non-const instance.
1703 if (!useMask
&& !bitmap
.HasPixbuf() && bitmap
.GetMask())
1704 bmpSource
.SetMask(NULL
);
1706 cairo_save(m_cairo
);
1708 // Prepare to draw the image.
1709 cairo_translate(m_cairo
, x
, y
);
1712 wxDouble scaleX
= (wxDouble
) XLOG2DEVREL(bw
) / (wxDouble
) bw
;
1713 wxDouble scaleY
= (wxDouble
) YLOG2DEVREL(bh
) / (wxDouble
) bh
;
1714 cairo_scale(m_cairo
, scaleX
, scaleY
);
1716 gdk_cairo_set_source_pixbuf(m_cairo
, bmpSource
.GetPixbuf(), 0, 0);
1717 cairo_pattern_set_filter(cairo_get_source(m_cairo
), CAIRO_FILTER_NEAREST
);
1718 // Use the original size here since the context is scaled already.
1719 cairo_rectangle(m_cairo
, 0, 0, bw
, bh
);
1720 // Fill the rectangle using the pattern.
1721 cairo_fill(m_cairo
);
1723 CalcBoundingBox(0,0);
1724 CalcBoundingBox(bw
,bh
);
1726 cairo_restore(m_cairo
);
1729 void wxGtkPrinterDCImpl::DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
)
1731 DoDrawRotatedText( text
, x
, y
, 0.0 );
1734 void wxGtkPrinterDCImpl::DoDrawRotatedText(const wxString
& text
, wxCoord x
, wxCoord y
, double angle
)
1736 double xx
= XLOG2DEV(x
);
1737 double yy
= YLOG2DEV(y
);
1741 bool underlined
= m_font
.Ok() && m_font
.GetUnderlined();
1743 const wxScopedCharBuffer data
= text
.utf8_str();
1745 size_t datalen
= strlen(data
);
1746 pango_layout_set_text( m_layout
, data
, datalen
);
1750 PangoAttrList
*attrs
= pango_attr_list_new();
1751 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1753 a
->end_index
= datalen
;
1754 pango_attr_list_insert(attrs
, a
);
1755 pango_layout_set_attributes(m_layout
, attrs
);
1756 pango_attr_list_unref(attrs
);
1759 if (m_textForegroundColour
.Ok())
1761 unsigned char red
= m_textForegroundColour
.Red();
1762 unsigned char blue
= m_textForegroundColour
.Blue();
1763 unsigned char green
= m_textForegroundColour
.Green();
1764 unsigned char alpha
= m_textForegroundColour
.Alpha();
1766 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
&& alpha
== m_currentAlpha
))
1768 double redPS
= (double)(red
) / 255.0;
1769 double bluePS
= (double)(blue
) / 255.0;
1770 double greenPS
= (double)(green
) / 255.0;
1771 double alphaPS
= (double)(alpha
) / 255.0;
1773 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1776 m_currentBlue
= blue
;
1777 m_currentGreen
= green
;
1778 m_currentAlpha
= alpha
;
1783 cairo_move_to (m_cairo
, xx
, yy
);
1785 cairo_save( m_cairo
);
1787 if (fabs(angle
) > 0.00001)
1788 cairo_rotate( m_cairo
, angle
*DEG2RAD
);
1790 cairo_scale(m_cairo
, m_scaleX
, m_scaleY
);
1793 pango_layout_get_pixel_size( m_layout
, &w
, &h
);
1795 if ( m_backgroundMode
== wxBRUSHSTYLE_SOLID
)
1797 unsigned char red
= m_textBackgroundColour
.Red();
1798 unsigned char blue
= m_textBackgroundColour
.Blue();
1799 unsigned char green
= m_textBackgroundColour
.Green();
1800 unsigned char alpha
= m_textBackgroundColour
.Alpha();
1802 double redPS
= (double)(red
) / 255.0;
1803 double bluePS
= (double)(blue
) / 255.0;
1804 double greenPS
= (double)(green
) / 255.0;
1805 double alphaPS
= (double)(alpha
) / 255.0;
1807 cairo_save(m_cairo
);
1808 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1809 cairo_rectangle(m_cairo
, 0, 0, w
, h
); // still in cairo units
1810 cairo_fill(m_cairo
);
1811 cairo_restore(m_cairo
);
1814 pango_cairo_update_layout (m_cairo
, m_layout
);
1815 pango_cairo_show_layout (m_cairo
, m_layout
);
1817 cairo_restore( m_cairo
);
1821 // Undo underline attributes setting
1822 pango_layout_set_attributes(m_layout
, NULL
);
1825 // Back to device units:
1826 CalcBoundingBox (x
, y
);
1827 CalcBoundingBox (x
+ w
, y
+ h
);
1830 void wxGtkPrinterDCImpl::Clear()
1832 // Clear does nothing for printing, but keep the code
1835 cairo_save(m_cairo);
1836 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
1837 SetBrush(m_backgroundBrush);
1838 cairo_paint(m_cairo);
1839 cairo_restore(m_cairo);
1843 void wxGtkPrinterDCImpl::SetFont( const wxFont
& font
)
1850 pango_font_description_free( m_fontdesc
);
1852 m_fontdesc
= pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
1854 float size
= pango_font_description_get_size( m_fontdesc
);
1855 size
= size
* GetFontPointSizeAdjustment(72.0);
1856 pango_font_description_set_size( m_fontdesc
, (gint
)size
);
1858 pango_layout_set_font_description( m_layout
, m_fontdesc
);
1862 void wxGtkPrinterDCImpl::SetPen( const wxPen
& pen
)
1864 if (!pen
.Ok()) return;
1870 if (m_pen
.GetWidth() <= 0)
1873 width
= (double) m_pen
.GetWidth();
1875 cairo_set_line_width( m_cairo
, width
* m_DEV2PS
* m_scaleX
);
1876 static const double dotted
[] = {2.0, 5.0};
1877 static const double short_dashed
[] = {4.0, 4.0};
1878 static const double long_dashed
[] = {4.0, 8.0};
1879 static const double dotted_dashed
[] = {6.0, 6.0, 2.0, 6.0};
1881 switch (m_pen
.GetStyle())
1883 case wxPENSTYLE_DOT
: cairo_set_dash( m_cairo
, dotted
, 2, 0 ); break;
1884 case wxPENSTYLE_SHORT_DASH
: cairo_set_dash( m_cairo
, short_dashed
, 2, 0 ); break;
1885 case wxPENSTYLE_LONG_DASH
: cairo_set_dash( m_cairo
, long_dashed
, 2, 0 ); break;
1886 case wxPENSTYLE_DOT_DASH
: cairo_set_dash( m_cairo
, dotted_dashed
, 4, 0 ); break;
1887 case wxPENSTYLE_USER_DASH
:
1890 int num
= m_pen
.GetDashes (&wx_dashes
);
1891 gdouble
*g_dashes
= g_new( gdouble
, num
);
1893 for (i
= 0; i
< num
; ++i
)
1894 g_dashes
[i
] = (gdouble
) wx_dashes
[i
];
1895 cairo_set_dash( m_cairo
, g_dashes
, num
, 0);
1899 case wxPENSTYLE_SOLID
:
1900 case wxPENSTYLE_TRANSPARENT
:
1901 default: cairo_set_dash( m_cairo
, NULL
, 0, 0 ); break;
1904 switch (m_pen
.GetCap())
1906 case wxCAP_PROJECTING
: cairo_set_line_cap (m_cairo
, CAIRO_LINE_CAP_SQUARE
); break;
1907 case wxCAP_BUTT
: cairo_set_line_cap (m_cairo
, CAIRO_LINE_CAP_BUTT
); break;
1909 default: cairo_set_line_cap (m_cairo
, CAIRO_LINE_CAP_ROUND
); break;
1912 switch (m_pen
.GetJoin())
1914 case wxJOIN_BEVEL
: cairo_set_line_join (m_cairo
, CAIRO_LINE_JOIN_BEVEL
); break;
1915 case wxJOIN_MITER
: cairo_set_line_join (m_cairo
, CAIRO_LINE_JOIN_MITER
); break;
1917 default: cairo_set_line_join (m_cairo
, CAIRO_LINE_JOIN_ROUND
); break;
1920 unsigned char red
= m_pen
.GetColour().Red();
1921 unsigned char blue
= m_pen
.GetColour().Blue();
1922 unsigned char green
= m_pen
.GetColour().Green();
1923 unsigned char alpha
= m_pen
.GetColour().Alpha();
1925 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
&& alpha
== m_currentAlpha
))
1927 double redPS
= (double)(red
) / 255.0;
1928 double bluePS
= (double)(blue
) / 255.0;
1929 double greenPS
= (double)(green
) / 255.0;
1930 double alphaPS
= (double)(alpha
) / 255.0;
1932 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1935 m_currentBlue
= blue
;
1936 m_currentGreen
= green
;
1937 m_currentAlpha
= alpha
;
1941 void wxGtkPrinterDCImpl::SetBrush( const wxBrush
& brush
)
1943 if (!brush
.Ok()) return;
1947 if (m_brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
1949 cairo_set_source_rgba( m_cairo
, 0, 0, 0, 0 );
1958 unsigned char red
= m_brush
.GetColour().Red();
1959 unsigned char blue
= m_brush
.GetColour().Blue();
1960 unsigned char green
= m_brush
.GetColour().Green();
1961 unsigned char alpha
= m_brush
.GetColour().Alpha();
1963 double redPS
= (double)(red
) / 255.0;
1964 double bluePS
= (double)(blue
) / 255.0;
1965 double greenPS
= (double)(green
) / 255.0;
1966 double alphaPS
= (double)(alpha
) / 255.0;
1968 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
&& alpha
== m_currentAlpha
))
1970 cairo_set_source_rgba( m_cairo
, redPS
, greenPS
, bluePS
, alphaPS
);
1973 m_currentBlue
= blue
;
1974 m_currentGreen
= green
;
1975 m_currentAlpha
= alpha
;
1978 if (m_brush
.IsHatch())
1981 cairo_surface_t
*surface
;
1982 surface
= cairo_surface_create_similar(cairo_get_target(m_cairo
),CAIRO_CONTENT_COLOR_ALPHA
,10,10);
1983 cr
= cairo_create(surface
);
1984 cairo_set_line_cap(cr
, CAIRO_LINE_CAP_SQUARE
);
1985 cairo_set_line_width(cr
, 1);
1986 cairo_set_line_join(cr
,CAIRO_LINE_JOIN_MITER
);
1988 switch (m_brush
.GetStyle())
1990 case wxBRUSHSTYLE_CROSS_HATCH
:
1991 cairo_move_to(cr
, 5, 0);
1992 cairo_line_to(cr
, 5, 10);
1993 cairo_move_to(cr
, 0, 5);
1994 cairo_line_to(cr
, 10, 5);
1996 case wxBRUSHSTYLE_BDIAGONAL_HATCH
:
1997 cairo_move_to(cr
, 0, 10);
1998 cairo_line_to(cr
, 10, 0);
2000 case wxBRUSHSTYLE_FDIAGONAL_HATCH
:
2001 cairo_move_to(cr
, 0, 0);
2002 cairo_line_to(cr
, 10, 10);
2004 case wxBRUSHSTYLE_CROSSDIAG_HATCH
:
2005 cairo_move_to(cr
, 0, 0);
2006 cairo_line_to(cr
, 10, 10);
2007 cairo_move_to(cr
, 10, 0);
2008 cairo_line_to(cr
, 0, 10);
2010 case wxBRUSHSTYLE_HORIZONTAL_HATCH
:
2011 cairo_move_to(cr
, 0, 5);
2012 cairo_line_to(cr
, 10, 5);
2014 case wxBRUSHSTYLE_VERTICAL_HATCH
:
2015 cairo_move_to(cr
, 5, 0);
2016 cairo_line_to(cr
, 5, 10);
2019 wxFAIL_MSG(_("Couldn't get hatch style from wxBrush."));
2022 cairo_set_source_rgba(cr
, redPS
, greenPS
, bluePS
, alphaPS
);
2026 cairo_pattern_t
* pattern
= cairo_pattern_create_for_surface (surface
);
2027 cairo_surface_destroy(surface
);
2028 cairo_pattern_set_extend (pattern
, CAIRO_EXTEND_REPEAT
);
2029 cairo_set_source(m_cairo
, pattern
);
2030 cairo_pattern_destroy(pattern
);
2034 void wxGtkPrinterDCImpl::SetLogicalFunction( wxRasterOperationMode function
)
2036 if (function
== wxCLEAR
)
2037 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_CLEAR
);
2038 else if (function
== wxOR
)
2039 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_OUT
);
2040 else if (function
== wxNO_OP
)
2041 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_DEST
);
2042 else if (function
== wxAND
)
2043 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_ADD
);
2044 else if (function
== wxSET
)
2045 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_SATURATE
);
2046 else if (function
== wxXOR
)
2047 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_XOR
);
2048 else // wxCOPY or anything else.
2049 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_SOURCE
);
2052 void wxGtkPrinterDCImpl::SetBackground( const wxBrush
& brush
)
2054 m_backgroundBrush
= brush
;
2055 cairo_save(m_cairo
);
2056 cairo_set_operator (m_cairo
, CAIRO_OPERATOR_DEST_OVER
);
2058 SetBrush(m_backgroundBrush
);
2059 cairo_paint(m_cairo
);
2060 cairo_restore(m_cairo
);
2063 void wxGtkPrinterDCImpl::SetBackgroundMode(int mode
)
2065 if (mode
== wxBRUSHSTYLE_SOLID
)
2066 m_backgroundMode
= wxBRUSHSTYLE_SOLID
;
2068 m_backgroundMode
= wxBRUSHSTYLE_TRANSPARENT
;
2071 void wxGtkPrinterDCImpl::DoSetClippingRegion(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
2073 cairo_rectangle ( m_cairo
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEVREL(width
), YLOG2DEVREL(height
));
2074 cairo_clip(m_cairo
);
2077 void wxGtkPrinterDCImpl::DestroyClippingRegion()
2079 cairo_reset_clip(m_cairo
);
2082 bool wxGtkPrinterDCImpl::StartDoc(const wxString
& WXUNUSED(message
))
2087 void wxGtkPrinterDCImpl::EndDoc()
2092 void wxGtkPrinterDCImpl::StartPage()
2097 void wxGtkPrinterDCImpl::EndPage()
2102 wxCoord
wxGtkPrinterDCImpl::GetCharHeight() const
2104 pango_layout_set_text( m_layout
, "H", 1 );
2107 pango_layout_get_pixel_size( m_layout
, &w
, &h
);
2109 return wxRound( h
* m_PS2DEV
);
2112 wxCoord
wxGtkPrinterDCImpl::GetCharWidth() const
2114 pango_layout_set_text( m_layout
, "H", 1 );
2117 pango_layout_get_pixel_size( m_layout
, &w
, &h
);
2119 return wxRound( w
* m_PS2DEV
);
2122 void wxGtkPrinterDCImpl::DoGetTextExtent(const wxString
& string
, wxCoord
*width
, wxCoord
*height
,
2124 wxCoord
*externalLeading
,
2125 const wxFont
*theFont
) const
2133 if ( externalLeading
)
2134 *externalLeading
= 0;
2141 cairo_save( m_cairo
);
2142 cairo_scale(m_cairo
, m_scaleX
, m_scaleY
);
2144 // Set layout's text
2145 const wxScopedCharBuffer dataUTF8
= string
.utf8_str();
2150 // scale the font and apply it
2151 PangoFontDescription
*desc
= theFont
->GetNativeFontInfo()->description
;
2152 float size
= pango_font_description_get_size(desc
);
2153 size
= size
* GetFontPointSizeAdjustment(72.0);
2154 pango_font_description_set_size(desc
, (gint
)size
);
2156 pango_layout_set_font_description(m_layout
, desc
);
2159 pango_layout_set_text( m_layout
, dataUTF8
, strlen(dataUTF8
) );
2162 pango_layout_get_pixel_size( m_layout
, width
, &h
);
2168 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
2169 int baseline
= pango_layout_iter_get_baseline(iter
);
2170 pango_layout_iter_free(iter
);
2171 *descent
= h
- PANGO_PIXELS(baseline
);
2176 // restore font and reset font's size back
2177 pango_layout_set_font_description(m_layout
, m_fontdesc
);
2179 PangoFontDescription
*desc
= theFont
->GetNativeFontInfo()->description
;
2180 pango_font_description_set_size(desc
, oldSize
);
2183 cairo_restore( m_cairo
);
2186 void wxGtkPrinterDCImpl::DoGetSize(int* width
, int* height
) const
2188 GtkPageSetup
*setup
= gtk_print_context_get_page_setup( m_gpc
);
2191 *width
= wxRound( (double)gtk_page_setup_get_paper_width( setup
, GTK_UNIT_POINTS
) * (double)m_resolution
/ 72.0 );
2193 *height
= wxRound( (double)gtk_page_setup_get_paper_height( setup
, GTK_UNIT_POINTS
) * (double)m_resolution
/ 72.0 );
2196 void wxGtkPrinterDCImpl::DoGetSizeMM(int *width
, int *height
) const
2198 GtkPageSetup
*setup
= gtk_print_context_get_page_setup( m_gpc
);
2201 *width
= wxRound( gtk_page_setup_get_paper_width( setup
, GTK_UNIT_MM
) );
2203 *height
= wxRound( gtk_page_setup_get_paper_height( setup
, GTK_UNIT_MM
) );
2206 wxSize
wxGtkPrinterDCImpl::GetPPI() const
2208 return wxSize( (int)m_resolution
, (int)m_resolution
);
2211 void wxGtkPrinterDCImpl::SetPrintData(const wxPrintData
& data
)
2216 // overridden for wxPrinterDC Impl
2218 wxRect
wxGtkPrinterDCImpl::GetPaperRect() const
2220 // Does GtkPrint support printer margins?
2223 DoGetSize( &w
, &h
);
2224 return wxRect( 0,0,w
,h
);
2227 int wxGtkPrinterDCImpl::GetResolution() const
2229 return m_resolution
;
2232 // ----------------------------------------------------------------------------
2234 // ----------------------------------------------------------------------------
2236 IMPLEMENT_CLASS(wxGtkPrintPreview
, wxPrintPreviewBase
)
2238 void wxGtkPrintPreview::Init(wxPrintout
* WXUNUSED(printout
),
2239 wxPrintout
* WXUNUSED(printoutForPrinting
),
2242 // convert wxPrintQuality to resolution (input pointer can be NULL)
2243 wxPrintQuality quality
= data
? data
->GetQuality() : wxPRINT_QUALITY_MEDIUM
;
2246 case wxPRINT_QUALITY_HIGH
:
2247 m_resolution
= 1200;
2250 case wxPRINT_QUALITY_LOW
:
2254 case wxPRINT_QUALITY_DRAFT
:
2261 // positive values directly indicate print resolution
2262 m_resolution
= quality
;
2266 wxFAIL_MSG( "unknown print quality" );
2269 case wxPRINT_QUALITY_MEDIUM
:
2278 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout
*printout
,
2279 wxPrintout
*printoutForPrinting
,
2280 wxPrintDialogData
*data
)
2281 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
2283 Init(printout
, printoutForPrinting
, data
? &data
->GetPrintData() : NULL
);
2286 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout
*printout
,
2287 wxPrintout
*printoutForPrinting
,
2289 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
2291 Init(printout
, printoutForPrinting
, data
);
2294 wxGtkPrintPreview::~wxGtkPrintPreview()
2298 bool wxGtkPrintPreview::Print(bool interactive
)
2300 if (!m_printPrintout
)
2303 wxPrinter
printer(& m_printDialogData
);
2304 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
2307 void wxGtkPrintPreview::DetermineScaling()
2309 wxPaperSize paperType
= m_printDialogData
.GetPrintData().GetPaperId();
2311 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
2313 paper
= wxThePrintPaperDatabase
->FindPaperType(wxPAPER_A4
);
2317 const wxSize screenPPI
= wxGetDisplayPPI();
2318 int logPPIScreenX
= screenPPI
.GetWidth();
2319 int logPPIScreenY
= screenPPI
.GetHeight();
2320 int logPPIPrinterX
= m_resolution
;
2321 int logPPIPrinterY
= m_resolution
;
2323 m_previewPrintout
->SetPPIScreen( logPPIScreenX
, logPPIScreenY
);
2324 m_previewPrintout
->SetPPIPrinter( logPPIPrinterX
, logPPIPrinterY
);
2326 // Get width and height in points (1/72th of an inch)
2327 wxSize
sizeDevUnits(paper
->GetSizeDeviceUnits());
2328 sizeDevUnits
.x
= wxRound((double)sizeDevUnits
.x
* (double)m_resolution
/ 72.0);
2329 sizeDevUnits
.y
= wxRound((double)sizeDevUnits
.y
* (double)m_resolution
/ 72.0);
2331 wxSize
sizeTenthsMM(paper
->GetSize());
2332 wxSize
sizeMM(sizeTenthsMM
.x
/ 10, sizeTenthsMM
.y
/ 10);
2334 // If in landscape mode, we need to swap the width and height.
2335 if ( m_printDialogData
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
2337 m_pageWidth
= sizeDevUnits
.y
;
2338 m_pageHeight
= sizeDevUnits
.x
;
2339 m_previewPrintout
->SetPageSizeMM(sizeMM
.y
, sizeMM
.x
);
2343 m_pageWidth
= sizeDevUnits
.x
;
2344 m_pageHeight
= sizeDevUnits
.y
;
2345 m_previewPrintout
->SetPageSizeMM(sizeMM
.x
, sizeMM
.y
);
2347 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
2348 m_previewPrintout
->SetPaperRectPixels(wxRect(0, 0, m_pageWidth
, m_pageHeight
));
2350 // At 100%, the page should look about page-size on the screen.
2351 m_previewScaleX
= float(logPPIScreenX
) / logPPIPrinterX
;
2352 m_previewScaleY
= float(logPPIScreenY
) / logPPIPrinterY
;