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

Contents of /trunk/Src/wptW32API.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 48 - (show annotations)
Mon Oct 31 21:14:11 2005 UTC (19 years, 4 months ago) by werner
File size: 11709 byte(s)
More changes.  Compiles again but there are at least gettext issues with
w32-gettext.c.  I can't get a gpg-error build with ENABLE_NLS.

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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26