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 wx_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");
134 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
135 EVT_MENU(WXPRINT_QUIT
, MyFrame::OnExit
)
136 EVT_MENU(WXPRINT_PRINT
, MyFrame::OnPrint
)
137 EVT_MENU(WXPRINT_PREVIEW
, MyFrame::OnPrintPreview
)
138 EVT_MENU(WXPRINT_PRINT_SETUP
, MyFrame::OnPrintSetup
)
139 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
140 EVT_MENU(WXPRINT_ABOUT
, MyFrame::OnPrintAbout
)
141 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
142 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame::OnPrintPS
)
143 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
144 EVT_MENU(WXPRINT_PRINT_SETUP_PS
, MyFrame::OnPrintSetupPS
)
145 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
149 // Define my frame constructor
150 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
151 wxFrame(frame
, -1, title
, pos
, size
)
153 canvas
= (MyCanvas
*) NULL
;
156 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
161 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
164 MyPrintout
printout("My printout");
165 if (!printer
.Print(this, &printout
, TRUE
))
166 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
);
169 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
171 wxPrintData printData
;
172 printData
.SetOrientation(orientation
);
174 // Pass two printout objects: for preview, and possible printing.
175 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
179 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK
);
183 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
184 frame
->Centre(wxBOTH
);
189 void MyFrame::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
192 data
.SetOrientation(orientation
);
194 wxPrintDialog
printerDialog(this, & data
);
196 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
197 printerDialog
.ShowModal();
199 orientation
= printerDialog
.GetPrintData().GetOrientation();
202 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
204 wxPageSetupData data
;
205 data
.SetOrientation(orientation
);
207 wxPageSetupDialog
pageSetupDialog(this, & data
);
208 pageSetupDialog
.ShowModal();
210 data
= pageSetupDialog
.GetPageSetupData();
211 orientation
= data
.GetOrientation();
214 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
215 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
217 wxPostScriptPrinter printer
;
218 MyPrintout
printout("My printout");
219 printer
.Print(this, &printout
, TRUE
);
222 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
224 wxPrintData printData
;
225 printData
.SetOrientation(orientation
);
227 // Pass two printout objects: for preview, and possible printing.
228 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
229 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
230 frame
->Centre(wxBOTH
);
235 void MyFrame::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
238 data
.SetOrientation(orientation
);
240 wxGenericPrintDialog
printerDialog(this, & data
);
241 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
242 printerDialog
.ShowModal();
244 orientation
= printerDialog
.GetPrintData().GetOrientation();
247 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
249 wxPageSetupData data
;
250 data
.SetOrientation(orientation
);
252 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
253 pageSetupDialog
.ShowModal();
255 orientation
= pageSetupDialog
.GetPageSetupData().GetOrientation();
260 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
262 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
263 "About wxWindows printing demo", wxOK
|wxCENTRE
);
266 void MyFrame::Draw(wxDC
& dc
)
268 dc
.SetFont(* wxGetApp().m_testFont
);
270 dc
.SetBackgroundMode(wxTRANSPARENT
);
272 dc
.SetBrush(* wxCYAN_BRUSH
);
273 dc
.SetPen(* wxRED_PEN
);
275 dc
.DrawRectangle(0, 30, 200, 100);
276 dc
.DrawText("Rectangle 200 by 100", 40, 40);
278 dc
.DrawEllipse(50, 140, 100, 50);
280 dc
.DrawText("Test message: this is in 10 point text", 10, 180);
282 dc
.SetPen(* wxBLACK_PEN
);
283 dc
.DrawLine(0, 0, 200, 200);
284 dc
.DrawLine(200, 0, 0, 200);
286 wxIcon my_icon
= wxICON(mondrian
) ;
288 dc
.DrawIcon( my_icon
, 100, 100);
291 void MyFrame::OnSize(wxSizeEvent
& event
)
293 wxFrame::OnSize(event
);
296 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
297 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
300 // Define a constructor for my canvas
301 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
302 wxScrolledWindow(frame
, -1, pos
, size
, style
)
306 MyCanvas::~MyCanvas(void)
310 // Define the repainting behaviour
311 void MyCanvas::OnDraw(wxDC
& dc
)
316 void MyCanvas::OnEvent(wxMouseEvent
& WXUNUSED(event
))
320 bool MyFrame::OnClose(void)
323 delete wxGetApp().m_testFont
;
328 bool MyPrintout::OnPrintPage(int page
)
338 dc
->SetDeviceOrigin(0, 0);
339 dc
->SetUserScale(1.0, 1.0);
342 sprintf(buf
, "PAGE %d", page
);
343 dc
->DrawText(buf
, 10, 10);
351 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
353 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
359 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
367 bool MyPrintout::HasPage(int pageNum
)
369 return (pageNum
== 1 || pageNum
== 2);
372 void MyPrintout::DrawPageOne(wxDC
*dc
)
374 /* You might use THIS code if you were scaling
375 * graphics of known size to fit on the page.
379 // We know the graphic is 200x200. If we didn't know this,
380 // we'd need to calculate it.
384 // Let's have at least 50 device units margin
388 // Add the margin to the graphic size
392 // Get the size of the DC in pixels
395 // Calculate a suitable scaling factor
396 float scaleX
=(float)(w
/maxX
);
397 float scaleY
=(float)(h
/maxY
);
399 // Use x or y scaling factor, whichever fits on the DC
400 float actualScale
= wxMin(scaleX
,scaleY
);
402 // Calculate the position on the DC for centring the graphic
403 float posX
= (float)((w
- (200*actualScale
))/2.0);
404 float posY
= (float)((h
- (200*actualScale
))/2.0);
406 // Set the scale and origin
407 dc
->SetUserScale(actualScale
, actualScale
);
408 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
413 void MyPrintout::DrawPageTwo(wxDC
*dc
)
415 /* You might use THIS code to set the printer DC to ROUGHLY reflect
416 * the screen text size. This page also draws lines of actual length 5cm
419 // Get the logical pixels per inch of screen and printer
420 int ppiScreenX
, ppiScreenY
;
421 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
422 int ppiPrinterX
, ppiPrinterY
;
423 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
425 // This scales the DC so that the printout roughly represents the
426 // the screen scaling. The text point size _should_ be the right size
427 // but in fact is too small for some reason. This is a detail that will
428 // need to be addressed at some point but can be fudged for the
430 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
432 // Now we have to check in case our real page size is reduced
433 // (e.g. because we're drawing to a print preview memory DC)
434 int pageWidth
, pageHeight
;
437 GetPageSizePixels(&pageWidth
, &pageHeight
);
439 // If printer pageWidth == current DC width, then this doesn't
440 // change. But w might be the preview bitmap width, so scale down.
441 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
442 dc
->SetUserScale(overallScale
, overallScale
);
444 // Calculate conversion factor for converting millimetres into
446 // There are approx. 25.1 mm to the inch. There are ppi
447 // device units to the inch. Therefore 1 mm corresponds to
448 // ppi/25.1 device units. We also divide by the
449 // screen-to-printer scaling factor, because we need to
450 // unscale to pass logical units to DrawLine.
452 // Draw 50 mm by 50 mm L shape
453 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.1));
454 float logUnits
= (float)(50*logUnitsFactor
);
455 dc
->SetPen(* wxBLACK_PEN
);
456 dc
->DrawLine(50, 250, (long)(50.0 + logUnits
), 250);
457 dc
->DrawLine(50, 250, 50, (long)(250.0 + logUnits
));
459 dc
->SetFont(* wxGetApp().m_testFont
);
460 dc
->SetBackgroundMode(wxTRANSPARENT
);
462 dc
->DrawText("Some test text", 200, 200 );
467 int rightMargin
= 20;
469 int bottomMargin
= 20;
471 int pageWidthMM
, pageHeightMM
;
472 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
474 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
475 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
476 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
477 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
479 dc
->SetPen(* wxRED_PEN
);
480 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
481 (long)rightMarginLogical
, (long)topMarginLogical
);
482 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
483 (long)rightMarginLogical
, (long)bottomMarginLogical
);
485 WritePageHeader(this, dc
, "A header", logUnitsFactor
);
488 // Writes a header on a page. Margin units are in millimetres.
489 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
)
492 static wxFont *headerFont = (wxFont *) NULL;
495 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
497 dc->SetFont(headerFont);
500 int pageWidthMM
, pageHeightMM
;
502 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
506 int rightMargin
= 10;
508 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
509 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
510 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
512 long xExtent
, yExtent
;
513 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
514 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
515 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
517 dc
->SetPen(* wxBLACK_PEN
);
518 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
519 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);