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