]>
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 | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
523fd221 RR |
13 | #pragma implementation "prntbase.h" |
14 | #pragma implementation "printdlg.h" | |
c801d85f KB |
15 | #endif |
16 | ||
17 | // For compilers that support precompilation, includes "wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
c801d85f KB |
24 | #include "wx/defs.h" |
25 | ||
d427503c VZ |
26 | #if wxUSE_PRINTING_ARCHITECTURE |
27 | ||
c801d85f KB |
28 | #ifndef WX_PRECOMP |
29 | #include "wx/utils.h" | |
30 | #include "wx/dc.h" | |
31 | #include "wx/app.h" | |
32 | #include "wx/msgdlg.h" | |
33 | #include "wx/layout.h" | |
34 | #include "wx/choice.h" | |
35 | #include "wx/button.h" | |
36 | #include "wx/settings.h" | |
37 | #include "wx/dcmemory.h" | |
38 | #include "wx/stattext.h" | |
39 | #include "wx/intl.h" | |
384a1303 | 40 | #include "wx/textdlg.h" |
2b5f62a0 VZ |
41 | #include "wx/sizer.h" |
42 | #endif // !WX_PRECOMP | |
c801d85f KB |
43 | |
44 | #include "wx/prntbase.h" | |
45 | #include "wx/dcprint.h" | |
46 | #include "wx/printdlg.h" | |
e81e3883 | 47 | #include "wx/print.h" |
2a47d3c1 | 48 | #include "wx/module.h" |
c801d85f KB |
49 | |
50 | #include <stdlib.h> | |
51 | #include <string.h> | |
52 | ||
2049ba38 | 53 | #ifdef __WXMSW__ |
d427503c VZ |
54 | #include "wx/msw/private.h" |
55 | #include <commdlg.h> | |
c801d85f | 56 | |
d427503c VZ |
57 | #ifndef __WIN32__ |
58 | #include <print.h> | |
59 | #endif | |
60 | #endif // __WXMSW__ | |
c801d85f | 61 | |
c801d85f KB |
62 | IMPLEMENT_CLASS(wxPrintPreviewBase, wxObject) |
63 | ||
e81e3883 RR |
64 | //---------------------------------------------------------------------------- |
65 | // wxPrintFactory | |
66 | //---------------------------------------------------------------------------- | |
67 | ||
68 | wxPrintFactory *wxPrintFactory::m_factory = NULL; | |
383f6abd | 69 | |
e81e3883 | 70 | void wxPrintFactory::SetPrintFactory( wxPrintFactory *factory ) |
383f6abd WS |
71 | { |
72 | if (wxPrintFactory::m_factory) | |
e81e3883 | 73 | delete wxPrintFactory::m_factory; |
383f6abd WS |
74 | |
75 | wxPrintFactory::m_factory = factory; | |
e81e3883 | 76 | } |
c801d85f | 77 | |
e81e3883 RR |
78 | wxPrintFactory *wxPrintFactory::GetFactory() |
79 | { | |
80 | if (!wxPrintFactory::m_factory) | |
383f6abd | 81 | wxPrintFactory::m_factory = new wxNativePrintFactory; |
c801d85f | 82 | |
e81e3883 RR |
83 | return wxPrintFactory::m_factory; |
84 | } | |
85 | ||
86 | //---------------------------------------------------------------------------- | |
87 | // wxNativePrintFactory | |
88 | //---------------------------------------------------------------------------- | |
89 | ||
90 | wxPrinterBase *wxNativePrintFactory::CreatePrinter( wxPrintDialogData *data ) | |
383f6abd WS |
91 | { |
92 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) | |
e81e3883 RR |
93 | return new wxWindowsPrinter( data ); |
94 | #elif defined(__WXMAC__) | |
95 | return new wxMacPrinter( data ); | |
21708c73 SN |
96 | #elif defined(__WXPM__) |
97 | return new wxOS2Printer( data ); | |
e81e3883 RR |
98 | #else |
99 | return new wxPostScriptPrinter( data ); | |
100 | #endif | |
101 | }; | |
102 | ||
383f6abd | 103 | wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview, |
e81e3883 RR |
104 | wxPrintout *printout, wxPrintDialogData *data ) |
105 | { | |
383f6abd | 106 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
e81e3883 RR |
107 | return new wxWindowsPrintPreview( preview, printout, data ); |
108 | #elif defined(__WXMAC__) | |
109 | return new wxMacPrintPreview( preview, printout, data ); | |
21708c73 SN |
110 | #elif defined(__WXPM__) |
111 | return new wxOS2PrintPreview( preview, printout, data ); | |
e81e3883 RR |
112 | #else |
113 | return new wxPostScriptPrintPreview( preview, printout, data ); | |
114 | #endif | |
115 | } | |
116 | ||
383f6abd | 117 | wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview, |
e81e3883 RR |
118 | wxPrintout *printout, wxPrintData *data ) |
119 | { | |
383f6abd | 120 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
e81e3883 RR |
121 | return new wxWindowsPrintPreview( preview, printout, data ); |
122 | #elif defined(__WXMAC__) | |
123 | return new wxMacPrintPreview( preview, printout, data ); | |
21708c73 SN |
124 | #elif defined(__WXPM__) |
125 | return new wxOS2PrintPreview( preview, printout, data ); | |
e81e3883 RR |
126 | #else |
127 | return new wxPostScriptPrintPreview( preview, printout, data ); | |
128 | #endif | |
129 | } | |
130 | ||
c061373d RR |
131 | wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent, |
132 | wxPrintDialogData *data ) | |
133 | { | |
134 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) | |
135 | return new wxWindowsPrintDialog( parent, data ); | |
136 | #elif defined(__WXMAC__) | |
137 | return new wxMacPrintDialog( parent, data ); | |
138 | #else | |
139 | return new wxGenericPrintDialog( parent, data ); | |
140 | #endif | |
141 | } | |
142 | ||
143 | wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent, | |
144 | wxPrintData *data ) | |
145 | { | |
146 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) | |
147 | return new wxWindowsPrintDialog( parent, data ); | |
148 | #elif defined(__WXMAC__) | |
149 | return new wxMacPrintDialog( parent, data ); | |
150 | #else | |
151 | return new wxGenericPrintDialog( parent, data ); | |
152 | #endif | |
153 | } | |
154 | ||
6038ec8e RR |
155 | bool wxNativePrintFactory::HasPrintSetupDialog() |
156 | { | |
157 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) | |
158 | return false; | |
159 | #elif defined(__WXMAC__) | |
160 | return false; | |
161 | #else | |
162 | // Only here do we need to provide the print setup | |
163 | // dialog ourselves, the other platforms either have | |
164 | // none, don't make it accessible or let you configure | |
165 | // the printer from the wxPrintDialog anyway. | |
166 | return true; | |
167 | #endif | |
168 | ||
169 | } | |
170 | ||
171 | wxDialog *wxNativePrintFactory::CreatePrintSetupDialog( wxWindow *parent, wxPrintData *data ) | |
172 | { | |
173 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) | |
ce403057 WS |
174 | wxUnusedVar(parent); |
175 | wxUnusedVar(data); | |
6038ec8e RR |
176 | return NULL; |
177 | #elif defined(__WXMAC__) | |
ce403057 WS |
178 | wxUnusedVar(parent); |
179 | wxUnusedVar(data); | |
6038ec8e RR |
180 | return NULL; |
181 | #else | |
182 | // Only here do we need to provide the print setup | |
183 | // dialog ourselves, the other platforms either have | |
184 | // none, don't make it accessible or let you configure | |
185 | // the printer from the wxPrintDialog anyway. | |
186 | return new wxGenericPrintSetupDialog( parent, data ); | |
187 | #endif | |
188 | } | |
189 | ||
190 | bool wxNativePrintFactory::HasOwnPrintToFile() | |
191 | { | |
192 | // Only relevant for PostScript and here the | |
193 | // setup dialog provides no "print to file" | |
194 | // option. In the GNOME setup dialog, the | |
195 | // setup dialog has its own print to file. | |
196 | return false; | |
197 | } | |
198 | ||
199 | bool wxNativePrintFactory::HasPrinterLine() | |
200 | { | |
201 | // Only relevant for PostScript for now | |
202 | return true; | |
203 | } | |
204 | ||
205 | wxString wxNativePrintFactory::CreatePrinterLine() | |
206 | { | |
207 | // Only relevant for PostScript for now | |
208 | ||
209 | // We should query "lpstat -d" here | |
210 | return _("Generic PostScript"); | |
211 | } | |
212 | ||
213 | bool wxNativePrintFactory::HasStatusLine() | |
214 | { | |
215 | // Only relevant for PostScript for now | |
216 | return true; | |
217 | } | |
218 | ||
219 | wxString wxNativePrintFactory::CreateStatusLine() | |
220 | { | |
221 | // Only relevant for PostScript for now | |
222 | ||
223 | // We should query "lpstat -r" or "lpstat -p" here | |
224 | return _("Ready"); | |
225 | } | |
226 | ||
8850cbd3 RR |
227 | wxPrintNativeDataBase *wxNativePrintFactory::CreatePrintNativeData() |
228 | { | |
229 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) | |
230 | return new wxWindowsPrintNativeData; | |
231 | #elif defined(__WXMAC__) | |
232 | return new wxMacPrintNativeData; | |
233 | #else | |
234 | return new wxPostScriptPrintNativeData; | |
235 | #endif | |
236 | } | |
237 | ||
238 | //---------------------------------------------------------------------------- | |
239 | // wxPrintNativeDataBase | |
240 | //---------------------------------------------------------------------------- | |
241 | ||
242 | IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase, wxObject) | |
243 | ||
244 | wxPrintNativeDataBase::wxPrintNativeDataBase() | |
245 | { | |
246 | m_ref = 1; | |
247 | } | |
248 | ||
0ab2de15 RR |
249 | //---------------------------------------------------------------------------- |
250 | // wxPrintFactoryModule | |
251 | //---------------------------------------------------------------------------- | |
252 | ||
253 | class wxPrintFactoryModule: public wxModule | |
254 | { | |
255 | public: | |
256 | wxPrintFactoryModule() {} | |
257 | bool OnInit() { return true; } | |
258 | void OnExit() { wxPrintFactory::SetPrintFactory( NULL ); } | |
259 | ||
260 | private: | |
261 | DECLARE_DYNAMIC_CLASS(wxPrintFactoryModule) | |
262 | }; | |
263 | ||
264 | IMPLEMENT_DYNAMIC_CLASS(wxPrintFactoryModule, wxModule) | |
265 | ||
e81e3883 RR |
266 | //---------------------------------------------------------------------------- |
267 | // wxPrinterBase | |
268 | //---------------------------------------------------------------------------- | |
269 | ||
270 | IMPLEMENT_CLASS(wxPrinterBase, wxObject) | |
7bcb11d3 JS |
271 | |
272 | wxPrinterBase::wxPrinterBase(wxPrintDialogData *data) | |
c801d85f | 273 | { |
7bcb11d3 JS |
274 | m_currentPrintout = (wxPrintout *) NULL; |
275 | sm_abortWindow = (wxWindow *) NULL; | |
7e548f6b | 276 | sm_abortIt = false; |
7bcb11d3 JS |
277 | if (data) |
278 | m_printDialogData = (*data); | |
f6bcfd97 | 279 | sm_lastError = wxPRINTER_NO_ERROR; |
c801d85f KB |
280 | } |
281 | ||
34da0970 | 282 | wxWindow *wxPrinterBase::sm_abortWindow = (wxWindow *) NULL; |
7e548f6b | 283 | bool wxPrinterBase::sm_abortIt = false; |
f6bcfd97 | 284 | wxPrinterError wxPrinterBase::sm_lastError = wxPRINTER_NO_ERROR; |
c801d85f | 285 | |
34da0970 | 286 | wxPrinterBase::~wxPrinterBase() |
c801d85f KB |
287 | { |
288 | } | |
289 | ||
fc799548 | 290 | wxWindow *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout * printout) |
c801d85f | 291 | { |
fc799548 | 292 | wxPrintAbortDialog *dialog = new wxPrintAbortDialog(parent, _("Printing ") , wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE); |
8826f46f | 293 | |
fc799548 | 294 | wxBoxSizer *button_sizer = new wxBoxSizer( wxVERTICAL ); |
7e548f6b | 295 | button_sizer->Add( new wxStaticText(dialog, wxID_ANY, _("Please wait while printing\n") + printout->GetTitle() ), 0, wxALL, 10 ); |
fc799548 | 296 | button_sizer->Add( new wxButton( dialog, wxID_CANCEL, wxT("Cancel") ), 0, wxALL | wxALIGN_CENTER, 10 ); |
8826f46f | 297 | |
7e548f6b | 298 | dialog->SetAutoLayout( true ); |
fc799548 JS |
299 | dialog->SetSizer( button_sizer ); |
300 | ||
301 | button_sizer->Fit(dialog); | |
302 | button_sizer->SetSizeHints (dialog) ; | |
8826f46f | 303 | |
7bcb11d3 | 304 | return dialog; |
c801d85f KB |
305 | } |
306 | ||
161f4f73 | 307 | void wxPrinterBase::ReportError(wxWindow *parent, wxPrintout *WXUNUSED(printout), const wxString& message) |
c801d85f | 308 | { |
7bcb11d3 | 309 | wxMessageBox(message, _("Printing Error"), wxOK, parent); |
c801d85f KB |
310 | } |
311 | ||
c061373d RR |
312 | wxPrintDialogData& wxPrinterBase::GetPrintDialogData() const |
313 | { | |
314 | return (wxPrintDialogData&) m_printDialogData; | |
315 | } | |
316 | ||
e81e3883 RR |
317 | //---------------------------------------------------------------------------- |
318 | // wxPrinter | |
319 | //---------------------------------------------------------------------------- | |
320 | ||
321 | IMPLEMENT_CLASS(wxPrinter, wxPrinterBase) | |
322 | ||
323 | wxPrinter::wxPrinter(wxPrintDialogData *data) | |
324 | { | |
325 | m_pimpl = wxPrintFactory::GetFactory()->CreatePrinter( data ); | |
326 | } | |
327 | ||
328 | wxPrinter::~wxPrinter() | |
329 | { | |
330 | delete m_pimpl; | |
331 | } | |
332 | ||
333 | wxWindow *wxPrinter::CreateAbortWindow(wxWindow *parent, wxPrintout *printout) | |
334 | { | |
335 | return m_pimpl->CreateAbortWindow( parent, printout ); | |
336 | } | |
337 | ||
338 | void wxPrinter::ReportError(wxWindow *parent, wxPrintout *printout, const wxString& message) | |
339 | { | |
340 | m_pimpl->ReportError( parent, printout, message ); | |
341 | } | |
342 | ||
343 | bool wxPrinter::Setup(wxWindow *parent) | |
344 | { | |
345 | return m_pimpl->Setup( parent ); | |
346 | } | |
347 | ||
348 | bool wxPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) | |
349 | { | |
350 | return m_pimpl->Print( parent, printout, prompt ); | |
351 | } | |
352 | ||
353 | wxDC* wxPrinter::PrintDialog(wxWindow *parent) | |
354 | { | |
355 | return m_pimpl->PrintDialog( parent ); | |
356 | } | |
357 | ||
c061373d RR |
358 | wxPrintDialogData& wxPrinter::GetPrintDialogData() const |
359 | { | |
360 | return m_pimpl->GetPrintDialogData(); | |
361 | } | |
362 | ||
363 | // --------------------------------------------------------------------------- | |
364 | // wxPrintDialogBase: the common dialog for printing. | |
365 | // --------------------------------------------------------------------------- | |
366 | ||
367 | IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase, wxObject) | |
368 | ||
6ce8c562 VZ |
369 | wxPrintDialogBase::wxPrintDialogBase(wxWindow *parent, |
370 | wxWindowID id, | |
371 | const wxString &title, | |
372 | const wxPoint &pos, | |
373 | const wxSize &size, | |
374 | long style) | |
375 | : wxDialog | |
376 | ( | |
377 | parent, | |
378 | id, | |
379 | title.empty() ? wxString(_("Print")) : title, | |
380 | pos, | |
381 | size, | |
382 | style | |
383 | ) | |
c061373d RR |
384 | { |
385 | } | |
386 | ||
387 | // --------------------------------------------------------------------------- | |
388 | // wxPrintDialog: the common dialog for printing. | |
389 | // --------------------------------------------------------------------------- | |
390 | ||
391 | IMPLEMENT_CLASS(wxPrintDialog, wxObject) | |
392 | ||
393 | wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintDialogData* data) | |
394 | { | |
395 | m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data ); | |
396 | } | |
397 | ||
398 | wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintData* data) | |
399 | { | |
400 | m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data ); | |
401 | } | |
402 | ||
403 | wxPrintDialog::~wxPrintDialog() | |
404 | { | |
405 | delete m_pimpl; | |
406 | } | |
407 | ||
408 | int wxPrintDialog::ShowModal() | |
409 | { | |
410 | return m_pimpl->ShowModal(); | |
411 | } | |
412 | ||
413 | wxPrintDialogData& wxPrintDialog::GetPrintDialogData() | |
414 | { | |
415 | return m_pimpl->GetPrintDialogData(); | |
416 | } | |
417 | ||
418 | wxPrintData& wxPrintDialog::GetPrintData() | |
419 | { | |
420 | return m_pimpl->GetPrintData(); | |
421 | } | |
422 | wxDC *wxPrintDialog::GetPrintDC() | |
423 | { | |
424 | return m_pimpl->GetPrintDC(); | |
425 | } | |
426 | ||
e81e3883 RR |
427 | //---------------------------------------------------------------------------- |
428 | // wxPrintAbortDialog | |
429 | //---------------------------------------------------------------------------- | |
430 | ||
431 | BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog) | |
432 | EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel) | |
433 | END_EVENT_TABLE() | |
434 | ||
435 | void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) | |
436 | { | |
437 | wxPrinterBase::sm_abortIt = true; | |
438 | wxPrinterBase::sm_abortWindow->Show(false); | |
439 | wxPrinterBase::sm_abortWindow->Close(true); | |
440 | wxPrinterBase::sm_abortWindow = (wxWindow *) NULL; | |
441 | } | |
442 | ||
443 | //---------------------------------------------------------------------------- | |
444 | // wxPrintout | |
445 | //---------------------------------------------------------------------------- | |
446 | ||
447 | IMPLEMENT_ABSTRACT_CLASS(wxPrintout, wxObject) | |
7bcb11d3 | 448 | |
34da0970 | 449 | wxPrintout::wxPrintout(const wxString& title) |
c801d85f | 450 | { |
7bcb11d3 JS |
451 | m_printoutTitle = title ; |
452 | m_printoutDC = (wxDC *) NULL; | |
453 | m_pageWidthMM = 0; | |
454 | m_pageHeightMM = 0; | |
455 | m_pageWidthPixels = 0; | |
456 | m_pageHeightPixels = 0; | |
457 | m_PPIScreenX = 0; | |
458 | m_PPIScreenY = 0; | |
459 | m_PPIPrinterX = 0; | |
460 | m_PPIPrinterY = 0; | |
7e548f6b | 461 | m_isPreview = false; |
c801d85f KB |
462 | } |
463 | ||
34da0970 | 464 | wxPrintout::~wxPrintout() |
c801d85f | 465 | { |
c801d85f KB |
466 | } |
467 | ||
468 | bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage), int WXUNUSED(endPage)) | |
469 | { | |
fc799548 | 470 | return GetDC()->StartDoc(_("Printing ") + m_printoutTitle); |
c801d85f KB |
471 | } |
472 | ||
34da0970 | 473 | void wxPrintout::OnEndDocument() |
c801d85f | 474 | { |
7bcb11d3 | 475 | GetDC()->EndDoc(); |
c801d85f KB |
476 | } |
477 | ||
34da0970 | 478 | void wxPrintout::OnBeginPrinting() |
c801d85f KB |
479 | { |
480 | } | |
481 | ||
34da0970 | 482 | void wxPrintout::OnEndPrinting() |
c801d85f KB |
483 | { |
484 | } | |
485 | ||
486 | bool wxPrintout::HasPage(int page) | |
487 | { | |
7bcb11d3 | 488 | return (page == 1); |
c801d85f KB |
489 | } |
490 | ||
491 | void wxPrintout::GetPageInfo(int *minPage, int *maxPage, int *fromPage, int *toPage) | |
492 | { | |
7bcb11d3 JS |
493 | *minPage = 1; |
494 | *maxPage = 32000; | |
495 | *fromPage = 1; | |
496 | *toPage = 1; | |
c801d85f KB |
497 | } |
498 | ||
e81e3883 RR |
499 | //---------------------------------------------------------------------------- |
500 | // wxPreviewCanvas | |
501 | //---------------------------------------------------------------------------- | |
502 | ||
503 | IMPLEMENT_CLASS(wxPreviewCanvas, wxWindow) | |
504 | ||
505 | BEGIN_EVENT_TABLE(wxPreviewCanvas, wxScrolledWindow) | |
506 | EVT_PAINT(wxPreviewCanvas::OnPaint) | |
507 | EVT_CHAR(wxPreviewCanvas::OnChar) | |
508 | EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged) | |
509 | END_EVENT_TABLE() | |
7bcb11d3 | 510 | |
e9cafd42 VZ |
511 | // VZ: the current code doesn't refresh properly without |
512 | // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have | |
513 | // really horrible flicker when resizing the preview frame, but without | |
514 | // this style it simply doesn't work correctly at all... | |
c801d85f | 515 | wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase *preview, wxWindow *parent, |
7bcb11d3 | 516 | const wxPoint& pos, const wxSize& size, long style, const wxString& name): |
7e548f6b | 517 | wxScrolledWindow(parent, wxID_ANY, pos, size, style | wxFULL_REPAINT_ON_RESIZE, name) |
c801d85f | 518 | { |
7bcb11d3 | 519 | m_printPreview = preview; |
46b24ef9 JS |
520 | #ifdef __WXMAC__ |
521 | // The app workspace colour is always white, but we should have | |
522 | // a contrast with the page. | |
ca5020c2 | 523 | wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW; |
46b24ef9 JS |
524 | #else |
525 | wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE; | |
e71fd398 | 526 | #endif |
46b24ef9 | 527 | SetBackgroundColour(wxSystemSettings::GetColour(colourIndex)); |
8826f46f | 528 | |
d2b354f9 | 529 | SetScrollbars(10, 10, 100, 100); |
c801d85f KB |
530 | } |
531 | ||
34da0970 | 532 | wxPreviewCanvas::~wxPreviewCanvas() |
c801d85f KB |
533 | { |
534 | } | |
535 | ||
536 | void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
537 | { | |
7bcb11d3 JS |
538 | wxPaintDC dc(this); |
539 | PrepareDC( dc ); | |
8826f46f | 540 | |
a56fcaaf | 541 | /* |
809934d2 RR |
542 | #ifdef __WXGTK__ |
543 | if (!GetUpdateRegion().IsEmpty()) | |
544 | dc.SetClippingRegion( GetUpdateRegion() ); | |
545 | #endif | |
a56fcaaf | 546 | */ |
809934d2 | 547 | |
7bcb11d3 JS |
548 | if (m_printPreview) |
549 | { | |
550 | m_printPreview->PaintPage(this, dc); | |
551 | } | |
c801d85f KB |
552 | } |
553 | ||
554 | // Responds to colour changes, and passes event on to children. | |
555 | void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event) | |
556 | { | |
46b24ef9 JS |
557 | #ifdef __WXMAC__ |
558 | // The app workspace colour is always white, but we should have | |
559 | // a contrast with the page. | |
ca5020c2 | 560 | wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW; |
46b24ef9 JS |
561 | #else |
562 | wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE; | |
e71fd398 | 563 | #endif |
46b24ef9 | 564 | SetBackgroundColour(wxSystemSettings::GetColour(colourIndex)); |
7bcb11d3 | 565 | Refresh(); |
8826f46f | 566 | |
7bcb11d3 JS |
567 | // Propagate the event to the non-top-level children |
568 | wxWindow::OnSysColourChanged(event); | |
c801d85f KB |
569 | } |
570 | ||
d2b354f9 JS |
571 | void wxPreviewCanvas::OnChar(wxKeyEvent &event) |
572 | { | |
b38b0d22 | 573 | wxPreviewControlBar* controlBar = ((wxPreviewFrame*) GetParent())->GetControlBar(); |
d2b354f9 JS |
574 | if (event.GetKeyCode() == WXK_ESCAPE) |
575 | { | |
7e548f6b | 576 | ((wxPreviewFrame*) GetParent())->Close(true); |
d2b354f9 | 577 | return; |
e71fd398 | 578 | } |
b38b0d22 JS |
579 | else if (event.GetKeyCode() == WXK_TAB) |
580 | { | |
581 | controlBar->OnGoto(); | |
582 | return; | |
d2b354f9 | 583 | } |
b38b0d22 JS |
584 | else if (event.GetKeyCode() == WXK_RETURN) |
585 | { | |
586 | controlBar->OnPrint(); | |
587 | return; | |
588 | } | |
589 | ||
d2b354f9 JS |
590 | if (!event.ControlDown()) |
591 | { | |
592 | event.Skip(); | |
593 | return; | |
594 | } | |
e71fd398 | 595 | |
b38b0d22 JS |
596 | switch(event.GetKeyCode()) |
597 | { | |
598 | case WXK_NEXT: | |
599 | controlBar->OnNext(); break; | |
600 | case WXK_PRIOR: | |
601 | controlBar->OnPrevious(); break; | |
602 | case WXK_HOME: | |
603 | controlBar->OnFirst(); break; | |
604 | case WXK_END: | |
605 | controlBar->OnLast(); break; | |
606 | default: | |
607 | event.Skip(); | |
608 | } | |
d2b354f9 JS |
609 | } |
610 | ||
e81e3883 RR |
611 | //---------------------------------------------------------------------------- |
612 | // wxPreviewControlBar | |
613 | //---------------------------------------------------------------------------- | |
614 | ||
615 | IMPLEMENT_CLASS(wxPreviewControlBar, wxWindow) | |
c801d85f KB |
616 | |
617 | BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel) | |
8826f46f | 618 | EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose) |
90b6b974 | 619 | EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrintButton) |
0f90ec93 KB |
620 | EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPreviousButton) |
621 | EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNextButton) | |
bf89b538 JS |
622 | EVT_BUTTON(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnFirstButton) |
623 | EVT_BUTTON(wxID_PREVIEW_LAST, wxPreviewControlBar::OnLastButton) | |
624 | EVT_BUTTON(wxID_PREVIEW_GOTO, wxPreviewControlBar::OnGotoButton) | |
8826f46f VZ |
625 | EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoom) |
626 | EVT_PAINT(wxPreviewControlBar::OnPaint) | |
c801d85f | 627 | END_EVENT_TABLE() |
7bcb11d3 | 628 | |
c801d85f | 629 | wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons, |
7bcb11d3 JS |
630 | wxWindow *parent, const wxPoint& pos, const wxSize& size, |
631 | long style, const wxString& name): | |
7e548f6b | 632 | wxPanel(parent, wxID_ANY, pos, size, style, name) |
c801d85f | 633 | { |
7bcb11d3 JS |
634 | m_printPreview = preview; |
635 | m_closeButton = (wxButton *) NULL; | |
636 | m_nextPageButton = (wxButton *) NULL; | |
637 | m_previousPageButton = (wxButton *) NULL; | |
638 | m_printButton = (wxButton *) NULL; | |
639 | m_zoomControl = (wxChoice *) NULL; | |
640 | m_buttonFlags = buttons; | |
c801d85f KB |
641 | } |
642 | ||
34da0970 | 643 | wxPreviewControlBar::~wxPreviewControlBar() |
c801d85f KB |
644 | { |
645 | } | |
646 | ||
647 | void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
648 | { | |
7bcb11d3 | 649 | wxPaintDC dc(this); |
8826f46f | 650 | |
7bcb11d3 JS |
651 | int w, h; |
652 | GetSize(&w, &h); | |
653 | dc.SetPen(*wxBLACK_PEN); | |
654 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
655 | dc.DrawLine( 0, h-1, w, h-1 ); | |
c801d85f KB |
656 | } |
657 | ||
c330a2cf | 658 | void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 659 | { |
7bcb11d3 | 660 | wxPreviewFrame *frame = (wxPreviewFrame *)GetParent(); |
7e548f6b | 661 | frame->Close(true); |
c801d85f KB |
662 | } |
663 | ||
b38b0d22 | 664 | void wxPreviewControlBar::OnPrint(void) |
c801d85f | 665 | { |
7bcb11d3 | 666 | wxPrintPreviewBase *preview = GetPrintPreview(); |
7e548f6b | 667 | preview->Print(true); |
c801d85f KB |
668 | } |
669 | ||
0f90ec93 | 670 | void wxPreviewControlBar::OnNext(void) |
c801d85f | 671 | { |
7bcb11d3 JS |
672 | wxPrintPreviewBase *preview = GetPrintPreview(); |
673 | if (preview) | |
c801d85f | 674 | { |
7bcb11d3 JS |
675 | int currentPage = preview->GetCurrentPage(); |
676 | if ((preview->GetMaxPage() > 0) && | |
677 | (currentPage < preview->GetMaxPage()) && | |
678 | preview->GetPrintout()->HasPage(currentPage + 1)) | |
679 | { | |
680 | preview->SetCurrentPage(currentPage + 1); | |
681 | } | |
c801d85f | 682 | } |
c801d85f KB |
683 | } |
684 | ||
0f90ec93 | 685 | void wxPreviewControlBar::OnPrevious(void) |
c801d85f | 686 | { |
7bcb11d3 JS |
687 | wxPrintPreviewBase *preview = GetPrintPreview(); |
688 | if (preview) | |
c801d85f | 689 | { |
7bcb11d3 JS |
690 | int currentPage = preview->GetCurrentPage(); |
691 | if ((preview->GetMinPage() > 0) && | |
692 | (currentPage > preview->GetMinPage()) && | |
693 | preview->GetPrintout()->HasPage(currentPage - 1)) | |
694 | { | |
695 | preview->SetCurrentPage(currentPage - 1); | |
696 | } | |
c801d85f | 697 | } |
c801d85f KB |
698 | } |
699 | ||
bf89b538 JS |
700 | void wxPreviewControlBar::OnFirst(void) |
701 | { | |
702 | wxPrintPreviewBase *preview = GetPrintPreview(); | |
703 | if (preview) | |
704 | { | |
705 | int currentPage = preview->GetMinPage(); | |
706 | if (preview->GetPrintout()->HasPage(currentPage)) | |
707 | { | |
708 | preview->SetCurrentPage(currentPage); | |
709 | } | |
710 | } | |
711 | } | |
712 | ||
713 | void wxPreviewControlBar::OnLast(void) | |
714 | { | |
715 | wxPrintPreviewBase *preview = GetPrintPreview(); | |
716 | if (preview) | |
717 | { | |
718 | int currentPage = preview->GetMaxPage(); | |
719 | if (preview->GetPrintout()->HasPage(currentPage)) | |
720 | { | |
721 | preview->SetCurrentPage(currentPage); | |
722 | } | |
723 | } | |
724 | } | |
725 | ||
726 | void wxPreviewControlBar::OnGoto(void) | |
727 | { | |
728 | wxPrintPreviewBase *preview = GetPrintPreview(); | |
729 | if (preview) | |
730 | { | |
731 | long currentPage; | |
732 | ||
733 | if (preview->GetMinPage() > 0) | |
734 | { | |
735 | wxString strPrompt; | |
736 | wxString strPage; | |
737 | ||
d2b354f9 | 738 | strPrompt.Printf( _("Enter a page number between %d and %d:"), |
bf89b538 | 739 | preview->GetMinPage(), preview->GetMaxPage()); |
7e99eddf | 740 | strPage.Printf( wxT("%d"), preview->GetCurrentPage() ); |
bf89b538 JS |
741 | |
742 | strPage = | |
d2b354f9 | 743 | wxGetTextFromUser( strPrompt, _("Goto Page"), strPage, GetParent()); |
bf89b538 JS |
744 | |
745 | if ( strPage.ToLong( ¤tPage ) ) | |
746 | if (preview->GetPrintout()->HasPage(currentPage)) | |
747 | { | |
748 | preview->SetCurrentPage(currentPage); | |
749 | } | |
750 | } | |
751 | } | |
752 | } | |
753 | ||
c801d85f KB |
754 | void wxPreviewControlBar::OnZoom(wxCommandEvent& WXUNUSED(event)) |
755 | { | |
7bcb11d3 JS |
756 | int zoom = GetZoomControl(); |
757 | if (GetPrintPreview()) | |
758 | GetPrintPreview()->SetZoom(zoom); | |
c801d85f KB |
759 | } |
760 | ||
34da0970 | 761 | void wxPreviewControlBar::CreateButtons() |
c801d85f | 762 | { |
7bcb11d3 | 763 | SetSize(0, 0, 400, 40); |
8826f46f | 764 | |
9dff8515 | 765 | wxBoxSizer *item0 = new wxBoxSizer( wxHORIZONTAL ); |
e71fd398 | 766 | |
9dff8515 JS |
767 | m_closeButton = new wxButton( this, wxID_PREVIEW_CLOSE, _("&Close"), wxDefaultPosition, wxDefaultSize, 0 ); |
768 | item0->Add( m_closeButton, 0, wxALIGN_CENTRE|wxALL, 5 ); | |
e71fd398 | 769 | |
7bcb11d3 JS |
770 | if (m_buttonFlags & wxPREVIEW_PRINT) |
771 | { | |
9dff8515 JS |
772 | m_printButton = new wxButton( this, wxID_PREVIEW_PRINT, _("&Print..."), wxDefaultPosition, wxDefaultSize, 0 ); |
773 | item0->Add( m_printButton, 0, wxALIGN_CENTRE|wxALL, 5 ); | |
7bcb11d3 | 774 | } |
e71fd398 | 775 | |
bf89b538 JS |
776 | if (m_buttonFlags & wxPREVIEW_FIRST) |
777 | { | |
681aeb18 | 778 | m_firstPageButton = new wxButton( this, wxID_PREVIEW_FIRST, _("|<<"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT ); |
9dff8515 | 779 | item0->Add( m_firstPageButton, 0, wxALIGN_CENTRE|wxALL, 5 ); |
bf89b538 | 780 | } |
e71fd398 | 781 | |
7bcb11d3 JS |
782 | if (m_buttonFlags & wxPREVIEW_PREVIOUS) |
783 | { | |
681aeb18 | 784 | m_previousPageButton = new wxButton( this, wxID_PREVIEW_PREVIOUS, _("<<"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT ); |
9dff8515 | 785 | item0->Add( m_previousPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 ); |
7bcb11d3 | 786 | } |
e71fd398 | 787 | |
7bcb11d3 JS |
788 | if (m_buttonFlags & wxPREVIEW_NEXT) |
789 | { | |
681aeb18 | 790 | m_nextPageButton = new wxButton( this, wxID_PREVIEW_NEXT, _(">>"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT ); |
9dff8515 | 791 | item0->Add( m_nextPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 ); |
bf89b538 | 792 | } |
e71fd398 | 793 | |
bf89b538 JS |
794 | if (m_buttonFlags & wxPREVIEW_LAST) |
795 | { | |
681aeb18 | 796 | m_lastPageButton = new wxButton( this, wxID_PREVIEW_LAST, _(">>|"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT ); |
9dff8515 | 797 | item0->Add( m_lastPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 ); |
bf89b538 | 798 | } |
e71fd398 | 799 | |
bf89b538 JS |
800 | if (m_buttonFlags & wxPREVIEW_GOTO) |
801 | { | |
9dff8515 JS |
802 | m_gotoPageButton = new wxButton( this, wxID_PREVIEW_GOTO, _("&Goto..."), wxDefaultPosition, wxDefaultSize, 0 ); |
803 | item0->Add( m_gotoPageButton, 0, wxALIGN_CENTRE|wxALL, 5 ); | |
7bcb11d3 | 804 | } |
e71fd398 | 805 | |
7bcb11d3 JS |
806 | if (m_buttonFlags & wxPREVIEW_ZOOM) |
807 | { | |
e71fd398 | 808 | wxString choices[] = |
c25ccf85 | 809 | { |
2b5f62a0 | 810 | wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"), |
9dff8515 JS |
811 | wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"), |
812 | wxT("120%"), wxT("150%"), wxT("200%") | |
c25ccf85 | 813 | }; |
c25ccf85 | 814 | int n = WXSIZEOF(choices); |
e71fd398 | 815 | |
7e548f6b | 816 | m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(70,wxDefaultCoord), n, choices, 0 ); |
9dff8515 | 817 | item0->Add( m_zoomControl, 0, wxALIGN_CENTRE|wxALL, 5 ); |
7bcb11d3 JS |
818 | SetZoomControl(m_printPreview->GetZoom()); |
819 | } | |
8826f46f | 820 | |
9dff8515 JS |
821 | SetSizer(item0); |
822 | item0->Fit(this); | |
c801d85f KB |
823 | } |
824 | ||
825 | void wxPreviewControlBar::SetZoomControl(int zoom) | |
826 | { | |
7bcb11d3 | 827 | if (m_zoomControl) |
963907fa RR |
828 | { |
829 | int n, count = m_zoomControl->GetCount(); | |
830 | long val; | |
831 | for (n=0; n<count; n++) | |
832 | { | |
833 | if (m_zoomControl->GetString(n).BeforeFirst(wxT('%')).ToLong(&val) && | |
834 | (val >= long(zoom))) | |
835 | { | |
836 | m_zoomControl->SetSelection(n); | |
837 | return; | |
838 | } | |
839 | } | |
7e548f6b | 840 | |
963907fa RR |
841 | m_zoomControl->SetSelection(count-1); |
842 | } | |
c801d85f KB |
843 | } |
844 | ||
34da0970 | 845 | int wxPreviewControlBar::GetZoomControl() |
c801d85f | 846 | { |
963907fa | 847 | if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxEmptyString)) |
7bcb11d3 | 848 | { |
963907fa RR |
849 | long val; |
850 | if (m_zoomControl->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val)) | |
851 | return int(val); | |
7bcb11d3 | 852 | } |
7e548f6b | 853 | |
963907fa | 854 | return 0; |
c801d85f KB |
855 | } |
856 | ||
857 | ||
858 | /* | |
7bcb11d3 JS |
859 | * Preview frame |
860 | */ | |
c801d85f | 861 | |
e81e3883 RR |
862 | IMPLEMENT_CLASS(wxPreviewFrame, wxFrame) |
863 | ||
e3065973 | 864 | BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame) |
0f90ec93 | 865 | EVT_CLOSE(wxPreviewFrame::OnCloseWindow) |
e3065973 JS |
866 | END_EVENT_TABLE() |
867 | ||
a5ae8241 | 868 | wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxWindow *parent, const wxString& title, |
7bcb11d3 | 869 | const wxPoint& pos, const wxSize& size, long style, const wxString& name): |
7e548f6b | 870 | wxFrame(parent, wxID_ANY, title, pos, size, style, name) |
c801d85f | 871 | { |
7bcb11d3 JS |
872 | m_printPreview = preview; |
873 | m_controlBar = NULL; | |
874 | m_previewCanvas = NULL; | |
7c995553 | 875 | m_windowDisabler = NULL; |
46b24ef9 | 876 | |
a5ae8241 | 877 | // Give the application icon |
46b24ef9 JS |
878 | #ifdef __WXMSW__ |
879 | wxFrame* topFrame = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame); | |
880 | if (topFrame) | |
881 | SetIcon(topFrame->GetIcon()); | |
e71fd398 | 882 | #endif |
c801d85f KB |
883 | } |
884 | ||
34da0970 | 885 | wxPreviewFrame::~wxPreviewFrame() |
c801d85f KB |
886 | { |
887 | } | |
888 | ||
74e3313b | 889 | void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
c801d85f | 890 | { |
7c995553 VZ |
891 | if (m_windowDisabler) |
892 | delete m_windowDisabler; | |
8826f46f | 893 | |
7bcb11d3 JS |
894 | // Need to delete the printout and the print preview |
895 | wxPrintout *printout = m_printPreview->GetPrintout(); | |
896 | if (printout) | |
897 | { | |
898 | delete printout; | |
899 | m_printPreview->SetPrintout(NULL); | |
900 | m_printPreview->SetCanvas(NULL); | |
901 | m_printPreview->SetFrame(NULL); | |
902 | } | |
903 | delete m_printPreview; | |
8826f46f | 904 | |
7bcb11d3 | 905 | Destroy(); |
c801d85f KB |
906 | } |
907 | ||
34da0970 | 908 | void wxPreviewFrame::Initialize() |
c801d85f | 909 | { |
3080bf59 | 910 | #if wxUSE_STATUSBAR |
7bcb11d3 | 911 | CreateStatusBar(); |
3080bf59 | 912 | #endif |
7bcb11d3 JS |
913 | CreateCanvas(); |
914 | CreateControlBar(); | |
8826f46f | 915 | |
7bcb11d3 JS |
916 | m_printPreview->SetCanvas(m_previewCanvas); |
917 | m_printPreview->SetFrame(this); | |
8826f46f | 918 | |
9dff8515 | 919 | wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL ); |
8826f46f | 920 | |
9dff8515 JS |
921 | item0->Add( m_controlBar, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5 ); |
922 | item0->Add( m_previewCanvas, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 ); | |
8826f46f | 923 | |
7e548f6b | 924 | SetAutoLayout( true ); |
9dff8515 | 925 | SetSizer( item0 ); |
8826f46f | 926 | |
7c995553 | 927 | m_windowDisabler = new wxWindowDisabler(this); |
8826f46f | 928 | |
7bcb11d3 | 929 | Layout(); |
e71fd398 | 930 | |
d2b354f9 JS |
931 | m_printPreview->AdjustScrollbars(m_previewCanvas); |
932 | m_previewCanvas->SetFocus(); | |
933 | m_controlBar->SetFocus(); | |
c801d85f KB |
934 | } |
935 | ||
34da0970 | 936 | void wxPreviewFrame::CreateCanvas() |
c801d85f | 937 | { |
7bcb11d3 | 938 | m_previewCanvas = new wxPreviewCanvas(m_printPreview, this); |
c801d85f KB |
939 | } |
940 | ||
34da0970 | 941 | void wxPreviewFrame::CreateControlBar() |
c801d85f | 942 | { |
7bcb11d3 JS |
943 | long buttons = wxPREVIEW_DEFAULT; |
944 | if (m_printPreview->GetPrintoutForPrinting()) | |
945 | buttons |= wxPREVIEW_PRINT; | |
8826f46f | 946 | |
7bcb11d3 JS |
947 | m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0, 0), wxSize(400, 40)); |
948 | m_controlBar->CreateButtons(); | |
c801d85f | 949 | } |
7bcb11d3 | 950 | |
c801d85f | 951 | /* |
7bcb11d3 JS |
952 | * Print preview |
953 | */ | |
c801d85f | 954 | |
8826f46f VZ |
955 | wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout, |
956 | wxPrintout *printoutForPrinting, | |
957 | wxPrintData *data) | |
958 | { | |
959 | if (data) | |
960 | m_printDialogData = (*data); | |
961 | ||
962 | Init(printout, printoutForPrinting); | |
963 | } | |
964 | ||
965 | wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout, | |
966 | wxPrintout *printoutForPrinting, | |
967 | wxPrintDialogData *data) | |
968 | { | |
969 | if (data) | |
970 | m_printDialogData = (*data); | |
971 | ||
972 | Init(printout, printoutForPrinting); | |
973 | } | |
974 | ||
975 | void wxPrintPreviewBase::Init(wxPrintout *printout, | |
976 | wxPrintout *printoutForPrinting) | |
c801d85f | 977 | { |
7e548f6b | 978 | m_isOk = true; |
7bcb11d3 JS |
979 | m_previewPrintout = printout; |
980 | if (m_previewPrintout) | |
7e548f6b | 981 | m_previewPrintout->SetIsPreview(true); |
8826f46f | 982 | |
7bcb11d3 | 983 | m_printPrintout = printoutForPrinting; |
8826f46f | 984 | |
7bcb11d3 JS |
985 | m_previewCanvas = NULL; |
986 | m_previewFrame = NULL; | |
987 | m_previewBitmap = NULL; | |
988 | m_currentPage = 1; | |
c25ccf85 | 989 | m_currentZoom = 70; |
7bcb11d3 JS |
990 | m_topMargin = 40; |
991 | m_leftMargin = 40; | |
992 | m_pageWidth = 0; | |
993 | m_pageHeight = 0; | |
7e548f6b | 994 | m_printingPrepared = false; |
d2b354f9 JS |
995 | m_minPage = 1; |
996 | m_maxPage = 1; | |
c801d85f KB |
997 | } |
998 | ||
34da0970 | 999 | wxPrintPreviewBase::~wxPrintPreviewBase() |
c801d85f | 1000 | { |
7bcb11d3 JS |
1001 | if (m_previewPrintout) |
1002 | delete m_previewPrintout; | |
1003 | if (m_previewBitmap) | |
1004 | delete m_previewBitmap; | |
1005 | if (m_printPrintout) | |
1006 | delete m_printPrintout; | |
c801d85f KB |
1007 | } |
1008 | ||
1009 | bool wxPrintPreviewBase::SetCurrentPage(int pageNum) | |
1010 | { | |
7bcb11d3 | 1011 | if (m_currentPage == pageNum) |
7e548f6b | 1012 | return true; |
8826f46f | 1013 | |
7bcb11d3 JS |
1014 | m_currentPage = pageNum; |
1015 | if (m_previewBitmap) | |
1016 | { | |
1017 | delete m_previewBitmap; | |
1018 | m_previewBitmap = NULL; | |
1019 | } | |
e71fd398 | 1020 | |
7bcb11d3 JS |
1021 | if (m_previewCanvas) |
1022 | { | |
d2b354f9 | 1023 | AdjustScrollbars(m_previewCanvas); |
e71fd398 | 1024 | |
9eee81a4 | 1025 | if (!RenderPage(pageNum)) |
7e548f6b | 1026 | return false; |
7bcb11d3 | 1027 | m_previewCanvas->Refresh(); |
d2b354f9 | 1028 | m_previewCanvas->SetFocus(); |
7bcb11d3 | 1029 | } |
7e548f6b | 1030 | return true; |
c801d85f KB |
1031 | } |
1032 | ||
383f6abd | 1033 | int wxPrintPreviewBase::GetCurrentPage() const |
e81e3883 | 1034 | { return m_currentPage; }; |
383f6abd | 1035 | void wxPrintPreviewBase::SetPrintout(wxPrintout *printout) |
e81e3883 | 1036 | { m_previewPrintout = printout; }; |
383f6abd | 1037 | wxPrintout *wxPrintPreviewBase::GetPrintout() const |
e81e3883 | 1038 | { return m_previewPrintout; }; |
383f6abd | 1039 | wxPrintout *wxPrintPreviewBase::GetPrintoutForPrinting() const |
e81e3883 | 1040 | { return m_printPrintout; }; |
383f6abd | 1041 | void wxPrintPreviewBase::SetFrame(wxFrame *frame) |
e81e3883 | 1042 | { m_previewFrame = frame; }; |
383f6abd | 1043 | void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas *canvas) |
e81e3883 | 1044 | { m_previewCanvas = canvas; }; |
383f6abd | 1045 | wxFrame *wxPrintPreviewBase::GetFrame() const |
e81e3883 | 1046 | { return m_previewFrame; } |
383f6abd | 1047 | wxPreviewCanvas *wxPrintPreviewBase::GetCanvas() const |
e81e3883 RR |
1048 | { return m_previewCanvas; } |
1049 | ||
d2b354f9 | 1050 | bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas *canvas, wxDC& dc) |
c801d85f | 1051 | { |
7bcb11d3 | 1052 | DrawBlankPage(canvas, dc); |
8826f46f | 1053 | |
7bcb11d3 | 1054 | if (!m_previewBitmap) |
9eee81a4 | 1055 | if (!RenderPage(m_currentPage)) |
7e548f6b | 1056 | return false; |
8826f46f | 1057 | |
7bcb11d3 | 1058 | if (!m_previewBitmap) |
7e548f6b | 1059 | return false; |
8826f46f | 1060 | |
7bcb11d3 | 1061 | if (!canvas) |
7e548f6b | 1062 | return false; |
8826f46f | 1063 | |
7bcb11d3 JS |
1064 | int canvasWidth, canvasHeight; |
1065 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
8826f46f | 1066 | |
7bcb11d3 JS |
1067 | double zoomScale = ((float)m_currentZoom/(float)100); |
1068 | double actualWidth = (zoomScale*m_pageWidth*m_previewScale); | |
1069 | // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale); | |
8826f46f | 1070 | |
7bcb11d3 JS |
1071 | int x = (int) ((canvasWidth - actualWidth)/2.0); |
1072 | if (x < m_leftMargin) | |
1073 | x = m_leftMargin; | |
1074 | int y = m_topMargin; | |
8826f46f | 1075 | |
7bcb11d3 JS |
1076 | wxMemoryDC temp_dc; |
1077 | temp_dc.SelectObject(*m_previewBitmap); | |
8826f46f | 1078 | |
7bcb11d3 | 1079 | dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0); |
8826f46f | 1080 | |
7bcb11d3 | 1081 | temp_dc.SelectObject(wxNullBitmap); |
8826f46f | 1082 | |
7e548f6b | 1083 | return true; |
c801d85f KB |
1084 | } |
1085 | ||
d2b354f9 JS |
1086 | // Adjusts the scrollbars for the current scale |
1087 | void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas *canvas) | |
1088 | { | |
1089 | if (!canvas) | |
1090 | return ; | |
1091 | ||
1092 | int canvasWidth, canvasHeight; | |
1093 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
1094 | ||
1095 | double zoomScale = ((float)m_currentZoom/(float)100); | |
1096 | double actualWidth = (zoomScale*m_pageWidth*m_previewScale); | |
1097 | double actualHeight = (zoomScale*m_pageHeight*m_previewScale); | |
1098 | ||
1099 | // Set the scrollbars appropriately | |
8b58d2df VZ |
1100 | int totalWidth = (int)(actualWidth + 2*m_leftMargin); |
1101 | int totalHeight = (int)(actualHeight + 2*m_topMargin); | |
d2b354f9 JS |
1102 | int scrollUnitsX = totalWidth/10; |
1103 | int scrollUnitsY = totalHeight/10; | |
1104 | wxSize virtualSize = canvas->GetVirtualSize(); | |
1105 | if (virtualSize.GetWidth() != totalWidth || virtualSize.GetHeight() != totalHeight) | |
7e548f6b | 1106 | canvas->SetScrollbars(10, 10, scrollUnitsX, scrollUnitsY, 0, 0, true); |
d2b354f9 JS |
1107 | } |
1108 | ||
c801d85f KB |
1109 | bool wxPrintPreviewBase::RenderPage(int pageNum) |
1110 | { | |
f6bcfd97 BP |
1111 | wxBusyCursor busy; |
1112 | ||
7bcb11d3 | 1113 | int canvasWidth, canvasHeight; |
8826f46f | 1114 | |
7bcb11d3 | 1115 | if (!m_previewCanvas) |
c801d85f | 1116 | { |
f6bcfd97 | 1117 | wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!")); |
771779ed | 1118 | |
7e548f6b | 1119 | return false; |
c801d85f | 1120 | } |
7bcb11d3 | 1121 | m_previewCanvas->GetSize(&canvasWidth, &canvasHeight); |
8826f46f | 1122 | |
7bcb11d3 JS |
1123 | double zoomScale = (m_currentZoom/100.0); |
1124 | int actualWidth = (int)(zoomScale*m_pageWidth*m_previewScale); | |
1125 | int actualHeight = (int)(zoomScale*m_pageHeight*m_previewScale); | |
8826f46f | 1126 | |
7bcb11d3 JS |
1127 | if (!m_previewBitmap) |
1128 | { | |
1129 | m_previewBitmap = new wxBitmap((int)actualWidth, (int)actualHeight); | |
1130 | if (!m_previewBitmap || !m_previewBitmap->Ok()) | |
1131 | { | |
9eee81a4 | 1132 | if (m_previewBitmap) { |
7bcb11d3 | 1133 | delete m_previewBitmap; |
9eee81a4 GD |
1134 | m_previewBitmap = NULL; |
1135 | } | |
7bcb11d3 | 1136 | wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK); |
7e548f6b | 1137 | return false; |
7bcb11d3 JS |
1138 | } |
1139 | } | |
8826f46f | 1140 | |
7bcb11d3 JS |
1141 | wxMemoryDC memoryDC; |
1142 | memoryDC.SelectObject(*m_previewBitmap); | |
8826f46f | 1143 | |
7bcb11d3 | 1144 | memoryDC.Clear(); |
8826f46f | 1145 | |
7bcb11d3 JS |
1146 | m_previewPrintout->SetDC(&memoryDC); |
1147 | m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight); | |
8826f46f | 1148 | |
1044a386 JS |
1149 | // Need to delay OnPreparePrinting until here, so we have enough information. |
1150 | if (!m_printingPrepared) | |
1151 | { | |
1152 | m_previewPrintout->OnPreparePrinting(); | |
d2b354f9 JS |
1153 | int selFrom, selTo; |
1154 | m_previewPrintout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo); | |
7e548f6b | 1155 | m_printingPrepared = true; |
1044a386 JS |
1156 | } |
1157 | ||
7bcb11d3 | 1158 | m_previewPrintout->OnBeginPrinting(); |
c801d85f | 1159 | |
7bcb11d3 JS |
1160 | if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage())) |
1161 | { | |
1162 | wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK); | |
8826f46f | 1163 | |
7bcb11d3 | 1164 | memoryDC.SelectObject(wxNullBitmap); |
8826f46f | 1165 | |
7bcb11d3 | 1166 | delete m_previewBitmap; |
9eee81a4 | 1167 | m_previewBitmap = NULL; |
7e548f6b | 1168 | return false; |
7bcb11d3 | 1169 | } |
8826f46f | 1170 | |
7bcb11d3 JS |
1171 | m_previewPrintout->OnPrintPage(pageNum); |
1172 | m_previewPrintout->OnEndDocument(); | |
1173 | m_previewPrintout->OnEndPrinting(); | |
8826f46f | 1174 | |
7bcb11d3 | 1175 | m_previewPrintout->SetDC(NULL); |
8826f46f | 1176 | |
c801d85f | 1177 | memoryDC.SelectObject(wxNullBitmap); |
8826f46f | 1178 | |
3080bf59 VZ |
1179 | #if wxUSE_STATUSBAR |
1180 | wxString status; | |
7bcb11d3 | 1181 | if (m_maxPage != 0) |
7e548f6b | 1182 | status = wxString::Format(_("Page %d of %d"), pageNum, m_maxPage); |
7bcb11d3 | 1183 | else |
7e548f6b | 1184 | status = wxString::Format(_("Page %d"), pageNum); |
8826f46f | 1185 | |
7bcb11d3 | 1186 | if (m_previewFrame) |
3080bf59 VZ |
1187 | m_previewFrame->SetStatusText(status); |
1188 | #endif | |
8826f46f | 1189 | |
7e548f6b | 1190 | return true; |
c801d85f KB |
1191 | } |
1192 | ||
1193 | ||
d2b354f9 | 1194 | bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc) |
c801d85f | 1195 | { |
7bcb11d3 JS |
1196 | int canvasWidth, canvasHeight; |
1197 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
8826f46f | 1198 | |
7bcb11d3 JS |
1199 | float zoomScale = (float)((float)m_currentZoom/(float)100); |
1200 | float actualWidth = zoomScale*m_pageWidth*m_previewScale; | |
1201 | float actualHeight = zoomScale*m_pageHeight*m_previewScale; | |
8826f46f | 1202 | |
7bcb11d3 JS |
1203 | float x = (float)((canvasWidth - actualWidth)/2.0); |
1204 | if (x < m_leftMargin) | |
1205 | x = (float)m_leftMargin; | |
1206 | float y = (float)m_topMargin; | |
8826f46f | 1207 | |
7bcb11d3 JS |
1208 | // Draw shadow, allowing for 1-pixel border AROUND the actual page |
1209 | int shadowOffset = 4; | |
1210 | dc.SetPen(*wxBLACK_PEN); | |
1211 | dc.SetBrush(*wxBLACK_BRUSH); | |
1212 | /* | |
1213 | dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2)); | |
1214 | */ | |
1215 | dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset); | |
1216 | dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight)); | |
8826f46f | 1217 | |
7bcb11d3 JS |
1218 | // Draw blank page allowing for 1-pixel border AROUND the actual page |
1219 | dc.SetPen(*wxBLACK_PEN); | |
1220 | dc.SetBrush(*wxWHITE_BRUSH); | |
8826f46f | 1221 | |
7bcb11d3 JS |
1222 | /* |
1223 | wxRegion update_region = canvas->GetUpdateRegion(); | |
1224 | wxRect r = update_region.GetBox(); | |
8826f46f | 1225 | |
7bcb11d3 JS |
1226 | printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height ); |
1227 | */ | |
8826f46f | 1228 | |
7bcb11d3 | 1229 | dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2)); |
8826f46f | 1230 | |
7e548f6b | 1231 | return true; |
2a47d3c1 JS |
1232 | } |
1233 | ||
7bcb11d3 | 1234 | void wxPrintPreviewBase::SetZoom(int percent) |
2a47d3c1 | 1235 | { |
7bcb11d3 JS |
1236 | if (m_currentZoom == percent) |
1237 | return; | |
8826f46f | 1238 | |
7bcb11d3 JS |
1239 | m_currentZoom = percent; |
1240 | if (m_previewBitmap) | |
1241 | { | |
1242 | delete m_previewBitmap; | |
1243 | m_previewBitmap = NULL; | |
1244 | } | |
aff37a19 | 1245 | |
7bcb11d3 JS |
1246 | if (m_previewCanvas) |
1247 | { | |
d2b354f9 | 1248 | AdjustScrollbars(m_previewCanvas); |
aff37a19 | 1249 | RenderPage(m_currentPage); |
95724b1a | 1250 | ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0); |
e71fd398 | 1251 | m_previewCanvas->ClearBackground(); |
7bcb11d3 | 1252 | m_previewCanvas->Refresh(); |
d2b354f9 | 1253 | m_previewCanvas->SetFocus(); |
7bcb11d3 | 1254 | } |
2a47d3c1 JS |
1255 | } |
1256 | ||
383f6abd WS |
1257 | wxPrintDialogData& wxPrintPreviewBase::GetPrintDialogData() |
1258 | { | |
e81e3883 RR |
1259 | return m_printDialogData; |
1260 | } | |
1261 | ||
383f6abd | 1262 | int wxPrintPreviewBase::GetZoom() const |
e81e3883 | 1263 | { return m_currentZoom; } |
383f6abd | 1264 | int wxPrintPreviewBase::GetMaxPage() const |
e81e3883 | 1265 | { return m_maxPage; } |
383f6abd | 1266 | int wxPrintPreviewBase::GetMinPage() const |
e81e3883 | 1267 | { return m_minPage; } |
383f6abd | 1268 | bool wxPrintPreviewBase::Ok() const |
e81e3883 | 1269 | { return m_isOk; } |
383f6abd | 1270 | void wxPrintPreviewBase::SetOk(bool ok) |
e81e3883 RR |
1271 | { m_isOk = ok; } |
1272 | //---------------------------------------------------------------------------- | |
1273 | // wxPrintPreview | |
1274 | //---------------------------------------------------------------------------- | |
1275 | ||
1276 | IMPLEMENT_CLASS(wxPrintPreview, wxPrintPreviewBase) | |
1277 | ||
1278 | wxPrintPreview::wxPrintPreview(wxPrintout *printout, | |
1279 | wxPrintout *printoutForPrinting, | |
1280 | wxPrintDialogData *data) : | |
1281 | wxPrintPreviewBase( printout, printoutForPrinting, data ) | |
1282 | { | |
1283 | m_pimpl = wxPrintFactory::GetFactory()-> | |
1284 | CreatePrintPreview( printout, printoutForPrinting, data ); | |
1285 | } | |
1286 | ||
1287 | wxPrintPreview::wxPrintPreview(wxPrintout *printout, | |
1288 | wxPrintout *printoutForPrinting, | |
1289 | wxPrintData *data ) : | |
1290 | wxPrintPreviewBase( printout, printoutForPrinting, data ) | |
1291 | { | |
1292 | m_pimpl = wxPrintFactory::GetFactory()-> | |
1293 | CreatePrintPreview( printout, printoutForPrinting, data ); | |
1294 | } | |
1295 | ||
1296 | wxPrintPreview::~wxPrintPreview() | |
1297 | { | |
1298 | delete m_pimpl; | |
383f6abd | 1299 | |
e81e3883 RR |
1300 | // don't delete twice |
1301 | m_printPrintout = NULL; | |
1302 | m_previewPrintout = NULL; | |
1303 | m_previewBitmap = NULL; | |
1304 | } | |
1305 | ||
1306 | bool wxPrintPreview::SetCurrentPage(int pageNum) | |
1307 | { | |
1308 | return m_pimpl->SetCurrentPage( pageNum ); | |
1309 | } | |
1310 | ||
383f6abd WS |
1311 | int wxPrintPreview::GetCurrentPage() const |
1312 | { | |
e81e3883 RR |
1313 | return m_pimpl->GetCurrentPage(); |
1314 | } | |
1315 | ||
383f6abd WS |
1316 | void wxPrintPreview::SetPrintout(wxPrintout *printout) |
1317 | { | |
e81e3883 RR |
1318 | m_pimpl->SetPrintout( printout ); |
1319 | } | |
1320 | ||
383f6abd WS |
1321 | wxPrintout *wxPrintPreview::GetPrintout() const |
1322 | { | |
e81e3883 RR |
1323 | return m_pimpl->GetPrintout(); |
1324 | } | |
1325 | ||
383f6abd WS |
1326 | wxPrintout *wxPrintPreview::GetPrintoutForPrinting() const |
1327 | { | |
e81e3883 RR |
1328 | return m_pimpl->GetPrintoutForPrinting(); |
1329 | } | |
1330 | ||
383f6abd WS |
1331 | void wxPrintPreview::SetFrame(wxFrame *frame) |
1332 | { | |
e81e3883 RR |
1333 | m_pimpl->SetFrame( frame ); |
1334 | } | |
1335 | ||
383f6abd WS |
1336 | void wxPrintPreview::SetCanvas(wxPreviewCanvas *canvas) |
1337 | { | |
e81e3883 RR |
1338 | m_pimpl->SetCanvas( canvas ); |
1339 | } | |
1340 | ||
383f6abd | 1341 | wxFrame *wxPrintPreview::GetFrame() const |
e81e3883 RR |
1342 | { |
1343 | return m_pimpl->GetFrame(); | |
1344 | } | |
1345 | ||
383f6abd WS |
1346 | wxPreviewCanvas *wxPrintPreview::GetCanvas() const |
1347 | { | |
e81e3883 RR |
1348 | return m_pimpl->GetCanvas(); |
1349 | } | |
1350 | ||
1351 | bool wxPrintPreview::PaintPage(wxPreviewCanvas *canvas, wxDC& dc) | |
1352 | { | |
1353 | return m_pimpl->PaintPage( canvas, dc ); | |
1354 | } | |
1355 | ||
1356 | bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc) | |
1357 | { | |
1358 | return m_pimpl->DrawBlankPage( canvas, dc ); | |
1359 | } | |
1360 | ||
1361 | void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas *canvas) | |
1362 | { | |
1363 | m_pimpl->AdjustScrollbars( canvas ); | |
1364 | } | |
1365 | ||
1366 | bool wxPrintPreview::RenderPage(int pageNum) | |
1367 | { | |
1368 | return m_pimpl->RenderPage( pageNum ); | |
1369 | } | |
1370 | ||
1371 | void wxPrintPreview::SetZoom(int percent) | |
1372 | { | |
1373 | m_pimpl->SetZoom( percent ); | |
1374 | } | |
1375 | ||
1376 | wxPrintDialogData& wxPrintPreview::GetPrintDialogData() | |
1377 | { | |
1378 | return m_pimpl->GetPrintDialogData(); | |
1379 | } | |
1380 | ||
1381 | int wxPrintPreview::GetMaxPage() const | |
1382 | { | |
1383 | return m_pimpl->GetMaxPage(); | |
1384 | } | |
1385 | ||
1386 | int wxPrintPreview::GetMinPage() const | |
1387 | { | |
1388 | return m_pimpl->GetMinPage(); | |
1389 | } | |
1390 | ||
1391 | bool wxPrintPreview::Ok() const | |
1392 | { | |
1393 | return m_pimpl->Ok(); | |
1394 | } | |
1395 | ||
1396 | void wxPrintPreview::SetOk(bool ok) | |
1397 | { | |
1398 | m_pimpl->SetOk( ok ); | |
1399 | } | |
1400 | ||
1401 | bool wxPrintPreview::Print(bool interactive) | |
1402 | { | |
1403 | return m_pimpl->Print( interactive ); | |
1404 | } | |
1405 | ||
1406 | void wxPrintPreview::DetermineScaling() | |
1407 | { | |
1408 | m_pimpl->DetermineScaling(); | |
1409 | } | |
1410 | ||
1411 | ||
d427503c | 1412 | #endif // wxUSE_PRINTING_ARCHITECTURE |