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