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