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

Annotation of /trunk/src/OEMisc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 16 - (hide annotations)
Tue Apr 11 06:56:23 2006 UTC (19 years ago) by twoaday
File MIME type: text/plain
File size: 6153 byte(s)


1 twoaday 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 twoaday 16 /* Convert the string from the native code page to UTF8. */
34 twoaday 12 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 twoaday 1 /* 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    
82 twoaday 10 result = (wchar_t*)xcalloc (1, (n+1) * sizeof *result);
83 twoaday 1
84     n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n);
85     if (n < 0) {
86     free (result);
87     return NULL;
88     }
89    
90     n = WideCharToMultiByte (GetACP (), 0, result, -1, NULL, 0, NULL, NULL);
91     if (n < 0)
92     return NULL;
93    
94     native = (char*)malloc (n+1);
95     if (!native)
96     abort ();
97    
98     n = WideCharToMultiByte (GetACP (), 0, result, -1, native, n, NULL, NULL);
99     if (n < 0) {
100     free (result);
101     return NULL;
102     }
103    
104     free (result);
105     return native;
106     }
107    
108    
109     /* Return the clipboard contents as a string or NULL
110     if the clipboard does not contain text. */
111     char*
112     get_clip_text (HWND hwnd)
113     {
114     HANDLE clipmem;
115     char *cliptxt, *p;
116     int len;
117    
118     if (OpenClipboard (hwnd) == FALSE)
119     return NULL;
120     clipmem = GetClipboardData (CF_TEXT);
121     if (clipmem == NULL) {
122     p = NULL;
123     goto leave;
124     }
125     cliptxt = (char *) GlobalLock (clipmem);
126     if (cliptxt == NULL) {
127     p = NULL;
128     goto leave;
129     }
130    
131     len = strlen (cliptxt);
132     p = xcalloc (1, len+1);
133     memcpy (p, cliptxt, len);
134     p[len] = '\0';
135     GlobalUnlock (clipmem);
136    
137     leave:
138     CloseClipboard ();
139     return p;
140     }
141    
142    
143     /* Set @text as the new clipboard content. */
144     int
145     set_clip_text (HWND hwnd, const char *text, int nbytes)
146     {
147     HANDLE clipmem;
148     char *p;
149    
150     if (OpenClipboard (hwnd) == FALSE)
151     return -1;
152     EmptyClipboard ();
153    
154     clipmem = GlobalAlloc (GHND, nbytes + 1);
155     if (clipmem == NULL)
156     abort ();
157     p = (char *) GlobalLock (clipmem);
158     if (p == NULL) {
159 twoaday 12 CloseClipboard ();
160     GlobalFree (clipmem);
161     return -1;
162 twoaday 1 }
163     memcpy (p, text, nbytes);
164     p[nbytes] = '\0';
165    
166 twoaday 12 SetClipboardData (CF_TEXT, clipmem);
167 twoaday 1 GlobalUnlock (clipmem);
168 twoaday 12 CloseClipboard ();
169 twoaday 16 GlobalFree (clipmem);
170 twoaday 1
171     return 0;
172     }
173    
174    
175     /* Safe wrapper for calloc. */
176     void*
177     xcalloc (size_t n, size_t m)
178     {
179     void *p = calloc (n, m);
180     if (!p)
181     abort ();
182     return p;
183     }
184    
185    
186     /* Safe wrapper for strdup. */
187     char*
188     xstrdup (const char *s)
189     {
190     char *p = strdup (s);
191     if (!p)
192     abort ();
193     return p;
194     }
195    
196    
197 twoaday 12 void
198     xfree (void *p)
199     {
200     if (p != NULL)
201     free (p);
202     }
203    
204    
205 twoaday 1 /* printf style message box. */
206     void
207     show_error (HWND hwnd, const char *caption, UINT type, const char *fmt, ...)
208     {
209     char buffer[1024];
210     va_list ptr;
211    
212     va_start (ptr, fmt);
213     _vsnprintf (buffer, sizeof (buffer)-1, fmt, ptr);
214     va_end (ptr);
215     MessageBox (hwnd, buffer, caption, type);
216     }
217 twoaday 16
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     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26