From 1dce4defe62b5e0ed70460b52c444dc51a701e18 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 22 Jan 2025 18:42:11 -0500 Subject: Make puglSetSizeHint() consistent with puglSetSize() In general, it's more convenient to have full-width integers as parameters, since C will promote any arithmetic on smaller types to them anyway. Using narrow types here, then, doesn't really make anything stricter, just forces an annoying cast when lots of warnings are enabled, which is likely unchecked. Better to handle it here, since it's more convenient, and the integer range checks the compiler can do aren't correct anyway (the max width/height is intentionally smaller than the max PuglSpan, so it can fit in a signed 16-bit integer). --- bindings/cpp/include/pugl/pugl.hpp | 2 +- include/pugl/pugl.h | 20 ++++++++++---------- src/internal.c | 16 ++++++++++++++++ src/internal.h | 7 +++++++ src/mac.m | 13 ++++--------- src/win.c | 12 +++--------- src/x11.c | 12 ++++-------- 7 files changed, 45 insertions(+), 37 deletions(-) diff --git a/bindings/cpp/include/pugl/pugl.hpp b/bindings/cpp/include/pugl/pugl.hpp index 5a97079..a4ee273 100644 --- a/bindings/cpp/include/pugl/pugl.hpp +++ b/bindings/cpp/include/pugl/pugl.hpp @@ -545,7 +545,7 @@ public: } /// @copydoc puglSetSizeHint - Status setSizeHint(SizeHint hint, PuglSpan width, PuglSpan height) noexcept + Status setSizeHint(SizeHint hint, unsigned width, unsigned height) noexcept { return static_cast( puglSetSizeHint(cobj(), static_cast(hint), width, height)); diff --git a/include/pugl/pugl.h b/include/pugl/pugl.h index e3ab504..b70b91d 100644 --- a/include/pugl/pugl.h +++ b/include/pugl/pugl.h @@ -1234,8 +1234,8 @@ PUGL_API PuglStatus puglSetSizeHint(PuglView* view, PuglSizeHint hint, - PuglSpan width, - PuglSpan height); + unsigned width, + unsigned height); /** @} @@ -1820,7 +1820,7 @@ static inline PUGL_DEPRECATED_BY("puglSetSizeHint") void puglInitWindowMinSize(PuglView* view, int width, int height) { - puglSetSizeHint(view, PUGL_MIN_SIZE, (PuglSpan)width, (PuglSpan)height); + puglSetSizeHint(view, PUGL_MIN_SIZE, (unsigned)width, (unsigned)height); } /** @@ -1841,8 +1841,8 @@ puglInitWindowAspectRatio(PuglView* view, int maxX, int maxY) { - puglSetSizeHint(view, PUGL_MIN_ASPECT, (PuglSpan)minX, (PuglSpan)minY); - puglSetSizeHint(view, PUGL_MAX_ASPECT, (PuglSpan)maxX, (PuglSpan)maxY); + puglSetSizeHint(view, PUGL_MIN_ASPECT, (unsigned)minX, (unsigned)minY); + puglSetSizeHint(view, PUGL_MAX_ASPECT, (unsigned)maxX, (unsigned)maxY); } /** @@ -2081,7 +2081,7 @@ PuglStatus puglSetDefaultSize(PuglView* view, int width, int height) { return puglSetSizeHint( - view, PUGL_DEFAULT_SIZE, (PuglSpan)width, (PuglSpan)height); + view, PUGL_DEFAULT_SIZE, (unsigned)width, (unsigned)height); } /** @@ -2098,7 +2098,7 @@ PuglStatus puglSetMinSize(PuglView* view, int width, int height) { return puglSetSizeHint( - view, PUGL_MIN_SIZE, (PuglSpan)width, (PuglSpan)height); + view, PUGL_MIN_SIZE, (unsigned)width, (unsigned)height); } /** @@ -2115,7 +2115,7 @@ PuglStatus puglSetMaxSize(PuglView* view, int width, int height) { return puglSetSizeHint( - view, PUGL_MAX_SIZE, (PuglSpan)width, (PuglSpan)height); + view, PUGL_MAX_SIZE, (unsigned)width, (unsigned)height); } /** @@ -2139,10 +2139,10 @@ PuglStatus puglSetAspectRatio(PuglView* view, int minX, int minY, int maxX, int maxY) { const PuglStatus st0 = - puglSetSizeHint(view, PUGL_MIN_ASPECT, (PuglSpan)minX, (PuglSpan)minY); + puglSetSizeHint(view, PUGL_MIN_ASPECT, (unsigned)minX, (unsigned)minY); const PuglStatus st1 = - puglSetSizeHint(view, PUGL_MAX_ASPECT, (PuglSpan)maxX, (PuglSpan)maxY); + puglSetSizeHint(view, PUGL_MAX_ASPECT, (unsigned)maxX, (unsigned)maxY); return st0 ? st0 : st1; } diff --git a/src/internal.c b/src/internal.c index 3c4d304..dfea0ca 100644 --- a/src/internal.c +++ b/src/internal.c @@ -68,6 +68,22 @@ puglSetString(char** dest, const char* string) } } +PuglStatus +puglStoreSizeHint(PuglView* const view, + const PuglSizeHint hint, + const unsigned width, + const unsigned height) +{ + if (width > INT16_MAX || height > INT16_MAX || + (unsigned)hint >= PUGL_NUM_SIZE_HINTS) { + return PUGL_BAD_PARAMETER; + } + + view->sizeHints[hint].width = (PuglSpan)width; + view->sizeHints[hint].height = (PuglSpan)height; + return PUGL_SUCCESS; +} + uint32_t puglDecodeUTF8(const uint8_t* buf) { diff --git a/src/internal.h b/src/internal.h index 3721c10..f978074 100644 --- a/src/internal.h +++ b/src/internal.h @@ -34,6 +34,13 @@ puglSetBlob(PuglBlob* dest, const void* data, size_t len); void puglSetString(char** dest, const char* string); +/// Store `width` and `height` as the current value of a size `hint` +PuglStatus +puglStoreSizeHint(PuglView* view, + PuglSizeHint hint, + unsigned width, + unsigned height); + /// Handle a changed string property PUGL_API PuglStatus diff --git a/src/mac.m b/src/mac.m index d68816a..26048a8 100644 --- a/src/mac.m +++ b/src/mac.m @@ -1849,17 +1849,12 @@ puglSetSize(PuglView* const view, const unsigned width, const unsigned height) PuglStatus puglSetSizeHint(PuglView* const view, const PuglSizeHint hint, - const PuglSpan width, - const PuglSpan height) + const unsigned width, + const unsigned height) { - if ((unsigned)hint >= PUGL_NUM_SIZE_HINTS) { - return PUGL_BAD_PARAMETER; - } - - view->sizeHints[hint].width = width; - view->sizeHints[hint].height = height; + const PuglStatus st = puglStoreSizeHint(view, hint, width, height); - return view->impl->window ? updateSizeHint(view, hint) : PUGL_SUCCESS; + return (!st && view->impl->window) ? updateSizeHint(view, hint) : st; } PuglStatus diff --git a/src/win.c b/src/win.c index bc13a0f..de648c7 100644 --- a/src/win.c +++ b/src/win.c @@ -1358,16 +1358,10 @@ puglSetSize(PuglView* const view, const unsigned width, const unsigned height) PuglStatus puglSetSizeHint(PuglView* const view, const PuglSizeHint hint, - const PuglSpan width, - const PuglSpan height) + const unsigned width, + const unsigned height) { - if ((unsigned)hint >= PUGL_NUM_SIZE_HINTS) { - return PUGL_BAD_PARAMETER; - } - - view->sizeHints[hint].width = width; - view->sizeHints[hint].height = height; - return PUGL_SUCCESS; + return puglStoreSizeHint(view, hint, width, height); } PuglStatus diff --git a/src/x11.c b/src/x11.c index 0513280..eabb715 100644 --- a/src/x11.c +++ b/src/x11.c @@ -2038,16 +2038,12 @@ puglSetSize(PuglView* const view, const unsigned width, const unsigned height) PuglStatus puglSetSizeHint(PuglView* const view, const PuglSizeHint hint, - const PuglSpan width, - const PuglSpan height) + const unsigned width, + const unsigned height) { - if ((unsigned)hint >= PUGL_NUM_SIZE_HINTS) { - return PUGL_BAD_PARAMETER; - } + const PuglStatus st = puglStoreSizeHint(view, hint, width, height); - view->sizeHints[hint].width = width; - view->sizeHints[hint].height = height; - return updateSizeHints(view); + return st ? st : updateSizeHints(view); } PuglStatus -- cgit v1.2.1