1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Printing demo for wxWindows
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
27 #if !wxUSE_PRINTING_ARCHITECTURE
28 #error You must set wxUSE_PRINTING_ARCHITECTURE to 1 in setup.h to compile this demo.
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
36 #include "wx/metafile.h"
38 #include "wx/printdlg.h"
42 #if wxTEST_POSTSCRIPT_IN_MSW
43 #include "wx/generic/printps.h"
44 #include "wx/generic/prntdlgg.h"
50 #include "mondrian.xpm"
54 MyFrame
*frame
= (MyFrame
*) NULL
;
55 // int orientation = wxPORTRAIT;
57 // Global print data, to remember settings during the session
58 wxPrintData
*g_printData
= (wxPrintData
*) NULL
;
60 // Global page setup data
61 wxPageSetupData
* g_pageSetupData
= (wxPageSetupData
*) NULL
;
66 // Writes a header on a page. Margin units are in millimetres.
67 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
);
73 // The `main program' equivalent, creating the windows and returning the
75 bool MyApp::OnInit(void)
77 m_testFont
= new wxFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
79 g_printData
= new wxPrintData
;
80 g_pageSetupData
= new wxPageSetupDialogData
;
82 // wxGetenv( wxT("HOME") );
84 // Compatibility with old system. In fact, we might keep wxThePrintSetupData
85 // just for useful default values which we can optionally assign to our
86 // own print data object.
88 #if defined(__WXGTK__) || defined(__WXMOTIF__)
89 (*g_printData
) = * wxThePrintSetupData
;
92 // Create the main frame window
93 frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows Printing Demo", wxPoint(0, 0), wxSize(400, 400));
95 // Give it a status line
96 frame
->CreateStatusBar(2);
98 // Load icon and bitmap
99 frame
->SetIcon( wxICON( mondrian
) );
102 wxMenu
*file_menu
= new wxMenu
;
104 file_menu
->Append(WXPRINT_PRINT
, "&Print...", "Print");
105 file_menu
->Append(WXPRINT_PRINT_SETUP
, "Print &Setup...", "Setup printer properties");
106 file_menu
->Append(WXPRINT_PAGE_SETUP
, "Page Set&up...", "Page setup");
107 file_menu
->Append(WXPRINT_PREVIEW
, "Print Pre&view", "Preview");
111 wxAcceleratorEntry entries
[1];
112 entries
[0].Set(wxACCEL_CTRL
, (int) 'V', WXPRINT_PREVIEW
);
113 wxAcceleratorTable
accel(1, entries
);
114 frame
->SetAcceleratorTable(accel
);
117 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
118 file_menu
->AppendSeparator();
119 file_menu
->Append(WXPRINT_PRINT_PS
, "Print PostScript...", "Print (PostScript)");
120 file_menu
->Append(WXPRINT_PRINT_SETUP_PS
, "Print Setup PostScript...", "Setup printer properties (PostScript)");
121 file_menu
->Append(WXPRINT_PAGE_SETUP_PS
, "Page Setup PostScript...", "Page setup (PostScript)");
122 file_menu
->Append(WXPRINT_PREVIEW_PS
, "Print Preview PostScript", "Preview (PostScript)");
124 file_menu
->AppendSeparator();
125 file_menu
->Append(WXPRINT_QUIT
, "E&xit", "Exit program");
127 wxMenu
*help_menu
= new wxMenu
;
128 help_menu
->Append(WXPRINT_ABOUT
, "&About", "About this demo");
130 wxMenuBar
*menu_bar
= new wxMenuBar
;
132 menu_bar
->Append(file_menu
, "&File");
133 menu_bar
->Append(help_menu
, "&Help");
135 // Associate the menu bar with the frame
136 frame
->SetMenuBar(menu_bar
);
138 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100), wxRETAINED
|wxHSCROLL
|wxVSCROLL
);
140 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
141 canvas
->SetScrollbars(20, 20, 50, 50);
143 frame
->canvas
= canvas
;
145 frame
->Centre(wxBOTH
);
148 frame
->SetStatusText("Printing demo");
157 delete wxGetApp().m_testFont
;
159 delete g_pageSetupData
;
163 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
164 EVT_MENU(WXPRINT_QUIT
, MyFrame::OnExit
)
165 EVT_MENU(WXPRINT_PRINT
, MyFrame::OnPrint
)
166 EVT_MENU(WXPRINT_PREVIEW
, MyFrame::OnPrintPreview
)
167 EVT_MENU(WXPRINT_PRINT_SETUP
, MyFrame::OnPrintSetup
)
168 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
169 EVT_MENU(WXPRINT_ABOUT
, MyFrame::OnPrintAbout
)
170 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
171 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame::OnPrintPS
)
172 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
173 EVT_MENU(WXPRINT_PRINT_SETUP_PS
, MyFrame::OnPrintSetupPS
)
174 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
178 // Define my frame constructor
179 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
180 wxFrame(frame
, -1, title
, pos
, size
)
182 canvas
= (MyCanvas
*) NULL
;
185 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
190 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
192 wxPrintDialogData
printDialogData(* g_printData
);
194 wxPrinter
printer(& printDialogData
);
195 MyPrintout
printout("My printout");
196 if (!printer
.Print(this, &printout
, TRUE
))
198 if (wxPrinter::GetLastError() == wxPRINTER_ERROR
)
199 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
);
201 wxMessageBox("You canceled printing", "Printing", wxOK
);
205 (*g_printData
) = printer
.GetPrintDialogData().GetPrintData();
209 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
211 // Pass two printout objects: for preview, and possible printing.
212 wxPrintDialogData
printDialogData(* g_printData
);
213 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printDialogData
);
217 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK
);
221 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
222 frame
->Centre(wxBOTH
);
227 void MyFrame::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
229 wxPrintDialogData
printDialogData(* g_printData
);
230 wxPrintDialog
printerDialog(this, & printDialogData
);
232 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
233 printerDialog
.ShowModal();
235 (*g_printData
) = printerDialog
.GetPrintDialogData().GetPrintData();
238 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
240 (*g_pageSetupData
) = * g_printData
;
242 wxPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
243 pageSetupDialog
.ShowModal();
245 (*g_printData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
246 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupData();
249 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
250 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
252 wxPostScriptPrinter
printer(g_printData
);
253 MyPrintout
printout("My printout");
254 printer
.Print(this, &printout
, TRUE
);
256 (*g_printData
) = printer
.GetPrintData();
259 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
261 // Pass two printout objects: for preview, and possible printing.
262 wxPrintDialogData
printDialogData(* g_printData
);
263 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printDialogData
);
264 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
265 frame
->Centre(wxBOTH
);
270 void MyFrame::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
272 wxPrintDialogData
printDialogData(* g_printData
);
273 wxGenericPrintDialog
printerDialog(this, & printDialogData
);
275 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
276 printerDialog
.ShowModal();
278 (*g_printData
) = printerDialog
.GetPrintDialogData().GetPrintData();
281 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
283 (*g_pageSetupData
) = * g_printData
;
285 wxGenericPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
286 pageSetupDialog
.ShowModal();
288 (*g_printData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
289 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupData();
294 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
296 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
297 "About wxWindows printing demo", wxOK
|wxCENTRE
);
300 void MyFrame::Draw(wxDC
& dc
)
302 dc
.SetBackground(*wxWHITE_BRUSH
);
304 dc
.SetFont(* wxGetApp().m_testFont
);
306 dc
.SetBackgroundMode(wxTRANSPARENT
);
308 dc
.SetBrush(* wxCYAN_BRUSH
);
309 dc
.SetPen(* wxRED_PEN
);
311 dc
.DrawRectangle(0, 30, 200, 100);
313 dc
.DrawText( wxT("Rectangle 200 by 100"), 40, 40);
315 dc
.DrawEllipse(50, 140, 100, 50);
317 dc
.DrawText( wxT("Test message: this is in 10 point text"), 10, 180);
320 char *test
= "Greek (Ελληνικά) Γειά σας -- Hebrew שלום -- Japanese (日本語)";
321 wxString tmp
= wxConvUTF8
.cMB2WC( test
);
322 dc
.DrawText( tmp
, 10, 200 );
325 dc
.SetPen(* wxBLACK_PEN
);
326 dc
.DrawLine(0, 0, 200, 200);
327 dc
.DrawLine(200, 0, 0, 200);
329 wxIcon my_icon
= wxICON(mondrian
) ;
331 dc
.DrawIcon( my_icon
, 100, 100);
334 void MyFrame::OnSize(wxSizeEvent
& event
)
336 wxFrame::OnSize(event
);
339 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
340 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
343 // Define a constructor for my canvas
344 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
345 wxScrolledWindow(frame
, -1, pos
, size
, style
)
347 SetBackgroundColour(* wxWHITE
);
350 MyCanvas::~MyCanvas(void)
354 // Define the repainting behaviour
355 void MyCanvas::OnDraw(wxDC
& dc
)
360 void MyCanvas::OnEvent(wxMouseEvent
& WXUNUSED(event
))
364 bool MyPrintout::OnPrintPage(int page
)
374 dc
->SetDeviceOrigin(0, 0);
375 dc
->SetUserScale(1.0, 1.0);
378 wxSprintf(buf
, wxT("PAGE %d"), page
);
379 dc
->DrawText(buf
, 10, 10);
387 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
389 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
395 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
403 bool MyPrintout::HasPage(int pageNum
)
405 return (pageNum
== 1 || pageNum
== 2);
408 void MyPrintout::DrawPageOne(wxDC
*dc
)
410 /* You might use THIS code if you were scaling
411 * graphics of known size to fit on the page.
415 // We know the graphic is 200x200. If we didn't know this,
416 // we'd need to calculate it.
420 // Let's have at least 50 device units margin
424 // Add the margin to the graphic size
428 // Get the size of the DC in pixels
431 // Calculate a suitable scaling factor
432 float scaleX
=(float)(w
/maxX
);
433 float scaleY
=(float)(h
/maxY
);
435 // Use x or y scaling factor, whichever fits on the DC
436 float actualScale
= wxMin(scaleX
,scaleY
);
438 // Calculate the position on the DC for centring the graphic
439 float posX
= (float)((w
- (200*actualScale
))/2.0);
440 float posY
= (float)((h
- (200*actualScale
))/2.0);
442 // Set the scale and origin
443 dc
->SetUserScale(actualScale
, actualScale
);
444 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
449 void MyPrintout::DrawPageTwo(wxDC
*dc
)
451 /* You might use THIS code to set the printer DC to ROUGHLY reflect
452 * the screen text size. This page also draws lines of actual length 5cm
455 // Get the logical pixels per inch of screen and printer
456 int ppiScreenX
, ppiScreenY
;
457 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
458 int ppiPrinterX
, ppiPrinterY
;
459 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
461 // This scales the DC so that the printout roughly represents the
462 // the screen scaling. The text point size _should_ be the right size
463 // but in fact is too small for some reason. This is a detail that will
464 // need to be addressed at some point but can be fudged for the
466 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
468 // Now we have to check in case our real page size is reduced
469 // (e.g. because we're drawing to a print preview memory DC)
470 int pageWidth
, pageHeight
;
473 GetPageSizePixels(&pageWidth
, &pageHeight
);
475 // If printer pageWidth == current DC width, then this doesn't
476 // change. But w might be the preview bitmap width, so scale down.
477 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
478 dc
->SetUserScale(overallScale
, overallScale
);
480 // Calculate conversion factor for converting millimetres into
482 // There are approx. 25.1 mm to the inch. There are ppi
483 // device units to the inch. Therefore 1 mm corresponds to
484 // ppi/25.1 device units. We also divide by the
485 // screen-to-printer scaling factor, because we need to
486 // unscale to pass logical units to DrawLine.
488 // Draw 50 mm by 50 mm L shape
489 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.1));
490 float logUnits
= (float)(50*logUnitsFactor
);
491 dc
->SetPen(* wxBLACK_PEN
);
492 dc
->DrawLine(50, 250, (long)(50.0 + logUnits
), 250);
493 dc
->DrawLine(50, 250, 50, (long)(250.0 + logUnits
));
497 dc
->SetFont(* wxGetApp().m_testFont
);
498 dc
->SetBackgroundMode(wxTRANSPARENT
);
501 { // GetTextExtent demo:
502 wxString words
[7] = {"This ", "is ", "GetTextExtent ", "testing ", "string. ", "Enjoy ", "it!"};
504 long x
= 200, y
= 250;
505 wxFont
fnt(15, wxSWISS
, wxNORMAL
, wxNORMAL
);
508 for (int i
= 0; i
< 7; i
++) {
509 dc
->GetTextExtent(words
[i
], &w
, &h
);
510 dc
->DrawRectangle(x
, y
, w
, h
);
511 dc
->DrawText(words
[i
], x
, y
);
514 dc
->SetFont(* wxGetApp().m_testFont
);
517 dc
->DrawText("Some test text", 200, 300 );
522 int rightMargin
= 20;
524 int bottomMargin
= 20;
526 int pageWidthMM
, pageHeightMM
;
527 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
529 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
530 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
531 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
532 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
534 dc
->SetPen(* wxRED_PEN
);
535 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
536 (long)rightMarginLogical
, (long)topMarginLogical
);
537 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
538 (long)rightMarginLogical
, (long)bottomMarginLogical
);
540 WritePageHeader(this, dc
, "A header", logUnitsFactor
);
543 // Writes a header on a page. Margin units are in millimetres.
544 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
)
547 static wxFont *headerFont = (wxFont *) NULL;
550 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
552 dc->SetFont(headerFont);
555 int pageWidthMM
, pageHeightMM
;
557 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
561 int rightMargin
= 10;
563 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
564 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
565 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
567 long xExtent
, yExtent
;
568 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
569 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
570 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
572 dc
->SetPen(* wxBLACK_PEN
);
573 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
574 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);