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 |
* |
* |
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" |
41 |
extern "C" void _SHFree (void *p); |
extern "C" void _SHFree (void *p); |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
/* Insert a new item into the menu @hm at position @pos. */ |
46 |
|
void |
47 |
|
insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text) |
48 |
|
{ |
49 |
|
MENUITEMINFO mi; |
50 |
|
|
51 |
|
memset (&mi, 0, sizeof mi); |
52 |
|
mi.cbSize = sizeof mi; |
53 |
|
mi.fType = MF_STRING; |
54 |
|
mi.dwTypeData = (char *)text; |
55 |
|
mi.cch = strlen (text); |
56 |
|
mi.wID = m_id; |
57 |
|
mi.fMask = MIIM_ID|MIIM_DATA| MIIM_TYPE; |
58 |
|
InsertMenuItem (hm, pos, FALSE, &mi); |
59 |
|
} |
60 |
|
|
61 |
|
|
62 |
static void |
static void |
63 |
set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text) |
set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text) |
64 |
{ |
{ |
75 |
|
|
76 |
/* Set the text of a menu item @m_uid to @text. */ |
/* Set the text of a menu item @m_uid to @text. */ |
77 |
void |
void |
78 |
set_menu_text (HMENU menu, int m_uid, const char *text) |
set_menu_text (HMENU menu, UINT m_uid, const char *text) |
79 |
{ |
{ |
80 |
set_menu_text_ext (menu, 0, m_uid, text); |
set_menu_text_ext (menu, 0, m_uid, text); |
81 |
} |
} |
83 |
|
|
84 |
/* 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. */ |
85 |
void |
void |
86 |
set_menu_text_bypos (HMENU menu, int pos, const char *text) |
set_menu_text_bypos (HMENU menu, UINT pos, const char *text) |
87 |
{ |
{ |
88 |
set_menu_text_ext (menu, 1, pos, text); |
set_menu_text_ext (menu, 1, pos, text); |
89 |
} |
} |
91 |
|
|
92 |
/* Set the state of a menu item @m_uid to @state. */ |
/* Set the state of a menu item @m_uid to @state. */ |
93 |
void |
void |
94 |
set_menu_state (HMENU menu, int m_uid, int state) |
set_menu_state (HMENU menu, UINT m_uid, UINT state) |
95 |
{ |
{ |
96 |
MENUITEMINFO mii; |
MENUITEMINFO mii; |
97 |
|
|
98 |
memset( &mii, 0, sizeof (mii) ); |
memset (&mii, 0, sizeof (mii)); |
99 |
mii.cbSize = sizeof (mii); |
mii.cbSize = sizeof (mii); |
100 |
mii.fMask = MIIM_STATE; |
mii.fMask = MIIM_STATE; |
101 |
mii.fState = state; |
mii.fState = state; |
103 |
} |
} |
104 |
|
|
105 |
|
|
106 |
|
/* Retrieve the state of the menu item @m_uid and return it. */ |
107 |
|
UINT |
108 |
|
get_menu_state (HMENU menu, UINT m_uid) |
109 |
|
{ |
110 |
|
MENUITEMINFO mii; |
111 |
|
|
112 |
|
memset (&mii, 0, sizeof (mii)); |
113 |
|
mii.cbSize = sizeof (mii); |
114 |
|
mii.fMask = MIIM_STATE; |
115 |
|
GetMenuItemInfo (menu, m_uid, FALSE, &mii); |
116 |
|
return mii.fState; |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
enum { |
enum { |
121 |
CDLG_FILE_OPEN = 0, |
CDLG_FILE_OPEN = 0, |
122 |
CDLG_FILE_SAVE = 1 |
CDLG_FILE_SAVE = 1 |
126 |
id can be either FILE_OPEN or FILE_SAVE. |
id can be either FILE_OPEN or FILE_SAVE. |
127 |
The return value is the file name or NULL if cancel was chosen. */ |
The return value is the file name or NULL if cancel was chosen. */ |
128 |
const char * |
const char * |
129 |
get_filename_dlg (HWND hwnd, int id, const char * title, |
get_filename_dlg (HWND hwnd, int id, const char *title, |
130 |
const char * filter, const char * name) |
const char *filter, const char *name) |
131 |
{ |
{ |
132 |
static char file[512] = ""; |
static char file[512] = ""; |
133 |
OPENFILENAME open; |
OPENFILENAME open; |
137 |
else |
else |
138 |
memset (file, 0, sizeof (file)); |
memset (file, 0, sizeof (file)); |
139 |
if (!filter) |
if (!filter) |
140 |
filter = _("All Files (*.*)\0*.*\0\0"); |
filter = "All Files (*.*)\0*.*\0\0"; |
141 |
/* XXX: problem with gettext because of the 'artificial' |
/* XXX: problem with gettext because of the 'artificial' |
142 |
double string termination!. */ |
double string termination!. */ |
143 |
memset (&open, 0, sizeof (open)); |
memset (&open, 0, sizeof (open)); |
168 |
return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name); |
return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name); |
169 |
} |
} |
170 |
|
|
171 |
const char * |
const char* |
172 |
get_fileopen_dlg (HWND hwnd, const char *title, const char *filter, |
get_fileopen_dlg (HWND hwnd, const char *title, const char *filter, |
173 |
const char *name) |
const char *name) |
174 |
{ |
{ |
179 |
/* Use the common dialog to allow the user to select a folder. |
/* Use the common dialog to allow the user to select a folder. |
180 |
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. */ |
181 |
const char* |
const char* |
182 |
get_folder_dlg (HWND hwnd, const char * title, const char * name) |
get_folder_dlg (HWND hwnd, const char *title, const char *name) |
183 |
{ |
{ |
184 |
static char folder[MAX_PATH+1] = ""; |
static char folder[MAX_PATH+1] = ""; |
185 |
BROWSEINFO bi; |
BROWSEINFO bi; |
186 |
ITEMIDLIST * il; |
ITEMIDLIST *il; |
187 |
|
|
188 |
memset (&bi, 0, sizeof (bi)); |
memset (&bi, 0, sizeof (bi)); |
189 |
bi.hwndOwner = hwnd; |
bi.hwndOwner = hwnd; |
264 |
|
|
265 |
GlobalUnlock (clipmem); |
GlobalUnlock (clipmem); |
266 |
SetClipboardData (CF_TEXT, clipmem); |
SetClipboardData (CF_TEXT, clipmem); |
267 |
|
GlobalFree (clipmem); |
268 |
|
|
269 |
leave: |
leave: |
270 |
CloseClipboard (); |
CloseClipboard (); |
271 |
return rc; |
return rc; |
272 |
} /* set_clip_text */ |
} |
273 |
|
|
274 |
|
|
275 |
/* Append or prepend some text to the clipboard contents. |
/* Append or prepend some text to the clipboard contents. |
296 |
} |
} |
297 |
|
|
298 |
|
|
299 |
/* 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 |
300 |
|
an extension. @ext. |
301 |
|
Return value: the full file name on success. */ |
302 |
char* |
char* |
303 |
make_filename (const char *path, const char *file, const char *ext) |
make_filename (const char *path, const char *file, const char *ext) |
304 |
{ |
{ |
325 |
strcat( p, ext ); |
strcat( p, ext ); |
326 |
} |
} |
327 |
return p; |
return p; |
328 |
} /* make_filename */ |
} |
329 |
|
|
330 |
|
|
331 |
|
/* Generate a file name from a special dirctory. */ |
332 |
|
char* |
333 |
|
make_special_filename (int folder, const char *file, const char *ext) |
334 |
|
{ |
335 |
|
BOOL ec; |
336 |
|
char path[MAX_PATH], *p; |
337 |
|
size_t n=0; |
338 |
|
|
339 |
|
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
340 |
|
ec = SHGetSpecialFolderPath (HWND_DESKTOP, path, folder, TRUE); |
341 |
|
if (ec != 1) { |
342 |
|
log_debug ("SHGetSpecialFolderPath() failed\r\n", (int)GetLastError ()); |
343 |
|
return NULL; |
344 |
|
} |
345 |
|
|
346 |
|
n = strlen (path)+1; |
347 |
|
if (file) |
348 |
|
n += strlen (file)+1; |
349 |
|
if (ext) |
350 |
|
n += strlen (ext)+1; |
351 |
|
p = new char[n+2]; |
352 |
|
if (!p) |
353 |
|
BUG (0); |
354 |
|
memset (p, 0, n+2); |
355 |
|
strcpy (p, path); |
356 |
|
if (file) { |
357 |
|
strcat (p, "\\"); |
358 |
|
strcat (p, file); |
359 |
|
} |
360 |
|
if (ext) |
361 |
|
strcat (p, ext); |
362 |
|
return p; |
363 |
|
} |
364 |
|
|
365 |
|
|
366 |
/* return 0 if it exists, otherwise >0. */ |
/* return 0 if the file @fname exists, otherwise >0. */ |
367 |
int |
int |
368 |
file_exist_check (const char * fname) |
file_exist_check (const char *fname) |
369 |
{ |
{ |
370 |
struct stat st; |
struct stat st; |
371 |
if (stat (fname, &st) == -1) |
if (stat (fname, &st) == -1) |
381 |
{ |
{ |
382 |
struct stat statbuf; |
struct stat statbuf; |
383 |
|
|
384 |
if( stat( dir, &statbuf ) == -1 ) |
if (stat (dir, &statbuf) == -1) |
385 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
386 |
if( statbuf.st_mode & _S_IFDIR ) |
if (statbuf.st_mode & _S_IFDIR) |
387 |
return 0; |
return 0; |
388 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
389 |
} |
} |
408 |
} |
} |
409 |
|
|
410 |
|
|
|
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 */ |
|
|
|
|
|
|
|
411 |
/* 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 |
412 |
dialog is not already openened. */ |
dialog is not already openened. */ |
413 |
int |
int |
414 |
dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc, |
dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc, |
415 |
LPARAM param, LPCTSTR title, int title_id ) |
LPARAM param, LPCTSTR title, int title_id) |
416 |
{ |
{ |
417 |
#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 ) ) |
|
418 |
return -1; |
return -1; |
419 |
#endif |
return DialogBoxParam (hinst, name, parent, fnc, param); |
420 |
|
} |
|
return DialogBoxParam( hinst, name, parent, fnc, param ); |
|
|
} /* dialog_box_param */ |
|
421 |
|
|
422 |
|
|
423 |
/* Wrapper for message box which forces the message box into the |
/* Wrapper for message box which forces the message box into the |
432 |
} |
} |
433 |
|
|
434 |
|
|
435 |
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 */ |
|
|
|
|
|
|
|
436 |
char* |
char* |
437 |
m_strdup (const char *str) |
m_strdup (const char *str) |
438 |
{ |
{ |
439 |
char * p = new char[strlen (str) + 1]; |
char *p = new char[strlen (str) + 1]; |
440 |
if (p) |
if (!p) |
441 |
strcpy (p, str); |
BUG (NULL); |
442 |
|
strcpy (p, str); |
443 |
return p; |
return p; |
444 |
} /* m_strdup */ |
} |
445 |
|
|
446 |
|
|
447 |
/* Center the hwndChild relative to parent. |
/* Center the hwndChild relative to parent. |
493 |
{ |
{ |
494 |
center_window2 (hwndChild, hwndParent, NULL); |
center_window2 (hwndChild, hwndParent, NULL); |
495 |
} |
} |
496 |
|
|
497 |
|
|
498 |
|
/* Retrieve the product verion of the given file @fname. |
499 |
|
Format: MAJOR.MINOR.PATCH1.PATCH2 |
500 |
|
Return value: 0 on success. */ |
501 |
|
int |
502 |
|
get_file_version (const char *fname, WORD *major, WORD *minor, |
503 |
|
WORD *patch1, WORD *patch2) |
504 |
|
{ |
505 |
|
VS_FIXEDFILEINFO *inf = {0}; |
506 |
|
char file[MAX_PATH+1] = {0}; |
507 |
|
LPVOID buf, data; |
508 |
|
DWORD arg; |
509 |
|
DWORD size; |
510 |
|
UINT qlen; |
511 |
|
|
512 |
|
strncpy (file, fname, MAX_PATH); |
513 |
|
size = GetFileVersionInfoSize (file, &arg); |
514 |
|
if (!size) |
515 |
|
return -1; |
516 |
|
buf = (LPVOID)new CHAR[size]; |
517 |
|
if (!buf) |
518 |
|
BUG (NULL); |
519 |
|
GetFileVersionInfo (file, 0, size, buf); |
520 |
|
|
521 |
|
qlen=0; |
522 |
|
VerQueryValue (buf, "\\", &data, &qlen); |
523 |
|
if (!qlen) { |
524 |
|
delete [] (char*)buf; |
525 |
|
return -1; |
526 |
|
} |
527 |
|
inf = (VS_FIXEDFILEINFO*)data; |
528 |
|
|
529 |
|
if (major) |
530 |
|
*major = HIWORD (inf->dwProductVersionMS); |
531 |
|
if (minor) |
532 |
|
*minor = LOWORD (inf->dwProductVersionMS); |
533 |
|
if (patch1) |
534 |
|
*patch1 = HIWORD (inf->dwProductVersionLS); |
535 |
|
if (patch2) |
536 |
|
*patch2 = LOWORD (inf->dwProductVersionLS); |
537 |
|
|
538 |
|
delete [] (char*)buf; |
539 |
|
return 0; |
540 |
|
} |
541 |
|
|
542 |
|
|
543 |
|
/* Return date in a format which complies with the |
544 |
|
system locale settings. */ |
545 |
|
const char* |
546 |
|
get_locale_date (long tm_t, char *buf, DWORD buflen) |
547 |
|
{ |
548 |
|
SYSTEMTIME st; |
549 |
|
struct tm *ptm; |
550 |
|
|
551 |
|
ptm = localtime (&tm_t); |
552 |
|
st.wYear = (WORD)ptm->tm_year; |
553 |
|
st.wMonth = (WORD)ptm->tm_mon; |
554 |
|
st.wDay = (WORD)ptm->tm_mday; |
555 |
|
st.wYear += 1900; |
556 |
|
st.wMonth += 1; |
557 |
|
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
558 |
|
NULL, buf, buflen)) |
559 |
|
return NULL; |
560 |
|
return buf; |
561 |
|
} |
562 |
|
|
563 |
|
|
564 |
|
/* Generate a temporary file name by using the users |
565 |
|
temp path and optionally a name @name provided by the caller. |
566 |
|
Return value: 0 on success. */ |
567 |
|
int |
568 |
|
get_temp_name (char *buf, DWORD buflen, const char *name) |
569 |
|
{ |
570 |
|
char tmp[32]; |
571 |
|
|
572 |
|
if (!name) { |
573 |
|
sprintf (tmp, "%08lX", GetTickCount ()); |
574 |
|
name = tmp; |
575 |
|
} |
576 |
|
|
577 |
|
/* in the mobile mode we use a local temp folder |
578 |
|
with the fixed name 'temp'. */ |
579 |
|
if (mobile_mode_active) { |
580 |
|
_snprintf (buf, buflen-1, "temp\\%s", name); |
581 |
|
return 0; |
582 |
|
} |
583 |
|
|
584 |
|
if (!GetTempPath (buflen - strlen (name) -2, buf)) { |
585 |
|
log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ()); |
586 |
|
return -1; |
587 |
|
} |
588 |
|
strcat (buf, name); |
589 |
|
return 0; |
590 |
|
} |
591 |
|
|
592 |
|
|
593 |
|
struct reminder_hd_s { |
594 |
|
int msecs; |
595 |
|
HWND dlg; |
596 |
|
HANDLE hd; |
597 |
|
}; |
598 |
|
|
599 |
|
|
600 |
|
static DWORD CALLBACK |
601 |
|
foreground_reminder_thread (void *c) |
602 |
|
{ |
603 |
|
struct reminder_hd_s *ctx = (struct reminder_hd_s *)c; |
604 |
|
Sleep (ctx->msecs); |
605 |
|
SetForegroundWindow (ctx->dlg); |
606 |
|
CloseHandle (ctx->hd); |
607 |
|
delete ctx; |
608 |
|
ExitThread (0); |
609 |
|
return 0; |
610 |
|
} |
611 |
|
|
612 |
|
/* Try to force the window @dlg to the foreground. |
613 |
|
On NT5 or later this will not work if the user |
614 |
|
is working in another window (console for example). */ |
615 |
|
void |
616 |
|
force_foreground_window (HWND dlg, int msecs) |
617 |
|
{ |
618 |
|
struct reminder_hd_s *hd; |
619 |
|
DWORD tid; |
620 |
|
|
621 |
|
hd = new reminder_hd_s; |
622 |
|
hd->dlg = dlg; |
623 |
|
hd->msecs = msecs; |
624 |
|
hd->hd = CreateThread (NULL, 0, foreground_reminder_thread, |
625 |
|
hd, NULL, &tid); |
626 |
|
} |