]> git.saurik.com Git - wxWidgets.git/blame - src/generic/printps.cpp
Add wxAnyScrollHelperBase to reduce code duplication in wxVarScrollHelperBase.
[wxWidgets.git] / src / generic / printps.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/generic/printps.cpp
c801d85f
KB
3// Purpose: Postscript print/preview framework
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
6aa89a22 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
c801d85f
KB
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
8826f46f 15 #pragma hdrstop
c801d85f
KB
16#endif
17
93763ad5
WS
18// ============================================================================
19// declarations
20// ============================================================================
21
22// ----------------------------------------------------------------------------
23// headers
24// ----------------------------------------------------------------------------
c801d85f 25
12bdd77c 26#if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT && (!defined(__WXMSW__) || wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
ce4169a4 27
c801d85f 28#ifndef WX_PRECOMP
8826f46f
VZ
29 #include "wx/utils.h"
30 #include "wx/dc.h"
31 #include "wx/app.h"
32 #include "wx/msgdlg.h"
8fa2e6a2
KB
33 #include "wx/intl.h"
34 #include "wx/progdlg.h"
ca65c044 35 #include "wx/log.h"
6d50343d 36 #include "wx/dcprint.h"
c801d85f
KB
37#endif
38
39#include "wx/generic/printps.h"
c801d85f
KB
40#include "wx/printdlg.h"
41#include "wx/generic/prntdlgg.h"
c31d9c7f 42#include "wx/progdlg.h"
7bcb11d3 43#include "wx/paper.h"
c801d85f
KB
44
45#include <stdlib.h>
46
8826f46f
VZ
47// ----------------------------------------------------------------------------
48// wxWin macros
49// ----------------------------------------------------------------------------
50
8826f46f
VZ
51 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptPrinter, wxPrinterBase)
52 IMPLEMENT_CLASS(wxPostScriptPrintPreview, wxPrintPreviewBase)
ca65c044 53
8826f46f
VZ
54// ============================================================================
55// implementation
56// ============================================================================
7bcb11d3 57
8826f46f
VZ
58// ----------------------------------------------------------------------------
59// Printer
60// ----------------------------------------------------------------------------
61
62wxPostScriptPrinter::wxPostScriptPrinter(wxPrintDialogData *data)
63 : wxPrinterBase(data)
c801d85f
KB
64{
65}
66
8826f46f 67wxPostScriptPrinter::~wxPostScriptPrinter()
c801d85f
KB
68{
69}
70
71bool wxPostScriptPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
72{
ca65c044 73 sm_abortIt = false;
d3b9f782 74 sm_abortWindow = NULL;
8826f46f 75
7bcb11d3 76 if (!printout)
f6bcfd97
BP
77 {
78 sm_lastError = wxPRINTER_ERROR;
ca65c044 79 return false;
f6bcfd97 80 }
8826f46f 81
d2b354f9
JS
82 if (m_printDialogData.GetMinPage() < 1)
83 m_printDialogData.SetMinPage(1);
84 if (m_printDialogData.GetMaxPage() < 1)
85 m_printDialogData.SetMaxPage(9999);
8fa2e6a2 86
8826f46f 87 // Create a suitable device context
999836aa 88 wxDC *dc;
7bcb11d3 89 if (prompt)
c801d85f 90 {
7bcb11d3
JS
91 dc = PrintDialog(parent);
92 if (!dc)
ca65c044 93 return false;
c801d85f 94 }
7bcb11d3
JS
95 else
96 {
f6bcfd97 97 dc = new wxPostScriptDC(GetPrintDialogData().GetPrintData());
7bcb11d3 98 }
8826f46f 99
7bcb11d3 100 // May have pressed cancel.
4f37154e 101 if (!dc || !dc->IsOk())
7bcb11d3
JS
102 {
103 if (dc) delete dc;
f6bcfd97 104 sm_lastError = wxPRINTER_ERROR;
ca65c044 105 return false;
7bcb11d3 106 }
8826f46f 107
904a68b6
RL
108 wxSize ScreenPixels = wxGetDisplaySize();
109 wxSize ScreenMM = wxGetDisplaySizeMM();
8826f46f 110
7a5e6267
JS
111 printout->SetPPIScreen( (int) ((ScreenPixels.GetWidth() * 25.4) / ScreenMM.GetWidth()),
112 (int) ((ScreenPixels.GetHeight() * 25.4) / ScreenMM.GetHeight()) );
4f37154e
RR
113 printout->SetPPIPrinter( dc->GetResolution(),
114 dc->GetResolution() );
8826f46f
VZ
115
116 // Set printout parameters
7bcb11d3 117 printout->SetDC(dc);
8826f46f 118
7bcb11d3
JS
119 int w, h;
120 dc->GetSize(&w, &h);
121 printout->SetPageSizePixels((int)w, (int)h);
f415cab9
JS
122 printout->SetPaperRectPixels(wxRect(0, 0, w, h));
123 int mw, mh;
124 dc->GetSizeMM(&mw, &mh);
125 printout->SetPageSizeMM((int)mw, (int)mh);
8826f46f 126
7bcb11d3
JS
127 // Create an abort window
128 wxBeginBusyCursor();
8826f46f 129
1044a386
JS
130 printout->OnPreparePrinting();
131
d2b354f9
JS
132 // Get some parameters from the printout, if defined
133 int fromPage, toPage;
134 int minPage, maxPage;
135 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
136
137 if (maxPage == 0)
138 {
139 sm_lastError = wxPRINTER_ERROR;
140 wxEndBusyCursor();
ca65c044 141 return false;
d2b354f9
JS
142 }
143
144 // Only set min and max, because from and to have been
145 // set by the user
146 m_printDialogData.SetMinPage(minPage);
147 m_printDialogData.SetMaxPage(maxPage);
398c701f 148
01b5396f
RR
149 if (m_printDialogData.GetFromPage() < minPage)
150 m_printDialogData.SetFromPage( minPage );
151 if (m_printDialogData.GetToPage() > maxPage)
152 m_printDialogData.SetToPage( maxPage );
398c701f 153
67df9783 154 int
e0eeb9b3 155 pagesPerCopy = m_printDialogData.GetToPage()-m_printDialogData.GetFromPage()+1,
67df9783
KB
156 totalPages = pagesPerCopy * m_printDialogData.GetNoCopies(),
157 printedPages = 0;
8fa2e6a2
KB
158 // Open the progress bar dialog
159 wxProgressDialog *progressDialog = new wxProgressDialog (
160 printout->GetTitle(),
161 _("Printing..."),
67df9783 162 totalPages,
ffdaed2f
KB
163 parent,
164 wxPD_CAN_ABORT|wxPD_AUTO_HIDE|wxPD_APP_MODAL);
2179d579 165
7bcb11d3 166 printout->OnBeginPrinting();
8826f46f 167
f6bcfd97
BP
168 sm_lastError = wxPRINTER_NO_ERROR;
169
ca65c044 170 bool keepGoing = true;
8826f46f 171
7bcb11d3
JS
172 int copyCount;
173 for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++)
174 {
175 if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
176 {
177 wxEndBusyCursor();
f6bcfd97
BP
178 wxLogError(_("Could not start printing."));
179 sm_lastError = wxPRINTER_ERROR;
7bcb11d3
JS
180 break;
181 }
182 if (sm_abortIt)
f6bcfd97
BP
183 {
184 sm_lastError = wxPRINTER_CANCELLED;
7bcb11d3 185 break;
f6bcfd97 186 }
8826f46f 187
7bcb11d3
JS
188 int pn;
189 for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
190 pn++)
191 {
192 if (sm_abortIt)
193 {
ca65c044 194 keepGoing = false;
f6bcfd97 195 sm_lastError = wxPRINTER_CANCELLED;
7bcb11d3
JS
196 break;
197 }
198 else
199 {
8fa2e6a2 200 wxString msg;
67df9783
KB
201 msg.Printf(_("Printing page %d..."), printedPages+1);
202 if(progressDialog->Update(printedPages++, msg))
8fa2e6a2
KB
203 {
204 dc->StartPage();
205 printout->OnPrintPage(pn);
206 dc->EndPage();
207 }
208 else
209 {
ca65c044 210 sm_abortIt = true;
f6bcfd97 211 sm_lastError = wxPRINTER_CANCELLED;
ca65c044 212 keepGoing = false;
8fa2e6a2 213 }
7bcb11d3 214 }
ca65c044 215 wxYield();
7bcb11d3
JS
216 }
217 printout->OnEndDocument();
218 }
8826f46f 219
7bcb11d3 220 printout->OnEndPrinting();
67df9783 221 delete progressDialog;
8826f46f 222
7bcb11d3 223 wxEndBusyCursor();
8826f46f 224
7bcb11d3 225 delete dc;
8826f46f 226
f6bcfd97 227 return (sm_lastError == wxPRINTER_NO_ERROR);
c801d85f
KB
228}
229
7bcb11d3 230wxDC* wxPostScriptPrinter::PrintDialog(wxWindow *parent)
c801d85f 231{
d3b9f782 232 wxDC* dc = NULL;
398c701f 233
01b5396f
RR
234 wxGenericPrintDialog dialog( parent, &m_printDialogData );
235 if (dialog.ShowModal() == wxID_OK)
7bcb11d3 236 {
01b5396f
RR
237 dc = dialog.GetPrintDC();
238 m_printDialogData = dialog.GetPrintDialogData();
398c701f 239
f6bcfd97
BP
240 if (dc == NULL)
241 sm_lastError = wxPRINTER_ERROR;
242 else
243 sm_lastError = wxPRINTER_NO_ERROR;
7bcb11d3 244 }
f6bcfd97
BP
245 else
246 sm_lastError = wxPRINTER_CANCELLED;
247
7bcb11d3 248 return dc;
c801d85f
KB
249}
250
4e4528f6 251bool wxPostScriptPrinter::Setup(wxWindow *WXUNUSED(parent))
c801d85f 252{
04a014a5 253#if 0
7bcb11d3 254 wxGenericPrintDialog* dialog = new wxGenericPrintDialog(parent, & m_printDialogData);
ca65c044 255 dialog->GetPrintDialogData().SetSetupDialog(true);
7bcb11d3
JS
256
257 int ret = dialog->ShowModal();
258
259 if (ret == wxID_OK)
260 {
261 m_printDialogData = dialog->GetPrintDialogData();
262 }
263
264 dialog->Destroy();
398c701f 265
7bcb11d3 266 return (ret == wxID_OK);
04a014a5
RR
267#endif
268
4e4528f6 269 return false;
c801d85f
KB
270}
271
8826f46f
VZ
272// ----------------------------------------------------------------------------
273// Print preview
274// ----------------------------------------------------------------------------
c801d85f 275
8826f46f
VZ
276void wxPostScriptPrintPreview::Init(wxPrintout * WXUNUSED(printout),
277 wxPrintout * WXUNUSED(printoutForPrinting))
c801d85f 278{
7bcb11d3
JS
279 // Have to call it here since base constructor can't call it
280 DetermineScaling();
c801d85f
KB
281}
282
8826f46f
VZ
283wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout *printout,
284 wxPrintout *printoutForPrinting,
285 wxPrintDialogData *data)
286 : wxPrintPreviewBase(printout, printoutForPrinting, data)
287{
288 Init(printout, printoutForPrinting);
289}
290
291wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout *printout,
292 wxPrintout *printoutForPrinting,
293 wxPrintData *data)
294 : wxPrintPreviewBase(printout, printoutForPrinting, data)
295{
296 Init(printout, printoutForPrinting);
297}
298
299wxPostScriptPrintPreview::~wxPostScriptPrintPreview()
c801d85f
KB
300{
301}
302
303bool wxPostScriptPrintPreview::Print(bool interactive)
304{
7bcb11d3 305 if (!m_printPrintout)
ca65c044 306 return false;
398c701f
JS
307
308 // Assume that on Unix, the preview may use the PostScript
309 // (generic) version, but printing using the native system is required.
310 // TODO: make a generic print preview class from which wxPostScriptPrintPreview
311 // is derived.
312#ifdef __UNIX__
313 wxPrinter printer(& m_printDialogData);
314#else
7bcb11d3 315 wxPostScriptPrinter printer(& m_printDialogData);
398c701f 316#endif
7bcb11d3 317 return printer.Print(m_previewFrame, m_printPrintout, interactive);
c801d85f
KB
318}
319
8826f46f 320void wxPostScriptPrintPreview::DetermineScaling()
c801d85f 321{
7bcb11d3
JS
322 wxPaperSize paperType = m_printDialogData.GetPrintData().GetPaperId();
323 if (paperType == wxPAPER_NONE)
324 paperType = wxPAPER_NONE;
8826f46f 325
c801d85f
KB
326 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
327 if (!paper)
7bcb11d3
JS
328 paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
329
c801d85f
KB
330 if (paper)
331 {
4f37154e
RR
332 int resolution = 600; // TODO, this is correct, but get this from wxPSDC somehow
333
ac78a90a
VZ
334 const wxSize screenPPI = wxGetDisplayPPI();
335 int logPPIScreenX = screenPPI.GetWidth();
336 int logPPIScreenY = screenPPI.GetHeight();
337 int logPPIPrinterX = resolution;
338 int logPPIPrinterY = resolution;
339
340 m_previewPrintout->SetPPIScreen( logPPIScreenX, logPPIScreenY );
341 m_previewPrintout->SetPPIPrinter( logPPIPrinterX, logPPIPrinterY );
8826f46f 342
7bcb11d3 343 wxSize sizeDevUnits(paper->GetSizeDeviceUnits());
4f37154e
RR
344 sizeDevUnits.x = (wxCoord)((float)sizeDevUnits.x * resolution / 72.0);
345 sizeDevUnits.y = (wxCoord)((float)sizeDevUnits.y * resolution / 72.0);
7bcb11d3
JS
346 wxSize sizeTenthsMM(paper->GetSize());
347 wxSize sizeMM(sizeTenthsMM.x / 10, sizeTenthsMM.y / 10);
348
349 // If in landscape mode, we need to swap the width and height.
350 if ( m_printDialogData.GetPrintData().GetOrientation() == wxLANDSCAPE )
351 {
352 m_pageWidth = sizeDevUnits.y;
353 m_pageHeight = sizeDevUnits.x;
354 m_previewPrintout->SetPageSizeMM(sizeMM.y, sizeMM.x);
7bcb11d3
JS
355 }
356 else
357 {
358 m_pageWidth = sizeDevUnits.x;
359 m_pageHeight = sizeDevUnits.y;
360 m_previewPrintout->SetPageSizeMM(sizeMM.x, sizeMM.y);
7bcb11d3 361 }
f415cab9
JS
362 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
363 m_previewPrintout->SetPaperRectPixels(wxRect(0, 0, m_pageWidth, m_pageHeight));
8826f46f 364
7bcb11d3 365 // At 100%, the page should look about page-size on the screen.
ac78a90a
VZ
366 m_previewScaleX = float(logPPIScreenX) / logPPIPrinterX;
367 m_previewScaleY = float(logPPIScreenY) / logPPIPrinterY;
c801d85f
KB
368 }
369}
370
ce4169a4 371#endif