]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/print.cpp
use the lower-case version of the parameter in GetEndianness() as intended (thanks...
[wxWidgets.git] / src / gtk / print.cpp
index 3ee388df0e990e9e1863a28d4048296b698e4fd2..d1f3080828ed6260990a0db5f0658026b2aba364 100644 (file)
@@ -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
@@ -183,12 +184,11 @@ wxPrintNativeDataBase *wxGtkPrintFactory::CreatePrintNativeData()
 //----------------------------------------------------------------------------
 
 // We use it to pass useful objets to gtk printing callback functions.
-typedef struct
+struct wxPrinterToGtkData
 {
    wxGtkPrinter * printer;
    wxPrintout * printout;
-}
-wxPrinterToGtkData;
+};
 
 extern "C"
 {
@@ -552,11 +552,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 +565,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 +637,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 +678,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 +847,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);
 
@@ -981,8 +991,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 +1033,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)
     {
@@ -1073,16 +1081,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 +1099,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);
@@ -1744,7 +1751,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 +1786,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 +1802,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 +1839,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 +1854,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};
@@ -2142,7 +2153,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);
@@ -2255,11 +2266,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());