libui/windows/entry.cpp

100 lines
2.3 KiB
C++
Raw Normal View History

// 8 april 2015
2016-04-22 19:14:12 -05:00
#include "uipriv_windows.hpp"
struct uiEntry {
uiWindowsControl c;
HWND hwnd;
void (*onChanged)(uiEntry *, void *);
void *onChangedData;
BOOL inhibitChanged;
};
uiWindowsDefineControlWithOnDestroy(
uiEntry, // type name
uiWindowsUnregisterWM_COMMANDHandler(me->hwnd); // on destroy
)
static BOOL onWM_COMMAND(uiControl *c, HWND hwnd, WORD code, LRESULT *lResult)
{
uiEntry *e = uiEntry(c);
if (code != EN_CHANGE)
return FALSE;
if (e->inhibitChanged)
return FALSE;
(*(e->onChanged))(e, e->onChangedData);
*lResult = 0;
return TRUE;
}
// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
#define entryWidth 107 /* this is actually the shorter progress bar width, but Microsoft only indicates as wide as necessary */
#define entryHeight 14
static void minimumSize(uiWindowsControl *c, uiWindowsSizing *d, intmax_t *width, intmax_t *height)
{
*width = uiWindowsDlgUnitsToX(entryWidth, d->BaseX);
*height = uiWindowsDlgUnitsToY(entryHeight, d->BaseY);
}
static void defaultOnChanged(uiEntry *e, void *data)
{
// do nothing
}
char *uiEntryText(uiEntry *e)
{
2016-04-22 19:04:30 -05:00
return uiWindowsWindowText(e->hwnd);
}
void uiEntrySetText(uiEntry *e, const char *text)
{
// doing this raises an EN_CHANGED
e->inhibitChanged = TRUE;
2016-04-22 19:04:30 -05:00
uiWindowsSetWindowText(e->hwnd, text);
e->inhibitChanged = FALSE;
// don't queue the control for resize; entry sizes are independent of their contents
}
void uiEntryOnChanged(uiEntry *e, void (*f)(uiEntry *, void *), void *data)
{
e->onChanged = f;
e->onChangedData = data;
}
int uiEntryReadOnly(uiEntry *e)
{
return (getStyle(e->hwnd) & ES_READONLY) != 0;
}
void uiEntrySetReadOnly(uiEntry *e, int readonly)
{
WPARAM ro;
ro = (WPARAM) FALSE;
if (readonly)
ro = (WPARAM) TRUE;
if (SendMessage(e->hwnd, EM_SETREADONLY, ro, 0) == 0)
2016-04-22 19:14:12 -05:00
logLastError(L"error making uiEntry read-only");
}
uiEntry *uiNewEntry(void)
{
uiEntry *e;
e = (uiEntry *) uiNewControl(uiEntry);
2015-08-31 11:33:44 -05:00
e->hwnd = uiWindowsEnsureCreateControlHWND(WS_EX_CLIENTEDGE,
L"edit", L"",
ES_AUTOHSCROLL | ES_LEFT | ES_NOHIDESEL | WS_TABSTOP,
hInstance, NULL,
TRUE);
uiWindowsRegisterWM_COMMANDHandler(e->hwnd, onWM_COMMAND, uiControl(e));
2015-09-01 06:21:18 -05:00
uiEntryOnChanged(e, defaultOnChanged, NULL);
uiWindowsFinishNewControl(e, uiEntry);
return e;
}