1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/printing.cpp
3 // Purpose: Printing demo for wxWidgets
4 // Author: Julian Smart
5 // Modified by: Francesco Montorsi
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
24 #if !wxUSE_PRINTING_ARCHITECTURE
25 #error "You must set wxUSE_PRINTING_ARCHITECTURE to 1 in setup.h, and recompile the library."
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
33 #include "wx/metafile.h"
35 #include "wx/printdlg.h"
39 #if wxTEST_POSTSCRIPT_IN_MSW
40 #include "wx/generic/printps.h"
41 #include "wx/generic/prntdlgg.h"
44 #if wxUSE_GRAPHICS_CONTEXT
45 #include "wx/graphics.h"
49 #include "wx/osx/printdlg.h"
55 #include "mondrian.xpm"
58 // Global print data, to remember settings during the session
59 wxPrintData
*g_printData
= NULL
;
61 // Global page setup data
62 wxPageSetupDialogData
* g_pageSetupData
= NULL
;
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
72 bool MyApp::OnInit(void)
74 if ( !wxApp::OnInit() )
77 wxInitAllImageHandlers();
80 // init global objects
81 // -------------------
83 g_printData
= new wxPrintData
;
85 // You could set an initial paper size here
87 g_printData
->SetPaperId(wxPAPER_LETTER
); // for Americans
88 g_printData
->SetPaperId(wxPAPER_A4
); // for everyone else
91 g_pageSetupData
= new wxPageSetupDialogData
;
93 // copy over initial paper size from print record
94 (*g_pageSetupData
) = *g_printData
;
96 // Set some initial page margins in mm.
97 g_pageSetupData
->SetMarginTopLeft(wxPoint(15, 15));
98 g_pageSetupData
->SetMarginBottomRight(wxPoint(15, 15));
101 // init local GUI objects
102 // ----------------------
105 wxImage
image( wxT("test.jpg") );
108 for (i
= 0; i
< image
.GetWidth(); i
++)
109 for (j
= 0; j
< image
.GetHeight(); j
++)
110 image
.SetAlpha( i
, j
, 50 );
114 m_testFont
.Create(10, wxFONTFAMILY_SWISS
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
);
117 // Create the main frame window
118 // ----------------------------
120 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, wxT("wxWidgets Printing Demo"),
121 wxPoint(0, 0), wxSize(400, 400));
123 frame
->Centre(wxBOTH
);
134 delete g_pageSetupData
;
136 return wxApp::OnExit();
139 void MyApp::Draw(wxDC
&dc
)
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
);
147 dc
.SetFont(m_testFont
);
149 // dc.SetBackgroundMode(wxTRANSPARENT);
151 dc
.SetPen(*wxBLACK_PEN
);
152 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
);
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
);
159 dc
.SetBrush(*wxCYAN_BRUSH
);
160 dc
.SetPen(*wxRED_PEN
);
162 dc
.DrawRoundedRectangle(0, 20, 200, 80, 20);
164 dc
.DrawText( wxT("Rectangle 200 by 80"), 40, 40);
166 dc
.SetPen( wxPen(*wxBLACK
,0,wxDOT_DASH
) );
167 dc
.DrawEllipse(50, 140, 100, 50);
168 dc
.SetPen(*wxRED_PEN
);
170 dc
.DrawText( wxT("Test message: this is in 10 point text"), 10, 180);
173 const char *test
= "Hebrew שלום -- Japanese (日本語)";
174 wxString tmp
= wxConvUTF8
.cMB2WC( test
);
175 dc
.DrawText( tmp
, 10, 200 );
189 dc
.DrawPolygon( 5, points
, 20, 250, wxODDEVEN_RULE
);
190 dc
.DrawPolygon( 5, points
, 50, 250, wxWINDING_RULE
);
192 dc
.DrawEllipticArc( 80, 250, 60, 30, 0.0, 270.0 );
202 dc
.DrawSpline( 4, points
);
204 dc
.DrawArc( 20,10, 10,10, 25,40 );
208 str
.Printf( wxT("---- Text at angle %d ----"), i
);
209 dc
.DrawRotatedText( str
, 100, 300, i
);
212 str
.Printf( wxT("---- Text at angle %d ----"), i
);
213 dc
.DrawRotatedText( str
, 100, 300, i
);
215 wxIcon my_icon
= wxICON(mondrian
);
217 dc
.DrawIcon( my_icon
, 100, 100);
220 dc
.DrawBitmap( m_bitmap
, 10, 10 );
222 #if wxUSE_GRAPHICS_CONTEXT
223 wxGraphicsContext
*gc
= NULL
;
225 wxPrinterDC
*printer_dc
= wxDynamicCast( &dc
, wxPrinterDC
);
227 gc
= wxGraphicsContext::Create( *printer_dc
);
229 wxWindowDC
*window_dc
= wxDynamicCast( &dc
, wxWindowDC
);
231 gc
= wxGraphicsContext::Create( *window_dc
);
235 // make a path that contains a circle and some lines, centered at 100,100
236 gc
->SetPen( *wxRED_PEN
);
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 );
245 path
.AddRectangle(25.0, 25.0, 50.0, 50.0);
247 gc
->StrokePath(path
);
250 wxString
text("Text by wxGraphicsContext");
251 gc
->SetFont( m_testFont
, *wxBLUE
);
252 gc
->DrawText(text
, 50.0, 50.0);
254 // draw rectangle around the text
256 gc
->GetTextExtent(text
, &w
, &h
, &d
, &el
);
257 gc
->SetPen( *wxBLUE_PEN
);
258 gc
->DrawRectangle(50.0, 50.0, w
, h
);
266 // ----------------------------------------------------------------------------
268 // ----------------------------------------------------------------------------
270 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
271 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
272 EVT_MENU(wxID_PRINT
, MyFrame::OnPrint
)
273 EVT_MENU(wxID_PREVIEW
, MyFrame::OnPrintPreview
)
274 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
275 EVT_MENU(wxID_ABOUT
, MyFrame::OnPrintAbout
)
276 #if defined(__WXMSW__) &&wxTEST_POSTSCRIPT_IN_MSW
277 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame::OnPrintPS
)
278 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
279 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
282 EVT_MENU(WXPRINT_PAGE_MARGINS
, MyFrame::OnPageMargins
)
284 EVT_MENU(WXPRINT_ANGLEUP
, MyFrame::OnAngleUp
)
285 EVT_MENU(WXPRINT_ANGLEDOWN
, MyFrame::OnAngleDown
)
288 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, const wxPoint
&pos
, const wxSize
&size
)
289 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
)
294 // Give us a status line
296 SetStatusText(wxT("Printing demo"));
297 #endif // wxUSE_STATUSBAR
299 // Load icon and bitmap
300 SetIcon( wxICON( mondrian
) );
303 wxMenu
*file_menu
= new wxMenu
;
305 file_menu
->Append(wxID_PRINT
, wxT("&Print..."), wxT("Print"));
306 file_menu
->Append(WXPRINT_PAGE_SETUP
, wxT("Page Set&up..."), wxT("Page setup"));
308 file_menu
->Append(WXPRINT_PAGE_MARGINS
, wxT("Page Margins..."), wxT("Page margins"));
310 file_menu
->Append(wxID_PREVIEW
, wxT("Print Pre&view"), wxT("Preview"));
314 wxAcceleratorEntry entries
[1];
315 entries
[0].Set(wxACCEL_CTRL
, (int) 'V', wxID_PREVIEW
);
316 wxAcceleratorTable
accel(1, entries
);
317 SetAcceleratorTable(accel
);
320 #if defined(__WXMSW__) &&wxTEST_POSTSCRIPT_IN_MSW
321 file_menu
->AppendSeparator();
322 file_menu
->Append(WXPRINT_PRINT_PS
, wxT("Print PostScript..."), wxT("Print (PostScript)"));
323 file_menu
->Append(WXPRINT_PAGE_SETUP_PS
, wxT("Page Setup PostScript..."), wxT("Page setup (PostScript)"));
324 file_menu
->Append(WXPRINT_PREVIEW_PS
, wxT("Print Preview PostScript"), wxT("Preview (PostScript)"));
327 file_menu
->AppendSeparator();
328 file_menu
->Append(WXPRINT_ANGLEUP
, wxT("Angle up\tAlt-U"), wxT("Raise rotated text angle"));
329 file_menu
->Append(WXPRINT_ANGLEDOWN
, wxT("Angle down\tAlt-D"), wxT("Lower rotated text angle"));
330 file_menu
->AppendSeparator();
331 file_menu
->Append(wxID_EXIT
, wxT("E&xit"), wxT("Exit program"));
333 wxMenu
*help_menu
= new wxMenu
;
334 help_menu
->Append(wxID_ABOUT
, wxT("&About"), wxT("About this demo"));
336 wxMenuBar
*menu_bar
= new wxMenuBar
;
338 menu_bar
->Append(file_menu
, wxT("&File"));
339 menu_bar
->Append(help_menu
, wxT("&Help"));
341 // Associate the menu bar with the frame
342 SetMenuBar(menu_bar
);
348 m_canvas
= new MyCanvas(this, wxPoint(0, 0), wxSize(100, 100),
349 wxRETAINED
|wxHSCROLL
|wxVSCROLL
);
351 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
352 m_canvas
->SetScrollbars(20, 20, 50, 50);
355 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
357 Close(true /*force closing*/);
360 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
362 wxPrintDialogData
printDialogData(* g_printData
);
364 wxPrinter
printer(&printDialogData
);
365 MyPrintout
printout(this, wxT("My printout"));
366 if (!printer
.Print(this, &printout
, true /*prompt*/))
368 if (wxPrinter::GetLastError() == wxPRINTER_ERROR
)
370 wxLogError(wxT("There was a problem printing. Perhaps your current printer is not set correctly?"));
374 wxLogMessage(wxT("You canceled printing"));
379 (*g_printData
) = printer
.GetPrintDialogData().GetPrintData();
383 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
385 // Pass two printout objects: for preview, and possible printing.
386 wxPrintDialogData
printDialogData(* g_printData
);
387 wxPrintPreview
*preview
=
388 new wxPrintPreview(new MyPrintout(this), new MyPrintout(this), &printDialogData
);
389 if (!preview
->IsOk())
392 wxLogError(wxT("There was a problem previewing.\nPerhaps your current printer is not set correctly?"));
396 wxPreviewFrame
*frame
=
397 new wxPreviewFrame(preview
, this, wxT("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
398 frame
->Centre(wxBOTH
);
403 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
405 (*g_pageSetupData
) = *g_printData
;
407 wxPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
408 pageSetupDialog
.ShowModal();
410 (*g_printData
) = pageSetupDialog
.GetPageSetupDialogData().GetPrintData();
411 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupDialogData();
414 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
415 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
417 wxPostScriptPrinter
printer(g_printData
);
418 MyPrintout
printout(wxT("My printout"));
419 printer
.Print(this, &printout
, true/*prompt*/);
421 (*g_printData
) = printer
.GetPrintData();
424 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
426 // Pass two printout objects: for preview, and possible printing.
427 wxPrintDialogData
printDialogData(* g_printData
);
428 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, &printDialogData
);
429 wxPreviewFrame
*frame
=
430 new wxPreviewFrame(preview
, this, wxT("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
431 frame
->Centre(wxBOTH
);
436 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
438 (*g_pageSetupData
) = * g_printData
;
440 wxGenericPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
441 pageSetupDialog
.ShowModal();
443 (*g_printData
) = pageSetupDialog
.GetPageSetupDialogData().GetPrintData();
444 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupDialogData();
449 void MyFrame::OnPageMargins(wxCommandEvent
& WXUNUSED(event
))
451 (*g_pageSetupData
) = *g_printData
;
453 wxMacPageMarginsDialog
pageMarginsDialog(this, g_pageSetupData
);
454 pageMarginsDialog
.ShowModal();
456 (*g_printData
) = pageMarginsDialog
.GetPageSetupDialogData().GetPrintData();
457 (*g_pageSetupData
) = pageMarginsDialog
.GetPageSetupDialogData();
461 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
463 (void)wxMessageBox(wxT("wxWidgets printing demo\nAuthor: Julian Smart"),
464 wxT("About wxWidgets printing demo"), wxOK
|wxCENTRE
);
467 void MyFrame::OnAngleUp(wxCommandEvent
& WXUNUSED(event
))
469 wxGetApp().IncrementAngle();
473 void MyFrame::OnAngleDown(wxCommandEvent
& WXUNUSED(event
))
475 wxGetApp().DecrementAngle();
480 // ----------------------------------------------------------------------------
482 // ----------------------------------------------------------------------------
484 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
485 // EVT_PAINT(MyCanvas::OnPaint)
488 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
&pos
, const wxSize
&size
, long style
)
489 : wxScrolledWindow(frame
, wxID_ANY
, pos
, size
, style
)
491 SetBackgroundColour(*wxWHITE
);
494 //void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(evt))
495 void MyCanvas::OnDraw(wxDC
& dc
)
497 //wxPaintDC dc(this);
502 // ----------------------------------------------------------------------------
504 // ----------------------------------------------------------------------------
506 bool MyPrintout::OnPrintPage(int page
)
516 // Draw page numbers at top left corner of printable area, sized so that
517 // screen size of text matches paper size.
518 MapScreenSizeToPage();
520 dc
->DrawText(wxString::Format(wxT("PAGE %d"), page
), 0, 0);
528 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
530 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
536 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
544 bool MyPrintout::HasPage(int pageNum
)
546 return (pageNum
== 1 || pageNum
== 2);
549 void MyPrintout::DrawPageOne()
551 // You might use THIS code if you were scaling graphics of known size to fit
552 // on the page. The commented-out code illustrates different ways of scaling
555 // We know the graphic is 230x350. If we didn't know this, we'd need to
560 // This sets the user scale and origin of the DC so that the image fits
561 // within the paper rectangle (but the edges could be cut off by printers
562 // that can't print to the edges of the paper -- which is most of them. Use
563 // this if your image already has its own margins.
564 // FitThisSizeToPaper(wxSize(maxX, maxY));
565 // wxRect fitRect = GetLogicalPaperRect();
567 // This sets the user scale and origin of the DC so that the image fits
568 // within the page rectangle, which is the printable area on Mac and MSW
569 // and is the entire page on other platforms.
570 // FitThisSizeToPage(wxSize(maxX, maxY));
571 // wxRect fitRect = GetLogicalPageRect();
573 // This sets the user scale and origin of the DC so that the image fits
574 // within the page margins as specified by g_PageSetupData, which you can
575 // change (on some platforms, at least) in the Page Setup dialog. Note that
576 // on Mac, the native Page Setup dialog doesn't let you change the margins
577 // of a wxPageSetupDialogData object, so you'll have to write your own dialog or
578 // use the Mac-only wxMacPageMarginsDialog, as we do in this program.
579 FitThisSizeToPageMargins(wxSize(maxX
, maxY
), *g_pageSetupData
);
580 wxRect fitRect
= GetLogicalPageMarginsRect(*g_pageSetupData
);
582 // This sets the user scale and origin of the DC so that the image appears
583 // on the paper at the same size that it appears on screen (i.e., 10-point
584 // type on screen is 10-point on the printed page) and is positioned in the
585 // top left corner of the page rectangle (just as the screen image appears
586 // in the top left corner of the window).
587 // MapScreenSizeToPage();
588 // wxRect fitRect = GetLogicalPageRect();
590 // You could also map the screen image to the entire paper at the same size
591 // as it appears on screen.
592 // MapScreenSizeToPaper();
593 // wxRect fitRect = GetLogicalPaperRect();
595 // You might also wish to do you own scaling in order to draw objects at
596 // full native device resolution. In this case, you should do the following.
597 // Note that you can use the GetLogicalXXXRect() commands to obtain the
598 // appropriate rect to scale to.
599 // MapScreenSizeToDevice();
600 // wxRect fitRect = GetLogicalPageRect();
602 // Each of the preceding Fit or Map routines positions the origin so that
603 // the drawn image is positioned at the top left corner of the reference
604 // rectangle. You can easily center or right- or bottom-justify the image as
607 // This offsets the image so that it is centered within the reference
608 // rectangle defined above.
609 wxCoord xoff
= (fitRect
.width
- maxX
) / 2;
610 wxCoord yoff
= (fitRect
.height
- maxY
) / 2;
611 OffsetLogicalOrigin(xoff
, yoff
);
613 // This offsets the image so that it is positioned at the bottom right of
614 // the reference rectangle defined above.
615 // wxCoord xoff = (fitRect.width - maxX);
616 // wxCoord yoff = (fitRect.height - maxY);
617 // OffsetLogicalOrigin(xoff, yoff);
619 wxGetApp().Draw(*GetDC());
622 void MyPrintout::DrawPageTwo()
624 // You might use THIS code to set the printer DC to ROUGHLY reflect
625 // the screen text size. This page also draws lines of actual length
628 // Compare this to DrawPageOne(), which uses the really convenient routines
629 // from wxPrintout to fit the screen image onto the printed page. This page
630 // illustrates how to do all the scaling calculations yourself, if you're so
635 // Get the logical pixels per inch of screen and printer
636 int ppiScreenX
, ppiScreenY
;
637 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
638 int ppiPrinterX
, ppiPrinterY
;
639 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
641 // This scales the DC so that the printout roughly represents the the screen
642 // scaling. The text point size _should_ be the right size but in fact is
643 // too small for some reason. This is a detail that will need to be
644 // addressed at some point but can be fudged for the moment.
645 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
647 // Now we have to check in case our real page size is reduced (e.g. because
648 // we're drawing to a print preview memory DC)
649 int pageWidth
, pageHeight
;
652 GetPageSizePixels(&pageWidth
, &pageHeight
);
654 // If printer pageWidth == current DC width, then this doesn't change. But w
655 // might be the preview bitmap width, so scale down.
656 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
657 dc
->SetUserScale(overallScale
, overallScale
);
659 // Calculate conversion factor for converting millimetres into logical
660 // units. There are approx. 25.4 mm to the inch. There are ppi device units
661 // to the inch. Therefore 1 mm corresponds to ppi/25.4 device units. We also
662 // divide by the screen-to-printer scaling factor, because we need to
663 // unscale to pass logical units to DrawLine.
665 // Draw 50 mm by 50 mm L shape
666 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.4));
667 float logUnits
= (float)(50*logUnitsFactor
);
668 dc
->SetPen(* wxBLACK_PEN
);
669 dc
->DrawLine(50, 250, (long)(50.0 + logUnits
), 250);
670 dc
->DrawLine(50, 250, 50, (long)(250.0 + logUnits
));
672 dc
->SetBackgroundMode(wxTRANSPARENT
);
673 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
675 { // GetTextExtent demo:
676 wxString words
[7] = { wxT("This "), wxT("is "), wxT("GetTextExtent "),
677 wxT("testing "), wxT("string. "), wxT("Enjoy "), wxT("it!") };
679 long x
= 200, y
= 250;
680 wxFont
fnt(15, wxSWISS
, wxNORMAL
, wxNORMAL
);
684 for (int i
= 0; i
< 7; i
++)
686 wxString word
= words
[i
];
687 word
.Remove( word
.Len()-1, 1 );
688 dc
->GetTextExtent(word
, &w
, &h
);
689 dc
->DrawRectangle(x
, y
, w
, h
);
690 dc
->GetTextExtent(words
[i
], &w
, &h
);
691 dc
->DrawText(words
[i
], x
, y
);
697 dc
->SetFont(wxGetApp().GetTestFont());
699 dc
->DrawText(wxT("Some test text"), 200, 300 );
704 int rightMargin
= 20;
706 int bottomMargin
= 20;
708 int pageWidthMM
, pageHeightMM
;
709 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
711 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
712 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
713 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
714 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
716 dc
->SetPen(* wxRED_PEN
);
717 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
718 (long)rightMarginLogical
, (long)topMarginLogical
);
719 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
720 (long)rightMarginLogical
, (long)bottomMarginLogical
);
722 WritePageHeader(this, dc
, wxT("A header"), logUnitsFactor
);
725 // Writes a header on a page. Margin units are in millimetres.
726 bool MyPrintout::WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, const wxString
&text
, float mmToLogical
)
729 static wxFont
*headerFont
= (wxFont
*) NULL
;
732 headerFont
= wxTheFontList
->FindOrCreateFont(16, wxSWISS
, wxNORMAL
, wxBOLD
);
734 dc
->SetFont(headerFont
);
737 int pageWidthMM
, pageHeightMM
;
739 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
740 wxUnusedVar(pageHeightMM
);
744 int rightMargin
= 10;
746 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
747 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
748 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
750 wxCoord xExtent
, yExtent
;
751 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
753 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
754 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
756 dc
->SetPen(* wxBLACK_PEN
);
757 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
758 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);