]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/gnome/gprint.cpp
Added --with-gnomeprint and its wxUSE_LIBGNOMEPRINT
[wxWidgets.git] / src / gtk / gnome / gprint.cpp
1 /////////////////////////////////////////////////////////////////////////////
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
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "gprint.cpp"
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
21 #include "wx/gtk/gnome/gprint.h"
22
23 #if wxUSE_LIBGNOMEPRINT
24
25 #include "wx/fontutil.h"
26 #include "wx/printdlg.h"
27 #include "wx/gtk/private.h"
28
29 #include <libgnomeprint/gnome-print.h>
30 #include <libgnomeprint/gnome-print-pango.h>
31 #include <libgnomeprintui/gnome-print-dialog.h>
32
33 //----------------------------------------------------------------------------
34 // wxGnomePrintNativeData
35 //----------------------------------------------------------------------------
36
37 IMPLEMENT_CLASS(wxGnomePrintNativeData, wxPrintNativeDataBase)
38
39 wxGnomePrintNativeData::wxGnomePrintNativeData()
40 {
41 m_config = gnome_print_config_default();
42 m_job = gnome_print_job_new( m_config );
43 }
44
45 wxGnomePrintNativeData::~wxGnomePrintNativeData()
46 {
47 g_object_unref (G_OBJECT (m_config));
48 g_object_unref (G_OBJECT (m_job));
49 }
50
51 bool wxGnomePrintNativeData::TransferTo( wxPrintData &data )
52 {
53 // TODO
54 return true;
55 }
56
57 bool wxGnomePrintNativeData::TransferFrom( const wxPrintData &data )
58 {
59 // TODO
60 return true;
61 }
62
63 //----------------------------------------------------------------------------
64 // wxGnomePrintFactory
65 //----------------------------------------------------------------------------
66
67 wxPrinterBase* wxGnomePrintFactory::CreatePrinter( wxPrintDialogData *data )
68 {
69 return new wxGnomePrinter( data );
70 }
71
72 wxPrintPreviewBase *wxGnomePrintFactory::CreatePrintPreview( wxPrintout *preview,
73 wxPrintout *printout,
74 wxPrintDialogData *data )
75 {
76 return new wxPostScriptPrintPreview( preview, printout, data );
77 }
78
79 wxPrintPreviewBase *wxGnomePrintFactory::CreatePrintPreview( wxPrintout *preview,
80 wxPrintout *printout,
81 wxPrintData *data )
82 {
83 return new wxPostScriptPrintPreview( preview, printout, data );
84 }
85
86 wxPrintDialogBase *wxGnomePrintFactory::CreatePrintDialog( wxWindow *parent,
87 wxPrintDialogData *data )
88 {
89 return new wxGenericPrintDialog( parent, data );
90 }
91
92 wxPrintDialogBase *wxGnomePrintFactory::CreatePrintDialog( wxWindow *parent,
93 wxPrintData *data )
94 {
95 return new wxGenericPrintDialog( parent, data );
96 }
97
98 bool wxGnomePrintFactory::HasPrintSetupDialog()
99 {
100 return true;
101 }
102
103 wxDialog *wxGnomePrintFactory::CreatePrintSetupDialog( wxWindow *parent, wxPrintData *data )
104 {
105 return new wxGnomePrintSetupDialog( parent, data );
106 }
107
108 bool wxGnomePrintFactory::HasOwnPrintToFile()
109 {
110 return true;
111 }
112
113 bool wxGnomePrintFactory::HasPrinterLine()
114 {
115 return true;
116 }
117
118 wxString wxGnomePrintFactory::CreatePrinterLine()
119 {
120 // We should query "gnome_config_default" here
121 return _("GNOME print");
122 }
123
124 bool wxGnomePrintFactory::HasStatusLine()
125 {
126 return true;
127 }
128
129 wxString wxGnomePrintFactory::CreateStatusLine()
130 {
131 // We should query "gnome_config_default" here
132 return _("Ready");
133 }
134
135 wxPrintNativeDataBase *wxGnomePrintFactory::CreatePrintNativeData()
136 {
137 return new wxGnomePrintNativeData;
138 }
139
140 //----------------------------------------------------------------------------
141 // wxGnomePrintSetupDialog
142 //----------------------------------------------------------------------------
143
144 IMPLEMENT_CLASS(wxGnomePrintSetupDialog, wxDialog)
145
146 wxGnomePrintSetupDialog::wxGnomePrintSetupDialog( wxWindow *parent, wxPrintData *data )
147 {
148 wxGnomePrintNativeData *native =
149 (wxGnomePrintNativeData*) data->GetNativeData();
150
151 m_widget = gnome_print_dialog_new (native->GetPrintJob(), (guchar*)"Print setup", 0);
152 }
153
154 wxGnomePrintSetupDialog::~wxGnomePrintSetupDialog()
155 {
156 m_widget = NULL;
157 }
158
159 int wxGnomePrintSetupDialog::ShowModal()
160 {
161 int response = gtk_dialog_run (GTK_DIALOG (m_widget));
162 gtk_widget_destroy(m_widget);
163 m_widget = NULL;
164
165 if (response == GNOME_PRINT_DIALOG_RESPONSE_CANCEL)
166 return wxID_CANCEL;
167
168 return wxID_OK;
169 }
170
171 bool wxGnomePrintSetupDialog::Validate()
172 {
173 return true;
174 }
175
176 bool wxGnomePrintSetupDialog::TransferDataToWindow()
177 {
178 return true;
179 }
180
181 bool wxGnomePrintSetupDialog::TransferDataFromWindow()
182 {
183 return true;
184 }
185
186
187 //----------------------------------------------------------------------------
188 // wxGnomePrinter
189 //----------------------------------------------------------------------------
190
191 IMPLEMENT_CLASS(wxGnomePrinter, wxPrinterBase)
192
193 wxGnomePrinter::wxGnomePrinter( wxPrintDialogData *data ) :
194 wxPrinterBase( data )
195 {
196 m_gpc = NULL;
197 }
198
199 wxGnomePrinter::~wxGnomePrinter()
200 {
201 }
202
203 bool wxGnomePrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt )
204 {
205 if (!printout)
206 {
207 sm_lastError = wxPRINTER_ERROR;
208 return false;
209 }
210
211 wxPrintData printdata = GetPrintDialogData().GetPrintData();
212 wxGnomePrintNativeData *data =
213 (wxGnomePrintNativeData*) printdata.GetNativeData();
214
215 GnomePrintJob *job = data->GetPrintJob();
216 m_gpc = gnome_print_job_get_context (job);
217
218 wxDC *dc;
219
220 if (prompt)
221 dc = PrintDialog( parent );
222 else
223 dc = new wxGnomePrintDC( this );
224
225 if (!dc)
226 {
227 gnome_print_job_close( job );
228 return false;
229 }
230
231 printout->SetDC(dc);
232
233 printout->OnPreparePrinting();
234 printout->OnBeginPrinting();
235
236 if (!printout->OnBeginDocument(0, 0))
237 {
238 sm_lastError = wxPRINTER_ERROR;
239 }
240 else
241 {
242 int pn;
243 for (pn = 1; pn <= 2; pn++)
244 {
245 dc->StartPage();
246 printout->OnPrintPage(pn);
247 dc->EndPage();
248 }
249
250 printout->OnEndDocument();
251 printout->OnEndPrinting();
252 }
253
254 gnome_print_job_close( job );
255 gnome_print_job_print( job );
256
257 delete dc;
258
259 return (sm_lastError == wxPRINTER_NO_ERROR);
260 }
261
262 wxDC* wxGnomePrinter::PrintDialog( wxWindow *parent )
263 {
264 wxPrintDialog dialog( parent, &m_printDialogData );
265 if (dialog.ShowModal() == wxID_CANCEL)
266 {
267 sm_lastError = wxPRINTER_ERROR;
268 return NULL;
269 }
270
271 return new wxGnomePrintDC( this );
272 }
273
274 bool wxGnomePrinter::Setup( wxWindow *parent )
275 {
276 }
277
278 //-----------------------------------------------------------------------------
279 // wxGnomePrintDC
280 //-----------------------------------------------------------------------------
281
282 IMPLEMENT_CLASS(wxGnomePrintDC, wxDCBase)
283
284 wxGnomePrintDC::wxGnomePrintDC( wxGnomePrinter *printer )
285 {
286 m_printer = printer;
287
288 m_gpc = printer->GetPrintContext();
289
290 m_layout = gnome_print_pango_create_layout( m_gpc );
291 m_fontdesc = pango_font_description_from_string( "Sans 12" );
292
293 m_currentRed = 0;
294 m_currentBlue = 0;
295 m_currentGreen = 0;
296
297 m_signX = 1; // default x-axis left to right
298 m_signY = -1; // default y-axis bottom up -> top down
299 }
300
301 wxGnomePrintDC::~wxGnomePrintDC()
302 {
303 }
304
305 bool wxGnomePrintDC::Ok() const
306 {
307 return true;
308 }
309
310 bool wxGnomePrintDC::DoFloodFill(wxCoord x1, wxCoord y1, const wxColour &col, int style )
311 {
312 return false;
313 }
314
315 bool wxGnomePrintDC::DoGetPixel(wxCoord x1, wxCoord y1, wxColour *col) const
316 {
317 return false;
318 }
319
320 void wxGnomePrintDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
321 {
322 if (m_pen.GetStyle() == wxTRANSPARENT) return;
323
324 SetPen( m_pen );
325
326 gnome_print_moveto ( m_gpc, XDEV2LOG(x1), YDEV2LOG(y1) );
327 gnome_print_lineto ( m_gpc, XDEV2LOG(x2), YDEV2LOG(y2) );
328 gnome_print_stroke ( m_gpc);
329
330 CalcBoundingBox( x1, y1 );
331 CalcBoundingBox( x2, y2 );
332 }
333
334 void wxGnomePrintDC::DoCrossHair(wxCoord x, wxCoord y)
335 {
336 }
337
338 void wxGnomePrintDC::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord xc,wxCoord yc)
339 {
340 }
341
342 void wxGnomePrintDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
343 {
344 }
345
346 void wxGnomePrintDC::DoDrawPoint(wxCoord x, wxCoord y)
347 {
348 }
349
350 void wxGnomePrintDC::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
351 {
352 }
353
354 void wxGnomePrintDC::DoDrawPolygon(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
355 {
356 }
357
358 void wxGnomePrintDC::DoDrawPolyPolygon(int n, int count[], wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
359 {
360 }
361
362 void wxGnomePrintDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
363 {
364 if (m_brush.GetStyle () != wxTRANSPARENT)
365 {
366 SetBrush( m_brush );
367
368 gnome_print_newpath( m_gpc );
369 gnome_print_moveto( m_gpc, XLOG2DEV(x), YLOG2DEV(y) );
370 gnome_print_lineto( m_gpc, XLOG2DEV(x + width), YLOG2DEV(y) );
371 gnome_print_lineto( m_gpc, XLOG2DEV(x + width), YLOG2DEV(y + height) );
372 gnome_print_lineto( m_gpc, XLOG2DEV(x), YLOG2DEV(y + height) );
373 gnome_print_closepath( m_gpc );
374 gnome_print_fill( m_gpc );
375
376 CalcBoundingBox( x, y );
377 CalcBoundingBox( x + width, y + height );
378 }
379
380 if (m_pen.GetStyle () != wxTRANSPARENT)
381 {
382 SetPen (m_pen);
383
384 gnome_print_newpath( m_gpc );
385 gnome_print_moveto( m_gpc, XLOG2DEV(x), YLOG2DEV(y) );
386 gnome_print_lineto( m_gpc, XLOG2DEV(x + width), YLOG2DEV(y) );
387 gnome_print_lineto( m_gpc, XLOG2DEV(x + width), YLOG2DEV(y + height) );
388 gnome_print_lineto( m_gpc, XLOG2DEV(x), YLOG2DEV(y + height) );
389 gnome_print_closepath( m_gpc );
390 gnome_print_stroke( m_gpc );
391
392 CalcBoundingBox( x, y );
393 CalcBoundingBox( x + width, y + height );
394 }
395 }
396
397 void wxGnomePrintDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
398 {
399 }
400
401 void wxGnomePrintDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
402 {
403 }
404
405 void wxGnomePrintDC::DoDrawSpline(wxList *points)
406 {
407 }
408
409 bool wxGnomePrintDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
410 wxDC *source, wxCoord xsrc, wxCoord ysrc, int rop, bool useMask,
411 wxCoord xsrcMask, wxCoord ysrcMask)
412 {
413 return false;
414 }
415
416 void wxGnomePrintDC::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
417 {
418 }
419
420 void wxGnomePrintDC::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool useMask )
421 {
422 }
423
424 void wxGnomePrintDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y )
425 {
426 if (m_textForegroundColour.Ok())
427 {
428 unsigned char red = m_textForegroundColour.Red();
429 unsigned char blue = m_textForegroundColour.Blue();
430 unsigned char green = m_textForegroundColour.Green();
431
432 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
433 {
434 double redPS = (double)(red) / 255.0;
435 double bluePS = (double)(blue) / 255.0;
436 double greenPS = (double)(green) / 255.0;
437
438 gnome_print_setrgbcolor( m_gpc, redPS, bluePS, greenPS );
439
440 m_currentRed = red;
441 m_currentBlue = blue;
442 m_currentGreen = green;
443 }
444 }
445
446 x = XLOG2DEV(x);
447 y = YLOG2DEV(y);
448
449 wxPrintf( wxT("x,y: %d,%d\n"), x, y );
450
451 bool underlined = m_font.Ok() && m_font.GetUnderlined();
452
453 #if wxUSE_UNICODE
454 const wxCharBuffer data = wxConvUTF8.cWC2MB( text );
455 #else
456 const wxWCharBuffer wdata = wxConvLocal.cMB2WC( text );
457 if ( !wdata )
458 return;
459 const wxCharBuffer data = wxConvUTF8.cWC2MB( wdata );
460 #endif
461
462 size_t datalen = strlen((const char*)data);
463 pango_layout_set_text( m_layout, (const char*) data, datalen);
464
465 if (underlined)
466 {
467 PangoAttrList *attrs = pango_attr_list_new();
468 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
469 a->start_index = 0;
470 a->end_index = datalen;
471 pango_attr_list_insert(attrs, a);
472 pango_layout_set_attributes(m_layout, attrs);
473 pango_attr_list_unref(attrs);
474 }
475
476 gnome_print_moveto (m_gpc, x, y);
477 gnome_print_pango_layout( m_gpc, m_layout );
478
479 if (underlined)
480 {
481 // undo underline attributes setting:
482 pango_layout_set_attributes(m_layout, NULL);
483 }
484
485 // CalcBoundingBox (x + width, y + height);
486 CalcBoundingBox (x, y);
487 }
488
489 void wxGnomePrintDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
490 {
491 }
492
493 void wxGnomePrintDC::Clear()
494 {
495 }
496
497 void wxGnomePrintDC::SetFont( const wxFont& font )
498 {
499 m_font = font;
500
501 if (m_font.Ok())
502 {
503 if (m_fontdesc)
504 pango_font_description_free( m_fontdesc );
505
506 m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description );
507
508 pango_layout_set_font_description( m_layout, m_fontdesc );
509 }
510 }
511
512 void wxGnomePrintDC::SetPen( const wxPen& pen )
513 {
514 if (!pen.Ok()) return;
515
516 int oldStyle = m_pen.GetStyle();
517
518 m_pen = pen;
519
520 gnome_print_setlinewidth( m_gpc, XLOG2DEVREL( 1000 * m_pen.GetWidth() ) / 1000.0f );
521
522 unsigned char red = m_pen.GetColour().Red();
523 unsigned char blue = m_pen.GetColour().Blue();
524 unsigned char green = m_pen.GetColour().Green();
525
526 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
527 {
528 double redPS = (double)(red) / 255.0;
529 double bluePS = (double)(blue) / 255.0;
530 double greenPS = (double)(green) / 255.0;
531
532 gnome_print_setrgbcolor( m_gpc, redPS, bluePS, greenPS );
533
534 m_currentRed = red;
535 m_currentBlue = blue;
536 m_currentGreen = green;
537 }
538 }
539
540 void wxGnomePrintDC::SetBrush( const wxBrush& brush )
541 {
542 }
543
544 void wxGnomePrintDC::SetLogicalFunction( int function )
545 {
546 }
547
548 void wxGnomePrintDC::SetBackground( const wxBrush& brush )
549 {
550 }
551
552 void wxGnomePrintDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
553 {
554 }
555
556 void wxGnomePrintDC::DestroyClippingRegion()
557 {
558 }
559
560 bool wxGnomePrintDC::StartDoc(const wxString& message)
561 {
562 SetDeviceOrigin( 0,0 );
563
564 return true;
565 }
566
567 void wxGnomePrintDC::EndDoc()
568 {
569 }
570
571 void wxGnomePrintDC::StartPage()
572 {
573 gnome_print_beginpage( m_gpc, (const guchar*) "1" );
574 }
575
576 void wxGnomePrintDC::EndPage()
577 {
578 gnome_print_showpage( m_gpc );
579 }
580
581 wxCoord wxGnomePrintDC::GetCharHeight() const
582 {
583 return 0;
584 }
585
586 wxCoord wxGnomePrintDC::GetCharWidth() const
587 {
588 return 0;
589 }
590
591 void wxGnomePrintDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
592 wxCoord *descent,
593 wxCoord *externalLeading,
594 wxFont *theFont ) const
595 {
596 }
597
598 void wxGnomePrintDC::DoGetSize(int* width, int* height) const
599 {
600 // No idea if that is efficient
601 GnomePrintConfig *config = gnome_print_config_default();
602
603 double w,h;
604 bool result = gnome_print_config_get_page_size( config, &w, &h );
605
606 if (!result)
607 {
608 // Standard PS resolution DIN A4 size.
609 w = 595.0;
610 h = 842.0;
611 }
612
613 if (width)
614 *width = (int) w;
615 if (height)
616 *height = (int) h;
617
618 wxPrintf( wxT("size %d,%d\n"), *width, *height );
619 }
620
621 void wxGnomePrintDC::DoGetSizeMM(int *width, int *height) const
622 {
623 double w,h;
624
625 /// Later, for now DIN A4
626 w = 210.0;
627 h = 297.0;
628
629 if (width)
630 *width = (int) w;
631 if (height)
632 *height = (int) h;
633 }
634
635 wxSize wxGnomePrintDC::GetPPI() const
636 {
637 return wxSize(72,72);
638 }
639
640 void wxGnomePrintDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
641 {
642 m_signX = (xLeftRight ? 1 : -1);
643 m_signY = (yBottomUp ? 1 : -1);
644
645 ComputeScaleAndOrigin();
646 }
647
648 void wxGnomePrintDC::SetDeviceOrigin( wxCoord x, wxCoord y )
649 {
650 int h = 0;
651 int w = 0;
652 GetSize( &w, &h );
653
654 wxDC::SetDeviceOrigin( x, h-y );
655 }
656
657 void wxGnomePrintDC::SetResolution(int ppi)
658 {
659 }
660
661 int wxGnomePrintDC::GetResolution()
662 {
663 return 72;
664 }
665
666 #endif
667 // wxUSE_LIBGNOMEPRINT