]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/printmac.cpp
Avoid an assert if no alignment style is specified
[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"
72e7876b
SC
27#endif
28
d5301e33 29#include "wx/math.h"
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);
159
dc7ccb9c 160 return true ;
746d7582
SC
161}
162
dc7ccb9c 163bool wxMacCarbonPrintData::TransferTo( wxPrintData &data )
746d7582
SC
164{
165 OSStatus err = noErr ;
670f9935 166
746d7582
SC
167 UInt32 copies ;
168 err = PMGetCopies( m_macPrintSettings , &copies ) ;
169 if ( err == noErr )
670f9935
WS
170 data.SetNoCopies( copies ) ;
171
746d7582
SC
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
670f9935 192
746d7582
SC
193 PMColorMode color ;
194 err = PMGetColorMode( m_macPrintSettings, &color ) ;
195 if ( err == noErr )
dc7ccb9c 196 data.SetColour( !(color == kPMBlackAndWhite) ) ;
670f9935 197
746d7582
SC
198 // PMDuplexMode not yet accessible via API
199 // PMQualityMode not yet accessible via API
200 // todo paperSize
670f9935 201 PMRect rPaper;
746d7582
SC
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{
1aa7b427 221 PMRect rPaper;
746d7582
SC
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 (
670f9935
WS
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) );
670f9935
WS
251 }
252 }
746d7582
SC
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 ) ;
34a4e912
SC
267 if ( to >= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages
268 {
269 data->SetAllPages( true ) ;
270 // This means all pages, more or less
271 data->SetFromPage(1);
272 data->SetToPage(32000);
273 }
274 else
275 {
276 data->SetFromPage( from ) ;
277 data->SetToPage( to ) ;
278 data->SetAllPages( false );
279 }
746d7582
SC
280}
281
282void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData* data )
283{
284 PMSetPageRange( m_macPrintSettings , data->GetMinPage() , data->GetMaxPage() ) ;
285 PMSetCopies( m_macPrintSettings , data->GetNoCopies() , false ) ;
286 PMSetFirstPage( m_macPrintSettings , data->GetFromPage() , false ) ;
b386cd7a 287
34a4e912 288 if (data->GetAllPages() || data->GetFromPage() == 0)
1aa7b427
DS
289 PMSetLastPage( m_macPrintSettings , (UInt32) kPMPrintAllPages, true ) ;
290 else
291 PMSetLastPage( m_macPrintSettings , (UInt32) data->GetToPage() , false ) ;
746d7582
SC
292}
293
72e7876b 294/*
e40298d5
JS
295* Printer
296*/
297
72e7876b 298wxMacPrinter::wxMacPrinter(wxPrintDialogData *data):
e40298d5 299wxPrinterBase(data)
72e7876b
SC
300{
301}
302
303wxMacPrinter::~wxMacPrinter(void)
304{
305}
306
307bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
308{
1aa7b427 309 sm_abortIt = false;
e40298d5 310 sm_abortWindow = NULL;
1aa7b427 311
e40298d5 312 if (!printout)
1aa7b427
DS
313 return false;
314
315 printout->SetIsPreview(false);
d2b354f9
JS
316 if (m_printDialogData.GetMinPage() < 1)
317 m_printDialogData.SetMinPage(1);
318 if (m_printDialogData.GetMaxPage() < 1)
319 m_printDialogData.SetMaxPage(9999);
320
670f9935 321 // Create a suitable device context
e40298d5
JS
322 wxDC *dc = NULL;
323 if (prompt)
324 {
2f1ae414
SC
325 wxPrintDialog dialog(parent, & m_printDialogData);
326 if (dialog.ShowModal() == wxID_OK)
e40298d5
JS
327 {
328 dc = dialog.GetPrintDC();
ffcd5195 329 m_printDialogData = dialog.GetPrintDialogData();
e40298d5
JS
330 }
331 }
332 else
72e7876b 333 {
e40298d5 334 dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ;
72e7876b 335 }
1aa7b427 336
e40298d5
JS
337 // May have pressed cancel.
338 if (!dc || !dc->Ok())
72e7876b 339 {
1aa7b427
DS
340 if (dc)
341 delete dc;
342 return false;
72e7876b 343 }
1aa7b427 344
e40298d5 345 // on the mac we have always pixels as addressing mode with 72 dpi
e40298d5 346 printout->SetPPIScreen(72, 72);
8b285841
SC
347 PMResolution res;
348 wxMacCarbonPrintData* nativeData = (wxMacCarbonPrintData*)
349 (m_printDialogData.GetPrintData().GetNativeData());
350 PMGetResolution((PMPageFormat) (nativeData->m_macPageFormat), &res);
351 printout->SetPPIPrinter(int(res.hRes), int(res.vRes));
1aa7b427
DS
352
353 // Set printout parameters
e40298d5 354 printout->SetDC(dc);
1aa7b427 355
e40298d5
JS
356 int w, h;
357 wxCoord ww, hh;
358 dc->GetSize(&w, &h);
359 printout->SetPageSizePixels((int)w, (int)h);
360 dc->GetSizeMM(&ww, &hh);
361 printout->SetPageSizeMM((int)ww, (int)hh);
1aa7b427 362
e40298d5
JS
363 // Create an abort window
364 wxBeginBusyCursor();
1aa7b427 365
d2b354f9 366 printout->OnPreparePrinting();
1aa7b427 367
d2b354f9
JS
368 // Get some parameters from the printout, if defined
369 int fromPage, toPage;
370 int minPage, maxPage;
371 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
1aa7b427 372
d2b354f9
JS
373 if (maxPage == 0)
374 {
375 wxEndBusyCursor();
1aa7b427 376 return false;
d2b354f9 377 }
1aa7b427 378
d2b354f9
JS
379 // Only set min and max, because from and to have been
380 // set by the user
381 m_printDialogData.SetMinPage(minPage);
382 m_printDialogData.SetMaxPage(maxPage);
1aa7b427 383
e40298d5
JS
384 wxWindow *win = CreateAbortWindow(parent, printout);
385 wxSafeYield(win,true);
1aa7b427 386
e40298d5
JS
387 if (!win)
388 {
389 wxEndBusyCursor();
427ff662 390 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK, parent);
e40298d5 391 delete dc;
1aa7b427
DS
392
393 return false;
e40298d5 394 }
1aa7b427 395
e40298d5 396 sm_abortWindow = win;
1aa7b427 397 sm_abortWindow->Show(true);
e40298d5 398 wxSafeYield(win,true);
1aa7b427 399
e40298d5 400 printout->OnBeginPrinting();
1aa7b427
DS
401
402 bool keepGoing = true;
403
e40298d5
JS
404 int copyCount;
405 for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++)
406 {
407 if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
408 {
409 wxEndBusyCursor();
427ff662 410 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent);
e40298d5
JS
411 break;
412 }
413 if (sm_abortIt)
414 break;
1aa7b427 415
e40298d5 416 int pn;
670f9935 417 for (pn = m_printDialogData.GetFromPage();
1aa7b427 418 keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
e40298d5
JS
419 pn++)
420 {
421 if (sm_abortIt)
422 {
1aa7b427 423 keepGoing = false;
e40298d5
JS
424 break;
425 }
426 else
427 {
5c45e86b
SC
428#if TARGET_CARBON
429 if ( UMAGetSystemVersion() >= 0x1000 )
430#endif
431 {
432 GrafPtr thePort ;
433 GetPort( &thePort ) ;
434 wxSafeYield(win,true);
435 SetPort( thePort ) ;
436 }
e40298d5
JS
437 dc->StartPage();
438 keepGoing = printout->OnPrintPage(pn);
439 dc->EndPage();
440 }
441 }
442 printout->OnEndDocument();
443 }
670f9935 444
e40298d5 445 printout->OnEndPrinting();
670f9935 446
e40298d5
JS
447 if (sm_abortWindow)
448 {
1aa7b427 449 sm_abortWindow->Show(false);
e40298d5
JS
450 delete sm_abortWindow;
451 sm_abortWindow = NULL;
452 }
670f9935 453
e40298d5 454 wxEndBusyCursor();
670f9935 455
e40298d5 456 delete dc;
670f9935 457
1aa7b427 458 return true;
72e7876b
SC
459}
460
461wxDC* wxMacPrinter::PrintDialog(wxWindow *parent)
462{
463 wxDC* dc = (wxDC*) NULL;
670f9935 464
72e7876b
SC
465 wxPrintDialog dialog(parent, & m_printDialogData);
466 int ret = dialog.ShowModal();
670f9935 467
72e7876b
SC
468 if (ret == wxID_OK)
469 {
470 dc = dialog.GetPrintDC();
471 m_printDialogData = dialog.GetPrintDialogData();
472 }
670f9935 473
72e7876b
SC
474 return dc;
475}
476
477bool wxMacPrinter::Setup(wxWindow *parent)
478{
04a014a5 479#if 0
72e7876b 480 wxPrintDialog dialog(parent, & m_printDialogData);
1aa7b427 481 dialog.GetPrintDialogData().SetSetupDialog(true);
670f9935 482
72e7876b 483 int ret = dialog.ShowModal();
670f9935 484
72e7876b 485 if (ret == wxID_OK)
72e7876b 486 m_printDialogData = dialog.GetPrintDialogData();
670f9935 487
72e7876b 488 return (ret == wxID_OK);
04a014a5 489#endif
1aa7b427 490
04a014a5 491 return wxID_CANCEL;
72e7876b
SC
492}
493
494/*
e40298d5
JS
495* Print preview
496*/
72e7876b
SC
497
498wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout,
e40298d5
JS
499 wxPrintout *printoutForPrinting,
500 wxPrintDialogData *data)
501 : wxPrintPreviewBase(printout, printoutForPrinting, data)
72e7876b
SC
502{
503 DetermineScaling();
504}
505
506wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data):
e40298d5 507wxPrintPreviewBase(printout, printoutForPrinting, data)
72e7876b 508{
e40298d5 509 DetermineScaling();
72e7876b
SC
510}
511
512wxMacPrintPreview::~wxMacPrintPreview(void)
513{
514}
515
516bool wxMacPrintPreview::Print(bool interactive)
517{
e40298d5 518 if (!m_printPrintout)
1aa7b427
DS
519 return false;
520
e40298d5
JS
521 wxMacPrinter printer(&m_printDialogData);
522 return printer.Print(m_previewFrame, m_printPrintout, interactive);
72e7876b
SC
523}
524
525void wxMacPrintPreview::DetermineScaling(void)
526{
e40298d5
JS
527 int screenWidth , screenHeight ;
528 wxDisplaySize( &screenWidth , &screenHeight ) ;
670f9935 529
e40298d5
JS
530 m_previewPrintout->SetPPIScreen( 72 , 72 ) ;
531 m_previewPrintout->SetPPIPrinter( 72 , 72 ) ;
532 m_previewPrintout->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) );
533 m_previewPrintout->SetPageSizePixels( 8 * 72 , 11 * 72 ) ;
2f1ae414
SC
534 m_pageWidth = 8 * 72 ;
535 m_pageHeight = 11 * 72 ;
536 m_previewScale = 1 ;
670f9935 537
72e7876b 538 // Get a device context for the currently selected printer
2f1ae414
SC
539 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
540 if (printerDC.Ok())
72e7876b 541 {
e40298d5
JS
542 int x , y ;
543 wxCoord ww, hh;
544 printerDC.GetSizeMM(&ww, &hh);
545 printerDC.GetSize( &x , &y ) ;
546 m_previewPrintout->SetPageSizeMM((int)ww, (int)hh);
547 m_previewPrintout->SetPageSizePixels( x , y) ;
548 m_pageWidth = x ;
549 m_pageHeight = y ;
550 m_isOk = true ;
551 }
552 else
553 {
554 m_isOk = false ;
555 }
1aa7b427 556
72e7876b 557 // At 100%, the page should look about page-size on the screen.
2f1ae414
SC
558 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
559 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);
1aa7b427 560
670f9935 561 m_previewScale = 1 ;
72e7876b 562}
179e085f
RN
563
564#endif