]>
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 | ||
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 |
7bcb11d3 | 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) | |
70 | ||
71 | BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog) | |
7bcb11d3 | 72 | EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel) |
c801d85f KB |
73 | END_EVENT_TABLE() |
74 | ||
75 | BEGIN_EVENT_TABLE(wxPreviewCanvas, wxScrolledWindow) | |
7bcb11d3 JS |
76 | EVT_PAINT(wxPreviewCanvas::OnPaint) |
77 | EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged) | |
c801d85f KB |
78 | END_EVENT_TABLE() |
79 | #endif | |
80 | ||
81 | /* | |
7bcb11d3 JS |
82 | * Printer |
83 | */ | |
84 | ||
85 | wxPrinterBase::wxPrinterBase(wxPrintDialogData *data) | |
c801d85f | 86 | { |
7bcb11d3 JS |
87 | m_currentPrintout = (wxPrintout *) NULL; |
88 | sm_abortWindow = (wxWindow *) NULL; | |
89 | sm_abortIt = FALSE; | |
90 | if (data) | |
91 | m_printDialogData = (*data); | |
c801d85f KB |
92 | } |
93 | ||
34da0970 JS |
94 | wxWindow *wxPrinterBase::sm_abortWindow = (wxWindow *) NULL; |
95 | bool wxPrinterBase::sm_abortIt = FALSE; | |
c801d85f | 96 | |
34da0970 | 97 | wxPrinterBase::~wxPrinterBase() |
c801d85f KB |
98 | { |
99 | } | |
100 | ||
101 | void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) | |
102 | { | |
7bcb11d3 JS |
103 | wxPrinterBase::sm_abortIt = TRUE; |
104 | wxPrinterBase::sm_abortWindow->Show(FALSE); | |
105 | wxPrinterBase::sm_abortWindow->Close(TRUE); | |
106 | wxPrinterBase::sm_abortWindow = (wxWindow *) NULL; | |
c801d85f KB |
107 | } |
108 | ||
109 | wxWindow *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout *WXUNUSED(printout)) | |
110 | { | |
7bcb11d3 JS |
111 | wxPrintAbortDialog *dialog = new wxPrintAbortDialog(parent, _("Printing"), wxPoint(0, 0), wxSize(400, 400), wxDEFAULT_DIALOG_STYLE); |
112 | (void) new wxStaticText(dialog, -1, _("Please wait..."), wxPoint(5, 5)); | |
8826f46f | 113 | |
7bcb11d3 | 114 | wxButton *button = new wxButton(dialog, wxID_CANCEL, _("Cancel"), wxPoint(5, 30)); |
8826f46f | 115 | |
7bcb11d3 JS |
116 | dialog->Fit(); |
117 | button->Centre(wxHORIZONTAL); | |
8826f46f | 118 | |
7bcb11d3 JS |
119 | dialog->Centre(); |
120 | return dialog; | |
c801d85f KB |
121 | } |
122 | ||
123 | void wxPrinterBase::ReportError(wxWindow *parent, wxPrintout *WXUNUSED(printout), char *message) | |
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 | { | |
7bcb11d3 | 153 | return GetDC()->StartDoc(_("Printing")); |
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 JS |
190 | m_printPreview = preview; |
191 | SetBackgroundColour(wxSystemSettings::GetSystemColour(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 | |
7bcb11d3 JS |
205 | if (m_printPreview) |
206 | { | |
207 | m_printPreview->PaintPage(this, dc); | |
208 | } | |
c801d85f KB |
209 | } |
210 | ||
211 | // Responds to colour changes, and passes event on to children. | |
212 | void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event) | |
213 | { | |
7bcb11d3 JS |
214 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE)); |
215 | Refresh(); | |
8826f46f | 216 | |
7bcb11d3 JS |
217 | // Propagate the event to the non-top-level children |
218 | wxWindow::OnSysColourChanged(event); | |
c801d85f KB |
219 | } |
220 | ||
221 | /* | |
7bcb11d3 JS |
222 | * Preview control bar |
223 | */ | |
c801d85f KB |
224 | |
225 | BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel) | |
8826f46f VZ |
226 | EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose) |
227 | EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrint) | |
228 | EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPrevious) | |
229 | EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNext) | |
230 | EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoom) | |
231 | EVT_PAINT(wxPreviewControlBar::OnPaint) | |
c801d85f | 232 | END_EVENT_TABLE() |
7bcb11d3 | 233 | |
c801d85f | 234 | wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons, |
7bcb11d3 JS |
235 | wxWindow *parent, const wxPoint& pos, const wxSize& size, |
236 | long style, const wxString& name): | |
237 | wxPanel(parent, -1, pos, size, style, name) | |
c801d85f | 238 | { |
7bcb11d3 JS |
239 | m_printPreview = preview; |
240 | m_closeButton = (wxButton *) NULL; | |
241 | m_nextPageButton = (wxButton *) NULL; | |
242 | m_previousPageButton = (wxButton *) NULL; | |
243 | m_printButton = (wxButton *) NULL; | |
244 | m_zoomControl = (wxChoice *) NULL; | |
245 | m_buttonFlags = buttons; | |
c801d85f KB |
246 | } |
247 | ||
34da0970 | 248 | wxPreviewControlBar::~wxPreviewControlBar() |
c801d85f KB |
249 | { |
250 | } | |
251 | ||
252 | void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
253 | { | |
7bcb11d3 | 254 | wxPaintDC dc(this); |
8826f46f | 255 | |
7bcb11d3 JS |
256 | int w, h; |
257 | GetSize(&w, &h); | |
258 | dc.SetPen(*wxBLACK_PEN); | |
259 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
260 | dc.DrawLine( 0, h-1, w, h-1 ); | |
c801d85f KB |
261 | } |
262 | ||
c330a2cf | 263 | void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 264 | { |
7bcb11d3 JS |
265 | wxPreviewFrame *frame = (wxPreviewFrame *)GetParent(); |
266 | frame->Close(TRUE); | |
c801d85f KB |
267 | } |
268 | ||
269 | void wxPreviewControlBar::OnPrint(wxCommandEvent& WXUNUSED(event)) | |
270 | { | |
7bcb11d3 JS |
271 | wxPrintPreviewBase *preview = GetPrintPreview(); |
272 | preview->Print(TRUE); | |
c801d85f KB |
273 | } |
274 | ||
275 | void wxPreviewControlBar::OnNext(wxCommandEvent& WXUNUSED(event)) | |
276 | { | |
7bcb11d3 JS |
277 | wxPrintPreviewBase *preview = GetPrintPreview(); |
278 | if (preview) | |
c801d85f | 279 | { |
7bcb11d3 JS |
280 | int currentPage = preview->GetCurrentPage(); |
281 | if ((preview->GetMaxPage() > 0) && | |
282 | (currentPage < preview->GetMaxPage()) && | |
283 | preview->GetPrintout()->HasPage(currentPage + 1)) | |
284 | { | |
285 | preview->SetCurrentPage(currentPage + 1); | |
286 | } | |
c801d85f | 287 | } |
c801d85f KB |
288 | } |
289 | ||
290 | void wxPreviewControlBar::OnPrevious(wxCommandEvent& WXUNUSED(event)) | |
291 | { | |
7bcb11d3 JS |
292 | wxPrintPreviewBase *preview = GetPrintPreview(); |
293 | if (preview) | |
c801d85f | 294 | { |
7bcb11d3 JS |
295 | int currentPage = preview->GetCurrentPage(); |
296 | if ((preview->GetMinPage() > 0) && | |
297 | (currentPage > preview->GetMinPage()) && | |
298 | preview->GetPrintout()->HasPage(currentPage - 1)) | |
299 | { | |
300 | preview->SetCurrentPage(currentPage - 1); | |
301 | } | |
c801d85f | 302 | } |
c801d85f KB |
303 | } |
304 | ||
305 | void wxPreviewControlBar::OnZoom(wxCommandEvent& WXUNUSED(event)) | |
306 | { | |
7bcb11d3 JS |
307 | int zoom = GetZoomControl(); |
308 | if (GetPrintPreview()) | |
309 | GetPrintPreview()->SetZoom(zoom); | |
c801d85f KB |
310 | } |
311 | ||
34da0970 | 312 | void wxPreviewControlBar::CreateButtons() |
c801d85f | 313 | { |
7bcb11d3 | 314 | SetSize(0, 0, 400, 40); |
8826f46f | 315 | |
7bcb11d3 JS |
316 | /* |
317 | #ifdef __WXMSW__ | |
318 | int fontSize = 9; | |
319 | #else | |
320 | int fontSize = 10; | |
321 | #endif | |
8826f46f | 322 | |
7bcb11d3 JS |
323 | wxFont buttonFont(fontSize, wxSWISS, wxNORMAL, wxBOLD); |
324 | SetFont(buttonFont); | |
325 | */ | |
8826f46f | 326 | |
7bcb11d3 | 327 | int buttonWidth = 65; |
031b2a7b RR |
328 | #ifdef __WXGTK__ |
329 | int buttonHeight = -1; | |
330 | #else | |
331 | int buttonHeight = 24; | |
332 | #endif | |
8826f46f | 333 | |
7bcb11d3 JS |
334 | int x = 5; |
335 | int y = 5; | |
8826f46f | 336 | |
9838df2c | 337 | #ifdef __WXMOTIF__ |
7bcb11d3 | 338 | int gap = 15; |
9838df2c | 339 | #else |
7bcb11d3 | 340 | int gap = 5; |
9838df2c | 341 | #endif |
8826f46f | 342 | |
7bcb11d3 JS |
343 | m_closeButton = new wxButton(this, wxID_PREVIEW_CLOSE, _("Close"), |
344 | wxPoint(x, y), wxSize(buttonWidth, buttonHeight)); | |
8826f46f | 345 | |
7bcb11d3 | 346 | x += gap + buttonWidth; |
8826f46f | 347 | |
7bcb11d3 JS |
348 | if (m_buttonFlags & wxPREVIEW_PRINT) |
349 | { | |
350 | m_printButton = new wxButton(this, wxID_PREVIEW_PRINT, _("Print..."), wxPoint(x, y), | |
351 | wxSize(buttonWidth, buttonHeight)); | |
352 | x += gap + buttonWidth; | |
353 | } | |
8826f46f | 354 | |
7bcb11d3 JS |
355 | if (m_buttonFlags & wxPREVIEW_PREVIOUS) |
356 | { | |
357 | m_previousPageButton = new wxButton(this, wxID_PREVIEW_PREVIOUS, "<<", wxPoint(x, y), | |
358 | wxSize(buttonWidth, buttonHeight)); | |
359 | x += gap + buttonWidth; | |
360 | } | |
8826f46f | 361 | |
7bcb11d3 JS |
362 | if (m_buttonFlags & wxPREVIEW_NEXT) |
363 | { | |
364 | m_nextPageButton = new wxButton(this, wxID_PREVIEW_NEXT, ">>", | |
365 | wxPoint(x, y), wxSize(buttonWidth, buttonHeight)); | |
366 | x += gap + buttonWidth; | |
367 | } | |
8826f46f | 368 | |
966a3b2c VZ |
369 | static const char *choices[] = |
370 | { | |
371 | "10%", "15%", "20%", "25%", "30%", "35%", "40%", "45%", "50%", "55%", | |
372 | "60%", "65%", "70%", "75%", "80%", "85%", "90%", "95%", "100%", "110%", | |
373 | "120%", "150%", "200%" | |
374 | }; | |
375 | ||
7bcb11d3 JS |
376 | if (m_buttonFlags & wxPREVIEW_ZOOM) |
377 | { | |
966a3b2c VZ |
378 | m_zoomControl = new wxChoice(this, wxID_PREVIEW_ZOOM, |
379 | wxPoint(x, y), wxSize(100, -1)); | |
7bcb11d3 JS |
380 | SetZoomControl(m_printPreview->GetZoom()); |
381 | } | |
8826f46f | 382 | |
966a3b2c VZ |
383 | // Yes, this look stupid, but this is because gcc gives up otherwise. |
384 | int n = WXSIZEOF(choices); | |
385 | for ( int i = 0; i < n; i++ ) | |
386 | m_zoomControl->Append(choices[i]); | |
8826f46f | 387 | |
7bcb11d3 | 388 | // m_closeButton->SetDefault(); |
c801d85f KB |
389 | } |
390 | ||
391 | void wxPreviewControlBar::SetZoomControl(int zoom) | |
392 | { | |
7bcb11d3 JS |
393 | char buf[20]; |
394 | sprintf(buf, "%d%%", zoom); | |
395 | if (m_zoomControl) | |
396 | m_zoomControl->SetStringSelection(buf); | |
c801d85f KB |
397 | } |
398 | ||
34da0970 | 399 | int wxPreviewControlBar::GetZoomControl() |
c801d85f | 400 | { |
cf2f341a | 401 | wxChar buf[20]; |
4de6207a | 402 | if (m_zoomControl && (m_zoomControl->GetStringSelection() != _T(""))) |
7bcb11d3 | 403 | { |
cf2f341a OK |
404 | wxStrcpy(buf, m_zoomControl->GetStringSelection()); |
405 | buf[wxStrlen(buf) - 1] = 0; | |
406 | return (int)wxAtoi(buf); | |
7bcb11d3 JS |
407 | } |
408 | else return 0; | |
c801d85f KB |
409 | } |
410 | ||
411 | ||
412 | /* | |
7bcb11d3 JS |
413 | * Preview frame |
414 | */ | |
c801d85f | 415 | |
e3065973 | 416 | BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame) |
7bcb11d3 | 417 | EVT_CLOSE(wxPreviewFrame::OnCloseWindow) |
e3065973 JS |
418 | END_EVENT_TABLE() |
419 | ||
c801d85f | 420 | wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxFrame *parent, const wxString& title, |
7bcb11d3 JS |
421 | const wxPoint& pos, const wxSize& size, long style, const wxString& name): |
422 | wxFrame(parent, -1, title, pos, size, style, name) | |
c801d85f | 423 | { |
7bcb11d3 JS |
424 | m_printPreview = preview; |
425 | m_controlBar = NULL; | |
426 | m_previewCanvas = NULL; | |
c801d85f KB |
427 | } |
428 | ||
34da0970 | 429 | wxPreviewFrame::~wxPreviewFrame() |
c801d85f KB |
430 | { |
431 | } | |
432 | ||
74e3313b | 433 | void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
c801d85f | 434 | { |
7bcb11d3 | 435 | MakeModal(FALSE); |
8826f46f | 436 | |
7bcb11d3 JS |
437 | // Need to delete the printout and the print preview |
438 | wxPrintout *printout = m_printPreview->GetPrintout(); | |
439 | if (printout) | |
440 | { | |
441 | delete printout; | |
442 | m_printPreview->SetPrintout(NULL); | |
443 | m_printPreview->SetCanvas(NULL); | |
444 | m_printPreview->SetFrame(NULL); | |
445 | } | |
446 | delete m_printPreview; | |
8826f46f | 447 | |
7bcb11d3 | 448 | Destroy(); |
c801d85f KB |
449 | } |
450 | ||
34da0970 | 451 | void wxPreviewFrame::Initialize() |
c801d85f | 452 | { |
7bcb11d3 | 453 | CreateStatusBar(); |
8826f46f | 454 | |
7bcb11d3 JS |
455 | CreateCanvas(); |
456 | CreateControlBar(); | |
8826f46f | 457 | |
7bcb11d3 JS |
458 | m_printPreview->SetCanvas(m_previewCanvas); |
459 | m_printPreview->SetFrame(this); | |
8826f46f | 460 | |
7bcb11d3 | 461 | // Set layout constraints here |
8826f46f | 462 | |
7bcb11d3 JS |
463 | // Control bar constraints |
464 | wxLayoutConstraints *c1 = new wxLayoutConstraints; | |
465 | // int w, h; | |
466 | // m_controlBar->GetSize(&w, &h); | |
467 | int h; | |
031b2a7b | 468 | #if (defined(__WXMSW__) || defined(__WXGTK__)) |
7bcb11d3 | 469 | h = 40; |
c801d85f | 470 | #else |
7bcb11d3 | 471 | h = 60; |
c801d85f | 472 | #endif |
8826f46f | 473 | |
7bcb11d3 JS |
474 | c1->left.SameAs (this, wxLeft); |
475 | c1->top.SameAs (this, wxTop); | |
476 | c1->right.SameAs (this, wxRight); | |
477 | c1->height.Absolute (h); | |
8826f46f | 478 | |
7bcb11d3 | 479 | m_controlBar->SetConstraints(c1); |
8826f46f | 480 | |
7bcb11d3 JS |
481 | // Canvas constraints |
482 | wxLayoutConstraints *c2 = new wxLayoutConstraints; | |
8826f46f | 483 | |
7bcb11d3 JS |
484 | c2->left.SameAs (this, wxLeft); |
485 | c2->top.Below (m_controlBar); | |
486 | c2->right.SameAs (this, wxRight); | |
487 | c2->bottom.SameAs (this, wxBottom); | |
8826f46f | 488 | |
7bcb11d3 | 489 | m_previewCanvas->SetConstraints(c2); |
8826f46f | 490 | |
7bcb11d3 | 491 | SetAutoLayout(TRUE); |
8826f46f | 492 | |
7bcb11d3 | 493 | MakeModal(TRUE); |
8826f46f | 494 | |
7bcb11d3 | 495 | Layout(); |
c801d85f KB |
496 | } |
497 | ||
34da0970 | 498 | void wxPreviewFrame::CreateCanvas() |
c801d85f | 499 | { |
7bcb11d3 | 500 | m_previewCanvas = new wxPreviewCanvas(m_printPreview, this); |
c801d85f KB |
501 | } |
502 | ||
34da0970 | 503 | void wxPreviewFrame::CreateControlBar() |
c801d85f | 504 | { |
7bcb11d3 JS |
505 | long buttons = wxPREVIEW_DEFAULT; |
506 | if (m_printPreview->GetPrintoutForPrinting()) | |
507 | buttons |= wxPREVIEW_PRINT; | |
8826f46f | 508 | |
7bcb11d3 JS |
509 | m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0, 0), wxSize(400, 40)); |
510 | m_controlBar->CreateButtons(); | |
c801d85f | 511 | } |
7bcb11d3 | 512 | |
c801d85f | 513 | /* |
7bcb11d3 JS |
514 | * Print preview |
515 | */ | |
c801d85f | 516 | |
8826f46f VZ |
517 | wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout, |
518 | wxPrintout *printoutForPrinting, | |
519 | wxPrintData *data) | |
520 | { | |
521 | if (data) | |
522 | m_printDialogData = (*data); | |
523 | ||
524 | Init(printout, printoutForPrinting); | |
525 | } | |
526 | ||
527 | wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout, | |
528 | wxPrintout *printoutForPrinting, | |
529 | wxPrintDialogData *data) | |
530 | { | |
531 | if (data) | |
532 | m_printDialogData = (*data); | |
533 | ||
534 | Init(printout, printoutForPrinting); | |
535 | } | |
536 | ||
537 | void wxPrintPreviewBase::Init(wxPrintout *printout, | |
538 | wxPrintout *printoutForPrinting) | |
c801d85f | 539 | { |
7bcb11d3 JS |
540 | m_isOk = TRUE; |
541 | m_previewPrintout = printout; | |
542 | if (m_previewPrintout) | |
543 | m_previewPrintout->SetIsPreview(TRUE); | |
8826f46f | 544 | |
7bcb11d3 | 545 | m_printPrintout = printoutForPrinting; |
8826f46f | 546 | |
7bcb11d3 JS |
547 | m_previewCanvas = NULL; |
548 | m_previewFrame = NULL; | |
549 | m_previewBitmap = NULL; | |
550 | m_currentPage = 1; | |
551 | m_currentZoom = 30; | |
552 | m_topMargin = 40; | |
553 | m_leftMargin = 40; | |
554 | m_pageWidth = 0; | |
555 | m_pageHeight = 0; | |
8826f46f | 556 | |
7bcb11d3 | 557 | printout->OnPreparePrinting(); |
8826f46f | 558 | |
7bcb11d3 JS |
559 | // Get some parameters from the printout, if defined |
560 | int selFrom, selTo; | |
561 | printout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo); | |
c801d85f KB |
562 | } |
563 | ||
34da0970 | 564 | wxPrintPreviewBase::~wxPrintPreviewBase() |
c801d85f | 565 | { |
7bcb11d3 JS |
566 | if (m_previewPrintout) |
567 | delete m_previewPrintout; | |
568 | if (m_previewBitmap) | |
569 | delete m_previewBitmap; | |
570 | if (m_printPrintout) | |
571 | delete m_printPrintout; | |
c801d85f KB |
572 | } |
573 | ||
574 | bool wxPrintPreviewBase::SetCurrentPage(int pageNum) | |
575 | { | |
7bcb11d3 JS |
576 | if (m_currentPage == pageNum) |
577 | return TRUE; | |
8826f46f | 578 | |
7bcb11d3 JS |
579 | m_currentPage = pageNum; |
580 | if (m_previewBitmap) | |
581 | { | |
582 | delete m_previewBitmap; | |
583 | m_previewBitmap = NULL; | |
584 | } | |
8826f46f | 585 | |
7bcb11d3 JS |
586 | if (m_previewCanvas) |
587 | { | |
588 | RenderPage(pageNum); | |
589 | m_previewCanvas->Refresh(); | |
590 | } | |
c801d85f | 591 | return TRUE; |
c801d85f KB |
592 | } |
593 | ||
594 | bool wxPrintPreviewBase::PaintPage(wxWindow *canvas, wxDC& dc) | |
595 | { | |
7bcb11d3 | 596 | DrawBlankPage(canvas, dc); |
8826f46f | 597 | |
7bcb11d3 JS |
598 | if (!m_previewBitmap) |
599 | RenderPage(m_currentPage); | |
8826f46f | 600 | |
7bcb11d3 JS |
601 | if (!m_previewBitmap) |
602 | return FALSE; | |
8826f46f | 603 | |
7bcb11d3 JS |
604 | if (!canvas) |
605 | return FALSE; | |
8826f46f | 606 | |
7bcb11d3 JS |
607 | int canvasWidth, canvasHeight; |
608 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
8826f46f | 609 | |
7bcb11d3 JS |
610 | double zoomScale = ((float)m_currentZoom/(float)100); |
611 | double actualWidth = (zoomScale*m_pageWidth*m_previewScale); | |
612 | // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale); | |
8826f46f | 613 | |
7bcb11d3 JS |
614 | int x = (int) ((canvasWidth - actualWidth)/2.0); |
615 | if (x < m_leftMargin) | |
616 | x = m_leftMargin; | |
617 | int y = m_topMargin; | |
8826f46f | 618 | |
7bcb11d3 JS |
619 | wxMemoryDC temp_dc; |
620 | temp_dc.SelectObject(*m_previewBitmap); | |
8826f46f | 621 | |
7bcb11d3 | 622 | dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0); |
8826f46f | 623 | |
7bcb11d3 | 624 | temp_dc.SelectObject(wxNullBitmap); |
8826f46f | 625 | |
7bcb11d3 | 626 | return TRUE; |
c801d85f KB |
627 | } |
628 | ||
629 | bool wxPrintPreviewBase::RenderPage(int pageNum) | |
630 | { | |
7bcb11d3 | 631 | int canvasWidth, canvasHeight; |
8826f46f | 632 | |
7bcb11d3 | 633 | if (!m_previewCanvas) |
c801d85f | 634 | { |
7bcb11d3 JS |
635 | wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"), |
636 | _("Print Preview Failure"), wxOK); | |
637 | return FALSE; | |
c801d85f | 638 | } |
7bcb11d3 | 639 | m_previewCanvas->GetSize(&canvasWidth, &canvasHeight); |
8826f46f | 640 | |
7bcb11d3 JS |
641 | double zoomScale = (m_currentZoom/100.0); |
642 | int actualWidth = (int)(zoomScale*m_pageWidth*m_previewScale); | |
643 | int actualHeight = (int)(zoomScale*m_pageHeight*m_previewScale); | |
8826f46f | 644 | |
7bcb11d3 JS |
645 | int x = (int)((canvasWidth - actualWidth)/2.0); |
646 | if (x < m_leftMargin) | |
647 | x = m_leftMargin; | |
648 | // int y = m_topMargin; | |
8826f46f VZ |
649 | |
650 | ||
7bcb11d3 JS |
651 | if (!m_previewBitmap) |
652 | { | |
653 | m_previewBitmap = new wxBitmap((int)actualWidth, (int)actualHeight); | |
654 | if (!m_previewBitmap || !m_previewBitmap->Ok()) | |
655 | { | |
656 | if (m_previewBitmap) | |
657 | delete m_previewBitmap; | |
658 | wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK); | |
659 | return FALSE; | |
660 | } | |
661 | } | |
8826f46f | 662 | |
7bcb11d3 JS |
663 | wxMemoryDC memoryDC; |
664 | memoryDC.SelectObject(*m_previewBitmap); | |
8826f46f | 665 | |
7bcb11d3 | 666 | memoryDC.Clear(); |
8826f46f | 667 | |
7bcb11d3 JS |
668 | m_previewPrintout->SetDC(&memoryDC); |
669 | m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight); | |
8826f46f | 670 | |
7bcb11d3 | 671 | m_previewPrintout->OnBeginPrinting(); |
c801d85f | 672 | |
7bcb11d3 JS |
673 | if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage())) |
674 | { | |
675 | wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK); | |
8826f46f | 676 | |
7bcb11d3 | 677 | memoryDC.SelectObject(wxNullBitmap); |
8826f46f | 678 | |
7bcb11d3 JS |
679 | delete m_previewBitmap; |
680 | return FALSE; | |
681 | } | |
8826f46f | 682 | |
7bcb11d3 JS |
683 | m_previewPrintout->OnPrintPage(pageNum); |
684 | m_previewPrintout->OnEndDocument(); | |
685 | m_previewPrintout->OnEndPrinting(); | |
8826f46f | 686 | |
7bcb11d3 | 687 | m_previewPrintout->SetDC(NULL); |
8826f46f | 688 | |
c801d85f | 689 | memoryDC.SelectObject(wxNullBitmap); |
8826f46f | 690 | |
cf2f341a | 691 | wxChar buf[200]; |
7bcb11d3 | 692 | if (m_maxPage != 0) |
cf2f341a | 693 | wxSprintf(buf, _("Page %d of %d"), pageNum, m_maxPage); |
7bcb11d3 | 694 | else |
cf2f341a | 695 | wxSprintf(buf, _("Page %d"), pageNum); |
8826f46f | 696 | |
7bcb11d3 JS |
697 | if (m_previewFrame) |
698 | m_previewFrame->SetStatusText(buf); | |
8826f46f | 699 | |
7bcb11d3 | 700 | return TRUE; |
c801d85f KB |
701 | } |
702 | ||
703 | ||
704 | bool wxPrintPreviewBase::DrawBlankPage(wxWindow *canvas, wxDC& dc) | |
705 | { | |
7bcb11d3 JS |
706 | int canvasWidth, canvasHeight; |
707 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
8826f46f | 708 | |
7bcb11d3 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; | |
8826f46f | 712 | |
7bcb11d3 JS |
713 | float x = (float)((canvasWidth - actualWidth)/2.0); |
714 | if (x < m_leftMargin) | |
715 | x = (float)m_leftMargin; | |
716 | float y = (float)m_topMargin; | |
8826f46f | 717 | |
7bcb11d3 JS |
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); | |
722 | /* | |
723 | dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2)); | |
724 | */ | |
725 | dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset); | |
726 | dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight)); | |
8826f46f | 727 | |
7bcb11d3 JS |
728 | // Draw blank page allowing for 1-pixel border AROUND the actual page |
729 | dc.SetPen(*wxBLACK_PEN); | |
730 | dc.SetBrush(*wxWHITE_BRUSH); | |
8826f46f | 731 | |
7bcb11d3 JS |
732 | /* |
733 | wxRegion update_region = canvas->GetUpdateRegion(); | |
734 | wxRect r = update_region.GetBox(); | |
8826f46f | 735 | |
7bcb11d3 JS |
736 | printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height ); |
737 | */ | |
8826f46f | 738 | |
7bcb11d3 | 739 | dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2)); |
8826f46f | 740 | |
2a47d3c1 JS |
741 | return TRUE; |
742 | } | |
743 | ||
7bcb11d3 | 744 | void wxPrintPreviewBase::SetZoom(int percent) |
2a47d3c1 | 745 | { |
7bcb11d3 JS |
746 | if (m_currentZoom == percent) |
747 | return; | |
8826f46f | 748 | |
7bcb11d3 JS |
749 | m_currentZoom = percent; |
750 | if (m_previewBitmap) | |
751 | { | |
752 | delete m_previewBitmap; | |
753 | m_previewBitmap = NULL; | |
754 | } | |
aff37a19 | 755 | |
8826f46f | 756 | |
7bcb11d3 JS |
757 | if (m_previewCanvas) |
758 | { | |
aff37a19 | 759 | RenderPage(m_currentPage); |
7bcb11d3 JS |
760 | m_previewCanvas->Clear(); |
761 | m_previewCanvas->Refresh(); | |
762 | } | |
2a47d3c1 JS |
763 | } |
764 |