]>
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$ | |
55d99c7a JS |
8 | // Copyright: (c) Julian Smart |
9 | // Licence: wxWindows licence | |
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" | |
384a1303 | 39 | #include "wx/textdlg.h" |
2b5f62a0 VZ |
40 | #include "wx/sizer.h" |
41 | #endif // !WX_PRECOMP | |
c801d85f KB |
42 | |
43 | #include "wx/prntbase.h" | |
44 | #include "wx/dcprint.h" | |
45 | #include "wx/printdlg.h" | |
2a47d3c1 | 46 | #include "wx/module.h" |
c801d85f KB |
47 | |
48 | #include <stdlib.h> | |
49 | #include <string.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) | |
bf89b538 JS |
237 | EVT_BUTTON(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnFirstButton) |
238 | EVT_BUTTON(wxID_PREVIEW_LAST, wxPreviewControlBar::OnLastButton) | |
239 | EVT_BUTTON(wxID_PREVIEW_GOTO, wxPreviewControlBar::OnGotoButton) | |
0f90ec93 | 240 | EVT_CHAR(wxPreviewControlBar::OnChar) |
8826f46f VZ |
241 | EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoom) |
242 | EVT_PAINT(wxPreviewControlBar::OnPaint) | |
c801d85f | 243 | END_EVENT_TABLE() |
7bcb11d3 | 244 | |
c801d85f | 245 | wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons, |
7bcb11d3 JS |
246 | wxWindow *parent, const wxPoint& pos, const wxSize& size, |
247 | long style, const wxString& name): | |
248 | wxPanel(parent, -1, pos, size, style, name) | |
c801d85f | 249 | { |
7bcb11d3 JS |
250 | m_printPreview = preview; |
251 | m_closeButton = (wxButton *) NULL; | |
252 | m_nextPageButton = (wxButton *) NULL; | |
253 | m_previousPageButton = (wxButton *) NULL; | |
254 | m_printButton = (wxButton *) NULL; | |
255 | m_zoomControl = (wxChoice *) NULL; | |
256 | m_buttonFlags = buttons; | |
c801d85f KB |
257 | } |
258 | ||
34da0970 | 259 | wxPreviewControlBar::~wxPreviewControlBar() |
c801d85f KB |
260 | { |
261 | } | |
262 | ||
263 | void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
264 | { | |
7bcb11d3 | 265 | wxPaintDC dc(this); |
8826f46f | 266 | |
7bcb11d3 JS |
267 | int w, h; |
268 | GetSize(&w, &h); | |
269 | dc.SetPen(*wxBLACK_PEN); | |
270 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
271 | dc.DrawLine( 0, h-1, w, h-1 ); | |
c801d85f KB |
272 | } |
273 | ||
c330a2cf | 274 | void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 275 | { |
7bcb11d3 JS |
276 | wxPreviewFrame *frame = (wxPreviewFrame *)GetParent(); |
277 | frame->Close(TRUE); | |
c801d85f KB |
278 | } |
279 | ||
280 | void wxPreviewControlBar::OnPrint(wxCommandEvent& WXUNUSED(event)) | |
281 | { | |
7bcb11d3 JS |
282 | wxPrintPreviewBase *preview = GetPrintPreview(); |
283 | preview->Print(TRUE); | |
c801d85f KB |
284 | } |
285 | ||
0f90ec93 KB |
286 | void wxPreviewControlBar::OnChar(wxKeyEvent &event) |
287 | { | |
12a3f227 | 288 | switch(event.GetKeyCode()) |
0f90ec93 KB |
289 | { |
290 | case WXK_NEXT: | |
291 | OnNext(); break; | |
292 | case WXK_PRIOR: | |
293 | OnPrevious(); break; | |
bf89b538 JS |
294 | case WXK_HOME: |
295 | OnFirst(); break; | |
296 | case WXK_END: | |
297 | OnLast(); break; | |
298 | case WXK_TAB: | |
299 | OnGoto(); break; | |
0f90ec93 KB |
300 | default: |
301 | event.Skip(); | |
302 | } | |
303 | } | |
304 | ||
305 | void wxPreviewControlBar::OnNext(void) | |
c801d85f | 306 | { |
7bcb11d3 JS |
307 | wxPrintPreviewBase *preview = GetPrintPreview(); |
308 | if (preview) | |
c801d85f | 309 | { |
7bcb11d3 JS |
310 | int currentPage = preview->GetCurrentPage(); |
311 | if ((preview->GetMaxPage() > 0) && | |
312 | (currentPage < preview->GetMaxPage()) && | |
313 | preview->GetPrintout()->HasPage(currentPage + 1)) | |
314 | { | |
315 | preview->SetCurrentPage(currentPage + 1); | |
316 | } | |
c801d85f | 317 | } |
c801d85f KB |
318 | } |
319 | ||
0f90ec93 | 320 | void wxPreviewControlBar::OnPrevious(void) |
c801d85f | 321 | { |
7bcb11d3 JS |
322 | wxPrintPreviewBase *preview = GetPrintPreview(); |
323 | if (preview) | |
c801d85f | 324 | { |
7bcb11d3 JS |
325 | int currentPage = preview->GetCurrentPage(); |
326 | if ((preview->GetMinPage() > 0) && | |
327 | (currentPage > preview->GetMinPage()) && | |
328 | preview->GetPrintout()->HasPage(currentPage - 1)) | |
329 | { | |
330 | preview->SetCurrentPage(currentPage - 1); | |
331 | } | |
c801d85f | 332 | } |
c801d85f KB |
333 | } |
334 | ||
bf89b538 JS |
335 | void wxPreviewControlBar::OnFirst(void) |
336 | { | |
337 | wxPrintPreviewBase *preview = GetPrintPreview(); | |
338 | if (preview) | |
339 | { | |
340 | int currentPage = preview->GetMinPage(); | |
341 | if (preview->GetPrintout()->HasPage(currentPage)) | |
342 | { | |
343 | preview->SetCurrentPage(currentPage); | |
344 | } | |
345 | } | |
346 | } | |
347 | ||
348 | void wxPreviewControlBar::OnLast(void) | |
349 | { | |
350 | wxPrintPreviewBase *preview = GetPrintPreview(); | |
351 | if (preview) | |
352 | { | |
353 | int currentPage = preview->GetMaxPage(); | |
354 | if (preview->GetPrintout()->HasPage(currentPage)) | |
355 | { | |
356 | preview->SetCurrentPage(currentPage); | |
357 | } | |
358 | } | |
359 | } | |
360 | ||
361 | void wxPreviewControlBar::OnGoto(void) | |
362 | { | |
363 | wxPrintPreviewBase *preview = GetPrintPreview(); | |
364 | if (preview) | |
365 | { | |
366 | long currentPage; | |
367 | ||
368 | if (preview->GetMinPage() > 0) | |
369 | { | |
370 | wxString strPrompt; | |
371 | wxString strPage; | |
372 | ||
7e99eddf | 373 | strPrompt.Printf( wxT("%d...%d"), |
bf89b538 | 374 | preview->GetMinPage(), preview->GetMaxPage()); |
7e99eddf | 375 | strPage.Printf( wxT("%d"), preview->GetCurrentPage() ); |
bf89b538 JS |
376 | |
377 | strPage = | |
378 | wxGetTextFromUser( strPrompt, _("Goto Page"), strPage); | |
379 | ||
380 | if ( strPage.ToLong( ¤tPage ) ) | |
381 | if (preview->GetPrintout()->HasPage(currentPage)) | |
382 | { | |
383 | preview->SetCurrentPage(currentPage); | |
384 | } | |
385 | } | |
386 | } | |
387 | } | |
388 | ||
c801d85f KB |
389 | void wxPreviewControlBar::OnZoom(wxCommandEvent& WXUNUSED(event)) |
390 | { | |
7bcb11d3 JS |
391 | int zoom = GetZoomControl(); |
392 | if (GetPrintPreview()) | |
393 | GetPrintPreview()->SetZoom(zoom); | |
c801d85f KB |
394 | } |
395 | ||
34da0970 | 396 | void wxPreviewControlBar::CreateButtons() |
c801d85f | 397 | { |
7bcb11d3 | 398 | SetSize(0, 0, 400, 40); |
8826f46f | 399 | |
9dff8515 JS |
400 | wxBoxSizer *item0 = new wxBoxSizer( wxHORIZONTAL ); |
401 | ||
402 | int smallButtonWidth = 45; | |
403 | ||
404 | m_closeButton = new wxButton( this, wxID_PREVIEW_CLOSE, _("&Close"), wxDefaultPosition, wxDefaultSize, 0 ); | |
405 | item0->Add( m_closeButton, 0, wxALIGN_CENTRE|wxALL, 5 ); | |
406 | ||
7bcb11d3 JS |
407 | if (m_buttonFlags & wxPREVIEW_PRINT) |
408 | { | |
9dff8515 JS |
409 | m_printButton = new wxButton( this, wxID_PREVIEW_PRINT, _("&Print..."), wxDefaultPosition, wxDefaultSize, 0 ); |
410 | item0->Add( m_printButton, 0, wxALIGN_CENTRE|wxALL, 5 ); | |
7bcb11d3 | 411 | } |
9dff8515 | 412 | |
bf89b538 JS |
413 | if (m_buttonFlags & wxPREVIEW_FIRST) |
414 | { | |
9dff8515 JS |
415 | m_firstPageButton = new wxButton( this, wxID_PREVIEW_FIRST, _("|<<"), wxDefaultPosition, wxSize(smallButtonWidth,-1), 0 ); |
416 | item0->Add( m_firstPageButton, 0, wxALIGN_CENTRE|wxALL, 5 ); | |
bf89b538 | 417 | } |
9dff8515 | 418 | |
7bcb11d3 JS |
419 | if (m_buttonFlags & wxPREVIEW_PREVIOUS) |
420 | { | |
9dff8515 JS |
421 | m_previousPageButton = new wxButton( this, wxID_PREVIEW_PREVIOUS, _("<<"), wxDefaultPosition, wxSize(smallButtonWidth,-1), 0 ); |
422 | item0->Add( m_previousPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 ); | |
7bcb11d3 | 423 | } |
9dff8515 | 424 | |
7bcb11d3 JS |
425 | if (m_buttonFlags & wxPREVIEW_NEXT) |
426 | { | |
9dff8515 JS |
427 | m_nextPageButton = new wxButton( this, wxID_PREVIEW_NEXT, _(">>"), wxDefaultPosition, wxSize(smallButtonWidth,-1), 0 ); |
428 | item0->Add( m_nextPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 ); | |
bf89b538 | 429 | } |
9dff8515 | 430 | |
bf89b538 JS |
431 | if (m_buttonFlags & wxPREVIEW_LAST) |
432 | { | |
9dff8515 JS |
433 | m_lastPageButton = new wxButton( this, wxID_PREVIEW_LAST, _(">>|"), wxDefaultPosition, wxSize(smallButtonWidth,-1), 0 ); |
434 | item0->Add( m_lastPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 ); | |
bf89b538 | 435 | } |
9dff8515 | 436 | |
bf89b538 JS |
437 | if (m_buttonFlags & wxPREVIEW_GOTO) |
438 | { | |
9dff8515 JS |
439 | m_gotoPageButton = new wxButton( this, wxID_PREVIEW_GOTO, _("&Goto..."), wxDefaultPosition, wxDefaultSize, 0 ); |
440 | item0->Add( m_gotoPageButton, 0, wxALIGN_CENTRE|wxALL, 5 ); | |
7bcb11d3 | 441 | } |
9dff8515 | 442 | |
7bcb11d3 JS |
443 | if (m_buttonFlags & wxPREVIEW_ZOOM) |
444 | { | |
9dff8515 | 445 | wxString choices[] = |
c25ccf85 | 446 | { |
2b5f62a0 | 447 | wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"), |
9dff8515 JS |
448 | wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"), |
449 | wxT("120%"), wxT("150%"), wxT("200%") | |
c25ccf85 | 450 | }; |
c25ccf85 | 451 | int n = WXSIZEOF(choices); |
9dff8515 JS |
452 | |
453 | m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(70,-1), n, choices, 0 ); | |
454 | item0->Add( m_zoomControl, 0, wxALIGN_CENTRE|wxALL, 5 ); | |
7bcb11d3 JS |
455 | SetZoomControl(m_printPreview->GetZoom()); |
456 | } | |
8826f46f | 457 | |
9dff8515 JS |
458 | SetSizer(item0); |
459 | item0->Fit(this); | |
c801d85f KB |
460 | } |
461 | ||
462 | void wxPreviewControlBar::SetZoomControl(int zoom) | |
463 | { | |
2b5f62a0 VZ |
464 | wxChar buf[20]; |
465 | wxSprintf( buf, wxT("%d%%"), zoom ); | |
466 | ||
7bcb11d3 JS |
467 | if (m_zoomControl) |
468 | m_zoomControl->SetStringSelection(buf); | |
c801d85f KB |
469 | } |
470 | ||
34da0970 | 471 | int wxPreviewControlBar::GetZoomControl() |
c801d85f | 472 | { |
cf2f341a | 473 | wxChar buf[20]; |
223d09f6 | 474 | if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxT(""))) |
7bcb11d3 | 475 | { |
cf2f341a OK |
476 | wxStrcpy(buf, m_zoomControl->GetStringSelection()); |
477 | buf[wxStrlen(buf) - 1] = 0; | |
478 | return (int)wxAtoi(buf); | |
7bcb11d3 JS |
479 | } |
480 | else return 0; | |
c801d85f KB |
481 | } |
482 | ||
483 | ||
484 | /* | |
7bcb11d3 JS |
485 | * Preview frame |
486 | */ | |
c801d85f | 487 | |
e3065973 | 488 | BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame) |
0f90ec93 | 489 | EVT_CLOSE(wxPreviewFrame::OnCloseWindow) |
e3065973 JS |
490 | END_EVENT_TABLE() |
491 | ||
c801d85f | 492 | wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxFrame *parent, const wxString& title, |
7bcb11d3 JS |
493 | const wxPoint& pos, const wxSize& size, long style, const wxString& name): |
494 | wxFrame(parent, -1, title, pos, size, style, name) | |
c801d85f | 495 | { |
7bcb11d3 JS |
496 | m_printPreview = preview; |
497 | m_controlBar = NULL; | |
498 | m_previewCanvas = NULL; | |
c801d85f KB |
499 | } |
500 | ||
34da0970 | 501 | wxPreviewFrame::~wxPreviewFrame() |
c801d85f KB |
502 | { |
503 | } | |
504 | ||
74e3313b | 505 | void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
c801d85f | 506 | { |
7bcb11d3 | 507 | MakeModal(FALSE); |
8826f46f | 508 | |
7bcb11d3 JS |
509 | // Need to delete the printout and the print preview |
510 | wxPrintout *printout = m_printPreview->GetPrintout(); | |
511 | if (printout) | |
512 | { | |
513 | delete printout; | |
514 | m_printPreview->SetPrintout(NULL); | |
515 | m_printPreview->SetCanvas(NULL); | |
516 | m_printPreview->SetFrame(NULL); | |
517 | } | |
518 | delete m_printPreview; | |
8826f46f | 519 | |
7bcb11d3 | 520 | Destroy(); |
c801d85f KB |
521 | } |
522 | ||
34da0970 | 523 | void wxPreviewFrame::Initialize() |
c801d85f | 524 | { |
7bcb11d3 | 525 | CreateStatusBar(); |
7bcb11d3 JS |
526 | CreateCanvas(); |
527 | CreateControlBar(); | |
8826f46f | 528 | |
7bcb11d3 JS |
529 | m_printPreview->SetCanvas(m_previewCanvas); |
530 | m_printPreview->SetFrame(this); | |
8826f46f | 531 | |
9dff8515 | 532 | wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL ); |
8826f46f | 533 | |
9dff8515 JS |
534 | item0->Add( m_controlBar, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5 ); |
535 | item0->Add( m_previewCanvas, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 ); | |
8826f46f | 536 | |
9dff8515 JS |
537 | SetAutoLayout( TRUE ); |
538 | SetSizer( item0 ); | |
8826f46f | 539 | |
7bcb11d3 | 540 | MakeModal(TRUE); |
8826f46f | 541 | |
7bcb11d3 | 542 | Layout(); |
c801d85f KB |
543 | } |
544 | ||
34da0970 | 545 | void wxPreviewFrame::CreateCanvas() |
c801d85f | 546 | { |
7bcb11d3 | 547 | m_previewCanvas = new wxPreviewCanvas(m_printPreview, this); |
c801d85f KB |
548 | } |
549 | ||
34da0970 | 550 | void wxPreviewFrame::CreateControlBar() |
c801d85f | 551 | { |
7bcb11d3 JS |
552 | long buttons = wxPREVIEW_DEFAULT; |
553 | if (m_printPreview->GetPrintoutForPrinting()) | |
554 | buttons |= wxPREVIEW_PRINT; | |
8826f46f | 555 | |
7bcb11d3 JS |
556 | m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0, 0), wxSize(400, 40)); |
557 | m_controlBar->CreateButtons(); | |
c801d85f | 558 | } |
7bcb11d3 | 559 | |
c801d85f | 560 | /* |
7bcb11d3 JS |
561 | * Print preview |
562 | */ | |
c801d85f | 563 | |
8826f46f VZ |
564 | wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout, |
565 | wxPrintout *printoutForPrinting, | |
566 | wxPrintData *data) | |
567 | { | |
568 | if (data) | |
569 | m_printDialogData = (*data); | |
570 | ||
571 | Init(printout, printoutForPrinting); | |
572 | } | |
573 | ||
574 | wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout, | |
575 | wxPrintout *printoutForPrinting, | |
576 | wxPrintDialogData *data) | |
577 | { | |
578 | if (data) | |
579 | m_printDialogData = (*data); | |
580 | ||
581 | Init(printout, printoutForPrinting); | |
582 | } | |
583 | ||
584 | void wxPrintPreviewBase::Init(wxPrintout *printout, | |
585 | wxPrintout *printoutForPrinting) | |
c801d85f | 586 | { |
7bcb11d3 JS |
587 | m_isOk = TRUE; |
588 | m_previewPrintout = printout; | |
589 | if (m_previewPrintout) | |
590 | m_previewPrintout->SetIsPreview(TRUE); | |
8826f46f | 591 | |
7bcb11d3 | 592 | m_printPrintout = printoutForPrinting; |
8826f46f | 593 | |
7bcb11d3 JS |
594 | m_previewCanvas = NULL; |
595 | m_previewFrame = NULL; | |
596 | m_previewBitmap = NULL; | |
597 | m_currentPage = 1; | |
c25ccf85 | 598 | m_currentZoom = 70; |
7bcb11d3 JS |
599 | m_topMargin = 40; |
600 | m_leftMargin = 40; | |
601 | m_pageWidth = 0; | |
602 | m_pageHeight = 0; | |
1044a386 | 603 | m_printingPrepared = FALSE; |
8826f46f | 604 | |
1044a386 JS |
605 | // Too soon! Moved to RenderPage. |
606 | // printout->OnPreparePrinting(); | |
8826f46f | 607 | |
7bcb11d3 JS |
608 | // Get some parameters from the printout, if defined |
609 | int selFrom, selTo; | |
610 | printout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo); | |
c801d85f KB |
611 | } |
612 | ||
34da0970 | 613 | wxPrintPreviewBase::~wxPrintPreviewBase() |
c801d85f | 614 | { |
7bcb11d3 JS |
615 | if (m_previewPrintout) |
616 | delete m_previewPrintout; | |
617 | if (m_previewBitmap) | |
618 | delete m_previewBitmap; | |
619 | if (m_printPrintout) | |
620 | delete m_printPrintout; | |
c801d85f KB |
621 | } |
622 | ||
623 | bool wxPrintPreviewBase::SetCurrentPage(int pageNum) | |
624 | { | |
7bcb11d3 JS |
625 | if (m_currentPage == pageNum) |
626 | return TRUE; | |
8826f46f | 627 | |
7bcb11d3 JS |
628 | m_currentPage = pageNum; |
629 | if (m_previewBitmap) | |
630 | { | |
631 | delete m_previewBitmap; | |
632 | m_previewBitmap = NULL; | |
633 | } | |
8826f46f | 634 | |
7bcb11d3 JS |
635 | if (m_previewCanvas) |
636 | { | |
9eee81a4 | 637 | if (!RenderPage(pageNum)) |
bf89b538 | 638 | return FALSE; |
7bcb11d3 JS |
639 | m_previewCanvas->Refresh(); |
640 | } | |
c801d85f | 641 | return TRUE; |
c801d85f KB |
642 | } |
643 | ||
644 | bool wxPrintPreviewBase::PaintPage(wxWindow *canvas, wxDC& dc) | |
645 | { | |
7bcb11d3 | 646 | DrawBlankPage(canvas, dc); |
8826f46f | 647 | |
7bcb11d3 | 648 | if (!m_previewBitmap) |
9eee81a4 | 649 | if (!RenderPage(m_currentPage)) |
bf89b538 | 650 | return FALSE; |
8826f46f | 651 | |
7bcb11d3 JS |
652 | if (!m_previewBitmap) |
653 | return FALSE; | |
8826f46f | 654 | |
7bcb11d3 JS |
655 | if (!canvas) |
656 | return FALSE; | |
8826f46f | 657 | |
7bcb11d3 JS |
658 | int canvasWidth, canvasHeight; |
659 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
8826f46f | 660 | |
7bcb11d3 JS |
661 | double zoomScale = ((float)m_currentZoom/(float)100); |
662 | double actualWidth = (zoomScale*m_pageWidth*m_previewScale); | |
663 | // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale); | |
8826f46f | 664 | |
7bcb11d3 JS |
665 | int x = (int) ((canvasWidth - actualWidth)/2.0); |
666 | if (x < m_leftMargin) | |
667 | x = m_leftMargin; | |
668 | int y = m_topMargin; | |
8826f46f | 669 | |
7bcb11d3 JS |
670 | wxMemoryDC temp_dc; |
671 | temp_dc.SelectObject(*m_previewBitmap); | |
8826f46f | 672 | |
7bcb11d3 | 673 | dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0); |
8826f46f | 674 | |
7bcb11d3 | 675 | temp_dc.SelectObject(wxNullBitmap); |
8826f46f | 676 | |
7bcb11d3 | 677 | return TRUE; |
c801d85f KB |
678 | } |
679 | ||
680 | bool wxPrintPreviewBase::RenderPage(int pageNum) | |
681 | { | |
f6bcfd97 BP |
682 | wxBusyCursor busy; |
683 | ||
7bcb11d3 | 684 | int canvasWidth, canvasHeight; |
8826f46f | 685 | |
7bcb11d3 | 686 | if (!m_previewCanvas) |
c801d85f | 687 | { |
f6bcfd97 | 688 | wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!")); |
771779ed | 689 | |
7bcb11d3 | 690 | return FALSE; |
c801d85f | 691 | } |
7bcb11d3 | 692 | m_previewCanvas->GetSize(&canvasWidth, &canvasHeight); |
8826f46f | 693 | |
7bcb11d3 JS |
694 | double zoomScale = (m_currentZoom/100.0); |
695 | int actualWidth = (int)(zoomScale*m_pageWidth*m_previewScale); | |
696 | int actualHeight = (int)(zoomScale*m_pageHeight*m_previewScale); | |
8826f46f | 697 | |
7bcb11d3 JS |
698 | int x = (int)((canvasWidth - actualWidth)/2.0); |
699 | if (x < m_leftMargin) | |
700 | x = m_leftMargin; | |
701 | // int y = m_topMargin; | |
8826f46f VZ |
702 | |
703 | ||
7bcb11d3 JS |
704 | if (!m_previewBitmap) |
705 | { | |
706 | m_previewBitmap = new wxBitmap((int)actualWidth, (int)actualHeight); | |
707 | if (!m_previewBitmap || !m_previewBitmap->Ok()) | |
708 | { | |
9eee81a4 | 709 | if (m_previewBitmap) { |
7bcb11d3 | 710 | delete m_previewBitmap; |
9eee81a4 GD |
711 | m_previewBitmap = NULL; |
712 | } | |
7bcb11d3 JS |
713 | wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK); |
714 | return FALSE; | |
715 | } | |
716 | } | |
8826f46f | 717 | |
7bcb11d3 JS |
718 | wxMemoryDC memoryDC; |
719 | memoryDC.SelectObject(*m_previewBitmap); | |
8826f46f | 720 | |
7bcb11d3 | 721 | memoryDC.Clear(); |
8826f46f | 722 | |
7bcb11d3 JS |
723 | m_previewPrintout->SetDC(&memoryDC); |
724 | m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight); | |
8826f46f | 725 | |
1044a386 JS |
726 | // Need to delay OnPreparePrinting until here, so we have enough information. |
727 | if (!m_printingPrepared) | |
728 | { | |
729 | m_previewPrintout->OnPreparePrinting(); | |
730 | m_printingPrepared = TRUE; | |
731 | } | |
732 | ||
7bcb11d3 | 733 | m_previewPrintout->OnBeginPrinting(); |
c801d85f | 734 | |
7bcb11d3 JS |
735 | if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage())) |
736 | { | |
737 | wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK); | |
8826f46f | 738 | |
7bcb11d3 | 739 | memoryDC.SelectObject(wxNullBitmap); |
8826f46f | 740 | |
7bcb11d3 | 741 | delete m_previewBitmap; |
9eee81a4 | 742 | m_previewBitmap = NULL; |
7bcb11d3 JS |
743 | return FALSE; |
744 | } | |
8826f46f | 745 | |
7bcb11d3 JS |
746 | m_previewPrintout->OnPrintPage(pageNum); |
747 | m_previewPrintout->OnEndDocument(); | |
748 | m_previewPrintout->OnEndPrinting(); | |
8826f46f | 749 | |
7bcb11d3 | 750 | m_previewPrintout->SetDC(NULL); |
8826f46f | 751 | |
c801d85f | 752 | memoryDC.SelectObject(wxNullBitmap); |
8826f46f | 753 | |
cf2f341a | 754 | wxChar buf[200]; |
7bcb11d3 | 755 | if (m_maxPage != 0) |
cf2f341a | 756 | wxSprintf(buf, _("Page %d of %d"), pageNum, m_maxPage); |
7bcb11d3 | 757 | else |
cf2f341a | 758 | wxSprintf(buf, _("Page %d"), pageNum); |
8826f46f | 759 | |
7bcb11d3 JS |
760 | if (m_previewFrame) |
761 | m_previewFrame->SetStatusText(buf); | |
8826f46f | 762 | |
7bcb11d3 | 763 | return TRUE; |
c801d85f KB |
764 | } |
765 | ||
766 | ||
767 | bool wxPrintPreviewBase::DrawBlankPage(wxWindow *canvas, wxDC& dc) | |
768 | { | |
7bcb11d3 JS |
769 | int canvasWidth, canvasHeight; |
770 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
8826f46f | 771 | |
7bcb11d3 JS |
772 | float zoomScale = (float)((float)m_currentZoom/(float)100); |
773 | float actualWidth = zoomScale*m_pageWidth*m_previewScale; | |
774 | float actualHeight = zoomScale*m_pageHeight*m_previewScale; | |
8826f46f | 775 | |
7bcb11d3 JS |
776 | float x = (float)((canvasWidth - actualWidth)/2.0); |
777 | if (x < m_leftMargin) | |
778 | x = (float)m_leftMargin; | |
779 | float y = (float)m_topMargin; | |
8826f46f | 780 | |
7bcb11d3 JS |
781 | // Draw shadow, allowing for 1-pixel border AROUND the actual page |
782 | int shadowOffset = 4; | |
783 | dc.SetPen(*wxBLACK_PEN); | |
784 | dc.SetBrush(*wxBLACK_BRUSH); | |
785 | /* | |
786 | dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2)); | |
787 | */ | |
788 | dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset); | |
789 | dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight)); | |
8826f46f | 790 | |
7bcb11d3 JS |
791 | // Draw blank page allowing for 1-pixel border AROUND the actual page |
792 | dc.SetPen(*wxBLACK_PEN); | |
793 | dc.SetBrush(*wxWHITE_BRUSH); | |
8826f46f | 794 | |
7bcb11d3 JS |
795 | /* |
796 | wxRegion update_region = canvas->GetUpdateRegion(); | |
797 | wxRect r = update_region.GetBox(); | |
8826f46f | 798 | |
7bcb11d3 JS |
799 | printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height ); |
800 | */ | |
8826f46f | 801 | |
7bcb11d3 | 802 | dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2)); |
8826f46f | 803 | |
2a47d3c1 JS |
804 | return TRUE; |
805 | } | |
806 | ||
7bcb11d3 | 807 | void wxPrintPreviewBase::SetZoom(int percent) |
2a47d3c1 | 808 | { |
7bcb11d3 JS |
809 | if (m_currentZoom == percent) |
810 | return; | |
8826f46f | 811 | |
7bcb11d3 JS |
812 | m_currentZoom = percent; |
813 | if (m_previewBitmap) | |
814 | { | |
815 | delete m_previewBitmap; | |
816 | m_previewBitmap = NULL; | |
817 | } | |
aff37a19 | 818 | |
7bcb11d3 JS |
819 | if (m_previewCanvas) |
820 | { | |
aff37a19 | 821 | RenderPage(m_currentPage); |
95724b1a | 822 | ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0); |
7bcb11d3 JS |
823 | m_previewCanvas->Clear(); |
824 | m_previewCanvas->Refresh(); | |
825 | } | |
2a47d3c1 JS |
826 | } |
827 | ||
d427503c | 828 | #endif // wxUSE_PRINTING_ARCHITECTURE |