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

Contents of /trunk/Src/wptW32API.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (show 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 /* 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 extern "C" void _SHFree (void *p);
37
38 /* The the text of a menu item. */
39 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 /* 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 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 filter = _("All Files (*.*)\0*.*\0\0");
90 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 if (id == FILE_OPEN)
99 open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
100 else
101 open.Flags = OFN_OVERWRITEPROMPT;
102
103 if (id == FILE_OPEN && GetOpenFileName (&open))
104 return open.lpstrFile;
105 else if (id == FILE_SAVE && GetSaveFileName (&open))
106 return open.lpstrFile;
107
108 return NULL;
109 }
110
111 /* 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 get_folder_dlg (HWND hwnd, const char * title, const char * name)
115 {
116 static char folder[MAX_PATH+1] = "";
117 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 _SHFree (il);
132 return folder;
133 }
134 return NULL;
135 }
136
137
138 /* Return the clipboard contents as a string or NULL
139 if the clipboard does not contain text. */
140 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 }
172
173
174 /* Set the the given text to the clipboard. */
175 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 /* Append or prepend some text to the clipboard contents.
207 If as_footer = 1, append the text otherwise prepend. */
208 int
209 set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer)
210 {
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 if (as_footer == 0)
220 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 /* Make a file name out of the path, the file and an extension. */
231 char*
232 make_filename (const char *path, const char *file, const char *ext)
233 {
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 struct stat st;
265 if (stat (fname, &st) == -1)
266 return WPTERR_FILE_EXIST;
267 return 0;
268 }
269
270
271 /* Check if the current folder exists.
272 Return 0 for success. */
273 int
274 dir_exist_check (const char *dir)
275 {
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 }
284
285
286 /* Return the file size of the given file. */
287 size_t
288 get_file_size (const char *fname)
289 {
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 /* Start a dialog with the exception that before it is checked that the
337 dialog is not already openened. */
338 int
339 dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,
340 LPARAM param, LPCTSTR title, int title_id )
341 {
342 #ifndef LANG_DE
343 if( FindWindowEx( GetDesktopWindow(), NULL, NULL, title ) )
344 return -1;
345 #else
346 char strdesc[256];
347 LoadString( glob_hinst, title_id, strdesc, sizeof (strdesc) -1 );
348 if( FindWindowEx( GetDesktopWindow(), NULL, NULL, strdesc ) )
349 return -1;
350 #endif
351
352 return DialogBoxParam( hinst, name, parent, fnc, param );
353 } /* dialog_box_param */
354
355
356 /* Wrapper for message box which forces the message box into the
357 foreground and it is displayed always on top. */
358 int
359 msg_box (HWND hwnd, const char *text, const char *title, int mode)
360 {
361 mode |= MB_SETFOREGROUND;
362 mode |= MB_TASKMODAL;
363 mode |= MB_TOPMOST;
364 return MessageBox(hwnd, text, title, mode);
365 }
366
367
368 void
369 set_active_window( HWND dlg)
370 {
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 reminder_thread (void *ctx)
383 {
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
393 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 char*
403 m_strdup (const char *str)
404 {
405 char * p = new char[strlen (str) + 1];
406 if (p)
407 strcpy (p, str);
408 return p;
409 } /* m_strdup */
410
411
412 /* Center the hwndChild relative to parent.
413 The style param allows to specificy additional styles (like topmost). */
414 void
415 center_window2 (HWND hwndChild, HWND parent, HWND style)
416 {
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 hwndParent = parent;
425 if (hwndParent == NULL)
426 hwndParent = GetDesktopWindow ();
427 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 /* Center the given hwndChild window with no special style. */
456 void
457 center_window (HWND hwndChild, HWND hwndParent)
458 {
459 center_window2 (hwndChild, hwndParent, NULL);
460 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26