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