1 /////////////////////////////////////////////////////////////////////////////
3 // Author: Robert Roebling
4 // Purpose: Implement GNOME printing support
6 // Copyright: Robert Roebling
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "gprint.h"
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
21 #include "wx/gtk/gnome/gprint.h"
23 #if wxUSE_LIBGNOMEPRINT
25 #include "wx/fontutil.h"
26 #include "wx/printdlg.h"
27 #include "wx/gtk/private.h"
29 #include <libgnomeprint/gnome-print.h>
30 #include <libgnomeprint/gnome-print-pango.h>
31 #include <libgnomeprintui/gnome-print-dialog.h>
33 //----------------------------------------------------------------------------
34 // wxGnomePrintNativeData
35 //----------------------------------------------------------------------------
37 IMPLEMENT_CLASS(wxGnomePrintNativeData
, wxPrintNativeDataBase
)
39 wxGnomePrintNativeData::wxGnomePrintNativeData()
41 m_config
= gnome_print_config_default();
42 m_job
= gnome_print_job_new( m_config
);
45 wxGnomePrintNativeData::~wxGnomePrintNativeData()
47 g_object_unref (G_OBJECT (m_config
));
48 g_object_unref (G_OBJECT (m_job
));
51 bool wxGnomePrintNativeData::TransferTo( wxPrintData
&data
)
57 bool wxGnomePrintNativeData::TransferFrom( const wxPrintData
&data
)
63 //----------------------------------------------------------------------------
64 // wxGnomePrintFactory
65 //----------------------------------------------------------------------------
67 wxPrinterBase
* wxGnomePrintFactory::CreatePrinter( wxPrintDialogData
*data
)
69 return new wxGnomePrinter( data
);
72 wxPrintPreviewBase
*wxGnomePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
74 wxPrintDialogData
*data
)
76 return new wxPostScriptPrintPreview( preview
, printout
, data
);
79 wxPrintPreviewBase
*wxGnomePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
83 return new wxPostScriptPrintPreview( preview
, printout
, data
);
86 wxPrintDialogBase
*wxGnomePrintFactory::CreatePrintDialog( wxWindow
*parent
,
87 wxPrintDialogData
*data
)
89 return new wxGenericPrintDialog( parent
, data
);
92 wxPrintDialogBase
*wxGnomePrintFactory::CreatePrintDialog( wxWindow
*parent
,
95 return new wxGenericPrintDialog( parent
, data
);
98 bool wxGnomePrintFactory::HasPrintSetupDialog()
103 wxDialog
*wxGnomePrintFactory::CreatePrintSetupDialog( wxWindow
*parent
, wxPrintData
*data
)
105 return new wxGnomePrintSetupDialog( parent
, data
);
108 bool wxGnomePrintFactory::HasOwnPrintToFile()
113 bool wxGnomePrintFactory::HasPrinterLine()
118 wxString
wxGnomePrintFactory::CreatePrinterLine()
120 // We should query "gnome_config_default" here
121 return _("GNOME print");
124 bool wxGnomePrintFactory::HasStatusLine()
129 wxString
wxGnomePrintFactory::CreateStatusLine()
131 // We should query "gnome_config_default" here
135 wxPrintNativeDataBase
*wxGnomePrintFactory::CreatePrintNativeData()
137 return new wxGnomePrintNativeData
;
140 //----------------------------------------------------------------------------
141 // wxGnomePrintSetupDialog
142 //----------------------------------------------------------------------------
144 IMPLEMENT_CLASS(wxGnomePrintSetupDialog
, wxDialog
)
146 wxGnomePrintSetupDialog::wxGnomePrintSetupDialog( wxWindow
*parent
, wxPrintData
*data
)
148 wxGnomePrintNativeData
*native
=
149 (wxGnomePrintNativeData
*) data
->GetNativeData();
151 m_widget
= gnome_print_dialog_new (native
->GetPrintJob(), (guchar
*)"Print setup", 0);
154 wxGnomePrintSetupDialog::~wxGnomePrintSetupDialog()
159 int wxGnomePrintSetupDialog::ShowModal()
161 int response
= gtk_dialog_run (GTK_DIALOG (m_widget
));
162 gtk_widget_destroy(m_widget
);
165 if (response
== GNOME_PRINT_DIALOG_RESPONSE_CANCEL
)
171 bool wxGnomePrintSetupDialog::Validate()
176 bool wxGnomePrintSetupDialog::TransferDataToWindow()
181 bool wxGnomePrintSetupDialog::TransferDataFromWindow()
187 //----------------------------------------------------------------------------
189 //----------------------------------------------------------------------------
191 IMPLEMENT_CLASS(wxGnomePrinter
, wxPrinterBase
)
193 wxGnomePrinter::wxGnomePrinter( wxPrintDialogData
*data
) :
194 wxPrinterBase( data
)
199 wxGnomePrinter::~wxGnomePrinter()
203 bool wxGnomePrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
207 sm_lastError
= wxPRINTER_ERROR
;
211 wxPrintData printdata
= GetPrintDialogData().GetPrintData();
212 wxGnomePrintNativeData
*data
=
213 (wxGnomePrintNativeData
*) printdata
.GetNativeData();
215 GnomePrintJob
*job
= data
->GetPrintJob();
216 m_gpc
= gnome_print_job_get_context (job
);
221 dc
= PrintDialog( parent
);
223 dc
= new wxGnomePrintDC( this );
227 gnome_print_job_close( job
);
233 printout
->OnPreparePrinting();
234 printout
->OnBeginPrinting();
236 if (!printout
->OnBeginDocument(0, 0))
238 sm_lastError
= wxPRINTER_ERROR
;
243 for (pn
= 1; pn
<= 2; pn
++)
246 printout
->OnPrintPage(pn
);
250 printout
->OnEndDocument();
251 printout
->OnEndPrinting();
254 gnome_print_job_close( job
);
255 gnome_print_job_print( job
);
259 return (sm_lastError
== wxPRINTER_NO_ERROR
);
262 wxDC
* wxGnomePrinter::PrintDialog( wxWindow
*parent
)
264 wxPrintDialog
dialog( parent
, &m_printDialogData
);
265 if (dialog
.ShowModal() == wxID_CANCEL
)
267 sm_lastError
= wxPRINTER_ERROR
;
271 return new wxGnomePrintDC( this );
274 bool wxGnomePrinter::Setup( wxWindow
*parent
)
279 //-----------------------------------------------------------------------------
281 //-----------------------------------------------------------------------------
283 IMPLEMENT_CLASS(wxGnomePrintDC
, wxDCBase
)
285 wxGnomePrintDC::wxGnomePrintDC( wxGnomePrinter
*printer
)
289 m_gpc
= printer
->GetPrintContext();
291 m_layout
= gnome_print_pango_create_layout( m_gpc
);
292 m_fontdesc
= pango_font_description_from_string( "Sans 12" );
298 m_signX
= 1; // default x-axis left to right
299 m_signY
= -1; // default y-axis bottom up -> top down
302 wxGnomePrintDC::~wxGnomePrintDC()
306 bool wxGnomePrintDC::Ok() const
311 bool wxGnomePrintDC::DoFloodFill(wxCoord x1
, wxCoord y1
, const wxColour
&col
, int style
)
316 bool wxGnomePrintDC::DoGetPixel(wxCoord x1
, wxCoord y1
, wxColour
*col
) const
321 void wxGnomePrintDC::DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
323 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
327 gnome_print_moveto ( m_gpc
, XDEV2LOG(x1
), YDEV2LOG(y1
) );
328 gnome_print_lineto ( m_gpc
, XDEV2LOG(x2
), YDEV2LOG(y2
) );
329 gnome_print_stroke ( m_gpc
);
331 CalcBoundingBox( x1
, y1
);
332 CalcBoundingBox( x2
, y2
);
335 void wxGnomePrintDC::DoCrossHair(wxCoord x
, wxCoord y
)
339 void wxGnomePrintDC::DoDrawArc(wxCoord x1
,wxCoord y1
,wxCoord x2
,wxCoord y2
,wxCoord xc
,wxCoord yc
)
343 void wxGnomePrintDC::DoDrawEllipticArc(wxCoord x
,wxCoord y
,wxCoord w
,wxCoord h
,double sa
,double ea
)
347 void wxGnomePrintDC::DoDrawPoint(wxCoord x
, wxCoord y
)
351 void wxGnomePrintDC::DoDrawLines(int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
355 void wxGnomePrintDC::DoDrawPolygon(int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
, int fillStyle
)
359 void wxGnomePrintDC::DoDrawPolyPolygon(int n
, int count
[], wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
, int fillStyle
)
363 void wxGnomePrintDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
365 if (m_brush
.GetStyle () != wxTRANSPARENT
)
369 gnome_print_newpath( m_gpc
);
370 gnome_print_moveto( m_gpc
, XLOG2DEV(x
), YLOG2DEV(y
) );
371 gnome_print_lineto( m_gpc
, XLOG2DEV(x
+ width
), YLOG2DEV(y
) );
372 gnome_print_lineto( m_gpc
, XLOG2DEV(x
+ width
), YLOG2DEV(y
+ height
) );
373 gnome_print_lineto( m_gpc
, XLOG2DEV(x
), YLOG2DEV(y
+ height
) );
374 gnome_print_closepath( m_gpc
);
375 gnome_print_fill( m_gpc
);
377 CalcBoundingBox( x
, y
);
378 CalcBoundingBox( x
+ width
, y
+ height
);
381 if (m_pen
.GetStyle () != wxTRANSPARENT
)
385 gnome_print_newpath( m_gpc
);
386 gnome_print_moveto( m_gpc
, XLOG2DEV(x
), YLOG2DEV(y
) );
387 gnome_print_lineto( m_gpc
, XLOG2DEV(x
+ width
), YLOG2DEV(y
) );
388 gnome_print_lineto( m_gpc
, XLOG2DEV(x
+ width
), YLOG2DEV(y
+ height
) );
389 gnome_print_lineto( m_gpc
, XLOG2DEV(x
), YLOG2DEV(y
+ height
) );
390 gnome_print_closepath( m_gpc
);
391 gnome_print_stroke( m_gpc
);
393 CalcBoundingBox( x
, y
);
394 CalcBoundingBox( x
+ width
, y
+ height
);
398 void wxGnomePrintDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
402 void wxGnomePrintDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
406 void wxGnomePrintDC::DoDrawSpline(wxList
*points
)
410 bool wxGnomePrintDC::DoBlit(wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
411 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int rop
, bool useMask
,
412 wxCoord xsrcMask
, wxCoord ysrcMask
)
417 void wxGnomePrintDC::DoDrawIcon( const wxIcon
& icon
, wxCoord x
, wxCoord y
)
421 void wxGnomePrintDC::DoDrawBitmap( const wxBitmap
& bitmap
, wxCoord x
, wxCoord y
, bool useMask
)
425 void wxGnomePrintDC::DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
)
427 if (m_textForegroundColour
.Ok())
429 unsigned char red
= m_textForegroundColour
.Red();
430 unsigned char blue
= m_textForegroundColour
.Blue();
431 unsigned char green
= m_textForegroundColour
.Green();
433 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
435 double redPS
= (double)(red
) / 255.0;
436 double bluePS
= (double)(blue
) / 255.0;
437 double greenPS
= (double)(green
) / 255.0;
439 gnome_print_setrgbcolor( m_gpc
, redPS
, bluePS
, greenPS
);
442 m_currentBlue
= blue
;
443 m_currentGreen
= green
;
450 wxPrintf( wxT("x,y: %d,%d\n"), x
, y
);
452 bool underlined
= m_font
.Ok() && m_font
.GetUnderlined();
455 const wxCharBuffer data
= wxConvUTF8
.cWC2MB( text
);
457 const wxWCharBuffer wdata
= wxConvLocal
.cMB2WC( text
);
460 const wxCharBuffer data
= wxConvUTF8
.cWC2MB( wdata
);
463 size_t datalen
= strlen((const char*)data
);
464 pango_layout_set_text( m_layout
, (const char*) data
, datalen
);
468 PangoAttrList
*attrs
= pango_attr_list_new();
469 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
471 a
->end_index
= datalen
;
472 pango_attr_list_insert(attrs
, a
);
473 pango_layout_set_attributes(m_layout
, attrs
);
474 pango_attr_list_unref(attrs
);
477 gnome_print_moveto (m_gpc
, x
, y
);
478 gnome_print_pango_layout( m_gpc
, m_layout
);
482 // undo underline attributes setting:
483 pango_layout_set_attributes(m_layout
, NULL
);
486 // CalcBoundingBox (x + width, y + height);
487 CalcBoundingBox (x
, y
);
490 void wxGnomePrintDC::DoDrawRotatedText(const wxString
& text
, wxCoord x
, wxCoord y
, double angle
)
494 void wxGnomePrintDC::Clear()
498 void wxGnomePrintDC::SetFont( const wxFont
& font
)
505 pango_font_description_free( m_fontdesc
);
507 m_fontdesc
= pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
509 pango_layout_set_font_description( m_layout
, m_fontdesc
);
513 void wxGnomePrintDC::SetPen( const wxPen
& pen
)
515 if (!pen
.Ok()) return;
517 int oldStyle
= m_pen
.GetStyle();
521 gnome_print_setlinewidth( m_gpc
, XLOG2DEVREL( 1000 * m_pen
.GetWidth() ) / 1000.0f
);
523 unsigned char red
= m_pen
.GetColour().Red();
524 unsigned char blue
= m_pen
.GetColour().Blue();
525 unsigned char green
= m_pen
.GetColour().Green();
527 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
529 double redPS
= (double)(red
) / 255.0;
530 double bluePS
= (double)(blue
) / 255.0;
531 double greenPS
= (double)(green
) / 255.0;
533 gnome_print_setrgbcolor( m_gpc
, redPS
, bluePS
, greenPS
);
536 m_currentBlue
= blue
;
537 m_currentGreen
= green
;
541 void wxGnomePrintDC::SetBrush( const wxBrush
& brush
)
545 void wxGnomePrintDC::SetLogicalFunction( int function
)
549 void wxGnomePrintDC::SetBackground( const wxBrush
& brush
)
553 void wxGnomePrintDC::DoSetClippingRegion(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
557 void wxGnomePrintDC::DestroyClippingRegion()
561 bool wxGnomePrintDC::StartDoc(const wxString
& message
)
563 SetDeviceOrigin( 0,0 );
568 void wxGnomePrintDC::EndDoc()
572 void wxGnomePrintDC::StartPage()
574 gnome_print_beginpage( m_gpc
, (const guchar
*) "1" );
577 void wxGnomePrintDC::EndPage()
579 gnome_print_showpage( m_gpc
);
582 wxCoord
wxGnomePrintDC::GetCharHeight() const
587 wxCoord
wxGnomePrintDC::GetCharWidth() const
592 void wxGnomePrintDC::DoGetTextExtent(const wxString
& string
, wxCoord
*x
, wxCoord
*y
,
594 wxCoord
*externalLeading
,
595 wxFont
*theFont
) const
599 void wxGnomePrintDC::DoGetSize(int* width
, int* height
) const
601 // No idea if that is efficient
602 GnomePrintConfig
*config
= gnome_print_config_default();
605 bool result
= gnome_print_config_get_page_size( config
, &w
, &h
);
609 // Standard PS resolution DIN A4 size.
619 wxPrintf( wxT("size %d,%d\n"), *width
, *height
);
622 void wxGnomePrintDC::DoGetSizeMM(int *width
, int *height
) const
626 /// Later, for now DIN A4
636 wxSize
wxGnomePrintDC::GetPPI() const
638 return wxSize(72,72);
641 void wxGnomePrintDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
643 m_signX
= (xLeftRight
? 1 : -1);
644 m_signY
= (yBottomUp
? 1 : -1);
646 ComputeScaleAndOrigin();
649 void wxGnomePrintDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
655 wxDC::SetDeviceOrigin( x
, h
-y
);
658 void wxGnomePrintDC::SetResolution(int ppi
)
662 int wxGnomePrintDC::GetResolution()
668 class wxGnomePrintModule
: public wxModule
671 wxGnomePrintModule() {}
672 bool OnInit() { wxPrintFactory::SetPrintFactory( new wxGnomePrintFactory
); return true; }
676 DECLARE_DYNAMIC_CLASS(wxGnomePrintModule
)
679 IMPLEMENT_DYNAMIC_CLASS(wxGnomePrintModule
, wxModule
)
682 // wxUSE_LIBGNOMEPRINT