]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/printmac.cpp
A more complete fix for the generic control border issue, a fix for getting/setting...
[wxWidgets.git] / src / mac / carbon / printmac.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/mac/carbon/printwin.cpp
3// Purpose: wxMacPrinter framework
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
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
30#include "wx/mac/uma.h"
31
32#include "wx/mac/printmac.h"
33#include "wx/mac/private/print.h"
34
35#include "wx/printdlg.h"
36#include "wx/mac/printdlg.h"
37
38#include <stdlib.h>
39
40IMPLEMENT_DYNAMIC_CLASS(wxMacCarbonPrintData, wxPrintNativeDataBase)
41IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase)
42IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase)
43
44bool wxMacCarbonPrintData::IsOk() const
45{
46 return (m_macPageFormat != kPMNoPageFormat) && (m_macPrintSettings != kPMNoPrintSettings) && (m_macPrintSession != kPMNoReference);
47}
48wxMacCarbonPrintData::wxMacCarbonPrintData()
49{
50 m_macPageFormat = kPMNoPageFormat;
51 m_macPrintSettings = kPMNoPrintSettings;
52 m_macPrintSession = kPMNoReference ;
53 ValidateOrCreate() ;
54}
55
56wxMacCarbonPrintData::~wxMacCarbonPrintData()
57{
58 if (m_macPageFormat != kPMNoPageFormat)
59 {
60 (void)PMRelease(m_macPageFormat);
61 m_macPageFormat = kPMNoPageFormat;
62 }
63
64 if (m_macPrintSettings != kPMNoPrintSettings)
65 {
66 (void)PMRelease(m_macPrintSettings);
67 m_macPrintSettings = kPMNoPrintSettings;
68 }
69
70 if ( m_macPrintSession != kPMNoReference )
71 {
72 (void)PMRelease(m_macPrintSession);
73 m_macPrintSession = kPMNoReference;
74 }
75}
76
77void wxMacCarbonPrintData::ValidateOrCreate()
78{
79 OSStatus err = noErr ;
80 if ( m_macPrintSession == kPMNoReference )
81 {
82 err = PMCreateSession( (PMPrintSession *) &m_macPrintSession ) ;
83 }
84 // Set up a valid PageFormat object.
85 if ( m_macPageFormat == kPMNoPageFormat)
86 {
87 err = PMCreatePageFormat((PMPageFormat *) &m_macPageFormat);
88
89 // Note that PMPageFormat is not session-specific, but calling
90 // PMSessionDefaultPageFormat assigns values specific to the printer
91 // associated with the current printing session.
92 if ((err == noErr) &&
93 ( m_macPageFormat != kPMNoPageFormat))
94 {
95 err = PMSessionDefaultPageFormat((PMPrintSession) m_macPrintSession,
96 (PMPageFormat) m_macPageFormat);
97 }
98 }
99 else
100 {
101 err = PMSessionValidatePageFormat((PMPrintSession) m_macPrintSession,
102 (PMPageFormat) m_macPageFormat,
103 kPMDontWantBoolean);
104 }
105
106 // Set up a valid PrintSettings object.
107 if ( m_macPrintSettings == kPMNoPrintSettings)
108 {
109 err = PMCreatePrintSettings((PMPrintSettings *) &m_macPrintSettings);
110
111 // Note that PMPrintSettings is not session-specific, but calling
112 // PMSessionDefaultPrintSettings assigns values specific to the printer
113 // associated with the current printing session.
114 if ((err == noErr) &&
115 ( m_macPrintSettings != kPMNoPrintSettings))
116 {
117 err = PMSessionDefaultPrintSettings((PMPrintSession) m_macPrintSession,
118 (PMPrintSettings) m_macPrintSettings);
119 }
120 }
121 else
122 {
123 err = PMSessionValidatePrintSettings((PMPrintSession) m_macPrintSession,
124 (PMPrintSettings) m_macPrintSettings,
125 kPMDontWantBoolean);
126 }
127}
128
129bool wxMacCarbonPrintData::TransferFrom( const wxPrintData &data )
130{
131 ValidateOrCreate() ;
132 PMSetCopies( (PMPrintSettings) m_macPrintSettings , data.GetNoCopies() , false ) ;
133 if ( data.IsOrientationReversed() )
134 PMSetOrientation( (PMPageFormat) m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
135 kPMReverseLandscape : kPMReversePortrait , false ) ;
136 else
137 PMSetOrientation( (PMPageFormat) m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
138 kPMLandscape : kPMPortrait , false ) ;
139 // collate cannot be set
140#if 0 // not yet tested
141 if ( !m_printerName.empty() )
142 PMSessionSetCurrentPrinter( (PMPrintSession) m_macPrintSession , wxMacCFStringHolder( m_printerName , wxFont::GetDefaultEncoding() ) ) ;
143#endif
144#ifndef __LP64__
145 PMColorMode color ;
146 PMGetColorMode( (PMPrintSettings) m_macPrintSettings, &color ) ;
147 if ( data.GetColour() )
148 {
149 if ( color == kPMBlackAndWhite )
150 PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMColor ) ;
151 }
152 else
153 PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMBlackAndWhite ) ;
154#endif
155
156 // PMDuplexMode not yet accessible via API
157 // PMQualityMode not yet accessible via API
158 // todo paperSize
159
160 PMResolution res;
161 PMPrinter printer;
162 PMSessionGetCurrentPrinter(m_macPrintSession, &printer);
163#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
164 PMPrinterGetOutputResolution( printer,
165 (PMPrintSettings) m_macPrintSettings, &res) ;
166 // TODO transfer ? into page format ?
167#else
168 PMTag tag = kPMMaxSquareResolution;
169 PMPrinterGetPrinterResolution(printer, tag, &res);
170 PMSetResolution((PMPageFormat) m_macPageFormat, &res);
171#endif
172 // after setting the new resolution the format has to be updated, otherwise the page rect remains
173 // at the 'old' scaling
174 PMSessionValidatePageFormat((PMPrintSession) m_macPrintSession,
175 (PMPageFormat) m_macPageFormat,
176 kPMDontWantBoolean) ;
177
178 return true ;
179}
180
181bool wxMacCarbonPrintData::TransferTo( wxPrintData &data )
182{
183 OSStatus err = noErr ;
184
185 UInt32 copies ;
186 err = PMGetCopies( m_macPrintSettings , &copies ) ;
187 if ( err == noErr )
188 data.SetNoCopies( copies ) ;
189
190 PMOrientation orientation ;
191 err = PMGetOrientation( m_macPageFormat , &orientation ) ;
192 if ( err == noErr )
193 {
194 if ( orientation == kPMPortrait || orientation == kPMReversePortrait )
195 {
196 data.SetOrientation( wxPORTRAIT );
197 data.SetOrientationReversed( orientation == kPMReversePortrait );
198 }
199 else
200 {
201 data.SetOrientation( wxLANDSCAPE );
202 data.SetOrientationReversed( orientation == kPMReverseLandscape );
203 }
204 }
205
206 // collate cannot be set
207#if 0
208 {
209 wxMacCFStringHolder name ;
210 PMPrinter printer ;
211 PMSessionGetCurrentPrinter( m_macPrintSession ,
212 &printer ) ;
213 m_printerName = name.AsString() ;
214 }
215#endif
216
217#ifndef __LP64__
218 PMColorMode color ;
219 err = PMGetColorMode( m_macPrintSettings, &color ) ;
220 if ( err == noErr )
221 data.SetColour( !(color == kPMBlackAndWhite) ) ;
222#endif
223 // PMDuplexMode not yet accessible via API
224 // PMQualityMode not yet accessible via API
225 // todo paperSize
226 PMRect rPaper;
227 err = PMGetUnadjustedPaperRect( m_macPageFormat, &rPaper);
228 if ( err == noErr )
229 {
230 data.SetPaperSize( wxSize (
231 (int)(( rPaper.right - rPaper.left ) * pt2mm + 0.5 ) ,
232 (int)(( rPaper.bottom - rPaper.top ) * pt2mm + 0.5 ) ) );
233 }
234 return true ;
235}
236
237void wxMacCarbonPrintData::TransferFrom( wxPageSetupData *data )
238{
239 // should we setup the page rect here ?
240 // since MacOS sometimes has two same paper rects with different
241 // page rects we could make it roundtrip safe perhaps
242}
243
244void wxMacCarbonPrintData::TransferTo( wxPageSetupData* data )
245{
246 PMRect rPaper;
247 OSStatus err = PMGetUnadjustedPaperRect(m_macPageFormat, &rPaper);
248 if ( err == noErr )
249 {
250 PMRect rPage ;
251 err = PMGetUnadjustedPageRect(m_macPageFormat , &rPage ) ;
252 if ( err == noErr )
253 {
254 data->SetMinMarginTopLeft( wxPoint (
255 (int)(((double) rPage.left - rPaper.left ) * pt2mm) ,
256 (int)(((double) rPage.top - rPaper.top ) * pt2mm) ) ) ;
257
258 data->SetMinMarginBottomRight( wxPoint (
259 (wxCoord)(((double) rPaper.right - rPage.right ) * pt2mm),
260 (wxCoord)(((double) rPaper.bottom - rPage.bottom ) * pt2mm)) ) ;
261
262 if ( data->GetMarginTopLeft().x < data->GetMinMarginTopLeft().x )
263 data->SetMarginTopLeft( wxPoint( data->GetMinMarginTopLeft().x ,
264 data->GetMarginTopLeft().y ) ) ;
265
266 if ( data->GetMarginBottomRight().x < data->GetMinMarginBottomRight().x )
267 data->SetMarginBottomRight( wxPoint( data->GetMinMarginBottomRight().x ,
268 data->GetMarginBottomRight().y ) );
269
270 if ( data->GetMarginTopLeft().y < data->GetMinMarginTopLeft().y )
271 data->SetMarginTopLeft( wxPoint( data->GetMarginTopLeft().x , data->GetMinMarginTopLeft().y ) );
272
273 if ( data->GetMarginBottomRight().y < data->GetMinMarginBottomRight().y )
274 data->SetMarginBottomRight( wxPoint( data->GetMarginBottomRight().x ,
275 data->GetMinMarginBottomRight().y) );
276 }
277 }
278}
279
280void wxMacCarbonPrintData::TransferTo( wxPrintDialogData* data )
281{
282 UInt32 minPage , maxPage ;
283 PMGetPageRange( m_macPrintSettings , &minPage , &maxPage ) ;
284 data->SetMinPage( minPage ) ;
285 data->SetMaxPage( maxPage ) ;
286 UInt32 copies ;
287 PMGetCopies( m_macPrintSettings , &copies ) ;
288 data->SetNoCopies( copies ) ;
289 UInt32 from , to ;
290 PMGetFirstPage( m_macPrintSettings , &from ) ;
291 PMGetLastPage( m_macPrintSettings , &to ) ;
292 if ( to >= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
293 {
294 data->SetAllPages( true ) ;
295 // This means all pages, more or less
296 data->SetFromPage(1);
297 data->SetToPage(32000);
298 }
299 else
300 {
301 data->SetFromPage( from ) ;
302 data->SetToPage( to ) ;
303 data->SetAllPages( false );
304 }
305}
306
307void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData* data )
308{
309 PMSetPageRange( m_macPrintSettings , data->GetMinPage() , data->GetMaxPage() ) ;
310 PMSetCopies( m_macPrintSettings , data->GetNoCopies() , false ) ;
311 PMSetFirstPage( m_macPrintSettings , data->GetFromPage() , false ) ;
312
313 if (data->GetAllPages() || data->GetFromPage() == 0)
314 PMSetLastPage( m_macPrintSettings , (UInt32) kPMPrintAllPages, true ) ;
315 else
316 PMSetLastPage( m_macPrintSettings , (UInt32) data->GetToPage() , false ) ;
317}
318
319/*
320* Printer
321*/
322
323wxMacPrinter::wxMacPrinter(wxPrintDialogData *data):
324wxPrinterBase(data)
325{
326}
327
328wxMacPrinter::~wxMacPrinter(void)
329{
330}
331
332bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
333{
334 sm_abortIt = false;
335 sm_abortWindow = NULL;
336
337 if (!printout)
338 return false;
339
340 printout->SetIsPreview(false);
341 if (m_printDialogData.GetMinPage() < 1)
342 m_printDialogData.SetMinPage(1);
343 if (m_printDialogData.GetMaxPage() < 1)
344 m_printDialogData.SetMaxPage(9999);
345
346 // Create a suitable device context
347 wxPrinterDC *dc = NULL;
348 if (prompt)
349 {
350 wxMacPrintDialog dialog(parent, & m_printDialogData);
351 if (dialog.ShowModal() == wxID_OK)
352 {
353 dc = wxDynamicCast(dialog.GetPrintDC(), wxPrinterDC);
354 wxASSERT(dc);
355 m_printDialogData = dialog.GetPrintDialogData();
356 }
357 }
358 else
359 {
360 dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ;
361 }
362
363 // May have pressed cancel.
364 if (!dc || !dc->Ok())
365 {
366 if (dc)
367 delete dc;
368 return false;
369 }
370
371 // on the mac we have always pixels as addressing mode with 72 dpi
372 printout->SetPPIScreen(72, 72);
373 PMResolution res;
374 wxMacCarbonPrintData* nativeData = (wxMacCarbonPrintData*)
375 (m_printDialogData.GetPrintData().GetNativeData());
376#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
377 PMPrinter printer;
378 PMSessionGetCurrentPrinter(nativeData->m_macPrintSession, &printer);
379 PMPrinterGetOutputResolution( printer, nativeData->m_macPrintSettings, &res) ;
380#else
381 PMGetResolution((PMPageFormat) (nativeData->m_macPageFormat), &res);
382#endif
383 printout->SetPPIPrinter(int(res.hRes), int(res.vRes));
384
385 // Set printout parameters
386 printout->SetDC(dc);
387
388 int w, h;
389 dc->GetSize(&w, &h);
390 printout->SetPageSizePixels((int)w, (int)h);
391 printout->SetPaperRectPixels(dc->GetPaperRect());
392 wxCoord mw, mh;
393 dc->GetSizeMM(&mw, &mh);
394 printout->SetPageSizeMM((int)mw, (int)mh);
395
396 // Create an abort window
397 wxBeginBusyCursor();
398
399 printout->OnPreparePrinting();
400
401 // Get some parameters from the printout, if defined
402 int fromPage, toPage;
403 int minPage, maxPage;
404 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
405
406 if (maxPage == 0)
407 {
408 wxEndBusyCursor();
409 return false;
410 }
411
412 // Only set min and max, because from and to have been
413 // set by the user
414 m_printDialogData.SetMinPage(minPage);
415 m_printDialogData.SetMaxPage(maxPage);
416
417 wxWindow *win = CreateAbortWindow(parent, printout);
418 wxSafeYield(win,true);
419
420 if (!win)
421 {
422 wxEndBusyCursor();
423 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK, parent);
424 delete dc;
425
426 return false;
427 }
428
429 sm_abortWindow = win;
430 sm_abortWindow->Show(true);
431 wxSafeYield(win,true);
432
433 printout->OnBeginPrinting();
434
435 bool keepGoing = true;
436
437 int copyCount;
438 for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++)
439 {
440 if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
441 {
442 wxEndBusyCursor();
443 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent);
444 break;
445 }
446 if (sm_abortIt)
447 break;
448
449 int pn;
450 for (pn = m_printDialogData.GetFromPage();
451 keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
452 pn++)
453 {
454 if (sm_abortIt)
455 {
456 keepGoing = false;
457 break;
458 }
459 else
460 {
461#if TARGET_CARBON
462 if ( UMAGetSystemVersion() >= 0x1000 )
463#endif
464 {
465#if !wxMAC_USE_CORE_GRAPHICS
466 GrafPtr thePort ;
467 GetPort( &thePort ) ;
468#endif
469 wxSafeYield(win,true);
470#if !wxMAC_USE_CORE_GRAPHICS
471 SetPort( thePort ) ;
472#endif
473 }
474 dc->StartPage();
475 keepGoing = printout->OnPrintPage(pn);
476 dc->EndPage();
477 }
478 }
479 printout->OnEndDocument();
480 }
481
482 printout->OnEndPrinting();
483
484 if (sm_abortWindow)
485 {
486 sm_abortWindow->Show(false);
487 delete sm_abortWindow;
488 sm_abortWindow = NULL;
489 }
490
491 wxEndBusyCursor();
492
493 delete dc;
494
495 return true;
496}
497
498wxDC* wxMacPrinter::PrintDialog(wxWindow *parent)
499{
500 wxDC* dc = (wxDC*) NULL;
501
502 wxPrintDialog dialog(parent, & m_printDialogData);
503 int ret = dialog.ShowModal();
504
505 if (ret == wxID_OK)
506 {
507 dc = dialog.GetPrintDC();
508 m_printDialogData = dialog.GetPrintDialogData();
509 }
510
511 return dc;
512}
513
514bool wxMacPrinter::Setup(wxWindow *parent)
515{
516#if 0
517 wxPrintDialog dialog(parent, & m_printDialogData);
518 dialog.GetPrintDialogData().SetSetupDialog(true);
519
520 int ret = dialog.ShowModal();
521
522 if (ret == wxID_OK)
523 m_printDialogData = dialog.GetPrintDialogData();
524
525 return (ret == wxID_OK);
526#endif
527
528 return wxID_CANCEL;
529}
530
531/*
532* Print preview
533*/
534
535wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout,
536 wxPrintout *printoutForPrinting,
537 wxPrintDialogData *data)
538 : wxPrintPreviewBase(printout, printoutForPrinting, data)
539{
540 DetermineScaling();
541}
542
543wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data):
544wxPrintPreviewBase(printout, printoutForPrinting, data)
545{
546 DetermineScaling();
547}
548
549wxMacPrintPreview::~wxMacPrintPreview(void)
550{
551}
552
553bool wxMacPrintPreview::Print(bool interactive)
554{
555 if (!m_printPrintout)
556 return false;
557
558 wxMacPrinter printer(&m_printDialogData);
559 return printer.Print(m_previewFrame, m_printPrintout, interactive);
560}
561
562void wxMacPrintPreview::DetermineScaling(void)
563{
564 int screenWidth , screenHeight ;
565 wxDisplaySize( &screenWidth , &screenHeight ) ;
566
567 wxSize ppiScreen( 72 , 72 ) ;
568 wxSize ppiPrinter( 72 , 72 ) ;
569
570 // Note that with Leopard, screen dpi=72 is no longer a given
571 m_previewPrintout->SetPPIScreen( ppiScreen.x , ppiScreen.y ) ;
572
573 wxCoord w , h ;
574 wxCoord ww, hh;
575 wxRect paperRect;
576
577 // Get a device context for the currently selected printer
578 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
579 if (printerDC.Ok())
580 {
581 printerDC.GetSizeMM(&ww, &hh);
582 printerDC.GetSize( &w , &h ) ;
583 ppiPrinter = printerDC.GetPPI() ;
584 paperRect = printerDC.GetPaperRect();
585 m_isOk = true ;
586 }
587 else
588 {
589 // use some defaults
590 w = 8 * 72 ;
591 h = 11 * 72 ;
592 ww = (wxCoord) (w * 25.4 / ppiPrinter.x) ;
593 hh = (wxCoord) (h * 25.4 / ppiPrinter.y) ;
594 paperRect = wxRect(0, 0, w, h);
595 m_isOk = false ;
596 }
597 m_pageWidth = w;
598 m_pageHeight = h;
599
600 m_previewPrintout->SetPageSizePixels(w , h) ;
601 m_previewPrintout->SetPageSizeMM(ww, hh);
602 m_previewPrintout->SetPaperRectPixels(paperRect);
603 m_previewPrintout->SetPPIPrinter( ppiPrinter.x , ppiPrinter.y ) ;
604
605 m_previewScaleX = float(ppiScreen.x) / ppiPrinter.x;
606 m_previewScaleY = float(ppiScreen.y) / ppiPrinter.y;
607}
608
609#endif