/////////////////////////////////////////////////////////////////////////////
// Name:        wx/gtk/print.h
// Author:      Anthony Bretaudeau
// Purpose:     GTK printing support
// Created:     2007-08-25
// RCS-ID:      $Id: print.h,v 1 2007-08-25 05:44:44 PC Exp $
// Copyright:   (c) Anthony Bretaudeau
// Licence:     wxWindows Licence
/////////////////////////////////////////////////////////////////////////////

#ifndef _WX_GTK_PRINT_H_
#define _WX_GTK_PRINT_H_

#include "wx/defs.h"

#if wxUSE_GTKPRINT

#include "wx/print.h"
#include "wx/printdlg.h"
#include "wx/prntbase.h"
#include "wx/dc.h"
#include "wx/cairo.h"


typedef struct _GtkPrintOperation GtkPrintOperation;
typedef struct _GtkPrintContext GtkPrintContext;
typedef struct _GtkPrintSettings GtkPrintSettings;
typedef struct _GtkPageSetup GtkPageSetup;

typedef struct _cairo cairo_t;

//----------------------------------------------------------------------------
// wxGtkPrintFactory
//----------------------------------------------------------------------------

class wxGtkPrintFactory: public wxPrintFactory
{
public:
    virtual wxPrinterBase *CreatePrinter( wxPrintDialogData *data );

    virtual wxPrintPreviewBase *CreatePrintPreview( wxPrintout *preview,
                                                    wxPrintout *printout = NULL,
                                                    wxPrintDialogData *data = NULL );
    virtual wxPrintPreviewBase *CreatePrintPreview( wxPrintout *preview,
                                                    wxPrintout *printout,
                                                    wxPrintData *data );

    virtual wxPrintDialogBase *CreatePrintDialog( wxWindow *parent,
                                                  wxPrintDialogData *data = NULL );
    virtual wxPrintDialogBase *CreatePrintDialog( wxWindow *parent,
                                                  wxPrintData *data );

    virtual wxPageSetupDialogBase *CreatePageSetupDialog( wxWindow *parent,
                                                          wxPageSetupDialogData * data = NULL );

    virtual wxDC* CreatePrinterDC( const wxPrintData& data );

    virtual bool HasPrintSetupDialog();
    virtual wxDialog *CreatePrintSetupDialog( wxWindow *parent, wxPrintData *data );
    virtual bool HasOwnPrintToFile();
    virtual bool HasPrinterLine();
    virtual wxString CreatePrinterLine();
    virtual bool HasStatusLine();
    virtual wxString CreateStatusLine();

    virtual wxPrintNativeDataBase *CreatePrintNativeData();
};

//----------------------------------------------------------------------------
// wxGtkPrintDialog
//----------------------------------------------------------------------------

class WXDLLIMPEXP_CORE wxGtkPrintDialog: public wxPrintDialogBase
{
public:
    wxGtkPrintDialog( wxWindow *parent,
                         wxPrintDialogData* data = NULL );
    wxGtkPrintDialog( wxWindow *parent, wxPrintData* data);
    virtual ~wxGtkPrintDialog();

    wxPrintData& GetPrintData()
        { return m_printDialogData.GetPrintData(); }
    wxPrintDialogData& GetPrintDialogData()
        { return m_printDialogData; }

    wxDC *GetPrintDC() { return m_dc; }
    void SetPrintDC(wxDC * printDC) { m_dc = printDC; }

    virtual int ShowModal();

    virtual bool Validate() { return true; }
    virtual bool TransferDataToWindow() { return true; }
    virtual bool TransferDataFromWindow() { return true; }

    void SetShowDialog(bool show) { m_showDialog = show; }
    bool GetShowDialog() { return m_showDialog; }

protected:
    // Implement some base class methods to do nothing to avoid asserts and
    // GTK warnings, since this is not a real wxDialog.
    virtual void DoSetSize(int WXUNUSED(x), int WXUNUSED(y),
                           int WXUNUSED(width), int WXUNUSED(height),
                           int WXUNUSED(sizeFlags) = wxSIZE_AUTO) {}
    virtual void DoMoveWindow(int WXUNUSED(x), int WXUNUSED(y),
                              int WXUNUSED(width), int WXUNUSED(height)) {}

private:
    wxPrintDialogData    m_printDialogData;
    wxWindow            *m_parent;
    bool                 m_showDialog;
    wxDC                *m_dc;

    DECLARE_DYNAMIC_CLASS(wxGtkPrintDialog)
};

//----------------------------------------------------------------------------
// wxGtkPageSetupDialog
//----------------------------------------------------------------------------

class WXDLLIMPEXP_CORE wxGtkPageSetupDialog: public wxPageSetupDialogBase
{
public:
    wxGtkPageSetupDialog( wxWindow *parent,
                            wxPageSetupDialogData* data = NULL );
    virtual ~wxGtkPageSetupDialog();

    virtual wxPageSetupDialogData& GetPageSetupDialogData() { return m_pageDialogData; }

    virtual int ShowModal();

    virtual bool Validate() { return true; }
    virtual bool TransferDataToWindow() { return true; }
    virtual bool TransferDataFromWindow() { return true; }

protected:
    // Implement some base class methods to do nothing to avoid asserts and
    // GTK warnings, since this is not a real wxDialog.
    virtual void DoSetSize(int WXUNUSED(x), int WXUNUSED(y),
                           int WXUNUSED(width), int WXUNUSED(height),
                           int WXUNUSED(sizeFlags) = wxSIZE_AUTO) {}
    virtual void DoMoveWindow(int WXUNUSED(x), int WXUNUSED(y),
                              int WXUNUSED(width), int WXUNUSED(height)) {}

private:
    wxPageSetupDialogData    m_pageDialogData;
    wxWindow                *m_parent;

    DECLARE_DYNAMIC_CLASS(wxGtkPageSetupDialog)
};

//----------------------------------------------------------------------------
// wxGtkPrinter
//----------------------------------------------------------------------------

class WXDLLIMPEXP_CORE wxGtkPrinter : public wxPrinterBase
{
public:
    wxGtkPrinter(wxPrintDialogData *data = NULL);
    virtual ~wxGtkPrinter();

    virtual bool Print(wxWindow *parent,
                       wxPrintout *printout,
                       bool prompt = true);
    virtual wxDC* PrintDialog(wxWindow *parent);
    virtual bool Setup(wxWindow *parent);

    GtkPrintContext *GetPrintContext() { return m_gpc; }
    void SetPrintContext(GtkPrintContext *context) {m_gpc = context;}
    void BeginPrint(wxPrintout *printout, GtkPrintOperation *operation, GtkPrintContext *context);
    void DrawPage(wxPrintout *printout, GtkPrintOperation *operation, GtkPrintContext *context, int page_nr);

private:
    GtkPrintContext *m_gpc;
    bool             m_showDialog;
    wxDC            *m_dc;

    DECLARE_DYNAMIC_CLASS(wxGtkPrinter)
    DECLARE_NO_COPY_CLASS(wxGtkPrinter)
};

//----------------------------------------------------------------------------
// wxGtkPrintNativeData
//----------------------------------------------------------------------------

class WXDLLIMPEXP_CORE wxGtkPrintNativeData : public wxPrintNativeDataBase
{
public:
    wxGtkPrintNativeData();
    virtual ~wxGtkPrintNativeData();

    virtual bool TransferTo( wxPrintData &data );
    virtual bool TransferFrom( const wxPrintData &data );

    virtual bool Ok() const { return IsOk(); }
    virtual bool IsOk() const { return true; }

    GtkPrintSettings* GetPrintConfig() { return m_config; }
    void SetPrintConfig( GtkPrintSettings * config );

    void SetPrintJob( GtkPrintOperation *job ) { m_job = job; }
    GtkPrintOperation* GetPrintJob() { return m_job; }

    GtkPrintContext *GetPrintContext() { return m_context; }
    void SetPrintContext(GtkPrintContext *context) {m_context = context; }


    GtkPageSetup* GetPageSetupFromSettings(GtkPrintSettings* settings);
    void SetPageSetupToSettings(GtkPrintSettings* settings, GtkPageSetup* page_setup);

private:
    GtkPrintSettings    *m_config;
    GtkPrintOperation   *m_job;
    GtkPrintContext     *m_context;

    DECLARE_DYNAMIC_CLASS(wxGtkPrintNativeData)
};

//-----------------------------------------------------------------------------
// wxGtkPrintDC
//-----------------------------------------------------------------------------

class WXDLLIMPEXP_CORE wxGtkPrintDC: public wxDC
{
public:
    wxGtkPrintDC( const wxPrintData& data );
    virtual ~wxGtkPrintDC();

    bool Ok() const { return IsOk(); }
    bool IsOk() const;

    bool CanDrawBitmap() const { return true; }
    void Clear();
    void SetFont( const wxFont& font );
    void SetPen( const wxPen& pen );
    void SetBrush( const wxBrush& brush );
    void SetLogicalFunction( int function );
    void SetBackground( const wxBrush& brush );
    void DestroyClippingRegion();
    bool StartDoc(const wxString& message);
    void EndDoc();
    void StartPage();
    void EndPage();
    wxCoord GetCharHeight() const;
    wxCoord GetCharWidth() const;
    bool CanGetTextExtent() const { return true; }
    wxSize GetPPI() const;
    void SetLogicalOrigin( wxCoord x, wxCoord y );
    void SetDeviceOrigin( wxCoord x, wxCoord y );
    virtual int GetDepth() const { return 24; }
    void SetBackgroundMode(int WXUNUSED(mode));
    void SetPalette(const wxPalette& WXUNUSED(palette)) { }
    static void SetResolution(int ppi);
    static int GetResolution();
    void DrawScaledBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, wxCoord w, wxCoord h,
                    bool useMask = false, int quality = wxIMAGE_QUALITY_NORMAL)
        { DoDrawScaledBitmap( bmp, x, y, w, h, useMask, quality ); }
    void DrawScaledBitmap(const wxBitmap &bmp, const wxPoint& pt, const wxSize& sz,
                    bool useMask = false, int quality = wxIMAGE_QUALITY_NORMAL)
        { DoDrawScaledBitmap( bmp, pt.x, pt.y, sz.x, sz.y, useMask, quality ); }
    void DrawScaledBitmap(const wxBitmap &bmp, const wxRect& rect,
                    bool useMask = false, int quality = wxIMAGE_QUALITY_NORMAL)
        { DoDrawScaledBitmap( bmp, rect.x, rect.y, rect.width, rect.height, useMask, quality ); }

protected:
    bool DoFloodFill(wxCoord x1, wxCoord y1, const wxColour &col, int style=wxFLOOD_SURFACE );
    void DoGradientFillConcentric(const wxRect& rect, const wxColour& initialColour, const wxColour& destColour, const wxPoint& circleCenter);
    void DoGradientFillLinear(const wxRect& rect, const wxColour& initialColour, const wxColour& destColour, wxDirection nDirection = wxEAST);
    bool DoGetPixel(wxCoord x1, wxCoord y1, wxColour *col) const;
    void DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2);
    void DoCrossHair(wxCoord x, wxCoord y);
    void DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord xc,wxCoord yc);
    void DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea);
    void DoDrawPoint(wxCoord x, wxCoord y);
    void DoDrawLines(int n, wxPoint points[], wxCoord xoffset = 0, wxCoord yoffset = 0);
    void DoDrawPolygon(int n, wxPoint points[], wxCoord xoffset = 0, wxCoord yoffset = 0, int fillStyle=wxODDEVEN_RULE);
    void DoDrawPolyPolygon(int n, int count[], wxPoint points[], wxCoord xoffset = 0, wxCoord yoffset = 0, int fillStyle=wxODDEVEN_RULE);
    void DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height);
    void DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius = 20.0);
    void DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height);
#if wxUSE_SPLINES
    void DoDrawSpline(wxList *points);
#endif // wxUSE_SPLINES
    bool DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
            wxDC *source, wxCoord xsrc, wxCoord ysrc, int rop = wxCOPY, bool useMask = false,
            wxCoord xsrcMask = wxDefaultCoord, wxCoord ysrcMask = wxDefaultCoord);
    void DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y );
    void DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool useMask = false  );
    void DoDrawScaledBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, wxCoord w, wxCoord h, bool useMask = false, int quality = wxIMAGE_QUALITY_NORMAL );
    void DoDrawText(const wxString& text, wxCoord x, wxCoord y );
    void DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle);
    void DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height);
    void DoSetClippingRegionAsRegion( const wxRegion &WXUNUSED(clip) ) { }
    void DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
                     wxCoord *descent = (wxCoord *) NULL,
                     wxCoord *externalLeading = (wxCoord *) NULL,
                     const wxFont *theFont = (wxFont *) NULL ) const;
    void DoGetSize(int* width, int* height) const;
    void DoGetSizeMM(int *width, int *height) const;

    wxPrintData& GetPrintData() { return m_printData; }
    void SetPrintData(const wxPrintData& data);

    void ComputeScaleAndOrigin();

private:
    static float ms_PSScaleFactor;

    wxPrintData             m_printData;
    PangoContext           *m_context;
    PangoLayout            *m_layout;
    PangoFontDescription   *m_fontdesc;
    cairo_t                *m_cairo;

    unsigned char           m_currentRed;
    unsigned char           m_currentGreen;
    unsigned char           m_currentBlue;
    unsigned char           m_currentAlpha;

    int                     m_deviceOffsetY;
    int                     m_deviceOffsetX;

    GtkPrintContext      *m_gpc;
    static int            ms_resolution;

    wxCoord DeviceToLogicalX(wxCoord x) const
    {
        int addValue = 0;
        if (m_signX == -1) addValue = m_deviceOffsetX - m_deviceOriginX; // right to left
        else addValue = m_deviceOriginX;// left to right
        return (wxCoord) ((double)(x - addValue) * m_signX + m_logicalOriginX);
    }
    wxCoord DeviceToLogicalXRel(wxCoord x) const
    {
        return x;
    }
    wxCoord DeviceToLogicalY(wxCoord y) const
    {
        int addValue = 0;
        if (m_signY == -1) addValue = m_deviceOffsetY - m_deviceOriginY; // bottom to up
        else addValue = m_deviceOriginY;// up to bottom
        return (wxCoord) ((double)(y - addValue) * m_signY + m_logicalOriginY);
    }
    wxCoord DeviceToLogicalYRel(wxCoord y) const
    {
        return y;
    }
    wxCoord CairoToLogicalX(double x) const
    {
        int addValue = 0;
        if (m_signX == -1) addValue = m_deviceOffsetX - m_deviceOriginX; // right to left
        else addValue = m_deviceOriginX;// left to right
        return (wxCoord) MapFromCairo((double)(x - addValue) * m_signX + m_logicalOriginX);
    }
    wxCoord CairoToLogicalXRel(double x) const
    {
        return (wxCoord) MapFromCairo(x);
    }
    wxCoord CairoToLogicalY(double y) const
    {
        int addValue = 0;
        if (m_signY == -1) addValue = m_deviceOffsetY - m_deviceOriginY; // bottom to up
        else addValue = m_deviceOriginY;// up to bottom
        return (wxCoord) MapFromCairo((double)(y - addValue) * m_signY + m_logicalOriginY);
    }
    wxCoord CairoToLogicalYRel(double y) const
    {
        return (wxCoord) MapFromCairo(y);
    }
    wxCoord LogicalToDeviceX(wxCoord x) const
    {
        int addValue = 0;
        if (m_signX == -1) addValue = m_deviceOffsetX - m_deviceOriginX; // right to left
        else addValue = m_deviceOriginX;// left to right
        return (wxCoord) ((double)(x - m_logicalOriginX) * m_signX + addValue);
    }
    wxCoord LogicalToDeviceXRel(wxCoord x) const
    {
        return x;
    }
    wxCoord LogicalToDeviceY(wxCoord y) const
    {
        int addValue = 0;
        if (m_signY == -1) addValue = m_deviceOffsetY - m_deviceOriginY; // bottom to up
        else addValue = m_deviceOriginY;// up to bottom
        return (wxCoord) ((double)(y - m_logicalOriginY)  * m_signY + addValue);
    }
    wxCoord LogicalToDeviceYRel(wxCoord y) const
    {
        return y;
    }
    double LogicalToCairoX(wxCoord x) const
    {
        int addValue = 0;
        if (m_signX == -1) addValue = m_deviceOffsetX - m_deviceOriginX; // right to left
        else addValue = m_deviceOriginX;// left to right
        return MapToCairo((double)(x - m_logicalOriginX) * m_signX + addValue);
    }
    double LogicalToCairoXRel(wxCoord x) const
    {
        return MapToCairo(x);
    }
    double LogicalToCairoY(wxCoord y) const
    {
        int addValue = 0;
        if (m_signY == -1) addValue = m_deviceOffsetY - m_deviceOriginY; // bottom to up
        else addValue = m_deviceOriginY;// up to bottom
        return MapToCairo((double)(y - m_logicalOriginY)  * m_signY + addValue);
    }
    double LogicalToCairoYRel(wxCoord y) const
    {
        return MapToCairo(y);
    }
    double MapToCairo(wxCoord coordInt) const
    {
        return (double)coordInt*72.0/(double)ms_resolution;
    }
    double MapToCairo(double coordReal) const
    {
        return coordReal*72.0/(double)ms_resolution;
    }
    wxCoord MapFromCairo(wxCoord coordInt) const
    {
        return (wxCoord) ((double)coordInt*(double)ms_resolution/72.0);
    }
    wxCoord MapFromCairo(double coordReal) const
    {
        return (wxCoord) (coordReal*(double)ms_resolution/72.0);
    }
    DECLARE_DYNAMIC_CLASS(wxGtkPrintDC)
    DECLARE_NO_COPY_CLASS(wxGtkPrintDC)
};

// ----------------------------------------------------------------------------
// wxGtkPrintPreview: programmer creates an object of this class to preview a
// wxPrintout.
// ----------------------------------------------------------------------------

class WXDLLIMPEXP_CORE wxGtkPrintPreview : public wxPrintPreviewBase
{
public:
    wxGtkPrintPreview(wxPrintout *printout,
                             wxPrintout *printoutForPrinting = (wxPrintout *) NULL,
                             wxPrintDialogData *data = (wxPrintDialogData *) NULL);
    wxGtkPrintPreview(wxPrintout *printout,
                             wxPrintout *printoutForPrinting,
                             wxPrintData *data);

    virtual ~wxGtkPrintPreview();

    virtual bool Print(bool interactive);
    virtual void DetermineScaling();

private:
    void Init(wxPrintout *printout, wxPrintout *printoutForPrinting);

    DECLARE_CLASS(wxGtkPrintPreview)
};

#endif

#endif