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

Contents of /trunk/Src/wptW32API.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 121 - (show annotations)
Mon Dec 12 11:19:56 2005 UTC (19 years, 2 months ago) by twoaday
File size: 12648 byte(s)
2005-12-11  Timo Schulz  <ts@g10code.com>
 
        * wptW32API.cpp (get_file_version): New.
        * wptGPGUtil.cpp (create_process): Always hide window.
        * wptClipEditDlg.cpp (clipedit_dlg_proc): Use 'Close'
        instead of 'Exit'.
        * wptKeyManager.cpp (km_http_import): New filename
        generation code.
        (km_send_to_mail_recipient): Cleanups.
        * wptKeyEditDlg.cpp (showpref_dlg_proc): Localize dialog.
        * wptKeyManagerDlg.cpp (update_ui_items): Handle the case
        when multiple keys are selected.
        (popup_multiple): New.
        * WinPT.cpp (WinMain): Check that the PTD.dll and WinPT.exe
        file versions are equal. Rewrote --keymanager code.
         
Removed temporary w32gpgme dirctory, all code is now in Src.
Changed configure files.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26