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