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