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

Annotation of /trunk/Src/wptW32API.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (hide annotations)
Sat Oct 8 10:43:08 2005 UTC (19 years, 4 months ago) by twoaday
File size: 11538 byte(s)
Bug fixes to correct some problems introduced by
the MyGPGME to GPGME port.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26