More date/time picker work.

This commit is contained in:
Pietro Gagliardi 2015-05-22 13:24:07 -04:00
parent d5e956e254
commit d9f94d4326
2 changed files with 45 additions and 5 deletions

View File

@ -1,6 +1,8 @@
// 22 may 2015 // 22 may 2015
#include "uipriv_windows.h" #include "uipriv_windows.h"
// TODO does the control update builtin date and time formats with a locale change?
struct datetimepicker { struct datetimepicker {
uiDateTimePicker d; uiDateTimePicker d;
HWND hwnd; HWND hwnd;
@ -25,7 +27,7 @@ static void datetimepickerPreferredSize(uiControl *c, uiSizing *d, intmax_t *wid
*height = uiWindowsDlgUnitsToY(entryHeight, d->Sys->BaseY); *height = uiWindowsDlgUnitsToY(entryHeight, d->Sys->BaseY);
} }
uiDateTimePicker *finishNewDateTimePicker(DWORD style) uiDateTimePicker *finishNewDateTimePicker(DWORD style, WCHAR *format)
{ {
struct datetimepicker *d; struct datetimepicker *d;
uiWindowsMakeControlParams p; uiWindowsMakeControlParams p;
@ -46,23 +48,59 @@ uiDateTimePicker *finishNewDateTimePicker(DWORD style)
d->hwnd = (HWND) uiControlHandle(uiControl(d)); 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; uiControl(d)->PreferredSize = datetimepickerPreferredSize;
return uiDateTimePicker(d); 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) 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) uiDateTimePicker *uiNewDatePicker(void)
{ {
return finishNewDateTimePicker(DTS_SHORTDATECENTURYFORMAT); return finishNewDateTimePicker(DTS_SHORTDATECENTURYFORMAT, NULL);
} }
uiDateTimePicker *uiNewTimePicker(void) uiDateTimePicker *uiNewTimePicker(void)
{ {
return finishNewDateTimePicker(DTS_TIMEFORMAT); return finishNewDateTimePicker(DTS_TIMEFORMAT, NULL);
} }

View File

@ -3,6 +3,8 @@
// see http://stackoverflow.com/a/29556509/3408572 // 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) #define MBTWC(str, wstr, bufsiz) MultiByteToWideChar(CP_UTF8, 0, str, -1, wstr, bufsiz)
WCHAR *toUTF16(const char *str) WCHAR *toUTF16(const char *str)