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