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