First cut at printing support for wxRichTextCtrl

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@42494 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Julian Smart
2006-10-27 08:43:44 +00:00
parent 0d635035cc
commit 44219ff04f
4 changed files with 985 additions and 11 deletions

View File

@@ -101,6 +101,7 @@ class WXDLLIMPEXP_RICHTEXT wxTextAttrEx;
class WXDLLIMPEXP_RICHTEXT wxRichTextListStyleDefinition;
class WXDLLIMPEXP_RICHTEXT wxRichTextEvent;
class WXDLLIMPEXP_RICHTEXT wxRichTextRenderer;
class WXDLLIMPEXP_RICHTEXT wxRichTextBuffer;
/*!
* Flags determining the available space, passed to Layout
@@ -115,6 +116,15 @@ class WXDLLIMPEXP_RICHTEXT wxRichTextRenderer;
// the rect passed to Layout.
#define wxRICHTEXT_LAYOUT_SPECIFIED_RECT 0x10
/*!
* Flags to pass to Draw
*/
// Ignore paragraph cache optimization, e.g. for printing purposes
// where one line may be drawn higher (on the next page) compared
// with the previous line
#define wxRICHTEXT_DRAW_IGNORE_CACHE 0x01
/*!
* Flags returned from hit-testing
*/
@@ -714,6 +724,9 @@ public:
void SetDescent(int descent) { m_descent = descent; }
int GetDescent() const { return m_descent; }
/// Gets the containing buffer
wxRichTextBuffer* GetBuffer() const;
// Operations
/// Clone the object
@@ -727,8 +740,9 @@ public:
void Reference() { m_refCount ++; }
void Dereference();
/// Convert units in tends of a millimetre to device units
static int ConvertTenthsMMToPixels(wxDC& dc, int units);
/// Convert units in tenths of a millimetre to device units
int ConvertTenthsMMToPixels(wxDC& dc, int units);
static int ConvertTenthsMMToPixels(int ppi, int units);
protected:
wxSize m_size;
@@ -1861,6 +1875,11 @@ public:
/// Factor to multiply by character height to get a reasonable bullet size
static float GetBulletProportion() { return sm_bulletProportion; }
static void SetBulletProportion(float prop) { sm_bulletProportion = prop; }
/// Scale factor for calculating dimensions
double GetScale() const { return m_scale; }
void SetScale(double scale) { m_scale = scale; }
protected:
/// Command processor
@@ -1904,6 +1923,9 @@ protected:
/// Factor to multiply by character height to get a reasonable bullet size
static float sm_bulletProportion;
/// Scaling factor in use: needed to calculate correct dimensions when printing
double m_scale;
};
/*!

View File

@@ -0,0 +1,237 @@
/////////////////////////////////////////////////////////////////////////////
// Name: wx/richtext/richtextprint.h
// Purpose: Rich text printing classes
// Author: Julian Smart
// Created: 2006-10-23
// RCS-ID: $Id$
// Copyright: (c) Julian Smart
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#ifndef _WX_RICHTEXTPRINT_H_
#define _WX_RICHTEXTPRINT_H_
#include "wx/defs.h"
#if wxUSE_RICHTEXT & wxUSE_PRINTING_ARCHITECTURE
#include "wx/richtext/richtextbuffer.h"
#include "wx/print.h"
#include "wx/printdlg.h"
#define wxRICHTEXT_PRINT_MAX_PAGES 99999
// Header/footer page identifiers
enum wxRichTextOddEvenPage {
wxRICHTEXT_PAGE_ODD,
wxRICHTEXT_PAGE_EVEN,
wxRICHTEXT_PAGE_ALL,
};
// Header/footer text locations
enum wxRichTextPageLocation {
wxRICHTEXT_PAGE_LEFT,
wxRICHTEXT_PAGE_CENTRE,
wxRICHTEXT_PAGE_RIGHT
};
/*!
* Header/footer data
*/
class WXDLLIMPEXP_RICHTEXT wxRichTextHeaderFooterData: public wxObject
{
public:
wxRichTextHeaderFooterData() { Init(); }
wxRichTextHeaderFooterData(const wxRichTextHeaderFooterData& data) { Copy(data); }
/// Initialise
void Init() { m_headerMargin = 20; m_footerMargin = 20; m_showOnFirstPage = true; }
/// Copy
void Copy(const wxRichTextHeaderFooterData& data);
/// Assignment
void operator= (const wxRichTextHeaderFooterData& data) { Copy(data); }
/// Set/get header text, e.g. wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_LEFT
void SetHeaderText(const wxString& text, wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_ALL, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE);
wxString GetHeaderText(wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_EVEN, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE) const;
/// Set/get footer text, e.g. wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_LEFT
void SetFooterText(const wxString& text, wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_ALL, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE);
wxString GetFooterText(wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_EVEN, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE) const;
/// Set/get text
void SetText(const wxString& text, int headerFooter, wxRichTextOddEvenPage page, wxRichTextPageLocation location);
wxString GetText(int headerFooter, wxRichTextOddEvenPage page, wxRichTextPageLocation location) const;
/// Set/get margins between text and header or footer, in tenths of a millimeter
void SetMargins(int headerMargin, int footerMargin) { m_headerMargin = headerMargin; m_footerMargin = footerMargin; }
int GetHeaderMargin() const { return m_headerMargin; }
int GetFooterMargin() const { return m_footerMargin; }
/// Set/get whether to show header or footer on first page
void SetShowOnFirstPage(bool showOnFirstPage) { m_showOnFirstPage = showOnFirstPage; }
bool GetShowOnFirstPage() const { return m_showOnFirstPage; }
/// Clear all text
void Clear();
/// Set/get font
void SetFont(const wxFont& font) { m_font = font; }
const wxFont& GetFont() const { return m_font; }
/// Set/get colour
void SetTextColour(const wxColour& col) { m_colour = col; }
const wxColour& GetTextColour() const { return m_colour; }
DECLARE_CLASS(wxRichTextHeaderFooterData)
private:
// Strings for left, centre, right, top, bottom, odd, even
wxString m_text[12];
wxFont m_font;
wxColour m_colour;
int m_headerMargin;
int m_footerMargin;
bool m_showOnFirstPage;
};
/*!
* wxRichTextPrintout
*/
class WXDLLIMPEXP_RICHTEXT wxRichTextPrintout : public wxPrintout
{
public:
wxRichTextPrintout(const wxString& title = wxT("Printout"));
virtual ~wxRichTextPrintout();
/// The buffer to print
void SetRichTextBuffer(wxRichTextBuffer* buffer) { m_richTextBuffer = buffer; }
wxRichTextBuffer* GetRichTextBuffer() const { return m_richTextBuffer; }
/// Set/get header/footer data
void SetHeaderFooterData(const wxRichTextHeaderFooterData& data) { m_headerFooterData = data; }
const wxRichTextHeaderFooterData& GetHeaderFooterData() const { return m_headerFooterData; }
/// Sets margins in 10ths of millimetre. Defaults to 1 inch for margins.
void SetMargins(int top = 252, int bottom = 252, int left = 252, int right = 252);
/// Calculate scaling and rectangles, setting the device context scaling
void CalculateScaling(wxDC* dc, wxRect& textRect, wxRect& headerRect, wxRect& footerRect);
// wxPrintout virtual functions
virtual bool OnPrintPage(int page);
virtual bool HasPage(int page);
virtual void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo);
virtual bool OnBeginDocument(int startPage, int endPage);
virtual void OnPreparePrinting();
private:
/// Renders one page into dc
void RenderPage(wxDC *dc, int page);
/// Substitute keywords
static bool SubstituteKeywords(wxString& str, const wxString& title, int pageNum, int pageCount);
private:
wxRichTextBuffer* m_richTextBuffer;
int m_numPages;
wxArrayInt m_pageBreaksStart;
wxArrayInt m_pageBreaksEnd;
int m_marginLeft, m_marginTop, m_marginRight, m_marginBottom;
wxRichTextHeaderFooterData m_headerFooterData;
DECLARE_NO_COPY_CLASS(wxRichTextPrintout)
};
/*
*! wxRichTextPrinting
* A simple interface to perform wxRichTextBuffer printing.
*/
class WXDLLIMPEXP_RICHTEXT wxRichTextPrinting : public wxObject
{
public:
wxRichTextPrinting(const wxString& name = wxT("Printing"), wxWindow *parentWindow = NULL);
virtual ~wxRichTextPrinting();
/// Preview the file or buffer
bool PreviewFile(const wxString& richTextFile);
bool PreviewBuffer(const wxRichTextBuffer& buffer);
/// Print the file or buffer
bool PrintFile(const wxString& richTextFile);
bool PrintBuffer(const wxRichTextBuffer& buffer);
/// Shows page setup dialog
void PageSetup();
/// Set/get header/footer data
void SetHeaderFooterData(const wxRichTextHeaderFooterData& data) { m_headerFooterData = data; }
const wxRichTextHeaderFooterData& GetHeaderFooterData() const { return m_headerFooterData; }
/// Set/get header text, e.g. wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_LEFT
void SetHeaderText(const wxString& text, wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_ALL, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE);
wxString GetHeaderText(wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_EVEN, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE) const;
/// Set/get footer text, e.g. wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_LEFT
void SetFooterText(const wxString& text, wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_ALL, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE);
wxString GetFooterText(wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_EVEN, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE) const;
/// Show header/footer on first page, or not
bool SetShowOnFirstPage(bool show) { m_headerFooterData.SetShowOnFirstPage(show); }
/// Get print and page setup data
wxPrintData *GetPrintData();
wxPageSetupDialogData *GetPageSetupData() { return m_pageSetupData; }
/// Set the rich text buffer pointer, deleting the existing object if present
void SetRichTextBufferPreview(wxRichTextBuffer* buf);
wxRichTextBuffer* GetRichTextBufferPreview() const { return m_richTextBufferPreview; }
void SetRichTextBufferPrinting(wxRichTextBuffer* buf);
wxRichTextBuffer* GetRichTextBufferPrinting() const { return m_richTextBufferPrinting; }
/// Set/get the parent window
void SetParentWindow(wxWindow* parent) { m_parentWindow = parent; }
wxWindow* GetParentWindow() const { return m_parentWindow; }
/// Set/get the title
void SetTitle(const wxString& title) { m_title = title; }
const wxString& GetTitle() const { return m_title; }
/// Set/get the preview rect
void SetPreviewRect(const wxRect& rect) { m_previewRect = rect; }
const wxRect& GetPreviewRect() const { return m_previewRect; }
protected:
virtual wxRichTextPrintout *CreatePrintout();
virtual bool DoPreview(wxRichTextPrintout *printout1, wxRichTextPrintout *printout2);
virtual bool DoPrint(wxRichTextPrintout *printout);
private:
wxPrintData* m_printData;
wxPageSetupDialogData* m_pageSetupData;
wxRichTextHeaderFooterData m_headerFooterData;
wxString m_title;
wxWindow* m_parentWindow;
wxRichTextBuffer* m_richTextBufferPreview;
wxRichTextBuffer* m_richTextBufferPrinting;
wxRect m_previewRect;
DECLARE_NO_COPY_CLASS(wxRichTextPrinting)
};
#endif // wxUSE_RICHTEXT & wxUSE_PRINTING_ARCHITECTURE
#endif // _WX_RICHTEXTPRINT_H_