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