1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Printing demo for wxWidgets
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, and recompile the library."
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
, wxChar
*text
, float mmToLogical
);
73 // The `main program' equivalent, creating the windows and returning the
75 bool MyApp::OnInit(void)
77 m_testFont
.Create(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
79 g_printData
= new wxPrintData
;
80 g_pageSetupData
= new wxPageSetupDialogData
;
82 // Create the main frame window
83 frame
= new MyFrame((wxFrame
*) NULL
, _T("wxWidgets Printing Demo"), wxPoint(0, 0), wxSize(400, 400));
86 // Give it a status line
87 frame
->CreateStatusBar(2);
88 #endif // wxUSE_STATUSBAR
90 // Load icon and bitmap
91 frame
->SetIcon( wxICON( mondrian
) );
94 wxMenu
*file_menu
= new wxMenu
;
96 file_menu
->Append(WXPRINT_PRINT
, _T("&Print..."), _T("Print"));
97 file_menu
->Append(WXPRINT_PRINT_SETUP
, _T("Print &Setup..."), _T("Setup printer properties"));
98 file_menu
->Append(WXPRINT_PAGE_SETUP
, _T("Page Set&up..."), _T("Page setup"));
99 file_menu
->Append(WXPRINT_PREVIEW
, _T("Print Pre&view"), _T("Preview"));
103 wxAcceleratorEntry entries
[1];
104 entries
[0].Set(wxACCEL_CTRL
, (int) 'V', WXPRINT_PREVIEW
);
105 wxAcceleratorTable
accel(1, entries
);
106 frame
->SetAcceleratorTable(accel
);
109 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
110 file_menu
->AppendSeparator();
111 file_menu
->Append(WXPRINT_PRINT_PS
, _T("Print PostScript..."), _T("Print (PostScript)"));
112 file_menu
->Append(WXPRINT_PRINT_SETUP_PS
, _T("Print Setup PostScript..."), _T("Setup printer properties (PostScript)"));
113 file_menu
->Append(WXPRINT_PAGE_SETUP_PS
, _T("Page Setup PostScript..."), _T("Page setup (PostScript)"));
114 file_menu
->Append(WXPRINT_PREVIEW_PS
, _T("Print Preview PostScript"), _T("Preview (PostScript)"));
116 file_menu
->AppendSeparator();
117 file_menu
->Append(WXPRINT_QUIT
, _T("E&xit"), _T("Exit program"));
119 wxMenu
*help_menu
= new wxMenu
;
120 help_menu
->Append(WXPRINT_ABOUT
, _T("&About"), _T("About this demo"));
122 wxMenuBar
*menu_bar
= new wxMenuBar
;
124 menu_bar
->Append(file_menu
, _T("&File"));
125 menu_bar
->Append(help_menu
, _T("&Help"));
127 // Associate the menu bar with the frame
128 frame
->SetMenuBar(menu_bar
);
130 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100), wxRETAINED
|wxHSCROLL
|wxVSCROLL
);
132 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
133 canvas
->SetScrollbars(20, 20, 50, 50);
135 frame
->canvas
= canvas
;
137 frame
->Centre(wxBOTH
);
141 frame
->SetStatusText(_T("Printing demo"));
142 #endif // wxUSE_STATUSBAR
152 delete g_pageSetupData
;
156 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
157 EVT_MENU(WXPRINT_QUIT
, MyFrame::OnExit
)
158 EVT_MENU(WXPRINT_PRINT
, MyFrame::OnPrint
)
159 EVT_MENU(WXPRINT_PREVIEW
, MyFrame::OnPrintPreview
)
160 EVT_MENU(WXPRINT_PRINT_SETUP
, MyFrame::OnPrintSetup
)
161 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
162 EVT_MENU(WXPRINT_ABOUT
, MyFrame::OnPrintAbout
)
163 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
164 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame::OnPrintPS
)
165 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
166 EVT_MENU(WXPRINT_PRINT_SETUP_PS
, MyFrame::OnPrintSetupPS
)
167 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
171 // Define my frame constructor
172 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
173 wxFrame(frame
, wxID_ANY
, title
, pos
, size
)
178 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
180 Close(true /*force closing*/);
183 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
185 wxPrintDialogData
printDialogData(* g_printData
);
187 wxPrinter
printer(& printDialogData
);
188 MyPrintout
printout(_T("My printout"));
189 if (!printer
.Print(this, &printout
, true /*prompt*/))
191 if (wxPrinter::GetLastError() == wxPRINTER_ERROR
)
192 wxMessageBox(_T("There was a problem printing.\nPerhaps your current printer is not set correctly?"), _T("Printing"), wxOK
);
194 wxMessageBox(_T("You canceled printing"), _T("Printing"), wxOK
);
198 (*g_printData
) = printer
.GetPrintDialogData().GetPrintData();
202 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
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
);
210 wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK
);
214 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
215 frame
->Centre(wxBOTH
);
220 void MyFrame::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
222 wxPrintDialogData
printDialogData(* g_printData
);
223 wxPrintDialog
printerDialog(this, & printDialogData
);
225 printerDialog
.GetPrintDialogData().SetSetupDialog(true /*show print setup dialog*/);
226 printerDialog
.ShowModal();
228 (*g_printData
) = printerDialog
.GetPrintDialogData().GetPrintData();
231 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
233 (*g_pageSetupData
) = * g_printData
;
235 wxPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
236 pageSetupDialog
.ShowModal();
238 (*g_printData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
239 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupData();
242 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
243 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
245 wxPostScriptPrinter
printer(g_printData
);
246 MyPrintout
printout(_T("My printout"));
247 printer
.Print(this, &printout
, true/*prompt*/);
249 (*g_printData
) = printer
.GetPrintData();
252 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
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, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
258 frame
->Centre(wxBOTH
);
263 void MyFrame::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
265 wxPrintDialogData
printDialogData(* g_printData
);
266 wxGenericPrintDialog
printerDialog(this, & printDialogData
);
268 printerDialog
.GetPrintDialogData().SetSetupDialog(true /*show print setup dialog*/);
269 printerDialog
.ShowModal();
271 (*g_printData
) = printerDialog
.GetPrintDialogData().GetPrintData();
274 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
276 (*g_pageSetupData
) = * g_printData
;
278 wxGenericPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
279 pageSetupDialog
.ShowModal();
281 (*g_printData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
282 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupData();
287 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
289 (void)wxMessageBox(_T("wxWidgets printing demo\nAuthor: Julian Smart"),
290 _T("About wxWidgets printing demo"), wxOK
|wxCENTRE
);
293 void MyFrame::Draw(wxDC
& dc
)
295 dc
.SetBackground(*wxWHITE_BRUSH
);
297 dc
.SetFont(wxGetApp().m_testFont
);
299 dc
.SetBackgroundMode(wxTRANSPARENT
);
301 dc
.SetBrush(* wxCYAN_BRUSH
);
302 dc
.SetPen(* wxRED_PEN
);
304 dc
.DrawRectangle(0, 30, 200, 100);
306 dc
.DrawText( wxT("Rectangle 200 by 100"), 40, 40);
308 dc
.DrawEllipse(50, 140, 100, 50);
310 dc
.DrawText( wxT("Test message: this is in 10 point text"), 10, 180);
313 char *test
= "Hebrew שלום -- Japanese (日本語)";
314 wxString tmp
= wxConvUTF8
.cMB2WC( test
);
315 dc
.DrawText( tmp
, 10, 200 );
318 dc
.SetPen(* wxBLACK_PEN
);
319 dc
.DrawLine(0, 0, 200, 200);
320 dc
.DrawLine(200, 0, 0, 200);
322 wxIcon my_icon
= wxICON(mondrian
) ;
324 dc
.DrawIcon( my_icon
, 100, 100);
327 void MyFrame::OnSize(wxSizeEvent
& event
)
329 wxFrame::OnSize(event
);
332 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
333 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
336 // Define a constructor for my canvas
337 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
338 wxScrolledWindow(frame
, wxID_ANY
, pos
, size
, style
)
340 SetBackgroundColour(* wxWHITE
);
343 MyCanvas::~MyCanvas(void)
347 // Define the repainting behaviour
348 void MyCanvas::OnDraw(wxDC
& dc
)
353 void MyCanvas::OnEvent(wxMouseEvent
& WXUNUSED(event
))
357 bool MyPrintout::OnPrintPage(int page
)
367 dc
->SetDeviceOrigin(0, 0);
368 dc
->SetUserScale(1.0, 1.0);
371 wxSprintf(buf
, wxT("PAGE %d"), page
);
372 dc
->DrawText(buf
, 10, 10);
380 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
382 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
388 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
396 bool MyPrintout::HasPage(int pageNum
)
398 return (pageNum
== 1 || pageNum
== 2);
401 void MyPrintout::DrawPageOne(wxDC
*dc
)
403 // You might use THIS code if you were scaling
404 // graphics of known size to fit on the page.
406 // We know the graphic is 200x200. If we didn't know this,
407 // we'd need to calculate it.
411 // Let's have at least 50 device units margin
415 // Add the margin to the graphic size
419 // Get the size of the DC in pixels
423 // Calculate a suitable scaling factor
424 float scaleX
=(float)(w
/maxX
);
425 float scaleY
=(float)(h
/maxY
);
427 // Use x or y scaling factor, whichever fits on the DC
428 float actualScale
= wxMin(scaleX
,scaleY
);
430 // Calculate the position on the DC for centring the graphic
431 float posX
= (float)((w
- (200*actualScale
))/2.0);
432 float posY
= (float)((h
- (200*actualScale
))/2.0);
434 // Set the scale and origin
435 dc
->SetUserScale(actualScale
, actualScale
);
436 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
441 void MyPrintout::DrawPageTwo(wxDC
*dc
)
443 // You might use THIS code to set the printer DC to ROUGHLY reflect
444 // the screen text size. This page also draws lines of actual length
447 // Get the logical pixels per inch of screen and printer
448 int ppiScreenX
, ppiScreenY
;
449 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
450 int ppiPrinterX
, ppiPrinterY
;
451 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
453 // This scales the DC so that the printout roughly represents the
454 // the screen scaling. The text point size _should_ be the right size
455 // but in fact is too small for some reason. This is a detail that will
456 // need to be addressed at some point but can be fudged for the
458 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
460 // Now we have to check in case our real page size is reduced
461 // (e.g. because we're drawing to a print preview memory DC)
462 int pageWidth
, pageHeight
;
465 GetPageSizePixels(&pageWidth
, &pageHeight
);
467 // If printer pageWidth == current DC width, then this doesn't
468 // change. But w might be the preview bitmap width, so scale down.
469 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
470 dc
->SetUserScale(overallScale
, overallScale
);
472 // Calculate conversion factor for converting millimetres into
474 // There are approx. 25.1 mm to the inch. There are ppi
475 // device units to the inch. Therefore 1 mm corresponds to
476 // ppi/25.1 device units. We also divide by the
477 // screen-to-printer scaling factor, because we need to
478 // unscale to pass logical units to DrawLine.
480 // Draw 50 mm by 50 mm L shape
481 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.1));
482 float logUnits
= (float)(50*logUnitsFactor
);
483 dc
->SetPen(* wxBLACK_PEN
);
484 dc
->DrawLine(50, 250, (long)(50.0 + logUnits
), 250);
485 dc
->DrawLine(50, 250, 50, (long)(250.0 + logUnits
));
487 dc
->SetBackgroundMode(wxTRANSPARENT
);
490 { // GetTextExtent demo:
491 wxString words
[7] = {_T("This "), _T("is "), _T("GetTextExtent "), _T("testing "), _T("string. "), _T("Enjoy "), _T("it!")};
493 long x
= 200, y
= 250;
494 wxFont
fnt(15, wxSWISS
, wxNORMAL
, wxNORMAL
);
498 for (int i
= 0; i
< 7; i
++)
500 wxString word
= words
[i
];
501 word
.Remove( word
.Len()-1, 1 );
502 dc
->GetTextExtent(word
, &w
, &h
);
503 dc
->DrawRectangle(x
, y
, w
, h
);
504 dc
->GetTextExtent(words
[i
], &w
, &h
);
505 dc
->DrawText(words
[i
], x
, y
);
511 dc
->SetFont(wxGetApp().m_testFont
);
513 dc
->DrawText(_T("Some test text"), 200, 300 );
518 int rightMargin
= 20;
520 int bottomMargin
= 20;
522 int pageWidthMM
, pageHeightMM
;
523 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
525 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
526 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
527 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
528 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
530 dc
->SetPen(* wxRED_PEN
);
531 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
532 (long)rightMarginLogical
, (long)topMarginLogical
);
533 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
534 (long)rightMarginLogical
, (long)bottomMarginLogical
);
536 WritePageHeader(this, dc
, _T("A header"), logUnitsFactor
);
539 // Writes a header on a page. Margin units are in millimetres.
540 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, wxChar
*text
, float mmToLogical
)
543 static wxFont *headerFont = (wxFont *) NULL;
546 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
548 dc->SetFont(headerFont);
551 int pageWidthMM
, pageHeightMM
;
553 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
554 wxUnusedVar(pageHeightMM
);
558 int rightMargin
= 10;
560 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
561 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
562 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
564 long xExtent
, yExtent
;
565 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
566 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
567 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
569 dc
->SetPen(* wxBLACK_PEN
);
570 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
571 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);