commit 022fe1302ee7a0a1b07222bc3219e2be2960e58f from: Onana Onana Xavier Manuel date: Sun Sep 21 04:56:15 2025 UTC Label alignment - Added label alignement for center and right with macros commit - 749e270c9e436c9e475d2ef00a30660ad7f1b12e commit + 022fe1302ee7a0a1b07222bc3219e2be2960e58f blob - 7e285dc9cc7d37558104a0476578f5ec3960492c blob + e1bb4ca5fdb74148abbb1deea15b7c645763e8a8 --- TODO +++ TODO @@ -18,14 +18,11 @@ mostly for FreeBSD as OpenBSD and NetBSD already come with X11R6 and X11R7. -- Fix font line spacing in `mui_label`. When turning on setting the color - of the xcb_render picture in `context.c` there is a noticable gap at - the end of the texture. This is due to the layout height being multiplied - by 1.5 as a temporary hack. - - Makefile for OpenBSD ports. The make command currently only create a limbui.so file in the library folder. Change the makefile to install the library on an OpenBSD system. - Add label partial font styling to have different styles inside the same label. More customization to label in other world. + +- Make event poping faster blob - 1a3f4838d6f15495ff8fcdc0c8b1becb22a91327 blob + d22a4777aabf673199ee3f7ef4b102ccf6c86592 --- context.c +++ context.c @@ -371,17 +371,25 @@ ctx_create_label(struct ctx_node *node) { uint16_t width; uint16_t height; + uint16_t gwidth; xcb_rectangle_t rect; xcb_pixmap_t xcb_pixmap; xcb_render_picture_t xcb_picture; xcb_render_color_t xcb_color; struct mui_label *label; + struct mui_group *group; struct mui_layout *layout; + struct mui_layout *glayout; label = (struct mui_label *)node->ptr; layout = mui_get_label_layout(label); + group = mui_get_label_group(label); + glayout = mui_get_group_layout(group); + gwidth = mui_get_layout_width(glayout); + + width = mui_get_layout_width(layout); height = mui_get_layout_height(layout) * 1.5; @@ -392,13 +400,13 @@ ctx_create_label(struct ctx_node *node) rect.x = 0; rect.y = 0; - rect.width = width; + rect.width = gwidth; rect.height = height; xcb_pixmap = xcb_generate_id(xcb.conn); xcb_picture = xcb_generate_id(xcb.conn); - xcb_create_pixmap(xcb.conn, 32, xcb_pixmap, xcb.screen->root, width, height); + xcb_create_pixmap(xcb.conn, 32, xcb_pixmap, xcb.screen->root, gwidth, height); xcb_render_create_picture(xcb.conn, xcb_picture, xcb_pixmap, xcb.fmt_argb32, 0, NULL); xcb_render_fill_rectangles(xcb.conn, XCB_RENDER_PICT_OP_SRC, xcb_picture, xcb_color, 1, &rect); @@ -415,6 +423,7 @@ ctx_create_label(struct ctx_node *node) struct mui_color m_color; int tindex; /* Text index */ int last_width; + int flags; struct mui_gindice *ptr; struct mui_gindice *last_indice; @@ -453,7 +462,9 @@ ctx_create_label(struct ctx_node *node) tindex = 0; last_width = 0; last_indice = NULL; + flags = mui_get_label_flags(label); while ((ptr = mui_get_label_next_indice(label, ptr))) { + int offset = 0; header.dy += xcb.fonts[ptr->gs]->height; /* @@ -462,6 +473,12 @@ ctx_create_label(struct ctx_node *node) * * This line will offset properly the xcb_picture */ + + if (flags & MUI_TEXT_ALIGN_RIGHT) + offset = gwidth - mui_get_label_line_width(label, ptr->lnum); + else if (flags & MUI_TEXT_ALIGN_CENTER) + offset = (gwidth - mui_get_label_line_width(label, ptr->lnum)) / 2; + if (last_indice) { if (last_indice->lnum == ptr->lnum) { header.dx = last_width; @@ -472,6 +489,8 @@ ctx_create_label(struct ctx_node *node) } } + header.dx += offset; + header.count = ptr->len; text = mui_get_label_text(label); blob - 1c9fac3bbc67c9dd9f7c24349f0b5e23c2a18b85 blob + a4f4b80eca3835b543e760efe65029d21b816028 --- demo/text/main.c +++ demo/text/main.c @@ -14,13 +14,14 @@ char notice[] = "OpenBSD - Free, Functional and secure\n\n" "NetBSD- Free, Fast, Secure and highly portable Unix-like Open Source operating system\n\n" "FreeBSD - The Power to Serve\n\n" -"DragonflyBSD"; +"DragonflyBSD - The Power to Fly?"; int main(void) { struct mui_event event; struct mui_label *label; + struct mui_layout *layout; window.mw = mui_create_window("text.ex", 320, 240); window.group = mui_get_window_group(window.mw); @@ -28,8 +29,9 @@ main(void) window.handler = mui_get_window_event_handler(window.mw); label = mui_create_label(notice); - mui_set_label_flags(label, MUI_TEXT_WRAPPED | MUI_TEXT_MULTILINE); + layout = mui_get_label_layout(label); mui_group_add(window.group, MUI_LABEL, label); + mui_set_label_flags(label, MUI_TEXT_WRAPPED | MUI_TEXT_MULTILINE | MUI_TEXT_ALIGN_CENTER); while ((event = mui_pop_event(window.handler)).type != MUI_EVENT_QUIT) { mui_update(); blob - 7550594e8ce6f36f6d267ec6b8d9396befa14332 blob + f867e35956bb7b8d3076c09523d231ea5eb3d57d --- event.c +++ event.c @@ -1,9 +1,11 @@ #include "mui.h" +#define MUI_EVENT_MAX 4 + struct mui_event_handler { - uint8_t count; - struct mui_window *mw; - TAILQ_HEAD(ev_list, mui_event) head; + int capacity; + struct mui_window *mw; + TAILQ_HEAD(elist, mui_event) head; }; @@ -12,57 +14,47 @@ mui_create_event_handler(struct mui_window *mw) { struct mui_event_handler *handler; - handler = calloc(1, sizeof(*handler)); + handler = malloc(sizeof(*handler)); TAILQ_INIT(&handler->head); - handler->count = 0; + handler->capacity = 0; handler->mw = mw; - return handler; } void mui_push_event(struct mui_event_handler *handler, struct mui_event *event) { - struct mui_event *current; + struct mui_event *del; + struct mui_event *new; - if (handler->count == MUI_EVENT_MAX) { - struct mui_event *tail; - - tail = TAILQ_FIRST(&handler->head); - TAILQ_REMOVE(&handler->head, tail, entries); - handler->count--; - free(tail); + if (handler->capacity == MUI_EVENT_MAX) { + del = TAILQ_FIRST(&handler->head); + TAILQ_REMOVE(&handler->head, del, entries); + handler->capacity--; + free(del); } - current = calloc(1, sizeof(*current)); - memcpy(current, event, sizeof(struct mui_event)); - - current->target.id = MUI_UNDEF; - current->target.ptr = NULL; - mui_event_set_target(handler, current); - TAILQ_INSERT_HEAD(&handler->head, current, entries); - - handler->count++; + handler->capacity++; + new = calloc(1, sizeof(*new)); + memcpy(new, event, sizeof(*new)); + mui_event_set_target(handler, new); + TAILQ_INSERT_HEAD(&handler->head, new, entries); } struct mui_event mui_pop_event(struct mui_event_handler *handler) { - struct mui_event event; - struct mui_event *head; + struct mui_event event, *head; - event.type = MUI_EVENT_NONE; + event.type = MUI_UNDEF; + if (handler->capacity == 0) return event; - if (handler->count == 0) - return event; - - head = TAILQ_LAST(&handler->head, ev_list); - + head = TAILQ_LAST(&handler->head, elist); memcpy(&event, head, sizeof(event)); TAILQ_REMOVE(&handler->head, head, entries); free(head); - handler->count--; + handler->capacity--; return event; } blob - 27be9d05366f4aba761b51139b2701a9ca1877e5 blob + cc36c3ab9073768ecd209ad42b0b1672e3b29020 --- label.c +++ label.c @@ -10,6 +10,7 @@ struct mui_label { struct mui_group *group; struct mui_layout *layout; + uint16_t *widths; TAILQ_HEAD(mui_gindice_h, mui_gindice) head; }; @@ -24,6 +25,8 @@ mui_create_label(char *text) memcpy(label->text, text, label->len); label->text[label->len] = '\0'; label->font_id = 0; + label->flags = 0; + label->vlen = 0; label->group = NULL; label->color = (struct mui_color) { 0, 0, 0, 1.0 }; @@ -41,6 +44,7 @@ mui_delete_label(struct mui_label *label) mui_group_remove(label->group, MUI_LABEL, label); mui_delete_layout(label->layout); + free(label->widths); free(label->text); free(label); } @@ -64,6 +68,12 @@ mui_get_label_font_id(struct mui_label *label) } int +mui_get_label_line_width(struct mui_label *label, int line) +{ + return label->widths[line]; +} + +int mui_get_label_len(struct mui_label *label) { return label->len; @@ -75,6 +85,12 @@ mui_get_label_text(struct mui_label *label) return label->text; } +int +mui_get_label_flags(struct mui_label *label) +{ + return label->flags; +} + struct mui_color mui_get_label_color(struct mui_label *label) { @@ -130,6 +146,7 @@ mui_format_label(struct mui_label *label) uint16_t twidth; uint16_t lwidth; uint32_t gs, lnum; + uint16_t last_width; struct mui_gindice *np; struct mui_layout *layout; @@ -142,8 +159,10 @@ mui_format_label(struct mui_label *label) layout = mui_get_group_layout(label->group); lwidth = mui_get_layout_width(layout); + last_width = 0; gs = len = lnum = 0; width = height = 0; + label->vlen = 0; last_space = 0; twidth = 0; @@ -153,8 +172,10 @@ mui_format_label(struct mui_label *label) twidth += mui_ctx_get_glyph_width(label->text[i], label->font_id); - if (label->text[i] == ' ') + if (label->text[i] == ' ') { last_space = i; + last_width = twidth - mui_ctx_get_glyph_width(label->text[i], label->font_id); + } if (twidth > lwidth && last_space > 0 && label->flags & MUI_TEXT_WRAPPED) { /* Checking in current indice for last line */ @@ -166,6 +187,8 @@ mui_format_label(struct mui_label *label) width = (width < lwidth) ? lwidth : width; height += mui_ctx_get_font_height(label->font_id); + label->widths = reallocarray(label->widths, ++label->vlen, sizeof(uint16_t)); + label->widths[label->vlen-1] = last_width; TAILQ_INSERT_TAIL(&label->head, np, entries); i = last_space; @@ -178,24 +201,22 @@ mui_format_label(struct mui_label *label) } else { int cindex; struct mui_gindice *curr; - struct mui_gindice *next; cindex = i - len; curr = TAILQ_LAST(&label->head, mui_gindice_h); while (cindex - curr->len > last_space) { cindex -= curr->len; - curr = TAILQ_PREV(curr, mui_gindice_h, entries); + TAILQ_REMOVE(&label->head, curr, entries); + curr = TAILQ_LAST(&label->head, mui_gindice_h); } - while ((next = TAILQ_NEXT(curr, entries))) { - TAILQ_REMOVE(&label->head, next, entries); - free(next); - } - i = cindex - curr->len; - curr->len = last_space - i; + curr->len = (last_space - i); + curr->lnum = lnum; width = (width < lwidth) ? lwidth : width; + label->widths = reallocarray(label->widths, ++label->vlen, sizeof(uint16_t)); + label->widths[label->vlen-1] = last_width; i = last_space; last_space = 0; @@ -214,6 +235,8 @@ mui_format_label(struct mui_label *label) np->lnum = lnum; width = (width < twidth) ? twidth : width; height += mui_ctx_get_font_height(label->font_id); + label->widths = reallocarray(label->widths, ++label->vlen, sizeof(uint16_t)); + label->widths[label->vlen-1] = twidth; TAILQ_INSERT_TAIL(&label->head, np, entries); break; @@ -231,7 +254,6 @@ mui_format_label(struct mui_label *label) len = 0; TAILQ_INSERT_TAIL(&label->head, np, entries); - printf("{%d, %d, %d}, ", np->lnum, np->gs, np->len); } if (label->text[i] == '\n' && label->flags & MUI_TEXT_MULTILINE) { @@ -242,6 +264,8 @@ mui_format_label(struct mui_label *label) width = (width < twidth) ? twidth : width; height += mui_ctx_get_font_height(label->font_id); + label->widths = reallocarray(label->widths, ++label->vlen, sizeof(uint16_t)); + label->widths[label->vlen-1] = twidth; twidth = 0; TAILQ_INSERT_TAIL(&label->head, np, entries); @@ -282,4 +306,8 @@ void mui_set_label_flags(struct mui_label *label, int flags) { label->flags = flags; + mui_format_label(label); + + if (label->group) + mui_update_group_member_node(label->group, mui_in_group(label->group, MUI_LABEL, label)); } blob - c9736c535e1c840f9cf282847f9187ef8013f530 blob + 8061a2f9ee7a02d181460e7f4ea54b475ba0ecb7 --- layout.c +++ layout.c @@ -249,7 +249,14 @@ mui_scale_layout(struct mui_layout *layout, float scal layout->scale_x = l_scale_x; layout->scale_y = l_scale_y; - if (l_group) mui_update_group_member(l_group, mui_in_group(l_group, layout->type, layout->ptr)); + if (layout->type == MUI_LABEL) { + mui_format_label(layout->ptr); + if (l_group) + mui_update_group_member_node(l_group, mui_in_group(l_group, layout->type, layout->ptr)); + } else { + if (l_group) + mui_update_group_member(l_group, mui_in_group(l_group, layout->type, layout->ptr)); + } } void blob - daa50e1a8b330d153dcdaa012f2f6343b9e805b1 blob + b635ca1011256518c03b8d6efbc6ef21c123a3f5 --- mui.h +++ mui.h @@ -63,8 +63,6 @@ struct mui_window; struct mui_ctx; -#define MUI_EVENT_MAX 3 - enum { MUI_EVENT_NONE, MUI_EVENT_QUIT, @@ -143,6 +141,8 @@ struct mui_label *mui_create_label(c void mui_delete_label(struct mui_label *label); +void mui_format_label(struct mui_label *label); + struct mui_group *mui_get_label_group(struct mui_label *label); struct mui_layout *mui_get_label_layout(struct mui_label *label); @@ -153,10 +153,14 @@ char *mui_get_label_text int mui_get_label_len(struct mui_label *label); +int mui_get_label_flags(struct mui_label *label); + struct mui_color mui_get_label_color(struct mui_label *label); struct mui_gindice *mui_get_label_next_indice(struct mui_label *label, struct mui_gindice *ptr); +int mui_get_label_line_width(struct mui_label *label, int line); + void mui_set_label_color(struct mui_label *label, float r, float g, float b, float a); void mui_set_label_flags(struct mui_label *label, int flags); @@ -309,6 +313,8 @@ int mui_ctx_get_font_h #define MUI_TEXT_WRAPPED 1 #define MUI_TEXT_MULTILINE 2 +#define MUI_TEXT_ALIGN_RIGHT 4 +#define MUI_TEXT_ALIGN_CENTER 8 /* KEY CODES *