another "const" added
[wxWidgets.git] / src / generic / printps.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: printps.cpp
3 // Purpose: Postscript print/preview 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 "printps.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/generic/printps.h"
33 #include "wx/dcprint.h"
34 #include "wx/printdlg.h"
35 #include "wx/generic/prntdlgg.h"
36
37 #include <stdlib.h>
38
39 #if !USE_SHARED_LIBRARY
40 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptPrinter, wxPrinterBase)
41 IMPLEMENT_CLASS(wxPostScriptPrintPreview, wxPrintPreviewBase)
42 #endif
43
44 /*
45 * Printer
46 */
47
48 wxPostScriptPrinter::wxPostScriptPrinter(wxPrintData *data):
49 wxPrinterBase(data)
50 {
51 }
52
53 wxPostScriptPrinter::~wxPostScriptPrinter(void)
54 {
55 }
56
57 bool wxPostScriptPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
58 {
59 abortIt = FALSE;
60 abortWindow = NULL;
61
62 if (!printout)
63 return FALSE;
64
65 printout->SetIsPreview(FALSE);
66 printout->OnPreparePrinting();
67
68 // Get some parameters from the printout, if defined
69 int fromPage, toPage;
70 int minPage, maxPage;
71 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
72
73 if (maxPage == 0)
74 return FALSE;
75
76 printData.SetMinPage(minPage);
77 printData.SetMaxPage(maxPage);
78 if (fromPage != 0)
79 printData.SetFromPage(fromPage);
80 if (toPage != 0)
81 printData.SetToPage(toPage);
82
83 if (minPage != 0)
84 {
85 printData.EnablePageNumbers(TRUE);
86 if (printData.GetFromPage() < printData.GetMinPage())
87 printData.SetFromPage(printData.GetMinPage());
88 else if (printData.GetFromPage() > printData.GetMaxPage())
89 printData.SetFromPage(printData.GetMaxPage());
90 if (printData.GetToPage() > printData.GetMaxPage())
91 printData.SetToPage(printData.GetMaxPage());
92 else if (printData.GetToPage() < printData.GetMinPage())
93 printData.SetToPage(printData.GetMinPage());
94 }
95 else
96 printData.EnablePageNumbers(FALSE);
97
98 // Create a suitable device context
99 wxDC *dc = NULL;
100 if (prompt)
101 {
102 wxGenericPrintDialog dialog(parent, & printData);
103 if (dialog.ShowModal() == wxID_OK)
104 {
105 dc = dialog.GetPrintDC();
106 printData = dialog.GetPrintData();
107 }
108 }
109 else
110 {
111 dc = new wxPostScriptDC(wxThePrintSetupData->GetPrinterFile(), FALSE, NULL);
112 }
113
114 // May have pressed cancel.
115 if (!dc || !dc->Ok())
116 {
117 if (dc) delete dc;
118 return FALSE;
119 }
120
121 int logPPIScreenX = 0;
122 int logPPIScreenY = 0;
123 int logPPIPrinterX = 0;
124 int logPPIPrinterY = 0;
125
126 // Correct values for X/PostScript?
127 logPPIScreenX = 100;
128 logPPIScreenY = 100;
129 logPPIPrinterX = 100;
130 logPPIPrinterY = 100;
131
132 printout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
133 printout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
134
135 // Set printout parameters
136 printout->SetDC(dc);
137
138 int w, h;
139 long ww, hh;
140 dc->GetSize(&w, &h);
141 printout->SetPageSizePixels((int)w, (int)h);
142 dc->GetSizeMM(&ww, &hh);
143 printout->SetPageSizeMM((int)ww, (int)hh);
144
145 // Create an abort window
146 wxBeginBusyCursor();
147
148 printout->OnBeginPrinting();
149
150 bool keepGoing = TRUE;
151
152 int copyCount;
153 for (copyCount = 1; copyCount <= printData.GetNoCopies(); copyCount ++)
154 {
155 if (!printout->OnBeginDocument(printData.GetFromPage(), printData.GetToPage()))
156 {
157 wxEndBusyCursor();
158 wxMessageBox("Could not start printing.", "Print Error", wxOK, parent);
159 break;
160 }
161 if (abortIt)
162 break;
163
164 int pn;
165 for (pn = printData.GetFromPage(); keepGoing && (pn <= printData.GetToPage()) && printout->HasPage(pn);
166 pn++)
167 {
168 if (abortIt)
169 {
170 keepGoing = FALSE;
171 break;
172 }
173 else
174 {
175 dc->StartPage();
176 printout->OnPrintPage(pn);
177 dc->EndPage();
178 }
179 }
180 printout->OnEndDocument();
181 }
182
183 printout->OnEndPrinting();
184
185 wxEndBusyCursor();
186
187 delete dc;
188
189 return TRUE;
190 }
191
192 bool wxPostScriptPrinter::PrintDialog(wxWindow *parent)
193 {
194 wxGenericPrintDialog dialog(parent, & printData);
195 return (dialog.ShowModal() == wxID_OK);
196 }
197
198 bool wxPostScriptPrinter::Setup(wxWindow *parent)
199 {
200 wxGenericPrintDialog dialog(parent, & printData);
201 dialog.GetPrintData().SetSetupDialog(TRUE);
202 return (dialog.ShowModal() == wxID_OK);
203 }
204
205 /*
206 * Print preview
207 */
208
209 wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data):
210 wxPrintPreviewBase(printout, printoutForPrinting, data)
211 {
212 // Have to call it here since base constructor can't call it
213 DetermineScaling();
214 }
215
216 wxPostScriptPrintPreview::~wxPostScriptPrintPreview(void)
217 {
218 }
219
220 bool wxPostScriptPrintPreview::Print(bool interactive)
221 {
222 if (!printPrintout)
223 return FALSE;
224 wxPostScriptPrinter printer(&printData);
225 return printer.Print(previewFrame, printPrintout, interactive);
226 }
227
228 void wxPostScriptPrintPreview::DetermineScaling(void)
229 {
230 const char *paperType = wxThePrintSetupData->GetPaperName();
231 if (!paperType)
232 paperType = "A4 210 x 297 mm";
233
234 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
235 if (!paper)
236 paper = wxThePrintPaperDatabase->FindPaperType("A4 210 x 297 mm");
237 if (paper)
238 {
239 previewPrintout->SetPPIScreen(100, 100);
240 previewPrintout->SetPPIPrinter(100, 100);
241
242 // If in landscape mode, we need to swap the width and height.
243 if ( printData.GetOrientation() == wxLANDSCAPE )
244 {
245 pageWidth = paper->heightPixels;
246 pageHeight = paper->widthPixels;
247 previewPrintout->SetPageSizeMM(paper->heightMM, paper->widthMM);
248 previewPrintout->SetPageSizePixels(paper->heightPixels, paper->widthPixels);
249 }
250 else
251 {
252 pageWidth = paper->widthPixels;
253 pageHeight = paper->heightPixels;
254 previewPrintout->SetPageSizeMM(paper->widthMM, paper->heightMM);
255 previewPrintout->SetPageSizePixels(paper->widthPixels, paper->heightPixels);
256 }
257
258 // At 100%, the page should look about page-size on the screen.
259 previewScale = (float)0.8;
260 // previewScale = (float)((float)screenWidth/(float)printerWidth);
261 // previewScale = previewScale * (float)((float)screenXRes/(float)printerYRes);
262 }
263 }
264