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