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 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
166 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
169 MyPrintout
printout("My printout");
170 if (!printer
.Print(this, &printout
, TRUE
))
171 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
);
174 void MyFrame
::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
177 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
179 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
181 wxPrintData printData
;
182 printData
.SetOrientation(orientation
);
184 // Pass two printout objects: for preview, and possible printing.
185 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
189 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK
);
193 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
194 frame
->Centre(wxBOTH
);
199 void MyFrame
::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
202 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
204 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
207 data
.SetOrientation(orientation
);
210 wxPrintDialog
printerDialog(this, & data
);
212 wxGenericPrintDialog
printerDialog(this, & data
);
214 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
215 printerDialog
.ShowModal();
217 orientation
= printerDialog
.GetPrintData().GetOrientation();
220 void MyFrame
::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
223 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
225 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
227 wxPageSetupData data
;
228 data
.SetOrientation(orientation
);
231 wxPageSetupDialog
pageSetupDialog(this, & data
);
233 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
235 pageSetupDialog
.ShowModal();
237 data
= pageSetupDialog
.GetPageSetupData();
238 orientation
= data
.GetOrientation();
241 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
242 void MyFrame
::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
244 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
246 wxPostScriptPrinter printer
;
247 MyPrintout
printout("My printout");
248 printer
.Print(this, &printout
, TRUE
);
251 void MyFrame
::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
253 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
255 wxPrintData printData
;
256 printData
.SetOrientation(orientation
);
258 // Pass two printout objects: for preview, and possible printing.
259 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
260 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
261 frame
->Centre(wxBOTH
);
266 void MyFrame
::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
268 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
271 data
.SetOrientation(orientation
);
273 wxGenericPrintDialog
printerDialog(this, & data
);
274 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
275 printerDialog
.ShowModal();
277 orientation
= printerDialog
.GetPrintData().GetOrientation();
280 void MyFrame
::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
282 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
284 wxPageSetupData data
;
285 data
.SetOrientation(orientation
);
287 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
288 pageSetupDialog
.ShowModal();
290 orientation
= pageSetupDialog
.GetPageSetupData().GetOrientation();
295 void MyFrame
::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
297 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
298 "About wxWindows printing demo", wxOK
|wxCENTRE
);
301 void MyFrame
::Draw(wxDC
& dc
)
303 dc
.SetFont(* wxGetApp().m_testFont
);
305 dc
.SetBackgroundMode(wxTRANSPARENT
);
307 dc
.SetBrush(* wxCYAN_BRUSH
);
308 dc
.SetPen(* wxRED_PEN
);
310 dc
.DrawRectangle(0, 30, 200, 100);
311 dc
.DrawText("Rectangle 200 by 100", 40, 40);
313 dc
.DrawEllipse(50, 140, 100, 50);
315 dc
.DrawText("Test message: this is in 10 point text", 10, 180);
317 dc
.SetPen(* wxBLACK_PEN
);
318 dc
.DrawLine(0, 0, 200, 200);
319 dc
.DrawLine(200, 0, 0, 200);
321 wxIcon my_icon
= wxICON(mondrian
) ;
323 dc
.DrawIcon( my_icon
, 100, 100);
326 void MyFrame
::OnSize(wxSizeEvent
& event
)
328 wxFrame
::OnSize(event
);
331 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
332 EVT_MOUSE_EVENTS(MyCanvas
::OnEvent
)
335 // Define a constructor for my canvas
336 MyCanvas
::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
337 wxScrolledWindow(frame
, -1, pos
, size
, style
)
341 MyCanvas
::~MyCanvas(void)
345 // Define the repainting behaviour
346 void MyCanvas
::OnDraw(wxDC
& dc
)
351 void MyCanvas
::OnEvent(wxMouseEvent
& WXUNUSED(event
))
355 bool MyFrame
::OnClose(void)
358 delete wxGetApp().m_testFont
;
363 bool MyPrintout
::OnPrintPage(int page
)
373 dc
->SetDeviceOrigin(0, 0);
374 dc
->SetUserScale(1.0, 1.0);
377 sprintf(buf
, "PAGE %d", page
);
378 dc
->DrawText(buf
, 10, 10);
386 bool MyPrintout
::OnBeginDocument(int startPage
, int endPage
)
388 if (!wxPrintout
::OnBeginDocument(startPage
, endPage
))
394 void MyPrintout
::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
402 bool MyPrintout
::HasPage(int pageNum
)
404 return (pageNum
== 1 || pageNum
== 2);
407 void MyPrintout
::DrawPageOne(wxDC
*dc
)
409 /* You might use THIS code if you were scaling
410 * graphics of known size to fit on the page.
414 // We know the graphic is 200x200. If we didn't know this,
415 // we'd need to calculate it.
419 // Let's have at least 50 device units margin
423 // Add the margin to the graphic size
427 // Get the size of the DC in pixels
430 // Calculate a suitable scaling factor
431 float scaleX
=(float)(w
/maxX
);
432 float scaleY
=(float)(h
/maxY
);
434 // Use x or y scaling factor, whichever fits on the DC
435 float actualScale
= wxMin(scaleX
,scaleY
);
437 // Calculate the position on the DC for centring the graphic
438 float posX
= (float)((w
- (200*actualScale
))/2.0);
439 float posY
= (float)((h
- (200*actualScale
))/2.0);
441 // Set the scale and origin
442 dc
->SetUserScale(actualScale
, actualScale
);
443 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
448 void MyPrintout
::DrawPageTwo(wxDC
*dc
)
450 /* You might use THIS code to set the printer DC to ROUGHLY reflect
451 * the screen text size. This page also draws lines of actual length 5cm
454 // Get the logical pixels per inch of screen and printer
455 int ppiScreenX
, ppiScreenY
;
456 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
457 int ppiPrinterX
, ppiPrinterY
;
458 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
460 // This scales the DC so that the printout roughly represents the
461 // the screen scaling. The text point size _should_ be the right size
462 // but in fact is too small for some reason. This is a detail that will
463 // need to be addressed at some point but can be fudged for the
465 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
467 // Now we have to check in case our real page size is reduced
468 // (e.g. because we're drawing to a print preview memory DC)
469 int pageWidth
, pageHeight
;
472 GetPageSizePixels(&pageWidth
, &pageHeight
);
474 // If printer pageWidth == current DC width, then this doesn't
475 // change. But w might be the preview bitmap width, so scale down.
476 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
477 dc
->SetUserScale(overallScale
, overallScale
);
479 // Calculate conversion factor for converting millimetres into
481 // There are approx. 25.1 mm to the inch. There are ppi
482 // device units to the inch. Therefore 1 mm corresponds to
483 // ppi/25.1 device units. We also divide by the
484 // screen-to-printer scaling factor, because we need to
485 // unscale to pass logical units to DrawLine.
487 // Draw 50 mm by 50 mm L shape
488 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.1));
489 float logUnits
= (float)(50*logUnitsFactor
);
490 dc
->SetPen(* wxBLACK_PEN
);
491 dc
->DrawLine(50, 250, (long)(50.0 + logUnits
), 250);
492 dc
->DrawLine(50, 250, 50, (long)(250.0 + logUnits
));
494 dc
->SetFont(* wxGetApp().m_testFont
);
495 dc
->SetBackgroundMode(wxTRANSPARENT
);
497 dc
->DrawText("Some test text", 200, 200 );
502 int rightMargin
= 20;
504 int bottomMargin
= 20;
506 int pageWidthMM
, pageHeightMM
;
507 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
509 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
510 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
511 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
512 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
514 dc
->SetPen(* wxRED_PEN
);
515 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
516 (long)rightMarginLogical
, (long)topMarginLogical
);
517 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
518 (long)rightMarginLogical
, (long)bottomMarginLogical
);
520 WritePageHeader(this, dc
, "A header", logUnitsFactor
);
523 // Writes a header on a page. Margin units are in millimetres.
524 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
)
527 static wxFont *headerFont = (wxFont *) NULL;
530 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
532 dc->SetFont(headerFont);
535 int pageWidthMM
, pageHeightMM
;
537 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
541 int rightMargin
= 10;
543 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
544 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
545 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
547 long xExtent
, yExtent
;
548 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
549 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
550 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
552 dc
->SetPen(* wxBLACK_PEN
);
553 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
554 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);