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