35 |
#include "wptW32API.h" |
#include "wptW32API.h" |
36 |
#include "wptErrors.h" |
#include "wptErrors.h" |
37 |
#include "wptVersion.h" |
#include "wptVersion.h" |
38 |
|
#include "wptUTF8.h" |
39 |
#include "wptTypes.h" |
#include "wptTypes.h" |
40 |
|
|
41 |
|
|
42 |
extern "C" void _SHFree (void *p); |
extern "C" void _SHFree (void *p); |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
/* Insert a new item into the menu @hm at position @pos. */ |
47 |
|
void |
48 |
|
insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text) |
49 |
|
{ |
50 |
|
MENUITEMINFO mi; |
51 |
|
|
52 |
|
memset (&mi, 0, sizeof mi); |
53 |
|
mi.cbSize = sizeof mi; |
54 |
|
mi.fType = MF_STRING; |
55 |
|
mi.dwTypeData = (char *)text; |
56 |
|
mi.cch = strlen (text); |
57 |
|
mi.wID = m_id; |
58 |
|
mi.fMask = MIIM_ID|MIIM_DATA| MIIM_TYPE; |
59 |
|
InsertMenuItem (hm, pos, FALSE, &mi); |
60 |
|
} |
61 |
|
|
62 |
|
|
63 |
static void |
static void |
64 |
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) |
65 |
{ |
{ |
127 |
id can be either FILE_OPEN or FILE_SAVE. |
id can be either FILE_OPEN or FILE_SAVE. |
128 |
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. */ |
129 |
const char * |
const char * |
130 |
get_filename_dlg (HWND hwnd, int id, const char * title, |
get_filename_dlg (HWND hwnd, int id, const char *title, |
131 |
const char * filter, const char * name) |
const char *filter, const char *name) |
132 |
{ |
{ |
133 |
static char file[512] = ""; |
static char file[512] = ""; |
134 |
OPENFILENAME open; |
OPENFILENAME open; |
138 |
else |
else |
139 |
memset (file, 0, sizeof (file)); |
memset (file, 0, sizeof (file)); |
140 |
if (!filter) |
if (!filter) |
141 |
filter = _("All Files (*.*)\0*.*\0\0"); |
filter = "All Files (*.*)\0*.*\0\0"; |
142 |
/* XXX: problem with gettext because of the 'artificial' |
/* XXX: problem with gettext because of the 'artificial' |
143 |
double string termination!. */ |
double string termination!. */ |
144 |
memset (&open, 0, sizeof (open)); |
memset (&open, 0, sizeof (open)); |
257 |
BUG (NULL); |
BUG (NULL); |
258 |
p = (char *) GlobalLock (clipmem); |
p = (char *) GlobalLock (clipmem); |
259 |
if (p == NULL) { |
if (p == NULL) { |
260 |
rc = WPTERR_GENERAL;; |
CloseClipboard (); |
261 |
goto leave; |
GlobalFree (clipmem); |
262 |
|
return WPTERR_GENERAL; |
263 |
} |
} |
264 |
memcpy (p, text, nbytes); |
memcpy (p, text, nbytes); |
265 |
p[nbytes] = '\0'; |
p[nbytes] = '\0'; |
266 |
|
|
|
GlobalUnlock (clipmem); |
|
267 |
SetClipboardData (CF_TEXT, clipmem); |
SetClipboardData (CF_TEXT, clipmem); |
268 |
|
GlobalUnlock (clipmem); |
|
leave: |
|
269 |
CloseClipboard (); |
CloseClipboard (); |
270 |
|
GlobalFree (clipmem); |
271 |
|
|
272 |
return rc; |
return rc; |
273 |
} /* set_clip_text */ |
} |
274 |
|
|
275 |
|
|
276 |
/* Append or prepend some text to the clipboard contents. |
/* Append or prepend some text to the clipboard contents. |
306 |
char *p; |
char *p; |
307 |
size_t size = 0; |
size_t size = 0; |
308 |
|
|
309 |
if( path && *path ) |
if (path && *path) |
310 |
size += strlen( path ); |
size += strlen (path); |
311 |
if( file && *file ) |
if (file && *file) |
312 |
size += strlen( file ); |
size += strlen (file); |
313 |
if( ext && *ext ) |
if( ext && *ext ) |
314 |
size += strlen( ext ); |
size += strlen( ext ); |
315 |
p = new char[size + 4]; |
p = new char[size + 4]; |
316 |
|
if (!p) |
317 |
|
BUG (0); |
318 |
memset( p, 0, size ); |
memset( p, 0, size ); |
319 |
if( path ) { |
if( path ) { |
320 |
strcat( p, path ); |
strcat( p, path ); |
505 |
get_file_version (const char *fname, WORD *major, WORD *minor, |
get_file_version (const char *fname, WORD *major, WORD *minor, |
506 |
WORD *patch1, WORD *patch2) |
WORD *patch1, WORD *patch2) |
507 |
{ |
{ |
508 |
VS_FIXEDFILEINFO *inf = {0}; |
VS_FIXEDFILEINFO *inf; |
509 |
char file[MAX_PATH+1] = {0}; |
char file[MAX_PATH+1] = {0}; |
510 |
LPVOID buf, data; |
LPVOID buf, data; |
511 |
DWORD arg; |
DWORD arg; |
516 |
size = GetFileVersionInfoSize (file, &arg); |
size = GetFileVersionInfoSize (file, &arg); |
517 |
if (!size) |
if (!size) |
518 |
return -1; |
return -1; |
519 |
buf = (LPVOID)new CHAR[size]; |
buf = (LPVOID)new char[size]; |
520 |
if (!buf) |
if (!buf) |
521 |
BUG (NULL); |
BUG (NULL); |
522 |
GetFileVersionInfo (file, 0, size, buf); |
GetFileVersionInfo (file, 0, size, buf); |
524 |
qlen=0; |
qlen=0; |
525 |
VerQueryValue (buf, "\\", &data, &qlen); |
VerQueryValue (buf, "\\", &data, &qlen); |
526 |
if (!qlen) { |
if (!qlen) { |
527 |
delete [] (char*)buf; |
delete [](char*)buf; |
528 |
return -1; |
return -1; |
529 |
} |
} |
530 |
inf = (VS_FIXEDFILEINFO*)data; |
inf = (VS_FIXEDFILEINFO*)data; |
538 |
if (patch2) |
if (patch2) |
539 |
*patch2 = LOWORD (inf->dwProductVersionLS); |
*patch2 = LOWORD (inf->dwProductVersionLS); |
540 |
|
|
541 |
delete [] (char*)buf; |
delete [](char*)buf; |
542 |
return 0; |
return 0; |
543 |
} |
} |
544 |
|
|
564 |
} |
} |
565 |
|
|
566 |
|
|
567 |
struct reminder_hd_s { |
/* Generate a temporary file name by using the users |
568 |
int msecs; |
temp path and optionally a name @name provided by the caller. |
569 |
HWND dlg; |
Return value: 0 on success. */ |
570 |
HANDLE hd; |
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 |
static DWORD CALLBACK |
/* in the mobile mode we use a local temp folder |
581 |
foreground_reminder_thread (void *c) |
with the fixed name 'temp'. */ |
582 |
{ |
if (mobile_mode_active) { |
583 |
struct reminder_hd_s *ctx = (struct reminder_hd_s *)c; |
_snprintf (buf, buflen-1, "temp\\%s", name); |
584 |
Sleep (ctx->msecs); |
return 0; |
585 |
SetForegroundWindow (ctx->dlg); |
} |
586 |
CloseHandle (ctx->hd); |
|
587 |
delete ctx; |
if (!GetTempPath (buflen - strlen (name) -2, buf)) { |
588 |
ExitThread (0); |
log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ()); |
589 |
|
return -1; |
590 |
|
} |
591 |
|
strcat (buf, name); |
592 |
return 0; |
return 0; |
593 |
} |
} |
594 |
|
|
595 |
/* Try to force the window @dlg to the foreground. |
|
|
On NT5 or later this will not work if the user |
|
|
is working in another window (console for example). */ |
|
596 |
void |
void |
597 |
force_foreground_window (HWND dlg, int msecs) |
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 |
struct reminder_hd_s *hd; |
int len = GetWindowTextLength (GetDlgItem (dlg, id)); |
624 |
DWORD tid; |
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 |
hd = new reminder_hd_s; |
CloseHandle (hd); |
685 |
hd->dlg = dlg; |
return admin; |
|
hd->msecs = msecs; |
|
|
hd->hd = CreateThread (NULL, 0, foreground_reminder_thread, |
|
|
hd, NULL, &tid); |
|
686 |
} |
} |