MwNdocumentActivateHandler
Some checks failed
pyrite-dev/milsko/pipeline/head There was a failure building this commit

This commit is contained in:
Nishi 2026-09-17 02:03:09 +09:00
commit 8d48075f50
9 changed files with 322 additions and 26 deletions

View file

@ -30,14 +30,25 @@ static void MWAPI layout_document(MwWidget handle, void* user, void* call) {
NULL); NULL);
} }
static void MWAPI activate_document(MwWidget handle, void* user, void* call) {
if(strcmp((const char*)call, "you_clicked") == 0) {
MwVaApply(document,
MwNtext, "# You clicked the thing!",
NULL);
}
}
int main() { int main() {
char* text = malloc(64 * 1024); char* text = malloc(64 * 1024);
int i; int i;
strcpy(text, "# Hello world!\nThis is a __test__ text...\n\n`Some monospace here`\n\n~~Removed~~"); strcpy(text, "# Hello world!\nThis is a __test__ text...\n\n");
strcat(text, "\n```\nint main(){\n printf(\"Hello, world!\\n\");\n}\n```"); strcat(text, "`Some monospace here`\n\n");
strcat(text, "~~Removed~~\n");
strcat(text, "```\nint main(){\n printf(\"Hello, world!\\n\");\n}\n```\n");
strcat(text, "[Click me!](you_clicked)\n\n");
for(i = 0; i < 10; i++) { for(i = 0; i < 10; i++) {
strcat(text, "\n\n**Lorem ipsum** dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."); strcat(text, "**Lorem ipsum** dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\n");
} }
MwLibraryInit(); MwLibraryInit();
@ -54,6 +65,7 @@ int main() {
MwAddUserHandler(window, MwNresizeHandler, resize_window, NULL); MwAddUserHandler(window, MwNresizeHandler, resize_window, NULL);
MwAddUserHandler(document, MwNlayoutHandler, layout_document, NULL); MwAddUserHandler(document, MwNlayoutHandler, layout_document, NULL);
MwAddUserHandler(document, MwNdocumentActivateHandler, activate_document, NULL);
resize_window(window, NULL, NULL); resize_window(window, NULL, NULL);

View file

@ -130,6 +130,7 @@ enum MwDOCUMENT_LAYOUT {
MwDOCUMENT_MONOSPACE, MwDOCUMENT_MONOSPACE,
MwDOCUMENT_STRIKETHROUGH, MwDOCUMENT_STRIKETHROUGH,
MwDOCUMENT_UNDERLINE, MwDOCUMENT_UNDERLINE,
MwDOCUMENT_CLICKABLE
}; };
enum MwMOUSE { enum MwMOUSE {

View file

@ -63,6 +63,26 @@ MWDECL MwCursor MwCursorHidden;
*/ */
MWDECL MwCursor MwCursorHiddenMask; MWDECL MwCursor MwCursorHiddenMask;
/*!
* @brief Hand cursor
*/
MWDECL MwCursor MwCursorHand;
/*!
* @brief Hand cursor mask
*/
MWDECL MwCursor MwCursorHandMask;
/*!
* @brief Center cursor
*/
MWDECL MwCursor MwCursorCenter;
/*!
* @brief Center cursor mask
*/
MWDECL MwCursor MwCursorCenterMask;
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -84,6 +84,7 @@
#define MwNactivateHandler "Cactivate" #define MwNactivateHandler "Cactivate"
#define MwNlistBoxActivateHandler "ClistBoxActivate" #define MwNlistBoxActivateHandler "ClistBoxActivate"
#define MwNtreeViewActivateHandler "CtreeViewActivate" #define MwNtreeViewActivateHandler "CtreeViewActivate"
#define MwNdocumentActivateHandler "CdocumentActivate"
#define MwNresizeHandler "Cresize" #define MwNresizeHandler "Cresize"
#define MwNtickHandler "Ctick" #define MwNtickHandler "Ctick"
#define MwNmenuHandler "Cmenu" #define MwNmenuHandler "Cmenu"

View file

@ -33,6 +33,7 @@ typedef struct _MwTable* MwTable;
typedef struct _MwChartEntry MwChartEntry; typedef struct _MwChartEntry MwChartEntry;
typedef struct _MwChart* MwChart; typedef struct _MwChart* MwChart;
typedef struct _MwDocumentLayout MwDocumentLayout; typedef struct _MwDocumentLayout MwDocumentLayout;
typedef struct _MwDocumentClickable MwDocumentClickable;
typedef struct _MwDocument* MwDocument; typedef struct _MwDocument* MwDocument;
typedef struct _MwMouse MwMouse; typedef struct _MwMouse MwMouse;
typedef struct _MwTTFInfo* MwTTFInfo; typedef struct _MwTTFInfo* MwTTFInfo;
@ -278,9 +279,17 @@ struct _MwDocumentLayout {
MwFLFont font; MwFLFont font;
}; };
struct _MwDocumentClickable {
MwRect hitbox;
char* event;
};
struct _MwDocument { struct _MwDocument {
MwDocumentLayout* layouts; MwDocumentLayout* layouts;
MwFLFont headers[6]; MwDocumentClickable* clickables;
MwFLFont headers[6];
int center_cursor;
}; };
struct _MwTTFInfo { struct _MwTTFInfo {

View file

@ -154,6 +154,9 @@
<handler name="treeViewActivate"> <handler name="treeViewActivate">
<pointer /> <pointer />
</handler> </handler>
<handler name="documentActivate">
<string />
</handler>
<handler name="resize" common="yes" /> <handler name="resize" common="yes" />
<handler name="tick" common="yes" /> <handler name="tick" common="yes" />
<handler name="menu"> <handler name="menu">
@ -710,6 +713,7 @@
</properties> </properties>
<handlers> <handlers>
<handler name="layout" /> <handler name="layout" />
<handler name="documentActivate" />
</handlers> </handlers>
</widget> </widget>
<widget name="Entry"> <widget name="Entry">

43
src/cursor/center.c Normal file
View file

@ -0,0 +1,43 @@
#include <Mw/Milsko.h>
/**
* Created by bitmaptobdf
*
* Copyright notice:
* These ""glyphs"" are unencumbered
*/
MwCursor MwCursorCenter = {
10, 14, -4, -14, {48, /* ....##.... */
48, /* ....##.... */
120, /* ...####... */
120, /* ...####... */
252, /* ..######.. */
252, /* ..######.. */
510, /* .########. */
510, /* .########. */
819, /* ##..##..## */
561, /* #...##...# */
48, /* ....##.... */
48, /* ....##.... */
48, /* ....##.... */
48, /* ....##.... */
0, 0}};
MwCursor MwCursorCenterMask = {
12, 16, -5, -15, {
240, /* ....####.... */
240, /* ....####.... */
504, /* ...######... */
504, /* ...######... */
1020, /* ..########.. */
1020, /* ..########.. */
2046, /* .##########. */
2046, /* .##########. */
4095, /* ############ */
4095, /* ############ */
4095, /* ############ */
3831, /* ###.####.### */
240, /* ....####.... */
240, /* ....####.... */
240, /* ....####.... */
240 /* ....####.... */
}};

46
src/cursor/hand.c Normal file
View file

@ -0,0 +1,46 @@
#include <Mw/Milsko.h>
/**
* Created by bitmaptobdf
*
* Copyright notice:
* These ""glyphs"" are unencumbered
*/
MwCursor MwCursorHand = {
13, 16, -12, -16, {
3, /* ...........## */
15, /* .........#### */
60, /* .......####.. */
120, /* ......####... */
240, /* .....####.... */
504, /* ....######... */
1020, /* ...########.. */
3064, /* .#.#######... */
8188, /* ###########.. */
6140, /* #.#########.. */
504, /* ....######... */
496, /* ....#####.... */
4736, /* #..#.#....... */
6272, /* ##...#....... */
3328, /* .##.#........ */
1536 /* ..##......... */
}};
MwCursor MwCursorHandMask = {
13, 16, -12, -16, {
7, /* ..........### */
31, /* ........##### */
126, /* ......######. */
252, /* .....######.. */
504, /* ....######... */
1020, /* ...########.. */
4094, /* .###########. */
8190, /* ############. */
8190, /* ############. */
8190, /* ############. */
8188, /* ###########.. */
8184, /* ##########... */
8176, /* #########.... */
8128, /* #######...... */
8064, /* ######....... */
3840 /* .####........ */
}};

View file

@ -3,7 +3,7 @@
#include "../../external/md4c.h" #include "../../external/md4c.h"
#include "../../external/stb_ds.h" #include "../../external/stb_ds.h"
#define IsFontChange(x) ((x) == MwDOCUMENT_HEADER || (x) == MwDOCUMENT_BOLD || (x) == MwDOCUMENT_MONOSPACE || (x) == MwDOCUMENT_STRIKETHROUGH) #define IsFontChange(x) ((x) == MwDOCUMENT_HEADER || (x) == MwDOCUMENT_BOLD || (x) == MwDOCUMENT_MONOSPACE)
static void reset(MwWidget handle) { static void reset(MwWidget handle) {
MwDocument d = handle->internal; MwDocument d = handle->internal;
@ -13,6 +13,11 @@ static void reset(MwWidget handle) {
if(d->layouts[i].text != NULL) free(d->layouts[i].text); if(d->layouts[i].text != NULL) free(d->layouts[i].text);
} }
arrfree(d->layouts); arrfree(d->layouts);
for(i = 0; i < arrlen(d->clickables); i++) {
if(d->clickables[i].event != NULL) free(d->clickables[i].event);
}
arrfree(d->clickables);
} }
static int wcreate(MwWidget handle) { static int wcreate(MwWidget handle) {
@ -52,11 +57,11 @@ static void draw(MwWidget handle) {
MwRect r; MwRect r;
int i; int i;
MwFLFont font = NULL; MwFLFont font = NULL;
MwPoint u_p, s_p; MwPoint u_p, s_p, c_p;
int u = 0, s = 0; int u = 0, s = 0, c = 0;
u_p.x = u_p.y = 0; u_p.x = s_p.x = c_p.x = 0;
s_p.x = s_p.y = 0; u_p.y = s_p.y = c_p.y = 0;
r.x = 0; r.x = 0;
r.y = 0; r.y = 0;
@ -75,9 +80,7 @@ static void draw(MwWidget handle) {
p.x = l->x; p.x = l->x;
p.y = l->y + MwTextHeight(handle, font, l->text) / 2; p.y = l->y + MwTextHeight(handle, font, l->text) / 2;
MwDrawText(handle, font, &p, l->text, MwALIGNMENT_BEGINNING, text); if(u || s || c) {
if(u || s) {
MwPoint line[2]; MwPoint line[2];
int j; int j;
@ -85,31 +88,74 @@ static void draw(MwWidget handle) {
u_p.x = 0; u_p.x = 0;
u_p.y = l->y; u_p.y = l->y;
} }
if(s_p.y != l->y) {
s_p.x = 0;
s_p.y = l->y;
}
if(c_p.y != l->y) {
c_p.x = 0;
c_p.y = l->y;
}
line[0] = u_p; for(j = 0; j < 3; j++) {
line[1] = u_p; if(j == 0) {
if(!u) continue;
line[0] = u_p;
line[1] = u_p;
} else if(j == 1) {
if(!s) continue;
line[0] = s_p;
line[1] = s_p;
} else if(j == 2) {
if(!c) continue;
line[0] = c_p;
line[1] = c_p;
}
line[1].x = p.x + MwTextWidth(handle, font, l->text); line[1].x = p.x + MwTextWidth(handle, font, l->text);
u_p.x = line[1].x;
for(j = 0; j < 2; j++) { if(j == 0) u_p.x = line[1].x;
line[0].y += MwTextHeight(handle, font, l->text) / 2; if(j == 1) s_p.x = line[1].x;
line[1].y += MwTextHeight(handle, font, l->text) / 2; if(j == 2) c_p.x = line[1].x;
if((j == 0 && s) || (j == 1 && u)) MwLLLine(handle->lowlevel, line, text);
if(j == 2) {
MwRect c_r;
c_r.x = line[0].x;
c_r.y = line[0].y;
c_r.width = line[1].x - line[0].x;
c_r.height = MwTextHeight(handle, font, l->text);
MwDrawRect(handle, &c_r, text);
}
if(j == 0 || j == 1) {
line[0].y += MwTextHeight(handle, font, l->text) / 2 * (2 - j);
line[1].y += MwTextHeight(handle, font, l->text) / 2 * (2 - j);
MwLLLine(handle->lowlevel, line, c ? base : text);
}
} }
} }
handle->bgcolor = c ? text : NULL;
MwDrawText(handle, font, &p, l->text, MwALIGNMENT_BEGINNING, c ? base : text);
handle->bgcolor = NULL;
break; break;
} }
case MwDOCUMENT_UNDERLINE: case MwDOCUMENT_UNDERLINE:
case MwDOCUMENT_STRIKETHROUGH: case MwDOCUMENT_STRIKETHROUGH:
case MwDOCUMENT_CLICKABLE:
{ {
if(l->type == MwDOCUMENT_UNDERLINE) { if(l->type == MwDOCUMENT_UNDERLINE) {
u = l->integer; u = l->integer;
} else { } else if(l->type == MwDOCUMENT_STRIKETHROUGH) {
s = l->integer; s = l->integer;
} else {
c = l->integer;
} }
if(l->type == MwDOCUMENT_UNDERLINE ? u : s) { if(l->type == MwDOCUMENT_UNDERLINE ? u : l->type == MwDOCUMENT_STRIKETHROUGH ? s
: c) {
int j; int j;
for(j = i + 1; j < arrlen(d->layouts) && d->layouts[j].type != l->type; j++) { for(j = i + 1; j < arrlen(d->layouts) && d->layouts[j].type != l->type; j++) {
@ -121,8 +167,10 @@ static void draw(MwWidget handle) {
if(l->type == MwDOCUMENT_UNDERLINE) { if(l->type == MwDOCUMENT_UNDERLINE) {
u_p = p; u_p = p;
} else { } else if(l->type == MwDOCUMENT_STRIKETHROUGH) {
s_p = p; s_p = p;
} else {
c_p = p;
} }
break; break;
} }
@ -141,6 +189,29 @@ static void draw(MwWidget handle) {
MwLLFreeColor(base); MwLLFreeColor(base);
} }
static char* in_hitbox(MwWidget handle, MwPoint* p) {
int i;
MwDocument d = handle->internal;
for(i = 0; i < arrlen(d->clickables); i++) {
MwRect hitbox = d->clickables[i].hitbox;
if(hitbox.x <= p->x && p->x <= (hitbox.x + hitbox.width) && hitbox.y <= p->y && p->y <= (hitbox.y + hitbox.height)) {
return d->clickables[i].event;
}
}
return NULL;
}
static void click(MwWidget handle) {
char* event;
if((event = in_hitbox(handle, &handle->mouse_point)) != NULL) {
MwDispatchUserHandler(handle, MwNdocumentActivateHandler, event);
}
}
static int enter_block(MD_BLOCKTYPE type, void* detail, void* userdata) { static int enter_block(MD_BLOCKTYPE type, void* detail, void* userdata) {
MwDocument d = userdata; MwDocument d = userdata;
MwDocumentLayout l; MwDocumentLayout l;
@ -251,6 +322,21 @@ static int enter_span(MD_SPANTYPE type, void* detail, void* userdata) {
arrput(d->layouts, l); arrput(d->layouts, l);
break; break;
} }
case MD_SPAN_A:
{
MD_ATTRIBUTE* href = &((MD_SPAN_A_DETAIL*)detail)->href;
char* text = malloc(href->size + 1);
memcpy(text, href->text, href->size);
text[href->size] = 0;
l.type = MwDOCUMENT_CLICKABLE;
l.text = text;
l.integer = 1;
arrput(d->layouts, l);
break;
}
default: default:
break; break;
} }
@ -299,6 +385,14 @@ static int leave_span(MD_SPANTYPE type, void* detail, void* userdata) {
arrput(d->layouts, l); arrput(d->layouts, l);
break; break;
} }
case MD_SPAN_A:
{
l.type = MwDOCUMENT_CLICKABLE;
l.integer = 0;
arrput(d->layouts, l);
break;
}
default: default:
break; break;
} }
@ -371,6 +465,15 @@ static int text(MD_TEXTTYPE type, const MD_CHAR* text, MD_SIZE size, void* userd
#define TOPFONT fontstack[arrlen(fontstack) - 1] #define TOPFONT fontstack[arrlen(fontstack) - 1]
#define POP arrdel(fontstack, arrlen(fontstack) - 1); #define POP arrdel(fontstack, arrlen(fontstack) - 1);
static void add_clickable(MwDocument d, char* c_title, MwRect* clickable) {
MwDocumentClickable c;
c.hitbox = *clickable;
c.event = MwStringDuplicate(c_title);
arrput(d->clickables, c);
}
static void layout(MwWidget handle) { static void layout(MwWidget handle) {
MwDocument d = handle->internal; MwDocument d = handle->internal;
int w = MwGetInteger(handle, MwNwidth); int w = MwGetInteger(handle, MwNwidth);
@ -378,9 +481,16 @@ static void layout(MwWidget handle) {
int x = 0; int x = 0;
int y = 0; int y = 0;
MwFLFont* fontstack = NULL; MwFLFont* fontstack = NULL;
MwRect clickable;
char* c_title = NULL;
arrput(fontstack, NULL); arrput(fontstack, NULL);
for(i = 0; i < arrlen(d->clickables); i++) {
if(d->clickables[i].event != NULL) free(d->clickables[i].event);
}
arrfree(d->clickables);
for(i = 0; i < arrlen(d->layouts); i++) { for(i = 0; i < arrlen(d->layouts); i++) {
MwDocumentLayout* l = &d->layouts[i]; MwDocumentLayout* l = &d->layouts[i];
@ -399,7 +509,21 @@ static void layout(MwWidget handle) {
l->x = x; l->x = x;
l->y = y; l->y = y;
if(c_title != NULL) {
if(clickable.y < y) {
if(clickable.y >= 0 && clickable.width > 0) add_clickable(d, c_title, &clickable);
clickable.x = x;
clickable.y = y;
}
}
x += t; x += t;
if(c_title != NULL) {
clickable.width = x - clickable.x;
}
break; break;
} }
case MwDOCUMENT_SPACE: case MwDOCUMENT_SPACE:
@ -411,6 +535,11 @@ static void layout(MwWidget handle) {
{ {
x = 0; x = 0;
y += MwTextHeight(handle, TOPFONT, "M"); y += MwTextHeight(handle, TOPFONT, "M");
if(c_title != NULL) {
add_clickable(d, c_title, &clickable);
}
break; break;
} }
case MwDOCUMENT_BOLD: case MwDOCUMENT_BOLD:
@ -447,6 +576,22 @@ static void layout(MwWidget handle) {
} }
break; break;
} }
case MwDOCUMENT_CLICKABLE:
{
if(l->integer) {
clickable.y = -100;
clickable.width = 0;
clickable.height = MwTextHeight(handle, TOPFONT, "M");
c_title = MwStringDuplicate(l->text);
} else {
if(clickable.y >= 0 && clickable.width > 0) add_clickable(d, c_title, &clickable);
free(c_title);
c_title = NULL;
}
break;
}
} }
l->font = TOPFONT; l->font = TOPFONT;
@ -484,6 +629,21 @@ static void prop_change(MwWidget handle, const char* key) {
} }
} }
static void mouse_move(MwWidget handle) {
char* event = in_hitbox(handle, &handle->mouse_point);
MwDocument d = handle->internal;
if(d->center_cursor && event == NULL) {
MwLLSetCursor(handle->lowlevel, &MwCursorDefault, &MwCursorDefaultMask);
d->center_cursor = 0;
} else if(!d->center_cursor && event != NULL) {
MwLLSetCursor(handle->lowlevel, &MwCursorCenter, &MwCursorCenterMask);
d->center_cursor = 1;
}
}
static void resize(MwWidget handle) { static void resize(MwWidget handle) {
layout(handle); layout(handle);
} }
@ -492,10 +652,10 @@ MwClassRec MwDocumentClassRec = {
wcreate, /* create */ wcreate, /* create */
destroy, /* destroy */ destroy, /* destroy */
draw, /* draw */ draw, /* draw */
NULL, /* click */ click, /* click */
NULL, /* parent_resize */ NULL, /* parent_resize */
prop_change, /* prop_change */ prop_change, /* prop_change */
NULL, /* mouse_move */ mouse_move, /* mouse_move */
NULL, /* mouse_up */ NULL, /* mouse_up */
NULL, /* mouse_down */ NULL, /* mouse_down */
NULL, /* key */ NULL, /* key */