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