From 6dfb78ec7dc2dce0a941d2fd4354410ae9b9368a Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Thu, 16 Oct 2025 11:54:42 +0000 Subject: [PATCH] better git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@366 b9cfdab3-6d41-4d17-bbe4-086880011989 --- examples/gldemos/tripaint.c | 4 +-- include/Mw/TypeDefs.h | 19 +++++++----- src/widget/listbox.c | 60 ++++++++++++++++++++----------------- 3 files changed, 46 insertions(+), 37 deletions(-) diff --git a/examples/gldemos/tripaint.c b/examples/gldemos/tripaint.c index 62ed07c..ac353ba 100644 --- a/examples/gldemos/tripaint.c +++ b/examples/gldemos/tripaint.c @@ -98,8 +98,8 @@ static void mouse_move(MwWidget handle, void* user, void* call) { (void)handle; (void)user; - mx = point->x; - my = point->y; + mx = point->x; + my = point->y; if(click == 2) { t[ct].points[2 * 2 + 0] = point->x; diff --git a/include/Mw/TypeDefs.h b/include/Mw/TypeDefs.h index e44934d..e1a63c5 100644 --- a/include/Mw/TypeDefs.h +++ b/include/Mw/TypeDefs.h @@ -21,6 +21,7 @@ typedef struct _MwMenu* MwMenu; typedef struct _MwEntry* MwEntry; typedef struct _MwViewport* MwViewport; typedef struct _MwListBox* MwListBox; +typedef struct _MwListBoxEntry MwListBoxEntry; typedef struct _MwSizeHints MwSizeHints; typedef struct _MwDirectoryEntry MwDirectoryEntry; #ifdef _MILSKO @@ -119,14 +120,18 @@ struct _MwViewport { MwWidget inframe; }; +struct _MwListBoxEntry { + char* name; + MwLLPixmap pixmap; +}; + struct _MwListBox { - MwWidget vscroll; - MwWidget frame; - char** list; - MwLLPixmap* pixmap_list; - int selected; - unsigned long click_time; - int pressed; + MwWidget vscroll; + MwWidget frame; + MwListBoxEntry* list; + int selected; + unsigned long click_time; + int pressed; }; struct _MwSizeHints { diff --git a/src/widget/listbox.c b/src/widget/listbox.c index b0f8a45..3c0f0b0 100644 --- a/src/widget/listbox.c +++ b/src/widget/listbox.c @@ -113,21 +113,21 @@ static void frame_draw(MwWidget handle) { r2.x = 0; r2.y = p.y; r2.width = r.width; - r2.height = MwTextHeight(handle, lb->list[i]); + r2.height = MwTextHeight(handle, lb->list[i].name); MwDrawRect(handle, &r2, text); } - if(lb->pixmap_list[i] != NULL) { + if(lb->list[i].pixmap != NULL) { MwRect r2; - int h = (lb->pixmap_list[i]->height > MwTextHeight(handle, "M")) ? MwTextHeight(handle, "M") : lb->pixmap_list[i]->height; + int h = (lb->list[i].pixmap->height > MwTextHeight(handle, "M")) ? MwTextHeight(handle, "M") : lb->list[i].pixmap->height; r2.x = MwDefaultBorderWidth; r2.y = p.y + (MwTextHeight(handle, "M") - h) / 2; - r2.width = h * lb->pixmap_list[i]->width / lb->pixmap_list[i]->height; + r2.width = h * lb->list[i].pixmap->width / lb->list[i].pixmap->height; r2.height = h; - MwLLDrawPixmap(handle->lowlevel, &r2, lb->pixmap_list[i]); + MwLLDrawPixmap(handle->lowlevel, &r2, lb->list[i].pixmap); } - p.y += MwTextHeight(handle, lb->list[i]) / 2; - MwDrawText(handle, &p, lb->list[i], 0, MwALIGNMENT_BEGINNING, selected ? base : text); - p.y += MwTextHeight(handle, lb->list[i]) / 2; + p.y += MwTextHeight(handle, lb->list[i].name) / 2; + MwDrawText(handle, &p, lb->list[i].name, 0, MwALIGNMENT_BEGINNING, selected ? base : text); + p.y += MwTextHeight(handle, lb->list[i].name) / 2; } MwDrawFrame(handle, &r, base, 1); @@ -185,10 +185,9 @@ static int create(MwWidget handle) { MwSetDefault(handle); resize(handle); - lb->list = NULL; - lb->pixmap_list = NULL; - lb->selected = -1; - lb->click_time = 0; + lb->list = NULL; + lb->selected = -1; + lb->click_time = 0; MwSetInteger(handle, MwNleftPadding, 0); @@ -197,8 +196,11 @@ static int create(MwWidget handle) { static void destroy(MwWidget handle) { MwListBox lb = handle->internal; + int i; + for(i = 0; i < arrlen(lb->list); i++) { + free(lb->list[i].name); + } arrfree(lb->list); - arrfree(lb->pixmap_list); free(handle->internal); } @@ -243,14 +245,16 @@ MwClassRec MwListBoxClassRec = { MwClass MwListBoxClass = &MwListBoxClassRec; void MwListBoxInsert(MwWidget handle, int index, const char* text, MwLLPixmap pixmap) { - char* str = malloc(strlen(text) + 1); - MwListBox lb = handle->internal; + MwListBox lb = handle->internal; + MwListBoxEntry entry; - strcpy(str, text); + entry.name = malloc(strlen(text) + 1); + strcpy(entry.name, text); + + entry.pixmap = pixmap; if(index == -1) index = arrlen(lb->list); - arrins(lb->list, index, str); - arrins(lb->pixmap_list, index, pixmap); + arrins(lb->list, index, entry); resize(handle); if(index < (MwGetInteger(lb->vscroll, MwNvalue) + MwGetInteger(lb->vscroll, MwNareaShown))) { @@ -266,15 +270,15 @@ void MwListBoxInsertMultiple(MwWidget handle, int index, char** text, MwLLPixmap old = index; for(i = 0; i < count; i++) { - char* str = malloc(strlen(text[i]) + 1); - strcpy(str, text[i]); + MwListBoxEntry entry; - arrins(lb->list, index, str); - if(pixmap == NULL) { - arrins(lb->pixmap_list, index, NULL); - } else { - arrins(lb->pixmap_list, index, pixmap[i]); - } + entry.name = malloc(strlen(text[i]) + 1); + strcpy(entry.name, text[i]); + + entry.pixmap = NULL; + if(pixmap != NULL) entry.pixmap = pixmap[i]; + + arrins(lb->list, index, entry); index++; } @@ -288,8 +292,8 @@ void MwListBoxDelete(MwWidget handle, int index) { MwListBox lb = handle->internal; if(index == -1) index = arrlen(lb->list) - 1; + free(lb->list[index].name); arrdel(lb->list, index); - arrdel(lb->pixmap_list, index); if(lb->selected >= arrlen(lb->list)) { lb->selected = arrlen(lb->list) - 1; @@ -310,5 +314,5 @@ const char* MwListBoxGet(MwWidget handle, int index) { if(index < 0) return NULL; if(index >= arrlen(lb->list)) return NULL; - return lb->list[index]; + return lb->list[index].name; }