From 526fc352816f21f1cbfa8110262a58224c14d208 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Tue, 23 Nov 2021 02:21:38 +0200 Subject: [PATCH] draw window frame --- example.c | 4 +++- src/window.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/windtk.c | 35 ++++++++++++++++++++++------------- src/windtk.h | 2 +- src/wtimpl.h | 5 ++++- 5 files changed, 82 insertions(+), 16 deletions(-) diff --git a/example.c b/example.c index 8e7c8e7..4ff5bdc 100644 --- a/example.c +++ b/example.c @@ -36,10 +36,12 @@ int main(int argc, char **argv) glutMotionFunc(motion); glutPassiveMotionFunc(motion); + glClearColor(0.6, 0.6, 0.6, 1); + if(wt_init(800, 600, &gfx)) { return 1; } - wt_window(0, "foo", WT_WS_DEFAULT, 10, 10, 200, 200); + wt_window(0, "foo", WT_WS_DEFAULT, 100, 100, 200, 200); glutMainLoop(); return 0; diff --git a/src/window.c b/src/window.c index d0d8d74..cd39284 100644 --- a/src/window.c +++ b/src/window.c @@ -20,10 +20,62 @@ wt_widget *wt_window(wt_widget *par, const char *title, int style, int x, int y, return w; } +#define FRMTHICK 1 +#define FRMBEVEL 1 +#define FRMOFFS (FRMTHICK + FRMBEVEL * 2) +#define TBARTHICK 12 /* TODO: base it on font metrics */ +#define TBAROFFS (TBARTHICK + FRMBEVEL * 2) + static void draw_win(wt_widget *w, struct wt_graphics *gfx) { + struct wt_rect fr, tmprect; + int frmcol = COL_FFRM;//wt->focuswin == w ? COL_FFRM : COL_FRM; + wt_gfx_color(COL_BG); wt_gfx_fillrect(&w->rect); + + fr.x = w->rect.x - FRMOFFS; + fr.y = w->rect.y - TBAROFFS - FRMOFFS; + fr.width = w->rect.width + FRMOFFS * 2; + fr.height = w->rect.height + TBAROFFS + FRMOFFS * 2; + + wt_gfx_frame(&fr, FRM_OUT | FRM_NOFILL, frmcol); /* outer bevel */ + + /* draw main frame */ + wt_gfx_color(frmcol); + + fr.x += FRMBEVEL; + fr.y += FRMBEVEL; + fr.width -= FRMBEVEL * 2; + fr.height -= FRMBEVEL * 2; + + tmprect = fr; + tmprect.width = FRMTHICK; + wt_gfx_fillrect(&tmprect); /* left bar */ + tmprect.x += fr.width - FRMTHICK; + wt_gfx_fillrect(&tmprect); /* right bar */ + + tmprect = fr; + tmprect.height = FRMTHICK; + tmprect.x += FRMTHICK; + tmprect.width -= FRMTHICK * 2; + wt_gfx_fillrect(&tmprect); /* top bar */ + tmprect.y += fr.height - FRMTHICK; + wt_gfx_fillrect(&tmprect); /* bottom bar */ + + /* inner bevel */ + fr.x += FRMTHICK; + fr.y += FRMTHICK; + fr.width -= FRMTHICK * 2; + fr.height -= FRMTHICK * 2; + wt_gfx_frame(&fr, FRM_IN | FRM_NOFILL, frmcol); + + /* titlebar */ + fr.x = w->rect.x; + fr.width = w->rect.width; + fr.y += FRMBEVEL; + fr.height = TBARTHICK + FRMBEVEL * 2; + wt_gfx_frame(&fr, FRM_OUT, frmcol); } static void use_theme(wt_widget *w, struct wt_theme *theme) diff --git a/src/windtk.c b/src/windtk.c index d6b4d64..c34dc42 100644 --- a/src/windtk.c +++ b/src/windtk.c @@ -11,9 +11,9 @@ struct wt_context *wt_curctx_ = &defctx; static unsigned int def_colors[] = { 0xf0f0f0, /* font/foreground */ - 0x333333, /* background */ - 0x444444, /* highlight */ - 0x222222, /* shadow */ + 0x444444, /* background */ + 0x555555, /* highlight */ + 0x333333, /* shadow */ 0x403065, /* inactive frame */ 0x54349c, /* inactive frame highlight */ 0x221e2c, /* inactive frame shadow */ @@ -87,7 +87,7 @@ void wt_use_theme(struct wt_theme *theme) void wt_viewport(int x, int y, int w, int h) { - wt_setrect(&wt->vp, x, y, w, h); + wt_rect(&wt->vp, x, y, w, h); wt_move(wt->root, x, y); wt_resize(wt->root, w, h); } @@ -147,16 +147,25 @@ void wt_gfx_fillrect(struct wt_rect *r) wt->gfx.fillrect(r); } +void wt_gfx_fillrect4i(int x, int y, int w, int h) +{ + struct wt_rect r; + wt_rect(&r, x, y, w, h); + wt->gfx.fillrect(&r); +} + void wt_gfx_frame(struct wt_rect *r, int style, int basecol) { - wt_gfx_color(basecol); - wt_gfx_fillrect(r); - wt_gfx_color(style == FRM_OUT ? basecol + 2 : basecol + 1); - wt_gfx_line(r->x, r->y + r->height, r->x + r->width - 1, r->y + r->height); - wt_gfx_line(r->x + r->width, r->y + 1, r->x + r->width, r->y + r->height); - wt_gfx_color(style == FRM_OUT ? basecol + 1 : basecol + 2); - wt_gfx_line(r->x, r->y, r->x + r->width, r->y); - wt_gfx_line(r->x, r->y + 1, r->x, r->y + r->height - 1); + if((style & FRM_NOFILL) == 0) { + wt_gfx_color(basecol); + wt_gfx_fillrect(r); + } + wt_gfx_color(FRMSTYLE(style) == FRM_OUT ? basecol + 2 : basecol + 1); + wt_gfx_fillrect4i(r->x + 1, r->y + r->height - 1, r->width - 2, 1); + wt_gfx_fillrect4i(r->x + r->width - 1, r->y, 1, r->height); + wt_gfx_color(FRMSTYLE(style) == FRM_OUT ? basecol + 1 : basecol + 2); + wt_gfx_fillrect4i(r->x + 1, r->y, r->width - 2, 1); + wt_gfx_fillrect4i(r->x, r->y, 1, r->height); } void wt_gfx_line(int x0, int y0, int x1, int y1) @@ -164,7 +173,7 @@ void wt_gfx_line(int x0, int y0, int x1, int y1) wt->gfx.line(x0, y0, x1, y1); } -void wt_setrect(struct wt_rect *r, int x, int y, int w, int h) +void wt_rect(struct wt_rect *r, int x, int y, int w, int h) { r->x = x; r->y = y; diff --git a/src/windtk.h b/src/windtk.h index 56535c9..0435e6f 100644 --- a/src/windtk.h +++ b/src/windtk.h @@ -153,6 +153,6 @@ int wt_isenabled(wt_widget *w); void wt_callback(wt_widget *w, int type, wt_callback_func func, void *cls); -void wt_setrect(struct wt_rect *r, int x, int y, int w, int h); +void wt_rect(struct wt_rect *r, int x, int y, int w, int h); #endif /* WINDTK_H_ */ diff --git a/src/wtimpl.h b/src/wtimpl.h index f4956ac..76e51f1 100644 --- a/src/wtimpl.h +++ b/src/wtimpl.h @@ -20,7 +20,8 @@ enum { NUM_COLORS }; -enum { FRM_OUT, FRM_IN }; +enum { FRM_OUT, FRM_IN, FRM_NOFILL = 0x8000 }; +#define FRMSTYLE(x) ((x) & 0xff) struct wt_context { struct wt_graphics gfx; @@ -29,6 +30,8 @@ struct wt_context { struct wt_theme *theme; int colors[NUM_COLORS]; + + wt_widget *focuswin; }; extern struct wt_context *wt_curctx_; -- 1.7.10.4