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