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