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.
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
= (MyFrame
*) NULL
;
43 int orientation
= wxPORTRAIT
;
52 // Writes a header on a page. Margin units are in millimetres.
53 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
);
59 // The `main program' equivalent, creating the windows and returning the
61 bool MyApp::OnInit(void)
63 m_testFont
= new wxFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
65 // Create the main frame window
66 frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows Printing Demo", wxPoint(0, 0), wxSize(400, 400));
68 // Give it a status line
69 frame
->CreateStatusBar(2);
71 // Load icon and bitmap
73 frame
->SetIcon(wxIcon("mondrian"));
76 // frame->SetIcon(wxIcon(mondrian_bits, mondrian_width, mondrian_height));
80 wxMenu
*file_menu
= new wxMenu
;
82 file_menu
->Append(WXPRINT_PRINT
, "&Print...", "Print");
83 file_menu
->Append(WXPRINT_PRINT_SETUP
, "Print &Setup...", "Setup printer properties");
84 file_menu
->Append(WXPRINT_PAGE_SETUP
, "Page Set&up...", "Page setup");
85 file_menu
->Append(WXPRINT_PREVIEW
, "Print Pre&view", "Preview");
88 file_menu
->AppendSeparator();
89 file_menu
->Append(WXPRINT_PRINT_PS
, "Print PostScript...", "Print (PostScript)");
90 file_menu
->Append(WXPRINT_PRINT_SETUP_PS
, "Print Setup PostScript...", "Setup printer properties (PostScript)");
91 file_menu
->Append(WXPRINT_PAGE_SETUP_PS
, "Page Setup PostScript...", "Page setup (PostScript)");
92 file_menu
->Append(WXPRINT_PREVIEW_PS
, "Print Preview PostScript", "Preview (PostScript)");
94 file_menu
->AppendSeparator();
95 file_menu
->Append(WXPRINT_QUIT
, "E&xit", "Exit program");
97 wxMenu
*help_menu
= new wxMenu
;
98 help_menu
->Append(WXPRINT_ABOUT
, "&About", "About this demo");
100 wxMenuBar
*menu_bar
= new wxMenuBar
;
102 menu_bar
->Append(file_menu
, "&File");
103 menu_bar
->Append(help_menu
, "&Help");
105 // Associate the menu bar with the frame
106 frame
->SetMenuBar(menu_bar
);
108 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100), wxRETAINED
|wxHSCROLL
|wxVSCROLL
);
110 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
111 canvas
->SetScrollbars(20, 20, 50, 50);
113 frame
->canvas
= canvas
;
115 frame
->Centre(wxBOTH
);
118 frame
->SetStatusText("Printing demo");
125 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
126 EVT_MENU(WXPRINT_QUIT
, MyFrame::OnExit
)
127 EVT_MENU(WXPRINT_PRINT
, MyFrame::OnPrint
)
128 EVT_MENU(WXPRINT_PREVIEW
, MyFrame::OnPrintPreview
)
129 EVT_MENU(WXPRINT_PRINT_SETUP
, MyFrame::OnPrintSetup
)
130 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
131 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame::OnPrintPS
)
132 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
133 EVT_MENU(WXPRINT_PRINT_SETUP_PS
, MyFrame::OnPrintSetupPS
)
134 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
135 EVT_MENU(WXPRINT_ABOUT
, MyFrame::OnPrintAbout
)
138 // Define my frame constructor
139 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
140 wxFrame(frame
, -1, title
, pos
, size
)
142 canvas
= (MyCanvas
*) NULL
;
145 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
150 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
153 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
155 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
158 MyPrintout
printout("My printout");
159 if (!printer
.Print(this, &printout
, TRUE
))
160 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
);
163 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
165 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
167 wxPostScriptPrinter printer
;
168 MyPrintout
printout("My printout");
169 printer
.Print(this, &printout
, TRUE
);
172 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
175 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
177 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
179 wxPrintData printData
;
180 printData
.SetOrientation(orientation
);
182 // Pass two printout objects: for preview, and possible printing.
183 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
187 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK
);
191 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
192 frame
->Centre(wxBOTH
);
197 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
199 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
201 wxPrintData printData
;
202 printData
.SetOrientation(orientation
);
204 // Pass two printout objects: for preview, and possible printing.
205 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
206 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
207 frame
->Centre(wxBOTH
);
212 void MyFrame::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
215 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
217 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
220 data
.SetOrientation(orientation
);
223 wxPrintDialog
printerDialog(this, & data
);
225 wxGenericPrintDialog
printerDialog(this, & data
);
227 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
228 printerDialog
.ShowModal();
230 orientation
= printerDialog
.GetPrintData().GetOrientation();
233 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
236 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
238 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
240 wxPageSetupData data
;
241 data
.SetOrientation(orientation
);
244 wxPageSetupDialog
pageSetupDialog(this, & data
);
246 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
248 pageSetupDialog
.ShowModal();
250 data
= pageSetupDialog
.GetPageSetupData();
251 orientation
= data
.GetOrientation();
254 void MyFrame::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
256 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
259 data
.SetOrientation(orientation
);
261 wxGenericPrintDialog
printerDialog(this, & data
);
262 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
263 printerDialog
.ShowModal();
265 orientation
= printerDialog
.GetPrintData().GetOrientation();
268 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
270 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
272 wxPageSetupData data
;
273 data
.SetOrientation(orientation
);
275 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
276 pageSetupDialog
.ShowModal();
278 orientation
= pageSetupDialog
.GetPageSetupData().GetOrientation();
281 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
283 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
284 "About wxWindows printing demo", wxOK
|wxCENTRE
);
287 void MyFrame::Draw(wxDC
& dc
)
289 dc
.SetFont(* wxGetApp().m_testFont
);
291 dc
.SetBackgroundMode(wxTRANSPARENT
);
293 dc
.SetBrush(* wxCYAN_BRUSH
);
294 dc
.SetPen(* wxRED_PEN
);
296 dc
.DrawRectangle(0, 30, 200, 100);
297 dc
.DrawText("Rectangle 200 by 100", 40, 40);
299 dc
.DrawEllipse(50, 140, 100, 50);
301 dc
.DrawText("Test message: this is in 10 point text", 10, 180);
303 dc
.SetPen(* wxBLACK_PEN
);
304 dc
.DrawLine(0, 0, 200, 200);
305 dc
.DrawLine(200, 0, 0, 200);
307 #if defined(__WXGTK__)
308 wxIcon
my_icon( folder_xpm
);
309 #elif defined(__WXMSW__)
310 wxIcon
my_icon( "mondrian" );
312 #error "Platform not supported."
315 dc
.DrawIcon( my_icon
, 100, 100);
318 void MyFrame::OnSize(wxSizeEvent
& event
)
320 wxFrame::OnSize(event
);
323 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
324 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
327 // Define a constructor for my canvas
328 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
329 wxScrolledWindow(frame
, -1, pos
, size
, style
)
333 MyCanvas::~MyCanvas(void)
337 // Define the repainting behaviour
338 void MyCanvas::OnDraw(wxDC
& dc
)
343 void MyCanvas::OnEvent(wxMouseEvent
& WXUNUSED(event
))
347 bool MyFrame::OnClose(void)
350 delete wxGetApp().m_testFont
;
355 bool MyPrintout::OnPrintPage(int page
)
365 dc
->SetDeviceOrigin(0, 0);
368 sprintf(buf
, "PAGE %d", page
);
369 dc
->DrawText(buf
, 10, 10);
377 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
379 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
385 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
393 bool MyPrintout::HasPage(int pageNum
)
395 return (pageNum
== 1 || pageNum
== 2);
398 void MyPrintout::DrawPageOne(wxDC
*dc
)
400 /* You might use THIS code if you were scaling
401 * graphics of known size to fit on the page.
405 // We know the graphic is 200x200. If we didn't know this,
406 // we'd need to calculate it.
410 // Let's have at least 50 device units margin
414 // Add the margin to the graphic size
418 // Get the size of the DC in pixels
421 // Calculate a suitable scaling factor
422 float scaleX
=(float)(w
/maxX
);
423 float scaleY
=(float)(h
/maxY
);
425 // Use x or y scaling factor, whichever fits on the DC
426 float actualScale
= wxMin(scaleX
,scaleY
);
428 // Calculate the position on the DC for centring the graphic
429 float posX
= (float)((w
- (200*actualScale
))/2.0);
430 float posY
= (float)((h
- (200*actualScale
))/2.0);
432 // Set the scale and origin
433 dc
->SetUserScale(actualScale
, actualScale
);
434 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
439 void MyPrintout::DrawPageTwo(wxDC
*dc
)
441 /* You might use THIS code to set the printer DC to ROUGHLY reflect
442 * the screen text size. This page also draws lines of actual length 5cm
445 // Get the logical pixels per inch of screen and printer
446 int ppiScreenX
, ppiScreenY
;
447 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
448 int ppiPrinterX
, ppiPrinterY
;
449 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
451 // This scales the DC so that the printout roughly represents the
452 // the screen scaling. The text point size _should_ be the right size
453 // but in fact is too small for some reason. This is a detail that will
454 // need to be addressed at some point but can be fudged for the
456 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
458 // Now we have to check in case our real page size is reduced
459 // (e.g. because we're drawing to a print preview memory DC)
460 int pageWidth
, pageHeight
;
463 GetPageSizePixels(&pageWidth
, &pageHeight
);
465 // If printer pageWidth == current DC width, then this doesn't
466 // change. But w might be the preview bitmap width, so scale down.
467 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
468 dc
->SetUserScale(overallScale
, overallScale
);
470 // Calculate conversion factor for converting millimetres into
472 // There are approx. 25.1 mm to the inch. There are ppi
473 // device units to the inch. Therefore 1 mm corresponds to
474 // ppi/25.1 device units. We also divide by the
475 // screen-to-printer scaling factor, because we need to
476 // unscale to pass logical units to DrawLine.
478 // Draw 50 mm by 50 mm L shape
479 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.1));
480 float logUnits
= (float)(50*logUnitsFactor
);
481 dc
->SetPen(* wxBLACK_PEN
);
482 dc
->DrawLine(50, 50, (long)(50.0 + logUnits
), 50);
483 dc
->DrawLine(50, 50, 50, (long)(50.0 + logUnits
));
485 dc
->SetFont(* wxGetApp().m_testFont
);
486 dc
->SetBackgroundMode(wxTRANSPARENT
);
488 dc
->DrawText("Some test text", 200, 200 );
493 int rightMargin
= 20;
495 int bottomMargin
= 20;
497 int pageWidthMM
, pageHeightMM
;
498 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
500 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
501 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
502 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
503 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
505 dc
->SetPen(wxBLACK_PEN
);
506 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
507 (long)rightMarginLogical
, (long)topMarginLogical
);
508 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
509 (long)rightMarginLogical
, (long)bottomMarginLogical
);
511 WritePageHeader(this, dc
, "A header", logUnitsFactor
);
514 // Writes a header on a page. Margin units are in millimetres.
515 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
)
517 static wxFont
*headerFont
= (wxFont
*) NULL
;
520 headerFont
= wxTheFontList
->FindOrCreateFont(16, wxSWISS
, wxNORMAL
, wxBOLD
);
522 dc
->SetFont(headerFont
);
524 int pageWidthMM
, pageHeightMM
;
526 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
530 int rightMargin
= 10;
532 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
533 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
534 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
536 long xExtent
, yExtent
;
537 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
538 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
539 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
541 dc
->SetPen(* wxBLACK_PEN
);
542 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
543 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);