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