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("mondrian"));
88 // frame->SetIcon(wxIcon(mondrian_bits, mondrian_width, mondrian_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
& WXUNUSED(event
))
167 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(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
& WXUNUSED(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
& WXUNUSED(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
& WXUNUSED(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
& WXUNUSED(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
& WXUNUSED(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
& WXUNUSED(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
& WXUNUSED(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
& WXUNUSED(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, 30, 200, 100);
313 dc
.DrawText("Rectangle 200 by 100", 40, 40);
315 dc
.DrawEllipse(50, 140, 100, 50);
317 dc
.DrawText("Test message: this is in 11 point text", 10, 180);
319 dc
.SetPen(wxBLACK_PEN
);
320 dc
.DrawLine(0, 0, 200, 200);
321 dc
.DrawLine(200, 0, 0, 200);
323 #if defined(__WXGTK__)
324 wxIcon
my_icon( folder_xpm
);
325 #elif defined(__WXMSW__)
326 wxIcon
my_icon( "mondrian" );
328 #error "Platform not supported."
331 dc
.DrawIcon( my_icon
, 100, 100);
334 void MyFrame::OnSize(wxSizeEvent
& event
)
336 wxFrame::OnSize(event
);
339 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
340 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
343 // Define a constructor for my canvas
344 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
345 wxScrolledWindow(frame
, -1, pos
, size
, style
)
349 MyCanvas::~MyCanvas(void)
353 // Define the repainting behaviour
354 void MyCanvas::OnDraw(wxDC
& dc
)
359 void MyCanvas::OnEvent(wxMouseEvent
& WXUNUSED(event
))
363 // Define the behaviour for the frame closing
364 // - must delete all frames except for the main one.
365 bool MyFrame::OnClose(void)
372 bool MyPrintout::OnPrintPage(int page
)
382 dc
->SetDeviceOrigin(0, 0);
385 sprintf(buf
, "PAGE %d", page
);
386 dc
->DrawText(buf
, 10, 10);
394 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
396 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
402 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
410 bool MyPrintout::HasPage(int pageNum
)
412 return (pageNum
== 1 || pageNum
== 2);
415 void MyPrintout::DrawPageOne(wxDC
*dc
)
417 /* You might use THIS code if you were scaling
418 * graphics of known size to fit on the page.
422 // We know the graphic is 200x200. If we didn't know this,
423 // we'd need to calculate it.
427 // Let's have at least 50 device units margin
431 // Add the margin to the graphic size
435 // Get the size of the DC in pixels
438 // Calculate a suitable scaling factor
439 float scaleX
=(float)(w
/maxX
);
440 float scaleY
=(float)(h
/maxY
);
442 // Use x or y scaling factor, whichever fits on the DC
443 float actualScale
= wxMin(scaleX
,scaleY
);
445 // Calculate the position on the DC for centring the graphic
446 float posX
= (float)((w
- (200*actualScale
))/2.0);
447 float posY
= (float)((h
- (200*actualScale
))/2.0);
449 // Set the scale and origin
450 dc
->SetUserScale(actualScale
, actualScale
);
451 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
456 void MyPrintout::DrawPageTwo(wxDC
*dc
)
458 /* You might use THIS code to set the printer DC to ROUGHLY reflect
459 * the screen text size. This page also draws lines of actual length 5cm
462 // Get the logical pixels per inch of screen and printer
463 int ppiScreenX
, ppiScreenY
;
464 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
465 int ppiPrinterX
, ppiPrinterY
;
466 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
468 // This scales the DC so that the printout roughly represents the
469 // the screen scaling. The text point size _should_ be the right size
470 // but in fact is too small for some reason. This is a detail that will
471 // need to be addressed at some point but can be fudged for the
473 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
475 // Now we have to check in case our real page size is reduced
476 // (e.g. because we're drawing to a print preview memory DC)
477 int pageWidth
, pageHeight
;
480 GetPageSizePixels(&pageWidth
, &pageHeight
);
482 // If printer pageWidth == current DC width, then this doesn't
483 // change. But w might be the preview bitmap width, so scale down.
484 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
485 dc
->SetUserScale(overallScale
, overallScale
);
487 // Calculate conversion factor for converting millimetres into
489 // There are approx. 25.1 mm to the inch. There are ppi
490 // device units to the inch. Therefore 1 mm corresponds to
491 // ppi/25.1 device units. We also divide by the
492 // screen-to-printer scaling factor, because we need to
493 // unscale to pass logical units to DrawLine.
495 // Draw 50 mm by 50 mm L shape
496 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.1));
497 float logUnits
= (float)(50*logUnitsFactor
);
498 dc
->SetPen(wxBLACK_PEN
);
499 dc
->DrawLine(50, 50, (long)(50.0 + logUnits
), 50);
500 dc
->DrawLine(50, 50, 50, (long)(50.0 + logUnits
));
502 dc
->SetFont(itemFont
);
503 dc
->SetBackgroundMode(wxTRANSPARENT
);
505 dc
->DrawText("Some test text", 200, 200 );
510 int rightMargin
= 20;
512 int bottomMargin
= 20;
514 int pageWidthMM
, pageHeightMM
;
515 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
517 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
518 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
519 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
520 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
522 dc
->SetPen(wxBLACK_PEN
);
523 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
524 (long)rightMarginLogical
, (long)topMarginLogical
);
525 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
526 (long)rightMarginLogical
, (long)bottomMarginLogical
);
528 WritePageHeader(this, dc
, "A header", logUnitsFactor
);
531 // Writes a header on a page. Margin units are in millimetres.
532 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
)
534 static wxFont
*headerFont
= NULL
;
537 headerFont
= wxTheFontList
->FindOrCreateFont(16, wxSWISS
, wxNORMAL
, wxBOLD
);
539 dc
->SetFont(headerFont
);
541 int pageWidthMM
, pageHeightMM
;
543 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
547 int rightMargin
= 10;
549 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
550 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
551 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
553 long xExtent
, yExtent
;
554 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
555 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
556 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
558 dc
->SetPen(wxBLACK_PEN
);
559 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
560 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);