]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/printmac.cpp
getting out for 'addded' events early enough
[wxWidgets.git] / src / mac / carbon / printmac.cpp
CommitLineData
72e7876b 1/////////////////////////////////////////////////////////////////////////////
670f9935 2// Name: src/mac/carbon/printwin.cpp
72e7876b
SC
3// Purpose: wxMacPrinter framework
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
670f9935 9// Licence: wxWindows licence
72e7876b
SC
10/////////////////////////////////////////////////////////////////////////////
11
72e7876b
SC
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
179e085f
RN
15#if wxUSE_PRINTING_ARCHITECTURE
16
72e7876b 17#ifdef __BORLANDC__
670f9935 18 #pragma hdrstop
72e7876b
SC
19#endif
20
72e7876b 21#ifndef WX_PRECOMP
670f9935
WS
22 #include "wx/utils.h"
23 #include "wx/dc.h"
24 #include "wx/app.h"
25 #include "wx/msgdlg.h"
6d50343d 26 #include "wx/dcprint.h"
18680f86 27 #include "wx/math.h"
72e7876b
SC
28#endif
29
746d7582 30#include "wx/mac/uma.h"
76a5e5d2 31
72e7876b 32#include "wx/mac/printmac.h"
746d7582
SC
33#include "wx/mac/private/print.h"
34
72e7876b 35#include "wx/printdlg.h"
08680429 36#include "wx/mac/printdlg.h"
72e7876b
SC
37
38#include <stdlib.h>
39
dc7ccb9c 40IMPLEMENT_DYNAMIC_CLASS(wxMacCarbonPrintData, wxPrintNativeDataBase)
72e7876b
SC
41IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase)
42IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase)
72e7876b 43
670f9935 44bool wxMacCarbonPrintData::Ok() const
746d7582 45{
dc7ccb9c 46 return (m_macPageFormat != kPMNoPageFormat) && (m_macPrintSettings != kPMNoPrintSettings) && (m_macPrintSession != kPMNoReference);
746d7582 47}
746d7582
SC
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 }
670f9935 69
746d7582
SC
70 if ( m_macPrintSession != kPMNoReference )
71 {
72 (void)PMRelease(m_macPrintSession);
73 m_macPrintSession = kPMNoReference;
74 }
75}
76
670f9935 77void wxMacCarbonPrintData::ValidateOrCreate()
746d7582
SC
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);
670f9935 88
746d7582
SC
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 }
670f9935 105
746d7582
SC
106 // Set up a valid PrintSettings object.
107 if ( m_macPrintSettings == kPMNoPrintSettings)
108 {
109 err = PMCreatePrintSettings((PMPrintSettings *) &m_macPrintSettings);
670f9935 110
746d7582
SC
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
dc7ccb9c 129bool wxMacCarbonPrintData::TransferFrom( const wxPrintData &data )
746d7582
SC
130{
131 ValidateOrCreate() ;
dc7ccb9c
SC
132 PMSetCopies( (PMPrintSettings) m_macPrintSettings , data.GetNoCopies() , false ) ;
133 PMSetOrientation( (PMPageFormat) m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
746d7582
SC
134 kPMLandscape : kPMPortrait , false ) ;
135 // collate cannot be set
136#if 0 // not yet tested
670f9935 137 if ( !m_printerName.empty() )
a9412f8f 138 PMSessionSetCurrentPrinter( (PMPrintSession) m_macPrintSession , wxMacCFStringHolder( m_printerName , wxFont::GetDefaultEncoding() ) ) ;
746d7582
SC
139#endif
140 PMColorMode color ;
141 PMGetColorMode( (PMPrintSettings) m_macPrintSettings, &color ) ;
dc7ccb9c 142 if ( data.GetColour() )
746d7582
SC
143 {
144 if ( color == kPMBlackAndWhite )
145 PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMColor ) ;
146 }
147 else
148 PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMBlackAndWhite ) ;
670f9935 149
746d7582
SC
150 // PMDuplexMode not yet accessible via API
151 // PMQualityMode not yet accessible via API
152 // todo paperSize
8b285841
SC
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);
aee23798
SC
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) ;
8b285841 164
dc7ccb9c 165 return true ;
746d7582
SC
166}
167
dc7ccb9c 168bool wxMacCarbonPrintData::TransferTo( wxPrintData &data )
746d7582
SC
169{
170 OSStatus err = noErr ;
670f9935 171
746d7582
SC
172 UInt32 copies ;
173 err = PMGetCopies( m_macPrintSettings , &copies ) ;
174 if ( err == noErr )
670f9935
WS
175 data.SetNoCopies( copies ) ;
176
746d7582
SC
177 PMOrientation orientation ;
178 err = PMGetOrientation( m_macPageFormat , &orientation ) ;
179 if ( err == noErr )
180 {
181 if ( orientation == kPMPortrait || orientation == kPMReversePortrait )
dc7ccb9c 182 data.SetOrientation( wxPORTRAIT );
746d7582 183 else
dc7ccb9c 184 data.SetOrientation( wxLANDSCAPE );
746d7582
SC
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
670f9935 197
746d7582
SC
198 PMColorMode color ;
199 err = PMGetColorMode( m_macPrintSettings, &color ) ;
200 if ( err == noErr )
dc7ccb9c 201 data.SetColour( !(color == kPMBlackAndWhite) ) ;
670f9935 202
746d7582
SC
203 // PMDuplexMode not yet accessible via API
204 // PMQualityMode not yet accessible via API
205 // todo paperSize
670f9935 206 PMRect rPaper;
746d7582
SC
207 err = PMGetUnadjustedPaperRect( m_macPageFormat, &rPaper);
208 if ( err == noErr )
209 {
dc7ccb9c 210 data.SetPaperSize( wxSize (
746d7582
SC
211 (int)(( rPaper.right - rPaper.left ) * pt2mm + 0.5 ) ,
212 (int)(( rPaper.bottom - rPaper.top ) * pt2mm + 0.5 ) ) );
213 }
dc7ccb9c 214 return true ;
746d7582
SC
215}
216
217void 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
746d7582
SC
222}
223
224void wxMacCarbonPrintData::TransferTo( wxPageSetupData* data )
225{
1aa7b427 226 PMRect rPaper;
746d7582
SC
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 (
670f9935
WS
235 (int)(((double) rPage.left - rPaper.left ) * pt2mm) ,
236 (int)(((double) rPage.top - rPaper.top ) * pt2mm) ) ) ;
237
238 data->SetMinMarginBottomRight( wxPoint (
5be55d56
VZ
239 (wxCoord)(((double) rPaper.right - rPage.right ) * pt2mm),
240 (wxCoord)(((double) rPaper.bottom - rPage.bottom ) * pt2mm)) ) ;
dc7ccb9c
SC
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) );
670f9935
WS
256 }
257 }
746d7582
SC
258}
259
260void 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 ) ;
34a4e912
SC
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 }
746d7582
SC
285}
286
287void 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 ) ;
b386cd7a 292
34a4e912 293 if (data->GetAllPages() || data->GetFromPage() == 0)
1aa7b427
DS
294 PMSetLastPage( m_macPrintSettings , (UInt32) kPMPrintAllPages, true ) ;
295 else
296 PMSetLastPage( m_macPrintSettings , (UInt32) data->GetToPage() , false ) ;
746d7582
SC
297}
298
72e7876b 299/*
e40298d5
JS
300* Printer
301*/
302
72e7876b 303wxMacPrinter::wxMacPrinter(wxPrintDialogData *data):
e40298d5 304wxPrinterBase(data)
72e7876b
SC
305{
306}
307
308wxMacPrinter::~wxMacPrinter(void)
309{
310}
311
312bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
313{
1aa7b427 314 sm_abortIt = false;
e40298d5 315 sm_abortWindow = NULL;
1aa7b427 316
e40298d5 317 if (!printout)
1aa7b427
DS
318 return false;
319
320 printout->SetIsPreview(false);
d2b354f9
JS
321 if (m_printDialogData.GetMinPage() < 1)
322 m_printDialogData.SetMinPage(1);
323 if (m_printDialogData.GetMaxPage() < 1)
324 m_printDialogData.SetMaxPage(9999);
325
670f9935 326 // Create a suitable device context
e40298d5
JS
327 wxDC *dc = NULL;
328 if (prompt)
329 {
2f1ae414
SC
330 wxPrintDialog dialog(parent, & m_printDialogData);
331 if (dialog.ShowModal() == wxID_OK)
e40298d5
JS
332 {
333 dc = dialog.GetPrintDC();
ffcd5195 334 m_printDialogData = dialog.GetPrintDialogData();
e40298d5
JS
335 }
336 }
337 else
72e7876b 338 {
e40298d5 339 dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ;
72e7876b 340 }
1aa7b427 341
e40298d5
JS
342 // May have pressed cancel.
343 if (!dc || !dc->Ok())
72e7876b 344 {
1aa7b427
DS
345 if (dc)
346 delete dc;
347 return false;
72e7876b 348 }
1aa7b427 349
e40298d5 350 // on the mac we have always pixels as addressing mode with 72 dpi
e40298d5 351 printout->SetPPIScreen(72, 72);
8b285841
SC
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));
1aa7b427
DS
357
358 // Set printout parameters
e40298d5 359 printout->SetDC(dc);
1aa7b427 360
e40298d5
JS
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);
1aa7b427 367
e40298d5
JS
368 // Create an abort window
369 wxBeginBusyCursor();
1aa7b427 370
d2b354f9 371 printout->OnPreparePrinting();
1aa7b427 372
d2b354f9
JS
373 // Get some parameters from the printout, if defined
374 int fromPage, toPage;
375 int minPage, maxPage;
376 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
1aa7b427 377
d2b354f9
JS
378 if (maxPage == 0)
379 {
380 wxEndBusyCursor();
1aa7b427 381 return false;
d2b354f9 382 }
1aa7b427 383
d2b354f9
JS
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);
1aa7b427 388
e40298d5
JS
389 wxWindow *win = CreateAbortWindow(parent, printout);
390 wxSafeYield(win,true);
1aa7b427 391
e40298d5
JS
392 if (!win)
393 {
394 wxEndBusyCursor();
427ff662 395 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK, parent);
e40298d5 396 delete dc;
1aa7b427
DS
397
398 return false;
e40298d5 399 }
1aa7b427 400
e40298d5 401 sm_abortWindow = win;
1aa7b427 402 sm_abortWindow->Show(true);
e40298d5 403 wxSafeYield(win,true);
1aa7b427 404
e40298d5 405 printout->OnBeginPrinting();
1aa7b427
DS
406
407 bool keepGoing = true;
408
e40298d5
JS
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();
427ff662 415 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent);
e40298d5
JS
416 break;
417 }
418 if (sm_abortIt)
419 break;
1aa7b427 420
e40298d5 421 int pn;
670f9935 422 for (pn = m_printDialogData.GetFromPage();
1aa7b427 423 keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
e40298d5
JS
424 pn++)
425 {
426 if (sm_abortIt)
427 {
1aa7b427 428 keepGoing = false;
e40298d5
JS
429 break;
430 }
431 else
432 {
5c45e86b
SC
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 }
e40298d5
JS
442 dc->StartPage();
443 keepGoing = printout->OnPrintPage(pn);
444 dc->EndPage();
445 }
446 }
447 printout->OnEndDocument();
448 }
670f9935 449
e40298d5 450 printout->OnEndPrinting();
670f9935 451
e40298d5
JS
452 if (sm_abortWindow)
453 {
1aa7b427 454 sm_abortWindow->Show(false);
e40298d5
JS
455 delete sm_abortWindow;
456 sm_abortWindow = NULL;
457 }
670f9935 458
e40298d5 459 wxEndBusyCursor();
670f9935 460
e40298d5 461 delete dc;
670f9935 462
1aa7b427 463 return true;
72e7876b
SC
464}
465
466wxDC* wxMacPrinter::PrintDialog(wxWindow *parent)
467{
468 wxDC* dc = (wxDC*) NULL;
670f9935 469
72e7876b
SC
470 wxPrintDialog dialog(parent, & m_printDialogData);
471 int ret = dialog.ShowModal();
670f9935 472
72e7876b
SC
473 if (ret == wxID_OK)
474 {
475 dc = dialog.GetPrintDC();
476 m_printDialogData = dialog.GetPrintDialogData();
477 }
670f9935 478
72e7876b
SC
479 return dc;
480}
481
482bool wxMacPrinter::Setup(wxWindow *parent)
483{
04a014a5 484#if 0
72e7876b 485 wxPrintDialog dialog(parent, & m_printDialogData);
1aa7b427 486 dialog.GetPrintDialogData().SetSetupDialog(true);
670f9935 487
72e7876b 488 int ret = dialog.ShowModal();
670f9935 489
72e7876b 490 if (ret == wxID_OK)
72e7876b 491 m_printDialogData = dialog.GetPrintDialogData();
670f9935 492
72e7876b 493 return (ret == wxID_OK);
04a014a5 494#endif
1aa7b427 495
04a014a5 496 return wxID_CANCEL;
72e7876b
SC
497}
498
499/*
e40298d5
JS
500* Print preview
501*/
72e7876b
SC
502
503wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout,
e40298d5
JS
504 wxPrintout *printoutForPrinting,
505 wxPrintDialogData *data)
506 : wxPrintPreviewBase(printout, printoutForPrinting, data)
72e7876b
SC
507{
508 DetermineScaling();
509}
510
511wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data):
e40298d5 512wxPrintPreviewBase(printout, printoutForPrinting, data)
72e7876b 513{
e40298d5 514 DetermineScaling();
72e7876b
SC
515}
516
517wxMacPrintPreview::~wxMacPrintPreview(void)
518{
519}
520
521bool wxMacPrintPreview::Print(bool interactive)
522{
e40298d5 523 if (!m_printPrintout)
1aa7b427
DS
524 return false;
525
e40298d5
JS
526 wxMacPrinter printer(&m_printDialogData);
527 return printer.Print(m_previewFrame, m_printPrintout, interactive);
72e7876b
SC
528}
529
530void wxMacPrintPreview::DetermineScaling(void)
531{
e40298d5
JS
532 int screenWidth , screenHeight ;
533 wxDisplaySize( &screenWidth , &screenHeight ) ;
670f9935 534
aee23798
SC
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;
670f9935 542
72e7876b 543 // Get a device context for the currently selected printer
2f1ae414
SC
544 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
545 if (printerDC.Ok())
72e7876b 546 {
e40298d5
JS
547 printerDC.GetSizeMM(&ww, &hh);
548 printerDC.GetSize( &x , &y ) ;
aee23798 549 ppiPrinter = printerDC.GetPPI() ;
e40298d5
JS
550 m_isOk = true ;
551 }
552 else
553 {
aee23798
SC
554 // use some defaults
555 x = 8 * 72 ;
556 y = 11 * 72 ;
17d4f7ac
SC
557 ww = (int) (x * 25.4 / ppiPrinter.x) ;
558 hh = (int) (y * 25.4 / ppiPrinter.y) ;
e40298d5
JS
559 m_isOk = false ;
560 }
aee23798
SC
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 ) ;
1aa7b427 566
aee23798 567 m_previewScale = (float)((float)ppiScreen.x/(float)ppiPrinter.y);
72e7876b 568}
179e085f
RN
569
570#endif