]> git.saurik.com Git - wxWidgets.git/blob - samples/printing/printing.cpp
2017286a372afa0b1318de608f110ad4ecb512cd
[wxWidgets.git] / samples / printing / printing.cpp
1 /*
2 * File: printing.cc
3 * Purpose: Printing demo for wxWindows class library
4 * Author: Julian Smart
5 * Created: 1995
6 * Updated:
7 * Copyright: (c) 1995, AIAI, University of Edinburgh
8 */
9
10 /* static const char sccsid[] = "%W% %G%"; */
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #include "wx/postscrp.h"
26 #endif
27
28 #if !USE_PRINTING_ARCHITECTURE
29 #error You must set USE_PRINTING_ARCHITECTURE to 1 in wx_setup.h to compile this demo.
30 #endif
31
32 #include <ctype.h>
33 #include "wx/metafile.h"
34 #include "wx/print.h"
35 #include "wx/printdlg.h"
36 #include "wx/generic/printps.h"
37 #include "wx/generic/prntdlgg.h"
38
39 #include "printing.h"
40
41 // Declare a frame
42 MyFrame *frame = NULL;
43 int orientation = wxPORTRAIT;
44
45 // Main proc
46 IMPLEMENT_APP(MyApp)
47
48 // Must initialise these in OnInit, not statically
49 wxPen *red_pen;
50 wxFont *labelFont;
51 wxFont *itemFont;
52
53 float zoom_factor = 1.0;
54
55 #ifdef __X__
56 #include "aiai.xbm"
57 #endif
58
59 #ifdef __WXGTK__
60 #include "folder.xpm"
61 #endif
62
63
64 // Writes a header on a page. Margin units are in millimetres.
65 bool WritePageHeader(wxPrintout *printout, wxDC *dc, char *text, float mmToLogical);
66
67 MyApp::MyApp()
68 {
69 }
70
71 // The `main program' equivalent, creating the windows and returning the
72 // main frame
73 bool MyApp::OnInit(void)
74 {
75 // Create a red pen
76 red_pen = new wxPen("RED", 3, wxSOLID);
77
78 // Create a small font
79 itemFont = new wxFont(11, wxROMAN, wxNORMAL, wxNORMAL);
80 labelFont = new wxFont(12, wxROMAN, wxITALIC, wxBOLD);
81
82 // Create the main frame window
83 frame = new MyFrame(NULL, "wxWindows Printing Demo", wxPoint(0, 0), wxSize(400, 400));
84
85 // Give it a status line
86 frame->CreateStatusBar(2);
87
88 // Load icon and bitmap
89 #ifdef __WXMSW__
90 frame->SetIcon(wxIcon("aiai_icn"));
91 #endif
92 #ifdef __X__
93 frame->SetIcon(wxIcon(aiai_bits, aiai_width, aiai_height));
94 #endif
95
96 // Make a menubar
97 wxMenu *file_menu = new wxMenu;
98
99 file_menu->Append(WXPRINT_PRINT, "&Print...", "Print");
100 file_menu->Append(WXPRINT_PRINT_SETUP, "Print &Setup...", "Setup printer properties");
101 file_menu->Append(WXPRINT_PAGE_SETUP, "Page Set&up...", "Page setup");
102 file_menu->Append(WXPRINT_PREVIEW, "Print Pre&view", "Preview");
103
104 #ifdef __WXMSW__
105 file_menu->AppendSeparator();
106 file_menu->Append(WXPRINT_PRINT_PS, "Print PostScript...", "Print (PostScript)");
107 file_menu->Append(WXPRINT_PRINT_SETUP_PS, "Print Setup PostScript...", "Setup printer properties (PostScript)");
108 file_menu->Append(WXPRINT_PAGE_SETUP_PS, "Page Setup PostScript...", "Page setup (PostScript)");
109 file_menu->Append(WXPRINT_PREVIEW_PS, "Print Preview PostScript", "Preview (PostScript)");
110 #endif
111 file_menu->AppendSeparator();
112 file_menu->Append(WXPRINT_QUIT, "E&xit", "Exit program");
113
114 wxMenu *help_menu = new wxMenu;
115 help_menu->Append(WXPRINT_ABOUT, "&About", "About this demo");
116
117 wxMenuBar *menu_bar = new wxMenuBar;
118
119 menu_bar->Append(file_menu, "&File");
120 menu_bar->Append(help_menu, "&Help");
121
122 // Associate the menu bar with the frame
123 frame->SetMenuBar(menu_bar);
124
125 MyCanvas *canvas = new MyCanvas(frame, wxPoint(0, 0), wxSize(100, 100), wxRETAINED|wxHSCROLL|wxVSCROLL);
126
127 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
128 canvas->SetScrollbars(20, 20, 50, 50);
129
130 // This ensures that the fonts get created as _screen_
131 // fonts, not printer fonts.
132 canvas->SetFont(itemFont);
133 canvas->SetFont(labelFont);
134
135 frame->canvas = canvas;
136
137 frame->Centre(wxBOTH);
138 frame->Show(TRUE);
139
140 frame->SetStatusText("Printing demo");
141
142 SetTopWindow(frame);
143
144 return TRUE;
145 }
146
147 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
148 EVT_MENU(WXPRINT_QUIT, MyFrame::OnExit)
149 EVT_MENU(WXPRINT_PRINT, MyFrame::OnPrint)
150 EVT_MENU(WXPRINT_PREVIEW, MyFrame::OnPrintPreview)
151 EVT_MENU(WXPRINT_PRINT_SETUP, MyFrame::OnPrintSetup)
152 EVT_MENU(WXPRINT_PAGE_SETUP, MyFrame::OnPageSetup)
153 EVT_MENU(WXPRINT_PRINT_PS, MyFrame::OnPrintPS)
154 EVT_MENU(WXPRINT_PREVIEW_PS, MyFrame::OnPrintPreviewPS)
155 EVT_MENU(WXPRINT_PRINT_SETUP_PS, MyFrame::OnPrintSetupPS)
156 EVT_MENU(WXPRINT_PAGE_SETUP_PS, MyFrame::OnPageSetupPS)
157 EVT_MENU(WXPRINT_ABOUT, MyFrame::OnPrintAbout)
158 END_EVENT_TABLE()
159
160 // Define my frame constructor
161 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
162 wxFrame(frame, -1, title, pos, size)
163 {
164 canvas = NULL;
165 }
166
167 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
168 {
169 Close(TRUE);
170 }
171
172 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
173 {
174 #ifdef __WXMSW__
175 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
176 #else
177 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
178 #endif
179 wxPrinter printer;
180 MyPrintout printout("My printout");
181 if (!printer.Print(this, &printout, TRUE))
182 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK);
183 }
184
185 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
186 {
187 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
188
189 wxPostScriptPrinter printer;
190 MyPrintout printout("My printout");
191 printer.Print(this, &printout, TRUE);
192 }
193
194 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
195 {
196 #ifdef __WXMSW__
197 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
198 #else
199 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
200 #endif
201 wxPrintData printData;
202 printData.SetOrientation(orientation);
203
204 // Pass two printout objects: for preview, and possible printing.
205 wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printData);
206 if (!preview->Ok())
207 {
208 delete preview;
209 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
210 return;
211 }
212
213 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
214 frame->Centre(wxBOTH);
215 frame->Initialize();
216 frame->Show(TRUE);
217 }
218
219 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
220 {
221 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
222
223 wxPrintData printData;
224 printData.SetOrientation(orientation);
225
226 // Pass two printout objects: for preview, and possible printing.
227 wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printData);
228 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
229 frame->Centre(wxBOTH);
230 frame->Initialize();
231 frame->Show(TRUE);
232 }
233
234 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
235 {
236 #ifdef __WXMSW__
237 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
238 #else
239 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
240 #endif
241 wxPrintData data;
242 data.SetOrientation(orientation);
243
244 #ifdef __WXMSW__
245 wxPrintDialog printerDialog(this, & data);
246 #else
247 wxGenericPrintDialog printerDialog(this, & data);
248 #endif
249 printerDialog.GetPrintData().SetSetupDialog(TRUE);
250 printerDialog.ShowModal();
251
252 orientation = printerDialog.GetPrintData().GetOrientation();
253 }
254
255 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
256 {
257 #ifdef __WXMSW__
258 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
259 #else
260 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
261 #endif
262 wxPageSetupData data;
263 data.SetOrientation(orientation);
264
265 #ifdef __WXMSW__
266 wxPageSetupDialog pageSetupDialog(this, & data);
267 #else
268 wxGenericPageSetupDialog pageSetupDialog(this, & data);
269 #endif
270 pageSetupDialog.ShowModal();
271
272 data = pageSetupDialog.GetPageSetupData();
273 orientation = data.GetOrientation();
274 }
275
276 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
277 {
278 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
279
280 wxPrintData data;
281 data.SetOrientation(orientation);
282
283 wxGenericPrintDialog printerDialog(this, & data);
284 printerDialog.GetPrintData().SetSetupDialog(TRUE);
285 printerDialog.ShowModal();
286
287 orientation = printerDialog.GetPrintData().GetOrientation();
288 }
289
290 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
291 {
292 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
293
294 wxPageSetupData data;
295 data.SetOrientation(orientation);
296
297 wxGenericPageSetupDialog pageSetupDialog(this, & data);
298 pageSetupDialog.ShowModal();
299
300 orientation = pageSetupDialog.GetPageSetupData().GetOrientation();
301 }
302
303 void MyFrame::OnPrintAbout(wxCommandEvent& WXUNUSED(event))
304 {
305 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
306 "About wxWindows printing demo", wxOK|wxCENTRE);
307 }
308
309 void MyFrame::Draw(wxDC& dc)
310 {
311 dc.SetFont(itemFont);
312 dc.SetBackgroundMode(wxTRANSPARENT);
313
314 dc.SetBrush(wxCYAN_BRUSH);
315 dc.SetPen(wxRED_PEN);
316
317 dc.DrawRectangle(0, 30, 200, 100);
318 dc.DrawText("Rectangle 200 by 100", 40, 40);
319
320 dc.DrawEllipse(50, 140, 100, 50);
321
322 dc.DrawText("Test message: this is in 11 point text", 10, 180);
323
324 dc.SetPen(wxBLACK_PEN);
325 dc.DrawLine(0, 0, 200, 200);
326 dc.DrawLine(200, 0, 0, 200);
327
328 wxIcon my_icon( folder_xpm );
329 dc.DrawIcon( my_icon, 100, 100, TRUE );
330 }
331
332 void MyFrame::OnSize(wxSizeEvent& event )
333 {
334 wxFrame::OnSize(event);
335 }
336
337 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
338 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
339 END_EVENT_TABLE()
340
341 // Define a constructor for my canvas
342 MyCanvas::MyCanvas(wxFrame *frame, const wxPoint& pos, const wxSize& size, long style):
343 wxScrolledWindow(frame, -1, pos, size, style)
344 {
345 }
346
347 MyCanvas::~MyCanvas(void)
348 {
349 }
350
351 // Define the repainting behaviour
352 void MyCanvas::OnDraw(wxDC& dc)
353 {
354 frame->Draw(dc);
355 }
356
357 void MyCanvas::OnEvent(wxMouseEvent& WXUNUSED(event))
358 {
359 }
360
361 // Define the behaviour for the frame closing
362 // - must delete all frames except for the main one.
363 bool MyFrame::OnClose(void)
364 {
365 Show(FALSE);
366
367 return TRUE;
368 }
369
370 bool MyPrintout::OnPrintPage(int page)
371 {
372 wxDC *dc = GetDC();
373 if (dc)
374 {
375 if (page == 1)
376 DrawPageOne(dc);
377 else if (page == 2)
378 DrawPageTwo(dc);
379
380 dc->SetDeviceOrigin(0, 0);
381
382 char buf[200];
383 sprintf(buf, "PAGE %d", page);
384 dc->DrawText(buf, 10, 10);
385
386 return TRUE;
387 }
388 else
389 return FALSE;
390 }
391
392 bool MyPrintout::OnBeginDocument(int startPage, int endPage)
393 {
394 if (!wxPrintout::OnBeginDocument(startPage, endPage))
395 return FALSE;
396
397 return TRUE;
398 }
399
400 void MyPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
401 {
402 *minPage = 1;
403 *maxPage = 2;
404 *selPageFrom = 1;
405 *selPageTo = 2;
406 }
407
408 bool MyPrintout::HasPage(int pageNum)
409 {
410 return (pageNum == 1 || pageNum == 2);
411 }
412
413 void MyPrintout::DrawPageOne(wxDC *dc)
414 {
415 /* You might use THIS code if you were scaling
416 * graphics of known size to fit on the page.
417 */
418 int w, h;
419
420 // We know the graphic is 200x200. If we didn't know this,
421 // we'd need to calculate it.
422 float maxX = 200;
423 float maxY = 200;
424
425 // Let's have at least 50 device units margin
426 float marginX = 50;
427 float marginY = 50;
428
429 // Add the margin to the graphic size
430 maxX += (2*marginX);
431 maxY += (2*marginY);
432
433 // Get the size of the DC in pixels
434 dc->GetSize(&w, &h);
435
436 // Calculate a suitable scaling factor
437 float scaleX=(float)(w/maxX);
438 float scaleY=(float)(h/maxY);
439
440 // Use x or y scaling factor, whichever fits on the DC
441 float actualScale = wxMin(scaleX,scaleY);
442
443 // Calculate the position on the DC for centring the graphic
444 float posX = (float)((w - (200*actualScale))/2.0);
445 float posY = (float)((h - (200*actualScale))/2.0);
446
447 // Set the scale and origin
448 dc->SetUserScale(actualScale, actualScale);
449 dc->SetDeviceOrigin( (long)posX, (long)posY );
450
451 frame->Draw(*dc);
452 }
453
454 void MyPrintout::DrawPageTwo(wxDC *dc)
455 {
456 /* You might use THIS code to set the printer DC to ROUGHLY reflect
457 * the screen text size. This page also draws lines of actual length 5cm
458 * on the page.
459 */
460 // Get the logical pixels per inch of screen and printer
461 int ppiScreenX, ppiScreenY;
462 GetPPIScreen(&ppiScreenX, &ppiScreenY);
463 int ppiPrinterX, ppiPrinterY;
464 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
465
466 // This scales the DC so that the printout roughly represents the
467 // the screen scaling. The text point size _should_ be the right size
468 // but in fact is too small for some reason. This is a detail that will
469 // need to be addressed at some point but can be fudged for the
470 // moment.
471 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
472
473 // Now we have to check in case our real page size is reduced
474 // (e.g. because we're drawing to a print preview memory DC)
475 int pageWidth, pageHeight;
476 int w, h;
477 dc->GetSize(&w, &h);
478 GetPageSizePixels(&pageWidth, &pageHeight);
479
480 // If printer pageWidth == current DC width, then this doesn't
481 // change. But w might be the preview bitmap width, so scale down.
482 float overallScale = scale * (float)(w/(float)pageWidth);
483 dc->SetUserScale(overallScale, overallScale);
484
485 // Calculate conversion factor for converting millimetres into
486 // logical units.
487 // There are approx. 25.1 mm to the inch. There are ppi
488 // device units to the inch. Therefore 1 mm corresponds to
489 // ppi/25.1 device units. We also divide by the
490 // screen-to-printer scaling factor, because we need to
491 // unscale to pass logical units to DrawLine.
492
493 // Draw 50 mm by 50 mm L shape
494 float logUnitsFactor = (float)(ppiPrinterX/(scale*25.1));
495 float logUnits = (float)(50*logUnitsFactor);
496 dc->SetPen(wxBLACK_PEN);
497 dc->DrawLine(50, 50, (long)(50.0 + logUnits), 50);
498 dc->DrawLine(50, 50, 50, (long)(50.0 + logUnits));
499
500 dc->SetFont(itemFont);
501 dc->SetBackgroundMode(wxTRANSPARENT);
502
503 dc->DrawText("Some test text", 200, 200 );
504
505 // TESTING
506
507 int leftMargin = 20;
508 int rightMargin = 20;
509 int topMargin = 20;
510 int bottomMargin = 20;
511
512 int pageWidthMM, pageHeightMM;
513 GetPageSizeMM(&pageWidthMM, &pageHeightMM);
514
515 float leftMarginLogical = (float)(logUnitsFactor*leftMargin);
516 float topMarginLogical = (float)(logUnitsFactor*topMargin);
517 float bottomMarginLogical = (float)(logUnitsFactor*(pageHeightMM - bottomMargin));
518 float rightMarginLogical = (float)(logUnitsFactor*(pageWidthMM - rightMargin));
519
520 dc->SetPen(wxBLACK_PEN);
521 dc->DrawLine( (long)leftMarginLogical, (long)topMarginLogical,
522 (long)rightMarginLogical, (long)topMarginLogical);
523 dc->DrawLine( (long)leftMarginLogical, (long)bottomMarginLogical,
524 (long)rightMarginLogical, (long)bottomMarginLogical);
525
526 WritePageHeader(this, dc, "A header", logUnitsFactor);
527 }
528
529 // Writes a header on a page. Margin units are in millimetres.
530 bool WritePageHeader(wxPrintout *printout, wxDC *dc, char *text, float mmToLogical)
531 {
532 static wxFont *headerFont = NULL;
533 if (!headerFont)
534 {
535 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
536 }
537 dc->SetFont(headerFont);
538
539 int pageWidthMM, pageHeightMM;
540
541 printout->GetPageSizeMM(&pageWidthMM, &pageHeightMM);
542
543 int leftMargin = 10;
544 int topMargin = 10;
545 int rightMargin = 10;
546
547 float leftMarginLogical = (float)(mmToLogical*leftMargin);
548 float topMarginLogical = (float)(mmToLogical*topMargin);
549 float rightMarginLogical = (float)(mmToLogical*(pageWidthMM - rightMargin));
550
551 long xExtent, yExtent;
552 dc->GetTextExtent(text, &xExtent, &yExtent);
553 float xPos = (float)(((((pageWidthMM - leftMargin - rightMargin)/2.0)+leftMargin)*mmToLogical) - (xExtent/2.0));
554 dc->DrawText(text, (long)xPos, (long)topMarginLogical);
555
556 dc->SetPen(wxBLACK_PEN);
557 dc->DrawLine( (long)leftMarginLogical, (long)(topMarginLogical+yExtent),
558 (long)rightMarginLogical, (long)topMarginLogical+yExtent );
559
560 return TRUE;
561 }