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