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