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