Debian Codegraph @

src/af/xap/win/xap_Win32Dlg_Print.cpp

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