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