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

Contents of /trunk/Src/wptW32API.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26