]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/printmac.cpp
adding support for wxCB_SORT, SetString and Insert
[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 #if !USE_SHARED_LIBRARY
45 IMPLEMENT_DYNAMIC_CLASS(wxMacCarbonPrintData, wxPrintNativeDataBase)
46 IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase)
47 IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase)
48 #endif
49
50 bool wxMacCarbonPrintData::Ok() const
51 {
52 return (m_macPageFormat != kPMNoPageFormat) && (m_macPrintSettings != kPMNoPrintSettings) && (m_macPrintSession != kPMNoReference);
53 }
54 wxMacCarbonPrintData::wxMacCarbonPrintData()
55 {
56 m_macPageFormat = kPMNoPageFormat;
57 m_macPrintSettings = kPMNoPrintSettings;
58 m_macPrintSession = kPMNoReference ;
59 ValidateOrCreate() ;
60 }
61
62 wxMacCarbonPrintData::~wxMacCarbonPrintData()
63 {
64 if (m_macPageFormat != kPMNoPageFormat)
65 {
66 (void)PMRelease(m_macPageFormat);
67 m_macPageFormat = kPMNoPageFormat;
68 }
69
70 if (m_macPrintSettings != kPMNoPrintSettings)
71 {
72 (void)PMRelease(m_macPrintSettings);
73 m_macPrintSettings = kPMNoPrintSettings;
74 }
75
76 if ( m_macPrintSession != kPMNoReference )
77 {
78 (void)PMRelease(m_macPrintSession);
79 m_macPrintSession = kPMNoReference;
80 }
81 }
82
83 void wxMacCarbonPrintData::ValidateOrCreate()
84 {
85 OSStatus err = noErr ;
86 if ( m_macPrintSession == kPMNoReference )
87 {
88 err = PMCreateSession( (PMPrintSession *) &m_macPrintSession ) ;
89 }
90 // Set up a valid PageFormat object.
91 if ( m_macPageFormat == kPMNoPageFormat)
92 {
93 err = PMCreatePageFormat((PMPageFormat *) &m_macPageFormat);
94
95 // Note that PMPageFormat is not session-specific, but calling
96 // PMSessionDefaultPageFormat assigns values specific to the printer
97 // associated with the current printing session.
98 if ((err == noErr) &&
99 ( m_macPageFormat != kPMNoPageFormat))
100 {
101 err = PMSessionDefaultPageFormat((PMPrintSession) m_macPrintSession,
102 (PMPageFormat) m_macPageFormat);
103 }
104 }
105 else
106 {
107 err = PMSessionValidatePageFormat((PMPrintSession) m_macPrintSession,
108 (PMPageFormat) m_macPageFormat,
109 kPMDontWantBoolean);
110 }
111
112 // Set up a valid PrintSettings object.
113 if ( m_macPrintSettings == kPMNoPrintSettings)
114 {
115 err = PMCreatePrintSettings((PMPrintSettings *) &m_macPrintSettings);
116
117 // Note that PMPrintSettings is not session-specific, but calling
118 // PMSessionDefaultPrintSettings assigns values specific to the printer
119 // associated with the current printing session.
120 if ((err == noErr) &&
121 ( m_macPrintSettings != kPMNoPrintSettings))
122 {
123 err = PMSessionDefaultPrintSettings((PMPrintSession) m_macPrintSession,
124 (PMPrintSettings) m_macPrintSettings);
125 }
126 }
127 else
128 {
129 err = PMSessionValidatePrintSettings((PMPrintSession) m_macPrintSession,
130 (PMPrintSettings) m_macPrintSettings,
131 kPMDontWantBoolean);
132 }
133 }
134
135 bool wxMacCarbonPrintData::TransferFrom( const wxPrintData &data )
136 {
137 ValidateOrCreate() ;
138 PMSetCopies( (PMPrintSettings) m_macPrintSettings , data.GetNoCopies() , false ) ;
139 PMSetOrientation( (PMPageFormat) m_macPageFormat , ( data.GetOrientation() == wxLANDSCAPE ) ?
140 kPMLandscape : kPMPortrait , false ) ;
141 // collate cannot be set
142 #if 0 // not yet tested
143 if ( m_printerName.Length() > 0 )
144 PMSessionSetCurrentPrinter( (PMPrintSession) m_macPrintSession , wxMacCFStringHolder( m_printerName , wxFont::GetDefaultEncoding() ) ) ;
145 #endif
146 PMColorMode color ;
147 PMGetColorMode( (PMPrintSettings) m_macPrintSettings, &color ) ;
148 if ( data.GetColour() )
149 {
150 if ( color == kPMBlackAndWhite )
151 PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMColor ) ;
152 }
153 else
154 PMSetColorMode( (PMPrintSettings) m_macPrintSettings, kPMBlackAndWhite ) ;
155
156 // PMDuplexMode not yet accessible via API
157 // PMQualityMode not yet accessible via API
158 // todo paperSize
159 return true ;
160 }
161
162 bool wxMacCarbonPrintData::TransferTo( wxPrintData &data )
163 {
164 OSStatus err = noErr ;
165
166 UInt32 copies ;
167 err = PMGetCopies( m_macPrintSettings , &copies ) ;
168 if ( err == noErr )
169 data.SetNoCopies( copies ) ;
170
171 PMOrientation orientation ;
172 err = PMGetOrientation( m_macPageFormat , &orientation ) ;
173 if ( err == noErr )
174 {
175 if ( orientation == kPMPortrait || orientation == kPMReversePortrait )
176 data.SetOrientation( wxPORTRAIT );
177 else
178 data.SetOrientation( wxLANDSCAPE );
179 }
180
181 // collate cannot be set
182 #if 0
183 {
184 wxMacCFStringHolder name ;
185 PMPrinter printer ;
186 PMSessionGetCurrentPrinter( m_macPrintSession ,
187 &printer ) ;
188 m_printerName = name.AsString() ;
189 }
190 #endif
191
192 PMColorMode color ;
193 err = PMGetColorMode( m_macPrintSettings, &color ) ;
194 if ( err == noErr )
195 data.SetColour( !(color == kPMBlackAndWhite) ) ;
196
197 // PMDuplexMode not yet accessible via API
198 // PMQualityMode not yet accessible via API
199 // todo paperSize
200 PMRect rPaper;
201 err = PMGetUnadjustedPaperRect( m_macPageFormat, &rPaper);
202 if ( err == noErr )
203 {
204 data.SetPaperSize( wxSize (
205 (int)(( rPaper.right - rPaper.left ) * pt2mm + 0.5 ) ,
206 (int)(( rPaper.bottom - rPaper.top ) * pt2mm + 0.5 ) ) );
207 }
208 return true ;
209 }
210
211 void wxMacCarbonPrintData::TransferFrom( wxPageSetupData *data )
212 {
213 // should we setup the page rect here ?
214 // since MacOS sometimes has two same paper rects with different
215 // page rects we could make it roundtrip safe perhaps
216 }
217
218 void wxMacCarbonPrintData::TransferTo( wxPageSetupData* data )
219 {
220 PMRect rPaper;
221 OSStatus err = PMGetUnadjustedPaperRect(m_macPageFormat, &rPaper);
222 if ( err == noErr )
223 {
224 PMRect rPage ;
225 err = PMGetUnadjustedPageRect(m_macPageFormat , &rPage ) ;
226 if ( err == noErr )
227 {
228 data->SetMinMarginTopLeft( wxPoint (
229 (int)(((double) rPage.left - rPaper.left ) * pt2mm) ,
230 (int)(((double) rPage.top - rPaper.top ) * pt2mm) ) ) ;
231
232 data->SetMinMarginBottomRight( wxPoint (
233 (wxCoord)(((double) rPaper.right - rPage.right ) * pt2mm),
234 (wxCoord)(((double) rPaper.bottom - rPage.bottom ) * pt2mm)) ) ;
235
236 if ( data->GetMarginTopLeft().x < data->GetMinMarginTopLeft().x )
237 data->SetMarginTopLeft( wxPoint( data->GetMinMarginTopLeft().x ,
238 data->GetMarginTopLeft().y ) ) ;
239
240 if ( data->GetMarginBottomRight().x < data->GetMinMarginBottomRight().x )
241 data->SetMarginBottomRight( wxPoint( data->GetMinMarginBottomRight().x ,
242 data->GetMarginBottomRight().y ) );
243
244 if ( data->GetMarginTopLeft().y < data->GetMinMarginTopLeft().y )
245 data->SetMarginTopLeft( wxPoint( data->GetMarginTopLeft().x , data->GetMinMarginTopLeft().y ) );
246
247 if ( data->GetMarginBottomRight().y < data->GetMinMarginBottomRight().y )
248 data->SetMarginBottomRight( wxPoint( data->GetMarginBottomRight().x ,
249 data->GetMinMarginBottomRight().y) );
250
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 data->SetFromPage( from ) ;
268 data->SetToPage( to ) ;
269 }
270
271 void wxMacCarbonPrintData::TransferFrom( wxPrintDialogData* data )
272 {
273 PMSetPageRange( m_macPrintSettings , data->GetMinPage() , data->GetMaxPage() ) ;
274 PMSetCopies( m_macPrintSettings , data->GetNoCopies() , false ) ;
275 PMSetFirstPage( m_macPrintSettings , data->GetFromPage() , false ) ;
276
277 int toPage = data->GetToPage();
278 if (toPage < 1)
279 toPage = data->GetFromPage();
280 PMSetLastPage( m_macPrintSettings , toPage , false ) ;
281 }
282
283 /*
284 * Printer
285 */
286
287 wxMacPrinter::wxMacPrinter(wxPrintDialogData *data):
288 wxPrinterBase(data)
289 {
290 }
291
292 wxMacPrinter::~wxMacPrinter(void)
293 {
294 }
295
296 bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
297 {
298 sm_abortIt = FALSE;
299 sm_abortWindow = NULL;
300
301 if (!printout)
302 return FALSE;
303
304 printout->SetIsPreview(FALSE);
305 if (m_printDialogData.GetMinPage() < 1)
306 m_printDialogData.SetMinPage(1);
307 if (m_printDialogData.GetMaxPage() < 1)
308 m_printDialogData.SetMaxPage(9999);
309
310 // Create a suitable device context
311 wxDC *dc = NULL;
312 if (prompt)
313 {
314 wxPrintDialog dialog(parent, & m_printDialogData);
315 if (dialog.ShowModal() == wxID_OK)
316 {
317 dc = dialog.GetPrintDC();
318 m_printDialogData = dialog.GetPrintDialogData();
319 }
320 }
321 else
322 {
323 dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ;
324 }
325
326
327 // May have pressed cancel.
328 if (!dc || !dc->Ok())
329 {
330 if (dc) delete dc;
331 return FALSE;
332 }
333
334 // on the mac we have always pixels as addressing mode with 72 dpi
335
336 printout->SetPPIScreen(72, 72);
337 printout->SetPPIPrinter(72, 72);
338
339 // Set printout parameters
340 printout->SetDC(dc);
341
342 int w, h;
343 wxCoord ww, hh;
344 dc->GetSize(&w, &h);
345 printout->SetPageSizePixels((int)w, (int)h);
346 dc->GetSizeMM(&ww, &hh);
347 printout->SetPageSizeMM((int)ww, (int)hh);
348
349 // Create an abort window
350 wxBeginBusyCursor();
351
352 printout->OnPreparePrinting();
353
354 // Get some parameters from the printout, if defined
355 int fromPage, toPage;
356 int minPage, maxPage;
357 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
358
359 if (maxPage == 0)
360 {
361 wxEndBusyCursor();
362 return FALSE;
363 }
364
365 // Only set min and max, because from and to have been
366 // set by the user
367 m_printDialogData.SetMinPage(minPage);
368 m_printDialogData.SetMaxPage(maxPage);
369
370 wxWindow *win = CreateAbortWindow(parent, printout);
371 wxSafeYield(win,true);
372
373 if (!win)
374 {
375 wxEndBusyCursor();
376 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK, parent);
377 delete dc;
378 return FALSE;
379 }
380 sm_abortWindow = win;
381 sm_abortWindow->Show(TRUE);
382 wxSafeYield(win,true);
383
384 printout->OnBeginPrinting();
385
386 bool keepGoing = TRUE;
387
388 int copyCount;
389 for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++)
390 {
391 if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
392 {
393 wxEndBusyCursor();
394 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent);
395 break;
396 }
397 if (sm_abortIt)
398 break;
399
400 int pn;
401 for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
402 pn++)
403 {
404 if (sm_abortIt)
405 {
406 keepGoing = FALSE;
407 break;
408 }
409 else
410 {
411 #if TARGET_CARBON
412 if ( UMAGetSystemVersion() >= 0x1000 )
413 #endif
414 {
415 GrafPtr thePort ;
416 GetPort( &thePort ) ;
417 wxSafeYield(win,true);
418 SetPort( thePort ) ;
419 }
420 dc->StartPage();
421 keepGoing = printout->OnPrintPage(pn);
422 dc->EndPage();
423 }
424 }
425 printout->OnEndDocument();
426 }
427
428 printout->OnEndPrinting();
429
430 if (sm_abortWindow)
431 {
432 sm_abortWindow->Show(FALSE);
433 delete sm_abortWindow;
434 sm_abortWindow = NULL;
435 }
436
437 wxEndBusyCursor();
438
439 delete dc;
440
441 return TRUE;
442 }
443
444 wxDC* wxMacPrinter::PrintDialog(wxWindow *parent)
445 {
446 wxDC* dc = (wxDC*) NULL;
447
448 wxPrintDialog dialog(parent, & m_printDialogData);
449 int ret = dialog.ShowModal();
450
451 if (ret == wxID_OK)
452 {
453 dc = dialog.GetPrintDC();
454 m_printDialogData = dialog.GetPrintDialogData();
455 }
456
457 return dc;
458 }
459
460 bool wxMacPrinter::Setup(wxWindow *parent)
461 {
462 #if 0
463 wxPrintDialog dialog(parent, & m_printDialogData);
464 dialog.GetPrintDialogData().SetSetupDialog(TRUE);
465
466 int ret = dialog.ShowModal();
467
468 if (ret == wxID_OK)
469 {
470 m_printDialogData = dialog.GetPrintDialogData();
471 }
472
473 return (ret == wxID_OK);
474 #endif
475 return wxID_CANCEL;
476 }
477
478 /*
479 * Print preview
480 */
481
482 wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout,
483 wxPrintout *printoutForPrinting,
484 wxPrintDialogData *data)
485 : wxPrintPreviewBase(printout, printoutForPrinting, data)
486 {
487 DetermineScaling();
488 }
489
490 wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data):
491 wxPrintPreviewBase(printout, printoutForPrinting, data)
492 {
493 DetermineScaling();
494 }
495
496 wxMacPrintPreview::~wxMacPrintPreview(void)
497 {
498 }
499
500 bool wxMacPrintPreview::Print(bool interactive)
501 {
502 if (!m_printPrintout)
503 return FALSE;
504 wxMacPrinter printer(&m_printDialogData);
505 return printer.Print(m_previewFrame, m_printPrintout, interactive);
506 }
507
508 void wxMacPrintPreview::DetermineScaling(void)
509 {
510 int screenWidth , screenHeight ;
511 wxDisplaySize( &screenWidth , &screenHeight ) ;
512
513 m_previewPrintout->SetPPIScreen( 72 , 72 ) ;
514 m_previewPrintout->SetPPIPrinter( 72 , 72 ) ;
515 m_previewPrintout->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) );
516 m_previewPrintout->SetPageSizePixels( 8 * 72 , 11 * 72 ) ;
517 m_pageWidth = 8 * 72 ;
518 m_pageHeight = 11 * 72 ;
519 m_previewScale = 1 ;
520
521 // Get a device context for the currently selected printer
522 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
523 if (printerDC.Ok())
524 {
525 int x , y ;
526 wxCoord ww, hh;
527 printerDC.GetSizeMM(&ww, &hh);
528 printerDC.GetSize( &x , &y ) ;
529 m_previewPrintout->SetPageSizeMM((int)ww, (int)hh);
530 m_previewPrintout->SetPageSizePixels( x , y) ;
531 m_pageWidth = x ;
532 m_pageHeight = y ;
533 m_isOk = true ;
534 }
535 else
536 {
537 m_isOk = false ;
538 }
539 // At 100%, the page should look about page-size on the screen.
540 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
541 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);
542
543 m_previewScale = 1 ;
544 }
545
546 #endif