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