fix pixmap leak
git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@82 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
parent
e74b80da50
commit
8ea2d4a50a
15 changed files with 9440 additions and 1337 deletions
30
src/button.c
30
src/button.c
|
|
@ -11,6 +11,7 @@ static void draw(MwWidget handle) {
|
|||
MwLLColor base = MwParseColor(handle, MwGetText(handle, MwNbackground));
|
||||
MwLLColor text = MwParseColor(handle, MwGetText(handle, MwNforeground));
|
||||
const char* str = MwGetText(handle, MwNtext);
|
||||
MwLLPixmap px = MwGetVoid(handle, MwNpixmap);
|
||||
|
||||
if(str == NULL) str = "";
|
||||
|
||||
|
|
@ -22,10 +23,33 @@ static void draw(MwWidget handle) {
|
|||
MwDrawFrame(handle, &r, base, handle->pressed);
|
||||
MwDrawRect(handle, &r, base);
|
||||
|
||||
point.x = r.x + r.width / 2;
|
||||
point.y = r.x + r.height / 2;
|
||||
if(px != NULL) {
|
||||
int ow = r.width;
|
||||
int oh = r.height;
|
||||
|
||||
MwDrawText(handle, &point, str, text);
|
||||
double sw = (double)ow / px->width;
|
||||
double sh = (double)oh / px->height;
|
||||
|
||||
if(sw < sh) {
|
||||
r.width = px->width * sw;
|
||||
r.height = px->height * sw;
|
||||
} else {
|
||||
r.width = px->width * sh;
|
||||
r.height = px->height * sh;
|
||||
}
|
||||
r.width -= 10;
|
||||
r.height -= 10;
|
||||
|
||||
r.x += (double)(ow - r.width) / 2;
|
||||
r.y += (double)(oh - r.height) / 2;
|
||||
|
||||
MwLLDrawPixmap(handle->lowlevel, &r, px);
|
||||
} else {
|
||||
point.x = r.x + r.width / 2;
|
||||
point.y = r.x + r.height / 2;
|
||||
|
||||
MwDrawText(handle, &point, str, text);
|
||||
}
|
||||
|
||||
MwLLFreeColor(text);
|
||||
MwLLFreeColor(base);
|
||||
|
|
|
|||
14
src/core.c
14
src/core.c
|
|
@ -57,10 +57,12 @@ MwWidget MwCreateWidget(MwClass widget_class, const char* name, MwWidget parent,
|
|||
sh_new_strdup(h->text);
|
||||
sh_new_strdup(h->integer);
|
||||
sh_new_strdup(h->handler);
|
||||
sh_new_strdup(h->data);
|
||||
|
||||
shdefault(h->integer, -1);
|
||||
shdefault(h->text, NULL);
|
||||
shdefault(h->handler, NULL);
|
||||
shdefault(h->data, NULL);
|
||||
|
||||
MwDispatch(h, create);
|
||||
|
||||
|
|
@ -122,6 +124,7 @@ void MwDestroyWidget(MwWidget handle) {
|
|||
}
|
||||
shfree(handle->text);
|
||||
shfree(handle->handler);
|
||||
shfree(handle->data);
|
||||
|
||||
free(handle);
|
||||
}
|
||||
|
|
@ -182,6 +185,10 @@ void MwSetText(MwWidget handle, const char* key, const char* value) {
|
|||
}
|
||||
}
|
||||
|
||||
void MwSetVoid(MwWidget handle, const char* key, void* value) {
|
||||
shput(handle->data, key, value);
|
||||
}
|
||||
|
||||
int MwGetInteger(MwWidget handle, const char* key) {
|
||||
if(strcmp(key, MwNx) == 0 || strcmp(key, MwNy) == 0 || strcmp(key, MwNwidth) == 0 || strcmp(key, MwNheight) == 0) {
|
||||
int x, y;
|
||||
|
|
@ -203,6 +210,10 @@ const char* MwGetText(MwWidget handle, const char* key) {
|
|||
return shget(handle->text, key);
|
||||
}
|
||||
|
||||
void* MwGetVoid(MwWidget handle, const char* key) {
|
||||
return shget(handle->data, key);
|
||||
}
|
||||
|
||||
void MwVaApply(MwWidget handle, ...) {
|
||||
va_list va;
|
||||
|
||||
|
|
@ -228,6 +239,9 @@ void MwVaListApply(MwWidget handle, va_list va) {
|
|||
shput(handle->handler, key, h);
|
||||
ind = shgeti(handle->handler, key);
|
||||
handle->handler[ind].user_data = NULL;
|
||||
} else if(key[0] == 'V') {
|
||||
void* v = va_arg(va, void*);
|
||||
MwSetVoid(handle, key, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
16
src/draw.c
16
src/draw.c
|
|
@ -1,6 +1,8 @@
|
|||
/* $Id$ */
|
||||
#include <Mw/Milsko.h>
|
||||
|
||||
#include <stb_image.h>
|
||||
|
||||
static int hex(const char* txt, int len) {
|
||||
int i;
|
||||
int r = 0;
|
||||
|
|
@ -145,3 +147,17 @@ void MwDrawText(MwWidget handle, MwPoint* point, const char* text, MwLLColor col
|
|||
sx += fw * sc;
|
||||
}
|
||||
}
|
||||
|
||||
MwLLPixmap MwLoadImage(MwWidget handle, const char* path) {
|
||||
int width, height, ch;
|
||||
unsigned char* rgb = stbi_load(path, &width, &height, &ch, 3);
|
||||
MwLLPixmap px;
|
||||
|
||||
if(rgb == NULL) return NULL;
|
||||
|
||||
px = MwLLCreatePixmap(handle->lowlevel, rgb, width, height);
|
||||
|
||||
free(rgb);
|
||||
|
||||
return px;
|
||||
}
|
||||
|
|
|
|||
3
src/image.c
Normal file
3
src/image.c
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
/* $Id$ */
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
2443
src/stb_ds.h
generated
2443
src/stb_ds.h
generated
File diff suppressed because it is too large
Load diff
7988
src/stb_image.h
Normal file
7988
src/stb_image.h
Normal file
File diff suppressed because it is too large
Load diff
56
src/x11.c
56
src/x11.c
|
|
@ -3,6 +3,22 @@
|
|||
|
||||
static unsigned long mask = ExposureMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask;
|
||||
|
||||
static void create_pixmap(MwLL handle) {
|
||||
XWindowAttributes attr;
|
||||
int x, y;
|
||||
unsigned int w, h;
|
||||
|
||||
MwLLGetXYWH(handle, &x, &y, &w, &h);
|
||||
|
||||
XGetWindowAttributes(handle->display, handle->window, &attr);
|
||||
|
||||
handle->pixmap = XCreatePixmap(handle->display, handle->window, w, h, attr.depth);
|
||||
}
|
||||
|
||||
static void destroy_pixmap(MwLL handle) {
|
||||
XFreePixmap(handle->display, handle->pixmap);
|
||||
}
|
||||
|
||||
MwLL MwLLCreate(MwLL parent, int x, int y, int width, int height) {
|
||||
MwLL r;
|
||||
Window p;
|
||||
|
|
@ -18,13 +34,19 @@ MwLL MwLLCreate(MwLL parent, int x, int y, int width, int height) {
|
|||
r->display = parent->display;
|
||||
p = parent->window;
|
||||
}
|
||||
r->window = XCreateSimpleWindow(r->display, p, x, y, width, height, 0, 0, WhitePixel(r->display, XDefaultScreen(r->display)));
|
||||
r->window = XCreateSimpleWindow(r->display, p, x, y, width, height, 0, 0, WhitePixel(r->display, XDefaultScreen(r->display)));
|
||||
|
||||
r->width = width;
|
||||
r->height = height;
|
||||
|
||||
r->colormap = DefaultColormap(r->display, XDefaultScreen(r->display));
|
||||
r->wm_delete = XInternAtom(r->display, "WM_DELETE_WINDOW", False);
|
||||
XSetWMProtocols(r->display, r->window, &r->wm_delete, 1);
|
||||
|
||||
r->gc = XCreateGC(r->display, r->window, 0, 0);
|
||||
|
||||
create_pixmap(r);
|
||||
|
||||
XSetGraphicsExposures(r->display, r->gc, False);
|
||||
|
||||
XSelectInput(r->display, r->window, mask);
|
||||
|
|
@ -36,6 +58,7 @@ MwLL MwLLCreate(MwLL parent, int x, int y, int width, int height) {
|
|||
void MwLLDestroy(MwLL handle) {
|
||||
MwLLDestroyCommon(handle);
|
||||
|
||||
destroy_pixmap(handle);
|
||||
XFreeGC(handle->display, handle->gc);
|
||||
XDestroyWindow(handle->display, handle->window);
|
||||
free(handle);
|
||||
|
|
@ -51,7 +74,7 @@ void MwLLPolygon(MwLL handle, MwPoint* points, int points_count, MwLLColor color
|
|||
p[i].x = points[i].x;
|
||||
p[i].y = points[i].y;
|
||||
}
|
||||
XFillPolygon(handle->display, handle->window, handle->gc, p, points_count, Convex, CoordModeOrigin);
|
||||
XFillPolygon(handle->display, handle->pixmap, handle->gc, p, points_count, Convex, CoordModeOrigin);
|
||||
|
||||
free(p);
|
||||
}
|
||||
|
|
@ -110,25 +133,43 @@ int MwLLPending(MwLL handle) {
|
|||
void MwLLNextEvent(MwLL handle) {
|
||||
XEvent ev;
|
||||
while(XCheckTypedWindowEvent(handle->display, handle->window, ClientMessage, &ev) || XCheckWindowEvent(handle->display, handle->window, mask, &ev)) {
|
||||
int render = 0;
|
||||
if(ev.type == Expose) {
|
||||
MwLLDispatch(handle, draw);
|
||||
render = 1;
|
||||
} else if(ev.type == ButtonPress) {
|
||||
if(ev.xbutton.button == Button1) {
|
||||
MwLLDispatch(handle, down);
|
||||
MwLLDispatch(handle, draw);
|
||||
render = 1;
|
||||
}
|
||||
} else if(ev.type == ButtonRelease) {
|
||||
if(ev.xbutton.button == Button1) {
|
||||
MwLLDispatch(handle, up);
|
||||
MwLLDispatch(handle, draw);
|
||||
render = 1;
|
||||
}
|
||||
} else if(ev.type == ConfigureNotify) {
|
||||
MwLLDispatch(handle, resize);
|
||||
|
||||
if(handle->width != (unsigned int)ev.xconfigure.width || handle->height != (unsigned int)ev.xconfigure.height) {
|
||||
destroy_pixmap(handle);
|
||||
create_pixmap(handle);
|
||||
render = 1;
|
||||
}
|
||||
handle->width = ev.xconfigure.width;
|
||||
handle->height = ev.xconfigure.height;
|
||||
} else if(ev.type == ClientMessage) {
|
||||
if(ev.xclient.data.l[0] == (long)handle->wm_delete) {
|
||||
MwLLDispatch(handle, close);
|
||||
}
|
||||
}
|
||||
if(render) {
|
||||
int x, y;
|
||||
unsigned int w, h;
|
||||
|
||||
MwLLGetXYWH(handle, &x, &y, &w, &h);
|
||||
|
||||
MwLLDispatch(handle, draw);
|
||||
XCopyArea(handle->display, handle->pixmap, handle->window, handle->gc, 0, 0, w, h, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -232,11 +273,14 @@ void MwLLDrawPixmap(MwLL handle, MwRect* rect, MwLLPixmap pixmap) {
|
|||
}
|
||||
|
||||
src = XRenderCreatePicture(handle->display, px, format, 0, &attr);
|
||||
dest = XRenderCreatePicture(handle->display, handle->window, format, 0, &attr);
|
||||
dest = XRenderCreatePicture(handle->display, handle->pixmap, format, 0, &attr);
|
||||
|
||||
XRenderSetPictureTransform(handle->display, src, &m);
|
||||
XRenderComposite(handle->display, PictOpSrc, src, 0, dest, 0, 0, 0, 0, rect->x, rect->y, rect->width, rect->height);
|
||||
|
||||
XRenderFreePicture(handle->display, src);
|
||||
XRenderFreePicture(handle->display, dest);
|
||||
|
||||
XFreePixmap(handle->display, px);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue