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