1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/printing.cpp
3 // Purpose: Printing demo for wxWidgets
4 // Author: Julian Smart
5 // Modified by: Francesco Montorsi
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
23 #if !wxUSE_PRINTING_ARCHITECTURE
24 #error "You must set wxUSE_PRINTING_ARCHITECTURE to 1 in setup.h, and recompile the library."
28 #include "wx/metafile.h"
30 #include "wx/printdlg.h"
35 #include "wx/generic/printps.h"
36 #include "wx/generic/prntdlgg.h"
39 #if wxUSE_GRAPHICS_CONTEXT
40 #include "wx/graphics.h"
44 #include "wx/osx/printdlg.h"
49 #ifndef wxHAS_IMAGES_IN_RESOURCES
50 #include "../sample.xpm"
53 // Global print data, to remember settings during the session
54 wxPrintData
*g_printData
= NULL
;
56 // Global page setup data
57 wxPageSetupDialogData
* g_pageSetupData
= NULL
;
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
67 bool MyApp::OnInit(void)
69 if ( !wxApp::OnInit() )
72 wxInitAllImageHandlers();
75 // init global objects
76 // -------------------
78 g_printData
= new wxPrintData
;
80 // You could set an initial paper size here
82 g_printData
->SetPaperId(wxPAPER_LETTER
); // for Americans
83 g_printData
->SetPaperId(wxPAPER_A4
); // for everyone else
86 g_pageSetupData
= new wxPageSetupDialogData
;
88 // copy over initial paper size from print record
89 (*g_pageSetupData
) = *g_printData
;
91 // Set some initial page margins in mm.
92 g_pageSetupData
->SetMarginTopLeft(wxPoint(15, 15));
93 g_pageSetupData
->SetMarginBottomRight(wxPoint(15, 15));
96 // init local GUI objects
97 // ----------------------
100 wxImage
image( wxT("test.jpg") );
103 for (i
= 0; i
< image
.GetWidth(); i
++)
104 for (j
= 0; j
< image
.GetHeight(); j
++)
105 image
.SetAlpha( i
, j
, 50 );
109 m_testFont
.Create(10, wxFONTFAMILY_SWISS
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
);
112 // Create the main frame window
113 // ----------------------------
115 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, wxT("wxWidgets Printing Demo"),
116 wxPoint(0, 0), wxSize(400, 400));
118 frame
->Centre(wxBOTH
);
127 delete g_pageSetupData
;
129 return wxApp::OnExit();
132 void MyApp::Draw(wxDC
&dc
)
134 // This routine just draws a bunch of random stuff on the screen so that we
135 // can check that different types of object are being drawn consistently
136 // between the screen image, the print preview image (at various zoom
137 // levels), and the printed page.
138 dc
.SetBackground(*wxWHITE_BRUSH
);
140 dc
.SetFont(m_testFont
);
142 // dc.SetBackgroundMode(wxTRANSPARENT);
144 dc
.SetPen(*wxBLACK_PEN
);
145 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
);
147 dc
.DrawRectangle(0, 0, 230, 350);
148 dc
.DrawLine(0, 0, 229, 349);
149 dc
.DrawLine(229, 0, 0, 349);
150 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
152 dc
.SetBrush(*wxCYAN_BRUSH
);
153 dc
.SetPen(*wxRED_PEN
);
155 dc
.DrawRoundedRectangle(0, 20, 200, 80, 20);
157 dc
.DrawText( wxT("Rectangle 200 by 80"), 40, 40);
159 dc
.SetPen( wxPen(*wxBLACK
, 0, wxPENSTYLE_DOT_DASH
) );
160 dc
.DrawEllipse(50, 140, 100, 50);
161 dc
.SetPen(*wxRED_PEN
);
163 dc
.DrawText( wxT("Test message: this is in 10 point text"), 10, 180);
166 const char *test
= "Hebrew שלום -- Japanese (日本語)";
167 wxString tmp
= wxConvUTF8
.cMB2WC( test
);
168 dc
.DrawText( tmp
, 10, 200 );
182 dc
.DrawPolygon( 5, points
, 20, 250, wxODDEVEN_RULE
);
183 dc
.DrawPolygon( 5, points
, 50, 250, wxWINDING_RULE
);
185 dc
.DrawEllipticArc( 80, 250, 60, 30, 0.0, 270.0 );
195 dc
.DrawSpline( 4, points
);
197 dc
.DrawArc( 20,10, 10,10, 25,40 );
201 str
.Printf( wxT("---- Text at angle %d ----"), i
);
202 dc
.DrawRotatedText( str
, 100, 300, i
);
205 str
.Printf( wxT("---- Text at angle %d ----"), i
);
206 dc
.DrawRotatedText( str
, 100, 300, i
);
208 wxIcon my_icon
= wxICON(sample
);
210 dc
.DrawIcon( my_icon
, 100, 100);
213 dc
.DrawBitmap( m_bitmap
, 10, 10 );
215 #if wxUSE_GRAPHICS_CONTEXT
216 wxGraphicsContext
*gc
= NULL
;
218 wxPrinterDC
*printer_dc
= wxDynamicCast( &dc
, wxPrinterDC
);
220 gc
= wxGraphicsContext::Create( *printer_dc
);
222 wxWindowDC
*window_dc
= wxDynamicCast( &dc
, wxWindowDC
);
224 gc
= wxGraphicsContext::Create( *window_dc
);
227 wxEnhMetaFileDC
*emf_dc
= wxDynamicCast( &dc
, wxEnhMetaFileDC
);
229 gc
= wxGraphicsContext::Create( *emf_dc
);
234 // make a path that contains a circle and some lines, centered at 100,100
235 gc
->SetPen( *wxRED_PEN
);
237 wxGraphicsPath path
= gc
->CreatePath();
238 path
.AddCircle( 50.0, 50.0, 50.0 );
239 path
.MoveToPoint(0.0, 50.0);
240 path
.AddLineToPoint(100.0, 50.0);
241 path
.MoveToPoint(50.0, 0.0);
242 path
.AddLineToPoint(50.0, 100.0 );
244 path
.AddRectangle(25.0, 25.0, 50.0, 50.0);
246 gc
->StrokePath(path
);
249 wxString
text("Text by wxGraphicsContext");
250 gc
->SetFont( m_testFont
, *wxBLACK
);
251 gc
->DrawText(text
, 25.0, 60.0);
253 // draw rectangle around the text
255 gc
->GetTextExtent(text
, &w
, &h
, &d
, &el
);
256 gc
->SetPen( *wxBLACK_PEN
);
257 gc
->DrawRectangle(25.0, 60.0, w
, h
);
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
269 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
270 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
271 EVT_MENU(wxID_PRINT
, MyFrame::OnPrint
)
272 EVT_MENU(wxID_PREVIEW
, MyFrame::OnPrintPreview
)
273 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
274 EVT_MENU(wxID_ABOUT
, MyFrame::OnPrintAbout
)
276 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame::OnPrintPS
)
277 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
278 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
281 EVT_MENU(WXPRINT_PAGE_MARGINS
, MyFrame::OnPageMargins
)
283 EVT_MENU(WXPRINT_ANGLEUP
, MyFrame::OnAngleUp
)
284 EVT_MENU(WXPRINT_ANGLEDOWN
, MyFrame::OnAngleDown
)
286 EVT_MENU_RANGE(WXPRINT_FRAME_MODAL_APP
,
287 WXPRINT_FRAME_MODAL_NON
,
288 MyFrame::OnPreviewFrameModalityKind
)
291 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, const wxPoint
&pos
, const wxSize
&size
)
292 : wxFrame(frame
, wxID_ANY
, title
, pos
, size
)
295 m_previewModality
= wxPreviewFrame_AppModal
;
298 // Give us a status line
300 SetStatusText(wxT("Printing demo"));
301 #endif // wxUSE_STATUSBAR
303 // Load icon and bitmap
304 SetIcon( wxICON( sample
) );
307 wxMenu
*file_menu
= new wxMenu
;
309 file_menu
->Append(wxID_PRINT
, wxT("&Print..."), wxT("Print"));
310 file_menu
->Append(WXPRINT_PAGE_SETUP
, wxT("Page Set&up..."), wxT("Page setup"));
312 file_menu
->Append(WXPRINT_PAGE_MARGINS
, wxT("Page Margins..."), wxT("Page margins"));
314 file_menu
->Append(wxID_PREVIEW
, wxT("Print Pre&view"), wxT("Preview"));
316 wxMenu
* const menuModalKind
= new wxMenu
;
317 menuModalKind
->AppendRadioItem(WXPRINT_FRAME_MODAL_APP
, "&App modal");
318 menuModalKind
->AppendRadioItem(WXPRINT_FRAME_MODAL_WIN
, "&Window modal");
319 menuModalKind
->AppendRadioItem(WXPRINT_FRAME_MODAL_NON
, "&Not modal");
320 file_menu
->AppendSubMenu(menuModalKind
, "Preview frame &modal kind");
323 wxAcceleratorEntry entries
[1];
324 entries
[0].Set(wxACCEL_CTRL
, (int) 'V', wxID_PREVIEW
);
325 wxAcceleratorTable
accel(1, entries
);
326 SetAcceleratorTable(accel
);
330 file_menu
->AppendSeparator();
331 file_menu
->Append(WXPRINT_PRINT_PS
, wxT("Print PostScript..."), wxT("Print (PostScript)"));
332 file_menu
->Append(WXPRINT_PAGE_SETUP_PS
, wxT("Page Setup PostScript..."), wxT("Page setup (PostScript)"));
333 file_menu
->Append(WXPRINT_PREVIEW_PS
, wxT("Print Preview PostScript"), wxT("Preview (PostScript)"));
336 file_menu
->AppendSeparator();
337 file_menu
->Append(WXPRINT_ANGLEUP
, wxT("Angle up\tAlt-U"), wxT("Raise rotated text angle"));
338 file_menu
->Append(WXPRINT_ANGLEDOWN
, wxT("Angle down\tAlt-D"), wxT("Lower rotated text angle"));
339 file_menu
->AppendSeparator();
340 file_menu
->Append(wxID_EXIT
, wxT("E&xit"), wxT("Exit program"));
342 wxMenu
*help_menu
= new wxMenu
;
343 help_menu
->Append(wxID_ABOUT
, wxT("&About"), wxT("About this demo"));
345 wxMenuBar
*menu_bar
= new wxMenuBar
;
347 menu_bar
->Append(file_menu
, wxT("&File"));
348 menu_bar
->Append(help_menu
, wxT("&Help"));
350 // Associate the menu bar with the frame
351 SetMenuBar(menu_bar
);
357 m_canvas
= new MyCanvas(this, wxPoint(0, 0), wxSize(100, 100),
358 wxRETAINED
|wxHSCROLL
|wxVSCROLL
);
360 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
361 m_canvas
->SetScrollbars(20, 20, 50, 50);
364 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
366 Close(true /*force closing*/);
369 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
371 wxPrintDialogData
printDialogData(* g_printData
);
373 wxPrinter
printer(&printDialogData
);
374 MyPrintout
printout(this, wxT("My printout"));
375 if (!printer
.Print(this, &printout
, true /*prompt*/))
377 if (wxPrinter::GetLastError() == wxPRINTER_ERROR
)
379 wxLogError(wxT("There was a problem printing. Perhaps your current printer is not set correctly?"));
383 wxLogMessage(wxT("You canceled printing"));
388 (*g_printData
) = printer
.GetPrintDialogData().GetPrintData();
392 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
394 // Pass two printout objects: for preview, and possible printing.
395 wxPrintDialogData
printDialogData(* g_printData
);
396 wxPrintPreview
*preview
=
397 new wxPrintPreview(new MyPrintout(this), new MyPrintout(this), &printDialogData
);
398 if (!preview
->IsOk())
401 wxLogError(wxT("There was a problem previewing.\nPerhaps your current printer is not set correctly?"));
405 wxPreviewFrame
*frame
=
406 new wxPreviewFrame(preview
, this, wxT("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
407 frame
->Centre(wxBOTH
);
408 frame
->InitializeWithModality(m_previewModality
);
412 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
414 (*g_pageSetupData
) = *g_printData
;
416 wxPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
417 pageSetupDialog
.ShowModal();
419 (*g_printData
) = pageSetupDialog
.GetPageSetupDialogData().GetPrintData();
420 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupDialogData();
424 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
426 wxPrintDialogData
printDialogData(* g_printData
);
428 wxPostScriptPrinter
printer(&printDialogData
);
429 MyPrintout
printout(this, wxT("My printout"));
430 printer
.Print(this, &printout
, true/*prompt*/);
432 (*g_printData
) = printer
.GetPrintDialogData().GetPrintData();
435 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
437 // Pass two printout objects: for preview, and possible printing.
438 wxPrintDialogData
printDialogData(* g_printData
);
439 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout(this), new MyPrintout(this), &printDialogData
);
440 wxPreviewFrame
*frame
=
441 new wxPreviewFrame(preview
, this, wxT("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
442 frame
->Centre(wxBOTH
);
447 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
449 (*g_pageSetupData
) = * g_printData
;
451 wxGenericPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
452 pageSetupDialog
.ShowModal();
454 (*g_printData
) = pageSetupDialog
.GetPageSetupDialogData().GetPrintData();
455 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupDialogData();
460 void MyFrame::OnPageMargins(wxCommandEvent
& WXUNUSED(event
))
462 (*g_pageSetupData
) = *g_printData
;
464 wxMacPageMarginsDialog
pageMarginsDialog(this, g_pageSetupData
);
465 pageMarginsDialog
.ShowModal();
467 (*g_printData
) = pageMarginsDialog
.GetPageSetupDialogData().GetPrintData();
468 (*g_pageSetupData
) = pageMarginsDialog
.GetPageSetupDialogData();
472 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
474 (void)wxMessageBox(wxT("wxWidgets printing demo\nAuthor: Julian Smart"),
475 wxT("About wxWidgets printing demo"), wxOK
|wxCENTRE
);
478 void MyFrame::OnAngleUp(wxCommandEvent
& WXUNUSED(event
))
480 wxGetApp().IncrementAngle();
484 void MyFrame::OnAngleDown(wxCommandEvent
& WXUNUSED(event
))
486 wxGetApp().DecrementAngle();
490 void MyFrame::OnPreviewFrameModalityKind(wxCommandEvent
& event
)
492 m_previewModality
= static_cast<wxPreviewFrameModalityKind
>(
493 wxPreviewFrame_AppModal
+
494 (event
.GetId() - WXPRINT_FRAME_MODAL_APP
));
497 // ----------------------------------------------------------------------------
499 // ----------------------------------------------------------------------------
501 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
502 // EVT_PAINT(MyCanvas::OnPaint)
505 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
&pos
, const wxSize
&size
, long style
)
506 : wxScrolledWindow(frame
, wxID_ANY
, pos
, size
, style
)
508 SetBackgroundColour(*wxWHITE
);
511 //void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(evt))
512 void MyCanvas::OnDraw(wxDC
& dc
)
514 //wxPaintDC dc(this);
519 // ----------------------------------------------------------------------------
521 // ----------------------------------------------------------------------------
523 bool MyPrintout::OnPrintPage(int page
)
533 // Draw page numbers at top left corner of printable area, sized so that
534 // screen size of text matches paper size.
535 MapScreenSizeToPage();
537 dc
->DrawText(wxString::Format(wxT("PAGE %d"), page
), 0, 0);
545 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
547 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
553 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
561 bool MyPrintout::HasPage(int pageNum
)
563 return (pageNum
== 1 || pageNum
== 2);
566 void MyPrintout::DrawPageOne()
568 // You might use THIS code if you were scaling graphics of known size to fit
569 // on the page. The commented-out code illustrates different ways of scaling
572 // We know the graphic is 230x350. If we didn't know this, we'd need to
577 // This sets the user scale and origin of the DC so that the image fits
578 // within the paper rectangle (but the edges could be cut off by printers
579 // that can't print to the edges of the paper -- which is most of them. Use
580 // this if your image already has its own margins.
581 // FitThisSizeToPaper(wxSize(maxX, maxY));
582 // wxRect fitRect = GetLogicalPaperRect();
584 // This sets the user scale and origin of the DC so that the image fits
585 // within the page rectangle, which is the printable area on Mac and MSW
586 // and is the entire page on other platforms.
587 // FitThisSizeToPage(wxSize(maxX, maxY));
588 // wxRect fitRect = GetLogicalPageRect();
590 // This sets the user scale and origin of the DC so that the image fits
591 // within the page margins as specified by g_PageSetupData, which you can
592 // change (on some platforms, at least) in the Page Setup dialog. Note that
593 // on Mac, the native Page Setup dialog doesn't let you change the margins
594 // of a wxPageSetupDialogData object, so you'll have to write your own dialog or
595 // use the Mac-only wxMacPageMarginsDialog, as we do in this program.
596 FitThisSizeToPageMargins(wxSize(maxX
, maxY
), *g_pageSetupData
);
597 wxRect fitRect
= GetLogicalPageMarginsRect(*g_pageSetupData
);
599 // This sets the user scale and origin of the DC so that the image appears
600 // on the paper at the same size that it appears on screen (i.e., 10-point
601 // type on screen is 10-point on the printed page) and is positioned in the
602 // top left corner of the page rectangle (just as the screen image appears
603 // in the top left corner of the window).
604 // MapScreenSizeToPage();
605 // wxRect fitRect = GetLogicalPageRect();
607 // You could also map the screen image to the entire paper at the same size
608 // as it appears on screen.
609 // MapScreenSizeToPaper();
610 // wxRect fitRect = GetLogicalPaperRect();
612 // You might also wish to do you own scaling in order to draw objects at
613 // full native device resolution. In this case, you should do the following.
614 // Note that you can use the GetLogicalXXXRect() commands to obtain the
615 // appropriate rect to scale to.
616 // MapScreenSizeToDevice();
617 // wxRect fitRect = GetLogicalPageRect();
619 // Each of the preceding Fit or Map routines positions the origin so that
620 // the drawn image is positioned at the top left corner of the reference
621 // rectangle. You can easily center or right- or bottom-justify the image as
624 // This offsets the image so that it is centered within the reference
625 // rectangle defined above.
626 wxCoord xoff
= (fitRect
.width
- maxX
) / 2;
627 wxCoord yoff
= (fitRect
.height
- maxY
) / 2;
628 OffsetLogicalOrigin(xoff
, yoff
);
630 // This offsets the image so that it is positioned at the bottom right of
631 // the reference rectangle defined above.
632 // wxCoord xoff = (fitRect.width - maxX);
633 // wxCoord yoff = (fitRect.height - maxY);
634 // OffsetLogicalOrigin(xoff, yoff);
636 wxGetApp().Draw(*GetDC());
639 void MyPrintout::DrawPageTwo()
641 // You might use THIS code to set the printer DC to ROUGHLY reflect
642 // the screen text size. This page also draws lines of actual length
645 // Compare this to DrawPageOne(), which uses the really convenient routines
646 // from wxPrintout to fit the screen image onto the printed page. This page
647 // illustrates how to do all the scaling calculations yourself, if you're so
652 // Get the logical pixels per inch of screen and printer
653 int ppiScreenX
, ppiScreenY
;
654 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
655 int ppiPrinterX
, ppiPrinterY
;
656 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
658 // This scales the DC so that the printout roughly represents the screen
659 // scaling. The text point size _should_ be the right size but in fact is
660 // too small for some reason. This is a detail that will need to be
661 // addressed at some point but can be fudged for the moment.
662 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
664 // Now we have to check in case our real page size is reduced (e.g. because
665 // we're drawing to a print preview memory DC)
666 int pageWidth
, pageHeight
;
669 GetPageSizePixels(&pageWidth
, &pageHeight
);
671 // If printer pageWidth == current DC width, then this doesn't change. But w
672 // might be the preview bitmap width, so scale down.
673 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
674 dc
->SetUserScale(overallScale
, overallScale
);
676 // Calculate conversion factor for converting millimetres into logical
677 // units. There are approx. 25.4 mm to the inch. There are ppi device units
678 // to the inch. Therefore 1 mm corresponds to ppi/25.4 device units. We also
679 // divide by the screen-to-printer scaling factor, because we need to
680 // unscale to pass logical units to DrawLine.
682 // Draw 50 mm by 50 mm L shape
683 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.4));
684 float logUnits
= (float)(50*logUnitsFactor
);
685 dc
->SetPen(* wxBLACK_PEN
);
686 dc
->DrawLine(50, 250, (long)(50.0 + logUnits
), 250);
687 dc
->DrawLine(50, 250, 50, (long)(250.0 + logUnits
));
689 dc
->SetBackgroundMode(wxTRANSPARENT
);
690 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
692 { // GetTextExtent demo:
693 wxString words
[7] = { wxT("This "), wxT("is "), wxT("GetTextExtent "),
694 wxT("testing "), wxT("string. "), wxT("Enjoy "), wxT("it!") };
696 long x
= 200, y
= 250;
697 wxFont
fnt(15, wxSWISS
, wxNORMAL
, wxNORMAL
);
701 for (int i
= 0; i
< 7; i
++)
703 wxString word
= words
[i
];
704 word
.Remove( word
.Len()-1, 1 );
705 dc
->GetTextExtent(word
, &w
, &h
);
706 dc
->DrawRectangle(x
, y
, w
, h
);
707 dc
->GetTextExtent(words
[i
], &w
, &h
);
708 dc
->DrawText(words
[i
], x
, y
);
714 dc
->SetFont(wxGetApp().GetTestFont());
716 dc
->DrawText(wxT("Some test text"), 200, 300 );
721 int rightMargin
= 20;
723 int bottomMargin
= 20;
725 int pageWidthMM
, pageHeightMM
;
726 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
728 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
729 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
730 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
731 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
733 dc
->SetPen(* wxRED_PEN
);
734 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
735 (long)rightMarginLogical
, (long)topMarginLogical
);
736 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
737 (long)rightMarginLogical
, (long)bottomMarginLogical
);
739 WritePageHeader(this, dc
, wxT("A header"), logUnitsFactor
);
742 // Writes a header on a page. Margin units are in millimetres.
743 bool MyPrintout::WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, const wxString
&text
, float mmToLogical
)
746 static wxFont
*headerFont
= (wxFont
*) NULL
;
749 headerFont
= wxTheFontList
->FindOrCreateFont(16, wxSWISS
, wxNORMAL
, wxBOLD
);
751 dc
->SetFont(headerFont
);
754 int pageWidthMM
, pageHeightMM
;
756 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
757 wxUnusedVar(pageHeightMM
);
761 int rightMargin
= 10;
763 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
764 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
765 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
767 wxCoord xExtent
, yExtent
;
768 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
770 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
771 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
773 dc
->SetPen(* wxBLACK_PEN
);
774 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
775 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);