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