]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/gnome/gprint.cpp
wxChar* usage changed over to wxString in various places
[wxWidgets.git] / src / gtk / gnome / gprint.cpp
index 40f01e84bc3b61844b9990d3e2d6fb8bc9980fd2..12f74b4db3661ed4e1140a0bbcde82ace9893135 100644 (file)
@@ -1,12 +1,14 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        gnomeprint.cpp
-// Author:      XX
-// Created:     XX/XX/XX
-// Copyright:   XX
+// Name:        gprint.cpp
+// Author:      Robert Roebling
+// Purpose:     Implement GNOME printing support
+// Created:     09/20/04
+// Copyright:   Robert Roebling
+// Licence:     wxWindows Licence
 /////////////////////////////////////////////////////////////////////////////
 
-#ifdef __GNUG__
-    #pragma implementation "gprint.cpp"
+#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
+#pragma implementation "gprint.h"
 #endif
 
 // For compilers that support precompilation, includes "wx/wx.h".
     #pragma hdrstop
 #endif
 
-#include "gprint.h"
+#include "wx/gtk/gnome/gprint.h"
+
+#if wxUSE_LIBGNOMEPRINT
+
+#include "math.h"
+
 #include "wx/fontutil.h"
 #include "wx/printdlg.h"
 #include "wx/gtk/private.h"
@@ -207,11 +214,20 @@ bool wxGnomePrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt )
     wxGnomePrintNativeData *data = 
         (wxGnomePrintNativeData*) printdata.GetNativeData();
 
+    // The GnomePrintJob is temporarily stored in the 
+    // native print data as the native print setup dialog
+    // needs to access it.
     GnomePrintJob *job = data->GetPrintJob();
     m_gpc = gnome_print_job_get_context (job);
+
+    printout->SetIsPreview(false);
+
+    if (m_printDialogData.GetMinPage() < 1)
+        m_printDialogData.SetMinPage(1);
+    if (m_printDialogData.GetMaxPage() < 1)
+        m_printDialogData.SetMaxPage(9999);
     
     wxDC *dc;
-    
     if (prompt)
         dc = PrintDialog( parent );
     else
@@ -220,11 +236,26 @@ bool wxGnomePrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt )
     if (!dc)
     {
         gnome_print_job_close( job );
+        sm_lastError = wxPRINTER_ERROR;
         return false;
     }
 
+    wxSize ScreenPixels = wxGetDisplaySize();
+    wxSize ScreenMM = wxGetDisplaySizeMM();
+
+    printout->SetPPIScreen( (int) ((ScreenPixels.GetWidth() * 25.4) / ScreenMM.GetWidth()),
+                            (int) ((ScreenPixels.GetHeight() * 25.4) / ScreenMM.GetHeight()) );
+    printout->SetPPIPrinter( wxPostScriptDC::GetResolution(),
+                             wxPostScriptDC::GetResolution() );
+                             
     printout->SetDC(dc);
 
+    int w, h;
+    dc->GetSize(&w, &h);
+    printout->SetPageSizePixels((int)w, (int)h);
+    dc->GetSizeMM(&w, &h);
+    printout->SetPageSizeMM((int)w, (int)h);
+    
     printout->OnPreparePrinting();
     printout->OnBeginPrinting();
 
@@ -268,6 +299,7 @@ wxDC* wxGnomePrinter::PrintDialog( wxWindow *parent )
 
 bool wxGnomePrinter::Setup( wxWindow *parent )
 {
+    return false;
 }
 
 //-----------------------------------------------------------------------------
@@ -318,8 +350,8 @@ void wxGnomePrintDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
     
     SetPen( m_pen );
 
-       gnome_print_moveto ( m_gpc, XDEV2LOG(x1), YDEV2LOG(y1) );
-       gnome_print_lineto ( m_gpc, XDEV2LOG(x2), YDEV2LOG(y2) );
+       gnome_print_moveto ( m_gpc, XLOG2DEV(x1), YLOG2DEV(y1) );
+       gnome_print_lineto ( m_gpc, XLOG2DEV(x2), YLOG2DEV(y2) );
        gnome_print_stroke ( m_gpc);
        
     CalcBoundingBox( x1, y1 );
@@ -410,10 +442,36 @@ bool wxGnomePrintDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord
 
 void wxGnomePrintDC::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
 {
+    DoDrawBitmap( icon, x, y, true );
 }
 
 void wxGnomePrintDC::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool useMask )
 {
+    if (!bitmap.Ok()) return;
+    
+#if 0
+    // TODO do something clever here
+    if (bitmap.HasPixbuf())
+    {
+    }
+    else
+#endif
+    {
+        wxImage image = bitmap.ConvertToImage();
+
+        if (!image.Ok()) return;
+        
+        double matrix[6];
+       matrix[0] = XLOG2DEVREL(image.GetWidth());
+       matrix[1] = 0;
+       matrix[2] = 0;
+       matrix[3] = YLOG2DEVREL(image.GetHeight());
+       matrix[4] = XLOG2DEV(x);
+        matrix[5] = YLOG2DEV(y+image.GetHeight());
+       gnome_print_concat( m_gpc, matrix );
+           gnome_print_moveto(  m_gpc, 0, 0 );
+        gnome_print_rgbimage( m_gpc, (guchar*) image.GetData(), image.GetWidth(), image.GetHeight(), image.GetWidth()*3 );
+    }
 }
 
 void wxGnomePrintDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y )
@@ -441,8 +499,6 @@ void wxGnomePrintDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y )
     x = XLOG2DEV(x);
     y = YLOG2DEV(y);
     
-    wxPrintf( wxT("x,y: %d,%d\n"), x, y );
-
     bool underlined = m_font.Ok() && m_font.GetUnderlined();
 
 #if wxUSE_UNICODE
@@ -468,8 +524,58 @@ void wxGnomePrintDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y )
         pango_attr_list_unref(attrs);
     }
 
-       gnome_print_moveto (m_gpc, x, y);
-       gnome_print_pango_layout( m_gpc, m_layout );
+    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 );
+#if 0
+        if ( m_backgroundMode == wxSOLID )
+        {
+            gdk_gc_set_foreground(m_textGC, m_textBackgroundColour.GetColor());
+            gdk_draw_rectangle(m_window, m_textGC, TRUE, x, y, w, h);
+            gdk_gc_set_foreground(m_textGC, m_textForegroundColour.GetColor());
+        }
+#endif         
+        // Draw layout.
+       gnome_print_moveto (m_gpc, x, y);
+           gnome_print_pango_layout( m_gpc, m_layout );
+         
+        // reset unscaled size
+        pango_font_description_set_size( m_fontdesc, oldSize );
+         
+        // actually apply unscaled font
+        pango_layout_set_font_description( m_layout, m_fontdesc );
+    }
+    else
+    {
+        pango_layout_get_pixel_size( m_layout, &w, &h );
+#if 0
+        if ( m_backgroundMode == wxSOLID )
+        {
+            gdk_gc_set_foreground(m_textGC, m_textBackgroundColour.GetColor());
+            gdk_draw_rectangle(m_window, m_textGC, TRUE, x, y, w, h);
+            gdk_gc_set_foreground(m_textGC, m_textForegroundColour.GetColor());
+        }
+#endif        
+        // Draw layout.
+       gnome_print_moveto (m_gpc, x, y);
+           gnome_print_pango_layout( m_gpc, m_layout );
+    }
+
+
 
     if (underlined)
     {
@@ -534,6 +640,41 @@ void wxGnomePrintDC::SetPen( const wxPen& pen )
 
 void wxGnomePrintDC::SetBrush( const wxBrush& brush )
 {
+    if (!brush.Ok()) return;
+
+    m_brush = brush;
+
+    // Brush colour
+    unsigned char red = m_brush.GetColour().Red();
+    unsigned char blue = m_brush.GetColour().Blue();
+    unsigned char green = m_brush.GetColour().Green();
+
+    if (!m_colour)
+    {
+        // Anything not white is black
+        if (! (red == (unsigned char) 255 &&
+               blue == (unsigned char) 255 &&
+               green == (unsigned char) 255) )
+        {
+            red = (unsigned char) 0;
+            green = (unsigned char) 0;
+            blue = (unsigned char) 0;
+        }
+        // setgray here ?
+    }
+
+    if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
+    {
+        double redPS = (double)(red) / 255.0;
+        double bluePS = (double)(blue) / 255.0;
+        double greenPS = (double)(green) / 255.0;
+
+        gnome_print_setrgbcolor( m_gpc, redPS, bluePS, greenPS );
+
+        m_currentRed = red;
+        m_currentBlue = blue;
+        m_currentGreen = green;
+    }
 }
 
 void wxGnomePrintDC::SetLogicalFunction( int function )
@@ -583,11 +724,71 @@ wxCoord wxGnomePrintDC::GetCharWidth() const
     return 0;
 }
 
-void wxGnomePrintDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
+void wxGnomePrintDC::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoord *height,
                      wxCoord *descent,
                      wxCoord *externalLeading,
                      wxFont *theFont ) const
 {
+    if ( width )
+        *width = 0;
+    if ( height )
+        *height = 0;
+    if ( descent )
+        *descent = 0;
+    if ( externalLeading )
+        *externalLeading = 0;
+
+    if (string.IsEmpty())
+    {
+        return;
+    }
+    
+    // Set new font description
+    if (theFont)
+        pango_layout_set_font_description( m_layout, theFont->GetNativeFontInfo()->description );
+        
+    // Set layout's text
+#if wxUSE_UNICODE
+    const wxCharBuffer data = wxConvUTF8.cWC2MB( string );
+    const char *dataUTF8 = (const char *)data;
+#else
+    const wxWCharBuffer wdata = wxConvLocal.cMB2WC( string );
+    if ( !wdata )
+    {
+        if (width) (*width) = 0;
+        if (height) (*height) = 0;
+        return;
+    }
+    const wxCharBuffer data = wxConvUTF8.cWC2MB( wdata );
+    const char *dataUTF8 = (const char *)data;
+#endif
+
+    if ( !dataUTF8 )
+    {
+        // hardly ideal, but what else can we do if conversion failed?
+        return;
+    }
+
+    pango_layout_set_text( m_layout, dataUTF8, strlen(dataUTF8) );
+    int w,h;
+    pango_layout_get_pixel_size( m_layout, &w, &h );
+    
+    if (width)
+        *width = (wxCoord) w; 
+    if (height)
+        *height = (wxCoord) h;
+    if (descent)
+    {
+        PangoLayoutIter *iter = pango_layout_get_iter(m_layout);
+        int baseline = pango_layout_iter_get_baseline(iter);
+        pango_layout_iter_free(iter);
+        *descent = h - PANGO_PIXELS(baseline);
+    }
+    
+    // Reset old font description
+    if (theFont)
+        pango_layout_set_font_description( m_layout, m_fontdesc );
 }
 
 void wxGnomePrintDC::DoGetSize(int* width, int* height) const
@@ -609,8 +810,6 @@ void wxGnomePrintDC::DoGetSize(int* width, int* height) const
         *width = (int) w;
     if (height)
         *height = (int) h;
-        
-    wxPrintf( wxT("size %d,%d\n"), *width, *height );
 }
 
 void wxGnomePrintDC::DoGetSizeMM(int *width, int *height) const
@@ -657,3 +856,20 @@ int wxGnomePrintDC::GetResolution()
 {
     return 72;
 }
+
+
+class wxGnomePrintModule: public wxModule
+{
+public:
+    wxGnomePrintModule() {}
+    bool OnInit() { wxPrintFactory::SetPrintFactory( new wxGnomePrintFactory ); return true; }
+    void OnExit() { }
+    
+private:
+    DECLARE_DYNAMIC_CLASS(wxGnomePrintModule)
+};
+
+IMPLEMENT_DYNAMIC_CLASS(wxGnomePrintModule, wxModule)
+   
+#endif
+    // wxUSE_LIBGNOMEPRINT