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