More image list refinements.

This commit is contained in:
Pietro Gagliardi 2014-10-21 14:39:46 -04:00
parent 41d143d509
commit d18fc4296d
1 changed files with 20 additions and 12 deletions

View File

@ -44,6 +44,7 @@ struct table {
int headerHeight; int headerHeight;
intptr_t nColumns; intptr_t nColumns;
HIMAGELIST imagelist; HIMAGELIST imagelist;
int imagelistHeight;
}; };
static LONG rowHeight(struct table *t) static LONG rowHeight(struct table *t)
@ -51,6 +52,7 @@ static LONG rowHeight(struct table *t)
HFONT thisfont, prevfont; HFONT thisfont, prevfont;
TEXTMETRICW tm; TEXTMETRICW tm;
HDC dc; HDC dc;
LONG ret;
dc = GetDC(t->hwnd); dc = GetDC(t->hwnd);
if (dc == NULL) if (dc == NULL)
@ -65,7 +67,10 @@ static LONG rowHeight(struct table *t)
abort(); abort();
if (ReleaseDC(t->hwnd, dc) == 0) if (ReleaseDC(t->hwnd, dc) == 0)
abort(); abort();
return tm.tmHeight; ret = tm.tmHeight;
if (ret < t->imagelistHeight)
ret = t->imagelistHeight;
return ret;
} }
static void redrawAll(struct table *t) static void redrawAll(struct table *t)
@ -307,7 +312,7 @@ static void resize(struct table *t)
static void drawItems(struct table *t, HDC dc, RECT cliprect) static void drawItems(struct table *t, HDC dc, RECT cliprect)
{ {
HFONT thisfont, prevfont; HFONT thisfont, prevfont;
TEXTMETRICW tm; LONG height;
LONG y; LONG y;
intptr_t i; intptr_t i;
RECT controlSize; // for filling the entire selected row RECT controlSize; // for filling the entire selected row
@ -317,33 +322,33 @@ static void drawItems(struct table *t, HDC dc, RECT cliprect)
if (GetClientRect(t->hwnd, &controlSize) == 0) if (GetClientRect(t->hwnd, &controlSize) == 0)
abort(); abort();
height = rowHeight(t);
thisfont = t->font; // in case WM_SETFONT happens before we return thisfont = t->font; // in case WM_SETFONT happens before we return
prevfont = (HFONT) SelectObject(dc, thisfont); prevfont = (HFONT) SelectObject(dc, thisfont);
if (prevfont == NULL) if (prevfont == NULL)
abort(); abort();
if (GetTextMetricsW(dc, &tm) == 0)
abort();
// adjust the clip rect and the window so that (0, 0) is always the first item // adjust the clip rect and the window so that (0, 0) is always the first item
// adjust the viewport so that everything is shifted down t->headerHeight pixels // adjust the viewport so that everything is shifted down t->headerHeight pixels
if (OffsetRect(&cliprect, 0, t->firstVisible * tm.tmHeight) == 0) if (OffsetRect(&cliprect, 0, t->firstVisible * height) == 0)
abort(); abort();
if (GetWindowOrgEx(dc, &prevOrigin) == 0) if (GetWindowOrgEx(dc, &prevOrigin) == 0)
abort(); abort();
if (SetWindowOrgEx(dc, prevOrigin.x, prevOrigin.y + (t->firstVisible * tm.tmHeight), NULL) == 0) if (SetWindowOrgEx(dc, prevOrigin.x, prevOrigin.y + (t->firstVisible * height), NULL) == 0)
abort(); abort();
if (SetViewportOrgEx(dc, 0, t->headerHeight, &prevViewportOrigin) == 0) if (SetViewportOrgEx(dc, 0, t->headerHeight, &prevViewportOrigin) == 0)
abort(); abort();
// 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 // 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
first = cliprect.top / tm.tmHeight; first = cliprect.top / height;
if (first < 0) if (first < 0)
first = 0; first = 0;
last = (cliprect.bottom + tm.tmHeight - 1) / tm.tmHeight; last = (cliprect.bottom + height - 1) / height;
if (last >= t->count) if (last >= t->count)
last = t->count; last = t->count;
y = first * tm.tmHeight; y = first * height;
for (i = first; i < last; i++) { for (i = first; i < last; i++) {
RECT rsel; RECT rsel;
HBRUSH background; HBRUSH background;
@ -371,7 +376,7 @@ static void drawItems(struct table *t, HDC dc, RECT cliprect)
rsel.left = controlSize.left; rsel.left = controlSize.left;
rsel.top = y; rsel.top = y;
rsel.right = controlSize.right - controlSize.left; rsel.right = controlSize.right - controlSize.left;
rsel.bottom = y + tm.tmHeight; rsel.bottom = y + height;
if (FillRect(dc, &rsel, background) == 0) if (FillRect(dc, &rsel, background) == 0)
abort(); abort();
@ -410,11 +415,12 @@ static void drawItems(struct table *t, HDC dc, RECT cliprect)
rsel.left = headeritem.left + xoff; rsel.left = headeritem.left + xoff;
rsel.top = y; rsel.top = y;
rsel.right = headeritem.right; rsel.right = headeritem.right;
rsel.bottom = y + tm.tmHeight; 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) if (DrawTextExW(dc, msg, wsprintf(msg, L"Item %d", i), &rsel, DT_END_ELLIPSIS | DT_LEFT | DT_NOPREFIX | DT_SINGLELINE, NULL) == 0)
abort(); abort();
} }
y += tm.tmHeight; y += height;
} }
// reset everything // reset everything
@ -480,9 +486,11 @@ t->imagelist = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(
if(t->imagelist==NULL)abort(); if(t->imagelist==NULL)abort();
{ {
HICON icon; HICON icon;
int unused;
icon = LoadIconW(NULL, IDI_ERROR); icon = LoadIconW(NULL, IDI_ERROR);
if(icon == NULL)abort(); if(icon == NULL)abort();
if (ImageList_AddIcon(t->imagelist, icon) == -1)abort(); if (ImageList_AddIcon(t->imagelist, icon) == -1)abort();
if (ImageList_GetIconSize(t->imagelist, &unused, &(t->imagelistHeight)) == 0)abort();
} }
} }
SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LONG_PTR) t); SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LONG_PTR) t);