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