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