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