]> git.saurik.com Git - wxWidgets.git/blame - samples/printing/printing.cpp
Add wxHtmlHelpController::SetShouldPreventAppExit().
[wxWidgets.git] / samples / printing / printing.cpp
CommitLineData
6adaedf0 1/////////////////////////////////////////////////////////////////////////////
f415cab9 2// Name: samples/printing.cpp
be5a51fb 3// Purpose: Printing demo for wxWidgets
6adaedf0 4// Author: Julian Smart
48ed4a89 5// Modified by: Francesco Montorsi
6adaedf0
JS
6// Created: 1995
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
c801d85f 11
c801d85f
KB
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
48ed4a89 16 #pragma hdrstop
c801d85f
KB
17#endif
18
19#ifndef WX_PRECOMP
48ed4a89
FM
20 #include "wx/wx.h"
21 #include "wx/log.h"
c801d85f
KB
22#endif
23
e4b19d9b 24#if !wxUSE_PRINTING_ARCHITECTURE
48ed4a89 25 #error "You must set wxUSE_PRINTING_ARCHITECTURE to 1 in setup.h, and recompile the library."
c801d85f
KB
26#endif
27
dfad0599
JS
28// Set this to 1 if you want to test PostScript printing under MSW.
29// However, you'll also need to edit src/msw/makefile.nt.
30#define wxTEST_POSTSCRIPT_IN_MSW 0
31
c801d85f
KB
32#include <ctype.h>
33#include "wx/metafile.h"
34#include "wx/print.h"
35#include "wx/printdlg.h"
cac7ac30 36#include "wx/image.h"
031b2a7b
RR
37#include "wx/accel.h"
38
dfad0599 39#if wxTEST_POSTSCRIPT_IN_MSW
48ed4a89
FM
40 #include "wx/generic/printps.h"
41 #include "wx/generic/prntdlgg.h"
dfad0599 42#endif
c801d85f 43
7e38ae60 44#if wxUSE_GRAPHICS_CONTEXT
48ed4a89 45 #include "wx/graphics.h"
7e38ae60
RR
46#endif
47
f415cab9 48#ifdef __WXMAC__
5f05e514 49 #include "wx/osx/printdlg.h"
f415cab9
JS
50#endif
51
c801d85f
KB
52#include "printing.h"
53
3cb332c1
VZ
54#if !defined(__WXMSW__) && !defined(__WXPM__)
55 #include "../sample.xpm"
4bc67cc5
RR
56#endif
57
7bcb11d3 58// Global print data, to remember settings during the session
48ed4a89 59wxPrintData *g_printData = NULL;
7bcb11d3
JS
60
61// Global page setup data
48ed4a89 62wxPageSetupDialogData* g_pageSetupData = NULL;
c801d85f 63
c801d85f 64
c801d85f 65
48ed4a89
FM
66// ----------------------------------------------------------------------------
67// MyApp
68// ----------------------------------------------------------------------------
69
70IMPLEMENT_APP(MyApp)
7b25d8e7 71
c801d85f
KB
72bool MyApp::OnInit(void)
73{
45e6e6f8
VZ
74 if ( !wxApp::OnInit() )
75 return false;
76
cac7ac30
RR
77 wxInitAllImageHandlers();
78
48ed4a89
FM
79
80 // init global objects
81 // -------------------
9134af5b 82
7bcb11d3 83 g_printData = new wxPrintData;
48ed4a89 84
d5eaa19f 85 // You could set an initial paper size here
48ed4a89
FM
86#if 0
87 g_printData->SetPaperId(wxPAPER_LETTER); // for Americans
88 g_printData->SetPaperId(wxPAPER_A4); // for everyone else
89#endif
f415cab9 90
7bcb11d3 91 g_pageSetupData = new wxPageSetupDialogData;
48ed4a89 92
f415cab9
JS
93 // copy over initial paper size from print record
94 (*g_pageSetupData) = *g_printData;
48ed4a89
FM
95
96 // Set some initial page margins in mm.
f415cab9
JS
97 g_pageSetupData->SetMarginTopLeft(wxPoint(15, 15));
98 g_pageSetupData->SetMarginBottomRight(wxPoint(15, 15));
9134af5b 99
48ed4a89
FM
100
101 // init local GUI objects
102 // ----------------------
103
104#if 0
105 wxImage image( wxT("test.jpg") );
106 image.SetAlpha();
107 int i,j;
108 for (i = 0; i < image.GetWidth(); i++)
109 for (j = 0; j < image.GetHeight(); j++)
110 image.SetAlpha( i, j, 50 );
111 m_bitmap = image;
112#endif
113 m_angle = 30;
114 m_testFont.Create(10, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
115
116
7bcb11d3 117 // Create the main frame window
48ed4a89
FM
118 // ----------------------------
119
9a83f860 120 MyFrame* frame = new MyFrame((wxFrame *) NULL, wxT("wxWidgets Printing Demo"),
48ed4a89
FM
121 wxPoint(0, 0), wxSize(400, 400));
122
123 frame->Centre(wxBOTH);
124 frame->Show();
125
48ed4a89
FM
126 return true;
127}
128
129int MyApp::OnExit()
130{
131 delete g_printData;
132 delete g_pageSetupData;
133
134 return wxApp::OnExit();
135}
136
137void MyApp::Draw(wxDC&dc)
138{
139 // This routine just draws a bunch of random stuff on the screen so that we
140 // can check that different types of object are being drawn consistently
141 // between the screen image, the print preview image (at various zoom
142 // levels), and the printed page.
143 dc.SetBackground(*wxWHITE_BRUSH);
144 // dc.Clear();
145 dc.SetFont(m_testFont);
146
147 // dc.SetBackgroundMode(wxTRANSPARENT);
148
149 dc.SetPen(*wxBLACK_PEN);
150 dc.SetBrush(*wxLIGHT_GREY_BRUSH);
151
152 dc.DrawRectangle(0, 0, 230, 350);
153 dc.DrawLine(0, 0, 229, 349);
154 dc.DrawLine(229, 0, 0, 349);
155 dc.SetBrush(*wxTRANSPARENT_BRUSH);
156
157 dc.SetBrush(*wxCYAN_BRUSH);
158 dc.SetPen(*wxRED_PEN);
159
160 dc.DrawRoundedRectangle(0, 20, 200, 80, 20);
161
162 dc.DrawText( wxT("Rectangle 200 by 80"), 40, 40);
163
164 dc.SetPen( wxPen(*wxBLACK,0,wxDOT_DASH) );
165 dc.DrawEllipse(50, 140, 100, 50);
166 dc.SetPen(*wxRED_PEN);
167
168 dc.DrawText( wxT("Test message: this is in 10 point text"), 10, 180);
169
170#if wxUSE_UNICODE
171 const char *test = "Hebrew שלום -- Japanese (日本語)";
172 wxString tmp = wxConvUTF8.cMB2WC( test );
173 dc.DrawText( tmp, 10, 200 );
174#endif
175
176 wxPoint points[5];
177 points[0].x = 0;
178 points[0].y = 0;
179 points[1].x = 20;
180 points[1].y = 0;
181 points[2].x = 20;
182 points[2].y = 20;
183 points[3].x = 10;
184 points[3].y = 20;
185 points[4].x = 10;
186 points[4].y = -20;
187 dc.DrawPolygon( 5, points, 20, 250, wxODDEVEN_RULE );
188 dc.DrawPolygon( 5, points, 50, 250, wxWINDING_RULE );
189
190 dc.DrawEllipticArc( 80, 250, 60, 30, 0.0, 270.0 );
191
192 points[0].x = 150;
193 points[0].y = 250;
194 points[1].x = 180;
195 points[1].y = 250;
196 points[2].x = 180;
197 points[2].y = 220;
198 points[3].x = 200;
199 points[3].y = 220;
200 dc.DrawSpline( 4, points );
201
202 dc.DrawArc( 20,10, 10,10, 25,40 );
203
204 wxString str;
205 int i = 0;
206 str.Printf( wxT("---- Text at angle %d ----"), i );
207 dc.DrawRotatedText( str, 100, 300, i );
208
209 i = m_angle;
210 str.Printf( wxT("---- Text at angle %d ----"), i );
211 dc.DrawRotatedText( str, 100, 300, i );
212
3cb332c1 213 wxIcon my_icon = wxICON(sample);
48ed4a89
FM
214
215 dc.DrawIcon( my_icon, 100, 100);
216
217 if (m_bitmap.IsOk())
218 dc.DrawBitmap( m_bitmap, 10, 10 );
219
220#if wxUSE_GRAPHICS_CONTEXT
221 wxGraphicsContext *gc = NULL;
222
223 wxPrinterDC *printer_dc = wxDynamicCast( &dc, wxPrinterDC );
224 if (printer_dc)
225 gc = wxGraphicsContext::Create( *printer_dc );
226
227 wxWindowDC *window_dc = wxDynamicCast( &dc, wxWindowDC );
228 if (window_dc)
229 gc = wxGraphicsContext::Create( *window_dc );
230
8371a353
JS
231#ifdef __WXMSW__
232 wxEnhMetaFileDC *emf_dc = wxDynamicCast( &dc, wxEnhMetaFileDC );
233 if (emf_dc)
234 gc = wxGraphicsContext::Create( *emf_dc );
235#endif
236
48ed4a89
FM
237 if (gc)
238 {
239 // make a path that contains a circle and some lines, centered at 100,100
240 gc->SetPen( *wxRED_PEN );
a5e40d85 241
48ed4a89
FM
242 wxGraphicsPath path = gc->CreatePath();
243 path.AddCircle( 50.0, 50.0, 50.0 );
244 path.MoveToPoint(0.0, 50.0);
245 path.AddLineToPoint(100.0, 50.0);
246 path.MoveToPoint(50.0, 0.0);
247 path.AddLineToPoint(50.0, 100.0 );
248 path.CloseSubpath();
249 path.AddRectangle(25.0, 25.0, 50.0, 50.0);
250
251 gc->StrokePath(path);
252
a5e40d85 253 // draw some text
a29df062 254 wxString text("Text by wxGraphicsContext");
17c02cc8
JS
255 gc->SetFont( m_testFont, *wxBLACK );
256 gc->DrawText(text, 25.0, 60.0);
a5e40d85
JS
257
258 // draw rectangle around the text
259 double w, h, d, el;
260 gc->GetTextExtent(text, &w, &h, &d, &el);
17c02cc8
JS
261 gc->SetPen( *wxBLACK_PEN );
262 gc->DrawRectangle(25.0, 60.0, w, h);
a5e40d85 263
48ed4a89
FM
264 delete gc;
265 }
266#endif
267}
268
269
270// ----------------------------------------------------------------------------
271// MyFrame
272// ----------------------------------------------------------------------------
273
274BEGIN_EVENT_TABLE(MyFrame, wxFrame)
275 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
276 EVT_MENU(wxID_PRINT, MyFrame::OnPrint)
277 EVT_MENU(wxID_PREVIEW, MyFrame::OnPrintPreview)
278 EVT_MENU(WXPRINT_PAGE_SETUP, MyFrame::OnPageSetup)
279 EVT_MENU(wxID_ABOUT, MyFrame::OnPrintAbout)
280#if defined(__WXMSW__) &&wxTEST_POSTSCRIPT_IN_MSW
281 EVT_MENU(WXPRINT_PRINT_PS, MyFrame::OnPrintPS)
282 EVT_MENU(WXPRINT_PREVIEW_PS, MyFrame::OnPrintPreviewPS)
283 EVT_MENU(WXPRINT_PAGE_SETUP_PS, MyFrame::OnPageSetupPS)
284#endif
285#ifdef __WXMAC__
286 EVT_MENU(WXPRINT_PAGE_MARGINS, MyFrame::OnPageMargins)
287#endif
288 EVT_MENU(WXPRINT_ANGLEUP, MyFrame::OnAngleUp)
289 EVT_MENU(WXPRINT_ANGLEDOWN, MyFrame::OnAngleDown)
290END_EVENT_TABLE()
291
292MyFrame::MyFrame(wxFrame *frame, const wxString&title, const wxPoint&pos, const wxSize&size)
293 : wxFrame(frame, wxID_ANY, title, pos, size)
294{
295 m_canvas = NULL;
9134af5b 296
8520f137 297#if wxUSE_STATUSBAR
48ed4a89
FM
298 // Give us a status line
299 CreateStatusBar(2);
9a83f860 300 SetStatusText(wxT("Printing demo"));
8520f137 301#endif // wxUSE_STATUSBAR
9134af5b 302
7bcb11d3 303 // Load icon and bitmap
3cb332c1 304 SetIcon( wxICON( sample) );
9134af5b 305
7bcb11d3
JS
306 // Make a menubar
307 wxMenu *file_menu = new wxMenu;
9134af5b 308
9a83f860
VZ
309 file_menu->Append(wxID_PRINT, wxT("&Print..."), wxT("Print"));
310 file_menu->Append(WXPRINT_PAGE_SETUP, wxT("Page Set&up..."), wxT("Page setup"));
f415cab9 311#ifdef __WXMAC__
9a83f860 312 file_menu->Append(WXPRINT_PAGE_MARGINS, wxT("Page Margins..."), wxT("Page margins"));
f415cab9 313#endif
9a83f860 314 file_menu->Append(wxID_PREVIEW, wxT("Print Pre&view"), wxT("Preview"));
9134af5b 315
3d2d8da1 316#if wxUSE_ACCEL
031b2a7b
RR
317 // Accelerators
318 wxAcceleratorEntry entries[1];
48ed4a89 319 entries[0].Set(wxACCEL_CTRL, (int) 'V', wxID_PREVIEW);
031b2a7b 320 wxAcceleratorTable accel(1, entries);
48ed4a89 321 SetAcceleratorTable(accel);
3d2d8da1
RR
322#endif
323
48ed4a89 324#if defined(__WXMSW__) &&wxTEST_POSTSCRIPT_IN_MSW
7bcb11d3 325 file_menu->AppendSeparator();
9a83f860
VZ
326 file_menu->Append(WXPRINT_PRINT_PS, wxT("Print PostScript..."), wxT("Print (PostScript)"));
327 file_menu->Append(WXPRINT_PAGE_SETUP_PS, wxT("Page Setup PostScript..."), wxT("Page setup (PostScript)"));
328 file_menu->Append(WXPRINT_PREVIEW_PS, wxT("Print Preview PostScript"), wxT("Preview (PostScript)"));
c801d85f 329#endif
f415cab9 330
cac7ac30 331 file_menu->AppendSeparator();
9a83f860
VZ
332 file_menu->Append(WXPRINT_ANGLEUP, wxT("Angle up\tAlt-U"), wxT("Raise rotated text angle"));
333 file_menu->Append(WXPRINT_ANGLEDOWN, wxT("Angle down\tAlt-D"), wxT("Lower rotated text angle"));
7bcb11d3 334 file_menu->AppendSeparator();
9a83f860 335 file_menu->Append(wxID_EXIT, wxT("E&xit"), wxT("Exit program"));
9134af5b 336
7bcb11d3 337 wxMenu *help_menu = new wxMenu;
9a83f860 338 help_menu->Append(wxID_ABOUT, wxT("&About"), wxT("About this demo"));
9134af5b 339
7bcb11d3 340 wxMenuBar *menu_bar = new wxMenuBar;
9134af5b 341
9a83f860
VZ
342 menu_bar->Append(file_menu, wxT("&File"));
343 menu_bar->Append(help_menu, wxT("&Help"));
9134af5b 344
7bcb11d3 345 // Associate the menu bar with the frame
48ed4a89 346 SetMenuBar(menu_bar);
9134af5b 347
c801d85f 348
48ed4a89
FM
349 // create the canvas
350 // -----------------
e3065973 351
48ed4a89
FM
352 m_canvas = new MyCanvas(this, wxPoint(0, 0), wxSize(100, 100),
353 wxRETAINED|wxHSCROLL|wxVSCROLL);
c801d85f 354
48ed4a89
FM
355 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
356 m_canvas->SetScrollbars(20, 20, 50, 50);
c801d85f
KB
357}
358
e3e65dac 359void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
c801d85f 360{
9134af5b 361 Close(true /*force closing*/);
c801d85f
KB
362}
363
e3e65dac 364void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
c801d85f 365{
7bcb11d3
JS
366 wxPrintDialogData printDialogData(* g_printData);
367
48ed4a89 368 wxPrinter printer(&printDialogData);
9a83f860 369 MyPrintout printout(this, wxT("My printout"));
9134af5b 370 if (!printer.Print(this, &printout, true /*prompt*/))
f6bcfd97
BP
371 {
372 if (wxPrinter::GetLastError() == wxPRINTER_ERROR)
43b2d5e7 373 {
9a83f860 374 wxLogError(wxT("There was a problem printing. Perhaps your current printer is not set correctly?"));
43b2d5e7 375 }
f6bcfd97 376 else
43b2d5e7 377 {
9a83f860 378 wxLogMessage(wxT("You canceled printing"));
43b2d5e7 379 }
f6bcfd97 380 }
7bcb11d3
JS
381 else
382 {
383 (*g_printData) = printer.GetPrintDialogData().GetPrintData();
384 }
c801d85f
KB
385}
386
e3e65dac 387void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
c801d85f 388{
7bcb11d3
JS
389 // Pass two printout objects: for preview, and possible printing.
390 wxPrintDialogData printDialogData(* g_printData);
48ed4a89
FM
391 wxPrintPreview *preview =
392 new wxPrintPreview(new MyPrintout(this), new MyPrintout(this), &printDialogData);
393 if (!preview->IsOk())
7bcb11d3 394 {
c801d85f 395 delete preview;
9a83f860 396 wxLogError(wxT("There was a problem previewing.\nPerhaps your current printer is not set correctly?"));
c801d85f 397 return;
7bcb11d3 398 }
9134af5b 399
48ed4a89 400 wxPreviewFrame *frame =
9a83f860 401 new wxPreviewFrame(preview, this, wxT("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
7bcb11d3
JS
402 frame->Centre(wxBOTH);
403 frame->Initialize();
9134af5b 404 frame->Show();
c801d85f
KB
405}
406
e3e65dac 407void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
c801d85f 408{
cac7ac30 409 (*g_pageSetupData) = *g_printData;
c801d85f 410
7bcb11d3
JS
411 wxPageSetupDialog pageSetupDialog(this, g_pageSetupData);
412 pageSetupDialog.ShowModal();
9134af5b 413
f415cab9
JS
414 (*g_printData) = pageSetupDialog.GetPageSetupDialogData().GetPrintData();
415 (*g_pageSetupData) = pageSetupDialog.GetPageSetupDialogData();
c801d85f
KB
416}
417
dfad0599
JS
418#if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
419void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
420{
7bcb11d3 421 wxPostScriptPrinter printer(g_printData);
9a83f860 422 MyPrintout printout(wxT("My printout"));
9134af5b 423 printer.Print(this, &printout, true/*prompt*/);
7bcb11d3
JS
424
425 (*g_printData) = printer.GetPrintData();
dfad0599
JS
426}
427
428void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
429{
7bcb11d3
JS
430 // Pass two printout objects: for preview, and possible printing.
431 wxPrintDialogData printDialogData(* g_printData);
48ed4a89
FM
432 wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, &printDialogData);
433 wxPreviewFrame *frame =
9a83f860 434 new wxPreviewFrame(preview, this, wxT("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
7bcb11d3
JS
435 frame->Centre(wxBOTH);
436 frame->Initialize();
9134af5b 437 frame->Show();
dfad0599
JS
438}
439
e3e65dac 440void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
c801d85f 441{
7bcb11d3 442 (*g_pageSetupData) = * g_printData;
c801d85f 443
7bcb11d3
JS
444 wxGenericPageSetupDialog pageSetupDialog(this, g_pageSetupData);
445 pageSetupDialog.ShowModal();
c801d85f 446
f415cab9
JS
447 (*g_printData) = pageSetupDialog.GetPageSetupDialogData().GetPrintData();
448 (*g_pageSetupData) = pageSetupDialog.GetPageSetupDialogData();
449}
450#endif
451
f415cab9
JS
452#ifdef __WXMAC__
453void MyFrame::OnPageMargins(wxCommandEvent& WXUNUSED(event))
454{
455 (*g_pageSetupData) = *g_printData;
456
457 wxMacPageMarginsDialog pageMarginsDialog(this, g_pageSetupData);
458 pageMarginsDialog.ShowModal();
459
460 (*g_printData) = pageMarginsDialog.GetPageSetupDialogData().GetPrintData();
461 (*g_pageSetupData) = pageMarginsDialog.GetPageSetupDialogData();
c801d85f 462}
dfad0599
JS
463#endif
464
e3e65dac 465void MyFrame::OnPrintAbout(wxCommandEvent& WXUNUSED(event))
c801d85f 466{
9a83f860
VZ
467 (void)wxMessageBox(wxT("wxWidgets printing demo\nAuthor: Julian Smart"),
468 wxT("About wxWidgets printing demo"), wxOK|wxCENTRE);
c801d85f
KB
469}
470
abc2a4cc 471void MyFrame::OnAngleUp(wxCommandEvent& WXUNUSED(event))
cac7ac30 472{
48ed4a89
FM
473 wxGetApp().IncrementAngle();
474 m_canvas->Refresh();
cac7ac30
RR
475}
476
abc2a4cc 477void MyFrame::OnAngleDown(wxCommandEvent& WXUNUSED(event))
cac7ac30 478{
48ed4a89
FM
479 wxGetApp().DecrementAngle();
480 m_canvas->Refresh();
cac7ac30
RR
481}
482
c801d85f 483
48ed4a89
FM
484// ----------------------------------------------------------------------------
485// MyCanvas
486// ----------------------------------------------------------------------------
c801d85f
KB
487
488BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
48ed4a89 489 // EVT_PAINT(MyCanvas::OnPaint)
c801d85f
KB
490END_EVENT_TABLE()
491
48ed4a89
FM
492MyCanvas::MyCanvas(wxFrame *frame, const wxPoint&pos, const wxSize&size, long style)
493 : wxScrolledWindow(frame, wxID_ANY, pos, size, style)
c801d85f 494{
48ed4a89 495 SetBackgroundColour(*wxWHITE);
c801d85f
KB
496}
497
48ed4a89 498//void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(evt))
c801d85f
KB
499void MyCanvas::OnDraw(wxDC& dc)
500{
48ed4a89
FM
501 //wxPaintDC dc(this);
502 wxGetApp().Draw(dc);
c801d85f
KB
503}
504
48ed4a89
FM
505
506// ----------------------------------------------------------------------------
507// MyPrintout
508// ----------------------------------------------------------------------------
c801d85f 509
c801d85f
KB
510bool MyPrintout::OnPrintPage(int page)
511{
7bcb11d3
JS
512 wxDC *dc = GetDC();
513 if (dc)
514 {
515 if (page == 1)
f415cab9 516 DrawPageOne();
7bcb11d3 517 else if (page == 2)
f415cab9 518 DrawPageTwo();
9134af5b 519
f415cab9
JS
520 // Draw page numbers at top left corner of printable area, sized so that
521 // screen size of text matches paper size.
522 MapScreenSizeToPage();
48ed4a89
FM
523
524 dc->DrawText(wxString::Format(wxT("PAGE %d"), page), 0, 0);
9134af5b
DS
525
526 return true;
7bcb11d3
JS
527 }
528 else
9134af5b 529 return false;
c801d85f
KB
530}
531
532bool MyPrintout::OnBeginDocument(int startPage, int endPage)
533{
7bcb11d3 534 if (!wxPrintout::OnBeginDocument(startPage, endPage))
9134af5b
DS
535 return false;
536
537 return true;
c801d85f
KB
538}
539
540void MyPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
541{
7bcb11d3
JS
542 *minPage = 1;
543 *maxPage = 2;
544 *selPageFrom = 1;
545 *selPageTo = 2;
c801d85f
KB
546}
547
548bool MyPrintout::HasPage(int pageNum)
549{
7bcb11d3 550 return (pageNum == 1 || pageNum == 2);
c801d85f
KB
551}
552
f415cab9 553void MyPrintout::DrawPageOne()
c801d85f 554{
f415cab9
JS
555 // You might use THIS code if you were scaling graphics of known size to fit
556 // on the page. The commented-out code illustrates different ways of scaling
557 // the graphics.
558
559 // We know the graphic is 230x350. If we didn't know this, we'd need to
560 // calculate it.
561 wxCoord maxX = 230;
562 wxCoord maxY = 350;
563
564 // This sets the user scale and origin of the DC so that the image fits
565 // within the paper rectangle (but the edges could be cut off by printers
566 // that can't print to the edges of the paper -- which is most of them. Use
567 // this if your image already has its own margins.
568// FitThisSizeToPaper(wxSize(maxX, maxY));
569// wxRect fitRect = GetLogicalPaperRect();
570
571 // This sets the user scale and origin of the DC so that the image fits
572 // within the page rectangle, which is the printable area on Mac and MSW
573 // and is the entire page on other platforms.
574// FitThisSizeToPage(wxSize(maxX, maxY));
575// wxRect fitRect = GetLogicalPageRect();
576
577 // This sets the user scale and origin of the DC so that the image fits
578 // within the page margins as specified by g_PageSetupData, which you can
579 // change (on some platforms, at least) in the Page Setup dialog. Note that
580 // on Mac, the native Page Setup dialog doesn't let you change the margins
581 // of a wxPageSetupDialogData object, so you'll have to write your own dialog or
582 // use the Mac-only wxMacPageMarginsDialog, as we do in this program.
583 FitThisSizeToPageMargins(wxSize(maxX, maxY), *g_pageSetupData);
584 wxRect fitRect = GetLogicalPageMarginsRect(*g_pageSetupData);
585
586 // This sets the user scale and origin of the DC so that the image appears
587 // on the paper at the same size that it appears on screen (i.e., 10-point
588 // type on screen is 10-point on the printed page) and is positioned in the
589 // top left corner of the page rectangle (just as the screen image appears
590 // in the top left corner of the window).
591// MapScreenSizeToPage();
592// wxRect fitRect = GetLogicalPageRect();
593
594 // You could also map the screen image to the entire paper at the same size
595 // as it appears on screen.
596// MapScreenSizeToPaper();
597// wxRect fitRect = GetLogicalPaperRect();
598
599 // You might also wish to do you own scaling in order to draw objects at
600 // full native device resolution. In this case, you should do the following.
601 // Note that you can use the GetLogicalXXXRect() commands to obtain the
602 // appropriate rect to scale to.
603// MapScreenSizeToDevice();
604// wxRect fitRect = GetLogicalPageRect();
605
606 // Each of the preceding Fit or Map routines positions the origin so that
607 // the drawn image is positioned at the top left corner of the reference
608 // rectangle. You can easily center or right- or bottom-justify the image as
609 // follows.
610
611 // This offsets the image so that it is centered within the reference
612 // rectangle defined above.
613 wxCoord xoff = (fitRect.width - maxX) / 2;
614 wxCoord yoff = (fitRect.height - maxY) / 2;
615 OffsetLogicalOrigin(xoff, yoff);
616
617 // This offsets the image so that it is positioned at the bottom right of
618 // the reference rectangle defined above.
619// wxCoord xoff = (fitRect.width - maxX);
620// wxCoord yoff = (fitRect.height - maxY);
621// OffsetLogicalOrigin(xoff, yoff);
622
48ed4a89 623 wxGetApp().Draw(*GetDC());
c801d85f
KB
624}
625
f415cab9 626void MyPrintout::DrawPageTwo()
c801d85f 627{
77efa6a8
RR
628 // You might use THIS code to set the printer DC to ROUGHLY reflect
629 // the screen text size. This page also draws lines of actual length
630 // 5cm on the page.
9134af5b 631
f415cab9
JS
632 // Compare this to DrawPageOne(), which uses the really convenient routines
633 // from wxPrintout to fit the screen image onto the printed page. This page
634 // illustrates how to do all the scaling calculations yourself, if you're so
635 // inclined.
636
637 wxDC *dc = GetDC();
638
7bcb11d3
JS
639 // Get the logical pixels per inch of screen and printer
640 int ppiScreenX, ppiScreenY;
641 GetPPIScreen(&ppiScreenX, &ppiScreenY);
642 int ppiPrinterX, ppiPrinterY;
643 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
9134af5b 644
f415cab9
JS
645 // This scales the DC so that the printout roughly represents the the screen
646 // scaling. The text point size _should_ be the right size but in fact is
647 // too small for some reason. This is a detail that will need to be
648 // addressed at some point but can be fudged for the moment.
7bcb11d3 649 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
9134af5b 650
f415cab9
JS
651 // Now we have to check in case our real page size is reduced (e.g. because
652 // we're drawing to a print preview memory DC)
7bcb11d3
JS
653 int pageWidth, pageHeight;
654 int w, h;
655 dc->GetSize(&w, &h);
656 GetPageSizePixels(&pageWidth, &pageHeight);
9134af5b 657
f415cab9
JS
658 // If printer pageWidth == current DC width, then this doesn't change. But w
659 // might be the preview bitmap width, so scale down.
7bcb11d3
JS
660 float overallScale = scale * (float)(w/(float)pageWidth);
661 dc->SetUserScale(overallScale, overallScale);
9134af5b 662
f415cab9
JS
663 // Calculate conversion factor for converting millimetres into logical
664 // units. There are approx. 25.4 mm to the inch. There are ppi device units
665 // to the inch. Therefore 1 mm corresponds to ppi/25.4 device units. We also
666 // divide by the screen-to-printer scaling factor, because we need to
7bcb11d3 667 // unscale to pass logical units to DrawLine.
9134af5b 668
7bcb11d3 669 // Draw 50 mm by 50 mm L shape
70949ed5 670 float logUnitsFactor = (float)(ppiPrinterX/(scale*25.4));
7bcb11d3
JS
671 float logUnits = (float)(50*logUnitsFactor);
672 dc->SetPen(* wxBLACK_PEN);
673 dc->DrawLine(50, 250, (long)(50.0 + logUnits), 250);
674 dc->DrawLine(50, 250, 50, (long)(250.0 + logUnits));
9134af5b 675
7bcb11d3 676 dc->SetBackgroundMode(wxTRANSPARENT);
1db4e8dd 677 dc->SetBrush(*wxTRANSPARENT_BRUSH);
9134af5b 678
d57bf685 679 { // GetTextExtent demo:
9a83f860
VZ
680 wxString words[7] = { wxT("This "), wxT("is "), wxT("GetTextExtent "),
681 wxT("testing "), wxT("string. "), wxT("Enjoy "), wxT("it!") };
6f207e66 682 wxCoord w, h;
d57bf685
VS
683 long x = 200, y= 250;
684 wxFont fnt(15, wxSWISS, wxNORMAL, wxNORMAL);
9134af5b 685
d57bf685 686 dc->SetFont(fnt);
9134af5b 687
77efa6a8
RR
688 for (int i = 0; i < 7; i++)
689 {
690 wxString word = words[i];
691 word.Remove( word.Len()-1, 1 );
692 dc->GetTextExtent(word, &w, &h);
d57bf685 693 dc->DrawRectangle(x, y, w, h);
77efa6a8 694 dc->GetTextExtent(words[i], &w, &h);
d57bf685
VS
695 dc->DrawText(words[i], x, y);
696 x += w;
697 }
9134af5b 698
d57bf685 699 }
9134af5b 700
48ed4a89 701 dc->SetFont(wxGetApp().GetTestFont());
9134af5b 702
9a83f860 703 dc->DrawText(wxT("Some test text"), 200, 300 );
92980e90 704
7bcb11d3 705 // TESTING
9134af5b 706
7bcb11d3
JS
707 int leftMargin = 20;
708 int rightMargin = 20;
709 int topMargin = 20;
710 int bottomMargin = 20;
9134af5b 711
7bcb11d3
JS
712 int pageWidthMM, pageHeightMM;
713 GetPageSizeMM(&pageWidthMM, &pageHeightMM);
9134af5b 714
7bcb11d3
JS
715 float leftMarginLogical = (float)(logUnitsFactor*leftMargin);
716 float topMarginLogical = (float)(logUnitsFactor*topMargin);
717 float bottomMarginLogical = (float)(logUnitsFactor*(pageHeightMM - bottomMargin));
718 float rightMarginLogical = (float)(logUnitsFactor*(pageWidthMM - rightMargin));
9134af5b 719
7bcb11d3 720 dc->SetPen(* wxRED_PEN);
9134af5b 721 dc->DrawLine( (long)leftMarginLogical, (long)topMarginLogical,
7bcb11d3 722 (long)rightMarginLogical, (long)topMarginLogical);
9134af5b 723 dc->DrawLine( (long)leftMarginLogical, (long)bottomMarginLogical,
7bcb11d3 724 (long)rightMarginLogical, (long)bottomMarginLogical);
9134af5b 725
9a83f860 726 WritePageHeader(this, dc, wxT("A header"), logUnitsFactor);
c801d85f
KB
727}
728
729// Writes a header on a page. Margin units are in millimetres.
48ed4a89 730bool MyPrintout::WritePageHeader(wxPrintout *printout, wxDC *dc, const wxString&text, float mmToLogical)
c801d85f 731{
48ed4a89
FM
732#if 0
733 static wxFont *headerFont = (wxFont *) NULL;
734 if (!headerFont)
735 {
736 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
737 }
738 dc->SetFont(headerFont);
739#endif
9134af5b 740
7bcb11d3 741 int pageWidthMM, pageHeightMM;
9134af5b 742
7bcb11d3 743 printout->GetPageSizeMM(&pageWidthMM, &pageHeightMM);
8afa4fde 744 wxUnusedVar(pageHeightMM);
9134af5b 745
7bcb11d3
JS
746 int leftMargin = 10;
747 int topMargin = 10;
748 int rightMargin = 10;
9134af5b 749
7bcb11d3
JS
750 float leftMarginLogical = (float)(mmToLogical*leftMargin);
751 float topMarginLogical = (float)(mmToLogical*topMargin);
752 float rightMarginLogical = (float)(mmToLogical*(pageWidthMM - rightMargin));
9134af5b 753
6f207e66 754 wxCoord xExtent, yExtent;
7bcb11d3 755 dc->GetTextExtent(text, &xExtent, &yExtent);
48ed4a89 756
7bcb11d3
JS
757 float xPos = (float)(((((pageWidthMM - leftMargin - rightMargin)/2.0)+leftMargin)*mmToLogical) - (xExtent/2.0));
758 dc->DrawText(text, (long)xPos, (long)topMarginLogical);
9134af5b 759
7bcb11d3 760 dc->SetPen(* wxBLACK_PEN);
9134af5b 761 dc->DrawLine( (long)leftMarginLogical, (long)(topMarginLogical+yExtent),
48ed4a89 762 (long)rightMarginLogical, (long)topMarginLogical+yExtent );
9134af5b
DS
763
764 return true;
c801d85f 765}