1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Printing demo for wxWidgets
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #if !wxUSE_PRINTING_ARCHITECTURE
24 #error "You must set wxUSE_PRINTING_ARCHITECTURE to 1 in setup.h, and recompile the library."
27 // Set this to 1 if you want to test PostScript printing under MSW.
28 // However, you'll also need to edit src/msw/makefile.nt.
29 #define wxTEST_POSTSCRIPT_IN_MSW 0
32 #include "wx/metafile.h"
34 #include "wx/printdlg.h"
38 #if wxTEST_POSTSCRIPT_IN_MSW
39 #include "wx/generic/printps.h"
40 #include "wx/generic/prntdlgg.h"
46 #include "mondrian.xpm"
49 #if wxUSE_LIBGNOMEPRINT
50 #include "wx/html/forcelnk.h"
51 FORCE_LINK(gnome_print
)
56 MyFrame
*frame
= (MyFrame
*) NULL
;
57 // int orientation = wxPORTRAIT;
59 // Global print data, to remember settings during the session
60 wxPrintData
*g_printData
= (wxPrintData
*) NULL
;
62 // Global page setup data
63 wxPageSetupData
* g_pageSetupData
= (wxPageSetupData
*) NULL
;
68 // Writes a header on a page. Margin units are in millimetres.
69 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, const wxChar
*text
, float mmToLogical
);
71 // The `main program' equivalent, creating the windows and returning the
74 bool MyApp::OnInit(void)
76 wxInitAllImageHandlers();
78 m_testFont
.Create(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
80 g_printData
= new wxPrintData
;
81 g_pageSetupData
= new wxPageSetupDialogData
;
83 // Create the main frame window
84 frame
= new MyFrame((wxFrame
*) NULL
, _T("wxWidgets Printing Demo"), wxPoint(0, 0), wxSize(400, 400));
87 // Give it a status line
88 frame
->CreateStatusBar(2);
89 #endif // wxUSE_STATUSBAR
91 // Load icon and bitmap
92 frame
->SetIcon( wxICON( mondrian
) );
95 wxMenu
*file_menu
= new wxMenu
;
97 file_menu
->Append(WXPRINT_PRINT
, _T("&Print..."), _T("Print"));
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_PAGE_SETUP_PS
, _T("Page Setup PostScript..."), _T("Page setup (PostScript)"));
113 file_menu
->Append(WXPRINT_PREVIEW_PS
, _T("Print Preview PostScript"), _T("Preview (PostScript)"));
115 file_menu
->AppendSeparator();
116 file_menu
->Append(WXPRINT_ANGLEUP
, _T("Angle up\tAlt-U"), _T("Raise rotated text angle"));
117 file_menu
->Append(WXPRINT_ANGLEDOWN
, _T("Angle down\tAlt-D"), _T("Lower rotated text angle"));
118 file_menu
->AppendSeparator();
119 file_menu
->Append(WXPRINT_QUIT
, _T("E&xit"), _T("Exit program"));
121 wxMenu
*help_menu
= new wxMenu
;
122 help_menu
->Append(WXPRINT_ABOUT
, _T("&About"), _T("About this demo"));
124 wxMenuBar
*menu_bar
= new wxMenuBar
;
126 menu_bar
->Append(file_menu
, _T("&File"));
127 menu_bar
->Append(help_menu
, _T("&Help"));
129 // Associate the menu bar with the frame
130 frame
->SetMenuBar(menu_bar
);
132 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100), wxRETAINED
|wxHSCROLL
|wxVSCROLL
);
134 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
135 canvas
->SetScrollbars(20, 20, 50, 50);
137 frame
->canvas
= canvas
;
139 frame
->Centre(wxBOTH
);
143 frame
->SetStatusText(_T("Printing demo"));
144 #endif // wxUSE_STATUSBAR
154 delete g_pageSetupData
;
158 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
159 EVT_MENU(WXPRINT_QUIT
, MyFrame::OnExit
)
160 EVT_MENU(WXPRINT_PRINT
, MyFrame::OnPrint
)
161 EVT_MENU(WXPRINT_PREVIEW
, MyFrame::OnPrintPreview
)
162 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
163 EVT_MENU(WXPRINT_ABOUT
, MyFrame::OnPrintAbout
)
164 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
165 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame::OnPrintPS
)
166 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
167 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
169 EVT_MENU(WXPRINT_ANGLEUP
, MyFrame::OnAngleUp
)
170 EVT_MENU(WXPRINT_ANGLEDOWN
, MyFrame::OnAngleDown
)
173 // Define my frame constructor
174 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
175 wxFrame(frame
, wxID_ANY
, title
, pos
, size
)
180 wxImage
image( wxT("test.jpg") );
183 for (i
= 0; i
< image
.GetWidth(); i
++)
184 for (j
= 0; j
< image
.GetHeight(); j
++)
185 image
.SetAlpha( i
, j
, 50 );
190 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
192 Close(true /*force closing*/);
195 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
197 wxPrintDialogData
printDialogData(* g_printData
);
199 wxPrinter
printer(& printDialogData
);
200 MyPrintout
printout(_T("My printout"));
201 if (!printer
.Print(this, &printout
, true /*prompt*/))
203 if (wxPrinter::GetLastError() == wxPRINTER_ERROR
)
204 wxMessageBox(_T("There was a problem printing.\nPerhaps your current printer is not set correctly?"), _T("Printing"), wxOK
);
206 wxMessageBox(_T("You canceled printing"), _T("Printing"), wxOK
);
210 (*g_printData
) = printer
.GetPrintDialogData().GetPrintData();
214 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
216 // Pass two printout objects: for preview, and possible printing.
217 wxPrintDialogData
printDialogData(* g_printData
);
218 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printDialogData
);
222 wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK
);
226 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
227 frame
->Centre(wxBOTH
);
232 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
234 (*g_pageSetupData
) = *g_printData
;
236 wxPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
237 pageSetupDialog
.ShowModal();
239 (*g_printData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
240 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupData();
243 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
244 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
246 wxPostScriptPrinter
printer(g_printData
);
247 MyPrintout
printout(_T("My printout"));
248 printer
.Print(this, &printout
, true/*prompt*/);
250 (*g_printData
) = printer
.GetPrintData();
253 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
255 // Pass two printout objects: for preview, and possible printing.
256 wxPrintDialogData
printDialogData(* g_printData
);
257 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printDialogData
);
258 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
259 frame
->Centre(wxBOTH
);
264 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
266 (*g_pageSetupData
) = * g_printData
;
268 wxGenericPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
269 pageSetupDialog
.ShowModal();
271 (*g_printData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
272 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupData();
277 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
279 (void)wxMessageBox(_T("wxWidgets printing demo\nAuthor: Julian Smart"),
280 _T("About wxWidgets printing demo"), wxOK
|wxCENTRE
);
283 void MyFrame::OnAngleUp(wxCommandEvent
& WXUNUSED(event
))
289 void MyFrame::OnAngleDown(wxCommandEvent
& WXUNUSED(event
))
295 void MyFrame::Draw(wxDC
& dc
)
297 dc
.SetBackground(*wxWHITE_BRUSH
);
299 dc
.SetFont(wxGetApp().m_testFont
);
301 dc
.SetBackgroundMode(wxTRANSPARENT
);
303 dc
.SetBrush(*wxCYAN_BRUSH
);
304 dc
.SetPen(*wxRED_PEN
);
306 dc
.DrawRoundedRectangle(0, 20, 200, 80, 20);
308 dc
.DrawText( wxT("Rectangle 200 by 80"), 40, 40);
310 dc
.SetPen( wxPen(*wxBLACK
,0,wxDOT_DASH
) );
311 dc
.DrawEllipse(50, 140, 100, 50);
312 dc
.SetPen(*wxRED_PEN
);
314 dc
.DrawText( wxT("Test message: this is in 10 point text"), 10, 180);
318 char *test
= "Hebrew שלום -- Japanese (日本語)";
319 wxString tmp
= wxConvUTF8
.cMB2WC( test
);
320 dc
.DrawText( tmp
, 10, 200 );
334 dc
.DrawPolygon( 5, points
, 20, 250, wxODDEVEN_RULE
);
335 dc
.DrawPolygon( 5, points
, 50, 250, wxWINDING_RULE
);
337 dc
.DrawEllipticArc( 80, 250, 60, 30, 0.0, 270.0 );
347 dc
.DrawSpline( 4, points
);
349 dc
.DrawArc( 20,10, 10,10, 25,40 );
353 str
.Printf( wxT("---- Text at angle %d ----"), i
);
354 dc
.DrawRotatedText( str
, 100, 300, i
);
357 str
.Printf( wxT("---- Text at angle %d ----"), i
);
358 dc
.DrawRotatedText( str
, 100, 300, i
);
360 dc
.SetPen(* wxBLACK_PEN
);
361 dc
.DrawLine(0, 0, 200, 200);
362 dc
.DrawLine(200, 0, 0, 200);
364 wxIcon my_icon
= wxICON(mondrian
) ;
366 dc
.DrawIcon( my_icon
, 100, 100);
369 dc
.DrawBitmap( m_bitmap
, 10, 10 );
372 void MyFrame::OnSize(wxSizeEvent
& event
)
374 wxFrame::OnSize(event
);
377 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
378 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
381 // Define a constructor for my canvas
382 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
383 wxScrolledWindow(frame
, wxID_ANY
, pos
, size
, style
)
385 SetBackgroundColour(* wxWHITE
);
388 // Define the repainting behaviour
389 void MyCanvas::OnDraw(wxDC
& dc
)
394 void MyCanvas::OnEvent(wxMouseEvent
& WXUNUSED(event
))
398 bool MyPrintout::OnPrintPage(int page
)
408 dc
->SetDeviceOrigin(0, 0);
409 dc
->SetUserScale(1.0, 1.0);
412 wxSprintf(buf
, wxT("PAGE %d"), page
);
413 dc
->DrawText(buf
, 10, 10);
421 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
423 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
429 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
437 bool MyPrintout::HasPage(int pageNum
)
439 return (pageNum
== 1 || pageNum
== 2);
442 void MyPrintout::DrawPageOne(wxDC
*dc
)
444 // You might use THIS code if you were scaling
445 // graphics of known size to fit on the page.
447 // We know the graphic is 200x200. If we didn't know this,
448 // we'd need to calculate it.
452 // Let's have at least 50 device units margin
456 // Add the margin to the graphic size
460 // Get the size of the DC in pixels
464 // Calculate a suitable scaling factor
465 float scaleX
=(float)(w
/maxX
);
466 float scaleY
=(float)(h
/maxY
);
468 // Use x or y scaling factor, whichever fits on the DC
469 float actualScale
= wxMin(scaleX
,scaleY
);
471 // Calculate the position on the DC for centring the graphic
472 float posX
= (float)((w
- (200*actualScale
))/2.0);
473 float posY
= (float)((h
- (200*actualScale
))/2.0);
475 // Set the scale and origin
476 dc
->SetUserScale(actualScale
, actualScale
);
477 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
482 void MyPrintout::DrawPageTwo(wxDC
*dc
)
484 // You might use THIS code to set the printer DC to ROUGHLY reflect
485 // the screen text size. This page also draws lines of actual length
488 // Get the logical pixels per inch of screen and printer
489 int ppiScreenX
, ppiScreenY
;
490 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
491 int ppiPrinterX
, ppiPrinterY
;
492 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
494 // This scales the DC so that the printout roughly represents the
495 // the screen scaling. The text point size _should_ be the right size
496 // but in fact is too small for some reason. This is a detail that will
497 // need to be addressed at some point but can be fudged for the
499 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
501 // Now we have to check in case our real page size is reduced
502 // (e.g. because we're drawing to a print preview memory DC)
503 int pageWidth
, pageHeight
;
506 GetPageSizePixels(&pageWidth
, &pageHeight
);
508 // If printer pageWidth == current DC width, then this doesn't
509 // change. But w might be the preview bitmap width, so scale down.
510 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
511 dc
->SetUserScale(overallScale
, overallScale
);
513 // Calculate conversion factor for converting millimetres into
515 // There are approx. 25.4 mm to the inch. There are ppi
516 // device units to the inch. Therefore 1 mm corresponds to
517 // ppi/25.4 device units. We also divide by the
518 // screen-to-printer scaling factor, because we need to
519 // unscale to pass logical units to DrawLine.
521 // Draw 50 mm by 50 mm L shape
522 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.4));
523 float logUnits
= (float)(50*logUnitsFactor
);
524 dc
->SetPen(* wxBLACK_PEN
);
525 dc
->DrawLine(50, 250, (long)(50.0 + logUnits
), 250);
526 dc
->DrawLine(50, 250, 50, (long)(250.0 + logUnits
));
528 dc
->SetBackgroundMode(wxTRANSPARENT
);
529 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
531 { // GetTextExtent demo:
532 wxString words
[7] = {_T("This "), _T("is "), _T("GetTextExtent "), _T("testing "), _T("string. "), _T("Enjoy "), _T("it!")};
534 long x
= 200, y
= 250;
535 wxFont
fnt(15, wxSWISS
, wxNORMAL
, wxNORMAL
);
539 for (int i
= 0; i
< 7; i
++)
541 wxString word
= words
[i
];
542 word
.Remove( word
.Len()-1, 1 );
543 dc
->GetTextExtent(word
, &w
, &h
);
544 dc
->DrawRectangle(x
, y
, w
, h
);
545 dc
->GetTextExtent(words
[i
], &w
, &h
);
546 dc
->DrawText(words
[i
], x
, y
);
552 dc
->SetFont(wxGetApp().m_testFont
);
554 dc
->DrawText(_T("Some test text"), 200, 300 );
559 int rightMargin
= 20;
561 int bottomMargin
= 20;
563 int pageWidthMM
, pageHeightMM
;
564 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
566 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
567 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
568 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
569 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
571 dc
->SetPen(* wxRED_PEN
);
572 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
573 (long)rightMarginLogical
, (long)topMarginLogical
);
574 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
575 (long)rightMarginLogical
, (long)bottomMarginLogical
);
577 WritePageHeader(this, dc
, _T("A header"), logUnitsFactor
);
580 // Writes a header on a page. Margin units are in millimetres.
581 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, const wxChar
*text
, float mmToLogical
)
584 static wxFont *headerFont = (wxFont *) NULL;
587 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
589 dc->SetFont(headerFont);
592 int pageWidthMM
, pageHeightMM
;
594 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
595 wxUnusedVar(pageHeightMM
);
599 int rightMargin
= 10;
601 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
602 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
603 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
605 long xExtent
, yExtent
;
606 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
607 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
608 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
610 dc
->SetPen(* wxBLACK_PEN
);
611 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
612 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);