]>
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 | |
6 | // RCS-ID: $Id: print.cpp,v 1 2007-08-25 05:44:44 PC Exp $ | |
7 | // Copyright: (c) 2007 wxWidgets development team | |
8 | // Licence: wxWindows Licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // For compilers that support precompilation, includes "wx/wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #if wxUSE_GTKPRINT | |
19 | ||
20 | #include "wx/gtk/print.h" | |
21 | ||
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/log.h" | |
24 | #include "wx/dcmemory.h" | |
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 | { |
04ee05f9 | 1269 | if (m_pen.GetStyle() == wxPENSTYLE_TRANSPARENT) return; |
fa034c45 RR |
1270 | |
1271 | SetPen( m_pen ); | |
f7b8e3d6 VS |
1272 | cairo_move_to ( m_cairo, XLOG2DEV(x1), YLOG2DEV(y1) ); |
1273 | cairo_line_to ( m_cairo, XLOG2DEV(x2), YLOG2DEV(y2) ); | |
1274 | cairo_stroke ( m_cairo ); | |
fa034c45 RR |
1275 | |
1276 | CalcBoundingBox( x1, y1 ); | |
1277 | CalcBoundingBox( x2, y2 ); | |
1278 | } | |
1279 | ||
888dde65 | 1280 | void wxGtkPrinterDCImpl::DoCrossHair(wxCoord x, wxCoord y) |
fa034c45 | 1281 | { |
a9312950 RR |
1282 | int w, h; |
1283 | DoGetSize(&w, &h); | |
fa034c45 RR |
1284 | |
1285 | SetPen(m_pen); | |
1286 | ||
f7b8e3d6 VS |
1287 | cairo_move_to (m_cairo, XLOG2DEV(x), 0); |
1288 | cairo_line_to (m_cairo, XLOG2DEV(x), YLOG2DEVREL(h)); | |
1289 | cairo_move_to (m_cairo, 0, YLOG2DEV(y)); | |
1290 | cairo_line_to (m_cairo, XLOG2DEVREL(w), YLOG2DEV(y)); | |
fa034c45 | 1291 | |
f7b8e3d6 | 1292 | cairo_stroke (m_cairo); |
fa034c45 | 1293 | CalcBoundingBox( 0, 0 ); |
a9312950 | 1294 | CalcBoundingBox( w, h ); |
fa034c45 RR |
1295 | } |
1296 | ||
888dde65 | 1297 | void wxGtkPrinterDCImpl::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord xc,wxCoord yc) |
fa034c45 RR |
1298 | { |
1299 | double dx = x1 - xc; | |
1300 | double dy = y1 - yc; | |
1301 | double radius = sqrt((double)(dx*dx+dy*dy)); | |
1302 | ||
1303 | double alpha1, alpha2; | |
1304 | if (x1 == x2 && y1 == y2) | |
1305 | { | |
1306 | alpha1 = 0.0; | |
1307 | alpha2 = 360.0; | |
1308 | } | |
1309 | else | |
1310 | if (radius == 0.0) | |
1311 | { | |
1312 | alpha1 = alpha2 = 0.0; | |
1313 | } | |
1314 | else | |
1315 | { | |
1316 | alpha1 = (x1 - xc == 0) ? | |
1317 | (y1 - yc < 0) ? 90.0 : -90.0 : | |
1318 | atan2(double(y1-yc), double(x1-xc)) * RAD2DEG; | |
1319 | alpha2 = (x2 - xc == 0) ? | |
1320 | (y2 - yc < 0) ? 90.0 : -90.0 : | |
1321 | atan2(double(y2-yc), double(x2-xc)) * RAD2DEG; | |
1322 | ||
1323 | while (alpha1 <= 0) alpha1 += 360; | |
1324 | while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between. | |
1325 | while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree. | |
1326 | while (alpha2 > 360) alpha2 -= 360; | |
1327 | } | |
1328 | ||
1329 | alpha1 *= DEG2RAD; | |
1330 | alpha2 *= DEG2RAD; | |
1331 | ||
f7b8e3d6 | 1332 | cairo_new_path(m_cairo); |
da249bc3 | 1333 | |
f7b8e3d6 VS |
1334 | cairo_arc_negative ( m_cairo, XLOG2DEV(xc), YLOG2DEV(yc), XLOG2DEVREL((int)radius), alpha1, alpha2); |
1335 | cairo_line_to(m_cairo, XLOG2DEV(xc), YLOG2DEV(yc)); | |
1336 | cairo_close_path (m_cairo); | |
fa034c45 RR |
1337 | |
1338 | SetBrush( m_brush ); | |
f7b8e3d6 | 1339 | cairo_fill_preserve( m_cairo ); |
fa034c45 RR |
1340 | |
1341 | SetPen (m_pen); | |
f7b8e3d6 | 1342 | cairo_stroke( m_cairo ); |
fa034c45 RR |
1343 | |
1344 | CalcBoundingBox (x1, y1); | |
1345 | CalcBoundingBox (xc, yc); | |
1346 | CalcBoundingBox (x2, y2); | |
1347 | } | |
1348 | ||
888dde65 | 1349 | void wxGtkPrinterDCImpl::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea) |
fa034c45 | 1350 | { |
f7b8e3d6 | 1351 | cairo_save( m_cairo ); |
fa034c45 | 1352 | |
f7b8e3d6 | 1353 | cairo_new_path(m_cairo); |
da249bc3 | 1354 | |
f7b8e3d6 | 1355 | cairo_translate( m_cairo, XLOG2DEV((wxCoord) (x + w / 2.)), XLOG2DEV((wxCoord) (y + h / 2.)) ); |
a9312950 | 1356 | double scale = (double)YLOG2DEVREL(h) / (double) XLOG2DEVREL(w); |
f7b8e3d6 | 1357 | cairo_scale( m_cairo, 1.0, scale ); |
fa034c45 | 1358 | |
f7b8e3d6 | 1359 | cairo_arc_negative ( m_cairo, 0, 0, XLOG2DEVREL(w/2), -sa*DEG2RAD, -ea*DEG2RAD); |
fa034c45 RR |
1360 | |
1361 | SetPen (m_pen); | |
f7b8e3d6 | 1362 | cairo_stroke_preserve( m_cairo ); |
fa034c45 | 1363 | |
f7b8e3d6 | 1364 | cairo_line_to(m_cairo, 0,0); |
fa034c45 RR |
1365 | |
1366 | SetBrush( m_brush ); | |
f7b8e3d6 | 1367 | cairo_fill( m_cairo ); |
fa034c45 | 1368 | |
f7b8e3d6 | 1369 | cairo_restore( m_cairo ); |
fa034c45 RR |
1370 | |
1371 | CalcBoundingBox( x, y); | |
1372 | CalcBoundingBox( x+w, y+h ); | |
1373 | } | |
1374 | ||
888dde65 | 1375 | void wxGtkPrinterDCImpl::DoDrawPoint(wxCoord x, wxCoord y) |
fa034c45 | 1376 | { |
04ee05f9 | 1377 | if (m_pen.GetStyle() == wxPENSTYLE_TRANSPARENT) return; |
fa034c45 RR |
1378 | |
1379 | SetPen( m_pen ); | |
1380 | ||
f7b8e3d6 VS |
1381 | cairo_move_to ( m_cairo, XLOG2DEV(x), YLOG2DEV(y) ); |
1382 | cairo_line_to ( m_cairo, XLOG2DEV(x), YLOG2DEV(y) ); | |
1383 | cairo_stroke ( m_cairo ); | |
fa034c45 RR |
1384 | |
1385 | CalcBoundingBox( x, y ); | |
1386 | } | |
1387 | ||
888dde65 | 1388 | void wxGtkPrinterDCImpl::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset) |
fa034c45 | 1389 | { |
04ee05f9 | 1390 | if (m_pen.GetStyle() == wxPENSTYLE_TRANSPARENT) return; |
fa034c45 RR |
1391 | |
1392 | if (n <= 0) return; | |
1393 | ||
1394 | SetPen (m_pen); | |
1395 | ||
1396 | int i; | |
1397 | for ( i =0; i<n ; i++ ) | |
1398 | CalcBoundingBox( points[i].x+xoffset, points[i].y+yoffset); | |
1399 | ||
f7b8e3d6 | 1400 | cairo_move_to ( m_cairo, XLOG2DEV(points[0].x+xoffset), YLOG2DEV(points[0].y+yoffset) ); |
fa034c45 RR |
1401 | |
1402 | for (i = 1; i < n; i++) | |
f7b8e3d6 | 1403 | cairo_line_to ( m_cairo, XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset) ); |
fa034c45 | 1404 | |
f7b8e3d6 | 1405 | cairo_stroke ( m_cairo); |
fa034c45 RR |
1406 | } |
1407 | ||
03647350 VZ |
1408 | void wxGtkPrinterDCImpl::DoDrawPolygon(int n, wxPoint points[], |
1409 | wxCoord xoffset, wxCoord yoffset, | |
89efaf2b | 1410 | wxPolygonFillMode fillStyle) |
fa034c45 RR |
1411 | { |
1412 | if (n==0) return; | |
1413 | ||
f7b8e3d6 | 1414 | cairo_save(m_cairo); |
fa034c45 | 1415 | if (fillStyle == wxWINDING_RULE) |
f7b8e3d6 | 1416 | cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_WINDING); |
fa034c45 | 1417 | else |
f7b8e3d6 | 1418 | cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_EVEN_ODD); |
fa034c45 RR |
1419 | |
1420 | int x = points[0].x + xoffset; | |
1421 | int y = points[0].y + yoffset; | |
f7b8e3d6 VS |
1422 | cairo_new_path(m_cairo); |
1423 | cairo_move_to( m_cairo, XLOG2DEV(x), YLOG2DEV(y) ); | |
fa034c45 RR |
1424 | int i; |
1425 | for (i = 1; i < n; i++) | |
1426 | { | |
1427 | int x = points[i].x + xoffset; | |
1428 | int y = points[i].y + yoffset; | |
f7b8e3d6 | 1429 | cairo_line_to( m_cairo, XLOG2DEV(x), YLOG2DEV(y) ); |
fa034c45 | 1430 | } |
f7b8e3d6 | 1431 | cairo_close_path(m_cairo); |
fa034c45 RR |
1432 | |
1433 | SetBrush( m_brush ); | |
f7b8e3d6 | 1434 | cairo_fill_preserve( m_cairo ); |
fa034c45 RR |
1435 | |
1436 | SetPen (m_pen); | |
f7b8e3d6 | 1437 | cairo_stroke( m_cairo ); |
fa034c45 RR |
1438 | |
1439 | CalcBoundingBox( x, y ); | |
1440 | ||
f7b8e3d6 | 1441 | cairo_restore(m_cairo); |
fa034c45 RR |
1442 | } |
1443 | ||
89efaf2b | 1444 | void wxGtkPrinterDCImpl::DoDrawPolyPolygon(int n, int count[], wxPoint points[], |
03647350 | 1445 | wxCoord xoffset, wxCoord yoffset, |
89efaf2b | 1446 | wxPolygonFillMode fillStyle) |
fa034c45 | 1447 | { |
888dde65 | 1448 | wxDCImpl::DoDrawPolyPolygon( n, count, points, xoffset, yoffset, fillStyle ); |
fa034c45 RR |
1449 | } |
1450 | ||
888dde65 | 1451 | void wxGtkPrinterDCImpl::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height) |
fa034c45 | 1452 | { |
728ddc45 RR |
1453 | width--; |
1454 | height--; | |
1455 | ||
f7b8e3d6 VS |
1456 | cairo_new_path(m_cairo); |
1457 | cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height)); | |
fa034c45 RR |
1458 | |
1459 | SetBrush( m_brush ); | |
f7b8e3d6 | 1460 | cairo_fill_preserve( m_cairo ); |
fa034c45 RR |
1461 | |
1462 | SetPen (m_pen); | |
f7b8e3d6 | 1463 | cairo_stroke( m_cairo ); |
fa034c45 RR |
1464 | |
1465 | CalcBoundingBox( x, y ); | |
1466 | CalcBoundingBox( x + width, y + height ); | |
1467 | } | |
1468 | ||
888dde65 | 1469 | void wxGtkPrinterDCImpl::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius) |
fa034c45 | 1470 | { |
728ddc45 RR |
1471 | width--; |
1472 | height--; | |
1473 | ||
fa034c45 RR |
1474 | if (radius < 0.0) radius = - radius * ((width < height) ? width : height); |
1475 | ||
1476 | wxCoord dd = 2 * (wxCoord) radius; | |
1477 | if (dd > width) dd = width; | |
1478 | if (dd > height) dd = height; | |
1479 | radius = dd / 2; | |
1480 | ||
1481 | wxCoord rad = (wxCoord) radius; | |
1482 | ||
f7b8e3d6 VS |
1483 | cairo_new_path(m_cairo); |
1484 | cairo_move_to(m_cairo,XLOG2DEV(x + rad),YLOG2DEV(y)); | |
1485 | cairo_curve_to(m_cairo, | |
5cdcb787 RR |
1486 | XLOG2DEV(x + rad),YLOG2DEV(y), |
1487 | XLOG2DEV(x),YLOG2DEV(y), | |
1488 | XLOG2DEV(x),YLOG2DEV(y + rad)); | |
f7b8e3d6 VS |
1489 | cairo_line_to(m_cairo,XLOG2DEV(x),YLOG2DEV(y + height - rad)); |
1490 | cairo_curve_to(m_cairo, | |
5cdcb787 RR |
1491 | XLOG2DEV(x),YLOG2DEV(y + height - rad), |
1492 | XLOG2DEV(x),YLOG2DEV(y + height), | |
1493 | XLOG2DEV(x + rad),YLOG2DEV(y + height)); | |
f7b8e3d6 VS |
1494 | cairo_line_to(m_cairo,XLOG2DEV(x + width - rad),YLOG2DEV(y + height)); |
1495 | cairo_curve_to(m_cairo, | |
5cdcb787 RR |
1496 | XLOG2DEV(x + width - rad),YLOG2DEV(y + height), |
1497 | XLOG2DEV(x + width),YLOG2DEV(y + height), | |
1498 | XLOG2DEV(x + width),YLOG2DEV(y + height - rad)); | |
f7b8e3d6 VS |
1499 | cairo_line_to(m_cairo,XLOG2DEV(x + width),YLOG2DEV(y + rad)); |
1500 | cairo_curve_to(m_cairo, | |
5cdcb787 RR |
1501 | XLOG2DEV(x + width),YLOG2DEV(y + rad), |
1502 | XLOG2DEV(x + width),YLOG2DEV(y), | |
1503 | XLOG2DEV(x + width - rad),YLOG2DEV(y)); | |
f7b8e3d6 VS |
1504 | cairo_line_to(m_cairo,XLOG2DEV(x + rad),YLOG2DEV(y)); |
1505 | cairo_close_path(m_cairo); | |
fa034c45 RR |
1506 | |
1507 | SetBrush(m_brush); | |
f7b8e3d6 | 1508 | cairo_fill_preserve(m_cairo); |
fa034c45 RR |
1509 | |
1510 | SetPen(m_pen); | |
f7b8e3d6 | 1511 | cairo_stroke(m_cairo); |
fa034c45 RR |
1512 | |
1513 | CalcBoundingBox(x,y); | |
1514 | CalcBoundingBox(x+width,y+height); | |
1515 | } | |
1516 | ||
888dde65 | 1517 | void wxGtkPrinterDCImpl::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height) |
fa034c45 | 1518 | { |
728ddc45 RR |
1519 | width--; |
1520 | height--; | |
1521 | ||
f7b8e3d6 | 1522 | cairo_save (m_cairo); |
fa034c45 | 1523 | |
f7b8e3d6 | 1524 | cairo_new_path(m_cairo); |
da249bc3 | 1525 | |
f7b8e3d6 VS |
1526 | cairo_translate (m_cairo, XLOG2DEV((wxCoord) (x + width / 2.)), YLOG2DEV((wxCoord) (y + height / 2.))); |
1527 | cairo_scale(m_cairo, 1, (double)YLOG2DEVREL(height)/(double)XLOG2DEVREL(width)); | |
1528 | cairo_arc ( m_cairo, 0, 0, XLOG2DEVREL(width/2), 0, 2 * M_PI); | |
fa034c45 RR |
1529 | |
1530 | SetBrush( m_brush ); | |
f7b8e3d6 | 1531 | cairo_fill_preserve( m_cairo ); |
fa034c45 RR |
1532 | |
1533 | SetPen (m_pen); | |
f7b8e3d6 | 1534 | cairo_stroke( m_cairo ); |
fa034c45 RR |
1535 | |
1536 | CalcBoundingBox( x, y ); | |
1537 | CalcBoundingBox( x + width, y + height ); | |
1538 | ||
f7b8e3d6 | 1539 | cairo_restore (m_cairo); |
fa034c45 RR |
1540 | } |
1541 | ||
1542 | #if wxUSE_SPLINES | |
888dde65 | 1543 | void wxGtkPrinterDCImpl::DoDrawSpline(const wxPointList *points) |
fa034c45 RR |
1544 | { |
1545 | SetPen (m_pen); | |
1546 | ||
1547 | double c, d, x1, y1, x2, y2, x3, y3; | |
1548 | wxPoint *p, *q; | |
1549 | ||
b0d7707b RR |
1550 | wxPointList::compatibility_iterator node = points->GetFirst(); |
1551 | p = node->GetData(); | |
fa034c45 RR |
1552 | x1 = p->x; |
1553 | y1 = p->y; | |
1554 | ||
1555 | node = node->GetNext(); | |
b0d7707b | 1556 | p = node->GetData(); |
fa034c45 RR |
1557 | c = p->x; |
1558 | d = p->y; | |
1559 | x3 = | |
1560 | (double)(x1 + c) / 2; | |
1561 | y3 = | |
1562 | (double)(y1 + d) / 2; | |
1563 | ||
f7b8e3d6 VS |
1564 | cairo_new_path( m_cairo ); |
1565 | cairo_move_to( m_cairo, XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1) ); | |
1566 | cairo_line_to( m_cairo, XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) ); | |
fa034c45 RR |
1567 | |
1568 | CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 ); | |
1569 | CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 ); | |
1570 | ||
1571 | node = node->GetNext(); | |
1572 | while (node) | |
1573 | { | |
b0d7707b | 1574 | q = node->GetData(); |
fa034c45 RR |
1575 | |
1576 | x1 = x3; | |
1577 | y1 = y3; | |
1578 | x2 = c; | |
1579 | y2 = d; | |
1580 | c = q->x; | |
1581 | d = q->y; | |
1582 | x3 = (double)(x2 + c) / 2; | |
1583 | y3 = (double)(y2 + d) / 2; | |
1584 | ||
f7b8e3d6 | 1585 | cairo_curve_to(m_cairo, |
5cdcb787 RR |
1586 | XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1), |
1587 | XLOG2DEV((wxCoord)x2), YLOG2DEV((wxCoord)y2), | |
1588 | XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) ); | |
fa034c45 RR |
1589 | |
1590 | CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 ); | |
1591 | CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 ); | |
1592 | ||
1593 | node = node->GetNext(); | |
1594 | } | |
1595 | ||
f7b8e3d6 | 1596 | cairo_line_to ( m_cairo, XLOG2DEV((wxCoord)c), YLOG2DEV((wxCoord)d) ); |
fa034c45 | 1597 | |
f7b8e3d6 | 1598 | cairo_stroke( m_cairo ); |
fa034c45 RR |
1599 | } |
1600 | #endif // wxUSE_SPLINES | |
1601 | ||
888dde65 | 1602 | bool wxGtkPrinterDCImpl::DoBlit(wxCoord xdest, wxCoord ydest, |
e0d1fd7f VZ |
1603 | wxCoord width, wxCoord height, |
1604 | wxDC *source, wxCoord xsrc, wxCoord ysrc, | |
89efaf2b | 1605 | wxRasterOperationMode rop, bool useMask, |
e0d1fd7f VZ |
1606 | wxCoord WXUNUSED_UNLESS_DEBUG(xsrcMask), |
1607 | wxCoord WXUNUSED_UNLESS_DEBUG(ysrcMask)) | |
fa034c45 | 1608 | { |
e0d1fd7f VZ |
1609 | wxASSERT_MSG( xsrcMask == wxDefaultCoord && ysrcMask == wxDefaultCoord, |
1610 | wxT("mask coordinates are not supported") ); | |
1611 | ||
fa034c45 RR |
1612 | wxCHECK_MSG( source, false, wxT("invalid source dc") ); |
1613 | ||
1614 | // Blit into a bitmap. | |
1615 | wxBitmap bitmap( width, height ); | |
1616 | wxMemoryDC memDC; | |
1617 | memDC.SelectObject(bitmap); | |
1618 | memDC.Blit(0, 0, width, height, source, xsrc, ysrc, rop); | |
1619 | memDC.SelectObject(wxNullBitmap); | |
1620 | ||
1621 | // Draw bitmap. scaling and positioning is done there. | |
4f37154e | 1622 | GetOwner()->DrawBitmap( bitmap, xdest, ydest, useMask ); |
fa034c45 RR |
1623 | |
1624 | return true; | |
1625 | } | |
1626 | ||
888dde65 | 1627 | void wxGtkPrinterDCImpl::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y ) |
fa034c45 RR |
1628 | { |
1629 | DoDrawBitmap( icon, x, y, true ); | |
1630 | } | |
1631 | ||
888dde65 | 1632 | void wxGtkPrinterDCImpl::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool useMask ) |
fa034c45 | 1633 | { |
888dde65 | 1634 | wxCHECK_RET( bitmap.IsOk(), wxT("Invalid bitmap in wxGtkPrinterDCImpl::DoDrawBitmap")); |
fa034c45 | 1635 | |
5cdcb787 RR |
1636 | x = wxCoord(XLOG2DEV(x)); |
1637 | y = wxCoord(YLOG2DEV(y)); | |
fa034c45 RR |
1638 | int bw = bitmap.GetWidth(); |
1639 | int bh = bitmap.GetHeight(); | |
1640 | wxBitmap bmpSource = bitmap; // we need a non-const instance. | |
fb14d960 PC |
1641 | if (!useMask && !bitmap.HasPixbuf() && bitmap.GetMask()) |
1642 | bmpSource.SetMask(NULL); | |
fa034c45 | 1643 | |
f7b8e3d6 | 1644 | cairo_save(m_cairo); |
fa034c45 RR |
1645 | |
1646 | // Prepare to draw the image. | |
f7b8e3d6 | 1647 | cairo_translate(m_cairo, x, y); |
4d1d071d RR |
1648 | |
1649 | // Scale the image | |
5cdcb787 RR |
1650 | wxDouble scaleX = (wxDouble) XLOG2DEVREL(bw) / (wxDouble) bw; |
1651 | wxDouble scaleY = (wxDouble) YLOG2DEVREL(bh) / (wxDouble) bh; | |
f7b8e3d6 | 1652 | cairo_scale(m_cairo, scaleX, scaleY); |
4d1d071d | 1653 | |
fb14d960 PC |
1654 | gdk_cairo_set_source_pixbuf(m_cairo, bmpSource.GetPixbuf(), 0, 0); |
1655 | cairo_pattern_set_filter(cairo_get_source(m_cairo), CAIRO_FILTER_NEAREST); | |
fa034c45 | 1656 | // Use the original size here since the context is scaled already. |
f7b8e3d6 | 1657 | cairo_rectangle(m_cairo, 0, 0, bw, bh); |
fa034c45 | 1658 | // Fill the rectangle using the pattern. |
f7b8e3d6 | 1659 | cairo_fill(m_cairo); |
fa034c45 | 1660 | |
fa034c45 RR |
1661 | CalcBoundingBox(0,0); |
1662 | CalcBoundingBox(bw,bh); | |
1663 | ||
f7b8e3d6 | 1664 | cairo_restore(m_cairo); |
fa034c45 RR |
1665 | } |
1666 | ||
888dde65 | 1667 | void wxGtkPrinterDCImpl::DoDrawText(const wxString& text, wxCoord x, wxCoord y ) |
fa034c45 RR |
1668 | { |
1669 | DoDrawRotatedText( text, x, y, 0.0 ); | |
1670 | } | |
1671 | ||
888dde65 | 1672 | void wxGtkPrinterDCImpl::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle) |
fa034c45 | 1673 | { |
a9312950 RR |
1674 | double xx = XLOG2DEV(x); |
1675 | double yy = YLOG2DEV(y); | |
fa034c45 RR |
1676 | |
1677 | angle = -angle; | |
1678 | ||
1679 | bool underlined = m_font.Ok() && m_font.GetUnderlined(); | |
1680 | ||
197380a0 | 1681 | const wxScopedCharBuffer data = text.utf8_str(); |
fa034c45 RR |
1682 | |
1683 | size_t datalen = strlen(data); | |
1684 | pango_layout_set_text( m_layout, data, datalen); | |
1685 | ||
1686 | if (underlined) | |
1687 | { | |
1688 | PangoAttrList *attrs = pango_attr_list_new(); | |
1689 | PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE); | |
1690 | a->start_index = 0; | |
1691 | a->end_index = datalen; | |
1692 | pango_attr_list_insert(attrs, a); | |
1693 | pango_layout_set_attributes(m_layout, attrs); | |
1694 | pango_attr_list_unref(attrs); | |
1695 | } | |
1696 | ||
1697 | if (m_textForegroundColour.Ok()) | |
1698 | { | |
1699 | unsigned char red = m_textForegroundColour.Red(); | |
1700 | unsigned char blue = m_textForegroundColour.Blue(); | |
1701 | unsigned char green = m_textForegroundColour.Green(); | |
1702 | unsigned char alpha = m_textForegroundColour.Alpha(); | |
1703 | ||
1704 | if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha)) | |
1705 | { | |
1706 | double redPS = (double)(red) / 255.0; | |
1707 | double bluePS = (double)(blue) / 255.0; | |
1708 | double greenPS = (double)(green) / 255.0; | |
1709 | double alphaPS = (double)(alpha) / 255.0; | |
1710 | ||
f7b8e3d6 | 1711 | cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS ); |
fa034c45 RR |
1712 | |
1713 | m_currentRed = red; | |
1714 | m_currentBlue = blue; | |
1715 | m_currentGreen = green; | |
1716 | m_currentAlpha = alpha; | |
1717 | } | |
1718 | } | |
1719 | ||
a9312950 | 1720 | // Draw layout. |
f7b8e3d6 | 1721 | cairo_move_to (m_cairo, xx, yy); |
a7060b8c | 1722 | |
f7b8e3d6 | 1723 | cairo_save( m_cairo ); |
a7060b8c | 1724 | |
a9312950 | 1725 | if (fabs(angle) > 0.00001) |
f7b8e3d6 | 1726 | cairo_rotate( m_cairo, angle*DEG2RAD ); |
a7060b8c | 1727 | |
12ca5586 VS |
1728 | cairo_scale(m_cairo, m_scaleX, m_scaleY); |
1729 | ||
1730 | int w,h; | |
1731 | pango_layout_get_pixel_size( m_layout, &w, &h ); | |
1732 | ||
1733 | if ( m_backgroundMode == wxBRUSHSTYLE_SOLID ) | |
1734 | { | |
1735 | unsigned char red = m_textBackgroundColour.Red(); | |
1736 | unsigned char blue = m_textBackgroundColour.Blue(); | |
1737 | unsigned char green = m_textBackgroundColour.Green(); | |
1738 | unsigned char alpha = m_textBackgroundColour.Alpha(); | |
1739 | ||
1740 | double redPS = (double)(red) / 255.0; | |
1741 | double bluePS = (double)(blue) / 255.0; | |
1742 | double greenPS = (double)(green) / 255.0; | |
1743 | double alphaPS = (double)(alpha) / 255.0; | |
1744 | ||
1745 | cairo_save(m_cairo); | |
1746 | cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS ); | |
1747 | cairo_rectangle(m_cairo, 0, 0, w, h); // still in cairo units | |
1748 | cairo_fill(m_cairo); | |
1749 | cairo_restore(m_cairo); | |
1750 | } | |
1751 | ||
f7b8e3d6 VS |
1752 | pango_cairo_update_layout (m_cairo, m_layout); |
1753 | pango_cairo_show_layout (m_cairo, m_layout); | |
a7060b8c | 1754 | |
f7b8e3d6 | 1755 | cairo_restore( m_cairo ); |
fa034c45 RR |
1756 | |
1757 | if (underlined) | |
1758 | { | |
1759 | // Undo underline attributes setting | |
1760 | pango_layout_set_attributes(m_layout, NULL); | |
1761 | } | |
a7060b8c | 1762 | |
1d9fe50d | 1763 | // Back to device units: |
a9312950 RR |
1764 | CalcBoundingBox (x, y); |
1765 | CalcBoundingBox (x + w, y + h); | |
fa034c45 RR |
1766 | } |
1767 | ||
888dde65 | 1768 | void wxGtkPrinterDCImpl::Clear() |
fa034c45 | 1769 | { |
0187f0bc | 1770 | // Clear does nothing for printing, but keep the code |
6d52ca53 | 1771 | // for later reuse |
0187f0bc | 1772 | /* |
f7b8e3d6 VS |
1773 | cairo_save(m_cairo); |
1774 | cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE); | |
fa034c45 | 1775 | SetBrush(m_backgroundBrush); |
f7b8e3d6 VS |
1776 | cairo_paint(m_cairo); |
1777 | cairo_restore(m_cairo); | |
0187f0bc | 1778 | */ |
fa034c45 RR |
1779 | } |
1780 | ||
888dde65 | 1781 | void wxGtkPrinterDCImpl::SetFont( const wxFont& font ) |
fa034c45 RR |
1782 | { |
1783 | m_font = font; | |
1784 | ||
1785 | if (m_font.Ok()) | |
1786 | { | |
1787 | if (m_fontdesc) | |
1788 | pango_font_description_free( m_fontdesc ); | |
1789 | ||
3e7ea45c | 1790 | m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description ); |
1d9fe50d | 1791 | |
12ca5586 VS |
1792 | float size = pango_font_description_get_size( m_fontdesc ); |
1793 | size = size * GetFontPointSizeAdjustment(72.0); | |
1794 | pango_font_description_set_size( m_fontdesc, (gint)size ); | |
1795 | ||
fa034c45 RR |
1796 | pango_layout_set_font_description( m_layout, m_fontdesc ); |
1797 | } | |
1798 | } | |
1799 | ||
888dde65 | 1800 | void wxGtkPrinterDCImpl::SetPen( const wxPen& pen ) |
fa034c45 RR |
1801 | { |
1802 | if (!pen.Ok()) return; | |
1803 | ||
1804 | m_pen = pen; | |
1805 | ||
b29b9485 | 1806 | double width; |
6d52ca53 | 1807 | |
b29b9485 RR |
1808 | if (m_pen.GetWidth() <= 0) |
1809 | width = 0.1; | |
1810 | else | |
1811 | width = (double) m_pen.GetWidth(); | |
fa034c45 | 1812 | |
f7b8e3d6 | 1813 | cairo_set_line_width( m_cairo, width * m_DEV2PS * m_scaleX ); |
fa034c45 RR |
1814 | static const double dotted[] = {2.0, 5.0}; |
1815 | static const double short_dashed[] = {4.0, 4.0}; | |
1816 | static const double long_dashed[] = {4.0, 8.0}; | |
1817 | static const double dotted_dashed[] = {6.0, 6.0, 2.0, 6.0}; | |
1818 | ||
1819 | switch (m_pen.GetStyle()) | |
1820 | { | |
f7b8e3d6 VS |
1821 | case wxPENSTYLE_DOT: cairo_set_dash( m_cairo, dotted, 2, 0 ); break; |
1822 | case wxPENSTYLE_SHORT_DASH: cairo_set_dash( m_cairo, short_dashed, 2, 0 ); break; | |
1823 | case wxPENSTYLE_LONG_DASH: cairo_set_dash( m_cairo, long_dashed, 2, 0 ); break; | |
1824 | case wxPENSTYLE_DOT_DASH: cairo_set_dash( m_cairo, dotted_dashed, 4, 0 ); break; | |
04ee05f9 | 1825 | case wxPENSTYLE_USER_DASH: |
fa034c45 RR |
1826 | { |
1827 | wxDash *wx_dashes; | |
da249bc3 | 1828 | int num = m_pen.GetDashes (&wx_dashes); |
fa034c45 RR |
1829 | gdouble *g_dashes = g_new( gdouble, num ); |
1830 | int i; | |
1831 | for (i = 0; i < num; ++i) | |
1832 | g_dashes[i] = (gdouble) wx_dashes[i]; | |
f7b8e3d6 | 1833 | cairo_set_dash( m_cairo, g_dashes, num, 0); |
fa034c45 RR |
1834 | g_free( g_dashes ); |
1835 | } | |
1836 | break; | |
04ee05f9 PC |
1837 | case wxPENSTYLE_SOLID: |
1838 | case wxPENSTYLE_TRANSPARENT: | |
f7b8e3d6 | 1839 | default: cairo_set_dash( m_cairo, NULL, 0, 0 ); break; |
fa034c45 RR |
1840 | } |
1841 | ||
1842 | switch (m_pen.GetCap()) | |
1843 | { | |
f7b8e3d6 VS |
1844 | case wxCAP_PROJECTING: cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_SQUARE); break; |
1845 | case wxCAP_BUTT: cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_BUTT); break; | |
fa034c45 | 1846 | case wxCAP_ROUND: |
f7b8e3d6 | 1847 | default: cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_ROUND); break; |
fa034c45 RR |
1848 | } |
1849 | ||
1850 | switch (m_pen.GetJoin()) | |
1851 | { | |
f7b8e3d6 VS |
1852 | case wxJOIN_BEVEL: cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_BEVEL); break; |
1853 | case wxJOIN_MITER: cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_MITER); break; | |
fa034c45 | 1854 | case wxJOIN_ROUND: |
f7b8e3d6 | 1855 | default: cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_ROUND); break; |
fa034c45 RR |
1856 | } |
1857 | ||
1858 | unsigned char red = m_pen.GetColour().Red(); | |
1859 | unsigned char blue = m_pen.GetColour().Blue(); | |
1860 | unsigned char green = m_pen.GetColour().Green(); | |
1861 | unsigned char alpha = m_pen.GetColour().Alpha(); | |
1862 | ||
1863 | if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha)) | |
1864 | { | |
1865 | double redPS = (double)(red) / 255.0; | |
1866 | double bluePS = (double)(blue) / 255.0; | |
1867 | double greenPS = (double)(green) / 255.0; | |
1868 | double alphaPS = (double)(alpha) / 255.0; | |
1869 | ||
f7b8e3d6 | 1870 | cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS ); |
fa034c45 RR |
1871 | |
1872 | m_currentRed = red; | |
1873 | m_currentBlue = blue; | |
1874 | m_currentGreen = green; | |
1875 | m_currentAlpha = alpha; | |
1876 | } | |
1877 | } | |
1878 | ||
888dde65 | 1879 | void wxGtkPrinterDCImpl::SetBrush( const wxBrush& brush ) |
fa034c45 RR |
1880 | { |
1881 | if (!brush.Ok()) return; | |
1882 | ||
1883 | m_brush = brush; | |
1884 | ||
04ee05f9 | 1885 | if (m_brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT) |
a9312950 | 1886 | { |
f7b8e3d6 | 1887 | cairo_set_source_rgba( m_cairo, 0, 0, 0, 0 ); |
a9312950 RR |
1888 | m_currentRed = 0; |
1889 | m_currentBlue = 0; | |
1890 | m_currentGreen = 0; | |
1891 | m_currentAlpha = 0; | |
1892 | return; | |
1893 | } | |
1894 | ||
fa034c45 RR |
1895 | // Brush colour. |
1896 | unsigned char red = m_brush.GetColour().Red(); | |
1897 | unsigned char blue = m_brush.GetColour().Blue(); | |
1898 | unsigned char green = m_brush.GetColour().Green(); | |
1899 | unsigned char alpha = m_brush.GetColour().Alpha(); | |
1900 | ||
1901 | double redPS = (double)(red) / 255.0; | |
1902 | double bluePS = (double)(blue) / 255.0; | |
1903 | double greenPS = (double)(green) / 255.0; | |
1904 | double alphaPS = (double)(alpha) / 255.0; | |
1905 | ||
1906 | if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha)) | |
1907 | { | |
f7b8e3d6 | 1908 | cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS ); |
fa034c45 RR |
1909 | |
1910 | m_currentRed = red; | |
1911 | m_currentBlue = blue; | |
1912 | m_currentGreen = green; | |
1913 | m_currentAlpha = alpha; | |
1914 | } | |
1915 | ||
1916 | if (m_brush.IsHatch()) | |
1917 | { | |
1918 | cairo_t * cr; | |
1919 | cairo_surface_t *surface; | |
f7b8e3d6 VS |
1920 | surface = cairo_surface_create_similar(cairo_get_target(m_cairo),CAIRO_CONTENT_COLOR_ALPHA,10,10); |
1921 | cr = cairo_create(surface); | |
1922 | cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE); | |
1923 | cairo_set_line_width(cr, 1); | |
1924 | cairo_set_line_join(cr,CAIRO_LINE_JOIN_MITER); | |
fa034c45 RR |
1925 | |
1926 | switch (m_brush.GetStyle()) | |
1927 | { | |
04ee05f9 | 1928 | case wxBRUSHSTYLE_CROSS_HATCH: |
f7b8e3d6 VS |
1929 | cairo_move_to(cr, 5, 0); |
1930 | cairo_line_to(cr, 5, 10); | |
1931 | cairo_move_to(cr, 0, 5); | |
1932 | cairo_line_to(cr, 10, 5); | |
fa034c45 | 1933 | break; |
04ee05f9 | 1934 | case wxBRUSHSTYLE_BDIAGONAL_HATCH: |
f7b8e3d6 VS |
1935 | cairo_move_to(cr, 0, 10); |
1936 | cairo_line_to(cr, 10, 0); | |
fa034c45 | 1937 | break; |
04ee05f9 | 1938 | case wxBRUSHSTYLE_FDIAGONAL_HATCH: |
f7b8e3d6 VS |
1939 | cairo_move_to(cr, 0, 0); |
1940 | cairo_line_to(cr, 10, 10); | |
fa034c45 | 1941 | break; |
04ee05f9 | 1942 | case wxBRUSHSTYLE_CROSSDIAG_HATCH: |
f7b8e3d6 VS |
1943 | cairo_move_to(cr, 0, 0); |
1944 | cairo_line_to(cr, 10, 10); | |
1945 | cairo_move_to(cr, 10, 0); | |
1946 | cairo_line_to(cr, 0, 10); | |
fa034c45 | 1947 | break; |
04ee05f9 | 1948 | case wxBRUSHSTYLE_HORIZONTAL_HATCH: |
f7b8e3d6 VS |
1949 | cairo_move_to(cr, 0, 5); |
1950 | cairo_line_to(cr, 10, 5); | |
fa034c45 | 1951 | break; |
04ee05f9 | 1952 | case wxBRUSHSTYLE_VERTICAL_HATCH: |
f7b8e3d6 VS |
1953 | cairo_move_to(cr, 5, 0); |
1954 | cairo_line_to(cr, 5, 10); | |
fa034c45 RR |
1955 | break; |
1956 | default: | |
1957 | wxFAIL_MSG(_("Couldn't get hatch style from wxBrush.")); | |
1958 | } | |
1959 | ||
f7b8e3d6 VS |
1960 | cairo_set_source_rgba(cr, redPS, greenPS, bluePS, alphaPS); |
1961 | cairo_stroke (cr); | |
fa034c45 | 1962 | |
f7b8e3d6 VS |
1963 | cairo_destroy(cr); |
1964 | cairo_pattern_t * pattern = cairo_pattern_create_for_surface (surface); | |
1965 | cairo_surface_destroy(surface); | |
1966 | cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT); | |
1967 | cairo_set_source(m_cairo, pattern); | |
1968 | cairo_pattern_destroy(pattern); | |
fa034c45 RR |
1969 | } |
1970 | } | |
1971 | ||
89efaf2b | 1972 | void wxGtkPrinterDCImpl::SetLogicalFunction( wxRasterOperationMode function ) |
fa034c45 RR |
1973 | { |
1974 | if (function == wxCLEAR) | |
f7b8e3d6 | 1975 | cairo_set_operator (m_cairo, CAIRO_OPERATOR_CLEAR); |
fa034c45 | 1976 | else if (function == wxOR) |
f7b8e3d6 | 1977 | cairo_set_operator (m_cairo, CAIRO_OPERATOR_OUT); |
fa034c45 | 1978 | else if (function == wxNO_OP) |
f7b8e3d6 | 1979 | cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST); |
fa034c45 | 1980 | else if (function == wxAND) |
f7b8e3d6 | 1981 | cairo_set_operator (m_cairo, CAIRO_OPERATOR_ADD); |
fa034c45 | 1982 | else if (function == wxSET) |
f7b8e3d6 | 1983 | cairo_set_operator (m_cairo, CAIRO_OPERATOR_SATURATE); |
fa034c45 | 1984 | else if (function == wxXOR) |
f7b8e3d6 | 1985 | cairo_set_operator (m_cairo, CAIRO_OPERATOR_XOR); |
fa034c45 | 1986 | else // wxCOPY or anything else. |
f7b8e3d6 | 1987 | cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE); |
fa034c45 RR |
1988 | } |
1989 | ||
888dde65 | 1990 | void wxGtkPrinterDCImpl::SetBackground( const wxBrush& brush ) |
fa034c45 RR |
1991 | { |
1992 | m_backgroundBrush = brush; | |
f7b8e3d6 VS |
1993 | cairo_save(m_cairo); |
1994 | cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST_OVER); | |
fa034c45 RR |
1995 | |
1996 | SetBrush(m_backgroundBrush); | |
f7b8e3d6 VS |
1997 | cairo_paint(m_cairo); |
1998 | cairo_restore(m_cairo); | |
fa034c45 RR |
1999 | } |
2000 | ||
888dde65 | 2001 | void wxGtkPrinterDCImpl::SetBackgroundMode(int mode) |
fa034c45 | 2002 | { |
04ee05f9 PC |
2003 | if (mode == wxBRUSHSTYLE_SOLID) |
2004 | m_backgroundMode = wxBRUSHSTYLE_SOLID; | |
e0d1fd7f | 2005 | else |
04ee05f9 | 2006 | m_backgroundMode = wxBRUSHSTYLE_TRANSPARENT; |
fa034c45 RR |
2007 | } |
2008 | ||
888dde65 | 2009 | void wxGtkPrinterDCImpl::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height) |
fa034c45 | 2010 | { |
f7b8e3d6 VS |
2011 | cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height)); |
2012 | cairo_clip(m_cairo); | |
fa034c45 RR |
2013 | } |
2014 | ||
888dde65 | 2015 | void wxGtkPrinterDCImpl::DestroyClippingRegion() |
fa034c45 | 2016 | { |
f7b8e3d6 | 2017 | cairo_reset_clip(m_cairo); |
fa034c45 RR |
2018 | } |
2019 | ||
888dde65 | 2020 | bool wxGtkPrinterDCImpl::StartDoc(const wxString& WXUNUSED(message)) |
fa034c45 RR |
2021 | { |
2022 | return true; | |
2023 | } | |
2024 | ||
888dde65 | 2025 | void wxGtkPrinterDCImpl::EndDoc() |
fa034c45 RR |
2026 | { |
2027 | return; | |
2028 | } | |
2029 | ||
888dde65 | 2030 | void wxGtkPrinterDCImpl::StartPage() |
fa034c45 RR |
2031 | { |
2032 | return; | |
2033 | } | |
2034 | ||
888dde65 | 2035 | void wxGtkPrinterDCImpl::EndPage() |
fa034c45 RR |
2036 | { |
2037 | return; | |
2038 | } | |
2039 | ||
888dde65 | 2040 | wxCoord wxGtkPrinterDCImpl::GetCharHeight() const |
fa034c45 RR |
2041 | { |
2042 | pango_layout_set_text( m_layout, "H", 1 ); | |
2043 | ||
2044 | int w,h; | |
2045 | pango_layout_get_pixel_size( m_layout, &w, &h ); | |
2046 | ||
a9312950 | 2047 | return wxRound( h * m_PS2DEV ); |
fa034c45 RR |
2048 | } |
2049 | ||
888dde65 | 2050 | wxCoord wxGtkPrinterDCImpl::GetCharWidth() const |
fa034c45 RR |
2051 | { |
2052 | pango_layout_set_text( m_layout, "H", 1 ); | |
2053 | ||
2054 | int w,h; | |
2055 | pango_layout_get_pixel_size( m_layout, &w, &h ); | |
2056 | ||
a9312950 | 2057 | return wxRound( w * m_PS2DEV ); |
fa034c45 RR |
2058 | } |
2059 | ||
888dde65 | 2060 | void wxGtkPrinterDCImpl::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoord *height, |
fa034c45 RR |
2061 | wxCoord *descent, |
2062 | wxCoord *externalLeading, | |
2063 | const wxFont *theFont ) const | |
2064 | { | |
2065 | if ( width ) | |
2066 | *width = 0; | |
2067 | if ( height ) | |
2068 | *height = 0; | |
2069 | if ( descent ) | |
2070 | *descent = 0; | |
2071 | if ( externalLeading ) | |
2072 | *externalLeading = 0; | |
2073 | ||
2074 | if (string.empty()) | |
2075 | { | |
2076 | return; | |
2077 | } | |
2078 | ||
12ca5586 VS |
2079 | cairo_save( m_cairo ); |
2080 | cairo_scale(m_cairo, m_scaleX, m_scaleY); | |
2081 | ||
fa034c45 | 2082 | // Set layout's text |
197380a0 | 2083 | const wxScopedCharBuffer dataUTF8 = string.utf8_str(); |
fa034c45 | 2084 | |
8cf694d4 | 2085 | gint oldSize=0; |
12ca5586 VS |
2086 | if ( theFont ) |
2087 | { | |
2088 | // scale the font and apply it | |
2089 | PangoFontDescription *desc = theFont->GetNativeFontInfo()->description; | |
2090 | float size = pango_font_description_get_size(desc); | |
2091 | size = size * GetFontPointSizeAdjustment(72.0); | |
2092 | pango_font_description_set_size(desc, (gint)size); | |
fa034c45 | 2093 | |
12ca5586 VS |
2094 | pango_layout_set_font_description(m_layout, desc); |
2095 | } | |
fa034c45 RR |
2096 | |
2097 | pango_layout_set_text( m_layout, dataUTF8, strlen(dataUTF8) ); | |
2098 | ||
12ca5586 VS |
2099 | int h; |
2100 | pango_layout_get_pixel_size( m_layout, width, &h ); | |
2101 | if ( height ) | |
2102 | *height = h; | |
a7060b8c | 2103 | |
fa034c45 RR |
2104 | if (descent) |
2105 | { | |
2106 | PangoLayoutIter *iter = pango_layout_get_iter(m_layout); | |
2107 | int baseline = pango_layout_iter_get_baseline(iter); | |
2108 | pango_layout_iter_free(iter); | |
12ca5586 | 2109 | *descent = h - PANGO_PIXELS(baseline); |
fa034c45 RR |
2110 | } |
2111 | ||
12ca5586 VS |
2112 | if ( theFont ) |
2113 | { | |
2114 | // restore font and reset font's size back | |
2115 | pango_layout_set_font_description(m_layout, m_fontdesc); | |
fa034c45 | 2116 | |
12ca5586 VS |
2117 | PangoFontDescription *desc = theFont->GetNativeFontInfo()->description; |
2118 | pango_font_description_set_size(desc, oldSize); | |
2119 | } | |
2120 | ||
2121 | cairo_restore( m_cairo ); | |
fa034c45 RR |
2122 | } |
2123 | ||
888dde65 | 2124 | void wxGtkPrinterDCImpl::DoGetSize(int* width, int* height) const |
fa034c45 | 2125 | { |
a9312950 RR |
2126 | GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc ); |
2127 | ||
fa034c45 | 2128 | if (width) |
eaeb9985 | 2129 | *width = wxRound( (double)gtk_page_setup_get_paper_width( setup, GTK_UNIT_POINTS ) * (double)m_resolution / 72.0 ); |
fa034c45 | 2130 | if (height) |
eaeb9985 | 2131 | *height = wxRound( (double)gtk_page_setup_get_paper_height( setup, GTK_UNIT_POINTS ) * (double)m_resolution / 72.0 ); |
fa034c45 RR |
2132 | } |
2133 | ||
888dde65 | 2134 | void wxGtkPrinterDCImpl::DoGetSizeMM(int *width, int *height) const |
fa034c45 | 2135 | { |
a9312950 | 2136 | GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc ); |
fa034c45 RR |
2137 | |
2138 | if (width) | |
a9312950 | 2139 | *width = wxRound( gtk_page_setup_get_paper_width( setup, GTK_UNIT_MM ) ); |
fa034c45 | 2140 | if (height) |
a9312950 | 2141 | *height = wxRound( gtk_page_setup_get_paper_height( setup, GTK_UNIT_MM ) ); |
fa034c45 RR |
2142 | } |
2143 | ||
888dde65 | 2144 | wxSize wxGtkPrinterDCImpl::GetPPI() const |
fa034c45 | 2145 | { |
a9312950 | 2146 | return wxSize( (int)m_resolution, (int)m_resolution ); |
fa034c45 RR |
2147 | } |
2148 | ||
888dde65 | 2149 | void wxGtkPrinterDCImpl::SetPrintData(const wxPrintData& data) |
fa034c45 RR |
2150 | { |
2151 | m_printData = data; | |
fa034c45 RR |
2152 | } |
2153 | ||
4f37154e RR |
2154 | // overriden for wxPrinterDC Impl |
2155 | ||
6d52ca53 | 2156 | wxRect wxGtkPrinterDCImpl::GetPaperRect() const |
fa034c45 | 2157 | { |
4f37154e RR |
2158 | // Does GtkPrint support printer margins? |
2159 | int w = 0; | |
2160 | int h = 0; | |
2161 | DoGetSize( &w, &h ); | |
2162 | return wxRect( 0,0,w,h ); | |
fa034c45 RR |
2163 | } |
2164 | ||
6d52ca53 | 2165 | int wxGtkPrinterDCImpl::GetResolution() const |
fa034c45 | 2166 | { |
a9312950 | 2167 | return m_resolution; |
fa034c45 RR |
2168 | } |
2169 | ||
2170 | // ---------------------------------------------------------------------------- | |
2171 | // Print preview | |
2172 | // ---------------------------------------------------------------------------- | |
2173 | ||
2174 | IMPLEMENT_CLASS(wxGtkPrintPreview, wxPrintPreviewBase) | |
2175 | ||
2176 | void wxGtkPrintPreview::Init(wxPrintout * WXUNUSED(printout), | |
115be92b VZ |
2177 | wxPrintout * WXUNUSED(printoutForPrinting), |
2178 | wxPrintData *data) | |
fa034c45 | 2179 | { |
115be92b VZ |
2180 | // convert wxPrintQuality to resolution (input pointer can be NULL) |
2181 | wxPrintQuality quality = data ? data->GetQuality() : wxPRINT_QUALITY_MEDIUM; | |
2182 | switch ( quality ) | |
2183 | { | |
2184 | case wxPRINT_QUALITY_HIGH: | |
2185 | m_resolution = 1200; | |
2186 | break; | |
2187 | ||
115be92b VZ |
2188 | case wxPRINT_QUALITY_LOW: |
2189 | m_resolution = 300; | |
2190 | break; | |
2191 | ||
2192 | case wxPRINT_QUALITY_DRAFT: | |
2193 | m_resolution = 150; | |
2194 | break; | |
1f2a1c3c VZ |
2195 | |
2196 | default: | |
2197 | if ( quality > 0 ) | |
2198 | { | |
2199 | // positive values directly indicate print resolution | |
2200 | m_resolution = quality; | |
2201 | break; | |
2202 | } | |
2203 | ||
2204 | wxFAIL_MSG( "unknown print quality" ); | |
2205 | // fall through | |
2206 | ||
2207 | case wxPRINT_QUALITY_MEDIUM: | |
2208 | m_resolution = 600; | |
2209 | break; | |
2210 | ||
115be92b | 2211 | } |
1f2a1c3c VZ |
2212 | |
2213 | DetermineScaling(); | |
fa034c45 RR |
2214 | } |
2215 | ||
2216 | wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout, | |
115be92b VZ |
2217 | wxPrintout *printoutForPrinting, |
2218 | wxPrintDialogData *data) | |
2219 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
fa034c45 | 2220 | { |
115be92b | 2221 | Init(printout, printoutForPrinting, data ? &data->GetPrintData() : NULL); |
fa034c45 RR |
2222 | } |
2223 | ||
2224 | wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout, | |
115be92b VZ |
2225 | wxPrintout *printoutForPrinting, |
2226 | wxPrintData *data) | |
2227 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
fa034c45 | 2228 | { |
115be92b | 2229 | Init(printout, printoutForPrinting, data); |
fa034c45 RR |
2230 | } |
2231 | ||
2232 | wxGtkPrintPreview::~wxGtkPrintPreview() | |
2233 | { | |
2234 | } | |
2235 | ||
2236 | bool wxGtkPrintPreview::Print(bool interactive) | |
2237 | { | |
2238 | if (!m_printPrintout) | |
2239 | return false; | |
2240 | ||
2241 | wxPrinter printer(& m_printDialogData); | |
2242 | return printer.Print(m_previewFrame, m_printPrintout, interactive); | |
2243 | } | |
2244 | ||
2245 | void wxGtkPrintPreview::DetermineScaling() | |
2246 | { | |
2247 | wxPaperSize paperType = m_printDialogData.GetPrintData().GetPaperId(); | |
2248 | ||
2249 | wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType); | |
2250 | if (!paper) | |
2251 | paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4); | |
2252 | ||
2253 | if (paper) | |
2254 | { | |
40fcf546 | 2255 | m_previewPrintout->SetPPIScreen(wxGetDisplayPPI()); |
115be92b | 2256 | m_previewPrintout->SetPPIPrinter( m_resolution, m_resolution ); |
a7060b8c | 2257 | |
fa034c45 RR |
2258 | // Get width and height in points (1/72th of an inch) |
2259 | wxSize sizeDevUnits(paper->GetSizeDeviceUnits()); | |
115be92b VZ |
2260 | sizeDevUnits.x = wxRound((double)sizeDevUnits.x * (double)m_resolution / 72.0); |
2261 | sizeDevUnits.y = wxRound((double)sizeDevUnits.y * (double)m_resolution / 72.0); | |
6d52ca53 | 2262 | |
fa034c45 RR |
2263 | wxSize sizeTenthsMM(paper->GetSize()); |
2264 | wxSize sizeMM(sizeTenthsMM.x / 10, sizeTenthsMM.y / 10); | |
2265 | ||
2266 | // If in landscape mode, we need to swap the width and height. | |
2267 | if ( m_printDialogData.GetPrintData().GetOrientation() == wxLANDSCAPE ) | |
2268 | { | |
2269 | m_pageWidth = sizeDevUnits.y; | |
2270 | m_pageHeight = sizeDevUnits.x; | |
2271 | m_previewPrintout->SetPageSizeMM(sizeMM.y, sizeMM.x); | |
2272 | } | |
2273 | else | |
2274 | { | |
2275 | m_pageWidth = sizeDevUnits.x; | |
2276 | m_pageHeight = sizeDevUnits.y; | |
2277 | m_previewPrintout->SetPageSizeMM(sizeMM.x, sizeMM.y); | |
2278 | } | |
2279 | m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight); | |
2280 | m_previewPrintout->SetPaperRectPixels(wxRect(0, 0, m_pageWidth, m_pageHeight)); | |
2281 | ||
2282 | // At 100%, the page should look about page-size on the screen. | |
115be92b | 2283 | m_previewScaleX = 0.8 * 72.0 / (double)m_resolution; |
fa034c45 RR |
2284 | m_previewScaleY = m_previewScaleX; |
2285 | } | |
2286 | } | |
2287 | ||
2288 | #endif | |
2289 | // wxUSE_GTKPRINT |