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