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