Commit | Line | Data |
---|---|---|
489468fe | 1 | ///////////////////////////////////////////////////////////////////////////// |
96dabe43 | 2 | // Name: src/osx/core/printmac.cpp |
489468fe | 3 | // Purpose: wxMacPrinter framework |
96dabe43 | 4 | // Author: Julian Smart, Stefan Csomor |
489468fe SC |
5 | // Modified by: |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
96dabe43 | 8 | // Copyright: (c) Julian Smart, Stefan Csomor |
489468fe SC |
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 | #include "wx/dcprint.h" | |
27 | #include "wx/math.h" | |
28 | #endif | |
29 | ||
524c47aa | 30 | #include "wx/osx/private.h" |
489468fe | 31 | |
1f0c8f31 SC |
32 | #include "wx/osx/printmac.h" |
33 | #include "wx/osx/private/print.h" | |
489468fe SC |
34 | |
35 | #include "wx/printdlg.h" | |
36 | #include "wx/paper.h" | |
1f0c8f31 | 37 | #include "wx/osx/printdlg.h" |
489468fe SC |
38 | |
39 | #include <stdlib.h> | |
40 | ||
c347101b SC |
41 | // |
42 | // move to print_osx.cpp | |
43 | // | |
489468fe | 44 | |
c347101b SC |
45 | IMPLEMENT_DYNAMIC_CLASS(wxOSXPrintData, wxPrintNativeDataBase) |
46 | ||
47 | bool wxOSXPrintData::IsOk() const | |
489468fe SC |
48 | { |
49 | return (m_macPageFormat != kPMNoPageFormat) && (m_macPrintSettings != kPMNoPrintSettings) && (m_macPrintSession != kPMNoReference); | |
50 | } | |
c347101b SC |
51 | |
52 | wxOSXPrintData::wxOSXPrintData() | |
489468fe SC |
53 | { |
54 | m_macPageFormat = kPMNoPageFormat; | |
55 | m_macPrintSettings = kPMNoPrintSettings; | |
56 | m_macPrintSession = kPMNoReference ; | |
c347101b | 57 | m_macPaper = kPMNoData; |
489468fe SC |
58 | } |
59 | ||
c347101b | 60 | wxOSXPrintData::~wxOSXPrintData() |
489468fe | 61 | { |
c347101b | 62 | } |
489468fe | 63 | |
c347101b SC |
64 | void wxOSXPrintData::UpdateFromPMState() |
65 | { | |
66 | } | |
489468fe | 67 | |
c347101b SC |
68 | void wxOSXPrintData::UpdateToPMState() |
69 | { | |
489468fe SC |
70 | } |
71 | ||
c347101b | 72 | bool wxOSXPrintData::TransferFrom( const wxPrintData &data ) |
489468fe | 73 | { |
c347101b SC |
74 | PMPrinter printer; |
75 | PMSessionGetCurrentPrinter(m_macPrintSession, &printer); | |
489468fe | 76 | |
c347101b SC |
77 | wxSize papersize = wxDefaultSize; |
78 | const wxPaperSize paperId = data.GetPaperId(); | |
79 | if ( paperId != wxPAPER_NONE && wxThePrintPaperDatabase ) | |
80 | { | |
81 | papersize = wxThePrintPaperDatabase->GetSize(paperId); | |
82 | if ( papersize != wxDefaultSize ) | |
489468fe | 83 | { |
c347101b SC |
84 | papersize.x /= 10; |
85 | papersize.y /= 10; | |
489468fe SC |
86 | } |
87 | } | |
88 | else | |
89 | { | |
c347101b | 90 | papersize = data.GetPaperSize(); |
489468fe | 91 | } |
c347101b SC |
92 | |
93 | if ( papersize != wxDefaultSize ) | |
489468fe | 94 | { |
c347101b SC |
95 | papersize.x = (wxInt32) (papersize.x * mm2pt); |
96 | papersize.y = (wxInt32) (papersize.y * mm2pt); | |
97 | ||
98 | double height, width; | |
99 | PMPaperGetHeight(m_macPaper, &height); | |
100 | PMPaperGetWidth(m_macPaper, &width); | |
101 | ||
102 | if ( fabs( width - papersize.x ) >= 5 || | |
103 | fabs( height - papersize.y ) >= 5 ) | |
489468fe | 104 | { |
c347101b SC |
105 | // we have to change the current paper |
106 | CFArrayRef paperlist = 0 ; | |
107 | if ( PMPrinterGetPaperList( printer, &paperlist ) == noErr ) | |
108 | { | |
109 | PMPaper bestPaper = kPMNoData ; | |
110 | CFIndex top = CFArrayGetCount(paperlist); | |
111 | for ( CFIndex i = 0 ; i < top ; ++ i ) | |
112 | { | |
113 | PMPaper paper = (PMPaper) CFArrayGetValueAtIndex( paperlist, i ); | |
114 | PMPaperGetHeight(paper, &height); | |
115 | PMPaperGetWidth(paper, &width); | |
116 | if ( fabs( width - papersize.x ) < 5 && | |
117 | fabs( height - papersize.y ) < 5 ) | |
118 | { | |
119 | // TODO test for duplicate hits and use additional | |
120 | // criteria for best match | |
121 | bestPaper = paper; | |
122 | } | |
123 | } | |
124 | PMPaper paper = kPMNoData; | |
125 | if ( bestPaper == kPMNoData ) | |
126 | { | |
127 | const PMPaperMargins margins = { 0.0, 0.0, 0.0, 0.0 }; | |
128 | wxString id, name(_T("Custom paper")); | |
129 | id.Printf(_T("wxPaperCustom%dx%d"), papersize.x, papersize.y); | |
130 | ||
131 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 | |
132 | if ( PMPaperCreateCustom != NULL) | |
133 | { | |
134 | PMPaperCreateCustom(printer, wxCFStringRef( id, wxFont::GetDefaultEncoding() ), wxCFStringRef( name, wxFont::GetDefaultEncoding() ), | |
135 | papersize.x, papersize.y, &margins, &paper); | |
136 | } | |
137 | #endif | |
138 | #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 | |
139 | if ( paper == kPMNoData ) | |
140 | { | |
141 | PMPaperCreate(printer, wxCFStringRef( id, wxFont::GetDefaultEncoding() ), wxCFStringRef( name, wxFont::GetDefaultEncoding() ), | |
142 | papersize.x, papersize.y, &margins, &paper); | |
143 | } | |
144 | #endif | |
145 | } | |
146 | if ( bestPaper != kPMNoData ) | |
147 | { | |
148 | PMPageFormat pageFormat; | |
149 | PMCreatePageFormatWithPMPaper(&pageFormat, bestPaper); | |
150 | PMCopyPageFormat( pageFormat, m_macPageFormat ); | |
151 | PMRelease(pageFormat); | |
152 | PMGetPageFormatPaper(m_macPageFormat, &m_macPaper); | |
153 | } | |
154 | PMRelease(paper); | |
155 | } | |
489468fe SC |
156 | } |
157 | } | |
489468fe | 158 | |
c0b65970 RR |
159 | CFArrayRef printerList; |
160 | CFIndex index, count; | |
c0b65970 RR |
161 | CFStringRef name; |
162 | ||
c0b65970 RR |
163 | if (PMServerCreatePrinterList(kPMServerLocal, &printerList) == noErr) |
164 | { | |
165 | count = CFArrayGetCount(printerList); | |
166 | for (index = 0; index < count; index++) | |
167 | { | |
168 | printer = (PMPrinter)CFArrayGetValueAtIndex(printerList, index); | |
169 | if ((data.GetPrinterName().empty()) && (PMPrinterIsDefault(printer))) | |
170 | break; | |
171 | else | |
172 | { | |
173 | name = PMPrinterGetName(printer); | |
174 | CFRetain(name); | |
175 | if (data.GetPrinterName() == wxCFStringRef(name).AsString()) | |
176 | break; | |
177 | } | |
178 | } | |
179 | if (index < count) | |
c347101b | 180 | PMSessionSetCurrentPMPrinter(m_macPrintSession, printer); |
c0b65970 RR |
181 | CFRelease(printerList); |
182 | } | |
183 | ||
184 | PMSetCopies( m_macPrintSettings , data.GetNoCopies() , false ) ; | |
185 | PMSetCollate(m_macPrintSettings, data.GetCollate()); | |
c347101b SC |
186 | if ( data.IsOrientationReversed() ) |
187 | PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ? | |
188 | kPMReverseLandscape : kPMReversePortrait , false ) ; | |
489468fe | 189 | else |
c347101b SC |
190 | PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ? |
191 | kPMLandscape : kPMPortrait , false ) ; | |
192 | ||
489468fe SC |
193 | PMDuplexMode mode = 0 ; |
194 | switch( data.GetDuplex() ) | |
195 | { | |
196 | case wxDUPLEX_HORIZONTAL : | |
197 | mode = kPMDuplexNoTumble ; | |
198 | break ; | |
199 | case wxDUPLEX_VERTICAL : | |
200 | mode = kPMDuplexTumble ; | |
201 | break ; | |
202 | case wxDUPLEX_SIMPLEX : | |
203 | default : | |
204 | mode = kPMDuplexNone ; | |
205 | break ; | |
206 | } | |
c347101b | 207 | PMSetDuplex( m_macPrintSettings, mode ) ; |
489468fe SC |
208 | |
209 | // PMQualityMode not yet accessible via API | |
c0b65970 | 210 | |
c0b65970 RR |
211 | |
212 | if ( data.IsOrientationReversed() ) | |
c347101b | 213 | PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ? |
c0b65970 RR |
214 | kPMReverseLandscape : kPMReversePortrait , false ) ; |
215 | else | |
c347101b | 216 | PMSetOrientation( m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ? |
c0b65970 RR |
217 | kPMLandscape : kPMPortrait , false ) ; |
218 | ||
c347101b SC |
219 | // PMQualityMode not accessible via API |
220 | // TODO: use our quality property to determine optimal resolution | |
221 | PMResolution res; | |
b2680ced SC |
222 | PMTag tag = kPMMaxSquareResolution; |
223 | PMPrinterGetPrinterResolution(printer, tag, &res); | |
c347101b SC |
224 | PMSetResolution( m_macPageFormat, &res); |
225 | ||
489468fe SC |
226 | // after setting the new resolution the format has to be updated, otherwise the page rect remains |
227 | // at the 'old' scaling | |
c347101b SC |
228 | |
229 | PMSessionValidatePageFormat(m_macPrintSession, | |
230 | m_macPageFormat, kPMDontWantBoolean); | |
231 | PMSessionValidatePrintSettings(m_macPrintSession, | |
232 | m_macPrintSettings, kPMDontWantBoolean); | |
233 | #if wxOSX_USE_COCOA | |
234 | UpdateFromPMState(); | |
235 | #endif | |
489468fe SC |
236 | |
237 | return true ; | |
238 | } | |
239 | ||
c347101b | 240 | bool wxOSXPrintData::TransferTo( wxPrintData &data ) |
489468fe SC |
241 | { |
242 | OSStatus err = noErr ; | |
c347101b SC |
243 | #if wxOSX_USE_COCOA |
244 | UpdateToPMState(); | |
245 | #endif | |
489468fe SC |
246 | UInt32 copies ; |
247 | err = PMGetCopies( m_macPrintSettings , &copies ) ; | |
248 | if ( err == noErr ) | |
249 | data.SetNoCopies( copies ) ; | |
250 | ||
251 | PMOrientation orientation ; | |
252 | err = PMGetOrientation( m_macPageFormat , &orientation ) ; | |
253 | if ( err == noErr ) | |
254 | { | |
255 | if ( orientation == kPMPortrait || orientation == kPMReversePortrait ) | |
256 | { | |
257 | data.SetOrientation( wxPORTRAIT ); | |
258 | data.SetOrientationReversed( orientation == kPMReversePortrait ); | |
259 | } | |
260 | else | |
261 | { | |
262 | data.SetOrientation( wxLANDSCAPE ); | |
263 | data.SetOrientationReversed( orientation == kPMReverseLandscape ); | |
264 | } | |
265 | } | |
266 | ||
c0b65970 RR |
267 | Boolean collate; |
268 | if (PMGetCollate(m_macPrintSettings, &collate) == noErr) | |
269 | data.SetCollate(collate); | |
270 | ||
271 | CFStringRef name; | |
272 | PMPrinter printer ; | |
273 | PMSessionGetCurrentPrinter( m_macPrintSession, &printer ); | |
274 | if (PMPrinterIsDefault(printer)) | |
275 | data.SetPrinterName(wxEmptyString); | |
276 | else | |
489468fe | 277 | { |
c0b65970 RR |
278 | name = PMPrinterGetName(printer); |
279 | CFRetain(name); | |
280 | data.SetPrinterName(wxCFStringRef(name).AsString()); | |
489468fe | 281 | } |
c0b65970 | 282 | |
489468fe | 283 | PMDuplexMode mode = 0 ; |
c347101b | 284 | PMGetDuplex( m_macPrintSettings, &mode ) ; |
489468fe SC |
285 | switch( mode ) |
286 | { | |
287 | case kPMDuplexNoTumble : | |
288 | data.SetDuplex(wxDUPLEX_HORIZONTAL); | |
289 | break ; | |
290 | case kPMDuplexTumble : | |
291 | data.SetDuplex(wxDUPLEX_VERTICAL); | |
292 | break ; | |
293 | case kPMDuplexNone : | |
294 | default : | |
295 | data.SetDuplex(wxDUPLEX_SIMPLEX); | |
296 | break ; | |
297 | } | |
298 | // PMQualityMode not yet accessible via API | |
299 | ||
c347101b SC |
300 | double height, width; |
301 | PMPaperGetHeight(m_macPaper, &height); | |
302 | PMPaperGetWidth(m_macPaper, &width); | |
303 | ||
304 | wxSize sz((int)(width * pt2mm + 0.5 ) , | |
305 | (int)(height * pt2mm + 0.5 )); | |
306 | data.SetPaperSize(sz); | |
307 | wxPaperSize id = wxThePrintPaperDatabase->GetSize(wxSize(sz.x* 10, sz.y * 10)); | |
308 | if (id != wxPAPER_NONE) | |
489468fe | 309 | { |
c0b65970 | 310 | data.SetPaperId(id); |
489468fe SC |
311 | } |
312 | return true ; | |
313 | } | |
314 | ||
c347101b | 315 | void wxOSXPrintData::TransferFrom( wxPageSetupData *WXUNUSED(data) ) |
489468fe SC |
316 | { |
317 | // should we setup the page rect here ? | |
318 | // since MacOS sometimes has two same paper rects with different | |
319 | // page rects we could make it roundtrip safe perhaps | |
320 | } | |
321 | ||
c347101b | 322 | void wxOSXPrintData::TransferTo( wxPageSetupData* data ) |
489468fe | 323 | { |
c347101b SC |
324 | #if wxOSX_USE_COCOA |
325 | UpdateToPMState(); | |
326 | #endif | |
489468fe SC |
327 | PMRect rPaper; |
328 | OSStatus err = PMGetUnadjustedPaperRect(m_macPageFormat, &rPaper); | |
329 | if ( err == noErr ) | |
330 | { | |
331 | wxSize sz((int)(( rPaper.right - rPaper.left ) * pt2mm + 0.5 ) , | |
332 | (int)(( rPaper.bottom - rPaper.top ) * pt2mm + 0.5 )); | |
333 | data->SetPaperSize(sz); | |
334 | ||
335 | PMRect rPage ; | |
336 | err = PMGetUnadjustedPageRect(m_macPageFormat , &rPage ) ; | |
337 | if ( err == noErr ) | |
338 | { | |
339 | data->SetMinMarginTopLeft( wxPoint ( | |
340 | (int)(((double) rPage.left - rPaper.left ) * pt2mm) , | |
341 | (int)(((double) rPage.top - rPaper.top ) * pt2mm) ) ) ; | |
342 | ||
343 | data->SetMinMarginBottomRight( wxPoint ( | |
344 | (wxCoord)(((double) rPaper.right - rPage.right ) * pt2mm), | |
345 | (wxCoord)(((double) rPaper.bottom - rPage.bottom ) * pt2mm)) ) ; | |
346 | ||
347 | if ( data->GetMarginTopLeft().x < data->GetMinMarginTopLeft().x ) | |
348 | data->SetMarginTopLeft( wxPoint( data->GetMinMarginTopLeft().x , | |
349 | data->GetMarginTopLeft().y ) ) ; | |
350 | ||
351 | if ( data->GetMarginBottomRight().x < data->GetMinMarginBottomRight().x ) | |
352 | data->SetMarginBottomRight( wxPoint( data->GetMinMarginBottomRight().x , | |
353 | data->GetMarginBottomRight().y ) ); | |
354 | ||
355 | if ( data->GetMarginTopLeft().y < data->GetMinMarginTopLeft().y ) | |
356 | data->SetMarginTopLeft( wxPoint( data->GetMarginTopLeft().x , data->GetMinMarginTopLeft().y ) ); | |
357 | ||
358 | if ( data->GetMarginBottomRight().y < data->GetMinMarginBottomRight().y ) | |
359 | data->SetMarginBottomRight( wxPoint( data->GetMarginBottomRight().x , | |
360 | data->GetMinMarginBottomRight().y) ); | |
361 | } | |
362 | } | |
363 | } | |
364 | ||
c347101b | 365 | void wxOSXPrintData::TransferTo( wxPrintDialogData* data ) |
489468fe | 366 | { |
c347101b SC |
367 | #if wxOSX_USE_COCOA |
368 | UpdateToPMState(); | |
369 | #endif | |
489468fe SC |
370 | UInt32 minPage , maxPage ; |
371 | PMGetPageRange( m_macPrintSettings , &minPage , &maxPage ) ; | |
372 | data->SetMinPage( minPage ) ; | |
373 | data->SetMaxPage( maxPage ) ; | |
374 | UInt32 copies ; | |
375 | PMGetCopies( m_macPrintSettings , &copies ) ; | |
376 | data->SetNoCopies( copies ) ; | |
377 | UInt32 from , to ; | |
378 | PMGetFirstPage( m_macPrintSettings , &from ) ; | |
379 | PMGetLastPage( m_macPrintSettings , &to ) ; | |
380 | if ( to >= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages | |
381 | { | |
382 | data->SetAllPages( true ) ; | |
383 | // This means all pages, more or less | |
384 | data->SetFromPage(1); | |
c347101b | 385 | data->SetToPage(9999); |
489468fe SC |
386 | } |
387 | else | |
388 | { | |
389 | data->SetFromPage( from ) ; | |
390 | data->SetToPage( to ) ; | |
391 | data->SetAllPages( false ); | |
392 | } | |
393 | } | |
394 | ||
c347101b | 395 | void wxOSXPrintData::TransferFrom( wxPrintDialogData* data ) |
489468fe SC |
396 | { |
397 | // Respect the value of m_printAllPages | |
398 | if ( data->GetAllPages() ) | |
399 | PMSetPageRange( m_macPrintSettings , data->GetMinPage() , (UInt32) kPMPrintAllPages ) ; | |
400 | else | |
401 | PMSetPageRange( m_macPrintSettings , data->GetMinPage() , data->GetMaxPage() ) ; | |
402 | PMSetCopies( m_macPrintSettings , data->GetNoCopies() , false ) ; | |
403 | PMSetFirstPage( m_macPrintSettings , data->GetFromPage() , false ) ; | |
404 | ||
405 | if (data->GetAllPages() || data->GetFromPage() == 0) | |
406 | PMSetLastPage( m_macPrintSettings , (UInt32) kPMPrintAllPages, true ) ; | |
407 | else | |
408 | PMSetLastPage( m_macPrintSettings , (UInt32) data->GetToPage() , false ) ; | |
c347101b SC |
409 | #if wxOSX_USE_COCOA |
410 | UpdateFromPMState(); | |
411 | #endif | |
412 | } | |
413 | ||
414 | wxPrintNativeDataBase* wxOSXCreatePrintData() | |
415 | { | |
416 | #if wxOSX_USE_COCOA | |
417 | return new wxOSXCocoaPrintData(); | |
418 | #endif | |
419 | #if wxOSX_USE_CARBON | |
420 | return new wxOSXCarbonPrintData(); | |
421 | #endif | |
422 | return NULL; | |
489468fe SC |
423 | } |
424 | ||
425 | /* | |
426 | * Printer | |
427 | */ | |
428 | ||
c347101b SC |
429 | IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase) |
430 | ||
489468fe SC |
431 | wxMacPrinter::wxMacPrinter(wxPrintDialogData *data): |
432 | wxPrinterBase(data) | |
433 | { | |
434 | } | |
435 | ||
436 | wxMacPrinter::~wxMacPrinter(void) | |
437 | { | |
438 | } | |
439 | ||
440 | bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) | |
441 | { | |
442 | sm_abortIt = false; | |
443 | sm_abortWindow = NULL; | |
444 | ||
445 | if (!printout) | |
446 | return false; | |
447 | ||
448 | printout->SetIsPreview(false); | |
449 | if (m_printDialogData.GetMinPage() < 1) | |
450 | m_printDialogData.SetMinPage(1); | |
451 | if (m_printDialogData.GetMaxPage() < 1) | |
452 | m_printDialogData.SetMaxPage(9999); | |
453 | ||
454 | // Create a suitable device context | |
455 | wxPrinterDC *dc = NULL; | |
456 | if (prompt) | |
457 | { | |
458 | wxMacPrintDialog dialog(parent, & m_printDialogData); | |
459 | if (dialog.ShowModal() == wxID_OK) | |
460 | { | |
461 | dc = wxDynamicCast(dialog.GetPrintDC(), wxPrinterDC); | |
462 | wxASSERT(dc); | |
463 | m_printDialogData = dialog.GetPrintDialogData(); | |
464 | } | |
465 | } | |
466 | else | |
467 | { | |
468 | dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ; | |
469 | } | |
470 | ||
471 | // May have pressed cancel. | |
472 | if (!dc || !dc->IsOk()) | |
473 | { | |
0b1ca117 | 474 | delete dc; |
489468fe SC |
475 | return false; |
476 | } | |
477 | ||
478 | // on the mac we have always pixels as addressing mode with 72 dpi | |
479 | printout->SetPPIScreen(72, 72); | |
480 | PMResolution res; | |
c347101b | 481 | wxOSXPrintData* nativeData = (wxOSXPrintData*) |
489468fe | 482 | (m_printDialogData.GetPrintData().GetNativeData()); |
c347101b | 483 | PMGetResolution( (nativeData->GetPageFormat()), &res); |
489468fe SC |
484 | printout->SetPPIPrinter(int(res.hRes), int(res.vRes)); |
485 | ||
486 | // Set printout parameters | |
487 | printout->SetDC(dc); | |
488 | ||
489 | int w, h; | |
490 | dc->GetSize(&w, &h); | |
491 | printout->SetPageSizePixels((int)w, (int)h); | |
492 | printout->SetPaperRectPixels(dc->GetPaperRect()); | |
493 | wxCoord mw, mh; | |
494 | dc->GetSizeMM(&mw, &mh); | |
495 | printout->SetPageSizeMM((int)mw, (int)mh); | |
496 | ||
497 | // Create an abort window | |
498 | wxBeginBusyCursor(); | |
499 | ||
500 | printout->OnPreparePrinting(); | |
501 | ||
502 | // Get some parameters from the printout, if defined | |
503 | int fromPage, toPage; | |
504 | int minPage, maxPage; | |
505 | printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage); | |
506 | ||
507 | if (maxPage == 0) | |
508 | { | |
509 | wxEndBusyCursor(); | |
510 | return false; | |
511 | } | |
512 | ||
513 | // Only set min and max, because from and to have been | |
514 | // set by the user | |
515 | m_printDialogData.SetMinPage(minPage); | |
516 | m_printDialogData.SetMaxPage(maxPage); | |
517 | ||
518 | printout->OnBeginPrinting(); | |
519 | ||
520 | bool keepGoing = true; | |
521 | ||
522 | if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage())) | |
523 | { | |
524 | wxEndBusyCursor(); | |
525 | wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent); | |
526 | } | |
527 | ||
528 | int pn; | |
529 | for (pn = m_printDialogData.GetFromPage(); | |
530 | keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn); | |
531 | pn++) | |
532 | { | |
533 | if (sm_abortIt) | |
534 | { | |
535 | keepGoing = false; | |
536 | break; | |
537 | } | |
538 | else | |
539 | { | |
540 | dc->StartPage(); | |
541 | keepGoing = printout->OnPrintPage(pn); | |
542 | dc->EndPage(); | |
543 | } | |
544 | } | |
545 | printout->OnEndDocument(); | |
546 | ||
547 | printout->OnEndPrinting(); | |
548 | ||
549 | if (sm_abortWindow) | |
550 | { | |
551 | sm_abortWindow->Show(false); | |
552 | delete sm_abortWindow; | |
553 | sm_abortWindow = NULL; | |
554 | } | |
555 | ||
556 | wxEndBusyCursor(); | |
557 | ||
558 | delete dc; | |
559 | ||
560 | return true; | |
561 | } | |
562 | ||
563 | wxDC* wxMacPrinter::PrintDialog(wxWindow *parent) | |
564 | { | |
d3b9f782 | 565 | wxDC* dc = NULL; |
489468fe SC |
566 | |
567 | wxPrintDialog dialog(parent, & m_printDialogData); | |
568 | int ret = dialog.ShowModal(); | |
569 | ||
570 | if (ret == wxID_OK) | |
571 | { | |
572 | dc = dialog.GetPrintDC(); | |
573 | m_printDialogData = dialog.GetPrintDialogData(); | |
574 | } | |
575 | ||
576 | return dc; | |
577 | } | |
578 | ||
579 | bool wxMacPrinter::Setup(wxWindow *WXUNUSED(parent)) | |
580 | { | |
581 | #if 0 | |
582 | wxPrintDialog dialog(parent, & m_printDialogData); | |
583 | dialog.GetPrintDialogData().SetSetupDialog(true); | |
584 | ||
585 | int ret = dialog.ShowModal(); | |
586 | ||
587 | if (ret == wxID_OK) | |
588 | m_printDialogData = dialog.GetPrintDialogData(); | |
589 | ||
590 | return (ret == wxID_OK); | |
591 | #endif | |
592 | ||
593 | return wxID_CANCEL; | |
594 | } | |
595 | ||
596 | /* | |
597 | * Print preview | |
598 | */ | |
599 | ||
c347101b SC |
600 | IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase) |
601 | ||
489468fe SC |
602 | wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, |
603 | wxPrintout *printoutForPrinting, | |
604 | wxPrintDialogData *data) | |
605 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
606 | { | |
607 | DetermineScaling(); | |
608 | } | |
609 | ||
610 | wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data): | |
611 | wxPrintPreviewBase(printout, printoutForPrinting, data) | |
612 | { | |
613 | DetermineScaling(); | |
614 | } | |
615 | ||
616 | wxMacPrintPreview::~wxMacPrintPreview(void) | |
617 | { | |
618 | } | |
619 | ||
620 | bool wxMacPrintPreview::Print(bool interactive) | |
621 | { | |
622 | if (!m_printPrintout) | |
623 | return false; | |
624 | ||
625 | wxMacPrinter printer(&m_printDialogData); | |
626 | return printer.Print(m_previewFrame, m_printPrintout, interactive); | |
627 | } | |
628 | ||
629 | void wxMacPrintPreview::DetermineScaling(void) | |
630 | { | |
631 | int screenWidth , screenHeight ; | |
632 | wxDisplaySize( &screenWidth , &screenHeight ) ; | |
633 | ||
634 | wxSize ppiScreen( 72 , 72 ) ; | |
635 | wxSize ppiPrinter( 72 , 72 ) ; | |
636 | ||
637 | // Note that with Leopard, screen dpi=72 is no longer a given | |
638 | m_previewPrintout->SetPPIScreen( ppiScreen.x , ppiScreen.y ) ; | |
639 | ||
640 | wxCoord w , h ; | |
641 | wxCoord ww, hh; | |
642 | wxRect paperRect; | |
643 | ||
644 | // Get a device context for the currently selected printer | |
645 | wxPrinterDC printerDC(m_printDialogData.GetPrintData()); | |
646 | if (printerDC.IsOk()) | |
647 | { | |
648 | printerDC.GetSizeMM(&ww, &hh); | |
649 | printerDC.GetSize( &w , &h ) ; | |
650 | ppiPrinter = printerDC.GetPPI() ; | |
651 | paperRect = printerDC.GetPaperRect(); | |
652 | m_isOk = true ; | |
653 | } | |
654 | else | |
655 | { | |
656 | // use some defaults | |
657 | w = 8 * 72 ; | |
658 | h = 11 * 72 ; | |
659 | ww = (wxCoord) (w * 25.4 / ppiPrinter.x) ; | |
660 | hh = (wxCoord) (h * 25.4 / ppiPrinter.y) ; | |
661 | paperRect = wxRect(0, 0, w, h); | |
662 | m_isOk = false ; | |
663 | } | |
664 | m_pageWidth = w; | |
665 | m_pageHeight = h; | |
666 | ||
667 | m_previewPrintout->SetPageSizePixels(w , h) ; | |
668 | m_previewPrintout->SetPageSizeMM(ww, hh); | |
669 | m_previewPrintout->SetPaperRectPixels(paperRect); | |
670 | m_previewPrintout->SetPPIPrinter( ppiPrinter.x , ppiPrinter.y ) ; | |
671 | ||
672 | m_previewScaleX = float(ppiScreen.x) / ppiPrinter.x; | |
673 | m_previewScaleY = float(ppiScreen.y) / ppiPrinter.y; | |
674 | } | |
675 | ||
c347101b SC |
676 | // |
677 | // end of print_osx.cpp | |
678 | // | |
679 | ||
680 | #if wxOSX_USE_CARBON | |
681 | ||
682 | IMPLEMENT_DYNAMIC_CLASS(wxOSXCarbonPrintData, wxOSXPrintData) | |
683 | ||
684 | wxOSXCarbonPrintData::wxOSXCarbonPrintData() | |
685 | { | |
686 | if ( PMCreateSession( &m_macPrintSession ) == noErr ) | |
687 | { | |
688 | if ( PMCreatePageFormat(&m_macPageFormat) == noErr ) | |
689 | { | |
690 | PMSessionDefaultPageFormat(m_macPrintSession, | |
691 | m_macPageFormat); | |
692 | PMGetPageFormatPaper(m_macPageFormat, &m_macPaper); | |
693 | } | |
694 | ||
695 | if ( PMCreatePrintSettings(&m_macPrintSettings) == noErr ) | |
696 | { | |
697 | PMSessionDefaultPrintSettings(m_macPrintSession, | |
698 | m_macPrintSettings); | |
699 | } | |
700 | } | |
701 | } | |
702 | ||
703 | wxOSXCarbonPrintData::~wxOSXCarbonPrintData() | |
704 | { | |
705 | (void)PMRelease(m_macPageFormat); | |
706 | (void)PMRelease(m_macPrintSettings); | |
707 | (void)PMRelease(m_macPrintSession); | |
708 | } | |
709 | #endif | |
710 | ||
489468fe | 711 | #endif |