]> git.saurik.com Git - wxWidgets.git/blame - src/osx/core/printmac.cpp
Make wxEVT_CHAR_HOOK propagate upwards and send it to the window itself.
[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 }
4892e67d
SC
654 else
655 {
656 // fallback
657 res.hRes = res.vRes = 300;
658 }
884dad83
SC
659 printout->SetPPIPrinter(int(res.hRes), int(res.vRes));
660
489468fe
SC
661 // Set printout parameters
662 printout->SetDC(dc);
663
664 int w, h;
665 dc->GetSize(&w, &h);
666 printout->SetPageSizePixels((int)w, (int)h);
667 printout->SetPaperRectPixels(dc->GetPaperRect());
668 wxCoord mw, mh;
669 dc->GetSizeMM(&mw, &mh);
670 printout->SetPageSizeMM((int)mw, (int)mh);
671
672 // Create an abort window
673 wxBeginBusyCursor();
674
675 printout->OnPreparePrinting();
676
477618ea
SC
677 // Get some parameters from the printout, if defined
678 int fromPage, toPage;
679 int minPage, maxPage;
680 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
6f4968ce 681
477618ea
SC
682 if (maxPage == 0)
683 {
684 sm_lastError = wxPRINTER_ERROR;
685 return false;
686 }
6f4968ce 687
477618ea
SC
688 // Only set min and max, because from and to will be
689 // set by the user
690 m_printDialogData.SetMinPage(minPage);
691 m_printDialogData.SetMaxPage(maxPage);
6f4968ce 692
489468fe
SC
693 printout->OnBeginPrinting();
694
695 bool keepGoing = true;
696
697 if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
698 {
699 wxEndBusyCursor();
700 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent);
701 }
702
703 int pn;
704 for (pn = m_printDialogData.GetFromPage();
705 keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
706 pn++)
707 {
708 if (sm_abortIt)
709 {
489468fe
SC
710 break;
711 }
712 else
713 {
714 dc->StartPage();
715 keepGoing = printout->OnPrintPage(pn);
716 dc->EndPage();
717 }
718 }
719 printout->OnEndDocument();
720
721 printout->OnEndPrinting();
722
723 if (sm_abortWindow)
724 {
725 sm_abortWindow->Show(false);
5276b0a5 726 wxDELETE(sm_abortWindow);
489468fe
SC
727 }
728
729 wxEndBusyCursor();
730
731 delete dc;
732
733 return true;
734}
735
736wxDC* wxMacPrinter::PrintDialog(wxWindow *parent)
737{
d3b9f782 738 wxDC* dc = NULL;
489468fe
SC
739
740 wxPrintDialog dialog(parent, & m_printDialogData);
741 int ret = dialog.ShowModal();
742
743 if (ret == wxID_OK)
744 {
745 dc = dialog.GetPrintDC();
746 m_printDialogData = dialog.GetPrintDialogData();
747 }
748
749 return dc;
750}
751
752bool wxMacPrinter::Setup(wxWindow *WXUNUSED(parent))
753{
754#if 0
755 wxPrintDialog dialog(parent, & m_printDialogData);
756 dialog.GetPrintDialogData().SetSetupDialog(true);
757
758 int ret = dialog.ShowModal();
759
760 if (ret == wxID_OK)
761 m_printDialogData = dialog.GetPrintDialogData();
762
763 return (ret == wxID_OK);
764#endif
765
65391c8f 766 return false;
489468fe
SC
767}
768
769/*
770* Print preview
771*/
772
c347101b
SC
773IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase)
774
489468fe
SC
775wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout,
776 wxPrintout *printoutForPrinting,
777 wxPrintDialogData *data)
778 : wxPrintPreviewBase(printout, printoutForPrinting, data)
779{
780 DetermineScaling();
781}
782
783wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data):
784wxPrintPreviewBase(printout, printoutForPrinting, data)
785{
786 DetermineScaling();
787}
788
789wxMacPrintPreview::~wxMacPrintPreview(void)
790{
791}
792
793bool wxMacPrintPreview::Print(bool interactive)
794{
795 if (!m_printPrintout)
796 return false;
797
798 wxMacPrinter printer(&m_printDialogData);
799 return printer.Print(m_previewFrame, m_printPrintout, interactive);
800}
801
802void wxMacPrintPreview::DetermineScaling(void)
803{
804 int screenWidth , screenHeight ;
805 wxDisplaySize( &screenWidth , &screenHeight ) ;
806
807 wxSize ppiScreen( 72 , 72 ) ;
808 wxSize ppiPrinter( 72 , 72 ) ;
03647350 809
489468fe
SC
810 // Note that with Leopard, screen dpi=72 is no longer a given
811 m_previewPrintout->SetPPIScreen( ppiScreen.x , ppiScreen.y ) ;
03647350 812
489468fe
SC
813 wxCoord w , h ;
814 wxCoord ww, hh;
815 wxRect paperRect;
816
817 // Get a device context for the currently selected printer
818 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
819 if (printerDC.IsOk())
820 {
821 printerDC.GetSizeMM(&ww, &hh);
822 printerDC.GetSize( &w , &h ) ;
823 ppiPrinter = printerDC.GetPPI() ;
824 paperRect = printerDC.GetPaperRect();
825 m_isOk = true ;
826 }
827 else
828 {
829 // use some defaults
830 w = 8 * 72 ;
831 h = 11 * 72 ;
832 ww = (wxCoord) (w * 25.4 / ppiPrinter.x) ;
833 hh = (wxCoord) (h * 25.4 / ppiPrinter.y) ;
834 paperRect = wxRect(0, 0, w, h);
835 m_isOk = false ;
836 }
837 m_pageWidth = w;
838 m_pageHeight = h;
03647350 839
489468fe
SC
840 m_previewPrintout->SetPageSizePixels(w , h) ;
841 m_previewPrintout->SetPageSizeMM(ww, hh);
842 m_previewPrintout->SetPaperRectPixels(paperRect);
843 m_previewPrintout->SetPPIPrinter( ppiPrinter.x , ppiPrinter.y ) ;
844
845 m_previewScaleX = float(ppiScreen.x) / ppiPrinter.x;
846 m_previewScaleY = float(ppiScreen.y) / ppiPrinter.y;
847}
848
c347101b
SC
849//
850// end of print_osx.cpp
851//
852
853#if wxOSX_USE_CARBON
854
855IMPLEMENT_DYNAMIC_CLASS(wxOSXCarbonPrintData, wxOSXPrintData)
856
857wxOSXCarbonPrintData::wxOSXCarbonPrintData()
858{
859 if ( PMCreateSession( &m_macPrintSession ) == noErr )
860 {
861 if ( PMCreatePageFormat(&m_macPageFormat) == noErr )
862 {
863 PMSessionDefaultPageFormat(m_macPrintSession,
864 m_macPageFormat);
865 PMGetPageFormatPaper(m_macPageFormat, &m_macPaper);
866 }
03647350 867
c347101b
SC
868 if ( PMCreatePrintSettings(&m_macPrintSettings) == noErr )
869 {
870 PMSessionDefaultPrintSettings(m_macPrintSession,
871 m_macPrintSettings);
872 }
873 }
874}
875
876wxOSXCarbonPrintData::~wxOSXCarbonPrintData()
877{
878 (void)PMRelease(m_macPageFormat);
879 (void)PMRelease(m_macPrintSettings);
880 (void)PMRelease(m_macPrintSession);
881}
882#endif
883
489468fe 884#endif