/* 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 "shell/ShellKeyCodes.h" #include "uitoolkit/UIModalConfirmationDialog.h" #include "uitoolkit/UIToolkitAppearance.h" #include #define stemobject_implementation UIModalConfirmationDialog v_begin(); v_func(dispose); v_func(dialogKeyDown); v_end(); UIModalConfirmationDialog * UIModalConfirmationDialog_create(String title, String displayedMessage, UIModalConfirmationDialog_callback callback, void * callbackContext1, void * callbackContext2, UIWindowRoot * windowRoot, UIAppearance appearance, const char * button1Label, UIKeyShortcut * button1KeyShortcut, ...) { va_list args; va_start(args, button1KeyShortcut); UIModalConfirmationDialog * self = UIModalConfirmationDialog_vcreate(title, displayedMessage, callback, callbackContext1, callbackContext2, windowRoot, appearance, button1Label, button1KeyShortcut, args); va_end(args); return self; } UIModalConfirmationDialog * UIModalConfirmationDialog_vcreate(String title, String displayedMessage, UIModalConfirmationDialog_callback callback, void * callbackContext1, void * callbackContext2, UIWindowRoot * windowRoot, UIAppearance appearance, const char * button1Label, UIKeyShortcut * button1KeyShortcut, va_list args) { stemobject_create_implementation(vinit, title, displayedMessage, callback, callbackContext1, callbackContext2, windowRoot, appearance, button1Label, button1KeyShortcut, args) } bool UIModalConfirmationDialog_init(UIModalConfirmationDialog * self, String title, String displayedMessage, UIModalConfirmationDialog_callback callback, void * callbackContext1, void * callbackContext2, UIWindowRoot * windowRoot, UIAppearance appearance, const char * button1Label, UIKeyShortcut * button1KeyShortcut, ...) { va_list args; va_start(args, button1KeyShortcut); bool success = UIModalConfirmationDialog_vinit(self, title, displayedMessage, callback, callbackContext1, callbackContext2, windowRoot, appearance, button1Label, button1KeyShortcut, args); va_end(args); return success; } static void buttonCallback(UIButton * button, unsigned int modifiers, double timestamp, void * context) { UIModalConfirmationDialog * self = context; for (unsigned int buttonIndex = 0; buttonIndex < self->buttonCount; buttonIndex++) { if (self->buttons[buttonIndex].button == button) { self->callback(self, buttonIndex, self->callbackContext1, self->callbackContext2); break; } } } static void assignKeyShortcutTooltip(UIButton * button, UIKeyShortcut * shortcut) { if (shortcut != NULL) { char tooltipString[32]; UIKeyShortcut_getHumanReadableString(shortcut, true, true, tooltipString, sizeof(tooltipString)); call_virtual(setTooltipString, button, STR(tooltipString)); } } bool UIModalConfirmationDialog_vinit(UIModalConfirmationDialog * self, String title, String displayedMessage, UIModalConfirmationDialog_callback callback, void * callbackContext1, void * callbackContext2, UIWindowRoot * windowRoot, UIAppearance appearance, const char * button1Label, UIKeyShortcut * button1KeyShortcut, va_list args) { self->appearance = appearance; unsigned int buttonCount = 1; va_list copy; va_copy(copy, args); while (va_arg(copy, const char *) != NULL) { va_arg(copy, UIKeyShortcut *); buttonCount++; } va_end(copy); struct UIModalConfirmationDialog_button * buttons = malloc(buttonCount * sizeof(*buttons)); float edgePadding = getAppearanceFloat(self->appearance, UIModalDialog_edgePadding); float dialogButtonWidth = getAppearanceFloat(self->appearance, UIModalDialog_buttonWidth); float buttonPadding = getAppearanceFloat(self->appearance, UIModalDialog_buttonPadding); buttons[0].button = UIButton_create(STR(button1Label), VECTOR2f(0.0f, edgePadding), VECTOR2f_ZERO, dialogButtonWidth, OVERFLOW_RESIZE, buttonCallback, self, self->appearance); buttons[0].keyShortcut = UIKeyShortcut_copy(button1KeyShortcut); assignKeyShortcutTooltip(buttons[0].button, buttons[0].keyShortcut); float buttonWidth = buttons[0].button->width; for (unsigned int buttonIndex = 1; buttonIndex < buttonCount; buttonIndex++) { const char * buttonTitle = va_arg(args, const char *); buttons[buttonIndex].button = UIButton_create(STR(buttonTitle), VECTOR2f(0.0f, edgePadding), VECTOR2f_ZERO, dialogButtonWidth, OVERFLOW_RESIZE, buttonCallback, self, self->appearance); buttons[buttonIndex].keyShortcut = UIKeyShortcut_copy(va_arg(args, UIKeyShortcut *)); assignKeyShortcutTooltip(buttons[buttonIndex].button, buttons[buttonIndex].keyShortcut); buttonWidth += buttonPadding + buttons[buttonIndex].button->width; } self->messageLabelAppearance = UIAppearance_init(&self->appearance); setAppearanceReference(&self->messageLabelAppearance, UILabel_textColor, UIModalDialog_messageTextColor); self->messageLabel = UILabel_create(displayedMessage, VECTOR2f(edgePadding, call_virtual(getBounds, buttons[0].button).yMax + getAppearanceFloat(self->appearance, UIModalDialog_messagePadding)), VECTOR2f(getAppearanceFloat(self->appearance, UIModalDialog_messageWidthMax), 0.0f), VECTOR2f_ZERO, ALIGN_LEFT, WRAP_DEFAULT, OVERFLOW_RESIZE, OVERFLOW_RESIZE, self->messageLabelAppearance); float width = self->messageLabel->size.x; if (buttonWidth > width) { width = buttonWidth; } width += edgePadding * 2; float height = call_virtual(getBounds, self->messageLabel).yMax + edgePadding; call_super(init, self, VECTOR2f(0.0f, 0.0f), VECTOR2f(width, height), title, NULL, NULL, NULL, windowRoot, self->appearance); self->callback = callback; self->callbackContext1 = callbackContext1; self->callbackContext2 = callbackContext2; self->buttonCount = buttonCount; self->buttons = buttons; call_virtual(addElement, self, self->messageLabel, true); self->buttons[0].button->position.x = self->size.x - edgePadding - self->buttons[0].button->width; call_virtual(addElement, self, self->buttons[0].button, true); for (unsigned int buttonIndex = 1; buttonIndex < self->buttonCount; buttonIndex++) { self->buttons[buttonIndex].button->position.x = self->buttons[buttonIndex - 1].button->position.x - buttonPadding - self->buttons[buttonIndex].button->width; call_virtual(addElement, self, self->buttons[buttonIndex].button, true); UIElement_connect(self->buttons[buttonIndex - 1].button, UI_LEFT | UI_PREVIOUS, self->buttons[buttonIndex].button, true); } UIElement_connect(self->buttons[0].button, UI_NEXT, self->buttons[self->buttonCount - 1].button, true); UIElement_connect(self, UI_LEFT | UI_RIGHT | UI_UP | UI_DOWN | UI_NEXT | UI_PREVIOUS, self->buttons[0].button, false); bool defaultButtonAssigned = false; self->defaultButtonAppearance = UIAppearance_init(&self->appearance); for (unsigned int buttonIndex = 0; buttonIndex < self->buttonCount; buttonIndex++) { if (self->buttons[buttonIndex].keyShortcut != NULL && self->buttons[buttonIndex].keyShortcut->keyCode == KEY_CODE_ENTER && self->buttons[buttonIndex].keyShortcut->modifiers == 0) { setAppearanceReference(&self->defaultButtonAppearance, UIButton_backgroundColor, UIModalDialog_defaultButtonColor); self->buttons[buttonIndex].button->appearance = self->defaultButtonAppearance; call_virtual(setFocusedElement, self, self->buttons[buttonIndex].button, NULL, UI_NONE); defaultButtonAssigned = true; } } if (!defaultButtonAssigned) { call_virtual(setFocusedElement, self, self->buttons[0].button, NULL, UI_NONE); } return true; } void UIModalConfirmationDialog_dispose(UIModalConfirmationDialog * self) { UIAppearance_dispose(self->messageLabelAppearance); UIAppearance_dispose(self->defaultButtonAppearance); for (unsigned int buttonIndex = 0; buttonIndex < self->buttonCount; buttonIndex++) { UIKeyShortcut_free(self->buttons[buttonIndex].keyShortcut); } call_super_virtual(dispose, self); } bool UIModalConfirmationDialog_dialogKeyDown(UIModalConfirmationDialog * self, unsigned int charCode, unsigned int keyCode, unsigned int modifiers, bool isRepeat, double referenceTime) { HashTable * keyEquivalents = getDefaultKeyEquivalents(); for (unsigned int buttonIndex = 0; buttonIndex < self->buttonCount; buttonIndex++) { if (UIKeyShortcut_isMatch(self->buttons[buttonIndex].keyShortcut, keyCode, modifiers, keyEquivalents)) { UIButton_simulateClick(self->buttons[buttonIndex].button, 0); return true; } } return call_super_virtual(dialogKeyDown, self, charCode, keyCode, modifiers, isRepeat, referenceTime); }