widget: store supported align and use to render flex widgets

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-10-23 17:28:07 +02:00
parent 93be5b3cae
commit 2e3744de9d
4 changed files with 42 additions and 17 deletions

8
draw.h
View File

@ -45,10 +45,10 @@ typedef struct
typedef enum typedef enum
{ {
AlignLeft = 0, AlignLeft = (0),
AlignRight, AlignRight = (1),
AlignCenter, AlignCenter = (1 << 1),
AlignFlex, AlignFlex = (1 << 2),
} alignment_t; } alignment_t;
typedef struct vector_t vector_t; typedef struct vector_t vector_t;

View File

@ -143,6 +143,8 @@ struct widget_t
luaA_ref mouse_enter, mouse_leave; luaA_ref mouse_enter, mouse_leave;
/** Alignement */ /** Alignement */
alignment_t align; alignment_t align;
/** Supported alignment */
alignment_t align_supported;
/** Misc private data */ /** Misc private data */
void *data; void *data;
/** Button bindings */ /** Button bindings */

View File

@ -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; 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; int flex = 0;
for(int i = 0; i < widgets->len; i++) 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 == AlignFlex
&& widgets->tab[i].widget->isvisible)
{
if(widgets->tab[i].widget->align_supported & AlignFlex)
flex++; flex++;
else
fake_left += widgets->tab[i].widget->geometry(widgets->tab[i].widget,
screen, ctx->height,
ctx->width - (fake_left + right)).width;
}
if(flex) /* now compute everybody together! */
{ int flex_rendered = 0;
int length = (ctx->width - (left + right)) / flex;
for(int i = 0; i < widgets->len; i++) 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 == 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, widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
screen, ctx->height, screen, ctx->height,
length); 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; widgets->tab[i].geometry.x = left;
left += widgets->tab[i].geometry.width; left += widgets->tab[i].geometry.width;
} }
}
/* draw everything! */ /* draw everything! */
draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg); 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 void
widget_common_new(widget_t *widget) widget_common_new(widget_t *widget)
{ {
widget->align = AlignLeft;
widget->button = widget_common_button; widget->button = widget_common_button;
widget->align_supported = AlignLeft | AlignRight;
} }
/** Invalidate widgets which should be refresh upon /** Invalidate widgets which should be refresh upon

View File

@ -178,6 +178,7 @@ textbox_new(alignment_t align)
w = p_new(widget_t, 1); w = p_new(widget_t, 1);
widget_common_new(w); widget_common_new(w);
w->align = align; w->align = align;
w->align_supported |= AlignFlex;
w->draw = textbox_draw; w->draw = textbox_draw;
w->index = luaA_textbox_index; w->index = luaA_textbox_index;
w->newindex = luaA_textbox_newindex; w->newindex = luaA_textbox_newindex;