From 2e3744de9d2c5c80775dc77f9f25b8057e64f246 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 23 Oct 2008 17:28:07 +0200 Subject: [PATCH] widget: store supported align and use to render flex widgets Signed-off-by: Julien Danjou --- draw.h | 8 ++++---- structs.h | 2 ++ widget.c | 48 ++++++++++++++++++++++++++++++++++------------- widgets/textbox.c | 1 + 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/draw.h b/draw.h index 2a008dfa..62b4fac9 100644 --- a/draw.h +++ b/draw.h @@ -45,10 +45,10 @@ typedef struct typedef enum { - AlignLeft = 0, - AlignRight, - AlignCenter, - AlignFlex, + AlignLeft = (0), + AlignRight = (1), + AlignCenter = (1 << 1), + AlignFlex = (1 << 2), } alignment_t; typedef struct vector_t vector_t; diff --git a/structs.h b/structs.h index 8fbab0fb..979bca6e 100644 --- a/structs.h +++ b/structs.h @@ -143,6 +143,8 @@ struct widget_t luaA_ref mouse_enter, mouse_leave; /** Alignement */ alignment_t align; + /** Supported alignment */ + alignment_t align_supported; /** Misc private data */ void *data; /** Button bindings */ diff --git a/widget.c b/widget.c index 8b8d2aa1..e425d6c8 100644 --- a/widget.c +++ b/widget.c @@ -201,25 +201,47 @@ widget_render(widget_node_array_t *widgets, draw_context_t *ctx, xcb_gcontext_t widgets->tab[i].geometry.x = ctx->width - right; } + /* save left value */ + int fake_left = left; + + /* compute width of flex aligned widgets that does not support it */ int flex = 0; for(int i = 0; i < widgets->len; i++) - if(widgets->tab[i].widget->align == AlignFlex && widgets->tab[i].widget->isvisible) - flex++; + if(widgets->tab[i].widget->align == AlignFlex + && widgets->tab[i].widget->isvisible) + { + if(widgets->tab[i].widget->align_supported & AlignFlex) + flex++; + else + fake_left += widgets->tab[i].widget->geometry(widgets->tab[i].widget, + screen, ctx->height, + ctx->width - (fake_left + right)).width; + } - if(flex) - { - int length = (ctx->width - (left + right)) / flex; - - for(int i = 0; i < widgets->len; i++) - if(widgets->tab[i].widget->align == AlignFlex && widgets->tab[i].widget->isvisible) + /* now compute everybody together! */ + int flex_rendered = 0; + for(int i = 0; i < widgets->len; i++) + if(widgets->tab[i].widget->align == AlignFlex + && widgets->tab[i].widget->isvisible) + { + if(widgets->tab[i].widget->align_supported & AlignFlex) { + int width = (ctx->width - (right + fake_left)) / flex; + /* give last pixels to last flex to be rendered */ + if(flex_rendered == flex - 1) + width += (ctx->width - (right + fake_left)) % flex; widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget, screen, ctx->height, - length); - widgets->tab[i].geometry.x = left; - left += widgets->tab[i].geometry.width; + width); + flex_rendered++; } - } + else + widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget, + screen, ctx->height, + ctx->width - (left + right)); + widgets->tab[i].geometry.x = left; + left += widgets->tab[i].geometry.width; + } /* draw everything! */ draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg); @@ -259,8 +281,8 @@ widget_render(widget_node_array_t *widgets, draw_context_t *ctx, xcb_gcontext_t void widget_common_new(widget_t *widget) { - widget->align = AlignLeft; widget->button = widget_common_button; + widget->align_supported = AlignLeft | AlignRight; } /** Invalidate widgets which should be refresh upon diff --git a/widgets/textbox.c b/widgets/textbox.c index 86559a2c..ab92f02b 100644 --- a/widgets/textbox.c +++ b/widgets/textbox.c @@ -178,6 +178,7 @@ textbox_new(alignment_t align) w = p_new(widget_t, 1); widget_common_new(w); w->align = align; + w->align_supported |= AlignFlex; w->draw = textbox_draw; w->index = luaA_textbox_index; w->newindex = luaA_textbox_newindex;