Set up the framework for tracking scrolling.

This commit is contained in:
Pietro Gagliardi 2015-09-08 20:31:15 -04:00
parent afc2f25e85
commit 450dba41a1
3 changed files with 70 additions and 66 deletions

View File

@ -3,8 +3,15 @@
#define areaClass L"libui_uiAreaClass" #define areaClass L"libui_uiAreaClass"
static void doPaint(uiArea *a, uiAreaHandler *ah, HDC dc, RECT *client, RECT *clip) struct uiArea {
// uiWindowsControl c;
HWND hwnd;
uiAreaHandler *ah;
};
static void doPaint(uiArea *a, HDC dc, RECT *client, RECT *clip)
{ {
uiAreaHandler *ah = a->ah;
uiAreaDrawParams dp; uiAreaDrawParams dp;
dp.Context = newContext(dc); dp.Context = newContext(dc);
@ -29,51 +36,36 @@ static void doPaint(uiArea *a, uiAreaHandler *ah, HDC dc, RECT *client, RECT *cl
(*(ah->Draw))(ah, a, &dp); (*(ah->Draw))(ah, a, &dp);
} }
struct areainit {
uiArea *a;
uiAreaHandler *ah;
};
#define gwlpArea 0
#define gwlpAreaHandler (sizeof (uiArea *))
static LRESULT CALLBACK areaWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) static LRESULT CALLBACK areaWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{ {
uiArea *a; uiArea *a;
uiAreaHandler *ah;
struct areainit *ai;
CREATESTRUCTW *cs = (CREATESTRUCTW *) lParam; CREATESTRUCTW *cs = (CREATESTRUCTW *) lParam;
HDC dc; HDC dc;
PAINTSTRUCT ps; PAINTSTRUCT ps;
RECT client; RECT client;
a = (uiArea *) GetWindowLongPtrW(hwnd, gwlpArea); a = (uiArea *) GetWindowLongPtrW(hwnd, GWLP_USERDATA);
ah = (uiAreaHandler *) GetWindowLongPtrW(hwnd, gwlpAreaHandler); if (a == NULL) {
//TODO if (a == NULL) { if (uMsg == WM_CREATE)
if (ah == NULL) { SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LONG_PTR) (cs->lpCreateParams));
if (uMsg == WM_NCCREATE) {
ai = (struct areainit *) (cs->lpCreateParams);
SetWindowLongPtrW(hwnd, gwlpArea, (LONG_PTR) (ai->a));
SetWindowLongPtrW(hwnd, gwlpAreaHandler, (LONG_PTR) (ai->ah));
}
// fall through to DefWindowProcW() anyway // fall through to DefWindowProcW() anyway
return DefWindowProcW(hwnd, uMsg, wParam, lParam); return DefWindowProcW(hwnd, uMsg, wParam, lParam);
} }
switch (uMsg) { switch (uMsg) {
case WM_PAINT: case WM_PAINT:
dc = BeginPaint(hwnd, &ps); dc = BeginPaint(a->hwnd, &ps);
if (dc == NULL) if (dc == NULL)
logLastError("error beginning paint in areaWndProc"); logLastError("error beginning paint in areaWndProc");
if (GetClientRect(hwnd, &client) == 0) if (GetClientRect(a->hwnd, &client) == 0)
logLastError("error getting client rect in WM_PAINT in areaWndProc()"); logLastError("error getting client rect in WM_PAINT in areaWndProc()");
doPaint(a, ah, dc, &client, &(ps.rcPaint)); doPaint(a, dc, &client, &(ps.rcPaint));
EndPaint(hwnd, &ps); EndPaint(a->hwnd, &ps);
return 0; return 0;
case WM_PRINTCLIENT: case WM_PRINTCLIENT:
if (GetClientRect(hwnd, &client) == 0) if (GetClientRect(a->hwnd, &client) == 0)
logLastError("error getting client rect in WM_PRINTCLIENT in areaWndProc()"); logLastError("error getting client rect in WM_PRINTCLIENT in areaWndProc()");
doPaint(a, ah, (HDC) wParam, &client, &client); doPaint(a, (HDC) wParam, &client, &client);
return 0; return 0;
} }
return DefWindowProc(hwnd, uMsg, wParam, lParam); return DefWindowProc(hwnd, uMsg, wParam, lParam);
@ -91,7 +83,6 @@ ATOM registerAreaClass(void)
//TODO wc.hCursor = hDefaultCursor; //TODO wc.hCursor = hDefaultCursor;
wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1); wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
wc.style = CS_HREDRAW | CS_VREDRAW; wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbWndExtra = sizeof (uiArea *) + sizeof (uiAreaHandler *);
return RegisterClassW(&wc); return RegisterClassW(&wc);
} }
@ -103,13 +94,23 @@ void unregisterAreaClass(void)
HWND makeArea(DWORD exstyle, DWORD style, int x, int y, int cx, int cy, HWND parent, uiAreaHandler *ah) HWND makeArea(DWORD exstyle, DWORD style, int x, int y, int cx, int cy, HWND parent, uiAreaHandler *ah)
{ {
struct areainit ai; uiArea *a;
ai.a = NULL; // TODO
ai.ah = ah; a = malloc(sizeof (uiArea));
return CreateWindowExW(exstyle,
a->ah = ah;
a->hwnd = CreateWindowExW(exstyle,
areaClass, L"", areaClass, L"",
style | WS_HSCROLL | WS_VSCROLL, style | WS_HSCROLL | WS_VSCROLL,
x, y, cx, cy, x, y, cx, cy,
parent, NULL, hInstance, &ai); parent, NULL, hInstance, a);
return a->hwnd;
}
void areaUpdateScroll(HWND area)
{
// TODO
} }

View File

@ -14,3 +14,5 @@ extern HRESULT logHRESULT(const char *, HRESULT);
extern HRESULT logMemoryExhausted(const char *); extern HRESULT logMemoryExhausted(const char *);
extern uiDrawContext *newContext(HDC); extern uiDrawContext *newContext(HDC);
extern void areaUpdateScroll(HWND);

View File

@ -11,6 +11,8 @@ struct handler {
static HWND area; static HWND area;
static struct handler h; static struct handler h;
static HWND nhspinb;
static HWND nvspinb;
static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *p) static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *p)
{ {
@ -89,38 +91,33 @@ static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *p)
static uintmax_t handlerHScrollMax(uiAreaHandler *a, uiArea *area) static uintmax_t handlerHScrollMax(uiAreaHandler *a, uiArea *area)
{ {
return 0;//TODO return gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(nhspinb)); WCHAR c[50];
GetWindowTextW(nhspinb, c, 50);
return _wtoi(c);
} }
static uintmax_t handlerVScrollMax(uiAreaHandler *a, uiArea *area) static uintmax_t handlerVScrollMax(uiAreaHandler *a, uiArea *area)
{ {
return 0;//TODO return gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(nvspinb)); WCHAR c[50];
}
/* TODO GetWindowTextW(nvspinb, c, 50);
static void recalcScroll(GtkSpinButton *sb, gpointer data) return _wtoi(c);
{
areaUpdateScroll(area);
} }
static GtkWidget *makeSpinbox(int min)
{
GtkWidget *sb;
sb = gtk_spin_button_new_with_range(min, 100000, 1);
gtk_spin_button_set_digits(GTK_SPIN_BUTTON(sb), 0);
g_signal_connect(sb, "value-changed", G_CALLBACK(recalcScroll), NULL);
return sb;
}
*/
static void repos(HWND hwnd) static void repos(HWND hwnd)
{ {
RECT r; RECT r;
GetClientRect(hwnd, &r); GetClientRect(hwnd, &r);
SetWindowPos(area, NULL, r.left + 12, r.top + 12, SetWindowPos(nhspinb, NULL, r.left + 12, r.top + 12,
r.right - r.left - 24, r.bottom - r.top - 24, 120, 20,
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);
SetWindowPos(nvspinb, NULL, r.left + 12 + 120 + 6, r.top + 12,
120, 20,
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);
SetWindowPos(area, NULL, r.left + 12, r.top + 12 + 20 + 6,
r.right - r.left - 24, r.bottom - r.top - 24 - 20 - 6,
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER); SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);
} }
@ -129,6 +126,11 @@ static LRESULT CALLBACK wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lPar
WINDOWPOS *wp = (WINDOWPOS *) lParam; WINDOWPOS *wp = (WINDOWPOS *) lParam;
switch (uMsg) { switch (uMsg) {
case WM_COMMAND:
if (((HWND) lParam) == nhspinb || ((HWND) lParam) == nvspinb)
if (HIWORD(wParam) == EN_CHANGE)
areaUpdateScroll(area);
break;
case WM_WINDOWPOSCHANGED: case WM_WINDOWPOSCHANGED:
if ((wp->flags & SWP_NOSIZE) != 0) if ((wp->flags & SWP_NOSIZE) != 0)
break; break;
@ -176,23 +178,22 @@ int main(void)
mainwin, mainwin,
(uiAreaHandler *) (&h)); (uiAreaHandler *) (&h));
/* TODO nhspinb = CreateWindowExW(WS_EX_CLIENTEDGE,
gtk_grid_attach(GTK_GRID(grid), L"edit", L"0",
gtk_label_new("H Count"), ES_NUMBER | WS_CHILD | WS_VISIBLE,
0, 0, 1, 1); 0, 0, 100, 100,
nhspinb = makeSpinbox(0); mainwin, NULL, hInstance, NULL);
gtk_grid_attach(GTK_GRID(grid), nhspinb,
1, 0, 1, 1);
gtk_grid_attach(GTK_GRID(grid), nvspinb = CreateWindowExW(WS_EX_CLIENTEDGE,
gtk_label_new("V Count"), L"edit", L"0",
0, 1, 1, 1); ES_NUMBER | WS_CHILD | WS_VISIBLE,
nvspinb = makeSpinbox(0); 0, 0, 100, 100,
gtk_grid_attach(GTK_GRID(grid), nvspinb, mainwin, NULL, hInstance, NULL);
1, 1, 1, 1);
*/
// initial state
repos(mainwin); repos(mainwin);
areaUpdateScroll(area);
ShowWindow(mainwin, SW_SHOWDEFAULT); ShowWindow(mainwin, SW_SHOWDEFAULT);
UpdateWindow(mainwin); UpdateWindow(mainwin);