]> git.saurik.com Git - wxWidgets.git/blame - src/mac/printmac.cpp
Compile fix for app.cpp without precompiled headers.
[wxWidgets.git] / src / mac / printmac.cpp
CommitLineData
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
2f1ae414 38#if !USE_SHARED_LIBRARY
72e7876b
SC
39IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase)
40IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase)
2f1ae414 41#endif
72e7876b
SC
42
43/*
44 * Printer
45 */
46
47wxMacPrinter::wxMacPrinter(wxPrintDialogData *data):
48 wxPrinterBase(data)
49{
50}
51
52wxMacPrinter::~wxMacPrinter(void)
53{
54}
55
56bool 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
72e7876b
SC
97 // Create a suitable device context
98 wxDC *dc = NULL;
99 if (prompt)
100 {
2f1ae414
SC
101 wxPrintDialog dialog(parent, & m_printDialogData);
102 if (dialog.ShowModal() == wxID_OK)
103 {
104 dc = dialog.GetPrintDC();
105 m_printDialogData = dialog.GetPrintData();
106 }
72e7876b
SC
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;
2f1ae414 130 wxCoord ww, hh;
72e7876b
SC
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
72e7876b
SC
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;
4b7b750d 147 return FALSE;
72e7876b
SC
148 }
149 sm_abortWindow = win;
150 sm_abortWindow->Show(TRUE);
151 wxYield();
72e7876b
SC
152
153 printout->OnBeginPrinting();
154
155 bool keepGoing = TRUE;
156
157 int copyCount;
158 for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++)
159 {
160 if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
161 {
162 wxEndBusyCursor();
163 wxMessageBox("Could not start printing.", "Print Error", wxOK, parent);
164 break;
165 }
166 if (sm_abortIt)
167 break;
168
169 int pn;
170 for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
171 pn++)
172 {
173 if (sm_abortIt)
174 {
175 keepGoing = FALSE;
176 break;
177 }
178 else
179 {
5b781a67
SC
180 GrafPtr thePort ;
181 GetPort( &thePort ) ;
182 wxYield() ;
183 SetPort( thePort ) ;
184
72e7876b
SC
185 dc->StartPage();
186 keepGoing = printout->OnPrintPage(pn);
187 dc->EndPage();
188 }
189 }
190 printout->OnEndDocument();
191 }
192
193 printout->OnEndPrinting();
194
195 if (sm_abortWindow)
196 {
197 sm_abortWindow->Show(FALSE);
198 delete sm_abortWindow;
199 sm_abortWindow = NULL;
200 }
201
202 wxEndBusyCursor();
203
204 delete dc;
205
206 return TRUE;
207}
208
209wxDC* wxMacPrinter::PrintDialog(wxWindow *parent)
210{
211 wxDC* dc = (wxDC*) NULL;
212
213 wxPrintDialog dialog(parent, & m_printDialogData);
214 int ret = dialog.ShowModal();
215
216 if (ret == wxID_OK)
217 {
218 dc = dialog.GetPrintDC();
219 m_printDialogData = dialog.GetPrintDialogData();
220 }
221
222 return dc;
223}
224
225bool wxMacPrinter::Setup(wxWindow *parent)
226{
227 wxPrintDialog dialog(parent, & m_printDialogData);
228 dialog.GetPrintDialogData().SetSetupDialog(TRUE);
229
230 int ret = dialog.ShowModal();
231
232 if (ret == wxID_OK)
233 {
234 m_printDialogData = dialog.GetPrintDialogData();
235 }
236
237 return (ret == wxID_OK);
238}
239
240/*
241 * Print preview
242 */
243
244wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout,
245 wxPrintout *printoutForPrinting,
246 wxPrintDialogData *data)
247 : wxPrintPreviewBase(printout, printoutForPrinting, data)
248{
249 DetermineScaling();
250}
251
252wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data):
253 wxPrintPreviewBase(printout, printoutForPrinting, data)
254{
255 DetermineScaling();
256}
257
258wxMacPrintPreview::~wxMacPrintPreview(void)
259{
260}
261
262bool wxMacPrintPreview::Print(bool interactive)
263{
264 if (!m_printPrintout)
265 return FALSE;
266 wxMacPrinter printer(&m_printDialogData);
267 return printer.Print(m_previewFrame, m_printPrintout, interactive);
268}
269
270void wxMacPrintPreview::DetermineScaling(void)
271{
2f1ae414
SC
272 int screenWidth , screenHeight ;
273 wxDisplaySize( &screenWidth , &screenHeight ) ;
274
275 m_previewPrintout->SetPPIScreen( 72 , 72 ) ;
276 m_previewPrintout->SetPPIPrinter( 72 , 72 ) ;
277 m_previewPrintout->SetPageSizeMM( 8 * 25.6 , 11 * 25.6 ) ;
278 m_previewPrintout->SetPageSizePixels( 8 * 72 , 11 * 72 ) ;
279 m_pageWidth = 8 * 72 ;
280 m_pageHeight = 11 * 72 ;
281 m_previewScale = 1 ;
72e7876b
SC
282
283 // Get a device context for the currently selected printer
2f1ae414
SC
284 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
285 if (printerDC.Ok())
72e7876b 286 {
2f1ae414
SC
287 int x , y ;
288 wxCoord ww, hh;
289 printerDC.GetSizeMM(&ww, &hh);
290 printerDC.GetSize( &x , &y ) ;
291 m_previewPrintout->SetPageSizeMM((int)ww, (int)hh);
292 m_previewPrintout->SetPageSizePixels( x , y) ;
293 m_pageWidth = x ;
294 m_pageHeight = y ;
295 m_isOk = true ;
296
297 }
72e7876b 298 // At 100%, the page should look about page-size on the screen.
2f1ae414
SC
299 // m_previewScale = (float)((float)screenWidth/(float)printerWidth);
300 // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes);
301
302 m_previewScale = 1 ;
72e7876b 303}