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