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