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