]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
46 | #ifdef __WXMSW__ | |
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 | ||
59 | #endif | |
60 | // End __WXMSW__ | |
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 | { | |
86 | m_currentPrintout = (wxPrintout *) NULL; | |
87 | sm_abortWindow = (wxWindow *) NULL; | |
88 | sm_abortIt = FALSE; | |
89 | if (data) | |
90 | m_printData = (*data); | |
91 | } | |
92 | ||
93 | wxWindow *wxPrinterBase::sm_abortWindow = (wxWindow *) NULL; | |
94 | bool wxPrinterBase::sm_abortIt = FALSE; | |
95 | ||
96 | wxPrinterBase::~wxPrinterBase() | |
97 | { | |
98 | } | |
99 | ||
100 | void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) | |
101 | { | |
102 | wxPrinterBase::sm_abortIt = TRUE; | |
103 | wxPrinterBase::sm_abortWindow->Show(FALSE); | |
104 | wxPrinterBase::sm_abortWindow->Close(TRUE); | |
105 | wxPrinterBase::sm_abortWindow = (wxWindow *) NULL; | |
106 | } | |
107 | ||
108 | wxWindow *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout *WXUNUSED(printout)) | |
109 | { | |
110 | wxPrintAbortDialog *dialog = new wxPrintAbortDialog(parent, _("Printing"), wxPoint(0, 0), wxSize(400, 400), wxDEFAULT_DIALOG_STYLE); | |
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 | { | |
124 | wxMessageBox(message, _("Printing Error"), wxOK, parent); | |
125 | } | |
126 | ||
127 | /* | |
128 | * Printout class | |
129 | */ | |
130 | ||
131 | wxPrintout::wxPrintout(const wxString& title) | |
132 | { | |
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; | |
144 | } | |
145 | ||
146 | wxPrintout::~wxPrintout() | |
147 | { | |
148 | } | |
149 | ||
150 | bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage), int WXUNUSED(endPage)) | |
151 | { | |
152 | return GetDC()->StartDoc(_("Printing")); | |
153 | } | |
154 | ||
155 | void wxPrintout::OnEndDocument() | |
156 | { | |
157 | GetDC()->EndDoc(); | |
158 | } | |
159 | ||
160 | void wxPrintout::OnBeginPrinting() | |
161 | { | |
162 | } | |
163 | ||
164 | void wxPrintout::OnEndPrinting() | |
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 | { | |
189 | m_printPreview = preview; | |
190 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE)); | |
191 | ||
192 | SetScrollbars(15, 18, 100, 100); | |
193 | } | |
194 | ||
195 | wxPreviewCanvas::~wxPreviewCanvas() | |
196 | { | |
197 | } | |
198 | ||
199 | void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
200 | { | |
201 | wxPaintDC dc(this); | |
202 | PrepareDC( dc ); | |
203 | ||
204 | if (m_printPreview) | |
205 | { | |
206 | m_printPreview->PaintPage(this, dc); | |
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::OnWindowClose) | |
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 | { | |
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; | |
245 | } | |
246 | ||
247 | wxPreviewControlBar::~wxPreviewControlBar() | |
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::OnWindowClose(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 | ||
311 | void wxPreviewControlBar::CreateButtons() | |
312 | { | |
313 | SetSize(0, 0, 400, 40); | |
314 | ||
315 | /* | |
316 | #ifdef __WXMSW__ | |
317 | int fontSize = 9; | |
318 | #else | |
319 | int fontSize = 10; | |
320 | #endif | |
321 | ||
322 | wxFont buttonFont(fontSize, wxSWISS, wxNORMAL, wxBOLD); | |
323 | SetFont(buttonFont); | |
324 | */ | |
325 | ||
326 | int buttonWidth = 65; | |
327 | #ifdef __WXGTK__ | |
328 | int buttonHeight = -1; | |
329 | #else | |
330 | int buttonHeight = 24; | |
331 | #endif | |
332 | ||
333 | int x = 5; | |
334 | int y = 5; | |
335 | ||
336 | #ifdef __WXMOTIF__ | |
337 | int gap = 15; | |
338 | #else | |
339 | int gap = 5; | |
340 | #endif | |
341 | ||
342 | m_closeButton = new wxButton(this, wxID_PREVIEW_CLOSE, _("Close"), | |
343 | wxPoint(x, y), wxSize(buttonWidth, buttonHeight)); | |
344 | ||
345 | x += gap + buttonWidth; | |
346 | ||
347 | if (m_buttonFlags & wxPREVIEW_PRINT) | |
348 | { | |
349 | m_printButton = new wxButton(this, wxID_PREVIEW_PRINT, _("Print..."), wxPoint(x, y), | |
350 | wxSize(buttonWidth, buttonHeight)); | |
351 | x += gap + buttonWidth; | |
352 | } | |
353 | ||
354 | if (m_buttonFlags & wxPREVIEW_PREVIOUS) | |
355 | { | |
356 | m_previousPageButton = new wxButton(this, wxID_PREVIEW_PREVIOUS, "<<", wxPoint(x, y), | |
357 | wxSize(buttonWidth, buttonHeight)); | |
358 | x += gap + buttonWidth; | |
359 | } | |
360 | ||
361 | if (m_buttonFlags & wxPREVIEW_NEXT) | |
362 | { | |
363 | m_nextPageButton = new wxButton(this, wxID_PREVIEW_NEXT, ">>", | |
364 | wxPoint(x, y), wxSize(buttonWidth, buttonHeight)); | |
365 | x += gap + buttonWidth; | |
366 | } | |
367 | ||
368 | // Yes, this look stupid, but this is because gcc gives up otherwise. | |
369 | wxString *choices = new wxString[23]; | |
370 | choices[0] = "10%"; | |
371 | choices[1] = "15%"; | |
372 | choices[2] = "20%"; | |
373 | choices[3] = "25%"; | |
374 | choices[4] = "30%"; | |
375 | choices[5] = "35%"; | |
376 | choices[6] = "40%"; | |
377 | choices[7] = "45%"; | |
378 | choices[8] = "50%"; | |
379 | choices[9] = "55%"; | |
380 | choices[10] = "60%"; | |
381 | choices[11] = "65%"; | |
382 | choices[12] = "70%"; | |
383 | choices[13] = "75%"; | |
384 | choices[14] = "80%"; | |
385 | choices[15] = "85%"; | |
386 | choices[16] = "90%"; | |
387 | choices[17] = "95%"; | |
388 | choices[18] = "100%"; | |
389 | choices[19] = "110%"; | |
390 | choices[20] = "120%"; | |
391 | choices[21] = "150%"; | |
392 | choices[22] = "200%"; | |
393 | ||
394 | int n = 23; | |
395 | if (m_buttonFlags & wxPREVIEW_ZOOM) | |
396 | { | |
397 | m_zoomControl = new wxChoice(this, wxID_PREVIEW_ZOOM, wxPoint(x, y), | |
398 | wxSize(100, -1), n, (wxString *)choices); | |
399 | SetZoomControl(m_printPreview->GetZoom()); | |
400 | } | |
401 | ||
402 | delete[] choices; | |
403 | ||
404 | // m_closeButton->SetDefault(); | |
405 | } | |
406 | ||
407 | void wxPreviewControlBar::SetZoomControl(int zoom) | |
408 | { | |
409 | char buf[20]; | |
410 | sprintf(buf, "%d%%", zoom); | |
411 | if (m_zoomControl) | |
412 | m_zoomControl->SetStringSelection(buf); | |
413 | } | |
414 | ||
415 | int wxPreviewControlBar::GetZoomControl() | |
416 | { | |
417 | char buf[20]; | |
418 | if (m_zoomControl && (m_zoomControl->GetStringSelection() != "")) | |
419 | { | |
420 | strcpy(buf, m_zoomControl->GetStringSelection()); | |
421 | buf[strlen(buf) - 1] = 0; | |
422 | return (int)atoi(buf); | |
423 | } | |
424 | else return 0; | |
425 | } | |
426 | ||
427 | ||
428 | /* | |
429 | * Preview frame | |
430 | */ | |
431 | ||
432 | wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxFrame *parent, const wxString& title, | |
433 | const wxPoint& pos, const wxSize& size, long style, const wxString& name): | |
434 | wxFrame(parent, -1, title, pos, size, style, name) | |
435 | { | |
436 | m_printPreview = preview; | |
437 | m_controlBar = NULL; | |
438 | m_previewCanvas = NULL; | |
439 | } | |
440 | ||
441 | wxPreviewFrame::~wxPreviewFrame() | |
442 | { | |
443 | } | |
444 | ||
445 | bool wxPreviewFrame::OnClose() | |
446 | { | |
447 | MakeModal(FALSE); | |
448 | ||
449 | // Need to delete the printout and the print preview | |
450 | wxPrintout *printout = m_printPreview->GetPrintout(); | |
451 | if (printout) | |
452 | { | |
453 | delete printout; | |
454 | m_printPreview->SetPrintout(NULL); | |
455 | m_printPreview->SetCanvas(NULL); | |
456 | m_printPreview->SetFrame(NULL); | |
457 | } | |
458 | delete m_printPreview; | |
459 | return TRUE; | |
460 | } | |
461 | ||
462 | void wxPreviewFrame::Initialize() | |
463 | { | |
464 | CreateStatusBar(); | |
465 | ||
466 | CreateCanvas(); | |
467 | CreateControlBar(); | |
468 | ||
469 | m_printPreview->SetCanvas(m_previewCanvas); | |
470 | m_printPreview->SetFrame(this); | |
471 | ||
472 | // Set layout constraints here | |
473 | ||
474 | // Control bar constraints | |
475 | wxLayoutConstraints *c1 = new wxLayoutConstraints; | |
476 | // int w, h; | |
477 | // m_controlBar->GetSize(&w, &h); | |
478 | int h; | |
479 | #if (defined(__WXMSW__) || defined(__WXGTK__)) | |
480 | h = 40; | |
481 | #else | |
482 | h = 60; | |
483 | #endif | |
484 | ||
485 | c1->left.SameAs (this, wxLeft); | |
486 | c1->top.SameAs (this, wxTop); | |
487 | c1->right.SameAs (this, wxRight); | |
488 | c1->height.Absolute (h); | |
489 | ||
490 | m_controlBar->SetConstraints(c1); | |
491 | ||
492 | // Canvas constraints | |
493 | wxLayoutConstraints *c2 = new wxLayoutConstraints; | |
494 | ||
495 | c2->left.SameAs (this, wxLeft); | |
496 | c2->top.Below (m_controlBar); | |
497 | c2->right.SameAs (this, wxRight); | |
498 | c2->bottom.SameAs (this, wxBottom); | |
499 | ||
500 | m_previewCanvas->SetConstraints(c2); | |
501 | ||
502 | SetAutoLayout(TRUE); | |
503 | ||
504 | MakeModal(TRUE); | |
505 | ||
506 | Layout(); | |
507 | } | |
508 | ||
509 | void wxPreviewFrame::CreateCanvas() | |
510 | { | |
511 | m_previewCanvas = new wxPreviewCanvas(m_printPreview, this); | |
512 | } | |
513 | ||
514 | void wxPreviewFrame::CreateControlBar() | |
515 | { | |
516 | long buttons = wxPREVIEW_DEFAULT; | |
517 | if (m_printPreview->GetPrintoutForPrinting()) | |
518 | buttons |= wxPREVIEW_PRINT; | |
519 | ||
520 | m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0, 0), wxSize(400, 40)); | |
521 | m_controlBar->CreateButtons(); | |
522 | } | |
523 | ||
524 | /* | |
525 | * Print preview | |
526 | */ | |
527 | ||
528 | wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data) | |
529 | { | |
530 | m_isOk = TRUE; | |
531 | m_previewPrintout = printout; | |
532 | if (m_previewPrintout) | |
533 | m_previewPrintout->SetIsPreview(TRUE); | |
534 | ||
535 | m_printPrintout = printoutForPrinting; | |
536 | if (data) | |
537 | m_printData = (*data); | |
538 | ||
539 | m_previewCanvas = NULL; | |
540 | m_previewFrame = NULL; | |
541 | m_previewBitmap = NULL; | |
542 | m_currentPage = 1; | |
543 | m_currentZoom = 30; | |
544 | m_topMargin = 40; | |
545 | m_leftMargin = 40; | |
546 | m_pageWidth = 0; | |
547 | m_pageHeight = 0; | |
548 | ||
549 | printout->OnPreparePrinting(); | |
550 | ||
551 | // Get some parameters from the printout, if defined | |
552 | int selFrom, selTo; | |
553 | printout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo); | |
554 | } | |
555 | ||
556 | wxPrintPreviewBase::~wxPrintPreviewBase() | |
557 | { | |
558 | if (m_previewPrintout) | |
559 | delete m_previewPrintout; | |
560 | if (m_previewBitmap) | |
561 | delete m_previewBitmap; | |
562 | if (m_printPrintout) | |
563 | delete m_printPrintout; | |
564 | } | |
565 | ||
566 | bool wxPrintPreviewBase::SetCurrentPage(int pageNum) | |
567 | { | |
568 | if (m_currentPage == pageNum) | |
569 | return TRUE; | |
570 | ||
571 | m_currentPage = pageNum; | |
572 | if (m_previewBitmap) | |
573 | { | |
574 | delete m_previewBitmap; | |
575 | m_previewBitmap = NULL; | |
576 | } | |
577 | ||
578 | if (m_previewCanvas) | |
579 | { | |
580 | RenderPage(pageNum); | |
581 | m_previewCanvas->Refresh(); | |
582 | } | |
583 | return TRUE; | |
584 | } | |
585 | ||
586 | bool wxPrintPreviewBase::PaintPage(wxWindow *canvas, wxDC& dc) | |
587 | { | |
588 | DrawBlankPage(canvas, dc); | |
589 | ||
590 | if (!m_previewBitmap) | |
591 | RenderPage(m_currentPage); | |
592 | ||
593 | if (!m_previewBitmap) | |
594 | return FALSE; | |
595 | ||
596 | if (!canvas) | |
597 | return FALSE; | |
598 | ||
599 | int canvasWidth, canvasHeight; | |
600 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
601 | ||
602 | double zoomScale = ((float)m_currentZoom/(float)100); | |
603 | double actualWidth = (zoomScale*m_pageWidth*m_previewScale); | |
604 | // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale); | |
605 | ||
606 | int x = (int) ((canvasWidth - actualWidth)/2.0); | |
607 | if (x < m_leftMargin) | |
608 | x = m_leftMargin; | |
609 | int y = m_topMargin; | |
610 | ||
611 | wxMemoryDC temp_dc; | |
612 | temp_dc.SelectObject(*m_previewBitmap); | |
613 | ||
614 | dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0); | |
615 | ||
616 | temp_dc.SelectObject(wxNullBitmap); | |
617 | ||
618 | return TRUE; | |
619 | } | |
620 | ||
621 | bool wxPrintPreviewBase::RenderPage(int pageNum) | |
622 | { | |
623 | int canvasWidth, canvasHeight; | |
624 | ||
625 | if (!m_previewCanvas) | |
626 | { | |
627 | wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"), | |
628 | _("Print Preview Failure"), wxOK); | |
629 | return FALSE; | |
630 | } | |
631 | m_previewCanvas->GetSize(&canvasWidth, &canvasHeight); | |
632 | ||
633 | double zoomScale = (m_currentZoom/100.0); | |
634 | int actualWidth = (int)(zoomScale*m_pageWidth*m_previewScale); | |
635 | int actualHeight = (int)(zoomScale*m_pageHeight*m_previewScale); | |
636 | ||
637 | int x = (int)((canvasWidth - actualWidth)/2.0); | |
638 | if (x < m_leftMargin) | |
639 | x = m_leftMargin; | |
640 | // int y = m_topMargin; | |
641 | ||
642 | ||
643 | if (!m_previewBitmap) | |
644 | { | |
645 | m_previewBitmap = new wxBitmap((int)actualWidth, (int)actualHeight); | |
646 | if (!m_previewBitmap || !m_previewBitmap->Ok()) | |
647 | { | |
648 | if (m_previewBitmap) | |
649 | delete m_previewBitmap; | |
650 | wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK); | |
651 | return FALSE; | |
652 | } | |
653 | } | |
654 | ||
655 | wxMemoryDC memoryDC; | |
656 | memoryDC.SelectObject(*m_previewBitmap); | |
657 | ||
658 | memoryDC.Clear(); | |
659 | ||
660 | m_previewPrintout->SetDC(&memoryDC); | |
661 | m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight); | |
662 | ||
663 | m_previewPrintout->OnBeginPrinting(); | |
664 | ||
665 | ||
666 | if (!m_previewPrintout->OnBeginDocument(m_printData.GetFromPage(), m_printData.GetToPage())) | |
667 | { | |
668 | wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK); | |
669 | ||
670 | memoryDC.SelectObject(wxNullBitmap); | |
671 | ||
672 | delete m_previewBitmap; | |
673 | return FALSE; | |
674 | } | |
675 | ||
676 | m_previewPrintout->OnPrintPage(pageNum); | |
677 | m_previewPrintout->OnEndDocument(); | |
678 | m_previewPrintout->OnEndPrinting(); | |
679 | ||
680 | m_previewPrintout->SetDC(NULL); | |
681 | ||
682 | memoryDC.SelectObject(wxNullBitmap); | |
683 | ||
684 | char buf[200]; | |
685 | if (m_maxPage != 0) | |
686 | sprintf(buf, _("Page %d of %d"), pageNum, m_maxPage); | |
687 | else | |
688 | sprintf(buf, _("Page %d"), pageNum); | |
689 | ||
690 | if (m_previewFrame) | |
691 | m_previewFrame->SetStatusText(buf); | |
692 | ||
693 | return TRUE; | |
694 | } | |
695 | ||
696 | ||
697 | bool wxPrintPreviewBase::DrawBlankPage(wxWindow *canvas, wxDC& dc) | |
698 | { | |
699 | int canvasWidth, canvasHeight; | |
700 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
701 | ||
702 | float zoomScale = (float)((float)m_currentZoom/(float)100); | |
703 | float actualWidth = zoomScale*m_pageWidth*m_previewScale; | |
704 | float actualHeight = zoomScale*m_pageHeight*m_previewScale; | |
705 | ||
706 | float x = (float)((canvasWidth - actualWidth)/2.0); | |
707 | if (x < m_leftMargin) | |
708 | x = (float)m_leftMargin; | |
709 | float y = (float)m_topMargin; | |
710 | ||
711 | // Draw shadow, allowing for 1-pixel border AROUND the actual page | |
712 | int shadowOffset = 4; | |
713 | dc.SetPen(*wxBLACK_PEN); | |
714 | dc.SetBrush(*wxBLACK_BRUSH); | |
715 | /* | |
716 | dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2)); | |
717 | */ | |
718 | dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset); | |
719 | dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight)); | |
720 | ||
721 | // Draw blank page allowing for 1-pixel border AROUND the actual page | |
722 | dc.SetPen(*wxBLACK_PEN); | |
723 | dc.SetBrush(*wxWHITE_BRUSH); | |
724 | ||
725 | /* | |
726 | wxRegion update_region = canvas->GetUpdateRegion(); | |
727 | wxRect r = update_region.GetBox(); | |
728 | ||
729 | printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height ); | |
730 | */ | |
731 | ||
732 | dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2)); | |
733 | ||
734 | return TRUE; | |
735 | } | |
736 | ||
737 | void wxPrintPreviewBase::SetZoom(int percent) | |
738 | { | |
739 | if (m_currentZoom == percent) | |
740 | return; | |
741 | ||
742 | m_currentZoom = percent; | |
743 | if (m_previewBitmap) | |
744 | { | |
745 | delete m_previewBitmap; | |
746 | m_previewBitmap = NULL; | |
747 | } | |
748 | RenderPage(m_currentPage); | |
749 | ||
750 | if (m_previewCanvas) | |
751 | { | |
752 | m_previewCanvas->Clear(); | |
753 | m_previewCanvas->Refresh(); | |
754 | } | |
755 | } |