From d9f94d4326637e36452b73cfe64ca19886001093 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 22 May 2015 13:24:07 -0400 Subject: [PATCH] More date/time picker work. --- redo/windows/datetimepicker.c | 48 +++++++++++++++++++++++++++++++---- redo/windows/text.c | 2 ++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/redo/windows/datetimepicker.c b/redo/windows/datetimepicker.c index 785d82fd..05bef02b 100644 --- a/redo/windows/datetimepicker.c +++ b/redo/windows/datetimepicker.c @@ -1,6 +1,8 @@ // 22 may 2015 #include "uipriv_windows.h" +// TODO does the control update builtin date and time formats with a locale change? + struct datetimepicker { uiDateTimePicker d; HWND hwnd; @@ -25,7 +27,7 @@ static void datetimepickerPreferredSize(uiControl *c, uiSizing *d, intmax_t *wid *height = uiWindowsDlgUnitsToY(entryHeight, d->Sys->BaseY); } -uiDateTimePicker *finishNewDateTimePicker(DWORD style) +uiDateTimePicker *finishNewDateTimePicker(DWORD style, WCHAR *format) { struct datetimepicker *d; uiWindowsMakeControlParams p; @@ -46,23 +48,59 @@ uiDateTimePicker *finishNewDateTimePicker(DWORD style) d->hwnd = (HWND) uiControlHandle(uiControl(d)); + if (format != NULL) + if (SendMessageW(d->hwnd, DTM_SETFORMAT, 0, (LPARAM) format) == 0) + logLastError("error applying format string to date/time picker in finishNewDateTimePicker()"); + uiControl(d)->PreferredSize = datetimepickerPreferredSize; return uiDateTimePicker(d); } -// TODO +// TODO move to GetLocaleInfoEx() when making Vista-only +#define GLI(what, buf, n) GetLocaleInfoW(LOCALE_USER_DEFAULT, what, buf, n) + +// Windows has no combined date/time prebuilt constant; we have to build the format string ourselves uiDateTimePicker *uiNewDateTimePicker(void) { - return finishNewDateTimePicker(0); + WCHAR *date, *time, *datetime; + int ndate, ntime; + int n; + uiDateTimePicker *dtp; + + // TODO verify that this always returns a century year + ndate = GLI(LOCALE_SSHORTDATE, NULL, 0); + if (ndate == 0) + logLastError("error getting date string length in uiNewDateTimePicker()"); + date = (WCHAR *) uiAlloc(ndate * sizeof (WCHAR), "WCHAR[]"); + if (GLI(LOCALE_SSHORTDATE, date, ndate) == 0) + logLastError("error geting date string in uiNewDateTimePicker()"); + + ntime = GLI(LOCALE_STIMEFORMAT, NULL, 0); + if (ndate == 0) + logLastError("error getting time string length in uiNewDateTimePicker()"); + time = (WCHAR *) uiAlloc(ntime * sizeof (WCHAR), "WCHAR[]"); + if (GLI(LOCALE_STIMEFORMAT, time, ntime) == 0) + logLastError("error geting time string in uiNewDateTimePicker()"); + + n = _scwprintf(L"%s %s", date, time); + datetime = (WCHAR *) uiAlloc((n + 1) * sizeof (WCHAR), "WCHAR[]"); + snwprintf(datetime, n + 1, L"%s %s", date, time); + dtp = finishNewDateTimePicker(0, datetime); + + uiFree(datetime); + uiFree(time); + uiFree(date); + + return dtp; } uiDateTimePicker *uiNewDatePicker(void) { - return finishNewDateTimePicker(DTS_SHORTDATECENTURYFORMAT); + return finishNewDateTimePicker(DTS_SHORTDATECENTURYFORMAT, NULL); } uiDateTimePicker *uiNewTimePicker(void) { - return finishNewDateTimePicker(DTS_TIMEFORMAT); + return finishNewDateTimePicker(DTS_TIMEFORMAT, NULL); } diff --git a/redo/windows/text.c b/redo/windows/text.c index 8d6d0b0a..6cea0c9a 100644 --- a/redo/windows/text.c +++ b/redo/windows/text.c @@ -3,6 +3,8 @@ // see http://stackoverflow.com/a/29556509/3408572 +// TODO windows vista only options + #define MBTWC(str, wstr, bufsiz) MultiByteToWideChar(CP_UTF8, 0, str, -1, wstr, bufsiz) WCHAR *toUTF16(const char *str)