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
);
69 // The `main program' equivalent, creating the windows and returning the
72 bool MyApp::OnInit(void)
74 wxInitAllImageHandlers();
76 m_testFont
.Create(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
78 g_printData
= new wxPrintData
;
79 g_pageSetupData
= new wxPageSetupDialogData
;
81 // Create the main frame window
82 frame
= new MyFrame((wxFrame
*) NULL
, _T("wxWidgets Printing Demo"), wxPoint(0, 0), wxSize(400, 400));
85 // Give it a status line
86 frame
->CreateStatusBar(2);
87 #endif // wxUSE_STATUSBAR
89 // Load icon and bitmap
90 frame
->SetIcon( wxICON( mondrian
) );
93 wxMenu
*file_menu
= new wxMenu
;
95 file_menu
->Append(WXPRINT_PRINT
, _T("&Print..."), _T("Print"));
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_PAGE_SETUP_PS
, _T("Page Setup PostScript..."), _T("Page setup (PostScript)"));
111 file_menu
->Append(WXPRINT_PREVIEW_PS
, _T("Print Preview PostScript"), _T("Preview (PostScript)"));
113 file_menu
->AppendSeparator();
114 file_menu
->Append(WXPRINT_ANGLEUP
, _T("Angle up\tAlt-U"), _T("Raise rotated text angle"));
115 file_menu
->Append(WXPRINT_ANGLEDOWN
, _T("Angle down\tAlt-D"), _T("Lower rotated text angle"));
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_PAGE_SETUP
, MyFrame::OnPageSetup
)
161 EVT_MENU(WXPRINT_ABOUT
, MyFrame::OnPrintAbout
)
162 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
163 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame::OnPrintPS
)
164 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
165 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
167 EVT_MENU(WXPRINT_ANGLEUP
, MyFrame::OnAngleUp
)
168 EVT_MENU(WXPRINT_ANGLEDOWN
, MyFrame::OnAngleDown
)
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 wxImage
image( wxT("test.jpg") );
181 for (i
= 0; i
< image
.GetWidth(); i
++)
182 for (j
= 0; j
< image
.GetHeight(); j
++)
183 image
.SetAlpha( i
, j
, 50 );
188 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
190 Close(true /*force closing*/);
193 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
195 wxPrintDialogData
printDialogData(* g_printData
);
197 wxPrinter
printer(& printDialogData
);
198 MyPrintout
printout(_T("My printout"));
199 if (!printer
.Print(this, &printout
, true /*prompt*/))
201 if (wxPrinter::GetLastError() == wxPRINTER_ERROR
)
202 wxMessageBox(_T("There was a problem printing.\nPerhaps your current printer is not set correctly?"), _T("Printing"), wxOK
);
204 wxMessageBox(_T("You canceled printing"), _T("Printing"), wxOK
);
208 (*g_printData
) = printer
.GetPrintDialogData().GetPrintData();
212 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
214 // Pass two printout objects: for preview, and possible printing.
215 wxPrintDialogData
printDialogData(* g_printData
);
216 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printDialogData
);
220 wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK
);
224 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
225 frame
->Centre(wxBOTH
);
230 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
232 (*g_pageSetupData
) = *g_printData
;
234 wxPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
235 pageSetupDialog
.ShowModal();
237 (*g_printData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
238 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupData();
241 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
242 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
244 wxPostScriptPrinter
printer(g_printData
);
245 MyPrintout
printout(_T("My printout"));
246 printer
.Print(this, &printout
, true/*prompt*/);
248 (*g_printData
) = printer
.GetPrintData();
251 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
253 // Pass two printout objects: for preview, and possible printing.
254 wxPrintDialogData
printDialogData(* g_printData
);
255 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printDialogData
);
256 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
257 frame
->Centre(wxBOTH
);
262 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
264 (*g_pageSetupData
) = * g_printData
;
266 wxGenericPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
267 pageSetupDialog
.ShowModal();
269 (*g_printData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
270 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupData();
275 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
277 (void)wxMessageBox(_T("wxWidgets printing demo\nAuthor: Julian Smart"),
278 _T("About wxWidgets printing demo"), wxOK
|wxCENTRE
);
281 void MyFrame::OnAngleUp(wxCommandEvent
& WXUNUSED(event
))
287 void MyFrame::OnAngleDown(wxCommandEvent
& WXUNUSED(event
))
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
.SetPen( wxPen(*wxBLACK
,0,wxDOT_DASH
) );
309 dc
.DrawEllipse(50, 140, 100, 50);
310 dc
.SetPen(*wxRED_PEN
);
312 dc
.DrawText( wxT("Test message: this is in 10 point text"), 10, 180);
315 char *test
= "Hebrew שלום -- Japanese (日本語)";
316 wxString tmp
= wxConvUTF8
.cMB2WC( test
);
317 dc
.DrawText( tmp
, 10, 200 );
322 str
.Printf( wxT("---- Text at angle %d ----"), i
);
323 dc
.DrawRotatedText( str
, 100, 300, i
);
326 str
.Printf( wxT("---- Text at angle %d ----"), i
);
327 dc
.DrawRotatedText( str
, 100, 300, i
);
329 dc
.SetPen(* wxBLACK_PEN
);
330 dc
.DrawLine(0, 0, 200, 200);
331 dc
.DrawLine(200, 0, 0, 200);
333 wxIcon my_icon
= wxICON(mondrian
) ;
335 dc
.DrawIcon( my_icon
, 100, 100);
338 dc
.DrawBitmap( m_bitmap
, 10, 10 );
341 void MyFrame::OnSize(wxSizeEvent
& event
)
343 wxFrame::OnSize(event
);
346 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
347 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
350 // Define a constructor for my canvas
351 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
352 wxScrolledWindow(frame
, wxID_ANY
, pos
, size
, style
)
354 SetBackgroundColour(* wxWHITE
);
357 // Define the repainting behaviour
358 void MyCanvas::OnDraw(wxDC
& dc
)
363 void MyCanvas::OnEvent(wxMouseEvent
& WXUNUSED(event
))
367 bool MyPrintout::OnPrintPage(int page
)
377 dc
->SetDeviceOrigin(0, 0);
378 dc
->SetUserScale(1.0, 1.0);
381 wxSprintf(buf
, wxT("PAGE %d"), page
);
382 dc
->DrawText(buf
, 10, 10);
390 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
392 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
398 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
406 bool MyPrintout::HasPage(int pageNum
)
408 return (pageNum
== 1 || pageNum
== 2);
411 void MyPrintout::DrawPageOne(wxDC
*dc
)
413 // You might use THIS code if you were scaling
414 // graphics of known size to fit on the page.
416 // We know the graphic is 200x200. If we didn't know this,
417 // we'd need to calculate it.
421 // Let's have at least 50 device units margin
425 // Add the margin to the graphic size
429 // Get the size of the DC in pixels
433 // Calculate a suitable scaling factor
434 float scaleX
=(float)(w
/maxX
);
435 float scaleY
=(float)(h
/maxY
);
437 // Use x or y scaling factor, whichever fits on the DC
438 float actualScale
= wxMin(scaleX
,scaleY
);
440 // Calculate the position on the DC for centring the graphic
441 float posX
= (float)((w
- (200*actualScale
))/2.0);
442 float posY
= (float)((h
- (200*actualScale
))/2.0);
444 // Set the scale and origin
445 dc
->SetUserScale(actualScale
, actualScale
);
446 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
451 void MyPrintout::DrawPageTwo(wxDC
*dc
)
453 // You might use THIS code to set the printer DC to ROUGHLY reflect
454 // the screen text size. This page also draws lines of actual length
457 // Get the logical pixels per inch of screen and printer
458 int ppiScreenX
, ppiScreenY
;
459 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
460 int ppiPrinterX
, ppiPrinterY
;
461 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
463 // This scales the DC so that the printout roughly represents the
464 // the screen scaling. The text point size _should_ be the right size
465 // but in fact is too small for some reason. This is a detail that will
466 // need to be addressed at some point but can be fudged for the
468 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
470 // Now we have to check in case our real page size is reduced
471 // (e.g. because we're drawing to a print preview memory DC)
472 int pageWidth
, pageHeight
;
475 GetPageSizePixels(&pageWidth
, &pageHeight
);
477 // If printer pageWidth == current DC width, then this doesn't
478 // change. But w might be the preview bitmap width, so scale down.
479 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
480 dc
->SetUserScale(overallScale
, overallScale
);
482 // Calculate conversion factor for converting millimetres into
484 // There are approx. 25.1 mm to the inch. There are ppi
485 // device units to the inch. Therefore 1 mm corresponds to
486 // ppi/25.1 device units. We also divide by the
487 // screen-to-printer scaling factor, because we need to
488 // unscale to pass logical units to DrawLine.
490 // Draw 50 mm by 50 mm L shape
491 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.1));
492 float logUnits
= (float)(50*logUnitsFactor
);
493 dc
->SetPen(* wxBLACK_PEN
);
494 dc
->DrawLine(50, 250, (long)(50.0 + logUnits
), 250);
495 dc
->DrawLine(50, 250, 50, (long)(250.0 + logUnits
));
497 dc
->SetBackgroundMode(wxTRANSPARENT
);
498 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
500 { // GetTextExtent demo:
501 wxString words
[7] = {_T("This "), _T("is "), _T("GetTextExtent "), _T("testing "), _T("string. "), _T("Enjoy "), _T("it!")};
503 long x
= 200, y
= 250;
504 wxFont
fnt(15, wxSWISS
, wxNORMAL
, wxNORMAL
);
508 for (int i
= 0; i
< 7; i
++)
510 wxString word
= words
[i
];
511 word
.Remove( word
.Len()-1, 1 );
512 dc
->GetTextExtent(word
, &w
, &h
);
513 dc
->DrawRectangle(x
, y
, w
, h
);
514 dc
->GetTextExtent(words
[i
], &w
, &h
);
515 dc
->DrawText(words
[i
], x
, y
);
521 dc
->SetFont(wxGetApp().m_testFont
);
523 dc
->DrawText(_T("Some test text"), 200, 300 );
528 int rightMargin
= 20;
530 int bottomMargin
= 20;
532 int pageWidthMM
, pageHeightMM
;
533 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
535 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
536 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
537 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
538 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
540 dc
->SetPen(* wxRED_PEN
);
541 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
542 (long)rightMarginLogical
, (long)topMarginLogical
);
543 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
544 (long)rightMarginLogical
, (long)bottomMarginLogical
);
546 WritePageHeader(this, dc
, _T("A header"), logUnitsFactor
);
549 // Writes a header on a page. Margin units are in millimetres.
550 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, wxChar
*text
, float mmToLogical
)
553 static wxFont *headerFont = (wxFont *) NULL;
556 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
558 dc->SetFont(headerFont);
561 int pageWidthMM
, pageHeightMM
;
563 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
564 wxUnusedVar(pageHeightMM
);
568 int rightMargin
= 10;
570 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
571 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
572 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
574 long xExtent
, yExtent
;
575 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
576 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
577 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
579 dc
->SetPen(* wxBLACK_PEN
);
580 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
581 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);