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