]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/printmac.cpp
mouse-capture fix
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "printwin.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #if wxUSE_PRINTING_ARCHITECTURE
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
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
32 #include "wx/math.h"
33 #include "wx/mac/uma.h"
34
35 #include "wx/mac/printmac.h"
36 #include "wx/mac/private/print.h"
37
38 #include "wx/dcprint.h"
39 #include "wx/printdlg.h"
40 #include "wx/mac/printdlg.h"
41
42 #include <stdlib.h>
43
44 IMPLEMENT_DYNAMIC_CLASS(wxMacCarbonPrintData, wxPrintNativeDataBase)
45 IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase)
46 IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase)
47
48 bool wxMacCarbonPrintData::Ok() const
49 {
50 return (m_macPageFormat != kPMNoPageFormat) && (m_macPrintSettings != kPMNoPrintSettings) && (m_macPrintSession != kPMNoReference);
51 }
52 wxMacCarbonPrintData::wxMacCarbonPrintData()
53 {
54 m_macPageFormat = kPMNoPageFormat;
55 m_macPrintSettings = kPMNoPrintSettings;
56 m_macPrintSession = kPMNoReference ;
57 ValidateOrCreate() ;
58 }
59
60 wxMacCarbonPrintData::~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
81 void 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
133 bool wxMacCarbonPrintData::TransferFrom( const wxPrintData &data )
134 {
135 ValidateOrCreate() ;
136 PMSetCopies( (PMPrintSettings) m_macPrintSettings , data.GetNoCopies() , false ) ;
137 PMSetOrientation( (PMPageFormat) m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
138 kPMLandscape : kPMPortrait , false ) ;
139 // collate cannot be set
140 #if 0 // not yet tested
141 if ( m_printerName.Length() > 0 )
142 PMSessionSetCurrentPrinter( (PMPrintSession) m_macPrintSession , wxMacCFStringHolder( m_printerName , wxFont::GetDefaultEncoding() ) ) ;
143 #endif
144 PMColorMode color ;
145 PMGetColorMode( (PMPrintSettings) m_macPrintSettings, &color ) ;
146 if ( data.GetColour() )
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
157 return true ;
158 }
159
160 bool wxMacCarbonPrintData::TransferTo( wxPrintData &data )
161 {
162 OSStatus err = noErr ;
163
164 UInt32 copies ;
165 err = PMGetCopies( m_macPrintSettings , &copies ) ;
166 if ( err == noErr )
167 data.SetNoCopies( copies ) ;
168
169 PMOrientation orientation ;
170 err = PMGetOrientation( m_macPageFormat , &orientation ) ;
171 if ( err == noErr )
172 {
173 if ( orientation == kPMPortrait || orientation == kPMReversePortrait )
174 data.SetOrientation( wxPORTRAIT );
175 else
176 data.SetOrientation( wxLANDSCAPE );
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 )
193 data.SetColour( !(color == kPMBlackAndWhite) ) ;
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 {
202 data.SetPaperSize( wxSize (
203 (int)(( rPaper.right - rPaper.left ) * pt2mm + 0.5 ) ,
204 (int)(( rPaper.bottom - rPaper.top ) * pt2mm + 0.5 ) ) );
205 }
206 return true ;
207 }
208
209 void 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
214 }
215
216 void 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 (
231 (wxCoord)(((double) rPaper.right - rPage.right ) * pt2mm),
232 (wxCoord)(((double) rPaper.bottom - rPage.bottom ) * pt2mm)) ) ;
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
249 }
250 }
251 }
252
253 void 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 ) ;
265 data->SetFromPage( from ) ;
266 data->SetToPage( to ) ;
267 }
268
269 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData* data )
270 {
271 PMSetPageRange( m_macPrintSettings , data->GetMinPage() , data->GetMaxPage() ) ;
272 PMSetCopies( m_macPrintSettings , data->GetNoCopies() , false ) ;
273 PMSetFirstPage( m_macPrintSettings , data->GetFromPage() , false ) ;
274
275 int toPage = data->GetToPage();
276 if (toPage < 1)
277 toPage = data->GetFromPage();
278 PMSetLastPage( m_macPrintSettings , toPage , false ) ;
279 }
280
281 /*
282 * Printer
283 */
284
285 wxMacPrinter::wxMacPrinter(wxPrintDialogData *data):
286 wxPrinterBase(data)
287 {
288 }
289
290 wxMacPrinter::~wxMacPrinter(void)
291 {
292 }
293
294 bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
295 {
296 sm_abortIt = FALSE;
297 sm_abortWindow = NULL;
298
299 if (!printout)
300 return FALSE;
301
302 printout->SetIsPreview(FALSE);
303 if (m_printDialogData.GetMinPage() < 1)
304 m_printDialogData.SetMinPage(1);
305 if (m_printDialogData.GetMaxPage() < 1)
306 m_printDialogData.SetMaxPage(9999);
307
308 // Create a suitable device context
309 wxDC *dc = NULL;
310 if (prompt)
311 {
312 wxPrintDialog dialog(parent, & m_printDialogData);
313 if (dialog.ShowModal() == wxID_OK)
314 {
315 dc = dialog.GetPrintDC();
316 m_printDialogData = dialog.GetPrintDialogData();
317 }
318 }
319 else
320 {
321 dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ;
322 }
323
324
325 // May have pressed cancel.
326 if (!dc || !dc->Ok())
327 {
328 if (dc) delete dc;
329 return FALSE;
330 }
331
332 // on the mac we have always pixels as addressing mode with 72 dpi
333
334 printout->SetPPIScreen(72, 72);
335 printout->SetPPIPrinter(72, 72);
336
337 // Set printout parameters
338 printout->SetDC(dc);
339
340 int w, h;
341 wxCoord ww, hh;
342 dc->GetSize(&w, &h);
343 printout->SetPageSizePixels((int)w, (int)h);
344 dc->GetSizeMM(&ww, &hh);
345 printout->SetPageSizeMM((int)ww, (int)hh);
346
347 // Create an abort window
348 wxBeginBusyCursor();
349
350 printout->OnPreparePrinting();
351
352 // Get some parameters from the printout, if defined
353 int fromPage, toPage;
354 int minPage, maxPage;
355 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
356
357 if (maxPage == 0)
358 {
359 wxEndBusyCursor();
360 return FALSE;
361 }
362
363 // Only set min and max, because from and to have been
364 // set by the user
365 m_printDialogData.SetMinPage(minPage);
366 m_printDialogData.SetMaxPage(maxPage);
367
368 wxWindow *win = CreateAbortWindow(parent, printout);
369 wxSafeYield(win,true);
370
371 if (!win)
372 {
373 wxEndBusyCursor();
374 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK, parent);
375 delete dc;
376 return FALSE;
377 }
378 sm_abortWindow = win;
379 sm_abortWindow->Show(TRUE);
380 wxSafeYield(win,true);
381
382 printout->OnBeginPrinting();
383
384 bool keepGoing = TRUE;
385
386 int copyCount;
387 for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++)
388 {
389 if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
390 {
391 wxEndBusyCursor();
392 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent);
393 break;
394 }
395 if (sm_abortIt)
396 break;
397
398 int pn;
399 for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
400 pn++)
401 {
402 if (sm_abortIt)
403 {
404 keepGoing = FALSE;
405 break;
406 }
407 else
408 {
409 #if TARGET_CARBON
410 if ( UMAGetSystemVersion() >= 0x1000 )
411 #endif
412 {
413 GrafPtr thePort ;
414 GetPort( &thePort ) ;
415 wxSafeYield(win,true);
416 SetPort( thePort ) ;
417 }
418 dc->StartPage();
419 keepGoing = printout->OnPrintPage(pn);
420 dc->EndPage();
421 }
422 }
423 printout->OnEndDocument();
424 }
425
426 printout->OnEndPrinting();
427
428 if (sm_abortWindow)
429 {
430 sm_abortWindow->Show(FALSE);
431 delete sm_abortWindow;
432 sm_abortWindow = NULL;
433 }
434
435 wxEndBusyCursor();
436
437 delete dc;
438
439 return TRUE;
440 }
441
442 wxDC* wxMacPrinter::PrintDialog(wxWindow *parent)
443 {
444 wxDC* dc = (wxDC*) NULL;
445
446 wxPrintDialog dialog(parent, & m_printDialogData);
447 int ret = dialog.ShowModal();
448
449 if (ret == wxID_OK)
450 {
451 dc = dialog.GetPrintDC();
452 m_printDialogData = dialog.GetPrintDialogData();
453 }
454
455 return dc;
456 }
457
458 bool wxMacPrinter::Setup(wxWindow *parent)
459 {
460 #if 0
461 wxPrintDialog dialog(parent, & m_printDialogData);
462 dialog.GetPrintDialogData().SetSetupDialog(TRUE);
463
464 int ret = dialog.ShowModal();
465
466 if (ret == wxID_OK)
467 {
468 m_printDialogData = dialog.GetPrintDialogData();
469 }
470
471 return (ret == wxID_OK);
472 #endif
473 return wxID_CANCEL;
474 }
475
476 /*
477 * Print preview
478 */
479
480 wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout,
481 wxPrintout *printoutForPrinting,
482 wxPrintDialogData *data)
483 : wxPrintPreviewBase(printout, printoutForPrinting, data)
484 {
485 DetermineScaling();
486 }
487
488 wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data):
489 wxPrintPreviewBase(printout, printoutForPrinting, data)
490 {
491 DetermineScaling();
492 }
493
494 wxMacPrintPreview::~wxMacPrintPreview(void)
495 {
496 }
497
498 bool wxMacPrintPreview::Print(bool interactive)
499 {
500 if (!m_printPrintout)
501 return FALSE;
502 wxMacPrinter printer(&m_printDialogData);
503 return printer.Print(m_previewFrame, m_printPrintout, interactive);
504 }
505
506 void wxMacPrintPreview::DetermineScaling(void)
507 {
508 int screenWidth , screenHeight ;
509 wxDisplaySize( &screenWidth , &screenHeight ) ;
510
511 m_previewPrintout->SetPPIScreen( 72 , 72 ) ;
512 m_previewPrintout->SetPPIPrinter( 72 , 72 ) ;
513 m_previewPrintout->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) );
514 m_previewPrintout->SetPageSizePixels( 8 * 72 , 11 * 72 ) ;
515 m_pageWidth = 8 * 72 ;
516 m_pageHeight = 11 * 72 ;
517 m_previewScale = 1 ;
518
519 // Get a device context for the currently selected printer
520 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
521 if (printerDC.Ok())
522 {
523 int x , y ;
524 wxCoord ww, hh;
525 printerDC.GetSizeMM(&ww, &hh);
526 printerDC.GetSize( &x , &y ) ;
527 m_previewPrintout->SetPageSizeMM((int)ww, (int)hh);
528 m_previewPrintout->SetPageSizePixels( x , y) ;
529 m_pageWidth = x ;
530 m_pageHeight = y ;
531 m_isOk = true ;
532 }
533 else
534 {
535 m_isOk = false ;
536 }
537 // At 100%, the page should look about page-size on the screen.
538 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
539 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);
540
541 m_previewScale = 1 ;
542 }
543
544 #endif