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