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"
41 #if wxTEST_POSTSCRIPT_IN_MSW
42 #include "wx/generic/printps.h"
43 #include "wx/generic/prntdlgg.h"
49 MyFrame
*frame
= (MyFrame
*) NULL
;
50 int orientation
= wxPORTRAIT
;
55 #if defined(__WXGTK__) || defined(__WXMOTIF__)
59 // Writes a header on a page. Margin units are in millimetres.
60 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
);
66 // The `main program' equivalent, creating the windows and returning the
68 bool MyApp::OnInit(void)
70 m_testFont
= new wxFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
72 // Create the main frame window
73 frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows Printing Demo", wxPoint(0, 0), wxSize(400, 400));
75 // Give it a status line
76 frame
->CreateStatusBar(2);
78 // Load icon and bitmap
80 frame
->SetIcon(wxIcon("mondrian"));
83 // frame->SetIcon(wxIcon(mondrian_bits, mondrian_width, mondrian_height));
87 wxMenu
*file_menu
= new wxMenu
;
89 file_menu
->Append(WXPRINT_PRINT
, "&Print...", "Print");
90 file_menu
->Append(WXPRINT_PRINT_SETUP
, "Print &Setup...", "Setup printer properties");
91 file_menu
->Append(WXPRINT_PAGE_SETUP
, "Page Set&up...", "Page setup");
92 file_menu
->Append(WXPRINT_PREVIEW
, "Print Pre&view", "Preview");
94 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
95 file_menu
->AppendSeparator();
96 file_menu
->Append(WXPRINT_PRINT_PS
, "Print PostScript...", "Print (PostScript)");
97 file_menu
->Append(WXPRINT_PRINT_SETUP_PS
, "Print Setup PostScript...", "Setup printer properties (PostScript)");
98 file_menu
->Append(WXPRINT_PAGE_SETUP_PS
, "Page Setup PostScript...", "Page setup (PostScript)");
99 file_menu
->Append(WXPRINT_PREVIEW_PS
, "Print Preview PostScript", "Preview (PostScript)");
101 file_menu
->AppendSeparator();
102 file_menu
->Append(WXPRINT_QUIT
, "E&xit", "Exit program");
104 wxMenu
*help_menu
= new wxMenu
;
105 help_menu
->Append(WXPRINT_ABOUT
, "&About", "About this demo");
107 wxMenuBar
*menu_bar
= new wxMenuBar
;
109 menu_bar
->Append(file_menu
, "&File");
110 menu_bar
->Append(help_menu
, "&Help");
112 // Associate the menu bar with the frame
113 frame
->SetMenuBar(menu_bar
);
115 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100), wxRETAINED
|wxHSCROLL
|wxVSCROLL
);
117 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
118 canvas
->SetScrollbars(20, 20, 50, 50);
120 frame
->canvas
= canvas
;
122 frame
->Centre(wxBOTH
);
125 frame
->SetStatusText("Printing demo");
132 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
133 EVT_MENU(WXPRINT_QUIT
, MyFrame::OnExit
)
134 EVT_MENU(WXPRINT_PRINT
, MyFrame::OnPrint
)
135 EVT_MENU(WXPRINT_PREVIEW
, MyFrame::OnPrintPreview
)
136 EVT_MENU(WXPRINT_PRINT_SETUP
, MyFrame::OnPrintSetup
)
137 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
138 EVT_MENU(WXPRINT_ABOUT
, MyFrame::OnPrintAbout
)
139 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
140 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame::OnPrintPS
)
141 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
142 EVT_MENU(WXPRINT_PRINT_SETUP_PS
, MyFrame::OnPrintSetupPS
)
143 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
147 // Define my frame constructor
148 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
149 wxFrame(frame
, -1, title
, pos
, size
)
151 canvas
= (MyCanvas
*) NULL
;
154 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
159 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
162 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
164 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
167 MyPrintout
printout("My printout");
168 if (!printer
.Print(this, &printout
, TRUE
))
169 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
);
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::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
200 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
202 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
205 data
.SetOrientation(orientation
);
208 wxPrintDialog
printerDialog(this, & data
);
210 wxGenericPrintDialog
printerDialog(this, & data
);
212 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
213 printerDialog
.ShowModal();
215 orientation
= printerDialog
.GetPrintData().GetOrientation();
218 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
221 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
223 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
225 wxPageSetupData data
;
226 data
.SetOrientation(orientation
);
229 wxPageSetupDialog
pageSetupDialog(this, & data
);
231 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
233 pageSetupDialog
.ShowModal();
235 data
= pageSetupDialog
.GetPageSetupData();
236 orientation
= data
.GetOrientation();
239 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
240 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
242 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
244 wxPostScriptPrinter printer
;
245 MyPrintout
printout("My printout");
246 printer
.Print(this, &printout
, TRUE
);
249 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
251 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
253 wxPrintData printData
;
254 printData
.SetOrientation(orientation
);
256 // Pass two printout objects: for preview, and possible printing.
257 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
258 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
259 frame
->Centre(wxBOTH
);
264 void MyFrame::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
266 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
269 data
.SetOrientation(orientation
);
271 wxGenericPrintDialog
printerDialog(this, & data
);
272 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
273 printerDialog
.ShowModal();
275 orientation
= printerDialog
.GetPrintData().GetOrientation();
278 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
280 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
282 wxPageSetupData data
;
283 data
.SetOrientation(orientation
);
285 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
286 pageSetupDialog
.ShowModal();
288 orientation
= pageSetupDialog
.GetPageSetupData().GetOrientation();
293 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
295 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
296 "About wxWindows printing demo", wxOK
|wxCENTRE
);
299 void MyFrame::Draw(wxDC
& dc
)
301 dc
.SetFont(* wxGetApp().m_testFont
);
303 dc
.SetBackgroundMode(wxTRANSPARENT
);
305 dc
.SetBrush(* wxCYAN_BRUSH
);
306 dc
.SetPen(* wxRED_PEN
);
308 dc
.DrawRectangle(0, 30, 200, 100);
309 dc
.DrawText("Rectangle 200 by 100", 40, 40);
311 dc
.DrawEllipse(50, 140, 100, 50);
313 dc
.DrawText("Test message: this is in 10 point text", 10, 180);
315 dc
.SetPen(* wxBLACK_PEN
);
316 dc
.DrawLine(0, 0, 200, 200);
317 dc
.DrawLine(200, 0, 0, 200);
319 #if defined(__WXGTK__) || defined(__WXMOTIF__)
320 wxIcon
my_icon( folder_xpm
);
321 #elif defined(__WXMSW__)
322 wxIcon
my_icon( "mondrian" );
324 #error "Platform not supported."
327 dc
.DrawIcon( my_icon
, 100, 100);
330 void MyFrame::OnSize(wxSizeEvent
& event
)
332 wxFrame::OnSize(event
);
335 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
336 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
339 // Define a constructor for my canvas
340 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
341 wxScrolledWindow(frame
, -1, pos
, size
, style
)
345 MyCanvas::~MyCanvas(void)
349 // Define the repainting behaviour
350 void MyCanvas::OnDraw(wxDC
& dc
)
355 void MyCanvas::OnEvent(wxMouseEvent
& WXUNUSED(event
))
359 bool MyFrame::OnClose(void)
362 delete wxGetApp().m_testFont
;
367 bool MyPrintout::OnPrintPage(int page
)
377 dc
->SetDeviceOrigin(0, 0);
380 sprintf(buf
, "PAGE %d", page
);
381 dc
->DrawText(buf
, 10, 10);
389 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
391 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
397 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
405 bool MyPrintout::HasPage(int pageNum
)
407 return (pageNum
== 1 || pageNum
== 2);
410 void MyPrintout::DrawPageOne(wxDC
*dc
)
412 /* You might use THIS code if you were scaling
413 * graphics of known size to fit on the page.
417 // We know the graphic is 200x200. If we didn't know this,
418 // we'd need to calculate it.
422 // Let's have at least 50 device units margin
426 // Add the margin to the graphic size
430 // Get the size of the DC in pixels
433 // Calculate a suitable scaling factor
434 float scaleX
=(float)(w
/maxX
);
435 float scaleY
=(float)(h
/maxY
);
437 // Use x or y scaling factor, whichever fits on the DC
438 float actualScale
= wxMin(scaleX
,scaleY
);
440 // Calculate the position on the DC for centring the graphic
441 float posX
= (float)((w
- (200*actualScale
))/2.0);
442 float posY
= (float)((h
- (200*actualScale
))/2.0);
444 // Set the scale and origin
445 dc
->SetUserScale(actualScale
, actualScale
);
446 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
451 void MyPrintout::DrawPageTwo(wxDC
*dc
)
453 /* You might use THIS code to set the printer DC to ROUGHLY reflect
454 * the screen text size. This page also draws lines of actual length 5cm
457 // Get the logical pixels per inch of screen and printer
458 int ppiScreenX
, ppiScreenY
;
459 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
460 int ppiPrinterX
, ppiPrinterY
;
461 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
463 // This scales the DC so that the printout roughly represents the
464 // the screen scaling. The text point size _should_ be the right size
465 // but in fact is too small for some reason. This is a detail that will
466 // need to be addressed at some point but can be fudged for the
468 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
470 // Now we have to check in case our real page size is reduced
471 // (e.g. because we're drawing to a print preview memory DC)
472 int pageWidth
, pageHeight
;
475 GetPageSizePixels(&pageWidth
, &pageHeight
);
477 // If printer pageWidth == current DC width, then this doesn't
478 // change. But w might be the preview bitmap width, so scale down.
479 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
480 dc
->SetUserScale(overallScale
, overallScale
);
482 // Calculate conversion factor for converting millimetres into
484 // There are approx. 25.1 mm to the inch. There are ppi
485 // device units to the inch. Therefore 1 mm corresponds to
486 // ppi/25.1 device units. We also divide by the
487 // screen-to-printer scaling factor, because we need to
488 // unscale to pass logical units to DrawLine.
490 // Draw 50 mm by 50 mm L shape
491 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.1));
492 float logUnits
= (float)(50*logUnitsFactor
);
493 dc
->SetPen(* wxBLACK_PEN
);
494 dc
->DrawLine(50, 50, (long)(50.0 + logUnits
), 50);
495 dc
->DrawLine(50, 50, 50, (long)(50.0 + logUnits
));
497 dc
->SetFont(* wxGetApp().m_testFont
);
498 dc
->SetBackgroundMode(wxTRANSPARENT
);
500 dc
->DrawText("Some test text", 200, 200 );
505 int rightMargin
= 20;
507 int bottomMargin
= 20;
509 int pageWidthMM
, pageHeightMM
;
510 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
512 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
513 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
514 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
515 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
517 dc
->SetPen(wxBLACK_PEN
);
518 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
519 (long)rightMarginLogical
, (long)topMarginLogical
);
520 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
521 (long)rightMarginLogical
, (long)bottomMarginLogical
);
523 WritePageHeader(this, dc
, "A header", logUnitsFactor
);
526 // Writes a header on a page. Margin units are in millimetres.
527 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
)
529 static wxFont
*headerFont
= (wxFont
*) NULL
;
532 headerFont
= wxTheFontList
->FindOrCreateFont(16, wxSWISS
, wxNORMAL
, wxBOLD
);
534 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
);