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