1 |
/* wptW32API.cpp - Common W32 API functions |
/* wptW32API.cpp - Common W32 API functions |
2 |
* Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz |
* Copyright (C) 2001, 2002, 2003, 2005, 2006 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
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. |
|
* |
|
|
* You should have received a copy of the GNU General Public License |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
15 |
*/ |
*/ |
|
|
|
16 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
#include <config.h> |
18 |
#endif |
#endif |
37 |
extern "C" void _SHFree (void *p); |
extern "C" void _SHFree (void *p); |
38 |
|
|
39 |
|
|
|
|
|
40 |
/* Insert a new item into the menu @hm at position @pos. */ |
/* Insert a new item into the menu @hm at position @pos. */ |
41 |
void |
void |
42 |
insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text) |
insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text) |
124 |
get_filename_dlg (HWND hwnd, int id, const char *title, |
get_filename_dlg (HWND hwnd, int id, const char *title, |
125 |
const char *filter, const char *name) |
const char *filter, const char *name) |
126 |
{ |
{ |
127 |
static char file[512] = ""; |
static char file[2*MAX_PATH+1] = ""; |
128 |
|
char workdir[MAX_PATH+1]; |
129 |
|
char *sel_file = NULL; |
130 |
OPENFILENAME open; |
OPENFILENAME open; |
131 |
|
|
132 |
if (name && strlen (name) < (sizeof (file)-1)) |
if (name && strlen (name) < (DIM (file)-1)) |
133 |
strcpy (file, name); |
strcpy (file, name); |
134 |
else |
else |
135 |
memset (file, 0, sizeof (file)); |
memset (file, 0, sizeof (file)); |
136 |
if (!filter) |
if (!filter) |
137 |
filter = "All Files (*.*)\0*.*\0\0"; |
filter = "All Files (*.*)\0*.*\0\0"; |
138 |
/* XXX: problem with gettext because of the 'artificial' |
|
139 |
double string termination!. */ |
GetCurrentDirectory (MAX_PATH, workdir); |
140 |
memset (&open, 0, sizeof (open)); |
memset (&open, 0, sizeof (open)); |
141 |
open.lStructSize = sizeof (OPENFILENAME); |
open.lStructSize = sizeof (OPENFILENAME); |
142 |
open.hInstance = glob_hinst; |
open.hInstance = glob_hinst; |
144 |
open.lpstrFilter = filter; |
open.lpstrFilter = filter; |
145 |
open.hwndOwner = hwnd; |
open.hwndOwner = hwnd; |
146 |
open.lpstrFile = file; |
open.lpstrFile = file; |
147 |
open.nMaxFile = sizeof (file) - 1; |
open.nMaxFile = DIM (file) - 1; |
148 |
if (id == CDLG_FILE_OPEN) |
if (id == CDLG_FILE_OPEN) |
149 |
open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST; |
open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST; |
150 |
else |
else |
151 |
open.Flags = OFN_OVERWRITEPROMPT; |
open.Flags = OFN_OVERWRITEPROMPT; |
152 |
|
|
153 |
if (id == CDLG_FILE_OPEN && GetOpenFileName (&open)) |
if (id == CDLG_FILE_OPEN && GetOpenFileName (&open)) |
154 |
return open.lpstrFile; |
sel_file = open.lpstrFile; |
155 |
else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open)) |
else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open)) |
156 |
return open.lpstrFile; |
sel_file = open.lpstrFile; |
157 |
|
SetCurrentDirectory (workdir); |
158 |
|
|
159 |
return NULL; |
return sel_file; |
160 |
} |
} |
161 |
|
|
162 |
const char* |
const char* |
192 |
else |
else |
193 |
memset (folder, 0, sizeof (folder)); |
memset (folder, 0, sizeof (folder)); |
194 |
il = SHBrowseForFolder (&bi); |
il = SHBrowseForFolder (&bi); |
195 |
if (il) { |
if (il != NULL) { |
196 |
SHGetPathFromIDList (il, folder); |
SHGetPathFromIDList (il, folder); |
197 |
_SHFree (il); |
_SHFree (il); |
198 |
return folder; |
return folder; |
275 |
int |
int |
276 |
set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer) |
set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer) |
277 |
{ |
{ |
278 |
|
const char *fmt; |
279 |
char *p, *new_text; |
char *p, *new_text; |
280 |
|
|
281 |
p = get_clip_text (hwnd); |
p = get_clip_text (hwnd); |
282 |
if (!p) |
if (!p) |
283 |
return WPTERR_CLIP_GET; |
return WPTERR_CLIP_GET; |
284 |
new_text = new char [strlen (p)+strlen (text)+8]; |
if (as_footer == 0) |
285 |
|
fmt = "%s\r\n%s\r\n\r\n"; |
286 |
|
else |
287 |
|
fmt = "%s\n%s\n\n"; |
288 |
|
new_text = new char [strlen (p)+strlen (text)+strlen (fmt)+2]; |
289 |
if (!new_text) |
if (!new_text) |
290 |
BUG (0); |
BUG (0); |
291 |
if (as_footer == 0) |
if (as_footer == 0) |
292 |
sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p); |
sprintf (new_text, fmt, text, p); |
293 |
else |
else |
294 |
sprintf (new_text, "%s\n%s\n\n", p, text); |
sprintf (new_text, fmt, p, text); |
295 |
set_clip_text (hwnd, new_text, strlen (new_text)+1); |
set_clip_text (hwnd, new_text, strlen (new_text)+1); |
296 |
free_if_alloc (p); |
free_if_alloc (p); |
297 |
free_if_alloc (new_text); |
free_if_alloc (new_text); |
308 |
char *p; |
char *p; |
309 |
size_t size = 0; |
size_t size = 0; |
310 |
|
|
311 |
if( path && *path ) |
if (path && *path) |
312 |
size += strlen( path ); |
size += strlen (path); |
313 |
if( file && *file ) |
if (file && *file) |
314 |
size += strlen( file ); |
size += strlen (file); |
315 |
if( ext && *ext ) |
if (ext && *ext) |
316 |
size += strlen( ext ); |
size += strlen (ext); |
317 |
p = new char[size + 4]; |
p = new char[size + 4]; |
318 |
memset( p, 0, size ); |
if (!p) |
319 |
if( path ) { |
BUG (0); |
320 |
strcat( p, path ); |
memset (p, 0, size); |
321 |
if( path[strlen( path ) -1] != '\\' ) |
if (path) { |
322 |
strcat( p, "\\" ); |
strcat (p, path); |
323 |
} |
if (path[strlen (path) -1] != '\\') |
324 |
if( file ) |
strcat (p, "\\"); |
325 |
strcat( p, file ); |
} |
326 |
if( ext ) { |
if (file != NULL) |
327 |
strcat( p, "." ); |
strcat (p, file); |
328 |
strcat( p, ext ); |
if (ext != NULL) { |
329 |
|
strcat (p, "."); |
330 |
|
strcat (p, ext); |
331 |
} |
} |
332 |
return p; |
return p; |
333 |
} |
} |
437 |
} |
} |
438 |
|
|
439 |
|
|
|
/* Safe strdup version (C++ version). */ |
|
|
char* |
|
|
m_strdup (const char *str) |
|
|
{ |
|
|
char *p = new char[strlen (str) + 1]; |
|
|
if (!p) |
|
|
BUG (NULL); |
|
|
strcpy (p, str); |
|
|
return p; |
|
|
} |
|
|
|
|
|
|
|
440 |
/* Center the hwndChild relative to parent. |
/* Center the hwndChild relative to parent. |
441 |
The style param allows to specificy additional styles (like topmost). */ |
The style param allows to specificy additional styles (like topmost). */ |
442 |
void |
void |
482 |
|
|
483 |
/* Center the given hwndChild window with no special style. */ |
/* Center the given hwndChild window with no special style. */ |
484 |
void |
void |
485 |
center_window (HWND hwndChild, HWND hwndParent) |
center_window (HWND child, HWND parent) |
486 |
{ |
{ |
487 |
center_window2 (hwndChild, hwndParent, NULL); |
center_window2 (child, parent, NULL); |
488 |
} |
} |
489 |
|
|
490 |
|
|
492 |
Format: MAJOR.MINOR.PATCH1.PATCH2 |
Format: MAJOR.MINOR.PATCH1.PATCH2 |
493 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
494 |
int |
int |
495 |
get_file_version (const char *fname, WORD *major, WORD *minor, |
get_file_version (const char *fname, |
496 |
WORD *patch1, WORD *patch2) |
WORD *major, WORD *minor, WORD *patch1, WORD *patch2) |
497 |
{ |
{ |
498 |
VS_FIXEDFILEINFO *inf; |
VS_FIXEDFILEINFO *inf; |
499 |
char file[MAX_PATH+1] = {0}; |
char file[MAX_PATH+1] = {0}; |
534 |
|
|
535 |
|
|
536 |
/* Return date in a format which complies with the |
/* Return date in a format which complies with the |
537 |
system locale settings. */ |
locale user settings. */ |
538 |
const char* |
const char* |
539 |
get_locale_date (long tm_t, char *buf, DWORD buflen) |
get_locale_date (long tm_t, char *buf, DWORD buflen) |
540 |
{ |
{ |
541 |
SYSTEMTIME st; |
SYSTEMTIME st; |
542 |
struct tm *ptm; |
struct tm *ptm; |
543 |
|
|
544 |
ptm = localtime (&tm_t); |
ptm = localtime (&tm_t); |
545 |
st.wYear = (WORD)ptm->tm_year; |
memset (&st, 0, sizeof (st)); |
546 |
st.wMonth = (WORD)ptm->tm_mon; |
st.wYear = (WORD)ptm->tm_year+1900; |
547 |
|
st.wMonth = (WORD)ptm->tm_mon+1; |
548 |
st.wDay = (WORD)ptm->tm_mday; |
st.wDay = (WORD)ptm->tm_mday; |
|
st.wYear += 1900; |
|
|
st.wMonth += 1; |
|
549 |
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
550 |
NULL, buf, buflen)) |
NULL, buf, buflen)) |
551 |
return NULL; |
return NULL; |
552 |
return buf; |
return buf; |
553 |
} |
} |
554 |
|
|
555 |
|
/* Return the time in a format which complies with the locale user |
556 |
|
settings. */ |
557 |
|
const char* |
558 |
|
get_locale_time (long tm_t, char *buf, DWORD buflen) |
559 |
|
{ |
560 |
|
SYSTEMTIME st; |
561 |
|
struct tm *ptm; |
562 |
|
|
563 |
|
ptm = localtime (&tm_t); |
564 |
|
memset (&st, 0, sizeof (st)); |
565 |
|
st.wMinute = (WORD)ptm->tm_min; |
566 |
|
st.wHour = (WORD)ptm->tm_hour; |
567 |
|
st.wSecond = (WORD)ptm->tm_sec; |
568 |
|
|
569 |
|
if (!GetTimeFormat (LOCALE_USER_DEFAULT, TIME_FORCE24HOURFORMAT, &st, |
570 |
|
NULL, buf, buflen)) |
571 |
|
return NULL; |
572 |
|
return buf; |
573 |
|
} |
574 |
|
|
575 |
|
|
576 |
|
/* Return a combination of date+time which complies to the |
577 |
|
local user settings. */ |
578 |
|
const char* |
579 |
|
get_locale_timedate (long tm_t, char *buf, DWORD buflen) |
580 |
|
{ |
581 |
|
DWORD nleft = buflen; |
582 |
|
|
583 |
|
if (!get_locale_date (tm_t, buf, buflen)) |
584 |
|
return NULL; |
585 |
|
nleft -= strlen (buf)+1; |
586 |
|
if (nleft < 1) |
587 |
|
return NULL; |
588 |
|
strcat (buf, " "); |
589 |
|
nleft--; |
590 |
|
if (!get_locale_time (tm_t, buf+strlen (buf), nleft)) |
591 |
|
return NULL; |
592 |
|
return buf; |
593 |
|
} |
594 |
|
|
595 |
|
|
596 |
/* Generate a temporary file name by using the users |
/* Generate a temporary file name by using the users |
597 |
temp path and optionally a name @name provided by the caller. |
temp path and optionally a name @name provided by the caller. |
601 |
{ |
{ |
602 |
char tmp[32]; |
char tmp[32]; |
603 |
|
|
604 |
|
|
605 |
if (!name) { |
if (!name) { |
606 |
sprintf (tmp, "%08lX", GetTickCount ()); |
_snprintf (tmp, DIM (tmp)-1, "%08lX", GetTickCount ()); |
607 |
name = tmp; |
name = tmp; |
608 |
} |
} |
609 |
|
|
610 |
/* in the mobile mode we use a local temp folder |
/* In the mobile mode we need to use the current directory |
611 |
with the fixed name 'temp'. */ |
as the temp path because no files should be stored outside |
612 |
if (mobile_mode_active) { |
the USB disk drive. */ |
613 |
_snprintf (buf, buflen-1, "temp\\%s", name); |
#ifdef WINPT_MOBILE |
614 |
return 0; |
memset (buf, 0, buflen); |
615 |
} |
strncpy (buf, name, buflen-1); |
616 |
|
return 0; |
617 |
|
#endif |
618 |
|
|
619 |
if (!GetTempPath (buflen - strlen (name) -2, buf)) { |
if (!GetTempPath (buflen - strlen (name) -2, buf)) { |
620 |
log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ()); |
log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ()); |
621 |
return -1; |
return -1; |
622 |
} |
} |
623 |
|
|
624 |
strcat (buf, name); |
strcat (buf, name); |
625 |
return 0; |
return 0; |
626 |
} |
} |
640 |
void |
void |
641 |
ComboBox_AddString_utf8 (HWND cb, const char *txt) |
ComboBox_AddString_utf8 (HWND cb, const char *txt) |
642 |
{ |
{ |
|
|
|
643 |
char *utf8_txt; |
char *utf8_txt; |
644 |
|
|
645 |
utf8_txt = utf8_to_native (txt); |
utf8_txt = utf8_to_native (txt); |