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