From cdb3de64338c58655e2989d1127bffd4bbdc178f Mon Sep 17 00:00:00 2001 From: Mike Lowis Date: Thu, 11 Jan 2024 16:27:46 -0500 Subject: [PATCH] added skeleton code for button and pane widgets --- bin/uitest/main.c | 117 ++++++++++++++++++++++++++++++++++++-------- inc/impl/libui.h | 10 +--- inc/libui.h | 47 +++++++----------- lib/ui/font_close.c | 2 +- lib/ui/font_load.c | 2 +- 5 files changed, 119 insertions(+), 59 deletions(-) diff --git a/bin/uitest/main.c b/bin/uitest/main.c index 16cf741..972455a 100644 --- a/bin/uitest/main.c +++ b/bin/uitest/main.c @@ -4,6 +4,10 @@ #include +/** + DRAWING ROUTINES +**/ + #define COLOR(c) ((c) | ((c) >> 8)) void SetBackground(UIWin* win, int c) @@ -31,17 +35,105 @@ void DrawRectangle(UIWin* win, int px, int py, int width, int height) XftDrawRect(win->xft, &(win->fg), px, py, width, height); } -void DrawGlyphs(UIWin* win, XftFont* font, XftGlyphSpec* specs, long nspecs) +void DrawGlyphs(UIWin* win, UIFont font, XftGlyphSpec* specs, long nspecs) +{ + XftDrawGlyphSpec(win->xft, &(win->fg), (XftFont*)font, specs, nspecs); +} + +void DrawString(UIWin* win, UIFont font, int posx, int posy, char* str) +{ + XftDrawStringUtf8(win->xft, &(win->fg), (XftFont*)font, posx, posy, (const FcChar8*)str, strlen(str)); +} + +/** + BASE WIDGET ROUTINES +**/ + +Widget* Widget_New(WidgetClass* class) +{ + Widget* self = calloc(1, sizeof(Widget)); + self->class = class; + return self; +} + +void Widget_Add(Widget* parent, Widget* child) +{ + if (parent) + { + child->parent = parent; + // Add to list here + } +} + + +/** + PANEL CODE +**/ +void Pane_Layout(Widget* pane); +void Pane_Paint(Widget* pane); + +WidgetClass PaneClass = { + .onlayout = Pane_Layout, + .onpaint = Pane_Paint +}; + +Widget* Pane(Widget* parent) +{ + Widget* self = Widget_New(&PaneClass); + Widget_Add(parent, self); + return self; +} + +void Pane_Layout(Widget* pane) { - XftDrawGlyphSpec(win->xft, &(win->fg), font, specs, nspecs); + // Adjust region here based on layout and widget dimensions } -void DrawString(UIWin* win, XftFont* font, int posx, int posy, char* str) +void Pane_Paint(Widget* pane) { - XftDrawStringUtf8(win->xft, &(win->fg), font, posx, posy, (const FcChar8*)str, strlen(str)); + // Draw the widget here } +/** + BUTTON CODE +**/ +void Button_Layout(Widget* pane); +void Button_Paint(Widget* pane); + +WidgetClass ButtonClass = { + .onlayout = Button_Layout, + .onpaint = Button_Paint +}; + +Widget* Button(Widget* parent, char* label) +{ + Widget* self = Widget_New(&ButtonClass); + Widget_Add(parent, self); + return self; +} + +void Button_Layout(Widget* pane) +{ + // Adjust region here based on layout and widget dimensions +} + +void Button_Paint(Widget* pane) +{ + // Draw the widget here +} + + +/** + APPLICATION CODE +**/ + +Widget* initialize(void) +{ + Widget* pane = Pane(NULL); + Button(pane, "Click Me!"); + return pane; +} void update(UIWin* win) { @@ -54,6 +146,7 @@ void update(UIWin* win) int main(int argc, char** argv) { UIWin* win = win_create("UI Test"); + win->frame = initialize(); win_show(win); XEvent ev; @@ -66,19 +159,3 @@ int main(int argc, char** argv) return 0; } - -typedef struct WidgetClass WidgetClass; -typedef struct Widget Widget; - -struct WidgetClass { - void (*onresize)(); - void (*onredraw)(); - void (*onaction)(); -}; - -struct Widget { - WidgetClass* class; - Widget* first; - Widget* last; -}; - diff --git a/inc/impl/libui.h b/inc/impl/libui.h index 129766b..fd6e7c0 100644 --- a/inc/impl/libui.h +++ b/inc/impl/libui.h @@ -2,10 +2,6 @@ #include #include -struct UIFont { - XftFont* font; -}; - struct UIWin { struct UIWin* next; @@ -16,9 +12,9 @@ struct UIWin GC gc; XftDraw* xft; XftFont* font; - XftColor bg; XftColor fg; + Widget* frame; }; struct XConf @@ -34,7 +30,3 @@ struct XConf }; extern struct XConf X; - -enum { - WIN_BACKGROUND = 0xEAEAEA -}; diff --git a/inc/libui.h b/inc/libui.h index f4a783b..37c344e 100644 --- a/inc/libui.h +++ b/inc/libui.h @@ -1,15 +1,25 @@ -typedef struct UIFont UIFont; +typedef void* UIFont; typedef struct UIWin UIWin; -typedef struct { - /* keyboard events */ - void (*onkeyup)(); - void (*onkeydown)(); +typedef struct WidgetClass WidgetClass; +typedef struct Widget Widget; - /* mouse events */ - void (*onclick)(); - void (*onmousemove)(); -} UIEventHandlers; +typedef struct { int x,y,w,h; } LayoutRegion; + +struct WidgetClass { + void (*onlayout)(Widget* self); + void (*onpaint)(Widget* self); +// void (*onaction)(Widget* self); +}; + +struct Widget { + WidgetClass* class; + LayoutRegion dim; + int count; + Widget* parent; + Widget* first_child; + Widget* last_child; +}; UIWin* win_create(char* title); void win_delete(UIWin* win); @@ -17,24 +27,5 @@ void win_show(UIWin* win); void win_hide(UIWin* win); void win_repaint(UIWin* win); - UIFont font_load(char* patt); void font_close(UIFont font); - -//typedef struct Attr { -// struct Attr* next; -// char* name; -// char* value; -//} Attr; -// -//typedef struct Widget { -// Attr* attributes; -// void (*render)(struct Widget* w); -//} Widget; - -// -//typdef stuct UIWindow { -// void* model; -// void (*update)(struct UIWindow*, struct UIEvent*); -// void (*redraw)(struct UIWindow*); -//} UIWindow; \ No newline at end of file diff --git a/lib/ui/font_close.c b/lib/ui/font_close.c index c812ae8..08191f9 100644 --- a/lib/ui/font_close.c +++ b/lib/ui/font_close.c @@ -4,5 +4,5 @@ void font_close(UIFont font) { - XftFontClose(X.display, font.font); + XftFontClose(X.display, (XftFont*)font); } \ No newline at end of file diff --git a/lib/ui/font_load.c b/lib/ui/font_load.c index 24163ef..87767f7 100644 --- a/lib/ui/font_load.c +++ b/lib/ui/font_load.c @@ -32,5 +32,5 @@ UIFont font_load(char* patt) fatal("failed to load font"); } - return (UIFont){ .font = font }; + return font; } -- 2.55.0