]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/printmac.cpp
changed background color reference from light gray to assigned bkgd color
[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
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);
159
dc7ccb9c 160 return true ;
746d7582
SC
161}
162
dc7ccb9c 163bool wxMacCarbonPrintData::TransferTo( wxPrintData &data )
746d7582
SC
164{
165 OSStatus err = noErr ;
166
167 UInt32 copies ;
168 err = PMGetCopies( m_macPrintSettings , &copies ) ;
169 if ( err == noErr )
dc7ccb9c 170 data.SetNoCopies( copies ) ;
746d7582
SC
171
172 PMOrientation orientation ;
173 err = PMGetOrientation( m_macPageFormat , &orientation ) ;
174 if ( err == noErr )
175 {
176 if ( orientation == kPMPortrait || orientation == kPMReversePortrait )
dc7ccb9c 177 data.SetOrientation( wxPORTRAIT );
746d7582 178 else
dc7ccb9c 179 data.SetOrientation( wxLANDSCAPE );
746d7582
SC
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 )
dc7ccb9c 196 data.SetColour( !(color == kPMBlackAndWhite) ) ;
746d7582
SC
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 {
dc7ccb9c 205 data.SetPaperSize( wxSize (
746d7582
SC
206 (int)(( rPaper.right - rPaper.left ) * pt2mm + 0.5 ) ,
207 (int)(( rPaper.bottom - rPaper.top ) * pt2mm + 0.5 ) ) );
208 }
dc7ccb9c 209 return true ;
746d7582
SC
210}
211
212void 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
746d7582
SC
217}
218
219void 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 (
5be55d56
VZ
234 (wxCoord)(((double) rPaper.right - rPage.right ) * pt2mm),
235 (wxCoord)(((double) rPaper.bottom - rPage.bottom ) * pt2mm)) ) ;
dc7ccb9c
SC
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
746d7582
SC
252 }
253 }
254}
255
256void 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 ) ;
34a4e912
SC
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 }
746d7582
SC
281}
282
283void 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 ) ;
b386cd7a 288
34a4e912 289 if (data->GetAllPages() || data->GetFromPage() == 0)
35d1986e 290 {
34a4e912 291 PMSetLastPage( m_macPrintSettings , (UInt32) kPMPrintAllPages, true ) ;
35d1986e
SC
292 }
293 else
294 {
34a4e912 295 PMSetLastPage( m_macPrintSettings , (UInt32) data->GetToPage() , false ) ;
35d1986e 296 }
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{
e40298d5
JS
314 sm_abortIt = FALSE;
315 sm_abortWindow = NULL;
72e7876b 316
e40298d5
JS
317 if (!printout)
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
e40298d5
JS
326 // Create a suitable device context
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 }
e40298d5
JS
341
342
343 // May have pressed cancel.
344 if (!dc || !dc->Ok())
72e7876b 345 {
e40298d5
JS
346 if (dc) delete dc;
347 return FALSE;
72e7876b 348 }
e40298d5
JS
349
350 // on the mac we have always pixels as addressing mode with 72 dpi
351
352 printout->SetPPIScreen(72, 72);
8b285841
SC
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));
e40298d5
JS
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
d2b354f9
JS
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
e40298d5
JS
390 wxWindow *win = CreateAbortWindow(parent, printout);
391 wxSafeYield(win,true);
392
393 if (!win)
394 {
395 wxEndBusyCursor();
427ff662 396 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK, parent);
e40298d5
JS
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();
427ff662 414 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent);
e40298d5
JS
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 {
5c45e86b
SC
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 }
e40298d5
JS
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;
72e7876b
SC
462}
463
464wxDC* wxMacPrinter::PrintDialog(wxWindow *parent)
465{
466 wxDC* dc = (wxDC*) NULL;
e40298d5 467
72e7876b
SC
468 wxPrintDialog dialog(parent, & m_printDialogData);
469 int ret = dialog.ShowModal();
e40298d5 470
72e7876b
SC
471 if (ret == wxID_OK)
472 {
473 dc = dialog.GetPrintDC();
474 m_printDialogData = dialog.GetPrintDialogData();
475 }
e40298d5 476
72e7876b
SC
477 return dc;
478}
479
480bool wxMacPrinter::Setup(wxWindow *parent)
481{
04a014a5 482#if 0
72e7876b
SC
483 wxPrintDialog dialog(parent, & m_printDialogData);
484 dialog.GetPrintDialogData().SetSetupDialog(TRUE);
e40298d5 485
72e7876b 486 int ret = dialog.ShowModal();
e40298d5 487
72e7876b
SC
488 if (ret == wxID_OK)
489 {
490 m_printDialogData = dialog.GetPrintDialogData();
491 }
e40298d5 492
72e7876b 493 return (ret == wxID_OK);
04a014a5
RR
494#endif
495 return wxID_CANCEL;
72e7876b
SC
496}
497
498/*
e40298d5
JS
499* Print preview
500*/
72e7876b
SC
501
502wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout,
e40298d5
JS
503 wxPrintout *printoutForPrinting,
504 wxPrintDialogData *data)
505 : wxPrintPreviewBase(printout, printoutForPrinting, data)
72e7876b
SC
506{
507 DetermineScaling();
508}
509
510wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data):
e40298d5 511wxPrintPreviewBase(printout, printoutForPrinting, data)
72e7876b 512{
e40298d5 513 DetermineScaling();
72e7876b
SC
514}
515
516wxMacPrintPreview::~wxMacPrintPreview(void)
517{
518}
519
520bool wxMacPrintPreview::Print(bool interactive)
521{
e40298d5
JS
522 if (!m_printPrintout)
523 return FALSE;
524 wxMacPrinter printer(&m_printDialogData);
525 return printer.Print(m_previewFrame, m_printPrintout, interactive);
72e7876b
SC
526}
527
528void wxMacPrintPreview::DetermineScaling(void)
529{
e40298d5
JS
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 ) ;
2f1ae414
SC
537 m_pageWidth = 8 * 72 ;
538 m_pageHeight = 11 * 72 ;
539 m_previewScale = 1 ;
e40298d5 540
72e7876b 541 // Get a device context for the currently selected printer
2f1ae414
SC
542 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
543 if (printerDC.Ok())
72e7876b 544 {
e40298d5
JS
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 }
72e7876b 559 // At 100%, the page should look about page-size on the screen.
2f1ae414
SC
560 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
561 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);
562
34a4e912 563 m_previewScale = 1 ;
72e7876b 564}
179e085f
RN
565
566#endif