]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/printmac.cpp
remove sizing controls when being in fullscreen mode
[wxWidgets.git] / src / mac / carbon / printmac.cpp
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$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_PRINTING_ARCHITECTURE
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
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
28 #include "wx/math.h"
29 #include "wx/mac/uma.h"
30
31 #include "wx/mac/printmac.h"
32 #include "wx/mac/private/print.h"
33
34 #include "wx/dcprint.h"
35 #include "wx/printdlg.h"
36 #include "wx/mac/printdlg.h"
37
38 #include <stdlib.h>
39
40 IMPLEMENT_DYNAMIC_CLASS(wxMacCarbonPrintData, wxPrintNativeDataBase)
41 IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase)
42 IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase)
43
44 bool wxMacCarbonPrintData::Ok() const
45 {
46 return (m_macPageFormat != kPMNoPageFormat) && (m_macPrintSettings != kPMNoPrintSettings) && (m_macPrintSession != kPMNoReference);
47 }
48 wxMacCarbonPrintData::wxMacCarbonPrintData()
49 {
50 m_macPageFormat = kPMNoPageFormat;
51 m_macPrintSettings = kPMNoPrintSettings;
52 m_macPrintSession = kPMNoReference ;
53 ValidateOrCreate() ;
54 }
55
56 wxMacCarbonPrintData::~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
77 void 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
129 bool wxMacCarbonPrintData::TransferFrom( const wxPrintData &data )
130 {
131 ValidateOrCreate() ;
132 PMSetCopies( (PMPrintSettings) m_macPrintSettings , data.GetNoCopies() , false ) ;
133 PMSetOrientation( (PMPageFormat) m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
134 kPMLandscape : kPMPortrait , false ) ;
135 // collate cannot be set
136 #if 0 // not yet tested
137 if ( m_printerName.Length() > 0 )
138 PMSessionSetCurrentPrinter( (PMPrintSession) m_macPrintSession , wxMacCFStringHolder( m_printerName , wxFont::GetDefaultEncoding() ) ) ;
139 #endif
140 PMColorMode color ;
141 PMGetColorMode( (PMPrintSettings) m_macPrintSettings, &color ) ;
142 if ( data.GetColour() )
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
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
160 return true ;
161 }
162
163 bool wxMacCarbonPrintData::TransferTo( wxPrintData &data )
164 {
165 OSStatus err = noErr ;
166
167 UInt32 copies ;
168 err = PMGetCopies( m_macPrintSettings , &copies ) ;
169 if ( err == noErr )
170 data.SetNoCopies( copies ) ;
171
172 PMOrientation orientation ;
173 err = PMGetOrientation( m_macPageFormat , &orientation ) ;
174 if ( err == noErr )
175 {
176 if ( orientation == kPMPortrait || orientation == kPMReversePortrait )
177 data.SetOrientation( wxPORTRAIT );
178 else
179 data.SetOrientation( wxLANDSCAPE );
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 )
196 data.SetColour( !(color == kPMBlackAndWhite) ) ;
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 {
205 data.SetPaperSize( wxSize (
206 (int)(( rPaper.right - rPaper.left ) * pt2mm + 0.5 ) ,
207 (int)(( rPaper.bottom - rPaper.top ) * pt2mm + 0.5 ) ) );
208 }
209 return true ;
210 }
211
212 void 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
217 }
218
219 void 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 (
234 (wxCoord)(((double) rPaper.right - rPage.right ) * pt2mm),
235 (wxCoord)(((double) rPaper.bottom - rPage.bottom ) * pt2mm)) ) ;
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 }
252 }
253 }
254
255 void 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 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 }
280 }
281
282 void 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 ) ;
287
288 if (data->GetAllPages() || data->GetFromPage() == 0)
289 PMSetLastPage( m_macPrintSettings , (UInt32) kPMPrintAllPages, true ) ;
290 else
291 PMSetLastPage( m_macPrintSettings , (UInt32) data->GetToPage() , false ) ;
292 }
293
294 /*
295 * Printer
296 */
297
298 wxMacPrinter::wxMacPrinter(wxPrintDialogData *data):
299 wxPrinterBase(data)
300 {
301 }
302
303 wxMacPrinter::~wxMacPrinter(void)
304 {
305 }
306
307 bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
308 {
309 sm_abortIt = false;
310 sm_abortWindow = NULL;
311
312 if (!printout)
313 return false;
314
315 printout->SetIsPreview(false);
316 if (m_printDialogData.GetMinPage() < 1)
317 m_printDialogData.SetMinPage(1);
318 if (m_printDialogData.GetMaxPage() < 1)
319 m_printDialogData.SetMaxPage(9999);
320
321 // Create a suitable device context
322 wxDC *dc = NULL;
323 if (prompt)
324 {
325 wxPrintDialog dialog(parent, & m_printDialogData);
326 if (dialog.ShowModal() == wxID_OK)
327 {
328 dc = dialog.GetPrintDC();
329 m_printDialogData = dialog.GetPrintDialogData();
330 }
331 }
332 else
333 {
334 dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ;
335 }
336
337 // May have pressed cancel.
338 if (!dc || !dc->Ok())
339 {
340 if (dc)
341 delete dc;
342 return false;
343 }
344
345 // on the mac we have always pixels as addressing mode with 72 dpi
346 printout->SetPPIScreen(72, 72);
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));
352
353 // Set printout parameters
354 printout->SetDC(dc);
355
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);
362
363 // Create an abort window
364 wxBeginBusyCursor();
365
366 printout->OnPreparePrinting();
367
368 // Get some parameters from the printout, if defined
369 int fromPage, toPage;
370 int minPage, maxPage;
371 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
372
373 if (maxPage == 0)
374 {
375 wxEndBusyCursor();
376 return false;
377 }
378
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);
383
384 wxWindow *win = CreateAbortWindow(parent, printout);
385 wxSafeYield(win,true);
386
387 if (!win)
388 {
389 wxEndBusyCursor();
390 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK, parent);
391 delete dc;
392
393 return false;
394 }
395
396 sm_abortWindow = win;
397 sm_abortWindow->Show(true);
398 wxSafeYield(win,true);
399
400 printout->OnBeginPrinting();
401
402 bool keepGoing = true;
403
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();
410 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent);
411 break;
412 }
413 if (sm_abortIt)
414 break;
415
416 int pn;
417 for (pn = m_printDialogData.GetFromPage();
418 keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
419 pn++)
420 {
421 if (sm_abortIt)
422 {
423 keepGoing = false;
424 break;
425 }
426 else
427 {
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 }
437 dc->StartPage();
438 keepGoing = printout->OnPrintPage(pn);
439 dc->EndPage();
440 }
441 }
442 printout->OnEndDocument();
443 }
444
445 printout->OnEndPrinting();
446
447 if (sm_abortWindow)
448 {
449 sm_abortWindow->Show(false);
450 delete sm_abortWindow;
451 sm_abortWindow = NULL;
452 }
453
454 wxEndBusyCursor();
455
456 delete dc;
457
458 return true;
459 }
460
461 wxDC* wxMacPrinter::PrintDialog(wxWindow *parent)
462 {
463 wxDC* dc = (wxDC*) NULL;
464
465 wxPrintDialog dialog(parent, & m_printDialogData);
466 int ret = dialog.ShowModal();
467
468 if (ret == wxID_OK)
469 {
470 dc = dialog.GetPrintDC();
471 m_printDialogData = dialog.GetPrintDialogData();
472 }
473
474 return dc;
475 }
476
477 bool wxMacPrinter::Setup(wxWindow *parent)
478 {
479 #if 0
480 wxPrintDialog dialog(parent, & m_printDialogData);
481 dialog.GetPrintDialogData().SetSetupDialog(true);
482
483 int ret = dialog.ShowModal();
484
485 if (ret == wxID_OK)
486 m_printDialogData = dialog.GetPrintDialogData();
487
488 return (ret == wxID_OK);
489 #endif
490
491 return wxID_CANCEL;
492 }
493
494 /*
495 * Print preview
496 */
497
498 wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout,
499 wxPrintout *printoutForPrinting,
500 wxPrintDialogData *data)
501 : wxPrintPreviewBase(printout, printoutForPrinting, data)
502 {
503 DetermineScaling();
504 }
505
506 wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data):
507 wxPrintPreviewBase(printout, printoutForPrinting, data)
508 {
509 DetermineScaling();
510 }
511
512 wxMacPrintPreview::~wxMacPrintPreview(void)
513 {
514 }
515
516 bool wxMacPrintPreview::Print(bool interactive)
517 {
518 if (!m_printPrintout)
519 return false;
520
521 wxMacPrinter printer(&m_printDialogData);
522 return printer.Print(m_previewFrame, m_printPrintout, interactive);
523 }
524
525 void wxMacPrintPreview::DetermineScaling(void)
526 {
527 int screenWidth , screenHeight ;
528 wxDisplaySize( &screenWidth , &screenHeight ) ;
529
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 ) ;
534 m_pageWidth = 8 * 72 ;
535 m_pageHeight = 11 * 72 ;
536 m_previewScale = 1 ;
537
538 // Get a device context for the currently selected printer
539 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
540 if (printerDC.Ok())
541 {
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 }
556
557 // At 100%, the page should look about page-size on the screen.
558 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
559 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);
560
561 m_previewScale = 1 ;
562 }
563
564 #endif