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