]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: prntbase.cpp | |
3 | // Purpose: Printing framework base class implementation | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "prntbase.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
c801d85f KB |
23 | #include "wx/defs.h" |
24 | ||
25 | #ifndef WX_PRECOMP | |
26 | #include "wx/utils.h" | |
27 | #include "wx/dc.h" | |
28 | #include "wx/app.h" | |
29 | #include "wx/msgdlg.h" | |
30 | #include "wx/layout.h" | |
31 | #include "wx/choice.h" | |
32 | #include "wx/button.h" | |
33 | #include "wx/settings.h" | |
34 | #include "wx/dcmemory.h" | |
35 | #include "wx/stattext.h" | |
36 | #include "wx/intl.h" | |
37 | #endif | |
38 | ||
39 | #include "wx/prntbase.h" | |
40 | #include "wx/dcprint.h" | |
41 | #include "wx/printdlg.h" | |
42 | ||
43 | #include <stdlib.h> | |
44 | #include <string.h> | |
45 | ||
2049ba38 | 46 | #ifdef __WXMSW__ |
c801d85f KB |
47 | #include <windows.h> |
48 | #include <commdlg.h> | |
49 | ||
50 | // Clash with Windows header files | |
51 | #ifdef StartDoc | |
52 | #undef StartDoc | |
53 | #endif | |
54 | ||
55 | #ifndef __WIN32__ | |
56 | #include <print.h> | |
57 | #endif | |
58 | ||
c801d85f | 59 | #endif |
2049ba38 | 60 | // End __WXMSW__ |
c801d85f KB |
61 | |
62 | #if !USE_SHARED_LIBRARY | |
63 | IMPLEMENT_CLASS(wxPrinterBase, wxObject) | |
64 | IMPLEMENT_ABSTRACT_CLASS(wxPrintout, wxObject) | |
65 | IMPLEMENT_CLASS(wxPreviewCanvas, wxWindow) | |
66 | IMPLEMENT_CLASS(wxPreviewControlBar, wxWindow) | |
67 | IMPLEMENT_CLASS(wxPreviewFrame, wxFrame) | |
68 | IMPLEMENT_CLASS(wxPrintPreviewBase, wxObject) | |
69 | ||
70 | BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog) | |
71 | EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel) | |
72 | END_EVENT_TABLE() | |
73 | ||
74 | BEGIN_EVENT_TABLE(wxPreviewCanvas, wxScrolledWindow) | |
75 | EVT_PAINT(wxPreviewCanvas::OnPaint) | |
76 | EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged) | |
77 | END_EVENT_TABLE() | |
78 | #endif | |
79 | ||
80 | /* | |
81 | * Printer | |
82 | */ | |
83 | ||
84 | wxPrinterBase::wxPrinterBase(wxPrintData *data) | |
85 | { | |
34da0970 JS |
86 | m_currentPrintout = (wxPrintout *) NULL; |
87 | sm_abortWindow = (wxWindow *) NULL; | |
88 | sm_abortIt = FALSE; | |
c801d85f | 89 | if (data) |
34da0970 | 90 | m_printData = (*data); |
c801d85f KB |
91 | } |
92 | ||
34da0970 JS |
93 | wxWindow *wxPrinterBase::sm_abortWindow = (wxWindow *) NULL; |
94 | bool wxPrinterBase::sm_abortIt = FALSE; | |
c801d85f | 95 | |
34da0970 | 96 | wxPrinterBase::~wxPrinterBase() |
c801d85f KB |
97 | { |
98 | } | |
99 | ||
100 | void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) | |
101 | { | |
34da0970 JS |
102 | wxPrinterBase::sm_abortIt = TRUE; |
103 | wxPrinterBase::sm_abortWindow->Show(FALSE); | |
104 | wxPrinterBase::sm_abortWindow->Close(TRUE); | |
105 | wxPrinterBase::sm_abortWindow = (wxWindow *) NULL; | |
c801d85f KB |
106 | } |
107 | ||
108 | wxWindow *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout *WXUNUSED(printout)) | |
109 | { | |
34da0970 | 110 | wxPrintAbortDialog *dialog = new wxPrintAbortDialog(parent, _("Printing"), wxPoint(0, 0), wxSize(400, 400), wxDEFAULT_DIALOG_STYLE); |
c801d85f KB |
111 | (void) new wxStaticText(dialog, -1, _("Please wait..."), wxPoint(5, 5)); |
112 | ||
113 | wxButton *button = new wxButton(dialog, wxID_CANCEL, _("Cancel"), wxPoint(5, 30)); | |
114 | ||
115 | dialog->Fit(); | |
116 | button->Centre(wxHORIZONTAL); | |
117 | ||
118 | dialog->Centre(); | |
119 | return dialog; | |
120 | } | |
121 | ||
122 | void wxPrinterBase::ReportError(wxWindow *parent, wxPrintout *WXUNUSED(printout), char *message) | |
123 | { | |
1a5a8367 | 124 | wxMessageBox(message, _("Printing Error"), wxOK, parent); |
c801d85f KB |
125 | } |
126 | ||
127 | /* | |
128 | * Printout class | |
129 | */ | |
130 | ||
34da0970 | 131 | wxPrintout::wxPrintout(const wxString& title) |
c801d85f | 132 | { |
34da0970 JS |
133 | m_printoutTitle = title ; |
134 | m_printoutDC = (wxDC *) NULL; | |
135 | m_pageWidthMM = 0; | |
136 | m_pageHeightMM = 0; | |
137 | m_pageWidthPixels = 0; | |
138 | m_pageHeightPixels = 0; | |
139 | m_PPIScreenX = 0; | |
140 | m_PPIScreenY = 0; | |
141 | m_PPIPrinterX = 0; | |
142 | m_PPIPrinterY = 0; | |
143 | m_isPreview = FALSE; | |
c801d85f KB |
144 | } |
145 | ||
34da0970 | 146 | wxPrintout::~wxPrintout() |
c801d85f | 147 | { |
c801d85f KB |
148 | } |
149 | ||
150 | bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage), int WXUNUSED(endPage)) | |
151 | { | |
1a5a8367 | 152 | return GetDC()->StartDoc(_("Printing")); |
c801d85f KB |
153 | } |
154 | ||
34da0970 | 155 | void wxPrintout::OnEndDocument() |
c801d85f KB |
156 | { |
157 | GetDC()->EndDoc(); | |
158 | } | |
159 | ||
34da0970 | 160 | void wxPrintout::OnBeginPrinting() |
c801d85f KB |
161 | { |
162 | } | |
163 | ||
34da0970 | 164 | void wxPrintout::OnEndPrinting() |
c801d85f KB |
165 | { |
166 | } | |
167 | ||
168 | bool wxPrintout::HasPage(int page) | |
169 | { | |
170 | return (page == 1); | |
171 | } | |
172 | ||
173 | void wxPrintout::GetPageInfo(int *minPage, int *maxPage, int *fromPage, int *toPage) | |
174 | { | |
175 | *minPage = 1; | |
176 | *maxPage = 32000; | |
177 | *fromPage = 1; | |
178 | *toPage = 1; | |
179 | } | |
180 | ||
181 | /* | |
182 | * Preview canvas | |
183 | */ | |
184 | ||
185 | wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase *preview, wxWindow *parent, | |
186 | const wxPoint& pos, const wxSize& size, long style, const wxString& name): | |
187 | wxScrolledWindow(parent, -1, pos, size, style, name) | |
188 | { | |
34da0970 | 189 | m_printPreview = preview; |
c801d85f KB |
190 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE)); |
191 | ||
76ed8f8d | 192 | SetScrollbars(15, 18, 100, 100); |
c801d85f KB |
193 | } |
194 | ||
34da0970 | 195 | wxPreviewCanvas::~wxPreviewCanvas() |
c801d85f KB |
196 | { |
197 | } | |
198 | ||
199 | void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
200 | { | |
201 | wxPaintDC dc(this); | |
031b2a7b | 202 | PrepareDC( dc ); |
c801d85f | 203 | |
34da0970 | 204 | if (m_printPreview) |
c801d85f | 205 | { |
34da0970 | 206 | m_printPreview->PaintPage(this, dc); |
c801d85f KB |
207 | } |
208 | } | |
209 | ||
210 | // Responds to colour changes, and passes event on to children. | |
211 | void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event) | |
212 | { | |
213 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE)); | |
214 | Refresh(); | |
215 | ||
216 | // Propagate the event to the non-top-level children | |
217 | wxWindow::OnSysColourChanged(event); | |
218 | } | |
219 | ||
220 | /* | |
221 | * Preview control bar | |
222 | */ | |
223 | ||
224 | BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel) | |
225 | EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnClose) | |
226 | EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrint) | |
227 | EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPrevious) | |
228 | EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNext) | |
229 | EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoom) | |
230 | EVT_PAINT(wxPreviewControlBar::OnPaint) | |
231 | END_EVENT_TABLE() | |
232 | ||
233 | wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons, | |
234 | wxWindow *parent, const wxPoint& pos, const wxSize& size, | |
235 | long style, const wxString& name): | |
236 | wxPanel(parent, -1, pos, size, style, name) | |
237 | { | |
34da0970 JS |
238 | m_printPreview = preview; |
239 | m_closeButton = (wxButton *) NULL; | |
240 | m_nextPageButton = (wxButton *) NULL; | |
241 | m_previousPageButton = (wxButton *) NULL; | |
242 | m_printButton = (wxButton *) NULL; | |
243 | m_zoomControl = (wxChoice *) NULL; | |
244 | m_buttonFlags = buttons; | |
c801d85f KB |
245 | } |
246 | ||
34da0970 | 247 | wxPreviewControlBar::~wxPreviewControlBar() |
c801d85f KB |
248 | { |
249 | } | |
250 | ||
251 | void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
252 | { | |
253 | wxPaintDC dc(this); | |
254 | ||
255 | int w, h; | |
256 | GetSize(&w, &h); | |
257 | dc.SetPen(*wxBLACK_PEN); | |
258 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
259 | dc.DrawLine( 0, h-1, w, h-1 ); | |
260 | } | |
261 | ||
262 | void wxPreviewControlBar::OnClose(wxCommandEvent& WXUNUSED(event)) | |
263 | { | |
264 | wxPreviewFrame *frame = (wxPreviewFrame *)GetParent(); | |
265 | frame->Close(TRUE); | |
266 | } | |
267 | ||
268 | void wxPreviewControlBar::OnPrint(wxCommandEvent& WXUNUSED(event)) | |
269 | { | |
270 | wxPrintPreviewBase *preview = GetPrintPreview(); | |
271 | preview->Print(TRUE); | |
272 | } | |
273 | ||
274 | void wxPreviewControlBar::OnNext(wxCommandEvent& WXUNUSED(event)) | |
275 | { | |
276 | wxPrintPreviewBase *preview = GetPrintPreview(); | |
277 | if (preview) | |
278 | { | |
279 | int currentPage = preview->GetCurrentPage(); | |
280 | if ((preview->GetMaxPage() > 0) && | |
281 | (currentPage < preview->GetMaxPage()) && | |
282 | preview->GetPrintout()->HasPage(currentPage + 1)) | |
283 | { | |
284 | preview->SetCurrentPage(currentPage + 1); | |
285 | } | |
286 | } | |
287 | } | |
288 | ||
289 | void wxPreviewControlBar::OnPrevious(wxCommandEvent& WXUNUSED(event)) | |
290 | { | |
291 | wxPrintPreviewBase *preview = GetPrintPreview(); | |
292 | if (preview) | |
293 | { | |
294 | int currentPage = preview->GetCurrentPage(); | |
295 | if ((preview->GetMinPage() > 0) && | |
296 | (currentPage > preview->GetMinPage()) && | |
297 | preview->GetPrintout()->HasPage(currentPage - 1)) | |
298 | { | |
299 | preview->SetCurrentPage(currentPage - 1); | |
300 | } | |
301 | } | |
302 | } | |
303 | ||
304 | void wxPreviewControlBar::OnZoom(wxCommandEvent& WXUNUSED(event)) | |
305 | { | |
306 | int zoom = GetZoomControl(); | |
307 | if (GetPrintPreview()) | |
308 | GetPrintPreview()->SetZoom(zoom); | |
309 | } | |
310 | ||
34da0970 | 311 | void wxPreviewControlBar::CreateButtons() |
c801d85f | 312 | { |
c801d85f KB |
313 | SetSize(0, 0, 400, 40); |
314 | ||
2049ba38 | 315 | #ifdef __WXMSW__ |
c801d85f KB |
316 | int fontSize = 9; |
317 | #else | |
318 | int fontSize = 10; | |
319 | #endif | |
320 | ||
34da0970 | 321 | wxFont buttonFont(fontSize, wxSWISS, wxNORMAL, wxBOLD); |
a4294b78 | 322 | SetFont(buttonFont); |
c801d85f KB |
323 | |
324 | int buttonWidth = 65; | |
031b2a7b RR |
325 | #ifdef __WXGTK__ |
326 | int buttonHeight = -1; | |
327 | #else | |
328 | int buttonHeight = 24; | |
329 | #endif | |
c801d85f KB |
330 | |
331 | int x = 5; | |
332 | int y = 5; | |
333 | int gap = 5; | |
334 | ||
34da0970 | 335 | m_closeButton = new wxButton(this, wxID_PREVIEW_CLOSE, _("Close"), |
c801d85f KB |
336 | wxPoint(x, y), wxSize(buttonWidth, buttonHeight)); |
337 | ||
338 | x += gap + buttonWidth; | |
339 | ||
34da0970 | 340 | if (m_buttonFlags & wxPREVIEW_PRINT) |
c801d85f | 341 | { |
34da0970 | 342 | m_printButton = new wxButton(this, wxID_PREVIEW_PRINT, _("Print..."), wxPoint(x, y), |
c801d85f KB |
343 | wxSize(buttonWidth, buttonHeight)); |
344 | x += gap + buttonWidth; | |
345 | } | |
346 | ||
34da0970 | 347 | if (m_buttonFlags & wxPREVIEW_PREVIOUS) |
c801d85f | 348 | { |
34da0970 | 349 | m_previousPageButton = new wxButton(this, wxID_PREVIEW_PREVIOUS, "<<", wxPoint(x, y), |
c801d85f KB |
350 | wxSize(buttonWidth, buttonHeight)); |
351 | x += gap + buttonWidth; | |
352 | } | |
353 | ||
34da0970 | 354 | if (m_buttonFlags & wxPREVIEW_NEXT) |
c801d85f | 355 | { |
34da0970 | 356 | m_nextPageButton = new wxButton(this, wxID_PREVIEW_NEXT, ">>", |
c801d85f KB |
357 | wxPoint(x, y), wxSize(buttonWidth, buttonHeight)); |
358 | x += gap + buttonWidth; | |
359 | } | |
360 | ||
031b2a7b RR |
361 | // Yes, this look stupid, but this is because gcc gives up otherwise. |
362 | wxString *choices = new wxString[23]; | |
363 | choices[0] = "10%"; | |
364 | choices[1] = "15%"; | |
365 | choices[2] = "20%"; | |
366 | choices[3] = "25%"; | |
367 | choices[4] = "30%"; | |
368 | choices[5] = "35%"; | |
369 | choices[6] = "40%"; | |
370 | choices[7] = "45%"; | |
371 | choices[8] = "50%"; | |
372 | choices[9] = "55%"; | |
373 | choices[10] = "60%"; | |
374 | choices[11] = "65%"; | |
375 | choices[12] = "70%"; | |
376 | choices[13] = "75%"; | |
377 | choices[14] = "80%"; | |
378 | choices[15] = "85%"; | |
379 | choices[16] = "90%"; | |
380 | choices[17] = "95%"; | |
381 | choices[18] = "100%"; | |
382 | choices[19] = "110%"; | |
383 | choices[20] = "120%"; | |
384 | choices[21] = "150%"; | |
385 | choices[22] = "200%"; | |
386 | ||
387 | int n = 23; | |
34da0970 | 388 | if (m_buttonFlags & wxPREVIEW_ZOOM) |
c801d85f | 389 | { |
34da0970 | 390 | m_zoomControl = new wxChoice(this, wxID_PREVIEW_ZOOM, wxPoint(x, y), |
c801d85f | 391 | wxSize(100, -1), n, (wxString *)choices); |
34da0970 | 392 | SetZoomControl(m_printPreview->GetZoom()); |
c801d85f KB |
393 | } |
394 | ||
031b2a7b | 395 | delete[] choices; |
a4294b78 | 396 | |
031b2a7b | 397 | m_closeButton->SetDefault(); |
c801d85f KB |
398 | } |
399 | ||
400 | void wxPreviewControlBar::SetZoomControl(int zoom) | |
401 | { | |
c801d85f KB |
402 | char buf[20]; |
403 | sprintf(buf, "%d%%", zoom); | |
34da0970 JS |
404 | if (m_zoomControl) |
405 | m_zoomControl->SetStringSelection(buf); | |
c801d85f KB |
406 | } |
407 | ||
34da0970 | 408 | int wxPreviewControlBar::GetZoomControl() |
c801d85f | 409 | { |
c801d85f | 410 | char buf[20]; |
34da0970 | 411 | if (m_zoomControl && m_zoomControl->GetStringSelection()) |
c801d85f | 412 | { |
34da0970 | 413 | strcpy(buf, m_zoomControl->GetStringSelection()); |
c801d85f KB |
414 | buf[strlen(buf) - 1] = 0; |
415 | return (int)atoi(buf); | |
416 | } | |
417 | else return 0; | |
c801d85f KB |
418 | } |
419 | ||
420 | ||
421 | /* | |
422 | * Preview frame | |
423 | */ | |
424 | ||
425 | wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxFrame *parent, const wxString& title, | |
426 | const wxPoint& pos, const wxSize& size, long style, const wxString& name): | |
427 | wxFrame(parent, -1, title, pos, size, style, name) | |
428 | { | |
34da0970 JS |
429 | m_printPreview = preview; |
430 | m_controlBar = NULL; | |
431 | m_previewCanvas = NULL; | |
c801d85f KB |
432 | } |
433 | ||
34da0970 | 434 | wxPreviewFrame::~wxPreviewFrame() |
c801d85f KB |
435 | { |
436 | } | |
437 | ||
34da0970 | 438 | bool wxPreviewFrame::OnClose() |
c801d85f | 439 | { |
c801d85f KB |
440 | MakeModal(FALSE); |
441 | ||
442 | // Need to delete the printout and the print preview | |
34da0970 | 443 | wxPrintout *printout = m_printPreview->GetPrintout(); |
c801d85f KB |
444 | if (printout) |
445 | { | |
446 | delete printout; | |
34da0970 JS |
447 | m_printPreview->SetPrintout(NULL); |
448 | m_printPreview->SetCanvas(NULL); | |
449 | m_printPreview->SetFrame(NULL); | |
c801d85f | 450 | } |
34da0970 | 451 | delete m_printPreview; |
c801d85f | 452 | return TRUE; |
c801d85f KB |
453 | } |
454 | ||
34da0970 | 455 | void wxPreviewFrame::Initialize() |
c801d85f | 456 | { |
c801d85f KB |
457 | CreateStatusBar(); |
458 | ||
459 | CreateCanvas(); | |
460 | CreateControlBar(); | |
461 | ||
34da0970 JS |
462 | m_printPreview->SetCanvas(m_previewCanvas); |
463 | m_printPreview->SetFrame(this); | |
c801d85f KB |
464 | |
465 | // Set layout constraints here | |
466 | ||
467 | // Control bar constraints | |
468 | wxLayoutConstraints *c1 = new wxLayoutConstraints; | |
469 | // int w, h; | |
34da0970 | 470 | // m_controlBar->GetSize(&w, &h); |
c801d85f | 471 | int h; |
031b2a7b | 472 | #if (defined(__WXMSW__) || defined(__WXGTK__)) |
c801d85f KB |
473 | h = 40; |
474 | #else | |
475 | h = 60; | |
476 | #endif | |
477 | ||
478 | c1->left.SameAs (this, wxLeft); | |
479 | c1->top.SameAs (this, wxTop); | |
480 | c1->right.SameAs (this, wxRight); | |
481 | c1->height.Absolute (h); | |
482 | ||
34da0970 | 483 | m_controlBar->SetConstraints(c1); |
c801d85f KB |
484 | |
485 | // Canvas constraints | |
486 | wxLayoutConstraints *c2 = new wxLayoutConstraints; | |
487 | ||
488 | c2->left.SameAs (this, wxLeft); | |
34da0970 | 489 | c2->top.Below (m_controlBar); |
c801d85f KB |
490 | c2->right.SameAs (this, wxRight); |
491 | c2->bottom.SameAs (this, wxBottom); | |
492 | ||
34da0970 | 493 | m_previewCanvas->SetConstraints(c2); |
c801d85f KB |
494 | |
495 | SetAutoLayout(TRUE); | |
496 | ||
497 | MakeModal(TRUE); | |
498 | ||
499 | Layout(); | |
c801d85f KB |
500 | } |
501 | ||
34da0970 | 502 | void wxPreviewFrame::CreateCanvas() |
c801d85f | 503 | { |
34da0970 | 504 | m_previewCanvas = new wxPreviewCanvas(m_printPreview, this); |
c801d85f KB |
505 | } |
506 | ||
34da0970 | 507 | void wxPreviewFrame::CreateControlBar() |
c801d85f | 508 | { |
c801d85f | 509 | long buttons = wxPREVIEW_DEFAULT; |
34da0970 | 510 | if (m_printPreview->GetPrintoutForPrinting()) |
c801d85f KB |
511 | buttons |= wxPREVIEW_PRINT; |
512 | ||
34da0970 JS |
513 | m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0, 0), wxSize(400, 40)); |
514 | m_controlBar->CreateButtons(); | |
c801d85f KB |
515 | } |
516 | ||
517 | /* | |
518 | * Print preview | |
519 | */ | |
520 | ||
521 | wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data) | |
522 | { | |
34da0970 JS |
523 | m_isOk = TRUE; |
524 | m_previewPrintout = printout; | |
525 | if (m_previewPrintout) | |
526 | m_previewPrintout->SetIsPreview(TRUE); | |
c801d85f | 527 | |
34da0970 | 528 | m_printPrintout = printoutForPrinting; |
c801d85f | 529 | if (data) |
34da0970 JS |
530 | m_printData = (*data); |
531 | ||
532 | m_previewCanvas = NULL; | |
533 | m_previewFrame = NULL; | |
534 | m_previewBitmap = NULL; | |
535 | m_currentPage = 1; | |
536 | m_currentZoom = 30; | |
537 | m_topMargin = 40; | |
538 | m_leftMargin = 40; | |
539 | m_pageWidth = 0; | |
540 | m_pageHeight = 0; | |
c801d85f KB |
541 | |
542 | printout->OnPreparePrinting(); | |
543 | ||
544 | // Get some parameters from the printout, if defined | |
545 | int selFrom, selTo; | |
34da0970 | 546 | printout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo); |
c801d85f KB |
547 | } |
548 | ||
34da0970 | 549 | wxPrintPreviewBase::~wxPrintPreviewBase() |
c801d85f | 550 | { |
34da0970 JS |
551 | if (m_previewPrintout) |
552 | delete m_previewPrintout; | |
553 | if (m_previewBitmap) | |
554 | delete m_previewBitmap; | |
555 | if (m_printPrintout) | |
556 | delete m_printPrintout; | |
c801d85f KB |
557 | } |
558 | ||
559 | bool wxPrintPreviewBase::SetCurrentPage(int pageNum) | |
560 | { | |
34da0970 | 561 | if (m_currentPage == pageNum) |
c801d85f KB |
562 | return TRUE; |
563 | ||
34da0970 JS |
564 | m_currentPage = pageNum; |
565 | if (m_previewBitmap) | |
c801d85f | 566 | { |
34da0970 JS |
567 | delete m_previewBitmap; |
568 | m_previewBitmap = NULL; | |
c801d85f KB |
569 | } |
570 | ||
34da0970 | 571 | if (m_previewCanvas) |
c801d85f KB |
572 | { |
573 | RenderPage(pageNum); | |
34da0970 | 574 | m_previewCanvas->Refresh(); |
c801d85f | 575 | } |
c801d85f KB |
576 | return TRUE; |
577 | } | |
578 | ||
579 | bool wxPrintPreviewBase::PaintPage(wxWindow *canvas, wxDC& dc) | |
580 | { | |
c801d85f KB |
581 | DrawBlankPage(canvas, dc); |
582 | ||
34da0970 JS |
583 | if (!m_previewBitmap) |
584 | RenderPage(m_currentPage); | |
c801d85f | 585 | |
34da0970 | 586 | if (!m_previewBitmap) |
c801d85f KB |
587 | return FALSE; |
588 | ||
589 | if (!canvas) | |
590 | return FALSE; | |
591 | ||
592 | int canvasWidth, canvasHeight; | |
593 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
594 | ||
34da0970 JS |
595 | double zoomScale = ((float)m_currentZoom/(float)100); |
596 | double actualWidth = (zoomScale*m_pageWidth*m_previewScale); | |
597 | // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale); | |
c801d85f | 598 | |
341287bf | 599 | int x = (int) ((canvasWidth - actualWidth)/2.0); |
34da0970 JS |
600 | if (x < m_leftMargin) |
601 | x = m_leftMargin; | |
602 | int y = m_topMargin; | |
c801d85f KB |
603 | |
604 | wxMemoryDC temp_dc; | |
34da0970 | 605 | temp_dc.SelectObject(*m_previewBitmap); |
c801d85f | 606 | |
34da0970 | 607 | dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0); |
c801d85f KB |
608 | |
609 | temp_dc.SelectObject(wxNullBitmap); | |
610 | ||
c801d85f KB |
611 | return TRUE; |
612 | } | |
613 | ||
614 | bool wxPrintPreviewBase::RenderPage(int pageNum) | |
615 | { | |
616 | int canvasWidth, canvasHeight; | |
617 | ||
34da0970 | 618 | if (!m_previewCanvas) |
c801d85f | 619 | { |
1a5a8367 DP |
620 | wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"), |
621 | _("Print Preview Failure"), wxOK); | |
c801d85f KB |
622 | return FALSE; |
623 | } | |
34da0970 | 624 | m_previewCanvas->GetSize(&canvasWidth, &canvasHeight); |
c801d85f | 625 | |
34da0970 JS |
626 | double zoomScale = (m_currentZoom/100.0); |
627 | int actualWidth = (int)(zoomScale*m_pageWidth*m_previewScale); | |
628 | int actualHeight = (int)(zoomScale*m_pageHeight*m_previewScale); | |
c801d85f | 629 | |
34da0970 JS |
630 | int x = (int)((canvasWidth - actualWidth)/2.0); |
631 | if (x < m_leftMargin) | |
632 | x = m_leftMargin; | |
633 | // int y = m_topMargin; | |
c801d85f KB |
634 | |
635 | ||
34da0970 | 636 | if (!m_previewBitmap) |
c801d85f | 637 | { |
34da0970 JS |
638 | m_previewBitmap = new wxBitmap((int)actualWidth, (int)actualHeight); |
639 | if (!m_previewBitmap || !m_previewBitmap->Ok()) | |
c801d85f | 640 | { |
34da0970 JS |
641 | if (m_previewBitmap) |
642 | delete m_previewBitmap; | |
1a5a8367 | 643 | wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK); |
c801d85f KB |
644 | return FALSE; |
645 | } | |
646 | } | |
647 | ||
648 | wxMemoryDC memoryDC; | |
34da0970 | 649 | memoryDC.SelectObject(*m_previewBitmap); |
c801d85f KB |
650 | |
651 | memoryDC.Clear(); | |
652 | ||
34da0970 JS |
653 | m_previewPrintout->SetDC(&memoryDC); |
654 | m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight); | |
c801d85f | 655 | |
34da0970 | 656 | m_previewPrintout->OnBeginPrinting(); |
c801d85f KB |
657 | |
658 | ||
34da0970 | 659 | if (!m_previewPrintout->OnBeginDocument(m_printData.GetFromPage(), m_printData.GetToPage())) |
c801d85f | 660 | { |
1a5a8367 | 661 | wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK); |
c801d85f KB |
662 | |
663 | memoryDC.SelectObject(wxNullBitmap); | |
664 | ||
34da0970 | 665 | delete m_previewBitmap; |
c801d85f KB |
666 | return FALSE; |
667 | } | |
668 | ||
34da0970 JS |
669 | m_previewPrintout->OnPrintPage(pageNum); |
670 | m_previewPrintout->OnEndDocument(); | |
671 | m_previewPrintout->OnEndPrinting(); | |
c801d85f | 672 | |
34da0970 | 673 | m_previewPrintout->SetDC(NULL); |
c801d85f KB |
674 | |
675 | memoryDC.SelectObject(wxNullBitmap); | |
c801d85f KB |
676 | |
677 | char buf[200]; | |
34da0970 JS |
678 | if (m_maxPage != 0) |
679 | sprintf(buf, _("Page %d of %d"), pageNum, m_maxPage); | |
c801d85f | 680 | else |
1a5a8367 | 681 | sprintf(buf, _("Page %d"), pageNum); |
c801d85f | 682 | |
34da0970 JS |
683 | if (m_previewFrame) |
684 | m_previewFrame->SetStatusText(buf); | |
c801d85f KB |
685 | |
686 | return TRUE; | |
687 | } | |
688 | ||
689 | ||
690 | bool wxPrintPreviewBase::DrawBlankPage(wxWindow *canvas, wxDC& dc) | |
691 | { | |
c801d85f KB |
692 | int canvasWidth, canvasHeight; |
693 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
694 | ||
34da0970 JS |
695 | float zoomScale = (float)((float)m_currentZoom/(float)100); |
696 | float actualWidth = zoomScale*m_pageWidth*m_previewScale; | |
697 | float actualHeight = zoomScale*m_pageHeight*m_previewScale; | |
c801d85f KB |
698 | |
699 | float x = (float)((canvasWidth - actualWidth)/2.0); | |
34da0970 JS |
700 | if (x < m_leftMargin) |
701 | x = (float)m_leftMargin; | |
702 | float y = (float)m_topMargin; | |
c801d85f KB |
703 | |
704 | // Draw shadow, allowing for 1-pixel border AROUND the actual page | |
705 | int shadowOffset = 4; | |
706 | dc.SetPen(*wxBLACK_PEN); | |
707 | dc.SetBrush(*wxBLACK_BRUSH); | |
76ed8f8d | 708 | /* |
a4294b78 | 709 | dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2)); |
76ed8f8d | 710 | */ |
ed880dd4 | 711 | dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset); |
76ed8f8d | 712 | dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight)); |
c801d85f KB |
713 | |
714 | // Draw blank page allowing for 1-pixel border AROUND the actual page | |
715 | dc.SetPen(*wxBLACK_PEN); | |
716 | dc.SetBrush(*wxWHITE_BRUSH); | |
c801d85f | 717 | |
76ed8f8d RR |
718 | /* |
719 | wxRegion update_region = canvas->GetUpdateRegion(); | |
720 | wxRect r = update_region.GetBox(); | |
721 | ||
722 | printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height ); | |
723 | */ | |
724 | ||
ed880dd4 | 725 | dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2)); |
c801d85f | 726 | |
c801d85f KB |
727 | return TRUE; |
728 | } | |
729 | ||
730 | void wxPrintPreviewBase::SetZoom(int percent) | |
731 | { | |
34da0970 | 732 | if (m_currentZoom == percent) |
c801d85f KB |
733 | return; |
734 | ||
34da0970 JS |
735 | m_currentZoom = percent; |
736 | if (m_previewBitmap) | |
c801d85f | 737 | { |
34da0970 JS |
738 | delete m_previewBitmap; |
739 | m_previewBitmap = NULL; | |
c801d85f | 740 | } |
34da0970 | 741 | RenderPage(m_currentPage); |
c801d85f | 742 | |
34da0970 | 743 | if (m_previewCanvas) |
c801d85f | 744 | { |
34da0970 JS |
745 | m_previewCanvas->Clear(); |
746 | m_previewCanvas->Refresh(); | |
c801d85f | 747 | } |
c801d85f | 748 | } |