]> git.saurik.com Git - wxWidgets.git/blob - interface/print.h
5ad64366d1b41d7904114dbabaf046767f841b0e
[wxWidgets.git] / interface / print.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: print.h
3 // Purpose: documentation for wxPreviewControlBar class
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 @class wxPreviewControlBar
11 @wxheader{print.h}
12
13 This is the default implementation of the preview control bar, a panel
14 with buttons and a zoom control. You can derive a new class from this and
15 override some or all member functions to change the behaviour and appearance;
16 or you can leave it as it is.
17
18 @library{wxbase}
19 @category{printing}
20
21 @seealso
22 wxPreviewFrame, wxPreviewCanvas, wxPrintPreview
23 */
24 class wxPreviewControlBar : public wxPanel
25 {
26 public:
27 /**
28 Destructor.
29 */
30 ~wxPreviewControlBar();
31
32 /**
33 Creates buttons, according to value of the button style flags.
34 */
35 void CreateButtons();
36
37 /**
38 Gets the print preview object associated with the control bar.
39 */
40 wxPrintPreview* GetPrintPreview();
41
42 /**
43 Gets the current zoom setting in percent.
44 */
45 int GetZoomControl();
46
47 /**
48 Sets the zoom control.
49 */
50 void SetZoomControl(int percent);
51
52 /**
53 Constructor.
54 The buttons parameter may be a combination of the following, using the bitwise
55 'or' operator.
56
57 wxPREVIEW_PRINT
58
59 Create a print button.
60
61 wxPREVIEW_NEXT
62
63 Create a next page button.
64
65 wxPREVIEW_PREVIOUS
66
67 Create a previous page button.
68
69 wxPREVIEW_ZOOM
70
71 Create a zoom control.
72
73 wxPREVIEW_DEFAULT
74
75 Equivalent to a combination of wxPREVIEW_PREVIOUS, wxPREVIEW_NEXT and
76 wxPREVIEW_ZOOM.
77 */
78 wxPreviewControlBar(wxPrintPreview* preview, long buttons,
79 wxWindow* parent,
80 const wxPoint& pos = wxDefaultPosition,
81 const wxSize& size = wxDefaultSize,
82 long style = 0,
83 const wxString& name = "panel");
84 };
85
86
87 /**
88 @class wxPreviewCanvas
89 @wxheader{print.h}
90
91 A preview canvas is the default canvas used by the print preview
92 system to display the preview.
93
94 @library{wxbase}
95 @category{printing}
96
97 @seealso
98 wxPreviewFrame, wxPreviewControlBar, wxPrintPreview
99 */
100 class wxPreviewCanvas : public wxScrolledWindow
101 {
102 public:
103 /**
104 Constructor.
105 */
106 wxPreviewCanvas(wxPrintPreview* preview, wxWindow* parent,
107 const wxPoint& pos = wxDefaultPosition,
108 const wxSize& size = wxDefaultSize,
109 long style = 0,
110 const wxString& name = "canvas");
111
112 /**
113 Destructor.
114 */
115 ~wxPreviewCanvas();
116
117 /**
118 Calls wxPrintPreview::PaintPage to refresh the canvas.
119 */
120 void OnPaint(wxPaintEvent& event);
121 };
122
123
124 /**
125 @class wxPreviewFrame
126 @wxheader{print.h}
127
128 This class provides the default method of managing the print preview interface.
129 Member functions may be overridden to replace functionality, or the
130 class may be used without derivation.
131
132 @library{wxbase}
133 @category{printing}
134
135 @seealso
136 wxPreviewCanvas, wxPreviewControlBar, wxPrintPreview
137 */
138 class wxPreviewFrame : public wxFrame
139 {
140 public:
141 /**
142 Constructor. Pass a print preview object plus other normal frame arguments.
143 The print preview object will be destroyed by the frame when it closes.
144 */
145 wxPreviewFrame(wxPrintPreview* preview, wxWindow* parent,
146 const wxString& title,
147 const wxPoint& pos = wxDefaultPosition,
148 const wxSize& size size = wxDefaultSize,
149 long style = wxDEFAULT_FRAME_STYLE,
150 const wxString& name = "frame");
151
152 /**
153 Destructor.
154 */
155 ~wxPreviewFrame();
156
157 /**
158 Creates a wxPreviewCanvas. Override this function to allow
159 a user-defined preview canvas object to be created.
160 */
161 void CreateCanvas();
162
163 /**
164 Creates a wxPreviewControlBar. Override this function to allow
165 a user-defined preview control bar object to be created.
166 */
167 void CreateControlBar();
168
169 /**
170 Creates the preview canvas and control bar, and calls
171 wxWindow::MakeModal(@true) to disable other top-level windows
172 in the application.
173 This function should be called by the application prior to
174 showing the frame.
175 */
176 void Initialize();
177
178 /**
179 Enables the other frames in the application, and deletes the print preview
180 object, implicitly deleting any printout objects associated with the print
181 preview object.
182 */
183 void OnCloseWindow(wxCloseEvent& event);
184 };
185
186
187 /**
188 @class wxPrintPreview
189 @wxheader{print.h}
190
191 Objects of this class manage the print preview process. The object is passed
192 a wxPrintout object, and the wxPrintPreview object itself is passed to
193 a wxPreviewFrame object. Previewing is started by initializing and showing
194 the preview frame. Unlike wxPrinter::Print, flow of control returns to the
195 application
196 immediately after the frame is shown.
197
198 @library{wxbase}
199 @category{printing}
200
201 @seealso
202 @ref overview_printingoverview "Printing framework overview", wxPrinterDC,
203 wxPrintDialog, wxPrintout, wxPrinter, wxPreviewCanvas, wxPreviewControlBar, wxPreviewFrame.
204 */
205 class wxPrintPreview : public wxObject
206 {
207 public:
208 /**
209 Constructor. Pass a printout object, an optional printout object to be
210 used for actual printing, and the address of an optional
211 block of printer data, which will be copied to the print preview object's
212 print data.
213 If @a printoutForPrinting is non-@NULL, a @b Print... button will be placed on
214 the
215 preview frame so that the user can print directly from the preview interface.
216 Do not explicitly delete the printout objects once this destructor has been
217 called, since they will be deleted in the wxPrintPreview constructor.
218 The same does not apply to the @a data argument.
219 Test the Ok member to check whether the wxPrintPreview object was created
220 correctly.
221 Ok could return @false if there was a problem initializing the printer device
222 context
223 (current printer not set, for example).
224 */
225 wxPrintPreview(wxPrintout* printout,
226 wxPrintout* printoutForPrinting,
227 wxPrintData* data = NULL);
228
229 /**
230 Destructor. Deletes both print preview objects, so do not destroy these objects
231 in your application.
232 */
233 ~wxPrinter();
234
235 /**
236 Gets the preview window used for displaying the print preview image.
237 */
238 wxPreviewCanvas* GetCanvas();
239
240 /**
241 Gets the page currently being previewed.
242 */
243 int GetCurrentPage();
244
245 /**
246 Gets the frame used for displaying the print preview canvas
247 and control bar.
248 */
249 wxFrame* GetFrame();
250
251 /**
252 Returns the maximum page number.
253 */
254 int GetMaxPage();
255
256 /**
257 Returns the minimum page number.
258 */
259 int GetMinPage();
260
261 /**
262 Gets the preview printout object associated with the wxPrintPreview object.
263 */
264 wxPrintout* GetPrintout();
265
266 /**
267 Gets the printout object to be used for printing from within the preview
268 interface,
269 or @NULL if none exists.
270 */
271 wxPrintout* GetPrintoutForPrinting();
272
273 /**
274 Returns @true if the wxPrintPreview is valid, @false otherwise. It could return
275 @false if there was a
276 problem initializing the printer device context (current printer not set, for
277 example).
278 */
279 bool Ok();
280
281 /**
282 This refreshes the preview window with the preview image.
283 It must be called from the preview window's OnPaint member.
284 The implementation simply blits the preview bitmap onto
285 the canvas, creating a new preview bitmap if none exists.
286 */
287 bool PaintPage(wxPreviewCanvas* canvas, wxDC dc);
288
289 /**
290 Invokes the print process using the second wxPrintout object
291 supplied in the wxPrintPreview constructor.
292 Will normally be called by the @b Print... panel item on the
293 preview frame's control bar.
294 Returns @false in case of error -- call
295 wxPrinter::GetLastError to get detailed
296 information about the kind of the error.
297 */
298 bool Print(bool prompt);
299
300 /**
301 Renders a page into a wxMemoryDC. Used internally by wxPrintPreview.
302 */
303 bool RenderPage(int pageNum);
304
305 /**
306 Sets the window to be used for displaying the print preview image.
307 */
308 void SetCanvas(wxPreviewCanvas* window);
309
310 /**
311 Sets the current page to be previewed.
312 */
313 void SetCurrentPage(int pageNum);
314
315 /**
316 Sets the frame to be used for displaying the print preview canvas
317 and control bar.
318 */
319 void SetFrame(wxFrame* frame);
320
321 /**
322 Associates a printout object with the wxPrintPreview object.
323 */
324 void SetPrintout(wxPrintout* printout);
325
326 /**
327 Sets the percentage preview zoom, and refreshes the preview canvas
328 accordingly.
329 */
330 void SetZoom(int percent);
331 };
332
333
334 /**
335 @class wxPrinter
336 @wxheader{print.h}
337
338 This class represents the Windows or PostScript printer, and is the vehicle
339 through
340 which printing may be launched by an application. Printing can also
341 be achieved through using of lower functions and classes, but
342 this and associated classes provide a more convenient and general
343 method of printing.
344
345 @library{wxbase}
346 @category{printing}
347
348 @seealso
349 @ref overview_printingoverview "Printing framework overview", wxPrinterDC,
350 wxPrintDialog, wxPrintout, wxPrintPreview.
351 */
352 class wxPrinter : public wxObject
353 {
354 public:
355 /**
356 Constructor. Pass an optional pointer to a block of print
357 dialog data, which will be copied to the printer object's local data.
358
359 @see wxPrintDialogData, wxPrintData
360 */
361 wxPrinter(wxPrintDialogData* data = NULL);
362
363 /**
364 Creates the default printing abort window, with a cancel button.
365 */
366 void CreateAbortWindow(wxWindow* parent, wxPrintout* printout);
367
368 /**
369 Returns @true if the user has aborted the print job.
370 */
371 bool GetAbort();
372
373 /**
374 Return last error. Valid after calling Print(),
375 PrintDialog() or
376 wxPrintPreview::Print. These functions
377 set last error to @b wxPRINTER_NO_ERROR if no error happened.
378 Returned value is one of the following:
379
380 @b wxPRINTER_NO_ERROR
381
382 No error happened.
383
384 @b wxPRINTER_CANCELLED
385
386 The user cancelled printing.
387
388 @b wxPRINTER_ERROR
389
390 There was an error during printing.
391 */
392 static wxPrinterError GetLastError();
393
394 /**
395 Returns the @ref overview_wxprintdata "print data" associated with the printer
396 object.
397 */
398 wxPrintDialogData GetPrintDialogData();
399
400 /**
401 Starts the printing process. Provide a parent window, a user-defined wxPrintout
402 object which controls
403 the printing of a document, and whether the print dialog should be invoked
404 first.
405 Print could return @false if there was a problem initializing the printer device
406 context
407 (current printer not set, for example) or the user cancelled printing. Call
408 GetLastError() to get detailed
409 information about the kind of the error.
410 */
411 bool Print(wxWindow* parent, wxPrintout* printout,
412 bool prompt = true);
413
414 /**
415 Invokes the print dialog. If successful (the user did not press Cancel
416 and no error occurred), a suitable device context will be returned
417 (otherwise @NULL is returned -- call
418 GetLastError() to get detailed
419 information about the kind of the error).
420 The application must delete this device context to avoid a memory leak.
421 */
422 wxDC* PrintDialog(wxWindow* parent);
423
424 /**
425 Default error-reporting function.
426 */
427 void ReportError(wxWindow* parent, wxPrintout* printout,
428 const wxString& message);
429
430 /**
431 Invokes the print setup dialog. Note that the setup dialog is obsolete from
432 Windows 95, though retained for backward compatibility.
433 */
434 bool Setup(wxWindow* parent);
435 };
436
437
438 /**
439 @class wxPrintout
440 @wxheader{print.h}
441
442 This class encapsulates the functionality of printing out an application
443 document. A new class must be derived and members overridden to respond to calls
444 such as OnPrintPage and HasPage and to render the print image onto an associated
445 wxDC. Instances of this class are passed to wxPrinter::Print or
446 to a wxPrintPreview object to initiate printing or previewing.
447
448 Your derived wxPrintout is responsible for drawing both the preview image and
449 the printed page. If your windows' drawing routines accept an arbitrary DC as an
450 argument, you can re-use those routines within your wxPrintout subclass to draw
451 the printout image. You may also add additional drawing elements within your
452 wxPrintout subclass, like headers, footers, and/or page numbers. However, the
453 image on the printed page will often differ from the image drawn on the screen,
454 as will the print preview image -- not just in the presence of headers and
455 footers, but typically in scale. A high-resolution printer presents a much
456 larger drawing surface (i.e., a higher-resolution DC); a zoomed-out preview
457 image presents a much smaller drawing surface (lower-resolution DC). By using
458 the routines FitThisSizeToXXX() and/or MapScreenSizeToXXX() within your
459 wxPrintout subclass to set the user scale and origin of the associated DC, you
460 can easily use a single drawing routine to draw on your application's windows,
461 to create the print preview image, and to create the printed paper image, and
462 achieve a common appearance to the preview image and the printed page.
463
464 @library{wxbase}
465 @category{printing}
466
467 @seealso
468 @ref overview_printingoverview "Printing framework overview", wxPrinterDC,
469 wxPrintDialog, wxPageSetupDialog, wxPrinter, wxPrintPreview
470 */
471 class wxPrintout : public wxObject
472 {
473 public:
474 /**
475 Constructor. Pass an optional title argument - the current filename would be a
476 good idea. This will appear in the printing list
477 (at least in MSW)
478 */
479 wxPrintout(const wxString& title = "Printout");
480
481 /**
482 Destructor.
483 */
484 ~wxPrintout();
485
486 /**
487 Set the user scale and device origin of the wxDC associated with this wxPrintout
488 so that the given image size fits entirely within the page rectangle and the
489 origin is at the top left corner of the page rectangle. On MSW and Mac, the page
490 rectangle is the printable area of the page. On other platforms and PostScript
491 printing, the page rectangle is the entire paper. Use this if you want your
492 printed image as large as possible, but with the caveat that on some platforms,
493 portions of the image might be cut off at the edges.
494 */
495 void FitThisSizeToPage(const wxSize& imageSize);
496
497 /**
498 Set the user scale and device origin of the wxDC associated with this wxPrintout
499 so that the given image size fits entirely within the page margins set in the
500 given wxPageSetupDialogData object. This function provides the greatest
501 consistency across all platforms because it does not depend on having access to
502 the printable area of the paper. Note that on Mac, the native wxPageSetupDialog
503 does not let you set the page margins; you'll have to provide your own
504 mechanism,
505 or you can use the Mac-only class wxMacPageMarginsDialog.
506 */
507 void FitThisSizeToPageMargins(const wxSize& imageSize,
508 const wxPageSetupDialogData& pageSetupData);
509
510 /**
511 Set the user scale and device origin of the wxDC associated with this wxPrintout
512 so that the given image size fits entirely within the paper and the origin is at
513 the top left corner of the paper. Note that with most printers, the region
514 around the edges of the paper are not printable so that the edges of the image
515 could be cut off. Use this if you're managing your own page margins.
516 */
517 void FitThisSizeToPaper(const wxSize& imageSize);
518
519 /**
520 Returns the device context associated with the printout (given to the printout
521 at start of
522 printing or previewing). This will be a wxPrinterDC if printing under Windows
523 or Mac,
524 a wxPostScriptDC if printing on other platforms, and a wxMemoryDC if previewing.
525 */
526 wxDC* GetDC();
527
528 /**
529 Return the rectangle corresponding to the page margins specified by the given
530 wxPageSetupDialogData object in the associated wxDC's logical coordinates for
531 the
532 current user scale and device origin. The page margins are specified
533 with respect to the edges of the paper on all platforms.
534 */
535 wxRect GetLogicalPageMarginsRect(const wxPageSetupDialogData& pageSetupData);
536
537 /**
538 Return the rectangle corresponding to the page in the associated wxDC's
539 logical coordinates for the current user scale and device origin.
540 On MSW and Mac, this will be the printable area of the paper. On other platforms
541 and PostScript printing, this will be the full paper rectangle.
542 */
543 wxRect GetLogicalPageRect();
544
545 /**
546 Return the rectangle corresponding to the paper in the associated wxDC's
547 logical coordinates for the current user scale and device origin.
548 */
549 wxRect GetLogicalPaperRect();
550
551 /**
552 Returns the number of pixels per logical inch of the printer device context.
553 Dividing the printer PPI by the screen PPI can give a suitable scaling factor
554 for drawing text onto the printer. Remember to multiply this by a scaling factor
555 to take the preview DC size into account. Or you can just use the
556 FitThisSizeToXXX() and MapScreenSizeToXXX routines below, which do most of the
557 scaling calculations for you.
558 */
559 void GetPPIPrinter(int* w, int* h);
560
561 /**
562 Returns the number of pixels per logical inch of the screen device context.
563 Dividing the printer PPI by the screen PPI can give a suitable scaling factor
564 for drawing text onto the printer. If you are doing your own scaling, remember
565 to multiply this by a scaling factor to take the preview DC size into account.
566 */
567 void GetPPIScreen(int* w, int* h);
568
569 /**
570 Called by the framework to obtain information from the application about minimum
571 and maximum page values that the user can select, and the required page range to
572 be printed. By default this returns 1, 32000 for the page minimum and maximum
573 values, and 1, 1 for the required page range.
574 If @a minPage is zero, the page number controls in the print dialog will be
575 disabled.
576 */
577 void GetPageInfo(int* minPage, int* maxPage, int* pageFrom,
578 int* pageTo);
579
580 /**
581 Returns the size of the printer page in millimetres.
582 */
583 void GetPageSizeMM(int* w, int* h);
584
585 /**
586 Returns the size of the printer page in pixels, called the page rectangle.
587 The page rectangle has a top left corner at (0,0) and a bottom right corner at
588 (w,h). These values may not be the same as the values returned from
589 wxDC::GetSize; if the printout is being used for
590 previewing, a memory device context is used, which uses a bitmap size reflecting
591 the current preview zoom. The application must take this discrepancy into
592 account if previewing is to be supported.
593 */
594 void GetPageSizePixels(int* w, int* h);
595
596 /**
597 Returns the rectangle that corresponds to the entire paper in pixels, called the
598 paper rectangle. This distinction between paper rectangle and page
599 rectangle reflects the fact that most printers cannot print all the way to the
600 edge of the paper. The page rectangle is a rectangle whose top left corner is at
601 (0,0) and whose width and height are given by
602 wxDC::GetPageSizePixels. On MSW and Mac,
603 the page rectangle gives the printable area of the paper, while the paper
604 rectangle represents the entire paper, including non-printable borders. Thus,
605 the rectangle returned by GetPaperRectPixels will have a top left corner whose
606 coordinates are small negative numbers and the bottom right corner will have
607 values somewhat larger than the width and height given by
608 wxDC::GetPageSizePixels. On other
609 platforms and for PostScript printing, the paper is treated as if its entire
610 area were printable, so this function will return the same rectangle as the page
611 rectangle.
612 */
613 wxRect GetPaperRectPixels();
614
615 /**
616 Returns the title of the printout
617 */
618 wxString GetTitle();
619
620 /**
621 Should be overridden to return @true if the document has this page, or @false
622 if not. Returning @false signifies the end of the document. By default,
623 HasPage behaves as if the document has only one page.
624 */
625 bool HasPage(int pageNum);
626
627 /**
628 Returns @true if the printout is currently being used for previewing.
629 */
630 bool IsPreview();
631
632 /**
633 Set the user scale and device origin of the wxDC associated with this wxPrintout
634 so that one screen pixel maps to one device pixel on the DC. That is, the user
635 scale is set to (1,1) and the device origin is set to (0,0). Use this if you
636 want to do your own scaling prior to calling wxDC drawing calls, for example, if
637 your underlying model is floating-point and you want to achieve maximum drawing
638 precision on high-resolution printers. (Note that while the underlying drawing
639 model of Mac OS X is floating-point, wxWidgets's drawing model scales from
640 integer
641 coordinates.) You can use the GetLogicalXXXRect() routines below to obtain the
642 paper rectangle, page rectangle, or page margins rectangle to perform your own
643 scaling.
644 */
645 void MapScreenSizeToDevice();
646
647 /**
648 This sets the user scale of the wxDC assocated with this wxPrintout to the same
649 scale as MapScreenSizeToPaper() but sets
650 the logical origin to the top left corner of the page rectangle.
651 */
652 void MapScreenSizeToPage();
653
654 /**
655 This sets the user scale of the wxDC assocated with this wxPrintout to the same
656 scale as
657 MapScreenSizeToPageMargins() but
658 sets the logical origin to the top left corner of the page margins specified by
659 the given wxPageSetupDialogData object.
660 */
661 void MapScreenSizeToPageMargins(const wxPageSetupDialogData& pageSetupData);
662
663 /**
664 Set the user scale and device origin of the wxDC associated with this wxPrintout
665 so that the printed page matches the screen size as closely as possible
666 and the logical origin is in the top left corner of the paper rectangle.
667 That is,
668 a 100-pixel object on screen should appear at the same size on the printed
669 page. (It
670 will, of course, be larger or smaller in the preview image, depending on the
671 zoom
672 factor.) Use this if you want WYSIWYG behavior, e.g., in a text editor.
673 */
674 void MapScreenSizeToPaper();
675
676 /**
677 Shift the device origin by an amount specified in logical coordinates.
678 */
679 void OffsetLogicalOrigin(wxCoord xoff, wxCoord yoff);
680
681 /**
682 Called by the framework at the start of document printing. Return @false from
683 this function cancels the print job. OnBeginDocument is called once for every
684 copy printed.
685 The base OnBeginDocument() @e must be called (and the return value
686 checked) from within the overridden function, since it calls wxDC::StartDoc.
687 */
688 bool OnBeginDocument(int startPage, int endPage);
689
690 /**
691 Called by the framework at the start of printing. OnBeginPrinting is called
692 once for every
693 print job (regardless of how many copies are being printed).
694 */
695 void OnBeginPrinting();
696
697 /**
698 Called by the framework at the end of document printing. OnEndDocument
699 is called once for every copy printed.
700 The base OnEndDocument() @e must be called
701 from within the overridden function, since it calls wxDC::EndDoc.
702 */
703 void OnEndDocument();
704
705 /**
706 Called by the framework at the end of printing. OnEndPrinting
707 is called once for every print job (regardless of how many copies are being
708 printed).
709 */
710 void OnEndPrinting();
711
712 /**
713 Called once by the framework before any other demands are made of the
714 wxPrintout object. This gives the object an opportunity to calculate the
715 number of pages in the document, for example.
716 */
717 void OnPreparePrinting();
718
719 /**
720 Called by the framework when a page should be printed. Returning @false cancels
721 the print job. The application can use GetDC() to obtain a device
722 context to draw on.
723 */
724 bool OnPrintPage(int pageNum);
725
726 /**
727 Set the device origin of the associated wxDC so that the current logical point
728 becomes the new logical origin.
729 */
730 void SetLogicalOrigin(wxCoord x, wxCoord y);
731 };