]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/printmac.cpp
* Fix logical error in m_overflow attribute merging
[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 #ifdef __GNUG__
13 #pragma implementation "printwin.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/defs.h"
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/mac/private.h"
33
34 #include "wx/mac/printmac.h"
35 #include "wx/dcprint.h"
36 #include "wx/printdlg.h"
37
38 #include <stdlib.h>
39
40 #if !USE_SHARED_LIBRARY
41 IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase)
42 IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase)
43 #endif
44
45 /*
46 * Printer
47 */
48
49 wxMacPrinter::wxMacPrinter(wxPrintDialogData *data):
50 wxPrinterBase(data)
51 {
52 }
53
54 wxMacPrinter::~wxMacPrinter(void)
55 {
56 }
57
58 bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
59 {
60 sm_abortIt = FALSE;
61 sm_abortWindow = NULL;
62
63 if (!printout)
64 return FALSE;
65
66 printout->SetIsPreview(FALSE);
67 printout->OnPreparePrinting();
68
69 // Get some parameters from the printout, if defined
70 int fromPage, toPage;
71 int minPage, maxPage;
72 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
73
74 if (maxPage == 0)
75 return FALSE;
76
77 m_printDialogData.SetMinPage(minPage);
78 m_printDialogData.SetMaxPage(maxPage);
79 if (fromPage != 0)
80 m_printDialogData.SetFromPage(fromPage);
81 if (toPage != 0)
82 m_printDialogData.SetToPage(toPage);
83
84 if (minPage != 0)
85 {
86 m_printDialogData.EnablePageNumbers(TRUE);
87 if (m_printDialogData.GetFromPage() < m_printDialogData.GetMinPage())
88 m_printDialogData.SetFromPage(m_printDialogData.GetMinPage());
89 else if (m_printDialogData.GetFromPage() > m_printDialogData.GetMaxPage())
90 m_printDialogData.SetFromPage(m_printDialogData.GetMaxPage());
91 if (m_printDialogData.GetToPage() > m_printDialogData.GetMaxPage())
92 m_printDialogData.SetToPage(m_printDialogData.GetMaxPage());
93 else if (m_printDialogData.GetToPage() < m_printDialogData.GetMinPage())
94 m_printDialogData.SetToPage(m_printDialogData.GetMinPage());
95 }
96 else
97 m_printDialogData.EnablePageNumbers(FALSE);
98
99 // Create a suitable device context
100 wxDC *dc = NULL;
101 if (prompt)
102 {
103 wxPrintDialog dialog(parent, & m_printDialogData);
104 if (dialog.ShowModal() == wxID_OK)
105 {
106 dc = dialog.GetPrintDC();
107 m_printDialogData = dialog.GetPrintData();
108 }
109 }
110 else
111 {
112 dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ;
113 }
114
115
116 // May have pressed cancel.
117 if (!dc || !dc->Ok())
118 {
119 if (dc) delete dc;
120 return FALSE;
121 }
122
123 // on the mac we have always pixels as addressing mode with 72 dpi
124
125 printout->SetPPIScreen(72, 72);
126 printout->SetPPIPrinter(72, 72);
127
128 // Set printout parameters
129 printout->SetDC(dc);
130
131 int w, h;
132 wxCoord ww, hh;
133 dc->GetSize(&w, &h);
134 printout->SetPageSizePixels((int)w, (int)h);
135 dc->GetSizeMM(&ww, &hh);
136 printout->SetPageSizeMM((int)ww, (int)hh);
137
138 // Create an abort window
139 wxBeginBusyCursor();
140
141 wxWindow *win = CreateAbortWindow(parent, printout);
142 wxSafeYield(win,true);
143
144 if (!win)
145 {
146 wxEndBusyCursor();
147 wxMessageBox(wxT("Sorry, could not create an abort dialog."), wxT("Print Error"), wxOK, parent);
148 delete dc;
149 return FALSE;
150 }
151 sm_abortWindow = win;
152 sm_abortWindow->Show(TRUE);
153 wxSafeYield(win,true);
154
155 printout->OnBeginPrinting();
156
157 bool keepGoing = TRUE;
158
159 int copyCount;
160 for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++)
161 {
162 if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
163 {
164 wxEndBusyCursor();
165 wxMessageBox(wxT("Could not start printing."), wxT("Print Error"), wxOK, parent);
166 break;
167 }
168 if (sm_abortIt)
169 break;
170
171 int pn;
172 for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
173 pn++)
174 {
175 if (sm_abortIt)
176 {
177 keepGoing = FALSE;
178 break;
179 }
180 else
181 {
182 #if !TARGET_CARBON
183 GrafPtr thePort ;
184 GetPort( &thePort ) ;
185 wxSafeYield(win,true);
186 SetPort( thePort ) ;
187 #endif
188 dc->StartPage();
189 keepGoing = printout->OnPrintPage(pn);
190 dc->EndPage();
191 }
192 }
193 printout->OnEndDocument();
194 }
195
196 printout->OnEndPrinting();
197
198 if (sm_abortWindow)
199 {
200 sm_abortWindow->Show(FALSE);
201 delete sm_abortWindow;
202 sm_abortWindow = NULL;
203 }
204
205 wxEndBusyCursor();
206
207 delete dc;
208
209 return TRUE;
210 }
211
212 wxDC* wxMacPrinter::PrintDialog(wxWindow *parent)
213 {
214 wxDC* dc = (wxDC*) NULL;
215
216 wxPrintDialog dialog(parent, & m_printDialogData);
217 int ret = dialog.ShowModal();
218
219 if (ret == wxID_OK)
220 {
221 dc = dialog.GetPrintDC();
222 m_printDialogData = dialog.GetPrintDialogData();
223 }
224
225 return dc;
226 }
227
228 bool wxMacPrinter::Setup(wxWindow *parent)
229 {
230 wxPrintDialog dialog(parent, & m_printDialogData);
231 dialog.GetPrintDialogData().SetSetupDialog(TRUE);
232
233 int ret = dialog.ShowModal();
234
235 if (ret == wxID_OK)
236 {
237 m_printDialogData = dialog.GetPrintDialogData();
238 }
239
240 return (ret == wxID_OK);
241 }
242
243 /*
244 * Print preview
245 */
246
247 wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout,
248 wxPrintout *printoutForPrinting,
249 wxPrintDialogData *data)
250 : wxPrintPreviewBase(printout, printoutForPrinting, data)
251 {
252 DetermineScaling();
253 }
254
255 wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data):
256 wxPrintPreviewBase(printout, printoutForPrinting, data)
257 {
258 DetermineScaling();
259 }
260
261 wxMacPrintPreview::~wxMacPrintPreview(void)
262 {
263 }
264
265 bool wxMacPrintPreview::Print(bool interactive)
266 {
267 if (!m_printPrintout)
268 return FALSE;
269 wxMacPrinter printer(&m_printDialogData);
270 return printer.Print(m_previewFrame, m_printPrintout, interactive);
271 }
272
273 void wxMacPrintPreview::DetermineScaling(void)
274 {
275 int screenWidth , screenHeight ;
276 wxDisplaySize( &screenWidth , &screenHeight ) ;
277
278 m_previewPrintout->SetPPIScreen( 72 , 72 ) ;
279 m_previewPrintout->SetPPIPrinter( 72 , 72 ) ;
280 m_previewPrintout->SetPageSizeMM( (int) (8.0 * 25.6), (int) (11.0 * 25.6) );
281 m_previewPrintout->SetPageSizePixels( 8 * 72 , 11 * 72 ) ;
282 m_pageWidth = 8 * 72 ;
283 m_pageHeight = 11 * 72 ;
284 m_previewScale = 1 ;
285
286 // Get a device context for the currently selected printer
287 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
288 if (printerDC.Ok())
289 {
290 int x , y ;
291 wxCoord ww, hh;
292 printerDC.GetSizeMM(&ww, &hh);
293 printerDC.GetSize( &x , &y ) ;
294 m_previewPrintout->SetPageSizeMM((int)ww, (int)hh);
295 m_previewPrintout->SetPageSizePixels( x , y) ;
296 m_pageWidth = x ;
297 m_pageHeight = y ;
298 m_isOk = true ;
299 }
300 else
301 {
302 m_isOk = false ;
303 }
304 // At 100%, the page should look about page-size on the screen.
305 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
306 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);
307
308 m_previewScale = 1 ;
309 }