git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@105 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
NishiOwO
2025-10-01 15:55:53 +00:00
parent e2cfadea90
commit 5d7d47907a
10 changed files with 247 additions and 20 deletions

View File

@@ -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

View File

@@ -113,6 +113,12 @@
<dd>
<a href="#Mw_Draw_h__MwLoadImage">MwLoadImage</a>
</dd>
<dd>
<a href="#Mw_Draw_h__MwTextWidth">MwTextWidth</a>
</dd>
<dd>
<a href="#Mw_Draw_h__MwTextHeight">MwTextHeight</a>
</dd>
<dt>
<a href="#Mw_Error_h">Mw/Error.h</a>
</dt>
@@ -143,6 +149,12 @@
<dt>
<a href="#Mw_MachDep_h">Mw/MachDep.h</a>
</dt>
<dt>
<a href="#Mw_Menu_h">Mw/Menu.h</a>
</dt>
<dd>
<a href="#Mw_Menu_h__MwMenuClass">MwMenuClass</a>
</dd>
<dt>
<a href="#Mw_Milsko_h">Mw/Milsko.h</a>
</dt>
@@ -155,6 +167,9 @@
<dd>
<a href="#Mw_OpenGL_h__MwOpenGLMakeCurrent">MwOpenGLMakeCurrent</a>
</dd>
<dd>
<a href="#Mw_OpenGL_h__MwOpenGLGetProcAddress">MwOpenGLGetProcAddress</a>
</dd>
<dd>
<a href="#Mw_OpenGL_h__MwOpenGLSwapBuffer">MwOpenGLSwapBuffer</a>
</dd>
@@ -1109,6 +1124,62 @@
</dd>
</dl>
<hr>
<pre id="Mw_Draw_h__MwTextWidth"><code>MWDECL int MwTextWidth (
MwWidget handle,
const char* text
);</code></pre>
<dl>
<dd>
Calculates a text width.
</dd>
<dt>
Parameter <code>handle</code>
</dt>
<dd>
Widget.
</dd>
<dt>
Parameter <code>text</code>
</dt>
<dd>
Text.
</dd>
<dt>
Returns
</dt>
<dd>
Text width.
</dd>
</dl>
<hr>
<pre id="Mw_Draw_h__MwTextHeight"><code>MWDECL int MwTextHeight (
MwWidget handle,
const char* text
);</code></pre>
<dl>
<dd>
Calculates a text height.
</dd>
<dt>
Parameter <code>handle</code>
</dt>
<dd>
Widget.
</dd>
<dt>
Parameter <code>text</code>
</dt>
<dd>
Text.
</dd>
<dt>
Returns
</dt>
<dd>
Text height.
</dd>
</dl>
<hr>
<h2 align="center" id="Mw_Error_h">Mw/Error.h</h2>
<dl>
<dt>
@@ -1185,6 +1256,26 @@
</dl>
<hr>
<h2 align="center" id="Mw_MachDep_h">Mw/MachDep.h</h2>
<dl>
<dt>
Machine dependent headers and macros.
</dt>
</dl>
<hr>
<h2 align="center" id="Mw_Menu_h">Mw/Menu.h</h2>
<dl>
<dt>
Menu widget.
</dt>
</dl>
<hr>
<pre id="Mw_Menu_h__MwMenuClass"><code>MWDECL MwClass MwMenuClass;</code></pre>
<dl>
<dd>
Menu widget class.
</dd>
</dl>
<hr>
<h2 align="center" id="Mw_Milsko_h">Mw/Milsko.h</h2>
<dl>
<dt>
@@ -1221,6 +1312,34 @@
</dd>
</dl>
<hr>
<pre id="Mw_OpenGL_h__MwOpenGLGetProcAddress"><code>MWDECL void* MwOpenGLGetProcAddress (
MwWidget handle,
const char* name
);</code></pre>
<dl>
<dd>
Get a procedure from OpenGL.
</dd>
<dt>
Parameter <code>handle</code>
</dt>
<dd>
Widget.
</dd>
<dt>
Parameter <code>name</code>
</dt>
<dd>
Name.
</dd>
<dt>
Returns
</dt>
<dd>
Procedure.
</dd>
</dl>
<hr>
<pre id="Mw_OpenGL_h__MwOpenGLSwapBuffer"><code>MWDECL void MwOpenGLSwapBuffer (
MwWidget handle
);</code></pre>

View File

@@ -1,7 +1,7 @@
/* $Id$ */
#include <Mw/Milsko.h>
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);

View File

@@ -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

View File

@@ -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__

25
include/Mw/Menu.h Normal file
View File

@@ -0,0 +1,25 @@
/* $Id$ */
/*!
* %file Mw/Menu.h
* %brief Menu widget
*/
#ifndef __MW_MENU_H__
#define __MW_MENU_H__
#include <Mw/MachDep.h>
#include <Mw/TypeDefs.h>
#ifdef __cplusplus
extern "C" {
#endif
/*!
* %brief Menu widget class
*/
MWDECL MwClass MwMenuClass;
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -17,6 +17,7 @@
#include <Mw/Error.h>
#include <Mw/Window.h>
#include <Mw/Menu.h>
#include <Mw/Button.h>
#include <Mw/Frame.h>

View File

@@ -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);

42
src/menu.c Normal file
View File

@@ -0,0 +1,42 @@
/* $Id$ */
#include <Mw/Milsko.h>
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;

View File

@@ -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);
}