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