]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/print.cpp
setting correct 'default' for m_filterIndex, closes #11541
[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 wxFloodFillStyle 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[],
1431 wxCoord xoffset, wxCoord yoffset,
1432 wxPolygonFillMode fillStyle)
1433 {
1434 if (n==0) return;
1435
1436 cairo_save(m_cairo);
1437 if (fillStyle == wxWINDING_RULE)
1438 cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_WINDING);
1439 else
1440 cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_EVEN_ODD);
1441
1442 int x = points[0].x + xoffset;
1443 int y = points[0].y + yoffset;
1444 cairo_new_path(m_cairo);
1445 cairo_move_to( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1446 int i;
1447 for (i = 1; i < n; i++)
1448 {
1449 int x = points[i].x + xoffset;
1450 int y = points[i].y + yoffset;
1451 cairo_line_to( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1452 }
1453 cairo_close_path(m_cairo);
1454
1455 SetBrush( m_brush );
1456 cairo_fill_preserve( m_cairo );
1457
1458 SetPen (m_pen);
1459 cairo_stroke( m_cairo );
1460
1461 CalcBoundingBox( x, y );
1462
1463 cairo_restore(m_cairo);
1464 }
1465
1466 void wxGtkPrinterDCImpl::DoDrawPolyPolygon(int n, int count[], wxPoint points[],
1467 wxCoord xoffset, wxCoord yoffset,
1468 wxPolygonFillMode fillStyle)
1469 {
1470 wxDCImpl::DoDrawPolyPolygon( n, count, points, xoffset, yoffset, fillStyle );
1471 }
1472
1473 void wxGtkPrinterDCImpl::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1474 {
1475 width--;
1476 height--;
1477
1478 cairo_new_path(m_cairo);
1479 cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height));
1480
1481 SetBrush( m_brush );
1482 cairo_fill_preserve( m_cairo );
1483
1484 SetPen (m_pen);
1485 cairo_stroke( m_cairo );
1486
1487 CalcBoundingBox( x, y );
1488 CalcBoundingBox( x + width, y + height );
1489 }
1490
1491 void wxGtkPrinterDCImpl::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
1492 {
1493 width--;
1494 height--;
1495
1496 if (radius < 0.0) radius = - radius * ((width < height) ? width : height);
1497
1498 wxCoord dd = 2 * (wxCoord) radius;
1499 if (dd > width) dd = width;
1500 if (dd > height) dd = height;
1501 radius = dd / 2;
1502
1503 wxCoord rad = (wxCoord) radius;
1504
1505 cairo_new_path(m_cairo);
1506 cairo_move_to(m_cairo,XLOG2DEV(x + rad),YLOG2DEV(y));
1507 cairo_curve_to(m_cairo,
1508 XLOG2DEV(x + rad),YLOG2DEV(y),
1509 XLOG2DEV(x),YLOG2DEV(y),
1510 XLOG2DEV(x),YLOG2DEV(y + rad));
1511 cairo_line_to(m_cairo,XLOG2DEV(x),YLOG2DEV(y + height - rad));
1512 cairo_curve_to(m_cairo,
1513 XLOG2DEV(x),YLOG2DEV(y + height - rad),
1514 XLOG2DEV(x),YLOG2DEV(y + height),
1515 XLOG2DEV(x + rad),YLOG2DEV(y + height));
1516 cairo_line_to(m_cairo,XLOG2DEV(x + width - rad),YLOG2DEV(y + height));
1517 cairo_curve_to(m_cairo,
1518 XLOG2DEV(x + width - rad),YLOG2DEV(y + height),
1519 XLOG2DEV(x + width),YLOG2DEV(y + height),
1520 XLOG2DEV(x + width),YLOG2DEV(y + height - rad));
1521 cairo_line_to(m_cairo,XLOG2DEV(x + width),YLOG2DEV(y + rad));
1522 cairo_curve_to(m_cairo,
1523 XLOG2DEV(x + width),YLOG2DEV(y + rad),
1524 XLOG2DEV(x + width),YLOG2DEV(y),
1525 XLOG2DEV(x + width - rad),YLOG2DEV(y));
1526 cairo_line_to(m_cairo,XLOG2DEV(x + rad),YLOG2DEV(y));
1527 cairo_close_path(m_cairo);
1528
1529 SetBrush(m_brush);
1530 cairo_fill_preserve(m_cairo);
1531
1532 SetPen(m_pen);
1533 cairo_stroke(m_cairo);
1534
1535 CalcBoundingBox(x,y);
1536 CalcBoundingBox(x+width,y+height);
1537 }
1538
1539 void wxGtkPrinterDCImpl::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1540 {
1541 width--;
1542 height--;
1543
1544 cairo_save (m_cairo);
1545
1546 cairo_new_path(m_cairo);
1547
1548 cairo_translate (m_cairo, XLOG2DEV((wxCoord) (x + width / 2.)), YLOG2DEV((wxCoord) (y + height / 2.)));
1549 cairo_scale(m_cairo, 1, (double)YLOG2DEVREL(height)/(double)XLOG2DEVREL(width));
1550 cairo_arc ( m_cairo, 0, 0, XLOG2DEVREL(width/2), 0, 2 * M_PI);
1551
1552 SetBrush( m_brush );
1553 cairo_fill_preserve( m_cairo );
1554
1555 SetPen (m_pen);
1556 cairo_stroke( m_cairo );
1557
1558 CalcBoundingBox( x, y );
1559 CalcBoundingBox( x + width, y + height );
1560
1561 cairo_restore (m_cairo);
1562 }
1563
1564 #if wxUSE_SPLINES
1565 void wxGtkPrinterDCImpl::DoDrawSpline(const wxPointList *points)
1566 {
1567 SetPen (m_pen);
1568
1569 double c, d, x1, y1, x2, y2, x3, y3;
1570 wxPoint *p, *q;
1571
1572 wxPointList::compatibility_iterator node = points->GetFirst();
1573 p = node->GetData();
1574 x1 = p->x;
1575 y1 = p->y;
1576
1577 node = node->GetNext();
1578 p = node->GetData();
1579 c = p->x;
1580 d = p->y;
1581 x3 =
1582 (double)(x1 + c) / 2;
1583 y3 =
1584 (double)(y1 + d) / 2;
1585
1586 cairo_new_path( m_cairo );
1587 cairo_move_to( m_cairo, XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1) );
1588 cairo_line_to( m_cairo, XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
1589
1590 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1591 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1592
1593 node = node->GetNext();
1594 while (node)
1595 {
1596 q = node->GetData();
1597
1598 x1 = x3;
1599 y1 = y3;
1600 x2 = c;
1601 y2 = d;
1602 c = q->x;
1603 d = q->y;
1604 x3 = (double)(x2 + c) / 2;
1605 y3 = (double)(y2 + d) / 2;
1606
1607 cairo_curve_to(m_cairo,
1608 XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1),
1609 XLOG2DEV((wxCoord)x2), YLOG2DEV((wxCoord)y2),
1610 XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
1611
1612 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1613 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1614
1615 node = node->GetNext();
1616 }
1617
1618 cairo_line_to ( m_cairo, XLOG2DEV((wxCoord)c), YLOG2DEV((wxCoord)d) );
1619
1620 cairo_stroke( m_cairo );
1621 }
1622 #endif // wxUSE_SPLINES
1623
1624 bool wxGtkPrinterDCImpl::DoBlit(wxCoord xdest, wxCoord ydest,
1625 wxCoord width, wxCoord height,
1626 wxDC *source, wxCoord xsrc, wxCoord ysrc,
1627 wxRasterOperationMode rop, bool useMask,
1628 wxCoord WXUNUSED_UNLESS_DEBUG(xsrcMask),
1629 wxCoord WXUNUSED_UNLESS_DEBUG(ysrcMask))
1630 {
1631 wxASSERT_MSG( xsrcMask == wxDefaultCoord && ysrcMask == wxDefaultCoord,
1632 wxT("mask coordinates are not supported") );
1633
1634 wxCHECK_MSG( source, false, wxT("invalid source dc") );
1635
1636 // Blit into a bitmap.
1637 wxBitmap bitmap( width, height );
1638 wxMemoryDC memDC;
1639 memDC.SelectObject(bitmap);
1640 memDC.Blit(0, 0, width, height, source, xsrc, ysrc, rop);
1641 memDC.SelectObject(wxNullBitmap);
1642
1643 // Draw bitmap. scaling and positioning is done there.
1644 GetOwner()->DrawBitmap( bitmap, xdest, ydest, useMask );
1645
1646 return true;
1647 }
1648
1649 void wxGtkPrinterDCImpl::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
1650 {
1651 DoDrawBitmap( icon, x, y, true );
1652 }
1653
1654 void wxGtkPrinterDCImpl::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool useMask )
1655 {
1656 wxCHECK_RET( bitmap.IsOk(), wxT("Invalid bitmap in wxGtkPrinterDCImpl::DoDrawBitmap"));
1657
1658 x = wxCoord(XLOG2DEV(x));
1659 y = wxCoord(YLOG2DEV(y));
1660 int bw = bitmap.GetWidth();
1661 int bh = bitmap.GetHeight();
1662 wxBitmap bmpSource = bitmap; // we need a non-const instance.
1663 if (!useMask && !bitmap.HasPixbuf() && bitmap.GetMask())
1664 bmpSource.SetMask(NULL);
1665
1666 cairo_save(m_cairo);
1667
1668 // Prepare to draw the image.
1669 cairo_translate(m_cairo, x, y);
1670
1671 // Scale the image
1672 wxDouble scaleX = (wxDouble) XLOG2DEVREL(bw) / (wxDouble) bw;
1673 wxDouble scaleY = (wxDouble) YLOG2DEVREL(bh) / (wxDouble) bh;
1674 cairo_scale(m_cairo, scaleX, scaleY);
1675
1676 gdk_cairo_set_source_pixbuf(m_cairo, bmpSource.GetPixbuf(), 0, 0);
1677 cairo_pattern_set_filter(cairo_get_source(m_cairo), CAIRO_FILTER_NEAREST);
1678 // Use the original size here since the context is scaled already.
1679 cairo_rectangle(m_cairo, 0, 0, bw, bh);
1680 // Fill the rectangle using the pattern.
1681 cairo_fill(m_cairo);
1682
1683 CalcBoundingBox(0,0);
1684 CalcBoundingBox(bw,bh);
1685
1686 cairo_restore(m_cairo);
1687 }
1688
1689 void wxGtkPrinterDCImpl::DoDrawText(const wxString& text, wxCoord x, wxCoord y )
1690 {
1691 DoDrawRotatedText( text, x, y, 0.0 );
1692 }
1693
1694 void wxGtkPrinterDCImpl::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
1695 {
1696 double xx = XLOG2DEV(x);
1697 double yy = YLOG2DEV(y);
1698
1699 angle = -angle;
1700
1701 bool underlined = m_font.Ok() && m_font.GetUnderlined();
1702
1703 const wxScopedCharBuffer data = text.utf8_str();
1704
1705 size_t datalen = strlen(data);
1706 pango_layout_set_text( m_layout, data, datalen);
1707
1708 if (underlined)
1709 {
1710 PangoAttrList *attrs = pango_attr_list_new();
1711 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
1712 a->start_index = 0;
1713 a->end_index = datalen;
1714 pango_attr_list_insert(attrs, a);
1715 pango_layout_set_attributes(m_layout, attrs);
1716 pango_attr_list_unref(attrs);
1717 }
1718
1719 if (m_textForegroundColour.Ok())
1720 {
1721 unsigned char red = m_textForegroundColour.Red();
1722 unsigned char blue = m_textForegroundColour.Blue();
1723 unsigned char green = m_textForegroundColour.Green();
1724 unsigned char alpha = m_textForegroundColour.Alpha();
1725
1726 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1727 {
1728 double redPS = (double)(red) / 255.0;
1729 double bluePS = (double)(blue) / 255.0;
1730 double greenPS = (double)(green) / 255.0;
1731 double alphaPS = (double)(alpha) / 255.0;
1732
1733 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1734
1735 m_currentRed = red;
1736 m_currentBlue = blue;
1737 m_currentGreen = green;
1738 m_currentAlpha = alpha;
1739 }
1740 }
1741
1742 // Draw layout.
1743 cairo_move_to (m_cairo, xx, yy);
1744
1745 cairo_save( m_cairo );
1746
1747 if (fabs(angle) > 0.00001)
1748 cairo_rotate( m_cairo, angle*DEG2RAD );
1749
1750 cairo_scale(m_cairo, m_scaleX, m_scaleY);
1751
1752 int w,h;
1753 pango_layout_get_pixel_size( m_layout, &w, &h );
1754
1755 if ( m_backgroundMode == wxBRUSHSTYLE_SOLID )
1756 {
1757 unsigned char red = m_textBackgroundColour.Red();
1758 unsigned char blue = m_textBackgroundColour.Blue();
1759 unsigned char green = m_textBackgroundColour.Green();
1760 unsigned char alpha = m_textBackgroundColour.Alpha();
1761
1762 double redPS = (double)(red) / 255.0;
1763 double bluePS = (double)(blue) / 255.0;
1764 double greenPS = (double)(green) / 255.0;
1765 double alphaPS = (double)(alpha) / 255.0;
1766
1767 cairo_save(m_cairo);
1768 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1769 cairo_rectangle(m_cairo, 0, 0, w, h); // still in cairo units
1770 cairo_fill(m_cairo);
1771 cairo_restore(m_cairo);
1772 }
1773
1774 pango_cairo_update_layout (m_cairo, m_layout);
1775 pango_cairo_show_layout (m_cairo, m_layout);
1776
1777 cairo_restore( m_cairo );
1778
1779 if (underlined)
1780 {
1781 // Undo underline attributes setting
1782 pango_layout_set_attributes(m_layout, NULL);
1783 }
1784
1785 // Back to device units:
1786 CalcBoundingBox (x, y);
1787 CalcBoundingBox (x + w, y + h);
1788 }
1789
1790 void wxGtkPrinterDCImpl::Clear()
1791 {
1792 // Clear does nothing for printing, but keep the code
1793 // for later reuse
1794 /*
1795 cairo_save(m_cairo);
1796 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
1797 SetBrush(m_backgroundBrush);
1798 cairo_paint(m_cairo);
1799 cairo_restore(m_cairo);
1800 */
1801 }
1802
1803 void wxGtkPrinterDCImpl::SetFont( const wxFont& font )
1804 {
1805 m_font = font;
1806
1807 if (m_font.Ok())
1808 {
1809 if (m_fontdesc)
1810 pango_font_description_free( m_fontdesc );
1811
1812 m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description );
1813
1814 float size = pango_font_description_get_size( m_fontdesc );
1815 size = size * GetFontPointSizeAdjustment(72.0);
1816 pango_font_description_set_size( m_fontdesc, (gint)size );
1817
1818 pango_layout_set_font_description( m_layout, m_fontdesc );
1819 }
1820 }
1821
1822 void wxGtkPrinterDCImpl::SetPen( const wxPen& pen )
1823 {
1824 if (!pen.Ok()) return;
1825
1826 m_pen = pen;
1827
1828 double width;
1829
1830 if (m_pen.GetWidth() <= 0)
1831 width = 0.1;
1832 else
1833 width = (double) m_pen.GetWidth();
1834
1835 cairo_set_line_width( m_cairo, width * m_DEV2PS * m_scaleX );
1836 static const double dotted[] = {2.0, 5.0};
1837 static const double short_dashed[] = {4.0, 4.0};
1838 static const double long_dashed[] = {4.0, 8.0};
1839 static const double dotted_dashed[] = {6.0, 6.0, 2.0, 6.0};
1840
1841 switch (m_pen.GetStyle())
1842 {
1843 case wxPENSTYLE_DOT: cairo_set_dash( m_cairo, dotted, 2, 0 ); break;
1844 case wxPENSTYLE_SHORT_DASH: cairo_set_dash( m_cairo, short_dashed, 2, 0 ); break;
1845 case wxPENSTYLE_LONG_DASH: cairo_set_dash( m_cairo, long_dashed, 2, 0 ); break;
1846 case wxPENSTYLE_DOT_DASH: cairo_set_dash( m_cairo, dotted_dashed, 4, 0 ); break;
1847 case wxPENSTYLE_USER_DASH:
1848 {
1849 wxDash *wx_dashes;
1850 int num = m_pen.GetDashes (&wx_dashes);
1851 gdouble *g_dashes = g_new( gdouble, num );
1852 int i;
1853 for (i = 0; i < num; ++i)
1854 g_dashes[i] = (gdouble) wx_dashes[i];
1855 cairo_set_dash( m_cairo, g_dashes, num, 0);
1856 g_free( g_dashes );
1857 }
1858 break;
1859 case wxPENSTYLE_SOLID:
1860 case wxPENSTYLE_TRANSPARENT:
1861 default: cairo_set_dash( m_cairo, NULL, 0, 0 ); break;
1862 }
1863
1864 switch (m_pen.GetCap())
1865 {
1866 case wxCAP_PROJECTING: cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_SQUARE); break;
1867 case wxCAP_BUTT: cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_BUTT); break;
1868 case wxCAP_ROUND:
1869 default: cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_ROUND); break;
1870 }
1871
1872 switch (m_pen.GetJoin())
1873 {
1874 case wxJOIN_BEVEL: cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_BEVEL); break;
1875 case wxJOIN_MITER: cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_MITER); break;
1876 case wxJOIN_ROUND:
1877 default: cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_ROUND); break;
1878 }
1879
1880 unsigned char red = m_pen.GetColour().Red();
1881 unsigned char blue = m_pen.GetColour().Blue();
1882 unsigned char green = m_pen.GetColour().Green();
1883 unsigned char alpha = m_pen.GetColour().Alpha();
1884
1885 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1886 {
1887 double redPS = (double)(red) / 255.0;
1888 double bluePS = (double)(blue) / 255.0;
1889 double greenPS = (double)(green) / 255.0;
1890 double alphaPS = (double)(alpha) / 255.0;
1891
1892 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1893
1894 m_currentRed = red;
1895 m_currentBlue = blue;
1896 m_currentGreen = green;
1897 m_currentAlpha = alpha;
1898 }
1899 }
1900
1901 void wxGtkPrinterDCImpl::SetBrush( const wxBrush& brush )
1902 {
1903 if (!brush.Ok()) return;
1904
1905 m_brush = brush;
1906
1907 if (m_brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT)
1908 {
1909 cairo_set_source_rgba( m_cairo, 0, 0, 0, 0 );
1910 m_currentRed = 0;
1911 m_currentBlue = 0;
1912 m_currentGreen = 0;
1913 m_currentAlpha = 0;
1914 return;
1915 }
1916
1917 // Brush colour.
1918 unsigned char red = m_brush.GetColour().Red();
1919 unsigned char blue = m_brush.GetColour().Blue();
1920 unsigned char green = m_brush.GetColour().Green();
1921 unsigned char alpha = m_brush.GetColour().Alpha();
1922
1923 double redPS = (double)(red) / 255.0;
1924 double bluePS = (double)(blue) / 255.0;
1925 double greenPS = (double)(green) / 255.0;
1926 double alphaPS = (double)(alpha) / 255.0;
1927
1928 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1929 {
1930 cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1931
1932 m_currentRed = red;
1933 m_currentBlue = blue;
1934 m_currentGreen = green;
1935 m_currentAlpha = alpha;
1936 }
1937
1938 if (m_brush.IsHatch())
1939 {
1940 cairo_t * cr;
1941 cairo_surface_t *surface;
1942 surface = cairo_surface_create_similar(cairo_get_target(m_cairo),CAIRO_CONTENT_COLOR_ALPHA,10,10);
1943 cr = cairo_create(surface);
1944 cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
1945 cairo_set_line_width(cr, 1);
1946 cairo_set_line_join(cr,CAIRO_LINE_JOIN_MITER);
1947
1948 switch (m_brush.GetStyle())
1949 {
1950 case wxBRUSHSTYLE_CROSS_HATCH:
1951 cairo_move_to(cr, 5, 0);
1952 cairo_line_to(cr, 5, 10);
1953 cairo_move_to(cr, 0, 5);
1954 cairo_line_to(cr, 10, 5);
1955 break;
1956 case wxBRUSHSTYLE_BDIAGONAL_HATCH:
1957 cairo_move_to(cr, 0, 10);
1958 cairo_line_to(cr, 10, 0);
1959 break;
1960 case wxBRUSHSTYLE_FDIAGONAL_HATCH:
1961 cairo_move_to(cr, 0, 0);
1962 cairo_line_to(cr, 10, 10);
1963 break;
1964 case wxBRUSHSTYLE_CROSSDIAG_HATCH:
1965 cairo_move_to(cr, 0, 0);
1966 cairo_line_to(cr, 10, 10);
1967 cairo_move_to(cr, 10, 0);
1968 cairo_line_to(cr, 0, 10);
1969 break;
1970 case wxBRUSHSTYLE_HORIZONTAL_HATCH:
1971 cairo_move_to(cr, 0, 5);
1972 cairo_line_to(cr, 10, 5);
1973 break;
1974 case wxBRUSHSTYLE_VERTICAL_HATCH:
1975 cairo_move_to(cr, 5, 0);
1976 cairo_line_to(cr, 5, 10);
1977 break;
1978 default:
1979 wxFAIL_MSG(_("Couldn't get hatch style from wxBrush."));
1980 }
1981
1982 cairo_set_source_rgba(cr, redPS, greenPS, bluePS, alphaPS);
1983 cairo_stroke (cr);
1984
1985 cairo_destroy(cr);
1986 cairo_pattern_t * pattern = cairo_pattern_create_for_surface (surface);
1987 cairo_surface_destroy(surface);
1988 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
1989 cairo_set_source(m_cairo, pattern);
1990 cairo_pattern_destroy(pattern);
1991 }
1992 }
1993
1994 void wxGtkPrinterDCImpl::SetLogicalFunction( wxRasterOperationMode function )
1995 {
1996 if (function == wxCLEAR)
1997 cairo_set_operator (m_cairo, CAIRO_OPERATOR_CLEAR);
1998 else if (function == wxOR)
1999 cairo_set_operator (m_cairo, CAIRO_OPERATOR_OUT);
2000 else if (function == wxNO_OP)
2001 cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST);
2002 else if (function == wxAND)
2003 cairo_set_operator (m_cairo, CAIRO_OPERATOR_ADD);
2004 else if (function == wxSET)
2005 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SATURATE);
2006 else if (function == wxXOR)
2007 cairo_set_operator (m_cairo, CAIRO_OPERATOR_XOR);
2008 else // wxCOPY or anything else.
2009 cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
2010 }
2011
2012 void wxGtkPrinterDCImpl::SetBackground( const wxBrush& brush )
2013 {
2014 m_backgroundBrush = brush;
2015 cairo_save(m_cairo);
2016 cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST_OVER);
2017
2018 SetBrush(m_backgroundBrush);
2019 cairo_paint(m_cairo);
2020 cairo_restore(m_cairo);
2021 }
2022
2023 void wxGtkPrinterDCImpl::SetBackgroundMode(int mode)
2024 {
2025 if (mode == wxBRUSHSTYLE_SOLID)
2026 m_backgroundMode = wxBRUSHSTYLE_SOLID;
2027 else
2028 m_backgroundMode = wxBRUSHSTYLE_TRANSPARENT;
2029 }
2030
2031 void wxGtkPrinterDCImpl::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
2032 {
2033 cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height));
2034 cairo_clip(m_cairo);
2035 }
2036
2037 void wxGtkPrinterDCImpl::DestroyClippingRegion()
2038 {
2039 cairo_reset_clip(m_cairo);
2040 }
2041
2042 bool wxGtkPrinterDCImpl::StartDoc(const wxString& WXUNUSED(message))
2043 {
2044 return true;
2045 }
2046
2047 void wxGtkPrinterDCImpl::EndDoc()
2048 {
2049 return;
2050 }
2051
2052 void wxGtkPrinterDCImpl::StartPage()
2053 {
2054 return;
2055 }
2056
2057 void wxGtkPrinterDCImpl::EndPage()
2058 {
2059 return;
2060 }
2061
2062 wxCoord wxGtkPrinterDCImpl::GetCharHeight() const
2063 {
2064 pango_layout_set_text( m_layout, "H", 1 );
2065
2066 int w,h;
2067 pango_layout_get_pixel_size( m_layout, &w, &h );
2068
2069 return wxRound( h * m_PS2DEV );
2070 }
2071
2072 wxCoord wxGtkPrinterDCImpl::GetCharWidth() const
2073 {
2074 pango_layout_set_text( m_layout, "H", 1 );
2075
2076 int w,h;
2077 pango_layout_get_pixel_size( m_layout, &w, &h );
2078
2079 return wxRound( w * m_PS2DEV );
2080 }
2081
2082 void wxGtkPrinterDCImpl::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoord *height,
2083 wxCoord *descent,
2084 wxCoord *externalLeading,
2085 const wxFont *theFont ) const
2086 {
2087 if ( width )
2088 *width = 0;
2089 if ( height )
2090 *height = 0;
2091 if ( descent )
2092 *descent = 0;
2093 if ( externalLeading )
2094 *externalLeading = 0;
2095
2096 if (string.empty())
2097 {
2098 return;
2099 }
2100
2101 cairo_save( m_cairo );
2102 cairo_scale(m_cairo, m_scaleX, m_scaleY);
2103
2104 // Set layout's text
2105 const wxScopedCharBuffer dataUTF8 = string.utf8_str();
2106
2107 gint oldSize=0;
2108 if ( theFont )
2109 {
2110 // scale the font and apply it
2111 PangoFontDescription *desc = theFont->GetNativeFontInfo()->description;
2112 float size = pango_font_description_get_size(desc);
2113 size = size * GetFontPointSizeAdjustment(72.0);
2114 pango_font_description_set_size(desc, (gint)size);
2115
2116 pango_layout_set_font_description(m_layout, desc);
2117 }
2118
2119 pango_layout_set_text( m_layout, dataUTF8, strlen(dataUTF8) );
2120
2121 int h;
2122 pango_layout_get_pixel_size( m_layout, width, &h );
2123 if ( height )
2124 *height = h;
2125
2126 if (descent)
2127 {
2128 PangoLayoutIter *iter = pango_layout_get_iter(m_layout);
2129 int baseline = pango_layout_iter_get_baseline(iter);
2130 pango_layout_iter_free(iter);
2131 *descent = h - PANGO_PIXELS(baseline);
2132 }
2133
2134 if ( theFont )
2135 {
2136 // restore font and reset font's size back
2137 pango_layout_set_font_description(m_layout, m_fontdesc);
2138
2139 PangoFontDescription *desc = theFont->GetNativeFontInfo()->description;
2140 pango_font_description_set_size(desc, oldSize);
2141 }
2142
2143 cairo_restore( m_cairo );
2144 }
2145
2146 void wxGtkPrinterDCImpl::DoGetSize(int* width, int* height) const
2147 {
2148 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
2149
2150 if (width)
2151 *width = wxRound( (double)gtk_page_setup_get_paper_width( setup, GTK_UNIT_POINTS ) * (double)m_resolution / 72.0 );
2152 if (height)
2153 *height = wxRound( (double)gtk_page_setup_get_paper_height( setup, GTK_UNIT_POINTS ) * (double)m_resolution / 72.0 );
2154 }
2155
2156 void wxGtkPrinterDCImpl::DoGetSizeMM(int *width, int *height) const
2157 {
2158 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
2159
2160 if (width)
2161 *width = wxRound( gtk_page_setup_get_paper_width( setup, GTK_UNIT_MM ) );
2162 if (height)
2163 *height = wxRound( gtk_page_setup_get_paper_height( setup, GTK_UNIT_MM ) );
2164 }
2165
2166 wxSize wxGtkPrinterDCImpl::GetPPI() const
2167 {
2168 return wxSize( (int)m_resolution, (int)m_resolution );
2169 }
2170
2171 void wxGtkPrinterDCImpl::SetPrintData(const wxPrintData& data)
2172 {
2173 m_printData = data;
2174 }
2175
2176 // overriden for wxPrinterDC Impl
2177
2178 wxRect wxGtkPrinterDCImpl::GetPaperRect() const
2179 {
2180 // Does GtkPrint support printer margins?
2181 int w = 0;
2182 int h = 0;
2183 DoGetSize( &w, &h );
2184 return wxRect( 0,0,w,h );
2185 }
2186
2187 int wxGtkPrinterDCImpl::GetResolution() const
2188 {
2189 return m_resolution;
2190 }
2191
2192 // ----------------------------------------------------------------------------
2193 // Print preview
2194 // ----------------------------------------------------------------------------
2195
2196 IMPLEMENT_CLASS(wxGtkPrintPreview, wxPrintPreviewBase)
2197
2198 void wxGtkPrintPreview::Init(wxPrintout * WXUNUSED(printout),
2199 wxPrintout * WXUNUSED(printoutForPrinting),
2200 wxPrintData *data)
2201 {
2202 // convert wxPrintQuality to resolution (input pointer can be NULL)
2203 wxPrintQuality quality = data ? data->GetQuality() : wxPRINT_QUALITY_MEDIUM;
2204 switch ( quality )
2205 {
2206 case wxPRINT_QUALITY_HIGH:
2207 m_resolution = 1200;
2208 break;
2209
2210 case wxPRINT_QUALITY_LOW:
2211 m_resolution = 300;
2212 break;
2213
2214 case wxPRINT_QUALITY_DRAFT:
2215 m_resolution = 150;
2216 break;
2217
2218 default:
2219 if ( quality > 0 )
2220 {
2221 // positive values directly indicate print resolution
2222 m_resolution = quality;
2223 break;
2224 }
2225
2226 wxFAIL_MSG( "unknown print quality" );
2227 // fall through
2228
2229 case wxPRINT_QUALITY_MEDIUM:
2230 m_resolution = 600;
2231 break;
2232
2233 }
2234
2235 DetermineScaling();
2236 }
2237
2238 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout,
2239 wxPrintout *printoutForPrinting,
2240 wxPrintDialogData *data)
2241 : wxPrintPreviewBase(printout, printoutForPrinting, data)
2242 {
2243 Init(printout, printoutForPrinting, data ? &data->GetPrintData() : NULL);
2244 }
2245
2246 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout,
2247 wxPrintout *printoutForPrinting,
2248 wxPrintData *data)
2249 : wxPrintPreviewBase(printout, printoutForPrinting, data)
2250 {
2251 Init(printout, printoutForPrinting, data);
2252 }
2253
2254 wxGtkPrintPreview::~wxGtkPrintPreview()
2255 {
2256 }
2257
2258 bool wxGtkPrintPreview::Print(bool interactive)
2259 {
2260 if (!m_printPrintout)
2261 return false;
2262
2263 wxPrinter printer(& m_printDialogData);
2264 return printer.Print(m_previewFrame, m_printPrintout, interactive);
2265 }
2266
2267 void wxGtkPrintPreview::DetermineScaling()
2268 {
2269 wxPaperSize paperType = m_printDialogData.GetPrintData().GetPaperId();
2270
2271 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
2272 if (!paper)
2273 paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
2274
2275 if (paper)
2276 {
2277 m_previewPrintout->SetPPIScreen(wxGetDisplayPPI());
2278 m_previewPrintout->SetPPIPrinter( m_resolution, m_resolution );
2279
2280 // Get width and height in points (1/72th of an inch)
2281 wxSize sizeDevUnits(paper->GetSizeDeviceUnits());
2282 sizeDevUnits.x = wxRound((double)sizeDevUnits.x * (double)m_resolution / 72.0);
2283 sizeDevUnits.y = wxRound((double)sizeDevUnits.y * (double)m_resolution / 72.0);
2284
2285 wxSize sizeTenthsMM(paper->GetSize());
2286 wxSize sizeMM(sizeTenthsMM.x / 10, sizeTenthsMM.y / 10);
2287
2288 // If in landscape mode, we need to swap the width and height.
2289 if ( m_printDialogData.GetPrintData().GetOrientation() == wxLANDSCAPE )
2290 {
2291 m_pageWidth = sizeDevUnits.y;
2292 m_pageHeight = sizeDevUnits.x;
2293 m_previewPrintout->SetPageSizeMM(sizeMM.y, sizeMM.x);
2294 }
2295 else
2296 {
2297 m_pageWidth = sizeDevUnits.x;
2298 m_pageHeight = sizeDevUnits.y;
2299 m_previewPrintout->SetPageSizeMM(sizeMM.x, sizeMM.y);
2300 }
2301 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
2302 m_previewPrintout->SetPaperRectPixels(wxRect(0, 0, m_pageWidth, m_pageHeight));
2303
2304 // At 100%, the page should look about page-size on the screen.
2305 m_previewScaleX = 0.8 * 72.0 / (double)m_resolution;
2306 m_previewScaleY = m_previewScaleX;
2307 }
2308 }
2309
2310 #endif
2311 // wxUSE_GTKPRINT