From 85994d42f15f164f6e147a557333c0fd33b1c734 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Thu, 15 Jan 2026 05:16:14 +0900 Subject: [PATCH] fix issue #5 --- Makefile.pl | 72 ++++++++++++++++---------------- include/Mw/TypeDefs.h | 3 ++ pl/ostype/Darwin.pl | 2 +- pl/utils.pl | 2 +- src/backend/x11.c | 2 +- src/core.c | 97 +++++++++++++++++++++++++++---------------- 6 files changed, 104 insertions(+), 74 deletions(-) diff --git a/Makefile.pl b/Makefile.pl index ded79b0..f710f27 100755 --- a/Makefile.pl +++ b/Makefile.pl @@ -45,7 +45,7 @@ param_set("vulkan", 0); param_set("vulkan-string-helper", 1); param_set("shared", 1); param_set("static", 1); -param_set("examples", 1); +param_set("examples", 1); my %features = ( "classic-theme" => "use classic theme", @@ -221,11 +221,12 @@ foreach my $l (@library_targets) { my $o = $object_suffix; $o =~ s/\./\\\./g; - if ($l =~ /cocoa/) { - $s =~ s/$o$/.m/; - } else { - $s =~ s/$o$/.c/; - } + if ($l =~ /cocoa/) { + $s =~ s/$o$/.m/; + } + else { + $s =~ s/$o$/.c/; + } if ($l =~ /^external\//) { $warn = ""; @@ -236,37 +237,38 @@ foreach my $l (@library_targets) { } print(OUT "\n"); print(OUT "\n"); -if(param_get("examples")) { - print(OUT "examples: " . join(" ", @examples_targets) . "\n"); - print(OUT "\n"); - foreach my $l (@examples_targets) { - my $libs = ""; - my $s = $l; - my $o = $executable_suffix; - $o =~ s/\./\\\./g; - $s =~ s/$o$//; +if (param_get("examples")) { + print(OUT "examples: " . join(" ", @examples_targets) . "\n"); + print(OUT "\n"); + foreach my $l (@examples_targets) { + my $libs = ""; + my $s = $l; + my $o = $executable_suffix; + $o =~ s/\./\\\./g; + $s =~ s/$o$//; - if (defined($examples_libs{$l})) { - $libs = $examples_libs{$l}; - } + if (defined($examples_libs{$l})) { + $libs = $examples_libs{$l}; + } - print(OUT - "${l}: ${s}${object_suffix} src/${library_prefix}Mw${library_suffix}\n" - ); - if (grep(/^cocoa$/, @backends)) { - print(OUT - " \$(CC) -L./src \$\(LIBDIR) -o ${l} ${s}${object_suffix} -lMw ${math} ${libs}\n" - ); - } else { - print(OUT - " \$(CC) -L src -Wl,-R./src \$\(LIBDIR) -o ${l} ${s}${object_suffix} -lMw ${math} ${libs}\n" - ); - } - print(OUT "${s}${object_suffix}: ${s}.c\n"); - print(OUT - " \$(CC) -c \$\(INCDIR) -o ${s}${object_suffix} ${s}.c -lMw ${math}\n" - ); - } + print(OUT +"${l}: ${s}${object_suffix} src/${library_prefix}Mw${library_suffix}\n" + ); + if (grep(/^cocoa$/, @backends)) { + print(OUT +" \$(CC) -L./src \$\(LIBDIR) -o ${l} ${s}${object_suffix} -lMw ${math} ${libs}\n" + ); + } + else { + print(OUT +" \$(CC) -L src -Wl,-R./src \$\(LIBDIR) -o ${l} ${s}${object_suffix} -lMw ${math} ${libs}\n" + ); + } + print(OUT "${s}${object_suffix}: ${s}.c\n"); + print(OUT +" \$(CC) -c \$\(INCDIR) -o ${s}${object_suffix} ${s}.c -lMw ${math}\n" + ); + } } print(OUT "\n"); print(OUT "clean:\n"); diff --git a/include/Mw/TypeDefs.h b/include/Mw/TypeDefs.h index 6434f52..d175c2b 100644 --- a/include/Mw/TypeDefs.h +++ b/include/Mw/TypeDefs.h @@ -101,6 +101,9 @@ struct _MwWidget { MwWidget* tick_list; int destroyed; + + void* root_font; + void* root_boldfont; }; #endif diff --git a/pl/ostype/Darwin.pl b/pl/ostype/Darwin.pl index 72cfe27..c786eb8 100644 --- a/pl/ostype/Darwin.pl +++ b/pl/ostype/Darwin.pl @@ -1,4 +1,4 @@ -$library_suffix = ".dylib"; +$library_suffix = ".dylib"; set_shared_flag("-dynamiclib"); use_backend("cocoa"); diff --git a/pl/utils.pl b/pl/utils.pl index 264e679..f2f722b 100644 --- a/pl/utils.pl +++ b/pl/utils.pl @@ -41,7 +41,7 @@ sub new_example { } sub set_shared_flag { - @shared = $_[0]; + @shared = $_[0]; } sub new_object { diff --git a/src/backend/x11.c b/src/backend/x11.c index ce6dae0..f7c6fe4 100644 --- a/src/backend/x11.c +++ b/src/backend/x11.c @@ -158,7 +158,7 @@ static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) { r->x11.top = 0; r->x11.toplevel = 0; } - r->x11.window = XCreateSimpleWindow(r->x11.display, p, px, py, width, height, 0, 0, WhitePixel(r->x11.display, DefaultScreen(r->x11.display))); + r->x11.window = XCreateSimpleWindow(r->x11.display, p, px, py, width, height, 0, 0, WhitePixel(r->x11.display, DefaultScreen(r->x11.display))); sh.flags = PWinGravity; sh.win_gravity = StaticGravity; diff --git a/src/core.c b/src/core.c index 769de68..9ec87b2 100644 --- a/src/core.c +++ b/src/core.c @@ -114,6 +114,19 @@ static void llclipboardhandler(MwLL handle, void* data) { MwDispatchUserHandler(h, MwNclipboardHandler, data); } +/* if both of them are true + * 1. widget class is not NULL + * 2. parent is NULL + * or parent's widget class is NULL + * + * most likely this widget is first *visible* widget + * + * this macro is not used anywhere else - we could expose it if we have to + * + * (nishi) + */ +#define IsFirstVisible(handle) ((handle)->widget_class != NULL && ((handle)->parent == NULL || (handle)->parent->widget_class == NULL)) + MwWidget MwCreateWidget(MwClass widget_class, const char* name, MwWidget parent, int x, int y, unsigned int width, unsigned int height) { MwWidget h = malloc(sizeof(*h)); @@ -186,6 +199,17 @@ MwWidget MwCreateWidget(MwClass widget_class, const char* name, MwWidget parent, MwAddTickList(h); } +#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2) + if(IsFirstVisible(h)) { + h->root_font = MwFontLoad(MwTTFData, MwTTFDataSize); + h->root_boldfont = MwFontLoad(MwBoldTTFData, MwBoldTTFDataSize); + } else +#endif + { + h->root_font = NULL; + h->root_boldfont = NULL; + } + if(h->parent != NULL) MwDispatch(h->parent, children_update); return h; @@ -250,6 +274,9 @@ static void MwFreeWidget(MwWidget handle) { arrfree(handle->destroy_queue); arrfree(handle->tick_list); + if(handle->root_font != NULL) MwFontFree(handle->root_font); + if(handle->root_boldfont != NULL) MwFontFree(handle->root_boldfont); + free(handle); } @@ -507,8 +534,41 @@ const char* MwGetText(MwWidget handle, const char* key) { return shget(handle->text, key); } +static void* inherit_void(MwWidget handle, const char* key) { + void* v; + + if(handle->parent != NULL && (v = MwGetVoid(handle->parent, key)) != NULL) { + return v; + } + return NULL; +} + void* MwGetVoid(MwWidget handle, const char* key) { - return shget(handle->data, key); + void* v = shget(handle->data, key); + + if(v != NULL) return v; + +#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2) + if(strcmp(key, MwNfont) == 0 || strcmp(key, MwNboldFont) == 0) { + v = inherit_void(handle, key); + + if(v == NULL) { + MwWidget w = handle; + + while(w != NULL && v == NULL) { + if(strcmp(key, MwNfont) == 0) { + v = w->root_font; + } else { + v = w->root_boldfont; + } + + w = w->parent; + } + } + } +#endif + + return v; } void MwVaApply(MwWidget handle, ...) { @@ -576,43 +636,8 @@ void MwVaListApply(MwWidget handle, va_list va) { } } -#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2) -static void set_font(MwWidget handle) { - void* f; - MwWidget h = handle; - while(h != NULL) { - if((f = MwGetVoid(h, MwNfont)) != NULL) { - MwSetVoid(handle, MwNfont, f); - return; - } - h = h->parent; - } - f = MwFontLoad(MwTTFData, MwTTFDataSize); - MwSetVoid(handle, MwNfont, f); -} - -static void set_boldfont(MwWidget handle) { - void* f; - MwWidget h = handle; - while(h != NULL) { - if((f = MwGetVoid(h, MwNboldFont)) != NULL) { - MwSetVoid(handle, MwNboldFont, f); - return; - } - h = h->parent; - } - f = MwFontLoad(MwBoldTTFData, MwBoldTTFDataSize); - MwSetVoid(handle, MwNboldFont, f); -} -#endif - void MwSetDefault(MwWidget handle) { if(handle->lowlevel != NULL) MwLLSetCursor(handle->lowlevel, &MwCursorDefault, &MwCursorDefaultMask); - -#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2) - set_font(handle); - set_boldfont(handle); -#endif } void MwHideCursor(MwWidget handle) {