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