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

Annotation of /trunk/Src/wptW32API.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (hide annotations)
Fri Sep 30 10:10:16 2005 UTC (19 years, 5 months ago) by twoaday
File size: 11686 byte(s)
Almost finished phase 1 of the WinPT GPGME port.
Still need more cleanup, comments and tests.


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     {
264     HANDLE fh;
265    
266     fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ,
267     NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
268     if( fh == INVALID_HANDLE_VALUE )
269     return WPTERR_FILE_EXIST;
270     CloseHandle( fh );
271     return 0;
272 twoaday 23 }
273 twoaday 2
274    
275 twoaday 23 /* Check if the current folder exists.
276     Return 0 for success. */
277 twoaday 2 int
278 twoaday 23 dir_exist_check (const char *dir)
279 twoaday 2 {
280     struct stat statbuf;
281    
282     if( stat( dir, &statbuf ) == -1 )
283     return WPTERR_GENERAL;
284     if( statbuf.st_mode & _S_IFDIR )
285     return 0;
286     return WPTERR_GENERAL;
287 twoaday 23 }
288 twoaday 2
289    
290 twoaday 23 /* Return the file size of the given file. */
291 twoaday 2 size_t
292 twoaday 23 get_file_size (const char *fname)
293 twoaday 2 {
294     size_t fsize;
295     HANDLE fh;
296    
297     fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ,
298     NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
299     if( fh == INVALID_HANDLE_VALUE )
300     return 0;
301     fsize = GetFileSize( fh, NULL );
302     if( fsize == 0xFFFFFFFF )
303     fsize = 0;
304     CloseHandle( fh );
305     return fsize;
306     } /* get_file_size */
307    
308    
309     int
310     init_file_lock( LOCK *ctx, const char *file )
311     {
312    
313     ctx->size = get_file_size( file );
314     ctx->file = m_strdup( file );
315     ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL,
316     OPEN_ALWAYS, 0, NULL );
317     if( ctx->fh == INVALID_HANDLE_VALUE )
318     return WPTERR_GENERAL;
319     if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) {
320     CloseHandle( ctx->fh );
321     ctx->fh = INVALID_HANDLE_VALUE;
322     ctx->size = 0;
323     free( ctx->file );
324     return WPTERR_GENERAL;
325     }
326     return 0;
327     } /* init_file_lock */
328    
329    
330     void
331     release_file_lock( LOCK *ctx )
332     {
333     free_if_alloc( ctx->file );
334     ctx->file = NULL;
335     ctx->size = 0;
336     CloseHandle( ctx->fh );
337     } /* release_file_lock */
338    
339    
340 twoaday 23 /* Start a dialog with the exception that before it is checked that the
341     dialog is not already openened. */
342 twoaday 2 int
343     dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,
344     LPARAM param, LPCTSTR title, int title_id )
345     {
346 twoaday 6 #ifndef LANG_DE
347 twoaday 2 if( FindWindowEx( GetDesktopWindow(), NULL, NULL, title ) )
348     return -1;
349 twoaday 6 #else
350 twoaday 2 char strdesc[256];
351     LoadString( glob_hinst, title_id, strdesc, sizeof (strdesc) -1 );
352     if( FindWindowEx( GetDesktopWindow(), NULL, NULL, strdesc ) )
353     return -1;
354 twoaday 6 #endif
355 twoaday 2
356     return DialogBoxParam( hinst, name, parent, fnc, param );
357     } /* dialog_box_param */
358    
359    
360 twoaday 23 /* Wrapper for message box which forces the message box into the
361     foreground and it is displayed always on top. */
362 twoaday 2 int
363 twoaday 23 msg_box (HWND hwnd, const char *text, const char *title, int mode)
364 twoaday 2 {
365     mode |= MB_SETFOREGROUND;
366     mode |= MB_TASKMODAL;
367     mode |= MB_TOPMOST;
368     return MessageBox(hwnd, text, title, mode);
369 twoaday 23 }
370 twoaday 2
371    
372     void
373 twoaday 23 set_active_window( HWND dlg)
374 twoaday 2 {
375     activ_hwnd = dlg;
376     } /* set_active_window */
377    
378     void
379     reset_active_window( void )
380     {
381     activ_hwnd = NULL;
382     } /* reset_active_window */
383    
384    
385     static DWORD CALLBACK
386 twoaday 23 reminder_thread (void *ctx)
387 twoaday 2 {
388     reminder_ctx_s *c = (reminder_ctx_s *)ctx;
389    
390     Sleep( c->msecs );
391     SetForegroundWindow( activ_hwnd );
392    
393     return 0;
394     } /* reminder_thread */
395    
396 twoaday 23
397 twoaday 2 HANDLE
398     window_reminder( struct reminder_ctx_s *ctx )
399     {
400     DWORD tid = 0;
401    
402     return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid );
403     } /* window_reminder */
404    
405    
406 twoaday 23 char*
407 twoaday 22 m_strdup (const char *str)
408 twoaday 2 {
409 twoaday 22 char * p = new char[strlen (str) + 1];
410     if (p)
411     strcpy (p, str);
412 twoaday 2 return p;
413     } /* m_strdup */
414    
415    
416 twoaday 23 /* Center the hwndChild relative to parent.
417     The style param allows to specificy additional styles (like topmost). */
418 twoaday 2 void
419 twoaday 23 center_window2 (HWND hwndChild, HWND parent, HWND style)
420 twoaday 2 {
421     HWND hwndParent;
422     RECT rChild, rParent;
423     HDC hdc;
424     int wChild, hChild, wParent, hParent;
425     int wScreen, hScreen, xNew, yNew;
426     int flags = SWP_NOSIZE | SWP_NOZORDER;
427    
428 twoaday 23 hwndParent = parent;
429     if (hwndParent == NULL)
430     hwndParent = GetDesktopWindow ();
431 twoaday 2 GetWindowRect (hwndChild, &rChild);
432     wChild = rChild.right - rChild.left;
433     hChild = rChild.bottom - rChild.top;
434    
435     GetWindowRect (hwndParent, &rParent);
436     wParent = rParent.right - rParent.left;
437     hParent = rParent.bottom - rParent.top;
438    
439     hdc = GetDC (hwndChild);
440     wScreen = GetDeviceCaps (hdc, HORZRES);
441     hScreen = GetDeviceCaps (hdc, VERTRES);
442     ReleaseDC (hwndChild, hdc);
443     xNew = rParent.left + ((wParent - wChild) /2);
444     if (xNew < 0)
445     xNew = 0;
446     else if ((xNew+wChild) > wScreen)
447     xNew = wScreen - wChild;
448     yNew = rParent.top + ((hParent - hChild) /2);
449     if (yNew < 0)
450     yNew = 0;
451     else if ((yNew+hChild) > hScreen)
452     yNew = hScreen - hChild;
453     if (style == HWND_TOPMOST || style == HWND_NOTOPMOST)
454     flags = SWP_NOMOVE | SWP_NOSIZE;
455     SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags);
456     }
457    
458    
459 twoaday 23 /* Center the given hwndChild window with no special style. */
460 twoaday 2 void
461 twoaday 23 center_window (HWND hwndChild, HWND hwndParent)
462 twoaday 2 {
463 twoaday 23 center_window2 (hwndChild, hwndParent, NULL);
464 twoaday 2 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26