tunicodewrap.c - vaccinewars - be a doctor and try to vaccinate the world
HTML git clone git://src.adamsgaard.dk/vaccinewars
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
tunicodewrap.c (9104B)
---
1 /************************************************************************
2 * unicodewrap.c Unicode wrapper functions for Win32 *
3 * Copyright (C) 2002-2004 Ben Webb *
4 * Email: benwebb@users.sf.net *
5 * WWW: https://dopewars.sourceforge.io/ *
6 * *
7 * This program is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU General Public License *
9 * as published by the Free Software Foundation; either version 2 *
10 * of the License, or (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, *
20 * MA 02111-1307, USA. *
21 ************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #ifdef CYGWIN
28 #include <winsock2.h>
29 #include <windows.h>
30 #include <glib.h>
31
32 #include "unicodewrap.h"
33
34 /*
35 * Converts a string from our internal representation (UTF-8) to a form
36 * suitable for Windows Unicode-aware functions (i.e. UTF-16). This
37 * returned string must be g_free'd when no longer needed.
38 */
39 gunichar2 *strtow32(const char *instr, int len)
40 {
41 gunichar2 *outstr;
42 if (!instr) {
43 return NULL;
44 }
45 outstr = g_utf8_to_utf16(instr, len, NULL, NULL, NULL);
46 if (!outstr) {
47 outstr = g_utf8_to_utf16("[?]", len, NULL, NULL, NULL);
48 }
49 return outstr;
50 }
51
52 gchar *w32tostr(const gunichar2 *instr, int len)
53 {
54 gchar *outstr;
55 if (!instr) {
56 return NULL;
57 }
58 outstr = g_utf16_to_utf8(instr, len, NULL, NULL, NULL);
59 if (!outstr) {
60 outstr = g_strdup("[?]");
61 }
62 return outstr;
63 }
64
65 BOOL mySetWindowText(HWND hWnd, LPCTSTR lpString)
66 {
67 BOOL retval;
68 gunichar2 *text;
69 text = strtow32(lpString, -1);
70 retval = SetWindowTextW(hWnd, text);
71 g_free(text);
72 return retval;
73 }
74
75 HWND myCreateWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle,
76 int x, int y, int nWidth, int nHeight, HWND hwndParent,
77 HMENU hMenu, HANDLE hInstance, LPVOID lpParam)
78 {
79 return myCreateWindowEx(0, lpClassName, lpWindowName, dwStyle, x, y,
80 nWidth, nHeight, hwndParent, hMenu, hInstance,
81 lpParam);
82 }
83
84 HWND myCreateWindowEx(DWORD dwExStyle, LPCTSTR lpClassName,
85 LPCTSTR lpWindowName, DWORD dwStyle, int x, int y,
86 int nWidth, int nHeight, HWND hwndParent, HMENU hMenu,
87 HANDLE hInstance, LPVOID lpParam)
88 {
89 HWND retval;
90 gunichar2 *classname, *winname;
91 classname = strtow32(lpClassName, -1);
92 winname = strtow32(lpWindowName, -1);
93 retval = CreateWindowExW(dwExStyle, classname, winname, dwStyle, x, y,
94 nWidth, nHeight, hwndParent, hMenu, hInstance,
95 lpParam);
96 g_free(classname);
97 g_free(winname);
98 return retval;
99 }
100
101 gchar *myGetWindowText(HWND hWnd)
102 {
103 gint textlen;
104 gunichar2 *buffer;
105 gchar *retstr;
106
107 textlen = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0);
108
109 buffer = g_new0(gunichar2, textlen + 1);
110 GetWindowTextW(hWnd, buffer, textlen + 1);
111 buffer[textlen] = '\0';
112 retstr = w32tostr(buffer, textlen);
113 g_free(buffer);
114 return retstr;
115 }
116
117 int myDrawText(HDC hDC, LPCTSTR lpString, int nCount, LPRECT lpRect,
118 UINT uFormat)
119 {
120 int retval;
121 gunichar2 *text;
122
123 text = strtow32(lpString, nCount);
124 retval = DrawTextW(hDC, text, -1, lpRect, uFormat);
125 g_free(text);
126 return retval;
127 }
128
129 static BOOL makeMenuItemInfoW(LPMENUITEMINFOW lpmiiw, LPMENUITEMINFO lpmii)
130 {
131 BOOL strdata;
132 strdata = (lpmii->fMask & MIIM_TYPE)
133 && !(lpmii->fType & (MFT_BITMAP | MFT_SEPARATOR));
134
135 lpmiiw->cbSize = sizeof(MENUITEMINFOW);
136 lpmiiw->fMask = lpmii->fMask;
137 lpmiiw->fType = lpmii->fType;
138 lpmiiw->fState = lpmii->fState;
139 lpmiiw->wID = lpmii->wID;
140 lpmiiw->hSubMenu = lpmii->hSubMenu;
141 lpmiiw->hbmpChecked = lpmii->hbmpChecked;
142 lpmiiw->hbmpUnchecked = lpmii->hbmpUnchecked;
143 lpmiiw->dwItemData = lpmii->dwItemData;
144 lpmiiw->dwTypeData = strdata ? strtow32(lpmii->dwTypeData, -1)
145 : (LPWSTR)lpmii->dwTypeData;
146 lpmiiw->cch = lpmii->cch;
147 return strdata;
148 }
149
150 BOOL WINAPI mySetMenuItemInfo(HMENU hMenu, UINT uItem, BOOL fByPosition,
151 LPMENUITEMINFO lpmii)
152 {
153 BOOL retval;
154 MENUITEMINFOW miiw;
155 BOOL strdata;
156
157 strdata = makeMenuItemInfoW(&miiw, lpmii);
158 retval = SetMenuItemInfoW(hMenu, uItem, fByPosition, &miiw);
159 if (strdata) {
160 g_free(miiw.dwTypeData);
161 }
162 return retval;
163 }
164
165 BOOL WINAPI myInsertMenuItem(HMENU hMenu, UINT uItem, BOOL fByPosition,
166 LPMENUITEMINFO lpmii)
167 {
168 BOOL retval;
169 MENUITEMINFOW miiw;
170 BOOL strdata;
171
172 strdata = makeMenuItemInfoW(&miiw, lpmii);
173 retval = InsertMenuItemW(hMenu, uItem, fByPosition, &miiw);
174 if (strdata) {
175 g_free(miiw.dwTypeData);
176 }
177 return retval;
178 }
179
180 static BOOL makeHeaderItemW(HD_ITEMW *phdiw, const HD_ITEM *phdi)
181 {
182 BOOL strdata;
183
184 strdata = phdi->mask & HDI_TEXT;
185
186 phdiw->mask = phdi->mask;
187 phdiw->cxy = phdi->cxy;
188 phdiw->pszText = strdata ? strtow32(phdi->pszText, -1)
189 : (LPWSTR)phdi->pszText;
190 phdiw->hbm = phdi->hbm;
191 phdiw->cchTextMax = phdi->cchTextMax;
192 phdiw->fmt = phdi->fmt;
193 phdiw->lParam = phdi->lParam;
194 return strdata;
195 }
196
197 static BOOL makeTabItemW(TC_ITEMW *tiew, const TC_ITEM *tie)
198 {
199 BOOL strdata;
200
201 strdata = tie->mask & TCIF_TEXT;
202 tiew->mask = tie->mask;
203 tiew->pszText = strdata ? strtow32(tie->pszText, -1)
204 : (LPWSTR)tie->pszText;
205 tiew->cchTextMax = tie->cchTextMax;
206 tiew->iImage = tie->iImage;
207 tiew->lParam = tie->lParam;
208 return strdata;
209 }
210
211 int myHeader_InsertItem(HWND hWnd, int index, const HD_ITEM *phdi)
212 {
213 int retval;
214 if (IsWindowUnicode(hWnd)) {
215 HD_ITEMW hdiw;
216 BOOL strdata;
217
218 strdata = makeHeaderItemW(&hdiw, phdi);
219 retval = (int)SendMessageW(hWnd, HDM_INSERTITEMW, (WPARAM)index,
220 (LPARAM)&hdiw);
221 if (strdata) {
222 g_free(hdiw.pszText);
223 }
224 } else {
225 retval = (int)SendMessageA(hWnd, HDM_INSERTITEMA, (WPARAM)index,
226 (LPARAM)phdi);
227 }
228 return retval;
229 }
230
231 int myTabCtrl_InsertItem(HWND hWnd, int index, const TC_ITEM *pitem)
232 {
233 int retval;
234 TC_ITEMW tiew;
235 BOOL strdata;
236 strdata = makeTabItemW(&tiew, pitem);
237 retval = (int)SendMessageW(hWnd, TCM_INSERTITEMW, (WPARAM)index,
238 (LPARAM)&tiew);
239 if (strdata) {
240 g_free(tiew.pszText);
241 }
242 return retval;
243 }
244
245 ATOM myRegisterClass(CONST WNDCLASS *lpWndClass)
246 {
247 ATOM retval;
248 WNDCLASSW wcw;
249
250 wcw.style = lpWndClass->style;
251 wcw.lpfnWndProc = lpWndClass->lpfnWndProc;
252 wcw.cbClsExtra = lpWndClass->cbClsExtra;
253 wcw.cbWndExtra = lpWndClass->cbWndExtra;
254 wcw.hInstance = lpWndClass->hInstance;
255 wcw.hIcon = lpWndClass->hIcon;
256 wcw.hCursor = lpWndClass->hCursor;
257 wcw.hbrBackground = lpWndClass->hbrBackground;
258 wcw.lpszMenuName = strtow32(lpWndClass->lpszMenuName, -1);
259 wcw.lpszClassName = strtow32(lpWndClass->lpszClassName, -1);
260 retval = RegisterClassW(&wcw);
261 g_free((LPWSTR)wcw.lpszMenuName);
262 g_free((LPWSTR)wcw.lpszClassName);
263 return retval;
264 }
265
266 HWND myCreateDialog(HINSTANCE hInstance, LPCTSTR lpTemplate, HWND hWndParent,
267 DLGPROC lpDialogFunc)
268 {
269 HWND retval;
270 gunichar2 *text;
271 text = strtow32(lpTemplate, -1);
272 retval = CreateDialogW(hInstance, text, hWndParent, lpDialogFunc);
273 g_free(text);
274 return retval;
275 }
276
277 void myEditReplaceSel(HWND hWnd, BOOL fCanUndo, LPCSTR lParam)
278 {
279 gunichar2 *text;
280 text = strtow32(lParam, -1);
281 SendMessageW(hWnd, EM_REPLACESEL, (WPARAM)fCanUndo, (LPARAM)text);
282 g_free(text);
283 }
284
285 int myMessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
286 {
287 int retval;
288 gunichar2 *text, *caption;
289 text = strtow32(lpText, -1);
290 caption = strtow32(lpCaption, -1);
291 retval = MessageBoxW(hWnd, text, caption, uType);
292 g_free(text);
293 g_free(caption);
294 return retval;
295 }
296
297 size_t myw32strlen(const char *str)
298 {
299 return g_utf8_strlen(str, -1);
300 }
301
302 LRESULT myComboBox_AddString(HWND hWnd, LPCTSTR text)
303 {
304 LRESULT retval;
305 gunichar2 *w32text;
306 w32text = strtow32(text, -1);
307 retval = SendMessageW(hWnd, CB_ADDSTRING, 0, (LPARAM)w32text);
308 g_free(w32text);
309 return retval;
310 }
311
312 #endif /* CYGWIN */