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