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