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