]> git.saurik.com Git - wxWidgets.git/blame - src/osx/core/printmac.cpp
fix warnings about hiding virtual wxGraphicsContext::DrawText() overloads by using...
[wxWidgets.git] / src / osx / core / printmac.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
96dabe43 2// Name: src/osx/core/printmac.cpp
489468fe 3// Purpose: wxMacPrinter framework
96dabe43 4// Author: Julian Smart, Stefan Csomor
489468fe
SC
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
96dabe43 8// Copyright: (c) Julian Smart, Stefan Csomor
489468fe
SC
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#if wxUSE_PRINTING_ARCHITECTURE
16
17#ifdef __BORLANDC__
18 #pragma hdrstop
19#endif
20
21#ifndef WX_PRECOMP
22 #include "wx/utils.h"
23 #include "wx/dc.h"
24 #include "wx/app.h"
25 #include "wx/msgdlg.h"
26 #include "wx/dcprint.h"
27 #include "wx/math.h"
28#endif
29
524c47aa 30#include "wx/osx/private.h"
489468fe 31
1f0c8f31
SC
32#include "wx/osx/printmac.h"
33#include "wx/osx/private/print.h"
489468fe
SC
34
35#include "wx/printdlg.h"
36#include "wx/paper.h"
1f0c8f31 37#include "wx/osx/printdlg.h"
489468fe
SC
38
39#include <stdlib.h>
40
41IMPLEMENT_DYNAMIC_CLASS(wxMacCarbonPrintData, wxPrintNativeDataBase)
42IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase)
43IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase)
44
45bool wxMacCarbonPrintData::IsOk() const
46{
47 return (m_macPageFormat != kPMNoPageFormat) && (m_macPrintSettings != kPMNoPrintSettings) && (m_macPrintSession != kPMNoReference);
48}
49wxMacCarbonPrintData::wxMacCarbonPrintData()
50{
51 m_macPageFormat = kPMNoPageFormat;
52 m_macPrintSettings = kPMNoPrintSettings;
53 m_macPrintSession = kPMNoReference ;
54 ValidateOrCreate() ;
55}
56
57wxMacCarbonPrintData::~wxMacCarbonPrintData()
58{
59 if (m_macPageFormat != kPMNoPageFormat)
60 {
61 (void)PMRelease(m_macPageFormat);
62 m_macPageFormat = kPMNoPageFormat;
63 }
64
65 if (m_macPrintSettings != kPMNoPrintSettings)
66 {
67 (void)PMRelease(m_macPrintSettings);
68 m_macPrintSettings = kPMNoPrintSettings;
69 }
70
71 if ( m_macPrintSession != kPMNoReference )
72 {
73 (void)PMRelease(m_macPrintSession);
74 m_macPrintSession = kPMNoReference;
75 }
76}
77
78void wxMacCarbonPrintData::ValidateOrCreate()
79{
80 OSStatus err = noErr ;
81 if ( m_macPrintSession == kPMNoReference )
82 {
83 err = PMCreateSession( &m_macPrintSession ) ;
84 }
85 // Set up a valid PageFormat object.
86 if ( m_macPageFormat == kPMNoPageFormat)
87 {
88 err = PMCreatePageFormat(&m_macPageFormat);
89
90 // Note that PMPageFormat is not session-specific, but calling
91 // PMSessionDefaultPageFormat assigns values specific to the printer
92 // associated with the current printing session.
93 if ((err == noErr) &&
94 ( m_macPageFormat != kPMNoPageFormat))
95 {
96 err = PMSessionDefaultPageFormat(m_macPrintSession,
97 m_macPageFormat);
98 }
99 }
100 else
101 {
102 err = PMSessionValidatePageFormat(m_macPrintSession,
103 m_macPageFormat,
104 kPMDontWantBoolean);
105 }
106
107 // Set up a valid PrintSettings object.
108 if ( m_macPrintSettings == kPMNoPrintSettings)
109 {
110 err = PMCreatePrintSettings( &m_macPrintSettings);
111
112 // Note that PMPrintSettings is not session-specific, but calling
113 // PMSessionDefaultPrintSettings assigns values specific to the printer
114 // associated with the current printing session.
115 if ((err == noErr) &&
116 ( m_macPrintSettings != kPMNoPrintSettings))
117 {
118 err = PMSessionDefaultPrintSettings(m_macPrintSession,
119 m_macPrintSettings);
120 }
121 }
122 else
123 {
124 err = PMSessionValidatePrintSettings( m_macPrintSession,
125 m_macPrintSettings,
126 kPMDontWantBoolean);
127 }
128}
129
130bool wxMacCarbonPrintData::TransferFrom( const wxPrintData &data )
131{
132 ValidateOrCreate() ;
133 PMSetCopies( (PMPrintSettings) m_macPrintSettings , data.GetNoCopies() , false ) ;
134 if ( data.IsOrientationReversed() )
135 PMSetOrientation( (PMPageFormat) m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
136 kPMReverseLandscape : kPMReversePortrait , false ) ;
137 else
138 PMSetOrientation( (PMPageFormat) m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
139 kPMLandscape : kPMPortrait , false ) ;
140 // collate cannot be set
141#if 0 // not yet tested
142 if ( !m_printerName.empty() )
143 PMSessionSetCurrentPrinter( (PMPrintSession) m_macPrintSession , wxCFStringRef( m_printerName , wxFont::GetDefaultEncoding() ) ) ;
144#endif
b2680ced 145#if wxOSX_USE_CARBON
489468fe
SC
146 PMColorMode color ;
147 PMGetColorMode( (PMPrintSettings) m_macPrintSettings, &color ) ;
148 if ( data.GetColour() )
149 {
150 if ( color == kPMBlackAndWhite )
151 PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMColor ) ;
152 }
153 else
154 PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMBlackAndWhite ) ;
155#endif
156
157 PMDuplexMode mode = 0 ;
158 switch( data.GetDuplex() )
159 {
160 case wxDUPLEX_HORIZONTAL :
161 mode = kPMDuplexNoTumble ;
162 break ;
163 case wxDUPLEX_VERTICAL :
164 mode = kPMDuplexTumble ;
165 break ;
166 case wxDUPLEX_SIMPLEX :
167 default :
168 mode = kPMDuplexNone ;
169 break ;
170 }
171 PMSetDuplex( (PMPrintSettings) m_macPrintSettings, mode ) ;
172
173 // PMQualityMode not yet accessible via API
174 // todo paperSize
175
176 PMResolution res;
177 PMPrinter printer;
178 PMSessionGetCurrentPrinter(m_macPrintSession, &printer);
b2680ced
SC
179
180#if wxOSX_USE_CARBON
181 PMTag tag = kPMMaxSquareResolution;
182 PMPrinterGetPrinterResolution(printer, tag, &res);
183 PMSetResolution((PMPageFormat) m_macPageFormat, &res);
184#else
489468fe
SC
185 PMPrinterGetOutputResolution( printer,
186 (PMPrintSettings) m_macPrintSettings, &res) ;
187 // TODO transfer ? into page format ?
188 // may fail !
489468fe
SC
189#endif
190 // after setting the new resolution the format has to be updated, otherwise the page rect remains
191 // at the 'old' scaling
192 PMSessionValidatePageFormat((PMPrintSession) m_macPrintSession,
193 (PMPageFormat) m_macPageFormat,
194 kPMDontWantBoolean) ;
195
196 return true ;
197}
198
199bool wxMacCarbonPrintData::TransferTo( wxPrintData &data )
200{
201 OSStatus err = noErr ;
202
203 UInt32 copies ;
204 err = PMGetCopies( m_macPrintSettings , &copies ) ;
205 if ( err == noErr )
206 data.SetNoCopies( copies ) ;
207
208 PMOrientation orientation ;
209 err = PMGetOrientation( m_macPageFormat , &orientation ) ;
210 if ( err == noErr )
211 {
212 if ( orientation == kPMPortrait || orientation == kPMReversePortrait )
213 {
214 data.SetOrientation( wxPORTRAIT );
215 data.SetOrientationReversed( orientation == kPMReversePortrait );
216 }
217 else
218 {
219 data.SetOrientation( wxLANDSCAPE );
220 data.SetOrientationReversed( orientation == kPMReverseLandscape );
221 }
222 }
223
224 // collate cannot be set
225#if 0
226 {
227 wxCFStringRef name ;
228 PMPrinter printer ;
229 PMSessionGetCurrentPrinter( m_macPrintSession ,
230 &printer ) ;
231 m_printerName = name.AsString() ;
232 }
233#endif
234
235#ifndef __LP64__
236 PMColorMode color ;
237 err = PMGetColorMode( m_macPrintSettings, &color ) ;
238 if ( err == noErr )
239 data.SetColour( !(color == kPMBlackAndWhite) ) ;
240#endif
241 PMDuplexMode mode = 0 ;
242 PMGetDuplex( (PMPrintSettings) m_macPrintSettings, &mode ) ;
243 switch( mode )
244 {
245 case kPMDuplexNoTumble :
246 data.SetDuplex(wxDUPLEX_HORIZONTAL);
247 break ;
248 case kPMDuplexTumble :
249 data.SetDuplex(wxDUPLEX_VERTICAL);
250 break ;
251 case kPMDuplexNone :
252 default :
253 data.SetDuplex(wxDUPLEX_SIMPLEX);
254 break ;
255 }
256 // PMQualityMode not yet accessible via API
257
258 PMPaper paper ;
259 PMGetPageFormatPaper( m_macPageFormat, &paper );
260
261 PMRect rPaper;
262 err = PMGetUnadjustedPaperRect( m_macPageFormat, &rPaper);
263 if ( err == noErr )
264 {
265 wxSize sz((int)(( rPaper.right - rPaper.left ) * pt2mm + 0.5 ) ,
266 (int)(( rPaper.bottom - rPaper.top ) * pt2mm + 0.5 ));
267 data.SetPaperSize(sz);
268 wxPaperSize id = wxThePrintPaperDatabase->GetSize(wxSize(sz.x* 10, sz.y * 10));
269 if (id != wxPAPER_NONE)
270 {
271 data.SetPaperId(id);
272 }
273 }
274 return true ;
275}
276
277void wxMacCarbonPrintData::TransferFrom( wxPageSetupData *WXUNUSED(data) )
278{
279 // should we setup the page rect here ?
280 // since MacOS sometimes has two same paper rects with different
281 // page rects we could make it roundtrip safe perhaps
282}
283
284void wxMacCarbonPrintData::TransferTo( wxPageSetupData* data )
285{
286 PMRect rPaper;
287 OSStatus err = PMGetUnadjustedPaperRect(m_macPageFormat, &rPaper);
288 if ( err == noErr )
289 {
290 wxSize sz((int)(( rPaper.right - rPaper.left ) * pt2mm + 0.5 ) ,
291 (int)(( rPaper.bottom - rPaper.top ) * pt2mm + 0.5 ));
292 data->SetPaperSize(sz);
293
294 PMRect rPage ;
295 err = PMGetUnadjustedPageRect(m_macPageFormat , &rPage ) ;
296 if ( err == noErr )
297 {
298 data->SetMinMarginTopLeft( wxPoint (
299 (int)(((double) rPage.left - rPaper.left ) * pt2mm) ,
300 (int)(((double) rPage.top - rPaper.top ) * pt2mm) ) ) ;
301
302 data->SetMinMarginBottomRight( wxPoint (
303 (wxCoord)(((double) rPaper.right - rPage.right ) * pt2mm),
304 (wxCoord)(((double) rPaper.bottom - rPage.bottom ) * pt2mm)) ) ;
305
306 if ( data->GetMarginTopLeft().x < data->GetMinMarginTopLeft().x )
307 data->SetMarginTopLeft( wxPoint( data->GetMinMarginTopLeft().x ,
308 data->GetMarginTopLeft().y ) ) ;
309
310 if ( data->GetMarginBottomRight().x < data->GetMinMarginBottomRight().x )
311 data->SetMarginBottomRight( wxPoint( data->GetMinMarginBottomRight().x ,
312 data->GetMarginBottomRight().y ) );
313
314 if ( data->GetMarginTopLeft().y < data->GetMinMarginTopLeft().y )
315 data->SetMarginTopLeft( wxPoint( data->GetMarginTopLeft().x , data->GetMinMarginTopLeft().y ) );
316
317 if ( data->GetMarginBottomRight().y < data->GetMinMarginBottomRight().y )
318 data->SetMarginBottomRight( wxPoint( data->GetMarginBottomRight().x ,
319 data->GetMinMarginBottomRight().y) );
320 }
321 }
322}
323
324void wxMacCarbonPrintData::TransferTo( wxPrintDialogData* data )
325{
326 UInt32 minPage , maxPage ;
327 PMGetPageRange( m_macPrintSettings , &minPage , &maxPage ) ;
328 data->SetMinPage( minPage ) ;
329 data->SetMaxPage( maxPage ) ;
330 UInt32 copies ;
331 PMGetCopies( m_macPrintSettings , &copies ) ;
332 data->SetNoCopies( copies ) ;
333 UInt32 from , to ;
334 PMGetFirstPage( m_macPrintSettings , &from ) ;
335 PMGetLastPage( m_macPrintSettings , &to ) ;
336 if ( to >= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
337 {
338 data->SetAllPages( true ) ;
339 // This means all pages, more or less
340 data->SetFromPage(1);
341 data->SetToPage(32000);
342 }
343 else
344 {
345 data->SetFromPage( from ) ;
346 data->SetToPage( to ) ;
347 data->SetAllPages( false );
348 }
349}
350
351void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData* data )
352{
353 // Respect the value of m_printAllPages
354 if ( data->GetAllPages() )
355 PMSetPageRange( m_macPrintSettings , data->GetMinPage() , (UInt32) kPMPrintAllPages ) ;
356 else
357 PMSetPageRange( m_macPrintSettings , data->GetMinPage() , data->GetMaxPage() ) ;
358 PMSetCopies( m_macPrintSettings , data->GetNoCopies() , false ) ;
359 PMSetFirstPage( m_macPrintSettings , data->GetFromPage() , false ) ;
360
361 if (data->GetAllPages() || data->GetFromPage() == 0)
362 PMSetLastPage( m_macPrintSettings , (UInt32) kPMPrintAllPages, true ) ;
363 else
364 PMSetLastPage( m_macPrintSettings , (UInt32) data->GetToPage() , false ) ;
365}
366
367/*
368* Printer
369*/
370
371wxMacPrinter::wxMacPrinter(wxPrintDialogData *data):
372wxPrinterBase(data)
373{
374}
375
376wxMacPrinter::~wxMacPrinter(void)
377{
378}
379
380bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
381{
382 sm_abortIt = false;
383 sm_abortWindow = NULL;
384
385 if (!printout)
386 return false;
387
388 printout->SetIsPreview(false);
389 if (m_printDialogData.GetMinPage() < 1)
390 m_printDialogData.SetMinPage(1);
391 if (m_printDialogData.GetMaxPage() < 1)
392 m_printDialogData.SetMaxPage(9999);
393
394 // Create a suitable device context
395 wxPrinterDC *dc = NULL;
396 if (prompt)
397 {
398 wxMacPrintDialog dialog(parent, & m_printDialogData);
399 if (dialog.ShowModal() == wxID_OK)
400 {
401 dc = wxDynamicCast(dialog.GetPrintDC(), wxPrinterDC);
402 wxASSERT(dc);
403 m_printDialogData = dialog.GetPrintDialogData();
404 }
405 }
406 else
407 {
408 dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ;
409 }
410
411 // May have pressed cancel.
412 if (!dc || !dc->IsOk())
413 {
0b1ca117 414 delete dc;
489468fe
SC
415 return false;
416 }
417
418 // on the mac we have always pixels as addressing mode with 72 dpi
419 printout->SetPPIScreen(72, 72);
420 PMResolution res;
421 wxMacCarbonPrintData* nativeData = (wxMacCarbonPrintData*)
422 (m_printDialogData.GetPrintData().GetNativeData());
b2680ced
SC
423#if wxOSX_USE_CARBON
424 PMGetResolution((PMPageFormat) (nativeData->m_macPageFormat), &res);
425#else
489468fe
SC
426 PMPrinter printer;
427 PMSessionGetCurrentPrinter(nativeData->m_macPrintSession, &printer);
428 PMPrinterGetOutputResolution( printer, nativeData->m_macPrintSettings, &res) ;
489468fe
SC
429#endif
430 printout->SetPPIPrinter(int(res.hRes), int(res.vRes));
431
432 // Set printout parameters
433 printout->SetDC(dc);
434
435 int w, h;
436 dc->GetSize(&w, &h);
437 printout->SetPageSizePixels((int)w, (int)h);
438 printout->SetPaperRectPixels(dc->GetPaperRect());
439 wxCoord mw, mh;
440 dc->GetSizeMM(&mw, &mh);
441 printout->SetPageSizeMM((int)mw, (int)mh);
442
443 // Create an abort window
444 wxBeginBusyCursor();
445
446 printout->OnPreparePrinting();
447
448 // Get some parameters from the printout, if defined
449 int fromPage, toPage;
450 int minPage, maxPage;
451 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
452
453 if (maxPage == 0)
454 {
455 wxEndBusyCursor();
456 return false;
457 }
458
459 // Only set min and max, because from and to have been
460 // set by the user
461 m_printDialogData.SetMinPage(minPage);
462 m_printDialogData.SetMaxPage(maxPage);
463
464 printout->OnBeginPrinting();
465
466 bool keepGoing = true;
467
468 if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
469 {
470 wxEndBusyCursor();
471 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent);
472 }
473
474 int pn;
475 for (pn = m_printDialogData.GetFromPage();
476 keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
477 pn++)
478 {
479 if (sm_abortIt)
480 {
481 keepGoing = false;
482 break;
483 }
484 else
485 {
486 dc->StartPage();
487 keepGoing = printout->OnPrintPage(pn);
488 dc->EndPage();
489 }
490 }
491 printout->OnEndDocument();
492
493 printout->OnEndPrinting();
494
495 if (sm_abortWindow)
496 {
497 sm_abortWindow->Show(false);
498 delete sm_abortWindow;
499 sm_abortWindow = NULL;
500 }
501
502 wxEndBusyCursor();
503
504 delete dc;
505
506 return true;
507}
508
509wxDC* wxMacPrinter::PrintDialog(wxWindow *parent)
510{
511 wxDC* dc = (wxDC*) NULL;
512
513 wxPrintDialog dialog(parent, & m_printDialogData);
514 int ret = dialog.ShowModal();
515
516 if (ret == wxID_OK)
517 {
518 dc = dialog.GetPrintDC();
519 m_printDialogData = dialog.GetPrintDialogData();
520 }
521
522 return dc;
523}
524
525bool wxMacPrinter::Setup(wxWindow *WXUNUSED(parent))
526{
527#if 0
528 wxPrintDialog dialog(parent, & m_printDialogData);
529 dialog.GetPrintDialogData().SetSetupDialog(true);
530
531 int ret = dialog.ShowModal();
532
533 if (ret == wxID_OK)
534 m_printDialogData = dialog.GetPrintDialogData();
535
536 return (ret == wxID_OK);
537#endif
538
539 return wxID_CANCEL;
540}
541
542/*
543* Print preview
544*/
545
546wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout,
547 wxPrintout *printoutForPrinting,
548 wxPrintDialogData *data)
549 : wxPrintPreviewBase(printout, printoutForPrinting, data)
550{
551 DetermineScaling();
552}
553
554wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data):
555wxPrintPreviewBase(printout, printoutForPrinting, data)
556{
557 DetermineScaling();
558}
559
560wxMacPrintPreview::~wxMacPrintPreview(void)
561{
562}
563
564bool wxMacPrintPreview::Print(bool interactive)
565{
566 if (!m_printPrintout)
567 return false;
568
569 wxMacPrinter printer(&m_printDialogData);
570 return printer.Print(m_previewFrame, m_printPrintout, interactive);
571}
572
573void wxMacPrintPreview::DetermineScaling(void)
574{
575 int screenWidth , screenHeight ;
576 wxDisplaySize( &screenWidth , &screenHeight ) ;
577
578 wxSize ppiScreen( 72 , 72 ) ;
579 wxSize ppiPrinter( 72 , 72 ) ;
580
581 // Note that with Leopard, screen dpi=72 is no longer a given
582 m_previewPrintout->SetPPIScreen( ppiScreen.x , ppiScreen.y ) ;
583
584 wxCoord w , h ;
585 wxCoord ww, hh;
586 wxRect paperRect;
587
588 // Get a device context for the currently selected printer
589 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
590 if (printerDC.IsOk())
591 {
592 printerDC.GetSizeMM(&ww, &hh);
593 printerDC.GetSize( &w , &h ) ;
594 ppiPrinter = printerDC.GetPPI() ;
595 paperRect = printerDC.GetPaperRect();
596 m_isOk = true ;
597 }
598 else
599 {
600 // use some defaults
601 w = 8 * 72 ;
602 h = 11 * 72 ;
603 ww = (wxCoord) (w * 25.4 / ppiPrinter.x) ;
604 hh = (wxCoord) (h * 25.4 / ppiPrinter.y) ;
605 paperRect = wxRect(0, 0, w, h);
606 m_isOk = false ;
607 }
608 m_pageWidth = w;
609 m_pageHeight = h;
610
611 m_previewPrintout->SetPageSizePixels(w , h) ;
612 m_previewPrintout->SetPageSizeMM(ww, hh);
613 m_previewPrintout->SetPaperRectPixels(paperRect);
614 m_previewPrintout->SetPPIPrinter( ppiPrinter.x , ppiPrinter.y ) ;
615
616 m_previewScaleX = float(ppiScreen.x) / ppiPrinter.x;
617 m_previewScaleY = float(ppiScreen.y) / ppiPrinter.y;
618}
619
620#endif