]> git.saurik.com Git - wxWidgets.git/blob - samples/printing/printing.cpp
patch from Paul Gammans <paul.gammans@brunel.ac.uk> for cross compiling for MSW
[wxWidgets.git] / samples / printing / printing.cpp
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
54 MyFrame *frame = (MyFrame *) NULL;
55 // int orientation = wxPORTRAIT;
56
57 // Global print data, to remember settings during the session
58 wxPrintData *g_printData = (wxPrintData*) NULL ;
59
60 // Global page setup data
61 wxPageSetupData* g_pageSetupData = (wxPageSetupData*) NULL;
62
63 // Main proc
64 IMPLEMENT_APP(MyApp)
65
66 // Writes a header on a page. Margin units are in millimetres.
67 bool WritePageHeader(wxPrintout *printout, wxDC *dc, char *text, float mmToLogical);
68
69 MyApp::MyApp()
70 {
71 }
72
73 // The `main program' equivalent, creating the windows and returning the
74 // main frame
75 bool 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
153 int MyApp::OnExit()
154 {
155 delete wxGetApp().m_testFont;
156 delete g_printData;
157 delete g_pageSetupData;
158 return 1;
159 }
160
161 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
162 EVT_MENU(WXPRINT_QUIT, MyFrame::OnExit)
163 EVT_MENU(WXPRINT_PRINT, MyFrame::OnPrint)
164 EVT_MENU(WXPRINT_PREVIEW, MyFrame::OnPrintPreview)
165 EVT_MENU(WXPRINT_PRINT_SETUP, MyFrame::OnPrintSetup)
166 EVT_MENU(WXPRINT_PAGE_SETUP, MyFrame::OnPageSetup)
167 EVT_MENU(WXPRINT_ABOUT, MyFrame::OnPrintAbout)
168 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
169 EVT_MENU(WXPRINT_PRINT_PS, MyFrame::OnPrintPS)
170 EVT_MENU(WXPRINT_PREVIEW_PS, MyFrame::OnPrintPreviewPS)
171 EVT_MENU(WXPRINT_PRINT_SETUP_PS, MyFrame::OnPrintSetupPS)
172 EVT_MENU(WXPRINT_PAGE_SETUP_PS, MyFrame::OnPageSetupPS)
173 #endif
174 END_EVENT_TABLE()
175
176 // Define my frame constructor
177 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
178 wxFrame(frame, -1, title, pos, size)
179 {
180 canvas = (MyCanvas *) NULL;
181 }
182
183 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
184 {
185 Close(TRUE);
186 }
187
188 void 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 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK);
196 else
197 {
198 (*g_printData) = printer.GetPrintDialogData().GetPrintData();
199 }
200 }
201
202 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
203 {
204 // Pass two printout objects: for preview, and possible printing.
205 wxPrintDialogData printDialogData(* g_printData);
206 wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printDialogData);
207 if (!preview->Ok())
208 {
209 delete preview;
210 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
211 return;
212 }
213
214 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
215 frame->Centre(wxBOTH);
216 frame->Initialize();
217 frame->Show(TRUE);
218 }
219
220 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
221 {
222 wxPrintDialogData printDialogData(* g_printData);
223 wxPrintDialog printerDialog(this, & printDialogData);
224
225 printerDialog.GetPrintDialogData().SetSetupDialog(TRUE);
226 printerDialog.ShowModal();
227
228 (*g_printData) = printerDialog.GetPrintDialogData().GetPrintData();
229 }
230
231 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
232 {
233 (*g_pageSetupData) = * g_printData;
234
235 wxPageSetupDialog pageSetupDialog(this, g_pageSetupData);
236 pageSetupDialog.ShowModal();
237
238 (*g_printData) = pageSetupDialog.GetPageSetupData().GetPrintData();
239 (*g_pageSetupData) = pageSetupDialog.GetPageSetupData();
240 }
241
242 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
243 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
244 {
245 wxPostScriptPrinter printer(g_printData);
246 MyPrintout printout("My printout");
247 printer.Print(this, &printout, TRUE);
248
249 (*g_printData) = printer.GetPrintData();
250 }
251
252 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
253 {
254 // Pass two printout objects: for preview, and possible printing.
255 wxPrintDialogData printDialogData(* g_printData);
256 wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printDialogData);
257 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
258 frame->Centre(wxBOTH);
259 frame->Initialize();
260 frame->Show(TRUE);
261 }
262
263 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
264 {
265 wxPrintDialogData printDialogData(* g_printData);
266 wxGenericPrintDialog printerDialog(this, & printDialogData);
267
268 printerDialog.GetPrintDialogData().SetSetupDialog(TRUE);
269 printerDialog.ShowModal();
270
271 (*g_printData) = printerDialog.GetPrintDialogData().GetPrintData();
272 }
273
274 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
275 {
276 (*g_pageSetupData) = * g_printData;
277
278 wxGenericPageSetupDialog pageSetupDialog(this, g_pageSetupData);
279 pageSetupDialog.ShowModal();
280
281 (*g_printData) = pageSetupDialog.GetPageSetupData().GetPrintData();
282 (*g_pageSetupData) = pageSetupDialog.GetPageSetupData();
283 }
284 #endif
285
286
287 void MyFrame::OnPrintAbout(wxCommandEvent& WXUNUSED(event))
288 {
289 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
290 "About wxWindows printing demo", wxOK|wxCENTRE);
291 }
292
293 void MyFrame::Draw(wxDC& dc)
294 {
295 dc.SetBackground(*wxWHITE_BRUSH);
296 dc.Clear();
297 dc.SetFont(* wxGetApp().m_testFont);
298
299 dc.SetBackgroundMode(wxTRANSPARENT);
300
301 dc.SetBrush(* wxCYAN_BRUSH);
302 dc.SetPen(* wxRED_PEN);
303
304 dc.DrawRectangle(0, 30, 200, 100);
305 dc.DrawText("Rectangle 200 by 100", 40, 40);
306
307 dc.DrawEllipse(50, 140, 100, 50);
308
309 dc.DrawText("Test message: this is in 10 point text", 10, 180);
310
311 dc.SetPen(* wxBLACK_PEN);
312 dc.DrawLine(0, 0, 200, 200);
313 dc.DrawLine(200, 0, 0, 200);
314
315 wxIcon my_icon = wxICON(mondrian) ;
316
317 dc.DrawIcon( my_icon, 100, 100);
318 }
319
320 void MyFrame::OnSize(wxSizeEvent& event )
321 {
322 wxFrame::OnSize(event);
323 }
324
325 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
326 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
327 END_EVENT_TABLE()
328
329 // Define a constructor for my canvas
330 MyCanvas::MyCanvas(wxFrame *frame, const wxPoint& pos, const wxSize& size, long style):
331 wxScrolledWindow(frame, -1, pos, size, style)
332 {
333 SetBackgroundColour(* wxWHITE);
334 }
335
336 MyCanvas::~MyCanvas(void)
337 {
338 }
339
340 // Define the repainting behaviour
341 void MyCanvas::OnDraw(wxDC& dc)
342 {
343 frame->Draw(dc);
344 }
345
346 void MyCanvas::OnEvent(wxMouseEvent& WXUNUSED(event))
347 {
348 }
349
350 bool MyPrintout::OnPrintPage(int page)
351 {
352 wxDC *dc = GetDC();
353 if (dc)
354 {
355 if (page == 1)
356 DrawPageOne(dc);
357 else if (page == 2)
358 DrawPageTwo(dc);
359
360 dc->SetDeviceOrigin(0, 0);
361 dc->SetUserScale(1.0, 1.0);
362
363 char buf[200];
364 sprintf(buf, "PAGE %d", page);
365 dc->DrawText(buf, 10, 10);
366
367 return TRUE;
368 }
369 else
370 return FALSE;
371 }
372
373 bool MyPrintout::OnBeginDocument(int startPage, int endPage)
374 {
375 if (!wxPrintout::OnBeginDocument(startPage, endPage))
376 return FALSE;
377
378 return TRUE;
379 }
380
381 void MyPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
382 {
383 *minPage = 1;
384 *maxPage = 2;
385 *selPageFrom = 1;
386 *selPageTo = 2;
387 }
388
389 bool MyPrintout::HasPage(int pageNum)
390 {
391 return (pageNum == 1 || pageNum == 2);
392 }
393
394 void MyPrintout::DrawPageOne(wxDC *dc)
395 {
396 /* You might use THIS code if you were scaling
397 * graphics of known size to fit on the page.
398 */
399 int w, h;
400
401 // We know the graphic is 200x200. If we didn't know this,
402 // we'd need to calculate it.
403 float maxX = 200;
404 float maxY = 200;
405
406 // Let's have at least 50 device units margin
407 float marginX = 50;
408 float marginY = 50;
409
410 // Add the margin to the graphic size
411 maxX += (2*marginX);
412 maxY += (2*marginY);
413
414 // Get the size of the DC in pixels
415 dc->GetSize(&w, &h);
416
417 // Calculate a suitable scaling factor
418 float scaleX=(float)(w/maxX);
419 float scaleY=(float)(h/maxY);
420
421 // Use x or y scaling factor, whichever fits on the DC
422 float actualScale = wxMin(scaleX,scaleY);
423
424 // Calculate the position on the DC for centring the graphic
425 float posX = (float)((w - (200*actualScale))/2.0);
426 float posY = (float)((h - (200*actualScale))/2.0);
427
428 // Set the scale and origin
429 dc->SetUserScale(actualScale, actualScale);
430 dc->SetDeviceOrigin( (long)posX, (long)posY );
431
432 frame->Draw(*dc);
433 }
434
435 void MyPrintout::DrawPageTwo(wxDC *dc)
436 {
437 /* You might use THIS code to set the printer DC to ROUGHLY reflect
438 * the screen text size. This page also draws lines of actual length 5cm
439 * on the page.
440 */
441 // Get the logical pixels per inch of screen and printer
442 int ppiScreenX, ppiScreenY;
443 GetPPIScreen(&ppiScreenX, &ppiScreenY);
444 int ppiPrinterX, ppiPrinterY;
445 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
446
447 // This scales the DC so that the printout roughly represents the
448 // the screen scaling. The text point size _should_ be the right size
449 // but in fact is too small for some reason. This is a detail that will
450 // need to be addressed at some point but can be fudged for the
451 // moment.
452 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
453
454 // Now we have to check in case our real page size is reduced
455 // (e.g. because we're drawing to a print preview memory DC)
456 int pageWidth, pageHeight;
457 int w, h;
458 dc->GetSize(&w, &h);
459 GetPageSizePixels(&pageWidth, &pageHeight);
460
461 // If printer pageWidth == current DC width, then this doesn't
462 // change. But w might be the preview bitmap width, so scale down.
463 float overallScale = scale * (float)(w/(float)pageWidth);
464 dc->SetUserScale(overallScale, overallScale);
465
466 // Calculate conversion factor for converting millimetres into
467 // logical units.
468 // There are approx. 25.1 mm to the inch. There are ppi
469 // device units to the inch. Therefore 1 mm corresponds to
470 // ppi/25.1 device units. We also divide by the
471 // screen-to-printer scaling factor, because we need to
472 // unscale to pass logical units to DrawLine.
473
474 // Draw 50 mm by 50 mm L shape
475 float logUnitsFactor = (float)(ppiPrinterX/(scale*25.1));
476 float logUnits = (float)(50*logUnitsFactor);
477 dc->SetPen(* wxBLACK_PEN);
478 dc->DrawLine(50, 250, (long)(50.0 + logUnits), 250);
479 dc->DrawLine(50, 250, 50, (long)(250.0 + logUnits));
480
481 dc->SetFont(* wxGetApp().m_testFont);
482 dc->SetBackgroundMode(wxTRANSPARENT);
483
484 dc->DrawText("Some test text", 200, 200 );
485
486 { // GetTextExtent demo:
487 wxString words[7] = {"This ", "is ", "GetTextExtent ", "testing ", "string. ", "Enjoy ", "it!"};
488 long w, h;
489 long x = 200, y= 250;
490 wxFont fnt(15, wxSWISS, wxNORMAL, wxNORMAL);
491
492 dc->SetFont(fnt);
493 for (int i = 0; i < 7; i++) {
494 dc->GetTextExtent(words[i], &w, &h);
495 dc->DrawRectangle(x, y, w, h);
496 dc->DrawText(words[i], x, y);
497 x += w;
498 }
499 dc->SetFont(* wxGetApp().m_testFont);
500 }
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(* wxRED_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 /*
530 static wxFont *headerFont = (wxFont *) NULL;
531 if (!headerFont)
532 {
533 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
534 }
535 dc->SetFont(headerFont);
536 */
537
538 int pageWidthMM, pageHeightMM;
539
540 printout->GetPageSizeMM(&pageWidthMM, &pageHeightMM);
541
542 int leftMargin = 10;
543 int topMargin = 10;
544 int rightMargin = 10;
545
546 float leftMarginLogical = (float)(mmToLogical*leftMargin);
547 float topMarginLogical = (float)(mmToLogical*topMargin);
548 float rightMarginLogical = (float)(mmToLogical*(pageWidthMM - rightMargin));
549
550 long xExtent, yExtent;
551 dc->GetTextExtent(text, &xExtent, &yExtent);
552 float xPos = (float)(((((pageWidthMM - leftMargin - rightMargin)/2.0)+leftMargin)*mmToLogical) - (xExtent/2.0));
553 dc->DrawText(text, (long)xPos, (long)topMarginLogical);
554
555 dc->SetPen(* wxBLACK_PEN);
556 dc->DrawLine( (long)leftMarginLogical, (long)(topMarginLogical+yExtent),
557 (long)rightMarginLogical, (long)topMarginLogical+yExtent );
558
559 return TRUE;
560 }