1 // xap_Win32DialogHelper.cpp
2 
3 /* AbiWord
4  * Copyright (C) 1998 AbiSource, Inc.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301 USA.
20  */
21 
22 #define WIN32_LEAN_AND_MEAN
23 #include <windows.h>
24 
25 #include "ut_string.h"
26 #include "ut_assert.h"
27 #include "ut_debugmsg.h"
28 
29 #include "xap_App.h"
30 #include "xap_Win32App.h"
31 #include "xap_Win32DialogHelper.h"
32 #include "xap_Win32Toolbar_Icons.h"
33 #include "ut_Win32LocaleString.h"
34 
35 
_assertValidDlgHandle(HWND hDlg)36 static void _assertValidDlgHandle(HWND hDlg)
37 {
38 	UT_DEBUG_ONLY_ARG(hDlg);
39 	UT_ASSERT(IsWindow(hDlg));
40 }
41 
42 
43 
runModal(XAP_Frame * pFrame,XAP_Dialog_Id dialog_id,UT_sint32 resource_id,XAP_Dialog * p_dialog)44 void XAP_Win32DialogHelper::runModal(XAP_Frame * pFrame, XAP_Dialog_Id dialog_id, UT_sint32 resource_id, XAP_Dialog *p_dialog)
45 {
46 	UT_DEBUG_ONLY_ARG(dialog_id);
47 	UT_ASSERT(m_pDialog != NULL);
48 
49 	XAP_Win32App * pWin32App = static_cast<XAP_Win32App *>(p_dialog->getApp());
50 
51 	UT_ASSERT(p_dialog->getDialogId() == dialog_id);
52 
53 	LPCWSTR lpTemplate = MAKEINTRESOURCEW(resource_id);
54 
55 	UT_DebugOnly<int> result = DialogBoxParamW(pWin32App->getInstance(), lpTemplate,
56 								static_cast<XAP_Win32FrameImpl*>(pFrame->getFrameImpl())->getTopLevelWindow(),
57 								(DLGPROC)s_dlgProc, (LPARAM)this);
58 	UT_ASSERT((result != -1));
59 }
60 
61 
62 
runModeless(XAP_Frame * pFrame,XAP_Dialog_Id dialog_id,UT_sint32 resource_id,XAP_Dialog_Modeless * p_dialog)63 void XAP_Win32DialogHelper::runModeless(XAP_Frame * pFrame, XAP_Dialog_Id dialog_id, UT_sint32 resource_id, XAP_Dialog_Modeless *p_dialog)
64 {
65 	UT_ASSERT(pFrame);
66 
67 	// raise the dialog
68 	XAP_Win32App * pWin32App = static_cast<XAP_Win32App *>(p_dialog->getApp());
69 
70 	LPCWSTR lpTemplate = NULL;
71 
72 	UT_ASSERT(p_dialog->getDialogId() == dialog_id);
73 
74 	lpTemplate = MAKEINTRESOURCEW(resource_id);
75 
76 	HWND hWndDialog = CreateDialogParamW(pWin32App->getInstance(),lpTemplate,
77 								static_cast<XAP_Win32FrameImpl*>(pFrame->getFrameImpl())->getTopLevelWindow(),
78 								(DLGPROC)s_dlgProc,(LPARAM)this);
79 	ShowWindow(hWndDialog, SW_SHOW);
80 	UT_ASSERT((hWndDialog != NULL));
81 
82 	p_dialog->getApp()->rememberModelessId(dialog_id, p_dialog);
83 
84 }
85 
86 
s_dlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)87 BOOL CALLBACK XAP_Win32DialogHelper::s_dlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
88 {
89 	// This is a static function.
90 
91 	XAP_Win32DialogHelper * pThis;
92 
93 	switch (msg)
94 	{
95 	case WM_INITDIALOG:
96 		pThis = (XAP_Win32DialogHelper *)lParam;
97 		pThis->m_hDlg = hWnd;
98 		_assertValidDlgHandle(hWnd);
99 		SetWindowLongPtrW(hWnd,DWLP_USER,lParam);
100 		return pThis->m_pDialog->_onInitDialog(hWnd,wParam,lParam);
101 
102 	case WM_COMMAND:
103 		pThis = (XAP_Win32DialogHelper *)GetWindowLongPtrW(hWnd,DWLP_USER);
104 		if(pThis)
105 			return pThis->m_pDialog->_onCommand(hWnd,wParam,lParam);
106 		else
107 			return 0;
108 
109 	case WM_NOTIFY:
110 		pThis = (XAP_Win32DialogHelper *)GetWindowLongPtrW(hWnd,DWLP_USER);
111 		switch (((LPNMHDR)lParam)->code)
112 		{
113 			case UDN_DELTAPOS:		return pThis->m_pDialog->_onDeltaPos((NM_UPDOWN *)lParam);
114 			default:				return 0;
115 		}
116 
117 
118 	default:
119 		return 0;
120 	}
121 }
122 
123 
checkButton(UT_sint32 controlId,bool bChecked)124 void XAP_Win32DialogHelper::checkButton(UT_sint32 controlId, bool bChecked)
125 {
126 	_assertValidDlgHandle(m_hDlg);
127 	CheckDlgButton(m_hDlg, controlId, bChecked ? BST_CHECKED : BST_UNCHECKED);
128 }
129 
enableControl(UT_sint32 controlId,bool bChecked)130 void XAP_Win32DialogHelper::enableControl(UT_sint32 controlId, bool bChecked)
131 {
132 	_assertValidDlgHandle(m_hDlg);
133 	EnableWindow(GetDlgItem(m_hDlg, controlId), bChecked ? TRUE : FALSE);
134 }
135 
destroyWindow()136 void XAP_Win32DialogHelper::destroyWindow()
137 {
138 	_assertValidDlgHandle(m_hDlg);
139 	DestroyWindow(m_hDlg);
140 }
141 
setDialogTitle(LPCSTR p_str)142 void XAP_Win32DialogHelper::setDialogTitle(LPCSTR p_str)
143 {
144 	_assertValidDlgHandle(m_hDlg);
145 	UT_Win32LocaleString str;
146 	str.fromUTF8(p_str);
147 	SetWindowTextW(m_hDlg, str.c_str());
148 }
149 
showWindow(int Mode)150 int XAP_Win32DialogHelper::showWindow(int Mode )
151 {
152 	_assertValidDlgHandle(m_hDlg);
153 	return ShowWindow(m_hDlg, Mode);
154 }
155 
showControl(UT_sint32 controlId,int Mode)156 int XAP_Win32DialogHelper::showControl(UT_sint32 controlId, int Mode)
157 {
158 	_assertValidDlgHandle(m_hDlg);
159 	return ShowWindow(GetDlgItem(m_hDlg, controlId), Mode);
160 }
161 
bringWindowToTop()162 int XAP_Win32DialogHelper::bringWindowToTop()
163 {
164 	_assertValidDlgHandle(m_hDlg);
165 	const UINT uFlags =	SWP_NOMOVE |
166 						SWP_NOOWNERZORDER |
167 						SWP_NOSIZE |
168 						SWP_NOACTIVATE;
169 	return SetWindowPos(m_hDlg, HWND_TOP, 0, 0, 0, 0, uFlags);
170 }
171 
172 // Combo boxes.
173 
addItemToCombo(UT_sint32 controlId,LPCSTR p_str)174 int XAP_Win32DialogHelper::addItemToCombo(UT_sint32 controlId, LPCSTR p_str)
175 {
176 	_assertValidDlgHandle(m_hDlg);
177 	UT_Win32LocaleString str;
178 	str.fromUTF8(p_str);
179 	return SendDlgItemMessageW(m_hDlg, controlId, CB_ADDSTRING, 0, (LPARAM)str.c_str());
180 }
181 
setComboDataItem(UT_sint32 controlId,int nIndex,DWORD dwData)182 int XAP_Win32DialogHelper::setComboDataItem(UT_sint32 controlId, int nIndex, DWORD dwData)
183 {
184 	_assertValidDlgHandle(m_hDlg);
185 	return SendDlgItemMessageW(m_hDlg, controlId, CB_SETITEMDATA, nIndex, dwData);
186 }
187 
188 
getComboDataItem(UT_sint32 controlId,int nIndex)189 int XAP_Win32DialogHelper::getComboDataItem(UT_sint32 controlId, int nIndex)
190 {
191 	_assertValidDlgHandle(m_hDlg);
192 	return SendDlgItemMessageW(m_hDlg, controlId, CB_GETITEMDATA, nIndex,0);
193 }
194 
selectComboItem(UT_sint32 controlId,int index)195 void XAP_Win32DialogHelper::selectComboItem(UT_sint32 controlId, int index)
196 {
197 	_assertValidDlgHandle(m_hDlg);
198 	SendDlgItemMessageW(m_hDlg, controlId, CB_SETCURSEL, index, 0);
199 }
200 
getComboSelectedIndex(UT_sint32 controlId) const201 int XAP_Win32DialogHelper::getComboSelectedIndex(UT_sint32 controlId) const
202 {
203 	_assertValidDlgHandle(m_hDlg);
204 	return SendDlgItemMessageW(m_hDlg, controlId, CB_GETCURSEL, 0, 0);
205 }
206 
resetComboContent(UT_sint32 controlId)207 void XAP_Win32DialogHelper::resetComboContent(UT_sint32 controlId)
208 {
209 	_assertValidDlgHandle(m_hDlg);
210 	SendDlgItemMessageW(m_hDlg, controlId, CB_RESETCONTENT, 0, 0);
211 }
212 
213 // List boxes
214 
resetContent(UT_sint32 controlId)215 void XAP_Win32DialogHelper::resetContent(UT_sint32 controlId)
216 {
217 	_assertValidDlgHandle(m_hDlg);
218 	SendDlgItemMessageW(m_hDlg, controlId, LB_RESETCONTENT, 0, 0);
219 }
220 
221 
addItemToList(UT_sint32 controlId,LPCSTR p_str)222 int XAP_Win32DialogHelper::addItemToList(UT_sint32 controlId, LPCSTR p_str)
223 {
224 	_assertValidDlgHandle(m_hDlg);
225 	UT_Win32LocaleString str;
226 	str.fromUTF8(p_str);
227 	return SendDlgItemMessageW(m_hDlg, controlId, LB_ADDSTRING, 0, (LPARAM)str.c_str());
228 }
229 
setListDataItem(UT_sint32 controlId,int nIndex,DWORD dwData)230 int XAP_Win32DialogHelper::setListDataItem(UT_sint32 controlId, int nIndex, DWORD dwData)
231 {
232 	_assertValidDlgHandle(m_hDlg);
233 	return SendDlgItemMessageW(m_hDlg, controlId, LB_SETITEMDATA, nIndex, dwData);
234 }
235 
236 
getListDataItem(UT_sint32 controlId,int nIndex)237 int XAP_Win32DialogHelper::getListDataItem(UT_sint32 controlId, int nIndex)
238 {
239 	_assertValidDlgHandle(m_hDlg);
240 	return SendDlgItemMessageW(m_hDlg, controlId, LB_GETITEMDATA, nIndex,0);
241 }
242 
getListSelectedIndex(UT_sint32 controlId) const243 int XAP_Win32DialogHelper::getListSelectedIndex(UT_sint32 controlId) const
244 {
245 	_assertValidDlgHandle(m_hDlg);
246 	return SendDlgItemMessageW(m_hDlg, controlId, LB_GETCURSEL, 0, 0);
247 }
248 
selectListItem(UT_sint32 controlId,int index)249 void XAP_Win32DialogHelper::selectListItem(UT_sint32 controlId, int index)
250 {
251 	_assertValidDlgHandle(m_hDlg);
252 	SendDlgItemMessageW(m_hDlg, controlId, LB_SETCURSEL, index, 0);
253 }
254 
getListText(UT_sint32 controlId,int index,char * p_str) const255 void XAP_Win32DialogHelper::getListText(UT_sint32 controlId, int index, char *p_str) const
256 {
257 	_assertValidDlgHandle(m_hDlg);
258 	SendDlgItemMessageW(m_hDlg, controlId, LB_GETTEXT, index, (LPARAM)p_str);
259 }
260 
261 
262 // Controls
setControlText(UT_sint32 controlId,LPCSTR p_str)263 void XAP_Win32DialogHelper::setControlText(UT_sint32 controlId, LPCSTR p_str)
264 {
265 	_assertValidDlgHandle(m_hDlg);
266 	UT_Win32LocaleString str;
267 	str.fromUTF8(p_str);
268 	SetDlgItemTextW(m_hDlg, controlId, str.c_str());
269 }
270 
setControlInt(UT_sint32 controlId,int value)271 void XAP_Win32DialogHelper::setControlInt(UT_sint32 controlId, int value)
272 {
273 	_assertValidDlgHandle(m_hDlg);
274 	SetDlgItemInt(m_hDlg, controlId, value, TRUE);
275 }
276 
getControlInt(UT_sint32 controlId) const277 int XAP_Win32DialogHelper::getControlInt(UT_sint32 controlId) const
278 {
279 	_assertValidDlgHandle(m_hDlg);
280 	return GetDlgItemInt(m_hDlg, controlId, NULL, FALSE);
281 }
282 
selectControlText(UT_sint32 controlId,UT_sint32 start,UT_sint32 end)283 void XAP_Win32DialogHelper::selectControlText(UT_sint32 controlId, UT_sint32 start, UT_sint32 end)
284 {
285 	_assertValidDlgHandle(m_hDlg);
286 	SendDlgItemMessageW(m_hDlg, controlId, EM_SETSEL, start,end);
287 }
288 
isChecked(UT_sint32 controlId) const289 int XAP_Win32DialogHelper::isChecked(UT_sint32 controlId) const
290 {
291 	_assertValidDlgHandle(m_hDlg);
292 	return IsDlgButtonChecked(m_hDlg, controlId);
293 }
294 
getControlText(UT_sint32 controlId,LPSTR p_buffer,UT_sint32 Buffer_length) const295 void XAP_Win32DialogHelper::getControlText(	UT_sint32 controlId,
296 											LPSTR p_buffer,
297 											UT_sint32 Buffer_length) const
298 {
299 	LPWSTR buf;
300 	UINT   len;
301 	UT_Win32LocaleString str;
302 
303 	_assertValidDlgHandle(m_hDlg);
304 	len=GetWindowTextLengthW(GetDlgItem(m_hDlg, controlId));
305 	buf=g_new(WCHAR,len+1);
306 	GetDlgItemTextW(m_hDlg, controlId, buf, len+1);
307 	str.fromLocale(buf);
308 	FREEP(buf);
309 	// FIXME: validation
310 	strncpy(p_buffer,str.utf8_str().utf8_str(),Buffer_length);
311 }
312 
isControlVisible(UT_sint32 controlId) const313 bool XAP_Win32DialogHelper::isControlVisible(UT_sint32 controlId) const
314 {
315 	_assertValidDlgHandle(m_hDlg);
316 	HWND hControl = GetDlgItem(m_hDlg, controlId);
317 	if (hControl) {
318 		return (GetWindowLongPtrW(m_hDlg, GWL_STYLE) & WS_VISIBLE) ?
319 				true : false;
320 	}
321 	return false;
322 }
323 
isParentFrame(XAP_Frame & frame) const324 bool XAP_Win32DialogHelper::isParentFrame(/*const*/ XAP_Frame& frame) const
325 {
326 	_assertValidDlgHandle(m_hDlg);
327 	XAP_FrameImpl *pFrameImpl = frame.getFrameImpl();
328 	return ((HWND)GetWindowLongPtrW(m_hDlg, GWLP_HWNDPARENT) ==
329 		static_cast<XAP_Win32FrameImpl *>(pFrameImpl)->getTopLevelWindow()) ? true : false;
330 }
331 
setParentFrame(const XAP_Frame * pFrame)332 void XAP_Win32DialogHelper::setParentFrame(const XAP_Frame* pFrame)
333 {
334 	_assertValidDlgHandle(m_hDlg);
335 	SetWindowLongPtrW(m_hDlg, GWLP_HWNDPARENT, (LONG_PTR)(pFrame ? static_cast<XAP_Win32FrameImpl*>(pFrame->getFrameImpl())->getTopLevelWindow() : NULL));
336 	SetWindowPos(m_hDlg, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
337 }
338 
339 
getParentFrame()340 XAP_Frame* XAP_Win32DialogHelper::getParentFrame()
341 {
342 	_assertValidDlgHandle(m_hDlg);
343 	return reinterpret_cast<XAP_Frame*>(
344 			GetWindowLongPtrW(m_hDlg, GWLP_HWNDPARENT));
345 }
346 
centerDialog()347 void XAP_Win32DialogHelper::centerDialog()
348 {
349 	_assertValidDlgHandle(m_hDlg);
350 
351 	s_centerDialog(m_hDlg);
352 }
353 
s_centerDialog(HWND hWnd)354 void XAP_Win32DialogHelper::s_centerDialog(HWND hWnd)
355 {
356 	RECT 	rc, rcParent;
357 	int 	nWidth, nHeight;
358 	POINT 	pt;
359 
360     GetWindowRect(hWnd, &rc);
361 
362    	if (!GetParent(hWnd))
363 	  GetWindowRect (GetDesktopWindow(), &rcParent);
364 	else
365 	  GetClientRect (GetParent(hWnd), &rcParent);
366 
367 	nWidth = rc.right - rc.left;
368 	nHeight = rc.bottom - rc.top;
369 
370 	pt.x = (rcParent.right - rcParent.left) / 2;
371 	pt.y = (rcParent.bottom - rcParent.top) / 2;
372 
373 	if (!GetParent(hWnd))
374 	  ClientToScreen (GetDesktopWindow(), &pt);
375 	else
376 	  ClientToScreen (GetParent(hWnd), &pt);
377 
378 	pt.x -= nWidth / 2;
379 	pt.y -= nHeight / 2;
380 
381 	// Move your arse...
382 	MoveWindow (hWnd, pt.x, pt.y, nWidth, nHeight, TRUE);
383 }
384 
s_loadBitmap(HWND hWnd,UINT nId,const char * pName,int width,int height,const UT_RGBColor & color)385 HBITMAP XAP_Win32DialogHelper::s_loadBitmap(HWND hWnd, UINT nId,
386 					     const char* pName,
387 					     int width, int height,
388 					     const UT_RGBColor & color)
389 {
390 	HBITMAP hBitmap = NULL;
391 
392 	XAP_Win32Toolbar_Icons::getBitmapForIcon(hWnd, width,height, &color,	pName,	&hBitmap);
393 	SendDlgItemMessageW(hWnd,  nId,  BM_SETIMAGE,  IMAGE_BITMAP, (LPARAM) hBitmap);
394 	return hBitmap;
395 }
396