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