diff --git a/src/menu.c b/src/menu.c index 6d10523..528deeb 100644 --- a/src/menu.c +++ b/src/menu.c @@ -1117,39 +1117,35 @@ static WMenu *findMenu(WScreen * scr, int *x_ret, int *y_ret) wm_backend->pointer_get_position(&x, &y); - /* Check root menu and its cascades */ + /* Helper: check if (x,y) is inside a mapped menu */ +#define CHECK_MENU(m) do { \ + if ((m) && (m)->flags.mapped && \ + x >= (m)->frame_x && x < (m)->frame_x + MENUW(m) && \ + y >= (m)->frame_y && y < (m)->frame_y + MENUH(m)) { \ + *x_ret = x - (m)->frame_x; \ + *y_ret = y - (m)->frame_y; \ + return (m); \ + } \ +} while(0) + + /* Check root menu, its brother, and cascades */ menu = scr->root_menu; - if (menu && menu->flags.mapped) { - if (x >= menu->frame_x && x < menu->frame_x + MENUW(menu) && - y >= menu->frame_y && y < menu->frame_y + MENUH(menu)) { - *x_ret = x - menu->frame_x; - *y_ret = y - menu->frame_y; - return menu; - } - /* Check cascades */ + if (menu) { + CHECK_MENU(menu); + CHECK_MENU(menu->brother); for (int i = 0; i < menu->cascade_no; i++) { - WMenu *sub = menu->cascades[i]; - if (sub && sub->flags.mapped && - x >= sub->frame_x && x < sub->frame_x + MENUW(sub) && - y >= sub->frame_y && y < sub->frame_y + MENUH(sub)) { - *x_ret = x - sub->frame_x; - *y_ret = y - sub->frame_y; - return sub; - } + CHECK_MENU(menu->cascades[i]); + if (menu->cascades[i]) + CHECK_MENU(menu->cascades[i]->brother); } } /* Check switch menu */ menu = scr->switch_menu; - if (menu && menu->flags.mapped) { - if (x >= menu->frame_x && x < menu->frame_x + MENUW(menu) && - y >= menu->frame_y && y < menu->frame_y + MENUH(menu)) { - *x_ret = x - menu->frame_x; - *y_ret = y - menu->frame_y; - return menu; - } - } + CHECK_MENU(menu); + if (menu) CHECK_MENU(menu->brother); +#undef CHECK_MENU return NULL; }