/
xap_Win32Dlg_Print.cpp
344 lines (293 loc) · 11.1 KB
/
xap_Win32Dlg_Print.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* AbiSource Application Framework
* Copyright (C) 1998 AbiSource, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
#include <windows.h>
#include <stdio.h>
#include "ut_debugmsg.h"
#include "ut_string.h"
#include "ut_assert.h"
#include "xap_Dialog_Id.h"
#include "xap_Win32Dlg_Print.h"
#include "xap_Win32App.h"
#include "xap_Win32FrameImpl.h"
#include "xav_View.h"
#include "gr_Win32Graphics.h"
#include "xap_Win32DialogBase.h"
/*****************************************************************/
static UINT_PTR CALLBACK s_PrintHookProc(
HWND hdlg, // handle to the dialog box window
UINT uiMsg, // message identifier
WPARAM wParam, // message parameter
LPARAM lParam // message parameter
)
{
XAP_Win32Dialog_Print * pThis = NULL;
if(uiMsg == WM_INITDIALOG)
{
PRINTDLGW * pDlgInfo = (PRINTDLGW*)lParam;
pThis = (XAP_Win32Dialog_Print*)pDlgInfo->lCustData;
SetWindowLongPtrW(hdlg, DWLP_USER, (LONG_PTR) pThis);
// reset the 'closed' flag which indicates that the dialog should be considered
// 'closed' rather than aborted
pThis->setClosed(false);
// hide the selection radio since we do not support selection printing
if(pDlgInfo->Flags & PD_NOSELECTION)
{
HWND wPrintSelectionRadio = GetDlgItem(hdlg, rad2);
ShowWindow(wPrintSelectionRadio, SW_HIDE);
}
// remember the original printer selected; we use this to decide if upon closure
// we should notify associated views to rebuild to reflect the new font metrics
UT_uint32 iPrinter = SendDlgItemMessageW(hdlg, cmb4, CB_GETCURSEL, 0, 0);
pThis->setOrigPrinter(iPrinter);
pThis->setNewPrinter(iPrinter);
}
else
{
pThis = (XAP_Win32Dialog_Print*)GetWindowLongPtrW(hdlg, DWLP_USER);
}
if(uiMsg == WM_COMMAND)
{
// if the command indicates that the user changed printer, we change the Cancel
// button to Close; make sure we have valid this pointer.
if((int)LOWORD(wParam) == cmb4 && HIWORD(wParam) == CBN_SELCHANGE)
{
pThis->setNewPrinter(SendDlgItemMessageW(hdlg, cmb4, CB_GETCURSEL, 0, 0));
XAP_App* pApp = XAP_App::getApp();
const XAP_StringSet* pSS = pApp->getStringSet();
if(pThis->getNewPrinter() != pThis->getOrigPrinter())
{
XAP_Win32DialogBase::setDlgItemText(hdlg,IDCANCEL,pSS->getValue(XAP_STRING_ID_DLG_Close));
}
else
{
// the user set the printer back to what it used to be -- revert back to
// cancel button
XAP_Win32DialogBase::setDlgItemText(hdlg,IDCANCEL,pSS->getValue(XAP_STRING_ID_DLG_Cancel));
}
}
else if((int)LOWORD(wParam) == IDCANCEL && HIWORD(wParam) == 0)
{
// the user presed the Cancel/Close button; see if it is Cancel or Close
if(pThis->getNewPrinter() != pThis->getOrigPrinter())
{
// Different printer is selected to the one we started with, this is Close
// scenario -- eat this message and simulate click on the OK button
// instead so that the default dlg procedure fills the PRINTDLG struct
// with the data we need; set the m_bClosed flag, so we can differentiate
// this from the user pressing the OK button
pThis->setClosed(true);
PostMessageW(hdlg, WM_COMMAND, IDOK, 0);
return 1; // eat the message
}
}
}
return 0;
}
XAP_Dialog * XAP_Win32Dialog_Print::static_constructor(XAP_DialogFactory * pFactory,
XAP_Dialog_Id id)
{
XAP_Win32Dialog_Print * p = new XAP_Win32Dialog_Print(pFactory,id);
return p;
}
XAP_Win32Dialog_Print::XAP_Win32Dialog_Print(XAP_DialogFactory * pDlgFactory,
XAP_Dialog_Id id):
XAP_Dialog_Print(pDlgFactory,id),
m_iOrigPrinter(0),
m_iNewPrinter(0),
m_bClosed(false)
{
m_pPersistPrintDlg = (PRINTDLGW *)UT_calloc(1,sizeof(PRINTDLGW));
UT_return_if_fail(m_pPersistPrintDlg);
m_pPersistPrintDlg->lStructSize = sizeof(*m_pPersistPrintDlg);
}
XAP_Win32Dialog_Print::~XAP_Win32Dialog_Print(void)
{
if (m_pPersistPrintDlg)
{
if (m_pPersistPrintDlg->hDevMode)
GlobalFree(m_pPersistPrintDlg->hDevMode);
if (m_pPersistPrintDlg->hDevNames)
GlobalFree(m_pPersistPrintDlg->hDevNames);
g_free(m_pPersistPrintDlg);
}
}
GR_Graphics * XAP_Win32Dialog_Print::getPrinterGraphicsContext(void)
{
UT_ASSERT(m_answer == a_OK);
if (!m_pPersistPrintDlg || !m_pPersistPrintDlg->hDC)
return NULL; /* Prevents from passing NULL to GR_Win32Graphics*/
memset(&m_DocInfo,0,sizeof(m_DocInfo));
m_DocInfo.cbSize = sizeof(DOCINFOW);
m_docName.fromUTF8 (m_szDocumentPathname);
m_DocInfo.lpszDocName = m_docName.c_str();
if (m_bDoPrintToFile) {
m_fileName.fromUTF8 (m_szPrintToFilePathname);
m_DocInfo.lpszOutput = m_fileName.c_str();
} else {
m_DocInfo.lpszOutput = NULL;
}
GR_Win32AllocInfo ai(m_pPersistPrintDlg->hDC,&m_DocInfo, m_pPersistPrintDlg->hDevMode);
GR_Win32Graphics *pGr = (GR_Win32Graphics *)XAP_App::getApp()->newGraphics(ai);
UT_ASSERT(pGr);
return pGr;
}
void XAP_Win32Dialog_Print::releasePrinterGraphicsContext(GR_Graphics * pGraphics)
{
if (pGraphics)
delete pGraphics;
#if 0
// Do not delete the DC, since the original view will use it to do its layout and will
// delete it when no longer needed. Tomas
DeleteDC(m_pPersistPrintDlg->hDC);
#endif
if(m_pPersistPrintDlg)
m_pPersistPrintDlg->hDC = 0;
memset(&m_DocInfo, 0, sizeof(m_DocInfo));
}
/*****************************************************************/
void XAP_Win32Dialog_Print::runModal(XAP_Frame * pFrame)
{
UT_return_if_fail(pFrame);
UT_return_if_fail(m_pPersistPrintDlg);
HWND hwnd = static_cast<XAP_Win32FrameImpl*>(pFrame->getFrameImpl())->getTopLevelWindow();
m_pPersistPrintDlg->hwndOwner = hwnd;
m_pPersistPrintDlg->nFromPage = (WORD)m_nFirstPage;
m_pPersistPrintDlg->nToPage = (WORD)m_nLastPage;
m_pPersistPrintDlg->nMinPage = (WORD)m_nFirstPage;
m_pPersistPrintDlg->nMaxPage = (WORD)m_nLastPage;
m_pPersistPrintDlg->Flags = PD_ALLPAGES | PD_RETURNDC | PD_ENABLEPRINTHOOK;
m_pPersistPrintDlg->lpfnPrintHook = s_PrintHookProc;
m_pPersistPrintDlg->lCustData = (INT_PTR)this;
if (!m_bEnablePageRange)
m_pPersistPrintDlg->Flags |= PD_NOPAGENUMS;
if (!m_bEnablePrintSelection)
m_pPersistPrintDlg->Flags |= PD_NOSELECTION;
if (m_bEnablePrintToFile)
{
if (m_bDoPrintToFile)
m_pPersistPrintDlg->Flags |= PD_PRINTTOFILE;
}
else
{
m_pPersistPrintDlg->Flags |= PD_HIDEPRINTTOFILE;
}
if (!m_bPersistValid) // first time
{
// these values are either in the PRINTDLG structure or
// in the HDEVMODE structure within it depending upon
// the driver. since we don't destroy and recreate the
// structure on each use (and we don't provide our caller
// access to these fields), we only need to load them the
// first time.
m_pPersistPrintDlg->nCopies = (WORD)m_nCopies;
if (m_bCollate)
m_pPersistPrintDlg->Flags |= PD_COLLATE;
}
// see if they just want the properties of the printer without
// bothering the user.
if (m_bPersistValid && m_bBypassActualDialog)
{
_extractResults(pFrame);
if (m_answer == a_OK)
{
// create a new hDC for this printer...
DEVMODEW * pDevMode = (DEVMODEW *)GlobalLock(m_pPersistPrintDlg->hDevMode);
DEVNAMES * pDevNames = (DEVNAMES *)GlobalLock(m_pPersistPrintDlg->hDevNames);
UT_return_if_fail(pDevNames); //GlobalLock can return NULL
wchar_t * p = (wchar_t *)pDevNames;
m_pPersistPrintDlg->hDC = CreateDCW(p + pDevNames->wDriverOffset,
p + pDevNames->wDeviceOffset,
NULL,
pDevMode);
GlobalUnlock(m_pPersistPrintDlg->hDevMode);
GlobalUnlock(m_pPersistPrintDlg->hDevNames);
// notify layout of printer (this results in redoing doc layout !!!)
GR_Win32Graphics* pG=(GR_Win32Graphics*)XAP_App::getApp()->getLastFocussedFrame()->getCurrentView()->getGraphics();
UT_return_if_fail( pG );
pG->setPrintDC(m_pPersistPrintDlg->hDC);
}
}
else if (PrintDlgW(m_pPersistPrintDlg)) // raise the actual dialog.
{
_extractResults(pFrame);
// notify layout of printer (this results in redoing doc layout !!!)
GR_Win32Graphics* pG=(GR_Win32Graphics*)XAP_App::getApp()->getLastFocussedFrame()->getCurrentView()->getGraphics();
UT_return_if_fail( pG );
pG->setPrintDC(m_pPersistPrintDlg->hDC);
// we need to differentiate between actual OK being pressed by user and simulated
// by our hook procedure -- in the later case we want to return a_CANCEL so that
// the document does not get sent to the printer. We will also set m_bPersistValid
// as if OK was clicked
if(m_bClosed)
{
m_answer = a_CANCEL;
m_bPersistValid = true;
}
}
else
{
// The user clicked the cancel button and did not change the printer, there is
// nothing for us to do
m_answer = a_CANCEL;
}
return;
}
void XAP_Win32Dialog_Print::_extractResults(XAP_Frame *pFrame)
{
UT_return_if_fail(m_pPersistPrintDlg);
UT_return_if_fail(m_pPersistPrintDlg->hDevMode!=NULL);
m_bDoPrintRange = ((m_pPersistPrintDlg->Flags & PD_PAGENUMS) != 0);
m_bDoPrintSelection = ((m_pPersistPrintDlg->Flags & PD_SELECTION) != 0);
m_bDoPrintToFile = ((m_pPersistPrintDlg->Flags & PD_PRINTTOFILE) != 0);
m_nFirstPage = m_pPersistPrintDlg->nFromPage;
m_nLastPage = m_pPersistPrintDlg->nToPage;
// Most Win32 printer drivers support multicopies and collating,
//however we want Abi to do both by himself, like in the rest of platforms
DEVMODEW *pDevMode=(DEVMODEW *)GlobalLock(m_pPersistPrintDlg->hDevMode);
UT_return_if_fail(pDevMode); //GlobalLock can return NULL
m_nCopies = pDevMode->dmCopies;
m_bCollate = ((pDevMode->dmCollate & DMCOLLATE_TRUE) != 0);
pDevMode->dmCopies = 1;
pDevMode->dmCollate = DMCOLLATE_FALSE;
pDevMode->dmFields = DM_COPIES | DM_COLLATE;
GlobalUnlock(m_pPersistPrintDlg->hDevMode);
// any changes to the DEVMODE structure must be followed by this call before the
// modified structure can be used !!!
GR_Win32Graphics::fixDevMode(m_pPersistPrintDlg->hDevMode);
pDevMode=(DEVMODEW *)GlobalLock(m_pPersistPrintDlg->hDevMode);
ResetDCW(m_pPersistPrintDlg->hDC,pDevMode);
GlobalUnlock(pDevMode);
if (m_bDoPrintToFile)
{
char bufSuggestedName[1030];
memset(bufSuggestedName,0,sizeof(bufSuggestedName));
// we construct a suggested pathname for the print-to-file pathname.
// we append a .print to the string. it would be better to append
// a .ps or whatever, but we don't know what the technology/language
// of the device is....
sprintf(bufSuggestedName,"%s.print",m_szDocumentPathname);
if (!_getPrintToFilePathname(pFrame,bufSuggestedName))
goto Fail;
}
m_answer = a_OK;
return;
Fail:
m_answer = a_CANCEL;
return;
}