/* Copyright (c) 2022 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "uitoolkit/UIModalDialog.h" #include "uitoolkit/UIToolkitAppearance.h" #include "uitoolkit/UIWindowRoot.h" #include #include #define stemobject_implementation UIWindowRoot stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(hitTestList); stemobject_vtable_end(); UIWindowRoot * UIWindowRoot_create(void) { stemobject_create_implementation(init) } bool UIWindowRoot_init(UIWindowRoot * self) { call_super(init, self, VECTOR2f_ZERO, UICONTAINER_NO_CENTER, UICONTAINER_SIZE_AUTO, false, UIAppearance_init(NULL)); self->private_ivar(windowAllocatedCount) = 4; self->windows = malloc(self->private_ivar(windowAllocatedCount) * sizeof(*self->windows)); self->windowCount = 0; return true; } void UIWindowRoot_dispose(UIWindowRoot * self) { call_super_virtual(dispose, self); } void UIWindowRoot_hitTestList(UIWindowRoot * self, float x, float y, UIHitTestType type, int priorityOffset, UIHitTestResultIO * resultIO) { if (type == HIT_TEST_KEY_DOWN) { Vector2f offset = call_virtual(getRelativeOffset, self); x -= offset.x; y -= offset.y; for (unsigned int windowIndex = 0; windowIndex < self->windowCount; windowIndex++) { if (self->windows[windowIndex]->visible && UIElement_isInFocusChain(self->windows[windowIndex])) { call_virtual(hitTestList, self->windows[windowIndex], x, y, type, priorityOffset, resultIO); } } return; } call_super_virtual(hitTestList, self, x, y, type, priorityOffset, resultIO); } void UIWindowRoot_addWindow(UIWindowRoot * self, compat_type(UIWindowView *) window, bool takeOwnership) { if (self->windowCount + 1 > self->private_ivar(windowAllocatedCount)) { self->private_ivar(windowAllocatedCount) *= 2; self->windows = realloc(self->windows, self->private_ivar(windowAllocatedCount) * sizeof(*self->windows)); } self->windows[self->windowCount++] = window; call_super(addElement, self, window, takeOwnership); } void UIWindowRoot_removeWindow(UIWindowRoot * self, compat_type(UIWindowView *) window) { unsigned int windowIndex = UIWindowRoot_getWindowIndex(self, window); if (windowIndex == UINT_MAX) { #ifdef DEBUG fprintf(stderr, "Warning: UIWindowRoot_removeWindow() called with window %p, which is not in the window list\n", window); #endif } else { call_super(removeElement, self, window); self->windowCount--; for (; windowIndex < self->windowCount; windowIndex++) { self->windows[windowIndex] = self->windows[windowIndex + 1]; } } } unsigned int UIWindowRoot_getWindowIndex(UIWindowRoot * self, compat_type(UIWindowView *) window) { for (unsigned int windowIndex = 0; windowIndex < self->windowCount; windowIndex++) { if (self->windows[windowIndex] == window) { return windowIndex; } } return UINT_MAX; } bool UIWindowRoot_isFrontWindow(UIWindowRoot * self, compat_type(UIWindowView *) window) { return UIWindowRoot_getWindowIndex(self, window) == self->windowCount - 1; } void UIWindowRoot_bringWindowToFront(UIWindowRoot * self, compat_type(UIWindowView *) window) { for (unsigned int windowIndex = 0; windowIndex < self->windowCount; windowIndex++) { if (self->windows[windowIndex] == window) { for (; windowIndex < self->windowCount - 1; windowIndex++) { self->windows[windowIndex] = self->windows[windowIndex + 1]; } self->windows[windowIndex] = window; break; } } } bool UIWindowRoot_hasModalDialog(UIWindowRoot * self) { for (unsigned int windowIndex = 0; windowIndex < self->windowCount; windowIndex++) { if (StemObject_isClassOrSubclass(self->windows[windowIndex], &UIModalDialog_class)) { return true; } } return false; } UIWindowView * UIWindowRoot_hitTestWindow(UIWindowRoot * self, float x, float y) { for (unsigned int windowIndex = self->windowCount - 1; windowIndex < self->windowCount; windowIndex--) { UIWindowView * window = self->windows[windowIndex]; if (window->visible) { Rect4f bounds = call_virtual(getBounds, window); if (window->resizeCallback != NULL) { float resizeAreaOutset = getAppearanceFloat(window->appearance, UIWindowView_resizeAreaOutset); bounds = Rect4f_inset(bounds, VECTOR2f(-resizeAreaOutset, -resizeAreaOutset)); } if (Rect4f_containsVector2f(bounds, VECTOR2f(x, y))) { return window; } } } return NULL; } UIWindowView * UIWindowRoot_getFrontVisibleWindow(UIWindowRoot * self) { for (unsigned int windowIndex = self->windowCount - 1; windowIndex < self->windowCount; windowIndex--) { UIWindowView * window = self->windows[windowIndex]; if (window->visible) { return window; } } return NULL; } UIWindowView * UIWindowRoot_getFocusedWindow(UIWindowRoot * self) { for (unsigned int windowIndex = self->windowCount - 1; windowIndex < self->windowCount; windowIndex--) { UIWindowView * window = self->windows[windowIndex]; if (window->visible && UIElement_isInFocusChain(window)) { return window; } } return NULL; } void UIWindowRoot_focusFrontWindow(UIWindowRoot * self) { for (unsigned int windowIndex = self->windowCount - 1; windowIndex < self->windowCount; windowIndex--) { UIWindowView * window = self->windows[windowIndex]; if (window->visible) { call_virtual(setFocusedElement, UIElement_getTopParent(window), window, NULL, UI_NONE); break; } } }