]>
Commit | Line | Data |
---|---|---|
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 | return true ; | |
154 | } | |
155 | ||
156 | bool wxMacCarbonPrintData::TransferTo( wxPrintData &data ) | |
157 | { | |
158 | OSStatus err = noErr ; | |
159 | ||
160 | UInt32 copies ; | |
161 | err = PMGetCopies( m_macPrintSettings , &copies ) ; | |
162 | if ( err == noErr ) | |
163 | data.SetNoCopies( copies ) ; | |
164 | ||
165 | PMOrientation orientation ; | |
166 | err = PMGetOrientation( m_macPageFormat , &orientation ) ; | |
167 | if ( err == noErr ) | |
168 | { | |
169 | if ( orientation == kPMPortrait || orientation == kPMReversePortrait ) | |
170 | data.SetOrientation( wxPORTRAIT ); | |
171 | else | |
172 | data.SetOrientation( wxLANDSCAPE ); | |
173 | } | |
174 | ||
175 | // collate cannot be set | |
176 | #if 0 | |
177 | { | |
178 | wxMacCFStringHolder name ; | |
179 | PMPrinter printer ; | |
180 | PMSessionGetCurrentPrinter( m_macPrintSession , | |
181 | &printer ) ; | |
182 | m_printerName = name.AsString() ; | |
183 | } | |
184 | #endif | |
185 | ||
186 | PMColorMode color ; | |
187 | err = PMGetColorMode( m_macPrintSettings, &color ) ; | |
188 | if ( err == noErr ) | |
189 | data.SetColour( !(color == kPMBlackAndWhite) ) ; | |
190 | ||
191 | // PMDuplexMode not yet accessible via API | |
192 | // PMQualityMode not yet accessible via API | |
193 | // todo paperSize | |
194 | PMRect rPaper; | |
195 | err = PMGetUnadjustedPaperRect( m_macPageFormat, &rPaper); | |
196 | if ( err == noErr ) | |
197 | { | |
198 | data.SetPaperSize( wxSize ( | |
199 | (int)(( rPaper.right - rPaper.left ) * pt2mm + 0.5 ) , | |
200 | (int)(( rPaper.bottom - rPaper.top ) * pt2mm + 0.5 ) ) ); | |
201 | } | |
202 | return true ; | |
203 | } | |
204 | ||
205 | void wxMacCarbonPrintData::TransferFrom( wxPageSetupData *data ) | |
206 | { | |
207 | // should we setup the page rect here ? | |
208 | // since MacOS sometimes has two same paper rects with different | |
209 | // page rects we could make it roundtrip safe perhaps | |
210 | } | |
211 | ||
212 | void wxMacCarbonPrintData::TransferTo( wxPageSetupData* data ) | |
213 | { | |
214 | PMRect rPaper; | |
215 | OSStatus err = PMGetUnadjustedPaperRect(m_macPageFormat, &rPaper); | |
216 | if ( err == noErr ) | |
217 | { | |
218 | PMRect rPage ; | |
219 | err = PMGetUnadjustedPageRect(m_macPageFormat , &rPage ) ; | |
220 | if ( err == noErr ) | |
221 | { | |
222 | data->SetMinMarginTopLeft( wxPoint ( | |
223 | (int)(((double) rPage.left - rPaper.left ) * pt2mm) , | |
224 | (int)(((double) rPage.top - rPaper.top ) * pt2mm) ) ) ; | |
225 | ||
226 | data->SetMinMarginBottomRight( wxPoint ( | |
227 | (wxCoord)(((double) rPaper.right - rPage.right ) * pt2mm), | |
228 | (wxCoord)(((double) rPaper.bottom - rPage.bottom ) * pt2mm)) ) ; | |
229 | ||
230 | if ( data->GetMarginTopLeft().x < data->GetMinMarginTopLeft().x ) | |
231 | data->SetMarginTopLeft( wxPoint( data->GetMinMarginTopLeft().x , | |
232 | data->GetMarginTopLeft().y ) ) ; | |
233 | ||
234 | if ( data->GetMarginBottomRight().x < data->GetMinMarginBottomRight().x ) | |
235 | data->SetMarginBottomRight( wxPoint( data->GetMinMarginBottomRight().x , | |
236 | data->GetMarginBottomRight().y ) ); | |
237 | ||
238 | if ( data->GetMarginTopLeft().y < data->GetMinMarginTopLeft().y ) | |
239 | data->SetMarginTopLeft( wxPoint( data->GetMarginTopLeft().x , data->GetMinMarginTopLeft().y ) ); | |
240 | ||
241 | if ( data->GetMarginBottomRight().y < data->GetMinMarginBottomRight().y ) | |
242 | data->SetMarginBottomRight( wxPoint( data->GetMarginBottomRight().x , | |
243 | data->GetMinMarginBottomRight().y) ); | |
244 | ||
245 | } | |
246 | } | |
247 | } | |
248 | ||
249 | void wxMacCarbonPrintData::TransferTo( wxPrintDialogData* data ) | |
250 | { | |
251 | UInt32 minPage , maxPage ; | |
252 | PMGetPageRange( m_macPrintSettings , &minPage , &maxPage ) ; | |
253 | data->SetMinPage( minPage ) ; | |
254 | data->SetMaxPage( maxPage ) ; | |
255 | UInt32 copies ; | |
256 | PMGetCopies( m_macPrintSettings , &copies ) ; | |
257 | data->SetNoCopies( copies ) ; | |
258 | UInt32 from , to ; | |
259 | PMGetFirstPage( m_macPrintSettings , &from ) ; | |
260 | PMGetLastPage( m_macPrintSettings , &to ) ; | |
261 | if ( to >= 0x7FFFFFFF ) // due to an OS Bug we don't get back kPMPrintAllPages | |
262 | { | |
263 | data->SetAllPages( true ) ; | |
264 | // This means all pages, more or less | |
265 | data->SetFromPage(1); | |
266 | data->SetToPage(32000); | |
267 | } | |
268 | else | |
269 | { | |
270 | data->SetFromPage( from ) ; | |
271 | data->SetToPage( to ) ; | |
272 | data->SetAllPages( false ); | |
273 | } | |
274 | } | |
275 | ||
276 | void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData* data ) | |
277 | { | |
278 | PMSetPageRange( m_macPrintSettings , data->GetMinPage() , data->GetMaxPage() ) ; | |
279 | PMSetCopies( m_macPrintSettings , data->GetNoCopies() , false ) ; | |
280 | PMSetFirstPage( m_macPrintSettings , data->GetFromPage() , false ) ; | |
281 | ||
282 | if (data->GetAllPages() || data->GetFromPage() == 0) | |
283 | { | |
284 | PMSetLastPage( m_macPrintSettings , (UInt32) kPMPrintAllPages, true ) ; | |
285 | } | |
286 | else | |
287 | { | |
288 | PMSetLastPage( m_macPrintSettings , (UInt32) data->GetToPage() , false ) ; | |
289 | } | |
290 | } | |
291 | ||
292 | /* | |
293 | * Printer | |
294 | */ | |
295 | ||
296 | wxMacPrinter::wxMacPrinter(wxPrintDialogData *data): | |
297 | wxPrinterBase(data) | |
298 | { | |
299 | } | |
300 | ||
301 | wxMacPrinter::~wxMacPrinter(void) | |
302 | { | |
303 | } | |
304 | ||
305 | bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) | |
306 | { | |
307 | sm_abortIt = FALSE; | |
308 | sm_abortWindow = NULL; | |
309 | ||
310 | if (!printout) | |
311 | return FALSE; | |
312 | ||
313 | printout->SetIsPreview(FALSE); | |
314 | if (m_printDialogData.GetMinPage() < 1) | |
315 | m_printDialogData.SetMinPage(1); | |
316 | if (m_printDialogData.GetMaxPage() < 1) | |
317 | m_printDialogData.SetMaxPage(9999); | |
318 | ||
319 | // Create a suitable device context | |
320 | wxDC *dc = NULL; | |
321 | if (prompt) | |
322 | { | |
323 | wxPrintDialog dialog(parent, & m_printDialogData); | |
324 | if (dialog.ShowModal() == wxID_OK) | |
325 | { | |
326 | dc = dialog.GetPrintDC(); | |
327 | m_printDialogData = dialog.GetPrintDialogData(); | |
328 | } | |
329 | } | |
330 | else | |
331 | { | |
332 | dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ; | |
333 | } | |
334 | ||
335 | ||
336 | // May have pressed cancel. | |
337 | if (!dc || !dc->Ok()) | |
338 | { | |
339 | if (dc) delete dc; | |
340 | return FALSE; | |
341 | } | |
342 | ||
343 | // on the mac we have always pixels as addressing mode with 72 dpi | |
344 | ||
345 | printout->SetPPIScreen(72, 72); | |
346 | printout->SetPPIPrinter(72, 72); | |
347 | ||
348 | // Set printout parameters | |
349 | printout->SetDC(dc); | |
350 | ||
351 | int w, h; | |
352 | wxCoord ww, hh; | |
353 | dc->GetSize(&w, &h); | |
354 | printout->SetPageSizePixels((int)w, (int)h); | |
355 | dc->GetSizeMM(&ww, &hh); | |
356 | printout->SetPageSizeMM((int)ww, (int)hh); | |
357 | ||
358 | // Create an abort window | |
359 | wxBeginBusyCursor(); | |
360 | ||
361 | printout->OnPreparePrinting(); | |
362 | ||
363 | // Get some parameters from the printout, if defined | |
364 | int fromPage, toPage; | |
365 | int minPage, maxPage; | |
366 | printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage); | |
367 | ||
368 | if (maxPage == 0) | |
369 | { | |
370 | wxEndBusyCursor(); | |
371 | return FALSE; | |
372 | } | |
373 | ||
374 | // Only set min and max, because from and to have been | |
375 | // set by the user | |
376 | m_printDialogData.SetMinPage(minPage); | |
377 | m_printDialogData.SetMaxPage(maxPage); | |
378 | ||
379 | wxWindow *win = CreateAbortWindow(parent, printout); | |
380 | wxSafeYield(win,true); | |
381 | ||
382 | if (!win) | |
383 | { | |
384 | wxEndBusyCursor(); | |
385 | wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK, parent); | |
386 | delete dc; | |
387 | return FALSE; | |
388 | } | |
389 | sm_abortWindow = win; | |
390 | sm_abortWindow->Show(TRUE); | |
391 | wxSafeYield(win,true); | |
392 | ||
393 | printout->OnBeginPrinting(); | |
394 | ||
395 | bool keepGoing = TRUE; | |
396 | ||
397 | int copyCount; | |
398 | for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++) | |
399 | { | |
400 | if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage())) | |
401 | { | |
402 | wxEndBusyCursor(); | |
403 | wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent); | |
404 | break; | |
405 | } | |
406 | if (sm_abortIt) | |
407 | break; | |
408 | ||
409 | int pn; | |
410 | for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn); | |
411 | pn++) | |
412 | { | |
413 | if (sm_abortIt) | |
414 | { | |
415 | keepGoing = FALSE; | |
416 | break; | |
417 | } | |
418 | else | |
419 | { | |
420 | #if TARGET_CARBON | |
421 | if ( UMAGetSystemVersion() >= 0x1000 ) | |
422 | #endif | |
423 | { | |
424 | GrafPtr thePort ; | |
425 | GetPort( &thePort ) ; | |
426 | wxSafeYield(win,true); | |
427 | SetPort( thePort ) ; | |
428 | } | |
429 | dc->StartPage(); | |
430 | keepGoing = printout->OnPrintPage(pn); | |
431 | dc->EndPage(); | |
432 | } | |
433 | } | |
434 | printout->OnEndDocument(); | |
435 | } | |
436 | ||
437 | printout->OnEndPrinting(); | |
438 | ||
439 | if (sm_abortWindow) | |
440 | { | |
441 | sm_abortWindow->Show(FALSE); | |
442 | delete sm_abortWindow; | |
443 | sm_abortWindow = NULL; | |
444 | } | |
445 | ||
446 | wxEndBusyCursor(); | |
447 | ||
448 | delete dc; | |
449 | ||
450 | return TRUE; | |
451 | } | |
452 | ||
453 | wxDC* wxMacPrinter::PrintDialog(wxWindow *parent) | |
454 | { | |
455 | wxDC* dc = (wxDC*) NULL; | |
456 | ||
457 | wxPrintDialog dialog(parent, & m_printDialogData); | |
458 | int ret = dialog.ShowModal(); | |
459 | ||
460 | if (ret == wxID_OK) | |
461 | { | |
462 | dc = dialog.GetPrintDC(); | |
463 | m_printDialogData = dialog.GetPrintDialogData(); | |
464 | } | |
465 | ||
466 | return dc; | |
467 | } | |
468 | ||
469 | bool wxMacPrinter::Setup(wxWindow *parent) | |
470 | { | |
471 | #if 0 | |
472 | wxPrintDialog dialog(parent, & m_printDialogData); | |
473 | dialog.GetPrintDialogData().SetSetupDialog(TRUE); | |
474 | ||
475 | int ret = dialog.ShowModal(); | |
476 | ||
477 | if (ret == wxID_OK) | |
478 | { | |
479 | m_printDialogData = dialog.GetPrintDialogData(); | |
480 | } | |
481 | ||
482 | return (ret == wxID_OK); | |
483 | #endif | |
484 | return wxID_CANCEL; | |
485 | } | |
486 | ||
487 | /* | |
488 | * Print preview | |
489 | */ | |
490 | ||
491 | wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, | |
492 | wxPrintout *printoutForPrinting, | |
493 | wxPrintDialogData *data) | |
494 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
495 | { | |
496 | DetermineScaling(); | |
497 | } | |
498 | ||
499 | wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data): | |
500 | wxPrintPreviewBase(printout, printoutForPrinting, data) | |
501 | { | |
502 | DetermineScaling(); | |
503 | } | |
504 | ||
505 | wxMacPrintPreview::~wxMacPrintPreview(void) | |
506 | { | |
507 | } | |
508 | ||
509 | bool wxMacPrintPreview::Print(bool interactive) | |
510 | { | |
511 | if (!m_printPrintout) | |
512 | return FALSE; | |
513 | wxMacPrinter printer(&m_printDialogData); | |
514 | return printer.Print(m_previewFrame, m_printPrintout, interactive); | |
515 | } | |
516 | ||
517 | void wxMacPrintPreview::DetermineScaling(void) | |
518 | { | |
519 | int screenWidth , screenHeight ; | |
520 | wxDisplaySize( &screenWidth , &screenHeight ) ; | |
521 | ||
522 | m_previewPrintout->SetPPIScreen( 72 , 72 ) ; | |
523 | m_previewPrintout->SetPPIPrinter( 72 , 72 ) ; | |
524 | m_previewPrintout->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) ); | |
525 | m_previewPrintout->SetPageSizePixels( 8 * 72 , 11 * 72 ) ; | |
526 | m_pageWidth = 8 * 72 ; | |
527 | m_pageHeight = 11 * 72 ; | |
528 | m_previewScale = 1 ; | |
529 | ||
530 | // Get a device context for the currently selected printer | |
531 | wxPrinterDC printerDC(m_printDialogData.GetPrintData()); | |
532 | if (printerDC.Ok()) | |
533 | { | |
534 | int x , y ; | |
535 | wxCoord ww, hh; | |
536 | printerDC.GetSizeMM(&ww, &hh); | |
537 | printerDC.GetSize( &x , &y ) ; | |
538 | m_previewPrintout->SetPageSizeMM((int)ww, (int)hh); | |
539 | m_previewPrintout->SetPageSizePixels( x , y) ; | |
540 | m_pageWidth = x ; | |
541 | m_pageHeight = y ; | |
542 | m_isOk = true ; | |
543 | } | |
544 | else | |
545 | { | |
546 | m_isOk = false ; | |
547 | } | |
548 | // At 100%, the page should look about page-size on the screen. | |
549 | // m_previewScale = (float)((float)screenWidth/(float)printerWidth); | |
550 | // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes); | |
551 | ||
552 | m_previewScale = 1 ; | |
553 | } | |
554 | ||
555 | #endif |