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"
27 #if !wxUSE_PRINTING_ARCHITECTURE
28 #error You must set wxUSE_PRINTING_ARCHITECTURE to 1 in setup.h to compile this demo.
31 // Set this to 1 if you want to test PostScript printing under MSW.
32 // However, you'll also need to edit src/msw/makefile.nt.
33 #define wxTEST_POSTSCRIPT_IN_MSW 0
36 #include "wx/metafile.h"
38 #include "wx/printdlg.h"
42 #if wxTEST_POSTSCRIPT_IN_MSW
43 #include "wx/generic/printps.h"
44 #include "wx/generic/prntdlgg.h"
50 #include "mondrian.xpm"
54 MyFrame
*frame
= (MyFrame
*) NULL
;
55 int orientation
= wxPORTRAIT
;
60 // Writes a header on a page. Margin units are in millimetres.
61 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
);
67 // The `main program' equivalent, creating the windows and returning the
69 bool MyApp::OnInit(void)
71 m_testFont
= new wxFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
73 // Create the main frame window
74 frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows Printing Demo", wxPoint(0, 0), wxSize(400, 400));
76 // Give it a status line
77 frame
->CreateStatusBar(2);
79 // Load icon and bitmap
80 frame
->SetIcon( wxICON( mondrian
) );
83 wxMenu
*file_menu
= new wxMenu
;
85 file_menu
->Append(WXPRINT_PRINT
, "&Print...", "Print");
86 file_menu
->Append(WXPRINT_PRINT_SETUP
, "Print &Setup...", "Setup printer properties");
87 file_menu
->Append(WXPRINT_PAGE_SETUP
, "Page Set&up...", "Page setup");
88 file_menu
->Append(WXPRINT_PREVIEW
, "Print Pre&view", "Preview");
91 wxAcceleratorEntry entries
[1];
92 entries
[0].Set(wxACCEL_CTRL
, (int) 'V', WXPRINT_PREVIEW
);
93 wxAcceleratorTable
accel(1, entries
);
94 frame
->SetAcceleratorTable(accel
);
96 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
97 file_menu
->AppendSeparator();
98 file_menu
->Append(WXPRINT_PRINT_PS
, "Print PostScript...", "Print (PostScript)");
99 file_menu
->Append(WXPRINT_PRINT_SETUP_PS
, "Print Setup PostScript...", "Setup printer properties (PostScript)");
100 file_menu
->Append(WXPRINT_PAGE_SETUP_PS
, "Page Setup PostScript...", "Page setup (PostScript)");
101 file_menu
->Append(WXPRINT_PREVIEW_PS
, "Print Preview PostScript", "Preview (PostScript)");
103 file_menu
->AppendSeparator();
104 file_menu
->Append(WXPRINT_QUIT
, "E&xit", "Exit program");
106 wxMenu
*help_menu
= new wxMenu
;
107 help_menu
->Append(WXPRINT_ABOUT
, "&About", "About this demo");
109 wxMenuBar
*menu_bar
= new wxMenuBar
;
111 menu_bar
->Append(file_menu
, "&File");
112 menu_bar
->Append(help_menu
, "&Help");
114 // Associate the menu bar with the frame
115 frame
->SetMenuBar(menu_bar
);
117 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100), wxRETAINED
|wxHSCROLL
|wxVSCROLL
);
119 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
120 canvas
->SetScrollbars(20, 20, 50, 50);
122 frame
->canvas
= canvas
;
124 frame
->Centre(wxBOTH
);
127 frame
->SetStatusText("Printing demo");
136 delete wxGetApp().m_testFont
;
140 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
141 EVT_MENU(WXPRINT_QUIT
, MyFrame::OnExit
)
142 EVT_MENU(WXPRINT_PRINT
, MyFrame::OnPrint
)
143 EVT_MENU(WXPRINT_PREVIEW
, MyFrame::OnPrintPreview
)
144 EVT_MENU(WXPRINT_PRINT_SETUP
, MyFrame::OnPrintSetup
)
145 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
146 EVT_MENU(WXPRINT_ABOUT
, MyFrame::OnPrintAbout
)
147 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
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
)
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
)
159 canvas
= (MyCanvas
*) NULL
;
162 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
167 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
170 MyPrintout
printout("My printout");
171 if (!printer
.Print(this, &printout
, TRUE
))
172 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
);
175 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
177 wxPrintData printData
;
178 printData
.SetOrientation(orientation
);
180 // Pass two printout objects: for preview, and possible printing.
181 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
185 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK
);
189 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
190 frame
->Centre(wxBOTH
);
195 void MyFrame::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
198 data
.SetOrientation(orientation
);
200 wxPrintDialog
printerDialog(this, & data
);
202 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
203 printerDialog
.ShowModal();
205 orientation
= printerDialog
.GetPrintData().GetOrientation();
208 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
210 wxPageSetupData data
;
211 data
.SetOrientation(orientation
);
213 wxPageSetupDialog
pageSetupDialog(this, & data
);
214 pageSetupDialog
.ShowModal();
216 data
= pageSetupDialog
.GetPageSetupData();
217 orientation
= data
.GetOrientation();
220 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
221 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
223 wxPostScriptPrinter printer
;
224 MyPrintout
printout("My printout");
225 printer
.Print(this, &printout
, TRUE
);
228 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
230 wxPrintData printData
;
231 printData
.SetOrientation(orientation
);
233 // Pass two printout objects: for preview, and possible printing.
234 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
235 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
236 frame
->Centre(wxBOTH
);
241 void MyFrame::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
244 data
.SetOrientation(orientation
);
246 wxGenericPrintDialog
printerDialog(this, & data
);
247 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
248 printerDialog
.ShowModal();
250 orientation
= printerDialog
.GetPrintData().GetOrientation();
253 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
255 wxPageSetupData data
;
256 data
.SetOrientation(orientation
);
258 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
259 pageSetupDialog
.ShowModal();
261 orientation
= pageSetupDialog
.GetPageSetupData().GetOrientation();
266 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
268 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
269 "About wxWindows printing demo", wxOK
|wxCENTRE
);
272 void MyFrame::Draw(wxDC
& dc
)
274 dc
.SetFont(* wxGetApp().m_testFont
);
276 dc
.SetBackgroundMode(wxTRANSPARENT
);
278 dc
.SetBrush(* wxCYAN_BRUSH
);
279 dc
.SetPen(* wxRED_PEN
);
281 dc
.DrawRectangle(0, 30, 200, 100);
282 dc
.DrawText("Rectangle 200 by 100", 40, 40);
284 dc
.DrawEllipse(50, 140, 100, 50);
286 dc
.DrawText("Test message: this is in 10 point text", 10, 180);
288 dc
.SetPen(* wxBLACK_PEN
);
289 dc
.DrawLine(0, 0, 200, 200);
290 dc
.DrawLine(200, 0, 0, 200);
292 wxIcon my_icon
= wxICON(mondrian
) ;
294 dc
.DrawIcon( my_icon
, 100, 100);
297 void MyFrame::OnSize(wxSizeEvent
& event
)
299 wxFrame::OnSize(event
);
302 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
303 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
306 // Define a constructor for my canvas
307 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
308 wxScrolledWindow(frame
, -1, pos
, size
, style
)
312 MyCanvas::~MyCanvas(void)
316 // Define the repainting behaviour
317 void MyCanvas::OnDraw(wxDC
& dc
)
322 void MyCanvas::OnEvent(wxMouseEvent
& WXUNUSED(event
))
326 bool MyPrintout::OnPrintPage(int page
)
336 dc
->SetDeviceOrigin(0, 0);
337 dc
->SetUserScale(1.0, 1.0);
340 sprintf(buf
, "PAGE %d", page
);
341 dc
->DrawText(buf
, 10, 10);
349 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
351 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
357 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
365 bool MyPrintout::HasPage(int pageNum
)
367 return (pageNum
== 1 || pageNum
== 2);
370 void MyPrintout::DrawPageOne(wxDC
*dc
)
372 /* You might use THIS code if you were scaling
373 * graphics of known size to fit on the page.
377 // We know the graphic is 200x200. If we didn't know this,
378 // we'd need to calculate it.
382 // Let's have at least 50 device units margin
386 // Add the margin to the graphic size
390 // Get the size of the DC in pixels
393 // Calculate a suitable scaling factor
394 float scaleX
=(float)(w
/maxX
);
395 float scaleY
=(float)(h
/maxY
);
397 // Use x or y scaling factor, whichever fits on the DC
398 float actualScale
= wxMin(scaleX
,scaleY
);
400 // Calculate the position on the DC for centring the graphic
401 float posX
= (float)((w
- (200*actualScale
))/2.0);
402 float posY
= (float)((h
- (200*actualScale
))/2.0);
404 // Set the scale and origin
405 dc
->SetUserScale(actualScale
, actualScale
);
406 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
411 void MyPrintout::DrawPageTwo(wxDC
*dc
)
413 /* You might use THIS code to set the printer DC to ROUGHLY reflect
414 * the screen text size. This page also draws lines of actual length 5cm
417 // Get the logical pixels per inch of screen and printer
418 int ppiScreenX
, ppiScreenY
;
419 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
420 int ppiPrinterX
, ppiPrinterY
;
421 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
423 // This scales the DC so that the printout roughly represents the
424 // the screen scaling. The text point size _should_ be the right size
425 // but in fact is too small for some reason. This is a detail that will
426 // need to be addressed at some point but can be fudged for the
428 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
430 // Now we have to check in case our real page size is reduced
431 // (e.g. because we're drawing to a print preview memory DC)
432 int pageWidth
, pageHeight
;
435 GetPageSizePixels(&pageWidth
, &pageHeight
);
437 // If printer pageWidth == current DC width, then this doesn't
438 // change. But w might be the preview bitmap width, so scale down.
439 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
440 dc
->SetUserScale(overallScale
, overallScale
);
442 // Calculate conversion factor for converting millimetres into
444 // There are approx. 25.1 mm to the inch. There are ppi
445 // device units to the inch. Therefore 1 mm corresponds to
446 // ppi/25.1 device units. We also divide by the
447 // screen-to-printer scaling factor, because we need to
448 // unscale to pass logical units to DrawLine.
450 // Draw 50 mm by 50 mm L shape
451 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.1));
452 float logUnits
= (float)(50*logUnitsFactor
);
453 dc
->SetPen(* wxBLACK_PEN
);
454 dc
->DrawLine(50, 250, (long)(50.0 + logUnits
), 250);
455 dc
->DrawLine(50, 250, 50, (long)(250.0 + logUnits
));
457 dc
->SetFont(* wxGetApp().m_testFont
);
458 dc
->SetBackgroundMode(wxTRANSPARENT
);
460 dc
->DrawText("Some test text", 200, 200 );
465 int rightMargin
= 20;
467 int bottomMargin
= 20;
469 int pageWidthMM
, pageHeightMM
;
470 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
472 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
473 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
474 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
475 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
477 dc
->SetPen(* wxRED_PEN
);
478 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
479 (long)rightMarginLogical
, (long)topMarginLogical
);
480 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
481 (long)rightMarginLogical
, (long)bottomMarginLogical
);
483 WritePageHeader(this, dc
, "A header", logUnitsFactor
);
486 // Writes a header on a page. Margin units are in millimetres.
487 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
)
490 static wxFont *headerFont = (wxFont *) NULL;
493 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
495 dc->SetFont(headerFont);
498 int pageWidthMM
, pageHeightMM
;
500 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
504 int rightMargin
= 10;
506 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
507 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
508 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
510 long xExtent
, yExtent
;
511 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
512 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
513 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
515 dc
->SetPen(* wxBLACK_PEN
);
516 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
517 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);