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
, char *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 // Compatibility with old system. In fact, we might keep wxThePrintSetupData
83 // just for useful default values which we can optionally assign to our
84 // own print data object.
86 #if defined(__WXGTK__) || defined(__WXMOTIF__)
87 (*g_printData
) = * wxThePrintSetupData
;
90 // Create the main frame window
91 frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows Printing Demo", wxPoint(0, 0), wxSize(400, 400));
93 // Give it a status line
94 frame
->CreateStatusBar(2);
96 // Load icon and bitmap
97 frame
->SetIcon( wxICON( mondrian
) );
100 wxMenu
*file_menu
= new wxMenu
;
102 file_menu
->Append(WXPRINT_PRINT
, "&Print...", "Print");
103 file_menu
->Append(WXPRINT_PRINT_SETUP
, "Print &Setup...", "Setup printer properties");
104 file_menu
->Append(WXPRINT_PAGE_SETUP
, "Page Set&up...", "Page setup");
105 file_menu
->Append(WXPRINT_PREVIEW
, "Print Pre&view", "Preview");
108 wxAcceleratorEntry entries
[1];
109 entries
[0].Set(wxACCEL_CTRL
, (int) 'V', WXPRINT_PREVIEW
);
110 wxAcceleratorTable
accel(1, entries
);
111 frame
->SetAcceleratorTable(accel
);
113 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
114 file_menu
->AppendSeparator();
115 file_menu
->Append(WXPRINT_PRINT_PS
, "Print PostScript...", "Print (PostScript)");
116 file_menu
->Append(WXPRINT_PRINT_SETUP_PS
, "Print Setup PostScript...", "Setup printer properties (PostScript)");
117 file_menu
->Append(WXPRINT_PAGE_SETUP_PS
, "Page Setup PostScript...", "Page setup (PostScript)");
118 file_menu
->Append(WXPRINT_PREVIEW_PS
, "Print Preview PostScript", "Preview (PostScript)");
120 file_menu
->AppendSeparator();
121 file_menu
->Append(WXPRINT_QUIT
, "E&xit", "Exit program");
123 wxMenu
*help_menu
= new wxMenu
;
124 help_menu
->Append(WXPRINT_ABOUT
, "&About", "About this demo");
126 wxMenuBar
*menu_bar
= new wxMenuBar
;
128 menu_bar
->Append(file_menu
, "&File");
129 menu_bar
->Append(help_menu
, "&Help");
131 // Associate the menu bar with the frame
132 frame
->SetMenuBar(menu_bar
);
134 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100), wxRETAINED
|wxHSCROLL
|wxVSCROLL
);
136 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
137 canvas
->SetScrollbars(20, 20, 50, 50);
139 frame
->canvas
= canvas
;
141 frame
->Centre(wxBOTH
);
144 frame
->SetStatusText("Printing demo");
153 delete wxGetApp().m_testFont
;
155 delete g_pageSetupData
;
159 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
160 EVT_MENU(WXPRINT_QUIT
, MyFrame::OnExit
)
161 EVT_MENU(WXPRINT_PRINT
, MyFrame::OnPrint
)
162 EVT_MENU(WXPRINT_PREVIEW
, MyFrame::OnPrintPreview
)
163 EVT_MENU(WXPRINT_PRINT_SETUP
, MyFrame::OnPrintSetup
)
164 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
165 EVT_MENU(WXPRINT_ABOUT
, MyFrame::OnPrintAbout
)
166 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
167 EVT_MENU(WXPRINT_PRINT_PS
, MyFrame::OnPrintPS
)
168 EVT_MENU(WXPRINT_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
169 EVT_MENU(WXPRINT_PRINT_SETUP_PS
, MyFrame::OnPrintSetupPS
)
170 EVT_MENU(WXPRINT_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
174 // Define my frame constructor
175 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
176 wxFrame(frame
, -1, title
, pos
, size
)
178 canvas
= (MyCanvas
*) NULL
;
181 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
186 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
188 wxPrintDialogData
printDialogData(* g_printData
);
190 wxPrinter
printer(& printDialogData
);
191 MyPrintout
printout("My printout");
192 if (!printer
.Print(this, &printout
, TRUE
))
193 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
);
196 (*g_printData
) = printer
.GetPrintDialogData().GetPrintData();
200 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
202 // Pass two printout objects: for preview, and possible printing.
203 wxPrintDialogData
printDialogData(* g_printData
);
204 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printDialogData
);
208 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK
);
212 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
213 frame
->Centre(wxBOTH
);
218 void MyFrame::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
220 wxPrintDialogData
printDialogData(* g_printData
);
221 wxPrintDialog
printerDialog(this, & printDialogData
);
223 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
224 printerDialog
.ShowModal();
226 (*g_printData
) = printerDialog
.GetPrintDialogData().GetPrintData();
229 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
231 (*g_pageSetupData
) = * g_printData
;
233 wxPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
234 pageSetupDialog
.ShowModal();
236 (*g_printData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
237 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupData();
240 #if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW
241 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
243 wxPostScriptPrinter
printer(g_printData
);
244 MyPrintout
printout("My printout");
245 printer
.Print(this, &printout
, TRUE
);
247 (*g_printData
) = printer
.GetPrintData();
250 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
252 // Pass two printout objects: for preview, and possible printing.
253 wxPrintDialogData
printDialogData(* g_printData
);
254 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printDialogData
);
255 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
256 frame
->Centre(wxBOTH
);
261 void MyFrame::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
263 wxPrintDialogData
printDialogData(* g_printData
);
264 wxGenericPrintDialog
printerDialog(this, & printDialogData
);
266 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
267 printerDialog
.ShowModal();
269 (*g_printData
) = printerDialog
.GetPrintDialogData().GetPrintData();
272 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
274 (*g_pageSetupData
) = * g_printData
;
276 wxGenericPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
277 pageSetupDialog
.ShowModal();
279 (*g_printData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
280 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupData();
285 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
287 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk",
288 "About wxWindows printing demo", wxOK
|wxCENTRE
);
291 void MyFrame::Draw(wxDC
& dc
)
293 dc
.SetBackground(*wxWHITE_BRUSH
);
295 dc
.SetFont(* wxGetApp().m_testFont
);
297 dc
.SetBackgroundMode(wxTRANSPARENT
);
299 dc
.SetBrush(* wxCYAN_BRUSH
);
300 dc
.SetPen(* wxRED_PEN
);
302 dc
.DrawRectangle(0, 30, 200, 100);
303 dc
.DrawText("Rectangle 200 by 100", 40, 40);
305 dc
.DrawEllipse(50, 140, 100, 50);
307 dc
.DrawText("Test message: this is in 10 point text", 10, 180);
309 dc
.SetPen(* wxBLACK_PEN
);
310 dc
.DrawLine(0, 0, 200, 200);
311 dc
.DrawLine(200, 0, 0, 200);
313 wxIcon my_icon
= wxICON(mondrian
) ;
315 dc
.DrawIcon( my_icon
, 100, 100);
318 void MyFrame::OnSize(wxSizeEvent
& event
)
320 wxFrame::OnSize(event
);
323 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
324 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
327 // Define a constructor for my canvas
328 MyCanvas::MyCanvas(wxFrame
*frame
, const wxPoint
& pos
, const wxSize
& size
, long style
):
329 wxScrolledWindow(frame
, -1, pos
, size
, style
)
331 SetBackgroundColour(* wxWHITE
);
334 MyCanvas::~MyCanvas(void)
338 // Define the repainting behaviour
339 void MyCanvas::OnDraw(wxDC
& dc
)
344 void MyCanvas::OnEvent(wxMouseEvent
& WXUNUSED(event
))
348 bool MyPrintout::OnPrintPage(int page
)
358 dc
->SetDeviceOrigin(0, 0);
359 dc
->SetUserScale(1.0, 1.0);
362 sprintf(buf
, "PAGE %d", page
);
363 dc
->DrawText(buf
, 10, 10);
371 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
373 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
379 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
387 bool MyPrintout::HasPage(int pageNum
)
389 return (pageNum
== 1 || pageNum
== 2);
392 void MyPrintout::DrawPageOne(wxDC
*dc
)
394 /* You might use THIS code if you were scaling
395 * graphics of known size to fit on the page.
399 // We know the graphic is 200x200. If we didn't know this,
400 // we'd need to calculate it.
404 // Let's have at least 50 device units margin
408 // Add the margin to the graphic size
412 // Get the size of the DC in pixels
415 // Calculate a suitable scaling factor
416 float scaleX
=(float)(w
/maxX
);
417 float scaleY
=(float)(h
/maxY
);
419 // Use x or y scaling factor, whichever fits on the DC
420 float actualScale
= wxMin(scaleX
,scaleY
);
422 // Calculate the position on the DC for centring the graphic
423 float posX
= (float)((w
- (200*actualScale
))/2.0);
424 float posY
= (float)((h
- (200*actualScale
))/2.0);
426 // Set the scale and origin
427 dc
->SetUserScale(actualScale
, actualScale
);
428 dc
->SetDeviceOrigin( (long)posX
, (long)posY
);
433 void MyPrintout::DrawPageTwo(wxDC
*dc
)
435 /* You might use THIS code to set the printer DC to ROUGHLY reflect
436 * the screen text size. This page also draws lines of actual length 5cm
439 // Get the logical pixels per inch of screen and printer
440 int ppiScreenX
, ppiScreenY
;
441 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
442 int ppiPrinterX
, ppiPrinterY
;
443 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
445 // This scales the DC so that the printout roughly represents the
446 // the screen scaling. The text point size _should_ be the right size
447 // but in fact is too small for some reason. This is a detail that will
448 // need to be addressed at some point but can be fudged for the
450 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
452 // Now we have to check in case our real page size is reduced
453 // (e.g. because we're drawing to a print preview memory DC)
454 int pageWidth
, pageHeight
;
457 GetPageSizePixels(&pageWidth
, &pageHeight
);
459 // If printer pageWidth == current DC width, then this doesn't
460 // change. But w might be the preview bitmap width, so scale down.
461 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
462 dc
->SetUserScale(overallScale
, overallScale
);
464 // Calculate conversion factor for converting millimetres into
466 // There are approx. 25.1 mm to the inch. There are ppi
467 // device units to the inch. Therefore 1 mm corresponds to
468 // ppi/25.1 device units. We also divide by the
469 // screen-to-printer scaling factor, because we need to
470 // unscale to pass logical units to DrawLine.
472 // Draw 50 mm by 50 mm L shape
473 float logUnitsFactor
= (float)(ppiPrinterX
/(scale
*25.1));
474 float logUnits
= (float)(50*logUnitsFactor
);
475 dc
->SetPen(* wxBLACK_PEN
);
476 dc
->DrawLine(50, 250, (long)(50.0 + logUnits
), 250);
477 dc
->DrawLine(50, 250, 50, (long)(250.0 + logUnits
));
479 dc
->SetFont(* wxGetApp().m_testFont
);
480 dc
->SetBackgroundMode(wxTRANSPARENT
);
482 dc
->DrawText("Some test text", 200, 200 );
484 { // GetTextExtent demo:
485 wxString words
[7] = {"This ", "is ", "GetTextExtent ", "testing ", "string. ", "Enjoy ", "it!"};
487 long x
= 200, y
= 250;
488 wxFont
fnt(15, wxSWISS
, wxNORMAL
, wxNORMAL
);
491 for (int i
= 0; i
< 7; i
++) {
492 dc
->GetTextExtent(words
[i
], &w
, &h
);
493 dc
->DrawRectangle(x
, y
, w
, h
);
494 dc
->DrawText(words
[i
], x
, y
);
497 dc
->SetFont(* wxGetApp().m_testFont
);
503 int rightMargin
= 20;
505 int bottomMargin
= 20;
507 int pageWidthMM
, pageHeightMM
;
508 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
510 float leftMarginLogical
= (float)(logUnitsFactor
*leftMargin
);
511 float topMarginLogical
= (float)(logUnitsFactor
*topMargin
);
512 float bottomMarginLogical
= (float)(logUnitsFactor
*(pageHeightMM
- bottomMargin
));
513 float rightMarginLogical
= (float)(logUnitsFactor
*(pageWidthMM
- rightMargin
));
515 dc
->SetPen(* wxRED_PEN
);
516 dc
->DrawLine( (long)leftMarginLogical
, (long)topMarginLogical
,
517 (long)rightMarginLogical
, (long)topMarginLogical
);
518 dc
->DrawLine( (long)leftMarginLogical
, (long)bottomMarginLogical
,
519 (long)rightMarginLogical
, (long)bottomMarginLogical
);
521 WritePageHeader(this, dc
, "A header", logUnitsFactor
);
524 // Writes a header on a page. Margin units are in millimetres.
525 bool WritePageHeader(wxPrintout
*printout
, wxDC
*dc
, char *text
, float mmToLogical
)
528 static wxFont *headerFont = (wxFont *) NULL;
531 headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
533 dc->SetFont(headerFont);
536 int pageWidthMM
, pageHeightMM
;
538 printout
->GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
542 int rightMargin
= 10;
544 float leftMarginLogical
= (float)(mmToLogical
*leftMargin
);
545 float topMarginLogical
= (float)(mmToLogical
*topMargin
);
546 float rightMarginLogical
= (float)(mmToLogical
*(pageWidthMM
- rightMargin
));
548 long xExtent
, yExtent
;
549 dc
->GetTextExtent(text
, &xExtent
, &yExtent
);
550 float xPos
= (float)(((((pageWidthMM
- leftMargin
- rightMargin
)/2.0)+leftMargin
)*mmToLogical
) - (xExtent
/2.0));
551 dc
->DrawText(text
, (long)xPos
, (long)topMarginLogical
);
553 dc
->SetPen(* wxBLACK_PEN
);
554 dc
->DrawLine( (long)leftMarginLogical
, (long)(topMarginLogical
+yExtent
),
555 (long)rightMarginLogical
, (long)topMarginLogical
+yExtent
);