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