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