]> git.saurik.com Git - wxWidgets.git/blame - samples/printing/printing.cpp
1. corrections for compilation of the wxBase apps with wxApp
[wxWidgets.git] / samples / printing / printing.cpp
CommitLineData
6adaedf0
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: printing.cpp
3// Purpose: Printing demo for wxWindows
4// Author: Julian Smart
5// Modified by:
6// Created: 1995
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
c801d85f
KB
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
ad813b00 28#error You must set wxUSE_PRINTING_ARCHITECTURE to 1 in 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;
7bcb11d3
JS
55// int orientation = wxPORTRAIT;
56
57// Global print data, to remember settings during the session
58wxPrintData *g_printData = (wxPrintData*) NULL ;
59
60// Global page setup data
61wxPageSetupData* g_pageSetupData = (wxPageSetupData*) NULL;
c801d85f
KB
62
63// Main proc
64IMPLEMENT_APP(MyApp)
65
c801d85f
KB
66// Writes a header on a page. Margin units are in millimetres.
67bool WritePageHeader(wxPrintout *printout, wxDC *dc, char *text, float mmToLogical);
68
69MyApp::MyApp()
70{
71}
72
73// The `main program' equivalent, creating the windows and returning the
74// main frame
75bool MyApp::OnInit(void)
76{
7bcb11d3
JS
77 m_testFont = new wxFont(10, wxSWISS, wxNORMAL, wxNORMAL);
78
79 g_printData = new wxPrintData;
80 g_pageSetupData = new wxPageSetupDialogData;
c801d85f 81
7bcb11d3
JS
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.
c801d85f 85
7bcb11d3
JS
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
031b2a7b
RR
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
dfad0599 113#if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
7bcb11d3
JS
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)");
c801d85f 119#endif
7bcb11d3
JS
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;
c801d85f
KB
149}
150
e3065973
JS
151int MyApp::OnExit()
152{
7bcb11d3
JS
153 delete wxGetApp().m_testFont;
154 delete g_printData;
155 delete g_pageSetupData;
156 return 1;
e3065973
JS
157}
158
c801d85f 159BEGIN_EVENT_TABLE(MyFrame, wxFrame)
7bcb11d3
JS
160EVT_MENU(WXPRINT_QUIT, MyFrame::OnExit)
161EVT_MENU(WXPRINT_PRINT, MyFrame::OnPrint)
162EVT_MENU(WXPRINT_PREVIEW, MyFrame::OnPrintPreview)
163EVT_MENU(WXPRINT_PRINT_SETUP, MyFrame::OnPrintSetup)
164EVT_MENU(WXPRINT_PAGE_SETUP, MyFrame::OnPageSetup)
165EVT_MENU(WXPRINT_ABOUT, MyFrame::OnPrintAbout)
dfad0599 166#if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
7bcb11d3
JS
167EVT_MENU(WXPRINT_PRINT_PS, MyFrame::OnPrintPS)
168EVT_MENU(WXPRINT_PREVIEW_PS, MyFrame::OnPrintPreviewPS)
169EVT_MENU(WXPRINT_PRINT_SETUP_PS, MyFrame::OnPrintSetupPS)
170EVT_MENU(WXPRINT_PAGE_SETUP_PS, MyFrame::OnPageSetupPS)
dfad0599 171#endif
c801d85f
KB
172END_EVENT_TABLE()
173
174// Define my frame constructor
175MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
7bcb11d3 176wxFrame(frame, -1, title, pos, size)
c801d85f 177{
7bcb11d3 178 canvas = (MyCanvas *) NULL;
c801d85f
KB
179}
180
e3e65dac 181void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
c801d85f 182{
7bcb11d3 183 Close(TRUE);
c801d85f
KB
184}
185
e3e65dac 186void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
c801d85f 187{
7bcb11d3
JS
188 wxPrintDialogData printDialogData(* g_printData);
189
190 wxPrinter printer(& printDialogData);
191 MyPrintout printout("My printout");
192 if (!printer.Print(this, &printout, TRUE))
c801d85f 193 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK);
7bcb11d3
JS
194 else
195 {
196 (*g_printData) = printer.GetPrintDialogData().GetPrintData();
197 }
c801d85f
KB
198}
199
e3e65dac 200void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
c801d85f 201{
7bcb11d3
JS
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 {
c801d85f
KB
207 delete preview;
208 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
209 return;
7bcb11d3
JS
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);
c801d85f
KB
216}
217
e3e65dac 218void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
c801d85f 219{
7bcb11d3
JS
220 wxPrintDialogData printDialogData(* g_printData);
221 wxPrintDialog printerDialog(this, & printDialogData);
222
223 printerDialog.GetPrintDialogData().SetSetupDialog(TRUE);
224 printerDialog.ShowModal();
c801d85f 225
7bcb11d3 226 (*g_printData) = printerDialog.GetPrintDialogData().GetPrintData();
c801d85f
KB
227}
228
e3e65dac 229void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
c801d85f 230{
7bcb11d3 231 (*g_pageSetupData) = * g_printData;
c801d85f 232
7bcb11d3
JS
233 wxPageSetupDialog pageSetupDialog(this, g_pageSetupData);
234 pageSetupDialog.ShowModal();
235
236 (*g_printData) = pageSetupDialog.GetPageSetupData().GetPrintData();
237 (*g_pageSetupData) = pageSetupDialog.GetPageSetupData();
c801d85f
KB
238}
239
dfad0599
JS
240#if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
241void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
242{
7bcb11d3
JS
243 wxPostScriptPrinter printer(g_printData);
244 MyPrintout printout("My printout");
245 printer.Print(this, &printout, TRUE);
246
247 (*g_printData) = printer.GetPrintData();
dfad0599
JS
248}
249
250void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
251{
7bcb11d3
JS
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);
dfad0599
JS
259}
260
e3e65dac 261void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
c801d85f 262{
7bcb11d3
JS
263 wxPrintDialogData printDialogData(* g_printData);
264 wxGenericPrintDialog printerDialog(this, & printDialogData);
c801d85f 265
7bcb11d3
JS
266 printerDialog.GetPrintDialogData().SetSetupDialog(TRUE);
267 printerDialog.ShowModal();
c801d85f 268
7bcb11d3 269 (*g_printData) = printerDialog.GetPrintDialogData().GetPrintData();
c801d85f
KB
270}
271
e3e65dac 272void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
c801d85f 273{
7bcb11d3 274 (*g_pageSetupData) = * g_printData;
c801d85f 275
7bcb11d3
JS
276 wxGenericPageSetupDialog pageSetupDialog(this, g_pageSetupData);
277 pageSetupDialog.ShowModal();
c801d85f 278
7bcb11d3
JS
279 (*g_printData) = pageSetupDialog.GetPageSetupData().GetPrintData();
280 (*g_pageSetupData) = pageSetupDialog.GetPageSetupData();
c801d85f 281}
dfad0599
JS
282#endif
283
c801d85f 284
e3e65dac 285void MyFrame::OnPrintAbout(wxCommandEvent& WXUNUSED(event))
c801d85f 286{
7bcb11d3
JS
287 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
288 "About wxWindows printing demo", wxOK|wxCENTRE);
c801d85f
KB
289}
290
291void MyFrame::Draw(wxDC& dc)
292{
6adaedf0
JS
293 dc.SetBackground(*wxWHITE_BRUSH);
294 dc.Clear();
7bcb11d3
JS
295 dc.SetFont(* wxGetApp().m_testFont);
296
297 dc.SetBackgroundMode(wxTRANSPARENT);
298
299 dc.SetBrush(* wxCYAN_BRUSH);
300 dc.SetPen(* wxRED_PEN);
301
302 dc.DrawRectangle(0, 30, 200, 100);
303 dc.DrawText("Rectangle 200 by 100", 40, 40);
304
305 dc.DrawEllipse(50, 140, 100, 50);
306
307 dc.DrawText("Test message: this is in 10 point text", 10, 180);
308
309 dc.SetPen(* wxBLACK_PEN);
310 dc.DrawLine(0, 0, 200, 200);
311 dc.DrawLine(200, 0, 0, 200);
312
313 wxIcon my_icon = wxICON(mondrian) ;
314
315 dc.DrawIcon( my_icon, 100, 100);
c801d85f
KB
316}
317
e3e65dac 318void MyFrame::OnSize(wxSizeEvent& event )
c801d85f
KB
319{
320 wxFrame::OnSize(event);
321}
322
323BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
7bcb11d3 324EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
c801d85f
KB
325END_EVENT_TABLE()
326
327// Define a constructor for my canvas
328MyCanvas::MyCanvas(wxFrame *frame, const wxPoint& pos, const wxSize& size, long style):
7bcb11d3 329 wxScrolledWindow(frame, -1, pos, size, style)
c801d85f 330{
7bcb11d3 331 SetBackgroundColour(* wxWHITE);
c801d85f
KB
332}
333
334MyCanvas::~MyCanvas(void)
335{
336}
337
338// Define the repainting behaviour
339void MyCanvas::OnDraw(wxDC& dc)
340{
7bcb11d3 341 frame->Draw(dc);
c801d85f
KB
342}
343
e3e65dac 344void MyCanvas::OnEvent(wxMouseEvent& WXUNUSED(event))
c801d85f
KB
345{
346}
347
c801d85f
KB
348bool MyPrintout::OnPrintPage(int page)
349{
7bcb11d3
JS
350 wxDC *dc = GetDC();
351 if (dc)
352 {
353 if (page == 1)
354 DrawPageOne(dc);
355 else if (page == 2)
356 DrawPageTwo(dc);
357
358 dc->SetDeviceOrigin(0, 0);
359 dc->SetUserScale(1.0, 1.0);
360
361 char buf[200];
362 sprintf(buf, "PAGE %d", page);
363 dc->DrawText(buf, 10, 10);
364
365 return TRUE;
366 }
367 else
368 return FALSE;
c801d85f
KB
369}
370
371bool MyPrintout::OnBeginDocument(int startPage, int endPage)
372{
7bcb11d3
JS
373 if (!wxPrintout::OnBeginDocument(startPage, endPage))
374 return FALSE;
375
376 return TRUE;
c801d85f
KB
377}
378
379void MyPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
380{
7bcb11d3
JS
381 *minPage = 1;
382 *maxPage = 2;
383 *selPageFrom = 1;
384 *selPageTo = 2;
c801d85f
KB
385}
386
387bool MyPrintout::HasPage(int pageNum)
388{
7bcb11d3 389 return (pageNum == 1 || pageNum == 2);
c801d85f
KB
390}
391
392void MyPrintout::DrawPageOne(wxDC *dc)
393{
394/* You might use THIS code if you were scaling
7bcb11d3
JS
395* graphics of known size to fit on the page.
396 */
397 int w, h;
398
399 // We know the graphic is 200x200. If we didn't know this,
400 // we'd need to calculate it.
401 float maxX = 200;
402 float maxY = 200;
403
404 // Let's have at least 50 device units margin
405 float marginX = 50;
406 float marginY = 50;
407
408 // Add the margin to the graphic size
409 maxX += (2*marginX);
410 maxY += (2*marginY);
411
412 // Get the size of the DC in pixels
413 dc->GetSize(&w, &h);
414
415 // Calculate a suitable scaling factor
416 float scaleX=(float)(w/maxX);
417 float scaleY=(float)(h/maxY);
418
419 // Use x or y scaling factor, whichever fits on the DC
420 float actualScale = wxMin(scaleX,scaleY);
421
422 // Calculate the position on the DC for centring the graphic
423 float posX = (float)((w - (200*actualScale))/2.0);
424 float posY = (float)((h - (200*actualScale))/2.0);
425
426 // Set the scale and origin
427 dc->SetUserScale(actualScale, actualScale);
428 dc->SetDeviceOrigin( (long)posX, (long)posY );
429
430 frame->Draw(*dc);
c801d85f
KB
431}
432
433void MyPrintout::DrawPageTwo(wxDC *dc)
434{
435/* You might use THIS code to set the printer DC to ROUGHLY reflect
7bcb11d3
JS
436* the screen text size. This page also draws lines of actual length 5cm
437* on the page.
438 */
439 // Get the logical pixels per inch of screen and printer
440 int ppiScreenX, ppiScreenY;
441 GetPPIScreen(&ppiScreenX, &ppiScreenY);
442 int ppiPrinterX, ppiPrinterY;
443 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
444
445 // This scales the DC so that the printout roughly represents the
446 // the screen scaling. The text point size _should_ be the right size
447 // but in fact is too small for some reason. This is a detail that will
448 // need to be addressed at some point but can be fudged for the
449 // moment.
450 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
451
452 // Now we have to check in case our real page size is reduced
453 // (e.g. because we're drawing to a print preview memory DC)
454 int pageWidth, pageHeight;
455 int w, h;
456 dc->GetSize(&w, &h);
457 GetPageSizePixels(&pageWidth, &pageHeight);
458
459 // If printer pageWidth == current DC width, then this doesn't
460 // change. But w might be the preview bitmap width, so scale down.
461 float overallScale = scale * (float)(w/(float)pageWidth);
462 dc->SetUserScale(overallScale, overallScale);
463
464 // Calculate conversion factor for converting millimetres into
465 // logical units.
466 // There are approx. 25.1 mm to the inch. There are ppi
467 // device units to the inch. Therefore 1 mm corresponds to
468 // ppi/25.1 device units. We also divide by the
469 // screen-to-printer scaling factor, because we need to
470 // unscale to pass logical units to DrawLine.
471
472 // Draw 50 mm by 50 mm L shape
473 float logUnitsFactor = (float)(ppiPrinterX/(scale*25.1));
474 float logUnits = (float)(50*logUnitsFactor);
475 dc->SetPen(* wxBLACK_PEN);
476 dc->DrawLine(50, 250, (long)(50.0 + logUnits), 250);
477 dc->DrawLine(50, 250, 50, (long)(250.0 + logUnits));
478
479 dc->SetFont(* wxGetApp().m_testFont);
480 dc->SetBackgroundMode(wxTRANSPARENT);
481
482 dc->DrawText("Some test text", 200, 200 );
483
d57bf685
VS
484 { // GetTextExtent demo:
485 wxString words[7] = {"This ", "is ", "GetTextExtent ", "testing ", "string. ", "Enjoy ", "it!"};
486 long w, h;
487 long x = 200, y= 250;
488 wxFont fnt(15, wxSWISS, wxNORMAL, wxNORMAL);
489
490 dc->SetFont(fnt);
491 for (int i = 0; i < 7; i++) {
492 dc->GetTextExtent(words[i], &w, &h);
493 dc->DrawRectangle(x, y, w, h);
494 dc->DrawText(words[i], x, y);
495 x += w;
496 }
497 dc->SetFont(* wxGetApp().m_testFont);
498 }
499
7bcb11d3
JS
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
515 dc->SetPen(* wxRED_PEN);
516 dc->DrawLine( (long)leftMarginLogical, (long)topMarginLogical,
517 (long)rightMarginLogical, (long)topMarginLogical);
518 dc->DrawLine( (long)leftMarginLogical, (long)bottomMarginLogical,
519 (long)rightMarginLogical, (long)bottomMarginLogical);
520
521 WritePageHeader(this, dc, "A header", logUnitsFactor);
c801d85f
KB
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/*
7bcb11d3
JS
528static wxFont *headerFont = (wxFont *) NULL;
529if (!headerFont)
530{
531headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
532}
533dc->SetFont(headerFont);
534 */
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));
551 dc->DrawText(text, (long)xPos, (long)topMarginLogical);
552
553 dc->SetPen(* wxBLACK_PEN);
554 dc->DrawLine( (long)leftMarginLogical, (long)(topMarginLogical+yExtent),
555 (long)rightMarginLogical, (long)topMarginLogical+yExtent );
556
557 return TRUE;
c801d85f 558}