New PostScript 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 dc->SetUserScale(1.0, 1.0);
387
388 char buf[200];
389 sprintf(buf, "PAGE %d", page);
390 dc->DrawText(buf, 10, 10);
391
392 return TRUE;
393 }
394 else
395 return FALSE;
396 }
397
398 bool MyPrintout::OnBeginDocument(int startPage, int endPage)
399 {
400 if (!wxPrintout::OnBeginDocument(startPage, endPage))
401 return FALSE;
402
403 return TRUE;
404 }
405
406 void MyPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
407 {
408 *minPage = 1;
409 *maxPage = 2;
410 *selPageFrom = 1;
411 *selPageTo = 2;
412 }
413
414 bool MyPrintout::HasPage(int pageNum)
415 {
416 return (pageNum == 1 || pageNum == 2);
417 }
418
419 void MyPrintout::DrawPageOne(wxDC *dc)
420 {
421 /* You might use THIS code if you were scaling
422 * graphics of known size to fit on the page.
423 */
424 int w, h;
425
426 // We know the graphic is 200x200. If we didn't know this,
427 // we'd need to calculate it.
428 float maxX = 200;
429 float maxY = 200;
430
431 // Let's have at least 50 device units margin
432 float marginX = 50;
433 float marginY = 50;
434
435 // Add the margin to the graphic size
436 maxX += (2*marginX);
437 maxY += (2*marginY);
438
439 // Get the size of the DC in pixels
440 dc->GetSize(&w, &h);
441
442 // Calculate a suitable scaling factor
443 float scaleX=(float)(w/maxX);
444 float scaleY=(float)(h/maxY);
445
446 // Use x or y scaling factor, whichever fits on the DC
447 float actualScale = wxMin(scaleX,scaleY);
448
449 // Calculate the position on the DC for centring the graphic
450 float posX = (float)((w - (200*actualScale))/2.0);
451 float posY = (float)((h - (200*actualScale))/2.0);
452
453 // Set the scale and origin
454 dc->SetUserScale(actualScale, actualScale);
455 dc->SetDeviceOrigin( (long)posX, (long)posY );
456
457 frame->Draw(*dc);
458 }
459
460 void MyPrintout::DrawPageTwo(wxDC *dc)
461 {
462 /* You might use THIS code to set the printer DC to ROUGHLY reflect
463 * the screen text size. This page also draws lines of actual length 5cm
464 * on the page.
465 */
466 // Get the logical pixels per inch of screen and printer
467 int ppiScreenX, ppiScreenY;
468 GetPPIScreen(&ppiScreenX, &ppiScreenY);
469 int ppiPrinterX, ppiPrinterY;
470 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
471
472 // This scales the DC so that the printout roughly represents the
473 // the screen scaling. The text point size _should_ be the right size
474 // but in fact is too small for some reason. This is a detail that will
475 // need to be addressed at some point but can be fudged for the
476 // moment.
477 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
478
479 // Now we have to check in case our real page size is reduced
480 // (e.g. because we're drawing to a print preview memory DC)
481 int pageWidth, pageHeight;
482 int w, h;
483 dc->GetSize(&w, &h);
484 GetPageSizePixels(&pageWidth, &pageHeight);
485
486 // If printer pageWidth == current DC width, then this doesn't
487 // change. But w might be the preview bitmap width, so scale down.
488 float overallScale = scale * (float)(w/(float)pageWidth);
489 dc->SetUserScale(overallScale, overallScale);
490
491 // Calculate conversion factor for converting millimetres into
492 // logical units.
493 // There are approx. 25.1 mm to the inch. There are ppi
494 // device units to the inch. Therefore 1 mm corresponds to
495 // ppi/25.1 device units. We also divide by the
496 // screen-to-printer scaling factor, because we need to
497 // unscale to pass logical units to DrawLine.
498
499 // Draw 50 mm by 50 mm L shape
500 float logUnitsFactor = (float)(ppiPrinterX/(scale*25.1));
501 float logUnits = (float)(50*logUnitsFactor);
502 dc->SetPen(* wxBLACK_PEN);
503 dc->DrawLine(50, 250, (long)(50.0 + logUnits), 250);
504 dc->DrawLine(50, 250, 50, (long)(250.0 + logUnits));
505
506 dc->SetFont(* wxGetApp().m_testFont);
507 dc->SetBackgroundMode(wxTRANSPARENT);
508
509 dc->DrawText("Some test text", 200, 200 );
510
511 // TESTING
512
513 int leftMargin = 20;
514 int rightMargin = 20;
515 int topMargin = 20;
516 int bottomMargin = 20;
517
518 int pageWidthMM, pageHeightMM;
519 GetPageSizeMM(&pageWidthMM, &pageHeightMM);
520
521 float leftMarginLogical = (float)(logUnitsFactor*leftMargin);
522 float topMarginLogical = (float)(logUnitsFactor*topMargin);
523 float bottomMarginLogical = (float)(logUnitsFactor*(pageHeightMM - bottomMargin));
524 float rightMarginLogical = (float)(logUnitsFactor*(pageWidthMM - rightMargin));
525
526 dc->SetPen(wxRED_PEN);
527 dc->DrawLine( (long)leftMarginLogical, (long)topMarginLogical,
528 (long)rightMarginLogical, (long)topMarginLogical);
529 dc->DrawLine( (long)leftMarginLogical, (long)bottomMarginLogical,
530 (long)rightMarginLogical, (long)bottomMarginLogical);
531
532 WritePageHeader(this, dc, "A header", logUnitsFactor);
533 }
534
535 // Writes a header on a page. Margin units are in millimetres.
536 bool WritePageHeader(wxPrintout *printout, wxDC *dc, char *text, float mmToLogical)
537 {
538 /*
539 static wxFont *headerFont = (wxFont *) NULL;
540 if (!headerFont)
541 {
542 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
543 }
544 dc->SetFont(headerFont);
545 */
546
547 int pageWidthMM, pageHeightMM;
548
549 printout->GetPageSizeMM(&pageWidthMM, &pageHeightMM);
550
551 int leftMargin = 10;
552 int topMargin = 10;
553 int rightMargin = 10;
554
555 float leftMarginLogical = (float)(mmToLogical*leftMargin);
556 float topMarginLogical = (float)(mmToLogical*topMargin);
557 float rightMarginLogical = (float)(mmToLogical*(pageWidthMM - rightMargin));
558
559 long xExtent, yExtent;
560 dc->GetTextExtent(text, &xExtent, &yExtent);
561 float xPos = (float)(((((pageWidthMM - leftMargin - rightMargin)/2.0)+leftMargin)*mmToLogical) - (xExtent/2.0));
562 dc->DrawText(text, (long)xPos, (long)topMarginLogical);
563
564 dc->SetPen(* wxBLACK_PEN);
565 dc->DrawLine( (long)leftMarginLogical, (long)(topMarginLogical+yExtent),
566 (long)rightMarginLogical, (long)topMarginLogical+yExtent );
567
568 return TRUE;
569 }