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