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