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 MyFrame
*frame
= (MyFrame
*) NULL
;
52 int orientation
= wxPORTRAIT
;
57 #if defined(__WXGTK__) || defined(__WXMOTIF__)
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
82 frame
->SetIcon(wxIcon("mondrian"));
85 // frame->SetIcon(wxIcon(mondrian_bits, mondrian_width, mondrian_height));
89 wxMenu
*file_menu
= new wxMenu
;
91 file_menu
->Append(WXPRINT_PRINT
, "&Print...", "Print");
92 file_menu
->Append(WXPRINT_PRINT_SETUP
, "Print &Setup...", "Setup printer properties");
93 file_menu
->Append(WXPRINT_PAGE_SETUP
, "Page Set&up...", "Page setup");
94 file_menu
->Append(WXPRINT_PREVIEW
, "Print Pre&view", "Preview");
97 wxAcceleratorEntry entries
[1];
98 entries
[0].Set(wxACCEL_CTRL
, (int) 'V', WXPRINT_PREVIEW
);
99 wxAcceleratorTable
accel(1, entries
);
100 frame
->SetAcceleratorTable(accel
);
102 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
103 file_menu
->AppendSeparator();
104 file_menu
->Append(WXPRINT_PRINT_PS
, "Print PostScript...", "Print (PostScript)");
105 file_menu
->Append(WXPRINT_PRINT_SETUP_PS
, "Print Setup PostScript...", "Setup printer properties (PostScript)");
106 file_menu
->Append(WXPRINT_PAGE_SETUP_PS
, "Page Setup PostScript...", "Page setup (PostScript)");
107 file_menu
->Append(WXPRINT_PREVIEW_PS
, "Print Preview PostScript", "Preview (PostScript)");
109 file_menu
->AppendSeparator();
110 file_menu
->Append(WXPRINT_QUIT
, "E&xit", "Exit program");
112 wxMenu
*help_menu
= new wxMenu
;
113 help_menu
->Append(WXPRINT_ABOUT
, "&About", "About this demo");
115 wxMenuBar
*menu_bar
= new wxMenuBar
;
117 menu_bar
->Append(file_menu
, "&File");
118 menu_bar
->Append(help_menu
, "&Help");
120 // Associate the menu bar with the frame
121 frame
->SetMenuBar(menu_bar
);
123 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100), wxRETAINED
|wxHSCROLL
|wxVSCROLL
);
125 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
126 canvas
->SetScrollbars(20, 20, 50, 50);
128 frame
->canvas
= canvas
;
130 frame
->Centre(wxBOTH
);
133 frame
->SetStatusText("Printing demo");
140 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
141 EVT_MENU(WXPRINT_QUIT
, MyFrame
::OnExit
)
142 EVT_MENU(WXPRINT_PRINT
, MyFrame
::OnPrint
)
143 EVT_MENU(WXPRINT_PREVIEW
, MyFrame
::OnPrintPreview
)
144 EVT_MENU(WXPRINT_PRINT_SETUP
, MyFrame
::OnPrintSetup
)
145 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame
::OnPageSetup
)
146 EVT_MENU(WXPRINT_ABOUT
, MyFrame
::OnPrintAbout
)
147 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
148 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame
::OnPrintPS
)
149 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame
::OnPrintPreviewPS
)
150 EVT_MENU(WXPRINT_PRINT_SETUP_PS
, MyFrame
::OnPrintSetupPS
)
151 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame
::OnPageSetupPS
)
155 // Define my frame constructor
156 MyFrame
::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
157 wxFrame(frame
, -1, title
, pos
, size
)
159 canvas
= (MyCanvas
*) NULL
;
162 void MyFrame
::OnExit(wxCommandEvent
& WXUNUSED(event
))
167 void MyFrame
::OnPrint(wxCommandEvent
& WXUNUSED(event
))
170 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
172 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
175 MyPrintout
printout("My printout");
176 if (!printer
.Print(this, &printout
, TRUE
))
177 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
);
180 void MyFrame
::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
183 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
185 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
187 wxPrintData printData
;
188 printData
.SetOrientation(orientation
);
190 // Pass two printout objects: for preview, and possible printing.
191 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
195 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK
);
199 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
200 frame
->Centre(wxBOTH
);
205 void MyFrame
::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
208 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
210 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
213 data
.SetOrientation(orientation
);
216 wxPrintDialog
printerDialog(this, & data
);
218 wxGenericPrintDialog
printerDialog(this, & data
);
220 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
221 printerDialog
.ShowModal();
223 orientation
= printerDialog
.GetPrintData().GetOrientation();
226 void MyFrame
::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
229 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
231 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
233 wxPageSetupData data
;
234 data
.SetOrientation(orientation
);
237 wxPageSetupDialog
pageSetupDialog(this, & data
);
239 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
241 pageSetupDialog
.ShowModal();
243 data
= pageSetupDialog
.GetPageSetupData();
244 orientation
= data
.GetOrientation();
247 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
248 void MyFrame
::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
250 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
252 wxPostScriptPrinter printer
;
253 MyPrintout
printout("My printout");
254 printer
.Print(this, &printout
, TRUE
);
257 void MyFrame
::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
259 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
261 wxPrintData printData
;
262 printData
.SetOrientation(orientation
);
264 // Pass two printout objects: for preview, and possible printing.
265 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
266 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
267 frame
->Centre(wxBOTH
);
272 void MyFrame
::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
274 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
277 data
.SetOrientation(orientation
);
279 wxGenericPrintDialog
printerDialog(this, & data
);
280 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
281 printerDialog
.ShowModal();
283 orientation
= printerDialog
.GetPrintData().GetOrientation();
286 void MyFrame
::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
288 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
290 wxPageSetupData data
;
291 data
.SetOrientation(orientation
);
293 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
294 pageSetupDialog
.ShowModal();
296 orientation
= pageSetupDialog
.GetPageSetupData().GetOrientation();
301 void MyFrame
::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
303 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
304 "About wxWindows printing demo", wxOK
|wxCENTRE
);
307 void MyFrame
::Draw(wxDC
& dc
)
309 dc
.SetFont(* wxGetApp().m_testFont
);
311 dc
.SetBackgroundMode(wxTRANSPARENT
);
313 dc
.SetBrush(* wxCYAN_BRUSH
);
314 dc
.SetPen(* wxRED_PEN
);
316 dc
.DrawRectangle(0, 30, 200, 100);
317 dc
.DrawText("Rectangle 200 by 100", 40, 40);
319 dc
.DrawEllipse(50, 140, 100, 50);
321 dc
.DrawText("Test message: this is in 10 point text", 10, 180);
323 dc
.SetPen(* wxBLACK_PEN
);
324 dc
.DrawLine(0, 0, 200, 200);
325 dc
.DrawLine(200, 0, 0, 200);
327 #if defined(__WXGTK__) || defined(__WXMOTIF__)
328 wxIcon
my_icon( folder_xpm
);
329 #elif defined(__WXMSW__)
330 wxIcon
my_icon( "mondrian" );
332 #error "Platform not supported."
335 dc
.DrawIcon( my_icon
, 100, 100);
338 void MyFrame
::OnSize(wxSizeEvent
& event
)
340 wxFrame
::OnSize(event
);
343 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
344 EVT_MOUSE_EVENTS(MyCanvas
::OnEvent
)
347 // Define a constructor for my canvas
348 MyCanvas
::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
349 wxScrolledWindow(frame
, -1, pos
, size
, style
)
353 MyCanvas
::~MyCanvas(void)
357 // Define the repainting behaviour
358 void MyCanvas
::OnDraw(wxDC
& dc
)
363 void MyCanvas
::OnEvent(wxMouseEvent
& WXUNUSED(event
))
367 bool MyFrame
::OnClose(void)
370 delete wxGetApp().m_testFont
;
375 bool MyPrintout
::OnPrintPage(int page
)
385 dc
->SetDeviceOrigin(0, 0);
386 dc
->SetUserScale(1.0, 1.0);
389 sprintf(buf
, "PAGE %d", page
);
390 dc
->DrawText(buf
, 10, 10);
398 bool MyPrintout
::OnBeginDocument(int startPage
, int endPage
)
400 if (!wxPrintout
::OnBeginDocument(startPage
, endPage
))
406 void MyPrintout
::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
414 bool MyPrintout
::HasPage(int pageNum
)
416 return (pageNum
== 1 || pageNum
== 2);
419 void MyPrintout
::DrawPageOne(wxDC
*dc
)
421 /* You might use THIS code if you were scaling
422 * graphics of known size to fit on the page.
426 // We know the graphic is 200x200. If we didn't know this,
427 // we'd need to calculate it.
431 // Let's have at least 50 device units margin
435 // Add the margin to the graphic size
439 // Get the size of the DC in pixels
442 // Calculate a suitable scaling factor
443 float scaleX
=(float)(w
/maxX
);
444 float scaleY
=(float)(h
/maxY
);
446 // Use x or y scaling factor, whichever fits on the DC
447 float actualScale
= wxMin(scaleX
,scaleY
);
449 // Calculate the position on the DC for centring the graphic
450 float posX
= (float)((w
- (200*actualScale
))/2.0);
451 float posY
= (float)((h
- (200*actualScale
))/2.0);
453 // Set the scale and origin
454 dc
->SetUserScale(actualScale
, actualScale
);
455 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
460 void MyPrintout
::DrawPageTwo(wxDC
*dc
)
462 /* You might use THIS code to set the printer DC to ROUGHLY reflect
463 * the screen text size. This page also draws lines of actual length 5cm
466 // Get the logical pixels per inch of screen and printer
467 int ppiScreenX
, ppiScreenY
;
468 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
469 int ppiPrinterX
, ppiPrinterY
;
470 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
472 // This scales the DC so that the printout roughly represents the
473 // the screen scaling. The text point size _should_ be the right size
474 // but in fact is too small for some reason. This is a detail that will
475 // need to be addressed at some point but can be fudged for the
477 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
479 // Now we have to check in case our real page size is reduced
480 // (e.g. because we're drawing to a print preview memory DC)
481 int pageWidth
, pageHeight
;
484 GetPageSizePixels(&pageWidth
, &pageHeight
);
486 // If printer pageWidth == current DC width, then this doesn't
487 // change. But w might be the preview bitmap width, so scale down.
488 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
489 dc
->SetUserScale(overallScale
, overallScale
);
491 // Calculate conversion factor for converting millimetres into
493 // There are approx. 25.1 mm to the inch. There are ppi
494 // device units to the inch. Therefore 1 mm corresponds to
495 // ppi/25.1 device units. We also divide by the
496 // screen-to-printer scaling factor, because we need to
497 // unscale to pass logical units to DrawLine.
499 // Draw 50 mm by 50 mm L shape
500 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.1));
501 float logUnits
= (float)(50*logUnitsFactor
);
502 dc
->SetPen(* wxBLACK_PEN
);
503 dc
->DrawLine(50, 250, (long)(50.0 + logUnits
), 250);
504 dc
->DrawLine(50, 250, 50, (long)(250.0 + logUnits
));
506 dc
->SetFont(* wxGetApp().m_testFont
);
507 dc
->SetBackgroundMode(wxTRANSPARENT
);
509 dc
->DrawText("Some test text", 200, 200 );
514 int rightMargin
= 20;
516 int bottomMargin
= 20;
518 int pageWidthMM
, pageHeightMM
;
519 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
521 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
522 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
523 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
524 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
526 dc
->SetPen(wxRED_PEN
);
527 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
528 (long)rightMarginLogical
, (long)topMarginLogical
);
529 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
530 (long)rightMarginLogical
, (long)bottomMarginLogical
);
532 WritePageHeader(this, dc
, "A header", logUnitsFactor
);
535 // Writes a header on a page. Margin units are in millimetres.
536 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
)
539 static wxFont *headerFont = (wxFont *) NULL;
542 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
544 dc->SetFont(headerFont);
547 int pageWidthMM
, pageHeightMM
;
549 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
553 int rightMargin
= 10;
555 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
556 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
557 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
559 long xExtent
, yExtent
;
560 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
561 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
562 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
564 dc
->SetPen(* wxBLACK_PEN
);
565 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
566 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);