Added some Motif wxGLCanvas support; some more Motif bugs cured; print dialogs
[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 wxPrinter printer;
164 MyPrintout printout("My printout");
165 if (!printer.Print(this, &printout, TRUE))
166 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK);
167 }
168
169 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
170 {
171 wxPrintData printData;
172 printData.SetOrientation(orientation);
173
174 // Pass two printout objects: for preview, and possible printing.
175 wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printData);
176 if (!preview->Ok())
177 {
178 delete preview;
179 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
180 return;
181 }
182
183 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
184 frame->Centre(wxBOTH);
185 frame->Initialize();
186 frame->Show(TRUE);
187 }
188
189 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
190 {
191 wxPrintData data;
192 data.SetOrientation(orientation);
193
194 wxPrintDialog printerDialog(this, & data);
195
196 printerDialog.GetPrintData().SetSetupDialog(TRUE);
197 printerDialog.ShowModal();
198
199 orientation = printerDialog.GetPrintData().GetOrientation();
200 }
201
202 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
203 {
204 wxPageSetupData data;
205 data.SetOrientation(orientation);
206
207 wxPageSetupDialog pageSetupDialog(this, & data);
208 pageSetupDialog.ShowModal();
209
210 data = pageSetupDialog.GetPageSetupData();
211 orientation = data.GetOrientation();
212 }
213
214 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
215 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
216 {
217 wxPostScriptPrinter printer;
218 MyPrintout printout("My printout");
219 printer.Print(this, &printout, TRUE);
220 }
221
222 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
223 {
224 wxPrintData printData;
225 printData.SetOrientation(orientation);
226
227 // Pass two printout objects: for preview, and possible printing.
228 wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printData);
229 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
230 frame->Centre(wxBOTH);
231 frame->Initialize();
232 frame->Show(TRUE);
233 }
234
235 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
236 {
237 wxPrintData data;
238 data.SetOrientation(orientation);
239
240 wxGenericPrintDialog printerDialog(this, & data);
241 printerDialog.GetPrintData().SetSetupDialog(TRUE);
242 printerDialog.ShowModal();
243
244 orientation = printerDialog.GetPrintData().GetOrientation();
245 }
246
247 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
248 {
249 wxPageSetupData data;
250 data.SetOrientation(orientation);
251
252 wxGenericPageSetupDialog pageSetupDialog(this, & data);
253 pageSetupDialog.ShowModal();
254
255 orientation = pageSetupDialog.GetPageSetupData().GetOrientation();
256 }
257 #endif
258
259
260 void MyFrame::OnPrintAbout(wxCommandEvent& WXUNUSED(event))
261 {
262 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
263 "About wxWindows printing demo", wxOK|wxCENTRE);
264 }
265
266 void MyFrame::Draw(wxDC& dc)
267 {
268 dc.SetFont(* wxGetApp().m_testFont);
269
270 dc.SetBackgroundMode(wxTRANSPARENT);
271
272 dc.SetBrush(* wxCYAN_BRUSH);
273 dc.SetPen(* wxRED_PEN);
274
275 dc.DrawRectangle(0, 30, 200, 100);
276 dc.DrawText("Rectangle 200 by 100", 40, 40);
277
278 dc.DrawEllipse(50, 140, 100, 50);
279
280 dc.DrawText("Test message: this is in 10 point text", 10, 180);
281
282 dc.SetPen(* wxBLACK_PEN);
283 dc.DrawLine(0, 0, 200, 200);
284 dc.DrawLine(200, 0, 0, 200);
285
286 wxIcon my_icon = wxICON(mondrian) ;
287
288 dc.DrawIcon( my_icon, 100, 100);
289 }
290
291 void MyFrame::OnSize(wxSizeEvent& event )
292 {
293 wxFrame::OnSize(event);
294 }
295
296 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
297 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
298 END_EVENT_TABLE()
299
300 // Define a constructor for my canvas
301 MyCanvas::MyCanvas(wxFrame *frame, const wxPoint& pos, const wxSize& size, long style):
302 wxScrolledWindow(frame, -1, pos, size, style)
303 {
304 }
305
306 MyCanvas::~MyCanvas(void)
307 {
308 }
309
310 // Define the repainting behaviour
311 void MyCanvas::OnDraw(wxDC& dc)
312 {
313 frame->Draw(dc);
314 }
315
316 void MyCanvas::OnEvent(wxMouseEvent& WXUNUSED(event))
317 {
318 }
319
320 bool MyFrame::OnClose(void)
321 {
322 Show(FALSE);
323 delete wxGetApp().m_testFont;
324
325 return TRUE;
326 }
327
328 bool MyPrintout::OnPrintPage(int page)
329 {
330 wxDC *dc = GetDC();
331 if (dc)
332 {
333 if (page == 1)
334 DrawPageOne(dc);
335 else if (page == 2)
336 DrawPageTwo(dc);
337
338 dc->SetDeviceOrigin(0, 0);
339 dc->SetUserScale(1.0, 1.0);
340
341 char buf[200];
342 sprintf(buf, "PAGE %d", page);
343 dc->DrawText(buf, 10, 10);
344
345 return TRUE;
346 }
347 else
348 return FALSE;
349 }
350
351 bool MyPrintout::OnBeginDocument(int startPage, int endPage)
352 {
353 if (!wxPrintout::OnBeginDocument(startPage, endPage))
354 return FALSE;
355
356 return TRUE;
357 }
358
359 void MyPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
360 {
361 *minPage = 1;
362 *maxPage = 2;
363 *selPageFrom = 1;
364 *selPageTo = 2;
365 }
366
367 bool MyPrintout::HasPage(int pageNum)
368 {
369 return (pageNum == 1 || pageNum == 2);
370 }
371
372 void MyPrintout::DrawPageOne(wxDC *dc)
373 {
374 /* You might use THIS code if you were scaling
375 * graphics of known size to fit on the page.
376 */
377 int w, h;
378
379 // We know the graphic is 200x200. If we didn't know this,
380 // we'd need to calculate it.
381 float maxX = 200;
382 float maxY = 200;
383
384 // Let's have at least 50 device units margin
385 float marginX = 50;
386 float marginY = 50;
387
388 // Add the margin to the graphic size
389 maxX += (2*marginX);
390 maxY += (2*marginY);
391
392 // Get the size of the DC in pixels
393 dc->GetSize(&w, &h);
394
395 // Calculate a suitable scaling factor
396 float scaleX=(float)(w/maxX);
397 float scaleY=(float)(h/maxY);
398
399 // Use x or y scaling factor, whichever fits on the DC
400 float actualScale = wxMin(scaleX,scaleY);
401
402 // Calculate the position on the DC for centring the graphic
403 float posX = (float)((w - (200*actualScale))/2.0);
404 float posY = (float)((h - (200*actualScale))/2.0);
405
406 // Set the scale and origin
407 dc->SetUserScale(actualScale, actualScale);
408 dc->SetDeviceOrigin( (long)posX, (long)posY );
409
410 frame->Draw(*dc);
411 }
412
413 void MyPrintout::DrawPageTwo(wxDC *dc)
414 {
415 /* You might use THIS code to set the printer DC to ROUGHLY reflect
416 * the screen text size. This page also draws lines of actual length 5cm
417 * on the page.
418 */
419 // Get the logical pixels per inch of screen and printer
420 int ppiScreenX, ppiScreenY;
421 GetPPIScreen(&ppiScreenX, &ppiScreenY);
422 int ppiPrinterX, ppiPrinterY;
423 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
424
425 // This scales the DC so that the printout roughly represents the
426 // the screen scaling. The text point size _should_ be the right size
427 // but in fact is too small for some reason. This is a detail that will
428 // need to be addressed at some point but can be fudged for the
429 // moment.
430 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
431
432 // Now we have to check in case our real page size is reduced
433 // (e.g. because we're drawing to a print preview memory DC)
434 int pageWidth, pageHeight;
435 int w, h;
436 dc->GetSize(&w, &h);
437 GetPageSizePixels(&pageWidth, &pageHeight);
438
439 // If printer pageWidth == current DC width, then this doesn't
440 // change. But w might be the preview bitmap width, so scale down.
441 float overallScale = scale * (float)(w/(float)pageWidth);
442 dc->SetUserScale(overallScale, overallScale);
443
444 // Calculate conversion factor for converting millimetres into
445 // logical units.
446 // There are approx. 25.1 mm to the inch. There are ppi
447 // device units to the inch. Therefore 1 mm corresponds to
448 // ppi/25.1 device units. We also divide by the
449 // screen-to-printer scaling factor, because we need to
450 // unscale to pass logical units to DrawLine.
451
452 // Draw 50 mm by 50 mm L shape
453 float logUnitsFactor = (float)(ppiPrinterX/(scale*25.1));
454 float logUnits = (float)(50*logUnitsFactor);
455 dc->SetPen(* wxBLACK_PEN);
456 dc->DrawLine(50, 250, (long)(50.0 + logUnits), 250);
457 dc->DrawLine(50, 250, 50, (long)(250.0 + logUnits));
458
459 dc->SetFont(* wxGetApp().m_testFont);
460 dc->SetBackgroundMode(wxTRANSPARENT);
461
462 dc->DrawText("Some test text", 200, 200 );
463
464 // TESTING
465
466 int leftMargin = 20;
467 int rightMargin = 20;
468 int topMargin = 20;
469 int bottomMargin = 20;
470
471 int pageWidthMM, pageHeightMM;
472 GetPageSizeMM(&pageWidthMM, &pageHeightMM);
473
474 float leftMarginLogical = (float)(logUnitsFactor*leftMargin);
475 float topMarginLogical = (float)(logUnitsFactor*topMargin);
476 float bottomMarginLogical = (float)(logUnitsFactor*(pageHeightMM - bottomMargin));
477 float rightMarginLogical = (float)(logUnitsFactor*(pageWidthMM - rightMargin));
478
479 dc->SetPen(* wxRED_PEN);
480 dc->DrawLine( (long)leftMarginLogical, (long)topMarginLogical,
481 (long)rightMarginLogical, (long)topMarginLogical);
482 dc->DrawLine( (long)leftMarginLogical, (long)bottomMarginLogical,
483 (long)rightMarginLogical, (long)bottomMarginLogical);
484
485 WritePageHeader(this, dc, "A header", logUnitsFactor);
486 }
487
488 // Writes a header on a page. Margin units are in millimetres.
489 bool WritePageHeader(wxPrintout *printout, wxDC *dc, char *text, float mmToLogical)
490 {
491 /*
492 static wxFont *headerFont = (wxFont *) NULL;
493 if (!headerFont)
494 {
495 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
496 }
497 dc->SetFont(headerFont);
498 */
499
500 int pageWidthMM, pageHeightMM;
501
502 printout->GetPageSizeMM(&pageWidthMM, &pageHeightMM);
503
504 int leftMargin = 10;
505 int topMargin = 10;
506 int rightMargin = 10;
507
508 float leftMarginLogical = (float)(mmToLogical*leftMargin);
509 float topMarginLogical = (float)(mmToLogical*topMargin);
510 float rightMarginLogical = (float)(mmToLogical*(pageWidthMM - rightMargin));
511
512 long xExtent, yExtent;
513 dc->GetTextExtent(text, &xExtent, &yExtent);
514 float xPos = (float)(((((pageWidthMM - leftMargin - rightMargin)/2.0)+leftMargin)*mmToLogical) - (xExtent/2.0));
515 dc->DrawText(text, (long)xPos, (long)topMarginLogical);
516
517 dc->SetPen(* wxBLACK_PEN);
518 dc->DrawLine( (long)leftMarginLogical, (long)(topMarginLogical+yExtent),
519 (long)rightMarginLogical, (long)topMarginLogical+yExtent );
520
521 return TRUE;
522 }