1 /////////////////////////////////////////////////////////////////////////////
3 // Author: Robert Roebling
4 // Purpose: Implement GNOME printing support
6 // Copyright: Robert Roebling
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "gprint.cpp"
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
)
278 //-----------------------------------------------------------------------------
280 //-----------------------------------------------------------------------------
282 IMPLEMENT_CLASS(wxGnomePrintDC
, wxDCBase
)
284 wxGnomePrintDC::wxGnomePrintDC( wxGnomePrinter
*printer
)
288 m_gpc
= printer
->GetPrintContext();
290 m_layout
= gnome_print_pango_create_layout( m_gpc
);
291 m_fontdesc
= pango_font_description_from_string( "Sans 12" );
297 m_signX
= 1; // default x-axis left to right
298 m_signY
= -1; // default y-axis bottom up -> top down
301 wxGnomePrintDC::~wxGnomePrintDC()
305 bool wxGnomePrintDC::Ok() const
310 bool wxGnomePrintDC::DoFloodFill(wxCoord x1
, wxCoord y1
, const wxColour
&col
, int style
)
315 bool wxGnomePrintDC::DoGetPixel(wxCoord x1
, wxCoord y1
, wxColour
*col
) const
320 void wxGnomePrintDC::DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
322 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
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
);
330 CalcBoundingBox( x1
, y1
);
331 CalcBoundingBox( x2
, y2
);
334 void wxGnomePrintDC::DoCrossHair(wxCoord x
, wxCoord y
)
338 void wxGnomePrintDC::DoDrawArc(wxCoord x1
,wxCoord y1
,wxCoord x2
,wxCoord y2
,wxCoord xc
,wxCoord yc
)
342 void wxGnomePrintDC::DoDrawEllipticArc(wxCoord x
,wxCoord y
,wxCoord w
,wxCoord h
,double sa
,double ea
)
346 void wxGnomePrintDC::DoDrawPoint(wxCoord x
, wxCoord y
)
350 void wxGnomePrintDC::DoDrawLines(int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
354 void wxGnomePrintDC::DoDrawPolygon(int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
, int fillStyle
)
358 void wxGnomePrintDC::DoDrawPolyPolygon(int n
, int count
[], wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
, int fillStyle
)
362 void wxGnomePrintDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
364 if (m_brush
.GetStyle () != wxTRANSPARENT
)
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
);
376 CalcBoundingBox( x
, y
);
377 CalcBoundingBox( x
+ width
, y
+ height
);
380 if (m_pen
.GetStyle () != wxTRANSPARENT
)
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
);
392 CalcBoundingBox( x
, y
);
393 CalcBoundingBox( x
+ width
, y
+ height
);
397 void wxGnomePrintDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
401 void wxGnomePrintDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
405 void wxGnomePrintDC::DoDrawSpline(wxList
*points
)
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
)
416 void wxGnomePrintDC::DoDrawIcon( const wxIcon
& icon
, wxCoord x
, wxCoord y
)
420 void wxGnomePrintDC::DoDrawBitmap( const wxBitmap
& bitmap
, wxCoord x
, wxCoord y
, bool useMask
)
424 void wxGnomePrintDC::DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
)
426 if (m_textForegroundColour
.Ok())
428 unsigned char red
= m_textForegroundColour
.Red();
429 unsigned char blue
= m_textForegroundColour
.Blue();
430 unsigned char green
= m_textForegroundColour
.Green();
432 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
434 double redPS
= (double)(red
) / 255.0;
435 double bluePS
= (double)(blue
) / 255.0;
436 double greenPS
= (double)(green
) / 255.0;
438 gnome_print_setrgbcolor( m_gpc
, redPS
, bluePS
, greenPS
);
441 m_currentBlue
= blue
;
442 m_currentGreen
= green
;
449 wxPrintf( wxT("x,y: %d,%d\n"), x
, y
);
451 bool underlined
= m_font
.Ok() && m_font
.GetUnderlined();
454 const wxCharBuffer data
= wxConvUTF8
.cWC2MB( text
);
456 const wxWCharBuffer wdata
= wxConvLocal
.cMB2WC( text
);
459 const wxCharBuffer data
= wxConvUTF8
.cWC2MB( wdata
);
462 size_t datalen
= strlen((const char*)data
);
463 pango_layout_set_text( m_layout
, (const char*) data
, datalen
);
467 PangoAttrList
*attrs
= pango_attr_list_new();
468 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
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
);
476 gnome_print_moveto (m_gpc
, x
, y
);
477 gnome_print_pango_layout( m_gpc
, m_layout
);
481 // undo underline attributes setting:
482 pango_layout_set_attributes(m_layout
, NULL
);
485 // CalcBoundingBox (x + width, y + height);
486 CalcBoundingBox (x
, y
);
489 void wxGnomePrintDC::DoDrawRotatedText(const wxString
& text
, wxCoord x
, wxCoord y
, double angle
)
493 void wxGnomePrintDC::Clear()
497 void wxGnomePrintDC::SetFont( const wxFont
& font
)
504 pango_font_description_free( m_fontdesc
);
506 m_fontdesc
= pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
508 pango_layout_set_font_description( m_layout
, m_fontdesc
);
512 void wxGnomePrintDC::SetPen( const wxPen
& pen
)
514 if (!pen
.Ok()) return;
516 int oldStyle
= m_pen
.GetStyle();
520 gnome_print_setlinewidth( m_gpc
, XLOG2DEVREL( 1000 * m_pen
.GetWidth() ) / 1000.0f
);
522 unsigned char red
= m_pen
.GetColour().Red();
523 unsigned char blue
= m_pen
.GetColour().Blue();
524 unsigned char green
= m_pen
.GetColour().Green();
526 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
528 double redPS
= (double)(red
) / 255.0;
529 double bluePS
= (double)(blue
) / 255.0;
530 double greenPS
= (double)(green
) / 255.0;
532 gnome_print_setrgbcolor( m_gpc
, redPS
, bluePS
, greenPS
);
535 m_currentBlue
= blue
;
536 m_currentGreen
= green
;
540 void wxGnomePrintDC::SetBrush( const wxBrush
& brush
)
544 void wxGnomePrintDC::SetLogicalFunction( int function
)
548 void wxGnomePrintDC::SetBackground( const wxBrush
& brush
)
552 void wxGnomePrintDC::DoSetClippingRegion(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
556 void wxGnomePrintDC::DestroyClippingRegion()
560 bool wxGnomePrintDC::StartDoc(const wxString
& message
)
562 SetDeviceOrigin( 0,0 );
567 void wxGnomePrintDC::EndDoc()
571 void wxGnomePrintDC::StartPage()
573 gnome_print_beginpage( m_gpc
, (const guchar
*) "1" );
576 void wxGnomePrintDC::EndPage()
578 gnome_print_showpage( m_gpc
);
581 wxCoord
wxGnomePrintDC::GetCharHeight() const
586 wxCoord
wxGnomePrintDC::GetCharWidth() const
591 void wxGnomePrintDC::DoGetTextExtent(const wxString
& string
, wxCoord
*x
, wxCoord
*y
,
593 wxCoord
*externalLeading
,
594 wxFont
*theFont
) const
598 void wxGnomePrintDC::DoGetSize(int* width
, int* height
) const
600 // No idea if that is efficient
601 GnomePrintConfig
*config
= gnome_print_config_default();
604 bool result
= gnome_print_config_get_page_size( config
, &w
, &h
);
608 // Standard PS resolution DIN A4 size.
618 wxPrintf( wxT("size %d,%d\n"), *width
, *height
);
621 void wxGnomePrintDC::DoGetSizeMM(int *width
, int *height
) const
625 /// Later, for now DIN A4
635 wxSize
wxGnomePrintDC::GetPPI() const
637 return wxSize(72,72);
640 void wxGnomePrintDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
642 m_signX
= (xLeftRight
? 1 : -1);
643 m_signY
= (yBottomUp
? 1 : -1);
645 ComputeScaleAndOrigin();
648 void wxGnomePrintDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
654 wxDC::SetDeviceOrigin( x
, h
-y
);
657 void wxGnomePrintDC::SetResolution(int ppi
)
661 int wxGnomePrintDC::GetResolution()
667 // wxUSE_LIBGNOMEPRINT