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

Annotation of /trunk/Src/wptW32API.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (hide annotations)
Mon Oct 24 08:03:48 2005 UTC (19 years, 4 months ago) by twoaday
File size: 12147 byte(s)
2005-10-23  Timo Schulz  <twoaday@g10code.com>
 
        * wptFileManager.cpp (fm_get_file_type): Detect detached sigs.
        * wptKeyList.cpp (keylist_cmp_cb): Take care of expired/revoked keys.
        (get_ext_validity): New.
        * wptFileVerifyDlg.cpp (file_verify_dlg_proc): Several cleanups.
        * wptClipEditDlg.cpp (load_clipboard): Factored out some code into
        this function.
        (load_clipboard_from_file): Likewise.
        (save_clipboard_to_file): New.
        * wptKeyManagerDlg.cpp (keyprops_dlg_proc): Fix stack overflow.

For complete details, see the ChangeLog files.

1 twoaday 2 /* 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 twoaday 22 extern "C" void _SHFree (void *p);
37    
38 twoaday 32
39     static void
40     set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text)
41 twoaday 2 {
42     MENUITEMINFO mii;
43    
44     memset (&mii, 0, sizeof mii);
45     mii.cbSize = sizeof mii;
46     mii.fMask = MIIM_TYPE;
47     mii.fType = MFT_STRING;
48     mii.dwTypeData = (char *) text;
49 twoaday 32 SetMenuItemInfo (menu, m_uid, by_pos? TRUE : FALSE, &mii);
50     }
51 twoaday 2
52    
53 twoaday 32 /* Set the text of a menu item @m_uid to @text. */
54 twoaday 2 void
55 twoaday 32 set_menu_text (HMENU menu, int m_uid, const char *text)
56     {
57     set_menu_text_ext (menu, 0, m_uid, text);
58     }
59    
60    
61     /* Set the text of a menu item with the position @pos to @text. */
62     void
63     set_menu_text_bypos (HMENU menu, int pos, const char *text)
64     {
65     set_menu_text_ext (menu, 1, pos, text);
66     }
67    
68    
69     /* Set the state of a menu item @m_uid to @state. */
70     void
71 twoaday 2 set_menu_state (HMENU menu, int m_uid, int state)
72     {
73     MENUITEMINFO mii;
74    
75     memset( &mii, 0, sizeof (mii) );
76     mii.cbSize = sizeof (mii);
77     mii.fMask = MIIM_STATE;
78     mii.fState = state;
79     SetMenuItemInfo (menu, m_uid, FALSE, &mii);
80 twoaday 32 }
81 twoaday 2
82    
83 twoaday 25
84 twoaday 23 /* Use the common dialog to request a file from the user.
85     id can be either FILE_OPEN or FILE_SAVE.
86     The return value is the file name or NULL if cancel was chosen. */
87 twoaday 2 const char *
88     get_filename_dlg (HWND hwnd, int id, const char * title,
89     const char * filter, const char * name)
90     {
91     static char file[512] = "";
92     OPENFILENAME open;
93    
94     if (name && strlen (name) < (sizeof (file)-1))
95     strcpy (file, name);
96     else
97     memset (file, 0, sizeof (file));
98     if (!filter)
99 twoaday 23 filter = _("All Files (*.*)\0*.*\0\0");
100 twoaday 32 /* XXX: problem with gettext because of the 'artificial'
101     double string termination!. */
102 twoaday 2 memset (&open, 0, sizeof (open));
103     open.lStructSize = sizeof (OPENFILENAME);
104     open.hInstance = glob_hinst;
105     open.lpstrTitle = title;
106     open.lpstrFilter = filter;
107     open.hwndOwner = hwnd;
108     open.lpstrFile = file;
109     open.nMaxFile = sizeof (file) - 1;
110 twoaday 23 if (id == FILE_OPEN)
111     open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
112     else
113     open.Flags = OFN_OVERWRITEPROMPT;
114 twoaday 2
115 twoaday 23 if (id == FILE_OPEN && GetOpenFileName (&open))
116 twoaday 2 return open.lpstrFile;
117 twoaday 23 else if (id == FILE_SAVE && GetSaveFileName (&open))
118 twoaday 2 return open.lpstrFile;
119    
120     return NULL;
121 twoaday 23 }
122 twoaday 2
123 twoaday 25 const char*
124     get_filesave_dlg (HWND hwnd, const char *title,
125     const char *filter, const char *name)
126     {
127     return get_filename_dlg (hwnd, FILE_SAVE, title, filter, name);
128     }
129    
130     const char *
131     get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,
132     const char *name)
133     {
134     return get_filename_dlg (hwnd, FILE_OPEN, title, filter, name);
135     }
136    
137    
138 twoaday 23 /* Use the common dialog to allow the user to select a folder.
139     The return value is either the folder path or NULL if cancel was chosen. */
140     const char*
141 twoaday 2 get_folder_dlg (HWND hwnd, const char * title, const char * name)
142     {
143 twoaday 23 static char folder[MAX_PATH+1] = "";
144 twoaday 2 BROWSEINFO bi;
145     ITEMIDLIST * il;
146    
147     memset (&bi, 0, sizeof (bi));
148     bi.hwndOwner = hwnd;
149     if (title)
150     bi.lpszTitle = title;
151     if (name && strlen (name) < MAX_PATH-1)
152     strcpy (folder, name);
153     else
154     memset (folder, 0, sizeof (folder));
155     il = SHBrowseForFolder (&bi);
156     if (il) {
157     SHGetPathFromIDList (il, folder);
158 twoaday 22 _SHFree (il);
159 twoaday 2 return folder;
160     }
161     return NULL;
162     }
163    
164    
165 twoaday 23 /* Return the clipboard contents as a string or NULL
166     if the clipboard does not contain text. */
167 twoaday 2 char*
168     get_clip_text (HWND hwnd)
169     {
170     HANDLE clipmem;
171     char *cliptxt, *p;
172     int len;
173    
174     if (OpenClipboard (hwnd) == FALSE)
175     return NULL;
176     clipmem = GetClipboardData (CF_TEXT);
177     if (clipmem == NULL) {
178     p = NULL;
179     goto leave;
180     }
181     cliptxt = (char *) GlobalLock (clipmem);
182     if (cliptxt == NULL) {
183     p = NULL;
184     goto leave;
185     }
186    
187     len = strlen (cliptxt);
188     p = new char[len + 1];
189     if (!p)
190     BUG (NULL);
191     memcpy (p, cliptxt, len);
192     p[len] = '\0';
193     GlobalUnlock (clipmem);
194    
195     leave:
196     CloseClipboard ();
197     return p;
198 twoaday 23 }
199 twoaday 2
200    
201 twoaday 32 /* Set @text as the new clipboard content. */
202 twoaday 2 int
203     set_clip_text (HWND hwnd, const char *text, int nbytes)
204     {
205     HANDLE clipmem;
206     int rc = 0;
207     char *p;
208    
209     if (OpenClipboard (hwnd) == FALSE)
210     return WPTERR_CLIP_OPEN;
211     EmptyClipboard ();
212    
213     clipmem = GlobalAlloc (GHND, nbytes + 1);
214     if (clipmem == NULL)
215     BUG (NULL);
216     p = (char *) GlobalLock (clipmem);
217     if (p == NULL) {
218     rc = WPTERR_GENERAL;;
219     goto leave;
220     }
221     memcpy (p, text, nbytes);
222     p[nbytes] = '\0';
223    
224     GlobalUnlock (clipmem);
225     SetClipboardData (CF_TEXT, clipmem);
226    
227     leave:
228     CloseClipboard ();
229     return rc;
230     } /* set_clip_text */
231    
232    
233 twoaday 23 /* Append or prepend some text to the clipboard contents.
234     If as_footer = 1, append the text otherwise prepend. */
235 twoaday 2 int
236 twoaday 23 set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer)
237 twoaday 2 {
238     char *p, *new_text;
239    
240     p = get_clip_text (hwnd);
241     if (!p)
242     return WPTERR_CLIP_GET;
243     new_text = new char [strlen (p)+strlen (text)+8];
244     if (!new_text)
245     BUG (0);
246 twoaday 23 if (as_footer == 0)
247 twoaday 2 sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p);
248     else
249     sprintf (new_text, "%s\n%s\n\n", p, text);
250     set_clip_text (hwnd, new_text, strlen (new_text)+1);
251     free_if_alloc (p);
252     free_if_alloc (new_text);
253     return 0;
254     }
255    
256    
257 twoaday 23 /* Make a file name out of the path, the file and an extension. */
258 twoaday 2 char*
259 twoaday 23 make_filename (const char *path, const char *file, const char *ext)
260 twoaday 2 {
261     char *p;
262     size_t size = 0;
263    
264     if( path && *path )
265     size += strlen( path );
266     if( file && *file )
267     size += strlen( file );
268     if( ext && *ext )
269     size += strlen( ext );
270     p = new char[size + 4];
271     memset( p, 0, size );
272     if( path ) {
273     strcat( p, path );
274     if( path[strlen( path ) -1] != '\\' )
275     strcat( p, "\\" );
276     }
277     if( file )
278     strcat( p, file );
279     if( ext ) {
280     strcat( p, "." );
281     strcat( p, ext );
282     }
283     return p;
284     } /* make_filename */
285    
286    
287     /* return 0 if it exists, otherwise >0. */
288     int
289     file_exist_check (const char * fname)
290 twoaday 24 {
291     struct stat st;
292     if (stat (fname, &st) == -1)
293 twoaday 2 return WPTERR_FILE_EXIST;
294     return 0;
295 twoaday 23 }
296 twoaday 2
297    
298 twoaday 23 /* Check if the current folder exists.
299     Return 0 for success. */
300 twoaday 2 int
301 twoaday 23 dir_exist_check (const char *dir)
302 twoaday 2 {
303     struct stat statbuf;
304    
305     if( stat( dir, &statbuf ) == -1 )
306     return WPTERR_GENERAL;
307     if( statbuf.st_mode & _S_IFDIR )
308     return 0;
309     return WPTERR_GENERAL;
310 twoaday 23 }
311 twoaday 2
312    
313 twoaday 23 /* Return the file size of the given file. */
314 twoaday 2 size_t
315 twoaday 23 get_file_size (const char *fname)
316 twoaday 2 {
317     size_t fsize;
318     HANDLE fh;
319    
320     fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ,
321     NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
322     if( fh == INVALID_HANDLE_VALUE )
323     return 0;
324     fsize = GetFileSize( fh, NULL );
325     if( fsize == 0xFFFFFFFF )
326     fsize = 0;
327     CloseHandle( fh );
328     return fsize;
329     } /* get_file_size */
330    
331    
332     int
333     init_file_lock( LOCK *ctx, const char *file )
334     {
335    
336     ctx->size = get_file_size( file );
337     ctx->file = m_strdup( file );
338     ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL,
339     OPEN_ALWAYS, 0, NULL );
340     if( ctx->fh == INVALID_HANDLE_VALUE )
341     return WPTERR_GENERAL;
342     if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) {
343     CloseHandle( ctx->fh );
344     ctx->fh = INVALID_HANDLE_VALUE;
345     ctx->size = 0;
346     free( ctx->file );
347     return WPTERR_GENERAL;
348     }
349     return 0;
350     } /* init_file_lock */
351    
352    
353     void
354     release_file_lock( LOCK *ctx )
355     {
356     free_if_alloc( ctx->file );
357     ctx->file = NULL;
358     ctx->size = 0;
359     CloseHandle( ctx->fh );
360     } /* release_file_lock */
361    
362    
363 twoaday 23 /* Start a dialog with the exception that before it is checked that the
364     dialog is not already openened. */
365 twoaday 2 int
366     dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,
367     LPARAM param, LPCTSTR title, int title_id )
368     {
369 twoaday 6 #ifndef LANG_DE
370 twoaday 2 if( FindWindowEx( GetDesktopWindow(), NULL, NULL, title ) )
371     return -1;
372 twoaday 6 #else
373 twoaday 2 char strdesc[256];
374     LoadString( glob_hinst, title_id, strdesc, sizeof (strdesc) -1 );
375     if( FindWindowEx( GetDesktopWindow(), NULL, NULL, strdesc ) )
376     return -1;
377 twoaday 6 #endif
378 twoaday 2
379     return DialogBoxParam( hinst, name, parent, fnc, param );
380     } /* dialog_box_param */
381    
382    
383 twoaday 23 /* Wrapper for message box which forces the message box into the
384     foreground and it is displayed always on top. */
385 twoaday 2 int
386 twoaday 23 msg_box (HWND hwnd, const char *text, const char *title, int mode)
387 twoaday 2 {
388     mode |= MB_SETFOREGROUND;
389     mode |= MB_TASKMODAL;
390     mode |= MB_TOPMOST;
391     return MessageBox(hwnd, text, title, mode);
392 twoaday 23 }
393 twoaday 2
394    
395     void
396 twoaday 23 set_active_window( HWND dlg)
397 twoaday 2 {
398     activ_hwnd = dlg;
399     } /* set_active_window */
400    
401     void
402     reset_active_window( void )
403     {
404     activ_hwnd = NULL;
405     } /* reset_active_window */
406    
407    
408     static DWORD CALLBACK
409 twoaday 23 reminder_thread (void *ctx)
410 twoaday 2 {
411     reminder_ctx_s *c = (reminder_ctx_s *)ctx;
412    
413     Sleep( c->msecs );
414     SetForegroundWindow( activ_hwnd );
415    
416     return 0;
417     } /* reminder_thread */
418    
419 twoaday 23
420 twoaday 2 HANDLE
421     window_reminder( struct reminder_ctx_s *ctx )
422     {
423     DWORD tid = 0;
424    
425     return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid );
426     } /* window_reminder */
427    
428    
429 twoaday 23 char*
430 twoaday 22 m_strdup (const char *str)
431 twoaday 2 {
432 twoaday 22 char * p = new char[strlen (str) + 1];
433     if (p)
434     strcpy (p, str);
435 twoaday 2 return p;
436     } /* m_strdup */
437    
438    
439 twoaday 23 /* Center the hwndChild relative to parent.
440     The style param allows to specificy additional styles (like topmost). */
441 twoaday 2 void
442 twoaday 23 center_window2 (HWND hwndChild, HWND parent, HWND style)
443 twoaday 2 {
444     HWND hwndParent;
445     RECT rChild, rParent;
446     HDC hdc;
447     int wChild, hChild, wParent, hParent;
448     int wScreen, hScreen, xNew, yNew;
449     int flags = SWP_NOSIZE | SWP_NOZORDER;
450    
451 twoaday 23 hwndParent = parent;
452     if (hwndParent == NULL)
453     hwndParent = GetDesktopWindow ();
454 twoaday 2 GetWindowRect (hwndChild, &rChild);
455     wChild = rChild.right - rChild.left;
456     hChild = rChild.bottom - rChild.top;
457    
458     GetWindowRect (hwndParent, &rParent);
459     wParent = rParent.right - rParent.left;
460     hParent = rParent.bottom - rParent.top;
461    
462     hdc = GetDC (hwndChild);
463     wScreen = GetDeviceCaps (hdc, HORZRES);
464     hScreen = GetDeviceCaps (hdc, VERTRES);
465     ReleaseDC (hwndChild, hdc);
466     xNew = rParent.left + ((wParent - wChild) /2);
467     if (xNew < 0)
468     xNew = 0;
469     else if ((xNew+wChild) > wScreen)
470     xNew = wScreen - wChild;
471     yNew = rParent.top + ((hParent - hChild) /2);
472     if (yNew < 0)
473     yNew = 0;
474     else if ((yNew+hChild) > hScreen)
475     yNew = hScreen - hChild;
476     if (style == HWND_TOPMOST || style == HWND_NOTOPMOST)
477     flags = SWP_NOMOVE | SWP_NOSIZE;
478     SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags);
479     }
480    
481    
482 twoaday 23 /* Center the given hwndChild window with no special style. */
483 twoaday 2 void
484 twoaday 23 center_window (HWND hwndChild, HWND hwndParent)
485 twoaday 2 {
486 twoaday 23 center_window2 (hwndChild, hwndParent, NULL);
487 twoaday 2 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26