libui/windows/radiobuttons.cpp

156 lines
4.1 KiB
C++
Raw Normal View History

// 20 may 2015
#include "uipriv_windows.hpp"
2015-06-05 09:23:31 -05:00
// desired behavior:
2015-09-02 17:26:48 -05:00
// - tab moves between the radio buttons and the adjacent controls
2015-06-05 09:23:31 -05:00
// - arrow keys navigate between radio buttons
2015-09-02 17:26:48 -05:00
// - arrow keys do not leave the radio buttons (this is done in control.c)
// - arrow keys wrap around bare groups (if the previous control has WS_GROUP but the first radio button doesn't, then it doesn't; since our radio buttons are all in their own child window we can't do that)
2015-09-02 17:29:14 -05:00
// - clicking on a radio button draws a focus rect (TODO)
2015-06-05 09:23:31 -05:00
struct uiRadioButtons {
uiWindowsControl c;
HWND hwnd; // of the container
std::vector<HWND> *hwnds; // of the buttons
};
static void onDestroy(uiRadioButtons *);
2015-08-31 11:33:44 -05:00
uiWindowsDefineControlWithOnDestroy(
uiRadioButtons, // type name
onDestroy(me); // on destroy
2015-08-31 11:33:44 -05:00
)
2015-05-30 01:14:13 -05:00
static BOOL onWM_COMMAND(uiControl *c, HWND clicked, WORD code, LRESULT *lResult)
{
uiRadioButtons *r = uiRadioButtons(c);
WPARAM check;
uintmax_t i;
if (code != BN_CLICKED)
return FALSE;
for (const HWND &hwnd : *(r->hwnds)) {
check = BST_UNCHECKED;
if (clicked == hwnd)
check = BST_CHECKED;
SendMessage(hwnd, BM_SETCHECK, check, 0);
}
*lResult = 0;
return TRUE;
}
static void onDestroy(uiRadioButtons *r)
{
for (const HWND &hwnd : *(r->hwnds)) {
uiWindowsUnregisterWM_COMMANDHandler(hwnd);
uiWindowsEnsureDestroyWindow(hwnd);
}
delete r->hwnds;
}
// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
2015-05-21 16:26:25 -05:00
#define radiobuttonHeight 10
// from http://msdn.microsoft.com/en-us/library/windows/desktop/bb226818%28v=vs.85%29.aspx
2015-05-21 16:26:25 -05:00
#define radiobuttonXFromLeftOfBoxToLeftOfLabel 12
static void minimumSize(uiWindowsControl *c, uiWindowsSizing *d, intmax_t *width, intmax_t *height)
{
uiRadioButtons *r = uiRadioButtons(c);
intmax_t wid, maxwid;
maxwid = 0;
for (const HWND &hwnd : *(r->hwnds)) {
wid = uiWindowsWindowTextWidth(hwnd);
if (maxwid < wid)
maxwid = wid;
}
*width = uiWindowsDlgUnitsToX(radiobuttonXFromLeftOfBoxToLeftOfLabel, d->BaseX) + maxwid;
*height = uiWindowsDlgUnitsToY(radiobuttonHeight, d->BaseY) * r->hwnds->size();
}
static void radiobuttonsRelayout(uiWindowsControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height)
{
2015-09-01 15:10:29 -05:00
uiRadioButtons *r = uiRadioButtons(c);
uiWindowsSizing *d;
intmax_t height1;
intmax_t h;
2016-04-23 21:23:07 -05:00
uiWindowsEnsureMoveWindowDuringResize(r->hwnd, x, y, width, height);
2015-09-01 15:10:29 -05:00
x = 0;
y = 0;
d = uiWindowsNewSizing(r->hwnd);
height1 = uiWindowsDlgUnitsToY(radiobuttonHeight, d->BaseY);
uiWindowsFreeSizing(d);
for (const HWND &hwnd : *(r->hwnds)) {
h = height1;
if (h > height) // clip to height
h = height;
2016-04-23 21:23:07 -05:00
uiWindowsEnsureMoveWindowDuringResize(hwnd, x, y, width, h);
y += height1;
height -= height1;
if (height <= 0) // clip to height
break;
// TODO don't do the above to avoid overlap
// TODO in fact, only do this on add/remove/change labels/etc.
}
}
2015-09-01 15:10:29 -05:00
// TODO commit enable/disable
2015-09-02 17:26:48 -05:00
static void redoControlIDsZOrder(uiRadioButtons *r)
{
2015-09-02 17:26:48 -05:00
LONG_PTR controlID;
HWND insertAfter;
2015-09-02 17:26:48 -05:00
controlID = 100;
insertAfter = NULL;
for (const HWND &hwnd : *(r->hwnds)) {
2015-09-02 17:26:48 -05:00
uiWindowsEnsureAssignControlIDZOrder(hwnd, controlID, insertAfter);
controlID++;
insertAfter = hwnd;
}
2015-05-30 18:23:05 -05:00
}
void uiRadioButtonsAppend(uiRadioButtons *r, const char *text)
{
HWND hwnd;
WCHAR *wtext;
2015-09-02 17:26:48 -05:00
DWORD groupTabStop;
// the first radio button gets both WS_GROUP and WS_TABSTOP
// successive radio buttons get *neither*
groupTabStop = 0;
if (r->hwnds->size() == 0)
2015-09-02 17:26:48 -05:00
groupTabStop = WS_GROUP | WS_TABSTOP;
wtext = toUTF16(text);
2015-08-31 11:33:44 -05:00
hwnd = uiWindowsEnsureCreateControlHWND(0,
L"button", wtext,
2015-09-02 17:26:48 -05:00
BS_RADIOBUTTON | groupTabStop,
hInstance, NULL,
TRUE);
uiFree(wtext);
2015-09-01 15:10:29 -05:00
uiWindowsEnsureSetParent(hwnd, r->hwnd);
uiWindowsRegisterWM_COMMANDHandler(hwnd, onWM_COMMAND, uiControl(r));
r->hwnds->push_back(hwnd);
2015-09-02 17:26:48 -05:00
redoControlIDsZOrder(r);
2015-09-01 15:10:29 -05:00
uiWindowsControlQueueRelayout(uiWindowsControl(r));
}
uiRadioButtons *uiNewRadioButtons(void)
{
uiRadioButtons *r;
r = (uiRadioButtons *) uiNewControl(uiRadioButtonsType());
r->hwnd = newContainer();
r->hwnds = new std::vector<HWND>;
2015-08-31 11:33:44 -05:00
uiWindowsFinishNewControl(r, uiRadioButtons);
uiWindowsControl(r)->Relayout = radiobuttonsRelayout;
2015-08-31 11:33:44 -05:00
return r;
}