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