richtextprint.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/richtext/richtextprint.h
  3. // Purpose: Rich text printing classes
  4. // Author: Julian Smart
  5. // Created: 2006-10-23
  6. // Copyright: (c) Julian Smart
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_RICHTEXTPRINT_H_
  10. #define _WX_RICHTEXTPRINT_H_
  11. #include "wx/defs.h"
  12. #if wxUSE_RICHTEXT & wxUSE_PRINTING_ARCHITECTURE
  13. #include "wx/richtext/richtextbuffer.h"
  14. #include "wx/print.h"
  15. #include "wx/printdlg.h"
  16. #define wxRICHTEXT_PRINT_MAX_PAGES 99999
  17. // Header/footer page identifiers
  18. enum wxRichTextOddEvenPage {
  19. wxRICHTEXT_PAGE_ODD,
  20. wxRICHTEXT_PAGE_EVEN,
  21. wxRICHTEXT_PAGE_ALL
  22. };
  23. // Header/footer text locations
  24. enum wxRichTextPageLocation {
  25. wxRICHTEXT_PAGE_LEFT,
  26. wxRICHTEXT_PAGE_CENTRE,
  27. wxRICHTEXT_PAGE_RIGHT
  28. };
  29. /*!
  30. * Header/footer data
  31. */
  32. class WXDLLIMPEXP_RICHTEXT wxRichTextHeaderFooterData: public wxObject
  33. {
  34. public:
  35. wxRichTextHeaderFooterData() { Init(); }
  36. wxRichTextHeaderFooterData(const wxRichTextHeaderFooterData& data): wxObject() { Copy(data); }
  37. /// Initialise
  38. void Init() { m_headerMargin = 20; m_footerMargin = 20; m_showOnFirstPage = true; }
  39. /// Copy
  40. void Copy(const wxRichTextHeaderFooterData& data);
  41. /// Assignment
  42. void operator= (const wxRichTextHeaderFooterData& data) { Copy(data); }
  43. /// Set/get header text, e.g. wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_LEFT
  44. void SetHeaderText(const wxString& text, wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_ALL, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE);
  45. wxString GetHeaderText(wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_EVEN, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE) const;
  46. /// Set/get footer text, e.g. wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_LEFT
  47. void SetFooterText(const wxString& text, wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_ALL, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE);
  48. wxString GetFooterText(wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_EVEN, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE) const;
  49. /// Set/get text
  50. void SetText(const wxString& text, int headerFooter, wxRichTextOddEvenPage page, wxRichTextPageLocation location);
  51. wxString GetText(int headerFooter, wxRichTextOddEvenPage page, wxRichTextPageLocation location) const;
  52. /// Set/get margins between text and header or footer, in tenths of a millimeter
  53. void SetMargins(int headerMargin, int footerMargin) { m_headerMargin = headerMargin; m_footerMargin = footerMargin; }
  54. int GetHeaderMargin() const { return m_headerMargin; }
  55. int GetFooterMargin() const { return m_footerMargin; }
  56. /// Set/get whether to show header or footer on first page
  57. void SetShowOnFirstPage(bool showOnFirstPage) { m_showOnFirstPage = showOnFirstPage; }
  58. bool GetShowOnFirstPage() const { return m_showOnFirstPage; }
  59. /// Clear all text
  60. void Clear();
  61. /// Set/get font
  62. void SetFont(const wxFont& font) { m_font = font; }
  63. const wxFont& GetFont() const { return m_font; }
  64. /// Set/get colour
  65. void SetTextColour(const wxColour& col) { m_colour = col; }
  66. const wxColour& GetTextColour() const { return m_colour; }
  67. DECLARE_CLASS(wxRichTextHeaderFooterData)
  68. private:
  69. // Strings for left, centre, right, top, bottom, odd, even
  70. wxString m_text[12];
  71. wxFont m_font;
  72. wxColour m_colour;
  73. int m_headerMargin;
  74. int m_footerMargin;
  75. bool m_showOnFirstPage;
  76. };
  77. /*!
  78. * wxRichTextPrintout
  79. */
  80. class WXDLLIMPEXP_RICHTEXT wxRichTextPrintout : public wxPrintout
  81. {
  82. public:
  83. wxRichTextPrintout(const wxString& title = wxGetTranslation("Printout"));
  84. virtual ~wxRichTextPrintout();
  85. /// The buffer to print
  86. void SetRichTextBuffer(wxRichTextBuffer* buffer) { m_richTextBuffer = buffer; }
  87. wxRichTextBuffer* GetRichTextBuffer() const { return m_richTextBuffer; }
  88. /// Set/get header/footer data
  89. void SetHeaderFooterData(const wxRichTextHeaderFooterData& data) { m_headerFooterData = data; }
  90. const wxRichTextHeaderFooterData& GetHeaderFooterData() const { return m_headerFooterData; }
  91. /// Sets margins in 10ths of millimetre. Defaults to 1 inch for margins.
  92. void SetMargins(int top = 254, int bottom = 254, int left = 254, int right = 254);
  93. /// Calculate scaling and rectangles, setting the device context scaling
  94. void CalculateScaling(wxDC* dc, wxRect& textRect, wxRect& headerRect, wxRect& footerRect);
  95. // wxPrintout virtual functions
  96. virtual bool OnPrintPage(int page);
  97. virtual bool HasPage(int page);
  98. virtual void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo);
  99. virtual bool OnBeginDocument(int startPage, int endPage);
  100. virtual void OnPreparePrinting();
  101. private:
  102. /// Renders one page into dc
  103. void RenderPage(wxDC *dc, int page);
  104. /// Substitute keywords
  105. static bool SubstituteKeywords(wxString& str, const wxString& title, int pageNum, int pageCount);
  106. private:
  107. wxRichTextBuffer* m_richTextBuffer;
  108. int m_numPages;
  109. wxArrayInt m_pageBreaksStart;
  110. wxArrayInt m_pageBreaksEnd;
  111. wxArrayInt m_pageYOffsets;
  112. int m_marginLeft, m_marginTop, m_marginRight, m_marginBottom;
  113. wxRichTextHeaderFooterData m_headerFooterData;
  114. wxDECLARE_NO_COPY_CLASS(wxRichTextPrintout);
  115. };
  116. /*
  117. *! wxRichTextPrinting
  118. * A simple interface to perform wxRichTextBuffer printing.
  119. */
  120. class WXDLLIMPEXP_RICHTEXT wxRichTextPrinting : public wxObject
  121. {
  122. public:
  123. wxRichTextPrinting(const wxString& name = wxGetTranslation("Printing"), wxWindow *parentWindow = NULL);
  124. virtual ~wxRichTextPrinting();
  125. /// Preview the file or buffer
  126. #if wxUSE_FFILE && wxUSE_STREAMS
  127. bool PreviewFile(const wxString& richTextFile);
  128. #endif
  129. bool PreviewBuffer(const wxRichTextBuffer& buffer);
  130. /// Print the file or buffer
  131. #if wxUSE_FFILE && wxUSE_STREAMS
  132. bool PrintFile(const wxString& richTextFile, bool showPrintDialog = true);
  133. #endif
  134. bool PrintBuffer(const wxRichTextBuffer& buffer, bool showPrintDialog = true);
  135. /// Shows page setup dialog
  136. void PageSetup();
  137. /// Set/get header/footer data
  138. void SetHeaderFooterData(const wxRichTextHeaderFooterData& data) { m_headerFooterData = data; }
  139. const wxRichTextHeaderFooterData& GetHeaderFooterData() const { return m_headerFooterData; }
  140. /// Set/get header text, e.g. wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_LEFT
  141. void SetHeaderText(const wxString& text, wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_ALL, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE);
  142. wxString GetHeaderText(wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_EVEN, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE) const;
  143. /// Set/get footer text, e.g. wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_LEFT
  144. void SetFooterText(const wxString& text, wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_ALL, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE);
  145. wxString GetFooterText(wxRichTextOddEvenPage page = wxRICHTEXT_PAGE_EVEN, wxRichTextPageLocation location = wxRICHTEXT_PAGE_CENTRE) const;
  146. /// Show header/footer on first page, or not
  147. void SetShowOnFirstPage(bool show) { m_headerFooterData.SetShowOnFirstPage(show); }
  148. /// Set the font
  149. void SetHeaderFooterFont(const wxFont& font) { m_headerFooterData.SetFont(font); }
  150. /// Set the colour
  151. void SetHeaderFooterTextColour(const wxColour& font) { m_headerFooterData.SetTextColour(font); }
  152. /// Get print and page setup data
  153. wxPrintData *GetPrintData();
  154. wxPageSetupDialogData *GetPageSetupData() { return m_pageSetupData; }
  155. /// Set print and page setup data
  156. void SetPrintData(const wxPrintData& printData);
  157. void SetPageSetupData(const wxPageSetupDialogData& pageSetupData);
  158. /// Set the rich text buffer pointer, deleting the existing object if present
  159. void SetRichTextBufferPreview(wxRichTextBuffer* buf);
  160. wxRichTextBuffer* GetRichTextBufferPreview() const { return m_richTextBufferPreview; }
  161. void SetRichTextBufferPrinting(wxRichTextBuffer* buf);
  162. wxRichTextBuffer* GetRichTextBufferPrinting() const { return m_richTextBufferPrinting; }
  163. /// Set/get the parent window
  164. void SetParentWindow(wxWindow* parent) { m_parentWindow = parent; }
  165. wxWindow* GetParentWindow() const { return m_parentWindow; }
  166. /// Set/get the title
  167. void SetTitle(const wxString& title) { m_title = title; }
  168. const wxString& GetTitle() const { return m_title; }
  169. /// Set/get the preview rect
  170. void SetPreviewRect(const wxRect& rect) { m_previewRect = rect; }
  171. const wxRect& GetPreviewRect() const { return m_previewRect; }
  172. protected:
  173. virtual wxRichTextPrintout *CreatePrintout();
  174. virtual bool DoPreview(wxRichTextPrintout *printout1, wxRichTextPrintout *printout2);
  175. virtual bool DoPrint(wxRichTextPrintout *printout, bool showPrintDialog);
  176. private:
  177. wxPrintData* m_printData;
  178. wxPageSetupDialogData* m_pageSetupData;
  179. wxRichTextHeaderFooterData m_headerFooterData;
  180. wxString m_title;
  181. wxWindow* m_parentWindow;
  182. wxRichTextBuffer* m_richTextBufferPreview;
  183. wxRichTextBuffer* m_richTextBufferPrinting;
  184. wxRect m_previewRect;
  185. wxDECLARE_NO_COPY_CLASS(wxRichTextPrinting);
  186. };
  187. #endif // wxUSE_RICHTEXT & wxUSE_PRINTING_ARCHITECTURE
  188. #endif // _WX_RICHTEXTPRINT_H_