/[gpgoe]/trunk/src/OEMisc.c
ViewVC logotype

Contents of /trunk/src/OEMisc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (show annotations)
Sat Aug 18 10:55:14 2007 UTC (17 years, 8 months ago) by twoaday
File MIME type: text/plain
File size: 6123 byte(s)


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26