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

Contents of /trunk/Src/wptW32API.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 190 - (show annotations)
Mon Mar 27 10:05:14 2006 UTC (18 years, 11 months ago) by twoaday
File size: 14403 byte(s)
2006-03-26  Timo Schulz  <ts@g10code.de>
 
        * wptSigTreeDlg.cpp (sigtree_load): Always use UTF8.
        * wptMainProc.cpp (winpt_main_proc): Reload key cache
        when the GPG settings were changed.
        * wptKeygenDlg.cpp (keygen_wizard_dlg_proc,
        keygen_dlg_proc): Improved check for the email address.
        * wptKeyEditDlgs.cpp (keyedit_adduid_dlg_proc): Likewise.
        * wptKeyserverDlg.cpp (keyserver_dlg_proc): Modified design.
         


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26