From 8d48075f50780c03715c56e65616da4006e484e3 Mon Sep 17 00:00:00 2001 From: Nishi Date: Thu, 17 Sep 2026 02:03:09 +0900 Subject: [PATCH] MwNdocumentActivateHandler --- examples/basic/document.c | 18 +++- include/Mw/Constants.h | 1 + include/Mw/Resource/Cursor.h | 20 ++++ include/Mw/StringDefs.h | 1 + include/Mw/TypeDefs.h | 13 ++- milsko.xml | 4 + src/cursor/center.c | 43 ++++++++ src/cursor/hand.c | 46 ++++++++ src/widget/document.c | 202 +++++++++++++++++++++++++++++++---- 9 files changed, 322 insertions(+), 26 deletions(-) create mode 100644 src/cursor/center.c create mode 100644 src/cursor/hand.c diff --git a/examples/basic/document.c b/examples/basic/document.c index 1051a6b..f9529fe 100644 --- a/examples/basic/document.c +++ b/examples/basic/document.c @@ -30,14 +30,25 @@ static void MWAPI layout_document(MwWidget handle, void* user, void* call) { 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() { char* text = malloc(64 * 1024); int i; - strcpy(text, "# Hello world!\nThis is a __test__ text...\n\n`Some monospace here`\n\n~~Removed~~"); - strcat(text, "\n```\nint main(){\n printf(\"Hello, world!\\n\");\n}\n```"); + strcpy(text, "# Hello world!\nThis is a __test__ text...\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++) { - 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(); @@ -54,6 +65,7 @@ int main() { MwAddUserHandler(window, MwNresizeHandler, resize_window, NULL); MwAddUserHandler(document, MwNlayoutHandler, layout_document, NULL); + MwAddUserHandler(document, MwNdocumentActivateHandler, activate_document, NULL); resize_window(window, NULL, NULL); diff --git a/include/Mw/Constants.h b/include/Mw/Constants.h index 0a79ae6..a0eec8b 100644 --- a/include/Mw/Constants.h +++ b/include/Mw/Constants.h @@ -130,6 +130,7 @@ enum MwDOCUMENT_LAYOUT { MwDOCUMENT_MONOSPACE, MwDOCUMENT_STRIKETHROUGH, MwDOCUMENT_UNDERLINE, + MwDOCUMENT_CLICKABLE }; enum MwMOUSE { diff --git a/include/Mw/Resource/Cursor.h b/include/Mw/Resource/Cursor.h index 9a47802..c9347b9 100644 --- a/include/Mw/Resource/Cursor.h +++ b/include/Mw/Resource/Cursor.h @@ -63,6 +63,26 @@ MWDECL MwCursor MwCursorHidden; */ 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 } #endif diff --git a/include/Mw/StringDefs.h b/include/Mw/StringDefs.h index b3f527b..cf0a723 100644 --- a/include/Mw/StringDefs.h +++ b/include/Mw/StringDefs.h @@ -84,6 +84,7 @@ #define MwNactivateHandler "Cactivate" #define MwNlistBoxActivateHandler "ClistBoxActivate" #define MwNtreeViewActivateHandler "CtreeViewActivate" +#define MwNdocumentActivateHandler "CdocumentActivate" #define MwNresizeHandler "Cresize" #define MwNtickHandler "Ctick" #define MwNmenuHandler "Cmenu" diff --git a/include/Mw/TypeDefs.h b/include/Mw/TypeDefs.h index 2a11d36..3463b37 100644 --- a/include/Mw/TypeDefs.h +++ b/include/Mw/TypeDefs.h @@ -33,6 +33,7 @@ typedef struct _MwTable* MwTable; typedef struct _MwChartEntry MwChartEntry; typedef struct _MwChart* MwChart; typedef struct _MwDocumentLayout MwDocumentLayout; +typedef struct _MwDocumentClickable MwDocumentClickable; typedef struct _MwDocument* MwDocument; typedef struct _MwMouse MwMouse; typedef struct _MwTTFInfo* MwTTFInfo; @@ -278,9 +279,17 @@ struct _MwDocumentLayout { MwFLFont font; }; +struct _MwDocumentClickable { + MwRect hitbox; + char* event; +}; + struct _MwDocument { - MwDocumentLayout* layouts; - MwFLFont headers[6]; + MwDocumentLayout* layouts; + MwDocumentClickable* clickables; + MwFLFont headers[6]; + + int center_cursor; }; struct _MwTTFInfo { diff --git a/milsko.xml b/milsko.xml index 1bb19fd..5b90410 100644 --- a/milsko.xml +++ b/milsko.xml @@ -154,6 +154,9 @@ + + + @@ -710,6 +713,7 @@ + diff --git a/src/cursor/center.c b/src/cursor/center.c new file mode 100644 index 0000000..2340f57 --- /dev/null +++ b/src/cursor/center.c @@ -0,0 +1,43 @@ +#include + +/** + * 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 /* ....####.... */ + }}; diff --git a/src/cursor/hand.c b/src/cursor/hand.c new file mode 100644 index 0000000..07b015d --- /dev/null +++ b/src/cursor/hand.c @@ -0,0 +1,46 @@ +#include + +/** + * 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 /* .####........ */ + }}; diff --git a/src/widget/document.c b/src/widget/document.c index 2dc7ec9..7611c62 100644 --- a/src/widget/document.c +++ b/src/widget/document.c @@ -3,7 +3,7 @@ #include "../../external/md4c.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) { MwDocument d = handle->internal; @@ -13,6 +13,11 @@ static void reset(MwWidget handle) { if(d->layouts[i].text != NULL) free(d->layouts[i].text); } 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) { @@ -52,11 +57,11 @@ static void draw(MwWidget handle) { MwRect r; int i; MwFLFont font = NULL; - MwPoint u_p, s_p; - int u = 0, s = 0; + MwPoint u_p, s_p, c_p; + int u = 0, s = 0, c = 0; - u_p.x = u_p.y = 0; - s_p.x = s_p.y = 0; + u_p.x = s_p.x = c_p.x = 0; + u_p.y = s_p.y = c_p.y = 0; r.x = 0; r.y = 0; @@ -75,9 +80,7 @@ static void draw(MwWidget handle) { p.x = l->x; p.y = l->y + MwTextHeight(handle, font, l->text) / 2; - MwDrawText(handle, font, &p, l->text, MwALIGNMENT_BEGINNING, text); - - if(u || s) { + if(u || s || c) { MwPoint line[2]; int j; @@ -85,31 +88,74 @@ static void draw(MwWidget handle) { u_p.x = 0; 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; - line[1] = u_p; + for(j = 0; j < 3; j++) { + 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); - u_p.x = line[1].x; + line[1].x = p.x + MwTextWidth(handle, font, l->text); - for(j = 0; j < 2; j++) { - line[0].y += MwTextHeight(handle, font, l->text) / 2; - line[1].y += MwTextHeight(handle, font, l->text) / 2; - if((j == 0 && s) || (j == 1 && u)) MwLLLine(handle->lowlevel, line, text); + if(j == 0) u_p.x = line[1].x; + if(j == 1) s_p.x = line[1].x; + if(j == 2) c_p.x = line[1].x; + + 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; } case MwDOCUMENT_UNDERLINE: case MwDOCUMENT_STRIKETHROUGH: + case MwDOCUMENT_CLICKABLE: { if(l->type == MwDOCUMENT_UNDERLINE) { u = l->integer; - } else { + } else if(l->type == MwDOCUMENT_STRIKETHROUGH) { 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; 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) { u_p = p; - } else { + } else if(l->type == MwDOCUMENT_STRIKETHROUGH) { s_p = p; + } else { + c_p = p; } break; } @@ -141,6 +189,29 @@ static void draw(MwWidget handle) { 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) { MwDocument d = userdata; MwDocumentLayout l; @@ -251,6 +322,21 @@ static int enter_span(MD_SPANTYPE type, void* detail, void* userdata) { arrput(d->layouts, l); 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: break; } @@ -299,6 +385,14 @@ static int leave_span(MD_SPANTYPE type, void* detail, void* userdata) { arrput(d->layouts, l); break; } + case MD_SPAN_A: + { + l.type = MwDOCUMENT_CLICKABLE; + l.integer = 0; + + arrput(d->layouts, l); + break; + } default: 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 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) { MwDocument d = handle->internal; int w = MwGetInteger(handle, MwNwidth); @@ -378,9 +481,16 @@ static void layout(MwWidget handle) { int x = 0; int y = 0; MwFLFont* fontstack = NULL; + MwRect clickable; + char* c_title = 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++) { MwDocumentLayout* l = &d->layouts[i]; @@ -399,7 +509,21 @@ static void layout(MwWidget handle) { l->x = x; 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; + + if(c_title != NULL) { + clickable.width = x - clickable.x; + } + break; } case MwDOCUMENT_SPACE: @@ -411,6 +535,11 @@ static void layout(MwWidget handle) { { x = 0; y += MwTextHeight(handle, TOPFONT, "M"); + + if(c_title != NULL) { + add_clickable(d, c_title, &clickable); + } + break; } case MwDOCUMENT_BOLD: @@ -447,6 +576,22 @@ static void layout(MwWidget handle) { } 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; @@ -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) { layout(handle); } @@ -492,10 +652,10 @@ MwClassRec MwDocumentClassRec = { wcreate, /* create */ destroy, /* destroy */ draw, /* draw */ - NULL, /* click */ + click, /* click */ NULL, /* parent_resize */ prop_change, /* prop_change */ - NULL, /* mouse_move */ + mouse_move, /* mouse_move */ NULL, /* mouse_up */ NULL, /* mouse_down */ NULL, /* key */