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