]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/print.cpp
Fix compilation of gnome-vfs module after dynlib.h changes back in September:
[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 wxDC* wxGtkPrintFactory::CreatePrinterDC( const wxPrintData& data )
147 {
148 return new wxGtkPrintDC(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 wxGtkPrintDC *printDC = new wxGtkPrintDC( 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 wxGtkPrintDC 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 return new wxGtkPrintDC( m_printDialogData.GetPrintData() );
1069 }
1070
1071 bool wxGtkPrinter::Setup( wxWindow * WXUNUSED(parent) )
1072 {
1073 // Obsolete, for backward compatibility.
1074 return false;
1075 }
1076
1077 //-----------------------------------------------------------------------------
1078 // wxGtkPrintDC
1079 //-----------------------------------------------------------------------------
1080
1081 #define XLOG2DEV(x) ((double)(LogicalToDeviceX(x)) * m_DEV2PS)
1082 #define XLOG2DEVREL(x) ((double)(LogicalToDeviceXRel(x)) * m_DEV2PS)
1083 #define YLOG2DEV(x) ((double)(LogicalToDeviceY(x)) * m_DEV2PS)
1084 #define YLOG2DEVREL(x) ((double)(LogicalToDeviceYRel(x)) * m_DEV2PS)
1085
1086 IMPLEMENT_CLASS(wxGtkPrintDC, wxDC)
1087
1088 wxGtkPrintDC::wxGtkPrintDC( const wxPrintData& data )
1089 {
1090 m_printData = data;
1091
1092 wxGtkPrintNativeData *native =
1093 (wxGtkPrintNativeData*) m_printData.GetNativeData();
1094
1095 m_gpc = native->GetPrintContext();
1096
1097 // Match print quality to resolution (high = 1200dpi)
1098 m_resolution = m_printData.GetQuality(); // (int) gtk_print_context_get_dpi_x( m_gpc );
1099 if (m_resolution < 0)
1100 m_resolution = (1 << (m_resolution+4)) *150;
1101
1102 m_PS2DEV = (double)m_resolution / 72.0;
1103 m_DEV2PS = 72.0 / (double)m_resolution;
1104
1105 m_context = gtk_print_context_create_pango_context( m_gpc );
1106 m_layout = gtk_print_context_create_pango_layout ( m_gpc );
1107 m_fontdesc = pango_font_description_from_string( "Sans 12" );
1108
1109 m_cairo = gtk_print_context_get_cairo_context ( m_gpc );
1110
1111 m_currentRed = 0;
1112 m_currentBlue = 0;
1113 m_currentGreen = 0;
1114
1115 m_signX = 1; // default x-axis left to right.
1116 m_signY = 1; // default y-axis bottom up -> top down.
1117
1118 // By default the origin of the Cairo context is in the upper left
1119 // corner of the printable area. We need to translate it so that it
1120 // is in the upper left corner of the paper (without margins)
1121 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
1122 gdouble ml, mt;
1123 ml = gtk_page_setup_get_left_margin (setup, GTK_UNIT_POINTS);
1124 mt = gtk_page_setup_get_top_margin (setup, GTK_UNIT_POINTS);
1125 gs_cairo->cairo_translate(m_cairo, -ml, -mt);
1126 }
1127
1128 wxGtkPrintDC::~wxGtkPrintDC()
1129 {
1130 g_object_unref(m_context);
1131 g_object_unref(m_layout);
1132 }
1133
1134 bool wxGtkPrintDC::IsOk() const
1135 {
1136 return m_gpc != NULL;
1137 }
1138
1139 bool wxGtkPrintDC::DoFloodFill(wxCoord WXUNUSED(x1),
1140 wxCoord WXUNUSED(y1),
1141 const wxColour& WXUNUSED(col),
1142 int WXUNUSED(style))
1143 {
1144 // We can't access the given coord as a Cairo context is scalable, ie a
1145 // coord doesn't mean anything in this context.
1146 wxFAIL_MSG(_("not implemented"));
1147 return false;
1148 }
1149
1150 void wxGtkPrintDC::DoGradientFillConcentric(const wxRect& rect, const wxColour& initialColour, const wxColour& destColour, const wxPoint& circleCenter)
1151 {
1152 wxCoord xC = circleCenter.x;
1153 wxCoord yC = circleCenter.y;
1154 wxCoord xR = rect.x;
1155 wxCoord yR = rect.y;
1156 wxCoord w = rect.width;
1157 wxCoord h = rect.height;
1158
1159 double radius = sqrt((w/2)*(w/2)+(h/2)*(h/2));
1160
1161 unsigned char redI = initialColour.Red();
1162 unsigned char blueI = initialColour.Blue();
1163 unsigned char greenI = initialColour.Green();
1164 unsigned char alphaI = initialColour.Alpha();
1165 unsigned char redD = destColour.Red();
1166 unsigned char blueD = destColour.Blue();
1167 unsigned char greenD = destColour.Green();
1168 unsigned char alphaD = destColour.Alpha();
1169
1170 double redIPS = (double)(redI) / 255.0;
1171 double blueIPS = (double)(blueI) / 255.0;
1172 double greenIPS = (double)(greenI) / 255.0;
1173 double alphaIPS = (double)(alphaI) / 255.0;
1174 double redDPS = (double)(redD) / 255.0;
1175 double blueDPS = (double)(blueD) / 255.0;
1176 double greenDPS = (double)(greenD) / 255.0;
1177 double alphaDPS = (double)(alphaD) / 255.0;
1178
1179 // Create a pattern with the gradient.
1180 cairo_pattern_t* gradient;
1181 gradient = gs_cairo->cairo_pattern_create_radial (XLOG2DEV(xC+xR), YLOG2DEV(yC+yR), 0, XLOG2DEV(xC+xR), YLOG2DEV(yC+yR), radius * m_DEV2PS );
1182 gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 0.0, redIPS, greenIPS, blueIPS, alphaIPS);
1183 gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 1.0, redDPS, greenDPS, blueDPS, alphaDPS);
1184
1185 // Fill the rectangle with this pattern.
1186 gs_cairo->cairo_set_source(m_cairo, gradient);
1187 gs_cairo->cairo_rectangle (m_cairo, XLOG2DEV(xR), YLOG2DEV(yR), XLOG2DEVREL(w), YLOG2DEVREL(h) );
1188 gs_cairo->cairo_fill(m_cairo);
1189
1190 gs_cairo->cairo_pattern_destroy(gradient);
1191
1192 CalcBoundingBox(xR, yR);
1193 CalcBoundingBox(xR+w, yR+h);
1194 }
1195
1196 void wxGtkPrintDC::DoGradientFillLinear(const wxRect& rect, const wxColour& initialColour, const wxColour& destColour, wxDirection nDirection)
1197 {
1198 wxCoord x = rect.x;
1199 wxCoord y = rect.y;
1200 wxCoord w = rect.width;
1201 wxCoord h = rect.height;
1202
1203 unsigned char redI = initialColour.Red();
1204 unsigned char blueI = initialColour.Blue();
1205 unsigned char greenI = initialColour.Green();
1206 unsigned char alphaI = initialColour.Alpha();
1207 unsigned char redD = destColour.Red();
1208 unsigned char blueD = destColour.Blue();
1209 unsigned char greenD = destColour.Green();
1210 unsigned char alphaD = destColour.Alpha();
1211
1212 double redIPS = (double)(redI) / 255.0;
1213 double blueIPS = (double)(blueI) / 255.0;
1214 double greenIPS = (double)(greenI) / 255.0;
1215 double alphaIPS = (double)(alphaI) / 255.0;
1216 double redDPS = (double)(redD) / 255.0;
1217 double blueDPS = (double)(blueD) / 255.0;
1218 double greenDPS = (double)(greenD) / 255.0;
1219 double alphaDPS = (double)(alphaD) / 255.0;
1220
1221 // Create a pattern with the gradient.
1222 cairo_pattern_t* gradient;
1223 gradient = gs_cairo->cairo_pattern_create_linear (XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x+w), YLOG2DEV(y));
1224
1225 if (nDirection == wxWEST)
1226 {
1227 gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 0.0, redDPS, greenDPS, blueDPS, alphaDPS);
1228 gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 1.0, redIPS, greenIPS, blueIPS, alphaIPS);
1229 }
1230 else {
1231 gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 0.0, redIPS, greenIPS, blueIPS, alphaIPS);
1232 gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 1.0, redDPS, greenDPS, blueDPS, alphaDPS);
1233 }
1234
1235 // Fill the rectangle with this pattern.
1236 gs_cairo->cairo_set_source(m_cairo, gradient);
1237 gs_cairo->cairo_rectangle (m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(w), YLOG2DEVREL(h) );
1238 gs_cairo->cairo_fill(m_cairo);
1239
1240 gs_cairo->cairo_pattern_destroy(gradient);
1241
1242 CalcBoundingBox(x, y);
1243 CalcBoundingBox(x+w, y+h);
1244 }
1245
1246 bool wxGtkPrintDC::DoGetPixel(wxCoord WXUNUSED(x1),
1247 wxCoord WXUNUSED(y1),
1248 wxColour * WXUNUSED(col)) const
1249 {
1250 wxFAIL_MSG(_("not implemented"));
1251 return false;
1252 }
1253
1254 void wxGtkPrintDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
1255 {
1256 if (m_pen.GetStyle() == wxTRANSPARENT) return;
1257
1258 SetPen( m_pen );
1259 gs_cairo->cairo_move_to ( m_cairo, XLOG2DEV(x1), YLOG2DEV(y1) );
1260 gs_cairo->cairo_line_to ( m_cairo, XLOG2DEV(x2), YLOG2DEV(y2) );
1261 gs_cairo->cairo_stroke ( m_cairo );
1262
1263 CalcBoundingBox( x1, y1 );
1264 CalcBoundingBox( x2, y2 );
1265 }
1266
1267 void wxGtkPrintDC::DoCrossHair(wxCoord x, wxCoord y)
1268 {
1269 int w, h;
1270 DoGetSize(&w, &h);
1271
1272 SetPen(m_pen);
1273
1274 gs_cairo->cairo_move_to (m_cairo, XLOG2DEV(x), 0);
1275 gs_cairo->cairo_line_to (m_cairo, XLOG2DEV(x), YLOG2DEVREL(h));
1276 gs_cairo->cairo_move_to (m_cairo, 0, YLOG2DEV(y));
1277 gs_cairo->cairo_line_to (m_cairo, XLOG2DEVREL(w), YLOG2DEV(y));
1278
1279 gs_cairo->cairo_stroke (m_cairo);
1280 CalcBoundingBox( 0, 0 );
1281 CalcBoundingBox( w, h );
1282 }
1283
1284 void wxGtkPrintDC::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord xc,wxCoord yc)
1285 {
1286 double dx = x1 - xc;
1287 double dy = y1 - yc;
1288 double radius = sqrt((double)(dx*dx+dy*dy));
1289
1290 double alpha1, alpha2;
1291 if (x1 == x2 && y1 == y2)
1292 {
1293 alpha1 = 0.0;
1294 alpha2 = 360.0;
1295 }
1296 else
1297 if (radius == 0.0)
1298 {
1299 alpha1 = alpha2 = 0.0;
1300 }
1301 else
1302 {
1303 alpha1 = (x1 - xc == 0) ?
1304 (y1 - yc < 0) ? 90.0 : -90.0 :
1305 atan2(double(y1-yc), double(x1-xc)) * RAD2DEG;
1306 alpha2 = (x2 - xc == 0) ?
1307 (y2 - yc < 0) ? 90.0 : -90.0 :
1308 atan2(double(y2-yc), double(x2-xc)) * RAD2DEG;
1309
1310 while (alpha1 <= 0) alpha1 += 360;
1311 while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between.
1312 while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree.
1313 while (alpha2 > 360) alpha2 -= 360;
1314 }
1315
1316 alpha1 *= DEG2RAD;
1317 alpha2 *= DEG2RAD;
1318
1319 gs_cairo->cairo_new_path(m_cairo);
1320
1321 gs_cairo->cairo_arc_negative ( m_cairo, XLOG2DEV(xc), YLOG2DEV(yc), XLOG2DEVREL((int)radius), alpha1, alpha2);
1322 gs_cairo->cairo_line_to(m_cairo, XLOG2DEV(xc), YLOG2DEV(yc));
1323 gs_cairo->cairo_close_path (m_cairo);
1324
1325 SetBrush( m_brush );
1326 gs_cairo->cairo_fill_preserve( m_cairo );
1327
1328 SetPen (m_pen);
1329 gs_cairo->cairo_stroke( m_cairo );
1330
1331 CalcBoundingBox (x1, y1);
1332 CalcBoundingBox (xc, yc);
1333 CalcBoundingBox (x2, y2);
1334 }
1335
1336 void wxGtkPrintDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
1337 {
1338 gs_cairo->cairo_save( m_cairo );
1339
1340 gs_cairo->cairo_new_path(m_cairo);
1341
1342 gs_cairo->cairo_translate( m_cairo, XLOG2DEV((wxCoord) (x + w / 2.)), XLOG2DEV((wxCoord) (y + h / 2.)) );
1343 double scale = (double)YLOG2DEVREL(h) / (double) XLOG2DEVREL(w);
1344 gs_cairo->cairo_scale( m_cairo, 1.0, scale );
1345
1346 gs_cairo->cairo_arc_negative ( m_cairo, 0, 0, XLOG2DEVREL(w/2), -sa*DEG2RAD, -ea*DEG2RAD);
1347
1348 SetPen (m_pen);
1349 gs_cairo->cairo_stroke_preserve( m_cairo );
1350
1351 gs_cairo->cairo_line_to(m_cairo, 0,0);
1352
1353 SetBrush( m_brush );
1354 gs_cairo->cairo_fill( m_cairo );
1355
1356 gs_cairo->cairo_restore( m_cairo );
1357
1358 CalcBoundingBox( x, y);
1359 CalcBoundingBox( x+w, y+h );
1360 }
1361
1362 void wxGtkPrintDC::DoDrawPoint(wxCoord x, wxCoord y)
1363 {
1364 if (m_pen.GetStyle() == wxTRANSPARENT) return;
1365
1366 SetPen( m_pen );
1367
1368 gs_cairo->cairo_move_to ( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1369 gs_cairo->cairo_line_to ( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1370 gs_cairo->cairo_stroke ( m_cairo );
1371
1372 CalcBoundingBox( x, y );
1373 }
1374
1375 void wxGtkPrintDC::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
1376 {
1377 if (m_pen.GetStyle() == wxTRANSPARENT) return;
1378
1379 if (n <= 0) return;
1380
1381 SetPen (m_pen);
1382
1383 int i;
1384 for ( i =0; i<n ; i++ )
1385 CalcBoundingBox( points[i].x+xoffset, points[i].y+yoffset);
1386
1387 gs_cairo->cairo_move_to ( m_cairo, XLOG2DEV(points[0].x+xoffset), YLOG2DEV(points[0].y+yoffset) );
1388
1389 for (i = 1; i < n; i++)
1390 gs_cairo->cairo_line_to ( m_cairo, XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset) );
1391
1392 gs_cairo->cairo_stroke ( m_cairo);
1393 }
1394
1395 void wxGtkPrintDC::DoDrawPolygon(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
1396 {
1397 if (n==0) return;
1398
1399 gs_cairo->cairo_save(m_cairo);
1400 if (fillStyle == wxWINDING_RULE)
1401 gs_cairo->cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_WINDING);
1402 else
1403 gs_cairo->cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_EVEN_ODD);
1404
1405 int x = points[0].x + xoffset;
1406 int y = points[0].y + yoffset;
1407 gs_cairo->cairo_new_path(m_cairo);
1408 gs_cairo->cairo_move_to( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1409 int i;
1410 for (i = 1; i < n; i++)
1411 {
1412 int x = points[i].x + xoffset;
1413 int y = points[i].y + yoffset;
1414 gs_cairo->cairo_line_to( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
1415 }
1416 gs_cairo->cairo_close_path(m_cairo);
1417
1418 SetBrush( m_brush );
1419 gs_cairo->cairo_fill_preserve( m_cairo );
1420
1421 SetPen (m_pen);
1422 gs_cairo->cairo_stroke( m_cairo );
1423
1424 CalcBoundingBox( x, y );
1425
1426 gs_cairo->cairo_restore(m_cairo);
1427 }
1428
1429 void wxGtkPrintDC::DoDrawPolyPolygon(int n, int count[], wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
1430 {
1431 wxDC::DoDrawPolyPolygon( n, count, points, xoffset, yoffset, fillStyle );
1432 }
1433
1434 void wxGtkPrintDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1435 {
1436 width--;
1437 height--;
1438
1439 gs_cairo->cairo_new_path(m_cairo);
1440 gs_cairo->cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height));
1441
1442 SetBrush( m_brush );
1443 gs_cairo->cairo_fill_preserve( m_cairo );
1444
1445 SetPen (m_pen);
1446 gs_cairo->cairo_stroke( m_cairo );
1447
1448 CalcBoundingBox( x, y );
1449 CalcBoundingBox( x + width, y + height );
1450 }
1451
1452 void wxGtkPrintDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
1453 {
1454 width--;
1455 height--;
1456
1457 if (radius < 0.0) radius = - radius * ((width < height) ? width : height);
1458
1459 wxCoord dd = 2 * (wxCoord) radius;
1460 if (dd > width) dd = width;
1461 if (dd > height) dd = height;
1462 radius = dd / 2;
1463
1464 wxCoord rad = (wxCoord) radius;
1465
1466 gs_cairo->cairo_new_path(m_cairo);
1467 gs_cairo->cairo_move_to(m_cairo,XLOG2DEV(x + rad),YLOG2DEV(y));
1468 gs_cairo->cairo_curve_to(m_cairo,
1469 XLOG2DEV(x + rad),YLOG2DEV(y),
1470 XLOG2DEV(x),YLOG2DEV(y),
1471 XLOG2DEV(x),YLOG2DEV(y + rad));
1472 gs_cairo->cairo_line_to(m_cairo,XLOG2DEV(x),YLOG2DEV(y + height - rad));
1473 gs_cairo->cairo_curve_to(m_cairo,
1474 XLOG2DEV(x),YLOG2DEV(y + height - rad),
1475 XLOG2DEV(x),YLOG2DEV(y + height),
1476 XLOG2DEV(x + rad),YLOG2DEV(y + height));
1477 gs_cairo->cairo_line_to(m_cairo,XLOG2DEV(x + width - rad),YLOG2DEV(y + height));
1478 gs_cairo->cairo_curve_to(m_cairo,
1479 XLOG2DEV(x + width - rad),YLOG2DEV(y + height),
1480 XLOG2DEV(x + width),YLOG2DEV(y + height),
1481 XLOG2DEV(x + width),YLOG2DEV(y + height - rad));
1482 gs_cairo->cairo_line_to(m_cairo,XLOG2DEV(x + width),YLOG2DEV(y + rad));
1483 gs_cairo->cairo_curve_to(m_cairo,
1484 XLOG2DEV(x + width),YLOG2DEV(y + rad),
1485 XLOG2DEV(x + width),YLOG2DEV(y),
1486 XLOG2DEV(x + width - rad),YLOG2DEV(y));
1487 gs_cairo->cairo_line_to(m_cairo,XLOG2DEV(x + rad),YLOG2DEV(y));
1488 gs_cairo->cairo_close_path(m_cairo);
1489
1490 SetBrush(m_brush);
1491 gs_cairo->cairo_fill_preserve(m_cairo);
1492
1493 SetPen(m_pen);
1494 gs_cairo->cairo_stroke(m_cairo);
1495
1496 CalcBoundingBox(x,y);
1497 CalcBoundingBox(x+width,y+height);
1498 }
1499
1500 void wxGtkPrintDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1501 {
1502 width--;
1503 height--;
1504
1505 gs_cairo->cairo_save (m_cairo);
1506
1507 gs_cairo->cairo_new_path(m_cairo);
1508
1509 gs_cairo->cairo_translate (m_cairo, XLOG2DEV((wxCoord) (x + width / 2.)), YLOG2DEV((wxCoord) (y + height / 2.)));
1510 gs_cairo->cairo_scale(m_cairo, 1, (double)YLOG2DEVREL(height)/(double)XLOG2DEVREL(width));
1511 gs_cairo->cairo_arc ( m_cairo, 0, 0, XLOG2DEVREL(width/2), 0, 2 * M_PI);
1512
1513 SetBrush( m_brush );
1514 gs_cairo->cairo_fill_preserve( m_cairo );
1515
1516 SetPen (m_pen);
1517 gs_cairo->cairo_stroke( m_cairo );
1518
1519 CalcBoundingBox( x, y );
1520 CalcBoundingBox( x + width, y + height );
1521
1522 gs_cairo->cairo_restore (m_cairo);
1523 }
1524
1525 #if wxUSE_SPLINES
1526 void wxGtkPrintDC::DoDrawSpline(const wxPointList *points)
1527 {
1528 SetPen (m_pen);
1529
1530 double c, d, x1, y1, x2, y2, x3, y3;
1531 wxPoint *p, *q;
1532
1533 wxPointList::compatibility_iterator node = points->GetFirst();
1534 p = node->GetData();
1535 x1 = p->x;
1536 y1 = p->y;
1537
1538 node = node->GetNext();
1539 p = node->GetData();
1540 c = p->x;
1541 d = p->y;
1542 x3 =
1543 (double)(x1 + c) / 2;
1544 y3 =
1545 (double)(y1 + d) / 2;
1546
1547 gs_cairo->cairo_new_path( m_cairo );
1548 gs_cairo->cairo_move_to( m_cairo, XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1) );
1549 gs_cairo->cairo_line_to( m_cairo, XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
1550
1551 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1552 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1553
1554 node = node->GetNext();
1555 while (node)
1556 {
1557 q = node->GetData();
1558
1559 x1 = x3;
1560 y1 = y3;
1561 x2 = c;
1562 y2 = d;
1563 c = q->x;
1564 d = q->y;
1565 x3 = (double)(x2 + c) / 2;
1566 y3 = (double)(y2 + d) / 2;
1567
1568 gs_cairo->cairo_curve_to(m_cairo,
1569 XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1),
1570 XLOG2DEV((wxCoord)x2), YLOG2DEV((wxCoord)y2),
1571 XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
1572
1573 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1574 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1575
1576 node = node->GetNext();
1577 }
1578
1579 gs_cairo->cairo_line_to ( m_cairo, XLOG2DEV((wxCoord)c), YLOG2DEV((wxCoord)d) );
1580
1581 gs_cairo->cairo_stroke( m_cairo );
1582 }
1583 #endif // wxUSE_SPLINES
1584
1585 bool wxGtkPrintDC::DoBlit(wxCoord xdest, wxCoord ydest,
1586 wxCoord width, wxCoord height,
1587 wxDC *source, wxCoord xsrc, wxCoord ysrc,
1588 int rop, bool useMask,
1589 wxCoord WXUNUSED_UNLESS_DEBUG(xsrcMask),
1590 wxCoord WXUNUSED_UNLESS_DEBUG(ysrcMask))
1591 {
1592 wxASSERT_MSG( xsrcMask == wxDefaultCoord && ysrcMask == wxDefaultCoord,
1593 wxT("mask coordinates are not supported") );
1594
1595 wxCHECK_MSG( source, false, wxT("invalid source dc") );
1596
1597 // Blit into a bitmap.
1598 wxBitmap bitmap( width, height );
1599 wxMemoryDC memDC;
1600 memDC.SelectObject(bitmap);
1601 memDC.Blit(0, 0, width, height, source, xsrc, ysrc, rop);
1602 memDC.SelectObject(wxNullBitmap);
1603
1604 // Draw bitmap. scaling and positioning is done there.
1605 DrawBitmap( bitmap, xdest, ydest, useMask );
1606
1607 return true;
1608 }
1609
1610 void wxGtkPrintDC::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
1611 {
1612 DoDrawBitmap( icon, x, y, true );
1613 }
1614
1615 void wxGtkPrintDC::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool useMask )
1616 {
1617 wxCHECK_RET( bitmap.IsOk(), wxT("Invalid bitmap in wxGtkPrintDC::DoDrawBitmap"));
1618
1619 cairo_surface_t* surface;
1620 x = wxCoord(XLOG2DEV(x));
1621 y = wxCoord(YLOG2DEV(y));
1622 int bw = bitmap.GetWidth();
1623 int bh = bitmap.GetHeight();
1624 wxBitmap bmpSource = bitmap; // we need a non-const instance.
1625 unsigned char* buffer = new unsigned char[bw*bh*4];
1626 wxUint32* data = (wxUint32*)buffer;
1627
1628 wxMask *mask = NULL;
1629 if (useMask) mask = bmpSource.GetMask();
1630
1631 // Create a surface object and copy the bitmap pixel data to it. If the image has alpha (or a mask represented as alpha)
1632 // then we'll use a different format and iterator than if it doesn't.
1633 if (bmpSource.HasAlpha() || mask)
1634 {
1635 surface = gs_cairo->cairo_image_surface_create_for_data(
1636 buffer, CAIRO_FORMAT_ARGB32, bw, bh, bw*4);
1637 wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1638 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1639
1640 wxAlphaPixelData::Iterator p(pixData);
1641 int y, x;
1642 for (y=0; y<bh; y++)
1643 {
1644 wxAlphaPixelData::Iterator rowStart = p;
1645 for (x=0; x<bw; x++)
1646 {
1647 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1648 // with alpha in the upper 8 bits, then red, then green, then
1649 // blue. The 32-bit quantities are stored native-endian.
1650 // Pre-multiplied alpha is used.
1651 unsigned char alpha = p.Alpha();
1652
1653 if (!bmpSource.HasAlpha() && mask)
1654 alpha = 255;
1655
1656 if (alpha == 0)
1657 *data = 0;
1658 else
1659 *data = ( alpha << 24
1660 | (p.Red() * alpha/255) << 16
1661 | (p.Green() * alpha/255) << 8
1662 | (p.Blue() * alpha/255) );
1663 ++data;
1664 ++p;
1665 }
1666 p = rowStart;
1667 p.OffsetY(pixData, 1);
1668 }
1669 }
1670 else // no alpha
1671 {
1672 surface = gs_cairo->cairo_image_surface_create_for_data(
1673 buffer, CAIRO_FORMAT_RGB24, bw, bh, bw*4);
1674 wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1675 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1676
1677 wxNativePixelData::Iterator p(pixData);
1678 int y, x;
1679 for (y=0; y<bh; y++)
1680 {
1681 wxNativePixelData::Iterator rowStart = p;
1682 for (x=0; x<bw; x++)
1683 {
1684 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1685 // the upper 8 bits unused. Red, Green, and Blue are stored in
1686 // the remaining 24 bits in that order. The 32-bit quantities
1687 // are stored native-endian.
1688 *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() );
1689 ++data;
1690 ++p;
1691 }
1692 p = rowStart;
1693 p.OffsetY(pixData, 1);
1694 }
1695 }
1696
1697
1698 gs_cairo->cairo_save(m_cairo);
1699
1700 // Prepare to draw the image.
1701 gs_cairo->cairo_translate(m_cairo, x, y);
1702
1703 // Scale the image
1704 cairo_filter_t filter = CAIRO_FILTER_BILINEAR;
1705 cairo_pattern_t* pattern = cairo_pattern_create_for_surface(surface);
1706 cairo_pattern_set_filter(pattern,filter);
1707 wxDouble scaleX = (wxDouble) XLOG2DEVREL(bw) / (wxDouble) bw;
1708 wxDouble scaleY = (wxDouble) YLOG2DEVREL(bh) / (wxDouble) bh;
1709 cairo_scale(m_cairo, scaleX, scaleY);
1710
1711 gs_cairo->cairo_set_source(m_cairo, pattern);
1712 // Use the original size here since the context is scaled already.
1713 gs_cairo->cairo_rectangle(m_cairo, 0, 0, bw, bh);
1714 // Fill the rectangle using the pattern.
1715 gs_cairo->cairo_fill(m_cairo);
1716
1717 // Clean up.
1718 gs_cairo->cairo_pattern_destroy(pattern);
1719 gs_cairo->cairo_surface_destroy(surface);
1720 delete [] buffer;
1721
1722 CalcBoundingBox(0,0);
1723 CalcBoundingBox(bw,bh);
1724
1725 gs_cairo->cairo_restore(m_cairo);
1726 }
1727
1728 void wxGtkPrintDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y )
1729 {
1730 DoDrawRotatedText( text, x, y, 0.0 );
1731 }
1732
1733 void wxGtkPrintDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
1734 {
1735 double xx = XLOG2DEV(x);
1736 double yy = YLOG2DEV(y);
1737
1738 angle = -angle;
1739
1740 bool underlined = m_font.Ok() && m_font.GetUnderlined();
1741
1742 const wxUTF8Buf data = text.utf8_str();
1743
1744 size_t datalen = strlen(data);
1745 pango_layout_set_text( m_layout, data, datalen);
1746
1747 if (underlined)
1748 {
1749 PangoAttrList *attrs = pango_attr_list_new();
1750 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
1751 a->start_index = 0;
1752 a->end_index = datalen;
1753 pango_attr_list_insert(attrs, a);
1754 pango_layout_set_attributes(m_layout, attrs);
1755 pango_attr_list_unref(attrs);
1756 }
1757
1758 if (m_textForegroundColour.Ok())
1759 {
1760 unsigned char red = m_textForegroundColour.Red();
1761 unsigned char blue = m_textForegroundColour.Blue();
1762 unsigned char green = m_textForegroundColour.Green();
1763 unsigned char alpha = m_textForegroundColour.Alpha();
1764
1765 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1766 {
1767 double redPS = (double)(red) / 255.0;
1768 double bluePS = (double)(blue) / 255.0;
1769 double greenPS = (double)(green) / 255.0;
1770 double alphaPS = (double)(alpha) / 255.0;
1771
1772 gs_cairo->cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1773
1774 m_currentRed = red;
1775 m_currentBlue = blue;
1776 m_currentGreen = green;
1777 m_currentAlpha = alpha;
1778 }
1779 }
1780
1781 int w,h;
1782
1783 // Scale font description.
1784 gint oldSize = pango_font_description_get_size( m_fontdesc );
1785 double size = oldSize;
1786 size = size * m_scaleX;
1787 pango_font_description_set_size( m_fontdesc, (gint)size );
1788
1789 // Actually apply scaled font.
1790 pango_layout_set_font_description( m_layout, m_fontdesc );
1791
1792 pango_layout_get_pixel_size( m_layout, &w, &h );
1793
1794 if ( m_backgroundMode == wxSOLID )
1795 {
1796 unsigned char red = m_textBackgroundColour.Red();
1797 unsigned char blue = m_textBackgroundColour.Blue();
1798 unsigned char green = m_textBackgroundColour.Green();
1799 unsigned char alpha = m_textBackgroundColour.Alpha();
1800
1801 double redPS = (double)(red) / 255.0;
1802 double bluePS = (double)(blue) / 255.0;
1803 double greenPS = (double)(green) / 255.0;
1804 double alphaPS = (double)(alpha) / 255.0;
1805
1806 gs_cairo->cairo_save(m_cairo);
1807 gs_cairo->cairo_translate(m_cairo, xx, yy);
1808 gs_cairo->cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1809 gs_cairo->cairo_rotate(m_cairo,angle*DEG2RAD);
1810 gs_cairo->cairo_rectangle(m_cairo, 0, 0, w, h); // still in cairo units
1811 gs_cairo->cairo_fill(m_cairo);
1812 gs_cairo->cairo_restore(m_cairo);
1813 }
1814
1815 // Draw layout.
1816 gs_cairo->cairo_move_to (m_cairo, xx, yy);
1817
1818 gs_cairo->cairo_save( m_cairo );
1819
1820 if (fabs(angle) > 0.00001)
1821 gs_cairo->cairo_rotate( m_cairo, angle*DEG2RAD );
1822
1823 gs_cairo->pango_cairo_update_layout (m_cairo, m_layout);
1824 gs_cairo->pango_cairo_show_layout (m_cairo, m_layout);
1825
1826 gs_cairo->cairo_restore( m_cairo );
1827
1828 if (underlined)
1829 {
1830 // Undo underline attributes setting
1831 pango_layout_set_attributes(m_layout, NULL);
1832 }
1833
1834 // Reset unscaled size.
1835 pango_font_description_set_size( m_fontdesc, oldSize );
1836
1837 // Actually apply unscaled font.
1838 pango_layout_set_font_description( m_layout, m_fontdesc );
1839
1840 // Back to device units:
1841 CalcBoundingBox (x, y);
1842 CalcBoundingBox (x + w, y + h);
1843 }
1844
1845 void wxGtkPrintDC::Clear()
1846 {
1847 // Clear does nothing for printing, but keep the code
1848 // for later reuse
1849 /*
1850 gs_cairo->cairo_save(m_cairo);
1851 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
1852 SetBrush(m_backgroundBrush);
1853 gs_cairo->cairo_paint(m_cairo);
1854 gs_cairo->cairo_restore(m_cairo);
1855 */
1856 }
1857
1858 void wxGtkPrintDC::SetFont( const wxFont& font )
1859 {
1860 m_font = font;
1861
1862 if (m_font.Ok())
1863 {
1864 if (m_fontdesc)
1865 pango_font_description_free( m_fontdesc );
1866
1867 m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description ); // m_fontdesc is now set to device units
1868
1869 // Scale font description from device units to pango units
1870 gint oldSize = pango_font_description_get_size( m_fontdesc );
1871 double size = oldSize *m_DEV2PS; // scale to cairo units
1872 pango_font_description_set_size( m_fontdesc, (gint)size ); // apply to description
1873
1874 // Actually apply scaled font.
1875 pango_layout_set_font_description( m_layout, m_fontdesc );
1876 }
1877 }
1878
1879 void wxGtkPrintDC::SetPen( const wxPen& pen )
1880 {
1881 if (!pen.Ok()) return;
1882
1883 m_pen = pen;
1884
1885 double width;
1886
1887 if (m_pen.GetWidth() <= 0)
1888 width = 0.1;
1889 else
1890 width = (double) m_pen.GetWidth();
1891
1892 gs_cairo->cairo_set_line_width( m_cairo, width * m_DEV2PS * m_scaleX );
1893 static const double dotted[] = {2.0, 5.0};
1894 static const double short_dashed[] = {4.0, 4.0};
1895 static const double long_dashed[] = {4.0, 8.0};
1896 static const double dotted_dashed[] = {6.0, 6.0, 2.0, 6.0};
1897
1898 switch (m_pen.GetStyle())
1899 {
1900 case wxDOT: gs_cairo->cairo_set_dash( m_cairo, dotted, 2, 0 ); break;
1901 case wxSHORT_DASH: gs_cairo->cairo_set_dash( m_cairo, short_dashed, 2, 0 ); break;
1902 case wxLONG_DASH: gs_cairo->cairo_set_dash( m_cairo, long_dashed, 2, 0 ); break;
1903 case wxDOT_DASH: gs_cairo->cairo_set_dash( m_cairo, dotted_dashed, 4, 0 ); break;
1904 case wxUSER_DASH:
1905 {
1906 wxDash *wx_dashes;
1907 int num = m_pen.GetDashes (&wx_dashes);
1908 gdouble *g_dashes = g_new( gdouble, num );
1909 int i;
1910 for (i = 0; i < num; ++i)
1911 g_dashes[i] = (gdouble) wx_dashes[i];
1912 gs_cairo->cairo_set_dash( m_cairo, g_dashes, num, 0);
1913 g_free( g_dashes );
1914 }
1915 break;
1916 case wxSOLID:
1917 case wxTRANSPARENT:
1918 default: gs_cairo->cairo_set_dash( m_cairo, NULL, 0, 0 ); break;
1919 }
1920
1921 switch (m_pen.GetCap())
1922 {
1923 case wxCAP_PROJECTING: gs_cairo->cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_SQUARE); break;
1924 case wxCAP_BUTT: gs_cairo->cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_BUTT); break;
1925 case wxCAP_ROUND:
1926 default: gs_cairo->cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_ROUND); break;
1927 }
1928
1929 switch (m_pen.GetJoin())
1930 {
1931 case wxJOIN_BEVEL: gs_cairo->cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_BEVEL); break;
1932 case wxJOIN_MITER: gs_cairo->cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_MITER); break;
1933 case wxJOIN_ROUND:
1934 default: gs_cairo->cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_ROUND); break;
1935 }
1936
1937 unsigned char red = m_pen.GetColour().Red();
1938 unsigned char blue = m_pen.GetColour().Blue();
1939 unsigned char green = m_pen.GetColour().Green();
1940 unsigned char alpha = m_pen.GetColour().Alpha();
1941
1942 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1943 {
1944 double redPS = (double)(red) / 255.0;
1945 double bluePS = (double)(blue) / 255.0;
1946 double greenPS = (double)(green) / 255.0;
1947 double alphaPS = (double)(alpha) / 255.0;
1948
1949 gs_cairo->cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1950
1951 m_currentRed = red;
1952 m_currentBlue = blue;
1953 m_currentGreen = green;
1954 m_currentAlpha = alpha;
1955 }
1956 }
1957
1958 void wxGtkPrintDC::SetBrush( const wxBrush& brush )
1959 {
1960 if (!brush.Ok()) return;
1961
1962 m_brush = brush;
1963
1964 if (m_brush.GetStyle() == wxTRANSPARENT)
1965 {
1966 gs_cairo->cairo_set_source_rgba( m_cairo, 0, 0, 0, 0 );
1967 m_currentRed = 0;
1968 m_currentBlue = 0;
1969 m_currentGreen = 0;
1970 m_currentAlpha = 0;
1971 return;
1972 }
1973
1974 // Brush colour.
1975 unsigned char red = m_brush.GetColour().Red();
1976 unsigned char blue = m_brush.GetColour().Blue();
1977 unsigned char green = m_brush.GetColour().Green();
1978 unsigned char alpha = m_brush.GetColour().Alpha();
1979
1980 double redPS = (double)(red) / 255.0;
1981 double bluePS = (double)(blue) / 255.0;
1982 double greenPS = (double)(green) / 255.0;
1983 double alphaPS = (double)(alpha) / 255.0;
1984
1985 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
1986 {
1987 gs_cairo->cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
1988
1989 m_currentRed = red;
1990 m_currentBlue = blue;
1991 m_currentGreen = green;
1992 m_currentAlpha = alpha;
1993 }
1994
1995 if (m_brush.IsHatch())
1996 {
1997 cairo_t * cr;
1998 cairo_surface_t *surface;
1999 surface = gs_cairo->cairo_surface_create_similar(gs_cairo->cairo_get_target(m_cairo),CAIRO_CONTENT_COLOR_ALPHA,10,10);
2000 cr = gs_cairo->cairo_create(surface);
2001 gs_cairo->cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
2002 gs_cairo->cairo_set_line_width(cr, 1);
2003 gs_cairo->cairo_set_line_join(cr,CAIRO_LINE_JOIN_MITER);
2004
2005 switch (m_brush.GetStyle())
2006 {
2007 case wxCROSS_HATCH:
2008 gs_cairo->cairo_move_to(cr, 5, 0);
2009 gs_cairo->cairo_line_to(cr, 5, 10);
2010 gs_cairo->cairo_move_to(cr, 0, 5);
2011 gs_cairo->cairo_line_to(cr, 10, 5);
2012 break;
2013 case wxBDIAGONAL_HATCH:
2014 gs_cairo->cairo_move_to(cr, 0, 10);
2015 gs_cairo->cairo_line_to(cr, 10, 0);
2016 break;
2017 case wxFDIAGONAL_HATCH:
2018 gs_cairo->cairo_move_to(cr, 0, 0);
2019 gs_cairo->cairo_line_to(cr, 10, 10);
2020 break;
2021 case wxCROSSDIAG_HATCH:
2022 gs_cairo->cairo_move_to(cr, 0, 0);
2023 gs_cairo->cairo_line_to(cr, 10, 10);
2024 gs_cairo->cairo_move_to(cr, 10, 0);
2025 gs_cairo->cairo_line_to(cr, 0, 10);
2026 break;
2027 case wxHORIZONTAL_HATCH:
2028 gs_cairo->cairo_move_to(cr, 0, 5);
2029 gs_cairo->cairo_line_to(cr, 10, 5);
2030 break;
2031 case wxVERTICAL_HATCH:
2032 gs_cairo->cairo_move_to(cr, 5, 0);
2033 gs_cairo->cairo_line_to(cr, 5, 10);
2034 break;
2035 default:
2036 wxFAIL_MSG(_("Couldn't get hatch style from wxBrush."));
2037 }
2038
2039 gs_cairo->cairo_set_source_rgba(cr, redPS, greenPS, bluePS, alphaPS);
2040 gs_cairo->cairo_stroke (cr);
2041
2042 gs_cairo->cairo_destroy(cr);
2043 cairo_pattern_t * pattern = gs_cairo->cairo_pattern_create_for_surface (surface);
2044 gs_cairo->cairo_surface_destroy(surface);
2045 gs_cairo->cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
2046 gs_cairo->cairo_set_source(m_cairo, pattern);
2047 gs_cairo->cairo_pattern_destroy(pattern);
2048 }
2049 }
2050
2051 void wxGtkPrintDC::SetLogicalFunction( int function )
2052 {
2053 if (function == wxCLEAR)
2054 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_CLEAR);
2055 else if (function == wxOR)
2056 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_OUT);
2057 else if (function == wxNO_OP)
2058 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST);
2059 else if (function == wxAND)
2060 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_ADD);
2061 else if (function == wxSET)
2062 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_SATURATE);
2063 else if (function == wxXOR)
2064 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_XOR);
2065 else // wxCOPY or anything else.
2066 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
2067 }
2068
2069 void wxGtkPrintDC::SetBackground( const wxBrush& brush )
2070 {
2071 m_backgroundBrush = brush;
2072 gs_cairo->cairo_save(m_cairo);
2073 gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST_OVER);
2074
2075 SetBrush(m_backgroundBrush);
2076 gs_cairo->cairo_paint(m_cairo);
2077 gs_cairo->cairo_restore(m_cairo);
2078 }
2079
2080 void wxGtkPrintDC::SetBackgroundMode(int mode)
2081 {
2082 if (mode == wxSOLID)
2083 m_backgroundMode = wxSOLID;
2084 else
2085 m_backgroundMode = wxTRANSPARENT;
2086 }
2087
2088 void wxGtkPrintDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
2089 {
2090 gs_cairo->cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height));
2091 gs_cairo->cairo_clip(m_cairo);
2092 }
2093
2094 void wxGtkPrintDC::DestroyClippingRegion()
2095 {
2096 gs_cairo->cairo_reset_clip(m_cairo);
2097 }
2098
2099 bool wxGtkPrintDC::StartDoc(const wxString& WXUNUSED(message))
2100 {
2101 return true;
2102 }
2103
2104 void wxGtkPrintDC::EndDoc()
2105 {
2106 return;
2107 }
2108
2109 void wxGtkPrintDC::StartPage()
2110 {
2111 return;
2112 }
2113
2114 void wxGtkPrintDC::EndPage()
2115 {
2116 return;
2117 }
2118
2119 wxCoord wxGtkPrintDC::GetCharHeight() const
2120 {
2121 pango_layout_set_text( m_layout, "H", 1 );
2122
2123 int w,h;
2124 pango_layout_get_pixel_size( m_layout, &w, &h );
2125
2126 return wxRound( h * m_PS2DEV );
2127 }
2128
2129 wxCoord wxGtkPrintDC::GetCharWidth() const
2130 {
2131 pango_layout_set_text( m_layout, "H", 1 );
2132
2133 int w,h;
2134 pango_layout_get_pixel_size( m_layout, &w, &h );
2135
2136 return wxRound( w * m_PS2DEV );
2137 }
2138
2139 void wxGtkPrintDC::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoord *height,
2140 wxCoord *descent,
2141 wxCoord *externalLeading,
2142 const wxFont *theFont ) const
2143 {
2144 if ( width )
2145 *width = 0;
2146 if ( height )
2147 *height = 0;
2148 if ( descent )
2149 *descent = 0;
2150 if ( externalLeading )
2151 *externalLeading = 0;
2152
2153 if (string.empty())
2154 {
2155 return;
2156 }
2157
2158 // Set layout's text
2159 const wxUTF8Buf dataUTF8 = string.utf8_str();
2160
2161 PangoFontDescription *desc = m_fontdesc;
2162 if (theFont) desc = theFont->GetNativeFontInfo()->description;
2163
2164 gint oldSize = pango_font_description_get_size( desc );
2165 double size = oldSize;
2166 size = size * m_scaleY;
2167 pango_font_description_set_size( desc, (gint)size );
2168
2169 // apply scaled font
2170 pango_layout_set_font_description( m_layout, desc );
2171
2172 pango_layout_set_text( m_layout, dataUTF8, strlen(dataUTF8) );
2173
2174 int w, h;
2175 pango_layout_get_pixel_size( m_layout, &w, &h );
2176
2177 if (width)
2178 *width = wxRound( (double)w / m_scaleX * m_PS2DEV );
2179 if (height)
2180 *height = wxRound( (double)h / m_scaleY * m_PS2DEV );
2181
2182 if (descent)
2183 {
2184 PangoLayoutIter *iter = pango_layout_get_iter(m_layout);
2185 int baseline = pango_layout_iter_get_baseline(iter);
2186 pango_layout_iter_free(iter);
2187 *descent = wxRound( (h - PANGO_PIXELS(baseline)) * m_PS2DEV );
2188 }
2189
2190 // Reset unscaled size.
2191 pango_font_description_set_size( desc, oldSize );
2192
2193 // Reset unscaled font.
2194 pango_layout_set_font_description( m_layout, m_fontdesc );
2195 }
2196
2197 void wxGtkPrintDC::DoGetSize(int* width, int* height) const
2198 {
2199 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
2200
2201 if (width)
2202 *width = wxRound( gtk_page_setup_get_paper_width( setup, GTK_UNIT_POINTS ) * m_PS2DEV );
2203 if (height)
2204 *height = wxRound( gtk_page_setup_get_paper_height( setup, GTK_UNIT_POINTS ) * m_PS2DEV );
2205 }
2206
2207 void wxGtkPrintDC::DoGetSizeMM(int *width, int *height) const
2208 {
2209 GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
2210
2211 if (width)
2212 *width = wxRound( gtk_page_setup_get_paper_width( setup, GTK_UNIT_MM ) );
2213 if (height)
2214 *height = wxRound( gtk_page_setup_get_paper_height( setup, GTK_UNIT_MM ) );
2215 }
2216
2217 wxSize wxGtkPrintDC::GetPPI() const
2218 {
2219 return wxSize( (int)m_resolution, (int)m_resolution );
2220 }
2221
2222 void wxGtkPrintDC::SetPrintData(const wxPrintData& data)
2223 {
2224 m_printData = data;
2225 }
2226
2227 void wxGtkPrintDC::SetResolution(int WXUNUSED(ppi))
2228 {
2229 // We can't change ppi of the GtkPrintContext.
2230 // TODO: should we really support this?
2231 }
2232
2233 int wxGtkPrintDC::GetResolution()
2234 {
2235 return m_resolution;
2236 }
2237
2238 // ----------------------------------------------------------------------------
2239 // Print preview
2240 // ----------------------------------------------------------------------------
2241
2242 IMPLEMENT_CLASS(wxGtkPrintPreview, wxPrintPreviewBase)
2243
2244 void wxGtkPrintPreview::Init(wxPrintout * WXUNUSED(printout),
2245 wxPrintout * WXUNUSED(printoutForPrinting))
2246 {
2247 DetermineScaling();
2248 }
2249
2250 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout,
2251 wxPrintout *printoutForPrinting,
2252 wxPrintDialogData *data)
2253 : wxPrintPreviewBase(printout, printoutForPrinting, data)
2254 {
2255 Init(printout, printoutForPrinting);
2256 }
2257
2258 wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout,
2259 wxPrintout *printoutForPrinting,
2260 wxPrintData *data)
2261 : wxPrintPreviewBase(printout, printoutForPrinting, data)
2262 {
2263 Init(printout, printoutForPrinting);
2264 }
2265
2266 wxGtkPrintPreview::~wxGtkPrintPreview()
2267 {
2268 }
2269
2270 bool wxGtkPrintPreview::Print(bool interactive)
2271 {
2272 if (!m_printPrintout)
2273 return false;
2274
2275 wxPrinter printer(& m_printDialogData);
2276 return printer.Print(m_previewFrame, m_printPrintout, interactive);
2277 }
2278
2279 void wxGtkPrintPreview::DetermineScaling()
2280 {
2281 wxPaperSize paperType = m_printDialogData.GetPrintData().GetPaperId();
2282
2283 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
2284 if (!paper)
2285 paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
2286
2287 if (paper)
2288 {
2289 wxSize ScreenPixels = wxGetDisplaySize();
2290 wxSize ScreenMM = wxGetDisplaySizeMM();
2291
2292 m_previewPrintout->SetPPIScreen( (int) ((ScreenPixels.GetWidth() * 25.4) / ScreenMM.GetWidth()),
2293 (int) ((ScreenPixels.GetHeight() * 25.4) / ScreenMM.GetHeight()) );
2294
2295 // TODO !!!!!!!!!!!!!!!
2296 int resolution = 600;
2297 m_previewPrintout->SetPPIPrinter( resolution, resolution );
2298
2299 // Get width and height in points (1/72th of an inch)
2300 wxSize sizeDevUnits(paper->GetSizeDeviceUnits());
2301
2302 sizeDevUnits.x = wxRound((double)sizeDevUnits.x * (double)resolution / 72.0);
2303 sizeDevUnits.y = wxRound((double)sizeDevUnits.y * (double)resolution / 72.0);
2304 wxSize sizeTenthsMM(paper->GetSize());
2305 wxSize sizeMM(sizeTenthsMM.x / 10, sizeTenthsMM.y / 10);
2306
2307 // If in landscape mode, we need to swap the width and height.
2308 if ( m_printDialogData.GetPrintData().GetOrientation() == wxLANDSCAPE )
2309 {
2310 m_pageWidth = sizeDevUnits.y;
2311 m_pageHeight = sizeDevUnits.x;
2312 m_previewPrintout->SetPageSizeMM(sizeMM.y, sizeMM.x);
2313 }
2314 else
2315 {
2316 m_pageWidth = sizeDevUnits.x;
2317 m_pageHeight = sizeDevUnits.y;
2318 m_previewPrintout->SetPageSizeMM(sizeMM.x, sizeMM.y);
2319 }
2320 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
2321 m_previewPrintout->SetPaperRectPixels(wxRect(0, 0, m_pageWidth, m_pageHeight));
2322
2323 // At 100%, the page should look about page-size on the screen.
2324 m_previewScaleX = 0.8 * 72.0 / (double)resolution;
2325 m_previewScaleY = m_previewScaleX;
2326 }
2327 }
2328
2329 #endif
2330 // wxUSE_GTKPRINT