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