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