From 14eec09b1d6f0d11292b81e23633f6c764755d0f Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Tue, 21 Sep 2021 14:37:19 +0300 Subject: Fixed issues with popup windows --- src/ui/root.c | 3 ++- src/ui/util.c | 37 +++++++++++++++++++++++++++++++------ src/ui/util.h | 1 + src/ui/window.c | 1 - 4 files changed, 34 insertions(+), 8 deletions(-) (limited to 'src/ui') diff --git a/src/ui/root.c b/src/ui/root.c index 2e883787..90c0c6e4 100644 --- a/src/ui/root.c +++ b/src/ui/root.c @@ -436,8 +436,9 @@ static void updateNavBarIdentity_(iWidget *navBar) { const iString *subjectName = ident ? name_GmIdentity(ident) : NULL; const char * idLabel = subjectName ? format_CStr(uiTextAction_ColorEscape "%s", cstr_String(subjectName)) - : "///${menu.identity.notactive}"; + : "${menu.identity.notactive}"; setMenuItemLabelByIndex_Widget(menu, 0, idLabel); + setMenuItemDisabledByIndex_Widget(menu, 0, !ident); iLabelWidget *toolButton = findWidget_App("toolbar.ident"); iLabelWidget *toolName = findWidget_App("toolbar.name"); if (toolName) { diff --git a/src/ui/util.c b/src/ui/util.c index 6d518282..c75d7698 100644 --- a/src/ui/util.c +++ b/src/ui/util.c @@ -811,7 +811,7 @@ static void deleteMenuItems_(iArray *items) { iWidget *makeMenu_Widget(iWidget *parent, const iMenuItem *items, size_t n) { iWidget *menu = new_Widget(); -#if defined (iHaveNativeMenus) +#if defined (iHaveNativeContextMenus) setFlags_Widget(menu, hidden_WidgetFlag | nativeMenu_WidgetFlag, iTrue); setUserData_Object(menu, deepCopyMenuItems_(menu, items, n)); addChild_Widget(parent, menu); @@ -949,6 +949,9 @@ void setMenuItemLabel_Widget(iWidget *menu, const char *command, const char *new } void setMenuItemLabelByIndex_Widget(iWidget *menu, size_t index, const char *newLabel) { + if (!menu) { + return; + } if (flags_Widget(menu) & nativeMenu_WidgetFlag) { iArray *items = userData_Object(menu); iAssert(items); @@ -976,7 +979,7 @@ iLocalDef iBool isUsingMenuPopupWindows_(void) { } void releaseNativeMenu_Widget(iWidget *d) { -#if defined (iHaveNativeMenus) +#if defined (iHaveNativeContextMenus) iArray *items = userData_Object(d); iAssert(flags_Widget(d) & nativeMenu_WidgetFlag); iAssert(items); @@ -988,7 +991,7 @@ void releaseNativeMenu_Widget(iWidget *d) { } void openMenuFlags_Widget(iWidget *d, iInt2 windowCoord, iBool postCommands) { -#if defined (iHaveNativeMenus) +#if defined (iHaveNativeContextMenus) const iArray *items = userData_Object(d); iAssert(flags_Widget(d) & nativeMenu_WidgetFlag); iAssert(items); @@ -1014,9 +1017,19 @@ void openMenuFlags_Widget(iWidget *d, iInt2 windowCoord, iBool postCommands) { setFlags_Widget(d, keepOnTop_WidgetFlag, iFalse); setUserData_Object(d, parent_Widget(d)); removeChild_Widget(parent_Widget(d), d); /* we'll borrow the widget for a while */ - iInt2 mousePos; - SDL_GetGlobalMouseState(&mousePos.x, &mousePos.y); - iWindow *win = newPopup_Window(sub_I2(mousePos, divi_I2(gap2_UI, 2)), d); + const float pixelRatio = get_Window()->pixelRatio; + iInt2 menuPos = add_I2(get_MainWindow()->place.normalRect.pos, + divf_I2(windowCoord, pixelRatio)); + arrange_Widget(d); + /* Check display bounds. */ { + const iInt2 menuSize = divf_I2(d->rect.size, pixelRatio); + SDL_Rect displayRect; + SDL_GetDisplayBounds(SDL_GetWindowDisplayIndex(get_Window()->win), &displayRect); + menuPos.x = iMin(menuPos.x, displayRect.x + displayRect.w - menuSize.x); + menuPos.y = iMin(menuPos.y, displayRect.y + displayRect.h - menuSize.y); + } + // SDL_GetGlobalMouseState(&mousePos.x, &mousePos.y); + iWindow *win = newPopup_Window(menuPos, d); SDL_SetWindowTitle(win->win, "Menu"); addPopup_App(win); /* window takes the widget */ SDL_ShowWindow(win->win); @@ -1134,6 +1147,18 @@ void setMenuItemDisabled_Widget(iWidget *menu, const char *command, iBool disabl } } +void setMenuItemDisabledByIndex_Widget(iWidget *menu, size_t index, iBool disable) { + if (!menu) { + return; + } + if (flags_Widget(menu) & nativeMenu_WidgetFlag) { + setDisabled_NativeMenuItem(at_Array(userData_Object(menu), index), disable); + } + else { + setFlags_Widget(child_Widget(menu, index), disabled_WidgetFlag, disable); + } +} + int checkContextMenu_Widget(iWidget *menu, const SDL_Event *ev) { if (menu && ev->type == SDL_MOUSEBUTTONDOWN && ev->button.button == SDL_BUTTON_RIGHT) { if (isVisible_Widget(menu)) { diff --git a/src/ui/util.h b/src/ui/util.h index 574a255f..5d092700 100644 --- a/src/ui/util.h +++ b/src/ui/util.h @@ -241,6 +241,7 @@ iChar removeIconPrefix_String (iString *); iLabelWidget * findMenuItem_Widget (iWidget *menu, const char *command); iMenuItem * findNativeMenuItem_Widget (iWidget *menu, const char *commandSuffix); void setMenuItemDisabled_Widget (iWidget *menu, const char *command, iBool disable); +void setMenuItemDisabledByIndex_Widget(iWidget *menu, size_t index, iBool disable); void setMenuItemLabel_Widget (iWidget *menu, const char *command, const char *newLabel); void setMenuItemLabelByIndex_Widget (iWidget *menu, size_t index, const char *newLabel); diff --git a/src/ui/window.c b/src/ui/window.c index 27ef2bc3..3f8faaea 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -1582,7 +1582,6 @@ int snap_MainWindow(const iMainWindow *d) { /*----------------------------------------------------------------------------------------------*/ iWindow *newPopup_Window(iInt2 screenPos, iWidget *rootWidget) { - arrange_Widget(rootWidget); iWindow *win = new_Window(popup_WindowType, (iRect){ screenPos, divf_I2(rootWidget->rect.size, get_Window()->pixelRatio) }, -- cgit v1.2.3