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