diff options
Diffstat (limited to 'src/internal.c')
-rw-r--r-- | src/internal.c | 84 |
1 files changed, 80 insertions, 4 deletions
diff --git a/src/internal.c b/src/internal.c index 3c4d304..ca84ed1 100644 --- a/src/internal.c +++ b/src/internal.c @@ -5,19 +5,80 @@ #include "types.h" -#include "pugl/pugl.h" +#include <pugl/pugl.h> #include <assert.h> #include <stdbool.h> +#include <stdint.h> #include <stdlib.h> #include <string.h> +static PuglPoint +make_point(const PuglCoord x, const PuglCoord y) +{ + const PuglPoint point = {x, y}; + return point; +} + +bool +puglIsValidPosition(const int x, const int y) +{ + // INT16_MIN is a sentinel, INT16_MAX is impossible with non-zero size + return x > INT16_MIN && x < INT16_MAX && y > INT16_MIN && y < INT16_MAX; +} + +bool +puglIsValidSize(const unsigned width, const unsigned height) +{ + return width && height && width <= INT16_MAX && height <= INT16_MAX; +} + bool -puglIsValidSize(const PuglViewSize size) +puglIsValidArea(const PuglArea size) { return size.width && size.height; } +PuglArea +puglGetInitialSize(const PuglView* const view) +{ + if (view->lastConfigure.type == PUGL_CONFIGURE) { + // Use the last configured size + const PuglConfigureEvent config = view->lastConfigure; + const PuglArea size = {config.width, config.height}; + return size; + } + + // Use the default size hint set by the application + return view->sizeHints[PUGL_DEFAULT_SIZE]; +} + +PuglPoint +puglGetInitialPosition(const PuglView* const view, const PuglArea size) +{ + if (view->lastConfigure.type == PUGL_CONFIGURE) { + // Use the last configured frame + return make_point(view->lastConfigure.x, view->lastConfigure.y); + } + + const PuglPoint defaultPos = view->positionHints[PUGL_DEFAULT_POSITION]; + if (puglIsValidPosition(defaultPos.x, defaultPos.y)) { + // Use the default position hint set by the application + return make_point(defaultPos.x, defaultPos.y); + } + + if (view->parent) { + // Default to the top/left origin of the parent + return make_point(0, 0); + } + + // Center frame on a transient ancestor, or failing that, the screen + const PuglPoint center = puglGetAncestorCenter(view); + const PuglPoint pos = {(PuglCoord)(center.x - (size.width / 2)), + (PuglCoord)(center.y - (size.height / 2))}; + return pos; +} + void puglEnsureHint(PuglView* const view, const PuglViewHint hint, const int value) { @@ -68,6 +129,21 @@ puglSetString(char** dest, const char* string) } } +PuglStatus +puglStoreSizeHint(PuglView* const view, + const PuglSizeHint hint, + const unsigned width, + const unsigned height) +{ + if (!puglIsValidSize(width, height)) { + 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) { @@ -159,7 +235,7 @@ puglPreRealize(PuglView* const view) } // Ensure that the default size is set to a valid size - if (!puglIsValidSize(view->sizeHints[PUGL_DEFAULT_SIZE])) { + if (!puglIsValidArea(view->sizeHints[PUGL_DEFAULT_SIZE])) { return PUGL_BAD_CONFIGURATION; } @@ -173,7 +249,7 @@ puglDispatchSimpleEvent(PuglView* view, const PuglEventType type) type == PUGL_UPDATE || type == PUGL_CLOSE || type == PUGL_LOOP_ENTER || type == PUGL_LOOP_LEAVE); - const PuglEvent event = {{type, 0}}; + const PuglEvent event = {{type, 0U}}; return puglDispatchEvent(view, &event); } |