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);
388 sprintf(buf
, "PAGE %d", page
);
389 dc
->DrawText(buf
, 10, 10);
397 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
399 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
405 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
413 bool MyPrintout::HasPage(int pageNum
)
415 return (pageNum
== 1 || pageNum
== 2);
418 void MyPrintout::DrawPageOne(wxDC
*dc
)
420 /* You might use THIS code if you were scaling
421 * graphics of known size to fit on the page.
425 // We know the graphic is 200x200. If we didn't know this,
426 // we'd need to calculate it.
430 // Let's have at least 50 device units margin
434 // Add the margin to the graphic size
438 // Get the size of the DC in pixels
441 // Calculate a suitable scaling factor
442 float scaleX
=(float)(w
/maxX
);
443 float scaleY
=(float)(h
/maxY
);
445 // Use x or y scaling factor, whichever fits on the DC
446 float actualScale
= wxMin(scaleX
,scaleY
);
448 // Calculate the position on the DC for centring the graphic
449 float posX
= (float)((w
- (200*actualScale
))/2.0);
450 float posY
= (float)((h
- (200*actualScale
))/2.0);
452 // Set the scale and origin
453 dc
->SetUserScale(actualScale
, actualScale
);
454 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
459 void MyPrintout::DrawPageTwo(wxDC
*dc
)
461 /* You might use THIS code to set the printer DC to ROUGHLY reflect
462 * the screen text size. This page also draws lines of actual length 5cm
465 // Get the logical pixels per inch of screen and printer
466 int ppiScreenX
, ppiScreenY
;
467 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
468 int ppiPrinterX
, ppiPrinterY
;
469 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
471 // This scales the DC so that the printout roughly represents the
472 // the screen scaling. The text point size _should_ be the right size
473 // but in fact is too small for some reason. This is a detail that will
474 // need to be addressed at some point but can be fudged for the
476 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
478 // Now we have to check in case our real page size is reduced
479 // (e.g. because we're drawing to a print preview memory DC)
480 int pageWidth
, pageHeight
;
483 GetPageSizePixels(&pageWidth
, &pageHeight
);
485 // If printer pageWidth == current DC width, then this doesn't
486 // change. But w might be the preview bitmap width, so scale down.
487 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
488 dc
->SetUserScale(overallScale
, overallScale
);
490 // Calculate conversion factor for converting millimetres into
492 // There are approx. 25.1 mm to the inch. There are ppi
493 // device units to the inch. Therefore 1 mm corresponds to
494 // ppi/25.1 device units. We also divide by the
495 // screen-to-printer scaling factor, because we need to
496 // unscale to pass logical units to DrawLine.
498 // Draw 50 mm by 50 mm L shape
499 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.1));
500 float logUnits
= (float)(50*logUnitsFactor
);
501 dc
->SetPen(* wxBLACK_PEN
);
502 dc
->DrawLine(50, 50, (long)(50.0 + logUnits
), 50);
503 dc
->DrawLine(50, 50, 50, (long)(50.0 + logUnits
));
505 dc
->SetFont(* wxGetApp().m_testFont
);
506 dc
->SetBackgroundMode(wxTRANSPARENT
);
508 dc
->DrawText("Some test text", 200, 200 );
513 int rightMargin
= 20;
515 int bottomMargin
= 20;
517 int pageWidthMM
, pageHeightMM
;
518 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
520 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
521 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
522 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
523 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
525 dc
->SetPen(wxBLACK_PEN
);
526 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
527 (long)rightMarginLogical
, (long)topMarginLogical
);
528 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
529 (long)rightMarginLogical
, (long)bottomMarginLogical
);
531 WritePageHeader(this, dc
, "A header", logUnitsFactor
);
534 // Writes a header on a page. Margin units are in millimetres.
535 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
)
537 static wxFont
*headerFont
= (wxFont
*) NULL
;
540 headerFont
= wxTheFontList
->FindOrCreateFont(16, wxSWISS
, wxNORMAL
, wxBOLD
);
542 dc
->SetFont(headerFont
);
544 int pageWidthMM
, pageHeightMM
;
546 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
550 int rightMargin
= 10;
552 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
553 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
554 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
556 long xExtent
, yExtent
;
557 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
558 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
559 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
561 dc
->SetPen(* wxBLACK_PEN
);
562 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
563 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);