]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/print.cpp
Italian translation corrections from Roberto Boriotti.
[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
a1b806b9 1741 bool underlined = m_font.IsOk() && m_font.GetUnderlined();
fa034c45 1742
197380a0 1743 const wxScopedCharBuffer data = text.utf8_str();
fa034c45
RR
1744
1745 size_t datalen = strlen(data);
1746 pango_layout_set_text( m_layout, data, datalen);
1747
1748 if (underlined)
1749 {
1750 PangoAttrList *attrs = pango_attr_list_new();
1751 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
1752 a->start_index = 0;
1753 a->end_index = datalen;
1754 pango_attr_list_insert(attrs, a);
1755 pango_layout_set_attributes(m_layout, attrs);
1756 pango_attr_list_unref(attrs);
1757 }
1758
a1b806b9 1759 if (m_textForegroundColour.IsOk())
fa034c45
RR
1760 {
1761 unsigned char red = m_textForegroundColour.Red();
1762 unsigned char blue = m_textForegroundColour.Blue();
1763 unsigned char green = m_textForegroundColour.Green();
1764 unsigned char alpha = m_textForegroundColour.Alpha();
1765
1766 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1767 {
1768 double redPS = (double)(red) / 255.0;
1769 double bluePS = (double)(blue) / 255.0;
1770 double greenPS = (double)(green) / 255.0;
1771 double alphaPS = (double)(alpha) / 255.0;
1772
f7b8e3d6 1773 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
fa034c45
RR
1774
1775 m_currentRed = red;
1776 m_currentBlue = blue;
1777 m_currentGreen = green;
1778 m_currentAlpha = alpha;
1779 }
1780 }
1781
a9312950 1782 // Draw layout.
f7b8e3d6 1783 cairo_move_to (m_cairo, xx, yy);
a7060b8c 1784
f7b8e3d6 1785 cairo_save( m_cairo );
a7060b8c 1786
a9312950 1787 if (fabs(angle) > 0.00001)
f7b8e3d6 1788 cairo_rotate( m_cairo, angle*DEG2RAD );
a7060b8c 1789
12ca5586
VS
1790 cairo_scale(m_cairo, m_scaleX, m_scaleY);
1791
1792 int w,h;
1793 pango_layout_get_pixel_size( m_layout, &w, &h );
1794
1795 if ( m_backgroundMode == wxBRUSHSTYLE_SOLID )
1796 {
1797 unsigned char red = m_textBackgroundColour.Red();
1798 unsigned char blue = m_textBackgroundColour.Blue();
1799 unsigned char green = m_textBackgroundColour.Green();
1800 unsigned char alpha = m_textBackgroundColour.Alpha();
1801
1802 double redPS = (double)(red) / 255.0;
1803 double bluePS = (double)(blue) / 255.0;
1804 double greenPS = (double)(green) / 255.0;
1805 double alphaPS = (double)(alpha) / 255.0;
1806
1807 cairo_save(m_cairo);
1808 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1809 cairo_rectangle(m_cairo, 0, 0, w, h); // still in cairo units
1810 cairo_fill(m_cairo);
1811 cairo_restore(m_cairo);
1812 }
1813
f7b8e3d6
VS
1814 pango_cairo_update_layout (m_cairo, m_layout);
1815 pango_cairo_show_layout (m_cairo, m_layout);
a7060b8c 1816
f7b8e3d6 1817 cairo_restore( m_cairo );
fa034c45
RR
1818
1819 if (underlined)
1820 {
1821 // Undo underline attributes setting
1822 pango_layout_set_attributes(m_layout, NULL);
1823 }
a7060b8c 1824
1d9fe50d 1825 // Back to device units:
a9312950
RR
1826 CalcBoundingBox (x, y);
1827 CalcBoundingBox (x + w, y + h);
fa034c45
RR
1828}
1829
888dde65 1830void wxGtkPrinterDCImpl::Clear()
fa034c45 1831{
0187f0bc 1832// Clear does nothing for printing, but keep the code
6d52ca53 1833// for later reuse
0187f0bc 1834/*
f7b8e3d6
VS
1835 cairo_save(m_cairo);
1836 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
fa034c45 1837 SetBrush(m_backgroundBrush);
f7b8e3d6
VS
1838 cairo_paint(m_cairo);
1839 cairo_restore(m_cairo);
0187f0bc 1840*/
fa034c45
RR
1841}
1842
888dde65 1843void wxGtkPrinterDCImpl::SetFont( const wxFont& font )
fa034c45
RR
1844{
1845 m_font = font;
1846
a1b806b9 1847 if (m_font.IsOk())
fa034c45
RR
1848 {
1849 if (m_fontdesc)
1850 pango_font_description_free( m_fontdesc );
1851
3e7ea45c 1852 m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description );
1d9fe50d 1853
12ca5586
VS
1854 float size = pango_font_description_get_size( m_fontdesc );
1855 size = size * GetFontPointSizeAdjustment(72.0);
1856 pango_font_description_set_size( m_fontdesc, (gint)size );
1857
fa034c45
RR
1858 pango_layout_set_font_description( m_layout, m_fontdesc );
1859 }
1860}
1861
888dde65 1862void wxGtkPrinterDCImpl::SetPen( const wxPen& pen )
fa034c45 1863{
a1b806b9 1864 if (!pen.IsOk()) return;
fa034c45
RR
1865
1866 m_pen = pen;
1867
b29b9485 1868 double width;
6d52ca53 1869
b29b9485
RR
1870 if (m_pen.GetWidth() <= 0)
1871 width = 0.1;
1872 else
1873 width = (double) m_pen.GetWidth();
fa034c45 1874
f7b8e3d6 1875 cairo_set_line_width( m_cairo, width * m_DEV2PS * m_scaleX );
fa034c45
RR
1876 static const double dotted[] = {2.0, 5.0};
1877 static const double short_dashed[] = {4.0, 4.0};
1878 static const double long_dashed[] = {4.0, 8.0};
1879 static const double dotted_dashed[] = {6.0, 6.0, 2.0, 6.0};
1880
1881 switch (m_pen.GetStyle())
1882 {
f7b8e3d6
VS
1883 case wxPENSTYLE_DOT: cairo_set_dash( m_cairo, dotted, 2, 0 ); break;
1884 case wxPENSTYLE_SHORT_DASH: cairo_set_dash( m_cairo, short_dashed, 2, 0 ); break;
1885 case wxPENSTYLE_LONG_DASH: cairo_set_dash( m_cairo, long_dashed, 2, 0 ); break;
1886 case wxPENSTYLE_DOT_DASH: cairo_set_dash( m_cairo, dotted_dashed, 4, 0 ); break;
04ee05f9 1887 case wxPENSTYLE_USER_DASH:
fa034c45
RR
1888 {
1889 wxDash *wx_dashes;
da249bc3 1890 int num = m_pen.GetDashes (&wx_dashes);
fa034c45
RR
1891 gdouble *g_dashes = g_new( gdouble, num );
1892 int i;
1893 for (i = 0; i < num; ++i)
1894 g_dashes[i] = (gdouble) wx_dashes[i];
f7b8e3d6 1895 cairo_set_dash( m_cairo, g_dashes, num, 0);
fa034c45
RR
1896 g_free( g_dashes );
1897 }
1898 break;
04ee05f9
PC
1899 case wxPENSTYLE_SOLID:
1900 case wxPENSTYLE_TRANSPARENT:
f7b8e3d6 1901 default: cairo_set_dash( m_cairo, NULL, 0, 0 ); break;
fa034c45
RR
1902 }
1903
1904 switch (m_pen.GetCap())
1905 {
f7b8e3d6
VS
1906 case wxCAP_PROJECTING: cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_SQUARE); break;
1907 case wxCAP_BUTT: cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_BUTT); break;
fa034c45 1908 case wxCAP_ROUND:
f7b8e3d6 1909 default: cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_ROUND); break;
fa034c45
RR
1910 }
1911
1912 switch (m_pen.GetJoin())
1913 {
f7b8e3d6
VS
1914 case wxJOIN_BEVEL: cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_BEVEL); break;
1915 case wxJOIN_MITER: cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_MITER); break;
fa034c45 1916 case wxJOIN_ROUND:
f7b8e3d6 1917 default: cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_ROUND); break;
fa034c45
RR
1918 }
1919
1920 unsigned char red = m_pen.GetColour().Red();
1921 unsigned char blue = m_pen.GetColour().Blue();
1922 unsigned char green = m_pen.GetColour().Green();
1923 unsigned char alpha = m_pen.GetColour().Alpha();
1924
1925 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1926 {
1927 double redPS = (double)(red) / 255.0;
1928 double bluePS = (double)(blue) / 255.0;
1929 double greenPS = (double)(green) / 255.0;
1930 double alphaPS = (double)(alpha) / 255.0;
1931
f7b8e3d6 1932 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
fa034c45
RR
1933
1934 m_currentRed = red;
1935 m_currentBlue = blue;
1936 m_currentGreen = green;
1937 m_currentAlpha = alpha;
1938 }
1939}
1940
888dde65 1941void wxGtkPrinterDCImpl::SetBrush( const wxBrush& brush )
fa034c45 1942{
a1b806b9 1943 if (!brush.IsOk()) return;
fa034c45
RR
1944
1945 m_brush = brush;
1946
04ee05f9 1947 if (m_brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT)
a9312950 1948 {
f7b8e3d6 1949 cairo_set_source_rgba( m_cairo, 0, 0, 0, 0 );
a9312950
RR
1950 m_currentRed = 0;
1951 m_currentBlue = 0;
1952 m_currentGreen = 0;
1953 m_currentAlpha = 0;
1954 return;
1955 }
1956
fa034c45
RR
1957 // Brush colour.
1958 unsigned char red = m_brush.GetColour().Red();
1959 unsigned char blue = m_brush.GetColour().Blue();
1960 unsigned char green = m_brush.GetColour().Green();
1961 unsigned char alpha = m_brush.GetColour().Alpha();
1962
1963 double redPS = (double)(red) / 255.0;
1964 double bluePS = (double)(blue) / 255.0;
1965 double greenPS = (double)(green) / 255.0;
1966 double alphaPS = (double)(alpha) / 255.0;
1967
1968 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1969 {
f7b8e3d6 1970 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
fa034c45
RR
1971
1972 m_currentRed = red;
1973 m_currentBlue = blue;
1974 m_currentGreen = green;
1975 m_currentAlpha = alpha;
1976 }
1977
1978 if (m_brush.IsHatch())
1979 {
1980 cairo_t * cr;
1981 cairo_surface_t *surface;
f7b8e3d6
VS
1982 surface = cairo_surface_create_similar(cairo_get_target(m_cairo),CAIRO_CONTENT_COLOR_ALPHA,10,10);
1983 cr = cairo_create(surface);
1984 cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
1985 cairo_set_line_width(cr, 1);
1986 cairo_set_line_join(cr,CAIRO_LINE_JOIN_MITER);
fa034c45
RR
1987
1988 switch (m_brush.GetStyle())
1989 {
04ee05f9 1990 case wxBRUSHSTYLE_CROSS_HATCH:
f7b8e3d6
VS
1991 cairo_move_to(cr, 5, 0);
1992 cairo_line_to(cr, 5, 10);
1993 cairo_move_to(cr, 0, 5);
1994 cairo_line_to(cr, 10, 5);
fa034c45 1995 break;
04ee05f9 1996 case wxBRUSHSTYLE_BDIAGONAL_HATCH:
f7b8e3d6
VS
1997 cairo_move_to(cr, 0, 10);
1998 cairo_line_to(cr, 10, 0);
fa034c45 1999 break;
04ee05f9 2000 case wxBRUSHSTYLE_FDIAGONAL_HATCH:
f7b8e3d6
VS
2001 cairo_move_to(cr, 0, 0);
2002 cairo_line_to(cr, 10, 10);
fa034c45 2003 break;
04ee05f9 2004 case wxBRUSHSTYLE_CROSSDIAG_HATCH:
f7b8e3d6
VS
2005 cairo_move_to(cr, 0, 0);
2006 cairo_line_to(cr, 10, 10);
2007 cairo_move_to(cr, 10, 0);
2008 cairo_line_to(cr, 0, 10);
fa034c45 2009 break;
04ee05f9 2010 case wxBRUSHSTYLE_HORIZONTAL_HATCH:
f7b8e3d6
VS
2011 cairo_move_to(cr, 0, 5);
2012 cairo_line_to(cr, 10, 5);
fa034c45 2013 break;
04ee05f9 2014 case wxBRUSHSTYLE_VERTICAL_HATCH:
f7b8e3d6
VS
2015 cairo_move_to(cr, 5, 0);
2016 cairo_line_to(cr, 5, 10);
fa034c45
RR
2017 break;
2018 default:
2019 wxFAIL_MSG(_("Couldn't get hatch style from wxBrush."));
2020 }
2021
f7b8e3d6
VS
2022 cairo_set_source_rgba(cr, redPS, greenPS, bluePS, alphaPS);
2023 cairo_stroke (cr);
fa034c45 2024
f7b8e3d6
VS
2025 cairo_destroy(cr);
2026 cairo_pattern_t * pattern = cairo_pattern_create_for_surface (surface);
2027 cairo_surface_destroy(surface);
2028 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
2029 cairo_set_source(m_cairo, pattern);
2030 cairo_pattern_destroy(pattern);
fa034c45
RR
2031 }
2032}
2033
89efaf2b 2034void wxGtkPrinterDCImpl::SetLogicalFunction( wxRasterOperationMode function )
fa034c45
RR
2035{
2036 if (function == wxCLEAR)
f7b8e3d6 2037 cairo_set_operator (m_cairo, CAIRO_OPERATOR_CLEAR);
fa034c45 2038 else if (function == wxOR)
f7b8e3d6 2039 cairo_set_operator (m_cairo, CAIRO_OPERATOR_OUT);
fa034c45 2040 else if (function == wxNO_OP)
f7b8e3d6 2041 cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST);
fa034c45 2042 else if (function == wxAND)
f7b8e3d6 2043 cairo_set_operator (m_cairo, CAIRO_OPERATOR_ADD);
fa034c45 2044 else if (function == wxSET)
f7b8e3d6 2045 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SATURATE);
fa034c45 2046 else if (function == wxXOR)
f7b8e3d6 2047 cairo_set_operator (m_cairo, CAIRO_OPERATOR_XOR);
fa034c45 2048 else // wxCOPY or anything else.
f7b8e3d6 2049 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
fa034c45
RR
2050}
2051
888dde65 2052void wxGtkPrinterDCImpl::SetBackground( const wxBrush& brush )
fa034c45
RR
2053{
2054 m_backgroundBrush = brush;
f7b8e3d6
VS
2055 cairo_save(m_cairo);
2056 cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST_OVER);
fa034c45
RR
2057
2058 SetBrush(m_backgroundBrush);
f7b8e3d6
VS
2059 cairo_paint(m_cairo);
2060 cairo_restore(m_cairo);
fa034c45
RR
2061}
2062
888dde65 2063void wxGtkPrinterDCImpl::SetBackgroundMode(int mode)
fa034c45 2064{
04ee05f9
PC
2065 if (mode == wxBRUSHSTYLE_SOLID)
2066 m_backgroundMode = wxBRUSHSTYLE_SOLID;
e0d1fd7f 2067 else
04ee05f9 2068 m_backgroundMode = wxBRUSHSTYLE_TRANSPARENT;
fa034c45
RR
2069}
2070
888dde65 2071void wxGtkPrinterDCImpl::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
fa034c45 2072{
f7b8e3d6
VS
2073 cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height));
2074 cairo_clip(m_cairo);
fa034c45
RR
2075}
2076
888dde65 2077void wxGtkPrinterDCImpl::DestroyClippingRegion()
fa034c45 2078{
f7b8e3d6 2079 cairo_reset_clip(m_cairo);
fa034c45
RR
2080}
2081
888dde65 2082bool wxGtkPrinterDCImpl::StartDoc(const wxString& WXUNUSED(message))
fa034c45
RR
2083{
2084 return true;
2085}
2086
888dde65 2087void wxGtkPrinterDCImpl::EndDoc()
fa034c45
RR
2088{
2089 return;
2090}
2091
888dde65 2092void wxGtkPrinterDCImpl::StartPage()
fa034c45
RR
2093{
2094 return;
2095}
2096
888dde65 2097void wxGtkPrinterDCImpl::EndPage()
fa034c45
RR
2098{
2099 return;
2100}
2101
888dde65 2102wxCoord wxGtkPrinterDCImpl::GetCharHeight() const
fa034c45
RR
2103{
2104 pango_layout_set_text( m_layout, "H", 1 );
2105
2106 int w,h;
2107 pango_layout_get_pixel_size( m_layout, &w, &h );
2108
a9312950 2109 return wxRound( h * m_PS2DEV );
fa034c45
RR
2110}
2111
888dde65 2112wxCoord wxGtkPrinterDCImpl::GetCharWidth() const
fa034c45
RR
2113{
2114 pango_layout_set_text( m_layout, "H", 1 );
2115
2116 int w,h;
2117 pango_layout_get_pixel_size( m_layout, &w, &h );
2118
a9312950 2119 return wxRound( w * m_PS2DEV );
fa034c45
RR
2120}
2121
888dde65 2122void wxGtkPrinterDCImpl::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoord *height,
fa034c45
RR
2123 wxCoord *descent,
2124 wxCoord *externalLeading,
2125 const wxFont *theFont ) const
2126{
2127 if ( width )
2128 *width = 0;
2129 if ( height )
2130 *height = 0;
2131 if ( descent )
2132 *descent = 0;
2133 if ( externalLeading )
2134 *externalLeading = 0;
2135
2136 if (string.empty())
2137 {
2138 return;
2139 }
2140
12ca5586
VS
2141 cairo_save( m_cairo );
2142 cairo_scale(m_cairo, m_scaleX, m_scaleY);
2143
fa034c45 2144 // Set layout's text
197380a0 2145 const wxScopedCharBuffer dataUTF8 = string.utf8_str();
fa034c45 2146
8cf694d4 2147 gint oldSize=0;
12ca5586
VS
2148 if ( theFont )
2149 {
2150 // scale the font and apply it
2151 PangoFontDescription *desc = theFont->GetNativeFontInfo()->description;
2152 float size = pango_font_description_get_size(desc);
2153 size = size * GetFontPointSizeAdjustment(72.0);
2154 pango_font_description_set_size(desc, (gint)size);
fa034c45 2155
12ca5586
VS
2156 pango_layout_set_font_description(m_layout, desc);
2157 }
fa034c45
RR
2158
2159 pango_layout_set_text( m_layout, dataUTF8, strlen(dataUTF8) );
2160
12ca5586
VS
2161 int h;
2162 pango_layout_get_pixel_size( m_layout, width, &h );
2163 if ( height )
2164 *height = h;
a7060b8c 2165
fa034c45
RR
2166 if (descent)
2167 {
2168 PangoLayoutIter *iter = pango_layout_get_iter(m_layout);
2169 int baseline = pango_layout_iter_get_baseline(iter);
2170 pango_layout_iter_free(iter);
12ca5586 2171 *descent = h - PANGO_PIXELS(baseline);
fa034c45
RR
2172 }
2173
12ca5586
VS
2174 if ( theFont )
2175 {
2176 // restore font and reset font's size back
2177 pango_layout_set_font_description(m_layout, m_fontdesc);
fa034c45 2178
12ca5586
VS
2179 PangoFontDescription *desc = theFont->GetNativeFontInfo()->description;
2180 pango_font_description_set_size(desc, oldSize);
2181 }
2182
2183 cairo_restore( m_cairo );
fa034c45
RR
2184}
2185
888dde65 2186void wxGtkPrinterDCImpl::DoGetSize(int* width, int* height) const
fa034c45 2187{
a9312950
RR
2188 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
2189
fa034c45 2190 if (width)
eaeb9985 2191 *width = wxRound( (double)gtk_page_setup_get_paper_width( setup, GTK_UNIT_POINTS ) * (double)m_resolution / 72.0 );
fa034c45 2192 if (height)
eaeb9985 2193 *height = wxRound( (double)gtk_page_setup_get_paper_height( setup, GTK_UNIT_POINTS ) * (double)m_resolution / 72.0 );
fa034c45
RR
2194}
2195
888dde65 2196void wxGtkPrinterDCImpl::DoGetSizeMM(int *width, int *height) const
fa034c45 2197{
a9312950 2198 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
fa034c45
RR
2199
2200 if (width)
a9312950 2201 *width = wxRound( gtk_page_setup_get_paper_width( setup, GTK_UNIT_MM ) );
fa034c45 2202 if (height)
a9312950 2203 *height = wxRound( gtk_page_setup_get_paper_height( setup, GTK_UNIT_MM ) );
fa034c45
RR
2204}
2205
888dde65 2206wxSize wxGtkPrinterDCImpl::GetPPI() const
fa034c45 2207{
a9312950 2208 return wxSize( (int)m_resolution, (int)m_resolution );
fa034c45
RR
2209}
2210
888dde65 2211void wxGtkPrinterDCImpl::SetPrintData(const wxPrintData& data)
fa034c45
RR
2212{
2213 m_printData = data;
fa034c45
RR
2214}
2215
4c51a665 2216// overridden for wxPrinterDC Impl
4f37154e 2217
6d52ca53 2218wxRect wxGtkPrinterDCImpl::GetPaperRect() const
fa034c45 2219{
4f37154e
RR
2220 // Does GtkPrint support printer margins?
2221 int w = 0;
2222 int h = 0;
2223 DoGetSize( &w, &h );
2224 return wxRect( 0,0,w,h );
fa034c45
RR
2225}
2226
6d52ca53 2227int wxGtkPrinterDCImpl::GetResolution() const
fa034c45 2228{
a9312950 2229 return m_resolution;
fa034c45
RR
2230}
2231
2232// ----------------------------------------------------------------------------
2233// Print preview
2234// ----------------------------------------------------------------------------
2235
2236IMPLEMENT_CLASS(wxGtkPrintPreview, wxPrintPreviewBase)
2237
2238void wxGtkPrintPreview::Init(wxPrintout * WXUNUSED(printout),
115be92b
VZ
2239 wxPrintout * WXUNUSED(printoutForPrinting),
2240 wxPrintData *data)
fa034c45 2241{
115be92b
VZ
2242 // convert wxPrintQuality to resolution (input pointer can be NULL)
2243 wxPrintQuality quality = data ? data->GetQuality() : wxPRINT_QUALITY_MEDIUM;
2244 switch ( quality )
2245 {
2246 case wxPRINT_QUALITY_HIGH:
2247 m_resolution = 1200;
2248 break;
2249
115be92b
VZ
2250 case wxPRINT_QUALITY_LOW:
2251 m_resolution = 300;
2252 break;
2253
2254 case wxPRINT_QUALITY_DRAFT:
2255 m_resolution = 150;
2256 break;
1f2a1c3c
VZ
2257
2258 default:
2259 if ( quality > 0 )
2260 {
2261 // positive values directly indicate print resolution
2262 m_resolution = quality;
2263 break;
2264 }
2265
2266 wxFAIL_MSG( "unknown print quality" );
2267 // fall through
2268
2269 case wxPRINT_QUALITY_MEDIUM:
2270 m_resolution = 600;
2271 break;
2272
115be92b 2273 }
1f2a1c3c
VZ
2274
2275 DetermineScaling();
fa034c45
RR
2276}
2277
2278wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout,
115be92b
VZ
2279 wxPrintout *printoutForPrinting,
2280 wxPrintDialogData *data)
2281 : wxPrintPreviewBase(printout, printoutForPrinting, data)
fa034c45 2282{
115be92b 2283 Init(printout, printoutForPrinting, data ? &data->GetPrintData() : NULL);
fa034c45
RR
2284}
2285
2286wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout,
115be92b
VZ
2287 wxPrintout *printoutForPrinting,
2288 wxPrintData *data)
2289 : wxPrintPreviewBase(printout, printoutForPrinting, data)
fa034c45 2290{
115be92b 2291 Init(printout, printoutForPrinting, data);
fa034c45
RR
2292}
2293
2294wxGtkPrintPreview::~wxGtkPrintPreview()
2295{
2296}
2297
2298bool wxGtkPrintPreview::Print(bool interactive)
2299{
2300 if (!m_printPrintout)
2301 return false;
2302
2303 wxPrinter printer(& m_printDialogData);
2304 return printer.Print(m_previewFrame, m_printPrintout, interactive);
2305}
2306
2307void wxGtkPrintPreview::DetermineScaling()
2308{
2309 wxPaperSize paperType = m_printDialogData.GetPrintData().GetPaperId();
2310
2311 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
2312 if (!paper)
2313 paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
2314
2315 if (paper)
2316 {
ac78a90a
VZ
2317 const wxSize screenPPI = wxGetDisplayPPI();
2318 int logPPIScreenX = screenPPI.GetWidth();
2319 int logPPIScreenY = screenPPI.GetHeight();
2320 int logPPIPrinterX = m_resolution;
2321 int logPPIPrinterY = m_resolution;
2322
2323 m_previewPrintout->SetPPIScreen( logPPIScreenX, logPPIScreenY );
2324 m_previewPrintout->SetPPIPrinter( logPPIPrinterX, logPPIPrinterY );
a7060b8c 2325
fa034c45
RR
2326 // Get width and height in points (1/72th of an inch)
2327 wxSize sizeDevUnits(paper->GetSizeDeviceUnits());
115be92b
VZ
2328 sizeDevUnits.x = wxRound((double)sizeDevUnits.x * (double)m_resolution / 72.0);
2329 sizeDevUnits.y = wxRound((double)sizeDevUnits.y * (double)m_resolution / 72.0);
6d52ca53 2330
fa034c45
RR
2331 wxSize sizeTenthsMM(paper->GetSize());
2332 wxSize sizeMM(sizeTenthsMM.x / 10, sizeTenthsMM.y / 10);
2333
2334 // If in landscape mode, we need to swap the width and height.
2335 if ( m_printDialogData.GetPrintData().GetOrientation() == wxLANDSCAPE )
2336 {
2337 m_pageWidth = sizeDevUnits.y;
2338 m_pageHeight = sizeDevUnits.x;
2339 m_previewPrintout->SetPageSizeMM(sizeMM.y, sizeMM.x);
2340 }
2341 else
2342 {
2343 m_pageWidth = sizeDevUnits.x;
2344 m_pageHeight = sizeDevUnits.y;
2345 m_previewPrintout->SetPageSizeMM(sizeMM.x, sizeMM.y);
2346 }
2347 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
2348 m_previewPrintout->SetPaperRectPixels(wxRect(0, 0, m_pageWidth, m_pageHeight));
2349
2350 // At 100%, the page should look about page-size on the screen.
ac78a90a
VZ
2351 m_previewScaleX = float(logPPIScreenX) / logPPIPrinterX;
2352 m_previewScaleY = float(logPPIScreenY) / logPPIPrinterY;
fa034c45
RR
2353 }
2354}
2355
2356#endif
2357 // wxUSE_GTKPRINT