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;
64 // Writes a header on a page. Margin units are in millimetres.
65 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
);
71 // The `main program' equivalent, creating the windows and returning the
73 bool MyApp::OnInit(void)
76 red_pen
= new wxPen("RED", 3, wxSOLID
);
78 // Create a small font
79 itemFont
= new wxFont(11, wxROMAN
, wxNORMAL
, wxNORMAL
);
80 labelFont
= new wxFont(12, wxROMAN
, wxITALIC
, wxBOLD
);
82 // Create the main frame window
83 frame
= new MyFrame(NULL
, "wxWindows Printing Demo", wxPoint(0, 0), wxSize(400, 400));
85 // Give it a status line
86 frame
->CreateStatusBar(2);
88 // Load icon and bitmap
90 frame
->SetIcon(wxIcon("aiai_icn"));
93 frame
->SetIcon(wxIcon(aiai_bits
, aiai_width
, aiai_height
));
97 wxMenu
*file_menu
= new wxMenu
;
99 file_menu
->Append(WXPRINT_PRINT
, "&Print...", "Print");
100 file_menu
->Append(WXPRINT_PRINT_SETUP
, "Print &Setup...", "Setup printer properties");
101 file_menu
->Append(WXPRINT_PAGE_SETUP
, "Page Set&up...", "Page setup");
102 file_menu
->Append(WXPRINT_PREVIEW
, "Print Pre&view", "Preview");
105 file_menu
->AppendSeparator();
106 file_menu
->Append(WXPRINT_PRINT_PS
, "Print PostScript...", "Print (PostScript)");
107 file_menu
->Append(WXPRINT_PRINT_SETUP_PS
, "Print Setup PostScript...", "Setup printer properties (PostScript)");
108 file_menu
->Append(WXPRINT_PAGE_SETUP_PS
, "Page Setup PostScript...", "Page setup (PostScript)");
109 file_menu
->Append(WXPRINT_PREVIEW_PS
, "Print Preview PostScript", "Preview (PostScript)");
111 file_menu
->AppendSeparator();
112 file_menu
->Append(WXPRINT_QUIT
, "E&xit", "Exit program");
114 wxMenu
*help_menu
= new wxMenu
;
115 help_menu
->Append(WXPRINT_ABOUT
, "&About", "About this demo");
117 wxMenuBar
*menu_bar
= new wxMenuBar
;
119 menu_bar
->Append(file_menu
, "&File");
120 menu_bar
->Append(help_menu
, "&Help");
122 // Associate the menu bar with the frame
123 frame
->SetMenuBar(menu_bar
);
125 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100), wxRETAINED
|wxHSCROLL
|wxVSCROLL
);
127 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
128 canvas
->SetScrollbars(20, 20, 50, 50);
130 // This ensures that the fonts get created as _screen_
131 // fonts, not printer fonts.
132 canvas
->SetFont(itemFont
);
133 canvas
->SetFont(labelFont
);
135 frame
->canvas
= canvas
;
137 frame
->Centre(wxBOTH
);
140 frame
->SetStatusText("Printing demo");
147 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
148 EVT_MENU(WXPRINT_QUIT
, MyFrame::OnExit
)
149 EVT_MENU(WXPRINT_PRINT
, MyFrame::OnPrint
)
150 EVT_MENU(WXPRINT_PREVIEW
, MyFrame::OnPrintPreview
)
151 EVT_MENU(WXPRINT_PRINT_SETUP
, MyFrame::OnPrintSetup
)
152 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
153 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame::OnPrintPS
)
154 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
155 EVT_MENU(WXPRINT_PRINT_SETUP_PS
, MyFrame::OnPrintSetupPS
)
156 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
157 EVT_MENU(WXPRINT_ABOUT
, MyFrame::OnPrintAbout
)
160 // Define my frame constructor
161 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
162 wxFrame(frame
, -1, title
, pos
, size
)
167 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
172 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
175 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
177 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
180 MyPrintout
printout("My printout");
181 if (!printer
.Print(this, &printout
, TRUE
))
182 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
);
185 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
187 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
189 wxPostScriptPrinter printer
;
190 MyPrintout
printout("My printout");
191 printer
.Print(this, &printout
, TRUE
);
194 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
197 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
199 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
201 wxPrintData printData
;
202 printData
.SetOrientation(orientation
);
204 // Pass two printout objects: for preview, and possible printing.
205 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
209 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK
);
213 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
214 frame
->Centre(wxBOTH
);
219 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
221 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
223 wxPrintData printData
;
224 printData
.SetOrientation(orientation
);
226 // Pass two printout objects: for preview, and possible printing.
227 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
228 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
229 frame
->Centre(wxBOTH
);
234 void MyFrame::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
237 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
239 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
242 data
.SetOrientation(orientation
);
245 wxPrintDialog
printerDialog(this, & data
);
247 wxGenericPrintDialog
printerDialog(this, & data
);
249 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
250 printerDialog
.ShowModal();
252 orientation
= printerDialog
.GetPrintData().GetOrientation();
255 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
258 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
260 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
262 wxPageSetupData data
;
263 data
.SetOrientation(orientation
);
266 wxPageSetupDialog
pageSetupDialog(this, & data
);
268 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
270 pageSetupDialog
.ShowModal();
272 data
= pageSetupDialog
.GetPageSetupData();
273 orientation
= data
.GetOrientation();
276 void MyFrame::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
278 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
281 data
.SetOrientation(orientation
);
283 wxGenericPrintDialog
printerDialog(this, & data
);
284 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
285 printerDialog
.ShowModal();
287 orientation
= printerDialog
.GetPrintData().GetOrientation();
290 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
292 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
294 wxPageSetupData data
;
295 data
.SetOrientation(orientation
);
297 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
298 pageSetupDialog
.ShowModal();
300 orientation
= pageSetupDialog
.GetPageSetupData().GetOrientation();
303 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
305 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
306 "About wxWindows printing demo", wxOK
|wxCENTRE
);
309 void MyFrame::Draw(wxDC
& dc
)
311 dc
.SetFont(itemFont
);
312 dc
.SetBackgroundMode(wxTRANSPARENT
);
314 dc
.SetBrush(wxCYAN_BRUSH
);
315 dc
.SetPen(wxRED_PEN
);
317 dc
.DrawRectangle(0, 30, 200, 100);
318 dc
.DrawText("Rectangle 200 by 100", 40, 40);
320 dc
.DrawEllipse(50, 140, 100, 50);
322 dc
.DrawText("Test message: this is in 11 point text", 10, 180);
324 dc
.SetPen(wxBLACK_PEN
);
325 dc
.DrawLine(0, 0, 200, 200);
326 dc
.DrawLine(200, 0, 0, 200);
328 wxIcon
my_icon( folder_xpm
);
329 dc
.DrawIcon( my_icon
, 100, 100, TRUE
);
332 void MyFrame::OnSize(wxSizeEvent
& event
)
334 wxFrame::OnSize(event
);
337 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
338 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
341 // Define a constructor for my canvas
342 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
343 wxScrolledWindow(frame
, -1, pos
, size
, style
)
347 MyCanvas::~MyCanvas(void)
351 // Define the repainting behaviour
352 void MyCanvas::OnDraw(wxDC
& dc
)
357 void MyCanvas::OnEvent(wxMouseEvent
& WXUNUSED(event
))
361 // Define the behaviour for the frame closing
362 // - must delete all frames except for the main one.
363 bool MyFrame::OnClose(void)
370 bool MyPrintout::OnPrintPage(int page
)
380 dc
->SetDeviceOrigin(0, 0);
383 sprintf(buf
, "PAGE %d", page
);
384 dc
->DrawText(buf
, 10, 10);
392 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
394 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
400 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
408 bool MyPrintout::HasPage(int pageNum
)
410 return (pageNum
== 1 || pageNum
== 2);
413 void MyPrintout::DrawPageOne(wxDC
*dc
)
415 /* You might use THIS code if you were scaling
416 * graphics of known size to fit on the page.
420 // We know the graphic is 200x200. If we didn't know this,
421 // we'd need to calculate it.
425 // Let's have at least 50 device units margin
429 // Add the margin to the graphic size
433 // Get the size of the DC in pixels
436 // Calculate a suitable scaling factor
437 float scaleX
=(float)(w
/maxX
);
438 float scaleY
=(float)(h
/maxY
);
440 // Use x or y scaling factor, whichever fits on the DC
441 float actualScale
= wxMin(scaleX
,scaleY
);
443 // Calculate the position on the DC for centring the graphic
444 float posX
= (float)((w
- (200*actualScale
))/2.0);
445 float posY
= (float)((h
- (200*actualScale
))/2.0);
447 // Set the scale and origin
448 dc
->SetUserScale(actualScale
, actualScale
);
449 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
454 void MyPrintout::DrawPageTwo(wxDC
*dc
)
456 /* You might use THIS code to set the printer DC to ROUGHLY reflect
457 * the screen text size. This page also draws lines of actual length 5cm
460 // Get the logical pixels per inch of screen and printer
461 int ppiScreenX
, ppiScreenY
;
462 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
463 int ppiPrinterX
, ppiPrinterY
;
464 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
466 // This scales the DC so that the printout roughly represents the
467 // the screen scaling. The text point size _should_ be the right size
468 // but in fact is too small for some reason. This is a detail that will
469 // need to be addressed at some point but can be fudged for the
471 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
473 // Now we have to check in case our real page size is reduced
474 // (e.g. because we're drawing to a print preview memory DC)
475 int pageWidth
, pageHeight
;
478 GetPageSizePixels(&pageWidth
, &pageHeight
);
480 // If printer pageWidth == current DC width, then this doesn't
481 // change. But w might be the preview bitmap width, so scale down.
482 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
483 dc
->SetUserScale(overallScale
, overallScale
);
485 // Calculate conversion factor for converting millimetres into
487 // There are approx. 25.1 mm to the inch. There are ppi
488 // device units to the inch. Therefore 1 mm corresponds to
489 // ppi/25.1 device units. We also divide by the
490 // screen-to-printer scaling factor, because we need to
491 // unscale to pass logical units to DrawLine.
493 // Draw 50 mm by 50 mm L shape
494 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.1));
495 float logUnits
= (float)(50*logUnitsFactor
);
496 dc
->SetPen(wxBLACK_PEN
);
497 dc
->DrawLine(50, 50, (long)(50.0 + logUnits
), 50);
498 dc
->DrawLine(50, 50, 50, (long)(50.0 + logUnits
));
500 dc
->SetFont(itemFont
);
501 dc
->SetBackgroundMode(wxTRANSPARENT
);
503 dc
->DrawText("Some test text", 200, 200 );
508 int rightMargin
= 20;
510 int bottomMargin
= 20;
512 int pageWidthMM
, pageHeightMM
;
513 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
515 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
516 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
517 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
518 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
520 dc
->SetPen(wxBLACK_PEN
);
521 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
522 (long)rightMarginLogical
, (long)topMarginLogical
);
523 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
524 (long)rightMarginLogical
, (long)bottomMarginLogical
);
526 WritePageHeader(this, dc
, "A header", logUnitsFactor
);
529 // Writes a header on a page. Margin units are in millimetres.
530 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
)
532 static wxFont
*headerFont
= NULL
;
535 headerFont
= wxTheFontList
->FindOrCreateFont(16, wxSWISS
, wxNORMAL
, wxBOLD
);
537 dc
->SetFont(headerFont
);
539 int pageWidthMM
, pageHeightMM
;
541 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
545 int rightMargin
= 10;
547 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
548 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
549 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
551 long xExtent
, yExtent
;
552 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
553 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
554 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
556 dc
->SetPen(wxBLACK_PEN
);
557 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
558 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);