]>
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 |
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 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 | { | |
9134af5b DS |
77 | m_testFont.Create(10, wxSWISS, wxNORMAL, wxNORMAL); |
78 | ||
7bcb11d3 JS |
79 | g_printData = new wxPrintData; |
80 | g_pageSetupData = new wxPageSetupDialogData; | |
9134af5b | 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)); |
9134af5b | 84 | |
7bcb11d3 JS |
85 | // Give it a status line |
86 | frame->CreateStatusBar(2); | |
9134af5b | 87 | |
7bcb11d3 JS |
88 | // Load icon and bitmap |
89 | frame->SetIcon( wxICON( mondrian) ); | |
9134af5b | 90 | |
7bcb11d3 JS |
91 | // Make a menubar |
92 | wxMenu *file_menu = new wxMenu; | |
9134af5b | 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")); | |
9134af5b | 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")); |
9134af5b | 116 | |
7bcb11d3 | 117 | wxMenu *help_menu = new wxMenu; |
600683ca | 118 | help_menu->Append(WXPRINT_ABOUT, _T("&About"), _T("About this demo")); |
9134af5b | 119 | |
7bcb11d3 | 120 | wxMenuBar *menu_bar = new wxMenuBar; |
9134af5b | 121 | |
600683ca MB |
122 | menu_bar->Append(file_menu, _T("&File")); |
123 | menu_bar->Append(help_menu, _T("&Help")); | |
9134af5b | 124 | |
7bcb11d3 JS |
125 | // Associate the menu bar with the frame |
126 | frame->SetMenuBar(menu_bar); | |
9134af5b | 127 | |
7bcb11d3 | 128 | MyCanvas *canvas = new MyCanvas(frame, wxPoint(0, 0), wxSize(100, 100), wxRETAINED|wxHSCROLL|wxVSCROLL); |
9134af5b | 129 | |
7bcb11d3 JS |
130 | // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction |
131 | canvas->SetScrollbars(20, 20, 50, 50); | |
9134af5b | 132 | |
7bcb11d3 | 133 | frame->canvas = canvas; |
9134af5b | 134 | |
7bcb11d3 | 135 | frame->Centre(wxBOTH); |
9134af5b DS |
136 | frame->Show(); |
137 | ||
600683ca | 138 | frame->SetStatusText(_T("Printing demo")); |
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 | { |
600683ca MB |
285 | (void)wxMessageBox(_T("wxWindows printing demo\nAuthor: Julian Smart"), |
286 | _T("About wxWindows 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 | ||
339 | MyCanvas::~MyCanvas(void) | |
340 | { | |
341 | } | |
342 | ||
343 | // Define the repainting behaviour | |
344 | void MyCanvas::OnDraw(wxDC& dc) | |
345 | { | |
7bcb11d3 | 346 | frame->Draw(dc); |
c801d85f KB |
347 | } |
348 | ||
e3e65dac | 349 | void MyCanvas::OnEvent(wxMouseEvent& WXUNUSED(event)) |
c801d85f KB |
350 | { |
351 | } | |
352 | ||
c801d85f KB |
353 | bool MyPrintout::OnPrintPage(int page) |
354 | { | |
7bcb11d3 JS |
355 | wxDC *dc = GetDC(); |
356 | if (dc) | |
357 | { | |
358 | if (page == 1) | |
359 | DrawPageOne(dc); | |
360 | else if (page == 2) | |
361 | DrawPageTwo(dc); | |
9134af5b | 362 | |
7bcb11d3 JS |
363 | dc->SetDeviceOrigin(0, 0); |
364 | dc->SetUserScale(1.0, 1.0); | |
9134af5b | 365 | |
92980e90 RR |
366 | wxChar buf[200]; |
367 | wxSprintf(buf, wxT("PAGE %d"), page); | |
77efa6a8 | 368 | dc->DrawText(buf, 10, 10); |
9134af5b DS |
369 | |
370 | return true; | |
7bcb11d3 JS |
371 | } |
372 | else | |
9134af5b | 373 | return false; |
c801d85f KB |
374 | } |
375 | ||
376 | bool MyPrintout::OnBeginDocument(int startPage, int endPage) | |
377 | { | |
7bcb11d3 | 378 | if (!wxPrintout::OnBeginDocument(startPage, endPage)) |
9134af5b DS |
379 | return false; |
380 | ||
381 | return true; | |
c801d85f KB |
382 | } |
383 | ||
384 | void MyPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) | |
385 | { | |
7bcb11d3 JS |
386 | *minPage = 1; |
387 | *maxPage = 2; | |
388 | *selPageFrom = 1; | |
389 | *selPageTo = 2; | |
c801d85f KB |
390 | } |
391 | ||
392 | bool MyPrintout::HasPage(int pageNum) | |
393 | { | |
7bcb11d3 | 394 | return (pageNum == 1 || pageNum == 2); |
c801d85f KB |
395 | } |
396 | ||
397 | void MyPrintout::DrawPageOne(wxDC *dc) | |
398 | { | |
77efa6a8 RR |
399 | // You might use THIS code if you were scaling |
400 | // graphics of known size to fit on the page. | |
9134af5b | 401 | |
7bcb11d3 JS |
402 | // We know the graphic is 200x200. If we didn't know this, |
403 | // we'd need to calculate it. | |
404 | float maxX = 200; | |
405 | float maxY = 200; | |
9134af5b | 406 | |
7bcb11d3 JS |
407 | // Let's have at least 50 device units margin |
408 | float marginX = 50; | |
409 | float marginY = 50; | |
9134af5b | 410 | |
7bcb11d3 JS |
411 | // Add the margin to the graphic size |
412 | maxX += (2*marginX); | |
413 | maxY += (2*marginY); | |
9134af5b | 414 | |
7bcb11d3 | 415 | // Get the size of the DC in pixels |
77efa6a8 | 416 | int w, h; |
7bcb11d3 | 417 | dc->GetSize(&w, &h); |
9134af5b | 418 | |
7bcb11d3 JS |
419 | // Calculate a suitable scaling factor |
420 | float scaleX=(float)(w/maxX); | |
421 | float scaleY=(float)(h/maxY); | |
9134af5b | 422 | |
7bcb11d3 JS |
423 | // Use x or y scaling factor, whichever fits on the DC |
424 | float actualScale = wxMin(scaleX,scaleY); | |
9134af5b | 425 | |
7bcb11d3 JS |
426 | // Calculate the position on the DC for centring the graphic |
427 | float posX = (float)((w - (200*actualScale))/2.0); | |
428 | float posY = (float)((h - (200*actualScale))/2.0); | |
9134af5b | 429 | |
7bcb11d3 JS |
430 | // Set the scale and origin |
431 | dc->SetUserScale(actualScale, actualScale); | |
432 | dc->SetDeviceOrigin( (long)posX, (long)posY ); | |
9134af5b | 433 | |
7bcb11d3 | 434 | frame->Draw(*dc); |
c801d85f KB |
435 | } |
436 | ||
437 | void MyPrintout::DrawPageTwo(wxDC *dc) | |
438 | { | |
77efa6a8 RR |
439 | // You might use THIS code to set the printer DC to ROUGHLY reflect |
440 | // the screen text size. This page also draws lines of actual length | |
441 | // 5cm on the page. | |
9134af5b | 442 | |
7bcb11d3 JS |
443 | // Get the logical pixels per inch of screen and printer |
444 | int ppiScreenX, ppiScreenY; | |
445 | GetPPIScreen(&ppiScreenX, &ppiScreenY); | |
446 | int ppiPrinterX, ppiPrinterY; | |
447 | GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); | |
9134af5b | 448 | |
7bcb11d3 JS |
449 | // This scales the DC so that the printout roughly represents the |
450 | // the screen scaling. The text point size _should_ be the right size | |
451 | // but in fact is too small for some reason. This is a detail that will | |
452 | // need to be addressed at some point but can be fudged for the | |
453 | // moment. | |
454 | float scale = (float)((float)ppiPrinterX/(float)ppiScreenX); | |
9134af5b | 455 | |
7bcb11d3 JS |
456 | // Now we have to check in case our real page size is reduced |
457 | // (e.g. because we're drawing to a print preview memory DC) | |
458 | int pageWidth, pageHeight; | |
459 | int w, h; | |
460 | dc->GetSize(&w, &h); | |
461 | GetPageSizePixels(&pageWidth, &pageHeight); | |
9134af5b | 462 | |
7bcb11d3 JS |
463 | // If printer pageWidth == current DC width, then this doesn't |
464 | // change. But w might be the preview bitmap width, so scale down. | |
465 | float overallScale = scale * (float)(w/(float)pageWidth); | |
466 | dc->SetUserScale(overallScale, overallScale); | |
9134af5b | 467 | |
7bcb11d3 JS |
468 | // Calculate conversion factor for converting millimetres into |
469 | // logical units. | |
470 | // There are approx. 25.1 mm to the inch. There are ppi | |
471 | // device units to the inch. Therefore 1 mm corresponds to | |
472 | // ppi/25.1 device units. We also divide by the | |
473 | // screen-to-printer scaling factor, because we need to | |
474 | // unscale to pass logical units to DrawLine. | |
9134af5b | 475 | |
7bcb11d3 JS |
476 | // Draw 50 mm by 50 mm L shape |
477 | float logUnitsFactor = (float)(ppiPrinterX/(scale*25.1)); | |
478 | float logUnits = (float)(50*logUnitsFactor); | |
479 | dc->SetPen(* wxBLACK_PEN); | |
480 | dc->DrawLine(50, 250, (long)(50.0 + logUnits), 250); | |
481 | dc->DrawLine(50, 250, 50, (long)(250.0 + logUnits)); | |
9134af5b | 482 | |
7bcb11d3 | 483 | dc->SetBackgroundMode(wxTRANSPARENT); |
9134af5b DS |
484 | |
485 | ||
d57bf685 | 486 | { // GetTextExtent demo: |
600683ca | 487 | wxString words[7] = {_T("This "), _T("is "), _T("GetTextExtent "), _T("testing "), _T("string. "), _T("Enjoy "), _T("it!")}; |
d57bf685 VS |
488 | long w, h; |
489 | long x = 200, y= 250; | |
490 | wxFont fnt(15, wxSWISS, wxNORMAL, wxNORMAL); | |
9134af5b | 491 | |
d57bf685 | 492 | dc->SetFont(fnt); |
9134af5b | 493 | |
77efa6a8 RR |
494 | for (int i = 0; i < 7; i++) |
495 | { | |
496 | wxString word = words[i]; | |
497 | word.Remove( word.Len()-1, 1 ); | |
498 | dc->GetTextExtent(word, &w, &h); | |
d57bf685 | 499 | dc->DrawRectangle(x, y, w, h); |
77efa6a8 | 500 | dc->GetTextExtent(words[i], &w, &h); |
d57bf685 VS |
501 | dc->DrawText(words[i], x, y); |
502 | x += w; | |
503 | } | |
9134af5b | 504 | |
d57bf685 | 505 | } |
9134af5b DS |
506 | |
507 | dc->SetFont(wxGetApp().m_testFont); | |
508 | ||
600683ca | 509 | dc->DrawText(_T("Some test text"), 200, 300 ); |
92980e90 | 510 | |
7bcb11d3 | 511 | // TESTING |
9134af5b | 512 | |
7bcb11d3 JS |
513 | int leftMargin = 20; |
514 | int rightMargin = 20; | |
515 | int topMargin = 20; | |
516 | int bottomMargin = 20; | |
9134af5b | 517 | |
7bcb11d3 JS |
518 | int pageWidthMM, pageHeightMM; |
519 | GetPageSizeMM(&pageWidthMM, &pageHeightMM); | |
9134af5b | 520 | |
7bcb11d3 JS |
521 | float leftMarginLogical = (float)(logUnitsFactor*leftMargin); |
522 | float topMarginLogical = (float)(logUnitsFactor*topMargin); | |
523 | float bottomMarginLogical = (float)(logUnitsFactor*(pageHeightMM - bottomMargin)); | |
524 | float rightMarginLogical = (float)(logUnitsFactor*(pageWidthMM - rightMargin)); | |
9134af5b | 525 | |
7bcb11d3 | 526 | dc->SetPen(* wxRED_PEN); |
9134af5b | 527 | dc->DrawLine( (long)leftMarginLogical, (long)topMarginLogical, |
7bcb11d3 | 528 | (long)rightMarginLogical, (long)topMarginLogical); |
9134af5b | 529 | dc->DrawLine( (long)leftMarginLogical, (long)bottomMarginLogical, |
7bcb11d3 | 530 | (long)rightMarginLogical, (long)bottomMarginLogical); |
9134af5b | 531 | |
600683ca | 532 | WritePageHeader(this, dc, _T("A header"), logUnitsFactor); |
c801d85f KB |
533 | } |
534 | ||
535 | // Writes a header on a page. Margin units are in millimetres. | |
600683ca | 536 | bool WritePageHeader(wxPrintout *printout, wxDC *dc, wxChar *text, float mmToLogical) |
c801d85f | 537 | { |
ed880dd4 | 538 | /* |
7bcb11d3 JS |
539 | static wxFont *headerFont = (wxFont *) NULL; |
540 | if (!headerFont) | |
541 | { | |
542 | headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD); | |
543 | } | |
544 | dc->SetFont(headerFont); | |
545 | */ | |
9134af5b | 546 | |
7bcb11d3 | 547 | int pageWidthMM, pageHeightMM; |
9134af5b | 548 | |
7bcb11d3 | 549 | printout->GetPageSizeMM(&pageWidthMM, &pageHeightMM); |
9134af5b | 550 | |
7bcb11d3 JS |
551 | int leftMargin = 10; |
552 | int topMargin = 10; | |
553 | int rightMargin = 10; | |
9134af5b | 554 | |
7bcb11d3 JS |
555 | float leftMarginLogical = (float)(mmToLogical*leftMargin); |
556 | float topMarginLogical = (float)(mmToLogical*topMargin); | |
557 | float rightMarginLogical = (float)(mmToLogical*(pageWidthMM - rightMargin)); | |
9134af5b | 558 | |
7bcb11d3 JS |
559 | long xExtent, yExtent; |
560 | dc->GetTextExtent(text, &xExtent, &yExtent); | |
561 | float xPos = (float)(((((pageWidthMM - leftMargin - rightMargin)/2.0)+leftMargin)*mmToLogical) - (xExtent/2.0)); | |
562 | dc->DrawText(text, (long)xPos, (long)topMarginLogical); | |
9134af5b | 563 | |
7bcb11d3 | 564 | dc->SetPen(* wxBLACK_PEN); |
9134af5b | 565 | dc->DrawLine( (long)leftMarginLogical, (long)(topMarginLogical+yExtent), |
7bcb11d3 | 566 | (long)rightMarginLogical, (long)topMarginLogical+yExtent ); |
9134af5b DS |
567 | |
568 | return true; | |
c801d85f | 569 | } |