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