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