3 * Purpose: Printing demo for wxWindows class library
7 * Copyright: (c) 1995, AIAI, University of Edinburgh
10 /* static const char sccsid[] = "%W% %G%"; */
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/postscrp.h"
28 #if !USE_PRINTING_ARCHITECTURE
29 #error You must set USE_PRINTING_ARCHITECTURE to 1 in wx_setup.h to compile this demo.
33 #include "wx/metafile.h"
35 #include "wx/printdlg.h"
36 #include "wx/generic/printps.h"
37 #include "wx/generic/prntdlgg.h"
42 MyFrame
*frame
= NULL
;
43 int orientation
= wxPORTRAIT
;
48 // Must initialise these in OnInit, not statically
53 float zoom_factor
= 1.0;
59 // Writes a header on a page. Margin units are in millimetres.
60 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
);
66 // The `main program' equivalent, creating the windows and returning the
68 bool MyApp::OnInit(void)
71 red_pen
= new wxPen("RED", 3, wxSOLID
);
73 // Create a small font
74 itemFont
= new wxFont(11, wxROMAN
, wxNORMAL
, wxNORMAL
);
75 labelFont
= new wxFont(12, wxROMAN
, wxITALIC
, wxBOLD
);
77 // Create the main frame window
78 frame
= new MyFrame(NULL
, "wxWindows Printing Demo", wxPoint(0, 0), wxSize(400, 400));
80 // Give it a status line
81 frame
->CreateStatusBar(2);
83 // Load icon and bitmap
85 frame
->SetIcon(wxIcon("aiai_icn"));
88 frame
->SetIcon(wxIcon(aiai_bits
, aiai_width
, aiai_height
));
92 wxMenu
*file_menu
= new wxMenu
;
94 file_menu
->Append(WXPRINT_PRINT
, "&Print...", "Print");
95 file_menu
->Append(WXPRINT_PRINT_SETUP
, "Print &Setup...", "Setup printer properties");
96 file_menu
->Append(WXPRINT_PAGE_SETUP
, "Page Set&up...", "Page setup");
97 file_menu
->Append(WXPRINT_PREVIEW
, "Print Pre&view", "Preview");
100 file_menu
->AppendSeparator();
101 file_menu
->Append(WXPRINT_PRINT_PS
, "Print PostScript...", "Print (PostScript)");
102 file_menu
->Append(WXPRINT_PRINT_SETUP_PS
, "Print Setup PostScript...", "Setup printer properties (PostScript)");
103 file_menu
->Append(WXPRINT_PAGE_SETUP_PS
, "Page Setup PostScript...", "Page setup (PostScript)");
104 file_menu
->Append(WXPRINT_PREVIEW_PS
, "Print Preview PostScript", "Preview (PostScript)");
106 file_menu
->AppendSeparator();
107 file_menu
->Append(WXPRINT_QUIT
, "E&xit", "Exit program");
109 wxMenu
*help_menu
= new wxMenu
;
110 help_menu
->Append(WXPRINT_ABOUT
, "&About", "About this demo");
112 wxMenuBar
*menu_bar
= new wxMenuBar
;
114 menu_bar
->Append(file_menu
, "&File");
115 menu_bar
->Append(help_menu
, "&Help");
117 // Associate the menu bar with the frame
118 frame
->SetMenuBar(menu_bar
);
120 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100), wxRETAINED
|wxHSCROLL
|wxVSCROLL
);
122 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
123 canvas
->SetScrollbars(20, 20, 50, 50);
125 // This ensures that the fonts get created as _screen_
126 // fonts, not printer fonts.
127 canvas
->SetFont(itemFont
);
128 canvas
->SetFont(labelFont
);
130 frame
->canvas
= canvas
;
132 frame
->Centre(wxBOTH
);
135 frame
->SetStatusText("Printing demo");
142 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
143 EVT_MENU(WXPRINT_QUIT
, MyFrame::OnExit
)
144 EVT_MENU(WXPRINT_PRINT
, MyFrame::OnPrint
)
145 EVT_MENU(WXPRINT_PREVIEW
, MyFrame::OnPrintPreview
)
146 EVT_MENU(WXPRINT_PRINT_SETUP
, MyFrame::OnPrintSetup
)
147 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
148 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame::OnPrintPS
)
149 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
150 EVT_MENU(WXPRINT_PRINT_SETUP_PS
, MyFrame::OnPrintSetupPS
)
151 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
152 EVT_MENU(WXPRINT_ABOUT
, MyFrame::OnPrintAbout
)
155 // Define my frame constructor
156 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
157 wxFrame(frame
, -1, title
, pos
, size
)
162 void MyFrame::OnExit(wxCommandEvent
& event
)
167 void MyFrame::OnPrint(wxCommandEvent
& event
)
170 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
172 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
175 MyPrintout
printout("My printout");
176 if (!printer
.Print(this, &printout
, TRUE
))
177 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
);
180 void MyFrame::OnPrintPS(wxCommandEvent
& event
)
182 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
184 wxPostScriptPrinter printer
;
185 MyPrintout
printout("My printout");
186 printer
.Print(this, &printout
, TRUE
);
189 void MyFrame::OnPrintPreview(wxCommandEvent
& event
)
192 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
194 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
196 wxPrintData printData
;
197 printData
.SetOrientation(orientation
);
199 // Pass two printout objects: for preview, and possible printing.
200 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
204 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK
);
208 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
209 frame
->Centre(wxBOTH
);
214 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& event
)
216 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
218 wxPrintData printData
;
219 printData
.SetOrientation(orientation
);
221 // Pass two printout objects: for preview, and possible printing.
222 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
223 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
224 frame
->Centre(wxBOTH
);
229 void MyFrame::OnPrintSetup(wxCommandEvent
& event
)
232 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
234 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
237 data
.SetOrientation(orientation
);
240 wxPrintDialog
printerDialog(this, & data
);
242 wxGenericPrintDialog
printerDialog(this, & data
);
244 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
245 printerDialog
.ShowModal();
247 orientation
= printerDialog
.GetPrintData().GetOrientation();
250 void MyFrame::OnPageSetup(wxCommandEvent
& event
)
253 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
255 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
257 wxPageSetupData data
;
258 data
.SetOrientation(orientation
);
261 wxPageSetupDialog
pageSetupDialog(this, & data
);
263 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
265 pageSetupDialog
.ShowModal();
267 data
= pageSetupDialog
.GetPageSetupData();
268 orientation
= data
.GetOrientation();
271 void MyFrame::OnPrintSetupPS(wxCommandEvent
& event
)
273 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
276 data
.SetOrientation(orientation
);
278 wxGenericPrintDialog
printerDialog(this, & data
);
279 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
280 printerDialog
.ShowModal();
282 orientation
= printerDialog
.GetPrintData().GetOrientation();
285 void MyFrame::OnPageSetupPS(wxCommandEvent
& event
)
287 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
289 wxPageSetupData data
;
290 data
.SetOrientation(orientation
);
292 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
293 pageSetupDialog
.ShowModal();
295 orientation
= pageSetupDialog
.GetPageSetupData().GetOrientation();
298 void MyFrame::OnPrintAbout(wxCommandEvent
& event
)
300 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
301 "About wxWindows printing demo", wxOK
|wxCENTRE
);
304 void MyFrame::Draw(wxDC
& dc
)
306 dc
.SetFont(itemFont
);
307 dc
.SetBackgroundMode(wxTRANSPARENT
);
309 dc
.SetBrush(wxCYAN_BRUSH
);
310 dc
.SetPen(wxRED_PEN
);
312 dc
.DrawRectangle(0.0, 30.0, 200.0, 100.0);
313 dc
.DrawText("Rectangle 200 by 100", 40.0, 40.0);
315 dc
.DrawEllipse(50.0, 140.0, 100.0, 50.0);
317 dc
.DrawText("Test message: this is in 11 point text", 10.0, 180.0);
319 dc
.SetPen(wxBLACK_PEN
);
320 dc
.DrawLine(0.0, 0.0, 200.0, 200.0);
321 dc
.DrawLine(200.0, 0.0, 0.0, 200.0);
324 void MyFrame::OnSize(wxSizeEvent
& event
)
326 wxFrame::OnSize(event
);
329 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
330 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
333 // Define a constructor for my canvas
334 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
335 wxScrolledWindow(frame
, -1, pos
, size
, style
)
339 MyCanvas::~MyCanvas(void)
343 // Define the repainting behaviour
344 void MyCanvas::OnDraw(wxDC
& dc
)
349 void MyCanvas::OnEvent(wxMouseEvent
& event
)
353 // Define the behaviour for the frame closing
354 // - must delete all frames except for the main one.
355 bool MyFrame::OnClose(void)
362 bool MyPrintout::OnPrintPage(int page
)
372 dc
->SetDeviceOrigin(0, 0);
375 sprintf(buf
, "PAGE %d", page
);
376 dc
->DrawText(buf
, 10.0, 10.0);
384 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
386 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
392 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
400 bool MyPrintout::HasPage(int pageNum
)
402 return (pageNum
== 1 || pageNum
== 2);
405 void MyPrintout::DrawPageOne(wxDC
*dc
)
407 /* You might use THIS code if you were scaling
408 * graphics of known size to fit on the page.
412 // We know the graphic is 200x200. If we didn't know this,
413 // we'd need to calculate it.
417 // Let's have at least 50 device units margin
421 // Add the margin to the graphic size
425 // Get the size of the DC in pixels
428 // Calculate a suitable scaling factor
429 float scaleX
=(float)(w
/maxX
);
430 float scaleY
=(float)(h
/maxY
);
432 // Use x or y scaling factor, whichever fits on the DC
433 float actualScale
= wxMin(scaleX
,scaleY
);
435 // Calculate the position on the DC for centring the graphic
436 float posX
= (float)((w
- (200*actualScale
))/2.0);
437 float posY
= (float)((h
- (200*actualScale
))/2.0);
439 // Set the scale and origin
440 dc
->SetUserScale(actualScale
, actualScale
);
441 dc
->SetDeviceOrigin(posX
, posY
);
446 void MyPrintout::DrawPageTwo(wxDC
*dc
)
448 /* You might use THIS code to set the printer DC to ROUGHLY reflect
449 * the screen text size. This page also draws lines of actual length 5cm
452 // Get the logical pixels per inch of screen and printer
453 int ppiScreenX
, ppiScreenY
;
454 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
455 int ppiPrinterX
, ppiPrinterY
;
456 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
458 // This scales the DC so that the printout roughly represents the
459 // the screen scaling. The text point size _should_ be the right size
460 // but in fact is too small for some reason. This is a detail that will
461 // need to be addressed at some point but can be fudged for the
463 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
465 // Now we have to check in case our real page size is reduced
466 // (e.g. because we're drawing to a print preview memory DC)
467 int pageWidth
, pageHeight
;
470 GetPageSizePixels(&pageWidth
, &pageHeight
);
472 // If printer pageWidth == current DC width, then this doesn't
473 // change. But w might be the preview bitmap width, so scale down.
474 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
475 dc
->SetUserScale(overallScale
, overallScale
);
477 // Calculate conversion factor for converting millimetres into
479 // There are approx. 25.1 mm to the inch. There are ppi
480 // device units to the inch. Therefore 1 mm corresponds to
481 // ppi/25.1 device units. We also divide by the
482 // screen-to-printer scaling factor, because we need to
483 // unscale to pass logical units to DrawLine.
485 // Draw 50 mm by 50 mm L shape
486 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.1));
487 float logUnits
= (float)(50*logUnitsFactor
);
488 dc
->SetPen(wxBLACK_PEN
);
489 dc
->DrawLine(50.0, 50.0, (float)(50.0 + logUnits
), 50.0);
490 dc
->DrawLine(50.0, 50.0, 50.0, (float)(50.0 + logUnits
));
492 dc
->SetFont(itemFont
);
493 dc
->SetBackgroundMode(wxTRANSPARENT
);
495 dc
->DrawText("Some test text", 200.0, 200.0);
500 int rightMargin
= 20;
502 int bottomMargin
= 20;
504 int pageWidthMM
, pageHeightMM
;
505 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
507 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
508 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
509 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
510 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
512 dc
->SetPen(wxBLACK_PEN
);
513 dc
->DrawLine(leftMarginLogical
, topMarginLogical
, rightMarginLogical
, topMarginLogical
);
514 dc
->DrawLine(leftMarginLogical
, bottomMarginLogical
, rightMarginLogical
, bottomMarginLogical
);
516 WritePageHeader(this, dc
, "A header", logUnitsFactor
);
519 // Writes a header on a page. Margin units are in millimetres.
520 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
)
522 static wxFont
*headerFont
= NULL
;
525 headerFont
= wxTheFontList
->FindOrCreateFont(16, wxSWISS
, wxNORMAL
, wxBOLD
);
527 dc
->SetFont(headerFont
);
529 int pageWidthMM
, pageHeightMM
;
531 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
535 int rightMargin
= 10;
537 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
538 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
539 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
541 long xExtent
, yExtent
;
542 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
543 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
544 dc
->DrawText(text
, (long)xPos
, topMarginLogical
);
546 dc
->SetPen(wxBLACK_PEN
);
547 dc
->DrawLine(leftMarginLogical
, topMarginLogical
+yExtent
, rightMarginLogical
, topMarginLogical
+yExtent
);