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