1 |
/* wptW32API.cpp - Common W32 API functions |
/* wptW32API.cpp - Common W32 API functions |
2 |
* Copyright (C) 2001, 2002, 2003 Timo Schulz |
* Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
6 |
* WinPT is free software; you can redistribute it and/or modify |
* 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 |
* 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 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
* (at your option) any later version. |
10 |
* |
* |
11 |
* WinPT is distributed in the hope that it will be useful, |
* WinPT is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
* GNU General Public License for more details. |
15 |
* |
* |
16 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
*/ |
20 |
|
|
21 |
#include <windows.h> |
#ifdef HAVE_CONFIG_H |
22 |
#include <stdio.h> |
#include <config.h> |
23 |
#include <sys/types.h> |
#endif |
24 |
#include <sys/stat.h> |
|
25 |
#include <shellapi.h> |
#include <windows.h> |
26 |
#include <shlobj.h> |
#include <stdio.h> |
27 |
#include <commctrl.h> |
#include <sys/types.h> |
28 |
|
#include <sys/stat.h> |
29 |
#include "wptNLS.h" |
#include <shellapi.h> |
30 |
#include "wptW32API.h" |
#include <shlobj.h> |
31 |
#include "wptErrors.h" |
#include <commctrl.h> |
32 |
#include "wptVersion.h" |
#include <time.h> |
33 |
#include "wptTypes.h" |
|
34 |
|
#include "wptNLS.h" |
35 |
|
#include "wptW32API.h" |
36 |
extern "C" void _SHFree (void *p); |
#include "wptErrors.h" |
37 |
|
#include "wptVersion.h" |
38 |
/* |
#include "wptTypes.h" |
39 |
* The the text of a menu item. |
|
40 |
*/ |
|
41 |
void |
extern "C" void _SHFree (void *p); |
42 |
set_menu_text (HMENU menu, int m_uid, const char *text) |
|
43 |
{ |
|
44 |
MENUITEMINFO mii; |
static void |
45 |
char menu_text[80]; |
set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text) |
46 |
|
{ |
47 |
memset (&mii, 0, sizeof (mii)); |
MENUITEMINFO mii; |
48 |
mii.cbSize = sizeof (mii); |
|
49 |
mii.fMask = MIIM_TYPE; |
memset (&mii, 0, sizeof mii); |
50 |
mii.dwTypeData = menu_text; |
mii.cbSize = sizeof mii; |
51 |
mii.cch = sizeof (menu_text); |
mii.fMask = MIIM_TYPE; |
52 |
GetMenuItemInfo (menu, m_uid, FALSE, &mii); |
mii.fType = MFT_STRING; |
53 |
|
mii.dwTypeData = (char *) text; |
54 |
memset (&mii, 0, sizeof mii); |
SetMenuItemInfo (menu, m_uid, by_pos? TRUE : FALSE, &mii); |
55 |
mii.cbSize = sizeof mii; |
} |
56 |
mii.fMask = MIIM_TYPE; |
|
57 |
mii.fType = MFT_STRING; |
|
58 |
mii.dwTypeData = (char *) text; |
/* Set the text of a menu item @m_uid to @text. */ |
59 |
SetMenuItemInfo (menu, m_uid, FALSE, &mii); |
void |
60 |
} /* set_menu_text */ |
set_menu_text (HMENU menu, UINT m_uid, const char *text) |
61 |
|
{ |
62 |
|
set_menu_text_ext (menu, 0, m_uid, text); |
63 |
void |
} |
64 |
set_menu_state (HMENU menu, int m_uid, int state) |
|
65 |
{ |
|
66 |
MENUITEMINFO mii; |
/* Set the text of a menu item with the position @pos to @text. */ |
67 |
|
void |
68 |
memset( &mii, 0, sizeof (mii) ); |
set_menu_text_bypos (HMENU menu, UINT pos, const char *text) |
69 |
mii.cbSize = sizeof (mii); |
{ |
70 |
mii.fMask = MIIM_STATE; |
set_menu_text_ext (menu, 1, pos, text); |
71 |
mii.fState = state; |
} |
72 |
SetMenuItemInfo (menu, m_uid, FALSE, &mii); |
|
73 |
} /* set_menu_state */ |
|
74 |
|
/* Set the state of a menu item @m_uid to @state. */ |
75 |
|
void |
76 |
|
set_menu_state (HMENU menu, UINT m_uid, UINT state) |
77 |
const char * |
{ |
78 |
get_filename_dlg (HWND hwnd, int id, const char * title, |
MENUITEMINFO mii; |
79 |
const char * filter, const char * name) |
|
80 |
{ |
memset (&mii, 0, sizeof (mii)); |
81 |
static char file[512] = ""; |
mii.cbSize = sizeof (mii); |
82 |
OPENFILENAME open; |
mii.fMask = MIIM_STATE; |
83 |
|
mii.fState = state; |
84 |
if (name && strlen (name) < (sizeof (file)-1)) |
SetMenuItemInfo (menu, m_uid, FALSE, &mii); |
85 |
strcpy (file, name); |
} |
86 |
else |
|
87 |
memset (file, 0, sizeof (file)); |
|
88 |
if (!filter) |
/* Retrieve the state of the menu item @m_uid and return it. */ |
89 |
filter = _("All Files (*.*)\0*.*"); |
UINT |
90 |
memset (&open, 0, sizeof (open)); |
get_menu_state (HMENU menu, UINT m_uid) |
91 |
open.lStructSize = sizeof (OPENFILENAME); |
{ |
92 |
open.hInstance = glob_hinst; |
MENUITEMINFO mii; |
93 |
open.lpstrTitle = title; |
|
94 |
open.lpstrFilter = filter; |
memset (&mii, 0, sizeof (mii)); |
95 |
open.hwndOwner = hwnd; |
mii.cbSize = sizeof (mii); |
96 |
open.lpstrFile = file; |
mii.fMask = MIIM_STATE; |
97 |
open.nMaxFile = sizeof (file) - 1; |
GetMenuItemInfo (menu, m_uid, FALSE, &mii); |
98 |
open.Flags = 0; |
return mii.fState; |
99 |
|
} |
100 |
if (id == 0 && GetOpenFileName (&open)) |
|
101 |
return open.lpstrFile; |
|
102 |
else if (id == 1 && GetSaveFileName (&open)) |
enum { |
103 |
return open.lpstrFile; |
CDLG_FILE_OPEN = 0, |
104 |
|
CDLG_FILE_SAVE = 1 |
105 |
return NULL; |
}; |
106 |
} /* get_filename_dlg */ |
|
107 |
|
/* Use the common dialog to request a file from the user. |
108 |
|
id can be either FILE_OPEN or FILE_SAVE. |
109 |
const char * |
The return value is the file name or NULL if cancel was chosen. */ |
110 |
get_folder_dlg (HWND hwnd, const char * title, const char * name) |
const char * |
111 |
{ |
get_filename_dlg (HWND hwnd, int id, const char * title, |
112 |
static char folder[MAX_PATH] = ""; |
const char * filter, const char * name) |
113 |
BROWSEINFO bi; |
{ |
114 |
ITEMIDLIST * il; |
static char file[512] = ""; |
115 |
|
OPENFILENAME open; |
116 |
memset (&bi, 0, sizeof (bi)); |
|
117 |
bi.hwndOwner = hwnd; |
if (name && strlen (name) < (sizeof (file)-1)) |
118 |
if (title) |
strcpy (file, name); |
119 |
bi.lpszTitle = title; |
else |
120 |
if (name && strlen (name) < MAX_PATH-1) |
memset (file, 0, sizeof (file)); |
121 |
strcpy (folder, name); |
if (!filter) |
122 |
else |
filter = _("All Files (*.*)\0*.*\0\0"); |
123 |
memset (folder, 0, sizeof (folder)); |
/* XXX: problem with gettext because of the 'artificial' |
124 |
il = SHBrowseForFolder (&bi); |
double string termination!. */ |
125 |
if (il) { |
memset (&open, 0, sizeof (open)); |
126 |
SHGetPathFromIDList (il, folder); |
open.lStructSize = sizeof (OPENFILENAME); |
127 |
_SHFree (il); |
open.hInstance = glob_hinst; |
128 |
return folder; |
open.lpstrTitle = title; |
129 |
} |
open.lpstrFilter = filter; |
130 |
return NULL; |
open.hwndOwner = hwnd; |
131 |
} |
open.lpstrFile = file; |
132 |
|
open.nMaxFile = sizeof (file) - 1; |
133 |
|
if (id == CDLG_FILE_OPEN) |
134 |
char* |
open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST; |
135 |
get_clip_text (HWND hwnd) |
else |
136 |
{ |
open.Flags = OFN_OVERWRITEPROMPT; |
137 |
HANDLE clipmem; |
|
138 |
char *cliptxt, *p; |
if (id == CDLG_FILE_OPEN && GetOpenFileName (&open)) |
139 |
int len; |
return open.lpstrFile; |
140 |
|
else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open)) |
141 |
if (OpenClipboard (hwnd) == FALSE) |
return open.lpstrFile; |
142 |
return NULL; |
|
143 |
clipmem = GetClipboardData (CF_TEXT); |
return NULL; |
144 |
if (clipmem == NULL) { |
} |
145 |
p = NULL; |
|
146 |
goto leave; |
const char* |
147 |
} |
get_filesave_dlg (HWND hwnd, const char *title, |
148 |
cliptxt = (char *) GlobalLock (clipmem); |
const char *filter, const char *name) |
149 |
if (cliptxt == NULL) { |
{ |
150 |
p = NULL; |
return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name); |
151 |
goto leave; |
} |
152 |
} |
|
153 |
|
const char* |
154 |
len = strlen (cliptxt); |
get_fileopen_dlg (HWND hwnd, const char *title, const char *filter, |
155 |
p = new char[len + 1]; |
const char *name) |
156 |
if (!p) |
{ |
157 |
BUG (NULL); |
return get_filename_dlg (hwnd, CDLG_FILE_OPEN, title, filter, name); |
158 |
memcpy (p, cliptxt, len); |
} |
159 |
p[len] = '\0'; |
|
160 |
GlobalUnlock (clipmem); |
|
161 |
|
/* Use the common dialog to allow the user to select a folder. |
162 |
leave: |
The return value is either the folder path or NULL if cancel was chosen. */ |
163 |
CloseClipboard (); |
const char* |
164 |
return p; |
get_folder_dlg (HWND hwnd, const char *title, const char *name) |
165 |
} /* get_clip_text */ |
{ |
166 |
|
static char folder[MAX_PATH+1] = ""; |
167 |
|
BROWSEINFO bi; |
168 |
int |
ITEMIDLIST *il; |
169 |
set_clip_text (HWND hwnd, const char *text, int nbytes) |
|
170 |
{ |
memset (&bi, 0, sizeof (bi)); |
171 |
HANDLE clipmem; |
bi.hwndOwner = hwnd; |
172 |
int rc = 0; |
if (title) |
173 |
char *p; |
bi.lpszTitle = title; |
174 |
|
if (name && strlen (name) < MAX_PATH-1) |
175 |
if (OpenClipboard (hwnd) == FALSE) |
strcpy (folder, name); |
176 |
return WPTERR_CLIP_OPEN; |
else |
177 |
EmptyClipboard (); |
memset (folder, 0, sizeof (folder)); |
178 |
|
il = SHBrowseForFolder (&bi); |
179 |
clipmem = GlobalAlloc (GHND, nbytes + 1); |
if (il) { |
180 |
if (clipmem == NULL) |
SHGetPathFromIDList (il, folder); |
181 |
BUG (NULL); |
_SHFree (il); |
182 |
p = (char *) GlobalLock (clipmem); |
return folder; |
183 |
if (p == NULL) { |
} |
184 |
rc = WPTERR_GENERAL;; |
return NULL; |
185 |
goto leave; |
} |
186 |
} |
|
187 |
memcpy (p, text, nbytes); |
|
188 |
p[nbytes] = '\0'; |
/* Return the clipboard contents as a string or NULL |
189 |
|
if the clipboard does not contain text. */ |
190 |
GlobalUnlock (clipmem); |
char* |
191 |
SetClipboardData (CF_TEXT, clipmem); |
get_clip_text (HWND hwnd) |
192 |
|
{ |
193 |
leave: |
HANDLE clipmem; |
194 |
CloseClipboard (); |
char *cliptxt, *p; |
195 |
return rc; |
int len; |
196 |
} /* set_clip_text */ |
|
197 |
|
if (OpenClipboard (hwnd) == FALSE) |
198 |
|
return NULL; |
199 |
int |
clipmem = GetClipboardData (CF_TEXT); |
200 |
set_clip_text2 (HWND hwnd, const char *text, int nbytes, int head_foot) |
if (clipmem == NULL) { |
201 |
{ |
p = NULL; |
202 |
char *p, *new_text; |
goto leave; |
203 |
|
} |
204 |
p = get_clip_text (hwnd); |
cliptxt = (char *) GlobalLock (clipmem); |
205 |
if (!p) |
if (cliptxt == NULL) { |
206 |
return WPTERR_CLIP_GET; |
p = NULL; |
207 |
new_text = new char [strlen (p)+strlen (text)+8]; |
goto leave; |
208 |
if (!new_text) |
} |
209 |
BUG (0); |
|
210 |
if (head_foot == 0) |
len = strlen (cliptxt); |
211 |
sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p); |
p = new char[len + 1]; |
212 |
else |
if (!p) |
213 |
sprintf (new_text, "%s\n%s\n\n", p, text); |
BUG (NULL); |
214 |
set_clip_text (hwnd, new_text, strlen (new_text)+1); |
memcpy (p, cliptxt, len); |
215 |
free_if_alloc (p); |
p[len] = '\0'; |
216 |
free_if_alloc (new_text); |
GlobalUnlock (clipmem); |
217 |
return 0; |
|
218 |
} |
leave: |
219 |
|
CloseClipboard (); |
220 |
|
return p; |
221 |
char* |
} |
222 |
make_filename( const char *path, const char *file, const char *ext ) |
|
223 |
{ |
|
224 |
char *p; |
/* Set @text as the new clipboard content. */ |
225 |
size_t size = 0; |
int |
226 |
|
set_clip_text (HWND hwnd, const char *text, int nbytes) |
227 |
if( path && *path ) |
{ |
228 |
size += strlen( path ); |
HANDLE clipmem; |
229 |
if( file && *file ) |
int rc = 0; |
230 |
size += strlen( file ); |
char *p; |
231 |
if( ext && *ext ) |
|
232 |
size += strlen( ext ); |
if (OpenClipboard (hwnd) == FALSE) |
233 |
p = new char[size + 4]; |
return WPTERR_CLIP_OPEN; |
234 |
memset( p, 0, size ); |
EmptyClipboard (); |
235 |
if( path ) { |
|
236 |
strcat( p, path ); |
clipmem = GlobalAlloc (GHND, nbytes + 1); |
237 |
if( path[strlen( path ) -1] != '\\' ) |
if (clipmem == NULL) |
238 |
strcat( p, "\\" ); |
BUG (NULL); |
239 |
} |
p = (char *) GlobalLock (clipmem); |
240 |
if( file ) |
if (p == NULL) { |
241 |
strcat( p, file ); |
rc = WPTERR_GENERAL;; |
242 |
if( ext ) { |
goto leave; |
243 |
strcat( p, "." ); |
} |
244 |
strcat( p, ext ); |
memcpy (p, text, nbytes); |
245 |
} |
p[nbytes] = '\0'; |
246 |
return p; |
|
247 |
} /* make_filename */ |
GlobalUnlock (clipmem); |
248 |
|
SetClipboardData (CF_TEXT, clipmem); |
249 |
|
|
250 |
/* return 0 if it exists, otherwise >0. */ |
leave: |
251 |
int |
CloseClipboard (); |
252 |
file_exist_check (const char * fname) |
return rc; |
253 |
{ |
} /* set_clip_text */ |
254 |
HANDLE fh; |
|
255 |
|
|
256 |
fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ, |
/* Append or prepend some text to the clipboard contents. |
257 |
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); |
If as_footer = 1, append the text otherwise prepend. */ |
258 |
if( fh == INVALID_HANDLE_VALUE ) |
int |
259 |
return WPTERR_FILE_EXIST; |
set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer) |
260 |
CloseHandle( fh ); |
{ |
261 |
return 0; |
char *p, *new_text; |
262 |
} /* file_exist_check */ |
|
263 |
|
p = get_clip_text (hwnd); |
264 |
|
if (!p) |
265 |
int |
return WPTERR_CLIP_GET; |
266 |
dir_exist_check( const char *dir ) |
new_text = new char [strlen (p)+strlen (text)+8]; |
267 |
{ |
if (!new_text) |
268 |
struct stat statbuf; |
BUG (0); |
269 |
|
if (as_footer == 0) |
270 |
if( stat( dir, &statbuf ) == -1 ) |
sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p); |
271 |
return WPTERR_GENERAL; |
else |
272 |
if( statbuf.st_mode & _S_IFDIR ) |
sprintf (new_text, "%s\n%s\n\n", p, text); |
273 |
return 0; |
set_clip_text (hwnd, new_text, strlen (new_text)+1); |
274 |
return WPTERR_GENERAL; |
free_if_alloc (p); |
275 |
} /* dir_exist_check */ |
free_if_alloc (new_text); |
276 |
|
return 0; |
277 |
|
} |
278 |
size_t |
|
279 |
get_file_size( const char *fname ) |
|
280 |
{ |
/* Make a file name out of the path @path, the file @file and |
281 |
size_t fsize; |
an extension. @ext. |
282 |
HANDLE fh; |
Return value: the full file name on success. */ |
283 |
|
char* |
284 |
fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ, |
make_filename (const char *path, const char *file, const char *ext) |
285 |
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); |
{ |
286 |
if( fh == INVALID_HANDLE_VALUE ) |
char *p; |
287 |
return 0; |
size_t size = 0; |
288 |
fsize = GetFileSize( fh, NULL ); |
|
289 |
if( fsize == 0xFFFFFFFF ) |
if( path && *path ) |
290 |
fsize = 0; |
size += strlen( path ); |
291 |
CloseHandle( fh ); |
if( file && *file ) |
292 |
return fsize; |
size += strlen( file ); |
293 |
} /* get_file_size */ |
if( ext && *ext ) |
294 |
|
size += strlen( ext ); |
295 |
|
p = new char[size + 4]; |
296 |
int |
memset( p, 0, size ); |
297 |
init_file_lock( LOCK *ctx, const char *file ) |
if( path ) { |
298 |
{ |
strcat( p, path ); |
299 |
|
if( path[strlen( path ) -1] != '\\' ) |
300 |
ctx->size = get_file_size( file ); |
strcat( p, "\\" ); |
301 |
ctx->file = m_strdup( file ); |
} |
302 |
ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL, |
if( file ) |
303 |
OPEN_ALWAYS, 0, NULL ); |
strcat( p, file ); |
304 |
if( ctx->fh == INVALID_HANDLE_VALUE ) |
if( ext ) { |
305 |
return WPTERR_GENERAL; |
strcat( p, "." ); |
306 |
if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) { |
strcat( p, ext ); |
307 |
CloseHandle( ctx->fh ); |
} |
308 |
ctx->fh = INVALID_HANDLE_VALUE; |
return p; |
309 |
ctx->size = 0; |
} |
310 |
free( ctx->file ); |
|
311 |
return WPTERR_GENERAL; |
|
312 |
} |
/* return 0 if the file @fname exists, otherwise >0. */ |
313 |
return 0; |
int |
314 |
} /* init_file_lock */ |
file_exist_check (const char *fname) |
315 |
|
{ |
316 |
|
struct stat st; |
317 |
void |
if (stat (fname, &st) == -1) |
318 |
release_file_lock( LOCK *ctx ) |
return WPTERR_FILE_EXIST; |
319 |
{ |
return 0; |
320 |
free_if_alloc( ctx->file ); |
} |
321 |
ctx->file = NULL; |
|
322 |
ctx->size = 0; |
|
323 |
CloseHandle( ctx->fh ); |
/* Check if the current folder exists. |
324 |
} /* release_file_lock */ |
Return 0 for success. */ |
325 |
|
int |
326 |
|
dir_exist_check (const char *dir) |
327 |
int |
{ |
328 |
dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc, |
struct stat statbuf; |
329 |
LPARAM param, LPCTSTR title, int title_id ) |
|
330 |
{ |
if (stat (dir, &statbuf) == -1) |
331 |
#ifndef LANG_DE |
return WPTERR_GENERAL; |
332 |
if( FindWindowEx( GetDesktopWindow(), NULL, NULL, title ) ) |
if (statbuf.st_mode & _S_IFDIR) |
333 |
return -1; |
return 0; |
334 |
#else |
return WPTERR_GENERAL; |
335 |
char strdesc[256]; |
} |
336 |
LoadString( glob_hinst, title_id, strdesc, sizeof (strdesc) -1 ); |
|
337 |
if( FindWindowEx( GetDesktopWindow(), NULL, NULL, strdesc ) ) |
|
338 |
return -1; |
/* Return the file size of the given file @fname. */ |
339 |
#endif |
DWORD |
340 |
|
get_file_size (const char *fname) |
341 |
return DialogBoxParam( hinst, name, parent, fnc, param ); |
{ |
342 |
} /* dialog_box_param */ |
DWORD fsize; |
343 |
|
HANDLE fh; |
344 |
|
|
345 |
int |
fh = CreateFile (fname, GENERIC_READ, FILE_SHARE_READ, |
346 |
msg_box( HWND hwnd, const char *text, const char *title, int mode ) |
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
347 |
{ |
if (fh == INVALID_HANDLE_VALUE) |
348 |
mode |= MB_SETFOREGROUND; |
return 0; |
349 |
mode |= MB_TASKMODAL; |
fsize = GetFileSize (fh, NULL); |
350 |
mode |= MB_TOPMOST; |
if (fsize == 0xFFFFFFFF) |
351 |
return MessageBox(hwnd, text, title, mode); |
fsize = 0; |
352 |
} /* msg_box */ |
CloseHandle (fh); |
353 |
|
return fsize; |
354 |
|
} |
355 |
void |
|
356 |
set_active_window( HWND dlg ) |
|
357 |
{ |
/* Start a dialog with the exception that before it is checked that the |
358 |
activ_hwnd = dlg; |
dialog is not already openened. */ |
359 |
} /* set_active_window */ |
int |
360 |
|
dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc, |
361 |
void |
LPARAM param, LPCTSTR title, int title_id) |
362 |
reset_active_window( void ) |
{ |
363 |
{ |
if (FindWindowEx (GetDesktopWindow (), NULL, NULL, title)) |
364 |
activ_hwnd = NULL; |
return -1; |
365 |
} /* reset_active_window */ |
return DialogBoxParam (hinst, name, parent, fnc, param); |
366 |
|
} |
367 |
|
|
368 |
static DWORD CALLBACK |
|
369 |
reminder_thread( void *ctx ) |
/* Wrapper for message box which forces the message box into the |
370 |
{ |
foreground and it is displayed always on top. */ |
371 |
reminder_ctx_s *c = (reminder_ctx_s *)ctx; |
int |
372 |
|
msg_box (HWND hwnd, const char *text, const char *title, int mode) |
373 |
Sleep( c->msecs ); |
{ |
374 |
SetForegroundWindow( activ_hwnd ); |
mode |= MB_SETFOREGROUND; |
375 |
|
mode |= MB_TASKMODAL; |
376 |
return 0; |
mode |= MB_TOPMOST; |
377 |
} /* reminder_thread */ |
return MessageBox(hwnd, text, title, mode); |
378 |
|
} |
379 |
HANDLE |
|
380 |
window_reminder( struct reminder_ctx_s *ctx ) |
|
381 |
{ |
/* Safe strdup version (C++ version). */ |
382 |
DWORD tid = 0; |
char* |
383 |
|
m_strdup (const char *str) |
384 |
return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid ); |
{ |
385 |
} /* window_reminder */ |
char *p = new char[strlen (str) + 1]; |
386 |
|
if (!p) |
387 |
|
BUG (NULL); |
388 |
char * |
strcpy (p, str); |
389 |
m_strdup (const char *str) |
return p; |
390 |
{ |
} |
391 |
char * p = new char[strlen (str) + 1]; |
|
392 |
if (p) |
|
393 |
strcpy (p, str); |
/* Center the hwndChild relative to parent. |
394 |
return p; |
The style param allows to specificy additional styles (like topmost). */ |
395 |
} /* m_strdup */ |
void |
396 |
|
center_window2 (HWND hwndChild, HWND parent, HWND style) |
397 |
|
{ |
398 |
void |
HWND hwndParent; |
399 |
center_window2 (HWND hwndChild, HWND style) |
RECT rChild, rParent; |
400 |
{ |
HDC hdc; |
401 |
HWND hwndParent; |
int wChild, hChild, wParent, hParent; |
402 |
RECT rChild, rParent; |
int wScreen, hScreen, xNew, yNew; |
403 |
HDC hdc; |
int flags = SWP_NOSIZE | SWP_NOZORDER; |
404 |
int wChild, hChild, wParent, hParent; |
|
405 |
int wScreen, hScreen, xNew, yNew; |
hwndParent = parent; |
406 |
int flags = SWP_NOSIZE | SWP_NOZORDER; |
if (hwndParent == NULL) |
407 |
|
hwndParent = GetDesktopWindow (); |
408 |
hwndParent = GetDesktopWindow (); |
GetWindowRect (hwndChild, &rChild); |
409 |
GetWindowRect (hwndChild, &rChild); |
wChild = rChild.right - rChild.left; |
410 |
wChild = rChild.right - rChild.left; |
hChild = rChild.bottom - rChild.top; |
411 |
hChild = rChild.bottom - rChild.top; |
|
412 |
|
GetWindowRect (hwndParent, &rParent); |
413 |
GetWindowRect (hwndParent, &rParent); |
wParent = rParent.right - rParent.left; |
414 |
wParent = rParent.right - rParent.left; |
hParent = rParent.bottom - rParent.top; |
415 |
hParent = rParent.bottom - rParent.top; |
|
416 |
|
hdc = GetDC (hwndChild); |
417 |
hdc = GetDC (hwndChild); |
wScreen = GetDeviceCaps (hdc, HORZRES); |
418 |
wScreen = GetDeviceCaps (hdc, HORZRES); |
hScreen = GetDeviceCaps (hdc, VERTRES); |
419 |
hScreen = GetDeviceCaps (hdc, VERTRES); |
ReleaseDC (hwndChild, hdc); |
420 |
ReleaseDC (hwndChild, hdc); |
xNew = rParent.left + ((wParent - wChild) /2); |
421 |
xNew = rParent.left + ((wParent - wChild) /2); |
if (xNew < 0) |
422 |
if (xNew < 0) |
xNew = 0; |
423 |
xNew = 0; |
else if ((xNew+wChild) > wScreen) |
424 |
else if ((xNew+wChild) > wScreen) |
xNew = wScreen - wChild; |
425 |
xNew = wScreen - wChild; |
yNew = rParent.top + ((hParent - hChild) /2); |
426 |
yNew = rParent.top + ((hParent - hChild) /2); |
if (yNew < 0) |
427 |
if (yNew < 0) |
yNew = 0; |
428 |
yNew = 0; |
else if ((yNew+hChild) > hScreen) |
429 |
else if ((yNew+hChild) > hScreen) |
yNew = hScreen - hChild; |
430 |
yNew = hScreen - hChild; |
if (style == HWND_TOPMOST || style == HWND_NOTOPMOST) |
431 |
if (style == HWND_TOPMOST || style == HWND_NOTOPMOST) |
flags = SWP_NOMOVE | SWP_NOSIZE; |
432 |
flags = SWP_NOMOVE | SWP_NOSIZE; |
SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags); |
433 |
SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags); |
} |
434 |
} |
|
435 |
|
|
436 |
|
/* Center the given hwndChild window with no special style. */ |
437 |
void |
void |
438 |
center_window (HWND hwndChild) |
center_window (HWND hwndChild, HWND hwndParent) |
439 |
{ |
{ |
440 |
center_window2 (hwndChild, NULL); |
center_window2 (hwndChild, hwndParent, NULL); |
441 |
} |
} |
442 |
|
|
443 |
|
|
444 |
|
/* Retrieve the product verion of the given file @fname. |
445 |
|
Format: MAJOR.MINOR.PATCH1.PATCH2 |
446 |
|
Return value: 0 on success. */ |
447 |
|
int |
448 |
|
get_file_version (const char *fname, WORD *major, WORD *minor, |
449 |
|
WORD *patch1, WORD *patch2) |
450 |
|
{ |
451 |
|
VS_FIXEDFILEINFO *inf = {0}; |
452 |
|
char file[MAX_PATH+1] = {0}; |
453 |
|
LPVOID buf, data; |
454 |
|
DWORD arg; |
455 |
|
DWORD size; |
456 |
|
UINT qlen; |
457 |
|
|
458 |
|
strncpy (file, fname, MAX_PATH); |
459 |
|
size = GetFileVersionInfoSize (file, &arg); |
460 |
|
if (!size) |
461 |
|
return -1; |
462 |
|
buf = (LPVOID)new CHAR[size]; |
463 |
|
if (!buf) |
464 |
|
BUG (NULL); |
465 |
|
GetFileVersionInfo (file, 0, size, buf); |
466 |
|
|
467 |
|
qlen=0; |
468 |
|
VerQueryValue (buf, "\\", &data, &qlen); |
469 |
|
if (!qlen) { |
470 |
|
delete [] (char*)buf; |
471 |
|
return -1; |
472 |
|
} |
473 |
|
inf = (VS_FIXEDFILEINFO*)data; |
474 |
|
|
475 |
|
if (major) |
476 |
|
*major = HIWORD (inf->dwProductVersionMS); |
477 |
|
if (minor) |
478 |
|
*minor = LOWORD (inf->dwProductVersionMS); |
479 |
|
if (patch1) |
480 |
|
*patch1 = HIWORD (inf->dwProductVersionLS); |
481 |
|
if (patch2) |
482 |
|
*patch2 = LOWORD (inf->dwProductVersionLS); |
483 |
|
|
484 |
|
delete [] (char*)buf; |
485 |
|
return 0; |
486 |
|
} |
487 |
|
|
488 |
|
|
489 |
|
/* Return date in a format which complies with the |
490 |
|
system locale settings. */ |
491 |
|
const char* |
492 |
|
get_locale_date (long tm_t, char *buf, DWORD buflen) |
493 |
|
{ |
494 |
|
SYSTEMTIME st; |
495 |
|
struct tm *ptm; |
496 |
|
|
497 |
|
ptm = localtime (&tm_t); |
498 |
|
st.wYear = (WORD)ptm->tm_year; |
499 |
|
st.wMonth = (WORD)ptm->tm_mon; |
500 |
|
st.wDay = (WORD)ptm->tm_mday; |
501 |
|
st.wYear += 1900; |
502 |
|
st.wMonth += 1; |
503 |
|
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
504 |
|
NULL, buf, buflen)) |
505 |
|
return NULL; |
506 |
|
return buf; |
507 |
|
} |
508 |
|
|
509 |
|
|
510 |
|
struct reminder_hd_s { |
511 |
|
int msecs; |
512 |
|
HWND dlg; |
513 |
|
HANDLE hd; |
514 |
|
}; |
515 |
|
|
516 |
|
|
517 |
|
static DWORD CALLBACK |
518 |
|
foreground_reminder_thread (void *c) |
519 |
|
{ |
520 |
|
struct reminder_hd_s *ctx = (struct reminder_hd_s *)c; |
521 |
|
Sleep (ctx->msecs); |
522 |
|
SetForegroundWindow (ctx->dlg); |
523 |
|
CloseHandle (ctx->hd); |
524 |
|
delete ctx; |
525 |
|
ExitThread (0); |
526 |
|
return 0; |
527 |
|
} |
528 |
|
|
529 |
|
/* Try to force the window @dlg to the foreground. |
530 |
|
On NT5 or later this will not work if the user |
531 |
|
is working in another window (console for example). */ |
532 |
|
void |
533 |
|
force_foreground_window (HWND dlg, int msecs) |
534 |
|
{ |
535 |
|
struct reminder_hd_s *hd; |
536 |
|
DWORD tid; |
537 |
|
|
538 |
|
hd = new reminder_hd_s; |
539 |
|
hd->dlg = dlg; |
540 |
|
hd->msecs = msecs; |
541 |
|
hd->hd = CreateThread (NULL, 0, foreground_reminder_thread, |
542 |
|
hd, NULL, &tid); |
543 |
|
} |