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