]> git.saurik.com Git - wxWidgets.git/blame - src/osx/core/printmac.cpp
fixing non-precomp build, fixes #10985
[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
c347101b
SC
41//
42// move to print_osx.cpp
43//
489468fe 44
884dad83
SC
45static int ResolutionSorter(const void *e1, const void *e2)
46{
47 const PMResolution *res1 = (const PMResolution *)e1;
48 const PMResolution *res2 = (const PMResolution *)e2;
6f4968ce
VZ
49 const double area1 = res1->hRes * res1->vRes;
50 const double area2 = res2->hRes * res2->vRes;
51
884dad83
SC
52 if (area1 < area2)
53 return -1;
54 else if (area1 > area2)
55 return 1;
56 else
57 return 0;
58}
59
60static PMResolution *GetSupportedResolutions(PMPrinter printer, UInt32 *count)
61{
62 PMResolution res, *resolutions = NULL;
63 OSStatus status = PMPrinterGetPrinterResolutionCount(printer, count);
64 if (status == kPMNotImplemented)
65 {
66#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
67 resolutions = (PMResolution *)malloc(sizeof(PMResolution) * 4);
68 *count = 0;
69 if (PMPrinterGetPrinterResolution(printer, kPMMinRange, &res) == noErr)
70 resolutions[(*count)++] = res;
71 if (PMPrinterGetPrinterResolution(printer, kPMMinSquareResolution, &res) == noErr)
72 resolutions[(*count)++] = res;
73 if (PMPrinterGetPrinterResolution(printer, kPMMaxSquareResolution, &res) == noErr)
74 resolutions[(*count)++] = res;
75 if (PMPrinterGetPrinterResolution(printer, kPMMaxRange, &res) == noErr)
76 resolutions[(*count)++] = res;
77 if (*count == 0)
78 {
79 if (PMPrinterGetPrinterResolution(printer, kPMDefaultResolution, &res) == noErr)
80 resolutions[(*count)++] = res;
81 }
82#endif
83 }
84 else if (status == noErr)
85 {
86 resolutions = (PMResolution *)malloc(sizeof(PMResolution) * (*count));
87 UInt32 realCount = 0;
88 for (UInt32 i = 0; i < *count; i++)
89 {
90 if (PMPrinterGetIndexedPrinterResolution(printer, i + 1, &res) == noErr)
91 resolutions[realCount++] = res;
92 }
93 qsort(resolutions, realCount, sizeof(PMResolution), ResolutionSorter);
6f4968ce 94
884dad83
SC
95 *count = realCount;
96 }
97 if ((*count == 0) && (resolutions))
98 {
99 free(resolutions);
100 resolutions = NULL;
101 }
102 return resolutions;
103}
104
105
106
c347101b
SC
107IMPLEMENT_DYNAMIC_CLASS(wxOSXPrintData, wxPrintNativeDataBase)
108
109bool wxOSXPrintData::IsOk() const
489468fe
SC
110{
111 return (m_macPageFormat != kPMNoPageFormat) && (m_macPrintSettings != kPMNoPrintSettings) && (m_macPrintSession != kPMNoReference);
112}
c347101b
SC
113
114wxOSXPrintData::wxOSXPrintData()
489468fe
SC
115{
116 m_macPageFormat = kPMNoPageFormat;
117 m_macPrintSettings = kPMNoPrintSettings;
118 m_macPrintSession = kPMNoReference ;
c347101b 119 m_macPaper = kPMNoData;
489468fe
SC
120}
121
c347101b 122wxOSXPrintData::~wxOSXPrintData()
489468fe 123{
c347101b 124}
489468fe 125
c347101b
SC
126void wxOSXPrintData::UpdateFromPMState()
127{
128}
489468fe 129
c347101b
SC
130void wxOSXPrintData::UpdateToPMState()
131{
489468fe
SC
132}
133
c347101b 134bool wxOSXPrintData::TransferFrom( const wxPrintData &data )
489468fe 135{
884dad83
SC
136 CFArrayRef printerList;
137 CFIndex index, count;
138 CFStringRef name;
6f4968ce 139
884dad83
SC
140 if (PMServerCreatePrinterList(kPMServerLocal, &printerList) == noErr)
141 {
142 PMPrinter printer = NULL;
143 count = CFArrayGetCount(printerList);
144 for (index = 0; index < count; index++)
145 {
146 printer = (PMPrinter)CFArrayGetValueAtIndex(printerList, index);
147 if ((data.GetPrinterName().empty()) && (PMPrinterIsDefault(printer)))
148 break;
149 else
150 {
151 name = PMPrinterGetName(printer);
152 CFRetain(name);
153 if (data.GetPrinterName() == wxCFStringRef(name).AsString())
154 break;
155 }
156 }
157 if (index < count)
158 PMSessionSetCurrentPMPrinter(m_macPrintSession, printer);
159 CFRelease(printerList);
160 }
6f4968ce 161
c347101b
SC
162 PMPrinter printer;
163 PMSessionGetCurrentPrinter(m_macPrintSession, &printer);
489468fe 164
c347101b
SC
165 wxSize papersize = wxDefaultSize;
166 const wxPaperSize paperId = data.GetPaperId();
167 if ( paperId != wxPAPER_NONE && wxThePrintPaperDatabase )
168 {
169 papersize = wxThePrintPaperDatabase->GetSize(paperId);
170 if ( papersize != wxDefaultSize )
489468fe 171 {
c347101b
SC
172 papersize.x /= 10;
173 papersize.y /= 10;
489468fe
SC
174 }
175 }
176 else
177 {
c347101b 178 papersize = data.GetPaperSize();
489468fe 179 }
03647350 180
c347101b 181 if ( papersize != wxDefaultSize )
489468fe 182 {
c347101b
SC
183 papersize.x = (wxInt32) (papersize.x * mm2pt);
184 papersize.y = (wxInt32) (papersize.y * mm2pt);
03647350 185
c347101b
SC
186 double height, width;
187 PMPaperGetHeight(m_macPaper, &height);
188 PMPaperGetWidth(m_macPaper, &width);
03647350
VZ
189
190 if ( fabs( width - papersize.x ) >= 5 ||
c347101b 191 fabs( height - papersize.y ) >= 5 )
489468fe 192 {
c347101b
SC
193 // we have to change the current paper
194 CFArrayRef paperlist = 0 ;
195 if ( PMPrinterGetPaperList( printer, &paperlist ) == noErr )
196 {
197 PMPaper bestPaper = kPMNoData ;
198 CFIndex top = CFArrayGetCount(paperlist);
199 for ( CFIndex i = 0 ; i < top ; ++ i )
200 {
201 PMPaper paper = (PMPaper) CFArrayGetValueAtIndex( paperlist, i );
202 PMPaperGetHeight(paper, &height);
203 PMPaperGetWidth(paper, &width);
03647350 204 if ( fabs( width - papersize.x ) < 5 &&
c347101b
SC
205 fabs( height - papersize.y ) < 5 )
206 {
207 // TODO test for duplicate hits and use additional
208 // criteria for best match
209 bestPaper = paper;
210 }
211 }
212 PMPaper paper = kPMNoData;
213 if ( bestPaper == kPMNoData )
214 {
215 const PMPaperMargins margins = { 0.0, 0.0, 0.0, 0.0 };
03647350 216 wxString id, name(wxT("Custom paper"));
9a83f860 217 id.Printf(wxT("wxPaperCustom%dx%d"), papersize.x, papersize.y);
c347101b
SC
218
219#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
220 if ( PMPaperCreateCustom != NULL)
221 {
222 PMPaperCreateCustom(printer, wxCFStringRef( id, wxFont::GetDefaultEncoding() ), wxCFStringRef( name, wxFont::GetDefaultEncoding() ),
223 papersize.x, papersize.y, &margins, &paper);
224 }
225#endif
03647350 226#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
c347101b
SC
227 if ( paper == kPMNoData )
228 {
03647350 229 PMPaperCreate(printer, wxCFStringRef( id, wxFont::GetDefaultEncoding() ), wxCFStringRef( name, wxFont::GetDefaultEncoding() ),
c347101b
SC
230 papersize.x, papersize.y, &margins, &paper);
231 }
232#endif
233 }
234 if ( bestPaper != kPMNoData )
235 {
236 PMPageFormat pageFormat;
237 PMCreatePageFormatWithPMPaper(&pageFormat, bestPaper);
238 PMCopyPageFormat( pageFormat, m_macPageFormat );
239 PMRelease(pageFormat);
240 PMGetPageFormatPaper(m_macPageFormat, &m_macPaper);
241 }
242 PMRelease(paper);
243 }
489468fe
SC
244 }
245 }
489468fe 246
c0b65970
RR
247 PMSetCopies( m_macPrintSettings , data.GetNoCopies() , false ) ;
248 PMSetCollate(m_macPrintSettings, data.GetCollate());
c347101b
SC
249 if ( data.IsOrientationReversed() )
250 PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
251 kPMReverseLandscape : kPMReversePortrait , false ) ;
489468fe 252 else
c347101b
SC
253 PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
254 kPMLandscape : kPMPortrait , false ) ;
03647350 255
489468fe
SC
256 PMDuplexMode mode = 0 ;
257 switch( data.GetDuplex() )
258 {
259 case wxDUPLEX_HORIZONTAL :
260 mode = kPMDuplexNoTumble ;
261 break ;
262 case wxDUPLEX_VERTICAL :
263 mode = kPMDuplexTumble ;
264 break ;
265 case wxDUPLEX_SIMPLEX :
266 default :
267 mode = kPMDuplexNone ;
268 break ;
269 }
c347101b 270 PMSetDuplex( m_macPrintSettings, mode ) ;
489468fe 271
03647350 272
c0b65970 273 if ( data.IsOrientationReversed() )
c347101b 274 PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
c0b65970
RR
275 kPMReverseLandscape : kPMReversePortrait , false ) ;
276 else
c347101b 277 PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
c0b65970 278 kPMLandscape : kPMPortrait , false ) ;
03647350 279
884dad83
SC
280 UInt32 resCount;
281 PMResolution *resolutions = GetSupportedResolutions(printer, &resCount);
282 if (resolutions)
283 {
284 wxPrintQuality quality = data.GetQuality();
285 if (quality >= 0)
286 quality = wxPRINT_QUALITY_HIGH;
6f4968ce 287
884dad83
SC
288 PMResolution res = resolutions[((quality - wxPRINT_QUALITY_DRAFT) * (resCount - 1)) / 3];
289#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
290 if ( PMPrinterSetOutputResolution != NULL )
291 PMPrinterSetOutputResolution(printer, m_macPrintSettings, &res);
292 else
3f2cd15f 293#endif
884dad83
SC
294 {
295#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
296 PMSetResolution( m_macPageFormat, &res);
297#endif
298 }
c347101b 299
884dad83
SC
300 free(resolutions);
301 }
03647350 302 // after setting the new resolution the format has to be updated, otherwise the page rect remains
489468fe 303 // at the 'old' scaling
03647350 304
c347101b
SC
305 PMSessionValidatePageFormat(m_macPrintSession,
306 m_macPageFormat, kPMDontWantBoolean);
307 PMSessionValidatePrintSettings(m_macPrintSession,
308 m_macPrintSettings, kPMDontWantBoolean);
309#if wxOSX_USE_COCOA
310 UpdateFromPMState();
311#endif
489468fe
SC
312
313 return true ;
314}
315
c347101b 316bool wxOSXPrintData::TransferTo( wxPrintData &data )
489468fe
SC
317{
318 OSStatus err = noErr ;
c347101b
SC
319#if wxOSX_USE_COCOA
320 UpdateToPMState();
321#endif
489468fe
SC
322 UInt32 copies ;
323 err = PMGetCopies( m_macPrintSettings , &copies ) ;
324 if ( err == noErr )
325 data.SetNoCopies( copies ) ;
326
327 PMOrientation orientation ;
328 err = PMGetOrientation( m_macPageFormat , &orientation ) ;
329 if ( err == noErr )
330 {
331 if ( orientation == kPMPortrait || orientation == kPMReversePortrait )
332 {
333 data.SetOrientation( wxPORTRAIT );
334 data.SetOrientationReversed( orientation == kPMReversePortrait );
335 }
336 else
337 {
338 data.SetOrientation( wxLANDSCAPE );
339 data.SetOrientationReversed( orientation == kPMReverseLandscape );
340 }
341 }
342
c0b65970
RR
343 Boolean collate;
344 if (PMGetCollate(m_macPrintSettings, &collate) == noErr)
345 data.SetCollate(collate);
03647350 346
c0b65970
RR
347 CFStringRef name;
348 PMPrinter printer ;
349 PMSessionGetCurrentPrinter( m_macPrintSession, &printer );
350 if (PMPrinterIsDefault(printer))
351 data.SetPrinterName(wxEmptyString);
352 else
489468fe 353 {
c0b65970
RR
354 name = PMPrinterGetName(printer);
355 CFRetain(name);
356 data.SetPrinterName(wxCFStringRef(name).AsString());
489468fe 357 }
03647350 358
489468fe 359 PMDuplexMode mode = 0 ;
c347101b 360 PMGetDuplex( m_macPrintSettings, &mode ) ;
489468fe
SC
361 switch( mode )
362 {
363 case kPMDuplexNoTumble :
364 data.SetDuplex(wxDUPLEX_HORIZONTAL);
365 break ;
366 case kPMDuplexTumble :
367 data.SetDuplex(wxDUPLEX_VERTICAL);
368 break ;
369 case kPMDuplexNone :
370 default :
371 data.SetDuplex(wxDUPLEX_SIMPLEX);
372 break ;
373 }
03647350 374
884dad83
SC
375 /* assume high quality, will change below if we are able to */
376 data.SetQuality(wxPRINT_QUALITY_HIGH);
6f4968ce 377
884dad83
SC
378 PMResolution *resolutions;
379 UInt32 resCount;
380 resolutions = GetSupportedResolutions(printer, &resCount);
381 if (resolutions)
382 {
383 bool valid = false;
384 PMResolution res;
385#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
386 if ( PMPrinterGetOutputResolution != NULL )
387 {
388 if ( PMPrinterGetOutputResolution(printer, m_macPrintSettings, &res) == noErr )
389 valid = true;
390 }
391#endif
392#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
393 if (PMPrinterGetPrinterResolution(printer, kPMCurrentValue, &res) == noErr)
394 valid = true;
395#endif
396 if ( valid )
397 {
398 UInt32 i;
399 for (i = 0; i < resCount; i++)
400 {
401 if ((resolutions[i].hRes == res.hRes) && (resolutions[i].vRes = res.vRes))
402 break;
403 }
404 if (i < resCount)
405 data.SetQuality((((i + 1) * 3) / resCount) + wxPRINT_QUALITY_DRAFT);
406 }
407 free(resolutions);
6f4968ce
VZ
408 }
409
c347101b
SC
410 double height, width;
411 PMPaperGetHeight(m_macPaper, &height);
412 PMPaperGetWidth(m_macPaper, &width);
413
414 wxSize sz((int)(width * pt2mm + 0.5 ) ,
415 (int)(height * pt2mm + 0.5 ));
416 data.SetPaperSize(sz);
417 wxPaperSize id = wxThePrintPaperDatabase->GetSize(wxSize(sz.x* 10, sz.y * 10));
418 if (id != wxPAPER_NONE)
489468fe 419 {
c0b65970 420 data.SetPaperId(id);
489468fe
SC
421 }
422 return true ;
423}
424
c347101b 425void wxOSXPrintData::TransferFrom( wxPageSetupData *WXUNUSED(data) )
489468fe
SC
426{
427 // should we setup the page rect here ?
428 // since MacOS sometimes has two same paper rects with different
429 // page rects we could make it roundtrip safe perhaps
430}
431
c347101b 432void wxOSXPrintData::TransferTo( wxPageSetupData* data )
489468fe 433{
c347101b
SC
434#if wxOSX_USE_COCOA
435 UpdateToPMState();
436#endif
489468fe
SC
437 PMRect rPaper;
438 OSStatus err = PMGetUnadjustedPaperRect(m_macPageFormat, &rPaper);
439 if ( err == noErr )
440 {
441 wxSize sz((int)(( rPaper.right - rPaper.left ) * pt2mm + 0.5 ) ,
442 (int)(( rPaper.bottom - rPaper.top ) * pt2mm + 0.5 ));
443 data->SetPaperSize(sz);
444
445 PMRect rPage ;
446 err = PMGetUnadjustedPageRect(m_macPageFormat , &rPage ) ;
447 if ( err == noErr )
448 {
449 data->SetMinMarginTopLeft( wxPoint (
450 (int)(((double) rPage.left - rPaper.left ) * pt2mm) ,
451 (int)(((double) rPage.top - rPaper.top ) * pt2mm) ) ) ;
452
453 data->SetMinMarginBottomRight( wxPoint (
454 (wxCoord)(((double) rPaper.right - rPage.right ) * pt2mm),
455 (wxCoord)(((double) rPaper.bottom - rPage.bottom ) * pt2mm)) ) ;
456
457 if ( data->GetMarginTopLeft().x < data->GetMinMarginTopLeft().x )
458 data->SetMarginTopLeft( wxPoint( data->GetMinMarginTopLeft().x ,
459 data->GetMarginTopLeft().y ) ) ;
460
461 if ( data->GetMarginBottomRight().x < data->GetMinMarginBottomRight().x )
462 data->SetMarginBottomRight( wxPoint( data->GetMinMarginBottomRight().x ,
463 data->GetMarginBottomRight().y ) );
464
465 if ( data->GetMarginTopLeft().y < data->GetMinMarginTopLeft().y )
466 data->SetMarginTopLeft( wxPoint( data->GetMarginTopLeft().x , data->GetMinMarginTopLeft().y ) );
467
468 if ( data->GetMarginBottomRight().y < data->GetMinMarginBottomRight().y )
469 data->SetMarginBottomRight( wxPoint( data->GetMarginBottomRight().x ,
470 data->GetMinMarginBottomRight().y) );
471 }
472 }
473}
474
c347101b 475void wxOSXPrintData::TransferTo( wxPrintDialogData* data )
6f4968ce 476{
c347101b
SC
477#if wxOSX_USE_COCOA
478 UpdateToPMState();
479#endif
489468fe
SC
480 UInt32 minPage , maxPage ;
481 PMGetPageRange( m_macPrintSettings , &minPage , &maxPage ) ;
482 data->SetMinPage( minPage ) ;
483 data->SetMaxPage( maxPage ) ;
484 UInt32 copies ;
485 PMGetCopies( m_macPrintSettings , &copies ) ;
486 data->SetNoCopies( copies ) ;
487 UInt32 from , to ;
488 PMGetFirstPage( m_macPrintSettings , &from ) ;
489 PMGetLastPage( m_macPrintSettings , &to ) ;
490 if ( to >= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
491 {
492 data->SetAllPages( true ) ;
493 // This means all pages, more or less
494 data->SetFromPage(1);
c347101b 495 data->SetToPage(9999);
489468fe
SC
496 }
497 else
498 {
499 data->SetFromPage( from ) ;
500 data->SetToPage( to ) ;
501 data->SetAllPages( false );
502 }
503}
504
c347101b 505void wxOSXPrintData::TransferFrom( wxPrintDialogData* data )
489468fe
SC
506{
507 // Respect the value of m_printAllPages
508 if ( data->GetAllPages() )
509 PMSetPageRange( m_macPrintSettings , data->GetMinPage() , (UInt32) kPMPrintAllPages ) ;
510 else
511 PMSetPageRange( m_macPrintSettings , data->GetMinPage() , data->GetMaxPage() ) ;
512 PMSetCopies( m_macPrintSettings , data->GetNoCopies() , false ) ;
513 PMSetFirstPage( m_macPrintSettings , data->GetFromPage() , false ) ;
514
515 if (data->GetAllPages() || data->GetFromPage() == 0)
516 PMSetLastPage( m_macPrintSettings , (UInt32) kPMPrintAllPages, true ) ;
517 else
518 PMSetLastPage( m_macPrintSettings , (UInt32) data->GetToPage() , false ) ;
c347101b
SC
519#if wxOSX_USE_COCOA
520 UpdateFromPMState();
521#endif
522}
523
524wxPrintNativeDataBase* wxOSXCreatePrintData()
525{
526#if wxOSX_USE_COCOA
527 return new wxOSXCocoaPrintData();
65391c8f 528#elif wxOSX_USE_CARBON
c347101b 529 return new wxOSXCarbonPrintData();
65391c8f 530#else
c347101b 531 return NULL;
65391c8f 532#endif
489468fe
SC
533}
534
535/*
536* Printer
537*/
538
c347101b
SC
539IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase)
540
489468fe
SC
541wxMacPrinter::wxMacPrinter(wxPrintDialogData *data):
542wxPrinterBase(data)
543{
544}
545
546wxMacPrinter::~wxMacPrinter(void)
547{
548}
549
550bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
551{
552 sm_abortIt = false;
553 sm_abortWindow = NULL;
554
555 if (!printout)
477618ea
SC
556 {
557 sm_lastError = wxPRINTER_ERROR;
489468fe 558 return false;
477618ea 559 }
6f4968ce 560
489468fe 561 printout->SetIsPreview(false);
0661eb62 562
489468fe
SC
563 if (m_printDialogData.GetMinPage() < 1)
564 m_printDialogData.SetMinPage(1);
565 if (m_printDialogData.GetMaxPage() < 1)
566 m_printDialogData.SetMaxPage(9999);
6f4968ce 567
489468fe
SC
568 // Create a suitable device context
569 wxPrinterDC *dc = NULL;
570 if (prompt)
571 {
572 wxMacPrintDialog dialog(parent, & m_printDialogData);
573 if (dialog.ShowModal() == wxID_OK)
574 {
575 dc = wxDynamicCast(dialog.GetPrintDC(), wxPrinterDC);
576 wxASSERT(dc);
577 m_printDialogData = dialog.GetPrintDialogData();
578 }
579 }
580 else
581 {
582 dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ;
583 }
584
585 // May have pressed cancel.
586 if (!dc || !dc->IsOk())
587 {
0b1ca117 588 delete dc;
489468fe
SC
589 return false;
590 }
591
592 // on the mac we have always pixels as addressing mode with 72 dpi
593 printout->SetPPIScreen(72, 72);
884dad83 594
489468fe 595 PMResolution res;
884dad83 596 PMPrinter printer;
c347101b 597 wxOSXPrintData* nativeData = (wxOSXPrintData*)
489468fe 598 (m_printDialogData.GetPrintData().GetNativeData());
884dad83
SC
599
600 if (PMSessionGetCurrentPrinter(nativeData->GetPrintSession(), &printer) == noErr)
601 {
6f4968ce 602#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
884dad83
SC
603 if ( PMPrinterGetOutputResolution != NULL )
604 {
605 if (PMPrinterGetOutputResolution( printer, nativeData->GetPrintSettings(), &res) == -9589 /* kPMKeyNotFound */ )
606 {
607 res.hRes = res.vRes = 300;
608 }
609 }
610 else
3f2cd15f 611#endif
884dad83 612 {
6f4968ce 613#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
884dad83
SC
614 PMPrinterGetPrinterResolution(printer, kPMCurrentValue, &res);
615#endif
616 }
617 }
618 printout->SetPPIPrinter(int(res.hRes), int(res.vRes));
619
489468fe
SC
620 // Set printout parameters
621 printout->SetDC(dc);
622
623 int w, h;
624 dc->GetSize(&w, &h);
625 printout->SetPageSizePixels((int)w, (int)h);
626 printout->SetPaperRectPixels(dc->GetPaperRect());
627 wxCoord mw, mh;
628 dc->GetSizeMM(&mw, &mh);
629 printout->SetPageSizeMM((int)mw, (int)mh);
630
631 // Create an abort window
632 wxBeginBusyCursor();
633
634 printout->OnPreparePrinting();
635
477618ea
SC
636 // Get some parameters from the printout, if defined
637 int fromPage, toPage;
638 int minPage, maxPage;
639 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
6f4968ce 640
477618ea
SC
641 if (maxPage == 0)
642 {
643 sm_lastError = wxPRINTER_ERROR;
644 return false;
645 }
6f4968ce 646
477618ea
SC
647 // Only set min and max, because from and to will be
648 // set by the user
649 m_printDialogData.SetMinPage(minPage);
650 m_printDialogData.SetMaxPage(maxPage);
6f4968ce 651
489468fe
SC
652 printout->OnBeginPrinting();
653
654 bool keepGoing = true;
655
656 if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
657 {
658 wxEndBusyCursor();
659 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent);
660 }
661
662 int pn;
663 for (pn = m_printDialogData.GetFromPage();
664 keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
665 pn++)
666 {
667 if (sm_abortIt)
668 {
489468fe
SC
669 break;
670 }
671 else
672 {
673 dc->StartPage();
674 keepGoing = printout->OnPrintPage(pn);
675 dc->EndPage();
676 }
677 }
678 printout->OnEndDocument();
679
680 printout->OnEndPrinting();
681
682 if (sm_abortWindow)
683 {
684 sm_abortWindow->Show(false);
685 delete sm_abortWindow;
686 sm_abortWindow = NULL;
687 }
688
689 wxEndBusyCursor();
690
691 delete dc;
692
693 return true;
694}
695
696wxDC* wxMacPrinter::PrintDialog(wxWindow *parent)
697{
d3b9f782 698 wxDC* dc = NULL;
489468fe
SC
699
700 wxPrintDialog dialog(parent, & m_printDialogData);
701 int ret = dialog.ShowModal();
702
703 if (ret == wxID_OK)
704 {
705 dc = dialog.GetPrintDC();
706 m_printDialogData = dialog.GetPrintDialogData();
707 }
708
709 return dc;
710}
711
712bool wxMacPrinter::Setup(wxWindow *WXUNUSED(parent))
713{
714#if 0
715 wxPrintDialog dialog(parent, & m_printDialogData);
716 dialog.GetPrintDialogData().SetSetupDialog(true);
717
718 int ret = dialog.ShowModal();
719
720 if (ret == wxID_OK)
721 m_printDialogData = dialog.GetPrintDialogData();
722
723 return (ret == wxID_OK);
724#endif
725
65391c8f 726 return false;
489468fe
SC
727}
728
729/*
730* Print preview
731*/
732
c347101b
SC
733IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase)
734
489468fe
SC
735wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout,
736 wxPrintout *printoutForPrinting,
737 wxPrintDialogData *data)
738 : wxPrintPreviewBase(printout, printoutForPrinting, data)
739{
740 DetermineScaling();
741}
742
743wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data):
744wxPrintPreviewBase(printout, printoutForPrinting, data)
745{
746 DetermineScaling();
747}
748
749wxMacPrintPreview::~wxMacPrintPreview(void)
750{
751}
752
753bool wxMacPrintPreview::Print(bool interactive)
754{
755 if (!m_printPrintout)
756 return false;
757
758 wxMacPrinter printer(&m_printDialogData);
759 return printer.Print(m_previewFrame, m_printPrintout, interactive);
760}
761
762void wxMacPrintPreview::DetermineScaling(void)
763{
764 int screenWidth , screenHeight ;
765 wxDisplaySize( &screenWidth , &screenHeight ) ;
766
767 wxSize ppiScreen( 72 , 72 ) ;
768 wxSize ppiPrinter( 72 , 72 ) ;
03647350 769
489468fe
SC
770 // Note that with Leopard, screen dpi=72 is no longer a given
771 m_previewPrintout->SetPPIScreen( ppiScreen.x , ppiScreen.y ) ;
03647350 772
489468fe
SC
773 wxCoord w , h ;
774 wxCoord ww, hh;
775 wxRect paperRect;
776
777 // Get a device context for the currently selected printer
778 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
779 if (printerDC.IsOk())
780 {
781 printerDC.GetSizeMM(&ww, &hh);
782 printerDC.GetSize( &w , &h ) ;
783 ppiPrinter = printerDC.GetPPI() ;
784 paperRect = printerDC.GetPaperRect();
785 m_isOk = true ;
786 }
787 else
788 {
789 // use some defaults
790 w = 8 * 72 ;
791 h = 11 * 72 ;
792 ww = (wxCoord) (w * 25.4 / ppiPrinter.x) ;
793 hh = (wxCoord) (h * 25.4 / ppiPrinter.y) ;
794 paperRect = wxRect(0, 0, w, h);
795 m_isOk = false ;
796 }
797 m_pageWidth = w;
798 m_pageHeight = h;
03647350 799
489468fe
SC
800 m_previewPrintout->SetPageSizePixels(w , h) ;
801 m_previewPrintout->SetPageSizeMM(ww, hh);
802 m_previewPrintout->SetPaperRectPixels(paperRect);
803 m_previewPrintout->SetPPIPrinter( ppiPrinter.x , ppiPrinter.y ) ;
804
805 m_previewScaleX = float(ppiScreen.x) / ppiPrinter.x;
806 m_previewScaleY = float(ppiScreen.y) / ppiPrinter.y;
807}
808
c347101b
SC
809//
810// end of print_osx.cpp
811//
812
813#if wxOSX_USE_CARBON
814
815IMPLEMENT_DYNAMIC_CLASS(wxOSXCarbonPrintData, wxOSXPrintData)
816
817wxOSXCarbonPrintData::wxOSXCarbonPrintData()
818{
819 if ( PMCreateSession( &m_macPrintSession ) == noErr )
820 {
821 if ( PMCreatePageFormat(&m_macPageFormat) == noErr )
822 {
823 PMSessionDefaultPageFormat(m_macPrintSession,
824 m_macPageFormat);
825 PMGetPageFormatPaper(m_macPageFormat, &m_macPaper);
826 }
03647350 827
c347101b
SC
828 if ( PMCreatePrintSettings(&m_macPrintSettings) == noErr )
829 {
830 PMSessionDefaultPrintSettings(m_macPrintSession,
831 m_macPrintSettings);
832 }
833 }
834}
835
836wxOSXCarbonPrintData::~wxOSXCarbonPrintData()
837{
838 (void)PMRelease(m_macPageFormat);
839 (void)PMRelease(m_macPrintSettings);
840 (void)PMRelease(m_macPrintSession);
841}
842#endif
843
489468fe 844#endif