/[winpt]/trunk/Src/wptW32API.cpp
ViewVC logotype

Contents of /trunk/Src/wptW32API.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (show annotations)
Mon Apr 4 06:59:24 2005 UTC (19 years, 10 months ago) by twoaday
File size: 10346 byte(s)
2005-03-04  Timo Schulz  <twoaday@g10code.com>
 
        * GPG asks twice for the new PIN. Thanks to Achim.
        * wptCardDlg.cpp (card_changepin_dlg_proc): Reset the 'safety' pin also.        Only check the passphrase if the backup flag is enabled. Again thanks to        Achim.
 
2005-03-06  Timo Schulz  <twoaday@freakmail.de>
 
        * wptKeySignDlg.cpp (do_fill_seckeylist): Skip secret keys without
        a public key. Noted by Kurt Fitzner.
 
2005-03-22  Timo Schulz  <twoaday@freakmail.de>
 
        * WinPT.cpp (WinMain): --debug as an alias for --enable-debug.
        (enable_mobile_mode): New.
        * wptKeyEditDlg.cpp (keyedit_addsubkey_dlg_proc): Use new
        ID's for adding subkeys.
 


1 /* wptW32API.cpp - Common W32 API functions
2 * Copyright (C) 2001, 2002, 2003 Timo Schulz
3 *
4 * This file is part of WinPT.
5 *
6 * WinPT is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * WinPT 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 General Public License
17 * along with WinPT; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21 #include <windows.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <shellapi.h>
26 #include <shlobj.h>
27 #include <commctrl.h>
28
29 #include "wptNLS.h"
30 #include "wptW32API.h"
31 #include "wptErrors.h"
32 #include "wptVersion.h"
33 #include "wptTypes.h"
34
35
36 /*
37 * The the text of a menu item.
38 */
39 void
40 set_menu_text (HMENU menu, int m_uid, const char *text)
41 {
42 MENUITEMINFO mii;
43 char menu_text[80];
44
45 memset (&mii, 0, sizeof (mii));
46 mii.cbSize = sizeof (mii);
47 mii.fMask = MIIM_TYPE;
48 mii.dwTypeData = menu_text;
49 mii.cch = sizeof (menu_text);
50 GetMenuItemInfo (menu, m_uid, FALSE, &mii);
51
52 memset (&mii, 0, sizeof mii);
53 mii.cbSize = sizeof mii;
54 mii.fMask = MIIM_TYPE;
55 mii.fType = MFT_STRING;
56 mii.dwTypeData = (char *) text;
57 SetMenuItemInfo (menu, m_uid, FALSE, &mii);
58 } /* set_menu_text */
59
60
61 void
62 set_menu_state (HMENU menu, int m_uid, int state)
63 {
64 MENUITEMINFO mii;
65
66 memset( &mii, 0, sizeof (mii) );
67 mii.cbSize = sizeof (mii);
68 mii.fMask = MIIM_STATE;
69 mii.fState = state;
70 SetMenuItemInfo (menu, m_uid, FALSE, &mii);
71 } /* set_menu_state */
72
73
74
75 const char *
76 get_filename_dlg (HWND hwnd, int id, const char * title,
77 const char * filter, const char * name)
78 {
79 static char file[512] = "";
80 OPENFILENAME open;
81
82 if (name && strlen (name) < (sizeof (file)-1))
83 strcpy (file, name);
84 else
85 memset (file, 0, sizeof (file));
86 if (!filter)
87 filter = _("All Files (*.*)\0*.*");
88 memset (&open, 0, sizeof (open));
89 open.lStructSize = sizeof (OPENFILENAME);
90 open.hInstance = glob_hinst;
91 open.lpstrTitle = title;
92 open.lpstrFilter = filter;
93 open.hwndOwner = hwnd;
94 open.lpstrFile = file;
95 open.nMaxFile = sizeof (file) - 1;
96 open.Flags = 0;
97
98 if (id == 0 && GetOpenFileName (&open))
99 return open.lpstrFile;
100 else if (id == 1 && GetSaveFileName (&open))
101 return open.lpstrFile;
102
103 return NULL;
104 } /* get_filename_dlg */
105
106
107 const char *
108 get_folder_dlg (HWND hwnd, const char * title, const char * name)
109 {
110 static char folder[MAX_PATH] = "";
111 BROWSEINFO bi;
112 ITEMIDLIST * il;
113
114 memset (&bi, 0, sizeof (bi));
115 bi.hwndOwner = hwnd;
116 if (title)
117 bi.lpszTitle = title;
118 if (name && strlen (name) < MAX_PATH-1)
119 strcpy (folder, name);
120 else
121 memset (folder, 0, sizeof (folder));
122 il = SHBrowseForFolder (&bi);
123 if (il) {
124 SHGetPathFromIDList (il, folder);
125 return folder;
126 }
127 return NULL;
128 }
129
130
131 char*
132 get_clip_text (HWND hwnd)
133 {
134 HANDLE clipmem;
135 char *cliptxt, *p;
136 int len;
137
138 if (OpenClipboard (hwnd) == FALSE)
139 return NULL;
140 clipmem = GetClipboardData (CF_TEXT);
141 if (clipmem == NULL) {
142 p = NULL;
143 goto leave;
144 }
145 cliptxt = (char *) GlobalLock (clipmem);
146 if (cliptxt == NULL) {
147 p = NULL;
148 goto leave;
149 }
150
151 len = strlen (cliptxt);
152 p = new char[len + 1];
153 if (!p)
154 BUG (NULL);
155 memcpy (p, cliptxt, len);
156 p[len] = '\0';
157 GlobalUnlock (clipmem);
158
159 leave:
160 CloseClipboard ();
161 return p;
162 } /* get_clip_text */
163
164
165 int
166 set_clip_text (HWND hwnd, const char *text, int nbytes)
167 {
168 HANDLE clipmem;
169 int rc = 0;
170 char *p;
171
172 if (OpenClipboard (hwnd) == FALSE)
173 return WPTERR_CLIP_OPEN;
174 EmptyClipboard ();
175
176 clipmem = GlobalAlloc (GHND, nbytes + 1);
177 if (clipmem == NULL)
178 BUG (NULL);
179 p = (char *) GlobalLock (clipmem);
180 if (p == NULL) {
181 rc = WPTERR_GENERAL;;
182 goto leave;
183 }
184 memcpy (p, text, nbytes);
185 p[nbytes] = '\0';
186
187 GlobalUnlock (clipmem);
188 SetClipboardData (CF_TEXT, clipmem);
189
190 leave:
191 CloseClipboard ();
192 return rc;
193 } /* set_clip_text */
194
195
196 int
197 set_clip_text2 (HWND hwnd, const char *text, int nbytes, int head_foot)
198 {
199 char *p, *new_text;
200
201 p = get_clip_text (hwnd);
202 if (!p)
203 return WPTERR_CLIP_GET;
204 new_text = new char [strlen (p)+strlen (text)+8];
205 if (!new_text)
206 BUG (0);
207 if (head_foot == 0)
208 sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p);
209 else
210 sprintf (new_text, "%s\n%s\n\n", p, text);
211 set_clip_text (hwnd, new_text, strlen (new_text)+1);
212 free_if_alloc (p);
213 free_if_alloc (new_text);
214 return 0;
215 }
216
217
218 char*
219 make_filename( const char *path, const char *file, const char *ext )
220 {
221 char *p;
222 size_t size = 0;
223
224 if( path && *path )
225 size += strlen( path );
226 if( file && *file )
227 size += strlen( file );
228 if( ext && *ext )
229 size += strlen( ext );
230 p = new char[size + 4];
231 memset( p, 0, size );
232 if( path ) {
233 strcat( p, path );
234 if( path[strlen( path ) -1] != '\\' )
235 strcat( p, "\\" );
236 }
237 if( file )
238 strcat( p, file );
239 if( ext ) {
240 strcat( p, "." );
241 strcat( p, ext );
242 }
243 return p;
244 } /* make_filename */
245
246
247 /* return 0 if it exists, otherwise >0. */
248 int
249 file_exist_check (const char * fname)
250 {
251 HANDLE fh;
252
253 fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ,
254 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
255 if( fh == INVALID_HANDLE_VALUE )
256 return WPTERR_FILE_EXIST;
257 CloseHandle( fh );
258 return 0;
259 } /* file_exist_check */
260
261
262 int
263 dir_exist_check( const char *dir )
264 {
265 struct stat statbuf;
266
267 if( stat( dir, &statbuf ) == -1 )
268 return WPTERR_GENERAL;
269 if( statbuf.st_mode & _S_IFDIR )
270 return 0;
271 return WPTERR_GENERAL;
272 } /* dir_exist_check */
273
274
275 size_t
276 get_file_size( const char *fname )
277 {
278 size_t fsize;
279 HANDLE fh;
280
281 fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ,
282 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
283 if( fh == INVALID_HANDLE_VALUE )
284 return 0;
285 fsize = GetFileSize( fh, NULL );
286 if( fsize == 0xFFFFFFFF )
287 fsize = 0;
288 CloseHandle( fh );
289 return fsize;
290 } /* get_file_size */
291
292
293 int
294 init_file_lock( LOCK *ctx, const char *file )
295 {
296
297 ctx->size = get_file_size( file );
298 ctx->file = m_strdup( file );
299 ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL,
300 OPEN_ALWAYS, 0, NULL );
301 if( ctx->fh == INVALID_HANDLE_VALUE )
302 return WPTERR_GENERAL;
303 if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) {
304 CloseHandle( ctx->fh );
305 ctx->fh = INVALID_HANDLE_VALUE;
306 ctx->size = 0;
307 free( ctx->file );
308 return WPTERR_GENERAL;
309 }
310 return 0;
311 } /* init_file_lock */
312
313
314 void
315 release_file_lock( LOCK *ctx )
316 {
317 free_if_alloc( ctx->file );
318 ctx->file = NULL;
319 ctx->size = 0;
320 CloseHandle( ctx->fh );
321 } /* release_file_lock */
322
323
324 int
325 dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,
326 LPARAM param, LPCTSTR title, int title_id )
327 {
328 #ifndef LANG_DE
329 if( FindWindowEx( GetDesktopWindow(), NULL, NULL, title ) )
330 return -1;
331 #else
332 char strdesc[256];
333 LoadString( glob_hinst, title_id, strdesc, sizeof (strdesc) -1 );
334 if( FindWindowEx( GetDesktopWindow(), NULL, NULL, strdesc ) )
335 return -1;
336 #endif
337
338 return DialogBoxParam( hinst, name, parent, fnc, param );
339 } /* dialog_box_param */
340
341
342 int
343 msg_box( HWND hwnd, const char *text, const char *title, int mode )
344 {
345 mode |= MB_SETFOREGROUND;
346 mode |= MB_TASKMODAL;
347 mode |= MB_TOPMOST;
348 return MessageBox(hwnd, text, title, mode);
349 } /* msg_box */
350
351
352 void
353 set_active_window( HWND dlg )
354 {
355 activ_hwnd = dlg;
356 } /* set_active_window */
357
358 void
359 reset_active_window( void )
360 {
361 activ_hwnd = NULL;
362 } /* reset_active_window */
363
364
365 static DWORD CALLBACK
366 reminder_thread( void *ctx )
367 {
368 reminder_ctx_s *c = (reminder_ctx_s *)ctx;
369
370 Sleep( c->msecs );
371 SetForegroundWindow( activ_hwnd );
372
373 return 0;
374 } /* reminder_thread */
375
376 HANDLE
377 window_reminder( struct reminder_ctx_s *ctx )
378 {
379 DWORD tid = 0;
380
381 return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid );
382 } /* window_reminder */
383
384
385 char *
386 m_strdup( const char *str )
387 {
388 char * p = new char[strlen( str ) + 1];
389 if( p )
390 strcpy( p, str );
391 return p;
392 } /* m_strdup */
393
394
395 void
396 center_window2 (HWND hwndChild, HWND style)
397 {
398 HWND hwndParent;
399 RECT rChild, rParent;
400 HDC hdc;
401 int wChild, hChild, wParent, hParent;
402 int wScreen, hScreen, xNew, yNew;
403 int flags = SWP_NOSIZE | SWP_NOZORDER;
404
405 hwndParent = GetDesktopWindow ();
406 GetWindowRect (hwndChild, &rChild);
407 wChild = rChild.right - rChild.left;
408 hChild = rChild.bottom - rChild.top;
409
410 GetWindowRect (hwndParent, &rParent);
411 wParent = rParent.right - rParent.left;
412 hParent = rParent.bottom - rParent.top;
413
414 hdc = GetDC (hwndChild);
415 wScreen = GetDeviceCaps (hdc, HORZRES);
416 hScreen = GetDeviceCaps (hdc, VERTRES);
417 ReleaseDC (hwndChild, hdc);
418 xNew = rParent.left + ((wParent - wChild) /2);
419 if (xNew < 0)
420 xNew = 0;
421 else if ((xNew+wChild) > wScreen)
422 xNew = wScreen - wChild;
423 yNew = rParent.top + ((hParent - hChild) /2);
424 if (yNew < 0)
425 yNew = 0;
426 else if ((yNew+hChild) > hScreen)
427 yNew = hScreen - hChild;
428 if (style == HWND_TOPMOST || style == HWND_NOTOPMOST)
429 flags = SWP_NOMOVE | SWP_NOSIZE;
430 SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags);
431 }
432
433
434 void
435 center_window (HWND hwndChild)
436 {
437 center_window2 (hwndChild, NULL);
438 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26