functional listbox

git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@328 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
NishiOwO
2025-10-14 16:41:38 +00:00
parent c6e267a2b7
commit c30431f166
8 changed files with 237 additions and 34 deletions

View File

@@ -9,7 +9,7 @@ USE_STB_IMAGE = 1
CC = $(GCC)gcc
CXX = $(GCC)g++
CFLAGS = -Wall -Wextra -Wno-unused-parameter -Wno-implicit-fallthrough -Wno-unused-value -Iinclude
CFLAGS = -Wall -Wextra -Wno-unused-parameter -Wno-implicit-fallthrough -Wno-unused-value -Wno-sign-compare -Iinclude
LDFLAGS =
LIBS =

View File

@@ -82,6 +82,18 @@ MWDECL void MwDrawFrameEx(MwWidget handle, MwRect* rect, MwLLColor color, int in
*/
MWDECL void MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, int align, MwLLColor color);
/*!
* %brief Draws a text
* %param handle Widget
* %param point Center point of the text
* %param text Text
* %param bold Bold
* %param align Align
* %param color Color
* %param bgcolor Background color
*/
MWDECL void MwDrawTextEx(MwWidget handle, MwPoint* point, const char* text, int bold, int align, MwLLColor color, MwLLColor bgcolor);
/*!
* %brief Creates a pixmap from image
* %param handle Widget

View File

@@ -16,10 +16,11 @@ typedef struct _MwTextKeyValue MwTextKeyValue;
typedef struct _MwUserHandlerKeyValue MwUserHandlerKeyValue;
typedef struct _MwVoidKeyValue MwVoidKeyValue;
typedef struct _MwFont MwFont;
typedef struct _MwMenu* MwMenu;
typedef struct _MwCursor MwCursor;
typedef struct _MwMenu* MwMenu;
typedef struct _MwEntry* MwEntry;
typedef struct _MwViewport* MwViewport;
typedef struct _MwListBox* MwListBox;
typedef struct _MwSizeHints MwSizeHints;
#ifdef _MILSKO
typedef struct _MwWidget* MwWidget;
@@ -88,6 +89,7 @@ struct _MwWidget {
void* internal;
void* opaque;
void (*draw_inject)(MwWidget handle);
MwIntegerKeyValue* integer;
MwTextKeyValue* text;
@@ -118,6 +120,13 @@ struct _MwViewport {
MwWidget inframe;
};
struct _MwListBox {
MwWidget vscroll;
MwWidget frame;
char** list;
int selected;
};
struct _MwSizeHints {
int min_width;
int min_height;

View File

@@ -18,6 +18,14 @@ extern "C" {
*/
MWDECL MwClass MwListBoxClass;
/*!
* %brief Inserts item on the listbox
* %param handle Widget
* %param index Index
* %param text Text
*/
MWDECL void MwListBoxInsert(MwWidget handle, int index, const char* text);
#ifdef __cplusplus
}
#endif

View File

@@ -370,7 +370,8 @@ MwLLPixmap MwLLCreatePixmap(MwLL handle, unsigned char* data, int width, int hei
r->use_shm = XShmQueryExtension(handle->display) ? 1 : 0;
r->data = malloc(sizeof(unsigned long) * width * height);
XRenderQueryExtension(handle->display, &evbase, &erbase) ? 1 : 0;
r->use_render = XRenderQueryExtension(handle->display, &evbase, &erbase) ? 1 : 0;
r->use_render = 0;
/* FIXME */
r->use_shm = 0;
@@ -477,14 +478,14 @@ void MwLLDrawPixmap(MwLL handle, MwRect* rect, MwLLPixmap pixmap) {
for(y = 0; y < (int)rect->height; y++) {
for(x = 0; x < (int)rect->width; x++) {
double sy = (double)y / rect->height * pixmap->height;
double sx = (double)x / rect->width * pixmap->width;
int sy = y * pixmap->height / rect->height;
int sx = x * pixmap->width / rect->width;
char* ipx;
char* opx;
sy = (int)sy;
sx = (int)sx;
ipx = &pixmap->image->data[(int)(pixmap->width * sy + sx) * (pixmap->image->bitmap_unit / 8)];
ipx = &pixmap->image->data[(pixmap->width * sy + sx) * (pixmap->image->bitmap_unit / 8)];
opx = &d[(rect->width * y + x) * (pixmap->image->bitmap_unit / 8)];
memcpy(opx, ipx, pixmap->image->bitmap_unit / 8);
}

View File

@@ -9,6 +9,7 @@ static void lldrawhandler(MwLL handle, void* data) {
(void)data;
MwDispatch(h, draw);
if(h->draw_inject != NULL) h->draw_inject(h);
}
static void lluphandler(MwLL handle, void* data) {
@@ -110,6 +111,7 @@ MwWidget MwCreateWidget(MwClass widget_class, const char* name, MwWidget parent,
h->close = 0;
h->destroy_queue = NULL;
h->prop_event = 1;
h->draw_inject = NULL;
if(h->lowlevel != NULL) {
h->lowlevel->user = h;

View File

@@ -13,7 +13,6 @@
#define FontWidth 7
#define FontHeight 14
#define FontScale 1
#define ColorDiff 128
static int hex(const char* txt, int len) {
@@ -364,18 +363,24 @@ void MwDrawTriangle(MwWidget handle, MwRect* rect, MwLLColor color, int invert,
}
void MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, int align, MwLLColor color) {
int i = 0, x, y, sx, sy;
MwRect r;
sx = point->x;
sy = point->y - MwTextHeight(handle, text) / 2;
if(align == MwALIGNMENT_CENTER) {
sx -= strlen(text) * FontWidth * FontScale / 2;
} else if(align == MwALIGNMENT_END) {
sx -= strlen(text) * FontWidth * FontScale;
MwLLColor bg = MwParseColor(handle, MwGetText(handle, MwNbackground));
MwDrawTextEx(handle, point, text, bold, align, color, bg);
MwLLFreeColor(bg);
}
void MwDrawTextEx(MwWidget handle, MwPoint* point, const char* text, int bold, int align, MwLLColor color, MwLLColor bgcolor) {
int i = 0, x, y, sx, sy;
int tw = MwTextWidth(handle, text);
int th = MwTextHeight(handle, text);
unsigned char* px = malloc(tw * th * 4);
MwRect r;
MwLLPixmap p;
memset(px, 0, tw * th * 4);
sx = 0;
sy = 0;
while(text[i] != 0) {
int out;
i += MwUTF8ToUTF32(text + i, &out);
@@ -384,29 +389,50 @@ void MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, int
if(out == '\n') {
sx = 0;
sy += FontHeight * FontScale;
sy += FontHeight;
} else {
for(y = 0; y < FontHeight; y++) {
for(x = 0; x < FontWidth; x++) {
r.x = sx + x * FontScale;
r.y = sy + y * FontScale;
r.width = FontScale;
r.height = FontScale;
unsigned char* ppx = &px[((sy + y) * tw + sx + x) * 4];
if((bold ? MwBoldFontData : MwFontData)[out].data[y] & (1 << ((FontWidth - 1) - x))) {
MwDrawRect(handle, &r, color);
ppx[0] = color->red;
ppx[1] = color->green;
ppx[2] = color->blue;
ppx[3] = 255;
} else {
ppx[0] = bgcolor->red;
ppx[1] = bgcolor->green;
ppx[2] = bgcolor->blue;
ppx[3] = 255;
}
}
}
sx += FontWidth * FontScale;
sx += FontWidth;
}
}
p = MwLoadRaw(handle, px, tw, th);
r.x = point->x;
r.y = point->y - th / 2;
r.width = tw;
r.height = th;
if(align == MwALIGNMENT_CENTER) {
r.x -= tw / 2;
} else if(align == MwALIGNMENT_END) {
r.x -= tw;
}
MwLLDrawPixmap(handle->lowlevel, &r, p);
MwLLDestroyPixmap(p);
free(px);
}
int MwTextWidth(MwWidget handle, const char* text) {
(void)handle;
return strlen(text) * FontWidth * FontScale;
/* TODO: check newline */
return strlen(text) * FontWidth;
}
int MwTextHeight(MwWidget handle, const char* text) {
@@ -423,7 +449,7 @@ int MwTextHeight(MwWidget handle, const char* text) {
if(out == '\n') c++;
}
return FontHeight * FontScale * c;
return FontHeight * c;
}
#ifndef USE_STB_IMAGE

View File

@@ -1,24 +1,154 @@
/* $Id$ */
#include <Mw/Milsko.h>
static int create(MwWidget handle) {
int st;
#include "../../external/stb_ds.h"
if((st = MwViewportClass->create(handle)) != 0) return st;
static int get_first_entry(MwListBox lb) {
int st = 0;
st = MwGetInteger(lb->vscroll, MwNvalue);
st = st * (MwGetInteger(lb->vscroll, MwNmaxValue) - MwGetInteger(lb->vscroll, MwNareaShown)) / MwGetInteger(lb->vscroll, MwNmaxValue);
if(st < 0) st = 0;
return st;
}
static void vscroll_changed(MwWidget handle, void* user, void* call) {
MwListBox lb = handle->parent->internal;
MwForceRender(lb->frame);
}
static void frame_mouse_down(MwWidget handle, void* user, void* call) {
MwListBox lb = handle->parent->internal;
MwLLMouse* m = call;
int st = 0;
int i;
int y = MwDefaultBorderWidth;
int h = MwGetInteger(handle, MwNheight);
st = get_first_entry(lb);
for(i = 0; i < (h - MwDefaultBorderWidth * 2) / MwTextHeight(handle, "M"); i++) {
if(y <= m->point.y && m->point.y <= (y + MwTextHeight(handle, "M"))) {
lb->selected = st + i;
}
y += MwTextHeight(handle, "M");
}
MwForceRender(lb->frame);
}
static void frame_draw(MwWidget handle) {
MwRect r;
MwListBox lb = handle->parent->internal;
MwLLColor base = MwParseColor(handle, MwGetText(handle, MwNbackground));
MwLLColor text = MwParseColor(handle, MwGetText(handle, MwNforeground));
int i;
MwPoint p;
int st = 0;
r.x = 0;
r.y = 0;
r.width = MwGetInteger(handle, MwNwidth);
r.height = MwGetInteger(handle, MwNheight);
p.x = MwDefaultBorderWidth;
p.y = MwDefaultBorderWidth;
st = get_first_entry(lb);
for(i = st; i < arrlen(lb->list) && i < st + (r.height - MwDefaultBorderWidth * 2) / MwTextHeight(handle, "M"); i++) {
int selected = lb->selected == i ? 1 : 0;
if(selected) {
MwRect r2;
r2.x = 0;
r2.y = p.y;
r2.width = r.width;
r2.height = MwTextHeight(handle, lb->list[i]);
MwDrawRect(handle, &r2, text);
}
p.y += MwTextHeight(handle, lb->list[i]) / 2;
MwDrawTextEx(handle, &p, lb->list[i], 0, MwALIGNMENT_BEGINNING, selected ? base : text, selected ? text : base);
p.y += MwTextHeight(handle, lb->list[i]) / 2;
}
MwDrawFrame(handle, &r, base, 1);
MwLLFreeColor(text);
MwLLFreeColor(base);
}
static void resize(MwWidget handle) {
MwListBox lb = handle->internal;
int w = MwGetInteger(handle, MwNwidth);
int h = MwGetInteger(handle, MwNheight);
int ih;
if(lb->vscroll == NULL) {
lb->vscroll = MwVaCreateWidget(MwScrollBarClass, "vscroll", handle, w - 16, 0, 16, h, NULL);
MwAddUserHandler(lb->vscroll, MwNchangedHandler, vscroll_changed, NULL);
} else {
MwVaApply(lb->vscroll,
MwNx, w - 16,
MwNy, 0,
MwNwidth, 16,
MwNheight, h,
NULL);
}
if(lb->frame == NULL) {
lb->frame = MwVaCreateWidget(MwFrameClass, "frame", handle, 0, 0, w - 16, h, NULL);
lb->frame->draw_inject = frame_draw;
MwAddUserHandler(lb->frame, MwNmouseDownHandler, frame_mouse_down, NULL);
} else {
MwVaApply(lb->frame,
MwNx, 0,
MwNy, 0,
MwNwidth, w - 16,
MwNheight, h,
NULL);
}
ih = arrlen(lb->list);
MwVaApply(lb->vscroll,
MwNareaShown, h / MwTextHeight(handle, "M"),
MwNmaxValue, ih,
NULL);
}
static int create(MwWidget handle) {
MwListBox lb = malloc(sizeof(*lb));
memset(lb, 0, sizeof(*lb));
handle->internal = lb;
MwSetDefault(handle);
resize(handle);
lb->list = NULL;
lb->selected = -1;
return 0;
}
static void destroy(MwWidget handle) {
MwViewportClass->destroy(handle);
free(handle->internal);
}
static void draw(MwWidget handle) {
MwViewportClass->draw(handle);
MwRect r;
MwLLColor base = MwParseColor(handle, MwGetText(handle, MwNbackground));
r.x = 0;
r.y = 0;
r.width = MwGetInteger(handle, MwNwidth);
r.height = MwGetInteger(handle, MwNheight);
MwDrawRect(handle, &r, base);
MwLLFreeColor(base);
}
static void prop_change(MwWidget handle, const char* prop) {
MwViewportClass->prop_change(handle, prop);
if(strcmp(prop, MwNwidth) == 0 || strcmp(prop, MwNheight) == 0) resize(handle);
}
MwClassRec MwListBoxClassRec = {
@@ -38,3 +168,18 @@ MwClassRec MwListBoxClassRec = {
NULL,
NULL};
MwClass MwListBoxClass = &MwListBoxClassRec;
void MwListBoxInsert(MwWidget handle, int index, const char* text) {
char* str = malloc(strlen(text) + 1);
MwListBox lb = handle->internal;
strcpy(str, text);
if(index == -1) index = arrlen(lb->list);
arrins(lb->list, index, str);
resize(handle);
if(index < (MwGetInteger(lb->vscroll, MwNvalue) + MwGetInteger(lb->vscroll, MwNareaShown))) {
MwForceRender(lb->frame);
}
}