30 |
#include "GPGOE.h" |
#include "GPGOE.h" |
31 |
|
|
32 |
|
|
33 |
|
/* Convert the string from the native code page to UTF8. */ |
34 |
|
char* |
35 |
|
native_to_utf8 (const char *string) |
36 |
|
{ |
37 |
|
wchar_t *result; |
38 |
|
char *native; |
39 |
|
int n; |
40 |
|
|
41 |
|
n = MultiByteToWideChar (GetACP (), 0, string, -1, NULL, 0); |
42 |
|
if (n < 0) |
43 |
|
return NULL; |
44 |
|
|
45 |
|
result = (wchar_t*)xcalloc (1, (n+1) * sizeof *result); |
46 |
|
|
47 |
|
n = MultiByteToWideChar (GetACP (), 0, string, -1, result, n); |
48 |
|
if (n < 0) { |
49 |
|
free (result); |
50 |
|
return NULL; |
51 |
|
} |
52 |
|
|
53 |
|
n = WideCharToMultiByte (CP_UTF8, 0, result, -1, NULL, 0, NULL, NULL); |
54 |
|
if (n < 0) |
55 |
|
return NULL; |
56 |
|
|
57 |
|
native = (char*)xcalloc (1, n+1); |
58 |
|
|
59 |
|
n = WideCharToMultiByte (CP_UTF8, 0, result, -1, native, n, NULL, NULL); |
60 |
|
if (n < 0) { |
61 |
|
free (result); |
62 |
|
return NULL; |
63 |
|
} |
64 |
|
|
65 |
|
free (result); |
66 |
|
return native; |
67 |
|
} |
68 |
|
|
69 |
|
|
70 |
/* Convert the UTF8 string @string into the native charset. */ |
/* Convert the UTF8 string @string into the native charset. */ |
71 |
char* |
char* |
72 |
utf8_to_native (const char *string) |
utf8_to_native (const char *string) |
145 |
set_clip_text (HWND hwnd, const char *text, int nbytes) |
set_clip_text (HWND hwnd, const char *text, int nbytes) |
146 |
{ |
{ |
147 |
HANDLE clipmem; |
HANDLE clipmem; |
|
int rc = 0; |
|
148 |
char *p; |
char *p; |
149 |
|
|
150 |
if (OpenClipboard (hwnd) == FALSE) |
if (OpenClipboard (hwnd) == FALSE) |
156 |
abort (); |
abort (); |
157 |
p = (char *) GlobalLock (clipmem); |
p = (char *) GlobalLock (clipmem); |
158 |
if (p == NULL) { |
if (p == NULL) { |
159 |
rc = -1; |
CloseClipboard (); |
160 |
goto leave; |
GlobalFree (clipmem); |
161 |
|
return -1; |
162 |
} |
} |
163 |
memcpy (p, text, nbytes); |
memcpy (p, text, nbytes); |
164 |
p[nbytes] = '\0'; |
p[nbytes] = '\0'; |
165 |
|
|
|
GlobalUnlock (clipmem); |
|
166 |
SetClipboardData (CF_TEXT, clipmem); |
SetClipboardData (CF_TEXT, clipmem); |
167 |
GlobalFree (clipmem); |
GlobalUnlock (clipmem); |
|
|
|
|
leave: |
|
168 |
CloseClipboard (); |
CloseClipboard (); |
169 |
|
GlobalFree (clipmem); |
170 |
|
|
171 |
return 0; |
return 0; |
172 |
} |
} |
173 |
|
|
194 |
} |
} |
195 |
|
|
196 |
|
|
197 |
|
void |
198 |
|
xfree (void *p) |
199 |
|
{ |
200 |
|
if (p != NULL) |
201 |
|
free (p); |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
/* printf style message box. */ |
/* printf style message box. */ |
206 |
void |
void |
207 |
show_error (HWND hwnd, const char *caption, UINT type, const char *fmt, ...) |
show_error (HWND hwnd, const char *caption, UINT type, const char *fmt, ...) |
214 |
va_end (ptr); |
va_end (ptr); |
215 |
MessageBox (hwnd, buffer, caption, type); |
MessageBox (hwnd, buffer, caption, type); |
216 |
} |
} |
217 |
|
|
218 |
|
|
219 |
|
/* Prepend '> ' to line line in the buffer @inp |
220 |
|
and store the result in @r_outp. */ |
221 |
|
void |
222 |
|
quote_msg_text (const char *inp, char **r_outp) |
223 |
|
{ |
224 |
|
size_t i, n=0; |
225 |
|
char *p; |
226 |
|
char *outp; |
227 |
|
|
228 |
|
for (i=0; i < strlen (inp); i++) { |
229 |
|
if (inp[i] == '\r') |
230 |
|
n += 4; |
231 |
|
} |
232 |
|
outp = xcalloc (1, strlen (inp) + 1 + n + 1); |
233 |
|
p = strtok ((char *)inp, "\r"); |
234 |
|
while (p != NULL) { |
235 |
|
if (*p == '\n') |
236 |
|
p++; |
237 |
|
strcat (outp, "> "); |
238 |
|
strcat (outp, p); |
239 |
|
strcat (outp, "\r\n"); |
240 |
|
p = strtok (NULL, "\r"); |
241 |
|
} |
242 |
|
*r_outp = outp; |
243 |
|
} |
244 |
|
|
245 |
|
|
246 |
|
/* Center the window @hwndChild over the parent window @parent. */ |
247 |
|
void |
248 |
|
center_window (HWND hwndChild, HWND parent) |
249 |
|
{ |
250 |
|
HWND hwndParent; |
251 |
|
RECT rChild, rParent; |
252 |
|
HDC hdc; |
253 |
|
int wChild, hChild, wParent, hParent; |
254 |
|
int wScreen, hScreen, xNew, yNew; |
255 |
|
int flags = SWP_NOSIZE | SWP_NOZORDER; |
256 |
|
|
257 |
|
hwndParent = parent; |
258 |
|
if (hwndParent == NULL) |
259 |
|
hwndParent = GetDesktopWindow (); |
260 |
|
GetWindowRect (hwndChild, &rChild); |
261 |
|
wChild = rChild.right - rChild.left; |
262 |
|
hChild = rChild.bottom - rChild.top; |
263 |
|
|
264 |
|
GetWindowRect (hwndParent, &rParent); |
265 |
|
wParent = rParent.right - rParent.left; |
266 |
|
hParent = rParent.bottom - rParent.top; |
267 |
|
|
268 |
|
hdc = GetDC (hwndChild); |
269 |
|
wScreen = GetDeviceCaps (hdc, HORZRES); |
270 |
|
hScreen = GetDeviceCaps (hdc, VERTRES); |
271 |
|
ReleaseDC (hwndChild, hdc); |
272 |
|
xNew = rParent.left + ((wParent - wChild) /2); |
273 |
|
if (xNew < 0) |
274 |
|
xNew = 0; |
275 |
|
else if ((xNew+wChild) > wScreen) |
276 |
|
xNew = wScreen - wChild; |
277 |
|
yNew = rParent.top + ((hParent - hChild) /2); |
278 |
|
if (yNew < 0) |
279 |
|
yNew = 0; |
280 |
|
else if ((yNew+hChild) > hScreen) |
281 |
|
yNew = hScreen - hChild; |
282 |
|
SetWindowPos (hwndChild, NULL, xNew, yNew, 0, 0, flags); |
283 |
|
} |