2014-12-17 20:56:37 -06:00
|
|
|
// 16 august 2014
|
|
|
|
|
2014-12-18 18:24:32 -06:00
|
|
|
enum {
|
2014-12-22 20:16:42 -06:00
|
|
|
checkboxStateChecked = 1 << 0,
|
|
|
|
checkboxStateHot = 1 << 1,
|
|
|
|
checkboxStatePushed = 1 << 2,
|
|
|
|
checkboxnStates = 1 << 3,
|
2014-12-18 18:24:32 -06:00
|
|
|
};
|
2014-12-18 12:05:20 -06:00
|
|
|
|
2014-12-17 20:56:37 -06:00
|
|
|
// TODO actually make this
|
|
|
|
#define panichresult(a, b) panic(a)
|
|
|
|
|
|
|
|
static UINT dfcState(int cbstate)
|
|
|
|
{
|
|
|
|
UINT ret;
|
|
|
|
|
|
|
|
ret = DFCS_BUTTONCHECK;
|
|
|
|
if ((cbstate & checkboxStateChecked) != 0)
|
|
|
|
ret |= DFCS_CHECKED;
|
|
|
|
if ((cbstate & checkboxStateHot) != 0)
|
|
|
|
ret |= DFCS_HOT;
|
|
|
|
if ((cbstate & checkboxStatePushed) != 0)
|
|
|
|
ret |= DFCS_PUSHED;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-18 18:24:32 -06:00
|
|
|
static void drawFrameControlCheckbox(HDC dc, RECT *r, int cbState)
|
2014-12-17 20:56:37 -06:00
|
|
|
{
|
|
|
|
if (DrawFrameControl(dc, r, DFC_BUTTON, dfcState(cbState)) == 0)
|
|
|
|
panic("error drawing Table checkbox image with DrawFrameControl()");
|
|
|
|
}
|
|
|
|
|
2014-12-18 23:49:27 -06:00
|
|
|
static void getFrameControlCheckboxSize(HDC dc, int *width, int *height)
|
2014-12-17 20:56:37 -06:00
|
|
|
{
|
|
|
|
// there's no real metric around
|
|
|
|
// let's use SM_CX/YSMICON and hope for the best
|
|
|
|
*width = GetSystemMetrics(SM_CXSMICON);
|
|
|
|
*height = GetSystemMetrics(SM_CYSMICON);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int themestates[checkboxnStates] = {
|
|
|
|
CBS_UNCHECKEDNORMAL, // 0
|
|
|
|
CBS_CHECKEDNORMAL, // checked
|
|
|
|
CBS_UNCHECKEDHOT, // hot
|
|
|
|
CBS_CHECKEDHOT, // checked | hot
|
|
|
|
CBS_UNCHECKEDPRESSED, // pushed
|
|
|
|
CBS_CHECKEDPRESSED, // checked | pushed
|
|
|
|
CBS_UNCHECKEDPRESSED, // hot | pushed
|
|
|
|
CBS_CHECKEDPRESSED, // checked | hot | pushed
|
|
|
|
};
|
|
|
|
|
|
|
|
static SIZE getStateSize(HDC dc, int cbState, HTHEME theme)
|
|
|
|
{
|
|
|
|
SIZE s;
|
|
|
|
HRESULT res;
|
|
|
|
|
|
|
|
res = GetThemePartSize(theme, dc, BP_CHECKBOX, themestates[cbState], NULL, TS_DRAW, &s);
|
|
|
|
if (res != S_OK)
|
|
|
|
panichresult("error getting theme part size for Table checkboxes", res);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-12-18 18:24:32 -06:00
|
|
|
static void drawThemeCheckbox(HDC dc, RECT *r, int cbState, HTHEME theme)
|
2014-12-17 20:56:37 -06:00
|
|
|
{
|
|
|
|
HRESULT res;
|
|
|
|
|
|
|
|
res = DrawThemeBackground(theme, dc, BP_CHECKBOX, themestates[cbState], r, NULL);
|
|
|
|
if (res != S_OK)
|
|
|
|
panichresult("error drawing Table checkbox image from theme", res);
|
|
|
|
}
|
|
|
|
|
2014-12-18 18:24:32 -06:00
|
|
|
static void getThemeCheckboxSize(HDC dc, int *width, int *height, HTHEME theme)
|
2014-12-17 20:56:37 -06:00
|
|
|
{
|
|
|
|
SIZE size;
|
|
|
|
int cbState;
|
|
|
|
|
|
|
|
size = getStateSize(dc, 0, theme);
|
|
|
|
for (cbState = 1; cbState < checkboxnStates; cbState++) {
|
|
|
|
SIZE against;
|
|
|
|
|
|
|
|
against = getStateSize(dc, cbState, theme);
|
|
|
|
if (size.cx != against.cx || size.cy != against.cy)
|
|
|
|
// TODO make this use a no-information (or two ints) panic()
|
|
|
|
panic("size mismatch in Table checkbox states");
|
|
|
|
}
|
|
|
|
*width = (int) size.cx;
|
|
|
|
*height = (int) size.cy;
|
|
|
|
}
|
|
|
|
|
2014-12-22 20:28:17 -06:00
|
|
|
static void drawCheckbox(struct table *t, HDC dc, RECT *r, int cbState)
|
2014-12-17 20:56:37 -06:00
|
|
|
{
|
2014-12-18 18:24:32 -06:00
|
|
|
if (t->theme != NULL) {
|
2014-12-22 20:28:17 -06:00
|
|
|
drawThemeCheckbox(dc, r, cbState, t->theme);
|
2014-12-18 18:24:32 -06:00
|
|
|
return;
|
|
|
|
}
|
2014-12-22 20:28:17 -06:00
|
|
|
drawFrameControlCheckbox(dc, r, cbState);
|
2014-12-17 20:56:37 -06:00
|
|
|
}
|
|
|
|
|
2014-12-18 18:24:32 -06:00
|
|
|
static void freeCheckboxThemeData(struct table *t)
|
2014-12-17 20:56:37 -06:00
|
|
|
{
|
|
|
|
if (t->theme != NULL) {
|
|
|
|
HRESULT res;
|
|
|
|
|
|
|
|
res = CloseThemeData(t->theme);
|
|
|
|
if (res != S_OK)
|
2014-12-18 18:24:32 -06:00
|
|
|
panichresult("error closing Table checkbox theme", res);
|
2014-12-17 20:56:37 -06:00
|
|
|
t->theme = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 18:24:32 -06:00
|
|
|
static void loadCheckboxThemeData(struct table *t)
|
2014-12-17 20:56:37 -06:00
|
|
|
{
|
2014-12-18 18:24:32 -06:00
|
|
|
HDC dc;
|
2014-12-17 20:56:37 -06:00
|
|
|
|
2014-12-18 18:24:32 -06:00
|
|
|
freeCheckboxThemeData(t);
|
|
|
|
dc = GetDC(t->hwnd);
|
|
|
|
if (dc == NULL)
|
|
|
|
panic("error getting Table DC for loading checkbox theme data");
|
|
|
|
// ignore error; if it can't be done, we can fall back to DrawFrameControl()
|
|
|
|
if (t->theme == NULL) // try to open the theme
|
|
|
|
t->theme = OpenThemeData(t->hwnd, L"button");
|
|
|
|
if (t->theme != NULL) // use the theme
|
|
|
|
getThemeCheckboxSize(dc, &(t->checkboxWidth), &(t->checkboxHeight), t->theme);
|
|
|
|
else // couldn't open; fall back
|
2014-12-18 23:49:27 -06:00
|
|
|
getFrameControlCheckboxSize(dc, &(t->checkboxWidth), &(t->checkboxHeight));
|
2014-12-18 18:24:32 -06:00
|
|
|
if (ReleaseDC(t->hwnd, dc) == 0)
|
|
|
|
panic("error releasing Table DC for loading checkbox theme data");
|
2014-12-17 20:56:37 -06:00
|
|
|
}
|
2014-12-21 17:45:37 -06:00
|
|
|
|
2014-12-21 23:42:24 -06:00
|
|
|
static void redrawCheckboxRect(struct table *t, LPARAM lParam)
|
2014-12-21 17:45:37 -06:00
|
|
|
{
|
|
|
|
struct rowcol rc;
|
|
|
|
RECT r;
|
2014-12-21 23:42:24 -06:00
|
|
|
POINT pt;
|
2014-12-21 17:45:37 -06:00
|
|
|
|
2014-12-21 23:42:24 -06:00
|
|
|
rc = lParamToRowColumn(t, lParam);
|
|
|
|
if (rc.row == -1 && rc.column == -1)
|
|
|
|
return;
|
|
|
|
if (t->columnTypes[rc.column] != tableColumnCheckbox)
|
|
|
|
return;
|
|
|
|
if (!rowColumnToClientRect(t, rc, &r))
|
|
|
|
return;
|
|
|
|
// TODO only the checkbox rect?
|
|
|
|
if (InvalidateRect(t->hwnd, &r, TRUE) == 0)
|
|
|
|
panic("error redrawing Table checkbox rect for mouse events");
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLER(checkboxMouseMoveHandler)
|
|
|
|
{
|
|
|
|
// don't actually check to see if the mouse is in the checkbox rect
|
|
|
|
// if there's scrolling without mouse movement, that will change
|
|
|
|
// instead, drawCell() will handle it
|
2014-12-21 17:45:37 -06:00
|
|
|
if (!t->checkboxMouseOverLast) {
|
|
|
|
t->checkboxMouseOverLast = TRUE;
|
|
|
|
retrack(t);
|
2014-12-21 23:42:24 -06:00
|
|
|
} else
|
|
|
|
redrawCheckboxRect(t, t->checkboxMouseOverLastPoint);
|
2014-12-21 17:45:37 -06:00
|
|
|
t->checkboxMouseOverLastPoint = lParam;
|
2014-12-21 23:42:24 -06:00
|
|
|
redrawCheckboxRect(t, t->checkboxMouseOverLastPoint);
|
2014-12-21 17:45:37 -06:00
|
|
|
*lResult = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLER(checkboxMouseLeaveHandler)
|
|
|
|
{
|
2014-12-21 23:42:24 -06:00
|
|
|
if (t->checkboxMouseOverLast)
|
|
|
|
redrawCheckboxRect(t, t->checkboxMouseOverLastPoint);
|
2014-12-21 17:45:37 -06:00
|
|
|
// TODO remember what I wanted to do here in the case of a held mouse button
|
|
|
|
t->checkboxMouseOverLast = FALSE;
|
|
|
|
*lResult = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2014-12-22 19:15:10 -06:00
|
|
|
|
|
|
|
HANDLER(checkboxMouseDownHandler)
|
|
|
|
{
|
|
|
|
struct rowcol rc;
|
|
|
|
RECT r;
|
|
|
|
POINT pt;
|
|
|
|
|
|
|
|
rc = lParamToRowColumn(t, lParam);
|
|
|
|
if (rc.row == -1 || rc.column == -1)
|
|
|
|
return FALSE;
|
|
|
|
if (t->columnTypes[rc.column] != tableColumnCheckbox)
|
|
|
|
return FALSE;
|
|
|
|
if (!rowColumnToClientRect(t, rc, &r))
|
|
|
|
return FALSE;
|
|
|
|
toCheckboxRect(t, &r, 0);
|
|
|
|
pt.x = GET_X_LPARAM(lParam);
|
|
|
|
pt.y = GET_Y_LPARAM(lParam);
|
|
|
|
if (PtInRect(&r, pt) == 0)
|
|
|
|
return FALSE;
|
|
|
|
t->checkboxMouseDown = TRUE;
|
|
|
|
t->checkboxMouseDownRow = rc.row;
|
|
|
|
t->checkboxMouseDownColumn = rc.column;
|
|
|
|
// TODO redraw the whole cell?
|
|
|
|
if (InvalidateRect(t->hwnd, &r, TRUE) == 0)
|
|
|
|
panic("error redrawing Table checkbox after mouse down");
|
|
|
|
*lResult = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLER(checkboxMouseUpHandler)
|
|
|
|
{
|
|
|
|
struct rowcol rc;
|
|
|
|
RECT r;
|
|
|
|
POINT pt;
|
|
|
|
|
|
|
|
if (!t->checkboxMouseDown)
|
|
|
|
return FALSE;
|
|
|
|
// the logic behind goto wrongUp is that the mouse must be released on the same checkbox
|
|
|
|
rc = lParamToRowColumn(t, lParam);
|
|
|
|
if (rc.row == -1 || rc.column == -1)
|
|
|
|
goto wrongUp;
|
2014-12-22 20:16:42 -06:00
|
|
|
if (rc.row != t->checkboxMouseDownRow || rc.column != t->checkboxMouseDownColumn)
|
2014-12-22 19:15:10 -06:00
|
|
|
goto wrongUp;
|
|
|
|
if (t->columnTypes[rc.column] != tableColumnCheckbox)
|
|
|
|
goto wrongUp;
|
|
|
|
if (!rowColumnToClientRect(t, rc, &r))
|
|
|
|
goto wrongUp;
|
|
|
|
toCheckboxRect(t, &r, 0);
|
|
|
|
pt.x = GET_X_LPARAM(lParam);
|
|
|
|
pt.y = GET_Y_LPARAM(lParam);
|
|
|
|
if (PtInRect(&r, pt) == 0)
|
|
|
|
goto wrongUp;
|
2015-01-07 20:34:12 -06:00
|
|
|
notify(t, tableNotificationCellCheckboxToggled, rc.row, rc.column, 0);
|
2014-12-22 19:15:10 -06:00
|
|
|
t->checkboxMouseDown = FALSE;
|
|
|
|
// TODO redraw the whole cell?
|
|
|
|
if (InvalidateRect(t->hwnd, &r, TRUE) == 0)
|
|
|
|
panic("error redrawing Table checkbox after mouse up");
|
|
|
|
*lResult = 0;
|
|
|
|
return TRUE;
|
|
|
|
wrongUp:
|
2014-12-22 20:16:42 -06:00
|
|
|
if (t->checkboxMouseDown) {
|
|
|
|
rc.row = t->checkboxMouseDownRow;
|
|
|
|
rc.column = t->checkboxMouseDownColumn;
|
|
|
|
if (rowColumnToClientRect(t, rc, &r))
|
|
|
|
// TODO only the checkbox rect?
|
|
|
|
if (InvalidateRect(t->hwnd, &r, TRUE) == 0)
|
|
|
|
panic("error redrawing Table checkbox rect for aborted mouse up event");
|
|
|
|
}
|
|
|
|
// if we landed on another checkbox, be sure to draw that one too
|
|
|
|
if (t->checkboxMouseOverLast)
|
|
|
|
redrawCheckboxRect(t, t->checkboxMouseOverLastPoint);
|
2014-12-22 19:15:10 -06:00
|
|
|
t->checkboxMouseDown = FALSE;
|
|
|
|
return FALSE; // TODO really?
|
|
|
|
}
|