More Windows Table separation.

This commit is contained in:
Pietro Gagliardi 2014-11-30 17:05:42 -05:00
parent d23cdd7682
commit c67be58377
3 changed files with 252 additions and 246 deletions

166
wintable/draw.h Normal file
View File

@ -0,0 +1,166 @@
// 30 november 2014
static void resize(struct table *t)
{
RECT r;
SCROLLINFO si;
// do this first so our scrollbar calculations can be correct
repositionHeader(t);
// now adjust the scrollbars
r = realClientRect(t);
t->pagesize = (r.bottom - r.top) / rowHeight(t);
ZeroMemory(&si, sizeof (SCROLLINFO));
si.cbSize = sizeof (SCROLLINFO);
si.fMask = SIF_RANGE | SIF_PAGE;
si.nMin = 0;
si.nMax = t->count - 1;
si.nPage = t->pagesize;
SetScrollInfo(t->hwnd, SB_VERT, &si, TRUE);
recomputeHScroll(t);
}
// TODO alter this so that only the visible columns are redrawn
// TODO this means rename controlSize to clientRect
static void drawItem(struct table *t, HDC dc, intptr_t i, LONG y, LONG height, RECT controlSize)
{
RECT rsel;
HBRUSH background;
int textColor;
WCHAR msg[100];
RECT headeritem;
intptr_t j;
LRESULT xoff;
IMAGELISTDRAWPARAMS ip;
POINT pt;
// TODO verify these two
background = (HBRUSH) (COLOR_WINDOW + 1);
textColor = COLOR_WINDOWTEXT;
if (t->selected == i) {
// these are the colors wine uses (http://source.winehq.org/source/dlls/comctl32/listview.c)
// the two for unfocused are also suggested by http://stackoverflow.com/questions/10428710/windows-forms-inactive-highlight-color
background = (HBRUSH) (COLOR_HIGHLIGHT + 1);
textColor = COLOR_HIGHLIGHTTEXT;
if (GetFocus() != t->hwnd) {
background = (HBRUSH) (COLOR_BTNFACE + 1);
textColor = COLOR_BTNTEXT;
}
}
// first fill the selection rect
// note that this already only draws the visible area
rsel.left = controlSize.left;
rsel.top = y;
rsel.right = controlSize.right - controlSize.left;
rsel.bottom = y + height;
if (FillRect(dc, &rsel, background) == 0)
abort();
// TODO double-check to see if this takes any parameters
xoff = SendMessageW(t->header, HDM_GETBITMAPMARGIN, 0, 0);
// now adjust for horizontal scrolling
xoff -= t->hpos;
// now draw the cells
if (SetTextColor(dc, GetSysColor(textColor)) == CLR_INVALID)
abort();
if (SetBkMode(dc, TRANSPARENT) == 0)
abort();
for (j = 0; j < t->nColumns; j++) {
if (SendMessageW(t->header, HDM_GETITEMRECT, (WPARAM) j, (LPARAM) (&headeritem)) == 0)
abort();
switch (t->columnTypes[j]) {
case tableColumnText:
rsel.left = headeritem.left + xoff;
rsel.top = y;
rsel.right = headeritem.right;
rsel.bottom = y + height;
// TODO vertical center in case the height is less than the icon height?
if (DrawTextExW(dc, msg, wsprintf(msg, L"Item %d", i), &rsel, DT_END_ELLIPSIS | DT_LEFT | DT_NOPREFIX | DT_SINGLELINE, NULL) == 0)
abort();
break;
case tableColumnImage:
// TODO vertically center if image is smaller than text height
// TODO same for checkboxes
ZeroMemory(&ip, sizeof (IMAGELISTDRAWPARAMS));
ip.cbSize = sizeof (IMAGELISTDRAWPARAMS);
ip.himl = t->checkboxes;//t->imagelist;
ip.i = (i%8);//0;
ip.hdcDst = dc;
ip.x = headeritem.left + xoff;
ip.y = y;
ip.cx = 0; // draw whole image
ip.cy = 0;
ip.xBitmap = 0;
ip.yBitmap = 0;
ip.rgbBk = CLR_NONE;
ip.fStyle = ILD_NORMAL | ILD_SCALE; // TODO alpha-blend; ILD_DPISCALE?
// TODO ILS_ALPHA?
if (ImageList_DrawIndirect(&ip) == 0)
abort();
break;
case tableColumnCheckbox:
// TODO
break;
}
if (t->selected == i && t->focusedColumn == j) {
rsel.left = headeritem.left;
rsel.top = y;
rsel.right = headeritem.right;
rsel.bottom = y + height;
if (DrawFocusRect(dc, &rsel) == 0)
abort();
}
}
}
static void drawItems(struct table *t, HDC dc, RECT cliprect)
{
HFONT thisfont, prevfont;
LONG height;
LONG y;
intptr_t i;
RECT controlSize; // for filling the entire selected row
intptr_t first, last;
if (GetClientRect(t->hwnd, &controlSize) == 0)
abort();
height = rowHeight(t);
thisfont = t->font; // in case WM_SETFONT happens before we return
prevfont = (HFONT) SelectObject(dc, thisfont);
if (prevfont == NULL)
abort();
// ignore anything beneath the header
if (cliprect.top < t->headerHeight)
cliprect.top = t->headerHeight;
// now let's pretend the header isn't there
// we only need it in (or rather, before) the drawItem() calls below
cliprect.top -= t->headerHeight;
cliprect.bottom -= t->headerHeight;
// see http://blogs.msdn.com/b/oldnewthing/archive/2003/07/29/54591.aspx and http://blogs.msdn.com/b/oldnewthing/archive/2003/07/30/54600.aspx
// we need to add t->firstVisible here because cliprect is relative to the visible area
first = (cliprect.top / height) + t->firstVisible;
if (first < 0)
first = 0;
last = lastVisible(t, cliprect, height);
// now for the first y, discount firstVisible
y = (first - t->firstVisible) * height;
// and offset by the header height
y += t->headerHeight;
for (i = first; i < last; i++) {
drawItem(t, dc, i, y, height, controlSize);
y += height;
}
// reset everything
if (SelectObject(dc, prevfont) != (HGDIOBJ) (thisfont))
abort();
}

View File

@ -97,253 +97,10 @@ struct table {
#include "api.h"
#include "hscroll.h"
#include "vscroll.h"
#include "selection.h"
#include "draw.h"
static void finishSelect(struct table *t, intptr_t prev)
{
if (t->selected < 0)
t->selected = 0;
if (t->selected >= t->count)
t->selected = t->count - 1;
// always redraw the old and new rows to avoid artifacts when scrolling, even if they are the same (since the focused column may have changed)
redrawRow(t, prev);
if (prev != t->selected)
redrawRow(t, t->selected);
// if we need to scroll, the scrolling will force a redraw, so we don't have to worry about doing so ourselves
if (t->selected < t->firstVisible)
vscrollto(t, t->selected);
// note that this is not lastVisible(t) because the last visible row may only be partially visible and we want selections to make them fully visible
else if (t->selected >= (t->firstVisible + t->pagesize))
vscrollto(t, t->selected - t->pagesize + 1);
}
static void keySelect(struct table *t, WPARAM wParam, LPARAM lParam)
{
intptr_t prev;
// TODO figure out correct behavior with nothing selected
if (t->count == 0) // don't try to do anything if there's nothing to do
return;
prev = t->selected;
switch (wParam) {
case VK_UP:
t->selected--;
break;
case VK_DOWN:
t->selected++;
break;
case VK_PRIOR:
t->selected -= t->pagesize;
break;
case VK_NEXT:
t->selected += t->pagesize;
break;
case VK_HOME:
t->selected = 0;
break;
case VK_END:
t->selected = t->count - 1;
break;
case VK_LEFT:
t->focusedColumn--;
if (t->focusedColumn < 0)
if (t->nColumns == 0) // peg at -1
t->focusedColumn = -1;
else
t->focusedColumn = 0;
break;
case VK_RIGHT:
t->focusedColumn++;
if (t->focusedColumn >= t->nColumns)
if (t->nColumns == 0) // peg at -1
t->focusedColumn = -1;
else
t->focusedColumn = t->nColumns - 1;
break;
// TODO keyboard shortcuts for going to the first/last column?
default:
// don't touch anything
return;
}
finishSelect(t, prev);
}
// TODO rename
static void selectItem(struct table *t, WPARAM wParam, LPARAM lParam)
{
intptr_t prev;
prev = t->selected;
lParamToRowColumn(t, lParam, &(t->selected), &(t->focusedColumn));
finishSelect(t, prev);
}
static void resize(struct table *t)
{
RECT r;
SCROLLINFO si;
// do this first so our scrollbar calculations can be correct
repositionHeader(t);
// now adjust the scrollbars
r = realClientRect(t);
t->pagesize = (r.bottom - r.top) / rowHeight(t);
ZeroMemory(&si, sizeof (SCROLLINFO));
si.cbSize = sizeof (SCROLLINFO);
si.fMask = SIF_RANGE | SIF_PAGE;
si.nMin = 0;
si.nMax = t->count - 1;
si.nPage = t->pagesize;
SetScrollInfo(t->hwnd, SB_VERT, &si, TRUE);
recomputeHScroll(t);
}
// TODO alter this so that only the visible columns are redrawn
// TODO this means rename controlSize to clientRect
static void drawItem(struct table *t, HDC dc, intptr_t i, LONG y, LONG height, RECT controlSize)
{
RECT rsel;
HBRUSH background;
int textColor;
WCHAR msg[100];
RECT headeritem;
intptr_t j;
LRESULT xoff;
IMAGELISTDRAWPARAMS ip;
POINT pt;
// TODO verify these two
background = (HBRUSH) (COLOR_WINDOW + 1);
textColor = COLOR_WINDOWTEXT;
if (t->selected == i) {
// these are the colors wine uses (http://source.winehq.org/source/dlls/comctl32/listview.c)
// the two for unfocused are also suggested by http://stackoverflow.com/questions/10428710/windows-forms-inactive-highlight-color
background = (HBRUSH) (COLOR_HIGHLIGHT + 1);
textColor = COLOR_HIGHLIGHTTEXT;
if (GetFocus() != t->hwnd) {
background = (HBRUSH) (COLOR_BTNFACE + 1);
textColor = COLOR_BTNTEXT;
}
}
// first fill the selection rect
// note that this already only draws the visible area
rsel.left = controlSize.left;
rsel.top = y;
rsel.right = controlSize.right - controlSize.left;
rsel.bottom = y + height;
if (FillRect(dc, &rsel, background) == 0)
abort();
// TODO double-check to see if this takes any parameters
xoff = SendMessageW(t->header, HDM_GETBITMAPMARGIN, 0, 0);
// now adjust for horizontal scrolling
xoff -= t->hpos;
// now draw the cells
if (SetTextColor(dc, GetSysColor(textColor)) == CLR_INVALID)
abort();
if (SetBkMode(dc, TRANSPARENT) == 0)
abort();
for (j = 0; j < t->nColumns; j++) {
if (SendMessageW(t->header, HDM_GETITEMRECT, (WPARAM) j, (LPARAM) (&headeritem)) == 0)
abort();
switch (t->columnTypes[j]) {
case tableColumnText:
rsel.left = headeritem.left + xoff;
rsel.top = y;
rsel.right = headeritem.right;
rsel.bottom = y + height;
// TODO vertical center in case the height is less than the icon height?
if (DrawTextExW(dc, msg, wsprintf(msg, L"Item %d", i), &rsel, DT_END_ELLIPSIS | DT_LEFT | DT_NOPREFIX | DT_SINGLELINE, NULL) == 0)
abort();
break;
case tableColumnImage:
// TODO vertically center if image is smaller than text height
// TODO same for checkboxes
ZeroMemory(&ip, sizeof (IMAGELISTDRAWPARAMS));
ip.cbSize = sizeof (IMAGELISTDRAWPARAMS);
ip.himl = t->checkboxes;//t->imagelist;
ip.i = (i%8);//0;
ip.hdcDst = dc;
ip.x = headeritem.left + xoff;
ip.y = y;
ip.cx = 0; // draw whole image
ip.cy = 0;
ip.xBitmap = 0;
ip.yBitmap = 0;
ip.rgbBk = CLR_NONE;
ip.fStyle = ILD_NORMAL | ILD_SCALE; // TODO alpha-blend; ILD_DPISCALE?
// TODO ILS_ALPHA?
if (ImageList_DrawIndirect(&ip) == 0)
abort();
break;
case tableColumnCheckbox:
// TODO
break;
}
if (t->selected == i && t->focusedColumn == j) {
rsel.left = headeritem.left;
rsel.top = y;
rsel.right = headeritem.right;
rsel.bottom = y + height;
if (DrawFocusRect(dc, &rsel) == 0)
abort();
}
}
}
static void drawItems(struct table *t, HDC dc, RECT cliprect)
{
HFONT thisfont, prevfont;
LONG height;
LONG y;
intptr_t i;
RECT controlSize; // for filling the entire selected row
intptr_t first, last;
if (GetClientRect(t->hwnd, &controlSize) == 0)
abort();
height = rowHeight(t);
thisfont = t->font; // in case WM_SETFONT happens before we return
prevfont = (HFONT) SelectObject(dc, thisfont);
if (prevfont == NULL)
abort();
// ignore anything beneath the header
if (cliprect.top < t->headerHeight)
cliprect.top = t->headerHeight;
// now let's pretend the header isn't there
// we only need it in (or rather, before) the drawItem() calls below
cliprect.top -= t->headerHeight;
cliprect.bottom -= t->headerHeight;
// see http://blogs.msdn.com/b/oldnewthing/archive/2003/07/29/54591.aspx and http://blogs.msdn.com/b/oldnewthing/archive/2003/07/30/54600.aspx
// we need to add t->firstVisible here because cliprect is relative to the visible area
first = (cliprect.top / height) + t->firstVisible;
if (first < 0)
first = 0;
last = lastVisible(t, cliprect, height);
// now for the first y, discount firstVisible
y = (first - t->firstVisible) * height;
// and offset by the header height
y += t->headerHeight;
for (i = first; i < last; i++) {
drawItem(t, dc, i, y, height, controlSize);
y += height;
}
// reset everything
if (SelectObject(dc, prevfont) != (HGDIOBJ) (thisfont))
abort();
}
// TODO create a system where each of the above modules provide their own window procedures
static LRESULT CALLBACK tableWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
struct table *t;

83
wintable/selection.h Normal file
View File

@ -0,0 +1,83 @@
// 30 november 2014
static void finishSelect(struct table *t, intptr_t prev)
{
if (t->selected < 0)
t->selected = 0;
if (t->selected >= t->count)
t->selected = t->count - 1;
// always redraw the old and new rows to avoid artifacts when scrolling, even if they are the same (since the focused column may have changed)
redrawRow(t, prev);
if (prev != t->selected)
redrawRow(t, t->selected);
// if we need to scroll, the scrolling will force a redraw, so we don't have to worry about doing so ourselves
if (t->selected < t->firstVisible)
vscrollto(t, t->selected);
// note that this is not lastVisible(t) because the last visible row may only be partially visible and we want selections to make them fully visible
else if (t->selected >= (t->firstVisible + t->pagesize))
vscrollto(t, t->selected - t->pagesize + 1);
}
// TODO isolate functionality so other keyboard event handlers can run
static void keySelect(struct table *t, WPARAM wParam, LPARAM lParam)
{
intptr_t prev;
// TODO figure out correct behavior with nothing selected
if (t->count == 0) // don't try to do anything if there's nothing to do
return;
prev = t->selected;
switch (wParam) {
case VK_UP:
t->selected--;
break;
case VK_DOWN:
t->selected++;
break;
case VK_PRIOR:
t->selected -= t->pagesize;
break;
case VK_NEXT:
t->selected += t->pagesize;
break;
case VK_HOME:
t->selected = 0;
break;
case VK_END:
t->selected = t->count - 1;
break;
case VK_LEFT:
t->focusedColumn--;
if (t->focusedColumn < 0)
if (t->nColumns == 0) // peg at -1
t->focusedColumn = -1;
else
t->focusedColumn = 0;
break;
case VK_RIGHT:
t->focusedColumn++;
if (t->focusedColumn >= t->nColumns)
if (t->nColumns == 0) // peg at -1
t->focusedColumn = -1;
else
t->focusedColumn = t->nColumns - 1;
break;
// TODO keyboard shortcuts for going to the first/last column?
default:
// don't touch anything
return;
}
finishSelect(t, prev);
}
// TODO rename
static void selectItem(struct table *t, WPARAM wParam, LPARAM lParam)
{
intptr_t prev;
prev = t->selected;
lParamToRowColumn(t, lParam, &(t->selected), &(t->focusedColumn));
finishSelect(t, prev);
}