1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Base classes for printing framework
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_PRNTBASEH__
13 #define _WX_PRNTBASEH__
17 #if wxUSE_PRINTING_ARCHITECTURE
20 #include "wx/cmndata.h"
22 #include "wx/scrolwin.h"
23 #include "wx/dialog.h"
26 class WXDLLIMPEXP_FWD_CORE wxDC
;
27 class WXDLLIMPEXP_FWD_CORE wxButton
;
28 class WXDLLIMPEXP_FWD_CORE wxChoice
;
29 class WXDLLIMPEXP_FWD_CORE wxPrintout
;
30 class WXDLLIMPEXP_FWD_CORE wxPrinterBase
;
31 class WXDLLIMPEXP_FWD_CORE wxPrintDialogBase
;
32 class WXDLLIMPEXP_FWD_CORE wxPrintDialog
;
33 class WXDLLIMPEXP_FWD_CORE wxPageSetupDialogBase
;
34 class WXDLLIMPEXP_FWD_CORE wxPageSetupDialog
;
35 class WXDLLIMPEXP_FWD_CORE wxPrintPreviewBase
;
36 class WXDLLIMPEXP_FWD_CORE wxPreviewCanvas
;
37 class WXDLLIMPEXP_FWD_CORE wxPreviewControlBar
;
38 class WXDLLIMPEXP_FWD_CORE wxPreviewFrame
;
39 class WXDLLIMPEXP_FWD_CORE wxPrintFactory
;
40 class WXDLLIMPEXP_FWD_CORE wxPrintNativeDataBase
;
42 //----------------------------------------------------------------------------
44 //----------------------------------------------------------------------------
48 wxPRINTER_NO_ERROR
= 0,
53 //----------------------------------------------------------------------------
55 //----------------------------------------------------------------------------
57 class WXDLLEXPORT wxPrintFactory
61 virtual ~wxPrintFactory() {}
63 virtual wxPrinterBase
*CreatePrinter( wxPrintDialogData
* data
) = 0;
65 virtual wxPrintPreviewBase
*CreatePrintPreview( wxPrintout
*preview
,
66 wxPrintout
*printout
= NULL
,
67 wxPrintDialogData
*data
= NULL
) = 0;
68 virtual wxPrintPreviewBase
*CreatePrintPreview( wxPrintout
*preview
,
70 wxPrintData
*data
) = 0;
72 virtual wxPrintDialogBase
*CreatePrintDialog( wxWindow
*parent
,
73 wxPrintDialogData
*data
= NULL
) = 0;
74 virtual wxPrintDialogBase
*CreatePrintDialog( wxWindow
*parent
,
75 wxPrintData
*data
) = 0;
77 virtual wxPageSetupDialogBase
*CreatePageSetupDialog( wxWindow
*parent
,
78 wxPageSetupDialogData
* data
= NULL
) = 0;
80 virtual wxDC
* CreatePrinterDC( const wxPrintData
& data
) = 0;
82 // What to do and what to show in the wxPrintDialog
83 // a) Use the generic print setup dialog or a native one?
84 virtual bool HasPrintSetupDialog() = 0;
85 virtual wxDialog
*CreatePrintSetupDialog( wxWindow
*parent
, wxPrintData
*data
) = 0;
86 // b) Provide the "print to file" option ourselves or via print setup?
87 virtual bool HasOwnPrintToFile() = 0;
88 // c) Show current printer
89 virtual bool HasPrinterLine() = 0;
90 virtual wxString
CreatePrinterLine() = 0;
91 // d) Show Status line for current printer?
92 virtual bool HasStatusLine() = 0;
93 virtual wxString
CreateStatusLine() = 0;
96 virtual wxPrintNativeDataBase
*CreatePrintNativeData() = 0;
98 static void SetPrintFactory( wxPrintFactory
*factory
);
99 static wxPrintFactory
*GetFactory();
101 static wxPrintFactory
*m_factory
;
104 class WXDLLEXPORT wxNativePrintFactory
: public wxPrintFactory
107 virtual wxPrinterBase
*CreatePrinter( wxPrintDialogData
*data
);
109 virtual wxPrintPreviewBase
*CreatePrintPreview( wxPrintout
*preview
,
110 wxPrintout
*printout
= NULL
,
111 wxPrintDialogData
*data
= NULL
);
112 virtual wxPrintPreviewBase
*CreatePrintPreview( wxPrintout
*preview
,
113 wxPrintout
*printout
,
116 virtual wxPrintDialogBase
*CreatePrintDialog( wxWindow
*parent
,
117 wxPrintDialogData
*data
= NULL
);
118 virtual wxPrintDialogBase
*CreatePrintDialog( wxWindow
*parent
,
121 virtual wxPageSetupDialogBase
*CreatePageSetupDialog( wxWindow
*parent
,
122 wxPageSetupDialogData
* data
= NULL
);
124 virtual wxDC
* CreatePrinterDC( const wxPrintData
& data
);
126 virtual bool HasPrintSetupDialog();
127 virtual wxDialog
*CreatePrintSetupDialog( wxWindow
*parent
, wxPrintData
*data
);
128 virtual bool HasOwnPrintToFile();
129 virtual bool HasPrinterLine();
130 virtual wxString
CreatePrinterLine();
131 virtual bool HasStatusLine();
132 virtual wxString
CreateStatusLine();
134 virtual wxPrintNativeDataBase
*CreatePrintNativeData();
137 //----------------------------------------------------------------------------
138 // wxPrintNativeDataBase
139 //----------------------------------------------------------------------------
141 class WXDLLEXPORT wxPrintNativeDataBase
: public wxObject
144 wxPrintNativeDataBase();
145 virtual ~wxPrintNativeDataBase() {}
147 virtual bool TransferTo( wxPrintData
&data
) = 0;
148 virtual bool TransferFrom( const wxPrintData
&data
) = 0;
150 virtual bool Ok() const { return IsOk(); }
151 virtual bool IsOk() const = 0;
156 DECLARE_CLASS(wxPrintNativeDataBase
)
157 DECLARE_NO_COPY_CLASS(wxPrintNativeDataBase
)
160 //----------------------------------------------------------------------------
162 //----------------------------------------------------------------------------
165 * Represents the printer: manages printing a wxPrintout object
168 class WXDLLEXPORT wxPrinterBase
: public wxObject
171 wxPrinterBase(wxPrintDialogData
*data
= (wxPrintDialogData
*) NULL
);
172 virtual ~wxPrinterBase();
174 virtual wxWindow
*CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
);
175 virtual void ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
);
177 virtual wxPrintDialogData
& GetPrintDialogData() const;
178 bool GetAbort() const { return sm_abortIt
; }
180 static wxPrinterError
GetLastError() { return sm_lastError
; }
182 ///////////////////////////////////////////////////////////////////////////
185 virtual bool Setup(wxWindow
*parent
) = 0;
186 virtual bool Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
= true) = 0;
187 virtual wxDC
* PrintDialog(wxWindow
*parent
) = 0;
190 wxPrintDialogData m_printDialogData
;
191 wxPrintout
* m_currentPrintout
;
193 static wxPrinterError sm_lastError
;
196 static wxWindow
* sm_abortWindow
;
197 static bool sm_abortIt
;
200 DECLARE_CLASS(wxPrinterBase
)
201 DECLARE_NO_COPY_CLASS(wxPrinterBase
)
204 //----------------------------------------------------------------------------
206 //----------------------------------------------------------------------------
208 class WXDLLEXPORT wxPrinter
: public wxPrinterBase
211 wxPrinter(wxPrintDialogData
*data
= (wxPrintDialogData
*) NULL
);
212 virtual ~wxPrinter();
214 virtual wxWindow
*CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
);
215 virtual void ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
);
217 virtual bool Setup(wxWindow
*parent
);
218 virtual bool Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
= true);
219 virtual wxDC
* PrintDialog(wxWindow
*parent
);
221 virtual wxPrintDialogData
& GetPrintDialogData() const;
224 wxPrinterBase
*m_pimpl
;
227 DECLARE_CLASS(wxPrinter
)
228 DECLARE_NO_COPY_CLASS(wxPrinter
)
231 //----------------------------------------------------------------------------
233 //----------------------------------------------------------------------------
236 * Represents an object via which a document may be printed.
237 * The programmer derives from this, overrides (at least) OnPrintPage,
238 * and passes it to a wxPrinter object for printing, or a wxPrintPreview
239 * object for previewing.
242 class WXDLLEXPORT wxPrintout
: public wxObject
245 wxPrintout(const wxString
& title
= wxT("Printout"));
246 virtual ~wxPrintout();
248 virtual bool OnBeginDocument(int startPage
, int endPage
);
249 virtual void OnEndDocument();
250 virtual void OnBeginPrinting();
251 virtual void OnEndPrinting();
253 // Guaranteed to be before any other functions are called
254 virtual void OnPreparePrinting() { }
256 virtual bool HasPage(int page
);
257 virtual bool OnPrintPage(int page
) = 0;
258 virtual void GetPageInfo(int *minPage
, int *maxPage
, int *pageFrom
, int *pageTo
);
260 virtual wxString
GetTitle() const { return m_printoutTitle
; }
262 wxDC
*GetDC() const { return m_printoutDC
; }
263 void SetDC(wxDC
*dc
) { m_printoutDC
= dc
; }
265 void FitThisSizeToPaper(const wxSize
& imageSize
);
266 void FitThisSizeToPage(const wxSize
& imageSize
);
267 void FitThisSizeToPageMargins(const wxSize
& imageSize
, const wxPageSetupDialogData
& pageSetupData
);
268 void MapScreenSizeToPaper();
269 void MapScreenSizeToPage();
270 void MapScreenSizeToPageMargins(const wxPageSetupDialogData
& pageSetupData
);
271 void MapScreenSizeToDevice();
273 wxRect
GetLogicalPaperRect() const;
274 wxRect
GetLogicalPageRect() const;
275 wxRect
GetLogicalPageMarginsRect(const wxPageSetupDialogData
& pageSetupData
) const;
277 void SetLogicalOrigin(wxCoord x
, wxCoord y
);
278 void OffsetLogicalOrigin(wxCoord xoff
, wxCoord yoff
);
280 void SetPageSizePixels(int w
, int h
) { m_pageWidthPixels
= w
; m_pageHeightPixels
= h
; }
281 void GetPageSizePixels(int *w
, int *h
) const { *w
= m_pageWidthPixels
; *h
= m_pageHeightPixels
; }
282 void SetPageSizeMM(int w
, int h
) { m_pageWidthMM
= w
; m_pageHeightMM
= h
; }
283 void GetPageSizeMM(int *w
, int *h
) const { *w
= m_pageWidthMM
; *h
= m_pageHeightMM
; }
285 void SetPPIScreen(int x
, int y
) { m_PPIScreenX
= x
; m_PPIScreenY
= y
; }
286 void GetPPIScreen(int *x
, int *y
) const { *x
= m_PPIScreenX
; *y
= m_PPIScreenY
; }
287 void SetPPIPrinter(int x
, int y
) { m_PPIPrinterX
= x
; m_PPIPrinterY
= y
; }
288 void GetPPIPrinter(int *x
, int *y
) const { *x
= m_PPIPrinterX
; *y
= m_PPIPrinterY
; }
290 void SetPaperRectPixels(const wxRect
& paperRectPixels
) { m_paperRectPixels
= paperRectPixels
; }
291 wxRect
GetPaperRectPixels() const { return m_paperRectPixels
; }
293 virtual bool IsPreview() const { return m_isPreview
; }
295 virtual void SetIsPreview(bool p
) { m_isPreview
= p
; }
298 wxString m_printoutTitle
;
301 int m_pageWidthPixels
;
302 int m_pageHeightPixels
;
312 wxRect m_paperRectPixels
;
317 DECLARE_ABSTRACT_CLASS(wxPrintout
)
318 DECLARE_NO_COPY_CLASS(wxPrintout
)
321 //----------------------------------------------------------------------------
323 //----------------------------------------------------------------------------
326 * Canvas upon which a preview is drawn.
329 class WXDLLEXPORT wxPreviewCanvas
: public wxScrolledWindow
332 wxPreviewCanvas(wxPrintPreviewBase
*preview
,
334 const wxPoint
& pos
= wxDefaultPosition
,
335 const wxSize
& size
= wxDefaultSize
,
337 const wxString
& name
= wxT("canvas"));
338 virtual ~wxPreviewCanvas();
340 void OnPaint(wxPaintEvent
& event
);
341 void OnChar(wxKeyEvent
&event
);
342 // Responds to colour changes
343 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
347 void OnMouseWheel(wxMouseEvent
& event
);
348 #endif // wxUSE_MOUSEWHEEL
350 wxPrintPreviewBase
* m_printPreview
;
352 DECLARE_CLASS(wxPreviewCanvas
)
353 DECLARE_EVENT_TABLE()
354 DECLARE_NO_COPY_CLASS(wxPreviewCanvas
)
357 //----------------------------------------------------------------------------
359 //----------------------------------------------------------------------------
362 * Default frame for showing preview.
365 class WXDLLEXPORT wxPreviewFrame
: public wxFrame
368 wxPreviewFrame(wxPrintPreviewBase
*preview
,
370 const wxString
& title
= wxT("Print Preview"),
371 const wxPoint
& pos
= wxDefaultPosition
,
372 const wxSize
& size
= wxDefaultSize
,
373 long style
= wxDEFAULT_FRAME_STYLE
,
374 const wxString
& name
= wxT("frame"));
375 virtual ~wxPreviewFrame();
377 void OnCloseWindow(wxCloseEvent
& event
);
378 virtual void Initialize();
379 virtual void CreateCanvas();
380 virtual void CreateControlBar();
382 inline wxPreviewControlBar
* GetControlBar() const { return m_controlBar
; }
385 wxPreviewCanvas
* m_previewCanvas
;
386 wxPreviewControlBar
* m_controlBar
;
387 wxPrintPreviewBase
* m_printPreview
;
388 wxWindowDisabler
* m_windowDisabler
;
391 void OnChar(wxKeyEvent
& event
);
393 DECLARE_EVENT_TABLE()
394 DECLARE_CLASS(wxPreviewFrame
)
395 DECLARE_NO_COPY_CLASS(wxPreviewFrame
)
398 //----------------------------------------------------------------------------
399 // wxPreviewControlBar
400 //----------------------------------------------------------------------------
403 * A panel with buttons for controlling a print preview.
404 * The programmer may wish to use other means for controlling
408 #define wxPREVIEW_PRINT 1
409 #define wxPREVIEW_PREVIOUS 2
410 #define wxPREVIEW_NEXT 4
411 #define wxPREVIEW_ZOOM 8
412 #define wxPREVIEW_FIRST 16
413 #define wxPREVIEW_LAST 32
414 #define wxPREVIEW_GOTO 64
416 #define wxPREVIEW_DEFAULT (wxPREVIEW_PREVIOUS|wxPREVIEW_NEXT|wxPREVIEW_ZOOM\
417 |wxPREVIEW_FIRST|wxPREVIEW_GOTO|wxPREVIEW_LAST)
420 #define wxID_PREVIEW_CLOSE 1
421 #define wxID_PREVIEW_NEXT 2
422 #define wxID_PREVIEW_PREVIOUS 3
423 #define wxID_PREVIEW_PRINT 4
424 #define wxID_PREVIEW_ZOOM 5
425 #define wxID_PREVIEW_FIRST 6
426 #define wxID_PREVIEW_LAST 7
427 #define wxID_PREVIEW_GOTO 8
429 class WXDLLEXPORT wxPreviewControlBar
: public wxPanel
431 DECLARE_CLASS(wxPreviewControlBar
)
434 wxPreviewControlBar(wxPrintPreviewBase
*preview
,
437 const wxPoint
& pos
= wxDefaultPosition
,
438 const wxSize
& size
= wxDefaultSize
,
439 long style
= wxTAB_TRAVERSAL
,
440 const wxString
& name
= wxT("panel"));
441 virtual ~wxPreviewControlBar();
443 virtual void CreateButtons();
444 virtual void SetZoomControl(int zoom
);
445 virtual int GetZoomControl();
446 virtual wxPrintPreviewBase
*GetPrintPreview() const
447 { return m_printPreview
; }
449 void OnWindowClose(wxCommandEvent
& event
);
456 void OnPrintButton(wxCommandEvent
& WXUNUSED(event
)) { OnPrint(); }
457 void OnNextButton(wxCommandEvent
& WXUNUSED(event
)) { OnNext(); }
458 void OnPreviousButton(wxCommandEvent
& WXUNUSED(event
)) { OnPrevious(); }
459 void OnFirstButton(wxCommandEvent
& WXUNUSED(event
)) { OnFirst(); }
460 void OnLastButton(wxCommandEvent
& WXUNUSED(event
)) { OnLast(); }
461 void OnGotoButton(wxCommandEvent
& WXUNUSED(event
)) { OnGoto(); }
462 void OnZoom(wxCommandEvent
& event
);
463 void OnPaint(wxPaintEvent
& event
);
466 wxPrintPreviewBase
* m_printPreview
;
467 wxButton
* m_closeButton
;
468 wxButton
* m_nextPageButton
;
469 wxButton
* m_previousPageButton
;
470 wxButton
* m_printButton
;
471 wxChoice
* m_zoomControl
;
472 wxButton
* m_firstPageButton
;
473 wxButton
* m_lastPageButton
;
474 wxButton
* m_gotoPageButton
;
478 DECLARE_EVENT_TABLE()
479 DECLARE_NO_COPY_CLASS(wxPreviewControlBar
)
482 //----------------------------------------------------------------------------
483 // wxPrintPreviewBase
484 //----------------------------------------------------------------------------
487 * Programmer creates an object of this class to preview a wxPrintout.
490 class WXDLLEXPORT wxPrintPreviewBase
: public wxObject
493 wxPrintPreviewBase(wxPrintout
*printout
,
494 wxPrintout
*printoutForPrinting
= (wxPrintout
*) NULL
,
495 wxPrintDialogData
*data
= (wxPrintDialogData
*) NULL
);
496 wxPrintPreviewBase(wxPrintout
*printout
,
497 wxPrintout
*printoutForPrinting
,
499 virtual ~wxPrintPreviewBase();
501 virtual bool SetCurrentPage(int pageNum
);
502 virtual int GetCurrentPage() const;
504 virtual void SetPrintout(wxPrintout
*printout
);
505 virtual wxPrintout
*GetPrintout() const;
506 virtual wxPrintout
*GetPrintoutForPrinting() const;
508 virtual void SetFrame(wxFrame
*frame
);
509 virtual void SetCanvas(wxPreviewCanvas
*canvas
);
511 virtual wxFrame
*GetFrame() const;
512 virtual wxPreviewCanvas
*GetCanvas() const;
514 // This is a helper routine, used by the next 4 routines.
516 virtual void CalcRects(wxPreviewCanvas
*canvas
, wxRect
& printableAreaRect
, wxRect
& paperRect
);
518 // The preview canvas should call this from OnPaint
519 virtual bool PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
);
521 // This draws a blank page onto the preview canvas
522 virtual bool DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
);
524 // Adjusts the scrollbars for the current scale
525 virtual void AdjustScrollbars(wxPreviewCanvas
*canvas
);
527 // This is called by wxPrintPreview to render a page into a wxMemoryDC.
528 virtual bool RenderPage(int pageNum
);
531 virtual void SetZoom(int percent
);
532 virtual int GetZoom() const;
534 virtual wxPrintDialogData
& GetPrintDialogData();
536 virtual int GetMaxPage() const;
537 virtual int GetMinPage() const;
539 virtual bool Ok() const { return IsOk(); }
540 virtual bool IsOk() const;
541 virtual void SetOk(bool ok
);
543 ///////////////////////////////////////////////////////////////////////////
546 // If we own a wxPrintout that can be used for printing, this
547 // will invoke the actual printing procedure. Called
548 // by the wxPreviewControlBar.
549 virtual bool Print(bool interactive
) = 0;
551 // Calculate scaling that needs to be done to get roughly
552 // the right scaling for the screen pretending to be
553 // the currently selected printer.
554 virtual void DetermineScaling() = 0;
557 wxPrintDialogData m_printDialogData
;
558 wxPreviewCanvas
* m_previewCanvas
;
559 wxFrame
* m_previewFrame
;
560 wxBitmap
* m_previewBitmap
;
561 wxPrintout
* m_previewPrintout
;
562 wxPrintout
* m_printPrintout
;
565 float m_previewScaleX
;
566 float m_previewScaleY
;
575 bool m_printingPrepared
; // Called OnPreparePrinting?
578 void Init(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
);
580 DECLARE_NO_COPY_CLASS(wxPrintPreviewBase
)
581 DECLARE_CLASS(wxPrintPreviewBase
)
584 //----------------------------------------------------------------------------
586 //----------------------------------------------------------------------------
588 class WXDLLEXPORT wxPrintPreview
: public wxPrintPreviewBase
591 wxPrintPreview(wxPrintout
*printout
,
592 wxPrintout
*printoutForPrinting
= (wxPrintout
*) NULL
,
593 wxPrintDialogData
*data
= (wxPrintDialogData
*) NULL
);
594 wxPrintPreview(wxPrintout
*printout
,
595 wxPrintout
*printoutForPrinting
,
597 virtual ~wxPrintPreview();
599 virtual bool SetCurrentPage(int pageNum
);
600 virtual int GetCurrentPage() const;
601 virtual void SetPrintout(wxPrintout
*printout
);
602 virtual wxPrintout
*GetPrintout() const;
603 virtual wxPrintout
*GetPrintoutForPrinting() const;
604 virtual void SetFrame(wxFrame
*frame
);
605 virtual void SetCanvas(wxPreviewCanvas
*canvas
);
607 virtual wxFrame
*GetFrame() const;
608 virtual wxPreviewCanvas
*GetCanvas() const;
609 virtual bool PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
);
610 virtual bool DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
);
611 virtual void AdjustScrollbars(wxPreviewCanvas
*canvas
);
612 virtual bool RenderPage(int pageNum
);
613 virtual void SetZoom(int percent
);
614 virtual int GetZoom() const;
616 virtual bool Print(bool interactive
);
617 virtual void DetermineScaling();
619 virtual wxPrintDialogData
& GetPrintDialogData();
621 virtual int GetMaxPage() const;
622 virtual int GetMinPage() const;
624 virtual bool Ok() const { return IsOk(); }
625 virtual bool IsOk() const;
626 virtual void SetOk(bool ok
);
629 wxPrintPreviewBase
*m_pimpl
;
632 DECLARE_CLASS(wxPrintPreview
)
633 DECLARE_NO_COPY_CLASS(wxPrintPreview
)
636 //----------------------------------------------------------------------------
637 // wxPrintAbortDialog
638 //----------------------------------------------------------------------------
640 class WXDLLEXPORT wxPrintAbortDialog
: public wxDialog
643 wxPrintAbortDialog(wxWindow
*parent
,
644 const wxString
& title
,
645 const wxPoint
& pos
= wxDefaultPosition
,
646 const wxSize
& size
= wxDefaultSize
,
648 const wxString
& name
= wxT("dialog"))
649 : wxDialog(parent
, wxID_ANY
, title
, pos
, size
, style
, name
)
653 void OnCancel(wxCommandEvent
& event
);
656 DECLARE_EVENT_TABLE()
657 DECLARE_NO_COPY_CLASS(wxPrintAbortDialog
)
660 #endif // wxUSE_PRINTING_ARCHITECTURE