better listbox functions

git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@463 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
NishiOwO
2025-10-22 17:30:57 +00:00
parent 12b48a03df
commit 10fd79c54c
6 changed files with 208 additions and 129 deletions

View File

@@ -168,11 +168,20 @@ static void layout(MwWidget handle) {
ww = 160;
wh = h - 10 - 24 - 5 - 24 - 5 - 24 - 5;
if(fc->nav == NULL) {
MwListBoxPacket* p = MwListBoxCreatePacket();
int index;
index = MwListBoxPacketInsert(p, -1);
MwListBoxPacketSet(p, index, 0, "Home");
MwListBoxPacketSetIcon(p, index, fc->computer);
fc->nav = MwVaCreateWidget(MwListBoxClass, "nav", handle, wx, wy, ww, wh,
MwNleftPadding, 16,
NULL);
MwListBoxInsert(fc->nav, -1, fc->computer, "Home", NULL);
MwListBoxInsert(fc->nav, -1, p);
MwAddUserHandler(fc->nav, MwNactivateHandler, nav_activate, NULL);
MwListBoxDestroyPacket(p);
} else {
MwVaApply(fc->nav,
MwNx, wx,
@@ -357,13 +366,11 @@ static int qsort_files(const void* a, const void* b) {
}
static void scan(MwWidget handle, const char* path, int record) {
filechooser_t* fc = handle->opaque;
void* dir = MwDirectoryOpen(path);
int i;
char** names = NULL;
char** dates = NULL;
char** sizes = NULL;
MwLLPixmap* icons = NULL;
filechooser_t* fc = handle->opaque;
void* dir = MwDirectoryOpen(path);
int i;
MwListBoxPacket* p = MwListBoxCreatePacket();
int index;
if(dir != NULL) {
MwDirectoryEntry* entry;
@@ -403,57 +410,50 @@ static void scan(MwWidget handle, const char* path, int record) {
NULL);
MwListBoxReset(fc->files);
MwListBoxInsert(fc->files, -1, NULL, "Name", "Date modified", "Size", NULL);
MwListBoxSetWidth(fc->files, 0, -128 - 96);
MwListBoxSetWidth(fc->files, 1, 128);
MwListBoxSetWidth(fc->files, 2, 0);
icons = NULL;
names = NULL;
dates = NULL;
sizes = NULL;
index = MwListBoxPacketInsert(p, -1);
MwListBoxPacketSet(p, index, -1, "Name");
MwListBoxPacketSet(p, index, -1, "Date modified");
MwListBoxPacketSet(p, 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;
if(fc->entries[i]->type == MwDIRECTORY_DIRECTORY) {
char* date = malloc(128);
char date[128];
MwStringTime(date, fc->entries[i]->mtime);
arrput(names, fc->entries[i]->name);
arrput(dates, date);
arrput(sizes, NULL);
arrput(icons, fc->dir);
index = MwListBoxPacketInsert(p, -1);
MwListBoxPacketSetIcon(p, index, fc->dir);
MwListBoxPacketSet(p, index, -1, fc->entries[i]->name);
MwListBoxPacketSet(p, index, -1, date);
MwListBoxPacketSet(p, index, -1, NULL);
arrput(fc->sorted_entries, fc->entries[i]);
}
}
for(i = 0; i < arrlen(fc->entries); i++) {
if(fc->entries[i]->type == MwDIRECTORY_FILE) {
char* date = malloc(128);
char* size = malloc(128);
char date[128];
char size[128];
MwStringTime(date, fc->entries[i]->mtime);
MwStringSize(size, fc->entries[i]->size);
arrput(names, fc->entries[i]->name);
arrput(dates, date);
arrput(sizes, size);
arrput(icons, fc->file);
index = MwListBoxPacketInsert(p, -1);
MwListBoxPacketSetIcon(p, index, fc->file);
MwListBoxPacketSet(p, index, -1, fc->entries[i]->name);
MwListBoxPacketSet(p, index, -1, date);
MwListBoxPacketSet(p, index, -1, size);
arrput(fc->sorted_entries, fc->entries[i]);
}
}
MwListBoxInsertMultiple(fc->files, -1, arrlen(names), icons, names, dates, sizes, NULL);
for(i = 0; i < arrlen(dates); i++) {
if(dates[i] != NULL) free(dates[i]);
}
for(i = 0; i < arrlen(sizes); i++) {
if(sizes[i] != NULL) free(sizes[i]);
}
arrfree(sizes);
arrfree(dates);
arrfree(names);
arrfree(icons);
MwListBoxInsert(fc->files, -1, p);
MwListBoxDestroyPacket(p);
}
MwWidget MwFileChooser(MwWidget handle, const char* title) {

View File

@@ -3,6 +3,56 @@
#include "../../external/stb_ds.h"
MwListBoxPacket* MwListBoxCreatePacket(void) {
MwListBoxPacket* packet = malloc(sizeof(*packet));
memset(packet, 0, sizeof(*packet));
return packet;
}
void MwListBoxDestroyPacket(MwListBoxPacket* packet) {
int i;
for(i = 0; i < arrlen(packet->names); i++) {
}
arrfree(packet->names);
arrfree(packet->pixmaps);
free(packet);
}
int MwListBoxPacketInsert(MwListBoxPacket* packet, int index) {
int i;
if(index == -1) index = arrlen(packet->names);
for(i = arrlen(packet->names); i < index; i++) {
arrput(packet->names, NULL);
arrput(packet->pixmaps, NULL);
}
arrins(packet->names, index, NULL);
arrins(packet->pixmaps, index, NULL);
return index;
}
void MwListBoxPacketSet(MwListBoxPacket* packet, int index, int col, const char* text) {
char* t = text == NULL ? NULL : MwStringDupliacte(text);
int i;
if(col == -1) col = arrlen(packet->names[index]);
for(i = arrlen(packet->names[index]); i < col; i++) {
arrput(packet->names[index], NULL);
}
arrins(packet->names[index], col, t);
}
void MwListBoxPacketSetIcon(MwListBoxPacket* packet, int index, MwLLPixmap icon) {
packet->pixmaps[index] = icon;
}
static int get_first_entry(MwWidget handle, MwListBox lb) {
int st = 0;
int y = MwGetInteger(handle, MwNhasHeading) ? 1 : 0;
@@ -172,8 +222,12 @@ static void frame_draw(MwWidget handle) {
p.y += MwTextHeight(handle, "M") / 2;
p.x = MwGetInteger(handle->parent, MwNleftPadding);
for(j = 0; j < arrlen(lb->list[i].name); j++) {
char* t = lb->list[i].name[j];
if(t == NULL) t = "";
p.x += MwDefaultBorderWidth;
MwDrawText(handle, &p, lb->list[i].name[j], 0, MwALIGNMENT_BEGINNING, selected ? base : text);
MwDrawText(handle, &p, t, 0, MwALIGNMENT_BEGINNING, selected ? base : text);
p.x += get_col_width(lb, j) - MwDefaultBorderWidth;
if(j == 0) p.x -= MwGetInteger(handle->parent, MwNleftPadding);
@@ -312,62 +366,39 @@ static void prop_change(MwWidget handle, const char* prop) {
}
}
static void mwListBoxInsertImpl(MwWidget handle, int index, MwLLPixmap pixmap, va_list va) {
MwListBox lb = handle->internal;
MwListBoxEntry entry;
char* name;
entry.name = NULL;
while((name = va_arg(va, char*)) != NULL) {
name = MwStringDupliacte(name);
arrput(entry.name, name);
}
entry.pixmap = pixmap;
if(index == -1) index = arrlen(lb->list);
arrins(lb->list, index, entry);
resize(handle);
if(index < (MwGetInteger(lb->vscroll, MwNvalue) + MwGetInteger(lb->vscroll, MwNareaShown))) {
MwForceRender(lb->frame);
}
}
static void mwListBoxInsertMultipleImpl(MwWidget handle, int index, int count, MwLLPixmap* pixmap, va_list va) {
static void mwListBoxInsertImpl(MwWidget handle, int index, MwListBoxPacket* packet) {
int i;
MwListBox lb = handle->internal;
int old;
char*** vlist = NULL;
char** list;
int max = 0;
if(index == -1) index = arrlen(lb->list);
old = index;
while((list = va_arg(va, char**)) != NULL) {
arrput(vlist, list);
for(i = 0; i < arrlen(packet->names); i++) {
if(arrlen(packet->names[i]) > max) max = arrlen(packet->names[i]);
}
for(i = 0; i < count; i++) {
for(i = 0; i < arrlen(packet->names); i++) {
MwListBoxEntry entry;
char* name;
int j;
entry.name = NULL;
for(j = 0; j < arrlen(vlist); j++) {
if(vlist[j][i] == NULL) continue;
name = MwStringDupliacte(vlist[j][i]);
arrput(entry.name, name);
for(j = 0; j < max; j++) {
if(arrlen(packet->names[i]) > j && packet->names[i][j] != NULL) {
name = MwStringDupliacte(packet->names[i][j]);
arrput(entry.name, name);
} else {
arrput(entry.name, NULL);
}
}
entry.pixmap = NULL;
if(pixmap != NULL) entry.pixmap = pixmap[i];
entry.pixmap = packet->pixmaps[i];
arrins(lb->list, index, entry);
index++;
}
arrfree(vlist);
resize(handle);
if(old < (MwGetInteger(lb->vscroll, MwNvalue) + MwGetInteger(lb->vscroll, MwNareaShown))) {
MwForceRender(lb->frame);
@@ -456,21 +487,9 @@ static void func_handler(MwWidget handle, const char* name, void* out, va_list v
mwListBoxSetWidthImpl(handle, index, width);
}
if(strcmp(name, "mwListBoxInsert") == 0) {
int index = va_arg(va, int);
MwLLPixmap pixmap = va_arg(va, MwLLPixmap);
va_list* pva = va_arg(va, va_list*);
va_list va;
memcpy(&va, pva, sizeof(va));
mwListBoxInsertImpl(handle, index, pixmap, va);
}
if(strcmp(name, "mwListBoxInsertMultiple") == 0) {
int index = va_arg(va, int);
int count = va_arg(va, int);
MwLLPixmap* pixmap = va_arg(va, MwLLPixmap*);
va_list* pva = va_arg(va, va_list*);
va_list va;
memcpy(&va, pva, sizeof(va));
mwListBoxInsertMultipleImpl(handle, index, count, pixmap, va);
int index = va_arg(va, int);
MwListBoxPacket* packet = va_arg(va, MwListBoxPacket*);
mwListBoxInsertImpl(handle, index, packet);
}
}