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 "wptUTF8.h" |
39 |
* The the text of a menu item. |
#include "wptTypes.h" |
40 |
*/ |
|
41 |
void |
|
42 |
set_menu_text (HMENU menu, int m_uid, const char *text) |
extern "C" void _SHFree (void *p); |
43 |
{ |
|
44 |
MENUITEMINFO mii; |
|
45 |
char menu_text[80]; |
|
46 |
|
/* Insert a new item into the menu @hm at position @pos. */ |
47 |
memset (&mii, 0, sizeof (mii)); |
void |
48 |
mii.cbSize = sizeof (mii); |
insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text) |
49 |
mii.fMask = MIIM_TYPE; |
{ |
50 |
mii.dwTypeData = menu_text; |
MENUITEMINFO mi; |
51 |
mii.cch = sizeof (menu_text); |
|
52 |
GetMenuItemInfo (menu, m_uid, FALSE, &mii); |
memset (&mi, 0, sizeof mi); |
53 |
|
mi.cbSize = sizeof mi; |
54 |
memset (&mii, 0, sizeof mii); |
mi.fType = MF_STRING; |
55 |
mii.cbSize = sizeof mii; |
mi.dwTypeData = (char *)text; |
56 |
mii.fMask = MIIM_TYPE; |
mi.cch = strlen (text); |
57 |
mii.fType = MFT_STRING; |
mi.wID = m_id; |
58 |
mii.dwTypeData = (char *) text; |
mi.fMask = MIIM_ID|MIIM_DATA| MIIM_TYPE; |
59 |
SetMenuItemInfo (menu, m_uid, FALSE, &mii); |
InsertMenuItem (hm, pos, FALSE, &mi); |
60 |
} /* set_menu_text */ |
} |
61 |
|
|
62 |
|
|
63 |
void |
static void |
64 |
set_menu_state (HMENU menu, int m_uid, int state) |
set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text) |
65 |
{ |
{ |
66 |
MENUITEMINFO mii; |
MENUITEMINFO mii; |
67 |
|
|
68 |
memset( &mii, 0, sizeof (mii) ); |
memset (&mii, 0, sizeof mii); |
69 |
mii.cbSize = sizeof (mii); |
mii.cbSize = sizeof mii; |
70 |
mii.fMask = MIIM_STATE; |
mii.fMask = MIIM_TYPE; |
71 |
mii.fState = state; |
mii.fType = MFT_STRING; |
72 |
SetMenuItemInfo (menu, m_uid, FALSE, &mii); |
mii.dwTypeData = (char *) text; |
73 |
} /* set_menu_state */ |
SetMenuItemInfo (menu, m_uid, by_pos? TRUE : FALSE, &mii); |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
const char * |
/* Set the text of a menu item @m_uid to @text. */ |
78 |
get_filename_dlg (HWND hwnd, int id, const char * title, |
void |
79 |
const char * filter, const char * name) |
set_menu_text (HMENU menu, UINT m_uid, const char *text) |
80 |
{ |
{ |
81 |
static char file[512] = ""; |
set_menu_text_ext (menu, 0, m_uid, text); |
82 |
OPENFILENAME open; |
} |
83 |
|
|
84 |
if (name && strlen (name) < (sizeof (file)-1)) |
|
85 |
strcpy (file, name); |
/* Set the text of a menu item with the position @pos to @text. */ |
86 |
else |
void |
87 |
memset (file, 0, sizeof (file)); |
set_menu_text_bypos (HMENU menu, UINT pos, const char *text) |
88 |
if (!filter) |
{ |
89 |
filter = _("All Files (*.*)\0*.*"); |
set_menu_text_ext (menu, 1, pos, text); |
90 |
memset (&open, 0, sizeof (open)); |
} |
91 |
open.lStructSize = sizeof (OPENFILENAME); |
|
92 |
open.hInstance = glob_hinst; |
|
93 |
open.lpstrTitle = title; |
/* Set the state of a menu item @m_uid to @state. */ |
94 |
open.lpstrFilter = filter; |
void |
95 |
open.hwndOwner = hwnd; |
set_menu_state (HMENU menu, UINT m_uid, UINT state) |
96 |
open.lpstrFile = file; |
{ |
97 |
open.nMaxFile = sizeof (file) - 1; |
MENUITEMINFO mii; |
98 |
open.Flags = 0; |
|
99 |
|
memset (&mii, 0, sizeof (mii)); |
100 |
if (id == 0 && GetOpenFileName (&open)) |
mii.cbSize = sizeof (mii); |
101 |
return open.lpstrFile; |
mii.fMask = MIIM_STATE; |
102 |
else if (id == 1 && GetSaveFileName (&open)) |
mii.fState = state; |
103 |
return open.lpstrFile; |
SetMenuItemInfo (menu, m_uid, FALSE, &mii); |
104 |
|
} |
105 |
return NULL; |
|
106 |
} /* get_filename_dlg */ |
|
107 |
|
/* Retrieve the state of the menu item @m_uid and return it. */ |
108 |
|
UINT |
109 |
const char * |
get_menu_state (HMENU menu, UINT m_uid) |
110 |
get_folder_dlg (HWND hwnd, const char * title, const char * name) |
{ |
111 |
{ |
MENUITEMINFO mii; |
112 |
static char folder[MAX_PATH] = ""; |
|
113 |
BROWSEINFO bi; |
memset (&mii, 0, sizeof (mii)); |
114 |
ITEMIDLIST * il; |
mii.cbSize = sizeof (mii); |
115 |
|
mii.fMask = MIIM_STATE; |
116 |
memset (&bi, 0, sizeof (bi)); |
GetMenuItemInfo (menu, m_uid, FALSE, &mii); |
117 |
bi.hwndOwner = hwnd; |
return mii.fState; |
118 |
if (title) |
} |
119 |
bi.lpszTitle = title; |
|
120 |
if (name && strlen (name) < MAX_PATH-1) |
|
121 |
strcpy (folder, name); |
enum { |
122 |
else |
CDLG_FILE_OPEN = 0, |
123 |
memset (folder, 0, sizeof (folder)); |
CDLG_FILE_SAVE = 1 |
124 |
il = SHBrowseForFolder (&bi); |
}; |
125 |
if (il) { |
|
126 |
SHGetPathFromIDList (il, folder); |
/* Use the common dialog to request a file from the user. |
127 |
_SHFree (il); |
id can be either FILE_OPEN or FILE_SAVE. |
128 |
return folder; |
The return value is the file name or NULL if cancel was chosen. */ |
129 |
} |
const char * |
130 |
return NULL; |
get_filename_dlg (HWND hwnd, int id, const char *title, |
131 |
} |
const char *filter, const char *name) |
132 |
|
{ |
133 |
|
static char file[512] = ""; |
134 |
char* |
OPENFILENAME open; |
135 |
get_clip_text (HWND hwnd) |
|
136 |
{ |
if (name && strlen (name) < (sizeof (file)-1)) |
137 |
HANDLE clipmem; |
strcpy (file, name); |
138 |
char *cliptxt, *p; |
else |
139 |
int len; |
memset (file, 0, sizeof (file)); |
140 |
|
if (!filter) |
141 |
if (OpenClipboard (hwnd) == FALSE) |
filter = "All Files (*.*)\0*.*\0\0"; |
142 |
return NULL; |
/* XXX: problem with gettext because of the 'artificial' |
143 |
clipmem = GetClipboardData (CF_TEXT); |
double string termination!. */ |
144 |
if (clipmem == NULL) { |
memset (&open, 0, sizeof (open)); |
145 |
p = NULL; |
open.lStructSize = sizeof (OPENFILENAME); |
146 |
goto leave; |
open.hInstance = glob_hinst; |
147 |
} |
open.lpstrTitle = title; |
148 |
cliptxt = (char *) GlobalLock (clipmem); |
open.lpstrFilter = filter; |
149 |
if (cliptxt == NULL) { |
open.hwndOwner = hwnd; |
150 |
p = NULL; |
open.lpstrFile = file; |
151 |
goto leave; |
open.nMaxFile = sizeof (file) - 1; |
152 |
} |
if (id == CDLG_FILE_OPEN) |
153 |
|
open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST; |
154 |
len = strlen (cliptxt); |
else |
155 |
p = new char[len + 1]; |
open.Flags = OFN_OVERWRITEPROMPT; |
156 |
if (!p) |
|
157 |
BUG (NULL); |
if (id == CDLG_FILE_OPEN && GetOpenFileName (&open)) |
158 |
memcpy (p, cliptxt, len); |
return open.lpstrFile; |
159 |
p[len] = '\0'; |
else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open)) |
160 |
GlobalUnlock (clipmem); |
return open.lpstrFile; |
161 |
|
|
162 |
leave: |
return NULL; |
163 |
CloseClipboard (); |
} |
164 |
return p; |
|
165 |
} /* get_clip_text */ |
const char* |
166 |
|
get_filesave_dlg (HWND hwnd, const char *title, |
167 |
|
const char *filter, const char *name) |
168 |
int |
{ |
169 |
set_clip_text (HWND hwnd, const char *text, int nbytes) |
return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name); |
170 |
{ |
} |
171 |
HANDLE clipmem; |
|
172 |
int rc = 0; |
const char* |
173 |
char *p; |
get_fileopen_dlg (HWND hwnd, const char *title, const char *filter, |
174 |
|
const char *name) |
175 |
if (OpenClipboard (hwnd) == FALSE) |
{ |
176 |
return WPTERR_CLIP_OPEN; |
return get_filename_dlg (hwnd, CDLG_FILE_OPEN, title, filter, name); |
177 |
EmptyClipboard (); |
} |
178 |
|
|
179 |
clipmem = GlobalAlloc (GHND, nbytes + 1); |
|
180 |
if (clipmem == NULL) |
/* Use the common dialog to allow the user to select a folder. |
181 |
BUG (NULL); |
The return value is either the folder path or NULL if cancel was chosen. */ |
182 |
p = (char *) GlobalLock (clipmem); |
const char* |
183 |
if (p == NULL) { |
get_folder_dlg (HWND hwnd, const char *title, const char *name) |
184 |
rc = WPTERR_GENERAL;; |
{ |
185 |
goto leave; |
static char folder[MAX_PATH+1] = ""; |
186 |
} |
BROWSEINFO bi; |
187 |
memcpy (p, text, nbytes); |
ITEMIDLIST *il; |
188 |
p[nbytes] = '\0'; |
|
189 |
|
memset (&bi, 0, sizeof (bi)); |
190 |
GlobalUnlock (clipmem); |
bi.hwndOwner = hwnd; |
191 |
SetClipboardData (CF_TEXT, clipmem); |
if (title) |
192 |
|
bi.lpszTitle = title; |
193 |
leave: |
if (name && strlen (name) < MAX_PATH-1) |
194 |
CloseClipboard (); |
strcpy (folder, name); |
195 |
return rc; |
else |
196 |
} /* set_clip_text */ |
memset (folder, 0, sizeof (folder)); |
197 |
|
il = SHBrowseForFolder (&bi); |
198 |
|
if (il) { |
199 |
int |
SHGetPathFromIDList (il, folder); |
200 |
set_clip_text2 (HWND hwnd, const char *text, int nbytes, int head_foot) |
_SHFree (il); |
201 |
{ |
return folder; |
202 |
char *p, *new_text; |
} |
203 |
|
return NULL; |
204 |
p = get_clip_text (hwnd); |
} |
205 |
if (!p) |
|
206 |
return WPTERR_CLIP_GET; |
|
207 |
new_text = new char [strlen (p)+strlen (text)+8]; |
/* Return the clipboard contents as a string or NULL |
208 |
if (!new_text) |
if the clipboard does not contain text. */ |
209 |
BUG (0); |
char* |
210 |
if (head_foot == 0) |
get_clip_text (HWND hwnd) |
211 |
sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p); |
{ |
212 |
else |
HANDLE clipmem; |
213 |
sprintf (new_text, "%s\n%s\n\n", p, text); |
char *cliptxt, *p; |
214 |
set_clip_text (hwnd, new_text, strlen (new_text)+1); |
int len; |
215 |
free_if_alloc (p); |
|
216 |
free_if_alloc (new_text); |
if (OpenClipboard (hwnd) == FALSE) |
217 |
return 0; |
return NULL; |
218 |
} |
clipmem = GetClipboardData (CF_TEXT); |
219 |
|
if (clipmem == NULL) { |
220 |
|
p = NULL; |
221 |
char* |
goto leave; |
222 |
make_filename( const char *path, const char *file, const char *ext ) |
} |
223 |
{ |
cliptxt = (char *) GlobalLock (clipmem); |
224 |
char *p; |
if (cliptxt == NULL) { |
225 |
size_t size = 0; |
p = NULL; |
226 |
|
goto leave; |
227 |
if( path && *path ) |
} |
228 |
size += strlen( path ); |
|
229 |
if( file && *file ) |
len = strlen (cliptxt); |
230 |
size += strlen( file ); |
p = new char[len + 1]; |
231 |
if( ext && *ext ) |
if (!p) |
232 |
size += strlen( ext ); |
BUG (NULL); |
233 |
p = new char[size + 4]; |
memcpy (p, cliptxt, len); |
234 |
memset( p, 0, size ); |
p[len] = '\0'; |
235 |
if( path ) { |
GlobalUnlock (clipmem); |
236 |
strcat( p, path ); |
|
237 |
if( path[strlen( path ) -1] != '\\' ) |
leave: |
238 |
strcat( p, "\\" ); |
CloseClipboard (); |
239 |
} |
return p; |
240 |
if( file ) |
} |
241 |
strcat( p, file ); |
|
242 |
if( ext ) { |
|
243 |
strcat( p, "." ); |
/* Set @text as the new clipboard content. */ |
244 |
strcat( p, ext ); |
int |
245 |
} |
set_clip_text (HWND hwnd, const char *text, int nbytes) |
246 |
return p; |
{ |
247 |
} /* make_filename */ |
HANDLE clipmem; |
248 |
|
int rc = 0; |
249 |
|
char *p; |
250 |
/* return 0 if it exists, otherwise >0. */ |
|
251 |
int |
if (OpenClipboard (hwnd) == FALSE) |
252 |
file_exist_check (const char * fname) |
return WPTERR_CLIP_OPEN; |
253 |
{ |
EmptyClipboard (); |
254 |
HANDLE fh; |
|
255 |
|
clipmem = GlobalAlloc (GHND, nbytes + 1); |
256 |
fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ, |
if (clipmem == NULL) |
257 |
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); |
BUG (NULL); |
258 |
if( fh == INVALID_HANDLE_VALUE ) |
p = (char *) GlobalLock (clipmem); |
259 |
return WPTERR_FILE_EXIST; |
if (p == NULL) { |
260 |
CloseHandle( fh ); |
CloseClipboard (); |
261 |
return 0; |
GlobalFree (clipmem); |
262 |
} /* file_exist_check */ |
return WPTERR_GENERAL; |
263 |
|
} |
264 |
|
memcpy (p, text, nbytes); |
265 |
int |
p[nbytes] = '\0'; |
266 |
dir_exist_check( const char *dir ) |
|
267 |
{ |
SetClipboardData (CF_TEXT, clipmem); |
268 |
struct stat statbuf; |
GlobalUnlock (clipmem); |
269 |
|
CloseClipboard (); |
270 |
if( stat( dir, &statbuf ) == -1 ) |
GlobalFree (clipmem); |
271 |
return WPTERR_GENERAL; |
|
272 |
if( statbuf.st_mode & _S_IFDIR ) |
return rc; |
273 |
return 0; |
} |
274 |
return WPTERR_GENERAL; |
|
275 |
} /* dir_exist_check */ |
|
276 |
|
/* Append or prepend some text to the clipboard contents. |
277 |
|
If as_footer = 1, append the text otherwise prepend. */ |
278 |
size_t |
int |
279 |
get_file_size( const char *fname ) |
set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer) |
280 |
{ |
{ |
281 |
size_t fsize; |
char *p, *new_text; |
282 |
HANDLE fh; |
|
283 |
|
p = get_clip_text (hwnd); |
284 |
fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ, |
if (!p) |
285 |
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); |
return WPTERR_CLIP_GET; |
286 |
if( fh == INVALID_HANDLE_VALUE ) |
new_text = new char [strlen (p)+strlen (text)+8]; |
287 |
return 0; |
if (!new_text) |
288 |
fsize = GetFileSize( fh, NULL ); |
BUG (0); |
289 |
if( fsize == 0xFFFFFFFF ) |
if (as_footer == 0) |
290 |
fsize = 0; |
sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p); |
291 |
CloseHandle( fh ); |
else |
292 |
return fsize; |
sprintf (new_text, "%s\n%s\n\n", p, text); |
293 |
} /* get_file_size */ |
set_clip_text (hwnd, new_text, strlen (new_text)+1); |
294 |
|
free_if_alloc (p); |
295 |
|
free_if_alloc (new_text); |
296 |
int |
return 0; |
297 |
init_file_lock( LOCK *ctx, const char *file ) |
} |
298 |
{ |
|
299 |
|
|
300 |
ctx->size = get_file_size( file ); |
/* Make a file name out of the path @path, the file @file and |
301 |
ctx->file = m_strdup( file ); |
an extension. @ext. |
302 |
ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL, |
Return value: the full file name on success. */ |
303 |
OPEN_ALWAYS, 0, NULL ); |
char* |
304 |
if( ctx->fh == INVALID_HANDLE_VALUE ) |
make_filename (const char *path, const char *file, const char *ext) |
305 |
return WPTERR_GENERAL; |
{ |
306 |
if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) { |
char *p; |
307 |
CloseHandle( ctx->fh ); |
size_t size = 0; |
308 |
ctx->fh = INVALID_HANDLE_VALUE; |
|
309 |
ctx->size = 0; |
if( path && *path ) |
310 |
free( ctx->file ); |
size += strlen( path ); |
311 |
return WPTERR_GENERAL; |
if( file && *file ) |
312 |
} |
size += strlen( file ); |
313 |
return 0; |
if( ext && *ext ) |
314 |
} /* init_file_lock */ |
size += strlen( ext ); |
315 |
|
p = new char[size + 4]; |
316 |
|
memset( p, 0, size ); |
317 |
void |
if( path ) { |
318 |
release_file_lock( LOCK *ctx ) |
strcat( p, path ); |
319 |
{ |
if( path[strlen( path ) -1] != '\\' ) |
320 |
free_if_alloc( ctx->file ); |
strcat( p, "\\" ); |
321 |
ctx->file = NULL; |
} |
322 |
ctx->size = 0; |
if( file ) |
323 |
CloseHandle( ctx->fh ); |
strcat( p, file ); |
324 |
} /* release_file_lock */ |
if( ext ) { |
325 |
|
strcat( p, "." ); |
326 |
|
strcat( p, ext ); |
327 |
int |
} |
328 |
dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc, |
return p; |
329 |
LPARAM param, LPCTSTR title, int title_id ) |
} |
330 |
{ |
|
331 |
#ifndef LANG_DE |
|
332 |
if( FindWindowEx( GetDesktopWindow(), NULL, NULL, title ) ) |
/* Generate a file name from a special dirctory. */ |
333 |
return -1; |
char* |
334 |
#else |
make_special_filename (int folder, const char *file, const char *ext) |
335 |
char strdesc[256]; |
{ |
336 |
LoadString( glob_hinst, title_id, strdesc, sizeof (strdesc) -1 ); |
BOOL ec; |
337 |
if( FindWindowEx( GetDesktopWindow(), NULL, NULL, strdesc ) ) |
char path[MAX_PATH], *p; |
338 |
return -1; |
size_t n=0; |
339 |
#endif |
|
340 |
|
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
341 |
return DialogBoxParam( hinst, name, parent, fnc, param ); |
ec = SHGetSpecialFolderPath (HWND_DESKTOP, path, folder, TRUE); |
342 |
} /* dialog_box_param */ |
if (ec != 1) { |
343 |
|
log_debug ("SHGetSpecialFolderPath() failed\r\n", (int)GetLastError ()); |
344 |
|
return NULL; |
345 |
int |
} |
346 |
msg_box( HWND hwnd, const char *text, const char *title, int mode ) |
|
347 |
{ |
n = strlen (path)+1; |
348 |
mode |= MB_SETFOREGROUND; |
if (file) |
349 |
mode |= MB_TASKMODAL; |
n += strlen (file)+1; |
350 |
mode |= MB_TOPMOST; |
if (ext) |
351 |
return MessageBox(hwnd, text, title, mode); |
n += strlen (ext)+1; |
352 |
} /* msg_box */ |
p = new char[n+2]; |
353 |
|
if (!p) |
354 |
|
BUG (0); |
355 |
void |
memset (p, 0, n+2); |
356 |
set_active_window( HWND dlg ) |
strcpy (p, path); |
357 |
{ |
if (file) { |
358 |
activ_hwnd = dlg; |
strcat (p, "\\"); |
359 |
} /* set_active_window */ |
strcat (p, file); |
360 |
|
} |
361 |
void |
if (ext) |
362 |
reset_active_window( void ) |
strcat (p, ext); |
363 |
{ |
return p; |
364 |
activ_hwnd = NULL; |
} |
365 |
} /* reset_active_window */ |
|
366 |
|
|
367 |
|
/* return 0 if the file @fname exists, otherwise >0. */ |
368 |
static DWORD CALLBACK |
int |
369 |
reminder_thread( void *ctx ) |
file_exist_check (const char *fname) |
370 |
{ |
{ |
371 |
reminder_ctx_s *c = (reminder_ctx_s *)ctx; |
struct stat st; |
372 |
|
if (stat (fname, &st) == -1) |
373 |
Sleep( c->msecs ); |
return WPTERR_FILE_EXIST; |
374 |
SetForegroundWindow( activ_hwnd ); |
return 0; |
375 |
|
} |
376 |
return 0; |
|
377 |
} /* reminder_thread */ |
|
378 |
|
/* Check if the current folder exists. |
379 |
HANDLE |
Return 0 for success. */ |
380 |
window_reminder( struct reminder_ctx_s *ctx ) |
int |
381 |
{ |
dir_exist_check (const char *dir) |
382 |
DWORD tid = 0; |
{ |
383 |
|
struct stat statbuf; |
384 |
return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid ); |
|
385 |
} /* window_reminder */ |
if (stat (dir, &statbuf) == -1) |
386 |
|
return WPTERR_GENERAL; |
387 |
|
if (statbuf.st_mode & _S_IFDIR) |
388 |
char * |
return 0; |
389 |
m_strdup (const char *str) |
return WPTERR_GENERAL; |
390 |
{ |
} |
391 |
char * p = new char[strlen (str) + 1]; |
|
392 |
if (p) |
|
393 |
strcpy (p, str); |
/* Return the file size of the given file @fname. */ |
394 |
return p; |
DWORD |
395 |
} /* m_strdup */ |
get_file_size (const char *fname) |
396 |
|
{ |
397 |
|
DWORD fsize; |
398 |
void |
HANDLE fh; |
399 |
center_window2 (HWND hwndChild, HWND style) |
|
400 |
{ |
fh = CreateFile (fname, GENERIC_READ, FILE_SHARE_READ, |
401 |
HWND hwndParent; |
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
402 |
RECT rChild, rParent; |
if (fh == INVALID_HANDLE_VALUE) |
403 |
HDC hdc; |
return 0; |
404 |
int wChild, hChild, wParent, hParent; |
fsize = GetFileSize (fh, NULL); |
405 |
int wScreen, hScreen, xNew, yNew; |
if (fsize == 0xFFFFFFFF) |
406 |
int flags = SWP_NOSIZE | SWP_NOZORDER; |
fsize = 0; |
407 |
|
CloseHandle (fh); |
408 |
hwndParent = GetDesktopWindow (); |
return fsize; |
409 |
GetWindowRect (hwndChild, &rChild); |
} |
410 |
wChild = rChild.right - rChild.left; |
|
411 |
hChild = rChild.bottom - rChild.top; |
|
412 |
|
/* Start a dialog with the exception that before it is checked that the |
413 |
GetWindowRect (hwndParent, &rParent); |
dialog is not already openened. */ |
414 |
wParent = rParent.right - rParent.left; |
int |
415 |
hParent = rParent.bottom - rParent.top; |
dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc, |
416 |
|
LPARAM param, LPCTSTR title, int title_id) |
417 |
hdc = GetDC (hwndChild); |
{ |
418 |
wScreen = GetDeviceCaps (hdc, HORZRES); |
if (FindWindowEx (GetDesktopWindow (), NULL, NULL, title)) |
419 |
hScreen = GetDeviceCaps (hdc, VERTRES); |
return -1; |
420 |
ReleaseDC (hwndChild, hdc); |
return DialogBoxParam (hinst, name, parent, fnc, param); |
421 |
xNew = rParent.left + ((wParent - wChild) /2); |
} |
422 |
if (xNew < 0) |
|
423 |
xNew = 0; |
|
424 |
else if ((xNew+wChild) > wScreen) |
/* Wrapper for message box which forces the message box into the |
425 |
xNew = wScreen - wChild; |
foreground and it is displayed always on top. */ |
426 |
yNew = rParent.top + ((hParent - hChild) /2); |
int |
427 |
if (yNew < 0) |
msg_box (HWND hwnd, const char *text, const char *title, int mode) |
428 |
yNew = 0; |
{ |
429 |
else if ((yNew+hChild) > hScreen) |
mode |= MB_SETFOREGROUND; |
430 |
yNew = hScreen - hChild; |
mode |= MB_TASKMODAL; |
431 |
if (style == HWND_TOPMOST || style == HWND_NOTOPMOST) |
mode |= MB_TOPMOST; |
432 |
flags = SWP_NOMOVE | SWP_NOSIZE; |
return MessageBox(hwnd, text, title, mode); |
433 |
SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags); |
} |
434 |
} |
|
435 |
|
|
436 |
|
/* Safe strdup version (C++ version). */ |
437 |
void |
char* |
438 |
center_window (HWND hwndChild) |
m_strdup (const char *str) |
439 |
{ |
{ |
440 |
center_window2 (hwndChild, NULL); |
char *p = new char[strlen (str) + 1]; |
441 |
} |
if (!p) |
442 |
|
BUG (NULL); |
443 |
|
strcpy (p, str); |
444 |
|
return p; |
445 |
|
} |
446 |
|
|
447 |
|
|
448 |
|
/* Center the hwndChild relative to parent. |
449 |
|
The style param allows to specificy additional styles (like topmost). */ |
450 |
|
void |
451 |
|
center_window2 (HWND hwndChild, HWND parent, HWND style) |
452 |
|
{ |
453 |
|
HWND hwndParent; |
454 |
|
RECT rChild, rParent; |
455 |
|
HDC hdc; |
456 |
|
int wChild, hChild, wParent, hParent; |
457 |
|
int wScreen, hScreen, xNew, yNew; |
458 |
|
int flags = SWP_NOSIZE | SWP_NOZORDER; |
459 |
|
|
460 |
|
hwndParent = parent; |
461 |
|
if (hwndParent == NULL) |
462 |
|
hwndParent = GetDesktopWindow (); |
463 |
|
GetWindowRect (hwndChild, &rChild); |
464 |
|
wChild = rChild.right - rChild.left; |
465 |
|
hChild = rChild.bottom - rChild.top; |
466 |
|
|
467 |
|
GetWindowRect (hwndParent, &rParent); |
468 |
|
wParent = rParent.right - rParent.left; |
469 |
|
hParent = rParent.bottom - rParent.top; |
470 |
|
|
471 |
|
hdc = GetDC (hwndChild); |
472 |
|
wScreen = GetDeviceCaps (hdc, HORZRES); |
473 |
|
hScreen = GetDeviceCaps (hdc, VERTRES); |
474 |
|
ReleaseDC (hwndChild, hdc); |
475 |
|
xNew = rParent.left + ((wParent - wChild) /2); |
476 |
|
if (xNew < 0) |
477 |
|
xNew = 0; |
478 |
|
else if ((xNew+wChild) > wScreen) |
479 |
|
xNew = wScreen - wChild; |
480 |
|
yNew = rParent.top + ((hParent - hChild) /2); |
481 |
|
if (yNew < 0) |
482 |
|
yNew = 0; |
483 |
|
else if ((yNew+hChild) > hScreen) |
484 |
|
yNew = hScreen - hChild; |
485 |
|
if (style == HWND_TOPMOST || style == HWND_NOTOPMOST) |
486 |
|
flags = SWP_NOMOVE | SWP_NOSIZE; |
487 |
|
SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags); |
488 |
|
} |
489 |
|
|
490 |
|
|
491 |
|
/* Center the given hwndChild window with no special style. */ |
492 |
|
void |
493 |
|
center_window (HWND hwndChild, HWND hwndParent) |
494 |
|
{ |
495 |
|
center_window2 (hwndChild, hwndParent, NULL); |
496 |
|
} |
497 |
|
|
498 |
|
|
499 |
|
/* Retrieve the product verion of the given file @fname. |
500 |
|
Format: MAJOR.MINOR.PATCH1.PATCH2 |
501 |
|
Return value: 0 on success. */ |
502 |
|
int |
503 |
|
get_file_version (const char *fname, WORD *major, WORD *minor, |
504 |
|
WORD *patch1, WORD *patch2) |
505 |
|
{ |
506 |
|
VS_FIXEDFILEINFO *inf; |
507 |
|
char file[MAX_PATH+1] = {0}; |
508 |
|
LPVOID buf, data; |
509 |
|
DWORD arg; |
510 |
|
DWORD size; |
511 |
|
UINT qlen; |
512 |
|
|
513 |
|
strncpy (file, fname, MAX_PATH); |
514 |
|
size = GetFileVersionInfoSize (file, &arg); |
515 |
|
if (!size) |
516 |
|
return -1; |
517 |
|
buf = (LPVOID)new char[size]; |
518 |
|
if (!buf) |
519 |
|
BUG (NULL); |
520 |
|
GetFileVersionInfo (file, 0, size, buf); |
521 |
|
|
522 |
|
qlen=0; |
523 |
|
VerQueryValue (buf, "\\", &data, &qlen); |
524 |
|
if (!qlen) { |
525 |
|
delete [](char*)buf; |
526 |
|
return -1; |
527 |
|
} |
528 |
|
inf = (VS_FIXEDFILEINFO*)data; |
529 |
|
|
530 |
|
if (major) |
531 |
|
*major = HIWORD (inf->dwProductVersionMS); |
532 |
|
if (minor) |
533 |
|
*minor = LOWORD (inf->dwProductVersionMS); |
534 |
|
if (patch1) |
535 |
|
*patch1 = HIWORD (inf->dwProductVersionLS); |
536 |
|
if (patch2) |
537 |
|
*patch2 = LOWORD (inf->dwProductVersionLS); |
538 |
|
|
539 |
|
delete [](char*)buf; |
540 |
|
return 0; |
541 |
|
} |
542 |
|
|
543 |
|
|
544 |
|
/* Return date in a format which complies with the |
545 |
|
system locale settings. */ |
546 |
|
const char* |
547 |
|
get_locale_date (long tm_t, char *buf, DWORD buflen) |
548 |
|
{ |
549 |
|
SYSTEMTIME st; |
550 |
|
struct tm *ptm; |
551 |
|
|
552 |
|
ptm = localtime (&tm_t); |
553 |
|
st.wYear = (WORD)ptm->tm_year; |
554 |
|
st.wMonth = (WORD)ptm->tm_mon; |
555 |
|
st.wDay = (WORD)ptm->tm_mday; |
556 |
|
st.wYear += 1900; |
557 |
|
st.wMonth += 1; |
558 |
|
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
559 |
|
NULL, buf, buflen)) |
560 |
|
return NULL; |
561 |
|
return buf; |
562 |
|
} |
563 |
|
|
564 |
|
|
565 |
|
/* Generate a temporary file name by using the users |
566 |
|
temp path and optionally a name @name provided by the caller. |
567 |
|
Return value: 0 on success. */ |
568 |
|
int |
569 |
|
get_temp_name (char *buf, DWORD buflen, const char *name) |
570 |
|
{ |
571 |
|
char tmp[32]; |
572 |
|
|
573 |
|
if (!name) { |
574 |
|
sprintf (tmp, "%08lX", GetTickCount ()); |
575 |
|
name = tmp; |
576 |
|
} |
577 |
|
|
578 |
|
/* in the mobile mode we use a local temp folder |
579 |
|
with the fixed name 'temp'. */ |
580 |
|
if (mobile_mode_active) { |
581 |
|
_snprintf (buf, buflen-1, "temp\\%s", name); |
582 |
|
return 0; |
583 |
|
} |
584 |
|
|
585 |
|
if (!GetTempPath (buflen - strlen (name) -2, buf)) { |
586 |
|
log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ()); |
587 |
|
return -1; |
588 |
|
} |
589 |
|
strcat (buf, name); |
590 |
|
return 0; |
591 |
|
} |
592 |
|
|
593 |
|
|
594 |
|
void |
595 |
|
ListBox_AddString_utf8 (HWND lb, const char *txt) |
596 |
|
{ |
597 |
|
char *utf8_txt; |
598 |
|
|
599 |
|
utf8_txt = utf8_to_native (txt); |
600 |
|
SendMessage (lb, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt)); |
601 |
|
safe_free (utf8_txt); |
602 |
|
} |
603 |
|
|
604 |
|
|
605 |
|
void |
606 |
|
ComboBox_AddString_utf8 (HWND cb, const char *txt) |
607 |
|
{ |
608 |
|
|
609 |
|
char *utf8_txt; |
610 |
|
|
611 |
|
utf8_txt = utf8_to_native (txt); |
612 |
|
SendMessage ((cb), CB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt)); |
613 |
|
safe_free (utf8_txt); |
614 |
|
} |
615 |
|
|
616 |
|
|
617 |
|
/* GetDlgItemText replacement with UTF8 support. */ |
618 |
|
int |
619 |
|
GetDlgItemText_utf8 (HWND dlg, int id, char **r_txt) |
620 |
|
{ |
621 |
|
int len = GetWindowTextLength (GetDlgItem (dlg, id)); |
622 |
|
char *txt; |
623 |
|
|
624 |
|
*r_txt = NULL; |
625 |
|
if (len < 1) |
626 |
|
return 0; |
627 |
|
txt = new char[len+2]; |
628 |
|
if (!txt) |
629 |
|
BUG (NULL); |
630 |
|
GetDlgItemText (dlg, id, txt, len+1); |
631 |
|
*r_txt = native_to_utf8 (txt); |
632 |
|
free_if_alloc (txt); |
633 |
|
return len; |
634 |
|
} |
635 |
|
|
636 |
|
|
637 |
|
/* Return TRUE if the current user has admin privileges. */ |
638 |
|
BOOL |
639 |
|
user_is_admin (void) |
640 |
|
{ |
641 |
|
SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY; |
642 |
|
HANDLE hd; |
643 |
|
TOKEN_GROUPS *ptg = NULL; |
644 |
|
DWORD ngtoken; |
645 |
|
DWORD i; |
646 |
|
BOOL admin = FALSE; |
647 |
|
PSID psid = 0; |
648 |
|
|
649 |
|
if (GetVersion () & 0x80000000) /* Win9X */ |
650 |
|
return TRUE; |
651 |
|
|
652 |
|
if (!OpenThreadToken (GetCurrentThread (), TOKEN_QUERY, FALSE, &hd) && |
653 |
|
!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &hd)) |
654 |
|
return FALSE; |
655 |
|
|
656 |
|
if (!GetTokenInformation (hd, TokenGroups, NULL, 0, &ngtoken) && |
657 |
|
GetLastError() == ERROR_INSUFFICIENT_BUFFER) { |
658 |
|
ptg = (TOKEN_GROUPS*)GlobalAlloc (GPTR, ngtoken); |
659 |
|
if (!ptg) |
660 |
|
return FALSE; |
661 |
|
|
662 |
|
if (!GetTokenInformation (hd, TokenGroups, |
663 |
|
ptg, ngtoken, &ngtoken)) { |
664 |
|
GlobalFree (ptg); |
665 |
|
return FALSE; |
666 |
|
} |
667 |
|
AllocateAndInitializeSid (&SystemSidAuthority, |
668 |
|
2, SECURITY_BUILTIN_DOMAIN_RID, |
669 |
|
DOMAIN_ALIAS_RID_ADMINS, |
670 |
|
0, 0, 0, 0, 0, 0, |
671 |
|
&psid); |
672 |
|
for (i = 0; i < ptg->GroupCount; i++) { |
673 |
|
if (EqualSid (ptg->Groups[i].Sid, psid)) { |
674 |
|
admin = TRUE; |
675 |
|
break; |
676 |
|
} |
677 |
|
} |
678 |
|
FreeSid (psid); |
679 |
|
GlobalFree (ptg); |
680 |
|
} |
681 |
|
|
682 |
|
CloseHandle (hd); |
683 |
|
return admin; |
684 |
|
} |