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