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