From 55c562645adca97412ac2e8f604a9e46c9e67fd3 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Sat, 18 Apr 2026 19:40:28 +0900 Subject: [PATCH] listbox rewrite done --- examples/basic/listbox.c | 10 +++--- include/Mw/TypeDefs.h | 6 ---- include/Mw/Widget/ListBox.h | 4 +-- src/dialog/filechooser.c | 20 +++++------ src/widget/combobox.c | 2 +- src/widget/listbox.c | 67 ++++++++++++++++++++++++++++++++----- 6 files changed, 77 insertions(+), 32 deletions(-) diff --git a/examples/basic/listbox.c b/examples/basic/listbox.c index ce7e9ac..8f6ca5a 100644 --- a/examples/basic/listbox.c +++ b/examples/basic/listbox.c @@ -40,18 +40,18 @@ int main() { MwSetInteger(lb, MwNleftPadding, 20); - index = MwListBoxInsert(lb, -1, 0, "Harvard sentence"); - MwListBoxInsert(lb, index, -1, "Length"); + index = MwListBoxSet(lb, -1, 0, "Harvard sentence"); + MwListBoxSet(lb, index, -1, "Length"); for(i = 0; i < len; i++) { char sz[16]; MwStringPrintIntoBuffer(sz, 16, "%d", (int)strlen(harvard[i])); - index = MwListBoxInsert(lb, -1, 0, harvard[i]); - MwListBoxInsert(lb, index, -1, sz); + index = MwListBoxSet(lb, -1, 0, harvard[i]); + MwListBoxSet(lb, index, -1, sz); MwListBoxSetIcon(lb, index, px); } - MwAddUserHandler(lb, MwNactivateHandler, activate, NULL); + MwAddUserHandler(lb, MwNlistBoxActivateHandler, activate, NULL); MwVaApply(lb, MwNhasHeading, 1, NULL); diff --git a/include/Mw/TypeDefs.h b/include/Mw/TypeDefs.h index ca8e32b..21def05 100644 --- a/include/Mw/TypeDefs.h +++ b/include/Mw/TypeDefs.h @@ -26,7 +26,6 @@ typedef struct _MwLabelSegment MwLabelSegment; typedef struct _MwListBoxEntry MwListBoxEntry; typedef struct _MwTreeViewEntry MwTreeViewEntry; typedef struct _MwDirectoryEntry MwDirectoryEntry; -typedef struct _MwListBoxPacket MwListBoxPacket; typedef struct _MwBox* MwBox; typedef struct _MwMouse MwMouse; #ifdef _MILSKO @@ -202,11 +201,6 @@ struct _MwDirectoryEntry { time_t mtime; }; -struct _MwListBoxPacket { - MwLLPixmap* pixmaps; - char*** names; -}; - struct _MwBox { int layout; }; diff --git a/include/Mw/Widget/ListBox.h b/include/Mw/Widget/ListBox.h index ae07068..5f2ef05 100644 --- a/include/Mw/Widget/ListBox.h +++ b/include/Mw/Widget/ListBox.h @@ -26,10 +26,10 @@ MWDECL MwClass MwListBoxClass; * @param text Text * @return Index */ -MwInline int MwListBoxInsert(MwWidget handle, int row, int col, const char* text) { +MwInline int MwListBoxSet(MwWidget handle, int row, int col, const char* text) { int out; - MwVaWidgetExecute(handle, "mwListBoxInsert", (void*)&out, row, col, text); + MwVaWidgetExecute(handle, "mwListBoxSet", (void*)&out, row, col, text); return out; } diff --git a/src/dialog/filechooser.c b/src/dialog/filechooser.c index 42fbf08..f155f07 100644 --- a/src/dialog/filechooser.c +++ b/src/dialog/filechooser.c @@ -216,7 +216,7 @@ static void layout(MwWidget handle) { MwNleftPadding, 16, NULL); - index = MwListBoxInsert(fc->nav, -1, 0, "Home"); + index = MwListBoxSet(fc->nav, -1, 0, "Home"); MwListBoxSetIcon(fc->nav, index, fc->computer); MwAddUserHandler(fc->nav, MwNlistBoxActivateHandler, nav_activate, NULL); @@ -462,9 +462,9 @@ static void scan(MwWidget handle, const char* path, int record) { MwListBoxSetWidth(fc->files, 1, 128); MwListBoxSetWidth(fc->files, 2, 0); - index = MwListBoxInsert(fc->files, -1, 0, "Name"); - MwListBoxInsert(fc->files, index, -1, "Date modified"); - MwListBoxInsert(fc->files, index, -1, "Size"); + index = MwListBoxSet(fc->files, -1, 0, "Name"); + MwListBoxSet(fc->files, index, -1, "Date modified"); + MwListBoxSet(fc->files, index, -1, "Size"); for(i = 0; i < arrlen(fc->entries); i++) { if(strcmp(fc->entries[i]->name, ".") == 0 || strcmp(fc->entries[i]->name, "..") == 0) continue; @@ -473,9 +473,9 @@ static void scan(MwWidget handle, const char* path, int record) { MwStringTime(date, fc->entries[i]->mtime); - index = MwListBoxInsert(fc->files, -1, 0, fc->entries[i]->name); - MwListBoxInsert(fc->files, index, -1, date); - MwListBoxInsert(fc->files, index, -1, NULL); + index = MwListBoxSet(fc->files, -1, 0, fc->entries[i]->name); + MwListBoxSet(fc->files, index, -1, date); + MwListBoxSet(fc->files, index, -1, NULL); MwListBoxSetIcon(fc->files, index, fc->dir); arrput(fc->sorted_entries, fc->entries[i]); @@ -489,9 +489,9 @@ static void scan(MwWidget handle, const char* path, int record) { MwStringTime(date, fc->entries[i]->mtime); MwStringSize(size, fc->entries[i]->size); - index = MwListBoxInsert(fc->files, -1, 0, fc->entries[i]->name); - MwListBoxInsert(fc->files, index, -1, date); - MwListBoxInsert(fc->files, index, -1, size); + index = MwListBoxSet(fc->files, -1, 0, fc->entries[i]->name); + MwListBoxSet(fc->files, index, -1, date); + MwListBoxSet(fc->files, index, -1, size); MwListBoxSetIcon(fc->files, index, fc->file); arrput(fc->sorted_entries, fc->entries[i]); diff --git a/src/widget/combobox.c b/src/widget/combobox.c index f553444..823185d 100644 --- a/src/widget/combobox.c +++ b/src/widget/combobox.c @@ -125,7 +125,7 @@ static void click(MwWidget handle) { MwLLSetCursor(((MwListBox)cb->listbox->internal)->frame->lowlevel, &MwCursorArrow, &MwCursorArrowMask); for(i = 0; i < arrlen(cb->list); i++) { - MwListBoxInsert(cb->listbox, -1, 0, cb->list[i]); + MwListBoxSet(cb->listbox, -1, 0, cb->list[i]); } MwAddUserHandler(cb->listbox, MwNlistBoxActivateHandler, listbox_activate, NULL); diff --git a/src/widget/listbox.c b/src/widget/listbox.c index 31fd00f..b2acad7 100644 --- a/src/widget/listbox.c +++ b/src/widget/listbox.c @@ -387,18 +387,64 @@ static void prop_change(MwWidget handle, const char* prop) { } } -static void mwListBoxInsertImpl(MwWidget handle, int row, int col, const char* text) { +static void redrawIfNeeded(MwWidget handle, int row) { MwListBox lb = handle->internal; - int old; - if(row == -1) row = arrlen(lb->list); - old = row; - resize(handle); - if(old < (MwGetInteger(lb->vscroll, MwNvalue) + MwGetInteger(lb->vscroll, MwNareaShown))) { + if(MwGetInteger(lb->vscroll, MwNvalue) <= row && row <= (MwGetInteger(lb->vscroll, MwNvalue) + MwGetInteger(lb->vscroll, MwNareaShown))) { MwForceRender(lb->frame); } } +static int mwListBoxSetImpl(MwWidget handle, int row, int col, const char* text) { + MwListBox lb = handle->internal; + MwListBoxEntry entry; + char* t = text == NULL ? NULL : MwStringDuplicate(text); + if(row == -1) row = arrlen(lb->list); + + entry.name = NULL; + entry.pixmap = NULL; + if(row < arrlen(lb->list)) { + entry = lb->list[row]; + } + + if(col == -1) col = arrlen(entry.name); + arrins(entry.name, col, t); + + if(row < arrlen(lb->list)) { + lb->list[row] = entry; + } else { + arrins(lb->list, row, entry); + } + + if(row >= arrlen(lb->list)) resize(handle); + redrawIfNeeded(handle, row); + + return row; +} + +static void mwListBoxSetIconImpl(MwWidget handle, int index, MwLLPixmap icon) { + MwListBox lb = handle->internal; + MwListBoxEntry entry; + if(index == -1) index = arrlen(lb->list); + + entry.name = NULL; + entry.pixmap = NULL; + if(index < arrlen(lb->list)) { + entry = lb->list[index]; + } + + entry.pixmap = icon; + + if(index < arrlen(lb->list)) { + lb->list[index] = entry; + } else { + arrins(lb->list, index, entry); + } + + if(index >= arrlen(lb->list)) resize(handle); + redrawIfNeeded(handle, index); +} + static void mwListBoxDeleteImpl(MwWidget handle, int index) { MwListBox lb = handle->internal; int i; @@ -496,11 +542,16 @@ static void func_handler(MwWidget handle, const char* name, void* out, va_list v int alignment = va_arg(va, int); mwListBoxSetAlignmentImpl(handle, index, alignment); } - if(strcmp(name, "mwListBoxInsert") == 0) { + if(strcmp(name, "mwListBoxSet") == 0) { int row = va_arg(va, int); int col = va_arg(va, int); const char* text = va_arg(va, const char*); - mwListBoxInsertImpl(handle, row, col, text); + *(int*)out = mwListBoxSetImpl(handle, row, col, text); + } + if(strcmp(name, "mwListBoxSetIcon") == 0) { + int index = va_arg(va, int); + MwLLPixmap icon = va_arg(va, MwLLPixmap); + mwListBoxSetIconImpl(handle, index, icon); } }