Fixed module code
[wxWidgets.git] / samples / printing / printing.cpp
1 /*
2 * File: printing.cc
3 * Purpose: Printing demo for wxWindows class library
4 * Author: Julian Smart
5 * Created: 1995
6 * Updated:
7 * Copyright: (c) 1995, AIAI, University of Edinburgh
8 */
9
10 /* static const char sccsid[] = "%W% %G%"; */
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #include "wx/postscrp.h"
26 #endif
27
28 #if !wxUSE_PRINTING_ARCHITECTURE
29 #error You must set wxUSE_PRINTING_ARCHITECTURE to 1 in wx_setup.h to compile this demo.
30 #endif
31
32 // Set this to 1 if you want to test PostScript printing under MSW.
33 // However, you'll also need to edit src/msw/makefile.nt.
34 #define wxTEST_POSTSCRIPT_IN_MSW 0
35
36 #include <ctype.h>
37 #include "wx/metafile.h"
38 #include "wx/print.h"
39 #include "wx/printdlg.h"
40
41 #include "wx/accel.h"
42
43 #if wxTEST_POSTSCRIPT_IN_MSW
44 #include "wx/generic/printps.h"
45 #include "wx/generic/prntdlgg.h"
46 #endif
47
48 #include "printing.h"
49
50 // Declare a frame
51 MyFrame *frame = (MyFrame *) NULL;
52 int orientation = wxPORTRAIT;
53
54 // Main proc
55 IMPLEMENT_APP(MyApp)
56
57 #if defined(__WXGTK__) || defined(__WXMOTIF__)
58 #include "folder.xpm"
59 #endif
60
61 // Writes a header on a page. Margin units are in millimetres.
62 bool WritePageHeader(wxPrintout *printout, wxDC *dc, char *text, float mmToLogical);
63
64 MyApp::MyApp()
65 {
66 }
67
68 // The `main program' equivalent, creating the windows and returning the
69 // main frame
70 bool MyApp::OnInit(void)
71 {
72 m_testFont = new wxFont(10, wxSWISS, wxNORMAL, wxNORMAL);
73
74 // Create the main frame window
75 frame = new MyFrame((wxFrame *) NULL, (char *) "wxWindows Printing Demo", wxPoint(0, 0), wxSize(400, 400));
76
77 // Give it a status line
78 frame->CreateStatusBar(2);
79
80 // Load icon and bitmap
81 #ifdef __WXMSW__
82 frame->SetIcon(wxIcon("mondrian"));
83 #endif
84 #ifdef __X__
85 // frame->SetIcon(wxIcon(mondrian_bits, mondrian_width, mondrian_height));
86 #endif
87
88 // Make a menubar
89 wxMenu *file_menu = new wxMenu;
90
91 file_menu->Append(WXPRINT_PRINT, "&Print...", "Print");
92 file_menu->Append(WXPRINT_PRINT_SETUP, "Print &Setup...", "Setup printer properties");
93 file_menu->Append(WXPRINT_PAGE_SETUP, "Page Set&up...", "Page setup");
94 file_menu->Append(WXPRINT_PREVIEW, "Print Pre&view", "Preview");
95
96 // Accelerators
97 wxAcceleratorEntry entries[1];
98 entries[0].Set(wxACCEL_CTRL, (int) 'V', WXPRINT_PREVIEW);
99 wxAcceleratorTable accel(1, entries);
100 frame->SetAcceleratorTable(accel);
101
102 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
103 file_menu->AppendSeparator();
104 file_menu->Append(WXPRINT_PRINT_PS, "Print PostScript...", "Print (PostScript)");
105 file_menu->Append(WXPRINT_PRINT_SETUP_PS, "Print Setup PostScript...", "Setup printer properties (PostScript)");
106 file_menu->Append(WXPRINT_PAGE_SETUP_PS, "Page Setup PostScript...", "Page setup (PostScript)");
107 file_menu->Append(WXPRINT_PREVIEW_PS, "Print Preview PostScript", "Preview (PostScript)");
108 #endif
109 file_menu->AppendSeparator();
110 file_menu->Append(WXPRINT_QUIT, "E&xit", "Exit program");
111
112 wxMenu *help_menu = new wxMenu;
113 help_menu->Append(WXPRINT_ABOUT, "&About", "About this demo");
114
115 wxMenuBar *menu_bar = new wxMenuBar;
116
117 menu_bar->Append(file_menu, "&File");
118 menu_bar->Append(help_menu, "&Help");
119
120 // Associate the menu bar with the frame
121 frame->SetMenuBar(menu_bar);
122
123 MyCanvas *canvas = new MyCanvas(frame, wxPoint(0, 0), wxSize(100, 100), wxRETAINED|wxHSCROLL|wxVSCROLL);
124
125 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
126 canvas->SetScrollbars(20, 20, 50, 50);
127
128 frame->canvas = canvas;
129
130 frame->Centre(wxBOTH);
131 frame->Show(TRUE);
132
133 frame->SetStatusText("Printing demo");
134
135 SetTopWindow(frame);
136
137 return TRUE;
138 }
139
140 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
141 EVT_MENU(WXPRINT_QUIT, MyFrame::OnExit)
142 EVT_MENU(WXPRINT_PRINT, MyFrame::OnPrint)
143 EVT_MENU(WXPRINT_PREVIEW, MyFrame::OnPrintPreview)
144 EVT_MENU(WXPRINT_PRINT_SETUP, MyFrame::OnPrintSetup)
145 EVT_MENU(WXPRINT_PAGE_SETUP, MyFrame::OnPageSetup)
146 EVT_MENU(WXPRINT_ABOUT, MyFrame::OnPrintAbout)
147 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
148 EVT_MENU(WXPRINT_PRINT_PS, MyFrame::OnPrintPS)
149 EVT_MENU(WXPRINT_PREVIEW_PS, MyFrame::OnPrintPreviewPS)
150 EVT_MENU(WXPRINT_PRINT_SETUP_PS, MyFrame::OnPrintSetupPS)
151 EVT_MENU(WXPRINT_PAGE_SETUP_PS, MyFrame::OnPageSetupPS)
152 #endif
153 END_EVENT_TABLE()
154
155 // Define my frame constructor
156 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
157 wxFrame(frame, -1, title, pos, size)
158 {
159 canvas = (MyCanvas *) NULL;
160 }
161
162 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
163 {
164 Close(TRUE);
165 }
166
167 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
168 {
169 #ifdef __WXMSW__
170 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
171 #else
172 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
173 #endif
174 wxPrinter printer;
175 MyPrintout printout("My printout");
176 if (!printer.Print(this, &printout, TRUE))
177 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK);
178 }
179
180 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
181 {
182 #ifdef __WXMSW__
183 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
184 #else
185 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
186 #endif
187 wxPrintData printData;
188 printData.SetOrientation(orientation);
189
190 // Pass two printout objects: for preview, and possible printing.
191 wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printData);
192 if (!preview->Ok())
193 {
194 delete preview;
195 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
196 return;
197 }
198
199 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
200 frame->Centre(wxBOTH);
201 frame->Initialize();
202 frame->Show(TRUE);
203 }
204
205 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
206 {
207 #ifdef __WXMSW__
208 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
209 #else
210 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
211 #endif
212 wxPrintData data;
213 data.SetOrientation(orientation);
214
215 #ifdef __WXMSW__
216 wxPrintDialog printerDialog(this, & data);
217 #else
218 wxGenericPrintDialog printerDialog(this, & data);
219 #endif
220 printerDialog.GetPrintData().SetSetupDialog(TRUE);
221 printerDialog.ShowModal();
222
223 orientation = printerDialog.GetPrintData().GetOrientation();
224 }
225
226 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
227 {
228 #ifdef __WXMSW__
229 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
230 #else
231 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
232 #endif
233 wxPageSetupData data;
234 data.SetOrientation(orientation);
235
236 #ifdef __WXMSW__
237 wxPageSetupDialog pageSetupDialog(this, & data);
238 #else
239 wxGenericPageSetupDialog pageSetupDialog(this, & data);
240 #endif
241 pageSetupDialog.ShowModal();
242
243 data = pageSetupDialog.GetPageSetupData();
244 orientation = data.GetOrientation();
245 }
246
247 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
248 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
249 {
250 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
251
252 wxPostScriptPrinter printer;
253 MyPrintout printout("My printout");
254 printer.Print(this, &printout, TRUE);
255 }
256
257 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
258 {
259 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
260
261 wxPrintData printData;
262 printData.SetOrientation(orientation);
263
264 // Pass two printout objects: for preview, and possible printing.
265 wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printData);
266 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
267 frame->Centre(wxBOTH);
268 frame->Initialize();
269 frame->Show(TRUE);
270 }
271
272 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
273 {
274 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
275
276 wxPrintData data;
277 data.SetOrientation(orientation);
278
279 wxGenericPrintDialog printerDialog(this, & data);
280 printerDialog.GetPrintData().SetSetupDialog(TRUE);
281 printerDialog.ShowModal();
282
283 orientation = printerDialog.GetPrintData().GetOrientation();
284 }
285
286 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
287 {
288 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
289
290 wxPageSetupData data;
291 data.SetOrientation(orientation);
292
293 wxGenericPageSetupDialog pageSetupDialog(this, & data);
294 pageSetupDialog.ShowModal();
295
296 orientation = pageSetupDialog.GetPageSetupData().GetOrientation();
297 }
298 #endif
299
300
301 void MyFrame::OnPrintAbout(wxCommandEvent& WXUNUSED(event))
302 {
303 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
304 "About wxWindows printing demo", wxOK|wxCENTRE);
305 }
306
307 void MyFrame::Draw(wxDC& dc)
308 {
309 dc.SetFont(* wxGetApp().m_testFont);
310
311 dc.SetBackgroundMode(wxTRANSPARENT);
312
313 dc.SetBrush(* wxCYAN_BRUSH);
314 dc.SetPen(* wxRED_PEN);
315
316 dc.DrawRectangle(0, 30, 200, 100);
317 dc.DrawText("Rectangle 200 by 100", 40, 40);
318
319 dc.DrawEllipse(50, 140, 100, 50);
320
321 dc.DrawText("Test message: this is in 10 point text", 10, 180);
322
323 dc.SetPen(* wxBLACK_PEN);
324 dc.DrawLine(0, 0, 200, 200);
325 dc.DrawLine(200, 0, 0, 200);
326
327 #if defined(__WXGTK__) || defined(__WXMOTIF__)
328 wxIcon my_icon( folder_xpm );
329 #elif defined(__WXMSW__)
330 wxIcon my_icon( "mondrian" );
331 #elif
332 #error "Platform not supported."
333 #endif
334
335 dc.DrawIcon( my_icon, 100, 100);
336 }
337
338 void MyFrame::OnSize(wxSizeEvent& event )
339 {
340 wxFrame::OnSize(event);
341 }
342
343 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
344 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
345 END_EVENT_TABLE()
346
347 // Define a constructor for my canvas
348 MyCanvas::MyCanvas(wxFrame *frame, const wxPoint& pos, const wxSize& size, long style):
349 wxScrolledWindow(frame, -1, pos, size, style)
350 {
351 }
352
353 MyCanvas::~MyCanvas(void)
354 {
355 }
356
357 // Define the repainting behaviour
358 void MyCanvas::OnDraw(wxDC& dc)
359 {
360 frame->Draw(dc);
361 }
362
363 void MyCanvas::OnEvent(wxMouseEvent& WXUNUSED(event))
364 {
365 }
366
367 bool MyFrame::OnClose(void)
368 {
369 Show(FALSE);
370 delete wxGetApp().m_testFont;
371
372 return TRUE;
373 }
374
375 bool MyPrintout::OnPrintPage(int page)
376 {
377 wxDC *dc = GetDC();
378 if (dc)
379 {
380 if (page == 1)
381 DrawPageOne(dc);
382 else if (page == 2)
383 DrawPageTwo(dc);
384
385 dc->SetDeviceOrigin(0, 0);
386
387 char buf[200];
388 sprintf(buf, "PAGE %d", page);
389 dc->DrawText(buf, 10, 10);
390
391 return TRUE;
392 }
393 else
394 return FALSE;
395 }
396
397 bool MyPrintout::OnBeginDocument(int startPage, int endPage)
398 {
399 if (!wxPrintout::OnBeginDocument(startPage, endPage))
400 return FALSE;
401
402 return TRUE;
403 }
404
405 void MyPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
406 {
407 *minPage = 1;
408 *maxPage = 2;
409 *selPageFrom = 1;
410 *selPageTo = 2;
411 }
412
413 bool MyPrintout::HasPage(int pageNum)
414 {
415 return (pageNum == 1 || pageNum == 2);
416 }
417
418 void MyPrintout::DrawPageOne(wxDC *dc)
419 {
420 /* You might use THIS code if you were scaling
421 * graphics of known size to fit on the page.
422 */
423 int w, h;
424
425 // We know the graphic is 200x200. If we didn't know this,
426 // we'd need to calculate it.
427 float maxX = 200;
428 float maxY = 200;
429
430 // Let's have at least 50 device units margin
431 float marginX = 50;
432 float marginY = 50;
433
434 // Add the margin to the graphic size
435 maxX += (2*marginX);
436 maxY += (2*marginY);
437
438 // Get the size of the DC in pixels
439 dc->GetSize(&w, &h);
440
441 // Calculate a suitable scaling factor
442 float scaleX=(float)(w/maxX);
443 float scaleY=(float)(h/maxY);
444
445 // Use x or y scaling factor, whichever fits on the DC
446 float actualScale = wxMin(scaleX,scaleY);
447
448 // Calculate the position on the DC for centring the graphic
449 float posX = (float)((w - (200*actualScale))/2.0);
450 float posY = (float)((h - (200*actualScale))/2.0);
451
452 // Set the scale and origin
453 dc->SetUserScale(actualScale, actualScale);
454 dc->SetDeviceOrigin( (long)posX, (long)posY );
455
456 frame->Draw(*dc);
457 }
458
459 void MyPrintout::DrawPageTwo(wxDC *dc)
460 {
461 /* You might use THIS code to set the printer DC to ROUGHLY reflect
462 * the screen text size. This page also draws lines of actual length 5cm
463 * on the page.
464 */
465 // Get the logical pixels per inch of screen and printer
466 int ppiScreenX, ppiScreenY;
467 GetPPIScreen(&ppiScreenX, &ppiScreenY);
468 int ppiPrinterX, ppiPrinterY;
469 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
470
471 // This scales the DC so that the printout roughly represents the
472 // the screen scaling. The text point size _should_ be the right size
473 // but in fact is too small for some reason. This is a detail that will
474 // need to be addressed at some point but can be fudged for the
475 // moment.
476 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
477
478 // Now we have to check in case our real page size is reduced
479 // (e.g. because we're drawing to a print preview memory DC)
480 int pageWidth, pageHeight;
481 int w, h;
482 dc->GetSize(&w, &h);
483 GetPageSizePixels(&pageWidth, &pageHeight);
484
485 // If printer pageWidth == current DC width, then this doesn't
486 // change. But w might be the preview bitmap width, so scale down.
487 float overallScale = scale * (float)(w/(float)pageWidth);
488 dc->SetUserScale(overallScale, overallScale);
489
490 // Calculate conversion factor for converting millimetres into
491 // logical units.
492 // There are approx. 25.1 mm to the inch. There are ppi
493 // device units to the inch. Therefore 1 mm corresponds to
494 // ppi/25.1 device units. We also divide by the
495 // screen-to-printer scaling factor, because we need to
496 // unscale to pass logical units to DrawLine.
497
498 // Draw 50 mm by 50 mm L shape
499 float logUnitsFactor = (float)(ppiPrinterX/(scale*25.1));
500 float logUnits = (float)(50*logUnitsFactor);
501 dc->SetPen(* wxBLACK_PEN);
502 dc->DrawLine(50, 50, (long)(50.0 + logUnits), 50);
503 dc->DrawLine(50, 50, 50, (long)(50.0 + logUnits));
504
505 dc->SetFont(* wxGetApp().m_testFont);
506 dc->SetBackgroundMode(wxTRANSPARENT);
507
508 dc->DrawText("Some test text", 200, 200 );
509
510 // TESTING
511
512 int leftMargin = 20;
513 int rightMargin = 20;
514 int topMargin = 20;
515 int bottomMargin = 20;
516
517 int pageWidthMM, pageHeightMM;
518 GetPageSizeMM(&pageWidthMM, &pageHeightMM);
519
520 float leftMarginLogical = (float)(logUnitsFactor*leftMargin);
521 float topMarginLogical = (float)(logUnitsFactor*topMargin);
522 float bottomMarginLogical = (float)(logUnitsFactor*(pageHeightMM - bottomMargin));
523 float rightMarginLogical = (float)(logUnitsFactor*(pageWidthMM - rightMargin));
524
525 dc->SetPen(wxBLACK_PEN);
526 dc->DrawLine( (long)leftMarginLogical, (long)topMarginLogical,
527 (long)rightMarginLogical, (long)topMarginLogical);
528 dc->DrawLine( (long)leftMarginLogical, (long)bottomMarginLogical,
529 (long)rightMarginLogical, (long)bottomMarginLogical);
530
531 WritePageHeader(this, dc, "A header", logUnitsFactor);
532 }
533
534 // Writes a header on a page. Margin units are in millimetres.
535 bool WritePageHeader(wxPrintout *printout, wxDC *dc, char *text, float mmToLogical)
536 {
537 static wxFont *headerFont = (wxFont *) NULL;
538 if (!headerFont)
539 {
540 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
541 }
542 dc->SetFont(headerFont);
543
544 int pageWidthMM, pageHeightMM;
545
546 printout->GetPageSizeMM(&pageWidthMM, &pageHeightMM);
547
548 int leftMargin = 10;
549 int topMargin = 10;
550 int rightMargin = 10;
551
552 float leftMarginLogical = (float)(mmToLogical*leftMargin);
553 float topMarginLogical = (float)(mmToLogical*topMargin);
554 float rightMarginLogical = (float)(mmToLogical*(pageWidthMM - rightMargin));
555
556 long xExtent, yExtent;
557 dc->GetTextExtent(text, &xExtent, &yExtent);
558 float xPos = (float)(((((pageWidthMM - leftMargin - rightMargin)/2.0)+leftMargin)*mmToLogical) - (xExtent/2.0));
559 dc->DrawText(text, (long)xPos, (long)topMarginLogical);
560
561 dc->SetPen(* wxBLACK_PEN);
562 dc->DrawLine( (long)leftMarginLogical, (long)(topMarginLogical+yExtent),
563 (long)rightMarginLogical, (long)topMarginLogical+yExtent );
564
565 return TRUE;
566 }