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