]>
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) | |
c330a2cf | 225 | EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose) |
c801d85f KB |
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 | ||
c330a2cf | 262 | void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event)) |
c801d85f KB |
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 | ||
9838df2c | 315 | /* |
2049ba38 | 316 | #ifdef __WXMSW__ |
c801d85f KB |
317 | int fontSize = 9; |
318 | #else | |
319 | int fontSize = 10; | |
320 | #endif | |
321 | ||
34da0970 | 322 | wxFont buttonFont(fontSize, wxSWISS, wxNORMAL, wxBOLD); |
a4294b78 | 323 | SetFont(buttonFont); |
9838df2c | 324 | */ |
c801d85f KB |
325 | |
326 | int buttonWidth = 65; | |
031b2a7b RR |
327 | #ifdef __WXGTK__ |
328 | int buttonHeight = -1; | |
329 | #else | |
330 | int buttonHeight = 24; | |
331 | #endif | |
c801d85f KB |
332 | |
333 | int x = 5; | |
334 | int y = 5; | |
9838df2c JS |
335 | |
336 | #ifdef __WXMOTIF__ | |
337 | int gap = 15; | |
338 | #else | |
c801d85f | 339 | int gap = 5; |
9838df2c | 340 | #endif |
c801d85f | 341 | |
34da0970 | 342 | m_closeButton = new wxButton(this, wxID_PREVIEW_CLOSE, _("Close"), |
c801d85f KB |
343 | wxPoint(x, y), wxSize(buttonWidth, buttonHeight)); |
344 | ||
345 | x += gap + buttonWidth; | |
346 | ||
34da0970 | 347 | if (m_buttonFlags & wxPREVIEW_PRINT) |
c801d85f | 348 | { |
34da0970 | 349 | m_printButton = new wxButton(this, wxID_PREVIEW_PRINT, _("Print..."), wxPoint(x, y), |
c801d85f KB |
350 | wxSize(buttonWidth, buttonHeight)); |
351 | x += gap + buttonWidth; | |
352 | } | |
353 | ||
34da0970 | 354 | if (m_buttonFlags & wxPREVIEW_PREVIOUS) |
c801d85f | 355 | { |
34da0970 | 356 | m_previousPageButton = new wxButton(this, wxID_PREVIEW_PREVIOUS, "<<", wxPoint(x, y), |
c801d85f KB |
357 | wxSize(buttonWidth, buttonHeight)); |
358 | x += gap + buttonWidth; | |
359 | } | |
360 | ||
34da0970 | 361 | if (m_buttonFlags & wxPREVIEW_NEXT) |
c801d85f | 362 | { |
34da0970 | 363 | m_nextPageButton = new wxButton(this, wxID_PREVIEW_NEXT, ">>", |
c801d85f KB |
364 | wxPoint(x, y), wxSize(buttonWidth, buttonHeight)); |
365 | x += gap + buttonWidth; | |
366 | } | |
367 | ||
031b2a7b RR |
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; | |
34da0970 | 395 | if (m_buttonFlags & wxPREVIEW_ZOOM) |
c801d85f | 396 | { |
34da0970 | 397 | m_zoomControl = new wxChoice(this, wxID_PREVIEW_ZOOM, wxPoint(x, y), |
c801d85f | 398 | wxSize(100, -1), n, (wxString *)choices); |
34da0970 | 399 | SetZoomControl(m_printPreview->GetZoom()); |
c801d85f KB |
400 | } |
401 | ||
031b2a7b | 402 | delete[] choices; |
a4294b78 | 403 | |
9838df2c | 404 | // m_closeButton->SetDefault(); |
c801d85f KB |
405 | } |
406 | ||
407 | void wxPreviewControlBar::SetZoomControl(int zoom) | |
408 | { | |
c801d85f KB |
409 | char buf[20]; |
410 | sprintf(buf, "%d%%", zoom); | |
34da0970 JS |
411 | if (m_zoomControl) |
412 | m_zoomControl->SetStringSelection(buf); | |
c801d85f KB |
413 | } |
414 | ||
34da0970 | 415 | int wxPreviewControlBar::GetZoomControl() |
c801d85f | 416 | { |
c801d85f | 417 | char buf[20]; |
2432b92d | 418 | if (m_zoomControl && (m_zoomControl->GetStringSelection() != "")) |
c801d85f | 419 | { |
34da0970 | 420 | strcpy(buf, m_zoomControl->GetStringSelection()); |
c801d85f KB |
421 | buf[strlen(buf) - 1] = 0; |
422 | return (int)atoi(buf); | |
423 | } | |
424 | else return 0; | |
c801d85f KB |
425 | } |
426 | ||
427 | ||
428 | /* | |
429 | * Preview frame | |
430 | */ | |
431 | ||
e3065973 JS |
432 | BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame) |
433 | EVT_CLOSE(wxPreviewFrame::OnCloseWindow) | |
434 | END_EVENT_TABLE() | |
435 | ||
c801d85f KB |
436 | wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxFrame *parent, const wxString& title, |
437 | const wxPoint& pos, const wxSize& size, long style, const wxString& name): | |
438 | wxFrame(parent, -1, title, pos, size, style, name) | |
439 | { | |
34da0970 JS |
440 | m_printPreview = preview; |
441 | m_controlBar = NULL; | |
442 | m_previewCanvas = NULL; | |
c801d85f KB |
443 | } |
444 | ||
34da0970 | 445 | wxPreviewFrame::~wxPreviewFrame() |
c801d85f KB |
446 | { |
447 | } | |
448 | ||
e3065973 | 449 | void wxPreviewFrame::OnCloseWindow(wxCloseEvent& event) |
c801d85f | 450 | { |
c801d85f KB |
451 | MakeModal(FALSE); |
452 | ||
453 | // Need to delete the printout and the print preview | |
34da0970 | 454 | wxPrintout *printout = m_printPreview->GetPrintout(); |
c801d85f KB |
455 | if (printout) |
456 | { | |
457 | delete printout; | |
34da0970 JS |
458 | m_printPreview->SetPrintout(NULL); |
459 | m_printPreview->SetCanvas(NULL); | |
460 | m_printPreview->SetFrame(NULL); | |
c801d85f | 461 | } |
34da0970 | 462 | delete m_printPreview; |
e3065973 JS |
463 | |
464 | Destroy(); | |
c801d85f KB |
465 | } |
466 | ||
34da0970 | 467 | void wxPreviewFrame::Initialize() |
c801d85f | 468 | { |
c801d85f KB |
469 | CreateStatusBar(); |
470 | ||
471 | CreateCanvas(); | |
472 | CreateControlBar(); | |
473 | ||
34da0970 JS |
474 | m_printPreview->SetCanvas(m_previewCanvas); |
475 | m_printPreview->SetFrame(this); | |
c801d85f KB |
476 | |
477 | // Set layout constraints here | |
478 | ||
479 | // Control bar constraints | |
480 | wxLayoutConstraints *c1 = new wxLayoutConstraints; | |
481 | // int w, h; | |
34da0970 | 482 | // m_controlBar->GetSize(&w, &h); |
c801d85f | 483 | int h; |
031b2a7b | 484 | #if (defined(__WXMSW__) || defined(__WXGTK__)) |
c801d85f KB |
485 | h = 40; |
486 | #else | |
487 | h = 60; | |
488 | #endif | |
489 | ||
490 | c1->left.SameAs (this, wxLeft); | |
491 | c1->top.SameAs (this, wxTop); | |
492 | c1->right.SameAs (this, wxRight); | |
493 | c1->height.Absolute (h); | |
494 | ||
34da0970 | 495 | m_controlBar->SetConstraints(c1); |
c801d85f KB |
496 | |
497 | // Canvas constraints | |
498 | wxLayoutConstraints *c2 = new wxLayoutConstraints; | |
499 | ||
500 | c2->left.SameAs (this, wxLeft); | |
34da0970 | 501 | c2->top.Below (m_controlBar); |
c801d85f KB |
502 | c2->right.SameAs (this, wxRight); |
503 | c2->bottom.SameAs (this, wxBottom); | |
504 | ||
34da0970 | 505 | m_previewCanvas->SetConstraints(c2); |
c801d85f KB |
506 | |
507 | SetAutoLayout(TRUE); | |
508 | ||
509 | MakeModal(TRUE); | |
510 | ||
511 | Layout(); | |
c801d85f KB |
512 | } |
513 | ||
34da0970 | 514 | void wxPreviewFrame::CreateCanvas() |
c801d85f | 515 | { |
34da0970 | 516 | m_previewCanvas = new wxPreviewCanvas(m_printPreview, this); |
c801d85f KB |
517 | } |
518 | ||
34da0970 | 519 | void wxPreviewFrame::CreateControlBar() |
c801d85f | 520 | { |
c801d85f | 521 | long buttons = wxPREVIEW_DEFAULT; |
34da0970 | 522 | if (m_printPreview->GetPrintoutForPrinting()) |
c801d85f KB |
523 | buttons |= wxPREVIEW_PRINT; |
524 | ||
34da0970 JS |
525 | m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0, 0), wxSize(400, 40)); |
526 | m_controlBar->CreateButtons(); | |
c801d85f KB |
527 | } |
528 | ||
529 | /* | |
530 | * Print preview | |
531 | */ | |
532 | ||
533 | wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data) | |
534 | { | |
34da0970 JS |
535 | m_isOk = TRUE; |
536 | m_previewPrintout = printout; | |
537 | if (m_previewPrintout) | |
538 | m_previewPrintout->SetIsPreview(TRUE); | |
c801d85f | 539 | |
34da0970 | 540 | m_printPrintout = printoutForPrinting; |
c801d85f | 541 | if (data) |
34da0970 JS |
542 | m_printData = (*data); |
543 | ||
544 | m_previewCanvas = NULL; | |
545 | m_previewFrame = NULL; | |
546 | m_previewBitmap = NULL; | |
547 | m_currentPage = 1; | |
548 | m_currentZoom = 30; | |
549 | m_topMargin = 40; | |
550 | m_leftMargin = 40; | |
551 | m_pageWidth = 0; | |
552 | m_pageHeight = 0; | |
c801d85f KB |
553 | |
554 | printout->OnPreparePrinting(); | |
555 | ||
556 | // Get some parameters from the printout, if defined | |
557 | int selFrom, selTo; | |
34da0970 | 558 | printout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo); |
c801d85f KB |
559 | } |
560 | ||
34da0970 | 561 | wxPrintPreviewBase::~wxPrintPreviewBase() |
c801d85f | 562 | { |
34da0970 JS |
563 | if (m_previewPrintout) |
564 | delete m_previewPrintout; | |
565 | if (m_previewBitmap) | |
566 | delete m_previewBitmap; | |
567 | if (m_printPrintout) | |
568 | delete m_printPrintout; | |
c801d85f KB |
569 | } |
570 | ||
571 | bool wxPrintPreviewBase::SetCurrentPage(int pageNum) | |
572 | { | |
34da0970 | 573 | if (m_currentPage == pageNum) |
c801d85f KB |
574 | return TRUE; |
575 | ||
34da0970 JS |
576 | m_currentPage = pageNum; |
577 | if (m_previewBitmap) | |
c801d85f | 578 | { |
34da0970 JS |
579 | delete m_previewBitmap; |
580 | m_previewBitmap = NULL; | |
c801d85f KB |
581 | } |
582 | ||
34da0970 | 583 | if (m_previewCanvas) |
c801d85f KB |
584 | { |
585 | RenderPage(pageNum); | |
34da0970 | 586 | m_previewCanvas->Refresh(); |
c801d85f | 587 | } |
c801d85f KB |
588 | return TRUE; |
589 | } | |
590 | ||
591 | bool wxPrintPreviewBase::PaintPage(wxWindow *canvas, wxDC& dc) | |
592 | { | |
c801d85f KB |
593 | DrawBlankPage(canvas, dc); |
594 | ||
34da0970 JS |
595 | if (!m_previewBitmap) |
596 | RenderPage(m_currentPage); | |
c801d85f | 597 | |
34da0970 | 598 | if (!m_previewBitmap) |
c801d85f KB |
599 | return FALSE; |
600 | ||
601 | if (!canvas) | |
602 | return FALSE; | |
603 | ||
604 | int canvasWidth, canvasHeight; | |
605 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
606 | ||
34da0970 JS |
607 | double zoomScale = ((float)m_currentZoom/(float)100); |
608 | double actualWidth = (zoomScale*m_pageWidth*m_previewScale); | |
609 | // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale); | |
c801d85f | 610 | |
341287bf | 611 | int x = (int) ((canvasWidth - actualWidth)/2.0); |
34da0970 JS |
612 | if (x < m_leftMargin) |
613 | x = m_leftMargin; | |
614 | int y = m_topMargin; | |
c801d85f KB |
615 | |
616 | wxMemoryDC temp_dc; | |
34da0970 | 617 | temp_dc.SelectObject(*m_previewBitmap); |
c801d85f | 618 | |
34da0970 | 619 | dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0); |
c801d85f KB |
620 | |
621 | temp_dc.SelectObject(wxNullBitmap); | |
622 | ||
c801d85f KB |
623 | return TRUE; |
624 | } | |
625 | ||
626 | bool wxPrintPreviewBase::RenderPage(int pageNum) | |
627 | { | |
628 | int canvasWidth, canvasHeight; | |
629 | ||
34da0970 | 630 | if (!m_previewCanvas) |
c801d85f | 631 | { |
1a5a8367 DP |
632 | wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"), |
633 | _("Print Preview Failure"), wxOK); | |
c801d85f KB |
634 | return FALSE; |
635 | } | |
34da0970 | 636 | m_previewCanvas->GetSize(&canvasWidth, &canvasHeight); |
c801d85f | 637 | |
34da0970 JS |
638 | double zoomScale = (m_currentZoom/100.0); |
639 | int actualWidth = (int)(zoomScale*m_pageWidth*m_previewScale); | |
640 | int actualHeight = (int)(zoomScale*m_pageHeight*m_previewScale); | |
c801d85f | 641 | |
34da0970 JS |
642 | int x = (int)((canvasWidth - actualWidth)/2.0); |
643 | if (x < m_leftMargin) | |
644 | x = m_leftMargin; | |
645 | // int y = m_topMargin; | |
c801d85f KB |
646 | |
647 | ||
34da0970 | 648 | if (!m_previewBitmap) |
c801d85f | 649 | { |
34da0970 JS |
650 | m_previewBitmap = new wxBitmap((int)actualWidth, (int)actualHeight); |
651 | if (!m_previewBitmap || !m_previewBitmap->Ok()) | |
c801d85f | 652 | { |
34da0970 JS |
653 | if (m_previewBitmap) |
654 | delete m_previewBitmap; | |
1a5a8367 | 655 | wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK); |
c801d85f KB |
656 | return FALSE; |
657 | } | |
658 | } | |
659 | ||
660 | wxMemoryDC memoryDC; | |
34da0970 | 661 | memoryDC.SelectObject(*m_previewBitmap); |
c801d85f KB |
662 | |
663 | memoryDC.Clear(); | |
664 | ||
34da0970 JS |
665 | m_previewPrintout->SetDC(&memoryDC); |
666 | m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight); | |
c801d85f | 667 | |
34da0970 | 668 | m_previewPrintout->OnBeginPrinting(); |
c801d85f KB |
669 | |
670 | ||
34da0970 | 671 | if (!m_previewPrintout->OnBeginDocument(m_printData.GetFromPage(), m_printData.GetToPage())) |
c801d85f | 672 | { |
1a5a8367 | 673 | wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK); |
c801d85f KB |
674 | |
675 | memoryDC.SelectObject(wxNullBitmap); | |
676 | ||
34da0970 | 677 | delete m_previewBitmap; |
c801d85f KB |
678 | return FALSE; |
679 | } | |
680 | ||
34da0970 JS |
681 | m_previewPrintout->OnPrintPage(pageNum); |
682 | m_previewPrintout->OnEndDocument(); | |
683 | m_previewPrintout->OnEndPrinting(); | |
c801d85f | 684 | |
34da0970 | 685 | m_previewPrintout->SetDC(NULL); |
c801d85f KB |
686 | |
687 | memoryDC.SelectObject(wxNullBitmap); | |
c801d85f KB |
688 | |
689 | char buf[200]; | |
34da0970 JS |
690 | if (m_maxPage != 0) |
691 | sprintf(buf, _("Page %d of %d"), pageNum, m_maxPage); | |
c801d85f | 692 | else |
1a5a8367 | 693 | sprintf(buf, _("Page %d"), pageNum); |
c801d85f | 694 | |
34da0970 JS |
695 | if (m_previewFrame) |
696 | m_previewFrame->SetStatusText(buf); | |
c801d85f KB |
697 | |
698 | return TRUE; | |
699 | } | |
700 | ||
701 | ||
702 | bool wxPrintPreviewBase::DrawBlankPage(wxWindow *canvas, wxDC& dc) | |
703 | { | |
c801d85f KB |
704 | int canvasWidth, canvasHeight; |
705 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
706 | ||
34da0970 JS |
707 | float zoomScale = (float)((float)m_currentZoom/(float)100); |
708 | float actualWidth = zoomScale*m_pageWidth*m_previewScale; | |
709 | float actualHeight = zoomScale*m_pageHeight*m_previewScale; | |
c801d85f KB |
710 | |
711 | float x = (float)((canvasWidth - actualWidth)/2.0); | |
34da0970 JS |
712 | if (x < m_leftMargin) |
713 | x = (float)m_leftMargin; | |
714 | float y = (float)m_topMargin; | |
c801d85f KB |
715 | |
716 | // Draw shadow, allowing for 1-pixel border AROUND the actual page | |
717 | int shadowOffset = 4; | |
718 | dc.SetPen(*wxBLACK_PEN); | |
719 | dc.SetBrush(*wxBLACK_BRUSH); | |
76ed8f8d | 720 | /* |
a4294b78 | 721 | dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2)); |
76ed8f8d | 722 | */ |
ed880dd4 | 723 | dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset); |
76ed8f8d | 724 | dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight)); |
c801d85f KB |
725 | |
726 | // Draw blank page allowing for 1-pixel border AROUND the actual page | |
727 | dc.SetPen(*wxBLACK_PEN); | |
728 | dc.SetBrush(*wxWHITE_BRUSH); | |
c801d85f | 729 | |
76ed8f8d RR |
730 | /* |
731 | wxRegion update_region = canvas->GetUpdateRegion(); | |
732 | wxRect r = update_region.GetBox(); | |
733 | ||
734 | printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height ); | |
735 | */ | |
736 | ||
ed880dd4 | 737 | dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2)); |
c801d85f | 738 | |
c801d85f KB |
739 | return TRUE; |
740 | } | |
741 | ||
742 | void wxPrintPreviewBase::SetZoom(int percent) | |
743 | { | |
34da0970 | 744 | if (m_currentZoom == percent) |
c801d85f KB |
745 | return; |
746 | ||
34da0970 JS |
747 | m_currentZoom = percent; |
748 | if (m_previewBitmap) | |
c801d85f | 749 | { |
34da0970 JS |
750 | delete m_previewBitmap; |
751 | m_previewBitmap = NULL; | |
c801d85f | 752 | } |
34da0970 | 753 | RenderPage(m_currentPage); |
c801d85f | 754 | |
34da0970 | 755 | if (m_previewCanvas) |
c801d85f | 756 | { |
34da0970 JS |
757 | m_previewCanvas->Clear(); |
758 | m_previewCanvas->Refresh(); | |
c801d85f | 759 | } |
c801d85f | 760 | } |