From a3ad384a340956cc7a4b7deb692c605858ce28e6 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 19 Oct 2014 22:20:53 -0400 Subject: [PATCH] Set the DC viewport properly for the purposes of scrolling. --- wintable/main.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/wintable/main.c b/wintable/main.c index b469ad6..5012efa 100644 --- a/wintable/main.c +++ b/wintable/main.c @@ -29,6 +29,7 @@ struct table { HFONT font; intptr_t selected; intptr_t count; + intptr_t firstVisible; }; static void drawItems(struct table *t, HDC dc, RECT cliprect) @@ -39,6 +40,7 @@ static void drawItems(struct table *t, HDC dc, RECT cliprect) intptr_t i; RECT r; intptr_t first, last; + POINT prevOrigin; // TODO eliminate the need (only use cliprect) if (GetClientRect(t->hwnd, &r) == 0) @@ -51,6 +53,14 @@ static void drawItems(struct table *t, HDC dc, RECT cliprect) if (GetTextMetricsW(dc, &tm) == 0) abort(); + // adjust the clip rect and the viewport so that (0, 0) is always the first item + if (OffsetRect(&cliprect, 0, t->firstVisible * tm.tmHeight) == 0) + abort(); + if (GetWindowOrgEx(dc, &prevOrigin) == 0) + abort(); + if (SetWindowOrgEx(dc, prevOrigin.x, prevOrigin.y + (t->firstVisible * tm.tmHeight), NULL) == 0) + 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 first = cliprect.top / tm.tmHeight; if (first < 0) @@ -82,6 +92,9 @@ static void drawItems(struct table *t, HDC dc, RECT cliprect) y += tm.tmHeight; } + // reset everything + if (SetWindowOrgEx(dc, prevOrigin.x, prevOrigin.y, NULL) == 0) + abort(); if (SelectObject(dc, prevfont) != (HGDIOBJ) (thisfont)) abort(); }