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 |
/* The the text of a menu item. */ |
#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 |
/* Use the common dialog to request a file from the user. |
} |
75 |
id can be either FILE_OPEN or FILE_SAVE. |
|
76 |
The return value is the file name or NULL if cancel was chosen. */ |
|
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*.*\0\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 |
if (id == FILE_OPEN) |
|
99 |
open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST; |
memset (&mii, 0, sizeof (mii)); |
100 |
else |
mii.cbSize = sizeof (mii); |
101 |
open.Flags = OFN_OVERWRITEPROMPT; |
mii.fMask = MIIM_STATE; |
102 |
|
mii.fState = state; |
103 |
if (id == FILE_OPEN && GetOpenFileName (&open)) |
SetMenuItemInfo (menu, m_uid, FALSE, &mii); |
104 |
return open.lpstrFile; |
} |
105 |
else if (id == FILE_SAVE && GetSaveFileName (&open)) |
|
106 |
return open.lpstrFile; |
|
107 |
|
/* Retrieve the state of the menu item @m_uid and return it. */ |
108 |
return NULL; |
UINT |
109 |
} |
get_menu_state (HMENU menu, UINT m_uid) |
110 |
|
{ |
111 |
/* Use the common dialog to allow the user to select a folder. |
MENUITEMINFO mii; |
112 |
The return value is either the folder path or NULL if cancel was chosen. */ |
|
113 |
const char* |
memset (&mii, 0, sizeof (mii)); |
114 |
get_folder_dlg (HWND hwnd, const char * title, const char * name) |
mii.cbSize = sizeof (mii); |
115 |
{ |
mii.fMask = MIIM_STATE; |
116 |
static char folder[MAX_PATH+1] = ""; |
GetMenuItemInfo (menu, m_uid, FALSE, &mii); |
117 |
BROWSEINFO bi; |
return mii.fState; |
118 |
ITEMIDLIST * il; |
} |
119 |
|
|
120 |
memset (&bi, 0, sizeof (bi)); |
|
121 |
bi.hwndOwner = hwnd; |
enum { |
122 |
if (title) |
CDLG_FILE_OPEN = 0, |
123 |
bi.lpszTitle = title; |
CDLG_FILE_SAVE = 1 |
124 |
if (name && strlen (name) < MAX_PATH-1) |
}; |
125 |
strcpy (folder, name); |
|
126 |
else |
/* Use the common dialog to request a file from the user. |
127 |
memset (folder, 0, sizeof (folder)); |
id can be either FILE_OPEN or FILE_SAVE. |
128 |
il = SHBrowseForFolder (&bi); |
The return value is the file name or NULL if cancel was chosen. */ |
129 |
if (il) { |
const char * |
130 |
SHGetPathFromIDList (il, folder); |
get_filename_dlg (HWND hwnd, int id, const char *title, |
131 |
_SHFree (il); |
const char *filter, const char *name) |
132 |
return folder; |
{ |
133 |
} |
static char file[512] = ""; |
134 |
return NULL; |
OPENFILENAME open; |
135 |
} |
|
136 |
|
if (name && strlen (name) < (sizeof (file)-1)) |
137 |
|
strcpy (file, name); |
138 |
/* Return the clipboard contents as a string or NULL |
else |
139 |
if the clipboard does not contain text. */ |
memset (file, 0, sizeof (file)); |
140 |
char* |
if (!filter) |
141 |
get_clip_text (HWND hwnd) |
filter = "All Files (*.*)\0*.*\0\0"; |
142 |
{ |
/* XXX: problem with gettext because of the 'artificial' |
143 |
HANDLE clipmem; |
double string termination!. */ |
144 |
char *cliptxt, *p; |
memset (&open, 0, sizeof (open)); |
145 |
int len; |
open.lStructSize = sizeof (OPENFILENAME); |
146 |
|
open.hInstance = glob_hinst; |
147 |
if (OpenClipboard (hwnd) == FALSE) |
open.lpstrTitle = title; |
148 |
return NULL; |
open.lpstrFilter = filter; |
149 |
clipmem = GetClipboardData (CF_TEXT); |
open.hwndOwner = hwnd; |
150 |
if (clipmem == NULL) { |
open.lpstrFile = file; |
151 |
p = NULL; |
open.nMaxFile = sizeof (file) - 1; |
152 |
goto leave; |
if (id == CDLG_FILE_OPEN) |
153 |
} |
open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST; |
154 |
cliptxt = (char *) GlobalLock (clipmem); |
else |
155 |
if (cliptxt == NULL) { |
open.Flags = OFN_OVERWRITEPROMPT; |
156 |
p = NULL; |
|
157 |
goto leave; |
if (id == CDLG_FILE_OPEN && GetOpenFileName (&open)) |
158 |
} |
return open.lpstrFile; |
159 |
|
else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open)) |
160 |
len = strlen (cliptxt); |
return open.lpstrFile; |
161 |
p = new char[len + 1]; |
|
162 |
if (!p) |
return NULL; |
163 |
BUG (NULL); |
} |
164 |
memcpy (p, cliptxt, len); |
|
165 |
p[len] = '\0'; |
const char* |
166 |
GlobalUnlock (clipmem); |
get_filesave_dlg (HWND hwnd, const char *title, |
167 |
|
const char *filter, const char *name) |
168 |
leave: |
{ |
169 |
CloseClipboard (); |
return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name); |
170 |
return p; |
} |
171 |
} |
|
172 |
|
const char* |
173 |
|
get_fileopen_dlg (HWND hwnd, const char *title, const char *filter, |
174 |
/* Set the the given text to the clipboard. */ |
const char *name) |
175 |
int |
{ |
176 |
set_clip_text (HWND hwnd, const char *text, int nbytes) |
return get_filename_dlg (hwnd, CDLG_FILE_OPEN, title, filter, name); |
177 |
{ |
} |
178 |
HANDLE clipmem; |
|
179 |
int rc = 0; |
|
180 |
char *p; |
/* Use the common dialog to allow the user to select a folder. |
181 |
|
The return value is either the folder path or NULL if cancel was chosen. */ |
182 |
if (OpenClipboard (hwnd) == FALSE) |
const char* |
183 |
return WPTERR_CLIP_OPEN; |
get_folder_dlg (HWND hwnd, const char *title, const char *name) |
184 |
EmptyClipboard (); |
{ |
185 |
|
static char folder[MAX_PATH+1] = ""; |
186 |
clipmem = GlobalAlloc (GHND, nbytes + 1); |
BROWSEINFO bi; |
187 |
if (clipmem == NULL) |
ITEMIDLIST *il; |
188 |
BUG (NULL); |
|
189 |
p = (char *) GlobalLock (clipmem); |
memset (&bi, 0, sizeof (bi)); |
190 |
if (p == NULL) { |
bi.hwndOwner = hwnd; |
191 |
rc = WPTERR_GENERAL;; |
if (title) |
192 |
goto leave; |
bi.lpszTitle = title; |
193 |
} |
if (name && strlen (name) < MAX_PATH-1) |
194 |
memcpy (p, text, nbytes); |
strcpy (folder, name); |
195 |
p[nbytes] = '\0'; |
else |
196 |
|
memset (folder, 0, sizeof (folder)); |
197 |
GlobalUnlock (clipmem); |
il = SHBrowseForFolder (&bi); |
198 |
SetClipboardData (CF_TEXT, clipmem); |
if (il) { |
199 |
|
SHGetPathFromIDList (il, folder); |
200 |
leave: |
_SHFree (il); |
201 |
CloseClipboard (); |
return folder; |
202 |
return rc; |
} |
203 |
} /* set_clip_text */ |
return NULL; |
204 |
|
} |
205 |
|
|
206 |
/* Append or prepend some text to the clipboard contents. |
|
207 |
If as_footer = 1, append the text otherwise prepend. */ |
/* Return the clipboard contents as a string or NULL |
208 |
int |
if the clipboard does not contain text. */ |
209 |
set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer) |
char* |
210 |
{ |
get_clip_text (HWND hwnd) |
211 |
char *p, *new_text; |
{ |
212 |
|
HANDLE clipmem; |
213 |
p = get_clip_text (hwnd); |
char *cliptxt, *p; |
214 |
if (!p) |
int len; |
215 |
return WPTERR_CLIP_GET; |
|
216 |
new_text = new char [strlen (p)+strlen (text)+8]; |
if (OpenClipboard (hwnd) == FALSE) |
217 |
if (!new_text) |
return NULL; |
218 |
BUG (0); |
clipmem = GetClipboardData (CF_TEXT); |
219 |
if (as_footer == 0) |
if (clipmem == NULL) { |
220 |
sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p); |
p = NULL; |
221 |
else |
goto leave; |
222 |
sprintf (new_text, "%s\n%s\n\n", p, text); |
} |
223 |
set_clip_text (hwnd, new_text, strlen (new_text)+1); |
cliptxt = (char *) GlobalLock (clipmem); |
224 |
free_if_alloc (p); |
if (cliptxt == NULL) { |
225 |
free_if_alloc (new_text); |
p = NULL; |
226 |
return 0; |
goto leave; |
227 |
} |
} |
228 |
|
|
229 |
|
len = strlen (cliptxt); |
230 |
/* Make a file name out of the path, the file and an extension. */ |
p = new char[len + 1]; |
231 |
char* |
if (!p) |
232 |
make_filename (const char *path, const char *file, const char *ext) |
BUG (NULL); |
233 |
{ |
memcpy (p, cliptxt, len); |
234 |
char *p; |
p[len] = '\0'; |
235 |
size_t size = 0; |
GlobalUnlock (clipmem); |
236 |
|
|
237 |
if( path && *path ) |
leave: |
238 |
size += strlen( path ); |
CloseClipboard (); |
239 |
if( file && *file ) |
return p; |
240 |
size += strlen( file ); |
} |
241 |
if( ext && *ext ) |
|
242 |
size += strlen( ext ); |
|
243 |
p = new char[size + 4]; |
/* Set @text as the new clipboard content. */ |
244 |
memset( p, 0, size ); |
int |
245 |
if( path ) { |
set_clip_text (HWND hwnd, const char *text, int nbytes) |
246 |
strcat( p, path ); |
{ |
247 |
if( path[strlen( path ) -1] != '\\' ) |
HANDLE clipmem; |
248 |
strcat( p, "\\" ); |
int rc = 0; |
249 |
} |
char *p; |
250 |
if( file ) |
|
251 |
strcat( p, file ); |
if (OpenClipboard (hwnd) == FALSE) |
252 |
if( ext ) { |
return WPTERR_CLIP_OPEN; |
253 |
strcat( p, "." ); |
EmptyClipboard (); |
254 |
strcat( p, ext ); |
|
255 |
} |
clipmem = GlobalAlloc (GHND, nbytes + 1); |
256 |
return p; |
if (clipmem == NULL) |
257 |
} /* make_filename */ |
BUG (NULL); |
258 |
|
p = (char *) GlobalLock (clipmem); |
259 |
|
if (p == NULL) { |
260 |
/* return 0 if it exists, otherwise >0. */ |
CloseClipboard (); |
261 |
int |
GlobalFree (clipmem); |
262 |
file_exist_check (const char * fname) |
return WPTERR_GENERAL; |
263 |
{ |
} |
264 |
HANDLE fh; |
memcpy (p, text, nbytes); |
265 |
|
p[nbytes] = '\0'; |
266 |
fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ, |
|
267 |
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); |
SetClipboardData (CF_TEXT, clipmem); |
268 |
if( fh == INVALID_HANDLE_VALUE ) |
GlobalUnlock (clipmem); |
269 |
return WPTERR_FILE_EXIST; |
CloseClipboard (); |
270 |
CloseHandle( fh ); |
GlobalFree (clipmem); |
271 |
return 0; |
|
272 |
} |
return rc; |
273 |
|
} |
274 |
|
|
275 |
/* Check if the current folder exists. |
|
276 |
Return 0 for success. */ |
/* Append or prepend some text to the clipboard contents. |
277 |
int |
If as_footer = 1, append the text otherwise prepend. */ |
278 |
dir_exist_check (const char *dir) |
int |
279 |
{ |
set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer) |
280 |
struct stat statbuf; |
{ |
281 |
|
char *p, *new_text; |
282 |
if( stat( dir, &statbuf ) == -1 ) |
|
283 |
return WPTERR_GENERAL; |
p = get_clip_text (hwnd); |
284 |
if( statbuf.st_mode & _S_IFDIR ) |
if (!p) |
285 |
return 0; |
return WPTERR_CLIP_GET; |
286 |
return WPTERR_GENERAL; |
new_text = new char [strlen (p)+strlen (text)+8]; |
287 |
} |
if (!new_text) |
288 |
|
BUG (0); |
289 |
|
if (as_footer == 0) |
290 |
/* Return the file size of the given file. */ |
sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p); |
291 |
size_t |
else |
292 |
get_file_size (const char *fname) |
sprintf (new_text, "%s\n%s\n\n", p, text); |
293 |
{ |
set_clip_text (hwnd, new_text, strlen (new_text)+1); |
294 |
size_t fsize; |
free_if_alloc (p); |
295 |
HANDLE fh; |
free_if_alloc (new_text); |
296 |
|
return 0; |
297 |
fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ, |
} |
298 |
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); |
|
299 |
if( fh == INVALID_HANDLE_VALUE ) |
|
300 |
return 0; |
/* Make a file name out of the path @path, the file @file and |
301 |
fsize = GetFileSize( fh, NULL ); |
an extension. @ext. |
302 |
if( fsize == 0xFFFFFFFF ) |
Return value: the full file name on success. */ |
303 |
fsize = 0; |
char* |
304 |
CloseHandle( fh ); |
make_filename (const char *path, const char *file, const char *ext) |
305 |
return fsize; |
{ |
306 |
} /* get_file_size */ |
char *p; |
307 |
|
size_t size = 0; |
308 |
|
|
309 |
int |
if( path && *path ) |
310 |
init_file_lock( LOCK *ctx, const char *file ) |
size += strlen( path ); |
311 |
{ |
if( file && *file ) |
312 |
|
size += strlen( file ); |
313 |
ctx->size = get_file_size( file ); |
if( ext && *ext ) |
314 |
ctx->file = m_strdup( file ); |
size += strlen( ext ); |
315 |
ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL, |
p = new char[size + 4]; |
316 |
OPEN_ALWAYS, 0, NULL ); |
memset( p, 0, size ); |
317 |
if( ctx->fh == INVALID_HANDLE_VALUE ) |
if( path ) { |
318 |
return WPTERR_GENERAL; |
strcat( p, path ); |
319 |
if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) { |
if( path[strlen( path ) -1] != '\\' ) |
320 |
CloseHandle( ctx->fh ); |
strcat( p, "\\" ); |
321 |
ctx->fh = INVALID_HANDLE_VALUE; |
} |
322 |
ctx->size = 0; |
if( file ) |
323 |
free( ctx->file ); |
strcat( p, file ); |
324 |
return WPTERR_GENERAL; |
if( ext ) { |
325 |
} |
strcat( p, "." ); |
326 |
return 0; |
strcat( p, ext ); |
327 |
} /* init_file_lock */ |
} |
328 |
|
return p; |
329 |
|
} |
330 |
void |
|
331 |
release_file_lock( LOCK *ctx ) |
|
332 |
{ |
/* Generate a file name from a special dirctory. */ |
333 |
free_if_alloc( ctx->file ); |
char* |
334 |
ctx->file = NULL; |
make_special_filename (int folder, const char *file, const char *ext) |
335 |
ctx->size = 0; |
{ |
336 |
CloseHandle( ctx->fh ); |
BOOL ec; |
337 |
} /* release_file_lock */ |
char path[MAX_PATH], *p; |
338 |
|
size_t n=0; |
339 |
|
|
340 |
/* Start a dialog with the exception that before it is checked that the |
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
341 |
dialog is not already openened. */ |
ec = SHGetSpecialFolderPath (HWND_DESKTOP, path, folder, TRUE); |
342 |
int |
if (ec != 1) { |
343 |
dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc, |
log_debug ("SHGetSpecialFolderPath() failed\r\n", (int)GetLastError ()); |
344 |
LPARAM param, LPCTSTR title, int title_id ) |
return NULL; |
345 |
{ |
} |
346 |
#ifndef LANG_DE |
|
347 |
if( FindWindowEx( GetDesktopWindow(), NULL, NULL, title ) ) |
n = strlen (path)+1; |
348 |
return -1; |
if (file) |
349 |
#else |
n += strlen (file)+1; |
350 |
char strdesc[256]; |
if (ext) |
351 |
LoadString( glob_hinst, title_id, strdesc, sizeof (strdesc) -1 ); |
n += strlen (ext)+1; |
352 |
if( FindWindowEx( GetDesktopWindow(), NULL, NULL, strdesc ) ) |
p = new char[n+2]; |
353 |
return -1; |
if (!p) |
354 |
#endif |
BUG (0); |
355 |
|
memset (p, 0, n+2); |
356 |
return DialogBoxParam( hinst, name, parent, fnc, param ); |
strcpy (p, path); |
357 |
} /* dialog_box_param */ |
if (file) { |
358 |
|
strcat (p, "\\"); |
359 |
|
strcat (p, file); |
360 |
/* Wrapper for message box which forces the message box into the |
} |
361 |
foreground and it is displayed always on top. */ |
if (ext) |
362 |
int |
strcat (p, ext); |
363 |
msg_box (HWND hwnd, const char *text, const char *title, int mode) |
return p; |
364 |
{ |
} |
365 |
mode |= MB_SETFOREGROUND; |
|
366 |
mode |= MB_TASKMODAL; |
|
367 |
mode |= MB_TOPMOST; |
/* return 0 if the file @fname exists, otherwise >0. */ |
368 |
return MessageBox(hwnd, text, title, mode); |
int |
369 |
} |
file_exist_check (const char *fname) |
370 |
|
{ |
371 |
|
struct stat st; |
372 |
void |
if (stat (fname, &st) == -1) |
373 |
set_active_window( HWND dlg) |
return WPTERR_FILE_EXIST; |
374 |
{ |
return 0; |
375 |
activ_hwnd = dlg; |
} |
376 |
} /* set_active_window */ |
|
377 |
|
|
378 |
void |
/* Check if the current folder exists. |
379 |
reset_active_window( void ) |
Return 0 for success. */ |
380 |
{ |
int |
381 |
activ_hwnd = NULL; |
dir_exist_check (const char *dir) |
382 |
} /* reset_active_window */ |
{ |
383 |
|
struct stat statbuf; |
384 |
|
|
385 |
static DWORD CALLBACK |
if (stat (dir, &statbuf) == -1) |
386 |
reminder_thread (void *ctx) |
return WPTERR_GENERAL; |
387 |
{ |
if (statbuf.st_mode & _S_IFDIR) |
388 |
reminder_ctx_s *c = (reminder_ctx_s *)ctx; |
return 0; |
389 |
|
return WPTERR_GENERAL; |
390 |
Sleep( c->msecs ); |
} |
391 |
SetForegroundWindow( activ_hwnd ); |
|
392 |
|
|
393 |
return 0; |
/* Return the file size of the given file @fname. */ |
394 |
} /* reminder_thread */ |
DWORD |
395 |
|
get_file_size (const char *fname) |
396 |
|
{ |
397 |
HANDLE |
DWORD fsize; |
398 |
window_reminder( struct reminder_ctx_s *ctx ) |
HANDLE fh; |
399 |
{ |
|
400 |
DWORD tid = 0; |
fh = CreateFile (fname, GENERIC_READ, FILE_SHARE_READ, |
401 |
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
402 |
return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid ); |
if (fh == INVALID_HANDLE_VALUE) |
403 |
} /* window_reminder */ |
return 0; |
404 |
|
fsize = GetFileSize (fh, NULL); |
405 |
|
if (fsize == 0xFFFFFFFF) |
406 |
char* |
fsize = 0; |
407 |
m_strdup (const char *str) |
CloseHandle (fh); |
408 |
{ |
return fsize; |
409 |
char * p = new char[strlen (str) + 1]; |
} |
410 |
if (p) |
|
411 |
strcpy (p, str); |
|
412 |
return p; |
/* Start a dialog with the exception that before it is checked that the |
413 |
} /* m_strdup */ |
dialog is not already openened. */ |
414 |
|
int |
415 |
|
dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc, |
416 |
/* Center the hwndChild relative to parent. |
LPARAM param, LPCTSTR title, int title_id) |
417 |
The style param allows to specificy additional styles (like topmost). */ |
{ |
418 |
void |
if (FindWindowEx (GetDesktopWindow (), NULL, NULL, title)) |
419 |
center_window2 (HWND hwndChild, HWND parent, HWND style) |
return -1; |
420 |
{ |
return DialogBoxParam (hinst, name, parent, fnc, param); |
421 |
HWND hwndParent; |
} |
422 |
RECT rChild, rParent; |
|
423 |
HDC hdc; |
|
424 |
int wChild, hChild, wParent, hParent; |
/* Wrapper for message box which forces the message box into the |
425 |
int wScreen, hScreen, xNew, yNew; |
foreground and it is displayed always on top. */ |
426 |
int flags = SWP_NOSIZE | SWP_NOZORDER; |
int |
427 |
|
msg_box (HWND hwnd, const char *text, const char *title, int mode) |
428 |
hwndParent = parent; |
{ |
429 |
if (hwndParent == NULL) |
mode |= MB_SETFOREGROUND; |
430 |
hwndParent = GetDesktopWindow (); |
mode |= MB_TASKMODAL; |
431 |
GetWindowRect (hwndChild, &rChild); |
mode |= MB_TOPMOST; |
432 |
wChild = rChild.right - rChild.left; |
return MessageBox(hwnd, text, title, mode); |
433 |
hChild = rChild.bottom - rChild.top; |
} |
434 |
|
|
435 |
GetWindowRect (hwndParent, &rParent); |
|
436 |
wParent = rParent.right - rParent.left; |
/* Safe strdup version (C++ version). */ |
437 |
hParent = rParent.bottom - rParent.top; |
char* |
438 |
|
m_strdup (const char *str) |
439 |
hdc = GetDC (hwndChild); |
{ |
440 |
wScreen = GetDeviceCaps (hdc, HORZRES); |
char *p = new char[strlen (str) + 1]; |
441 |
hScreen = GetDeviceCaps (hdc, VERTRES); |
if (!p) |
442 |
ReleaseDC (hwndChild, hdc); |
BUG (NULL); |
443 |
xNew = rParent.left + ((wParent - wChild) /2); |
strcpy (p, str); |
444 |
if (xNew < 0) |
return p; |
445 |
xNew = 0; |
} |
446 |
else if ((xNew+wChild) > wScreen) |
|
447 |
xNew = wScreen - wChild; |
|
448 |
yNew = rParent.top + ((hParent - hChild) /2); |
/* Center the hwndChild relative to parent. |
449 |
if (yNew < 0) |
The style param allows to specificy additional styles (like topmost). */ |
450 |
yNew = 0; |
void |
451 |
else if ((yNew+hChild) > hScreen) |
center_window2 (HWND hwndChild, HWND parent, HWND style) |
452 |
yNew = hScreen - hChild; |
{ |
453 |
if (style == HWND_TOPMOST || style == HWND_NOTOPMOST) |
HWND hwndParent; |
454 |
flags = SWP_NOMOVE | SWP_NOSIZE; |
RECT rChild, rParent; |
455 |
SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags); |
HDC hdc; |
456 |
} |
int wChild, hChild, wParent, hParent; |
457 |
|
int wScreen, hScreen, xNew, yNew; |
458 |
|
int flags = SWP_NOSIZE | SWP_NOZORDER; |
459 |
/* Center the given hwndChild window with no special style. */ |
|
460 |
void |
hwndParent = parent; |
461 |
center_window (HWND hwndChild, HWND hwndParent) |
if (hwndParent == NULL) |
462 |
{ |
hwndParent = GetDesktopWindow (); |
463 |
center_window2 (hwndChild, hwndParent, NULL); |
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 = {0}; |
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 |
|
struct reminder_hd_s { |
638 |
|
int msecs; |
639 |
|
HWND dlg; |
640 |
|
HANDLE hd; |
641 |
|
}; |
642 |
|
|
643 |
|
|
644 |
|
static DWORD CALLBACK |
645 |
|
foreground_reminder_thread (void *c) |
646 |
|
{ |
647 |
|
struct reminder_hd_s *ctx = (struct reminder_hd_s *)c; |
648 |
|
Sleep (ctx->msecs); |
649 |
|
SetForegroundWindow (ctx->dlg); |
650 |
|
CloseHandle (ctx->hd); |
651 |
|
delete ctx; |
652 |
|
ExitThread (0); |
653 |
|
return 0; |
654 |
|
} |
655 |
|
|
656 |
|
/* Try to force the window @dlg to the foreground. |
657 |
|
On NT5 or later this will not work if the user |
658 |
|
is working in another window (console for example). */ |
659 |
|
void |
660 |
|
force_foreground_window (HWND dlg, int msecs) |
661 |
|
{ |
662 |
|
struct reminder_hd_s *hd; |
663 |
|
DWORD tid; |
664 |
|
|
665 |
|
hd = new reminder_hd_s; |
666 |
|
hd->dlg = dlg; |
667 |
|
hd->msecs = msecs; |
668 |
|
hd->hd = CreateThread (NULL, 0, foreground_reminder_thread, |
669 |
|
hd, NULL, &tid); |
670 |
|
} |