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