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
, wxChar
*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 // Create the main frame window
83 frame
= new MyFrame((wxFrame
*) NULL
, _T("wxWindows Printing Demo"), wxPoint(0, 0), wxSize(400, 400));
85 // Give it a status line
86 frame
->CreateStatusBar(2);
88 // Load icon and bitmap
89 frame
->SetIcon( wxICON( mondrian
) );
92 wxMenu
*file_menu
= new wxMenu
;
94 file_menu
->Append(WXPRINT_PRINT
, _T("&Print..."), _T("Print"));
95 file_menu
->Append(WXPRINT_PRINT_SETUP
, _T("Print &Setup..."), _T("Setup printer properties"));
96 file_menu
->Append(WXPRINT_PAGE_SETUP
, _T("Page Set&up..."), _T("Page setup"));
97 file_menu
->Append(WXPRINT_PREVIEW
, _T("Print Pre&view"), _T("Preview"));
101 wxAcceleratorEntry entries
[1];
102 entries
[0].Set(wxACCEL_CTRL
, (int) 'V', WXPRINT_PREVIEW
);
103 wxAcceleratorTable
accel(1, entries
);
104 frame
->SetAcceleratorTable(accel
);
107 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
108 file_menu
->AppendSeparator();
109 file_menu
->Append(WXPRINT_PRINT_PS
, _T("Print PostScript..."), _T("Print (PostScript)"));
110 file_menu
->Append(WXPRINT_PRINT_SETUP_PS
, _T("Print Setup PostScript..."), _T("Setup printer properties (PostScript)"));
111 file_menu
->Append(WXPRINT_PAGE_SETUP_PS
, _T("Page Setup PostScript..."), _T("Page setup (PostScript)"));
112 file_menu
->Append(WXPRINT_PREVIEW_PS
, _T("Print Preview PostScript"), _T("Preview (PostScript)"));
114 file_menu
->AppendSeparator();
115 file_menu
->Append(WXPRINT_QUIT
, _T("E&xit"), _T("Exit program"));
117 wxMenu
*help_menu
= new wxMenu
;
118 help_menu
->Append(WXPRINT_ABOUT
, _T("&About"), _T("About this demo"));
120 wxMenuBar
*menu_bar
= new wxMenuBar
;
122 menu_bar
->Append(file_menu
, _T("&File"));
123 menu_bar
->Append(help_menu
, _T("&Help"));
125 // Associate the menu bar with the frame
126 frame
->SetMenuBar(menu_bar
);
128 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100), wxRETAINED
|wxHSCROLL
|wxVSCROLL
);
130 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
131 canvas
->SetScrollbars(20, 20, 50, 50);
133 frame
->canvas
= canvas
;
135 frame
->Centre(wxBOTH
);
138 frame
->SetStatusText(_T("Printing demo"));
147 delete wxGetApp().m_testFont
;
149 delete g_pageSetupData
;
153 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
154 EVT_MENU(WXPRINT_QUIT
, MyFrame::OnExit
)
155 EVT_MENU(WXPRINT_PRINT
, MyFrame::OnPrint
)
156 EVT_MENU(WXPRINT_PREVIEW
, MyFrame::OnPrintPreview
)
157 EVT_MENU(WXPRINT_PRINT_SETUP
, MyFrame::OnPrintSetup
)
158 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
159 EVT_MENU(WXPRINT_ABOUT
, MyFrame::OnPrintAbout
)
160 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
161 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame::OnPrintPS
)
162 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
163 EVT_MENU(WXPRINT_PRINT_SETUP_PS
, MyFrame::OnPrintSetupPS
)
164 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
168 // Define my frame constructor
169 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
170 wxFrame(frame
, -1, title
, pos
, size
)
172 canvas
= (MyCanvas
*) NULL
;
175 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
180 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
182 wxPrintDialogData
printDialogData(* g_printData
);
184 wxPrinter
printer(& printDialogData
);
185 MyPrintout
printout(_T("My printout"));
186 if (!printer
.Print(this, &printout
, TRUE
))
188 if (wxPrinter::GetLastError() == wxPRINTER_ERROR
)
189 wxMessageBox(_T("There was a problem printing.\nPerhaps your current printer is not set correctly?"), _T("Printing"), wxOK
);
191 wxMessageBox(_T("You canceled printing"), _T("Printing"), wxOK
);
195 (*g_printData
) = printer
.GetPrintDialogData().GetPrintData();
199 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
201 // Pass two printout objects: for preview, and possible printing.
202 wxPrintDialogData
printDialogData(* g_printData
);
203 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printDialogData
);
207 wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK
);
211 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
212 frame
->Centre(wxBOTH
);
217 void MyFrame::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
219 wxPrintDialogData
printDialogData(* g_printData
);
220 wxPrintDialog
printerDialog(this, & printDialogData
);
222 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
223 printerDialog
.ShowModal();
225 (*g_printData
) = printerDialog
.GetPrintDialogData().GetPrintData();
228 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
230 (*g_pageSetupData
) = * g_printData
;
232 wxPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
233 pageSetupDialog
.ShowModal();
235 (*g_printData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
236 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupData();
239 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
240 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
242 wxPostScriptPrinter
printer(g_printData
);
243 MyPrintout
printout(_T("My printout"));
244 printer
.Print(this, &printout
, TRUE
);
246 (*g_printData
) = printer
.GetPrintData();
249 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
251 // Pass two printout objects: for preview, and possible printing.
252 wxPrintDialogData
printDialogData(* g_printData
);
253 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printDialogData
);
254 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
255 frame
->Centre(wxBOTH
);
260 void MyFrame::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
262 wxPrintDialogData
printDialogData(* g_printData
);
263 wxGenericPrintDialog
printerDialog(this, & printDialogData
);
265 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
266 printerDialog
.ShowModal();
268 (*g_printData
) = printerDialog
.GetPrintDialogData().GetPrintData();
271 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
273 (*g_pageSetupData
) = * g_printData
;
275 wxGenericPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
276 pageSetupDialog
.ShowModal();
278 (*g_printData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
279 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupData();
284 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
286 (void)wxMessageBox(_T("wxWindows printing demo\nAuthor: Julian Smart"),
287 _T("About wxWindows printing demo"), wxOK
|wxCENTRE
);
290 void MyFrame::Draw(wxDC
& dc
)
292 dc
.SetBackground(*wxWHITE_BRUSH
);
294 dc
.SetFont(* wxGetApp().m_testFont
);
296 dc
.SetBackgroundMode(wxTRANSPARENT
);
298 dc
.SetBrush(* wxCYAN_BRUSH
);
299 dc
.SetPen(* wxRED_PEN
);
301 dc
.DrawRectangle(0, 30, 200, 100);
303 dc
.DrawText( wxT("Rectangle 200 by 100"), 40, 40);
305 dc
.DrawEllipse(50, 140, 100, 50);
307 dc
.DrawText( wxT("Test message: this is in 10 point text"), 10, 180);
310 char *test
= "Greek (Ελληνικά) Γειά σας -- Hebrew שלום -- Japanese (日本語)";
311 wxString tmp
= wxConvUTF8
.cMB2WC( test
);
312 dc
.DrawText( tmp
, 10, 200 );
315 dc
.SetPen(* wxBLACK_PEN
);
316 dc
.DrawLine(0, 0, 200, 200);
317 dc
.DrawLine(200, 0, 0, 200);
319 wxIcon my_icon
= wxICON(mondrian
) ;
321 dc
.DrawIcon( my_icon
, 100, 100);
324 void MyFrame::OnSize(wxSizeEvent
& event
)
326 wxFrame::OnSize(event
);
329 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
330 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
333 // Define a constructor for my canvas
334 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
335 wxScrolledWindow(frame
, -1, pos
, size
, style
)
337 SetBackgroundColour(* wxWHITE
);
340 MyCanvas::~MyCanvas(void)
344 // Define the repainting behaviour
345 void MyCanvas::OnDraw(wxDC
& dc
)
350 void MyCanvas::OnEvent(wxMouseEvent
& WXUNUSED(event
))
354 bool MyPrintout::OnPrintPage(int page
)
364 dc
->SetDeviceOrigin(0, 0);
365 dc
->SetUserScale(1.0, 1.0);
368 wxSprintf(buf
, wxT("PAGE %d"), page
);
369 // dc->DrawText(buf, 10, 10);
377 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
379 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
385 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
393 bool MyPrintout::HasPage(int pageNum
)
395 return (pageNum
== 1 || pageNum
== 2);
398 void MyPrintout::DrawPageOne(wxDC
*dc
)
400 /* You might use THIS code if you were scaling
401 * graphics of known size to fit on the page.
405 // We know the graphic is 200x200. If we didn't know this,
406 // we'd need to calculate it.
410 // Let's have at least 50 device units margin
414 // Add the margin to the graphic size
418 // Get the size of the DC in pixels
421 // Calculate a suitable scaling factor
422 float scaleX
=(float)(w
/maxX
);
423 float scaleY
=(float)(h
/maxY
);
425 // Use x or y scaling factor, whichever fits on the DC
426 float actualScale
= wxMin(scaleX
,scaleY
);
428 // Calculate the position on the DC for centring the graphic
429 float posX
= (float)((w
- (200*actualScale
))/2.0);
430 float posY
= (float)((h
- (200*actualScale
))/2.0);
432 // Set the scale and origin
433 dc
->SetUserScale(actualScale
, actualScale
);
434 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
435 //dc->SetUserScale(1.0, 1.0);
440 void MyPrintout::DrawPageTwo(wxDC
*dc
)
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
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
);
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
457 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
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
;
464 GetPageSizePixels(&pageWidth
, &pageHeight
);
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
);
471 // Calculate conversion factor for converting millimetres into
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.
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
));
486 dc
->SetFont(* wxGetApp().m_testFont
);
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
);
497 for (int i
= 0; i
< 7; i
++) {
498 dc
->GetTextExtent(words
[i
], &w
, &h
);
499 dc
->DrawRectangle(x
, y
, w
, h
);
500 dc
->DrawText(words
[i
], x
, y
);
503 dc
->SetFont(* wxGetApp().m_testFont
);
506 dc
->DrawText(_T("Some test text"), 200, 300 );
511 int rightMargin
= 20;
513 int bottomMargin
= 20;
515 int pageWidthMM
, pageHeightMM
;
516 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
518 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
519 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
520 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
521 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
523 dc
->SetPen(* wxRED_PEN
);
524 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
525 (long)rightMarginLogical
, (long)topMarginLogical
);
526 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
527 (long)rightMarginLogical
, (long)bottomMarginLogical
);
529 WritePageHeader(this, dc
, _T("A header"), logUnitsFactor
);
532 // Writes a header on a page. Margin units are in millimetres.
533 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, wxChar
*text
, float mmToLogical
)
536 static wxFont *headerFont = (wxFont *) NULL;
539 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
541 dc->SetFont(headerFont);
544 int pageWidthMM
, pageHeightMM
;
546 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
550 int rightMargin
= 10;
552 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
553 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
554 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
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
);
561 dc
->SetPen(* wxBLACK_PEN
);
562 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
563 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);