(deps/libui) Cleanups

This commit is contained in:
twinaphex 2017-07-03 07:26:10 +02:00
parent 39b1e43cbc
commit abba2f387e

View File

@ -1,4 +1,4 @@
// 22 may 2015 /* 22 may 2015 */
#include "uipriv_windows.hpp" #include "uipriv_windows.hpp"
struct uiDateTimePicker { struct uiDateTimePicker {
@ -6,53 +6,58 @@ struct uiDateTimePicker {
HWND hwnd; HWND hwnd;
}; };
// utility functions #ifndef DTM_GETIDEALSIZE
#define DTM_GETIDEALSIZE 0x100f
#endif
/* utility functions */
#define GLI(what, buf, n) GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, what, buf, n) #define GLI(what, buf, n) GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, what, buf, n)
// The real date/time picker does a manual replacement of "yy" with "yyyy" for DTS_SHORTDATECENTURYFORMAT. /* The real date/time picker does a manual replacement of "yy" with "yyyy" for DTS_SHORTDATECENTURYFORMAT.
// Because we're also duplicating its functionality (see below), we have to do it too. * Because we're also duplicating its functionality (see below), we have to do it too. */
static WCHAR *expandYear(WCHAR *dts, int n) static WCHAR *expandYear(WCHAR *dts, int n)
{ {
WCHAR *out; WCHAR *p = NULL;
WCHAR *p, *q; int ny = 0;
int ny = 0;
// allocate more than we need to be safe /* allocate more than we need to be safe */
out = (WCHAR *) uiAlloc((n * 3) * sizeof (WCHAR), "WCHAR[]"); WCHAR *out = (WCHAR *) uiAlloc((n * 3) * sizeof (WCHAR), "WCHAR[]");
q = out; WCHAR *q = out;
for (p = dts; *p != L'\0'; p++) {
// first, if the current character is a y, increment the number of consecutive ys for (p = dts; *p != L'\0'; p++)
// otherwise, stop counting, and if there were only two, add two more to make four {
if (*p != L'y') { /* first, if the current character is a y, increment the number of consecutive ys
if (ny == 2) { * otherwise, stop counting, and if there were only two, add two more to make four */
*q++ = L'y'; if (*p != L'y') {
*q++ = L'y'; if (ny == 2) {
} *q++ = L'y';
ny = 0; *q++ = L'y';
} else }
ny++; ny = 0;
// next, handle quoted blocks } else
// we do this AFTER the above so yy'abc' becomes yyyy'abc' and not yy'abc'yy ny++;
// this handles the case of 'a''b' elegantly as well /* next, handle quoted blocks
if (*p == L'\'') { * we do this AFTER the above so yy'abc' becomes yyyy'abc' and not yy'abc'yy
// copy the opening quote * this handles the case of 'a''b' elegantly as well */
*q++ = *p; if (*p == L'\'') {
// copy the contents /* copy the opening quote */
for (;;) { *q++ = *p;
p++; /* copy the contents */
if (*p == L'\'') for (;;) {
break; p++;
if (*p == L'\0') if (*p == L'\'')
implbug("unterminated quote in system-provided locale date string in expandYear()"); break;
*q++ = *p; if (*p == L'\0')
} implbug("unterminated quote in system-provided locale date string in expandYear()");
// and fall through to copy the closing quote *q++ = *p;
} }
// copy the current character /* and fall through to copy the closing quote */
*q++ = *p; }
} /* copy the current character */
// handle trailing yy *q++ = *p;
}
/* handle trailing yy */
if (ny == 2) { if (ny == 2) {
*q++ = L'y'; *q++ = L'y';
*q++ = L'y'; *q++ = L'y';
@ -61,22 +66,23 @@ static WCHAR *expandYear(WCHAR *dts, int n)
return out; return out;
} }
// Windows has no combined date/time prebuilt constant; we have to build the format string ourselves /* Windows has no combined date/time prebuilt constant;
// TODO use a default format if one fails * we have to build the format string ourselves
* TODO use a default format if one fails */
static void setDateTimeFormat(HWND hwnd) static void setDateTimeFormat(HWND hwnd)
{ {
WCHAR *unexpandedDate, *date; WCHAR *unexpandedDate, *date;
WCHAR *time; WCHAR *time;
WCHAR *datetime; WCHAR *datetime;
int ndate, ntime; int ntime;
int ndate = GLI(LOCALE_SSHORTDATE, NULL, 0);
ndate = GLI(LOCALE_SSHORTDATE, NULL, 0);
if (ndate == 0) if (ndate == 0)
logLastError(L"error getting date string length"); logLastError(L"error getting date string length");
date = (WCHAR *) uiAlloc(ndate * sizeof (WCHAR), "WCHAR[]"); date = (WCHAR *) uiAlloc(ndate * sizeof (WCHAR), "WCHAR[]");
if (GLI(LOCALE_SSHORTDATE, date, ndate) == 0) if (GLI(LOCALE_SSHORTDATE, date, ndate) == 0)
logLastError(L"error geting date string"); logLastError(L"error geting date string");
unexpandedDate = date; // so we can free it unexpandedDate = date; /* so we can free it */ */ */ */
date = expandYear(unexpandedDate, ndate); date = expandYear(unexpandedDate, ndate);
uiFree(unexpandedDate); uiFree(unexpandedDate);
@ -96,7 +102,7 @@ static void setDateTimeFormat(HWND hwnd)
uiFree(date); uiFree(date);
} }
// control implementation /* control implementation */
static void uiDateTimePickerDestroy(uiControl *c) static void uiDateTimePickerDestroy(uiControl *c)
{ {
@ -109,15 +115,15 @@ static void uiDateTimePickerDestroy(uiControl *c)
uiWindowsControlAllDefaultsExceptDestroy(uiDateTimePicker) uiWindowsControlAllDefaultsExceptDestroy(uiDateTimePicker)
// the height returned from DTM_GETIDEALSIZE is unreliable; see http://stackoverflow.com/questions/30626549/what-is-the-proper-use-of-dtm-getidealsize-treating-the-returned-size-as-pixels /* the height returned from DTM_GETIDEALSIZE is unreliable; see http://stackoverflow.com/questions/30626549/what-is-the-proper-use-of-dtm-getidealsize-treating-the-returned-size-as-pixels
// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing * from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing */
#define entryHeight 14 #define entryHeight 14
static void uiDateTimePickerMinimumSize(uiWindowsControl *c, int *width, int *height) static void uiDateTimePickerMinimumSize(uiWindowsControl *c, int *width, int *height)
{ {
uiDateTimePicker *d = uiDateTimePicker(c);
SIZE s; SIZE s;
uiWindowsSizing sizing; uiWindowsSizing sizing;
uiDateTimePicker *d = uiDateTimePicker(c);
int y; int y;
s.cx = 0; s.cx = 0;
@ -143,41 +149,42 @@ static uiDateTimePicker *finishNewDateTimePicker(DWORD style)
hInstance, NULL, hInstance, NULL,
TRUE); TRUE);
// automatically update date/time format when user changes locale settings /* automatically update date/time format when user changes locale settings
// for the standard styles, this is in the date-time picker itself * for the standard styles, this is in the date-time picker itself
// for our date/time mode, we do it in a subclass assigned in uiNewDateTimePicker() * for our date/time mode, we do it in a subclass assigned in uiNewDateTimePicker() */
uiWindowsRegisterReceiveWM_WININICHANGE(d->hwnd); uiWindowsRegisterReceiveWM_WININICHANGE(d->hwnd);
return d; return d;
} }
static LRESULT CALLBACK datetimepickerSubProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) static LRESULT CALLBACK datetimepickerSubProc(HWND hwnd, UINT uMsg, WPARAM wParam,
LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{ {
switch (uMsg) { switch (uMsg)
case WM_WININICHANGE: {
// we can optimize this by only doing it when the real date/time picker does it case WM_WININICHANGE:
// unfortunately, I don't know when that is :/ // we can optimize this by only doing it when the real date/time picker does it
// hopefully this won't hurt // unfortunately, I don't know when that is :/
setDateTimeFormat(hwnd); // hopefully this won't hurt
return 0; setDateTimeFormat(hwnd);
case WM_NCDESTROY: return 0;
if (RemoveWindowSubclass(hwnd, datetimepickerSubProc, uIdSubclass) == FALSE) case WM_NCDESTROY:
logLastError(L"error removing date-time picker locale change handling subclass"); if (RemoveWindowSubclass(hwnd, datetimepickerSubProc, uIdSubclass) == FALSE)
break; logLastError(L"error removing date-time picker locale change handling subclass");
} break;
return DefSubclassProc(hwnd, uMsg, wParam, lParam); }
return DefSubclassProc(hwnd, uMsg, wParam, lParam);
} }
uiDateTimePicker *uiNewDateTimePicker(void) uiDateTimePicker *uiNewDateTimePicker(void)
{ {
uiDateTimePicker *d; uiDateTimePicker *d = finishNewDateTimePicker(0);
d = finishNewDateTimePicker(0); setDateTimeFormat(d->hwnd);
setDateTimeFormat(d->hwnd); if (SetWindowSubclass(d->hwnd, datetimepickerSubProc, 0, (DWORD_PTR) d) == FALSE)
if (SetWindowSubclass(d->hwnd, datetimepickerSubProc, 0, (DWORD_PTR) d) == FALSE) logLastError(L"error subclassing date-time-picker to assist in locale change handling");
logLastError(L"error subclassing date-time-picker to assist in locale change handling"); /* TODO set a suitable default in this case */
// TODO set a suitable default in this case return d;
return d;
} }
uiDateTimePicker *uiNewDatePicker(void) uiDateTimePicker *uiNewDatePicker(void)