diff --git a/examples/basic/listbox.c b/examples/basic/listbox.c index 3aca836..f312404 100644 --- a/examples/basic/listbox.c +++ b/examples/basic/listbox.c @@ -54,7 +54,7 @@ int main() { MwVaApply(lb, MwNhasHeading, 1, NULL); - MwListBoxSetWidth(lb, 0, -64); + MwListBoxSetWidth(lb, 0, -128); MwListBoxInsert(lb, -1, packet); MwListBoxDestroyPacket(packet); diff --git a/include/Mw/TypeDefs.h b/include/Mw/TypeDefs.h index 63ab075..24d0d94 100644 --- a/include/Mw/TypeDefs.h +++ b/include/Mw/TypeDefs.h @@ -130,6 +130,7 @@ struct _MwListBox { unsigned long click_time; int pressed; int* width; + int* alignment; int changed; }; diff --git a/include/Mw/Widget/ListBox.h b/include/Mw/Widget/ListBox.h index 93e21bf..837e5e3 100644 --- a/include/Mw/Widget/ListBox.h +++ b/include/Mw/Widget/ListBox.h @@ -97,6 +97,16 @@ MwInline void MwListBoxSetWidth(MwWidget handle, int index, int width) { MwVaWidgetExecute(handle, "mwListBoxSetWidth", NULL, index, width); } +/*! + * @brief Sets an item alignment of the listbox + * @param handle Widget + * @param index Column index + * @param alignment Alignment + */ +MwInline void MwListBoxSetAlignment(MwWidget handle, int index, int alignment) { + MwVaWidgetExecute(handle, "mwListBoxSetAlignment", NULL, index, alignment); +} + /*! * @brief Resets the listbox * @param handle Widget diff --git a/src/widget/listbox.c b/src/widget/listbox.c index aace035..097e6a8 100644 --- a/src/widget/listbox.c +++ b/src/widget/listbox.c @@ -239,16 +239,28 @@ static void frame_draw(MwWidget handle) { MwLLDrawPixmap(handle->lowlevel, &r2, lb->list[i].pixmap); } p.y += MwTextHeight(handle, "M") / 2; - p.x = MwGetInteger(handle->parent, MwNleftPadding) + MwDefaultBorderWidth(handle) + 4; + p.x = MwGetInteger(handle->parent, MwNleftPadding) - MwDefaultBorderWidth(handle); for(j = 0; j < arrlen(lb->list[i].name); j++) { char* t = lb->list[i].name[j]; if(t == NULL) t = ""; - p.x += MwDefaultBorderWidth(handle); - MwDrawText(handle, &p, t, 0, MwALIGNMENT_BEGINNING, selected ? base2 : text2); - p.x += get_col_width(lb, j) - MwDefaultBorderWidth(handle); - if(j == 0) p.x -= 4; + if(j == (arrlen(lb->list[i].name) - 1)) p.x -= MwDefaultBorderWidth(handle); + if(arrlen(lb->alignment) <= j || lb->alignment[j] == MwALIGNMENT_BEGINNING) { + p.x += 4; + MwDrawText(handle, &p, t, 0, MwALIGNMENT_BEGINNING, selected ? base2 : text2); + p.x -= 4; + p.x += get_col_width(lb, j); + } else if(lb->alignment[j] == MwALIGNMENT_CENTER) { + p.x += get_col_width(lb, j) / 2; + MwDrawText(handle, &p, t, 0, MwALIGNMENT_CENTER, selected ? base2 : text2); + p.x += get_col_width(lb, j) / 2; + } else if(lb->alignment[j] == MwALIGNMENT_END) { + p.x += get_col_width(lb, j); + p.x -= 4; + MwDrawText(handle, &p, t, 0, MwALIGNMENT_END, selected ? base2 : text2); + p.x += 4; + } if(j == 0) p.x -= MwGetInteger(handle->parent, MwNleftPadding); } @@ -336,6 +348,7 @@ static int create(MwWidget handle) { lb->selected = -1; lb->click_time = 0; lb->width = NULL; + lb->alignment = NULL; lb->changed = 0; return 0; @@ -346,6 +359,7 @@ static void destroy(MwWidget handle) { MwListBoxReset(handle); arrfree(lb->list); arrfree(lb->width); + arrfree(lb->alignment); free(handle->internal); } @@ -377,11 +391,23 @@ static void draw(MwWidget handle) { x += MwDefaultBorderWidth(handle); - p.x = 4 + x; - p.y = r.y + r.height / 2; - MwDrawText(handle, &p, lb->list[0].name[i], 0, MwALIGNMENT_BEGINNING, text); + if(arrlen(lb->alignment) <= i || lb->alignment[i] == MwALIGNMENT_BEGINNING) { + p.x = 4 + x; + p.y = r.y + r.height / 2; + MwDrawText(handle, &p, lb->list[0].name[i], 0, MwALIGNMENT_BEGINNING, text); + } else if(lb->alignment[i] == MwALIGNMENT_CENTER) { + p.x = x + r.width / 2; + p.y = r.y + r.height / 2; + MwDrawText(handle, &p, lb->list[0].name[i], 0, MwALIGNMENT_CENTER, text); + } else if(lb->alignment[i] == MwALIGNMENT_END) { + p.x = x + r.width - 4; + p.y = r.y + r.height / 2; + MwDrawText(handle, &p, lb->list[0].name[i], 0, MwALIGNMENT_END, text); + } - x += r.width + MwDefaultBorderWidth(handle); + x += r.width; + + x += MwDefaultBorderWidth(handle); } } @@ -503,6 +529,17 @@ static void mwListBoxSetWidthImpl(MwWidget handle, int index, int width) { MwForceRender(lb->frame); } +static void mwListBoxSetAlignmentImpl(MwWidget handle, int index, int alignment) { + MwListBox lb = handle->internal; + + while(((index + 1) - arrlen(lb->alignment)) > 0) arrput(lb->alignment, MwALIGNMENT_BEGINNING); + + lb->alignment[index] = alignment; + + MwForceRender(handle); + MwForceRender(lb->frame); +} + static void func_handler(MwWidget handle, const char* name, void* out, va_list va) { if(strcmp(name, "mwListBoxDelete") == 0) { int index = va_arg(va, int); @@ -520,6 +557,11 @@ static void func_handler(MwWidget handle, const char* name, void* out, va_list v int width = va_arg(va, int); mwListBoxSetWidthImpl(handle, index, width); } + if(strcmp(name, "mwListBoxSetAlignment") == 0) { + int index = va_arg(va, int); + int alignment = va_arg(va, int); + mwListBoxSetAlignmentImpl(handle, index, alignment); + } if(strcmp(name, "mwListBoxInsert") == 0) { int index = va_arg(va, int); MwListBoxPacket* packet = va_arg(va, MwListBoxPacket*);