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 |
* |
* |
23 |
#endif |
#endif |
24 |
|
|
25 |
#include <windows.h> |
#include <windows.h> |
|
#include <windows.h> |
|
26 |
#include <stdio.h> |
#include <stdio.h> |
27 |
#include <sys/types.h> |
#include <sys/types.h> |
28 |
#include <sys/stat.h> |
#include <sys/stat.h> |
29 |
#include <shellapi.h> |
#include <shellapi.h> |
30 |
#include <shlobj.h> |
#include <shlobj.h> |
31 |
#include <commctrl.h> |
#include <commctrl.h> |
32 |
|
#include <time.h> |
33 |
|
|
34 |
#include "wptNLS.h" |
#include "wptNLS.h" |
35 |
#include "wptW32API.h" |
#include "wptW32API.h" |
57 |
|
|
58 |
/* Set the text of a menu item @m_uid to @text. */ |
/* Set the text of a menu item @m_uid to @text. */ |
59 |
void |
void |
60 |
set_menu_text (HMENU menu, int m_uid, const char *text) |
set_menu_text (HMENU menu, UINT m_uid, const char *text) |
61 |
{ |
{ |
62 |
set_menu_text_ext (menu, 0, m_uid, text); |
set_menu_text_ext (menu, 0, m_uid, text); |
63 |
} |
} |
65 |
|
|
66 |
/* Set the text of a menu item with the position @pos to @text. */ |
/* Set the text of a menu item with the position @pos to @text. */ |
67 |
void |
void |
68 |
set_menu_text_bypos (HMENU menu, int pos, const char *text) |
set_menu_text_bypos (HMENU menu, UINT pos, const char *text) |
69 |
{ |
{ |
70 |
set_menu_text_ext (menu, 1, pos, text); |
set_menu_text_ext (menu, 1, pos, text); |
71 |
} |
} |
73 |
|
|
74 |
/* Set the state of a menu item @m_uid to @state. */ |
/* Set the state of a menu item @m_uid to @state. */ |
75 |
void |
void |
76 |
set_menu_state (HMENU menu, int m_uid, int state) |
set_menu_state (HMENU menu, UINT m_uid, UINT state) |
77 |
{ |
{ |
78 |
MENUITEMINFO mii; |
MENUITEMINFO mii; |
79 |
|
|
80 |
memset( &mii, 0, sizeof (mii) ); |
memset (&mii, 0, sizeof (mii)); |
81 |
mii.cbSize = sizeof (mii); |
mii.cbSize = sizeof (mii); |
82 |
mii.fMask = MIIM_STATE; |
mii.fMask = MIIM_STATE; |
83 |
mii.fState = state; |
mii.fState = state; |
85 |
} |
} |
86 |
|
|
87 |
|
|
88 |
|
/* Retrieve the state of the menu item @m_uid and return it. */ |
89 |
|
UINT |
90 |
|
get_menu_state (HMENU menu, UINT m_uid) |
91 |
|
{ |
92 |
|
MENUITEMINFO mii; |
93 |
|
|
94 |
|
memset (&mii, 0, sizeof (mii)); |
95 |
|
mii.cbSize = sizeof (mii); |
96 |
|
mii.fMask = MIIM_STATE; |
97 |
|
GetMenuItemInfo (menu, m_uid, FALSE, &mii); |
98 |
|
return mii.fState; |
99 |
|
} |
100 |
|
|
101 |
|
|
102 |
|
enum { |
103 |
|
CDLG_FILE_OPEN = 0, |
104 |
|
CDLG_FILE_SAVE = 1 |
105 |
|
}; |
106 |
|
|
107 |
/* Use the common dialog to request a file from the user. |
/* Use the common dialog to request a file from the user. |
108 |
id can be either FILE_OPEN or FILE_SAVE. |
id can be either FILE_OPEN or FILE_SAVE. |
119 |
else |
else |
120 |
memset (file, 0, sizeof (file)); |
memset (file, 0, sizeof (file)); |
121 |
if (!filter) |
if (!filter) |
122 |
filter = _("All Files (*.*)\0*.*\0\0"); |
filter = "All Files (*.*)\0*.*\0\0"; |
123 |
/* XXX: problem with gettext because of the 'artificial' |
/* XXX: problem with gettext because of the 'artificial' |
124 |
double string termination!. */ |
double string termination!. */ |
125 |
memset (&open, 0, sizeof (open)); |
memset (&open, 0, sizeof (open)); |
130 |
open.hwndOwner = hwnd; |
open.hwndOwner = hwnd; |
131 |
open.lpstrFile = file; |
open.lpstrFile = file; |
132 |
open.nMaxFile = sizeof (file) - 1; |
open.nMaxFile = sizeof (file) - 1; |
133 |
if (id == FILE_OPEN) |
if (id == CDLG_FILE_OPEN) |
134 |
open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST; |
open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST; |
135 |
else |
else |
136 |
open.Flags = OFN_OVERWRITEPROMPT; |
open.Flags = OFN_OVERWRITEPROMPT; |
137 |
|
|
138 |
if (id == FILE_OPEN && GetOpenFileName (&open)) |
if (id == CDLG_FILE_OPEN && GetOpenFileName (&open)) |
139 |
return open.lpstrFile; |
return open.lpstrFile; |
140 |
else if (id == FILE_SAVE && GetSaveFileName (&open)) |
else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open)) |
141 |
return open.lpstrFile; |
return open.lpstrFile; |
142 |
|
|
143 |
return NULL; |
return NULL; |
147 |
get_filesave_dlg (HWND hwnd, const char *title, |
get_filesave_dlg (HWND hwnd, const char *title, |
148 |
const char *filter, const char *name) |
const char *filter, const char *name) |
149 |
{ |
{ |
150 |
return get_filename_dlg (hwnd, FILE_SAVE, title, filter, name); |
return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name); |
151 |
} |
} |
152 |
|
|
153 |
const char * |
const char* |
154 |
get_fileopen_dlg (HWND hwnd, const char *title, const char *filter, |
get_fileopen_dlg (HWND hwnd, const char *title, const char *filter, |
155 |
const char *name) |
const char *name) |
156 |
{ |
{ |
157 |
return get_filename_dlg (hwnd, FILE_OPEN, title, filter, name); |
return get_filename_dlg (hwnd, CDLG_FILE_OPEN, title, filter, name); |
158 |
} |
} |
159 |
|
|
160 |
|
|
161 |
/* Use the common dialog to allow the user to select a folder. |
/* Use the common dialog to allow the user to select a folder. |
162 |
The return value is either the folder path or NULL if cancel was chosen. */ |
The return value is either the folder path or NULL if cancel was chosen. */ |
163 |
const char* |
const char* |
164 |
get_folder_dlg (HWND hwnd, const char * title, const char * name) |
get_folder_dlg (HWND hwnd, const char *title, const char *name) |
165 |
{ |
{ |
166 |
static char folder[MAX_PATH+1] = ""; |
static char folder[MAX_PATH+1] = ""; |
167 |
BROWSEINFO bi; |
BROWSEINFO bi; |
168 |
ITEMIDLIST * il; |
ITEMIDLIST *il; |
169 |
|
|
170 |
memset (&bi, 0, sizeof (bi)); |
memset (&bi, 0, sizeof (bi)); |
171 |
bi.hwndOwner = hwnd; |
bi.hwndOwner = hwnd; |
277 |
} |
} |
278 |
|
|
279 |
|
|
280 |
/* Make a file name out of the path, the file and an extension. */ |
/* Make a file name out of the path @path, the file @file and |
281 |
|
an extension. @ext. |
282 |
|
Return value: the full file name on success. */ |
283 |
char* |
char* |
284 |
make_filename (const char *path, const char *file, const char *ext) |
make_filename (const char *path, const char *file, const char *ext) |
285 |
{ |
{ |
306 |
strcat( p, ext ); |
strcat( p, ext ); |
307 |
} |
} |
308 |
return p; |
return p; |
309 |
} /* make_filename */ |
} |
310 |
|
|
311 |
|
|
312 |
/* return 0 if it exists, otherwise >0. */ |
/* Generate a file name from a special dirctory. */ |
313 |
|
char* |
314 |
|
make_special_filename (int folder, const char *file, const char *ext) |
315 |
|
{ |
316 |
|
BOOL ec; |
317 |
|
char path[MAX_PATH], *p; |
318 |
|
size_t n=0; |
319 |
|
|
320 |
|
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
321 |
|
ec = SHGetSpecialFolderPath (HWND_DESKTOP, path, folder, TRUE); |
322 |
|
if (ec != 1) { |
323 |
|
log_debug ("SHGetSpecialFolderPath() failed\r\n", (int)GetLastError ()); |
324 |
|
return NULL; |
325 |
|
} |
326 |
|
|
327 |
|
n = strlen (path)+1; |
328 |
|
if (file) |
329 |
|
n += strlen (file)+1; |
330 |
|
if (ext) |
331 |
|
n += strlen (ext)+1; |
332 |
|
p = new char[n+2]; |
333 |
|
if (!p) |
334 |
|
BUG (0); |
335 |
|
memset (p, 0, n+2); |
336 |
|
strcpy (p, path); |
337 |
|
if (file) { |
338 |
|
strcat (p, "\\"); |
339 |
|
strcat (p, file); |
340 |
|
} |
341 |
|
if (ext) |
342 |
|
strcat (p, ext); |
343 |
|
return p; |
344 |
|
} |
345 |
|
|
346 |
|
|
347 |
|
/* return 0 if the file @fname exists, otherwise >0. */ |
348 |
int |
int |
349 |
file_exist_check (const char * fname) |
file_exist_check (const char *fname) |
350 |
{ |
{ |
351 |
struct stat st; |
struct stat st; |
352 |
if (stat (fname, &st) == -1) |
if (stat (fname, &st) == -1) |
362 |
{ |
{ |
363 |
struct stat statbuf; |
struct stat statbuf; |
364 |
|
|
365 |
if( stat( dir, &statbuf ) == -1 ) |
if (stat (dir, &statbuf) == -1) |
366 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
367 |
if( statbuf.st_mode & _S_IFDIR ) |
if (statbuf.st_mode & _S_IFDIR) |
368 |
return 0; |
return 0; |
369 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
370 |
} |
} |
371 |
|
|
372 |
|
|
373 |
/* Return the file size of the given file. */ |
/* Return the file size of the given file @fname. */ |
374 |
size_t |
DWORD |
375 |
get_file_size (const char *fname) |
get_file_size (const char *fname) |
376 |
{ |
{ |
377 |
size_t fsize; |
DWORD fsize; |
378 |
HANDLE fh; |
HANDLE fh; |
379 |
|
|
380 |
fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ, |
fh = CreateFile (fname, GENERIC_READ, FILE_SHARE_READ, |
381 |
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); |
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
382 |
if( fh == INVALID_HANDLE_VALUE ) |
if (fh == INVALID_HANDLE_VALUE) |
383 |
return 0; |
return 0; |
384 |
fsize = GetFileSize( fh, NULL ); |
fsize = GetFileSize (fh, NULL); |
385 |
if( fsize == 0xFFFFFFFF ) |
if (fsize == 0xFFFFFFFF) |
386 |
fsize = 0; |
fsize = 0; |
387 |
CloseHandle( fh ); |
CloseHandle (fh); |
388 |
return fsize; |
return fsize; |
389 |
} /* get_file_size */ |
} |
|
|
|
|
|
|
|
int |
|
|
init_file_lock( LOCK *ctx, const char *file ) |
|
|
{ |
|
|
|
|
|
ctx->size = get_file_size( file ); |
|
|
ctx->file = m_strdup( file ); |
|
|
ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL, |
|
|
OPEN_ALWAYS, 0, NULL ); |
|
|
if( ctx->fh == INVALID_HANDLE_VALUE ) |
|
|
return WPTERR_GENERAL; |
|
|
if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) { |
|
|
CloseHandle( ctx->fh ); |
|
|
ctx->fh = INVALID_HANDLE_VALUE; |
|
|
ctx->size = 0; |
|
|
free( ctx->file ); |
|
|
return WPTERR_GENERAL; |
|
|
} |
|
|
return 0; |
|
|
} /* init_file_lock */ |
|
|
|
|
|
|
|
|
void |
|
|
release_file_lock( LOCK *ctx ) |
|
|
{ |
|
|
free_if_alloc( ctx->file ); |
|
|
ctx->file = NULL; |
|
|
ctx->size = 0; |
|
|
CloseHandle( ctx->fh ); |
|
|
} /* release_file_lock */ |
|
390 |
|
|
391 |
|
|
392 |
/* Start a dialog with the exception that before it is checked that the |
/* Start a dialog with the exception that before it is checked that the |
393 |
dialog is not already openened. */ |
dialog is not already openened. */ |
394 |
int |
int |
395 |
dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc, |
dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc, |
396 |
LPARAM param, LPCTSTR title, int title_id ) |
LPARAM param, LPCTSTR title, int title_id) |
397 |
{ |
{ |
398 |
#ifndef LANG_DE |
if (FindWindowEx (GetDesktopWindow (), NULL, NULL, title)) |
|
if( FindWindowEx( GetDesktopWindow(), NULL, NULL, title ) ) |
|
|
return -1; |
|
|
#else |
|
|
char strdesc[256]; |
|
|
LoadString( glob_hinst, title_id, strdesc, sizeof (strdesc) -1 ); |
|
|
if( FindWindowEx( GetDesktopWindow(), NULL, NULL, strdesc ) ) |
|
399 |
return -1; |
return -1; |
400 |
#endif |
return DialogBoxParam (hinst, name, parent, fnc, param); |
401 |
|
} |
|
return DialogBoxParam( hinst, name, parent, fnc, param ); |
|
|
} /* dialog_box_param */ |
|
402 |
|
|
403 |
|
|
404 |
/* Wrapper for message box which forces the message box into the |
/* Wrapper for message box which forces the message box into the |
413 |
} |
} |
414 |
|
|
415 |
|
|
416 |
void |
/* Safe strdup version (C++ version). */ |
|
set_active_window( HWND dlg) |
|
|
{ |
|
|
activ_hwnd = dlg; |
|
|
} /* set_active_window */ |
|
|
|
|
|
void |
|
|
reset_active_window( void ) |
|
|
{ |
|
|
activ_hwnd = NULL; |
|
|
} /* reset_active_window */ |
|
|
|
|
|
|
|
|
static DWORD CALLBACK |
|
|
reminder_thread (void *ctx) |
|
|
{ |
|
|
reminder_ctx_s *c = (reminder_ctx_s *)ctx; |
|
|
|
|
|
Sleep( c->msecs ); |
|
|
SetForegroundWindow( activ_hwnd ); |
|
|
|
|
|
return 0; |
|
|
} /* reminder_thread */ |
|
|
|
|
|
|
|
|
HANDLE |
|
|
window_reminder( struct reminder_ctx_s *ctx ) |
|
|
{ |
|
|
DWORD tid = 0; |
|
|
|
|
|
return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid ); |
|
|
} /* window_reminder */ |
|
|
|
|
|
|
|
417 |
char* |
char* |
418 |
m_strdup (const char *str) |
m_strdup (const char *str) |
419 |
{ |
{ |
420 |
char * p = new char[strlen (str) + 1]; |
char *p = new char[strlen (str) + 1]; |
421 |
if (p) |
if (!p) |
422 |
strcpy (p, str); |
BUG (NULL); |
423 |
|
strcpy (p, str); |
424 |
return p; |
return p; |
425 |
} /* m_strdup */ |
} |
426 |
|
|
427 |
|
|
428 |
/* Center the hwndChild relative to parent. |
/* Center the hwndChild relative to parent. |
474 |
{ |
{ |
475 |
center_window2 (hwndChild, hwndParent, NULL); |
center_window2 (hwndChild, hwndParent, NULL); |
476 |
} |
} |
477 |
|
|
478 |
|
|
479 |
|
/* Retrieve the product verion of the given file @fname. |
480 |
|
Format: MAJOR.MINOR.PATCH1.PATCH2 |
481 |
|
Return value: 0 on success. */ |
482 |
|
int |
483 |
|
get_file_version (const char *fname, WORD *major, WORD *minor, |
484 |
|
WORD *patch1, WORD *patch2) |
485 |
|
{ |
486 |
|
VS_FIXEDFILEINFO *inf = {0}; |
487 |
|
char file[MAX_PATH+1] = {0}; |
488 |
|
LPVOID buf, data; |
489 |
|
DWORD arg; |
490 |
|
DWORD size; |
491 |
|
UINT qlen; |
492 |
|
|
493 |
|
strncpy (file, fname, MAX_PATH); |
494 |
|
size = GetFileVersionInfoSize (file, &arg); |
495 |
|
if (!size) |
496 |
|
return -1; |
497 |
|
buf = (LPVOID)new CHAR[size]; |
498 |
|
if (!buf) |
499 |
|
BUG (NULL); |
500 |
|
GetFileVersionInfo (file, 0, size, buf); |
501 |
|
|
502 |
|
qlen=0; |
503 |
|
VerQueryValue (buf, "\\", &data, &qlen); |
504 |
|
if (!qlen) { |
505 |
|
delete [] (char*)buf; |
506 |
|
return -1; |
507 |
|
} |
508 |
|
inf = (VS_FIXEDFILEINFO*)data; |
509 |
|
|
510 |
|
if (major) |
511 |
|
*major = HIWORD (inf->dwProductVersionMS); |
512 |
|
if (minor) |
513 |
|
*minor = LOWORD (inf->dwProductVersionMS); |
514 |
|
if (patch1) |
515 |
|
*patch1 = HIWORD (inf->dwProductVersionLS); |
516 |
|
if (patch2) |
517 |
|
*patch2 = LOWORD (inf->dwProductVersionLS); |
518 |
|
|
519 |
|
delete [] (char*)buf; |
520 |
|
return 0; |
521 |
|
} |
522 |
|
|
523 |
|
|
524 |
|
/* Return date in a format which complies with the |
525 |
|
system locale settings. */ |
526 |
|
const char* |
527 |
|
get_locale_date (long tm_t, char *buf, DWORD buflen) |
528 |
|
{ |
529 |
|
SYSTEMTIME st; |
530 |
|
struct tm *ptm; |
531 |
|
|
532 |
|
ptm = localtime (&tm_t); |
533 |
|
st.wYear = (WORD)ptm->tm_year; |
534 |
|
st.wMonth = (WORD)ptm->tm_mon; |
535 |
|
st.wDay = (WORD)ptm->tm_mday; |
536 |
|
st.wYear += 1900; |
537 |
|
st.wMonth += 1; |
538 |
|
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
539 |
|
NULL, buf, buflen)) |
540 |
|
return NULL; |
541 |
|
return buf; |
542 |
|
} |
543 |
|
|
544 |
|
|
545 |
|
struct reminder_hd_s { |
546 |
|
int msecs; |
547 |
|
HWND dlg; |
548 |
|
HANDLE hd; |
549 |
|
}; |
550 |
|
|
551 |
|
|
552 |
|
static DWORD CALLBACK |
553 |
|
foreground_reminder_thread (void *c) |
554 |
|
{ |
555 |
|
struct reminder_hd_s *ctx = (struct reminder_hd_s *)c; |
556 |
|
Sleep (ctx->msecs); |
557 |
|
SetForegroundWindow (ctx->dlg); |
558 |
|
CloseHandle (ctx->hd); |
559 |
|
delete ctx; |
560 |
|
ExitThread (0); |
561 |
|
return 0; |
562 |
|
} |
563 |
|
|
564 |
|
/* Try to force the window @dlg to the foreground. |
565 |
|
On NT5 or later this will not work if the user |
566 |
|
is working in another window (console for example). */ |
567 |
|
void |
568 |
|
force_foreground_window (HWND dlg, int msecs) |
569 |
|
{ |
570 |
|
struct reminder_hd_s *hd; |
571 |
|
DWORD tid; |
572 |
|
|
573 |
|
hd = new reminder_hd_s; |
574 |
|
hd->dlg = dlg; |
575 |
|
hd->msecs = msecs; |
576 |
|
hd->hd = CreateThread (NULL, 0, foreground_reminder_thread, |
577 |
|
hd, NULL, &tid); |
578 |
|
} |