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