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