1 |
twoaday |
2 |
/* 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 |
twoaday |
22 |
extern "C" void _SHFree (void *p); |
37 |
|
|
|
38 |
twoaday |
2 |
/* |
39 |
|
|
* The the text of a menu item. |
40 |
|
|
*/ |
41 |
|
|
void |
42 |
|
|
set_menu_text (HMENU menu, int m_uid, const char *text) |
43 |
|
|
{ |
44 |
|
|
MENUITEMINFO mii; |
45 |
|
|
char menu_text[80]; |
46 |
|
|
|
47 |
|
|
memset (&mii, 0, sizeof (mii)); |
48 |
|
|
mii.cbSize = sizeof (mii); |
49 |
|
|
mii.fMask = MIIM_TYPE; |
50 |
|
|
mii.dwTypeData = menu_text; |
51 |
|
|
mii.cch = sizeof (menu_text); |
52 |
|
|
GetMenuItemInfo (menu, m_uid, FALSE, &mii); |
53 |
|
|
|
54 |
|
|
memset (&mii, 0, sizeof mii); |
55 |
|
|
mii.cbSize = sizeof mii; |
56 |
|
|
mii.fMask = MIIM_TYPE; |
57 |
|
|
mii.fType = MFT_STRING; |
58 |
|
|
mii.dwTypeData = (char *) text; |
59 |
|
|
SetMenuItemInfo (menu, m_uid, FALSE, &mii); |
60 |
|
|
} /* set_menu_text */ |
61 |
|
|
|
62 |
|
|
|
63 |
|
|
void |
64 |
|
|
set_menu_state (HMENU menu, int m_uid, int state) |
65 |
|
|
{ |
66 |
|
|
MENUITEMINFO mii; |
67 |
|
|
|
68 |
|
|
memset( &mii, 0, sizeof (mii) ); |
69 |
|
|
mii.cbSize = sizeof (mii); |
70 |
|
|
mii.fMask = MIIM_STATE; |
71 |
|
|
mii.fState = state; |
72 |
|
|
SetMenuItemInfo (menu, m_uid, FALSE, &mii); |
73 |
|
|
} /* set_menu_state */ |
74 |
|
|
|
75 |
|
|
|
76 |
|
|
|
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*.*"); |
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 |
|
|
open.Flags = 0; |
99 |
|
|
|
100 |
|
|
if (id == 0 && GetOpenFileName (&open)) |
101 |
|
|
return open.lpstrFile; |
102 |
|
|
else if (id == 1 && GetSaveFileName (&open)) |
103 |
|
|
return open.lpstrFile; |
104 |
|
|
|
105 |
|
|
return NULL; |
106 |
|
|
} /* get_filename_dlg */ |
107 |
|
|
|
108 |
|
|
|
109 |
|
|
const char * |
110 |
|
|
get_folder_dlg (HWND hwnd, const char * title, const char * name) |
111 |
|
|
{ |
112 |
|
|
static char folder[MAX_PATH] = ""; |
113 |
|
|
BROWSEINFO bi; |
114 |
|
|
ITEMIDLIST * il; |
115 |
|
|
|
116 |
|
|
memset (&bi, 0, sizeof (bi)); |
117 |
|
|
bi.hwndOwner = hwnd; |
118 |
|
|
if (title) |
119 |
|
|
bi.lpszTitle = title; |
120 |
|
|
if (name && strlen (name) < MAX_PATH-1) |
121 |
|
|
strcpy (folder, name); |
122 |
|
|
else |
123 |
|
|
memset (folder, 0, sizeof (folder)); |
124 |
|
|
il = SHBrowseForFolder (&bi); |
125 |
|
|
if (il) { |
126 |
|
|
SHGetPathFromIDList (il, folder); |
127 |
twoaday |
22 |
_SHFree (il); |
128 |
twoaday |
2 |
return folder; |
129 |
|
|
} |
130 |
|
|
return NULL; |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
|
134 |
|
|
char* |
135 |
|
|
get_clip_text (HWND hwnd) |
136 |
|
|
{ |
137 |
|
|
HANDLE clipmem; |
138 |
|
|
char *cliptxt, *p; |
139 |
|
|
int len; |
140 |
|
|
|
141 |
|
|
if (OpenClipboard (hwnd) == FALSE) |
142 |
|
|
return NULL; |
143 |
|
|
clipmem = GetClipboardData (CF_TEXT); |
144 |
|
|
if (clipmem == NULL) { |
145 |
|
|
p = NULL; |
146 |
|
|
goto leave; |
147 |
|
|
} |
148 |
|
|
cliptxt = (char *) GlobalLock (clipmem); |
149 |
|
|
if (cliptxt == NULL) { |
150 |
|
|
p = NULL; |
151 |
|
|
goto leave; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
len = strlen (cliptxt); |
155 |
|
|
p = new char[len + 1]; |
156 |
|
|
if (!p) |
157 |
|
|
BUG (NULL); |
158 |
|
|
memcpy (p, cliptxt, len); |
159 |
|
|
p[len] = '\0'; |
160 |
|
|
GlobalUnlock (clipmem); |
161 |
|
|
|
162 |
|
|
leave: |
163 |
|
|
CloseClipboard (); |
164 |
|
|
return p; |
165 |
|
|
} /* get_clip_text */ |
166 |
|
|
|
167 |
|
|
|
168 |
|
|
int |
169 |
|
|
set_clip_text (HWND hwnd, const char *text, int nbytes) |
170 |
|
|
{ |
171 |
|
|
HANDLE clipmem; |
172 |
|
|
int rc = 0; |
173 |
|
|
char *p; |
174 |
|
|
|
175 |
|
|
if (OpenClipboard (hwnd) == FALSE) |
176 |
|
|
return WPTERR_CLIP_OPEN; |
177 |
|
|
EmptyClipboard (); |
178 |
|
|
|
179 |
|
|
clipmem = GlobalAlloc (GHND, nbytes + 1); |
180 |
|
|
if (clipmem == NULL) |
181 |
|
|
BUG (NULL); |
182 |
|
|
p = (char *) GlobalLock (clipmem); |
183 |
|
|
if (p == NULL) { |
184 |
|
|
rc = WPTERR_GENERAL;; |
185 |
|
|
goto leave; |
186 |
|
|
} |
187 |
|
|
memcpy (p, text, nbytes); |
188 |
|
|
p[nbytes] = '\0'; |
189 |
|
|
|
190 |
|
|
GlobalUnlock (clipmem); |
191 |
|
|
SetClipboardData (CF_TEXT, clipmem); |
192 |
|
|
|
193 |
|
|
leave: |
194 |
|
|
CloseClipboard (); |
195 |
|
|
return rc; |
196 |
|
|
} /* set_clip_text */ |
197 |
|
|
|
198 |
|
|
|
199 |
|
|
int |
200 |
|
|
set_clip_text2 (HWND hwnd, const char *text, int nbytes, int head_foot) |
201 |
|
|
{ |
202 |
|
|
char *p, *new_text; |
203 |
|
|
|
204 |
|
|
p = get_clip_text (hwnd); |
205 |
|
|
if (!p) |
206 |
|
|
return WPTERR_CLIP_GET; |
207 |
|
|
new_text = new char [strlen (p)+strlen (text)+8]; |
208 |
|
|
if (!new_text) |
209 |
|
|
BUG (0); |
210 |
|
|
if (head_foot == 0) |
211 |
|
|
sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p); |
212 |
|
|
else |
213 |
|
|
sprintf (new_text, "%s\n%s\n\n", p, text); |
214 |
|
|
set_clip_text (hwnd, new_text, strlen (new_text)+1); |
215 |
|
|
free_if_alloc (p); |
216 |
|
|
free_if_alloc (new_text); |
217 |
|
|
return 0; |
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
|
221 |
|
|
char* |
222 |
|
|
make_filename( const char *path, const char *file, const char *ext ) |
223 |
|
|
{ |
224 |
|
|
char *p; |
225 |
|
|
size_t size = 0; |
226 |
|
|
|
227 |
|
|
if( path && *path ) |
228 |
|
|
size += strlen( path ); |
229 |
|
|
if( file && *file ) |
230 |
|
|
size += strlen( file ); |
231 |
|
|
if( ext && *ext ) |
232 |
|
|
size += strlen( ext ); |
233 |
|
|
p = new char[size + 4]; |
234 |
|
|
memset( p, 0, size ); |
235 |
|
|
if( path ) { |
236 |
|
|
strcat( p, path ); |
237 |
|
|
if( path[strlen( path ) -1] != '\\' ) |
238 |
|
|
strcat( p, "\\" ); |
239 |
|
|
} |
240 |
|
|
if( file ) |
241 |
|
|
strcat( p, file ); |
242 |
|
|
if( ext ) { |
243 |
|
|
strcat( p, "." ); |
244 |
|
|
strcat( p, ext ); |
245 |
|
|
} |
246 |
|
|
return p; |
247 |
|
|
} /* make_filename */ |
248 |
|
|
|
249 |
|
|
|
250 |
|
|
/* return 0 if it exists, otherwise >0. */ |
251 |
|
|
int |
252 |
|
|
file_exist_check (const char * fname) |
253 |
|
|
{ |
254 |
|
|
HANDLE fh; |
255 |
|
|
|
256 |
|
|
fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ, |
257 |
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); |
258 |
|
|
if( fh == INVALID_HANDLE_VALUE ) |
259 |
|
|
return WPTERR_FILE_EXIST; |
260 |
|
|
CloseHandle( fh ); |
261 |
|
|
return 0; |
262 |
|
|
} /* file_exist_check */ |
263 |
|
|
|
264 |
|
|
|
265 |
|
|
int |
266 |
|
|
dir_exist_check( const char *dir ) |
267 |
|
|
{ |
268 |
|
|
struct stat statbuf; |
269 |
|
|
|
270 |
|
|
if( stat( dir, &statbuf ) == -1 ) |
271 |
|
|
return WPTERR_GENERAL; |
272 |
|
|
if( statbuf.st_mode & _S_IFDIR ) |
273 |
|
|
return 0; |
274 |
|
|
return WPTERR_GENERAL; |
275 |
|
|
} /* dir_exist_check */ |
276 |
|
|
|
277 |
|
|
|
278 |
|
|
size_t |
279 |
|
|
get_file_size( const char *fname ) |
280 |
|
|
{ |
281 |
|
|
size_t fsize; |
282 |
|
|
HANDLE fh; |
283 |
|
|
|
284 |
|
|
fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ, |
285 |
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); |
286 |
|
|
if( fh == INVALID_HANDLE_VALUE ) |
287 |
|
|
return 0; |
288 |
|
|
fsize = GetFileSize( fh, NULL ); |
289 |
|
|
if( fsize == 0xFFFFFFFF ) |
290 |
|
|
fsize = 0; |
291 |
|
|
CloseHandle( fh ); |
292 |
|
|
return fsize; |
293 |
|
|
} /* get_file_size */ |
294 |
|
|
|
295 |
|
|
|
296 |
|
|
int |
297 |
|
|
init_file_lock( LOCK *ctx, const char *file ) |
298 |
|
|
{ |
299 |
|
|
|
300 |
|
|
ctx->size = get_file_size( file ); |
301 |
|
|
ctx->file = m_strdup( file ); |
302 |
|
|
ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL, |
303 |
|
|
OPEN_ALWAYS, 0, NULL ); |
304 |
|
|
if( ctx->fh == INVALID_HANDLE_VALUE ) |
305 |
|
|
return WPTERR_GENERAL; |
306 |
|
|
if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) { |
307 |
|
|
CloseHandle( ctx->fh ); |
308 |
|
|
ctx->fh = INVALID_HANDLE_VALUE; |
309 |
|
|
ctx->size = 0; |
310 |
|
|
free( ctx->file ); |
311 |
|
|
return WPTERR_GENERAL; |
312 |
|
|
} |
313 |
|
|
return 0; |
314 |
|
|
} /* init_file_lock */ |
315 |
|
|
|
316 |
|
|
|
317 |
|
|
void |
318 |
|
|
release_file_lock( LOCK *ctx ) |
319 |
|
|
{ |
320 |
|
|
free_if_alloc( ctx->file ); |
321 |
|
|
ctx->file = NULL; |
322 |
|
|
ctx->size = 0; |
323 |
|
|
CloseHandle( ctx->fh ); |
324 |
|
|
} /* release_file_lock */ |
325 |
|
|
|
326 |
|
|
|
327 |
|
|
int |
328 |
|
|
dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc, |
329 |
|
|
LPARAM param, LPCTSTR title, int title_id ) |
330 |
|
|
{ |
331 |
twoaday |
6 |
#ifndef LANG_DE |
332 |
twoaday |
2 |
if( FindWindowEx( GetDesktopWindow(), NULL, NULL, title ) ) |
333 |
|
|
return -1; |
334 |
twoaday |
6 |
#else |
335 |
twoaday |
2 |
char strdesc[256]; |
336 |
|
|
LoadString( glob_hinst, title_id, strdesc, sizeof (strdesc) -1 ); |
337 |
|
|
if( FindWindowEx( GetDesktopWindow(), NULL, NULL, strdesc ) ) |
338 |
|
|
return -1; |
339 |
twoaday |
6 |
#endif |
340 |
twoaday |
2 |
|
341 |
|
|
return DialogBoxParam( hinst, name, parent, fnc, param ); |
342 |
|
|
} /* dialog_box_param */ |
343 |
|
|
|
344 |
|
|
|
345 |
|
|
int |
346 |
|
|
msg_box( HWND hwnd, const char *text, const char *title, int mode ) |
347 |
|
|
{ |
348 |
|
|
mode |= MB_SETFOREGROUND; |
349 |
|
|
mode |= MB_TASKMODAL; |
350 |
|
|
mode |= MB_TOPMOST; |
351 |
|
|
return MessageBox(hwnd, text, title, mode); |
352 |
|
|
} /* msg_box */ |
353 |
|
|
|
354 |
|
|
|
355 |
|
|
void |
356 |
|
|
set_active_window( HWND dlg ) |
357 |
|
|
{ |
358 |
|
|
activ_hwnd = dlg; |
359 |
|
|
} /* set_active_window */ |
360 |
|
|
|
361 |
|
|
void |
362 |
|
|
reset_active_window( void ) |
363 |
|
|
{ |
364 |
|
|
activ_hwnd = NULL; |
365 |
|
|
} /* reset_active_window */ |
366 |
|
|
|
367 |
|
|
|
368 |
|
|
static DWORD CALLBACK |
369 |
|
|
reminder_thread( void *ctx ) |
370 |
|
|
{ |
371 |
|
|
reminder_ctx_s *c = (reminder_ctx_s *)ctx; |
372 |
|
|
|
373 |
|
|
Sleep( c->msecs ); |
374 |
|
|
SetForegroundWindow( activ_hwnd ); |
375 |
|
|
|
376 |
|
|
return 0; |
377 |
|
|
} /* reminder_thread */ |
378 |
|
|
|
379 |
|
|
HANDLE |
380 |
|
|
window_reminder( struct reminder_ctx_s *ctx ) |
381 |
|
|
{ |
382 |
|
|
DWORD tid = 0; |
383 |
|
|
|
384 |
|
|
return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid ); |
385 |
|
|
} /* window_reminder */ |
386 |
|
|
|
387 |
|
|
|
388 |
|
|
char * |
389 |
twoaday |
22 |
m_strdup (const char *str) |
390 |
twoaday |
2 |
{ |
391 |
twoaday |
22 |
char * p = new char[strlen (str) + 1]; |
392 |
|
|
if (p) |
393 |
|
|
strcpy (p, str); |
394 |
twoaday |
2 |
return p; |
395 |
|
|
} /* m_strdup */ |
396 |
|
|
|
397 |
|
|
|
398 |
|
|
void |
399 |
|
|
center_window2 (HWND hwndChild, HWND style) |
400 |
|
|
{ |
401 |
|
|
HWND hwndParent; |
402 |
|
|
RECT rChild, rParent; |
403 |
|
|
HDC hdc; |
404 |
|
|
int wChild, hChild, wParent, hParent; |
405 |
|
|
int wScreen, hScreen, xNew, yNew; |
406 |
|
|
int flags = SWP_NOSIZE | SWP_NOZORDER; |
407 |
|
|
|
408 |
|
|
hwndParent = GetDesktopWindow (); |
409 |
|
|
GetWindowRect (hwndChild, &rChild); |
410 |
|
|
wChild = rChild.right - rChild.left; |
411 |
|
|
hChild = rChild.bottom - rChild.top; |
412 |
|
|
|
413 |
|
|
GetWindowRect (hwndParent, &rParent); |
414 |
|
|
wParent = rParent.right - rParent.left; |
415 |
|
|
hParent = rParent.bottom - rParent.top; |
416 |
|
|
|
417 |
|
|
hdc = GetDC (hwndChild); |
418 |
|
|
wScreen = GetDeviceCaps (hdc, HORZRES); |
419 |
|
|
hScreen = GetDeviceCaps (hdc, VERTRES); |
420 |
|
|
ReleaseDC (hwndChild, hdc); |
421 |
|
|
xNew = rParent.left + ((wParent - wChild) /2); |
422 |
|
|
if (xNew < 0) |
423 |
|
|
xNew = 0; |
424 |
|
|
else if ((xNew+wChild) > wScreen) |
425 |
|
|
xNew = wScreen - wChild; |
426 |
|
|
yNew = rParent.top + ((hParent - hChild) /2); |
427 |
|
|
if (yNew < 0) |
428 |
|
|
yNew = 0; |
429 |
|
|
else if ((yNew+hChild) > hScreen) |
430 |
|
|
yNew = hScreen - hChild; |
431 |
|
|
if (style == HWND_TOPMOST || style == HWND_NOTOPMOST) |
432 |
|
|
flags = SWP_NOMOVE | SWP_NOSIZE; |
433 |
|
|
SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags); |
434 |
|
|
} |
435 |
|
|
|
436 |
|
|
|
437 |
|
|
void |
438 |
|
|
center_window (HWND hwndChild) |
439 |
|
|
{ |
440 |
|
|
center_window2 (hwndChild, NULL); |
441 |
|
|
} |