1 |
/* OEMisc.c - OE misc functions |
2 |
* Copyright (C) 2006 Timo Schulz |
3 |
* |
4 |
* This file is part of GPGOE. |
5 |
* |
6 |
* GPGOE is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU Lesser General Public License as published by |
8 |
* the Free Software Foundation; either version 2.1 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* GPGOE 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 |
|
17 |
#ifdef HAVE_CONFIG_H |
18 |
#include <config.h> |
19 |
#endif |
20 |
#include <windows.h> |
21 |
#include <stdio.h> |
22 |
#include <stdarg.h> |
23 |
#include <stdlib.h> |
24 |
|
25 |
#include "gpgme.h" |
26 |
#include "GPGOE.h" |
27 |
|
28 |
|
29 |
/* Convert the string from the native code page to UTF8. */ |
30 |
char* |
31 |
native_to_utf8 (const char *string) |
32 |
{ |
33 |
wchar_t *result; |
34 |
char *native; |
35 |
int n; |
36 |
|
37 |
n = MultiByteToWideChar (GetACP (), 0, string, -1, NULL, 0); |
38 |
if (n < 0) |
39 |
return NULL; |
40 |
|
41 |
result = (wchar_t*)xcalloc (1, (n+1) * sizeof *result); |
42 |
|
43 |
n = MultiByteToWideChar (GetACP (), 0, string, -1, result, n); |
44 |
if (n < 0) { |
45 |
xfree (result); |
46 |
return NULL; |
47 |
} |
48 |
|
49 |
n = WideCharToMultiByte (CP_UTF8, 0, result, -1, NULL, 0, NULL, NULL); |
50 |
if (n < 0) |
51 |
return NULL; |
52 |
|
53 |
native = (char*)xcalloc (1, n+1); |
54 |
|
55 |
n = WideCharToMultiByte (CP_UTF8, 0, result, -1, native, n, NULL, NULL); |
56 |
if (n < 0) { |
57 |
xfree (result); |
58 |
return NULL; |
59 |
} |
60 |
|
61 |
xfree (result); |
62 |
return native; |
63 |
} |
64 |
|
65 |
|
66 |
/* Convert the UTF8 string @string into the native charset. */ |
67 |
char* |
68 |
utf8_to_native (const char *string) |
69 |
{ |
70 |
wchar_t *result; |
71 |
char *native; |
72 |
int n; |
73 |
|
74 |
n = MultiByteToWideChar (CP_UTF8, 0, string, -1, NULL, 0); |
75 |
if (n < 0) |
76 |
return NULL; |
77 |
result = (wchar_t*)xcalloc (1, (n+1) * sizeof *result); |
78 |
n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n); |
79 |
if (n < 0) { |
80 |
xfree (result); |
81 |
return NULL; |
82 |
} |
83 |
|
84 |
n = WideCharToMultiByte (GetACP (), 0, result, -1, NULL, 0, NULL, NULL); |
85 |
if (n < 0) |
86 |
return NULL; |
87 |
native = (char*)xcalloc (1, n+1); |
88 |
n = WideCharToMultiByte (GetACP (), 0, result, -1, native, n, NULL, NULL); |
89 |
if (n < 0) { |
90 |
xfree (result); |
91 |
return NULL; |
92 |
} |
93 |
|
94 |
xfree (result); |
95 |
return native; |
96 |
} |
97 |
|
98 |
|
99 |
/* Return the clipboard contents as a string or NULL |
100 |
if the clipboard does not contain text. */ |
101 |
char* |
102 |
get_clip_text (HWND hwnd) |
103 |
{ |
104 |
HANDLE clipmem; |
105 |
char *cliptxt, *p; |
106 |
int len; |
107 |
|
108 |
if (OpenClipboard (hwnd) == FALSE) |
109 |
return NULL; |
110 |
clipmem = GetClipboardData (CF_TEXT); |
111 |
if (clipmem == NULL) { |
112 |
p = NULL; |
113 |
goto leave; |
114 |
} |
115 |
cliptxt = (char *) GlobalLock (clipmem); |
116 |
if (cliptxt == NULL) { |
117 |
p = NULL; |
118 |
goto leave; |
119 |
} |
120 |
|
121 |
len = strlen (cliptxt); |
122 |
p = xcalloc (1, len+1); |
123 |
memcpy (p, cliptxt, len); |
124 |
p[len] = '\0'; |
125 |
GlobalUnlock (clipmem); |
126 |
|
127 |
leave: |
128 |
CloseClipboard (); |
129 |
return p; |
130 |
} |
131 |
|
132 |
|
133 |
/* Set @text as the new clipboard content. */ |
134 |
int |
135 |
set_clip_text (HWND hwnd, const char *text, int nbytes) |
136 |
{ |
137 |
HANDLE clipmem; |
138 |
char *p; |
139 |
|
140 |
if (OpenClipboard (hwnd) == FALSE) |
141 |
return -1; |
142 |
EmptyClipboard (); |
143 |
|
144 |
clipmem = GlobalAlloc (GHND, nbytes + 1); |
145 |
if (clipmem == NULL) |
146 |
abort (); |
147 |
p = (char *) GlobalLock (clipmem); |
148 |
if (p == NULL) { |
149 |
CloseClipboard (); |
150 |
GlobalFree (clipmem); |
151 |
return -1; |
152 |
} |
153 |
memcpy (p, text, nbytes); |
154 |
p[nbytes] = '\0'; |
155 |
|
156 |
SetClipboardData (CF_TEXT, clipmem); |
157 |
GlobalUnlock (clipmem); |
158 |
CloseClipboard (); |
159 |
GlobalFree (clipmem); |
160 |
|
161 |
return 0; |
162 |
} |
163 |
|
164 |
|
165 |
/* Safe wrapper for calloc. */ |
166 |
void* |
167 |
xcalloc (size_t n, size_t m) |
168 |
{ |
169 |
void *p = calloc (n, m); |
170 |
if (!p) |
171 |
abort (); |
172 |
return p; |
173 |
} |
174 |
|
175 |
|
176 |
/* Safe wrapper for strdup. */ |
177 |
char* |
178 |
xstrdup (const char *s) |
179 |
{ |
180 |
char *p = strdup (s); |
181 |
if (!p) |
182 |
abort (); |
183 |
return p; |
184 |
} |
185 |
|
186 |
|
187 |
void |
188 |
xfree (void *p) |
189 |
{ |
190 |
if (p != NULL) |
191 |
free (p); |
192 |
} |
193 |
|
194 |
|
195 |
/* printf style message box. */ |
196 |
void |
197 |
show_error (HWND hwnd, const char *caption, UINT type, const char *fmt, ...) |
198 |
{ |
199 |
char buffer[1024]; |
200 |
va_list ptr; |
201 |
|
202 |
va_start (ptr, fmt); |
203 |
_vsnprintf (buffer, DIM (buffer)-1, fmt, ptr); |
204 |
va_end (ptr); |
205 |
MessageBox (hwnd, buffer, caption, type); |
206 |
} |
207 |
|
208 |
|
209 |
/* Prepend '> ' to line line in the buffer @inp |
210 |
and store the result in @r_outp. */ |
211 |
void |
212 |
quote_msg_text (const char *inp, char **r_outp) |
213 |
{ |
214 |
size_t i, n=0; |
215 |
char *p; |
216 |
char *outp; |
217 |
|
218 |
for (i=0; i < strlen (inp); i++) { |
219 |
if (inp[i] == '\r') |
220 |
n += 4; |
221 |
} |
222 |
outp = xcalloc (1, strlen (inp) + 1 + n + 1); |
223 |
p = strtok ((char *)inp, "\r"); |
224 |
while (p != NULL) { |
225 |
if (*p == '\n') |
226 |
p++; |
227 |
strcat (outp, "> "); |
228 |
strcat (outp, p); |
229 |
strcat (outp, "\r\n"); |
230 |
p = strtok (NULL, "\r"); |
231 |
} |
232 |
*r_outp = outp; |
233 |
} |
234 |
|
235 |
|
236 |
/* Center the window @hwndChild over the parent window @parent. */ |
237 |
void |
238 |
center_window (HWND hwndChild, HWND parent) |
239 |
{ |
240 |
HWND hwndParent; |
241 |
RECT rChild, rParent; |
242 |
HDC hdc; |
243 |
int wChild, hChild, wParent, hParent; |
244 |
int wScreen, hScreen, xNew, yNew; |
245 |
int flags = SWP_NOSIZE | SWP_NOZORDER; |
246 |
|
247 |
hwndParent = parent; |
248 |
if (hwndParent == NULL) |
249 |
hwndParent = GetDesktopWindow (); |
250 |
GetWindowRect (hwndChild, &rChild); |
251 |
wChild = rChild.right - rChild.left; |
252 |
hChild = rChild.bottom - rChild.top; |
253 |
|
254 |
GetWindowRect (hwndParent, &rParent); |
255 |
wParent = rParent.right - rParent.left; |
256 |
hParent = rParent.bottom - rParent.top; |
257 |
|
258 |
hdc = GetDC (hwndChild); |
259 |
wScreen = GetDeviceCaps (hdc, HORZRES); |
260 |
hScreen = GetDeviceCaps (hdc, VERTRES); |
261 |
ReleaseDC (hwndChild, hdc); |
262 |
xNew = rParent.left + ((wParent - wChild) /2); |
263 |
if (xNew < 0) |
264 |
xNew = 0; |
265 |
else if ((xNew+wChild) > wScreen) |
266 |
xNew = wScreen - wChild; |
267 |
yNew = rParent.top + ((hParent - hChild) /2); |
268 |
if (yNew < 0) |
269 |
yNew = 0; |
270 |
else if ((yNew+hChild) > hScreen) |
271 |
yNew = hScreen - hChild; |
272 |
SetWindowPos (hwndChild, NULL, xNew, yNew, 0, 0, flags); |
273 |
} |