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 !wxUSE_PRINTING_ARCHITECTURE
29 #error You must set wxUSE_PRINTING_ARCHITECTURE to 1 in wx_setup.h to compile this demo.
32 // Set this to 1 if you want to test PostScript printing under MSW.
33 // However, you'll also need to edit src/msw/makefile.nt.
34 #define wxTEST_POSTSCRIPT_IN_MSW 0
37 #include "wx/metafile.h"
39 #include "wx/printdlg.h"
43 #if wxTEST_POSTSCRIPT_IN_MSW
44 #include "wx/generic/printps.h"
45 #include "wx/generic/prntdlgg.h"
51 #include "mondrian.xpm"
55 MyFrame
*frame
= (MyFrame
*) NULL
;
56 int orientation
= wxPORTRAIT
;
61 // Writes a header on a page. Margin units are in millimetres.
62 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
);
68 // The `main program' equivalent, creating the windows and returning the
70 bool MyApp::OnInit(void)
72 m_testFont
= new wxFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
74 // Create the main frame window
75 frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows Printing Demo", wxPoint(0, 0), wxSize(400, 400));
77 // Give it a status line
78 frame
->CreateStatusBar(2);
80 // Load icon and bitmap
81 frame
->SetIcon( wxICON( mondrian
) );
84 wxMenu
*file_menu
= new wxMenu
;
86 file_menu
->Append(WXPRINT_PRINT
, "&Print...", "Print");
87 file_menu
->Append(WXPRINT_PRINT_SETUP
, "Print &Setup...", "Setup printer properties");
88 file_menu
->Append(WXPRINT_PAGE_SETUP
, "Page Set&up...", "Page setup");
89 file_menu
->Append(WXPRINT_PREVIEW
, "Print Pre&view", "Preview");
92 wxAcceleratorEntry entries
[1];
93 entries
[0].Set(wxACCEL_CTRL
, (int) 'V', WXPRINT_PREVIEW
);
94 wxAcceleratorTable
accel(1, entries
);
95 frame
->SetAcceleratorTable(accel
);
97 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
98 file_menu
->AppendSeparator();
99 file_menu
->Append(WXPRINT_PRINT_PS
, "Print PostScript...", "Print (PostScript)");
100 file_menu
->Append(WXPRINT_PRINT_SETUP_PS
, "Print Setup PostScript...", "Setup printer properties (PostScript)");
101 file_menu
->Append(WXPRINT_PAGE_SETUP_PS
, "Page Setup PostScript...", "Page setup (PostScript)");
102 file_menu
->Append(WXPRINT_PREVIEW_PS
, "Print Preview PostScript", "Preview (PostScript)");
104 file_menu
->AppendSeparator();
105 file_menu
->Append(WXPRINT_QUIT
, "E&xit", "Exit program");
107 wxMenu
*help_menu
= new wxMenu
;
108 help_menu
->Append(WXPRINT_ABOUT
, "&About", "About this demo");
110 wxMenuBar
*menu_bar
= new wxMenuBar
;
112 menu_bar
->Append(file_menu
, "&File");
113 menu_bar
->Append(help_menu
, "&Help");
115 // Associate the menu bar with the frame
116 frame
->SetMenuBar(menu_bar
);
118 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100), wxRETAINED
|wxHSCROLL
|wxVSCROLL
);
120 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
121 canvas
->SetScrollbars(20, 20, 50, 50);
123 frame
->canvas
= canvas
;
125 frame
->Centre(wxBOTH
);
128 frame
->SetStatusText("Printing demo");
135 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
136 EVT_MENU(WXPRINT_QUIT
, MyFrame::OnExit
)
137 EVT_MENU(WXPRINT_PRINT
, MyFrame::OnPrint
)
138 EVT_MENU(WXPRINT_PREVIEW
, MyFrame::OnPrintPreview
)
139 EVT_MENU(WXPRINT_PRINT_SETUP
, MyFrame::OnPrintSetup
)
140 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
141 EVT_MENU(WXPRINT_ABOUT
, MyFrame::OnPrintAbout
)
142 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
143 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame::OnPrintPS
)
144 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
145 EVT_MENU(WXPRINT_PRINT_SETUP_PS
, MyFrame::OnPrintSetupPS
)
146 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
150 // Define my frame constructor
151 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
152 wxFrame(frame
, -1, title
, pos
, size
)
154 canvas
= (MyCanvas
*) NULL
;
157 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
162 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
165 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
167 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
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
))
178 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
180 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
182 wxPrintData printData
;
183 printData
.SetOrientation(orientation
);
185 // Pass two printout objects: for preview, and possible printing.
186 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
190 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK
);
194 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
195 frame
->Centre(wxBOTH
);
200 void MyFrame::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
203 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
205 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
208 data
.SetOrientation(orientation
);
211 wxPrintDialog
printerDialog(this, & data
);
213 wxGenericPrintDialog
printerDialog(this, & data
);
215 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
216 printerDialog
.ShowModal();
218 orientation
= printerDialog
.GetPrintData().GetOrientation();
221 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
224 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
226 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
228 wxPageSetupData data
;
229 data
.SetOrientation(orientation
);
232 wxPageSetupDialog
pageSetupDialog(this, & data
);
234 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
236 pageSetupDialog
.ShowModal();
238 data
= pageSetupDialog
.GetPageSetupData();
239 orientation
= data
.GetOrientation();
242 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
243 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
245 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
247 wxPostScriptPrinter printer
;
248 MyPrintout
printout("My printout");
249 printer
.Print(this, &printout
, TRUE
);
252 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
254 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
256 wxPrintData printData
;
257 printData
.SetOrientation(orientation
);
259 // Pass two printout objects: for preview, and possible printing.
260 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
261 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
262 frame
->Centre(wxBOTH
);
267 void MyFrame::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
269 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
272 data
.SetOrientation(orientation
);
274 wxGenericPrintDialog
printerDialog(this, & data
);
275 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
276 printerDialog
.ShowModal();
278 orientation
= printerDialog
.GetPrintData().GetOrientation();
281 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
283 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
285 wxPageSetupData data
;
286 data
.SetOrientation(orientation
);
288 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
289 pageSetupDialog
.ShowModal();
291 orientation
= pageSetupDialog
.GetPageSetupData().GetOrientation();
296 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
298 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
299 "About wxWindows printing demo", wxOK
|wxCENTRE
);
302 void MyFrame::Draw(wxDC
& dc
)
304 dc
.SetFont(* wxGetApp().m_testFont
);
306 dc
.SetBackgroundMode(wxTRANSPARENT
);
308 dc
.SetBrush(* wxCYAN_BRUSH
);
309 dc
.SetPen(* wxRED_PEN
);
311 dc
.DrawRectangle(0, 30, 200, 100);
312 dc
.DrawText("Rectangle 200 by 100", 40, 40);
314 dc
.DrawEllipse(50, 140, 100, 50);
316 dc
.DrawText("Test message: this is in 10 point text", 10, 180);
318 dc
.SetPen(* wxBLACK_PEN
);
319 dc
.DrawLine(0, 0, 200, 200);
320 dc
.DrawLine(200, 0, 0, 200);
322 wxIcon
my_icon( wxICON(mondrian
) );
324 dc
.DrawIcon( my_icon
, 100, 100);
327 void MyFrame::OnSize(wxSizeEvent
& event
)
329 wxFrame::OnSize(event
);
332 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
333 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
336 // Define a constructor for my canvas
337 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
338 wxScrolledWindow(frame
, -1, pos
, size
, style
)
342 MyCanvas::~MyCanvas(void)
346 // Define the repainting behaviour
347 void MyCanvas::OnDraw(wxDC
& dc
)
352 void MyCanvas::OnEvent(wxMouseEvent
& WXUNUSED(event
))
356 bool MyFrame::OnClose(void)
359 delete wxGetApp().m_testFont
;
364 bool MyPrintout::OnPrintPage(int page
)
374 dc
->SetDeviceOrigin(0, 0);
375 dc
->SetUserScale(1.0, 1.0);
378 sprintf(buf
, "PAGE %d", page
);
379 dc
->DrawText(buf
, 10, 10);
387 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
389 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
395 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
403 bool MyPrintout::HasPage(int pageNum
)
405 return (pageNum
== 1 || pageNum
== 2);
408 void MyPrintout::DrawPageOne(wxDC
*dc
)
410 /* You might use THIS code if you were scaling
411 * graphics of known size to fit on the page.
415 // We know the graphic is 200x200. If we didn't know this,
416 // we'd need to calculate it.
420 // Let's have at least 50 device units margin
424 // Add the margin to the graphic size
428 // Get the size of the DC in pixels
431 // Calculate a suitable scaling factor
432 float scaleX
=(float)(w
/maxX
);
433 float scaleY
=(float)(h
/maxY
);
435 // Use x or y scaling factor, whichever fits on the DC
436 float actualScale
= wxMin(scaleX
,scaleY
);
438 // Calculate the position on the DC for centring the graphic
439 float posX
= (float)((w
- (200*actualScale
))/2.0);
440 float posY
= (float)((h
- (200*actualScale
))/2.0);
442 // Set the scale and origin
443 dc
->SetUserScale(actualScale
, actualScale
);
444 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
449 void MyPrintout::DrawPageTwo(wxDC
*dc
)
451 /* You might use THIS code to set the printer DC to ROUGHLY reflect
452 * the screen text size. This page also draws lines of actual length 5cm
455 // Get the logical pixels per inch of screen and printer
456 int ppiScreenX
, ppiScreenY
;
457 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
458 int ppiPrinterX
, ppiPrinterY
;
459 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
461 // This scales the DC so that the printout roughly represents the
462 // the screen scaling. The text point size _should_ be the right size
463 // but in fact is too small for some reason. This is a detail that will
464 // need to be addressed at some point but can be fudged for the
466 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
468 // Now we have to check in case our real page size is reduced
469 // (e.g. because we're drawing to a print preview memory DC)
470 int pageWidth
, pageHeight
;
473 GetPageSizePixels(&pageWidth
, &pageHeight
);
475 // If printer pageWidth == current DC width, then this doesn't
476 // change. But w might be the preview bitmap width, so scale down.
477 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
478 dc
->SetUserScale(overallScale
, overallScale
);
480 // Calculate conversion factor for converting millimetres into
482 // There are approx. 25.1 mm to the inch. There are ppi
483 // device units to the inch. Therefore 1 mm corresponds to
484 // ppi/25.1 device units. We also divide by the
485 // screen-to-printer scaling factor, because we need to
486 // unscale to pass logical units to DrawLine.
488 // Draw 50 mm by 50 mm L shape
489 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.1));
490 float logUnits
= (float)(50*logUnitsFactor
);
491 dc
->SetPen(* wxBLACK_PEN
);
492 dc
->DrawLine(50, 250, (long)(50.0 + logUnits
), 250);
493 dc
->DrawLine(50, 250, 50, (long)(250.0 + logUnits
));
495 dc
->SetFont(* wxGetApp().m_testFont
);
496 dc
->SetBackgroundMode(wxTRANSPARENT
);
498 dc
->DrawText("Some test text", 200, 200 );
503 int rightMargin
= 20;
505 int bottomMargin
= 20;
507 int pageWidthMM
, pageHeightMM
;
508 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
510 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
511 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
512 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
513 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
515 dc
->SetPen(wxRED_PEN
);
516 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
517 (long)rightMarginLogical
, (long)topMarginLogical
);
518 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
519 (long)rightMarginLogical
, (long)bottomMarginLogical
);
521 WritePageHeader(this, dc
, "A header", logUnitsFactor
);
524 // Writes a header on a page. Margin units are in millimetres.
525 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
)
528 static wxFont *headerFont = (wxFont *) NULL;
531 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
533 dc->SetFont(headerFont);
536 int pageWidthMM
, pageHeightMM
;
538 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
542 int rightMargin
= 10;
544 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
545 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
546 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
548 long xExtent
, yExtent
;
549 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
550 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
551 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
553 dc
->SetPen(* wxBLACK_PEN
);
554 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
555 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);