/* Copyright (c) 2021 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 "gamemath/Scalar.h" #include "shell/ShellKeyCodes.h" #include "uitoolkit/UIPopUpMenu.h" #include "uitoolkit/UIToolkitAppearance.h" #include "uitoolkit/UIToolkitContext.h" #include "uitoolkit/UIToolkitDrawing.h" #define stemobject_implementation UIPopUpMenu v_begin(); v_func(dispose); v_func(hitTest); v_func(mouseDown); v_func(mouseUp); v_func(mouseMoved); v_func(mouseDragged); v_func(mouseLeave); v_func(scrollWheel); v_func(keyDown); v_func(menuActionDown); v_func(menuActionUp); v_func(menuDirectionDown); v_func(acceptsFocus); v_func(ignoreClipForHitTest); v_func(getBounds); v_func(draw); v_func(listRenderables); v_func(setItems); v_func(setSelectedItemIdentifier); v_func(getSelectedItemIdentifier); v_func(getItemBounds); v_func(open); v_func(close); v_end(); #define SINGLE_CLICK_INTERVAL 0.3 struct UIPopUpMenu_itemPrivate { String title; int identifier; bool enabled; bool checked; UIKeyShortcut * shortcut; String shortcutString; }; UIPopUpMenu * UIPopUpMenu_create(unsigned int itemCount, UITextListView_item * items, Vector2f position, Vector2f relativeOrigin, float width, UIOverflowMode overflowMode, UIPopUpMenuActionCallback callback, void * callbackContext, UIAppearance appearance) { stemobject_create_implementation(init, itemCount, items, position, relativeOrigin, width, overflowMode, callback, callbackContext, appearance) } UIPopUpMenu * UIPopUpMenu_createWithMenuItems(unsigned itemCount, UIMenuItem * items, Vector2f position, Vector2f relativeOrigin, float width, UIOverflowMode overflowMode, UIPopUpMenuActionCallback callback, void * callbackContext, UIAppearance appearance) { stemobject_create_implementation(initWithMenuItems, itemCount, items, position, relativeOrigin, width, overflowMode, callback, callbackContext, appearance) } static void drawClosed(Vector2f offset, UIDrawingInterface * drawingInterface, VertexIO * vertexIO, void * context) { UIPopUpMenu * self = context; if (!self->visible) { return; } Rect4f bounds = Rect4f_offset(call_virtual(getBounds, self), offset); UIAtlasEntry frameAtlasEntry; if (self->rollover && self->enabled) { frameAtlasEntry = getAppearanceAtlasEntry(self->appearance, UIPopUpMenu_frameRollover); } else { frameAtlasEntry = getAppearanceAtlasEntry(self->appearance, UIPopUpMenu_frame); } call_virtual(drawSlicedQuad3x3, drawingInterface, bounds, UIAtlasEntry_boundsForScale(frameAtlasEntry, drawingInterface->scaleFactor), getAppearanceSliceGrid3x3(self->appearance, UIPopUpMenu_frameSlices), getAppearanceColor4f(self->appearance, UIPopUpMenu_backgroundColorClosed), vertexIO); Color4f textColor = getAppearanceColor4f(self->appearance, UIPopUpMenu_textColorClosed); if (!self->enabled) { if (drawingInterface->alphaPremultiplied) { textColor.red *= 0.5f; textColor.green *= 0.5f; textColor.blue *= 0.5f; } textColor.alpha *= 0.5f; } if (self->selectedItemIndex < self->itemCount) { unsigned int lastIndex = vertexIO->indexCount; UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, drawingInterface); Rect4f textPadding = getAppearanceRect4f(self->appearance, UIPopUpMenu_textPaddingClosed); float lineHeight = call_virtual(getLineHeight, typeface); call_virtual(drawString, drawingInterface, typeface, self->items[self->selectedItemIndex].title, VECTOR2f(roundpositivef(bounds.xMin + textPadding.xMin), roundpositivef(bounds.yMin + textPadding.yMin + (bounds.yMax - bounds.yMin - lineHeight - textPadding.yMin - textPadding.yMax) * 0.5f)), VECTOR2f(0.0f, 0.0f), 1.0f, textColor, vertexIO); if (self->overflowMode == OVERFLOW_TRUNCATE) { clipVerticesInsideRect(lastIndex, vertexIO->indexCount - lastIndex, bounds, vertexIO); } } UIAtlasEntry indicatorAtlasEntry = getAppearanceAtlasEntry(self->appearance, UIPopUpMenu_popUpMenuIndicator); Rect4f indicatorRect; indicatorRect.xMax = bounds.xMax - 2; indicatorRect.xMin = indicatorRect.xMax - indicatorAtlasEntry.size.x; indicatorRect.yMin = bounds.yMin + 2; indicatorRect.yMax = indicatorRect.yMin + indicatorAtlasEntry.size.y; Color4f indicatorColor = COLOR4f(1.0f, 1.0f, 1.0f, self->enabled ? 1.0f : 0.5f); if (drawingInterface->alphaPremultiplied) { indicatorColor = Color4f_premultiply(indicatorColor); } call_virtual(drawQuad, drawingInterface, indicatorRect, UIAtlasEntry_boundsForScale(indicatorAtlasEntry, drawingInterface->scaleFactor), indicatorColor, vertexIO); } static Rect4f getOpenBounds(UIPopUpMenu * self) { Rect4f closedBounds = call_virtual(getBounds, self); Rect4f openBounds; openBounds.xMin = closedBounds.xMin; openBounds.xMax = closedBounds.xMin + fmaxf(self->width, self->itemMaxWidth); unsigned int rowsAbove = self->openCenterIndex; if (rowsAbove > self->openItemCountMaxAbove) { rowsAbove = self->openItemCountMaxAbove; } unsigned int rowsBelow = self->itemCount - self->openCenterIndex - 1; if (rowsBelow == UINT_MAX) { rowsBelow = 0; } if (rowsBelow > self->openItemCountMaxBelow) { rowsBelow = self->openItemCountMaxBelow; } openBounds.yMax = closedBounds.yMax + rowsAbove * self->rowHeight; openBounds.yMax += (self->rowHeight + 2 - self->closedHeight) / 2; openBounds.yMin = openBounds.yMax - self->rowHeight * (rowsAbove + 1 + rowsBelow) - 2; Rect4f safeBounds = UIElement_rootToLocalRect(self, getAppearanceRect4f(self->appearance, UIToolkit_safeDisplayBounds)); openBounds = Rect4f_constrainWithin(openBounds, safeBounds); return openBounds; } static unsigned int getTopOpenItemIndex(UIPopUpMenu * self) { unsigned int topItemIndex = 0; if (self->openCenterIndex > self->openItemCountMaxAbove) { topItemIndex = self->openCenterIndex - self->openItemCountMaxAbove; } return topItemIndex; } static unsigned int getBottomOpenItemIndex(UIPopUpMenu * self) { unsigned int bottomItemIndex = self->itemCount - 1; if (self->itemCount >= self->openItemCountMaxBelow && self->openCenterIndex + self->openItemCountMaxBelow < self->itemCount - 1) { bottomItemIndex = self->openCenterIndex + self->openItemCountMaxBelow; } return bottomItemIndex; } static void drawOpen(Vector2f offset, UIDrawingInterface * drawingInterface, VertexIO * vertexIO, void * context) { UIPopUpMenu * self = context; if (!self->visible) { return; } Rect4f bounds = UIElement_localToRootRect(self, getOpenBounds(self)); float dropShadowOutset = getAppearanceFloat(self->appearance, UIMenuBar_dropShadowOutset); Vector2f dropShadowOffset = getAppearanceVector2f(self->appearance, UIMenuBar_dropShadowOffset); call_virtual(drawSlicedQuad3x3, drawingInterface, Rect4f_offset(Rect4f_inset(bounds, VECTOR2f(-dropShadowOutset, -dropShadowOutset)), dropShadowOffset), UIAtlasEntry_boundsForScale(getAppearanceAtlasEntry(self->appearance, UIMenuBar_menuDropShadow), drawingInterface->scaleFactor), getAppearanceSliceGrid3x3(self->appearance, UIMenuBar_dropShadowSlices), getAppearanceColor4f(self->appearance, UIMenuBar_dropShadowColor), vertexIO); call_virtual(drawSlicedQuad3x3, drawingInterface, bounds, UIAtlasEntry_boundsForScale(getAppearanceAtlasEntry(self->appearance, UIPopUpMenu_frame), drawingInterface->scaleFactor), getAppearanceSliceGrid3x3(self->appearance, UIPopUpMenu_frameSlices), getAppearanceColor4f(self->appearance, UIPopUpMenu_backgroundColorOpen), vertexIO); unsigned int topItemIndex = getTopOpenItemIndex(self); unsigned int bottomItemIndex = getBottomOpenItemIndex(self); unsigned int topItemIndexWithoutScrollArrow = topItemIndex; unsigned int highlightIndex = self->showKeyboardHighlight ? self->keyboardItemIndex : self->highlightedItemIndex; if (highlightIndex < self->itemCount && self->items[highlightIndex].enabled && (topItemIndex == 0 || highlightIndex != topItemIndex) && (bottomItemIndex == self->itemCount - 1 || highlightIndex != bottomItemIndex)) { Rect4f highlightBounds; highlightBounds.xMin = bounds.xMin + 1; highlightBounds.xMax = bounds.xMax - 1; highlightBounds.yMax = bounds.yMax - self->rowHeight * (highlightIndex - topItemIndex) - 1; highlightBounds.yMin = highlightBounds.yMax - self->rowHeight; call_virtual(drawQuad, drawingInterface, highlightBounds, getAppearanceAtlasEntry(self->appearance, UIToolkit_white).bounds, getAppearanceColor4f(self->appearance, UIPopUpMenu_highlightColor), vertexIO); } UIAtlasEntry arrowAtlasEntry = getAppearanceAtlasEntry(self->appearance, UIPopUpMenu_popUpScrollArrow); Rect4f arrowAtlasEntryBounds = UIAtlasEntry_boundsForScale(arrowAtlasEntry, drawingInterface->scaleFactor); Rect4f arrowBounds; arrowBounds.xMin = roundpositivef(bounds.xMin + (bounds.xMax - bounds.xMin - arrowAtlasEntry.size.x) / 2); arrowBounds.xMax = arrowBounds.xMin + arrowAtlasEntry.size.x; if (topItemIndex > 0) { Rect4f arrowAtlasEntryFlipped = arrowAtlasEntryBounds; arrowAtlasEntryFlipped.yMax = arrowAtlasEntryBounds.yMin; arrowAtlasEntryFlipped.yMin = arrowAtlasEntryBounds.yMax; arrowBounds.yMax = roundpositivef(bounds.yMax - (self->rowHeight - arrowAtlasEntry.size.y) / 2 - 1); arrowBounds.yMin = arrowBounds.yMax - arrowAtlasEntry.size.y; call_virtual(drawQuad, drawingInterface, arrowBounds, arrowAtlasEntryFlipped, COLOR4f(1.0f, 1.0f, 1.0f, 1.0f), vertexIO); topItemIndex++; } if (bottomItemIndex < self->itemCount - 1) { arrowBounds.yMin = roundpositivef(bounds.yMin + (self->rowHeight - arrowAtlasEntry.size.y) / 2 + 1); arrowBounds.yMax = arrowBounds.yMin + arrowAtlasEntry.size.y; call_virtual(drawQuad, drawingInterface, arrowBounds, arrowAtlasEntryBounds, COLOR4f(1.0f, 1.0f, 1.0f, 1.0f), vertexIO); bottomItemIndex--; } if (bottomItemIndex != UINT_MAX) { UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, drawingInterface); float lineHeight = call_virtual(getLineHeight, typeface); Color4f textColorDefault = getAppearanceColor4f(self->appearance, UIPopUpMenu_textColorOpen); Color4f textColorHighlight = getAppearanceColor4f(self->appearance, UIPopUpMenu_highlightTextColor); Color4f textColorDisabled = getAppearanceColor4f(self->appearance, UIPopUpMenu_textColorDisabled); Rect4f textPadding = getAppearanceRect4f(self->appearance, UIPopUpMenu_textPaddingOpen); for (unsigned int itemIndex = topItemIndex; itemIndex <= bottomItemIndex; itemIndex++) { float offsetY = self->rowHeight * (itemIndex - topItemIndexWithoutScrollArrow); Color4f textColor = self->items[itemIndex].enabled ? (itemIndex == highlightIndex) ? textColorHighlight : textColorDefault : textColorDisabled; call_virtual(drawString, drawingInterface, typeface, self->items[itemIndex].title, VECTOR2f(bounds.xMin + textPadding.xMin, bounds.yMax - offsetY - (self->rowHeight - lineHeight) / 2 - 1), VECTOR2f(0.0f, 1.0f), 1.0f, textColor, vertexIO); if (self->items[itemIndex].shortcutString.bytes != NULL) { call_virtual(drawString, drawingInterface, typeface, self->items[itemIndex].shortcutString, VECTOR2f(bounds.xMax - textPadding.xMax, bounds.yMax - offsetY - (self->rowHeight - lineHeight) / 2 - 1), VECTOR2f(1.0f, 1.0f), 1.0f, textColor, vertexIO); } } } } static void measureItemMaxWidth(UIPopUpMenu * self) { float maxWidth = 20.0f; UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, UIToolkit_currentContext()->drawingInterface); Rect4f textPadding = getAppearanceRect4f(self->appearance, UIPopUpMenu_textPaddingOpen); float shortcutPadding = getAppearanceFloat(self->appearance, UIMenuBar_itemShortcutPadding); for (unsigned int itemIndex = 0; itemIndex < self->itemCount; itemIndex++) { float itemWidth = ceilf(call_virtual(measureString, typeface, self->items[itemIndex].title) + textPadding.xMin + textPadding.xMax); if (self->items[itemIndex].shortcutString.bytes != NULL) { itemWidth += ceilf(call_virtual(measureString, typeface, self->items[itemIndex].shortcutString)) + shortcutPadding; } if (maxWidth < itemWidth) { maxWidth = itemWidth; } } self->itemMaxWidth = maxWidth; } static void disposeItems(UIPopUpMenu * self) { for (unsigned int itemIndex = 0; itemIndex < self->itemCount; itemIndex++) { String_free(self->items[itemIndex].title); UIKeyShortcut_free(self->items[itemIndex].shortcut); String_free(self->items[itemIndex].shortcutString); } free(self->items); } static void assignTextListViewItems(UIPopUpMenu * self, unsigned int itemCount, UITextListView_item * items) { self->itemCount = itemCount; self->items = malloc(itemCount * sizeof(*self->items)); for (unsigned int itemIndex = 0; itemIndex < itemCount; itemIndex++) { self->items[itemIndex].title = String_copy(items[itemIndex].text); self->items[itemIndex].identifier = items[itemIndex].identifier; self->items[itemIndex].enabled = true; self->items[itemIndex].checked = false; self->items[itemIndex].shortcut = UIKeyShortcut_none; self->items[itemIndex].shortcutString = STR_NULL; } } static String createKeyShortcutString(UIPopUpMenu * self, UIKeyShortcut * shortcut) { if (shortcut == NULL) { return STR_NULL; } char shortcutString[40] = {0}; UIKeyShortcut_getHumanReadableString(shortcut, self->useLocalizedKeyShortcuts, false, shortcutString, sizeof(shortcutString)); return String_copy(STR(shortcutString)); } static void assignMenuItems(UIPopUpMenu * self, unsigned int itemCount, UIMenuItem * items) { self->itemCount = itemCount; self->items = malloc(itemCount * sizeof(*self->items)); for (unsigned int itemIndex = 0; itemIndex < itemCount; itemIndex++) { self->items[itemIndex].title = String_copy(items[itemIndex].title); self->items[itemIndex].identifier = items[itemIndex].identifier; self->items[itemIndex].enabled = items[itemIndex].enabled; self->items[itemIndex].checked = items[itemIndex].checked; self->items[itemIndex].shortcut = UIKeyShortcut_copy(items[itemIndex].shortcut); self->items[itemIndex].shortcutString = createKeyShortcutString(self, items[itemIndex].shortcut); } } static void sharedInit(UIPopUpMenu * self, Vector2f position, Vector2f relativeOrigin, float width, UIOverflowMode overflowMode, UIPopUpMenuActionCallback callback, void * callbackContext, UIAppearance appearance) { call_super(initNoRenderable, self, position, relativeOrigin, appearance); self->renderable = createUIElementRenderableWithDefaultSettings(self, PRIMITIVE_TRIANGLES); self->callback = callback; self->callbackContext = callbackContext; self->selectedItemIndex = 0; self->enabled = true; self->highlightedItemIndex = 0; self->lastFocusedElement = NULL; self->rollover = false; self->open = false; self->openTime = 0.0; self->overflowMode = overflowMode; UITypeface * typeface = UIToolkit_getUITypeface(self->appearance, UIToolkit_currentContext()->drawingInterface); float lineHeight = call_virtual(getLineHeight, typeface); Rect4f textPaddingOpen = getAppearanceRect4f(self->appearance, UIPopUpMenu_textPaddingOpen); self->rowHeight = lineHeight + textPaddingOpen.yMin + textPaddingOpen.yMax - 2; Rect4f textPaddingClosed = getAppearanceRect4f(self->appearance, UIPopUpMenu_textPaddingClosed); self->closedHeight = lineHeight + textPaddingClosed.yMin + textPaddingClosed.yMax; self->scrollTimer = SHELL_TIMER_INVALID; self->scrollingFast = false; self->width = width; self->useLocalizedKeyShortcuts = true; } static void sharedInit2(UIPopUpMenu * self) { measureItemMaxWidth(self); if (self->overflowMode == OVERFLOW_RESIZE) { self->width = self->itemMaxWidth; } } bool UIPopUpMenu_init(UIPopUpMenu * self, unsigned int itemCount, UITextListView_item * items, Vector2f position, Vector2f relativeOrigin, float width, UIOverflowMode overflowMode, UIPopUpMenuActionCallback callback, void * callbackContext, UIAppearance appearance) { sharedInit(self, position, relativeOrigin, width, overflowMode, callback, callbackContext, appearance); assignTextListViewItems(self, itemCount, items); sharedInit2(self); return true; } bool UIPopUpMenu_initWithMenuItems(UIPopUpMenu * self, unsigned int itemCount, UIMenuItem * items, Vector2f position, Vector2f relativeOrigin, float width, UIOverflowMode overflowMode, UIPopUpMenuActionCallback callback, void * callbackContext, UIAppearance appearance) { sharedInit(self, position, relativeOrigin, width, overflowMode, callback, callbackContext, appearance); assignMenuItems(self, itemCount, items); sharedInit2(self); return true; } void UIPopUpMenu_dispose(UIPopUpMenu * self) { disposeItems(self); Shell_cancelTimer(self->scrollTimer); call_super_virtual(dispose, self); } bool UIPopUpMenu_hitTest(UIPopUpMenu * self, float x, float y, UIHitTestType type, int * outPriority, bool * outForwardNext) { if (self->open) { *outPriority = POP_UP_MENU_OPEN_HIT_TEST_PRIORITY; return true; } return call_super_virtual(hitTest, self, x, y, type, outPriority, outForwardNext); } UIEventResponse UIPopUpMenu_mouseDown(UIPopUpMenu * self, unsigned int buttonNumber, unsigned int buttonMask, float x, float y, unsigned int modifiers, bool isFinalTarget, double referenceTime) { if (!self->visible || !self->enabled) { return RESPONSE_UNHANDLED; } if (self->open) { if (!Rect4f_containsVector2f(getOpenBounds(self), VECTOR2f(x, y))) { call_virtual(close, self); } else { self->openTime = 0.0; } } else { UIElement * topParent = UIElement_getTopParent(self); self->lastFocusedElement = call_virtual(getFocusedElement, topParent); if (self->lastFocusedElement != (UIElement *) self) { call_virtual(setFocusedElement, topParent, self, NULL, UI_NONE); } call_virtual(open, self, referenceTime); } return RESPONSE_HANDLED; } bool UIPopUpMenu_mouseUp(UIPopUpMenu * self, unsigned int buttonNumber, unsigned int buttonMask, float x, float y, unsigned int modifiers, double referenceTime) { if (self->open && referenceTime - self->openTime >= SINGLE_CLICK_INTERVAL) { if (Rect4f_containsVector2f(getOpenBounds(self), VECTOR2f(x, y)) && self->itemCount > 0 && self->highlightedItemIndex < self->itemCount && self->items[self->highlightedItemIndex].enabled) { unsigned int topItemIndex = getTopOpenItemIndex(self); unsigned int bottomItemIndex = getBottomOpenItemIndex(self); if ((topItemIndex == 0 || self->highlightedItemIndex != topItemIndex) && (bottomItemIndex == self->itemCount - 1 || self->highlightedItemIndex != bottomItemIndex)) { self->selectedItemIndex = self->highlightedItemIndex; if (self->callback != NULL) { self->callback(self, self->selectedItemIndex, self->items[self->selectedItemIndex].identifier, modifiers, referenceTime, self->callbackContext); } } } call_virtual(close, self); return true; } return false; } #define MOUSE_SCROLL_INTERVAL_SLOW 0.15 #define MOUSE_SCROLL_INTERVAL_FAST 0.05 static void scrollTimerCallback(ShellTimer timerID, void * context) { UIPopUpMenu * self = context; unsigned int topItemIndex = getTopOpenItemIndex(self); unsigned int bottomItemIndex = getBottomOpenItemIndex(self); if (self->highlightedItemIndex == topItemIndex && topItemIndex > 0) { self->openCenterIndex--; self->highlightedItemIndex--; Shell_redisplay(); } else if (self->highlightedItemIndex == bottomItemIndex && bottomItemIndex < self->itemCount - 1) { self->openCenterIndex++; self->highlightedItemIndex++; Shell_redisplay(); } else { Shell_cancelTimer(timerID); self->scrollTimer = SHELL_TIMER_INVALID; self->scrollingFast = false; } } static bool mouseMovedOrDragged(UIPopUpMenu * self, float x, float y) { if (self->open) { unsigned int lastHighlightedItemIndex = self->highlightedItemIndex; bool lastShowKeyboardHighlight = self->showKeyboardHighlight; self->showKeyboardHighlight = false; Rect4f openBounds = getOpenBounds(self); unsigned int topItemIndex = getTopOpenItemIndex(self); unsigned int bottomItemIndex = getBottomOpenItemIndex(self); if (Rect4f_containsVector2f(openBounds, VECTOR2f(x, y))) { self->highlightedItemIndex = ((openBounds.yMax - openBounds.yMin) - (y - openBounds.yMin)) / self->rowHeight + topItemIndex; if (self->highlightedItemIndex > bottomItemIndex) { self->highlightedItemIndex = bottomItemIndex; } } else { if (topItemIndex > 0 && x >= openBounds.xMin && x <= openBounds.xMax && y > openBounds.yMax - 1 && !self->scrollingFast) { self->highlightedItemIndex = topItemIndex; if (self->scrollTimer == SHELL_TIMER_INVALID) { self->openCenterIndex--; self->highlightedItemIndex--; } else { Shell_cancelTimer(self->scrollTimer); } self->scrollTimer = Shell_setTimer(MOUSE_SCROLL_INTERVAL_FAST, true, scrollTimerCallback, self); self->scrollingFast = true; return true; } if (bottomItemIndex < self->itemCount - 1 && x >= openBounds.xMin && x <= openBounds.xMax && y < openBounds.yMin + 1 && !self->scrollingFast) { self->highlightedItemIndex = bottomItemIndex; if (self->scrollTimer == SHELL_TIMER_INVALID) { self->openCenterIndex++; self->highlightedItemIndex++; } else { Shell_cancelTimer(self->scrollTimer); } self->scrollTimer = Shell_setTimer(MOUSE_SCROLL_INTERVAL_FAST, true, scrollTimerCallback, self); self->scrollingFast = true; return true; } if (!self->scrollingFast || x < openBounds.xMin || x > openBounds.xMax) { self->highlightedItemIndex = ITEM_INDEX_NONE; } } if (self->highlightedItemIndex == topItemIndex && topItemIndex > 0) { if (self->scrollTimer == SHELL_TIMER_INVALID) { self->openCenterIndex--; self->highlightedItemIndex--; self->scrollTimer = Shell_setTimer(MOUSE_SCROLL_INTERVAL_SLOW, true, scrollTimerCallback, self); self->scrollingFast = false; } } else if (self->highlightedItemIndex == bottomItemIndex && bottomItemIndex < self->itemCount - 1) { if (self->scrollTimer == SHELL_TIMER_INVALID) { self->openCenterIndex++; self->highlightedItemIndex++; self->scrollTimer = Shell_setTimer(MOUSE_SCROLL_INTERVAL_SLOW, true, scrollTimerCallback, self); self->scrollingFast = false; } } else if (x < openBounds.xMin || x > openBounds.xMax || (x > openBounds.yMin && x < openBounds.yMax)) { Shell_cancelTimer(self->scrollTimer); self->scrollTimer = SHELL_TIMER_INVALID; self->scrollingFast = false; } return lastHighlightedItemIndex != self->highlightedItemIndex || lastShowKeyboardHighlight != self->showKeyboardHighlight; } return false; } bool UIPopUpMenu_mouseMoved(UIPopUpMenu * self, float x, float y, float deltaX, float deltaY, unsigned int modifiers, double referenceTime) { bool wasRolledOver = self->rollover; UIElement * topParent = UIElement_getTopParent(self); Vector2f rootPosition = UIElement_localToRootVector(self, VECTOR2f(x, y)); bool wasOpen = self->open; self->open = false; self->rollover = UIElement_hitTestSingle(topParent, rootPosition.x, rootPosition.y, HIT_TEST_MOUSE_OVER) == (UIElement *) self; self->open = wasOpen; if (!self->open && self->rollover != wasRolledOver) { self->dirty = true; return true; } return mouseMovedOrDragged(self, x, y); } bool UIPopUpMenu_mouseDragged(UIPopUpMenu * self, unsigned int buttonMask, float x, float y, float deltaX, float deltaY, unsigned int modifiers, double referenceTime) { return mouseMovedOrDragged(self, x, y); } bool UIPopUpMenu_mouseLeave(UIPopUpMenu * self, unsigned int modifiers, double referenceTime) { bool wasRolledOver = self->rollover; self->rollover = false; if (!self->open && self->rollover != wasRolledOver) { self->dirty = true; return true; } return false; } UIEventResponse UIPopUpMenu_scrollWheel(UIPopUpMenu * self, float x, float y, int scrollDeltaX, int scrollDeltaY, unsigned int buttonMask, unsigned int modifiers, bool isFinalTarget, double referenceTime) { if (self->open) { unsigned int topItemIndex = getTopOpenItemIndex(self); unsigned int bottomItemIndex = getBottomOpenItemIndex(self); if (topItemIndex > 0 && scrollDeltaY < 0) { if ((unsigned int) -scrollDeltaY > topItemIndex) { scrollDeltaY = -topItemIndex; } self->openCenterIndex += scrollDeltaY; if (self->highlightedItemIndex != ITEM_INDEX_NONE) { self->highlightedItemIndex += scrollDeltaY; } return RESPONSE_HANDLED; } if (bottomItemIndex < self->itemCount - 1 && scrollDeltaY > 0) { if ((unsigned int) scrollDeltaY > self->itemCount - bottomItemIndex - 1) { scrollDeltaY = self->itemCount - bottomItemIndex - 1; } self->openCenterIndex += scrollDeltaY; if (self->highlightedItemIndex != ITEM_INDEX_NONE) { self->highlightedItemIndex += scrollDeltaY; } return RESPONSE_HANDLED; } return RESPONSE_IGNORE; } return RESPONSE_UNHANDLED; } static void scrollToItem(UIPopUpMenu * self, unsigned int itemIndex) { unsigned int topItemIndex = getTopOpenItemIndex(self); if (topItemIndex > 0 && itemIndex != 0) { topItemIndex++; } unsigned int bottomItemIndex = getBottomOpenItemIndex(self); if (bottomItemIndex < self->itemCount - 1 && itemIndex != self->itemCount - 1) { bottomItemIndex--; } if (itemIndex < topItemIndex) { self->openCenterIndex -= topItemIndex - itemIndex; } else if (itemIndex > bottomItemIndex) { self->openCenterIndex += itemIndex - bottomItemIndex; } } static bool keyboardNavigateToItem(UIPopUpMenu * self, int offset) { unsigned int lastKeyboardItemIndex = self->keyboardItemIndex; bool lastShowKeyboardHighlight = self->showKeyboardHighlight; if (self->keyboardItemIndex >= self->itemCount) { if (offset == -1) { self->keyboardItemIndex = self->itemCount - 1; while (!self->items[self->keyboardItemIndex].enabled && self->keyboardItemIndex < self->itemCount) { self->keyboardItemIndex--; } } else { self->keyboardItemIndex = 0; while (!self->items[self->keyboardItemIndex].enabled && self->keyboardItemIndex < self->itemCount) { self->keyboardItemIndex++; } } } else { do { self->keyboardItemIndex = (self->keyboardItemIndex + self->itemCount + offset) % self->itemCount; } while (!self->items[self->keyboardItemIndex].enabled && self->keyboardItemIndex != lastKeyboardItemIndex); } self->showKeyboardHighlight = true; scrollToItem(self, self->keyboardItemIndex); return lastKeyboardItemIndex != self->keyboardItemIndex || lastShowKeyboardHighlight != self->showKeyboardHighlight; } UIEventResponse UIPopUpMenu_keyDown(UIPopUpMenu * self, unsigned int charCode, unsigned int keyCode, unsigned int modifiers, bool isRepeat, bool isFinalTarget, double referenceTime) { if (self->open) { switch (keyCode) { case KEY_CODE_ESCAPE: call_virtual(close, self); return RESPONSE_HANDLED; case KEY_CODE_ENTER: case KEY_CODE_NUMPAD_ENTER: if (self->keyboardItemIndex < self->itemCount) { self->selectedItemIndex = self->keyboardItemIndex; if (self->callback != NULL) { self->callback(self, self->selectedItemIndex, self->items[self->selectedItemIndex].identifier, 0, referenceTime, self->callbackContext); } } call_virtual(close, self); return RESPONSE_HANDLED; case KEY_CODE_UP_ARROW: return keyboardNavigateToItem(self, -1); case KEY_CODE_DOWN_ARROW: return keyboardNavigateToItem(self, 1); } for (unsigned int itemIndex = 0; itemIndex < self->itemCount; itemIndex++) { if (self->items[itemIndex].enabled && UIKeyShortcut_isMatch(self->items[itemIndex].shortcut, keyCode, modifiers, NULL)) { // TODO: Short animation showing selected item self->selectedItemIndex = itemIndex; if (self->callback != NULL) { self->callback(self, itemIndex, self->items[itemIndex].identifier, modifiers, referenceTime, self->callbackContext); } call_virtual(close, self); break; } } return RESPONSE_HANDLED; } return call_super_virtual(keyDown, self, charCode, keyCode, modifiers, isRepeat, isFinalTarget, referenceTime); } bool UIPopUpMenu_menuActionDown(UIPopUpMenu * self, unsigned int actionNumber, bool isRepeat, double referenceTime) { if (!self->visible || !self->enabled) { return false; } if (actionNumber == 0 && !isRepeat) { if (self->open) { self->selectedItemIndex = self->highlightedItemIndex; if (self->callback != NULL) { self->callback(self, self->selectedItemIndex, self->items[self->selectedItemIndex].identifier, 0, referenceTime, self->callbackContext); } call_virtual(close, self); } else { UIElement * topParent = UIElement_getTopParent(self); self->lastFocusedElement = call_virtual(getFocusedElement, topParent); call_virtual(open, self, referenceTime); } return true; } if (actionNumber == 1 && self->open) { call_virtual(close, self); return true; } return false; } bool UIPopUpMenu_menuActionUp(UIPopUpMenu * self, unsigned int actionNumber, double referenceTime) { if (self->open && referenceTime - self->openTime >= SINGLE_CLICK_INTERVAL) { self->selectedItemIndex = self->highlightedItemIndex; if (self->callback != NULL) { self->callback(self, self->selectedItemIndex, self->items[self->selectedItemIndex].identifier, 0, referenceTime, self->callbackContext); } call_virtual(close, self); return true; } return false; } bool UIPopUpMenu_menuDirectionDown(UIPopUpMenu * self, UINavigationDirection direction, bool isRepeat, double referenceTime) { if (self->open) { if (direction == UI_UP || direction == UI_PREVIOUS) { return keyboardNavigateToItem(self, -1); } if (direction == UI_DOWN || direction == UI_NEXT) { return keyboardNavigateToItem(self, 1); } return false; } return call_super_virtual(menuDirectionDown, self, direction, isRepeat, referenceTime); } bool UIPopUpMenu_acceptsFocus(UIPopUpMenu * self, bool finalTargetOnly) { return self->visible && self->enabled; } bool UIPopUpMenu_ignoreClipForHitTest(UIPopUpMenu * self, UIHitTestType type) { return self->open; } Rect4f UIPopUpMenu_getBounds(UIPopUpMenu * self) { return UIElement_boundsRectWithOrigin(self->position, self->relativeOrigin, VECTOR2f(self->width, self->closedHeight)); } void UIPopUpMenu_draw(UIPopUpMenu * self, Vector2f offset, UIDrawingInterface * drawingInterface, VertexIO * vertexIO) { call_super(draw, self, offset, drawingInterface, vertexIO); if (self->open) { drawOpen(offset, drawingInterface, vertexIO, self); } else { drawClosed(offset, drawingInterface, vertexIO, self); } } void UIPopUpMenu_listRenderables(UIPopUpMenu * self, RenderableIO * renderableIO, int drawOrderOffset, Rect4i clipBounds) { self->dirty = false; if (!self->visible) { return; } clipBounds = UIElement_intersectClipBounds(clipBounds, call_virtual(getAbsoluteClipBounds, self)); RenderableIO_addRenderable(renderableIO, self->renderable, drawOrderOffset + (self->open ? 1000 : 0), self->open ? RECT4i_EMPTY : clipBounds); } void UIPopUpMenu_setItems(UIPopUpMenu * self, unsigned int itemCount, UITextListView_item * items) { int selectedIdentifier = UIPopUpMenu_getSelectedItemIdentifier(self); disposeItems(self); assignTextListViewItems(self, itemCount, items); UIPopUpMenu_setSelectedItemIdentifier(self, selectedIdentifier); measureItemMaxWidth(self); if (self->overflowMode == OVERFLOW_RESIZE) { self->width = self->itemMaxWidth; } } void UIPopUpMenu_setMenuItems(UIPopUpMenu * self, unsigned int itemCount, UIMenuItem * items) { int selectedIdentifier = UIPopUpMenu_getSelectedItemIdentifier(self); disposeItems(self); assignMenuItems(self, itemCount, items); UIPopUpMenu_setSelectedItemIdentifier(self, selectedIdentifier); measureItemMaxWidth(self); if (self->overflowMode == OVERFLOW_RESIZE) { self->width = self->itemMaxWidth; } } bool UIPopUpMenu_setSelectedItemIdentifier(UIPopUpMenu * self, int identifier) { if (identifier == ITEM_IDENTIFIER_NONE) { self->selectedItemIndex = ITEM_INDEX_NONE; return true; } for (unsigned int itemIndex = 0; itemIndex < self->itemCount; itemIndex++) { if (self->items[itemIndex].identifier == identifier) { self->selectedItemIndex = itemIndex; return true; } } return false; } int UIPopUpMenu_getSelectedItemIdentifier(UIPopUpMenu * self) { if (self->selectedItemIndex >= self->itemCount) { return ITEM_IDENTIFIER_NONE; } return self->items[self->selectedItemIndex].identifier; } Rect4f UIPopUpMenu_getItemBounds(UIPopUpMenu * self, unsigned int itemIndex) { Rect4f bounds = getOpenBounds(self); unsigned int topItemIndex = getTopOpenItemIndex(self); Rect4f itemBounds; itemBounds.xMin = bounds.xMin + 1; itemBounds.xMax = bounds.xMax - 1; itemBounds.yMax = bounds.yMax - self->rowHeight * (itemIndex - topItemIndex) - 1; itemBounds.yMin = itemBounds.yMax - self->rowHeight; return itemBounds; } void UIPopUpMenu_open(UIPopUpMenu * self, double referenceTime) { self->open = true; self->openTime = referenceTime; self->openCenterIndex = self->selectedItemIndex; self->showKeyboardHighlight = true; if (self->openCenterIndex >= self->itemCount) { self->openCenterIndex = 0; } self->highlightedItemIndex = self->keyboardItemIndex = self->selectedItemIndex; if (self->highlightedItemIndex == ITEM_INDEX_NONE && self->itemCount > 0) { self->highlightedItemIndex = 0; } Rect4f safeBounds = UIElement_rootToLocalRect(self, getAppearanceRect4f(self->appearance, UIToolkit_safeDisplayBounds)); Rect4f closedBounds = call_virtual(getBounds, self); int rowsAbove = (safeBounds.yMax - closedBounds.yMax) / self->rowHeight; if (rowsAbove < 1) { self->openItemCountMaxAbove = 1; } else { self->openItemCountMaxAbove = rowsAbove; } int rowsBelow = (closedBounds.yMin - safeBounds.yMin) / self->rowHeight; if (rowsBelow < 1) { self->openItemCountMaxBelow = 1; } else { self->openItemCountMaxBelow = rowsBelow; } } void UIPopUpMenu_close(UIPopUpMenu * self) { self->open = false; UIElement * topParent = UIElement_getTopParent(self); if (topParent != NULL && self->lastFocusedElement != (UIElement *) self) { if (self->lastFocusedElement == NULL) { UIElement_unfocus(self); } else { call_virtual(setFocusedElement, topParent, self->lastFocusedElement, NULL, UI_NONE); } } self->lastFocusedElement = NULL; } void UIPopUpMenu_refreshAllShortcutStrings(UIPopUpMenu * self) { for (unsigned int itemIndex = 0; itemIndex < self->itemCount; itemIndex++) { String_free(self->items[itemIndex].shortcutString); self->items[itemIndex].shortcutString = createKeyShortcutString(self, self->items[itemIndex].shortcut); } }