]> git.saurik.com Git - wxWidgets.git/blame - samples/printing/printing.cpp
removed duplicated members which have been moved to wxTreeCtrlBase
[wxWidgets.git] / samples / printing / printing.cpp
CommitLineData
6adaedf0
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: printing.cpp
be5a51fb 3// Purpose: Printing demo for wxWidgets
6adaedf0
JS
4// Author: Julian Smart
5// Modified by:
6// Created: 1995
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
c801d85f 11
c801d85f
KB
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
c801d85f
KB
21#endif
22
e4b19d9b 23#if !wxUSE_PRINTING_ARCHITECTURE
9134af5b 24#error "You must set wxUSE_PRINTING_ARCHITECTURE to 1 in setup.h, and recompile the library."
c801d85f
KB
25#endif
26
dfad0599
JS
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
c801d85f
KB
31#include <ctype.h>
32#include "wx/metafile.h"
33#include "wx/print.h"
34#include "wx/printdlg.h"
cac7ac30 35#include "wx/image.h"
031b2a7b
RR
36#include "wx/accel.h"
37
dfad0599 38#if wxTEST_POSTSCRIPT_IN_MSW
c801d85f
KB
39#include "wx/generic/printps.h"
40#include "wx/generic/prntdlgg.h"
dfad0599 41#endif
c801d85f
KB
42
43#include "printing.h"
44
4bc67cc5
RR
45#ifndef __WXMSW__
46#include "mondrian.xpm"
47#endif
48
4843cdfe
RR
49#if wxUSE_LIBGNOMEPRINT
50#include "wx/html/forcelnk.h"
51FORCE_LINK(gnome_print)
52#endif
53
54
c801d85f 55// Declare a frame
c67daf87 56MyFrame *frame = (MyFrame *) NULL;
7bcb11d3
JS
57// int orientation = wxPORTRAIT;
58
59// Global print data, to remember settings during the session
60wxPrintData *g_printData = (wxPrintData*) NULL ;
61
62// Global page setup data
63wxPageSetupData* g_pageSetupData = (wxPageSetupData*) NULL;
c801d85f
KB
64
65// Main proc
66IMPLEMENT_APP(MyApp)
67
c801d85f 68// Writes a header on a page. Margin units are in millimetres.
fbfb8bcc 69bool WritePageHeader(wxPrintout *printout, wxDC *dc, const wxChar *text, float mmToLogical);
c801d85f 70
c801d85f
KB
71// The `main program' equivalent, creating the windows and returning the
72// main frame
7b25d8e7 73
c801d85f
KB
74bool MyApp::OnInit(void)
75{
cac7ac30
RR
76 wxInitAllImageHandlers();
77
9134af5b
DS
78 m_testFont.Create(10, wxSWISS, wxNORMAL, wxNORMAL);
79
7bcb11d3
JS
80 g_printData = new wxPrintData;
81 g_pageSetupData = new wxPageSetupDialogData;
9134af5b 82
7bcb11d3 83 // Create the main frame window
be5a51fb 84 frame = new MyFrame((wxFrame *) NULL, _T("wxWidgets Printing Demo"), wxPoint(0, 0), wxSize(400, 400));
9134af5b 85
8520f137 86#if wxUSE_STATUSBAR
7bcb11d3
JS
87 // Give it a status line
88 frame->CreateStatusBar(2);
8520f137 89#endif // wxUSE_STATUSBAR
9134af5b 90
7bcb11d3
JS
91 // Load icon and bitmap
92 frame->SetIcon( wxICON( mondrian) );
9134af5b 93
7bcb11d3
JS
94 // Make a menubar
95 wxMenu *file_menu = new wxMenu;
9134af5b 96
600683ca 97 file_menu->Append(WXPRINT_PRINT, _T("&Print..."), _T("Print"));
600683ca
MB
98 file_menu->Append(WXPRINT_PAGE_SETUP, _T("Page Set&up..."), _T("Page setup"));
99 file_menu->Append(WXPRINT_PREVIEW, _T("Print Pre&view"), _T("Preview"));
9134af5b 100
3d2d8da1 101#if wxUSE_ACCEL
031b2a7b
RR
102 // Accelerators
103 wxAcceleratorEntry entries[1];
104 entries[0].Set(wxACCEL_CTRL, (int) 'V', WXPRINT_PREVIEW);
105 wxAcceleratorTable accel(1, entries);
106 frame->SetAcceleratorTable(accel);
3d2d8da1
RR
107#endif
108
dfad0599 109#if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
7bcb11d3 110 file_menu->AppendSeparator();
600683ca 111 file_menu->Append(WXPRINT_PRINT_PS, _T("Print PostScript..."), _T("Print (PostScript)"));
600683ca
MB
112 file_menu->Append(WXPRINT_PAGE_SETUP_PS, _T("Page Setup PostScript..."), _T("Page setup (PostScript)"));
113 file_menu->Append(WXPRINT_PREVIEW_PS, _T("Print Preview PostScript"), _T("Preview (PostScript)"));
c801d85f 114#endif
cac7ac30
RR
115 file_menu->AppendSeparator();
116 file_menu->Append(WXPRINT_ANGLEUP, _T("Angle up\tAlt-U"), _T("Raise rotated text angle"));
117 file_menu->Append(WXPRINT_ANGLEDOWN, _T("Angle down\tAlt-D"), _T("Lower rotated text angle"));
7bcb11d3 118 file_menu->AppendSeparator();
600683ca 119 file_menu->Append(WXPRINT_QUIT, _T("E&xit"), _T("Exit program"));
9134af5b 120
7bcb11d3 121 wxMenu *help_menu = new wxMenu;
600683ca 122 help_menu->Append(WXPRINT_ABOUT, _T("&About"), _T("About this demo"));
9134af5b 123
7bcb11d3 124 wxMenuBar *menu_bar = new wxMenuBar;
9134af5b 125
600683ca
MB
126 menu_bar->Append(file_menu, _T("&File"));
127 menu_bar->Append(help_menu, _T("&Help"));
9134af5b 128
7bcb11d3
JS
129 // Associate the menu bar with the frame
130 frame->SetMenuBar(menu_bar);
9134af5b 131
7bcb11d3 132 MyCanvas *canvas = new MyCanvas(frame, wxPoint(0, 0), wxSize(100, 100), wxRETAINED|wxHSCROLL|wxVSCROLL);
9134af5b 133
7bcb11d3
JS
134 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
135 canvas->SetScrollbars(20, 20, 50, 50);
9134af5b 136
7bcb11d3 137 frame->canvas = canvas;
9134af5b 138
7bcb11d3 139 frame->Centre(wxBOTH);
9134af5b
DS
140 frame->Show();
141
8520f137 142#if wxUSE_STATUSBAR
600683ca 143 frame->SetStatusText(_T("Printing demo"));
8520f137 144#endif // wxUSE_STATUSBAR
9134af5b 145
7bcb11d3 146 SetTopWindow(frame);
9134af5b
DS
147
148 return true;
c801d85f
KB
149}
150
e3065973
JS
151int MyApp::OnExit()
152{
7bcb11d3
JS
153 delete g_printData;
154 delete g_pageSetupData;
155 return 1;
e3065973
JS
156}
157
c801d85f 158BEGIN_EVENT_TABLE(MyFrame, wxFrame)
7bcb11d3
JS
159EVT_MENU(WXPRINT_QUIT, MyFrame::OnExit)
160EVT_MENU(WXPRINT_PRINT, MyFrame::OnPrint)
161EVT_MENU(WXPRINT_PREVIEW, MyFrame::OnPrintPreview)
7bcb11d3
JS
162EVT_MENU(WXPRINT_PAGE_SETUP, MyFrame::OnPageSetup)
163EVT_MENU(WXPRINT_ABOUT, MyFrame::OnPrintAbout)
dfad0599 164#if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
7bcb11d3
JS
165EVT_MENU(WXPRINT_PRINT_PS, MyFrame::OnPrintPS)
166EVT_MENU(WXPRINT_PREVIEW_PS, MyFrame::OnPrintPreviewPS)
7bcb11d3 167EVT_MENU(WXPRINT_PAGE_SETUP_PS, MyFrame::OnPageSetupPS)
dfad0599 168#endif
cac7ac30
RR
169EVT_MENU(WXPRINT_ANGLEUP, MyFrame::OnAngleUp)
170EVT_MENU(WXPRINT_ANGLEDOWN, MyFrame::OnAngleDown)
c801d85f
KB
171END_EVENT_TABLE()
172
173// Define my frame constructor
174MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
9134af5b 175wxFrame(frame, wxID_ANY, title, pos, size)
c801d85f 176{
9134af5b 177 canvas = NULL;
cac7ac30
RR
178 m_angle = 30;
179#if 0
180 wxImage image( wxT("test.jpg") );
181 image.SetAlpha();
182 int i,j;
183 for (i = 0; i < image.GetWidth(); i++)
184 for (j = 0; j < image.GetHeight(); j++)
185 image.SetAlpha( i, j, 50 );
186 m_bitmap = image;
187#endif
c801d85f
KB
188}
189
e3e65dac 190void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
c801d85f 191{
9134af5b 192 Close(true /*force closing*/);
c801d85f
KB
193}
194
e3e65dac 195void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
c801d85f 196{
7bcb11d3
JS
197 wxPrintDialogData printDialogData(* g_printData);
198
199 wxPrinter printer(& printDialogData);
600683ca 200 MyPrintout printout(_T("My printout"));
9134af5b 201 if (!printer.Print(this, &printout, true /*prompt*/))
f6bcfd97
BP
202 {
203 if (wxPrinter::GetLastError() == wxPRINTER_ERROR)
600683ca 204 wxMessageBox(_T("There was a problem printing.\nPerhaps your current printer is not set correctly?"), _T("Printing"), wxOK);
f6bcfd97 205 else
600683ca 206 wxMessageBox(_T("You canceled printing"), _T("Printing"), wxOK);
f6bcfd97 207 }
7bcb11d3
JS
208 else
209 {
210 (*g_printData) = printer.GetPrintDialogData().GetPrintData();
211 }
c801d85f
KB
212}
213
e3e65dac 214void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
c801d85f 215{
7bcb11d3
JS
216 // Pass two printout objects: for preview, and possible printing.
217 wxPrintDialogData printDialogData(* g_printData);
218 wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printDialogData);
219 if (!preview->Ok())
220 {
c801d85f 221 delete preview;
600683ca 222 wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK);
c801d85f 223 return;
7bcb11d3 224 }
9134af5b 225
600683ca 226 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
7bcb11d3
JS
227 frame->Centre(wxBOTH);
228 frame->Initialize();
9134af5b 229 frame->Show();
c801d85f
KB
230}
231
e3e65dac 232void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
c801d85f 233{
cac7ac30 234 (*g_pageSetupData) = *g_printData;
c801d85f 235
7bcb11d3
JS
236 wxPageSetupDialog pageSetupDialog(this, g_pageSetupData);
237 pageSetupDialog.ShowModal();
9134af5b 238
7bcb11d3
JS
239 (*g_printData) = pageSetupDialog.GetPageSetupData().GetPrintData();
240 (*g_pageSetupData) = pageSetupDialog.GetPageSetupData();
c801d85f
KB
241}
242
dfad0599
JS
243#if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
244void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
245{
7bcb11d3 246 wxPostScriptPrinter printer(g_printData);
600683ca 247 MyPrintout printout(_T("My printout"));
9134af5b 248 printer.Print(this, &printout, true/*prompt*/);
7bcb11d3
JS
249
250 (*g_printData) = printer.GetPrintData();
dfad0599
JS
251}
252
253void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
254{
7bcb11d3
JS
255 // Pass two printout objects: for preview, and possible printing.
256 wxPrintDialogData printDialogData(* g_printData);
257 wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printDialogData);
600683ca 258 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
7bcb11d3
JS
259 frame->Centre(wxBOTH);
260 frame->Initialize();
9134af5b 261 frame->Show();
dfad0599
JS
262}
263
e3e65dac 264void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
c801d85f 265{
7bcb11d3 266 (*g_pageSetupData) = * g_printData;
c801d85f 267
7bcb11d3
JS
268 wxGenericPageSetupDialog pageSetupDialog(this, g_pageSetupData);
269 pageSetupDialog.ShowModal();
c801d85f 270
7bcb11d3
JS
271 (*g_printData) = pageSetupDialog.GetPageSetupData().GetPrintData();
272 (*g_pageSetupData) = pageSetupDialog.GetPageSetupData();
c801d85f 273}
dfad0599
JS
274#endif
275
c801d85f 276
e3e65dac 277void MyFrame::OnPrintAbout(wxCommandEvent& WXUNUSED(event))
c801d85f 278{
be5a51fb
JS
279 (void)wxMessageBox(_T("wxWidgets printing demo\nAuthor: Julian Smart"),
280 _T("About wxWidgets printing demo"), wxOK|wxCENTRE);
c801d85f
KB
281}
282
abc2a4cc 283void MyFrame::OnAngleUp(wxCommandEvent& WXUNUSED(event))
cac7ac30
RR
284{
285 m_angle += 5;
286 canvas->Refresh();
287}
288
abc2a4cc 289void MyFrame::OnAngleDown(wxCommandEvent& WXUNUSED(event))
cac7ac30
RR
290{
291 m_angle -= 5;
292 canvas->Refresh();
293}
294
c801d85f
KB
295void MyFrame::Draw(wxDC& dc)
296{
6adaedf0
JS
297 dc.SetBackground(*wxWHITE_BRUSH);
298 dc.Clear();
9134af5b
DS
299 dc.SetFont(wxGetApp().m_testFont);
300
7bcb11d3 301 dc.SetBackgroundMode(wxTRANSPARENT);
9134af5b 302
cac7ac30
RR
303 dc.SetBrush(*wxCYAN_BRUSH);
304 dc.SetPen(*wxRED_PEN);
9134af5b 305
3d7cfc2f 306 dc.DrawRoundedRectangle(0, 20, 200, 80, 20);
9134af5b 307
3d7cfc2f 308 dc.DrawText( wxT("Rectangle 200 by 80"), 40, 40);
9134af5b 309
cac7ac30 310 dc.SetPen( wxPen(*wxBLACK,0,wxDOT_DASH) );
7bcb11d3 311 dc.DrawEllipse(50, 140, 100, 50);
cac7ac30 312 dc.SetPen(*wxRED_PEN);
9134af5b 313
92980e90 314 dc.DrawText( wxT("Test message: this is in 10 point text"), 10, 180);
3d7cfc2f 315
abc2a4cc 316
92980e90 317#if wxUSE_UNICODE
77efa6a8 318 char *test = "Hebrew שלום -- Japanese (日本語)";
92980e90 319 wxString tmp = wxConvUTF8.cMB2WC( test );
9134af5b 320 dc.DrawText( tmp, 10, 200 );
92980e90 321#endif
9134af5b 322
3d7cfc2f
RR
323 wxPoint points[5];
324 points[0].x = 0;
325 points[0].y = 0;
326 points[1].x = 20;
327 points[1].y = 0;
328 points[2].x = 20;
329 points[2].y = 20;
330 points[3].x = 10;
331 points[3].y = 20;
332 points[4].x = 10;
333 points[4].y = -20;
334 dc.DrawPolygon( 5, points, 20, 250, wxODDEVEN_RULE );
335 dc.DrawPolygon( 5, points, 50, 250, wxWINDING_RULE );
336
337 dc.DrawEllipticArc( 80, 250, 60, 30, 0.0, 270.0 );
338
339 points[0].x = 150;
340 points[0].y = 250;
341 points[1].x = 180;
342 points[1].y = 250;
343 points[2].x = 180;
344 points[2].y = 220;
345 points[3].x = 200;
346 points[3].y = 220;
347 dc.DrawSpline( 4, points );
348
349 dc.DrawArc( 20,10, 10,10, 25,40 );
350
cac7ac30
RR
351 wxString str;
352 int i = 0;
353 str.Printf( wxT("---- Text at angle %d ----"), i );
354 dc.DrawRotatedText( str, 100, 300, i );
355
356 i = m_angle;
357 str.Printf( wxT("---- Text at angle %d ----"), i );
358 dc.DrawRotatedText( str, 100, 300, i );
359
7bcb11d3
JS
360 dc.SetPen(* wxBLACK_PEN);
361 dc.DrawLine(0, 0, 200, 200);
362 dc.DrawLine(200, 0, 0, 200);
9134af5b 363
7bcb11d3 364 wxIcon my_icon = wxICON(mondrian) ;
9134af5b 365
7bcb11d3 366 dc.DrawIcon( my_icon, 100, 100);
abc2a4cc 367
cac7ac30
RR
368 if (m_bitmap.Ok())
369 dc.DrawBitmap( m_bitmap, 10, 10 );
c801d85f
KB
370}
371
e3e65dac 372void MyFrame::OnSize(wxSizeEvent& event )
c801d85f
KB
373{
374 wxFrame::OnSize(event);
375}
376
377BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
7bcb11d3 378EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
c801d85f
KB
379END_EVENT_TABLE()
380
381// Define a constructor for my canvas
382MyCanvas::MyCanvas(wxFrame *frame, const wxPoint& pos, const wxSize& size, long style):
9134af5b 383 wxScrolledWindow(frame, wxID_ANY, pos, size, style)
c801d85f 384{
7bcb11d3 385 SetBackgroundColour(* wxWHITE);
c801d85f
KB
386}
387
c801d85f
KB
388// Define the repainting behaviour
389void MyCanvas::OnDraw(wxDC& dc)
390{
7bcb11d3 391 frame->Draw(dc);
c801d85f
KB
392}
393
e3e65dac 394void MyCanvas::OnEvent(wxMouseEvent& WXUNUSED(event))
c801d85f
KB
395{
396}
397
c801d85f
KB
398bool MyPrintout::OnPrintPage(int page)
399{
7bcb11d3
JS
400 wxDC *dc = GetDC();
401 if (dc)
402 {
403 if (page == 1)
404 DrawPageOne(dc);
405 else if (page == 2)
406 DrawPageTwo(dc);
9134af5b 407
7bcb11d3
JS
408 dc->SetDeviceOrigin(0, 0);
409 dc->SetUserScale(1.0, 1.0);
9134af5b 410
92980e90
RR
411 wxChar buf[200];
412 wxSprintf(buf, wxT("PAGE %d"), page);
77efa6a8 413 dc->DrawText(buf, 10, 10);
9134af5b
DS
414
415 return true;
7bcb11d3
JS
416 }
417 else
9134af5b 418 return false;
c801d85f
KB
419}
420
421bool MyPrintout::OnBeginDocument(int startPage, int endPage)
422{
7bcb11d3 423 if (!wxPrintout::OnBeginDocument(startPage, endPage))
9134af5b
DS
424 return false;
425
426 return true;
c801d85f
KB
427}
428
429void MyPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
430{
7bcb11d3
JS
431 *minPage = 1;
432 *maxPage = 2;
433 *selPageFrom = 1;
434 *selPageTo = 2;
c801d85f
KB
435}
436
437bool MyPrintout::HasPage(int pageNum)
438{
7bcb11d3 439 return (pageNum == 1 || pageNum == 2);
c801d85f
KB
440}
441
442void MyPrintout::DrawPageOne(wxDC *dc)
443{
77efa6a8
RR
444 // You might use THIS code if you were scaling
445 // graphics of known size to fit on the page.
9134af5b 446
7bcb11d3
JS
447 // We know the graphic is 200x200. If we didn't know this,
448 // we'd need to calculate it.
449 float maxX = 200;
450 float maxY = 200;
9134af5b 451
7bcb11d3
JS
452 // Let's have at least 50 device units margin
453 float marginX = 50;
454 float marginY = 50;
9134af5b 455
7bcb11d3
JS
456 // Add the margin to the graphic size
457 maxX += (2*marginX);
458 maxY += (2*marginY);
9134af5b 459
7bcb11d3 460 // Get the size of the DC in pixels
77efa6a8 461 int w, h;
7bcb11d3 462 dc->GetSize(&w, &h);
9134af5b 463
7bcb11d3
JS
464 // Calculate a suitable scaling factor
465 float scaleX=(float)(w/maxX);
466 float scaleY=(float)(h/maxY);
9134af5b 467
7bcb11d3
JS
468 // Use x or y scaling factor, whichever fits on the DC
469 float actualScale = wxMin(scaleX,scaleY);
9134af5b 470
7bcb11d3
JS
471 // Calculate the position on the DC for centring the graphic
472 float posX = (float)((w - (200*actualScale))/2.0);
473 float posY = (float)((h - (200*actualScale))/2.0);
9134af5b 474
7bcb11d3
JS
475 // Set the scale and origin
476 dc->SetUserScale(actualScale, actualScale);
477 dc->SetDeviceOrigin( (long)posX, (long)posY );
9134af5b 478
7bcb11d3 479 frame->Draw(*dc);
c801d85f
KB
480}
481
482void MyPrintout::DrawPageTwo(wxDC *dc)
483{
77efa6a8
RR
484 // You might use THIS code to set the printer DC to ROUGHLY reflect
485 // the screen text size. This page also draws lines of actual length
486 // 5cm on the page.
9134af5b 487
7bcb11d3
JS
488 // Get the logical pixels per inch of screen and printer
489 int ppiScreenX, ppiScreenY;
490 GetPPIScreen(&ppiScreenX, &ppiScreenY);
491 int ppiPrinterX, ppiPrinterY;
492 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
9134af5b 493
7bcb11d3
JS
494 // This scales the DC so that the printout roughly represents the
495 // the screen scaling. The text point size _should_ be the right size
496 // but in fact is too small for some reason. This is a detail that will
497 // need to be addressed at some point but can be fudged for the
498 // moment.
499 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
9134af5b 500
7bcb11d3
JS
501 // Now we have to check in case our real page size is reduced
502 // (e.g. because we're drawing to a print preview memory DC)
503 int pageWidth, pageHeight;
504 int w, h;
505 dc->GetSize(&w, &h);
506 GetPageSizePixels(&pageWidth, &pageHeight);
9134af5b 507
7bcb11d3
JS
508 // If printer pageWidth == current DC width, then this doesn't
509 // change. But w might be the preview bitmap width, so scale down.
510 float overallScale = scale * (float)(w/(float)pageWidth);
511 dc->SetUserScale(overallScale, overallScale);
9134af5b 512
7bcb11d3
JS
513 // Calculate conversion factor for converting millimetres into
514 // logical units.
70949ed5 515 // There are approx. 25.4 mm to the inch. There are ppi
7bcb11d3 516 // device units to the inch. Therefore 1 mm corresponds to
70949ed5 517 // ppi/25.4 device units. We also divide by the
7bcb11d3
JS
518 // screen-to-printer scaling factor, because we need to
519 // unscale to pass logical units to DrawLine.
9134af5b 520
7bcb11d3 521 // Draw 50 mm by 50 mm L shape
70949ed5 522 float logUnitsFactor = (float)(ppiPrinterX/(scale*25.4));
7bcb11d3
JS
523 float logUnits = (float)(50*logUnitsFactor);
524 dc->SetPen(* wxBLACK_PEN);
525 dc->DrawLine(50, 250, (long)(50.0 + logUnits), 250);
526 dc->DrawLine(50, 250, 50, (long)(250.0 + logUnits));
9134af5b 527
7bcb11d3 528 dc->SetBackgroundMode(wxTRANSPARENT);
1db4e8dd 529 dc->SetBrush(*wxTRANSPARENT_BRUSH);
9134af5b 530
d57bf685 531 { // GetTextExtent demo:
600683ca 532 wxString words[7] = {_T("This "), _T("is "), _T("GetTextExtent "), _T("testing "), _T("string. "), _T("Enjoy "), _T("it!")};
d57bf685
VS
533 long w, h;
534 long x = 200, y= 250;
535 wxFont fnt(15, wxSWISS, wxNORMAL, wxNORMAL);
9134af5b 536
d57bf685 537 dc->SetFont(fnt);
9134af5b 538
77efa6a8
RR
539 for (int i = 0; i < 7; i++)
540 {
541 wxString word = words[i];
542 word.Remove( word.Len()-1, 1 );
543 dc->GetTextExtent(word, &w, &h);
d57bf685 544 dc->DrawRectangle(x, y, w, h);
77efa6a8 545 dc->GetTextExtent(words[i], &w, &h);
d57bf685
VS
546 dc->DrawText(words[i], x, y);
547 x += w;
548 }
9134af5b 549
d57bf685 550 }
9134af5b
DS
551
552 dc->SetFont(wxGetApp().m_testFont);
553
600683ca 554 dc->DrawText(_T("Some test text"), 200, 300 );
92980e90 555
7bcb11d3 556 // TESTING
9134af5b 557
7bcb11d3
JS
558 int leftMargin = 20;
559 int rightMargin = 20;
560 int topMargin = 20;
561 int bottomMargin = 20;
9134af5b 562
7bcb11d3
JS
563 int pageWidthMM, pageHeightMM;
564 GetPageSizeMM(&pageWidthMM, &pageHeightMM);
9134af5b 565
7bcb11d3
JS
566 float leftMarginLogical = (float)(logUnitsFactor*leftMargin);
567 float topMarginLogical = (float)(logUnitsFactor*topMargin);
568 float bottomMarginLogical = (float)(logUnitsFactor*(pageHeightMM - bottomMargin));
569 float rightMarginLogical = (float)(logUnitsFactor*(pageWidthMM - rightMargin));
9134af5b 570
7bcb11d3 571 dc->SetPen(* wxRED_PEN);
9134af5b 572 dc->DrawLine( (long)leftMarginLogical, (long)topMarginLogical,
7bcb11d3 573 (long)rightMarginLogical, (long)topMarginLogical);
9134af5b 574 dc->DrawLine( (long)leftMarginLogical, (long)bottomMarginLogical,
7bcb11d3 575 (long)rightMarginLogical, (long)bottomMarginLogical);
9134af5b 576
600683ca 577 WritePageHeader(this, dc, _T("A header"), logUnitsFactor);
c801d85f
KB
578}
579
580// Writes a header on a page. Margin units are in millimetres.
fbfb8bcc 581bool WritePageHeader(wxPrintout *printout, wxDC *dc, const wxChar *text, float mmToLogical)
c801d85f 582{
ed880dd4 583/*
7bcb11d3
JS
584static wxFont *headerFont = (wxFont *) NULL;
585if (!headerFont)
586{
587headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
588}
589dc->SetFont(headerFont);
590 */
9134af5b 591
7bcb11d3 592 int pageWidthMM, pageHeightMM;
9134af5b 593
7bcb11d3 594 printout->GetPageSizeMM(&pageWidthMM, &pageHeightMM);
8afa4fde 595 wxUnusedVar(pageHeightMM);
9134af5b 596
7bcb11d3
JS
597 int leftMargin = 10;
598 int topMargin = 10;
599 int rightMargin = 10;
9134af5b 600
7bcb11d3
JS
601 float leftMarginLogical = (float)(mmToLogical*leftMargin);
602 float topMarginLogical = (float)(mmToLogical*topMargin);
603 float rightMarginLogical = (float)(mmToLogical*(pageWidthMM - rightMargin));
9134af5b 604
7bcb11d3
JS
605 long xExtent, yExtent;
606 dc->GetTextExtent(text, &xExtent, &yExtent);
607 float xPos = (float)(((((pageWidthMM - leftMargin - rightMargin)/2.0)+leftMargin)*mmToLogical) - (xExtent/2.0));
608 dc->DrawText(text, (long)xPos, (long)topMarginLogical);
9134af5b 609
7bcb11d3 610 dc->SetPen(* wxBLACK_PEN);
9134af5b 611 dc->DrawLine( (long)leftMarginLogical, (long)(topMarginLogical+yExtent),
7bcb11d3 612 (long)rightMarginLogical, (long)topMarginLogical+yExtent );
9134af5b
DS
613
614 return true;
c801d85f 615}