]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/printmac.cpp
don't create conversion objects unless we really need to convert
[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
3d1a4878 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
72e7876b
SC
13#pragma implementation "printwin.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
179e085f
RN
19#if wxUSE_PRINTING_ARCHITECTURE
20
72e7876b
SC
21#ifdef __BORLANDC__
22#pragma hdrstop
23#endif
24
72e7876b
SC
25#ifndef WX_PRECOMP
26#include "wx/utils.h"
27#include "wx/dc.h"
28#include "wx/app.h"
29#include "wx/msgdlg.h"
30#endif
31
d5301e33 32#include "wx/math.h"
746d7582 33#include "wx/mac/uma.h"
76a5e5d2 34
72e7876b 35#include "wx/mac/printmac.h"
746d7582
SC
36#include "wx/mac/private/print.h"
37
72e7876b
SC
38#include "wx/dcprint.h"
39#include "wx/printdlg.h"
08680429 40#include "wx/mac/printdlg.h"
72e7876b
SC
41
42#include <stdlib.h>
43
dc7ccb9c 44IMPLEMENT_DYNAMIC_CLASS(wxMacCarbonPrintData, wxPrintNativeDataBase)
72e7876b
SC
45IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase)
46IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase)
72e7876b 47
dc7ccb9c 48bool wxMacCarbonPrintData::Ok() const
746d7582 49{
dc7ccb9c 50 return (m_macPageFormat != kPMNoPageFormat) && (m_macPrintSettings != kPMNoPrintSettings) && (m_macPrintSession != kPMNoReference);
746d7582 51}
746d7582
SC
52wxMacCarbonPrintData::wxMacCarbonPrintData()
53{
54 m_macPageFormat = kPMNoPageFormat;
55 m_macPrintSettings = kPMNoPrintSettings;
56 m_macPrintSession = kPMNoReference ;
57 ValidateOrCreate() ;
58}
59
60wxMacCarbonPrintData::~wxMacCarbonPrintData()
61{
62 if (m_macPageFormat != kPMNoPageFormat)
63 {
64 (void)PMRelease(m_macPageFormat);
65 m_macPageFormat = kPMNoPageFormat;
66 }
67
68 if (m_macPrintSettings != kPMNoPrintSettings)
69 {
70 (void)PMRelease(m_macPrintSettings);
71 m_macPrintSettings = kPMNoPrintSettings;
72 }
73
74 if ( m_macPrintSession != kPMNoReference )
75 {
76 (void)PMRelease(m_macPrintSession);
77 m_macPrintSession = kPMNoReference;
78 }
79}
80
81void wxMacCarbonPrintData::ValidateOrCreate()
82{
83 OSStatus err = noErr ;
84 if ( m_macPrintSession == kPMNoReference )
85 {
86 err = PMCreateSession( (PMPrintSession *) &m_macPrintSession ) ;
87 }
88 // Set up a valid PageFormat object.
89 if ( m_macPageFormat == kPMNoPageFormat)
90 {
91 err = PMCreatePageFormat((PMPageFormat *) &m_macPageFormat);
92
93 // Note that PMPageFormat is not session-specific, but calling
94 // PMSessionDefaultPageFormat assigns values specific to the printer
95 // associated with the current printing session.
96 if ((err == noErr) &&
97 ( m_macPageFormat != kPMNoPageFormat))
98 {
99 err = PMSessionDefaultPageFormat((PMPrintSession) m_macPrintSession,
100 (PMPageFormat) m_macPageFormat);
101 }
102 }
103 else
104 {
105 err = PMSessionValidatePageFormat((PMPrintSession) m_macPrintSession,
106 (PMPageFormat) m_macPageFormat,
107 kPMDontWantBoolean);
108 }
109
110 // Set up a valid PrintSettings object.
111 if ( m_macPrintSettings == kPMNoPrintSettings)
112 {
113 err = PMCreatePrintSettings((PMPrintSettings *) &m_macPrintSettings);
114
115 // Note that PMPrintSettings is not session-specific, but calling
116 // PMSessionDefaultPrintSettings assigns values specific to the printer
117 // associated with the current printing session.
118 if ((err == noErr) &&
119 ( m_macPrintSettings != kPMNoPrintSettings))
120 {
121 err = PMSessionDefaultPrintSettings((PMPrintSession) m_macPrintSession,
122 (PMPrintSettings) m_macPrintSettings);
123 }
124 }
125 else
126 {
127 err = PMSessionValidatePrintSettings((PMPrintSession) m_macPrintSession,
128 (PMPrintSettings) m_macPrintSettings,
129 kPMDontWantBoolean);
130 }
131}
132
dc7ccb9c 133bool wxMacCarbonPrintData::TransferFrom( const wxPrintData &data )
746d7582
SC
134{
135 ValidateOrCreate() ;
dc7ccb9c
SC
136 PMSetCopies( (PMPrintSettings) m_macPrintSettings , data.GetNoCopies() , false ) ;
137 PMSetOrientation( (PMPageFormat) m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
746d7582
SC
138 kPMLandscape : kPMPortrait , false ) ;
139 // collate cannot be set
140#if 0 // not yet tested
141 if ( m_printerName.Length() > 0 )
a9412f8f 142 PMSessionSetCurrentPrinter( (PMPrintSession) m_macPrintSession , wxMacCFStringHolder( m_printerName , wxFont::GetDefaultEncoding() ) ) ;
746d7582
SC
143#endif
144 PMColorMode color ;
145 PMGetColorMode( (PMPrintSettings) m_macPrintSettings, &color ) ;
dc7ccb9c 146 if ( data.GetColour() )
746d7582
SC
147 {
148 if ( color == kPMBlackAndWhite )
149 PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMColor ) ;
150 }
151 else
152 PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMBlackAndWhite ) ;
153
154 // PMDuplexMode not yet accessible via API
155 // PMQualityMode not yet accessible via API
156 // todo paperSize
dc7ccb9c 157 return true ;
746d7582
SC
158}
159
dc7ccb9c 160bool wxMacCarbonPrintData::TransferTo( wxPrintData &data )
746d7582
SC
161{
162 OSStatus err = noErr ;
163
164 UInt32 copies ;
165 err = PMGetCopies( m_macPrintSettings , &copies ) ;
166 if ( err == noErr )
dc7ccb9c 167 data.SetNoCopies( copies ) ;
746d7582
SC
168
169 PMOrientation orientation ;
170 err = PMGetOrientation( m_macPageFormat , &orientation ) ;
171 if ( err == noErr )
172 {
173 if ( orientation == kPMPortrait || orientation == kPMReversePortrait )
dc7ccb9c 174 data.SetOrientation( wxPORTRAIT );
746d7582 175 else
dc7ccb9c 176 data.SetOrientation( wxLANDSCAPE );
746d7582
SC
177 }
178
179 // collate cannot be set
180#if 0
181 {
182 wxMacCFStringHolder name ;
183 PMPrinter printer ;
184 PMSessionGetCurrentPrinter( m_macPrintSession ,
185 &printer ) ;
186 m_printerName = name.AsString() ;
187 }
188#endif
189
190 PMColorMode color ;
191 err = PMGetColorMode( m_macPrintSettings, &color ) ;
192 if ( err == noErr )
dc7ccb9c 193 data.SetColour( !(color == kPMBlackAndWhite) ) ;
746d7582
SC
194
195 // PMDuplexMode not yet accessible via API
196 // PMQualityMode not yet accessible via API
197 // todo paperSize
198 PMRect rPaper;
199 err = PMGetUnadjustedPaperRect( m_macPageFormat, &rPaper);
200 if ( err == noErr )
201 {
dc7ccb9c 202 data.SetPaperSize( wxSize (
746d7582
SC
203 (int)(( rPaper.right - rPaper.left ) * pt2mm + 0.5 ) ,
204 (int)(( rPaper.bottom - rPaper.top ) * pt2mm + 0.5 ) ) );
205 }
dc7ccb9c 206 return true ;
746d7582
SC
207}
208
209void wxMacCarbonPrintData::TransferFrom( wxPageSetupData *data )
210{
211 // should we setup the page rect here ?
212 // since MacOS sometimes has two same paper rects with different
213 // page rects we could make it roundtrip safe perhaps
746d7582
SC
214}
215
216void wxMacCarbonPrintData::TransferTo( wxPageSetupData* data )
217{
218 PMRect rPaper;
219 OSStatus err = PMGetUnadjustedPaperRect(m_macPageFormat, &rPaper);
220 if ( err == noErr )
221 {
222 PMRect rPage ;
223 err = PMGetUnadjustedPageRect(m_macPageFormat , &rPage ) ;
224 if ( err == noErr )
225 {
226 data->SetMinMarginTopLeft( wxPoint (
227 (int)(((double) rPage.left - rPaper.left ) * pt2mm) ,
228 (int)(((double) rPage.top - rPaper.top ) * pt2mm) ) ) ;
229
230 data->SetMinMarginBottomRight( wxPoint (
5be55d56
VZ
231 (wxCoord)(((double) rPaper.right - rPage.right ) * pt2mm),
232 (wxCoord)(((double) rPaper.bottom - rPage.bottom ) * pt2mm)) ) ;
dc7ccb9c
SC
233
234 if ( data->GetMarginTopLeft().x < data->GetMinMarginTopLeft().x )
235 data->SetMarginTopLeft( wxPoint( data->GetMinMarginTopLeft().x ,
236 data->GetMarginTopLeft().y ) ) ;
237
238 if ( data->GetMarginBottomRight().x < data->GetMinMarginBottomRight().x )
239 data->SetMarginBottomRight( wxPoint( data->GetMinMarginBottomRight().x ,
240 data->GetMarginBottomRight().y ) );
241
242 if ( data->GetMarginTopLeft().y < data->GetMinMarginTopLeft().y )
243 data->SetMarginTopLeft( wxPoint( data->GetMarginTopLeft().x , data->GetMinMarginTopLeft().y ) );
244
245 if ( data->GetMarginBottomRight().y < data->GetMinMarginBottomRight().y )
246 data->SetMarginBottomRight( wxPoint( data->GetMarginBottomRight().x ,
247 data->GetMinMarginBottomRight().y) );
248
746d7582
SC
249 }
250 }
251}
252
253void wxMacCarbonPrintData::TransferTo( wxPrintDialogData* data )
254{
255 UInt32 minPage , maxPage ;
256 PMGetPageRange( m_macPrintSettings , &minPage , &maxPage ) ;
257 data->SetMinPage( minPage ) ;
258 data->SetMaxPage( maxPage ) ;
259 UInt32 copies ;
260 PMGetCopies( m_macPrintSettings , &copies ) ;
261 data->SetNoCopies( copies ) ;
262 UInt32 from , to ;
263 PMGetFirstPage( m_macPrintSettings , &from ) ;
264 PMGetLastPage( m_macPrintSettings , &to ) ;
34a4e912
SC
265 if ( to >= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
266 {
267 data->SetAllPages( true ) ;
268 // This means all pages, more or less
269 data->SetFromPage(1);
270 data->SetToPage(32000);
271 }
272 else
273 {
274 data->SetFromPage( from ) ;
275 data->SetToPage( to ) ;
276 data->SetAllPages( false );
277 }
746d7582
SC
278}
279
280void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData* data )
281{
282 PMSetPageRange( m_macPrintSettings , data->GetMinPage() , data->GetMaxPage() ) ;
283 PMSetCopies( m_macPrintSettings , data->GetNoCopies() , false ) ;
284 PMSetFirstPage( m_macPrintSettings , data->GetFromPage() , false ) ;
b386cd7a 285
34a4e912 286 if (data->GetAllPages() || data->GetFromPage() == 0)
35d1986e 287 {
34a4e912 288 PMSetLastPage( m_macPrintSettings , (UInt32) kPMPrintAllPages, true ) ;
35d1986e
SC
289 }
290 else
291 {
34a4e912 292 PMSetLastPage( m_macPrintSettings , (UInt32) data->GetToPage() , false ) ;
35d1986e 293 }
746d7582
SC
294}
295
72e7876b 296/*
e40298d5
JS
297* Printer
298*/
299
72e7876b 300wxMacPrinter::wxMacPrinter(wxPrintDialogData *data):
e40298d5 301wxPrinterBase(data)
72e7876b
SC
302{
303}
304
305wxMacPrinter::~wxMacPrinter(void)
306{
307}
308
309bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
310{
e40298d5
JS
311 sm_abortIt = FALSE;
312 sm_abortWindow = NULL;
72e7876b 313
e40298d5
JS
314 if (!printout)
315 return FALSE;
316
317 printout->SetIsPreview(FALSE);
d2b354f9
JS
318 if (m_printDialogData.GetMinPage() < 1)
319 m_printDialogData.SetMinPage(1);
320 if (m_printDialogData.GetMaxPage() < 1)
321 m_printDialogData.SetMaxPage(9999);
322
e40298d5
JS
323 // Create a suitable device context
324 wxDC *dc = NULL;
325 if (prompt)
326 {
2f1ae414
SC
327 wxPrintDialog dialog(parent, & m_printDialogData);
328 if (dialog.ShowModal() == wxID_OK)
e40298d5
JS
329 {
330 dc = dialog.GetPrintDC();
ffcd5195 331 m_printDialogData = dialog.GetPrintDialogData();
e40298d5
JS
332 }
333 }
334 else
72e7876b 335 {
e40298d5 336 dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ;
72e7876b 337 }
e40298d5
JS
338
339
340 // May have pressed cancel.
341 if (!dc || !dc->Ok())
72e7876b 342 {
e40298d5
JS
343 if (dc) delete dc;
344 return FALSE;
72e7876b 345 }
e40298d5
JS
346
347 // on the mac we have always pixels as addressing mode with 72 dpi
348
349 printout->SetPPIScreen(72, 72);
350 printout->SetPPIPrinter(72, 72);
351
352 // Set printout parameters
353 printout->SetDC(dc);
354
355 int w, h;
356 wxCoord ww, hh;
357 dc->GetSize(&w, &h);
358 printout->SetPageSizePixels((int)w, (int)h);
359 dc->GetSizeMM(&ww, &hh);
360 printout->SetPageSizeMM((int)ww, (int)hh);
361
362 // Create an abort window
363 wxBeginBusyCursor();
364
d2b354f9
JS
365 printout->OnPreparePrinting();
366
367 // Get some parameters from the printout, if defined
368 int fromPage, toPage;
369 int minPage, maxPage;
370 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
371
372 if (maxPage == 0)
373 {
374 wxEndBusyCursor();
375 return FALSE;
376 }
377
378 // Only set min and max, because from and to have been
379 // set by the user
380 m_printDialogData.SetMinPage(minPage);
381 m_printDialogData.SetMaxPage(maxPage);
382
e40298d5
JS
383 wxWindow *win = CreateAbortWindow(parent, printout);
384 wxSafeYield(win,true);
385
386 if (!win)
387 {
388 wxEndBusyCursor();
427ff662 389 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK, parent);
e40298d5
JS
390 delete dc;
391 return FALSE;
392 }
393 sm_abortWindow = win;
394 sm_abortWindow->Show(TRUE);
395 wxSafeYield(win,true);
396
397 printout->OnBeginPrinting();
398
399 bool keepGoing = TRUE;
400
401 int copyCount;
402 for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++)
403 {
404 if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
405 {
406 wxEndBusyCursor();
427ff662 407 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent);
e40298d5
JS
408 break;
409 }
410 if (sm_abortIt)
411 break;
412
413 int pn;
414 for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
415 pn++)
416 {
417 if (sm_abortIt)
418 {
419 keepGoing = FALSE;
420 break;
421 }
422 else
423 {
5c45e86b
SC
424#if TARGET_CARBON
425 if ( UMAGetSystemVersion() >= 0x1000 )
426#endif
427 {
428 GrafPtr thePort ;
429 GetPort( &thePort ) ;
430 wxSafeYield(win,true);
431 SetPort( thePort ) ;
432 }
e40298d5
JS
433 dc->StartPage();
434 keepGoing = printout->OnPrintPage(pn);
435 dc->EndPage();
436 }
437 }
438 printout->OnEndDocument();
439 }
440
441 printout->OnEndPrinting();
442
443 if (sm_abortWindow)
444 {
445 sm_abortWindow->Show(FALSE);
446 delete sm_abortWindow;
447 sm_abortWindow = NULL;
448 }
449
450 wxEndBusyCursor();
451
452 delete dc;
453
454 return TRUE;
72e7876b
SC
455}
456
457wxDC* wxMacPrinter::PrintDialog(wxWindow *parent)
458{
459 wxDC* dc = (wxDC*) NULL;
e40298d5 460
72e7876b
SC
461 wxPrintDialog dialog(parent, & m_printDialogData);
462 int ret = dialog.ShowModal();
e40298d5 463
72e7876b
SC
464 if (ret == wxID_OK)
465 {
466 dc = dialog.GetPrintDC();
467 m_printDialogData = dialog.GetPrintDialogData();
468 }
e40298d5 469
72e7876b
SC
470 return dc;
471}
472
473bool wxMacPrinter::Setup(wxWindow *parent)
474{
04a014a5 475#if 0
72e7876b
SC
476 wxPrintDialog dialog(parent, & m_printDialogData);
477 dialog.GetPrintDialogData().SetSetupDialog(TRUE);
e40298d5 478
72e7876b 479 int ret = dialog.ShowModal();
e40298d5 480
72e7876b
SC
481 if (ret == wxID_OK)
482 {
483 m_printDialogData = dialog.GetPrintDialogData();
484 }
e40298d5 485
72e7876b 486 return (ret == wxID_OK);
04a014a5
RR
487#endif
488 return wxID_CANCEL;
72e7876b
SC
489}
490
491/*
e40298d5
JS
492* Print preview
493*/
72e7876b
SC
494
495wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout,
e40298d5
JS
496 wxPrintout *printoutForPrinting,
497 wxPrintDialogData *data)
498 : wxPrintPreviewBase(printout, printoutForPrinting, data)
72e7876b
SC
499{
500 DetermineScaling();
501}
502
503wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data):
e40298d5 504wxPrintPreviewBase(printout, printoutForPrinting, data)
72e7876b 505{
e40298d5 506 DetermineScaling();
72e7876b
SC
507}
508
509wxMacPrintPreview::~wxMacPrintPreview(void)
510{
511}
512
513bool wxMacPrintPreview::Print(bool interactive)
514{
e40298d5
JS
515 if (!m_printPrintout)
516 return FALSE;
517 wxMacPrinter printer(&m_printDialogData);
518 return printer.Print(m_previewFrame, m_printPrintout, interactive);
72e7876b
SC
519}
520
521void wxMacPrintPreview::DetermineScaling(void)
522{
e40298d5
JS
523 int screenWidth , screenHeight ;
524 wxDisplaySize( &screenWidth , &screenHeight ) ;
525
526 m_previewPrintout->SetPPIScreen( 72 , 72 ) ;
527 m_previewPrintout->SetPPIPrinter( 72 , 72 ) ;
528 m_previewPrintout->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) );
529 m_previewPrintout->SetPageSizePixels( 8 * 72 , 11 * 72 ) ;
2f1ae414
SC
530 m_pageWidth = 8 * 72 ;
531 m_pageHeight = 11 * 72 ;
532 m_previewScale = 1 ;
e40298d5 533
72e7876b 534 // Get a device context for the currently selected printer
2f1ae414
SC
535 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
536 if (printerDC.Ok())
72e7876b 537 {
e40298d5
JS
538 int x , y ;
539 wxCoord ww, hh;
540 printerDC.GetSizeMM(&ww, &hh);
541 printerDC.GetSize( &x , &y ) ;
542 m_previewPrintout->SetPageSizeMM((int)ww, (int)hh);
543 m_previewPrintout->SetPageSizePixels( x , y) ;
544 m_pageWidth = x ;
545 m_pageHeight = y ;
546 m_isOk = true ;
547 }
548 else
549 {
550 m_isOk = false ;
551 }
72e7876b 552 // At 100%, the page should look about page-size on the screen.
2f1ae414
SC
553 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
554 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);
555
34a4e912 556 m_previewScale = 1 ;
72e7876b 557}
179e085f
RN
558
559#endif