]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/gnome/gprint.cpp
Crash fix under VC++
[wxWidgets.git] / src / gtk1 / gnome / gprint.cpp
CommitLineData
ff910433 1/////////////////////////////////////////////////////////////////////////////
f85b45fb
RR
2// Name: gprint.cpp
3// Author: Robert Roebling
4// Purpose: Implement GNOME printing support
5// Created: 09/20/04
6// Copyright: Robert Roebling
7// Licence: wxWindows Licence
ff910433
RR
8/////////////////////////////////////////////////////////////////////////////
9
58c30cd8
RR
10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11#pragma implementation "gprint.h"
ff910433
RR
12#endif
13
14// For compilers that support precompilation, includes "wx/wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18 #pragma hdrstop
19#endif
20
7c72311f
RR
21#include "wx/gtk/gnome/gprint.h"
22
23#if wxUSE_LIBGNOMEPRINT
24
fa499247
RR
25#include "math.h"
26
ff910433
RR
27#include "wx/fontutil.h"
28#include "wx/printdlg.h"
29#include "wx/gtk/private.h"
08680429 30#include "wx/module.h"
06cfd325 31#include "wx/generic/prntdlgg.h"
c6efd6e0 32#include "wx/dynlib.h"
ff910433
RR
33
34#include <libgnomeprint/gnome-print.h>
35#include <libgnomeprint/gnome-print-pango.h>
06cfd325 36#include <libgnomeprint/gnome-print-config.h>
ff910433 37#include <libgnomeprintui/gnome-print-dialog.h>
0be7709e 38#include <libgnomeprintui/gnome-print-job-preview.h>
08680429 39#include <libgnomeprintui/gnome-print-paper-selector.h>
ff910433 40
d6c6acd6
RR
41
42#include "wx/html/forcelnk.h"
43FORCE_LINK_ME(gnome_print)
44
c6efd6e0
RR
45//----------------------------------------------------------------------------
46// wxGnomePrintLibrary
47//----------------------------------------------------------------------------
48
49#define wxDL_METHOD_DEFINE( rettype, name, args, shortargs, defret ) \
50 typedef rettype (* name ## Type) args ; \
51 name ## Type pfn_ ## name; \
52 rettype name args \
53 { if (m_ok) return pfn_ ## name shortargs ; return defret; }
54
55#define wxDL_METHOD_LOAD( lib, name, success ) \
56 pfn_ ## name = (name ## Type) lib->GetSymbol( wxT(#name), &success ); \
57 if (!success) return;
58
59class wxGnomePrintLibrary
60{
61public:
62 wxGnomePrintLibrary();
63 ~wxGnomePrintLibrary();
64
65 bool IsOk();
66 void InitializeMethods();
67
68private:
69 bool m_ok;
70 wxDynamicLibrary *m_gnome_print_lib;
59c73be8 71 wxDynamicLibrary *m_gnome_printui_lib;
c6efd6e0
RR
72
73public:
74 wxDL_METHOD_DEFINE( gint, gnome_print_newpath,
75 (GnomePrintContext *pc), (pc), 0 )
76 wxDL_METHOD_DEFINE( gint, gnome_print_moveto,
77 (GnomePrintContext *pc, gdouble x, gdouble y), (pc, x, y), 0 )
78 wxDL_METHOD_DEFINE( gint, gnome_print_lineto,
79 (GnomePrintContext *pc, gdouble x, gdouble y), (pc, x, y), 0 )
80 wxDL_METHOD_DEFINE( gint, gnome_print_curveto,
81 (GnomePrintContext *pc, gdouble x1, gdouble y1, gdouble x2, gdouble y2, gdouble x3, gdouble y3), (pc, x1, y1, x2, y2, x3, y3), 0 )
82 wxDL_METHOD_DEFINE( gint, gnome_print_closepath,
83 (GnomePrintContext *pc), (pc), 0 )
84 wxDL_METHOD_DEFINE( gint, gnome_print_stroke,
85 (GnomePrintContext *pc), (pc), 0 )
86 wxDL_METHOD_DEFINE( gint, gnome_print_fill,
87 (GnomePrintContext *pc), (pc), 0 )
88 wxDL_METHOD_DEFINE( gint, gnome_print_setrgbcolor,
89 (GnomePrintContext *pc, gdouble r, gdouble g, gdouble b), (pc, r, g, b), 0 )
90 wxDL_METHOD_DEFINE( gint, gnome_print_setlinewidth,
91 (GnomePrintContext *pc, gdouble width), (pc, width), 0 )
59c73be8
RR
92 wxDL_METHOD_DEFINE( gint, gnome_print_setdash,
93 (GnomePrintContext *pc, gint n_values, const gdouble *values, gdouble offset), (pc, n_values, values, offset), 0 )
94
95 wxDL_METHOD_DEFINE( gint, gnome_print_rgbimage,
96 (GnomePrintContext *pc, const guchar *data, gint width, gint height, gint rowstride), (pc, data, width, height, rowstride ), 0 )
97 wxDL_METHOD_DEFINE( gint, gnome_print_rgbaimage,
98 (GnomePrintContext *pc, const guchar *data, gint width, gint height, gint rowstride), (pc, data, width, height, rowstride ), 0 )
99
100 wxDL_METHOD_DEFINE( gint, gnome_print_concat,
101 (GnomePrintContext *pc, const gdouble *matrix), (pc, matrix), 0 )
102 wxDL_METHOD_DEFINE( gint, gnome_print_scale,
103 (GnomePrintContext *pc, gdouble sx, gdouble sy), (pc, sx, sy), 0 )
104 wxDL_METHOD_DEFINE( gint, gnome_print_rotate,
105 (GnomePrintContext *pc, gdouble theta), (pc, theta), 0 )
106
107 wxDL_METHOD_DEFINE( gint, gnome_print_gsave,
108 (GnomePrintContext *pc), (pc), 0 )
109 wxDL_METHOD_DEFINE( gint, gnome_print_grestore,
110 (GnomePrintContext *pc), (pc), 0 )
111
112 wxDL_METHOD_DEFINE( gint, gnome_print_beginpage,
113 (GnomePrintContext *pc, const guchar* name), (pc, name), 0 )
114 wxDL_METHOD_DEFINE( gint, gnome_print_showpage,
115 (GnomePrintContext *pc), (pc), 0 )
116 wxDL_METHOD_DEFINE( gint, gnome_print_end_doc,
117 (GnomePrintContext *pc), (pc), 0 )
118
119 wxDL_METHOD_DEFINE( PangoLayout*, gnome_print_pango_create_layout,
120 (GnomePrintContext *gpc), (gpc), NULL )
121 wxDL_METHOD_DEFINE( void, gnome_print_pango_layout,
122 (GnomePrintContext *gpc, PangoLayout *layout), (gpc, layout), /**/ )
123
124 wxDL_METHOD_DEFINE( GnomePrintJob*, gnome_print_job_new,
125 (GnomePrintConfig *config), (config), NULL )
126 wxDL_METHOD_DEFINE( GnomePrintContext*, gnome_print_job_get_context,
127 (GnomePrintJob *job), (job), NULL )
128 wxDL_METHOD_DEFINE( gint, gnome_print_job_close,
129 (GnomePrintJob *job), (job), 0 )
130 wxDL_METHOD_DEFINE( gint, gnome_print_job_print,
131 (GnomePrintJob *job), (job), 0 )
132 wxDL_METHOD_DEFINE( gboolean, gnome_print_job_get_page_size,
133 (GnomePrintJob *job, gdouble *width, gdouble *height), (job, width, height), 0 )
134
135 wxDL_METHOD_DEFINE( GnomePrintUnit*, gnome_print_unit_get_by_abbreviation,
136 (const guchar *abbreviation), (abbreviation), NULL )
137 wxDL_METHOD_DEFINE( gboolean, gnome_print_convert_distance,
138 (gdouble *distance, const GnomePrintUnit *from, const GnomePrintUnit *to), (distance, from, to), false )
139
140 wxDL_METHOD_DEFINE( GnomePrintConfig*, gnome_print_config_default,
141 (void), (), NULL )
142 wxDL_METHOD_DEFINE( gboolean, gnome_print_config_set,
143 (GnomePrintConfig *config, const guchar *key, const guchar *value), (config, key, value), false )
144 wxDL_METHOD_DEFINE( gboolean, gnome_print_config_get_length,
145 (GnomePrintConfig *config, const guchar *key, gdouble *val, const GnomePrintUnit **unit), (config, key, val, unit), false )
146
147 wxDL_METHOD_DEFINE( GtkWidget*, gnome_print_dialog_new,
148 (GnomePrintJob *gpj, const guchar *title, gint flags), (gpj, title, flags), NULL )
149 wxDL_METHOD_DEFINE( void, gnome_print_dialog_construct_range_page,
150 (GnomePrintDialog *gpd, gint flags, gint start, gint end,
151 const guchar *currentlabel, const guchar *rangelabel),
152 (gpd, flags, start, end, currentlabel, rangelabel), /**/ )
153 wxDL_METHOD_DEFINE( void, gnome_print_dialog_get_copies,
154 (GnomePrintDialog *gpd, gint *copies, gboolean *collate), (gpd, copies, collate), /**/ )
155 wxDL_METHOD_DEFINE( void, gnome_print_dialog_set_copies,
156 (GnomePrintDialog *gpd, gint copies, gint collate), (gpd, copies, collate), /**/ )
157 wxDL_METHOD_DEFINE( GnomePrintRangeType, gnome_print_dialog_get_range,
158 (GnomePrintDialog *gpd), (gpd), GNOME_PRINT_RANGETYPE_NONE )
159 wxDL_METHOD_DEFINE( int, gnome_print_dialog_get_range_page,
160 (GnomePrintDialog *gpd, gint *start, gint *end), (gpd, start, end), 0 )
161
162 wxDL_METHOD_DEFINE( GtkWidget*, gnome_paper_selector_new_with_flags,
163 (GnomePrintConfig *config, gint flags), (config, flags), NULL )
164
165 wxDL_METHOD_DEFINE( GtkWidget*, gnome_print_job_preview_new,
166 (GnomePrintJob *gpm, const guchar *title), (gpm, title), NULL )
c6efd6e0
RR
167};
168
169wxGnomePrintLibrary::wxGnomePrintLibrary()
170{
59c73be8
RR
171 m_gnome_print_lib = NULL;
172 m_gnome_printui_lib = NULL;
173
c6efd6e0 174 wxLogNull log;
59c73be8 175
c6efd6e0
RR
176 m_gnome_print_lib = new wxDynamicLibrary( wxT("libgnomeprint-2-2.so") );
177 m_ok = m_gnome_print_lib->IsLoaded();
59c73be8
RR
178 if (!m_ok) return;
179
180 m_gnome_printui_lib = new wxDynamicLibrary( wxT("libgnomeprintui-2-2.so") );
181 m_ok = m_gnome_printui_lib->IsLoaded();
182 if (!m_ok) return;
c6efd6e0 183
59c73be8 184 InitializeMethods();
c6efd6e0
RR
185}
186
187wxGnomePrintLibrary::~wxGnomePrintLibrary()
188{
59c73be8
RR
189 if (m_gnome_print_lib)
190 delete m_gnome_print_lib;
191 if (m_gnome_printui_lib)
192 delete m_gnome_printui_lib;
c6efd6e0
RR
193}
194
195bool wxGnomePrintLibrary::IsOk()
196{
197 return m_ok;
198}
199
200void wxGnomePrintLibrary::InitializeMethods()
201{
202 m_ok = false;
203 bool success;
204
205 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_newpath, success )
206 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_moveto, success )
207 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_lineto, success )
208 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_curveto, success )
209 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_closepath, success )
210 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_stroke, success )
211 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_fill, success )
212 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_setrgbcolor, success )
213 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_setlinewidth, success )
59c73be8
RR
214 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_setdash, success )
215
216 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_rgbimage, success )
217 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_rgbaimage, success )
218
219 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_concat, success )
220 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_scale, success )
221 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_rotate, success )
222
223 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_gsave, success )
224 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_grestore, success )
225
226 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_beginpage, success )
227 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_showpage, success )
228 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_end_doc, success )
229
230 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_pango_create_layout, success )
231 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_pango_layout, success )
232
233 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_job_new, success )
234 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_job_get_context, success )
235 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_job_close, success )
236 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_job_print, success )
237 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_job_get_page_size, success )
238
239 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_unit_get_by_abbreviation, success )
240 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_convert_distance, success )
241
242 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_config_default, success )
243 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_config_set, success )
244 wxDL_METHOD_LOAD( m_gnome_print_lib, gnome_print_config_get_length, success )
245
246 wxDL_METHOD_LOAD( m_gnome_printui_lib, gnome_print_dialog_new, success )
247 wxDL_METHOD_LOAD( m_gnome_printui_lib, gnome_print_dialog_construct_range_page, success )
248 wxDL_METHOD_LOAD( m_gnome_printui_lib, gnome_print_dialog_get_copies, success )
249 wxDL_METHOD_LOAD( m_gnome_printui_lib, gnome_print_dialog_set_copies, success )
250 wxDL_METHOD_LOAD( m_gnome_printui_lib, gnome_print_dialog_get_range, success )
251 wxDL_METHOD_LOAD( m_gnome_printui_lib, gnome_print_dialog_get_range_page, success )
252
253 wxDL_METHOD_LOAD( m_gnome_printui_lib, gnome_paper_selector_new_with_flags, success )
254
255 wxDL_METHOD_LOAD( m_gnome_printui_lib, gnome_print_job_preview_new, success )
256
c6efd6e0
RR
257 m_ok = true;
258}
259
260static wxGnomePrintLibrary* gs_lgp = NULL;
261
ff910433
RR
262//----------------------------------------------------------------------------
263// wxGnomePrintNativeData
264//----------------------------------------------------------------------------
265
266IMPLEMENT_CLASS(wxGnomePrintNativeData, wxPrintNativeDataBase)
267
268wxGnomePrintNativeData::wxGnomePrintNativeData()
269{
59c73be8
RR
270 m_config = gs_lgp->gnome_print_config_default();
271 m_job = gs_lgp->gnome_print_job_new( m_config );
ff910433
RR
272}
273
274wxGnomePrintNativeData::~wxGnomePrintNativeData()
275{
276 g_object_unref (G_OBJECT (m_config));
ff910433
RR
277}
278
279bool wxGnomePrintNativeData::TransferTo( wxPrintData &data )
280{
281 // TODO
282 return true;
283}
284
285bool wxGnomePrintNativeData::TransferFrom( const wxPrintData &data )
286{
287 // TODO
288 return true;
289}
290
291//----------------------------------------------------------------------------
292// wxGnomePrintFactory
293//----------------------------------------------------------------------------
294
295wxPrinterBase* wxGnomePrintFactory::CreatePrinter( wxPrintDialogData *data )
296{
297 return new wxGnomePrinter( data );
298}
299
300wxPrintPreviewBase *wxGnomePrintFactory::CreatePrintPreview( wxPrintout *preview,
301 wxPrintout *printout,
302 wxPrintDialogData *data )
303{
304 return new wxPostScriptPrintPreview( preview, printout, data );
305}
306
307wxPrintPreviewBase *wxGnomePrintFactory::CreatePrintPreview( wxPrintout *preview,
308 wxPrintout *printout,
309 wxPrintData *data )
310{
311 return new wxPostScriptPrintPreview( preview, printout, data );
312}
313
314wxPrintDialogBase *wxGnomePrintFactory::CreatePrintDialog( wxWindow *parent,
315 wxPrintDialogData *data )
316{
2934005d 317 return new wxGnomePrintDialog( parent, data );
ff910433
RR
318}
319
320wxPrintDialogBase *wxGnomePrintFactory::CreatePrintDialog( wxWindow *parent,
321 wxPrintData *data )
322{
2934005d 323 return new wxGnomePrintDialog( parent, data );
ff910433
RR
324}
325
08680429
RR
326wxPageSetupDialogBase *wxGnomePrintFactory::CreatePageSetupDialog( wxWindow *parent,
327 wxPageSetupDialogData * data )
06cfd325
RR
328{
329// The native page setup dialog is broken. It
330// miscalculates newly entered values for the
331// margins if you have not chose "points" but
cffcf831
RR
332// e.g. centimerters.
333// This has been fixed in GNOME CVS (maybe
334// fixed in libgnomeprintui 2.8.1)
06cfd325 335
cffcf831 336 return new wxGnomePageSetupDialog( parent, data );
08680429
RR
337}
338
ff910433
RR
339bool wxGnomePrintFactory::HasPrintSetupDialog()
340{
2934005d 341 return false;
ff910433
RR
342}
343
344wxDialog *wxGnomePrintFactory::CreatePrintSetupDialog( wxWindow *parent, wxPrintData *data )
345{
2934005d 346 return NULL;
ff910433
RR
347}
348
349bool wxGnomePrintFactory::HasOwnPrintToFile()
350{
351 return true;
352}
353
354bool wxGnomePrintFactory::HasPrinterLine()
355{
356 return true;
357}
358
359wxString wxGnomePrintFactory::CreatePrinterLine()
360{
2934005d
RR
361 // redundant now
362 return wxEmptyString;
ff910433
RR
363}
364
365bool wxGnomePrintFactory::HasStatusLine()
366{
2934005d 367 // redundant now
ff910433
RR
368 return true;
369}
370
371wxString wxGnomePrintFactory::CreateStatusLine()
372{
2934005d
RR
373 // redundant now
374 return wxEmptyString;
ff910433
RR
375}
376
377wxPrintNativeDataBase *wxGnomePrintFactory::CreatePrintNativeData()
378{
379 return new wxGnomePrintNativeData;
380}
381
382//----------------------------------------------------------------------------
383// wxGnomePrintSetupDialog
384//----------------------------------------------------------------------------
385
2934005d
RR
386IMPLEMENT_CLASS(wxGnomePrintDialog, wxPrintDialogBase)
387
388wxGnomePrintDialog::wxGnomePrintDialog( wxWindow *parent, wxPrintDialogData *data )
389 : wxPrintDialogBase(parent, wxID_ANY, _("Print"),
390 wxPoint(0, 0), wxSize(600, 600),
391 wxDEFAULT_DIALOG_STYLE |
392 wxTAB_TRAVERSAL)
393{
394 if (data)
395 m_printDialogData = *data;
396
397 Init();
398}
399
400wxGnomePrintDialog::wxGnomePrintDialog( wxWindow *parent, wxPrintData *data )
401 : wxPrintDialogBase(parent, wxID_ANY, _("Print"),
402 wxPoint(0, 0), wxSize(600, 600),
403 wxDEFAULT_DIALOG_STYLE |
404 wxTAB_TRAVERSAL)
405{
406 if (data)
407 m_printDialogData = *data;
408
409 Init();
410}
ff910433 411
2934005d 412void wxGnomePrintDialog::Init()
ff910433 413{
2934005d
RR
414 wxPrintData data = m_printDialogData.GetPrintData();
415
ff910433 416 wxGnomePrintNativeData *native =
2934005d 417 (wxGnomePrintNativeData*) data.GetNativeData();
ff910433 418
59c73be8 419 m_widget = gs_lgp->gnome_print_dialog_new( native->GetPrintJob(),
2934005d
RR
420 (guchar*)"Print",
421 GNOME_PRINT_DIALOG_RANGE|GNOME_PRINT_DIALOG_COPIES );
b199de59
RR
422
423 int flag = 0;
424 if (m_printDialogData.GetEnableSelection())
425 flag |= GNOME_PRINT_RANGE_SELECTION;
426 if (m_printDialogData.GetEnablePageNumbers())
427 flag |= GNOME_PRINT_RANGE_ALL|GNOME_PRINT_RANGE_RANGE;
428
59c73be8 429 gs_lgp->gnome_print_dialog_construct_range_page( (GnomePrintDialog*) m_widget,
b199de59
RR
430 flag,
431 m_printDialogData.GetMinPage(),
432 m_printDialogData.GetMaxPage(),
433 NULL,
434 NULL );
ff910433
RR
435}
436
2934005d 437wxGnomePrintDialog::~wxGnomePrintDialog()
ff910433
RR
438{
439 m_widget = NULL;
440}
441
2934005d 442int wxGnomePrintDialog::ShowModal()
ff910433 443{
2934005d
RR
444 // Transfer data from m_printDalogData to dialog here
445
ff910433 446 int response = gtk_dialog_run (GTK_DIALOG (m_widget));
ff910433
RR
447
448 if (response == GNOME_PRINT_DIALOG_RESPONSE_CANCEL)
b199de59
RR
449 {
450 gtk_widget_destroy(m_widget);
451 m_widget = NULL;
452
ff910433 453 return wxID_CANCEL;
b199de59 454 }
ff910433 455
b199de59
RR
456 gint copies = 1;
457 gboolean collate = false;
59c73be8 458 gs_lgp->gnome_print_dialog_get_copies( (GnomePrintDialog*) m_widget, &copies, &collate );
b199de59
RR
459 m_printDialogData.SetNoCopies( copies );
460 m_printDialogData.SetCollate( collate );
2934005d 461
59c73be8 462 switch (gs_lgp->gnome_print_dialog_get_range( (GnomePrintDialog*) m_widget ))
b199de59
RR
463 {
464 case GNOME_PRINT_RANGE_SELECTION:
465 m_printDialogData.SetSelection( true );
466 break;
467 case GNOME_PRINT_RANGE_ALL:
468 m_printDialogData.SetAllPages( true );
469 m_printDialogData.SetFromPage( 0 );
470 m_printDialogData.SetToPage( 9999 );
471 break;
472 case GNOME_PRINT_RANGE_RANGE:
473 default:
474 gint start,end;
59c73be8 475 gs_lgp->gnome_print_dialog_get_range_page( (GnomePrintDialog*) m_widget, &start, &end );
b199de59
RR
476 m_printDialogData.SetFromPage( start );
477 m_printDialogData.SetToPage( end );
478 break;
479 }
480
481 gtk_widget_destroy(m_widget);
482 m_widget = NULL;
483
0be7709e
RR
484 if (response == GNOME_PRINT_DIALOG_RESPONSE_PREVIEW)
485 return wxID_PREVIEW;
486
ff910433
RR
487 return wxID_OK;
488}
489
2934005d 490wxDC *wxGnomePrintDialog::GetPrintDC()
ff910433 491{
2934005d
RR
492 // Later
493 return NULL;
ff910433
RR
494}
495
2934005d 496bool wxGnomePrintDialog::Validate()
ff910433
RR
497{
498 return true;
499}
500
2934005d 501bool wxGnomePrintDialog::TransferDataToWindow()
ff910433
RR
502{
503 return true;
504}
505
2934005d
RR
506bool wxGnomePrintDialog::TransferDataFromWindow()
507{
508 return true;
509}
ff910433 510
08680429
RR
511//----------------------------------------------------------------------------
512// wxGnomePageSetupDialog
513//----------------------------------------------------------------------------
514
515IMPLEMENT_CLASS(wxGnomePageSetupDialog, wxPageSetupDialogBase)
516
517wxGnomePageSetupDialog::wxGnomePageSetupDialog( wxWindow *parent,
518 wxPageSetupDialogData* data )
519{
520 if (data)
521 m_pageDialogData = *data;
522
523 wxGnomePrintNativeData *native =
524 (wxGnomePrintNativeData*) m_pageDialogData.GetPrintData().GetNativeData();
cffcf831
RR
525
526 // This is required as the page setup dialog
527 // calculates wrong values otherwise.
59c73be8 528 gs_lgp->gnome_print_config_set( native->GetPrintConfig(),
cffcf831
RR
529 (const guchar*) GNOME_PRINT_KEY_PREFERED_UNIT,
530 (const guchar*) "Pts" );
08680429
RR
531
532 m_widget = gtk_dialog_new();
533
534 gtk_window_set_title( GTK_WINDOW(m_widget), wxGTK_CONV( _("Page setup") ) );
cffcf831 535
59c73be8 536 GtkWidget *main = gs_lgp->gnome_paper_selector_new_with_flags( native->GetPrintConfig(),
08680429
RR
537 GNOME_PAPER_SELECTOR_MARGINS|GNOME_PAPER_SELECTOR_FEED_ORIENTATION );
538 gtk_container_set_border_width (GTK_CONTAINER (main), 8);
539 gtk_widget_show (main);
540
541 gtk_container_add( GTK_CONTAINER (GTK_DIALOG (m_widget)->vbox), main );
542
543 gtk_dialog_set_has_separator (GTK_DIALOG (m_widget), TRUE);
544
545 gtk_dialog_add_buttons (GTK_DIALOG (m_widget),
546 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
547 GTK_STOCK_OK, GTK_RESPONSE_OK,
548 NULL);
549
550 gtk_dialog_set_default_response (GTK_DIALOG (m_widget),
551 GTK_RESPONSE_OK);
552}
553
554wxGnomePageSetupDialog::~wxGnomePageSetupDialog()
555{
556}
557
558wxPageSetupDialogData& wxGnomePageSetupDialog::GetPageSetupDialogData()
559{
560 return m_pageDialogData;
561}
562
563int wxGnomePageSetupDialog::ShowModal()
564{
06cfd325
RR
565 wxGnomePrintNativeData *native =
566 (wxGnomePrintNativeData*) m_pageDialogData.GetPrintData().GetNativeData();
567 GnomePrintConfig *config = native->GetPrintConfig();
568
08680429
RR
569 // Transfer data from m_pageDialogData to native dialog
570
571 int ret = gtk_dialog_run( GTK_DIALOG(m_widget) );
572
573 if (ret == GTK_RESPONSE_OK)
574 {
575 // Transfer data back to m_pageDialogData
cffcf831
RR
576
577 // I don't know how querying the last parameter works
578 // I cannot test it as the dialog is currently broken
579 // anyways (it only works for points).
06cfd325 580 double ml,mr,mt,mb,pw,ph;
59c73be8 581 gs_lgp->gnome_print_config_get_length (config,
06cfd325 582 (const guchar*) GNOME_PRINT_KEY_PAGE_MARGIN_LEFT, &ml, NULL);
59c73be8 583 gs_lgp->gnome_print_config_get_length (config,
06cfd325 584 (const guchar*) GNOME_PRINT_KEY_PAGE_MARGIN_RIGHT, &mr, NULL);
59c73be8 585 gs_lgp->gnome_print_config_get_length (config,
06cfd325 586 (const guchar*) GNOME_PRINT_KEY_PAGE_MARGIN_TOP, &mt, NULL);
59c73be8 587 gs_lgp->gnome_print_config_get_length (config,
06cfd325 588 (const guchar*) GNOME_PRINT_KEY_PAGE_MARGIN_BOTTOM, &mb, NULL);
59c73be8 589 gs_lgp->gnome_print_config_get_length (config,
06cfd325 590 (const guchar*) GNOME_PRINT_KEY_PAPER_WIDTH, &pw, NULL);
59c73be8 591 gs_lgp->gnome_print_config_get_length (config,
06cfd325 592 (const guchar*) GNOME_PRINT_KEY_PAPER_HEIGHT, &ph, NULL);
cffcf831
RR
593
594 // This probably assumes that the user entered the
595 // values in Pts. Since that is the only the dialog
596 // works right now, we need to fix this later.
59c73be8
RR
597 const GnomePrintUnit *mm_unit = gs_lgp->gnome_print_unit_get_by_abbreviation( (const guchar*) "mm" );
598 const GnomePrintUnit *pts_unit = gs_lgp->gnome_print_unit_get_by_abbreviation( (const guchar*) "Pts" );
599 gs_lgp->gnome_print_convert_distance( &ml, pts_unit, mm_unit );
600 gs_lgp->gnome_print_convert_distance( &mr, pts_unit, mm_unit );
601 gs_lgp->gnome_print_convert_distance( &mt, pts_unit, mm_unit );
602 gs_lgp->gnome_print_convert_distance( &mb, pts_unit, mm_unit );
603 gs_lgp->gnome_print_convert_distance( &pw, pts_unit, mm_unit );
604 gs_lgp->gnome_print_convert_distance( &ph, pts_unit, mm_unit );
cffcf831
RR
605
606 m_pageDialogData.SetMarginTopLeft( wxPoint( (int)(ml+0.5), (int)(mt+0.5)) );
06cfd325
RR
607 m_pageDialogData.SetMarginBottomRight( wxPoint( (int)(mr+0.5), (int)(mb+0.5)) );
608
609 m_pageDialogData.SetPaperSize( wxSize( (int)(pw+0.5), (int)(ph+0.5) ) );
cffcf831 610
cac7ac30 611#if 0
06cfd325
RR
612 wxPrintf( wxT("paper %d %d, top margin %d\n"),
613 m_pageDialogData.GetPaperSize().x,
614 m_pageDialogData.GetPaperSize().y,
615 m_pageDialogData.GetMarginTopLeft().x );
616#endif
617
08680429
RR
618 ret = wxID_OK;
619 }
620 else
621 {
622 ret = wxID_CANCEL;
623 }
624
625 gtk_widget_destroy( m_widget );
626 m_widget = NULL;
627
628 return ret;
629}
630
631bool wxGnomePageSetupDialog::Validate()
632{
633 return true;
634}
635
636bool wxGnomePageSetupDialog::TransferDataToWindow()
637{
638 return true;
639}
640
641bool wxGnomePageSetupDialog::TransferDataFromWindow()
642{
643 return true;
644}
645
ff910433
RR
646//----------------------------------------------------------------------------
647// wxGnomePrinter
648//----------------------------------------------------------------------------
649
650IMPLEMENT_CLASS(wxGnomePrinter, wxPrinterBase)
651
652wxGnomePrinter::wxGnomePrinter( wxPrintDialogData *data ) :
653 wxPrinterBase( data )
654{
655 m_gpc = NULL;
0be7709e 656 m_native_preview = false;
ff910433
RR
657}
658
659wxGnomePrinter::~wxGnomePrinter()
660{
661}
662
663bool wxGnomePrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt )
664{
665 if (!printout)
666 {
667 sm_lastError = wxPRINTER_ERROR;
668 return false;
669 }
670
671 wxPrintData printdata = GetPrintDialogData().GetPrintData();
cffcf831 672 wxGnomePrintNativeData *native =
ff910433
RR
673 (wxGnomePrintNativeData*) printdata.GetNativeData();
674
59c73be8
RR
675 GnomePrintJob *job = gs_lgp->gnome_print_job_new( native->GetPrintConfig() );
676 m_gpc = gs_lgp->gnome_print_job_get_context (job);
cffcf831 677
981a6af1 678 // The GnomePrintJob is temporarily stored in the
2934005d 679 // native print data as the native print dialog
981a6af1 680 // needs to access it.
cffcf831
RR
681 native->SetPrintJob( job );
682
981a6af1
RR
683
684 printout->SetIsPreview(false);
685
686 if (m_printDialogData.GetMinPage() < 1)
687 m_printDialogData.SetMinPage(1);
688 if (m_printDialogData.GetMaxPage() < 1)
689 m_printDialogData.SetMaxPage(9999);
ff910433
RR
690
691 wxDC *dc;
ff910433
RR
692 if (prompt)
693 dc = PrintDialog( parent );
694 else
695 dc = new wxGnomePrintDC( this );
696
0be7709e
RR
697 if (m_native_preview)
698 printout->SetIsPreview(true);
699
ff910433
RR
700 if (!dc)
701 {
59c73be8 702 gs_lgp->gnome_print_job_close( job );
cffcf831 703 g_object_unref (G_OBJECT (job));
981a6af1 704 sm_lastError = wxPRINTER_ERROR;
ff910433
RR
705 return false;
706 }
707
981a6af1
RR
708 wxSize ScreenPixels = wxGetDisplaySize();
709 wxSize ScreenMM = wxGetDisplaySizeMM();
710
711 printout->SetPPIScreen( (int) ((ScreenPixels.GetWidth() * 25.4) / ScreenMM.GetWidth()),
712 (int) ((ScreenPixels.GetHeight() * 25.4) / ScreenMM.GetHeight()) );
b199de59
RR
713 printout->SetPPIPrinter( wxGnomePrintDC::GetResolution(),
714 wxGnomePrintDC::GetResolution() );
981a6af1 715
ff910433
RR
716 printout->SetDC(dc);
717
981a6af1
RR
718 int w, h;
719 dc->GetSize(&w, &h);
720 printout->SetPageSizePixels((int)w, (int)h);
721 dc->GetSizeMM(&w, &h);
722 printout->SetPageSizeMM((int)w, (int)h);
723
ff910433 724 printout->OnPreparePrinting();
b199de59
RR
725
726 // Get some parameters from the printout, if defined
727 int fromPage, toPage;
728 int minPage, maxPage;
729 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
ff910433 730
b199de59 731 if (maxPage == 0)
ff910433 732 {
59c73be8 733 gs_lgp->gnome_print_job_close( job );
cffcf831 734 g_object_unref (G_OBJECT (job));
ff910433 735 sm_lastError = wxPRINTER_ERROR;
b199de59 736 return false;
ff910433 737 }
b199de59
RR
738
739 printout->OnBeginPrinting();
740
741 int minPageNum = minPage, maxPageNum = maxPage;
742
743 if ( !m_printDialogData.GetAllPages() )
ff910433 744 {
b199de59
RR
745 minPageNum = m_printDialogData.GetFromPage();
746 maxPageNum = m_printDialogData.GetToPage();
747 }
748
749
750 int copyCount;
751 for ( copyCount = 1;
752 copyCount <= m_printDialogData.GetNoCopies();
753 copyCount++ )
754 {
755 if (!printout->OnBeginDocument(minPageNum, maxPageNum))
ff910433 756 {
b199de59
RR
757 wxLogError(_("Could not start printing."));
758 sm_lastError = wxPRINTER_ERROR;
759 break;
760 }
761
762 int pn;
763 for ( pn = minPageNum;
764 pn <= maxPageNum && printout->HasPage(pn);
765 pn++ )
766 {
ff910433
RR
767 dc->StartPage();
768 printout->OnPrintPage(pn);
769 dc->EndPage();
770 }
b199de59 771
ff910433
RR
772 printout->OnEndDocument();
773 printout->OnEndPrinting();
774 }
775
59c73be8 776 gs_lgp->gnome_print_job_close( job );
0be7709e
RR
777 if (m_native_preview)
778 {
779 wxString title( _("Print preview") );
59c73be8 780 gtk_widget_show( gs_lgp->gnome_print_job_preview_new( job, (const guchar*)(const char*)wxGTK_CONV(title) ));
0be7709e
RR
781 }
782 else
783 {
59c73be8 784 gs_lgp->gnome_print_job_print( job );
0be7709e 785 }
ff910433 786
cffcf831 787 g_object_unref (G_OBJECT (job));
ff910433
RR
788 delete dc;
789
790 return (sm_lastError == wxPRINTER_NO_ERROR);
791}
792
793wxDC* wxGnomePrinter::PrintDialog( wxWindow *parent )
794{
2934005d 795 wxGnomePrintDialog dialog( parent, &m_printDialogData );
0be7709e
RR
796 int ret = dialog.ShowModal();
797 if (ret == wxID_CANCEL)
ff910433
RR
798 {
799 sm_lastError = wxPRINTER_ERROR;
800 return NULL;
801 }
802
0be7709e
RR
803 m_native_preview = ret == wxID_PREVIEW;
804
b199de59 805 m_printDialogData = dialog.GetPrintDialogData();
ff910433
RR
806 return new wxGnomePrintDC( this );
807}
808
809bool wxGnomePrinter::Setup( wxWindow *parent )
810{
58c30cd8 811 return false;
ff910433
RR
812}
813
814//-----------------------------------------------------------------------------
815// wxGnomePrintDC
816//-----------------------------------------------------------------------------
817
818IMPLEMENT_CLASS(wxGnomePrintDC, wxDCBase)
819
820wxGnomePrintDC::wxGnomePrintDC( wxGnomePrinter *printer )
821{
822 m_printer = printer;
823
824 m_gpc = printer->GetPrintContext();
825
59c73be8 826 m_layout = gs_lgp->gnome_print_pango_create_layout( m_gpc );
ff910433
RR
827 m_fontdesc = pango_font_description_from_string( "Sans 12" );
828
829 m_currentRed = 0;
830 m_currentBlue = 0;
831 m_currentGreen = 0;
832
833 m_signX = 1; // default x-axis left to right
834 m_signY = -1; // default y-axis bottom up -> top down
835}
836
837wxGnomePrintDC::~wxGnomePrintDC()
838{
839}
840
841bool wxGnomePrintDC::Ok() const
842{
843 return true;
844}
845
846bool wxGnomePrintDC::DoFloodFill(wxCoord x1, wxCoord y1, const wxColour &col, int style )
847{
848 return false;
849}
850
851bool wxGnomePrintDC::DoGetPixel(wxCoord x1, wxCoord y1, wxColour *col) const
852{
853 return false;
854}
855
856void wxGnomePrintDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
857{
858 if (m_pen.GetStyle() == wxTRANSPARENT) return;
859
860 SetPen( m_pen );
861
c6efd6e0
RR
862 gs_lgp->gnome_print_moveto ( m_gpc, XLOG2DEV(x1), YLOG2DEV(y1) );
863 gs_lgp->gnome_print_lineto ( m_gpc, XLOG2DEV(x2), YLOG2DEV(y2) );
864 gs_lgp->gnome_print_stroke ( m_gpc);
cac7ac30 865
ff910433
RR
866 CalcBoundingBox( x1, y1 );
867 CalcBoundingBox( x2, y2 );
868}
869
870void wxGnomePrintDC::DoCrossHair(wxCoord x, wxCoord y)
871{
872}
873
874void wxGnomePrintDC::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord xc,wxCoord yc)
875{
876}
877
878void wxGnomePrintDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
879{
880}
881
882void wxGnomePrintDC::DoDrawPoint(wxCoord x, wxCoord y)
883{
884}
885
886void wxGnomePrintDC::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
887{
cac7ac30
RR
888 if (m_pen.GetStyle() == wxTRANSPARENT) return;
889
890 if (n <= 0) return;
891
892 SetPen (m_pen);
893
894 int i;
895 for ( i =0; i<n ; i++ )
896 CalcBoundingBox( XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset));
897
c6efd6e0 898 gs_lgp->gnome_print_moveto ( m_gpc, XLOG2DEV(points[0].x+xoffset), YLOG2DEV(points[0].y+yoffset) );
cac7ac30
RR
899
900 for (i = 1; i < n; i++)
c6efd6e0 901 gs_lgp->gnome_print_lineto ( m_gpc, XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset) );
cac7ac30 902
c6efd6e0 903 gs_lgp->gnome_print_stroke ( m_gpc);
ff910433
RR
904}
905
906void wxGnomePrintDC::DoDrawPolygon(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
907{
908}
909
910void wxGnomePrintDC::DoDrawPolyPolygon(int n, int count[], wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
911{
912}
913
914void wxGnomePrintDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
915{
916 if (m_brush.GetStyle () != wxTRANSPARENT)
917 {
918 SetBrush( m_brush );
919
c6efd6e0
RR
920 gs_lgp->gnome_print_newpath( m_gpc );
921 gs_lgp->gnome_print_moveto( m_gpc, XLOG2DEV(x), YLOG2DEV(y) );
922 gs_lgp->gnome_print_lineto( m_gpc, XLOG2DEV(x + width), YLOG2DEV(y) );
923 gs_lgp->gnome_print_lineto( m_gpc, XLOG2DEV(x + width), YLOG2DEV(y + height) );
924 gs_lgp->gnome_print_lineto( m_gpc, XLOG2DEV(x), YLOG2DEV(y + height) );
925 gs_lgp->gnome_print_closepath( m_gpc );
926 gs_lgp->gnome_print_fill( m_gpc );
ff910433
RR
927
928 CalcBoundingBox( x, y );
929 CalcBoundingBox( x + width, y + height );
930 }
931
932 if (m_pen.GetStyle () != wxTRANSPARENT)
933 {
934 SetPen (m_pen);
935
c6efd6e0
RR
936 gs_lgp->gnome_print_newpath( m_gpc );
937 gs_lgp->gnome_print_moveto( m_gpc, XLOG2DEV(x), YLOG2DEV(y) );
938 gs_lgp->gnome_print_lineto( m_gpc, XLOG2DEV(x + width), YLOG2DEV(y) );
939 gs_lgp->gnome_print_lineto( m_gpc, XLOG2DEV(x + width), YLOG2DEV(y + height) );
940 gs_lgp->gnome_print_lineto( m_gpc, XLOG2DEV(x), YLOG2DEV(y + height) );
941 gs_lgp->gnome_print_closepath( m_gpc );
942 gs_lgp->gnome_print_stroke( m_gpc );
ff910433
RR
943
944 CalcBoundingBox( x, y );
945 CalcBoundingBox( x + width, y + height );
946 }
947}
948
949void wxGnomePrintDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
950{
951}
952
953void wxGnomePrintDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
954{
cac7ac30
RR
955 if (m_brush.GetStyle () != wxTRANSPARENT)
956 {
957 SetBrush( m_brush );
958
c6efd6e0
RR
959 gs_lgp->gnome_print_newpath( m_gpc );
960 gs_lgp->gnome_print_moveto( m_gpc,
cac7ac30
RR
961 XLOG2DEV(x), YLOG2DEV(y+height/2) );
962
963 // start with top half
c6efd6e0 964 gs_lgp->gnome_print_curveto( m_gpc,
cac7ac30
RR
965 XLOG2DEV(x), YLOG2DEV(y),
966 XLOG2DEV(x+width), YLOG2DEV(y),
967 XLOG2DEV(x+width), YLOG2DEV(y+height/2) );
968 // lower half
c6efd6e0 969 gs_lgp->gnome_print_curveto( m_gpc,
cac7ac30
RR
970 XLOG2DEV(x+width), YLOG2DEV(y+height),
971 XLOG2DEV(x), YLOG2DEV(y+height),
972 XLOG2DEV(x), YLOG2DEV(y+height/2) );
973
c6efd6e0
RR
974 gs_lgp->gnome_print_closepath( m_gpc );
975 gs_lgp->gnome_print_fill( m_gpc );
cac7ac30
RR
976
977 CalcBoundingBox( x, y );
978 CalcBoundingBox( x + width, y + height );
979 }
980
981 if (m_pen.GetStyle () != wxTRANSPARENT)
982 {
983 SetPen (m_pen);
984
c6efd6e0
RR
985 gs_lgp->gnome_print_newpath( m_gpc );
986 gs_lgp->gnome_print_moveto( m_gpc,
cac7ac30
RR
987 XLOG2DEV(x), YLOG2DEV(y+height/2) );
988
989 // start with top half
c6efd6e0 990 gs_lgp->gnome_print_curveto( m_gpc,
cac7ac30
RR
991 XLOG2DEV(x), YLOG2DEV(y),
992 XLOG2DEV(x+width), YLOG2DEV(y),
993 XLOG2DEV(x+width), YLOG2DEV(y+height/2) );
994 // lower half
c6efd6e0 995 gs_lgp->gnome_print_curveto( m_gpc,
cac7ac30
RR
996 XLOG2DEV(x+width), YLOG2DEV(y+height),
997 XLOG2DEV(x), YLOG2DEV(y+height),
998 XLOG2DEV(x), YLOG2DEV(y+height/2) );
999
c6efd6e0
RR
1000 gs_lgp->gnome_print_closepath( m_gpc );
1001 gs_lgp->gnome_print_stroke( m_gpc );
cac7ac30
RR
1002
1003 CalcBoundingBox( x, y );
1004 CalcBoundingBox( x + width, y + height );
1005 }
ff910433
RR
1006}
1007
1008void wxGnomePrintDC::DoDrawSpline(wxList *points)
1009{
1010}
1011
1012bool wxGnomePrintDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
1013 wxDC *source, wxCoord xsrc, wxCoord ysrc, int rop, bool useMask,
1014 wxCoord xsrcMask, wxCoord ysrcMask)
1015{
1016 return false;
1017}
1018
1019void wxGnomePrintDC::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
1020{
e1bf3ad3 1021 DoDrawBitmap( icon, x, y, true );
ff910433
RR
1022}
1023
1024void wxGnomePrintDC::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool useMask )
1025{
e1bf3ad3
RR
1026 if (!bitmap.Ok()) return;
1027
e1bf3ad3
RR
1028 if (bitmap.HasPixbuf())
1029 {
2934005d
RR
1030 GdkPixbuf *pixbuf = bitmap.GetPixbuf();
1031 guchar *raw_image = gdk_pixbuf_get_pixels( pixbuf );
1032 bool has_alpha = gdk_pixbuf_get_has_alpha( pixbuf );
1033 int rowstride = gdk_pixbuf_get_rowstride( pixbuf );
1034 int height = gdk_pixbuf_get_height( pixbuf );
1035 int width = gdk_pixbuf_get_width( pixbuf );
1036
59c73be8 1037 gs_lgp->gnome_print_gsave( m_gpc );
2934005d
RR
1038 double matrix[6];
1039 matrix[0] = XLOG2DEVREL(width);
1040 matrix[1] = 0;
1041 matrix[2] = 0;
1042 matrix[3] = YLOG2DEVREL(height);
1043 matrix[4] = XLOG2DEV(x);
1044 matrix[5] = YLOG2DEV(y+height);
59c73be8 1045 gs_lgp->gnome_print_concat( m_gpc, matrix );
c6efd6e0 1046 gs_lgp->gnome_print_moveto( m_gpc, 0, 0 );
2934005d 1047 if (has_alpha)
59c73be8 1048 gs_lgp->gnome_print_rgbaimage( m_gpc, (guchar *)raw_image, width, height, rowstride );
2934005d 1049 else
59c73be8
RR
1050 gs_lgp->gnome_print_rgbimage( m_gpc, (guchar *)raw_image, width, height, rowstride );
1051 gs_lgp->gnome_print_grestore( m_gpc );
e1bf3ad3
RR
1052 }
1053 else
e1bf3ad3
RR
1054 {
1055 wxImage image = bitmap.ConvertToImage();
1056
1057 if (!image.Ok()) return;
2934005d 1058
59c73be8 1059 gs_lgp->gnome_print_gsave( m_gpc );
b4382784
RR
1060 double matrix[6];
1061 matrix[0] = XLOG2DEVREL(image.GetWidth());
1062 matrix[1] = 0;
1063 matrix[2] = 0;
1064 matrix[3] = YLOG2DEVREL(image.GetHeight());
1065 matrix[4] = XLOG2DEV(x);
1066 matrix[5] = YLOG2DEV(y+image.GetHeight());
59c73be8 1067 gs_lgp->gnome_print_concat( m_gpc, matrix );
c6efd6e0 1068 gs_lgp->gnome_print_moveto( m_gpc, 0, 0 );
59c73be8
RR
1069 gs_lgp->gnome_print_rgbimage( m_gpc, (guchar*) image.GetData(), image.GetWidth(), image.GetHeight(), image.GetWidth()*3 );
1070 gs_lgp->gnome_print_grestore( m_gpc );
e1bf3ad3 1071 }
ff910433
RR
1072}
1073
1074void wxGnomePrintDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y )
b199de59
RR
1075{
1076 DoDrawRotatedText( text, x, y, 0.0 );
1077}
1078
1079void wxGnomePrintDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
ff910433 1080{
ff910433
RR
1081 x = XLOG2DEV(x);
1082 y = YLOG2DEV(y);
1083
ff910433
RR
1084 bool underlined = m_font.Ok() && m_font.GetUnderlined();
1085
1086#if wxUSE_UNICODE
1087 const wxCharBuffer data = wxConvUTF8.cWC2MB( text );
1088#else
1089 const wxWCharBuffer wdata = wxConvLocal.cMB2WC( text );
1090 if ( !wdata )
1091 return;
1092 const wxCharBuffer data = wxConvUTF8.cWC2MB( wdata );
1093#endif
1094
1095 size_t datalen = strlen((const char*)data);
1096 pango_layout_set_text( m_layout, (const char*) data, datalen);
1097
1098 if (underlined)
1099 {
1100 PangoAttrList *attrs = pango_attr_list_new();
1101 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
1102 a->start_index = 0;
1103 a->end_index = datalen;
1104 pango_attr_list_insert(attrs, a);
1105 pango_layout_set_attributes(m_layout, attrs);
1106 pango_attr_list_unref(attrs);
1107 }
1108
2934005d
RR
1109 if (m_textForegroundColour.Ok())
1110 {
1111 unsigned char red = m_textForegroundColour.Red();
1112 unsigned char blue = m_textForegroundColour.Blue();
1113 unsigned char green = m_textForegroundColour.Green();
1114
b199de59
RR
1115 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1116 {
1117 double redPS = (double)(red) / 255.0;
1118 double bluePS = (double)(blue) / 255.0;
1119 double greenPS = (double)(green) / 255.0;
1120
c6efd6e0 1121 gs_lgp->gnome_print_setrgbcolor( m_gpc, redPS, greenPS, bluePS );
b199de59
RR
1122
1123 m_currentRed = red;
1124 m_currentBlue = blue;
1125 m_currentGreen = green;
1126 }
2934005d
RR
1127 }
1128
fa499247
RR
1129 int w,h;
1130
1131 if (fabs(m_scaleY - 1.0) > 0.00001)
1132 {
1133 // If there is a user or actually any scale applied to
1134 // the device context, scale the font.
1135
1136 // scale font description
1137 gint oldSize = pango_font_description_get_size( m_fontdesc );
1138 double size = oldSize;
1139 size = size * m_scaleY;
1140 pango_font_description_set_size( m_fontdesc, (gint)size );
1141
1142 // actually apply scaled font
1143 pango_layout_set_font_description( m_layout, m_fontdesc );
1144
1145 pango_layout_get_pixel_size( m_layout, &w, &h );
1146#if 0
1147 if ( m_backgroundMode == wxSOLID )
1148 {
1149 gdk_gc_set_foreground(m_textGC, m_textBackgroundColour.GetColor());
1150 gdk_draw_rectangle(m_window, m_textGC, TRUE, x, y, w, h);
1151 gdk_gc_set_foreground(m_textGC, m_textForegroundColour.GetColor());
1152 }
1153#endif
1154 // Draw layout.
c6efd6e0 1155 gs_lgp->gnome_print_moveto (m_gpc, x, y);
b199de59
RR
1156 if (fabs(angle) > 0.00001)
1157 {
59c73be8
RR
1158 gs_lgp->gnome_print_gsave( m_gpc );
1159 gs_lgp->gnome_print_rotate( m_gpc, angle );
1160 gs_lgp->gnome_print_pango_layout( m_gpc, m_layout );
1161 gs_lgp->gnome_print_grestore( m_gpc );
b199de59
RR
1162 }
1163 else
1164 {
59c73be8 1165 gs_lgp->gnome_print_pango_layout( m_gpc, m_layout );
b199de59 1166 }
fa499247
RR
1167
1168 // reset unscaled size
1169 pango_font_description_set_size( m_fontdesc, oldSize );
1170
1171 // actually apply unscaled font
1172 pango_layout_set_font_description( m_layout, m_fontdesc );
1173 }
1174 else
1175 {
1176 pango_layout_get_pixel_size( m_layout, &w, &h );
1177#if 0
1178 if ( m_backgroundMode == wxSOLID )
1179 {
1180 gdk_gc_set_foreground(m_textGC, m_textBackgroundColour.GetColor());
1181 gdk_draw_rectangle(m_window, m_textGC, TRUE, x, y, w, h);
1182 gdk_gc_set_foreground(m_textGC, m_textForegroundColour.GetColor());
1183 }
1184#endif
1185 // Draw layout.
c6efd6e0 1186 gs_lgp->gnome_print_moveto (m_gpc, x, y);
b199de59
RR
1187 if (fabs(angle) > 0.00001)
1188 {
59c73be8
RR
1189 gs_lgp->gnome_print_gsave( m_gpc );
1190 gs_lgp->gnome_print_rotate( m_gpc, angle );
1191 gs_lgp->gnome_print_pango_layout( m_gpc, m_layout );
1192 gs_lgp->gnome_print_grestore( m_gpc );
b199de59
RR
1193 }
1194 else
1195 {
59c73be8 1196 gs_lgp->gnome_print_pango_layout( m_gpc, m_layout );
b199de59 1197 }
fa499247
RR
1198 }
1199
ff910433
RR
1200 if (underlined)
1201 {
1202 // undo underline attributes setting:
1203 pango_layout_set_attributes(m_layout, NULL);
1204 }
1205
b199de59 1206 CalcBoundingBox (x + w, y + h);
ff910433
RR
1207}
1208
1209void wxGnomePrintDC::Clear()
1210{
1211}
1212
1213void wxGnomePrintDC::SetFont( const wxFont& font )
1214{
1215 m_font = font;
1216
1217 if (m_font.Ok())
1218 {
1219 if (m_fontdesc)
1220 pango_font_description_free( m_fontdesc );
1221
1222 m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description );
1223
1224 pango_layout_set_font_description( m_layout, m_fontdesc );
1225 }
1226}
1227
1228void wxGnomePrintDC::SetPen( const wxPen& pen )
1229{
1230 if (!pen.Ok()) return;
1231
ff910433
RR
1232 m_pen = pen;
1233
c6efd6e0 1234 gs_lgp->gnome_print_setlinewidth( m_gpc, XLOG2DEVREL( 1000 * m_pen.GetWidth() ) / 1000.0f );
cac7ac30
RR
1235
1236 static const double dotted[] = {2.0, 5.0};
1237 static const double short_dashed[] = {4.0, 4.0};
1238 static const double wxCoord_dashed[] = {4.0, 8.0};
1239 static const double dotted_dashed[] = {6.0, 6.0, 2.0, 6.0};
1240
1241 switch (m_pen.GetStyle())
1242 {
59c73be8
RR
1243 case wxDOT: gs_lgp->gnome_print_setdash( m_gpc, 2, dotted, 0 ); break;
1244 case wxSHORT_DASH: gs_lgp->gnome_print_setdash( m_gpc, 2, short_dashed, 0 ); break;
1245 case wxLONG_DASH: gs_lgp->gnome_print_setdash( m_gpc, 2, wxCoord_dashed, 0 ); break;
1246 case wxDOT_DASH: gs_lgp->gnome_print_setdash( m_gpc, 4, dotted_dashed, 0 ); break;
cac7ac30
RR
1247 case wxSOLID:
1248 case wxTRANSPARENT:
59c73be8 1249 default: gs_lgp->gnome_print_setdash( m_gpc, 0, NULL, 0 ); break;
cac7ac30
RR
1250 }
1251
1252
ff910433
RR
1253 unsigned char red = m_pen.GetColour().Red();
1254 unsigned char blue = m_pen.GetColour().Blue();
1255 unsigned char green = m_pen.GetColour().Green();
1256
1257 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1258 {
1259 double redPS = (double)(red) / 255.0;
1260 double bluePS = (double)(blue) / 255.0;
1261 double greenPS = (double)(green) / 255.0;
1262
c6efd6e0 1263 gs_lgp->gnome_print_setrgbcolor( m_gpc, redPS, greenPS, bluePS );
ff910433
RR
1264
1265 m_currentRed = red;
1266 m_currentBlue = blue;
1267 m_currentGreen = green;
1268 }
1269}
1270
1271void wxGnomePrintDC::SetBrush( const wxBrush& brush )
1272{
fa499247
RR
1273 if (!brush.Ok()) return;
1274
1275 m_brush = brush;
1276
1277 // Brush colour
1278 unsigned char red = m_brush.GetColour().Red();
1279 unsigned char blue = m_brush.GetColour().Blue();
1280 unsigned char green = m_brush.GetColour().Green();
1281
1282 if (!m_colour)
1283 {
1284 // Anything not white is black
1285 if (! (red == (unsigned char) 255 &&
1286 blue == (unsigned char) 255 &&
1287 green == (unsigned char) 255) )
1288 {
1289 red = (unsigned char) 0;
1290 green = (unsigned char) 0;
1291 blue = (unsigned char) 0;
1292 }
1293 // setgray here ?
1294 }
1295
1296 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1297 {
1298 double redPS = (double)(red) / 255.0;
1299 double bluePS = (double)(blue) / 255.0;
1300 double greenPS = (double)(green) / 255.0;
1301
c6efd6e0 1302 gs_lgp->gnome_print_setrgbcolor( m_gpc, redPS, greenPS, bluePS );
fa499247
RR
1303
1304 m_currentRed = red;
1305 m_currentBlue = blue;
1306 m_currentGreen = green;
1307 }
ff910433
RR
1308}
1309
1310void wxGnomePrintDC::SetLogicalFunction( int function )
1311{
1312}
1313
1314void wxGnomePrintDC::SetBackground( const wxBrush& brush )
1315{
1316}
1317
1318void wxGnomePrintDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1319{
1320}
1321
1322void wxGnomePrintDC::DestroyClippingRegion()
1323{
1324}
1325
1326bool wxGnomePrintDC::StartDoc(const wxString& message)
1327{
1328 SetDeviceOrigin( 0,0 );
1329
1330 return true;
1331}
1332
1333void wxGnomePrintDC::EndDoc()
1334{
59c73be8 1335 gs_lgp->gnome_print_end_doc( m_gpc );
ff910433
RR
1336}
1337
1338void wxGnomePrintDC::StartPage()
1339{
59c73be8 1340 gs_lgp->gnome_print_beginpage( m_gpc, (const guchar*) "page" );
ff910433
RR
1341}
1342
1343void wxGnomePrintDC::EndPage()
1344{
59c73be8 1345 gs_lgp->gnome_print_showpage( m_gpc );
ff910433
RR
1346}
1347
1348wxCoord wxGnomePrintDC::GetCharHeight() const
1349{
b199de59
RR
1350 pango_layout_set_text( m_layout, "H", 1 );
1351
1352 int w,h;
1353 pango_layout_get_pixel_size( m_layout, &w, &h );
1354
1355 return h;
ff910433
RR
1356}
1357
1358wxCoord wxGnomePrintDC::GetCharWidth() const
1359{
b199de59
RR
1360 pango_layout_set_text( m_layout, "H", 1 );
1361
1362 int w,h;
1363 pango_layout_get_pixel_size( m_layout, &w, &h );
1364
1365 return w;
ff910433
RR
1366}
1367
981a6af1 1368void wxGnomePrintDC::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoord *height,
ff910433
RR
1369 wxCoord *descent,
1370 wxCoord *externalLeading,
1371 wxFont *theFont ) const
1372{
981a6af1
RR
1373 if ( width )
1374 *width = 0;
1375 if ( height )
1376 *height = 0;
1377 if ( descent )
1378 *descent = 0;
1379 if ( externalLeading )
1380 *externalLeading = 0;
1381
1382 if (string.IsEmpty())
1383 {
1384 return;
1385 }
1386
1387 // Set new font description
1388 if (theFont)
1389 pango_layout_set_font_description( m_layout, theFont->GetNativeFontInfo()->description );
1390
1391 // Set layout's text
1392#if wxUSE_UNICODE
1393 const wxCharBuffer data = wxConvUTF8.cWC2MB( string );
1394 const char *dataUTF8 = (const char *)data;
1395#else
1396 const wxWCharBuffer wdata = wxConvLocal.cMB2WC( string );
1397 if ( !wdata )
1398 {
1399 if (width) (*width) = 0;
1400 if (height) (*height) = 0;
1401 return;
1402 }
1403 const wxCharBuffer data = wxConvUTF8.cWC2MB( wdata );
1404 const char *dataUTF8 = (const char *)data;
1405#endif
1406
1407 if ( !dataUTF8 )
1408 {
1409 // hardly ideal, but what else can we do if conversion failed?
1410 return;
1411 }
1412
1413 pango_layout_set_text( m_layout, dataUTF8, strlen(dataUTF8) );
1414
1415 int w,h;
1416 pango_layout_get_pixel_size( m_layout, &w, &h );
1417
1418 if (width)
15236815 1419 *width = (wxCoord)(w / m_scaleX);
981a6af1 1420 if (height)
15236815 1421 *height = (wxCoord)(h / m_scaleY);
981a6af1
RR
1422 if (descent)
1423 {
1424 PangoLayoutIter *iter = pango_layout_get_iter(m_layout);
1425 int baseline = pango_layout_iter_get_baseline(iter);
1426 pango_layout_iter_free(iter);
1427 *descent = h - PANGO_PIXELS(baseline);
1428 }
1429
1430 // Reset old font description
1431 if (theFont)
1432 pango_layout_set_font_description( m_layout, m_fontdesc );
ff910433
RR
1433}
1434
1435void wxGnomePrintDC::DoGetSize(int* width, int* height) const
1436{
cffcf831
RR
1437 wxGnomePrintNativeData *native =
1438 (wxGnomePrintNativeData*) m_printData.GetNativeData();
1439
1440 // Query page size. This seems to omit the margins
1441 // right now, although it shouldn't
1442 double pw,ph;
59c73be8 1443 gs_lgp->gnome_print_job_get_page_size( native->GetPrintJob(), &pw, &ph );
ff910433 1444
ff910433 1445 if (width)
cffcf831 1446 *width = (int) (pw + 0.5);
ff910433 1447 if (height)
cffcf831 1448 *height = (int) (ph + 0.5);
ff910433
RR
1449}
1450
1451void wxGnomePrintDC::DoGetSizeMM(int *width, int *height) const
1452{
cffcf831
RR
1453 wxGnomePrintNativeData *native =
1454 (wxGnomePrintNativeData*) m_printData.GetNativeData();
1455
1456 // This code assumes values in Pts.
1457
1458 double pw,ph;
59c73be8 1459 gs_lgp->gnome_print_job_get_page_size( native->GetPrintJob(), &pw, &ph );
cffcf831
RR
1460
1461 // Convert to mm.
ff910433 1462
59c73be8
RR
1463 const GnomePrintUnit *mm_unit = gs_lgp->gnome_print_unit_get_by_abbreviation( (const guchar*) "mm" );
1464 const GnomePrintUnit *pts_unit = gs_lgp->gnome_print_unit_get_by_abbreviation( (const guchar*) "Pts" );
1465 gs_lgp->gnome_print_convert_distance( &pw, pts_unit, mm_unit );
1466 gs_lgp->gnome_print_convert_distance( &ph, pts_unit, mm_unit );
ff910433
RR
1467
1468 if (width)
cffcf831 1469 *width = (int) (pw + 0.5);
ff910433 1470 if (height)
cffcf831 1471 *height = (int) (ph + 0.5);
ff910433
RR
1472}
1473
1474wxSize wxGnomePrintDC::GetPPI() const
1475{
1476 return wxSize(72,72);
1477}
1478
1479void wxGnomePrintDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
1480{
1481 m_signX = (xLeftRight ? 1 : -1);
1482 m_signY = (yBottomUp ? 1 : -1);
1483
1484 ComputeScaleAndOrigin();
1485}
1486
1487void wxGnomePrintDC::SetDeviceOrigin( wxCoord x, wxCoord y )
1488{
1489 int h = 0;
1490 int w = 0;
1491 GetSize( &w, &h );
1492
1493 wxDC::SetDeviceOrigin( x, h-y );
1494}
1495
1496void wxGnomePrintDC::SetResolution(int ppi)
1497{
1498}
1499
1500int wxGnomePrintDC::GetResolution()
1501{
1502 return 72;
1503}
7c72311f 1504
58c30cd8
RR
1505
1506class wxGnomePrintModule: public wxModule
1507{
1508public:
1509 wxGnomePrintModule() {}
c6efd6e0
RR
1510 bool OnInit();
1511 void OnExit();
58c30cd8
RR
1512
1513private:
1514 DECLARE_DYNAMIC_CLASS(wxGnomePrintModule)
1515};
1516
c6efd6e0
RR
1517bool wxGnomePrintModule::OnInit()
1518{
1519 gs_lgp = new wxGnomePrintLibrary;
1520 if (gs_lgp->IsOk())
1521 wxPrintFactory::SetPrintFactory( new wxGnomePrintFactory );
1522 return true;
1523}
1524
1525void wxGnomePrintModule::OnExit()
1526{
1527 delete gs_lgp;
1528}
1529
58c30cd8 1530IMPLEMENT_DYNAMIC_CLASS(wxGnomePrintModule, wxModule)
06cfd325 1531
7c72311f
RR
1532#endif
1533 // wxUSE_LIBGNOMEPRINT