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"
53 #if wxUSE_LIBGNOMEPRINT
54 #include "wx/html/forcelnk.h"
55 FORCE_LINK(gnome_print
)
60 MyFrame
*frame
= (MyFrame
*) NULL
;
61 // int orientation = wxPORTRAIT;
63 // Global print data, to remember settings during the session
64 wxPrintData
*g_printData
= (wxPrintData
*) NULL
;
66 // Global page setup data
67 wxPageSetupData
* g_pageSetupData
= (wxPageSetupData
*) NULL
;
72 // Writes a header on a page. Margin units are in millimetres.
73 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, wxChar
*text
, float mmToLogical
);
75 // The `main program' equivalent, creating the windows and returning the
78 bool MyApp::OnInit(void)
80 wxInitAllImageHandlers();
82 m_testFont
.Create(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
84 g_printData
= new wxPrintData
;
85 g_pageSetupData
= new wxPageSetupDialogData
;
87 // Create the main frame window
88 frame
= new MyFrame((wxFrame
*) NULL
, _T("wxWidgets Printing Demo"), wxPoint(0, 0), wxSize(400, 400));
91 // Give it a status line
92 frame
->CreateStatusBar(2);
93 #endif // wxUSE_STATUSBAR
95 // Load icon and bitmap
96 frame
->SetIcon( wxICON( mondrian
) );
99 wxMenu
*file_menu
= new wxMenu
;
101 file_menu
->Append(WXPRINT_PRINT
, _T("&Print..."), _T("Print"));
102 file_menu
->Append(WXPRINT_PAGE_SETUP
, _T("Page Set&up..."), _T("Page setup"));
103 file_menu
->Append(WXPRINT_PREVIEW
, _T("Print Pre&view"), _T("Preview"));
107 wxAcceleratorEntry entries
[1];
108 entries
[0].Set(wxACCEL_CTRL
, (int) 'V', WXPRINT_PREVIEW
);
109 wxAcceleratorTable
accel(1, entries
);
110 frame
->SetAcceleratorTable(accel
);
113 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
114 file_menu
->AppendSeparator();
115 file_menu
->Append(WXPRINT_PRINT_PS
, _T("Print PostScript..."), _T("Print (PostScript)"));
116 file_menu
->Append(WXPRINT_PAGE_SETUP_PS
, _T("Page Setup PostScript..."), _T("Page setup (PostScript)"));
117 file_menu
->Append(WXPRINT_PREVIEW_PS
, _T("Print Preview PostScript"), _T("Preview (PostScript)"));
119 file_menu
->AppendSeparator();
120 file_menu
->Append(WXPRINT_ANGLEUP
, _T("Angle up\tAlt-U"), _T("Raise rotated text angle"));
121 file_menu
->Append(WXPRINT_ANGLEDOWN
, _T("Angle down\tAlt-D"), _T("Lower rotated text angle"));
122 file_menu
->AppendSeparator();
123 file_menu
->Append(WXPRINT_QUIT
, _T("E&xit"), _T("Exit program"));
125 wxMenu
*help_menu
= new wxMenu
;
126 help_menu
->Append(WXPRINT_ABOUT
, _T("&About"), _T("About this demo"));
128 wxMenuBar
*menu_bar
= new wxMenuBar
;
130 menu_bar
->Append(file_menu
, _T("&File"));
131 menu_bar
->Append(help_menu
, _T("&Help"));
133 // Associate the menu bar with the frame
134 frame
->SetMenuBar(menu_bar
);
136 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100), wxRETAINED
|wxHSCROLL
|wxVSCROLL
);
138 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
139 canvas
->SetScrollbars(20, 20, 50, 50);
141 frame
->canvas
= canvas
;
143 frame
->Centre(wxBOTH
);
147 frame
->SetStatusText(_T("Printing demo"));
148 #endif // wxUSE_STATUSBAR
158 delete g_pageSetupData
;
162 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
163 EVT_MENU(WXPRINT_QUIT
, MyFrame::OnExit
)
164 EVT_MENU(WXPRINT_PRINT
, MyFrame::OnPrint
)
165 EVT_MENU(WXPRINT_PREVIEW
, MyFrame::OnPrintPreview
)
166 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
167 EVT_MENU(WXPRINT_ABOUT
, MyFrame::OnPrintAbout
)
168 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
169 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame::OnPrintPS
)
170 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
171 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
173 EVT_MENU(WXPRINT_ANGLEUP
, MyFrame::OnAngleUp
)
174 EVT_MENU(WXPRINT_ANGLEDOWN
, MyFrame::OnAngleDown
)
177 // Define my frame constructor
178 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
179 wxFrame(frame
, wxID_ANY
, title
, pos
, size
)
184 wxImage
image( wxT("test.jpg") );
187 for (i
= 0; i
< image
.GetWidth(); i
++)
188 for (j
= 0; j
< image
.GetHeight(); j
++)
189 image
.SetAlpha( i
, j
, 50 );
194 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
196 Close(true /*force closing*/);
199 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
201 wxPrintDialogData
printDialogData(* g_printData
);
203 wxPrinter
printer(& printDialogData
);
204 MyPrintout
printout(_T("My printout"));
205 if (!printer
.Print(this, &printout
, true /*prompt*/))
207 if (wxPrinter::GetLastError() == wxPRINTER_ERROR
)
208 wxMessageBox(_T("There was a problem printing.\nPerhaps your current printer is not set correctly?"), _T("Printing"), wxOK
);
210 wxMessageBox(_T("You canceled printing"), _T("Printing"), wxOK
);
214 (*g_printData
) = printer
.GetPrintDialogData().GetPrintData();
218 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
220 // Pass two printout objects: for preview, and possible printing.
221 wxPrintDialogData
printDialogData(* g_printData
);
222 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printDialogData
);
226 wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK
);
230 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
231 frame
->Centre(wxBOTH
);
236 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
238 (*g_pageSetupData
) = *g_printData
;
240 wxPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
241 pageSetupDialog
.ShowModal();
243 (*g_printData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
244 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupData();
247 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
248 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
250 wxPostScriptPrinter
printer(g_printData
);
251 MyPrintout
printout(_T("My printout"));
252 printer
.Print(this, &printout
, true/*prompt*/);
254 (*g_printData
) = printer
.GetPrintData();
257 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
259 // Pass two printout objects: for preview, and possible printing.
260 wxPrintDialogData
printDialogData(* g_printData
);
261 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printDialogData
);
262 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
263 frame
->Centre(wxBOTH
);
268 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
270 (*g_pageSetupData
) = * g_printData
;
272 wxGenericPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
273 pageSetupDialog
.ShowModal();
275 (*g_printData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
276 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupData();
281 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
283 (void)wxMessageBox(_T("wxWidgets printing demo\nAuthor: Julian Smart"),
284 _T("About wxWidgets printing demo"), wxOK
|wxCENTRE
);
287 void MyFrame::OnAngleUp(wxCommandEvent
& WXUNUSED(event
))
293 void MyFrame::OnAngleDown(wxCommandEvent
& WXUNUSED(event
))
299 void MyFrame::Draw(wxDC
& dc
)
301 dc
.SetBackground(*wxWHITE_BRUSH
);
303 dc
.SetFont(wxGetApp().m_testFont
);
305 dc
.SetBackgroundMode(wxTRANSPARENT
);
307 dc
.SetBrush(*wxCYAN_BRUSH
);
308 dc
.SetPen(*wxRED_PEN
);
310 dc
.DrawRoundedRectangle(0, 20, 200, 80, 20);
312 dc
.DrawText( wxT("Rectangle 200 by 80"), 40, 40);
314 dc
.SetPen( wxPen(*wxBLACK
,0,wxDOT_DASH
) );
315 dc
.DrawEllipse(50, 140, 100, 50);
316 dc
.SetPen(*wxRED_PEN
);
318 dc
.DrawText( wxT("Test message: this is in 10 point text"), 10, 180);
322 char *test
= "Hebrew שלום -- Japanese (日本語)";
323 wxString tmp
= wxConvUTF8
.cMB2WC( test
);
324 dc
.DrawText( tmp
, 10, 200 );
338 dc
.DrawPolygon( 5, points
, 20, 250, wxODDEVEN_RULE
);
339 dc
.DrawPolygon( 5, points
, 50, 250, wxWINDING_RULE
);
341 dc
.DrawEllipticArc( 80, 250, 60, 30, 0.0, 270.0 );
351 dc
.DrawSpline( 4, points
);
353 dc
.DrawArc( 20,10, 10,10, 25,40 );
357 str
.Printf( wxT("---- Text at angle %d ----"), i
);
358 dc
.DrawRotatedText( str
, 100, 300, i
);
361 str
.Printf( wxT("---- Text at angle %d ----"), i
);
362 dc
.DrawRotatedText( str
, 100, 300, i
);
364 dc
.SetPen(* wxBLACK_PEN
);
365 dc
.DrawLine(0, 0, 200, 200);
366 dc
.DrawLine(200, 0, 0, 200);
368 wxIcon my_icon
= wxICON(mondrian
) ;
370 dc
.DrawIcon( my_icon
, 100, 100);
373 dc
.DrawBitmap( m_bitmap
, 10, 10 );
376 void MyFrame::OnSize(wxSizeEvent
& event
)
378 wxFrame::OnSize(event
);
381 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
382 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
385 // Define a constructor for my canvas
386 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
387 wxScrolledWindow(frame
, wxID_ANY
, pos
, size
, style
)
389 SetBackgroundColour(* wxWHITE
);
392 // Define the repainting behaviour
393 void MyCanvas::OnDraw(wxDC
& dc
)
398 void MyCanvas::OnEvent(wxMouseEvent
& WXUNUSED(event
))
402 bool MyPrintout::OnPrintPage(int page
)
412 dc
->SetDeviceOrigin(0, 0);
413 dc
->SetUserScale(1.0, 1.0);
416 wxSprintf(buf
, wxT("PAGE %d"), page
);
417 dc
->DrawText(buf
, 10, 10);
425 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
427 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
433 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
441 bool MyPrintout::HasPage(int pageNum
)
443 return (pageNum
== 1 || pageNum
== 2);
446 void MyPrintout::DrawPageOne(wxDC
*dc
)
448 // You might use THIS code if you were scaling
449 // graphics of known size to fit on the page.
451 // We know the graphic is 200x200. If we didn't know this,
452 // we'd need to calculate it.
456 // Let's have at least 50 device units margin
460 // Add the margin to the graphic size
464 // Get the size of the DC in pixels
468 // Calculate a suitable scaling factor
469 float scaleX
=(float)(w
/maxX
);
470 float scaleY
=(float)(h
/maxY
);
472 // Use x or y scaling factor, whichever fits on the DC
473 float actualScale
= wxMin(scaleX
,scaleY
);
475 // Calculate the position on the DC for centring the graphic
476 float posX
= (float)((w
- (200*actualScale
))/2.0);
477 float posY
= (float)((h
- (200*actualScale
))/2.0);
479 // Set the scale and origin
480 dc
->SetUserScale(actualScale
, actualScale
);
481 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
486 void MyPrintout::DrawPageTwo(wxDC
*dc
)
488 // You might use THIS code to set the printer DC to ROUGHLY reflect
489 // the screen text size. This page also draws lines of actual length
492 // Get the logical pixels per inch of screen and printer
493 int ppiScreenX
, ppiScreenY
;
494 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
495 int ppiPrinterX
, ppiPrinterY
;
496 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
498 // This scales the DC so that the printout roughly represents the
499 // the screen scaling. The text point size _should_ be the right size
500 // but in fact is too small for some reason. This is a detail that will
501 // need to be addressed at some point but can be fudged for the
503 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
505 // Now we have to check in case our real page size is reduced
506 // (e.g. because we're drawing to a print preview memory DC)
507 int pageWidth
, pageHeight
;
510 GetPageSizePixels(&pageWidth
, &pageHeight
);
512 // If printer pageWidth == current DC width, then this doesn't
513 // change. But w might be the preview bitmap width, so scale down.
514 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
515 dc
->SetUserScale(overallScale
, overallScale
);
517 // Calculate conversion factor for converting millimetres into
519 // There are approx. 25.4 mm to the inch. There are ppi
520 // device units to the inch. Therefore 1 mm corresponds to
521 // ppi/25.4 device units. We also divide by the
522 // screen-to-printer scaling factor, because we need to
523 // unscale to pass logical units to DrawLine.
525 // Draw 50 mm by 50 mm L shape
526 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.4));
527 float logUnits
= (float)(50*logUnitsFactor
);
528 dc
->SetPen(* wxBLACK_PEN
);
529 dc
->DrawLine(50, 250, (long)(50.0 + logUnits
), 250);
530 dc
->DrawLine(50, 250, 50, (long)(250.0 + logUnits
));
532 dc
->SetBackgroundMode(wxTRANSPARENT
);
533 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
535 { // GetTextExtent demo:
536 wxString words
[7] = {_T("This "), _T("is "), _T("GetTextExtent "), _T("testing "), _T("string. "), _T("Enjoy "), _T("it!")};
538 long x
= 200, y
= 250;
539 wxFont
fnt(15, wxSWISS
, wxNORMAL
, wxNORMAL
);
543 for (int i
= 0; i
< 7; i
++)
545 wxString word
= words
[i
];
546 word
.Remove( word
.Len()-1, 1 );
547 dc
->GetTextExtent(word
, &w
, &h
);
548 dc
->DrawRectangle(x
, y
, w
, h
);
549 dc
->GetTextExtent(words
[i
], &w
, &h
);
550 dc
->DrawText(words
[i
], x
, y
);
556 dc
->SetFont(wxGetApp().m_testFont
);
558 dc
->DrawText(_T("Some test text"), 200, 300 );
563 int rightMargin
= 20;
565 int bottomMargin
= 20;
567 int pageWidthMM
, pageHeightMM
;
568 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
570 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
571 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
572 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
573 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
575 dc
->SetPen(* wxRED_PEN
);
576 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
577 (long)rightMarginLogical
, (long)topMarginLogical
);
578 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
579 (long)rightMarginLogical
, (long)bottomMarginLogical
);
581 WritePageHeader(this, dc
, _T("A header"), logUnitsFactor
);
584 // Writes a header on a page. Margin units are in millimetres.
585 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, wxChar
*text
, float mmToLogical
)
588 static wxFont *headerFont = (wxFont *) NULL;
591 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
593 dc->SetFont(headerFont);
596 int pageWidthMM
, pageHeightMM
;
598 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
599 wxUnusedVar(pageHeightMM
);
603 int rightMargin
= 10;
605 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
606 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
607 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
609 long xExtent
, yExtent
;
610 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
611 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
612 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
614 dc
->SetPen(* wxBLACK_PEN
);
615 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
616 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);