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