diff --git a/GNUmakefile b/GNUmakefile index da50be3..134c23a 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -16,7 +16,7 @@ E_LDFLAGS = $(LDFLAGS) -Lsrc E_LIBS = $(LIBS) -lMw L_OBJS = src/ds.o src/core.o src/default.o src/draw.o src/lowlevel.o src/font.o src/image.o -L_OBJS += src/window.o src/button.o src/frame.o +L_OBJS += src/window.o src/button.o src/frame.o src/menu.o ifeq ($(TARGET),NetBSD) CFLAGS += -I/usr/X11R7/include -I/usr/pkg/include diff --git a/doc/index.html b/doc/index.html index 40e4251..4910a84 100644 --- a/doc/index.html +++ b/doc/index.html @@ -113,6 +113,12 @@
MwLoadImage
+
+ MwTextWidth +
+
+ MwTextHeight +
Mw/Error.h
@@ -143,6 +149,12 @@
Mw/MachDep.h
+
+ Mw/Menu.h +
+
+ MwMenuClass +
Mw/Milsko.h
@@ -155,6 +167,9 @@
MwOpenGLMakeCurrent
+
+ MwOpenGLGetProcAddress +
MwOpenGLSwapBuffer
@@ -1109,6 +1124,62 @@
+
MWDECL int MwTextWidth (
+	MwWidget handle,
+	const char* text
+);
+
+
+ Calculates a text width. +
+
+ Parameter handle +
+
+ Widget. +
+
+ Parameter text +
+
+ Text. +
+
+ Returns +
+
+ Text width. +
+
+
+
MWDECL int MwTextHeight (
+	MwWidget handle,
+	const char* text
+);
+
+
+ Calculates a text height. +
+
+ Parameter handle +
+
+ Widget. +
+
+ Parameter text +
+
+ Text. +
+
+ Returns +
+
+ Text height. +
+
+

Mw/Error.h

@@ -1185,6 +1256,26 @@

Mw/MachDep.h

+
+
+ Machine dependent headers and macros. +
+
+
+

Mw/Menu.h

+
+
+ Menu widget. +
+
+
+
MWDECL MwClass MwMenuClass;
+
+
+ Menu widget class. +
+
+

Mw/Milsko.h

@@ -1221,6 +1312,34 @@

+
MWDECL void* MwOpenGLGetProcAddress (
+	MwWidget handle,
+	const char* name
+);
+
+
+ Get a procedure from OpenGL. +
+
+ Parameter handle +
+
+ Widget. +
+
+ Parameter name +
+
+ Name. +
+
+ Returns +
+
+ Procedure. +
+
+
MWDECL void MwOpenGLSwapBuffer (
 	MwWidget handle
 );
diff --git a/examples/example.c b/examples/example.c index f17ebfa..5ad0652 100644 --- a/examples/example.c +++ b/examples/example.c @@ -1,7 +1,7 @@ /* $Id$ */ #include -MwWidget window, button, button2, button3, button4; +MwWidget window, menu, button, button2, button3, button4; void handler(MwWidget handle, void* user_data, void* call_data) { (void)handle; @@ -12,34 +12,35 @@ void handler(MwWidget handle, void* user_data, void* call_data) { } void resize(MwWidget handle, void* user_data, void* call_data) { - unsigned int w, h; + unsigned int w, h, mh; (void)user_data; (void)call_data; w = MwGetInteger(handle, MwNwidth); - h = MwGetInteger(handle, MwNheight); + h = MwGetInteger(handle, MwNheight) - (mh = MwGetInteger(menu, MwNheight)); MwVaApply(button, + MwNy, 50 + mh, MwNwidth, w - 50 * 2, MwNheight, h - 125 - 50 * 3, NULL); MwVaApply(button2, MwNx, 50 + (w - 50 * 2) / 3 * 0, - MwNy, h - 50 - 125, + MwNy, h - 50 - 125 + mh, MwNwidth, (w - 50 * 2) / 3, NULL); MwVaApply(button3, MwNx, 50 + (w - 50 * 2) / 3 * 1, - MwNy, h - 50 - 125, + MwNy, h - 50 - 125 + mh, MwNwidth, (w - 50 * 2) / 3, NULL); MwVaApply(button4, MwNx, 50 + (w - 50 * 2) / 3 * 2, - MwNy, h - 50 - 125, + MwNy, h - 50 - 125 + mh, MwNwidth, (w - 50 * 2) / 3, NULL); } @@ -48,6 +49,7 @@ int main() { window = MwVaCreateWidget(MwWindowClass, "main", NULL, 0, 0, 400, 400, MwNtitle, "hello world", NULL); + menu = MwCreateWidget(MwMenuClass, "menu", window, 0, 0, 0, 0); button = MwVaCreateWidget(MwButtonClass, "button", window, 50, 50, 300, 125, MwNtext, "lorem ipsum", NULL); diff --git a/include/Mw/Draw.h b/include/Mw/Draw.h index d191559..f620fbd 100644 --- a/include/Mw/Draw.h +++ b/include/Mw/Draw.h @@ -68,6 +68,22 @@ MWDECL void MwDrawText(MwWidget handle, MwPoint* point, const char* text, MwLLCo */ MWDECL MwLLPixmap MwLoadImage(MwWidget handle, const char* path); +/*! + * %brief Calculates a text width + * %param handle Widget + * %param text Text + * %return Text width + */ +MWDECL int MwTextWidth(MwWidget handle, const char* text); + +/*! + * %brief Calculates a text height + * %param handle Widget + * %param text Text + * %return Text height + */ +MWDECL int MwTextHeight(MwWidget handle, const char* text); + #ifdef __cplusplus } #endif diff --git a/include/Mw/MachDep.h b/include/Mw/MachDep.h index f6e19cf..dbb4134 100644 --- a/include/Mw/MachDep.h +++ b/include/Mw/MachDep.h @@ -1,7 +1,7 @@ /* $Id$ */ /*! * %file Mw/MachDep.h - * %param Machine dependent headers and macros + * %brief Machine dependent headers and macros */ #ifndef __MW_MACHDEP_H__ #define __MW_MACHDEP_H__ diff --git a/include/Mw/Menu.h b/include/Mw/Menu.h new file mode 100644 index 0000000..a792fc8 --- /dev/null +++ b/include/Mw/Menu.h @@ -0,0 +1,25 @@ +/* $Id$ */ +/*! + * %file Mw/Menu.h + * %brief Menu widget + */ +#ifndef __MW_MENU_H__ +#define __MW_MENU_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/*! + * %brief Menu widget class + */ +MWDECL MwClass MwMenuClass; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/Mw/Milsko.h b/include/Mw/Milsko.h index 71096be..83ba060 100644 --- a/include/Mw/Milsko.h +++ b/include/Mw/Milsko.h @@ -17,6 +17,7 @@ #include #include +#include #include #include diff --git a/src/draw.c b/src/draw.c index 1e216c6..ce388d0 100644 --- a/src/draw.c +++ b/src/draw.c @@ -3,6 +3,10 @@ #include "stb_image.h" +#define FontWidth 7 +#define FontHeight 14 +#define FontScale 1 + static int hex(const char* txt, int len) { int i; int r = 0; @@ -124,30 +128,42 @@ void MwDrawFrameEx(MwWidget handle, MwRect* rect, MwLLColor color, int invert, i } void MwDrawText(MwWidget handle, MwPoint* point, const char* text, MwLLColor color) { - int i, x, y, sx, sy, sc = 1; - int fw = 7, fh = 14; + int i, x, y, sx, sy; MwRect r; - sx = point->x - strlen(text) * fw * sc / 2; - sy = point->y - fh * sc / 2; + sx = point->x - strlen(text) * FontWidth * FontScale / 2; + sy = point->y - FontHeight * FontScale / 2; for(i = 0; text[i] != 0; i++) { - for(y = 0; y < fh; y++) { - for(x = 0; x < fw; x++) { - r.x = sx + x * sc; - r.y = sy + y * sc; - r.width = sc; - r.height = sc; + 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; - if(MwFontData[(unsigned char)text[i]].data[y] & (1 << ((fw - 1) - x))) { + if(MwFontData[(unsigned char)text[i]].data[y] & (1 << ((FontWidth - 1) - x))) { MwDrawRect(handle, &r, color); } } } - sx += fw * sc; + sx += FontWidth * FontScale; } } +int MwTextWidth(MwWidget handle, const char* text) { + (void)handle; + + return strlen(text) * FontWidth * FontScale; +} + +int MwTextHeight(MwWidget handle, const char* text) { + (void)handle; + (void)text; + + return FontHeight * FontScale; +} + MwLLPixmap MwLoadImage(MwWidget handle, const char* path) { int width, height, ch; unsigned char* rgb = stbi_load(path, &width, &height, &ch, 4); diff --git a/src/menu.c b/src/menu.c new file mode 100644 index 0000000..f51b7aa --- /dev/null +++ b/src/menu.c @@ -0,0 +1,42 @@ +/* $Id$ */ +#include + +static void set_xywh(MwWidget handle) { + int height = 0; + + height += 20; + + MwVaApply(handle, + MwNx, 0, + MwNy, 0, + MwNwidth, MwGetInteger(handle->parent, MwNwidth), + MwNheight, height, + NULL); +} + +static void create(MwWidget handle) { + MwSetDefault(handle); + + set_xywh(handle); +} + +static void draw(MwWidget 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); + + MwDrawFrame(handle, &r, base, 0); + MwDrawRect(handle, &r, base); +} + +MwClassRec MwMenuClassRec = { + create, /* create */ + NULL, /* destroy */ + draw, /* draw */ + NULL /* click */ +}; +MwClass MwMenuClass = &MwMenuClassRec; diff --git a/src/x11.c b/src/x11.c index 1914181..83746c9 100644 --- a/src/x11.c +++ b/src/x11.c @@ -27,6 +27,9 @@ MwLL MwLLCreate(MwLL parent, int x, int y, int width, int height) { MwLLCreateCommon(r); + if(width < 1) width = 1; + if(height < 1) height = 1; + if(parent == NULL) { r->display = XOpenDisplay(NULL); p = XRootWindow(r->display, XDefaultScreen(r->display)); @@ -115,6 +118,9 @@ void MwLLSetXY(MwLL handle, int x, int y) { } void MwLLSetWH(MwLL handle, int w, int h) { + if(w < 1) w = 1; + if(h < 1) h = 1; + XResizeWindow(handle->display, handle->window, w, h); }