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