X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/fa034c4511b0b3e9d7cfc9eb170ed063be92ca90..12bb29f5432174ecbd65549bda832d70d34a98ae:/src/gtk/print.cpp?ds=sidebyside diff --git a/src/gtk/print.cpp b/src/gtk/print.cpp index d3c20a3d0b..9b7e7a2ca0 100644 --- a/src/gtk/print.cpp +++ b/src/gtk/print.cpp @@ -1,9 +1,9 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: wx/gtk/print.cpp +// Name: src/gtk/print.cpp // Author: Anthony Bretaudeau // Purpose: GTK printing support // Created: 2007-08-25 -// RCS-ID: $Id: print.cpp,v 1 2007-08-25 05:44:44 PC Exp $ +// RCS-ID: $Id$ // Copyright: (c) 2007 wxWidgets development team // Licence: wxWindows Licence ///////////////////////////////////////////////////////////////////////////// @@ -22,20 +22,32 @@ #ifndef WX_PRECOMP #include "wx/log.h" #include "wx/dcmemory.h" +#include "wx/dcprint.h" #include "wx/icon.h" #include "wx/math.h" #include "wx/image.h" #include "wx/module.h" +#include "wx/crt.h" #endif #include "wx/fontutil.h" -#include "wx/gtk/private.h" #include "wx/dynlib.h" #include "wx/paper.h" -#include "wx/rawbmp.h" +#include "wx/scopeguard.h" +#include "wx/modalhook.h" #include + +#if GTK_CHECK_VERSION(2,14,0) +#include +#else #include +#endif + + +#if wxUSE_GRAPHICS_CONTEXT +#include "wx/graphics.h" +#endif #include "wx/link.h" wxFORCE_LINK_THIS_MODULE(gtk_print) @@ -44,7 +56,9 @@ wxFORCE_LINK_THIS_MODULE(gtk_print) #include "wx/gtk/gnome/gprint.h" #endif -// Usefull to convert angles from/to Rad to/from Deg. +#include "wx/gtk/private/object.h" + +// Useful to convert angles from/to Rad to/from Deg. static const double RAD2DEG = 180.0 / M_PI; static const double DEG2RAD = M_PI / 180.0; @@ -58,15 +72,15 @@ static const double DEG2RAD = M_PI / 180.0; class wxGtkPrintModule: public wxModule { public: - wxGtkPrintModule() + wxGtkPrintModule() { #if wxUSE_LIBGNOMEPRINT // This module must be initialized AFTER gnomeprint's one - AddDependency(CLASSINFO(wxGnomePrintModule)); + AddDependency(wxCLASSINFO(wxGnomePrintModule)); #endif } bool OnInit(); - void OnExit(); + void OnExit() {} private: DECLARE_DYNAMIC_CLASS(wxGtkPrintModule) @@ -74,19 +88,17 @@ private: bool wxGtkPrintModule::OnInit() { +#ifndef __WXGTK3__ if (gtk_check_version(2,10,0) == NULL) +#endif + { wxPrintFactory::SetPrintFactory( new wxGtkPrintFactory ); - + } return true; } -void wxGtkPrintModule::OnExit() -{ -} - IMPLEMENT_DYNAMIC_CLASS(wxGtkPrintModule, wxModule) - //---------------------------------------------------------------------------- // wxGtkPrintFactory //---------------------------------------------------------------------------- @@ -133,14 +145,16 @@ bool wxGtkPrintFactory::HasPrintSetupDialog() return false; } -wxDialog *wxGtkPrintFactory::CreatePrintSetupDialog( wxWindow *parent, wxPrintData *data ) +wxDialog * +wxGtkPrintFactory::CreatePrintSetupDialog(wxWindow * WXUNUSED(parent), + wxPrintData * WXUNUSED(data)) { return NULL; } -wxDC* wxGtkPrintFactory::CreatePrinterDC( const wxPrintData& data ) +wxDCImpl* wxGtkPrintFactory::CreatePrinterDCImpl( wxPrinterDC *owner, const wxPrintData& data ) { - return new wxGtkPrintDC(data); + return new wxGtkPrinterDCImpl( owner, data ); } bool wxGtkPrintFactory::HasOwnPrintToFile() @@ -180,13 +194,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" { @@ -204,25 +217,14 @@ 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) - { - wxPrintout *printout = (wxPrintout *) user_data; - - printout->SetIsPreview(true); - - /* We create a cairo context with 72dpi resolution. This resolution is only used for positionning. */ - cairo_t *cairo = gdk_cairo_create(GTK_WIDGET(parent)->window); - gtk_print_context_set_cairo_context(context, cairo, 72, 72); - - return false; - } } //---------------------------------------------------------------------------- @@ -234,11 +236,13 @@ IMPLEMENT_CLASS(wxGtkPrintNativeData, wxPrintNativeDataBase) wxGtkPrintNativeData::wxGtkPrintNativeData() { m_config = gtk_print_settings_new(); + m_job = NULL; + m_context = NULL; } wxGtkPrintNativeData::~wxGtkPrintNativeData() { - g_object_unref (m_config); + g_object_unref(m_config); } // Convert datas stored in m_config to a wxPrintData. @@ -248,15 +252,24 @@ bool wxGtkPrintNativeData::TransferTo( wxPrintData &data ) if(!m_config) return false; - GtkPrintQuality quality = gtk_print_settings_get_quality(m_config); - if (quality == GTK_PRINT_QUALITY_HIGH) - data.SetQuality(wxPRINT_QUALITY_HIGH); - else if (quality == GTK_PRINT_QUALITY_LOW) - data.SetQuality(wxPRINT_QUALITY_LOW); - else if (quality == GTK_PRINT_QUALITY_DRAFT) - data.SetQuality(wxPRINT_QUALITY_DRAFT); - else - data.SetQuality(wxPRINT_QUALITY_MEDIUM); + int resolution = gtk_print_settings_get_resolution(m_config); + if ( resolution > 0 ) + { + // if resolution is explicitly set, use it + data.SetQuality(resolution); + } + else // use more vague "quality" + { + GtkPrintQuality quality = gtk_print_settings_get_quality(m_config); + if (quality == GTK_PRINT_QUALITY_HIGH) + data.SetQuality(wxPRINT_QUALITY_HIGH); + else if (quality == GTK_PRINT_QUALITY_LOW) + data.SetQuality(wxPRINT_QUALITY_LOW); + else if (quality == GTK_PRINT_QUALITY_DRAFT) + data.SetQuality(wxPRINT_QUALITY_DRAFT); + else + data.SetQuality(wxPRINT_QUALITY_MEDIUM); + } data.SetNoCopies(gtk_print_settings_get_n_copies(m_config)); @@ -389,6 +402,9 @@ bool wxGtkPrintNativeData::TransferTo( wxPrintData &data ) data.SetPaperId( wxPAPER_FANFOLD_LGL_GERMAN); else data.SetPaperId(wxPAPER_NONE); + + data.SetPrinterName(gtk_print_settings_get_printer(m_config)); + return true; } @@ -537,6 +553,8 @@ bool wxGtkPrintNativeData::TransferFrom( const wxPrintData &data ) default: break; } + gtk_print_settings_set_printer(m_config, data.GetPrinterName().utf8_str()); + return true; } @@ -550,11 +568,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; } @@ -563,7 +581,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)); } //---------------------------------------------------------------------------- @@ -606,7 +624,7 @@ wxGtkPrintDialog::~wxGtkPrintDialog() // This is called even if we actually don't want the dialog to appear. int wxGtkPrintDialog::ShowModal() { - GtkPrintOperationResult response; + WX_HOOK_MODAL_DIALOG(); // We need to restore the settings given in the constructor. wxPrintData data = m_printDialogData.GetPrintData(); @@ -632,17 +650,26 @@ int wxGtkPrintDialog::ShowModal() gtk_print_settings_set_page_ranges (settings, range, 1); } + GtkPrintOperation * const printOp = native->GetPrintJob(); + // 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_print_settings (printOp, settings); + gtk_print_operation_set_default_page_setup (printOp, 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); + GtkPrintOperationResult response = gtk_print_operation_run + ( + printOp, + GetShowDialog() + ? GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG + : GTK_PRINT_OPERATION_ACTION_PRINT, + m_parent + ? GTK_WINDOW(gtk_widget_get_toplevel(m_parent->m_widget)) + : NULL, + &gError + ); // Does everything went well? if (response == GTK_PRINT_OPERATION_RESULT_CANCEL) @@ -651,37 +678,51 @@ int wxGtkPrintDialog::ShowModal() } else if (response == GTK_PRINT_OPERATION_RESULT_ERROR) { + wxLogError(_("Error while printing: ") + wxString(gError ? gError->message : "???")); g_error_free (gError); - wxLogError(_("Error while printing: ") + wxString::Format(_("%s"), gError->message)); return wxID_NO; // We use wxID_NO because there is no wxID_ERROR available } // Now get the settings and save it. - GtkPrintSettings* newSettings = gtk_print_operation_get_print_settings (native->GetPrintJob()); + GtkPrintSettings* newSettings = gtk_print_operation_get_print_settings(printOp); native->SetPrintConfig(newSettings); data.ConvertFromNative(); + // Set PrintDialogData variables + m_printDialogData.SetPrintData(data); + m_printDialogData.SetCollate(data.GetCollate()); + m_printDialogData.SetNoCopies(data.GetNoCopies()); + m_printDialogData.SetPrintToFile(data.GetPrinterName() == "Print to File"); + // Same problem as a few lines before. switch (gtk_print_settings_get_print_pages(newSettings)) { case GTK_PRINT_PAGES_CURRENT: m_printDialogData.SetSelection( true ); break; - case GTK_PRINT_PAGES_ALL: - m_printDialogData.SetAllPages( true ); - m_printDialogData.SetFromPage( 0 ); - m_printDialogData.SetToPage( 9999 ); - break; case GTK_PRINT_PAGES_RANGES: - default: - // wxWidgets doesn't support multiple ranges, so we can only save the first one even if the user wants to print others. + {// wxWidgets doesn't support multiple ranges, so we can only save the first one even if the user wants to print others. // For example, the user enters "1-3;5-7" in the dialog: pages 1-3 and 5-7 will be correctly printed when the user // will hit "OK" button. However we can only save 1-3 in the print data. gint num_ranges = 0; GtkPageRange* range; range = gtk_print_settings_get_page_ranges (newSettings, &num_ranges); - m_printDialogData.SetFromPage( range[0].start ); - m_printDialogData.SetToPage( range[0].end ); + if (num_ranges >= 1) + { + m_printDialogData.SetFromPage( range[0].start ); + m_printDialogData.SetToPage( range[0].end ); + } + else { + m_printDialogData.SetAllPages( true ); + m_printDialogData.SetFromPage( 0 ); + m_printDialogData.SetToPage( 9999 ); + } + break;} + case GTK_PRINT_PAGES_ALL: + default: + m_printDialogData.SetAllPages( true ); + m_printDialogData.SetFromPage( 0 ); + m_printDialogData.SetToPage( 9999 ); break; } @@ -709,6 +750,8 @@ wxGtkPageSetupDialog::~wxGtkPageSetupDialog() int wxGtkPageSetupDialog::ShowModal() { + WX_HOOK_MODAL_DIALOG(); + // Get the config. m_pageDialogData.GetPrintData().ConvertToNative(); wxGtkPrintNativeData *native = (wxGtkPrintNativeData*) m_pageDialogData.GetPrintData().GetNativeData(); @@ -730,43 +773,82 @@ int wxGtkPageSetupDialog::ShowModal() } } - // Now show the dialog. - GtkPageSetup* newPageSetup = gtk_print_run_page_setup_dialog (GTK_WINDOW(m_parent->m_widget), - oldPageSetup, - nativeData); - - int ret; - if (newPageSetup != oldPageSetup) - { - native->SetPageSetupToSettings(nativeData, newPageSetup); - m_pageDialogData.GetPrintData().ConvertFromNative(); - // Store custom paper format if any. - if (m_pageDialogData.GetPrintData().GetPaperId() == wxPAPER_NONE) - { - gdouble ml,mr,mt,mb,pw,ph; - ml = gtk_page_setup_get_left_margin (newPageSetup, GTK_UNIT_MM); - mr = gtk_page_setup_get_right_margin (newPageSetup, GTK_UNIT_MM); - mt = gtk_page_setup_get_top_margin (newPageSetup, GTK_UNIT_MM); - mb = gtk_page_setup_get_bottom_margin (newPageSetup, GTK_UNIT_MM); + // Set selected printer + gtk_print_settings_set(nativeData, "format-for-printer", + gtk_print_settings_get_printer(nativeData)); - pw = gtk_page_setup_get_paper_width (newPageSetup, GTK_UNIT_MM); - ph = gtk_page_setup_get_paper_height (newPageSetup, GTK_UNIT_MM); + // Create custom dialog + wxString title(GetTitle()); + if ( title.empty() ) + title = _("Page Setup"); + GtkWidget * + dlg = gtk_page_setup_unix_dialog_new(title.utf8_str(), + m_parent + ? GTK_WINDOW(m_parent->m_widget) + : NULL); - m_pageDialogData.SetMarginTopLeft( wxPoint( (int)(ml+0.5), (int)(mt+0.5)) ); - m_pageDialogData.SetMarginBottomRight( wxPoint( (int)(mr+0.5), (int)(mb+0.5)) ); + gtk_page_setup_unix_dialog_set_print_settings( + GTK_PAGE_SETUP_UNIX_DIALOG(dlg), nativeData); + gtk_page_setup_unix_dialog_set_page_setup( + GTK_PAGE_SETUP_UNIX_DIALOG(dlg), oldPageSetup); - m_pageDialogData.SetPaperSize( wxSize( (int)(pw+0.5), (int)(ph+0.5) ) ); - } + int result = gtk_dialog_run(GTK_DIALOG(dlg)); + gtk_widget_hide(dlg); - ret = wxID_OK; - } - else + switch ( result ) { - ret = wxID_CANCEL; + case GTK_RESPONSE_OK: + case GTK_RESPONSE_APPLY: + { + // Store Selected Printer Name + gtk_print_settings_set_printer + ( + nativeData, + gtk_print_settings_get(nativeData, "format-for-printer") + ); + + wxGtkObject + newPageSetup(gtk_page_setup_unix_dialog_get_page_setup( + GTK_PAGE_SETUP_UNIX_DIALOG(dlg))); + native->SetPageSetupToSettings(nativeData, newPageSetup); + + m_pageDialogData.GetPrintData().ConvertFromNative(); + + // Store custom paper format if any. + if ( m_pageDialogData.GetPrintData().GetPaperId() == wxPAPER_NONE ) + { + gdouble ml,mr,mt,mb,pw,ph; + ml = gtk_page_setup_get_left_margin (newPageSetup, GTK_UNIT_MM); + mr = gtk_page_setup_get_right_margin (newPageSetup, GTK_UNIT_MM); + mt = gtk_page_setup_get_top_margin (newPageSetup, GTK_UNIT_MM); + mb = gtk_page_setup_get_bottom_margin (newPageSetup, GTK_UNIT_MM); + + pw = gtk_page_setup_get_paper_width (newPageSetup, GTK_UNIT_MM); + ph = gtk_page_setup_get_paper_height (newPageSetup, GTK_UNIT_MM); + + m_pageDialogData.SetMarginTopLeft(wxPoint((int)(ml+0.5), + (int)(mt+0.5))); + m_pageDialogData.SetMarginBottomRight(wxPoint((int)(mr+0.5), + (int)(mb+0.5))); + + m_pageDialogData.SetPaperSize(wxSize((int)(pw+0.5), + (int)(ph+0.5))); + } + + result = wxID_OK; + } + break; + + default: + case GTK_RESPONSE_CANCEL: + result = wxID_CANCEL; + break; } - return ret; + gtk_widget_destroy(dlg); + + return result; } //---------------------------------------------------------------------------- @@ -827,31 +909,37 @@ bool wxGtkPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt ) wxPrintData printdata = GetPrintDialogData().GetPrintData(); wxGtkPrintNativeData *native = (wxGtkPrintNativeData*) printdata.GetNativeData(); - GtkPrintOperation *printOp = gtk_print_operation_new (); - - native->SetPrintJob( printOp ); - - printout->SetIsPreview(false); + wxGtkObject printOp(gtk_print_operation_new()); + native->SetPrintJob(printOp); + wxON_BLOCK_EXIT_OBJ1(*native, wxGtkPrintNativeData::SetPrintJob, + static_cast(NULL)); wxPrinterToGtkData dataToSend; 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; - - // 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; + // 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); - g_object_unref (printOp); + // 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.")); + } return (sm_lastError == wxPRINTER_NO_ERROR); } @@ -861,27 +949,31 @@ void wxGtkPrinter::BeginPrint(wxPrintout *printout, GtkPrintOperation *operation wxPrintData printdata = GetPrintDialogData().GetPrintData(); wxGtkPrintNativeData *native = (wxGtkPrintNativeData*) printdata.GetNativeData(); + // We need to update printdata with the new data from the dialog and we + // have to do this here because this method needs this new data and we + // cannot update it earlier + native->SetPrintConfig(gtk_print_operation_get_print_settings(operation)); + printdata.ConvertFromNative(); + SetPrintContext(context); native->SetPrintContext( context ); - m_dc = new wxGtkPrintDC( printdata ); + wxPrinterDC *printDC = new wxPrinterDC( printdata ); + m_dc = printDC; if (!m_dc->IsOk()) { if (sm_lastError != wxPRINTER_CANCELLED) { sm_lastError = wxPRINTER_ERROR; - wxFAIL_MSG(_("The wxGtkPrintDC cannot be used.")); + wxFAIL_MSG(_("The wxGtkPrinterDC cannot be used.")); } return; } - wxSize ScreenPixels = wxGetDisplaySize(); - wxSize ScreenMM = wxGetDisplaySizeMM(); - printout->SetPPIScreen( (int) ((ScreenPixels.GetWidth() * 25.4) / ScreenMM.GetWidth()), - (int) ((ScreenPixels.GetHeight() * 25.4) / ScreenMM.GetHeight()) ); - printout->SetPPIPrinter( wxGtkPrintDC::GetResolution(), - wxGtkPrintDC::GetResolution() ); + printout->SetPPIScreen(wxGetDisplayPPI()); + printout->SetPPIPrinter( printDC->GetResolution(), + printDC->GetResolution() ); printout->SetDC(m_dc); @@ -947,7 +1039,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); @@ -968,8 +1063,15 @@ void wxGtkPrinter::DrawPage(wxPrintout *printout, GtkPrintOperation *operation, GtkPageRange* range; range = gtk_print_settings_get_page_ranges (settings, &num_ranges); // We don't need to verify these values as it has already been done in wxGtkPrinter::BeginPrint. - startPage = range[0].start + 1; - endPage = range[0].end + 1; + if (num_ranges >= 1) + { + startPage = range[0].start + 1; + endPage = range[0].end + 1; + } + else { + startPage = minPage; + endPage = maxPage; + } break;} case GTK_PRINT_PAGES_ALL: default: @@ -1005,13 +1107,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) { @@ -1026,25 +1126,42 @@ wxDC* wxGtkPrinter::PrintDialog( wxWindow *parent ) } m_printDialogData = dialog.GetPrintDialogData(); - return new wxGtkPrintDC( m_printDialogData.GetPrintData() ); + + return new wxPrinterDC( m_printDialogData.GetPrintData() ); } -bool wxGtkPrinter::Setup( wxWindow *parent ) +bool wxGtkPrinter::Setup( wxWindow * WXUNUSED(parent) ) { // Obsolete, for backward compatibility. return false; } //----------------------------------------------------------------------------- -// wxGtkPrintDC +// wxGtkPrinterDC //----------------------------------------------------------------------------- -IMPLEMENT_CLASS(wxGtkPrintDC, wxDC) +#define wxCAIRO_SCALE 1 + +#if wxCAIRO_SCALE + +#define XLOG2DEV(x) LogicalToDeviceX(x) +#define XLOG2DEVREL(x) LogicalToDeviceXRel(x) +#define YLOG2DEV(x) LogicalToDeviceY(x) +#define YLOG2DEVREL(x) LogicalToDeviceYRel(x) -// Define the default resolution for this DC. This resolution is just used for positioning as the cairo context is scalable. -int wxGtkPrintDC::ms_resolution = 72; +#else + +#define XLOG2DEV(x) ((double)(LogicalToDeviceX(x)) * m_DEV2PS) +#define XLOG2DEVREL(x) ((double)(LogicalToDeviceXRel(x)) * m_DEV2PS) +#define YLOG2DEV(x) ((double)(LogicalToDeviceY(x)) * m_DEV2PS) +#define YLOG2DEVREL(x) ((double)(LogicalToDeviceYRel(x)) * m_DEV2PS) + +#endif + +IMPLEMENT_ABSTRACT_CLASS(wxGtkPrinterDCImpl, wxDCImpl) -wxGtkPrintDC::wxGtkPrintDC( const wxPrintData& data ) +wxGtkPrinterDCImpl::wxGtkPrinterDCImpl(wxPrinterDC *owner, const wxPrintData& data) + : wxDCImpl( owner ) { m_printData = data; @@ -1053,52 +1170,66 @@ wxGtkPrintDC::wxGtkPrintDC( const wxPrintData& data ) m_gpc = native->GetPrintContext(); - ms_resolution = (int) gtk_print_context_get_dpi_x(m_gpc); + // 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) + m_resolution = (1 << (m_resolution+4)) *150; + 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" ); m_cairo = gtk_print_context_get_cairo_context ( m_gpc ); +#if wxCAIRO_SCALE + m_PS2DEV = 1.0; + m_DEV2PS = 1.0; +#else + m_PS2DEV = (double)m_resolution / 72.0; + m_DEV2PS = 72.0 / (double)m_resolution; +#endif + m_currentRed = 0; 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. - - GetSize( &m_deviceOffsetX, &m_deviceOffsetY ); } -wxGtkPrintDC::~wxGtkPrintDC() +wxGtkPrinterDCImpl::~wxGtkPrinterDCImpl() { g_object_unref(m_context); g_object_unref(m_layout); } -bool wxGtkPrintDC::IsOk() const +bool wxGtkPrinterDCImpl::IsOk() const { - return true; + return m_gpc != NULL; } -void wxGtkPrintDC::ComputeScaleAndOrigin() +void* wxGtkPrinterDCImpl::GetCairoContext() const { - // Called when the scale and/or origin of the context has to be changed. - m_scaleX = m_logicalScaleX * m_userScaleX; - m_scaleY = m_logicalScaleY * m_userScaleY; + return (void*) cairo_reference( m_cairo ); +} - cairo_translate(m_cairo, m_deviceOriginX, m_deviceOriginY); - cairo_scale(m_cairo, m_scaleX, m_scaleY ); +void* wxGtkPrinterDCImpl::GetHandle() const +{ + return GetCairoContext(); } -bool wxGtkPrintDC::DoFloodFill(wxCoord x1, wxCoord y1, const wxColour &col, int style ) +bool wxGtkPrinterDCImpl::DoFloodFill(wxCoord WXUNUSED(x1), + wxCoord WXUNUSED(y1), + const wxColour& WXUNUSED(col), + wxFloodFillStyle 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; } -void wxGtkPrintDC::DoGradientFillConcentric(const wxRect& rect, const wxColour& initialColour, const wxColour& destColour, const wxPoint& circleCenter) +void wxGtkPrinterDCImpl::DoGradientFillConcentric(const wxRect& rect, const wxColour& initialColour, const wxColour& destColour, const wxPoint& circleCenter) { wxCoord xC = circleCenter.x; wxCoord yC = circleCenter.y; @@ -1107,7 +1238,8 @@ void wxGtkPrintDC::DoGradientFillConcentric(const wxRect& rect, const wxColour& wxCoord w = rect.width; wxCoord h = rect.height; - double radius = sqrt((w/2)*(w/2)+(h/2)*(h/2)); + const double r2 = (w/2)*(w/2)+(h/2)*(h/2); + double radius = sqrt(r2); unsigned char redI = initialColour.Red(); unsigned char blueI = initialColour.Blue(); @@ -1129,13 +1261,13 @@ void wxGtkPrintDC::DoGradientFillConcentric(const wxRect& rect, const wxColour& // Create a pattern with the gradient. cairo_pattern_t* gradient; - gradient = cairo_pattern_create_radial (LogicalToDeviceX(xC+xR), LogicalToDeviceY(yC+yR), 0, LogicalToDeviceX(xC+xR), LogicalToDeviceY(yC+yR), radius); + gradient = cairo_pattern_create_radial (XLOG2DEV(xC+xR), YLOG2DEV(yC+yR), 0, XLOG2DEV(xC+xR), YLOG2DEV(yC+yR), radius * m_DEV2PS ); cairo_pattern_add_color_stop_rgba (gradient, 0.0, redIPS, greenIPS, blueIPS, alphaIPS); cairo_pattern_add_color_stop_rgba (gradient, 1.0, redDPS, greenDPS, blueDPS, alphaDPS); // Fill the rectangle with this pattern. cairo_set_source(m_cairo, gradient); - cairo_rectangle (m_cairo, LogicalToDeviceX(xR), LogicalToDeviceY(yR), LogicalToDeviceXRel(w), LogicalToDeviceYRel(h) ); + cairo_rectangle (m_cairo, XLOG2DEV(xR), YLOG2DEV(yR), XLOG2DEVREL(w), YLOG2DEVREL(h) ); cairo_fill(m_cairo); cairo_pattern_destroy(gradient); @@ -1144,7 +1276,7 @@ void wxGtkPrintDC::DoGradientFillConcentric(const wxRect& rect, const wxColour& CalcBoundingBox(xR+w, yR+h); } -void wxGtkPrintDC::DoGradientFillLinear(const wxRect& rect, const wxColour& initialColour, const wxColour& destColour, wxDirection nDirection) +void wxGtkPrinterDCImpl::DoGradientFillLinear(const wxRect& rect, const wxColour& initialColour, const wxColour& destColour, wxDirection nDirection) { wxCoord x = rect.x; wxCoord y = rect.y; @@ -1171,7 +1303,7 @@ void wxGtkPrintDC::DoGradientFillLinear(const wxRect& rect, const wxColour& init // Create a pattern with the gradient. cairo_pattern_t* gradient; - gradient = cairo_pattern_create_linear (LogicalToDeviceX(x), LogicalToDeviceY(y), LogicalToDeviceX(x+w), LogicalToDeviceY(y)); + gradient = cairo_pattern_create_linear (XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x+w), YLOG2DEV(y)); if (nDirection == wxWEST) { @@ -1185,7 +1317,7 @@ void wxGtkPrintDC::DoGradientFillLinear(const wxRect& rect, const wxColour& init // Fill the rectangle with this pattern. cairo_set_source(m_cairo, gradient); - cairo_rectangle (m_cairo, LogicalToDeviceX(x), LogicalToDeviceY(y), LogicalToDeviceXRel(w), LogicalToDeviceYRel(h) ); + cairo_rectangle (m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(w), YLOG2DEVREL(h) ); cairo_fill(m_cairo); cairo_pattern_destroy(gradient); @@ -1194,49 +1326,46 @@ 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 wxGtkPrinterDCImpl::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; } -void wxGtkPrintDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2) +void wxGtkPrinterDCImpl::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2) { - if (m_pen.GetStyle() == wxTRANSPARENT) return; + if ( m_pen.IsTransparent() ) + return; SetPen( m_pen ); - cairo_move_to ( m_cairo, LogicalToDeviceX(x1), LogicalToDeviceY(y1) ); - cairo_line_to ( m_cairo, LogicalToDeviceX(x2), LogicalToDeviceY(y2) ); + cairo_move_to ( m_cairo, XLOG2DEV(x1), YLOG2DEV(y1) ); + cairo_line_to ( m_cairo, XLOG2DEV(x2), YLOG2DEV(y2) ); cairo_stroke ( m_cairo ); CalcBoundingBox( x1, y1 ); CalcBoundingBox( x2, y2 ); } -void wxGtkPrintDC::DoCrossHair(wxCoord x, wxCoord y) +void wxGtkPrinterDCImpl::DoCrossHair(wxCoord x, wxCoord y) { - int *w, *h; - w = new int; - h = new int; - DoGetSize(w, h); + int w, h; + DoGetSize(&w, &h); SetPen(m_pen); - cairo_move_to (m_cairo, LogicalToDeviceX(x), 0); - cairo_line_to (m_cairo, LogicalToDeviceX(x), *h); - cairo_move_to (m_cairo, 0, LogicalToDeviceY(y)); - cairo_line_to (m_cairo, *w, LogicalToDeviceY(y)); + cairo_move_to (m_cairo, XLOG2DEV(x), 0); + cairo_line_to (m_cairo, XLOG2DEV(x), YLOG2DEVREL(h)); + cairo_move_to (m_cairo, 0, YLOG2DEV(y)); + cairo_line_to (m_cairo, XLOG2DEVREL(w), YLOG2DEV(y)); cairo_stroke (m_cairo); CalcBoundingBox( 0, 0 ); - CalcBoundingBox( *w, *h ); - - delete w; - delete h; + CalcBoundingBox( w, h ); } -void wxGtkPrintDC::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord xc,wxCoord yc) +void wxGtkPrinterDCImpl::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord xc,wxCoord yc) { double dx = x1 - xc; double dy = y1 - yc; @@ -1271,8 +1400,10 @@ void wxGtkPrintDC::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord alpha1 *= DEG2RAD; alpha2 *= DEG2RAD; - cairo_arc_negative ( m_cairo, LogicalToDeviceX(xc), LogicalToDeviceY(yc), LogicalToDeviceXRel((int)radius), alpha1, alpha2); - cairo_line_to(m_cairo, LogicalToDeviceX(xc), LogicalToDeviceY(yc)); + cairo_new_path(m_cairo); + + cairo_arc_negative ( m_cairo, XLOG2DEV(xc), YLOG2DEV(yc), XLOG2DEVREL((int)radius), alpha1, alpha2); + cairo_line_to(m_cairo, XLOG2DEV(xc), YLOG2DEV(yc)); cairo_close_path (m_cairo); SetBrush( m_brush ); @@ -1286,15 +1417,17 @@ void wxGtkPrintDC::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord CalcBoundingBox (x2, y2); } -void wxGtkPrintDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea) +void wxGtkPrinterDCImpl::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea) { cairo_save( m_cairo ); - cairo_translate( m_cairo, LogicalToDeviceX((wxCoord) (x + w / 2.)), LogicalToDeviceX((wxCoord) (y + h / 2.)) ); - double scale = (double)LogicalToDeviceYRel(h) / (double) LogicalToDeviceXRel(w); + cairo_new_path(m_cairo); + + cairo_translate( m_cairo, XLOG2DEV((wxCoord) (x + w / 2.)), XLOG2DEV((wxCoord) (y + h / 2.)) ); + double scale = (double)YLOG2DEVREL(h) / (double) XLOG2DEVREL(w); cairo_scale( m_cairo, 1.0, scale ); - cairo_arc_negative ( m_cairo, 0, 0, LogicalToDeviceXRel(w/2), -sa*DEG2RAD, -ea*DEG2RAD); + cairo_arc_negative ( m_cairo, 0, 0, XLOG2DEVREL(w/2), -sa*DEG2RAD, -ea*DEG2RAD); SetPen (m_pen); cairo_stroke_preserve( m_cairo ); @@ -1310,22 +1443,25 @@ void wxGtkPrintDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,dou CalcBoundingBox( x+w, y+h ); } -void wxGtkPrintDC::DoDrawPoint(wxCoord x, wxCoord y) +void wxGtkPrinterDCImpl::DoDrawPoint(wxCoord x, wxCoord y) { - if (m_pen.GetStyle() == wxTRANSPARENT) return; + if ( m_pen.IsTransparent() ) + return; SetPen( m_pen ); - cairo_move_to ( m_cairo, LogicalToDeviceX(x), LogicalToDeviceY(y) ); - cairo_line_to ( m_cairo, LogicalToDeviceX(x), LogicalToDeviceY(y) ); + cairo_move_to ( m_cairo, XLOG2DEV(x), YLOG2DEV(y) ); + cairo_line_to ( m_cairo, XLOG2DEV(x), YLOG2DEV(y) ); cairo_stroke ( m_cairo ); CalcBoundingBox( x, y ); } -void wxGtkPrintDC::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset) +void wxGtkPrinterDCImpl::DoDrawLines(int n, const wxPoint points[], wxCoord xoffset, wxCoord yoffset) { - if (m_pen.GetStyle() == wxTRANSPARENT) return; + if ( m_pen.IsTransparent() ) + return; + if (n <= 0) return; @@ -1335,15 +1471,17 @@ void wxGtkPrintDC::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord for ( i =0; iGetFirst(); - 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 = @@ -1484,8 +1636,8 @@ void wxGtkPrintDC::DoDrawSpline(wxList *points) (double)(y1 + d) / 2; cairo_new_path( m_cairo ); - cairo_move_to( m_cairo, LogicalToDeviceX((wxCoord)x1), LogicalToDeviceY((wxCoord)y1) ); - cairo_line_to( m_cairo, LogicalToDeviceX((wxCoord)x3), LogicalToDeviceY((wxCoord)y3) ); + cairo_move_to( m_cairo, XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1) ); + cairo_line_to( m_cairo, XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) ); CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 ); CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 ); @@ -1493,7 +1645,7 @@ void wxGtkPrintDC::DoDrawSpline(wxList *points) node = node->GetNext(); while (node) { - q = (wxPoint *)node->GetData(); + q = node->GetData(); x1 = x3; y1 = y3; @@ -1505,9 +1657,9 @@ void wxGtkPrintDC::DoDrawSpline(wxList *points) y3 = (double)(y2 + d) / 2; cairo_curve_to(m_cairo, - LogicalToDeviceX((wxCoord)x1), LogicalToDeviceY((wxCoord)y1), - LogicalToDeviceX((wxCoord)x2), LogicalToDeviceY((wxCoord)y2), - LogicalToDeviceX((wxCoord)x3), LogicalToDeviceY((wxCoord)y3) ); + XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1), + XLOG2DEV((wxCoord)x2), YLOG2DEV((wxCoord)y2), + XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) ); CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 ); CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 ); @@ -1515,16 +1667,22 @@ void wxGtkPrintDC::DoDrawSpline(wxList *points) node = node->GetNext(); } - cairo_line_to ( m_cairo, LogicalToDeviceX((wxCoord)c), LogicalToDeviceY((wxCoord)d) ); + cairo_line_to ( m_cairo, XLOG2DEV((wxCoord)c), YLOG2DEV((wxCoord)d) ); cairo_stroke( m_cairo ); } #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 wxGtkPrinterDCImpl::DoBlit(wxCoord xdest, wxCoord ydest, + wxCoord width, wxCoord height, + wxDC *source, wxCoord xsrc, wxCoord ysrc, + wxRasterOperationMode 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. @@ -1535,227 +1693,50 @@ bool wxGtkPrintDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord h memDC.SelectObject(wxNullBitmap); // Draw bitmap. scaling and positioning is done there. - DrawBitmap( bitmap, xdest, ydest, useMask ); + GetOwner()->DrawBitmap( bitmap, xdest, ydest, useMask ); return true; } -void wxGtkPrintDC::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y ) +void wxGtkPrinterDCImpl::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y ) { DoDrawBitmap( icon, x, y, true ); } -void wxGtkPrintDC::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool useMask ) -{ - wxCHECK_RET( bitmap.IsOk(), wxT("Invalid bitmap in wxGtkPrintDC::DoDrawBitmap")); - - cairo_surface_t* surface; - x = LogicalToDeviceX(x); - y = LogicalToDeviceY(y); - int bw = bitmap.GetWidth(); - int bh = bitmap.GetHeight(); - wxBitmap bmpSource = bitmap; // we need a non-const instance. - unsigned char* buffer = new unsigned char[bw*bh*4]; - wxUint32* data = (wxUint32*)buffer; - - wxMask *mask = NULL; - if (useMask) mask = bmpSource.GetMask(); - - // Create a surface object and copy the bitmap pixel data to it. If the image has alpha (or a mask represented as alpha) - // then we'll use a different format and iterator than if it doesn't. - if (bmpSource.HasAlpha() || mask) - { - surface = cairo_image_surface_create_for_data( - buffer, CAIRO_FORMAT_ARGB32, bw, bh, bw*4); - wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh)); - wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data.")); - - wxAlphaPixelData::Iterator p(pixData); - int y, x; - for (y=0; ystart_index = 0; - a->end_index = datalen; - pango_attr_list_insert(attrs, a); - pango_layout_set_attributes(m_layout, attrs); - pango_attr_list_unref(attrs); - } + pango_layout_set_text(m_layout, data, data.length()); - if (m_textForegroundColour.Ok()) + const bool setAttrs = m_font.GTKSetPangoAttrs(m_layout); + if (m_textForegroundColour.IsOk()) { unsigned char red = m_textForegroundColour.Red(); unsigned char blue = m_textForegroundColour.Blue(); @@ -1821,154 +1784,100 @@ void wxGtkPrintDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, } } - int w,h; - - if (fabs(m_scaleY - 1.0) > 0.00001) - { - // If there is a user or actually any scale applied to the device context, scale the font. - - // Scale font description. - gint oldSize = pango_font_description_get_size( m_fontdesc ); - double size = oldSize; - size = size * m_scaleY; - pango_font_description_set_size( m_fontdesc, (gint)size ); - - // Actually apply scaled font. - pango_layout_set_font_description( m_layout, m_fontdesc ); - - pango_layout_get_pixel_size( m_layout, &w, &h ); - w = LogicalToDeviceXRel(w); - h = LogicalToDeviceYRel(h); - - if ( m_backgroundMode == wxSOLID ) - { - unsigned char red = m_textBackgroundColour.Red(); - unsigned char blue = m_textBackgroundColour.Blue(); - unsigned char green = m_textBackgroundColour.Green(); - unsigned char alpha = m_textBackgroundColour.Alpha(); + // Draw layout. + cairo_move_to (m_cairo, xx, yy); - double redPS = (double)(red) / 255.0; - double bluePS = (double)(blue) / 255.0; - double greenPS = (double)(green) / 255.0; - double alphaPS = (double)(alpha) / 255.0; + cairo_save( m_cairo ); - cairo_save(m_cairo); - cairo_translate(m_cairo, x, y); - cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS ); - cairo_rotate(m_cairo,angle*DEG2RAD); - cairo_rectangle(m_cairo, 0, 0, w, h); - cairo_fill(m_cairo); - cairo_restore(m_cairo); - } + if (fabs(angle) > 0.00001) + cairo_rotate( m_cairo, angle*DEG2RAD ); - // Draw layout. - cairo_move_to (m_cairo, x, y); - if (fabs(angle) > 0.00001) - { - cairo_save( m_cairo ); - cairo_rotate( m_cairo, angle*DEG2RAD ); - pango_cairo_update_layout (m_cairo, m_layout); - pango_cairo_show_layout (m_cairo, m_layout); - cairo_restore( m_cairo ); - } - else - { - pango_cairo_update_layout (m_cairo, m_layout); - pango_cairo_show_layout (m_cairo, m_layout); - } + cairo_scale(m_cairo, m_scaleX, m_scaleY); - // Reset unscaled size. - pango_font_description_set_size( m_fontdesc, oldSize ); + int w,h; + pango_layout_get_pixel_size( m_layout, &w, &h ); - // Actually apply unscaled font. - pango_layout_set_font_description( m_layout, m_fontdesc ); - } - else + if ( m_backgroundMode == wxBRUSHSTYLE_SOLID ) { - pango_layout_get_pixel_size( m_layout, &w, &h ); + unsigned char red = m_textBackgroundColour.Red(); + unsigned char blue = m_textBackgroundColour.Blue(); + unsigned char green = m_textBackgroundColour.Green(); + unsigned char alpha = m_textBackgroundColour.Alpha(); - if ( m_backgroundMode == wxSOLID ) - { - unsigned char red = m_textBackgroundColour.Red(); - unsigned char blue = m_textBackgroundColour.Blue(); - unsigned char green = m_textBackgroundColour.Green(); - unsigned char alpha = m_textBackgroundColour.Alpha(); + double redPS = (double)(red) / 255.0; + double bluePS = (double)(blue) / 255.0; + double greenPS = (double)(green) / 255.0; + double alphaPS = (double)(alpha) / 255.0; - double redPS = (double)(red) / 255.0; - double bluePS = (double)(blue) / 255.0; - double greenPS = (double)(green) / 255.0; - double alphaPS = (double)(alpha) / 255.0; + cairo_save(m_cairo); + cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS ); + cairo_rectangle(m_cairo, 0, 0, w, h); // still in cairo units + cairo_fill(m_cairo); + cairo_restore(m_cairo); + } - cairo_save(m_cairo); - cairo_translate(m_cairo, x, y); - cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS ); - cairo_rotate(m_cairo,angle*DEG2RAD); - cairo_rectangle(m_cairo, 0, 0, w, h); - cairo_fill(m_cairo); - cairo_restore(m_cairo); - } + pango_cairo_update_layout (m_cairo, m_layout); + pango_cairo_show_layout (m_cairo, m_layout); - // Draw layout. - cairo_move_to (m_cairo, x, y); - if (fabs(angle) > 0.00001) - { - cairo_save( m_cairo ); - cairo_rotate( m_cairo, angle*DEG2RAD ); - pango_cairo_update_layout (m_cairo, m_layout); - pango_cairo_show_layout (m_cairo, m_layout); - cairo_restore( m_cairo ); - } - else - { - pango_cairo_update_layout (m_cairo, m_layout); - pango_cairo_show_layout (m_cairo, m_layout); - } - } + cairo_restore( m_cairo ); - if (underlined) + if (setAttrs) { // Undo underline attributes setting pango_layout_set_attributes(m_layout, NULL); } - CalcBoundingBox (x,y); + // Back to device units: + CalcBoundingBox (x, y); CalcBoundingBox (x + w, y + h); } -void wxGtkPrintDC::Clear() +void wxGtkPrinterDCImpl::Clear() { +// Clear does nothing for printing, but keep the code +// for later reuse +/* cairo_save(m_cairo); cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE); SetBrush(m_backgroundBrush); cairo_paint(m_cairo); cairo_restore(m_cairo); +*/ } -void wxGtkPrintDC::SetFont( const wxFont& font ) +void wxGtkPrinterDCImpl::SetFont( const wxFont& font ) { m_font = font; - if (m_font.Ok()) + if (m_font.IsOk()) { if (m_fontdesc) pango_font_description_free( m_fontdesc ); m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description ); + float size = pango_font_description_get_size( m_fontdesc ); + size = size * GetFontPointSizeAdjustment(72.0); + pango_font_description_set_size( m_fontdesc, (gint)size ); + pango_layout_set_font_description( m_layout, m_fontdesc ); } } -void wxGtkPrintDC::SetPen( const wxPen& pen ) +void wxGtkPrinterDCImpl::SetPen( const wxPen& pen ) { - if (!pen.Ok()) return; + if (!pen.IsOk()) return; 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(); - cairo_set_line_width( m_cairo, LogicalToDeviceXRel( (wxCoord) (1000 * width )) / 1000.0f ); + 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}; @@ -1976,14 +1885,14 @@ void wxGtkPrintDC::SetPen( const wxPen& pen ) switch (m_pen.GetStyle()) { - case wxDOT: cairo_set_dash( m_cairo, dotted, 1, 0 ); break; - case wxSHORT_DASH: cairo_set_dash( m_cairo, short_dashed, 1, 0 ); break; - case wxLONG_DASH: cairo_set_dash( m_cairo, long_dashed, 1, 0 ); break; - case wxDOT_DASH: cairo_set_dash( m_cairo, dotted_dashed, 3, 0 ); break; - case wxUSER_DASH: + case wxPENSTYLE_DOT: cairo_set_dash( m_cairo, dotted, 2, 0 ); break; + case wxPENSTYLE_SHORT_DASH: cairo_set_dash( m_cairo, short_dashed, 2, 0 ); break; + case wxPENSTYLE_LONG_DASH: cairo_set_dash( m_cairo, long_dashed, 2, 0 ); break; + case wxPENSTYLE_DOT_DASH: cairo_set_dash( m_cairo, dotted_dashed, 4, 0 ); break; + case wxPENSTYLE_USER_DASH: { wxDash *wx_dashes; - int num = m_pen.GetDashes (&wx_dashes) - 1; + int num = m_pen.GetDashes (&wx_dashes); gdouble *g_dashes = g_new( gdouble, num ); int i; for (i = 0; i < num; ++i) @@ -1992,8 +1901,8 @@ void wxGtkPrintDC::SetPen( const wxPen& pen ) g_free( g_dashes ); } break; - case wxSOLID: - case wxTRANSPARENT: + case wxPENSTYLE_SOLID: + case wxPENSTYLE_TRANSPARENT: default: cairo_set_dash( m_cairo, NULL, 0, 0 ); break; } @@ -2034,12 +1943,22 @@ void wxGtkPrintDC::SetPen( const wxPen& pen ) } } -void wxGtkPrintDC::SetBrush( const wxBrush& brush ) +void wxGtkPrinterDCImpl::SetBrush( const wxBrush& brush ) { - if (!brush.Ok()) return; + if (!brush.IsOk()) return; m_brush = brush; + if (m_brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT) + { + cairo_set_source_rgba( m_cairo, 0, 0, 0, 0 ); + m_currentRed = 0; + m_currentBlue = 0; + m_currentGreen = 0; + m_currentAlpha = 0; + return; + } + // Brush colour. unsigned char red = m_brush.GetColour().Red(); unsigned char blue = m_brush.GetColour().Blue(); @@ -2073,31 +1992,31 @@ void wxGtkPrintDC::SetBrush( const wxBrush& brush ) switch (m_brush.GetStyle()) { - case wxCROSS_HATCH: + case wxBRUSHSTYLE_CROSS_HATCH: cairo_move_to(cr, 5, 0); cairo_line_to(cr, 5, 10); cairo_move_to(cr, 0, 5); cairo_line_to(cr, 10, 5); break; - case wxBDIAGONAL_HATCH: + case wxBRUSHSTYLE_BDIAGONAL_HATCH: cairo_move_to(cr, 0, 10); cairo_line_to(cr, 10, 0); break; - case wxFDIAGONAL_HATCH: + case wxBRUSHSTYLE_FDIAGONAL_HATCH: cairo_move_to(cr, 0, 0); cairo_line_to(cr, 10, 10); break; - case wxCROSSDIAG_HATCH: + case wxBRUSHSTYLE_CROSSDIAG_HATCH: cairo_move_to(cr, 0, 0); cairo_line_to(cr, 10, 10); cairo_move_to(cr, 10, 0); cairo_line_to(cr, 0, 10); break; - case wxHORIZONTAL_HATCH: + case wxBRUSHSTYLE_HORIZONTAL_HATCH: cairo_move_to(cr, 0, 5); cairo_line_to(cr, 10, 5); break; - case wxVERTICAL_HATCH: + case wxBRUSHSTYLE_VERTICAL_HATCH: cairo_move_to(cr, 5, 0); cairo_line_to(cr, 5, 10); break; @@ -2117,7 +2036,7 @@ void wxGtkPrintDC::SetBrush( const wxBrush& brush ) } } -void wxGtkPrintDC::SetLogicalFunction( int function ) +void wxGtkPrinterDCImpl::SetLogicalFunction( wxRasterOperationMode function ) { if (function == wxCLEAR) cairo_set_operator (m_cairo, CAIRO_OPERATOR_CLEAR); @@ -2135,7 +2054,7 @@ void wxGtkPrintDC::SetLogicalFunction( int function ) cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE); } -void wxGtkPrintDC::SetBackground( const wxBrush& brush ) +void wxGtkPrinterDCImpl::SetBackground( const wxBrush& brush ) { m_backgroundBrush = brush; cairo_save(m_cairo); @@ -2146,64 +2065,85 @@ void wxGtkPrintDC::SetBackground( const wxBrush& brush ) cairo_restore(m_cairo); } -void wxGtkPrintDC::SetBackgroundMode(int mode) +void wxGtkPrinterDCImpl::SetBackgroundMode(int mode) { - if (mode == wxSOLID) m_backgroundMode = wxSOLID; - else m_backgroundMode = wxTRANSPARENT; + if (mode == wxBRUSHSTYLE_SOLID) + m_backgroundMode = wxBRUSHSTYLE_SOLID; + else + m_backgroundMode = wxBRUSHSTYLE_TRANSPARENT; } -void wxGtkPrintDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height) +void wxGtkPrinterDCImpl::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height) { - cairo_rectangle ( m_cairo, LogicalToDeviceX(x), LogicalToDeviceY(y), LogicalToDeviceXRel(width), LogicalToDeviceYRel(height)); + cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height)); cairo_clip(m_cairo); + + wxDCImpl::DoSetClippingRegion(x, y, width, height); } -void wxGtkPrintDC::DestroyClippingRegion() +void wxGtkPrinterDCImpl::DestroyClippingRegion() { cairo_reset_clip(m_cairo); } -bool wxGtkPrintDC::StartDoc(const wxString& message) +bool wxGtkPrinterDCImpl::StartDoc(const wxString& WXUNUSED(message)) { return true; } -void wxGtkPrintDC::EndDoc() +void wxGtkPrinterDCImpl::EndDoc() { return; } -void wxGtkPrintDC::StartPage() +void wxGtkPrinterDCImpl::StartPage() { - return; + // Notice that we may change the Cairo transformation matrix only here and + // not before (e.g. in wxGtkPrinterDCImpl ctor as we used to do) in order + // to not affect _gtk_print_context_rotate_according_to_orientation() which + // is used in GTK+ itself and wouldn't work correctly if we applied these + // transformations before it is called. + + // 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); + mt = gtk_page_setup_get_top_margin (setup, GTK_UNIT_POINTS); + cairo_translate(m_cairo, -ml, -mt); + +#if wxCAIRO_SCALE + cairo_scale( m_cairo, 72.0 / (double)m_resolution, 72.0 / (double)m_resolution ); +#endif } -void wxGtkPrintDC::EndPage() +void wxGtkPrinterDCImpl::EndPage() { return; } -wxCoord wxGtkPrintDC::GetCharHeight() const +wxCoord wxGtkPrinterDCImpl::GetCharHeight() const { pango_layout_set_text( m_layout, "H", 1 ); int w,h; pango_layout_get_pixel_size( m_layout, &w, &h ); - return DeviceToLogicalYRel(h); + return wxRound( h * m_PS2DEV ); } -wxCoord wxGtkPrintDC::GetCharWidth() const +wxCoord wxGtkPrinterDCImpl::GetCharWidth() const { pango_layout_set_text( m_layout, "H", 1 ); int w,h; pango_layout_get_pixel_size( m_layout, &w, &h ); - return DeviceToLogicalXRel(w); + return wxRound( w * m_PS2DEV ); } -void wxGtkPrintDC::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoord *height, +void wxGtkPrinterDCImpl::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoord *height, wxCoord *descent, wxCoord *externalLeading, const wxFont *theFont ) const @@ -2222,34 +2162,31 @@ void wxGtkPrintDC::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoo return; } - // 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 + cairo_save( m_cairo ); + cairo_scale(m_cairo, m_scaleX, m_scaleY); - PangoFontDescription *desc = m_fontdesc; - if (theFont) desc = theFont->GetNativeFontInfo()->description; + // Set layout's text + const wxScopedCharBuffer dataUTF8 = string.utf8_str(); - gint oldSize = pango_font_description_get_size( desc ); - double size = oldSize; - size = size * m_scaleY; - pango_font_description_set_size( desc, (gint)size ); + gint oldSize=0; + if ( theFont ) + { + // scale the font and apply it + PangoFontDescription *desc = theFont->GetNativeFontInfo()->description; + oldSize = pango_font_description_get_size(desc); + const float size = oldSize * GetFontPointSizeAdjustment(72.0); + pango_font_description_set_size(desc, (gint)size); - // apply scaled font - pango_layout_set_font_description( m_layout, desc ); + pango_layout_set_font_description(m_layout, desc); + } pango_layout_set_text( m_layout, dataUTF8, strlen(dataUTF8) ); - int w, h; - pango_layout_get_pixel_size( m_layout, &w, &h ); + int h; + pango_layout_get_pixel_size( m_layout, width, &h ); + if ( height ) + *height = h; - if (width) - *width = (wxCoord)(w / m_scaleX); - if (height) - *height = (wxCoord)(h / m_scaleY); if (descent) { PangoLayoutIter *iter = pango_layout_get_iter(m_layout); @@ -2258,69 +2195,62 @@ void wxGtkPrintDC::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoo *descent = h - PANGO_PIXELS(baseline); } - // Reset unscaled size. - pango_font_description_set_size( desc, oldSize ); + if ( theFont ) + { + // restore font and reset font's size back + pango_layout_set_font_description(m_layout, m_fontdesc); - // Reset unscaled font. - pango_layout_set_font_description( m_layout, m_fontdesc ); -} + PangoFontDescription *desc = theFont->GetNativeFontInfo()->description; + pango_font_description_set_size(desc, oldSize); + } -void wxGtkPrintDC::DoGetSize(int* width, int* height) const -{ - if (width) - *width = (int) (gtk_print_context_get_width( m_gpc ) + 0.5); - if (height) - *height = (int) (gtk_print_context_get_height( m_gpc ) + 0.5); + cairo_restore( m_cairo ); } -void wxGtkPrintDC::DoGetSizeMM(int *width, int *height) const +void wxGtkPrinterDCImpl::DoGetSize(int* width, int* height) const { - // This function takes margins into consideration. - gdouble w = gtk_page_setup_get_page_width( gtk_print_context_get_page_setup( m_gpc ), GTK_UNIT_MM); - gdouble h = gtk_page_setup_get_page_height( gtk_print_context_get_page_setup( m_gpc ), GTK_UNIT_MM); + GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc ); if (width) - *width = (int) (w + 0.5); + *width = wxRound( (double)gtk_page_setup_get_paper_width( setup, GTK_UNIT_POINTS ) * (double)m_resolution / 72.0 ); if (height) - *height = (int) (h + 0.5); + *height = wxRound( (double)gtk_page_setup_get_paper_height( setup, GTK_UNIT_POINTS ) * (double)m_resolution / 72.0 ); } -wxSize wxGtkPrintDC::GetPPI() const +void wxGtkPrinterDCImpl::DoGetSizeMM(int *width, int *height) const { - gdouble xDpi = gtk_print_context_get_dpi_x( m_gpc ); - gdouble yDpi = gtk_print_context_get_dpi_y( m_gpc ); - return wxSize((int) xDpi,(int) yDpi); -} + GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc ); -void wxGtkPrintDC::SetLogicalOrigin( wxCoord x, wxCoord y ) -{ - wxDC::SetLogicalOrigin( x, y ); + if (width) + *width = wxRound( gtk_page_setup_get_paper_width( setup, GTK_UNIT_MM ) ); + if (height) + *height = wxRound( gtk_page_setup_get_paper_height( setup, GTK_UNIT_MM ) ); } -void wxGtkPrintDC::SetDeviceOrigin( wxCoord x, wxCoord y ) +wxSize wxGtkPrinterDCImpl::GetPPI() const { - wxDC::SetDeviceOrigin( x, y ); + return wxSize( (int)m_resolution, (int)m_resolution ); } -void wxGtkPrintDC::SetPrintData(const wxPrintData& data) +void wxGtkPrinterDCImpl::SetPrintData(const wxPrintData& data) { m_printData = data; - - if (m_printData.GetOrientation() == wxPORTRAIT) - GetSize( &m_deviceOffsetX, &m_deviceOffsetY ); - else - GetSize( &m_deviceOffsetY, &m_deviceOffsetX ); } -void wxGtkPrintDC::SetResolution(int ppi) +// overridden for wxPrinterDC Impl + +wxRect wxGtkPrinterDCImpl::GetPaperRect() const { - // We can't change ppi of the GtkPrintContext. - ms_resolution = ppi; + // Does GtkPrint support printer margins? + int w = 0; + int h = 0; + DoGetSize( &w, &h ); + return wxRect( 0,0,w,h ); } -int wxGtkPrintDC::GetResolution() +int wxGtkPrinterDCImpl::GetResolution() const { - return ms_resolution; + return m_resolution; } // ---------------------------------------------------------------------------- @@ -2330,25 +2260,59 @@ int wxGtkPrintDC::GetResolution() IMPLEMENT_CLASS(wxGtkPrintPreview, wxPrintPreviewBase) void wxGtkPrintPreview::Init(wxPrintout * WXUNUSED(printout), - wxPrintout * WXUNUSED(printoutForPrinting)) + wxPrintout * WXUNUSED(printoutForPrinting), + wxPrintData *data) { + // convert wxPrintQuality to resolution (input pointer can be NULL) + wxPrintQuality quality = data ? data->GetQuality() : wxPRINT_QUALITY_MEDIUM; + switch ( quality ) + { + case wxPRINT_QUALITY_HIGH: + m_resolution = 1200; + break; + + case wxPRINT_QUALITY_LOW: + m_resolution = 300; + break; + + case wxPRINT_QUALITY_DRAFT: + m_resolution = 150; + break; + + default: + if ( quality > 0 ) + { + // positive values directly indicate print resolution + m_resolution = quality; + break; + } + + wxFAIL_MSG( "unknown print quality" ); + // fall through + + case wxPRINT_QUALITY_MEDIUM: + m_resolution = 600; + break; + + } + DetermineScaling(); } wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout, - wxPrintout *printoutForPrinting, - wxPrintDialogData *data) - : wxPrintPreviewBase(printout, printoutForPrinting, data) + wxPrintout *printoutForPrinting, + wxPrintDialogData *data) + : wxPrintPreviewBase(printout, printoutForPrinting, data) { - Init(printout, printoutForPrinting); + Init(printout, printoutForPrinting, data ? &data->GetPrintData() : NULL); } wxGtkPrintPreview::wxGtkPrintPreview(wxPrintout *printout, - wxPrintout *printoutForPrinting, - wxPrintData *data) - : wxPrintPreviewBase(printout, printoutForPrinting, data) + wxPrintout *printoutForPrinting, + wxPrintData *data) + : wxPrintPreviewBase(printout, printoutForPrinting, data) { - Init(printout, printoutForPrinting); + Init(printout, printoutForPrinting, data); } wxGtkPrintPreview::~wxGtkPrintPreview() @@ -2374,17 +2338,20 @@ void wxGtkPrintPreview::DetermineScaling() if (paper) { - wxSize ScreenPixels = wxGetDisplaySize(); - wxSize ScreenMM = wxGetDisplaySizeMM(); + const wxSize screenPPI = wxGetDisplayPPI(); + int logPPIScreenX = screenPPI.GetWidth(); + int logPPIScreenY = screenPPI.GetHeight(); + int logPPIPrinterX = m_resolution; + int logPPIPrinterY = m_resolution; + + m_previewPrintout->SetPPIScreen( logPPIScreenX, logPPIScreenY ); + m_previewPrintout->SetPPIPrinter( logPPIPrinterX, logPPIPrinterY ); - m_previewPrintout->SetPPIScreen( (int) ((ScreenPixels.GetWidth() * 25.4) / ScreenMM.GetWidth()), - (int) ((ScreenPixels.GetHeight() * 25.4) / ScreenMM.GetHeight()) ); - m_previewPrintout->SetPPIPrinter(wxGtkPrintDC::GetResolution(), wxGtkPrintDC::GetResolution()); // Get width and height in points (1/72th of an inch) wxSize sizeDevUnits(paper->GetSizeDeviceUnits()); + sizeDevUnits.x = wxRound((double)sizeDevUnits.x * (double)m_resolution / 72.0); + sizeDevUnits.y = wxRound((double)sizeDevUnits.y * (double)m_resolution / 72.0); - sizeDevUnits.x = (wxCoord)((float)sizeDevUnits.x * wxGtkPrintDC::GetResolution() / 72.0); - sizeDevUnits.y = (wxCoord)((float)sizeDevUnits.y * wxGtkPrintDC::GetResolution() / 72.0); wxSize sizeTenthsMM(paper->GetSize()); wxSize sizeMM(sizeTenthsMM.x / 10, sizeTenthsMM.y / 10); @@ -2405,8 +2372,8 @@ void wxGtkPrintPreview::DetermineScaling() m_previewPrintout->SetPaperRectPixels(wxRect(0, 0, m_pageWidth, m_pageHeight)); // At 100%, the page should look about page-size on the screen. - m_previewScaleX = (float)0.8 * 72.0 / (float)wxGtkPrintDC::GetResolution(); - m_previewScaleY = m_previewScaleX; + m_previewScaleX = float(logPPIScreenX) / logPPIPrinterX; + m_previewScaleY = float(logPPIScreenY) / logPPIPrinterY; } }