3 * Purpose: Printing demo for wxWindows class library
5 * modified by Vaclav Slavik (wxHTML stuffs)
8 * Copyright: (c) 1995, AIAI, University of Edinburgh
11 /* static const char sccsid[] = "%W% %G%"; */
14 #pragma implementation
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
28 #if !wxUSE_PRINTING_ARCHITECTURE
29 #error You must set wxUSE_PRINTING_ARCHITECTURE to 1 in setup.h to compile this demo.
32 // Set this to 1 if you want to test PostScript printing under MSW.
33 // However, you'll also need to edit src/msw/makefile.nt.
35 //!!! DON'T DO THAT! This is wxHTML sample now
36 #define wxTEST_POSTSCRIPT_IN_MSW 0
39 #include "wx/metafile.h"
41 #include "wx/printdlg.h"
45 #if wxTEST_POSTSCRIPT_IN_MSW
46 #include "wx/generic/printps.h"
47 #include "wx/generic/prntdlgg.h"
50 #include <wx/wxhtml.h>
51 #include <wx/wfstream.h>
55 #include "mondrian.xpm"
58 // Global print data, to remember settings during the session
59 wxPrintData
*g_printData
= (wxPrintData
*) NULL
;
61 // Global page setup data
62 wxPageSetupData
* g_pageSetupData
= (wxPageSetupData
*) NULL
;
66 MyFrame
*frame
= (MyFrame
*) NULL
;
67 wxHtmlWindow
*html
= NULL
;
68 int orientation
= wxPORTRAIT
;
78 // The `main program' equivalent, creating the windows and returning the
80 bool MyApp::OnInit(void)
82 g_printData
= new wxPrintData
;
83 g_pageSetupData
= new wxPageSetupDialogData
;
85 // Create the main frame window
86 frame
= new MyFrame((wxFrame
*) NULL
, (char *) "wxWindows Printing Demo", wxPoint(0, 0), wxSize(600, 400));
88 // Give it a status line
89 frame
->CreateStatusBar(2);
91 // Load icon and bitmap
92 frame
->SetIcon( wxICON( mondrian
) );
95 wxMenu
*file_menu
= new wxMenu
;
97 file_menu
->Append(WXPRINT_PRINT
, "&Print...", "Print");
98 file_menu
->Append(WXPRINT_PRINT_SETUP
, "Print &Setup...", "Setup printer properties");
99 file_menu
->Append(WXPRINT_PAGE_SETUP
, "Page Set&up...", "Page setup");
100 file_menu
->Append(WXPRINT_PREVIEW
, "Print Pre&view", "Preview");
103 wxAcceleratorEntry entries
[1];
104 entries
[0].Set(wxACCEL_CTRL
, (int) 'V', WXPRINT_PREVIEW
);
105 wxAcceleratorTable
accel(1, entries
);
106 frame
->SetAcceleratorTable(accel
);
108 file_menu
->AppendSeparator();
109 file_menu
->Append(WXPRINT_QUIT
, "E&xit", "Exit program");
111 wxMenu
*help_menu
= new wxMenu
;
112 help_menu
->Append(WXPRINT_ABOUT
, "&About", "About this demo");
114 wxMenuBar
*menu_bar
= new wxMenuBar
;
116 menu_bar
->Append(file_menu
, "&File");
117 menu_bar
->Append(help_menu
, "&Help");
119 // Associate the menu bar with the frame
120 frame
->SetMenuBar(menu_bar
);
122 frame
->Centre(wxBOTH
);
125 frame
->SetStatusText("Printing demo");
135 delete g_pageSetupData
;
139 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
140 EVT_MENU(WXPRINT_QUIT
, MyFrame::OnExit
)
141 EVT_MENU(WXPRINT_PRINT
, MyFrame::OnPrint
)
142 EVT_MENU(WXPRINT_PREVIEW
, MyFrame::OnPrintPreview
)
143 EVT_MENU(WXPRINT_PRINT_SETUP
, MyFrame::OnPrintSetup
)
144 EVT_MENU(WXPRINT_PAGE_SETUP
, MyFrame::OnPageSetup
)
145 EVT_MENU(WXPRINT_ABOUT
, MyFrame::OnPrintAbout
)
148 // Define my frame constructor
149 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
150 wxFrame(frame
, -1, title
, pos
, size
)
152 html
= new wxHtmlWindow(this);
153 html
-> LoadPage("test.htm");
156 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
161 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
164 MyPrintout
printout("My printout");
165 if (!printer
.Print(this, &printout
, TRUE
))
166 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
);
169 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
171 wxPrintData printData
;
172 printData
.SetOrientation(orientation
);
174 // Pass two printout objects: for preview, and possible printing.
175 wxPrintPreview
*preview
= new wxPrintPreview(new MyPrintout
, new MyPrintout
, & printData
);
179 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK
);
183 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
184 frame
->Centre(wxBOTH
);
189 void MyFrame::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
191 wxPrintDialogData
printDialogData(* g_printData
);
192 wxPrintDialog
printerDialog(this, & printDialogData
);
194 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
195 printerDialog
.ShowModal();
197 (*g_printData
) = printerDialog
.GetPrintDialogData().GetPrintData();
200 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
202 (*g_pageSetupData
) = * g_printData
;
204 wxPageSetupDialog
pageSetupDialog(this, g_pageSetupData
);
205 pageSetupDialog
.ShowModal();
207 (*g_printData
) = pageSetupDialog
.GetPageSetupData().GetPrintData();
208 (*g_pageSetupData
) = pageSetupDialog
.GetPageSetupData();
213 void MyFrame::OnPrintAbout(wxCommandEvent
& WXUNUSED(event
))
215 (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk\n\nModified by Vaclav Slavik to show wxHtml features",
216 "About wxWindows printing demo", wxOK
|wxCENTRE
);
220 bool MyPrintout::OnPrintPage(int page
)
234 bool MyPrintout::OnBeginDocument(int startPage
, int endPage
)
236 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
242 void MyPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
250 bool MyPrintout::HasPage(int pageNum
)
252 return (pageNum
== 1);
256 void MyPrintout::DrawPageOne(wxDC
*dc
)
261 /* You might use THIS code to set the printer DC to ROUGHLY reflect
262 * the screen text size. This page also draws lines of actual length 5cm
265 // Get the logical pixels per inch of screen and printer
266 int ppiScreenX
, ppiScreenY
;
267 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
268 int ppiPrinterX
, ppiPrinterY
;
269 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
271 // Here we obtain internal cell representation of HTML document:
272 wxHtmlContainerCell
*cell
= html
-> GetInternalRepresentation();
274 // Now we have to check in case our real page size is reduced
275 // (e.g. because we're drawing to a print preview memory DC)
276 int pageWidth
, pageHeight
;
279 GetPageSizePixels(&pageWidth
, &pageHeight
);
281 // Now we must scale it somehow. The best would be to suppose that html window
282 // width is equal to page width:
284 float scale
= (float)((float)(pageWidth
- 0 * leftMargin
)/((float)cell
-> GetMaxLineWidth() + 2 * leftMargin
));
286 // If printer pageWidth == current DC width, then this doesn't
287 // change. But w might be the preview bitmap width, so scale down.
288 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
289 dc
->SetUserScale(overallScale
, overallScale
);
291 // Calculate conversion factor for converting millimetres into
293 // There are approx. 25.1 mm to the inch. There are ppi
294 // device units to the inch. Therefore 1 mm corresponds to
295 // ppi/25.1 device units. We also divide by the
296 // screen-to-printer scaling factor, because we need to
297 // unscale to pass logical units to DrawLine.
299 dc
->SetBackgroundMode(wxTRANSPARENT
);
303 int pageWidthMM
, pageHeightMM
;
304 GetPageSizeMM(&pageWidthMM
, &pageHeightMM
);
307 // This is all the printing :
308 cell
-> Draw(*dc
, leftMargin
, topMargin
, 0, cell
-> GetHeight());