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