]>
Commit | Line | Data |
---|---|---|
5526e819 VS |
1 | /* |
2 | * File: printing.cc | |
3 | * Purpose: Printing demo for wxWindows class library | |
4 | * Author: Julian Smart | |
5 | * modified by Vaclav Slavik (wxHTML stuffs) | |
6 | * Created: 1995 | |
7 | * Updated: | |
8 | * Copyright: (c) 1995, AIAI, University of Edinburgh | |
9 | */ | |
10 | ||
11 | /* static const char sccsid[] = "%W% %G%"; */ | |
12 | ||
13 | #ifdef __GNUG__ | |
14 | #pragma implementation | |
15 | #endif | |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/wx.h" | |
26 | #endif | |
27 | ||
28 | #if !wxUSE_PRINTING_ARCHITECTURE | |
29 | #error You must set wxUSE_PRINTING_ARCHITECTURE to 1 in setup.h to compile this demo. | |
30 | #endif | |
31 | ||
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. | |
34 | ||
35 | //!!! DON'T DO THAT! This is wxHTML sample now | |
36 | #define wxTEST_POSTSCRIPT_IN_MSW 0 | |
37 | ||
38 | #include <ctype.h> | |
39 | #include "wx/metafile.h" | |
40 | #include "wx/print.h" | |
41 | #include "wx/printdlg.h" | |
42 | ||
43 | #include "wx/accel.h" | |
44 | ||
45 | #if wxTEST_POSTSCRIPT_IN_MSW | |
46 | #include "wx/generic/printps.h" | |
47 | #include "wx/generic/prntdlgg.h" | |
48 | #endif | |
49 | ||
50 | #include <wx/wxhtml.h> | |
51 | #include <wx/wfstream.h> | |
52 | #include "printing.h" | |
53 | ||
54 | #ifndef __WXMSW__ | |
55 | #include "mondrian.xpm" | |
56 | #endif | |
57 | ||
58 | // Global print data, to remember settings during the session | |
59 | wxPrintData *g_printData = (wxPrintData*) NULL ; | |
60 | ||
61 | // Global page setup data | |
62 | wxPageSetupData* g_pageSetupData = (wxPageSetupData*) NULL; | |
63 | ||
64 | ||
65 | // Declare a frame | |
66 | MyFrame *frame = (MyFrame *) NULL; | |
67 | wxHtmlWindow *html = NULL; | |
68 | int orientation = wxPORTRAIT; | |
69 | ||
70 | // Main proc | |
71 | IMPLEMENT_APP(MyApp) | |
72 | ||
73 | ||
74 | MyApp::MyApp() | |
75 | { | |
76 | } | |
77 | ||
78 | // The `main program' equivalent, creating the windows and returning the | |
79 | // main frame | |
80 | bool MyApp::OnInit(void) | |
81 | { | |
82 | g_printData = new wxPrintData; | |
83 | g_pageSetupData = new wxPageSetupDialogData; | |
84 | ||
85 | // Create the main frame window | |
86 | frame = new MyFrame((wxFrame *) NULL, (char *) "wxWindows Printing Demo", wxPoint(0, 0), wxSize(600, 400)); | |
87 | ||
88 | // Give it a status line | |
89 | frame->CreateStatusBar(2); | |
90 | ||
91 | // Load icon and bitmap | |
92 | frame->SetIcon( wxICON( mondrian) ); | |
93 | ||
94 | // Make a menubar | |
95 | wxMenu *file_menu = new wxMenu; | |
96 | ||
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"); | |
101 | ||
102 | // Accelerators | |
103 | wxAcceleratorEntry entries[1]; | |
104 | entries[0].Set(wxACCEL_CTRL, (int) 'V', WXPRINT_PREVIEW); | |
105 | wxAcceleratorTable accel(1, entries); | |
106 | frame->SetAcceleratorTable(accel); | |
107 | ||
108 | file_menu->AppendSeparator(); | |
109 | file_menu->Append(WXPRINT_QUIT, "E&xit", "Exit program"); | |
110 | ||
111 | wxMenu *help_menu = new wxMenu; | |
112 | help_menu->Append(WXPRINT_ABOUT, "&About", "About this demo"); | |
113 | ||
114 | wxMenuBar *menu_bar = new wxMenuBar; | |
115 | ||
116 | menu_bar->Append(file_menu, "&File"); | |
117 | menu_bar->Append(help_menu, "&Help"); | |
118 | ||
119 | // Associate the menu bar with the frame | |
120 | frame->SetMenuBar(menu_bar); | |
121 | ||
122 | frame->Centre(wxBOTH); | |
123 | frame->Show(TRUE); | |
124 | ||
125 | frame->SetStatusText("Printing demo"); | |
126 | ||
127 | SetTopWindow(frame); | |
128 | ||
129 | return TRUE; | |
130 | } | |
131 | ||
132 | int MyApp::OnExit() | |
133 | { | |
134 | delete g_printData; | |
135 | delete g_pageSetupData; | |
136 | return 1; | |
137 | } | |
138 | ||
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) | |
146 | END_EVENT_TABLE() | |
147 | ||
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) | |
151 | { | |
152 | html = new wxHtmlWindow(this); | |
153 | html -> LoadPage("test.htm"); | |
154 | } | |
155 | ||
156 | void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event)) | |
157 | { | |
158 | Close(TRUE); | |
159 | } | |
160 | ||
161 | void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event)) | |
162 | { | |
163 | wxPrinter printer; | |
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); | |
167 | } | |
168 | ||
169 | void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event)) | |
170 | { | |
171 | wxPrintData printData; | |
172 | printData.SetOrientation(orientation); | |
173 | ||
174 | // Pass two printout objects: for preview, and possible printing. | |
175 | wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printData); | |
176 | if (!preview->Ok()) | |
177 | { | |
178 | delete preview; | |
179 | wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK); | |
180 | return; | |
181 | } | |
182 | ||
183 | wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650)); | |
184 | frame->Centre(wxBOTH); | |
185 | frame->Initialize(); | |
186 | frame->Show(TRUE); | |
187 | } | |
188 | ||
189 | void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event)) | |
190 | { | |
191 | wxPrintDialogData printDialogData(* g_printData); | |
192 | wxPrintDialog printerDialog(this, & printDialogData); | |
193 | ||
194 | printerDialog.GetPrintDialogData().SetSetupDialog(TRUE); | |
195 | printerDialog.ShowModal(); | |
196 | ||
197 | (*g_printData) = printerDialog.GetPrintDialogData().GetPrintData(); | |
198 | } | |
199 | ||
200 | void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event)) | |
201 | { | |
202 | (*g_pageSetupData) = * g_printData; | |
203 | ||
204 | wxPageSetupDialog pageSetupDialog(this, g_pageSetupData); | |
205 | pageSetupDialog.ShowModal(); | |
206 | ||
207 | (*g_printData) = pageSetupDialog.GetPageSetupData().GetPrintData(); | |
208 | (*g_pageSetupData) = pageSetupDialog.GetPageSetupData(); | |
209 | } | |
210 | ||
211 | ||
212 | ||
213 | void MyFrame::OnPrintAbout(wxCommandEvent& WXUNUSED(event)) | |
214 | { | |
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); | |
217 | } | |
218 | ||
219 | ||
220 | bool MyPrintout::OnPrintPage(int page) | |
221 | { | |
222 | wxDC *dc = GetDC(); | |
223 | if (dc) | |
224 | { | |
225 | if (page == 1) | |
226 | DrawPageOne(dc); | |
227 | ||
228 | return TRUE; | |
229 | } | |
230 | else | |
231 | return FALSE; | |
232 | } | |
233 | ||
234 | bool MyPrintout::OnBeginDocument(int startPage, int endPage) | |
235 | { | |
236 | if (!wxPrintout::OnBeginDocument(startPage, endPage)) | |
237 | return FALSE; | |
238 | ||
239 | return TRUE; | |
240 | } | |
241 | ||
242 | void MyPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) | |
243 | { | |
244 | *minPage = 1; | |
245 | *maxPage = 1; | |
246 | *selPageFrom = 1; | |
247 | *selPageTo = 1; | |
248 | } | |
249 | ||
250 | bool MyPrintout::HasPage(int pageNum) | |
251 | { | |
252 | return (pageNum == 1); | |
253 | } | |
254 | ||
255 | ||
256 | void MyPrintout::DrawPageOne(wxDC *dc) | |
257 | { | |
258 | int leftMargin = 20; | |
259 | int topMargin = 40; | |
260 | ||
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 | |
263 | * on the page. | |
264 | */ | |
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); | |
270 | ||
271 | // Here we obtain internal cell representation of HTML document: | |
272 | wxHtmlContainerCell *cell = html -> GetInternalRepresentation(); | |
273 | ||
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; | |
277 | int w, h; | |
278 | dc->GetSize(&w, &h); | |
279 | GetPageSizePixels(&pageWidth, &pageHeight); | |
280 | ||
281 | // Now we must scale it somehow. The best would be to suppose that html window | |
282 | // width is equal to page width: | |
283 | ||
284 | float scale = (float)((float)(pageWidth - 0 * leftMargin)/((float)cell -> GetMaxLineWidth() + 2 * leftMargin)); | |
285 | ||
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); | |
290 | ||
291 | // Calculate conversion factor for converting millimetres into | |
292 | // logical units. | |
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. | |
298 | ||
299 | dc->SetBackgroundMode(wxTRANSPARENT); | |
300 | ||
301 | // TESTING | |
302 | ||
303 | int pageWidthMM, pageHeightMM; | |
304 | GetPageSizeMM(&pageWidthMM, &pageHeightMM); | |
305 | ||
306 | ||
307 | // This is all the printing : | |
308 | cell -> Draw(*dc, leftMargin, topMargin, 0, cell -> GetHeight()); | |
309 | } | |
310 | ||
311 | ||
312 |