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