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