]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: prntbase.cpp | |
3 | // Purpose: Printing framework base class implementation | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "prntbase.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
c801d85f KB |
23 | #include "wx/defs.h" |
24 | ||
25 | #ifndef WX_PRECOMP | |
26 | #include "wx/utils.h" | |
27 | #include "wx/dc.h" | |
28 | #include "wx/app.h" | |
29 | #include "wx/msgdlg.h" | |
30 | #include "wx/layout.h" | |
31 | #include "wx/choice.h" | |
32 | #include "wx/button.h" | |
33 | #include "wx/settings.h" | |
34 | #include "wx/dcmemory.h" | |
35 | #include "wx/stattext.h" | |
36 | #include "wx/intl.h" | |
37 | #endif | |
38 | ||
39 | #include "wx/prntbase.h" | |
40 | #include "wx/dcprint.h" | |
41 | #include "wx/printdlg.h" | |
2a47d3c1 | 42 | #include "wx/module.h" |
c801d85f KB |
43 | |
44 | #include <stdlib.h> | |
45 | #include <string.h> | |
46 | ||
2049ba38 | 47 | #ifdef __WXMSW__ |
c801d85f KB |
48 | #include <windows.h> |
49 | #include <commdlg.h> | |
50 | ||
51 | // Clash with Windows header files | |
52 | #ifdef StartDoc | |
53 | #undef StartDoc | |
54 | #endif | |
55 | ||
56 | #ifndef __WIN32__ | |
57 | #include <print.h> | |
58 | #endif | |
59 | ||
c801d85f | 60 | #endif |
2049ba38 | 61 | // End __WXMSW__ |
c801d85f KB |
62 | |
63 | #if !USE_SHARED_LIBRARY | |
64 | IMPLEMENT_CLASS(wxPrinterBase, wxObject) | |
65 | IMPLEMENT_ABSTRACT_CLASS(wxPrintout, wxObject) | |
66 | IMPLEMENT_CLASS(wxPreviewCanvas, wxWindow) | |
67 | IMPLEMENT_CLASS(wxPreviewControlBar, wxWindow) | |
68 | IMPLEMENT_CLASS(wxPreviewFrame, wxFrame) | |
69 | IMPLEMENT_CLASS(wxPrintPreviewBase, wxObject) | |
2a47d3c1 | 70 | IMPLEMENT_DYNAMIC_CLASS(wxPrintPaperType, wxObject) |
c801d85f KB |
71 | |
72 | BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog) | |
73 | EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel) | |
74 | END_EVENT_TABLE() | |
75 | ||
76 | BEGIN_EVENT_TABLE(wxPreviewCanvas, wxScrolledWindow) | |
77 | EVT_PAINT(wxPreviewCanvas::OnPaint) | |
78 | EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged) | |
79 | END_EVENT_TABLE() | |
80 | #endif | |
81 | ||
82 | /* | |
83 | * Printer | |
84 | */ | |
85 | ||
86 | wxPrinterBase::wxPrinterBase(wxPrintData *data) | |
87 | { | |
34da0970 JS |
88 | m_currentPrintout = (wxPrintout *) NULL; |
89 | sm_abortWindow = (wxWindow *) NULL; | |
90 | sm_abortIt = FALSE; | |
c801d85f | 91 | if (data) |
34da0970 | 92 | m_printData = (*data); |
c801d85f KB |
93 | } |
94 | ||
34da0970 JS |
95 | wxWindow *wxPrinterBase::sm_abortWindow = (wxWindow *) NULL; |
96 | bool wxPrinterBase::sm_abortIt = FALSE; | |
c801d85f | 97 | |
34da0970 | 98 | wxPrinterBase::~wxPrinterBase() |
c801d85f KB |
99 | { |
100 | } | |
101 | ||
102 | void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) | |
103 | { | |
34da0970 JS |
104 | wxPrinterBase::sm_abortIt = TRUE; |
105 | wxPrinterBase::sm_abortWindow->Show(FALSE); | |
106 | wxPrinterBase::sm_abortWindow->Close(TRUE); | |
107 | wxPrinterBase::sm_abortWindow = (wxWindow *) NULL; | |
c801d85f KB |
108 | } |
109 | ||
110 | wxWindow *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout *WXUNUSED(printout)) | |
111 | { | |
34da0970 | 112 | wxPrintAbortDialog *dialog = new wxPrintAbortDialog(parent, _("Printing"), wxPoint(0, 0), wxSize(400, 400), wxDEFAULT_DIALOG_STYLE); |
c801d85f KB |
113 | (void) new wxStaticText(dialog, -1, _("Please wait..."), wxPoint(5, 5)); |
114 | ||
115 | wxButton *button = new wxButton(dialog, wxID_CANCEL, _("Cancel"), wxPoint(5, 30)); | |
116 | ||
117 | dialog->Fit(); | |
118 | button->Centre(wxHORIZONTAL); | |
119 | ||
120 | dialog->Centre(); | |
121 | return dialog; | |
122 | } | |
123 | ||
124 | void wxPrinterBase::ReportError(wxWindow *parent, wxPrintout *WXUNUSED(printout), char *message) | |
125 | { | |
1a5a8367 | 126 | wxMessageBox(message, _("Printing Error"), wxOK, parent); |
c801d85f KB |
127 | } |
128 | ||
129 | /* | |
130 | * Printout class | |
131 | */ | |
132 | ||
34da0970 | 133 | wxPrintout::wxPrintout(const wxString& title) |
c801d85f | 134 | { |
34da0970 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 | { | |
1a5a8367 | 154 | return GetDC()->StartDoc(_("Printing")); |
c801d85f KB |
155 | } |
156 | ||
34da0970 | 157 | void wxPrintout::OnEndDocument() |
c801d85f KB |
158 | { |
159 | GetDC()->EndDoc(); | |
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 | { | |
172 | return (page == 1); | |
173 | } | |
174 | ||
175 | void wxPrintout::GetPageInfo(int *minPage, int *maxPage, int *fromPage, int *toPage) | |
176 | { | |
177 | *minPage = 1; | |
178 | *maxPage = 32000; | |
179 | *fromPage = 1; | |
180 | *toPage = 1; | |
181 | } | |
182 | ||
183 | /* | |
184 | * Preview canvas | |
185 | */ | |
186 | ||
187 | wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase *preview, wxWindow *parent, | |
188 | const wxPoint& pos, const wxSize& size, long style, const wxString& name): | |
189 | wxScrolledWindow(parent, -1, pos, size, style, name) | |
190 | { | |
34da0970 | 191 | m_printPreview = preview; |
c801d85f KB |
192 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE)); |
193 | ||
76ed8f8d | 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 | { | |
203 | wxPaintDC dc(this); | |
031b2a7b | 204 | PrepareDC( dc ); |
c801d85f | 205 | |
34da0970 | 206 | if (m_printPreview) |
c801d85f | 207 | { |
34da0970 | 208 | m_printPreview->PaintPage(this, dc); |
c801d85f KB |
209 | } |
210 | } | |
211 | ||
212 | // Responds to colour changes, and passes event on to children. | |
213 | void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event) | |
214 | { | |
215 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE)); | |
216 | Refresh(); | |
217 | ||
218 | // Propagate the event to the non-top-level children | |
219 | wxWindow::OnSysColourChanged(event); | |
220 | } | |
221 | ||
222 | /* | |
223 | * Preview control bar | |
224 | */ | |
225 | ||
226 | BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel) | |
c330a2cf | 227 | EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose) |
c801d85f KB |
228 | EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrint) |
229 | EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPrevious) | |
230 | EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNext) | |
231 | EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoom) | |
232 | EVT_PAINT(wxPreviewControlBar::OnPaint) | |
233 | END_EVENT_TABLE() | |
234 | ||
235 | wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons, | |
236 | wxWindow *parent, const wxPoint& pos, const wxSize& size, | |
237 | long style, const wxString& name): | |
238 | wxPanel(parent, -1, pos, size, style, name) | |
239 | { | |
34da0970 JS |
240 | m_printPreview = preview; |
241 | m_closeButton = (wxButton *) NULL; | |
242 | m_nextPageButton = (wxButton *) NULL; | |
243 | m_previousPageButton = (wxButton *) NULL; | |
244 | m_printButton = (wxButton *) NULL; | |
245 | m_zoomControl = (wxChoice *) NULL; | |
246 | m_buttonFlags = buttons; | |
c801d85f KB |
247 | } |
248 | ||
34da0970 | 249 | wxPreviewControlBar::~wxPreviewControlBar() |
c801d85f KB |
250 | { |
251 | } | |
252 | ||
253 | void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
254 | { | |
255 | wxPaintDC dc(this); | |
256 | ||
257 | int w, h; | |
258 | GetSize(&w, &h); | |
259 | dc.SetPen(*wxBLACK_PEN); | |
260 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
261 | dc.DrawLine( 0, h-1, w, h-1 ); | |
262 | } | |
263 | ||
c330a2cf | 264 | void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event)) |
c801d85f KB |
265 | { |
266 | wxPreviewFrame *frame = (wxPreviewFrame *)GetParent(); | |
267 | frame->Close(TRUE); | |
268 | } | |
269 | ||
270 | void wxPreviewControlBar::OnPrint(wxCommandEvent& WXUNUSED(event)) | |
271 | { | |
272 | wxPrintPreviewBase *preview = GetPrintPreview(); | |
273 | preview->Print(TRUE); | |
274 | } | |
275 | ||
276 | void wxPreviewControlBar::OnNext(wxCommandEvent& WXUNUSED(event)) | |
277 | { | |
278 | wxPrintPreviewBase *preview = GetPrintPreview(); | |
279 | if (preview) | |
280 | { | |
281 | int currentPage = preview->GetCurrentPage(); | |
282 | if ((preview->GetMaxPage() > 0) && | |
283 | (currentPage < preview->GetMaxPage()) && | |
284 | preview->GetPrintout()->HasPage(currentPage + 1)) | |
285 | { | |
286 | preview->SetCurrentPage(currentPage + 1); | |
287 | } | |
288 | } | |
289 | } | |
290 | ||
291 | void wxPreviewControlBar::OnPrevious(wxCommandEvent& WXUNUSED(event)) | |
292 | { | |
293 | wxPrintPreviewBase *preview = GetPrintPreview(); | |
294 | if (preview) | |
295 | { | |
296 | int currentPage = preview->GetCurrentPage(); | |
297 | if ((preview->GetMinPage() > 0) && | |
298 | (currentPage > preview->GetMinPage()) && | |
299 | preview->GetPrintout()->HasPage(currentPage - 1)) | |
300 | { | |
301 | preview->SetCurrentPage(currentPage - 1); | |
302 | } | |
303 | } | |
304 | } | |
305 | ||
306 | void wxPreviewControlBar::OnZoom(wxCommandEvent& WXUNUSED(event)) | |
307 | { | |
308 | int zoom = GetZoomControl(); | |
309 | if (GetPrintPreview()) | |
310 | GetPrintPreview()->SetZoom(zoom); | |
311 | } | |
312 | ||
34da0970 | 313 | void wxPreviewControlBar::CreateButtons() |
c801d85f | 314 | { |
c801d85f KB |
315 | SetSize(0, 0, 400, 40); |
316 | ||
9838df2c | 317 | /* |
2049ba38 | 318 | #ifdef __WXMSW__ |
c801d85f KB |
319 | int fontSize = 9; |
320 | #else | |
321 | int fontSize = 10; | |
322 | #endif | |
323 | ||
34da0970 | 324 | wxFont buttonFont(fontSize, wxSWISS, wxNORMAL, wxBOLD); |
a4294b78 | 325 | SetFont(buttonFont); |
9838df2c | 326 | */ |
c801d85f KB |
327 | |
328 | int buttonWidth = 65; | |
031b2a7b RR |
329 | #ifdef __WXGTK__ |
330 | int buttonHeight = -1; | |
331 | #else | |
332 | int buttonHeight = 24; | |
333 | #endif | |
c801d85f KB |
334 | |
335 | int x = 5; | |
336 | int y = 5; | |
9838df2c JS |
337 | |
338 | #ifdef __WXMOTIF__ | |
339 | int gap = 15; | |
340 | #else | |
c801d85f | 341 | int gap = 5; |
9838df2c | 342 | #endif |
c801d85f | 343 | |
34da0970 | 344 | m_closeButton = new wxButton(this, wxID_PREVIEW_CLOSE, _("Close"), |
c801d85f KB |
345 | wxPoint(x, y), wxSize(buttonWidth, buttonHeight)); |
346 | ||
347 | x += gap + buttonWidth; | |
348 | ||
34da0970 | 349 | if (m_buttonFlags & wxPREVIEW_PRINT) |
c801d85f | 350 | { |
34da0970 | 351 | m_printButton = new wxButton(this, wxID_PREVIEW_PRINT, _("Print..."), wxPoint(x, y), |
c801d85f KB |
352 | wxSize(buttonWidth, buttonHeight)); |
353 | x += gap + buttonWidth; | |
354 | } | |
355 | ||
34da0970 | 356 | if (m_buttonFlags & wxPREVIEW_PREVIOUS) |
c801d85f | 357 | { |
34da0970 | 358 | m_previousPageButton = new wxButton(this, wxID_PREVIEW_PREVIOUS, "<<", wxPoint(x, y), |
c801d85f KB |
359 | wxSize(buttonWidth, buttonHeight)); |
360 | x += gap + buttonWidth; | |
361 | } | |
362 | ||
34da0970 | 363 | if (m_buttonFlags & wxPREVIEW_NEXT) |
c801d85f | 364 | { |
34da0970 | 365 | m_nextPageButton = new wxButton(this, wxID_PREVIEW_NEXT, ">>", |
c801d85f KB |
366 | wxPoint(x, y), wxSize(buttonWidth, buttonHeight)); |
367 | x += gap + buttonWidth; | |
368 | } | |
369 | ||
031b2a7b RR |
370 | // Yes, this look stupid, but this is because gcc gives up otherwise. |
371 | wxString *choices = new wxString[23]; | |
372 | choices[0] = "10%"; | |
373 | choices[1] = "15%"; | |
374 | choices[2] = "20%"; | |
375 | choices[3] = "25%"; | |
376 | choices[4] = "30%"; | |
377 | choices[5] = "35%"; | |
378 | choices[6] = "40%"; | |
379 | choices[7] = "45%"; | |
380 | choices[8] = "50%"; | |
381 | choices[9] = "55%"; | |
382 | choices[10] = "60%"; | |
383 | choices[11] = "65%"; | |
384 | choices[12] = "70%"; | |
385 | choices[13] = "75%"; | |
386 | choices[14] = "80%"; | |
387 | choices[15] = "85%"; | |
388 | choices[16] = "90%"; | |
389 | choices[17] = "95%"; | |
390 | choices[18] = "100%"; | |
391 | choices[19] = "110%"; | |
392 | choices[20] = "120%"; | |
393 | choices[21] = "150%"; | |
394 | choices[22] = "200%"; | |
395 | ||
396 | int n = 23; | |
34da0970 | 397 | if (m_buttonFlags & wxPREVIEW_ZOOM) |
c801d85f | 398 | { |
34da0970 | 399 | m_zoomControl = new wxChoice(this, wxID_PREVIEW_ZOOM, wxPoint(x, y), |
c801d85f | 400 | wxSize(100, -1), n, (wxString *)choices); |
34da0970 | 401 | SetZoomControl(m_printPreview->GetZoom()); |
c801d85f KB |
402 | } |
403 | ||
031b2a7b | 404 | delete[] choices; |
a4294b78 | 405 | |
9838df2c | 406 | // m_closeButton->SetDefault(); |
c801d85f KB |
407 | } |
408 | ||
409 | void wxPreviewControlBar::SetZoomControl(int zoom) | |
410 | { | |
c801d85f KB |
411 | char buf[20]; |
412 | sprintf(buf, "%d%%", zoom); | |
34da0970 JS |
413 | if (m_zoomControl) |
414 | m_zoomControl->SetStringSelection(buf); | |
c801d85f KB |
415 | } |
416 | ||
34da0970 | 417 | int wxPreviewControlBar::GetZoomControl() |
c801d85f | 418 | { |
c801d85f | 419 | char buf[20]; |
2432b92d | 420 | if (m_zoomControl && (m_zoomControl->GetStringSelection() != "")) |
c801d85f | 421 | { |
34da0970 | 422 | strcpy(buf, m_zoomControl->GetStringSelection()); |
c801d85f KB |
423 | buf[strlen(buf) - 1] = 0; |
424 | return (int)atoi(buf); | |
425 | } | |
426 | else return 0; | |
c801d85f KB |
427 | } |
428 | ||
429 | ||
430 | /* | |
431 | * Preview frame | |
432 | */ | |
433 | ||
e3065973 JS |
434 | BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame) |
435 | EVT_CLOSE(wxPreviewFrame::OnCloseWindow) | |
436 | END_EVENT_TABLE() | |
437 | ||
c801d85f KB |
438 | wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxFrame *parent, const wxString& title, |
439 | const wxPoint& pos, const wxSize& size, long style, const wxString& name): | |
440 | wxFrame(parent, -1, title, pos, size, style, name) | |
441 | { | |
34da0970 JS |
442 | m_printPreview = preview; |
443 | m_controlBar = NULL; | |
444 | m_previewCanvas = NULL; | |
c801d85f KB |
445 | } |
446 | ||
34da0970 | 447 | wxPreviewFrame::~wxPreviewFrame() |
c801d85f KB |
448 | { |
449 | } | |
450 | ||
e3065973 | 451 | void wxPreviewFrame::OnCloseWindow(wxCloseEvent& event) |
c801d85f | 452 | { |
c801d85f KB |
453 | MakeModal(FALSE); |
454 | ||
455 | // Need to delete the printout and the print preview | |
34da0970 | 456 | wxPrintout *printout = m_printPreview->GetPrintout(); |
c801d85f KB |
457 | if (printout) |
458 | { | |
459 | delete printout; | |
34da0970 JS |
460 | m_printPreview->SetPrintout(NULL); |
461 | m_printPreview->SetCanvas(NULL); | |
462 | m_printPreview->SetFrame(NULL); | |
c801d85f | 463 | } |
34da0970 | 464 | delete m_printPreview; |
e3065973 JS |
465 | |
466 | Destroy(); | |
c801d85f KB |
467 | } |
468 | ||
34da0970 | 469 | void wxPreviewFrame::Initialize() |
c801d85f | 470 | { |
c801d85f KB |
471 | CreateStatusBar(); |
472 | ||
473 | CreateCanvas(); | |
474 | CreateControlBar(); | |
475 | ||
34da0970 JS |
476 | m_printPreview->SetCanvas(m_previewCanvas); |
477 | m_printPreview->SetFrame(this); | |
c801d85f KB |
478 | |
479 | // Set layout constraints here | |
480 | ||
481 | // Control bar constraints | |
482 | wxLayoutConstraints *c1 = new wxLayoutConstraints; | |
483 | // int w, h; | |
34da0970 | 484 | // m_controlBar->GetSize(&w, &h); |
c801d85f | 485 | int h; |
031b2a7b | 486 | #if (defined(__WXMSW__) || defined(__WXGTK__)) |
c801d85f KB |
487 | h = 40; |
488 | #else | |
489 | h = 60; | |
490 | #endif | |
491 | ||
492 | c1->left.SameAs (this, wxLeft); | |
493 | c1->top.SameAs (this, wxTop); | |
494 | c1->right.SameAs (this, wxRight); | |
495 | c1->height.Absolute (h); | |
496 | ||
34da0970 | 497 | m_controlBar->SetConstraints(c1); |
c801d85f KB |
498 | |
499 | // Canvas constraints | |
500 | wxLayoutConstraints *c2 = new wxLayoutConstraints; | |
501 | ||
502 | c2->left.SameAs (this, wxLeft); | |
34da0970 | 503 | c2->top.Below (m_controlBar); |
c801d85f KB |
504 | c2->right.SameAs (this, wxRight); |
505 | c2->bottom.SameAs (this, wxBottom); | |
506 | ||
34da0970 | 507 | m_previewCanvas->SetConstraints(c2); |
c801d85f KB |
508 | |
509 | SetAutoLayout(TRUE); | |
510 | ||
511 | MakeModal(TRUE); | |
512 | ||
513 | Layout(); | |
c801d85f KB |
514 | } |
515 | ||
34da0970 | 516 | void wxPreviewFrame::CreateCanvas() |
c801d85f | 517 | { |
34da0970 | 518 | m_previewCanvas = new wxPreviewCanvas(m_printPreview, this); |
c801d85f KB |
519 | } |
520 | ||
34da0970 | 521 | void wxPreviewFrame::CreateControlBar() |
c801d85f | 522 | { |
c801d85f | 523 | long buttons = wxPREVIEW_DEFAULT; |
34da0970 | 524 | if (m_printPreview->GetPrintoutForPrinting()) |
c801d85f KB |
525 | buttons |= wxPREVIEW_PRINT; |
526 | ||
34da0970 JS |
527 | m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0, 0), wxSize(400, 40)); |
528 | m_controlBar->CreateButtons(); | |
c801d85f KB |
529 | } |
530 | ||
531 | /* | |
532 | * Print preview | |
533 | */ | |
534 | ||
535 | wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data) | |
536 | { | |
34da0970 JS |
537 | m_isOk = TRUE; |
538 | m_previewPrintout = printout; | |
539 | if (m_previewPrintout) | |
540 | m_previewPrintout->SetIsPreview(TRUE); | |
c801d85f | 541 | |
34da0970 | 542 | m_printPrintout = printoutForPrinting; |
c801d85f | 543 | if (data) |
34da0970 JS |
544 | m_printData = (*data); |
545 | ||
546 | m_previewCanvas = NULL; | |
547 | m_previewFrame = NULL; | |
548 | m_previewBitmap = NULL; | |
549 | m_currentPage = 1; | |
550 | m_currentZoom = 30; | |
551 | m_topMargin = 40; | |
552 | m_leftMargin = 40; | |
553 | m_pageWidth = 0; | |
554 | m_pageHeight = 0; | |
c801d85f KB |
555 | |
556 | printout->OnPreparePrinting(); | |
557 | ||
558 | // Get some parameters from the printout, if defined | |
559 | int selFrom, selTo; | |
34da0970 | 560 | printout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo); |
c801d85f KB |
561 | } |
562 | ||
34da0970 | 563 | wxPrintPreviewBase::~wxPrintPreviewBase() |
c801d85f | 564 | { |
34da0970 JS |
565 | if (m_previewPrintout) |
566 | delete m_previewPrintout; | |
567 | if (m_previewBitmap) | |
568 | delete m_previewBitmap; | |
569 | if (m_printPrintout) | |
570 | delete m_printPrintout; | |
c801d85f KB |
571 | } |
572 | ||
573 | bool wxPrintPreviewBase::SetCurrentPage(int pageNum) | |
574 | { | |
34da0970 | 575 | if (m_currentPage == pageNum) |
c801d85f KB |
576 | return TRUE; |
577 | ||
34da0970 JS |
578 | m_currentPage = pageNum; |
579 | if (m_previewBitmap) | |
c801d85f | 580 | { |
34da0970 JS |
581 | delete m_previewBitmap; |
582 | m_previewBitmap = NULL; | |
c801d85f KB |
583 | } |
584 | ||
34da0970 | 585 | if (m_previewCanvas) |
c801d85f KB |
586 | { |
587 | RenderPage(pageNum); | |
34da0970 | 588 | m_previewCanvas->Refresh(); |
c801d85f | 589 | } |
c801d85f KB |
590 | return TRUE; |
591 | } | |
592 | ||
593 | bool wxPrintPreviewBase::PaintPage(wxWindow *canvas, wxDC& dc) | |
594 | { | |
c801d85f KB |
595 | DrawBlankPage(canvas, dc); |
596 | ||
34da0970 JS |
597 | if (!m_previewBitmap) |
598 | RenderPage(m_currentPage); | |
c801d85f | 599 | |
34da0970 | 600 | if (!m_previewBitmap) |
c801d85f KB |
601 | return FALSE; |
602 | ||
603 | if (!canvas) | |
604 | return FALSE; | |
605 | ||
606 | int canvasWidth, canvasHeight; | |
607 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
608 | ||
34da0970 JS |
609 | double zoomScale = ((float)m_currentZoom/(float)100); |
610 | double actualWidth = (zoomScale*m_pageWidth*m_previewScale); | |
611 | // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale); | |
c801d85f | 612 | |
341287bf | 613 | int x = (int) ((canvasWidth - actualWidth)/2.0); |
34da0970 JS |
614 | if (x < m_leftMargin) |
615 | x = m_leftMargin; | |
616 | int y = m_topMargin; | |
c801d85f KB |
617 | |
618 | wxMemoryDC temp_dc; | |
34da0970 | 619 | temp_dc.SelectObject(*m_previewBitmap); |
c801d85f | 620 | |
34da0970 | 621 | dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0); |
c801d85f KB |
622 | |
623 | temp_dc.SelectObject(wxNullBitmap); | |
624 | ||
c801d85f KB |
625 | return TRUE; |
626 | } | |
627 | ||
628 | bool wxPrintPreviewBase::RenderPage(int pageNum) | |
629 | { | |
630 | int canvasWidth, canvasHeight; | |
631 | ||
34da0970 | 632 | if (!m_previewCanvas) |
c801d85f | 633 | { |
1a5a8367 DP |
634 | wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"), |
635 | _("Print Preview Failure"), wxOK); | |
c801d85f KB |
636 | return FALSE; |
637 | } | |
34da0970 | 638 | m_previewCanvas->GetSize(&canvasWidth, &canvasHeight); |
c801d85f | 639 | |
34da0970 JS |
640 | double zoomScale = (m_currentZoom/100.0); |
641 | int actualWidth = (int)(zoomScale*m_pageWidth*m_previewScale); | |
642 | int actualHeight = (int)(zoomScale*m_pageHeight*m_previewScale); | |
c801d85f | 643 | |
34da0970 JS |
644 | int x = (int)((canvasWidth - actualWidth)/2.0); |
645 | if (x < m_leftMargin) | |
646 | x = m_leftMargin; | |
647 | // int y = m_topMargin; | |
c801d85f KB |
648 | |
649 | ||
34da0970 | 650 | if (!m_previewBitmap) |
c801d85f | 651 | { |
34da0970 JS |
652 | m_previewBitmap = new wxBitmap((int)actualWidth, (int)actualHeight); |
653 | if (!m_previewBitmap || !m_previewBitmap->Ok()) | |
c801d85f | 654 | { |
34da0970 JS |
655 | if (m_previewBitmap) |
656 | delete m_previewBitmap; | |
1a5a8367 | 657 | wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK); |
c801d85f KB |
658 | return FALSE; |
659 | } | |
660 | } | |
661 | ||
662 | wxMemoryDC memoryDC; | |
34da0970 | 663 | memoryDC.SelectObject(*m_previewBitmap); |
c801d85f KB |
664 | |
665 | memoryDC.Clear(); | |
666 | ||
34da0970 JS |
667 | m_previewPrintout->SetDC(&memoryDC); |
668 | m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight); | |
c801d85f | 669 | |
34da0970 | 670 | m_previewPrintout->OnBeginPrinting(); |
c801d85f KB |
671 | |
672 | ||
34da0970 | 673 | if (!m_previewPrintout->OnBeginDocument(m_printData.GetFromPage(), m_printData.GetToPage())) |
c801d85f | 674 | { |
1a5a8367 | 675 | wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK); |
c801d85f KB |
676 | |
677 | memoryDC.SelectObject(wxNullBitmap); | |
678 | ||
34da0970 | 679 | delete m_previewBitmap; |
c801d85f KB |
680 | return FALSE; |
681 | } | |
682 | ||
34da0970 JS |
683 | m_previewPrintout->OnPrintPage(pageNum); |
684 | m_previewPrintout->OnEndDocument(); | |
685 | m_previewPrintout->OnEndPrinting(); | |
c801d85f | 686 | |
34da0970 | 687 | m_previewPrintout->SetDC(NULL); |
c801d85f KB |
688 | |
689 | memoryDC.SelectObject(wxNullBitmap); | |
c801d85f KB |
690 | |
691 | char buf[200]; | |
34da0970 JS |
692 | if (m_maxPage != 0) |
693 | sprintf(buf, _("Page %d of %d"), pageNum, m_maxPage); | |
c801d85f | 694 | else |
1a5a8367 | 695 | sprintf(buf, _("Page %d"), pageNum); |
c801d85f | 696 | |
34da0970 JS |
697 | if (m_previewFrame) |
698 | m_previewFrame->SetStatusText(buf); | |
c801d85f KB |
699 | |
700 | return TRUE; | |
701 | } | |
702 | ||
703 | ||
704 | bool wxPrintPreviewBase::DrawBlankPage(wxWindow *canvas, wxDC& dc) | |
705 | { | |
c801d85f KB |
706 | int canvasWidth, canvasHeight; |
707 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
708 | ||
34da0970 JS |
709 | float zoomScale = (float)((float)m_currentZoom/(float)100); |
710 | float actualWidth = zoomScale*m_pageWidth*m_previewScale; | |
711 | float actualHeight = zoomScale*m_pageHeight*m_previewScale; | |
c801d85f KB |
712 | |
713 | float x = (float)((canvasWidth - actualWidth)/2.0); | |
34da0970 JS |
714 | if (x < m_leftMargin) |
715 | x = (float)m_leftMargin; | |
716 | float y = (float)m_topMargin; | |
c801d85f KB |
717 | |
718 | // Draw shadow, allowing for 1-pixel border AROUND the actual page | |
719 | int shadowOffset = 4; | |
720 | dc.SetPen(*wxBLACK_PEN); | |
721 | dc.SetBrush(*wxBLACK_BRUSH); | |
76ed8f8d | 722 | /* |
a4294b78 | 723 | dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2)); |
76ed8f8d | 724 | */ |
ed880dd4 | 725 | dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset); |
76ed8f8d | 726 | dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight)); |
c801d85f KB |
727 | |
728 | // Draw blank page allowing for 1-pixel border AROUND the actual page | |
729 | dc.SetPen(*wxBLACK_PEN); | |
730 | dc.SetBrush(*wxWHITE_BRUSH); | |
c801d85f | 731 | |
76ed8f8d RR |
732 | /* |
733 | wxRegion update_region = canvas->GetUpdateRegion(); | |
734 | wxRect r = update_region.GetBox(); | |
735 | ||
736 | printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height ); | |
737 | */ | |
738 | ||
ed880dd4 | 739 | dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2)); |
c801d85f | 740 | |
c801d85f KB |
741 | return TRUE; |
742 | } | |
743 | ||
744 | void wxPrintPreviewBase::SetZoom(int percent) | |
745 | { | |
34da0970 | 746 | if (m_currentZoom == percent) |
c801d85f KB |
747 | return; |
748 | ||
34da0970 JS |
749 | m_currentZoom = percent; |
750 | if (m_previewBitmap) | |
c801d85f | 751 | { |
34da0970 JS |
752 | delete m_previewBitmap; |
753 | m_previewBitmap = NULL; | |
c801d85f | 754 | } |
34da0970 | 755 | RenderPage(m_currentPage); |
c801d85f | 756 | |
34da0970 | 757 | if (m_previewCanvas) |
c801d85f | 758 | { |
34da0970 JS |
759 | m_previewCanvas->Clear(); |
760 | m_previewCanvas->Refresh(); | |
c801d85f | 761 | } |
c801d85f | 762 | } |
2a47d3c1 JS |
763 | |
764 | /* | |
765 | * Paper size database for PostScript or where the generic page setup dialog is | |
766 | * needed | |
767 | */ | |
768 | ||
769 | wxPrintPaperType::wxPrintPaperType(const char *name, int wmm, int hmm, int wp, int hp) | |
770 | { | |
771 | widthMM = wmm; | |
772 | heightMM = hmm; | |
773 | widthPixels = wp; | |
774 | heightPixels = hp; | |
775 | pageName = copystring(name); | |
776 | } | |
777 | ||
778 | wxPrintPaperType::~wxPrintPaperType() | |
779 | { | |
780 | delete[] pageName; | |
781 | } | |
782 | ||
783 | /* | |
784 | * Print paper database for PostScript | |
785 | */ | |
786 | ||
787 | wxPrintPaperDatabase* wxThePrintPaperDatabase = (wxPrintPaperDatabase*) NULL; | |
788 | ||
789 | #if !USE_SHARED_LIBRARIES | |
790 | IMPLEMENT_DYNAMIC_CLASS(wxPrintPaperDatabase, wxList) | |
791 | #endif | |
792 | ||
793 | wxPrintPaperDatabase::wxPrintPaperDatabase():wxList(wxKEY_STRING) | |
794 | { | |
795 | DeleteContents(TRUE); | |
796 | } | |
797 | ||
798 | wxPrintPaperDatabase::~wxPrintPaperDatabase() | |
799 | { | |
800 | } | |
801 | ||
802 | void wxPrintPaperDatabase::CreateDatabase() | |
803 | { | |
804 | // Need correct values for page size in pixels. | |
805 | // Each unit is one 'point' = 1/72 of an inch. | |
806 | // NOTE: WE NEED ALSO TO MAKE ADJUSTMENTS WHEN TRANSLATING | |
807 | // in wxPostScriptDC code, so we can start from top left. | |
808 | // So access this database and translate by appropriate number | |
809 | // of points for this paper size. OR IS IT OK ALREADY? | |
810 | // Can't remember where the PostScript origin is by default. | |
811 | // Heck, someone will know how to make it hunky-dory... | |
812 | // JACS 25/5/95 | |
813 | ||
814 | AddPaperType(_("A4 210 x 297 mm"), 210, 297, 595, 842); | |
815 | AddPaperType(_("A3 297 x 420 mm"), 297, 420, 842, 1191); | |
816 | AddPaperType(_("Letter 8 1/2 x 11 in"), 216, 279, 612, 791); | |
817 | AddPaperType(_("Legal 8 1/2 x 14 in"), 216, 356, 612, 1009); | |
818 | ||
819 | /* | |
820 | This is for 100 ppi | |
821 | ||
822 | AddPaperType(_("A4 210 x 297 mm"), 210, 297, 210*4, 297*4 ); | |
823 | AddPaperType(_("A3 297 x 420 mm"), 297, 420, 297*4, 420*4 ); | |
824 | AddPaperType(_("Letter 8 1/2 x 11 in"), 216, 279, 216*4, 279*4 ); | |
825 | AddPaperType(_("Legal 8 1/2 x 14 in"), 216, 356, 216*4, 356*4 ); | |
826 | */ | |
827 | } | |
828 | ||
829 | void wxPrintPaperDatabase::ClearDatabase() | |
830 | { | |
831 | Clear(); | |
832 | } | |
833 | ||
834 | void wxPrintPaperDatabase::AddPaperType(const char *name, int wmm, int hmm, int wp, int hp) | |
835 | { | |
836 | Append(name, new wxPrintPaperType(name, wmm, hmm, wp, hp)); | |
837 | } | |
838 | ||
839 | wxPrintPaperType *wxPrintPaperDatabase::FindPaperType(const char *name) | |
840 | { | |
841 | wxNode *node = Find(name); | |
842 | if (node) | |
843 | return (wxPrintPaperType *)node->Data(); | |
844 | else | |
845 | return (wxPrintPaperType *) NULL; | |
846 | } | |
847 | ||
848 | // A module to allow initialization/cleanup of print paper | |
849 | // things without calling these functions from app.cpp. | |
850 | ||
851 | class WXDLLEXPORT wxPrintBaseModule: public wxModule | |
852 | { | |
853 | DECLARE_DYNAMIC_CLASS(wxPrintBaseModule) | |
854 | public: | |
855 | wxPrintBaseModule() {} | |
856 | bool OnInit(); | |
857 | void OnExit(); | |
858 | }; | |
859 | ||
860 | IMPLEMENT_DYNAMIC_CLASS(wxPrintBaseModule, wxModule) | |
861 | ||
862 | /* | |
863 | * Initialization/cleanup module | |
864 | */ | |
865 | ||
866 | bool wxPrintBaseModule::OnInit() | |
867 | { | |
868 | wxThePrintPaperDatabase = new wxPrintPaperDatabase; | |
869 | wxThePrintPaperDatabase->CreateDatabase(); | |
870 | ||
871 | return TRUE; | |
872 | } | |
873 | ||
874 | void wxPrintBaseModule::OnExit() | |
875 | { | |
876 | delete wxThePrintPaperDatabase; | |
877 | wxThePrintPaperDatabase = NULL; | |
878 | } | |
879 | ||
880 |