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