]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: samples/printing.cpp | |
3 | // Purpose: Printing demo for wxWidgets | |
4 | // Author: Julian Smart | |
5 | // Modified by: Francesco Montorsi | |
6 | // Created: 1995 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx/wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/wx.h" | |
21 | #include "wx/log.h" | |
22 | #endif | |
23 | ||
24 | #if !wxUSE_PRINTING_ARCHITECTURE | |
25 | #error "You must set wxUSE_PRINTING_ARCHITECTURE to 1 in setup.h, and recompile the library." | |
26 | #endif | |
27 | ||
28 | // Set this to 1 if you want to test PostScript printing under MSW. | |
29 | // However, you'll also need to edit src/msw/makefile.nt. | |
30 | #define wxTEST_POSTSCRIPT_IN_MSW 0 | |
31 | ||
32 | #include <ctype.h> | |
33 | #include "wx/metafile.h" | |
34 | #include "wx/print.h" | |
35 | #include "wx/printdlg.h" | |
36 | #include "wx/image.h" | |
37 | #include "wx/accel.h" | |
38 | ||
39 | #if wxTEST_POSTSCRIPT_IN_MSW | |
40 | #include "wx/generic/printps.h" | |
41 | #include "wx/generic/prntdlgg.h" | |
42 | #endif | |
43 | ||
44 | #if wxUSE_GRAPHICS_CONTEXT | |
45 | #include "wx/graphics.h" | |
46 | #endif | |
47 | ||
48 | #ifdef __WXMAC__ | |
49 | #include "wx/osx/printdlg.h" | |
50 | #endif | |
51 | ||
52 | #include "printing.h" | |
53 | ||
54 | #ifndef wxHAS_IMAGES_IN_RESOURCES | |
55 | #include "../sample.xpm" | |
56 | #endif | |
57 | ||
58 | // Global print data, to remember settings during the session | |
59 | wxPrintData *g_printData = NULL; | |
60 | ||
61 | // Global page setup data | |
62 | wxPageSetupDialogData* g_pageSetupData = NULL; | |
63 | ||
64 | ||
65 | ||
66 | // ---------------------------------------------------------------------------- | |
67 | // MyApp | |
68 | // ---------------------------------------------------------------------------- | |
69 | ||
70 | IMPLEMENT_APP(MyApp) | |
71 | ||
72 | bool MyApp::OnInit(void) | |
73 | { | |
74 | if ( !wxApp::OnInit() ) | |
75 | return false; | |
76 | ||
77 | wxInitAllImageHandlers(); | |
78 | ||
79 | ||
80 | // init global objects | |
81 | // ------------------- | |
82 | ||
83 | g_printData = new wxPrintData; | |
84 | ||
85 | // You could set an initial paper size here | |
86 | #if 0 | |
87 | g_printData->SetPaperId(wxPAPER_LETTER); // for Americans | |
88 | g_printData->SetPaperId(wxPAPER_A4); // for everyone else | |
89 | #endif | |
90 | ||
91 | g_pageSetupData = new wxPageSetupDialogData; | |
92 | ||
93 | // copy over initial paper size from print record | |
94 | (*g_pageSetupData) = *g_printData; | |
95 | ||
96 | // Set some initial page margins in mm. | |
97 | g_pageSetupData->SetMarginTopLeft(wxPoint(15, 15)); | |
98 | g_pageSetupData->SetMarginBottomRight(wxPoint(15, 15)); | |
99 | ||
100 | ||
101 | // init local GUI objects | |
102 | // ---------------------- | |
103 | ||
104 | #if 0 | |
105 | wxImage image( wxT("test.jpg") ); | |
106 | image.SetAlpha(); | |
107 | int i,j; | |
108 | for (i = 0; i < image.GetWidth(); i++) | |
109 | for (j = 0; j < image.GetHeight(); j++) | |
110 | image.SetAlpha( i, j, 50 ); | |
111 | m_bitmap = image; | |
112 | #endif | |
113 | m_angle = 30; | |
114 | m_testFont.Create(10, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL); | |
115 | ||
116 | ||
117 | // Create the main frame window | |
118 | // ---------------------------- | |
119 | ||
120 | MyFrame* frame = new MyFrame((wxFrame *) NULL, wxT("wxWidgets Printing Demo"), | |
121 | wxPoint(0, 0), wxSize(400, 400)); | |
122 | ||
123 | frame->Centre(wxBOTH); | |
124 | frame->Show(); | |
125 | ||
126 | return true; | |
127 | } | |
128 | ||
129 | int MyApp::OnExit() | |
130 | { | |
131 | delete g_printData; | |
132 | delete g_pageSetupData; | |
133 | ||
134 | return wxApp::OnExit(); | |
135 | } | |
136 | ||
137 | void MyApp::Draw(wxDC&dc) | |
138 | { | |
139 | // This routine just draws a bunch of random stuff on the screen so that we | |
140 | // can check that different types of object are being drawn consistently | |
141 | // between the screen image, the print preview image (at various zoom | |
142 | // levels), and the printed page. | |
143 | dc.SetBackground(*wxWHITE_BRUSH); | |
144 | // dc.Clear(); | |
145 | dc.SetFont(m_testFont); | |
146 | ||
147 | // dc.SetBackgroundMode(wxTRANSPARENT); | |
148 | ||
149 | dc.SetPen(*wxBLACK_PEN); | |
150 | dc.SetBrush(*wxLIGHT_GREY_BRUSH); | |
151 | ||
152 | dc.DrawRectangle(0, 0, 230, 350); | |
153 | dc.DrawLine(0, 0, 229, 349); | |
154 | dc.DrawLine(229, 0, 0, 349); | |
155 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
156 | ||
157 | dc.SetBrush(*wxCYAN_BRUSH); | |
158 | dc.SetPen(*wxRED_PEN); | |
159 | ||
160 | dc.DrawRoundedRectangle(0, 20, 200, 80, 20); | |
161 | ||
162 | dc.DrawText( wxT("Rectangle 200 by 80"), 40, 40); | |
163 | ||
164 | dc.SetPen( wxPen(*wxBLACK,0,wxDOT_DASH) ); | |
165 | dc.DrawEllipse(50, 140, 100, 50); | |
166 | dc.SetPen(*wxRED_PEN); | |
167 | ||
168 | dc.DrawText( wxT("Test message: this is in 10 point text"), 10, 180); | |
169 | ||
170 | #if wxUSE_UNICODE | |
171 | const char *test = "Hebrew שלום -- Japanese (日本語)"; | |
172 | wxString tmp = wxConvUTF8.cMB2WC( test ); | |
173 | dc.DrawText( tmp, 10, 200 ); | |
174 | #endif | |
175 | ||
176 | wxPoint points[5]; | |
177 | points[0].x = 0; | |
178 | points[0].y = 0; | |
179 | points[1].x = 20; | |
180 | points[1].y = 0; | |
181 | points[2].x = 20; | |
182 | points[2].y = 20; | |
183 | points[3].x = 10; | |
184 | points[3].y = 20; | |
185 | points[4].x = 10; | |
186 | points[4].y = -20; | |
187 | dc.DrawPolygon( 5, points, 20, 250, wxODDEVEN_RULE ); | |
188 | dc.DrawPolygon( 5, points, 50, 250, wxWINDING_RULE ); | |
189 | ||
190 | dc.DrawEllipticArc( 80, 250, 60, 30, 0.0, 270.0 ); | |
191 | ||
192 | points[0].x = 150; | |
193 | points[0].y = 250; | |
194 | points[1].x = 180; | |
195 | points[1].y = 250; | |
196 | points[2].x = 180; | |
197 | points[2].y = 220; | |
198 | points[3].x = 200; | |
199 | points[3].y = 220; | |
200 | dc.DrawSpline( 4, points ); | |
201 | ||
202 | dc.DrawArc( 20,10, 10,10, 25,40 ); | |
203 | ||
204 | wxString str; | |
205 | int i = 0; | |
206 | str.Printf( wxT("---- Text at angle %d ----"), i ); | |
207 | dc.DrawRotatedText( str, 100, 300, i ); | |
208 | ||
209 | i = m_angle; | |
210 | str.Printf( wxT("---- Text at angle %d ----"), i ); | |
211 | dc.DrawRotatedText( str, 100, 300, i ); | |
212 | ||
213 | wxIcon my_icon = wxICON(sample); | |
214 | ||
215 | dc.DrawIcon( my_icon, 100, 100); | |
216 | ||
217 | if (m_bitmap.IsOk()) | |
218 | dc.DrawBitmap( m_bitmap, 10, 10 ); | |
219 | ||
220 | #if wxUSE_GRAPHICS_CONTEXT | |
221 | wxGraphicsContext *gc = NULL; | |
222 | ||
223 | wxPrinterDC *printer_dc = wxDynamicCast( &dc, wxPrinterDC ); | |
224 | if (printer_dc) | |
225 | gc = wxGraphicsContext::Create( *printer_dc ); | |
226 | ||
227 | wxWindowDC *window_dc = wxDynamicCast( &dc, wxWindowDC ); | |
228 | if (window_dc) | |
229 | gc = wxGraphicsContext::Create( *window_dc ); | |
230 | ||
231 | #ifdef __WXMSW__ | |
232 | wxEnhMetaFileDC *emf_dc = wxDynamicCast( &dc, wxEnhMetaFileDC ); | |
233 | if (emf_dc) | |
234 | gc = wxGraphicsContext::Create( *emf_dc ); | |
235 | #endif | |
236 | ||
237 | if (gc) | |
238 | { | |
239 | // make a path that contains a circle and some lines, centered at 100,100 | |
240 | gc->SetPen( *wxRED_PEN ); | |
241 | ||
242 | wxGraphicsPath path = gc->CreatePath(); | |
243 | path.AddCircle( 50.0, 50.0, 50.0 ); | |
244 | path.MoveToPoint(0.0, 50.0); | |
245 | path.AddLineToPoint(100.0, 50.0); | |
246 | path.MoveToPoint(50.0, 0.0); | |
247 | path.AddLineToPoint(50.0, 100.0 ); | |
248 | path.CloseSubpath(); | |
249 | path.AddRectangle(25.0, 25.0, 50.0, 50.0); | |
250 | ||
251 | gc->StrokePath(path); | |
252 | ||
253 | // draw some text | |
254 | wxString text("Text by wxGraphicsContext"); | |
255 | gc->SetFont( m_testFont, *wxBLACK ); | |
256 | gc->DrawText(text, 25.0, 60.0); | |
257 | ||
258 | // draw rectangle around the text | |
259 | double w, h, d, el; | |
260 | gc->GetTextExtent(text, &w, &h, &d, &el); | |
261 | gc->SetPen( *wxBLACK_PEN ); | |
262 | gc->DrawRectangle(25.0, 60.0, w, h); | |
263 | ||
264 | delete gc; | |
265 | } | |
266 | #endif | |
267 | } | |
268 | ||
269 | ||
270 | // ---------------------------------------------------------------------------- | |
271 | // MyFrame | |
272 | // ---------------------------------------------------------------------------- | |
273 | ||
274 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
275 | EVT_MENU(wxID_EXIT, MyFrame::OnExit) | |
276 | EVT_MENU(wxID_PRINT, MyFrame::OnPrint) | |
277 | EVT_MENU(wxID_PREVIEW, MyFrame::OnPrintPreview) | |
278 | EVT_MENU(WXPRINT_PAGE_SETUP, MyFrame::OnPageSetup) | |
279 | EVT_MENU(wxID_ABOUT, MyFrame::OnPrintAbout) | |
280 | #if defined(__WXMSW__) &&wxTEST_POSTSCRIPT_IN_MSW | |
281 | EVT_MENU(WXPRINT_PRINT_PS, MyFrame::OnPrintPS) | |
282 | EVT_MENU(WXPRINT_PREVIEW_PS, MyFrame::OnPrintPreviewPS) | |
283 | EVT_MENU(WXPRINT_PAGE_SETUP_PS, MyFrame::OnPageSetupPS) | |
284 | #endif | |
285 | #ifdef __WXMAC__ | |
286 | EVT_MENU(WXPRINT_PAGE_MARGINS, MyFrame::OnPageMargins) | |
287 | #endif | |
288 | EVT_MENU(WXPRINT_ANGLEUP, MyFrame::OnAngleUp) | |
289 | EVT_MENU(WXPRINT_ANGLEDOWN, MyFrame::OnAngleDown) | |
290 | ||
291 | EVT_MENU_RANGE(WXPRINT_FRAME_MODAL_APP, | |
292 | WXPRINT_FRAME_MODAL_NON, | |
293 | MyFrame::OnPreviewFrameModalityKind) | |
294 | END_EVENT_TABLE() | |
295 | ||
296 | MyFrame::MyFrame(wxFrame *frame, const wxString&title, const wxPoint&pos, const wxSize&size) | |
297 | : wxFrame(frame, wxID_ANY, title, pos, size) | |
298 | { | |
299 | m_canvas = NULL; | |
300 | m_previewModality = wxPreviewFrame_AppModal; | |
301 | ||
302 | #if wxUSE_STATUSBAR | |
303 | // Give us a status line | |
304 | CreateStatusBar(2); | |
305 | SetStatusText(wxT("Printing demo")); | |
306 | #endif // wxUSE_STATUSBAR | |
307 | ||
308 | // Load icon and bitmap | |
309 | SetIcon( wxICON( sample) ); | |
310 | ||
311 | // Make a menubar | |
312 | wxMenu *file_menu = new wxMenu; | |
313 | ||
314 | file_menu->Append(wxID_PRINT, wxT("&Print..."), wxT("Print")); | |
315 | file_menu->Append(WXPRINT_PAGE_SETUP, wxT("Page Set&up..."), wxT("Page setup")); | |
316 | #ifdef __WXMAC__ | |
317 | file_menu->Append(WXPRINT_PAGE_MARGINS, wxT("Page Margins..."), wxT("Page margins")); | |
318 | #endif | |
319 | file_menu->Append(wxID_PREVIEW, wxT("Print Pre&view"), wxT("Preview")); | |
320 | ||
321 | wxMenu * const menuModalKind = new wxMenu; | |
322 | menuModalKind->AppendRadioItem(WXPRINT_FRAME_MODAL_APP, "&App modal"); | |
323 | menuModalKind->AppendRadioItem(WXPRINT_FRAME_MODAL_WIN, "&Window modal"); | |
324 | menuModalKind->AppendRadioItem(WXPRINT_FRAME_MODAL_NON, "&Not modal"); | |
325 | file_menu->AppendSubMenu(menuModalKind, "Preview frame &modal kind"); | |
326 | #if wxUSE_ACCEL | |
327 | // Accelerators | |
328 | wxAcceleratorEntry entries[1]; | |
329 | entries[0].Set(wxACCEL_CTRL, (int) 'V', wxID_PREVIEW); | |
330 | wxAcceleratorTable accel(1, entries); | |
331 | SetAcceleratorTable(accel); | |
332 | #endif | |
333 | ||
334 | #if defined(__WXMSW__) &&wxTEST_POSTSCRIPT_IN_MSW | |
335 | file_menu->AppendSeparator(); | |
336 | file_menu->Append(WXPRINT_PRINT_PS, wxT("Print PostScript..."), wxT("Print (PostScript)")); | |
337 | file_menu->Append(WXPRINT_PAGE_SETUP_PS, wxT("Page Setup PostScript..."), wxT("Page setup (PostScript)")); | |
338 | file_menu->Append(WXPRINT_PREVIEW_PS, wxT("Print Preview PostScript"), wxT("Preview (PostScript)")); | |
339 | #endif | |
340 | ||
341 | file_menu->AppendSeparator(); | |
342 | file_menu->Append(WXPRINT_ANGLEUP, wxT("Angle up\tAlt-U"), wxT("Raise rotated text angle")); | |
343 | file_menu->Append(WXPRINT_ANGLEDOWN, wxT("Angle down\tAlt-D"), wxT("Lower rotated text angle")); | |
344 | file_menu->AppendSeparator(); | |
345 | file_menu->Append(wxID_EXIT, wxT("E&xit"), wxT("Exit program")); | |
346 | ||
347 | wxMenu *help_menu = new wxMenu; | |
348 | help_menu->Append(wxID_ABOUT, wxT("&About"), wxT("About this demo")); | |
349 | ||
350 | wxMenuBar *menu_bar = new wxMenuBar; | |
351 | ||
352 | menu_bar->Append(file_menu, wxT("&File")); | |
353 | menu_bar->Append(help_menu, wxT("&Help")); | |
354 | ||
355 | // Associate the menu bar with the frame | |
356 | SetMenuBar(menu_bar); | |
357 | ||
358 | ||
359 | // create the canvas | |
360 | // ----------------- | |
361 | ||
362 | m_canvas = new MyCanvas(this, wxPoint(0, 0), wxSize(100, 100), | |
363 | wxRETAINED|wxHSCROLL|wxVSCROLL); | |
364 | ||
365 | // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction | |
366 | m_canvas->SetScrollbars(20, 20, 50, 50); | |
367 | } | |
368 | ||
369 | void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event)) | |
370 | { | |
371 | Close(true /*force closing*/); | |
372 | } | |
373 | ||
374 | void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event)) | |
375 | { | |
376 | wxPrintDialogData printDialogData(* g_printData); | |
377 | ||
378 | wxPrinter printer(&printDialogData); | |
379 | MyPrintout printout(this, wxT("My printout")); | |
380 | if (!printer.Print(this, &printout, true /*prompt*/)) | |
381 | { | |
382 | if (wxPrinter::GetLastError() == wxPRINTER_ERROR) | |
383 | { | |
384 | wxLogError(wxT("There was a problem printing. Perhaps your current printer is not set correctly?")); | |
385 | } | |
386 | else | |
387 | { | |
388 | wxLogMessage(wxT("You canceled printing")); | |
389 | } | |
390 | } | |
391 | else | |
392 | { | |
393 | (*g_printData) = printer.GetPrintDialogData().GetPrintData(); | |
394 | } | |
395 | } | |
396 | ||
397 | void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event)) | |
398 | { | |
399 | // Pass two printout objects: for preview, and possible printing. | |
400 | wxPrintDialogData printDialogData(* g_printData); | |
401 | wxPrintPreview *preview = | |
402 | new wxPrintPreview(new MyPrintout(this), new MyPrintout(this), &printDialogData); | |
403 | if (!preview->IsOk()) | |
404 | { | |
405 | delete preview; | |
406 | wxLogError(wxT("There was a problem previewing.\nPerhaps your current printer is not set correctly?")); | |
407 | return; | |
408 | } | |
409 | ||
410 | wxPreviewFrame *frame = | |
411 | new wxPreviewFrame(preview, this, wxT("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650)); | |
412 | frame->Centre(wxBOTH); | |
413 | frame->InitializeWithModality(m_previewModality); | |
414 | frame->Show(); | |
415 | } | |
416 | ||
417 | void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event)) | |
418 | { | |
419 | (*g_pageSetupData) = *g_printData; | |
420 | ||
421 | wxPageSetupDialog pageSetupDialog(this, g_pageSetupData); | |
422 | pageSetupDialog.ShowModal(); | |
423 | ||
424 | (*g_printData) = pageSetupDialog.GetPageSetupDialogData().GetPrintData(); | |
425 | (*g_pageSetupData) = pageSetupDialog.GetPageSetupDialogData(); | |
426 | } | |
427 | ||
428 | #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW | |
429 | void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event)) | |
430 | { | |
431 | wxPostScriptPrinter printer(g_printData); | |
432 | MyPrintout printout(wxT("My printout")); | |
433 | printer.Print(this, &printout, true/*prompt*/); | |
434 | ||
435 | (*g_printData) = printer.GetPrintData(); | |
436 | } | |
437 | ||
438 | void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event)) | |
439 | { | |
440 | // Pass two printout objects: for preview, and possible printing. | |
441 | wxPrintDialogData printDialogData(* g_printData); | |
442 | wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, &printDialogData); | |
443 | wxPreviewFrame *frame = | |
444 | new wxPreviewFrame(preview, this, wxT("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650)); | |
445 | frame->Centre(wxBOTH); | |
446 | frame->Initialize(); | |
447 | frame->Show(); | |
448 | } | |
449 | ||
450 | void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event)) | |
451 | { | |
452 | (*g_pageSetupData) = * g_printData; | |
453 | ||
454 | wxGenericPageSetupDialog pageSetupDialog(this, g_pageSetupData); | |
455 | pageSetupDialog.ShowModal(); | |
456 | ||
457 | (*g_printData) = pageSetupDialog.GetPageSetupDialogData().GetPrintData(); | |
458 | (*g_pageSetupData) = pageSetupDialog.GetPageSetupDialogData(); | |
459 | } | |
460 | #endif | |
461 | ||
462 | #ifdef __WXMAC__ | |
463 | void MyFrame::OnPageMargins(wxCommandEvent& WXUNUSED(event)) | |
464 | { | |
465 | (*g_pageSetupData) = *g_printData; | |
466 | ||
467 | wxMacPageMarginsDialog pageMarginsDialog(this, g_pageSetupData); | |
468 | pageMarginsDialog.ShowModal(); | |
469 | ||
470 | (*g_printData) = pageMarginsDialog.GetPageSetupDialogData().GetPrintData(); | |
471 | (*g_pageSetupData) = pageMarginsDialog.GetPageSetupDialogData(); | |
472 | } | |
473 | #endif | |
474 | ||
475 | void MyFrame::OnPrintAbout(wxCommandEvent& WXUNUSED(event)) | |
476 | { | |
477 | (void)wxMessageBox(wxT("wxWidgets printing demo\nAuthor: Julian Smart"), | |
478 | wxT("About wxWidgets printing demo"), wxOK|wxCENTRE); | |
479 | } | |
480 | ||
481 | void MyFrame::OnAngleUp(wxCommandEvent& WXUNUSED(event)) | |
482 | { | |
483 | wxGetApp().IncrementAngle(); | |
484 | m_canvas->Refresh(); | |
485 | } | |
486 | ||
487 | void MyFrame::OnAngleDown(wxCommandEvent& WXUNUSED(event)) | |
488 | { | |
489 | wxGetApp().DecrementAngle(); | |
490 | m_canvas->Refresh(); | |
491 | } | |
492 | ||
493 | void MyFrame::OnPreviewFrameModalityKind(wxCommandEvent& event) | |
494 | { | |
495 | m_previewModality = static_cast<wxPreviewFrameModalityKind>( | |
496 | wxPreviewFrame_AppModal + | |
497 | (event.GetId() - WXPRINT_FRAME_MODAL_APP)); | |
498 | } | |
499 | ||
500 | // ---------------------------------------------------------------------------- | |
501 | // MyCanvas | |
502 | // ---------------------------------------------------------------------------- | |
503 | ||
504 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
505 | // EVT_PAINT(MyCanvas::OnPaint) | |
506 | END_EVENT_TABLE() | |
507 | ||
508 | MyCanvas::MyCanvas(wxFrame *frame, const wxPoint&pos, const wxSize&size, long style) | |
509 | : wxScrolledWindow(frame, wxID_ANY, pos, size, style) | |
510 | { | |
511 | SetBackgroundColour(*wxWHITE); | |
512 | } | |
513 | ||
514 | //void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(evt)) | |
515 | void MyCanvas::OnDraw(wxDC& dc) | |
516 | { | |
517 | //wxPaintDC dc(this); | |
518 | wxGetApp().Draw(dc); | |
519 | } | |
520 | ||
521 | ||
522 | // ---------------------------------------------------------------------------- | |
523 | // MyPrintout | |
524 | // ---------------------------------------------------------------------------- | |
525 | ||
526 | bool MyPrintout::OnPrintPage(int page) | |
527 | { | |
528 | wxDC *dc = GetDC(); | |
529 | if (dc) | |
530 | { | |
531 | if (page == 1) | |
532 | DrawPageOne(); | |
533 | else if (page == 2) | |
534 | DrawPageTwo(); | |
535 | ||
536 | // Draw page numbers at top left corner of printable area, sized so that | |
537 | // screen size of text matches paper size. | |
538 | MapScreenSizeToPage(); | |
539 | ||
540 | dc->DrawText(wxString::Format(wxT("PAGE %d"), page), 0, 0); | |
541 | ||
542 | return true; | |
543 | } | |
544 | else | |
545 | return false; | |
546 | } | |
547 | ||
548 | bool MyPrintout::OnBeginDocument(int startPage, int endPage) | |
549 | { | |
550 | if (!wxPrintout::OnBeginDocument(startPage, endPage)) | |
551 | return false; | |
552 | ||
553 | return true; | |
554 | } | |
555 | ||
556 | void MyPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) | |
557 | { | |
558 | *minPage = 1; | |
559 | *maxPage = 2; | |
560 | *selPageFrom = 1; | |
561 | *selPageTo = 2; | |
562 | } | |
563 | ||
564 | bool MyPrintout::HasPage(int pageNum) | |
565 | { | |
566 | return (pageNum == 1 || pageNum == 2); | |
567 | } | |
568 | ||
569 | void MyPrintout::DrawPageOne() | |
570 | { | |
571 | // You might use THIS code if you were scaling graphics of known size to fit | |
572 | // on the page. The commented-out code illustrates different ways of scaling | |
573 | // the graphics. | |
574 | ||
575 | // We know the graphic is 230x350. If we didn't know this, we'd need to | |
576 | // calculate it. | |
577 | wxCoord maxX = 230; | |
578 | wxCoord maxY = 350; | |
579 | ||
580 | // This sets the user scale and origin of the DC so that the image fits | |
581 | // within the paper rectangle (but the edges could be cut off by printers | |
582 | // that can't print to the edges of the paper -- which is most of them. Use | |
583 | // this if your image already has its own margins. | |
584 | // FitThisSizeToPaper(wxSize(maxX, maxY)); | |
585 | // wxRect fitRect = GetLogicalPaperRect(); | |
586 | ||
587 | // This sets the user scale and origin of the DC so that the image fits | |
588 | // within the page rectangle, which is the printable area on Mac and MSW | |
589 | // and is the entire page on other platforms. | |
590 | // FitThisSizeToPage(wxSize(maxX, maxY)); | |
591 | // wxRect fitRect = GetLogicalPageRect(); | |
592 | ||
593 | // This sets the user scale and origin of the DC so that the image fits | |
594 | // within the page margins as specified by g_PageSetupData, which you can | |
595 | // change (on some platforms, at least) in the Page Setup dialog. Note that | |
596 | // on Mac, the native Page Setup dialog doesn't let you change the margins | |
597 | // of a wxPageSetupDialogData object, so you'll have to write your own dialog or | |
598 | // use the Mac-only wxMacPageMarginsDialog, as we do in this program. | |
599 | FitThisSizeToPageMargins(wxSize(maxX, maxY), *g_pageSetupData); | |
600 | wxRect fitRect = GetLogicalPageMarginsRect(*g_pageSetupData); | |
601 | ||
602 | // This sets the user scale and origin of the DC so that the image appears | |
603 | // on the paper at the same size that it appears on screen (i.e., 10-point | |
604 | // type on screen is 10-point on the printed page) and is positioned in the | |
605 | // top left corner of the page rectangle (just as the screen image appears | |
606 | // in the top left corner of the window). | |
607 | // MapScreenSizeToPage(); | |
608 | // wxRect fitRect = GetLogicalPageRect(); | |
609 | ||
610 | // You could also map the screen image to the entire paper at the same size | |
611 | // as it appears on screen. | |
612 | // MapScreenSizeToPaper(); | |
613 | // wxRect fitRect = GetLogicalPaperRect(); | |
614 | ||
615 | // You might also wish to do you own scaling in order to draw objects at | |
616 | // full native device resolution. In this case, you should do the following. | |
617 | // Note that you can use the GetLogicalXXXRect() commands to obtain the | |
618 | // appropriate rect to scale to. | |
619 | // MapScreenSizeToDevice(); | |
620 | // wxRect fitRect = GetLogicalPageRect(); | |
621 | ||
622 | // Each of the preceding Fit or Map routines positions the origin so that | |
623 | // the drawn image is positioned at the top left corner of the reference | |
624 | // rectangle. You can easily center or right- or bottom-justify the image as | |
625 | // follows. | |
626 | ||
627 | // This offsets the image so that it is centered within the reference | |
628 | // rectangle defined above. | |
629 | wxCoord xoff = (fitRect.width - maxX) / 2; | |
630 | wxCoord yoff = (fitRect.height - maxY) / 2; | |
631 | OffsetLogicalOrigin(xoff, yoff); | |
632 | ||
633 | // This offsets the image so that it is positioned at the bottom right of | |
634 | // the reference rectangle defined above. | |
635 | // wxCoord xoff = (fitRect.width - maxX); | |
636 | // wxCoord yoff = (fitRect.height - maxY); | |
637 | // OffsetLogicalOrigin(xoff, yoff); | |
638 | ||
639 | wxGetApp().Draw(*GetDC()); | |
640 | } | |
641 | ||
642 | void MyPrintout::DrawPageTwo() | |
643 | { | |
644 | // You might use THIS code to set the printer DC to ROUGHLY reflect | |
645 | // the screen text size. This page also draws lines of actual length | |
646 | // 5cm on the page. | |
647 | ||
648 | // Compare this to DrawPageOne(), which uses the really convenient routines | |
649 | // from wxPrintout to fit the screen image onto the printed page. This page | |
650 | // illustrates how to do all the scaling calculations yourself, if you're so | |
651 | // inclined. | |
652 | ||
653 | wxDC *dc = GetDC(); | |
654 | ||
655 | // Get the logical pixels per inch of screen and printer | |
656 | int ppiScreenX, ppiScreenY; | |
657 | GetPPIScreen(&ppiScreenX, &ppiScreenY); | |
658 | int ppiPrinterX, ppiPrinterY; | |
659 | GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); | |
660 | ||
661 | // This scales the DC so that the printout roughly represents the screen | |
662 | // scaling. The text point size _should_ be the right size but in fact is | |
663 | // too small for some reason. This is a detail that will need to be | |
664 | // addressed at some point but can be fudged for the moment. | |
665 | float scale = (float)((float)ppiPrinterX/(float)ppiScreenX); | |
666 | ||
667 | // Now we have to check in case our real page size is reduced (e.g. because | |
668 | // we're drawing to a print preview memory DC) | |
669 | int pageWidth, pageHeight; | |
670 | int w, h; | |
671 | dc->GetSize(&w, &h); | |
672 | GetPageSizePixels(&pageWidth, &pageHeight); | |
673 | ||
674 | // If printer pageWidth == current DC width, then this doesn't change. But w | |
675 | // might be the preview bitmap width, so scale down. | |
676 | float overallScale = scale * (float)(w/(float)pageWidth); | |
677 | dc->SetUserScale(overallScale, overallScale); | |
678 | ||
679 | // Calculate conversion factor for converting millimetres into logical | |
680 | // units. There are approx. 25.4 mm to the inch. There are ppi device units | |
681 | // to the inch. Therefore 1 mm corresponds to ppi/25.4 device units. We also | |
682 | // divide by the screen-to-printer scaling factor, because we need to | |
683 | // unscale to pass logical units to DrawLine. | |
684 | ||
685 | // Draw 50 mm by 50 mm L shape | |
686 | float logUnitsFactor = (float)(ppiPrinterX/(scale*25.4)); | |
687 | float logUnits = (float)(50*logUnitsFactor); | |
688 | dc->SetPen(* wxBLACK_PEN); | |
689 | dc->DrawLine(50, 250, (long)(50.0 + logUnits), 250); | |
690 | dc->DrawLine(50, 250, 50, (long)(250.0 + logUnits)); | |
691 | ||
692 | dc->SetBackgroundMode(wxTRANSPARENT); | |
693 | dc->SetBrush(*wxTRANSPARENT_BRUSH); | |
694 | ||
695 | { // GetTextExtent demo: | |
696 | wxString words[7] = { wxT("This "), wxT("is "), wxT("GetTextExtent "), | |
697 | wxT("testing "), wxT("string. "), wxT("Enjoy "), wxT("it!") }; | |
698 | wxCoord w, h; | |
699 | long x = 200, y= 250; | |
700 | wxFont fnt(15, wxSWISS, wxNORMAL, wxNORMAL); | |
701 | ||
702 | dc->SetFont(fnt); | |
703 | ||
704 | for (int i = 0; i < 7; i++) | |
705 | { | |
706 | wxString word = words[i]; | |
707 | word.Remove( word.Len()-1, 1 ); | |
708 | dc->GetTextExtent(word, &w, &h); | |
709 | dc->DrawRectangle(x, y, w, h); | |
710 | dc->GetTextExtent(words[i], &w, &h); | |
711 | dc->DrawText(words[i], x, y); | |
712 | x += w; | |
713 | } | |
714 | ||
715 | } | |
716 | ||
717 | dc->SetFont(wxGetApp().GetTestFont()); | |
718 | ||
719 | dc->DrawText(wxT("Some test text"), 200, 300 ); | |
720 | ||
721 | // TESTING | |
722 | ||
723 | int leftMargin = 20; | |
724 | int rightMargin = 20; | |
725 | int topMargin = 20; | |
726 | int bottomMargin = 20; | |
727 | ||
728 | int pageWidthMM, pageHeightMM; | |
729 | GetPageSizeMM(&pageWidthMM, &pageHeightMM); | |
730 | ||
731 | float leftMarginLogical = (float)(logUnitsFactor*leftMargin); | |
732 | float topMarginLogical = (float)(logUnitsFactor*topMargin); | |
733 | float bottomMarginLogical = (float)(logUnitsFactor*(pageHeightMM - bottomMargin)); | |
734 | float rightMarginLogical = (float)(logUnitsFactor*(pageWidthMM - rightMargin)); | |
735 | ||
736 | dc->SetPen(* wxRED_PEN); | |
737 | dc->DrawLine( (long)leftMarginLogical, (long)topMarginLogical, | |
738 | (long)rightMarginLogical, (long)topMarginLogical); | |
739 | dc->DrawLine( (long)leftMarginLogical, (long)bottomMarginLogical, | |
740 | (long)rightMarginLogical, (long)bottomMarginLogical); | |
741 | ||
742 | WritePageHeader(this, dc, wxT("A header"), logUnitsFactor); | |
743 | } | |
744 | ||
745 | // Writes a header on a page. Margin units are in millimetres. | |
746 | bool MyPrintout::WritePageHeader(wxPrintout *printout, wxDC *dc, const wxString&text, float mmToLogical) | |
747 | { | |
748 | #if 0 | |
749 | static wxFont *headerFont = (wxFont *) NULL; | |
750 | if (!headerFont) | |
751 | { | |
752 | headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD); | |
753 | } | |
754 | dc->SetFont(headerFont); | |
755 | #endif | |
756 | ||
757 | int pageWidthMM, pageHeightMM; | |
758 | ||
759 | printout->GetPageSizeMM(&pageWidthMM, &pageHeightMM); | |
760 | wxUnusedVar(pageHeightMM); | |
761 | ||
762 | int leftMargin = 10; | |
763 | int topMargin = 10; | |
764 | int rightMargin = 10; | |
765 | ||
766 | float leftMarginLogical = (float)(mmToLogical*leftMargin); | |
767 | float topMarginLogical = (float)(mmToLogical*topMargin); | |
768 | float rightMarginLogical = (float)(mmToLogical*(pageWidthMM - rightMargin)); | |
769 | ||
770 | wxCoord xExtent, yExtent; | |
771 | dc->GetTextExtent(text, &xExtent, &yExtent); | |
772 | ||
773 | float xPos = (float)(((((pageWidthMM - leftMargin - rightMargin)/2.0)+leftMargin)*mmToLogical) - (xExtent/2.0)); | |
774 | dc->DrawText(text, (long)xPos, (long)topMarginLogical); | |
775 | ||
776 | dc->SetPen(* wxBLACK_PEN); | |
777 | dc->DrawLine( (long)leftMarginLogical, (long)(topMarginLogical+yExtent), | |
778 | (long)rightMarginLogical, (long)topMarginLogical+yExtent ); | |
779 | ||
780 | return true; | |
781 | } |