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