1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gnomeprint.cpp
6 /////////////////////////////////////////////////////////////////////////////
9 #pragma implementation "gprint.cpp"
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
20 #include "wx/fontutil.h"
21 #include "wx/printdlg.h"
22 #include "wx/gtk/private.h"
24 #include <libgnomeprint/gnome-print.h>
25 #include <libgnomeprint/gnome-print-pango.h>
26 #include <libgnomeprintui/gnome-print-dialog.h>
28 //----------------------------------------------------------------------------
29 // wxGnomePrintNativeData
30 //----------------------------------------------------------------------------
32 IMPLEMENT_CLASS(wxGnomePrintNativeData
, wxPrintNativeDataBase
)
34 wxGnomePrintNativeData::wxGnomePrintNativeData()
36 m_config
= gnome_print_config_default();
37 m_job
= gnome_print_job_new( m_config
);
40 wxGnomePrintNativeData::~wxGnomePrintNativeData()
42 g_object_unref (G_OBJECT (m_config
));
43 g_object_unref (G_OBJECT (m_job
));
46 bool wxGnomePrintNativeData::TransferTo( wxPrintData
&data
)
52 bool wxGnomePrintNativeData::TransferFrom( const wxPrintData
&data
)
58 //----------------------------------------------------------------------------
59 // wxGnomePrintFactory
60 //----------------------------------------------------------------------------
62 wxPrinterBase
* wxGnomePrintFactory::CreatePrinter( wxPrintDialogData
*data
)
64 return new wxGnomePrinter( data
);
67 wxPrintPreviewBase
*wxGnomePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
69 wxPrintDialogData
*data
)
71 return new wxPostScriptPrintPreview( preview
, printout
, data
);
74 wxPrintPreviewBase
*wxGnomePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
78 return new wxPostScriptPrintPreview( preview
, printout
, data
);
81 wxPrintDialogBase
*wxGnomePrintFactory::CreatePrintDialog( wxWindow
*parent
,
82 wxPrintDialogData
*data
)
84 return new wxGenericPrintDialog( parent
, data
);
87 wxPrintDialogBase
*wxGnomePrintFactory::CreatePrintDialog( wxWindow
*parent
,
90 return new wxGenericPrintDialog( parent
, data
);
93 bool wxGnomePrintFactory::HasPrintSetupDialog()
98 wxDialog
*wxGnomePrintFactory::CreatePrintSetupDialog( wxWindow
*parent
, wxPrintData
*data
)
100 return new wxGnomePrintSetupDialog( parent
, data
);
103 bool wxGnomePrintFactory::HasOwnPrintToFile()
108 bool wxGnomePrintFactory::HasPrinterLine()
113 wxString
wxGnomePrintFactory::CreatePrinterLine()
115 // We should query "gnome_config_default" here
116 return _("GNOME print");
119 bool wxGnomePrintFactory::HasStatusLine()
124 wxString
wxGnomePrintFactory::CreateStatusLine()
126 // We should query "gnome_config_default" here
130 wxPrintNativeDataBase
*wxGnomePrintFactory::CreatePrintNativeData()
132 return new wxGnomePrintNativeData
;
135 //----------------------------------------------------------------------------
136 // wxGnomePrintSetupDialog
137 //----------------------------------------------------------------------------
139 IMPLEMENT_CLASS(wxGnomePrintSetupDialog
, wxDialog
)
141 wxGnomePrintSetupDialog::wxGnomePrintSetupDialog( wxWindow
*parent
, wxPrintData
*data
)
143 wxGnomePrintNativeData
*native
=
144 (wxGnomePrintNativeData
*) data
->GetNativeData();
146 m_widget
= gnome_print_dialog_new (native
->GetPrintJob(), (guchar
*)"Print setup", 0);
149 wxGnomePrintSetupDialog::~wxGnomePrintSetupDialog()
154 int wxGnomePrintSetupDialog::ShowModal()
156 int response
= gtk_dialog_run (GTK_DIALOG (m_widget
));
157 gtk_widget_destroy(m_widget
);
160 if (response
== GNOME_PRINT_DIALOG_RESPONSE_CANCEL
)
166 bool wxGnomePrintSetupDialog::Validate()
171 bool wxGnomePrintSetupDialog::TransferDataToWindow()
176 bool wxGnomePrintSetupDialog::TransferDataFromWindow()
182 //----------------------------------------------------------------------------
184 //----------------------------------------------------------------------------
186 IMPLEMENT_CLASS(wxGnomePrinter
, wxPrinterBase
)
188 wxGnomePrinter::wxGnomePrinter( wxPrintDialogData
*data
) :
189 wxPrinterBase( data
)
194 wxGnomePrinter::~wxGnomePrinter()
198 bool wxGnomePrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
202 sm_lastError
= wxPRINTER_ERROR
;
206 wxPrintData printdata
= GetPrintDialogData().GetPrintData();
207 wxGnomePrintNativeData
*data
=
208 (wxGnomePrintNativeData
*) printdata
.GetNativeData();
210 GnomePrintJob
*job
= data
->GetPrintJob();
211 m_gpc
= gnome_print_job_get_context (job
);
216 dc
= PrintDialog( parent
);
218 dc
= new wxGnomePrintDC( this );
222 gnome_print_job_close( job
);
228 printout
->OnPreparePrinting();
229 printout
->OnBeginPrinting();
231 if (!printout
->OnBeginDocument(0, 0))
233 sm_lastError
= wxPRINTER_ERROR
;
238 for (pn
= 1; pn
<= 2; pn
++)
241 printout
->OnPrintPage(pn
);
245 printout
->OnEndDocument();
246 printout
->OnEndPrinting();
249 gnome_print_job_close( job
);
250 gnome_print_job_print( job
);
254 return (sm_lastError
== wxPRINTER_NO_ERROR
);
257 wxDC
* wxGnomePrinter::PrintDialog( wxWindow
*parent
)
259 wxPrintDialog
dialog( parent
, &m_printDialogData
);
260 if (dialog
.ShowModal() == wxID_CANCEL
)
262 sm_lastError
= wxPRINTER_ERROR
;
266 return new wxGnomePrintDC( this );
269 bool wxGnomePrinter::Setup( wxWindow
*parent
)
273 //-----------------------------------------------------------------------------
275 //-----------------------------------------------------------------------------
277 IMPLEMENT_CLASS(wxGnomePrintDC
, wxDCBase
)
279 wxGnomePrintDC::wxGnomePrintDC( wxGnomePrinter
*printer
)
283 m_gpc
= printer
->GetPrintContext();
285 m_layout
= gnome_print_pango_create_layout( m_gpc
);
286 m_fontdesc
= pango_font_description_from_string( "Sans 12" );
292 m_signX
= 1; // default x-axis left to right
293 m_signY
= -1; // default y-axis bottom up -> top down
296 wxGnomePrintDC::~wxGnomePrintDC()
300 bool wxGnomePrintDC::Ok() const
305 bool wxGnomePrintDC::DoFloodFill(wxCoord x1
, wxCoord y1
, const wxColour
&col
, int style
)
310 bool wxGnomePrintDC::DoGetPixel(wxCoord x1
, wxCoord y1
, wxColour
*col
) const
315 void wxGnomePrintDC::DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
317 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
321 gnome_print_moveto ( m_gpc
, XDEV2LOG(x1
), YDEV2LOG(y1
) );
322 gnome_print_lineto ( m_gpc
, XDEV2LOG(x2
), YDEV2LOG(y2
) );
323 gnome_print_stroke ( m_gpc
);
325 CalcBoundingBox( x1
, y1
);
326 CalcBoundingBox( x2
, y2
);
329 void wxGnomePrintDC::DoCrossHair(wxCoord x
, wxCoord y
)
333 void wxGnomePrintDC::DoDrawArc(wxCoord x1
,wxCoord y1
,wxCoord x2
,wxCoord y2
,wxCoord xc
,wxCoord yc
)
337 void wxGnomePrintDC::DoDrawEllipticArc(wxCoord x
,wxCoord y
,wxCoord w
,wxCoord h
,double sa
,double ea
)
341 void wxGnomePrintDC::DoDrawPoint(wxCoord x
, wxCoord y
)
345 void wxGnomePrintDC::DoDrawLines(int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
349 void wxGnomePrintDC::DoDrawPolygon(int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
, int fillStyle
)
353 void wxGnomePrintDC::DoDrawPolyPolygon(int n
, int count
[], wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
, int fillStyle
)
357 void wxGnomePrintDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
359 if (m_brush
.GetStyle () != wxTRANSPARENT
)
363 gnome_print_newpath( m_gpc
);
364 gnome_print_moveto( m_gpc
, XLOG2DEV(x
), YLOG2DEV(y
) );
365 gnome_print_lineto( m_gpc
, XLOG2DEV(x
+ width
), YLOG2DEV(y
) );
366 gnome_print_lineto( m_gpc
, XLOG2DEV(x
+ width
), YLOG2DEV(y
+ height
) );
367 gnome_print_lineto( m_gpc
, XLOG2DEV(x
), YLOG2DEV(y
+ height
) );
368 gnome_print_closepath( m_gpc
);
369 gnome_print_fill( m_gpc
);
371 CalcBoundingBox( x
, y
);
372 CalcBoundingBox( x
+ width
, y
+ height
);
375 if (m_pen
.GetStyle () != wxTRANSPARENT
)
379 gnome_print_newpath( m_gpc
);
380 gnome_print_moveto( m_gpc
, XLOG2DEV(x
), YLOG2DEV(y
) );
381 gnome_print_lineto( m_gpc
, XLOG2DEV(x
+ width
), YLOG2DEV(y
) );
382 gnome_print_lineto( m_gpc
, XLOG2DEV(x
+ width
), YLOG2DEV(y
+ height
) );
383 gnome_print_lineto( m_gpc
, XLOG2DEV(x
), YLOG2DEV(y
+ height
) );
384 gnome_print_closepath( m_gpc
);
385 gnome_print_stroke( m_gpc
);
387 CalcBoundingBox( x
, y
);
388 CalcBoundingBox( x
+ width
, y
+ height
);
392 void wxGnomePrintDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
396 void wxGnomePrintDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
400 void wxGnomePrintDC::DoDrawSpline(wxList
*points
)
404 bool wxGnomePrintDC::DoBlit(wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
405 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int rop
, bool useMask
,
406 wxCoord xsrcMask
, wxCoord ysrcMask
)
411 void wxGnomePrintDC::DoDrawIcon( const wxIcon
& icon
, wxCoord x
, wxCoord y
)
415 void wxGnomePrintDC::DoDrawBitmap( const wxBitmap
& bitmap
, wxCoord x
, wxCoord y
, bool useMask
)
419 void wxGnomePrintDC::DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
)
421 if (m_textForegroundColour
.Ok())
423 unsigned char red
= m_textForegroundColour
.Red();
424 unsigned char blue
= m_textForegroundColour
.Blue();
425 unsigned char green
= m_textForegroundColour
.Green();
427 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
429 double redPS
= (double)(red
) / 255.0;
430 double bluePS
= (double)(blue
) / 255.0;
431 double greenPS
= (double)(green
) / 255.0;
433 gnome_print_setrgbcolor( m_gpc
, redPS
, bluePS
, greenPS
);
436 m_currentBlue
= blue
;
437 m_currentGreen
= green
;
444 wxPrintf( wxT("x,y: %d,%d\n"), x
, y
);
446 bool underlined
= m_font
.Ok() && m_font
.GetUnderlined();
449 const wxCharBuffer data
= wxConvUTF8
.cWC2MB( text
);
451 const wxWCharBuffer wdata
= wxConvLocal
.cMB2WC( text
);
454 const wxCharBuffer data
= wxConvUTF8
.cWC2MB( wdata
);
457 size_t datalen
= strlen((const char*)data
);
458 pango_layout_set_text( m_layout
, (const char*) data
, datalen
);
462 PangoAttrList
*attrs
= pango_attr_list_new();
463 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
465 a
->end_index
= datalen
;
466 pango_attr_list_insert(attrs
, a
);
467 pango_layout_set_attributes(m_layout
, attrs
);
468 pango_attr_list_unref(attrs
);
471 gnome_print_moveto (m_gpc
, x
, y
);
472 gnome_print_pango_layout( m_gpc
, m_layout
);
476 // undo underline attributes setting:
477 pango_layout_set_attributes(m_layout
, NULL
);
480 // CalcBoundingBox (x + width, y + height);
481 CalcBoundingBox (x
, y
);
484 void wxGnomePrintDC::DoDrawRotatedText(const wxString
& text
, wxCoord x
, wxCoord y
, double angle
)
488 void wxGnomePrintDC::Clear()
492 void wxGnomePrintDC::SetFont( const wxFont
& font
)
499 pango_font_description_free( m_fontdesc
);
501 m_fontdesc
= pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
503 pango_layout_set_font_description( m_layout
, m_fontdesc
);
507 void wxGnomePrintDC::SetPen( const wxPen
& pen
)
509 if (!pen
.Ok()) return;
511 int oldStyle
= m_pen
.GetStyle();
515 gnome_print_setlinewidth( m_gpc
, XLOG2DEVREL( 1000 * m_pen
.GetWidth() ) / 1000.0f
);
517 unsigned char red
= m_pen
.GetColour().Red();
518 unsigned char blue
= m_pen
.GetColour().Blue();
519 unsigned char green
= m_pen
.GetColour().Green();
521 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
523 double redPS
= (double)(red
) / 255.0;
524 double bluePS
= (double)(blue
) / 255.0;
525 double greenPS
= (double)(green
) / 255.0;
527 gnome_print_setrgbcolor( m_gpc
, redPS
, bluePS
, greenPS
);
530 m_currentBlue
= blue
;
531 m_currentGreen
= green
;
535 void wxGnomePrintDC::SetBrush( const wxBrush
& brush
)
539 void wxGnomePrintDC::SetLogicalFunction( int function
)
543 void wxGnomePrintDC::SetBackground( const wxBrush
& brush
)
547 void wxGnomePrintDC::DoSetClippingRegion(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
551 void wxGnomePrintDC::DestroyClippingRegion()
555 bool wxGnomePrintDC::StartDoc(const wxString
& message
)
557 SetDeviceOrigin( 0,0 );
562 void wxGnomePrintDC::EndDoc()
566 void wxGnomePrintDC::StartPage()
568 gnome_print_beginpage( m_gpc
, (const guchar
*) "1" );
571 void wxGnomePrintDC::EndPage()
573 gnome_print_showpage( m_gpc
);
576 wxCoord
wxGnomePrintDC::GetCharHeight() const
581 wxCoord
wxGnomePrintDC::GetCharWidth() const
586 void wxGnomePrintDC::DoGetTextExtent(const wxString
& string
, wxCoord
*x
, wxCoord
*y
,
588 wxCoord
*externalLeading
,
589 wxFont
*theFont
) const
593 void wxGnomePrintDC::DoGetSize(int* width
, int* height
) const
595 // No idea if that is efficient
596 GnomePrintConfig
*config
= gnome_print_config_default();
599 bool result
= gnome_print_config_get_page_size( config
, &w
, &h
);
603 // Standard PS resolution DIN A4 size.
613 wxPrintf( wxT("size %d,%d\n"), *width
, *height
);
616 void wxGnomePrintDC::DoGetSizeMM(int *width
, int *height
) const
620 /// Later, for now DIN A4
630 wxSize
wxGnomePrintDC::GetPPI() const
632 return wxSize(72,72);
635 void wxGnomePrintDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
637 m_signX
= (xLeftRight
? 1 : -1);
638 m_signY
= (yBottomUp
? 1 : -1);
640 ComputeScaleAndOrigin();
643 void wxGnomePrintDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
649 wxDC::SetDeviceOrigin( x
, h
-y
);
652 void wxGnomePrintDC::SetResolution(int ppi
)
656 int wxGnomePrintDC::GetResolution()