]> git.saurik.com Git - wxWidgets.git/blame - src/generic/printps.cpp
1. wxFileDataObject fixes from Ricky Gonzales - seems to work, so demo added
[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
c801d85f 57#if !USE_SHARED_LIBRARY
8826f46f
VZ
58 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptPrinter, wxPrinterBase)
59 IMPLEMENT_CLASS(wxPostScriptPrintPreview, wxPrintPreviewBase)
c801d85f
KB
60#endif
61
8826f46f
VZ
62// ============================================================================
63// implementation
64// ============================================================================
7bcb11d3 65
8826f46f
VZ
66// ----------------------------------------------------------------------------
67// Printer
68// ----------------------------------------------------------------------------
69
70wxPostScriptPrinter::wxPostScriptPrinter(wxPrintDialogData *data)
71 : wxPrinterBase(data)
c801d85f
KB
72{
73}
74
8826f46f 75wxPostScriptPrinter::~wxPostScriptPrinter()
c801d85f
KB
76{
77}
78
79bool wxPostScriptPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
80{
7bcb11d3
JS
81 sm_abortIt = FALSE;
82 sm_abortWindow = (wxWindow *) NULL;
8826f46f 83
7bcb11d3
JS
84 if (!printout)
85 return FALSE;
8826f46f 86
7bcb11d3 87 printout->SetIsPreview(FALSE);
1044a386
JS
88
89 // 4/9/99, JACS: this is a silly place to allow preparation, considering
90 // the DC and no parameters have been set in the printout object.
91 // Moved further down.
92
93 // printout->OnPreparePrinting();
8826f46f 94
7bcb11d3
JS
95 // Get some parameters from the printout, if defined
96 int fromPage, toPage;
97 int minPage, maxPage;
98 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
8826f46f 99
7bcb11d3
JS
100 if (maxPage == 0)
101 return FALSE;
8826f46f 102
7bcb11d3
JS
103 m_printDialogData.SetMinPage(minPage);
104 m_printDialogData.SetMaxPage(maxPage);
105 if (fromPage != 0)
106 m_printDialogData.SetFromPage(fromPage);
107 if (toPage != 0)
108 m_printDialogData.SetToPage(toPage);
109
110 if (minPage != 0)
c801d85f 111 {
7bcb11d3
JS
112 m_printDialogData.EnablePageNumbers(TRUE);
113 if (m_printDialogData.GetFromPage() < m_printDialogData.GetMinPage())
114 m_printDialogData.SetFromPage(m_printDialogData.GetMinPage());
115 else if (m_printDialogData.GetFromPage() > m_printDialogData.GetMaxPage())
116 m_printDialogData.SetFromPage(m_printDialogData.GetMaxPage());
117 if (m_printDialogData.GetToPage() > m_printDialogData.GetMaxPage())
118 m_printDialogData.SetToPage(m_printDialogData.GetMaxPage());
119 else if (m_printDialogData.GetToPage() < m_printDialogData.GetMinPage())
120 m_printDialogData.SetToPage(m_printDialogData.GetMinPage());
c801d85f 121 }
7bcb11d3
JS
122 else
123 m_printDialogData.EnablePageNumbers(FALSE);
c801d85f 124
8fa2e6a2 125
8826f46f 126 // Create a suitable device context
7bcb11d3
JS
127 wxDC *dc = (wxDC *) NULL;
128 if (prompt)
c801d85f 129 {
7bcb11d3
JS
130 dc = PrintDialog(parent);
131 if (!dc)
132 return FALSE;
c801d85f 133 }
7bcb11d3
JS
134 else
135 {
136 dc = new wxPostScriptDC(wxThePrintSetupData->GetPrinterFile(), FALSE, (wxWindow *) NULL);
137 }
8826f46f 138
7bcb11d3
JS
139 // May have pressed cancel.
140 if (!dc || !dc->Ok())
141 {
142 if (dc) delete dc;
143 return FALSE;
144 }
8826f46f 145
7bcb11d3
JS
146 int logPPIScreenX = 0;
147 int logPPIScreenY = 0;
148 int logPPIPrinterX = 0;
149 int logPPIPrinterY = 0;
8826f46f 150
7bcb11d3
JS
151 logPPIScreenX = 100;
152 logPPIScreenY = 100;
8826f46f 153
7bcb11d3
JS
154 /*
155 // Correct values for X/PostScript?
156 logPPIPrinterX = 100;
157 logPPIPrinterY = 100;
158 */
8826f46f 159
7bcb11d3
JS
160 logPPIPrinterX = 72;
161 logPPIPrinterY = 72;
8826f46f 162
7bcb11d3
JS
163 printout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
164 printout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
8826f46f
VZ
165
166 // Set printout parameters
7bcb11d3 167 printout->SetDC(dc);
8826f46f 168
7bcb11d3
JS
169 int w, h;
170 dc->GetSize(&w, &h);
171 printout->SetPageSizePixels((int)w, (int)h);
172 dc->GetSizeMM(&w, &h);
173 printout->SetPageSizeMM((int)w, (int)h);
8826f46f 174
7bcb11d3
JS
175 // Create an abort window
176 wxBeginBusyCursor();
8826f46f 177
1044a386
JS
178 printout->OnPreparePrinting();
179
67df9783 180 int
e0eeb9b3 181 pagesPerCopy = m_printDialogData.GetToPage()-m_printDialogData.GetFromPage()+1,
67df9783
KB
182 totalPages = pagesPerCopy * m_printDialogData.GetNoCopies(),
183 printedPages = 0;
8fa2e6a2
KB
184 // Open the progress bar dialog
185 wxProgressDialog *progressDialog = new wxProgressDialog (
186 printout->GetTitle(),
187 _("Printing..."),
67df9783 188 totalPages,
ffdaed2f
KB
189 parent,
190 wxPD_CAN_ABORT|wxPD_AUTO_HIDE|wxPD_APP_MODAL);
2179d579 191
7bcb11d3 192 printout->OnBeginPrinting();
8826f46f 193
7bcb11d3 194 bool keepGoing = TRUE;
8826f46f 195
7bcb11d3
JS
196 int copyCount;
197 for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++)
198 {
199 if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
200 {
201 wxEndBusyCursor();
202 wxMessageBox(_("Could not start printing."), _("Print Error"), wxOK, parent);
203 break;
204 }
205 if (sm_abortIt)
206 break;
8826f46f 207
7bcb11d3
JS
208 int pn;
209 for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
210 pn++)
211 {
212 if (sm_abortIt)
213 {
214 keepGoing = FALSE;
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
RR
229 sm_abortIt = TRUE;
230 keepGoing = FALSE;
8fa2e6a2 231 }
7bcb11d3 232 }
4080c40a 233 wxYield();
7bcb11d3
JS
234 }
235 printout->OnEndDocument();
236 }
8826f46f 237
7bcb11d3 238 printout->OnEndPrinting();
67df9783 239 delete progressDialog;
8826f46f 240
7bcb11d3 241 wxEndBusyCursor();
8826f46f 242
7bcb11d3 243 delete dc;
8826f46f 244
7bcb11d3 245 return TRUE;
c801d85f
KB
246}
247
7bcb11d3 248wxDC* wxPostScriptPrinter::PrintDialog(wxWindow *parent)
c801d85f 249{
7bcb11d3
JS
250 wxDC* dc = (wxDC*) NULL;
251 wxGenericPrintDialog* dialog = new wxGenericPrintDialog(parent, & m_printDialogData);
252 int ret = dialog->ShowModal() ;
253 if (ret == wxID_OK)
254 {
255 dc = dialog->GetPrintDC();
256 m_printDialogData = dialog->GetPrintDialogData();
257 }
258 dialog->Destroy();
259
260 return dc;
c801d85f
KB
261}
262
263bool wxPostScriptPrinter::Setup(wxWindow *parent)
264{
7bcb11d3
JS
265 wxGenericPrintDialog* dialog = new wxGenericPrintDialog(parent, & m_printDialogData);
266 dialog->GetPrintDialogData().SetSetupDialog(TRUE);
267
268 int ret = dialog->ShowModal();
269
270 if (ret == wxID_OK)
271 {
272 m_printDialogData = dialog->GetPrintDialogData();
273 }
274
275 dialog->Destroy();
276
277 return (ret == wxID_OK);
c801d85f
KB
278}
279
8826f46f
VZ
280// ----------------------------------------------------------------------------
281// Print preview
282// ----------------------------------------------------------------------------
c801d85f 283
8826f46f
VZ
284void wxPostScriptPrintPreview::Init(wxPrintout * WXUNUSED(printout),
285 wxPrintout * WXUNUSED(printoutForPrinting))
c801d85f 286{
7bcb11d3
JS
287 // Have to call it here since base constructor can't call it
288 DetermineScaling();
c801d85f
KB
289}
290
8826f46f
VZ
291wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout *printout,
292 wxPrintout *printoutForPrinting,
293 wxPrintDialogData *data)
294 : wxPrintPreviewBase(printout, printoutForPrinting, data)
295{
296 Init(printout, printoutForPrinting);
297}
298
299wxPostScriptPrintPreview::wxPostScriptPrintPreview(wxPrintout *printout,
300 wxPrintout *printoutForPrinting,
301 wxPrintData *data)
302 : wxPrintPreviewBase(printout, printoutForPrinting, data)
303{
304 Init(printout, printoutForPrinting);
305}
306
307wxPostScriptPrintPreview::~wxPostScriptPrintPreview()
c801d85f
KB
308{
309}
310
311bool wxPostScriptPrintPreview::Print(bool interactive)
312{
7bcb11d3
JS
313 if (!m_printPrintout)
314 return FALSE;
315 wxPostScriptPrinter printer(& m_printDialogData);
316 return printer.Print(m_previewFrame, m_printPrintout, interactive);
c801d85f
KB
317}
318
8826f46f 319void wxPostScriptPrintPreview::DetermineScaling()
c801d85f 320{
7bcb11d3
JS
321 wxPaperSize paperType = m_printDialogData.GetPrintData().GetPaperId();
322 if (paperType == wxPAPER_NONE)
323 paperType = wxPAPER_NONE;
8826f46f 324
c801d85f
KB
325 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
326 if (!paper)
7bcb11d3
JS
327 paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
328
c801d85f
KB
329 if (paper)
330 {
7bcb11d3
JS
331 m_previewPrintout->SetPPIScreen(100, 100);
332 // m_previewPrintout->SetPPIPrinter(100, 100);
333 m_previewPrintout->SetPPIPrinter(72, 72);
8826f46f 334
7bcb11d3
JS
335 wxSize sizeDevUnits(paper->GetSizeDeviceUnits());
336 wxSize sizeTenthsMM(paper->GetSize());
337 wxSize sizeMM(sizeTenthsMM.x / 10, sizeTenthsMM.y / 10);
338
339 // If in landscape mode, we need to swap the width and height.
340 if ( m_printDialogData.GetPrintData().GetOrientation() == wxLANDSCAPE )
341 {
342 m_pageWidth = sizeDevUnits.y;
343 m_pageHeight = sizeDevUnits.x;
344 m_previewPrintout->SetPageSizeMM(sizeMM.y, sizeMM.x);
345 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
346 }
347 else
348 {
349 m_pageWidth = sizeDevUnits.x;
350 m_pageHeight = sizeDevUnits.y;
351 m_previewPrintout->SetPageSizeMM(sizeMM.x, sizeMM.y);
352 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
353 }
8826f46f 354
7bcb11d3
JS
355 // At 100%, the page should look about page-size on the screen.
356 m_previewScale = (float)0.8;
c801d85f
KB
357 }
358}
359
ce4169a4 360#endif