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