Added new wxPrintFactory code and made wxPrinter
[wxWidgets.git] / include / wx / prntbase.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: prntbase.h
3 // Purpose: Base classes for printing framework
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_PRNTBASEH__
13 #define _WX_PRNTBASEH__
14
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "prntbase.h"
17 #endif
18
19 #include "wx/defs.h"
20
21 #if wxUSE_PRINTING_ARCHITECTURE
22
23 #include "wx/event.h"
24 #include "wx/cmndata.h"
25 #include "wx/panel.h"
26 #include "wx/scrolwin.h"
27 #include "wx/dialog.h"
28 #include "wx/frame.h"
29
30 class WXDLLEXPORT wxDC;
31 class WXDLLEXPORT wxButton;
32 class WXDLLEXPORT wxChoice;
33 class WXDLLEXPORT wxPrintout;
34 class WXDLLEXPORT wxPrinterBase;
35 class WXDLLEXPORT wxPrintDialog;
36 class WXDLLEXPORT wxPrintPreviewBase;
37 class WXDLLEXPORT wxPreviewCanvas;
38 class WXDLLEXPORT wxPreviewControlBar;
39 class WXDLLEXPORT wxPreviewFrame;
40 class WXDLLEXPORT wxPrintFactory;
41
42 //----------------------------------------------------------------------------
43 // error consts
44 //----------------------------------------------------------------------------
45
46 enum wxPrinterError
47 {
48 wxPRINTER_NO_ERROR = 0,
49 wxPRINTER_CANCELLED,
50 wxPRINTER_ERROR
51 };
52
53 //----------------------------------------------------------------------------
54 // wxPrintFactory
55 //----------------------------------------------------------------------------
56
57 class WXDLLEXPORT wxPrintFactory
58 {
59 public:
60 wxPrintFactory() {}
61 virtual ~wxPrintFactory() {}
62
63 virtual bool HasPageSetupDialog() = 0;
64 virtual bool HasPrintSetupDialog() = 0;
65
66 virtual wxPrinterBase *CreatePrinter( wxPrintDialogData* data ) = 0;
67 virtual wxPrintPreviewBase *CreatePrintPreview( wxPrintout *preview,
68 wxPrintout *printout = NULL,
69 wxPrintDialogData *data = NULL ) = 0;
70 virtual wxPrintPreviewBase *CreatePrintPreview( wxPrintout *preview,
71 wxPrintout *printout,
72 wxPrintData *data ) = 0;
73
74 static void SetPrintFactory( wxPrintFactory *factory );
75 static wxPrintFactory *GetFactory();
76 static wxPrintFactory *m_factory;
77 };
78
79 class WXDLLEXPORT wxNativePrintFactory: public wxPrintFactory
80 {
81 public:
82 virtual bool HasPageSetupDialog()
83 { return true; }
84 virtual bool HasPrintSetupDialog()
85 { return true; }
86
87 virtual wxPrinterBase *CreatePrinter( wxPrintDialogData *data );
88 virtual wxPrintPreviewBase *CreatePrintPreview( wxPrintout *preview,
89 wxPrintout *printout = NULL,
90 wxPrintDialogData *data = NULL );
91 virtual wxPrintPreviewBase *CreatePrintPreview( wxPrintout *preview,
92 wxPrintout *printout,
93 wxPrintData *data );
94 };
95
96 //----------------------------------------------------------------------------
97 // wxPrinterBase
98 //----------------------------------------------------------------------------
99
100 /*
101 * Represents the printer: manages printing a wxPrintout object
102 */
103
104 class WXDLLEXPORT wxPrinterBase: public wxObject
105 {
106 public:
107 wxPrinterBase(wxPrintDialogData *data = (wxPrintDialogData *) NULL);
108 virtual ~wxPrinterBase();
109
110 virtual wxWindow *CreateAbortWindow(wxWindow *parent, wxPrintout *printout);
111 virtual void ReportError(wxWindow *parent, wxPrintout *printout, const wxString& message);
112
113 wxPrintDialogData& GetPrintDialogData() const
114 { return (wxPrintDialogData&) m_printDialogData; }
115 bool GetAbort() const { return sm_abortIt; }
116
117 static wxPrinterError GetLastError() { return sm_lastError; }
118
119 ///////////////////////////////////////////////////////////////////////////
120 // OVERRIDES
121
122 virtual bool Setup(wxWindow *parent) = 0;
123 virtual bool Print(wxWindow *parent, wxPrintout *printout, bool prompt = true) = 0;
124 virtual wxDC* PrintDialog(wxWindow *parent) = 0;
125
126 protected:
127 wxPrintDialogData m_printDialogData;
128 wxPrintout* m_currentPrintout;
129
130 static wxPrinterError sm_lastError;
131
132 public:
133 static wxWindow* sm_abortWindow;
134 static bool sm_abortIt;
135
136 private:
137 DECLARE_CLASS(wxPrinterBase)
138 DECLARE_NO_COPY_CLASS(wxPrinterBase)
139 };
140
141 //----------------------------------------------------------------------------
142 // wxPrinter
143 //----------------------------------------------------------------------------
144
145 class WXDLLEXPORT wxPrinter: public wxPrinterBase
146 {
147 public:
148 wxPrinter(wxPrintDialogData *data = (wxPrintDialogData *) NULL);
149 virtual ~wxPrinter();
150
151 virtual wxWindow *CreateAbortWindow(wxWindow *parent, wxPrintout *printout);
152 virtual void ReportError(wxWindow *parent, wxPrintout *printout, const wxString& message);
153
154 virtual bool Setup(wxWindow *parent);
155 virtual bool Print(wxWindow *parent, wxPrintout *printout, bool prompt = true);
156 virtual wxDC* PrintDialog(wxWindow *parent);
157
158 protected:
159 wxPrinterBase *m_pimpl;
160
161 private:
162 DECLARE_CLASS(wxPrinter)
163 DECLARE_NO_COPY_CLASS(wxPrinter)
164 };
165
166 //----------------------------------------------------------------------------
167 // wxPrintout
168 //----------------------------------------------------------------------------
169
170 /*
171 * Represents an object via which a document may be printed.
172 * The programmer derives from this, overrides (at least) OnPrintPage,
173 * and passes it to a wxPrinter object for printing, or a wxPrintPreview
174 * object for previewing.
175 */
176
177 class WXDLLEXPORT wxPrintout: public wxObject
178 {
179 public:
180 wxPrintout(const wxString& title = wxT("Printout"));
181 virtual ~wxPrintout();
182
183 virtual bool OnBeginDocument(int startPage, int endPage);
184 virtual void OnEndDocument();
185 virtual void OnBeginPrinting();
186 virtual void OnEndPrinting();
187
188 // Guaranteed to be before any other functions are called
189 virtual void OnPreparePrinting() { }
190
191 virtual bool HasPage(int page);
192 virtual bool OnPrintPage(int page) = 0;
193 virtual void GetPageInfo(int *minPage, int *maxPage, int *pageFrom, int *pageTo);
194
195 virtual wxString GetTitle() const { return m_printoutTitle; }
196
197 wxDC *GetDC() const { return m_printoutDC; }
198 void SetDC(wxDC *dc) { m_printoutDC = dc; }
199 void SetPageSizePixels(int w, int h) { m_pageWidthPixels = w; m_pageHeightPixels = h; }
200 void GetPageSizePixels(int *w, int *h) const { *w = m_pageWidthPixels; *h = m_pageHeightPixels; }
201 void SetPageSizeMM(int w, int h) { m_pageWidthMM = w; m_pageHeightMM = h; }
202 void GetPageSizeMM(int *w, int *h) const { *w = m_pageWidthMM; *h = m_pageHeightMM; }
203
204 void SetPPIScreen(int x, int y) { m_PPIScreenX = x; m_PPIScreenY = y; }
205 void GetPPIScreen(int *x, int *y) const { *x = m_PPIScreenX; *y = m_PPIScreenY; }
206 void SetPPIPrinter(int x, int y) { m_PPIPrinterX = x; m_PPIPrinterY = y; }
207 void GetPPIPrinter(int *x, int *y) const { *x = m_PPIPrinterX; *y = m_PPIPrinterY; }
208
209 virtual bool IsPreview() const { return m_isPreview; }
210
211 virtual void SetIsPreview(bool p) { m_isPreview = p; }
212
213 private:
214 wxString m_printoutTitle;
215 wxDC* m_printoutDC;
216
217 int m_pageWidthPixels;
218 int m_pageHeightPixels;
219
220 int m_pageWidthMM;
221 int m_pageHeightMM;
222
223 int m_PPIScreenX;
224 int m_PPIScreenY;
225 int m_PPIPrinterX;
226 int m_PPIPrinterY;
227
228 bool m_isPreview;
229
230 private:
231 DECLARE_ABSTRACT_CLASS(wxPrintout)
232 DECLARE_NO_COPY_CLASS(wxPrintout)
233 };
234
235 /*
236 * wxPreviewCanvas
237 * Canvas upon which a preview is drawn.
238 */
239
240 class WXDLLEXPORT wxPreviewCanvas: public wxScrolledWindow
241 {
242 public:
243 wxPreviewCanvas(wxPrintPreviewBase *preview,
244 wxWindow *parent,
245 const wxPoint& pos = wxDefaultPosition,
246 const wxSize& size = wxDefaultSize,
247 long style = 0,
248 const wxString& name = wxT("canvas"));
249 ~wxPreviewCanvas();
250
251 void OnPaint(wxPaintEvent& event);
252 void OnChar(wxKeyEvent &event);
253
254 // Responds to colour changes
255 void OnSysColourChanged(wxSysColourChangedEvent& event);
256
257 private:
258 wxPrintPreviewBase* m_printPreview;
259
260 DECLARE_CLASS(wxPreviewCanvas)
261 DECLARE_EVENT_TABLE()
262 DECLARE_NO_COPY_CLASS(wxPreviewCanvas)
263 };
264
265 /*
266 * wxPreviewFrame
267 * Default frame for showing preview.
268 */
269
270 class WXDLLEXPORT wxPreviewFrame: public wxFrame
271 {
272 public:
273 wxPreviewFrame(wxPrintPreviewBase *preview,
274 wxWindow *parent,
275 const wxString& title = wxT("Print Preview"),
276 const wxPoint& pos = wxDefaultPosition,
277 const wxSize& size = wxDefaultSize,
278 long style = wxDEFAULT_FRAME_STYLE,
279 const wxString& name = wxT("frame"));
280 ~wxPreviewFrame();
281
282 void OnCloseWindow(wxCloseEvent& event);
283 virtual void Initialize();
284 virtual void CreateCanvas();
285 virtual void CreateControlBar();
286
287 inline wxPreviewControlBar* GetControlBar() const { return m_controlBar; }
288
289 protected:
290 wxPreviewCanvas* m_previewCanvas;
291 wxPreviewControlBar* m_controlBar;
292 wxPrintPreviewBase* m_printPreview;
293 wxWindowDisabler* m_windowDisabler;
294
295 private:
296 DECLARE_CLASS(wxPreviewFrame)
297 DECLARE_EVENT_TABLE()
298 DECLARE_NO_COPY_CLASS(wxPreviewFrame)
299 };
300
301 /*
302 * wxPreviewControlBar
303 * A panel with buttons for controlling a print preview.
304 * The programmer may wish to use other means for controlling
305 * the print preview.
306 */
307
308 #define wxPREVIEW_PRINT 1
309 #define wxPREVIEW_PREVIOUS 2
310 #define wxPREVIEW_NEXT 4
311 #define wxPREVIEW_ZOOM 8
312 #define wxPREVIEW_FIRST 16
313 #define wxPREVIEW_LAST 32
314 #define wxPREVIEW_GOTO 64
315
316 #define wxPREVIEW_DEFAULT wxPREVIEW_PREVIOUS|wxPREVIEW_NEXT|wxPREVIEW_ZOOM\
317 |wxPREVIEW_FIRST|wxPREVIEW_GOTO|wxPREVIEW_LAST
318
319 // Ids for controls
320 #define wxID_PREVIEW_CLOSE 1
321 #define wxID_PREVIEW_NEXT 2
322 #define wxID_PREVIEW_PREVIOUS 3
323 #define wxID_PREVIEW_PRINT 4
324 #define wxID_PREVIEW_ZOOM 5
325 #define wxID_PREVIEW_FIRST 6
326 #define wxID_PREVIEW_LAST 7
327 #define wxID_PREVIEW_GOTO 8
328
329 class WXDLLEXPORT wxPreviewControlBar: public wxPanel
330 {
331 DECLARE_CLASS(wxPreviewControlBar)
332
333 public:
334 wxPreviewControlBar(wxPrintPreviewBase *preview,
335 long buttons,
336 wxWindow *parent,
337 const wxPoint& pos = wxDefaultPosition,
338 const wxSize& size = wxDefaultSize,
339 long style = wxTAB_TRAVERSAL,
340 const wxString& name = wxT("panel"));
341 ~wxPreviewControlBar();
342
343 virtual void CreateButtons();
344 virtual void SetZoomControl(int zoom);
345 virtual int GetZoomControl();
346 virtual wxPrintPreviewBase *GetPrintPreview() const
347 { return m_printPreview; }
348
349 void OnWindowClose(wxCommandEvent& event);
350 void OnNext();
351 void OnPrevious();
352 void OnFirst();
353 void OnLast();
354 void OnGoto();
355 void OnPrint();
356 void OnPrintButton(wxCommandEvent& WXUNUSED(event)) { OnPrint(); }
357 void OnNextButton(wxCommandEvent & WXUNUSED(event)) { OnNext(); }
358 void OnPreviousButton(wxCommandEvent & WXUNUSED(event)) { OnPrevious(); }
359 void OnFirstButton(wxCommandEvent & WXUNUSED(event)) { OnFirst(); }
360 void OnLastButton(wxCommandEvent & WXUNUSED(event)) { OnLast(); }
361 void OnGotoButton(wxCommandEvent & WXUNUSED(event)) { OnGoto(); }
362 void OnZoom(wxCommandEvent& event);
363 void OnPaint(wxPaintEvent& event);
364
365 protected:
366 wxPrintPreviewBase* m_printPreview;
367 wxButton* m_closeButton;
368 wxButton* m_nextPageButton;
369 wxButton* m_previousPageButton;
370 wxButton* m_printButton;
371 wxChoice* m_zoomControl;
372 wxButton* m_firstPageButton;
373 wxButton* m_lastPageButton;
374 wxButton* m_gotoPageButton;
375 long m_buttonFlags;
376
377 private:
378 DECLARE_EVENT_TABLE()
379 DECLARE_NO_COPY_CLASS(wxPreviewControlBar)
380 };
381
382 //----------------------------------------------------------------------------
383 // wxPrintPreviewBase
384 //----------------------------------------------------------------------------
385
386 /*
387 * Programmer creates an object of this class to preview a wxPrintout.
388 */
389
390 class WXDLLEXPORT wxPrintPreviewBase: public wxObject
391 {
392 public:
393 wxPrintPreviewBase(wxPrintout *printout,
394 wxPrintout *printoutForPrinting = (wxPrintout *) NULL,
395 wxPrintDialogData *data = (wxPrintDialogData *) NULL);
396 wxPrintPreviewBase(wxPrintout *printout,
397 wxPrintout *printoutForPrinting,
398 wxPrintData *data);
399 virtual ~wxPrintPreviewBase();
400
401 virtual bool SetCurrentPage(int pageNum);
402 virtual int GetCurrentPage() const;
403
404 virtual void SetPrintout(wxPrintout *printout);
405 virtual wxPrintout *GetPrintout() const;
406 virtual wxPrintout *GetPrintoutForPrinting() const;
407
408 virtual void SetFrame(wxFrame *frame);
409 virtual void SetCanvas(wxPreviewCanvas *canvas);
410
411 virtual wxFrame *GetFrame() const;
412 virtual wxPreviewCanvas *GetCanvas() const;
413
414 // The preview canvas should call this from OnPaint
415 virtual bool PaintPage(wxPreviewCanvas *canvas, wxDC& dc);
416
417 // This draws a blank page onto the preview canvas
418 virtual bool DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc);
419
420 // Adjusts the scrollbars for the current scale
421 virtual void AdjustScrollbars(wxPreviewCanvas *canvas);
422
423 // This is called by wxPrintPreview to render a page into a wxMemoryDC.
424 virtual bool RenderPage(int pageNum);
425
426
427 virtual void SetZoom(int percent);
428 virtual int GetZoom() const;
429
430 virtual wxPrintDialogData& GetPrintDialogData();
431
432 virtual int GetMaxPage() const;
433 virtual int GetMinPage() const;
434
435 virtual bool Ok() const;
436 virtual void SetOk(bool ok);
437
438 ///////////////////////////////////////////////////////////////////////////
439 // OVERRIDES
440
441 // If we own a wxPrintout that can be used for printing, this
442 // will invoke the actual printing procedure. Called
443 // by the wxPreviewControlBar.
444 virtual bool Print(bool interactive) = 0;
445
446 // Calculate scaling that needs to be done to get roughly
447 // the right scaling for the screen pretending to be
448 // the currently selected printer.
449 virtual void DetermineScaling() = 0;
450
451 protected:
452 wxPrintDialogData m_printDialogData;
453 wxPreviewCanvas* m_previewCanvas;
454 wxFrame* m_previewFrame;
455 wxBitmap* m_previewBitmap;
456 wxPrintout* m_previewPrintout;
457 wxPrintout* m_printPrintout;
458 int m_currentPage;
459 int m_currentZoom;
460 float m_previewScale;
461 int m_topMargin;
462 int m_leftMargin;
463 int m_pageWidth;
464 int m_pageHeight;
465 int m_minPage;
466 int m_maxPage;
467
468 bool m_isOk;
469 bool m_printingPrepared; // Called OnPreparePrinting?
470
471 private:
472 void Init(wxPrintout *printout, wxPrintout *printoutForPrinting);
473
474 DECLARE_NO_COPY_CLASS(wxPrintPreviewBase)
475 DECLARE_CLASS(wxPrintPreviewBase)
476 };
477
478 //----------------------------------------------------------------------------
479 // wxPrintPreview
480 //----------------------------------------------------------------------------
481
482 class WXDLLEXPORT wxPrintPreview: public wxPrintPreviewBase
483 {
484 public:
485 wxPrintPreview(wxPrintout *printout,
486 wxPrintout *printoutForPrinting = (wxPrintout *) NULL,
487 wxPrintDialogData *data = (wxPrintDialogData *) NULL);
488 wxPrintPreview(wxPrintout *printout,
489 wxPrintout *printoutForPrinting,
490 wxPrintData *data);
491 virtual ~wxPrintPreview();
492
493 virtual bool SetCurrentPage(int pageNum);
494 virtual int GetCurrentPage() const;
495 virtual void SetPrintout(wxPrintout *printout);
496 virtual wxPrintout *GetPrintout() const;
497 virtual wxPrintout *GetPrintoutForPrinting() const;
498 virtual void SetFrame(wxFrame *frame);
499 virtual void SetCanvas(wxPreviewCanvas *canvas);
500
501 virtual wxFrame *GetFrame() const;
502 virtual wxPreviewCanvas *GetCanvas() const;
503 virtual bool PaintPage(wxPreviewCanvas *canvas, wxDC& dc);
504 virtual bool DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc);
505 virtual void AdjustScrollbars(wxPreviewCanvas *canvas);
506 virtual bool RenderPage(int pageNum);
507 virtual void SetZoom(int percent);
508
509 virtual bool Print(bool interactive);
510 virtual void DetermineScaling();
511
512 virtual wxPrintDialogData& GetPrintDialogData();
513
514 virtual int GetMaxPage() const;
515 virtual int GetMinPage() const;
516
517 virtual bool Ok() const;
518 virtual void SetOk(bool ok);
519
520 private:
521 wxPrintPreviewBase *m_pimpl;
522
523 private:
524 DECLARE_CLASS(wxPrintPreview)
525 DECLARE_NO_COPY_CLASS(wxPrintPreview)
526 };
527
528 //----------------------------------------------------------------------------
529 // wxPrintAbortDialog
530 //----------------------------------------------------------------------------
531
532 class WXDLLEXPORT wxPrintAbortDialog: public wxDialog
533 {
534 public:
535 wxPrintAbortDialog(wxWindow *parent,
536 const wxString& title,
537 const wxPoint& pos = wxDefaultPosition,
538 const wxSize& size = wxDefaultSize,
539 long style = 0,
540 const wxString& name = wxT("dialog"))
541 : wxDialog(parent, wxID_ANY, title, pos, size, style, name)
542 {
543 }
544
545 void OnCancel(wxCommandEvent& event);
546
547 private:
548 DECLARE_EVENT_TABLE()
549 DECLARE_NO_COPY_CLASS(wxPrintAbortDialog)
550 };
551
552 #endif // wxUSE_PRINTING_ARCHITECTURE
553
554 #endif
555 // _WX_PRNTBASEH__