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