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