listbox improvement

git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@769 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
Nishi 2025-11-21 19:36:33 +00:00
commit 39cd88e547
4 changed files with 63 additions and 10 deletions

View file

@ -54,7 +54,7 @@ int main() {
MwVaApply(lb, MwVaApply(lb,
MwNhasHeading, 1, MwNhasHeading, 1,
NULL); NULL);
MwListBoxSetWidth(lb, 0, -64); MwListBoxSetWidth(lb, 0, -128);
MwListBoxInsert(lb, -1, packet); MwListBoxInsert(lb, -1, packet);
MwListBoxDestroyPacket(packet); MwListBoxDestroyPacket(packet);

View file

@ -130,6 +130,7 @@ struct _MwListBox {
unsigned long click_time; unsigned long click_time;
int pressed; int pressed;
int* width; int* width;
int* alignment;
int changed; int changed;
}; };

View file

@ -97,6 +97,16 @@ MwInline void MwListBoxSetWidth(MwWidget handle, int index, int width) {
MwVaWidgetExecute(handle, "mwListBoxSetWidth", NULL, index, 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 * @brief Resets the listbox
* @param handle Widget * @param handle Widget

View file

@ -239,16 +239,28 @@ static void frame_draw(MwWidget handle) {
MwLLDrawPixmap(handle->lowlevel, &r2, lb->list[i].pixmap); MwLLDrawPixmap(handle->lowlevel, &r2, lb->list[i].pixmap);
} }
p.y += MwTextHeight(handle, "M") / 2; 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++) { for(j = 0; j < arrlen(lb->list[i].name); j++) {
char* t = lb->list[i].name[j]; char* t = lb->list[i].name[j];
if(t == NULL) t = ""; if(t == NULL) t = "";
p.x += MwDefaultBorderWidth(handle); if(j == (arrlen(lb->list[i].name) - 1)) p.x -= MwDefaultBorderWidth(handle);
MwDrawText(handle, &p, t, 0, MwALIGNMENT_BEGINNING, selected ? base2 : text2); if(arrlen(lb->alignment) <= j || lb->alignment[j] == MwALIGNMENT_BEGINNING) {
p.x += get_col_width(lb, j) - MwDefaultBorderWidth(handle); p.x += 4;
if(j == 0) 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); if(j == 0) p.x -= MwGetInteger(handle->parent, MwNleftPadding);
} }
@ -336,6 +348,7 @@ static int create(MwWidget handle) {
lb->selected = -1; lb->selected = -1;
lb->click_time = 0; lb->click_time = 0;
lb->width = NULL; lb->width = NULL;
lb->alignment = NULL;
lb->changed = 0; lb->changed = 0;
return 0; return 0;
@ -346,6 +359,7 @@ static void destroy(MwWidget handle) {
MwListBoxReset(handle); MwListBoxReset(handle);
arrfree(lb->list); arrfree(lb->list);
arrfree(lb->width); arrfree(lb->width);
arrfree(lb->alignment);
free(handle->internal); free(handle->internal);
} }
@ -377,11 +391,23 @@ static void draw(MwWidget handle) {
x += MwDefaultBorderWidth(handle); x += MwDefaultBorderWidth(handle);
p.x = 4 + x; if(arrlen(lb->alignment) <= i || lb->alignment[i] == MwALIGNMENT_BEGINNING) {
p.y = r.y + r.height / 2; p.x = 4 + x;
MwDrawText(handle, &p, lb->list[0].name[i], 0, MwALIGNMENT_BEGINNING, text); 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); 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) { static void func_handler(MwWidget handle, const char* name, void* out, va_list va) {
if(strcmp(name, "mwListBoxDelete") == 0) { if(strcmp(name, "mwListBoxDelete") == 0) {
int index = va_arg(va, int); 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); int width = va_arg(va, int);
mwListBoxSetWidthImpl(handle, index, width); 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) { if(strcmp(name, "mwListBoxInsert") == 0) {
int index = va_arg(va, int); int index = va_arg(va, int);
MwListBoxPacket* packet = va_arg(va, MwListBoxPacket*); MwListBoxPacket* packet = va_arg(va, MwListBoxPacket*);