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