X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5cdcb78775c94667cc827acd4eaa39900026ba19..952a3bd46dc50f95c462606fac8214573022f612:/src/gtk/print.cpp diff --git a/src/gtk/print.cpp b/src/gtk/print.cpp index 3ee388df0e..e9b17367a8 100644 --- a/src/gtk/print.cpp +++ b/src/gtk/print.cpp @@ -26,6 +26,7 @@ #include "wx/math.h" #include "wx/image.h" #include "wx/module.h" +#include "wx/crt.h" #endif #include "wx/fontutil.h" @@ -60,7 +61,7 @@ static wxCairoLibrary* gs_cairo = NULL; class wxGtkPrintModule: public wxModule { public: - wxGtkPrintModule() + wxGtkPrintModule() { #if wxUSE_LIBGNOMEPRINT // This module must be initialized AFTER gnomeprint's one @@ -135,7 +136,9 @@ bool wxGtkPrintFactory::HasPrintSetupDialog() return false; } -wxDialog *wxGtkPrintFactory::CreatePrintSetupDialog( wxWindow *parent, wxPrintData *data ) +wxDialog * +wxGtkPrintFactory::CreatePrintSetupDialog(wxWindow * WXUNUSED(parent), + wxPrintData * WXUNUSED(data)) { return NULL; } @@ -182,13 +185,12 @@ wxPrintNativeDataBase *wxGtkPrintFactory::CreatePrintNativeData() // Callback functions for Gtk Printings. //---------------------------------------------------------------------------- -// We use it to pass useful objets to gtk printing callback functions. -typedef struct +// We use it to pass useful objects to GTK printing callback functions. +struct wxPrinterToGtkData { wxGtkPrinter * printer; wxPrintout * printout; -} -wxPrinterToGtkData; +}; extern "C" { @@ -206,20 +208,28 @@ extern "C" data->printer->DrawPage(data->printout, operation, context, page_nr); } - static void gtk_end_print_callback (GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data) + static void gtk_end_print_callback(GtkPrintOperation * WXUNUSED(operation), + GtkPrintContext * WXUNUSED(context), + gpointer user_data) { wxPrintout *printout = (wxPrintout *) user_data; printout->OnEndPrinting(); } - static gboolean gtk_preview_print_callback (GtkPrintOperation *operation, GtkPrintOperationPreview *preview, GtkPrintContext *context, GtkWindow *parent, gpointer user_data) + static gboolean + gtk_preview_print_callback(GtkPrintOperation * WXUNUSED(operation), + GtkPrintOperationPreview * WXUNUSED(preview), + GtkPrintContext *context, + GtkWindow *parent, + gpointer user_data) { wxPrintout *printout = (wxPrintout *) user_data; printout->SetIsPreview(true); - /* We create a cairo context with 72dpi resolution. This resolution is only used for positionning. */ + /* We create a Cairo context with 72dpi resolution. This resolution is + * only used for positioning. */ cairo_t *cairo = gdk_cairo_create(GTK_WIDGET(parent)->window); gtk_print_context_set_cairo_context(context, cairo, 72, 72); @@ -552,11 +562,11 @@ void wxGtkPrintNativeData::SetPrintConfig( GtkPrintSettings * config ) GtkPageSetup* wxGtkPrintNativeData::GetPageSetupFromSettings(GtkPrintSettings* settings) { GtkPageSetup* page_setup = gtk_page_setup_new(); - gtk_page_setup_set_orientation (page_setup, gtk_print_settings_get_orientation (settings)); + gtk_page_setup_set_orientation (page_setup, gtk_print_settings_get_orientation (settings)); - GtkPaperSize *paper_size = gtk_print_settings_get_paper_size (settings); - if (paper_size != NULL) - gtk_page_setup_set_paper_size_and_default_margins (page_setup, paper_size); + GtkPaperSize *paper_size = gtk_print_settings_get_paper_size (settings); + if (paper_size != NULL) + gtk_page_setup_set_paper_size_and_default_margins (page_setup, paper_size); return page_setup; } @@ -565,7 +575,7 @@ GtkPageSetup* wxGtkPrintNativeData::GetPageSetupFromSettings(GtkPrintSettings* s void wxGtkPrintNativeData::SetPageSetupToSettings(GtkPrintSettings* settings, GtkPageSetup* page_setup) { gtk_print_settings_set_orientation ( settings, gtk_page_setup_get_orientation (page_setup)); - gtk_print_settings_set_paper_size ( settings, gtk_page_setup_get_paper_size (page_setup)); + gtk_print_settings_set_paper_size ( settings, gtk_page_setup_get_paper_size (page_setup)); } //---------------------------------------------------------------------------- @@ -637,14 +647,14 @@ int wxGtkPrintDialog::ShowModal() // If the settings are OK, we restore it. if (settings != NULL) gtk_print_operation_set_print_settings (native->GetPrintJob(), settings); - gtk_print_operation_set_default_page_setup (native->GetPrintJob(), native->GetPageSetupFromSettings(settings)); + gtk_print_operation_set_default_page_setup (native->GetPrintJob(), native->GetPageSetupFromSettings(settings)); // Show the dialog if needed. GError* gError = NULL; if (GetShowDialog()) response = gtk_print_operation_run (native->GetPrintJob(), GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, GTK_WINDOW(gtk_widget_get_toplevel(m_parent->m_widget) ), &gError); else - response = gtk_print_operation_run (native->GetPrintJob(), GTK_PRINT_OPERATION_ACTION_PRINT, (GtkWindow *) m_parent, &gError); + response = gtk_print_operation_run (native->GetPrintJob(), GTK_PRINT_OPERATION_ACTION_PRINT, GTK_WINDOW(gtk_widget_get_toplevel(m_parent->m_widget)), &gError); // Does everything went well? if (response == GTK_PRINT_OPERATION_RESULT_CANCEL) @@ -678,8 +688,8 @@ int wxGtkPrintDialog::ShowModal() range = gtk_print_settings_get_page_ranges (newSettings, &num_ranges); if (num_ranges >= 1) { - m_printDialogData.SetFromPage( range[0].start ); - m_printDialogData.SetToPage( range[0].end ); + m_printDialogData.SetFromPage( range[0].start ); + m_printDialogData.SetToPage( range[0].end ); } else { m_printDialogData.SetAllPages( true ); @@ -847,19 +857,29 @@ bool wxGtkPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt ) dataToSend.printer = this; dataToSend.printout = printout; - // These Gtk signals are catched here. + // These Gtk signals are caught here. g_signal_connect (printOp, "begin-print", G_CALLBACK (gtk_begin_print_callback), &dataToSend); g_signal_connect (printOp, "draw-page", G_CALLBACK (gtk_draw_page_print_callback), &dataToSend); g_signal_connect (printOp, "end-print", G_CALLBACK (gtk_end_print_callback), printout); g_signal_connect (printOp, "preview", G_CALLBACK (gtk_preview_print_callback), printout); - m_showDialog = true; - if (!prompt) - m_showDialog = false; + // This is used to setup the DC and + // show the dialog if desired + wxGtkPrintDialog dialog( parent, &m_printDialogData ); + dialog.SetPrintDC(m_dc); + dialog.SetShowDialog(prompt); - // PrintDialog returns a wxDC but we created it before so we don't need it anymore: we just delete it. - wxDC* uselessdc = PrintDialog( parent ); - delete uselessdc; + // doesn't necessarily show + int ret = dialog.ShowModal(); + if (ret == wxID_CANCEL) + { + sm_lastError = wxPRINTER_CANCELLED; + } + if (ret == wxID_NO) + { + sm_lastError = wxPRINTER_ERROR; + wxFAIL_MSG(_("The print dialog returned an error.")); + } g_object_unref (printOp); @@ -958,7 +978,10 @@ void wxGtkPrinter::BeginPrint(wxPrintout *printout, GtkPrintOperation *operation gtk_print_operation_set_n_pages(operation, numPages); } -void wxGtkPrinter::DrawPage(wxPrintout *printout, GtkPrintOperation *operation, GtkPrintContext *context, int page_nr) +void wxGtkPrinter::DrawPage(wxPrintout *printout, + GtkPrintOperation *operation, + GtkPrintContext * WXUNUSED(context), + int page_nr) { int fromPage, toPage, minPage, maxPage, startPage, endPage; printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage); @@ -981,8 +1004,8 @@ void wxGtkPrinter::DrawPage(wxPrintout *printout, GtkPrintOperation *operation, // We don't need to verify these values as it has already been done in wxGtkPrinter::BeginPrint. if (num_ranges >= 1) { - startPage = range[0].start + 1; - endPage = range[0].end + 1; + startPage = range[0].start + 1; + endPage = range[0].end + 1; } else { startPage = minPage; @@ -1023,13 +1046,11 @@ void wxGtkPrinter::DrawPage(wxPrintout *printout, GtkPrintOperation *operation, wxDC* wxGtkPrinter::PrintDialog( wxWindow *parent ) { wxGtkPrintDialog dialog( parent, &m_printDialogData ); - int ret; dialog.SetPrintDC(m_dc); + dialog.SetShowDialog(true); - dialog.SetShowDialog(m_showDialog); - - ret = dialog.ShowModal(); + int ret = dialog.ShowModal(); if (ret == wxID_CANCEL) { @@ -1047,7 +1068,7 @@ wxDC* wxGtkPrinter::PrintDialog( wxWindow *parent ) return new wxGtkPrintDC( m_printDialogData.GetPrintData() ); } -bool wxGtkPrinter::Setup( wxWindow *parent ) +bool wxGtkPrinter::Setup( wxWindow * WXUNUSED(parent) ) { // Obsolete, for backward compatibility. return false; @@ -1073,16 +1094,14 @@ wxGtkPrintDC::wxGtkPrintDC( const wxPrintData& data ) m_gpc = native->GetPrintContext(); - // RR: what does this do? + // Match print quality to resolution (high = 1200dpi) m_resolution = m_printData.GetQuality(); // (int) gtk_print_context_get_dpi_x( m_gpc ); - if (m_resolution < 0) + if (m_resolution < 0) m_resolution = (1 << (m_resolution+4)) *150; - wxPrintf( "resolution %d\n", m_resolution ); - m_PS2DEV = (double)m_resolution / 72.0; m_DEV2PS = 72.0 / (double)m_resolution; - + m_context = gtk_print_context_create_pango_context( m_gpc ); m_layout = gtk_print_context_create_pango_layout ( m_gpc ); m_fontdesc = pango_font_description_from_string( "Sans 12" ); @@ -1093,11 +1112,12 @@ wxGtkPrintDC::wxGtkPrintDC( const wxPrintData& data ) m_currentBlue = 0; m_currentGreen = 0; - m_signX = 1; // default x-axis left to right. + m_signX = 1; // default x-axis left to right. m_signY = 1; // default y-axis bottom up -> top down. - // By default the origine of cairo contexte is in the upper left corner of the printable area. - // We need to translate it so that it is in the upper left corner of the paper (i.e. doesn't care about margins) + // By default the origin of the Cairo context is in the upper left + // corner of the printable area. We need to translate it so that it + // is in the upper left corner of the paper (without margins) GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc ); gdouble ml, mt; ml = gtk_page_setup_get_left_margin (setup, GTK_UNIT_POINTS); @@ -1113,12 +1133,16 @@ wxGtkPrintDC::~wxGtkPrintDC() bool wxGtkPrintDC::IsOk() const { - return (m_gpc != NULL); + return m_gpc != NULL; } -bool wxGtkPrintDC::DoFloodFill(wxCoord x1, wxCoord y1, const wxColour &col, int style ) +bool wxGtkPrintDC::DoFloodFill(wxCoord WXUNUSED(x1), + wxCoord WXUNUSED(y1), + const wxColour& WXUNUSED(col), + int WXUNUSED(style)) { - // We can't access the given coord as a cairo context is scalable, ie a coord doesn't mean anything in this context. + // We can't access the given coord as a Cairo context is scalable, ie a + // coord doesn't mean anything in this context. wxFAIL_MSG(_("not implemented")); return false; } @@ -1219,9 +1243,10 @@ void wxGtkPrintDC::DoGradientFillLinear(const wxRect& rect, const wxColour& init CalcBoundingBox(x+w, y+h); } -bool wxGtkPrintDC::DoGetPixel(wxCoord x1, wxCoord y1, wxColour *col) const +bool wxGtkPrintDC::DoGetPixel(wxCoord WXUNUSED(x1), + wxCoord WXUNUSED(y1), + wxColour * WXUNUSED(col)) const { - // We can't access the given coord as a cairo context is scalable, ie a coord doesn't mean anything in this context. wxFAIL_MSG(_("not implemented")); return false; } @@ -1408,6 +1433,9 @@ void wxGtkPrintDC::DoDrawPolyPolygon(int n, int count[], wxPoint points[], wxCoo void wxGtkPrintDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height) { + width--; + height--; + gs_cairo->cairo_new_path(m_cairo); gs_cairo->cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height)); @@ -1423,6 +1451,9 @@ void wxGtkPrintDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord void wxGtkPrintDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius) { + width--; + height--; + if (radius < 0.0) radius = - radius * ((width < height) ? width : height); wxCoord dd = 2 * (wxCoord) radius; @@ -1468,6 +1499,9 @@ void wxGtkPrintDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, w void wxGtkPrintDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height) { + width--; + height--; + gs_cairo->cairo_save (m_cairo); gs_cairo->cairo_new_path(m_cairo); @@ -1489,20 +1523,20 @@ void wxGtkPrintDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord he } #if wxUSE_SPLINES -void wxGtkPrintDC::DoDrawSpline(wxList *points) +void wxGtkPrintDC::DoDrawSpline(const wxPointList *points) { SetPen (m_pen); double c, d, x1, y1, x2, y2, x3, y3; wxPoint *p, *q; - wxList::compatibility_iterator node = points->GetFirst(); - p = (wxPoint *)node->GetData(); + wxPointList::compatibility_iterator node = points->GetFirst(); + p = node->GetData(); x1 = p->x; y1 = p->y; node = node->GetNext(); - p = (wxPoint *)node->GetData(); + p = node->GetData(); c = p->x; d = p->y; x3 = @@ -1520,7 +1554,7 @@ void wxGtkPrintDC::DoDrawSpline(wxList *points) node = node->GetNext(); while (node) { - q = (wxPoint *)node->GetData(); + q = node->GetData(); x1 = x3; y1 = y3; @@ -1548,10 +1582,16 @@ void wxGtkPrintDC::DoDrawSpline(wxList *points) } #endif // wxUSE_SPLINES -bool wxGtkPrintDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height, - wxDC *source, wxCoord xsrc, wxCoord ysrc, int rop, bool useMask, - wxCoord xsrcMask, wxCoord ysrcMask) +bool wxGtkPrintDC::DoBlit(wxCoord xdest, wxCoord ydest, + wxCoord width, wxCoord height, + wxDC *source, wxCoord xsrc, wxCoord ysrc, + int rop, bool useMask, + wxCoord WXUNUSED_UNLESS_DEBUG(xsrcMask), + wxCoord WXUNUSED_UNLESS_DEBUG(ysrcMask)) { + wxASSERT_MSG( xsrcMask == wxDefaultCoord && ysrcMask == wxDefaultCoord, + wxT("mask coordinates are not supported") ); + wxCHECK_MSG( source, false, wxT("invalid source dc") ); // Blit into a bitmap. @@ -1699,12 +1739,7 @@ void wxGtkPrintDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, bool underlined = m_font.Ok() && m_font.GetUnderlined(); -// FIXME-UTF8: wouldn't be needed if utf8_str() always returned a buffer -#if wxUSE_UNICODE_UTF8 - const char *data = text.utf8_str(); -#else - const wxCharBuffer data = text.utf8_str(); -#endif + const wxUTF8Buf data = text.utf8_str(); size_t datalen = strlen(data); pango_layout_set_text( m_layout, data, datalen); @@ -1744,7 +1779,7 @@ void wxGtkPrintDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, } int w,h; - + // Scale font description. gint oldSize = pango_font_description_get_size( m_fontdesc ); double size = oldSize; @@ -1779,15 +1814,15 @@ void wxGtkPrintDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, // Draw layout. gs_cairo->cairo_move_to (m_cairo, xx, yy); - + gs_cairo->cairo_save( m_cairo ); - + if (fabs(angle) > 0.00001) gs_cairo->cairo_rotate( m_cairo, angle*DEG2RAD ); - + gs_cairo->pango_cairo_update_layout (m_cairo, m_layout); gs_cairo->pango_cairo_show_layout (m_cairo, m_layout); - + gs_cairo->cairo_restore( m_cairo ); if (underlined) @@ -1795,7 +1830,7 @@ void wxGtkPrintDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, // Undo underline attributes setting pango_layout_set_attributes(m_layout, NULL); } - + // Reset unscaled size. pango_font_description_set_size( m_fontdesc, oldSize ); @@ -1832,7 +1867,7 @@ void wxGtkPrintDC::SetFont( const wxFont& font ) m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description ); // m_fontdesc is now set to device units // Scale font description from device units to pango units - gint oldSize = pango_font_description_get_size( m_fontdesc ); + gint oldSize = pango_font_description_get_size( m_fontdesc ); double size = oldSize *m_DEV2PS; // scale to cairo units pango_font_description_set_size( m_fontdesc, (gint)size ); // apply to description @@ -1847,10 +1882,14 @@ void wxGtkPrintDC::SetPen( const wxPen& pen ) m_pen = pen; - double width = (double) m_pen.GetWidth(); - if (width == 0) width = 0.1; + double width; + + if (m_pen.GetWidth() <= 0) + width = 0.1; + else + width = (double) m_pen.GetWidth(); - gs_cairo->cairo_set_line_width( m_cairo, (width * m_PS2DEV) / m_scaleX ); + gs_cairo->cairo_set_line_width( m_cairo, width * m_DEV2PS * m_scaleX ); static const double dotted[] = {2.0, 5.0}; static const double short_dashed[] = {4.0, 4.0}; static const double long_dashed[] = {4.0, 8.0}; @@ -2040,8 +2079,10 @@ void wxGtkPrintDC::SetBackground( const wxBrush& brush ) void wxGtkPrintDC::SetBackgroundMode(int mode) { - if (mode == wxSOLID) m_backgroundMode = wxSOLID; - else m_backgroundMode = wxTRANSPARENT; + if (mode == wxSOLID) + m_backgroundMode = wxSOLID; + else + m_backgroundMode = wxTRANSPARENT; } void wxGtkPrintDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height) @@ -2055,7 +2096,7 @@ void wxGtkPrintDC::DestroyClippingRegion() gs_cairo->cairo_reset_clip(m_cairo); } -bool wxGtkPrintDC::StartDoc(const wxString& message) +bool wxGtkPrintDC::StartDoc(const wxString& WXUNUSED(message)) { return true; } @@ -2115,12 +2156,7 @@ void wxGtkPrintDC::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoo } // Set layout's text - // FIXME-UTF8: wouldn't be needed if utf8_str() always returned a buffer -#if wxUSE_UNICODE_UTF8 - const char *dataUTF8 = string.utf8_str(); -#else - const wxCharBuffer dataUTF8 = string.utf8_str(); -#endif + const wxUTF8Buf dataUTF8 = string.utf8_str(); PangoFontDescription *desc = m_fontdesc; if (theFont) desc = theFont->GetNativeFontInfo()->description; @@ -2142,7 +2178,7 @@ void wxGtkPrintDC::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoo *width = wxRound( (double)w / m_scaleX * m_PS2DEV ); if (height) *height = wxRound( (double)h / m_scaleY * m_PS2DEV ); - + if (descent) { PangoLayoutIter *iter = pango_layout_get_iter(m_layout); @@ -2188,7 +2224,7 @@ void wxGtkPrintDC::SetPrintData(const wxPrintData& data) m_printData = data; } -void wxGtkPrintDC::SetResolution(int ppi) +void wxGtkPrintDC::SetResolution(int WXUNUSED(ppi)) { // We can't change ppi of the GtkPrintContext. // TODO: should we really support this? @@ -2255,11 +2291,11 @@ void wxGtkPrintPreview::DetermineScaling() m_previewPrintout->SetPPIScreen( (int) ((ScreenPixels.GetWidth() * 25.4) / ScreenMM.GetWidth()), (int) ((ScreenPixels.GetHeight() * 25.4) / ScreenMM.GetHeight()) ); - + // TODO !!!!!!!!!!!!!!! int resolution = 600; m_previewPrintout->SetPPIPrinter( resolution, resolution ); - + // Get width and height in points (1/72th of an inch) wxSize sizeDevUnits(paper->GetSizeDeviceUnits());