rem: if (menu->frame_y+menu->frame->top_width >= scr_height) new_y = scr_height - menu->frame->top_width;  if (menu->frame_x+MENUW(menu) >= scr_width) new_x = scr_width-MENUW(menu)-1;
add: if (menu->frame_y+menu->frame->top_width >= rect.pos.y + rect.size.height) new_y = rect.pos.y + rect.size.height - menu->frame->top_width;  if (menu->frame_x+MENUW(menu) >= rect.pos.x + rect.size.width) new_x = rect.pos.x + rect.size.width - MENUW(menu) - 1;
context: keyboardMenu(WMenu *menu){    XEvent event;    KeySym ksym=NoSymbol;    int done=0;    int index;    WMenuEntry *entry;    int old_pos_x = menu->frame_x;    int old_pos_y = menu->frame_y;    int new_x = old_pos_x, new_y = old_pos_y;    int scr_width = menu->frame->screen_ptr->scr_width;    int scr_height = menu->frame->screen_ptr->scr_height;    if (menu->flags.editing)        return False;    XGrabKeyboard(dpy, menu->frame->core->window, True, GrabModeAsync,                  GrabModeAsync, CurrentTime);    if (menu->frame_y+menu->frame->top_width >= scr_height)        new_y = scr_height - menu->frame->top_width;    if (menu->frame_x+MENUW(menu) >= scr_width)        new_x = scr_width-MENUW(menu)-1;    move_menus(menu, new_x, new_y);    while (!done && menu->flags.mapped) {        XAllowEvents(dpy, AsyncKeyboard, CurrentTime);        WMMaskEvent(dpy, ExposureMask|ButtonMotionMask|ButtonPressMask                    |ButtonReleaseMask|KeyPressMask|KeyReleaseMask                    |SubstructureNotifyMask, &event);        switch (event.type) {        case KeyPress:            ksym = XLookupKeysym(&event.xkey, 0);            switch (ksym) {            case XK_Escape:                done = 1;                break;            case XK_Home:#ifdef XK_KP_Home            case XK_KP_Home:#endif                selectEntry(menu, 0);                makeVisible(menu);                break;            case XK_End:#ifdef XK_KP_End            case XK_KP_End:#endif                selectEntry(menu, menu->entry_no-1);                makeVisible(menu);                break;            case XK_Up:#ifdef ARROWLESS_KBD            case XK_k:#endif#ifdef XK_KP_Up            case XK_KP_Up:#endif                if (menu->selected_entry <= 0)                    selectEntry(menu, menu->entry_no-1);                else                    selectEntry(menu, menu->selected_entry-1);                makeVisible(menu);                break;            case XK_Down:#ifdef ARROWLESS_KBD            case XK_j:#endif#ifdef XK_KP_Down            case XK_KP_Down:#endif                if (menu->selected_entry<0)                    selectEntry(menu, 0);                else if (menu->selected_entry == menu->entry_no-1)                    selectEntry(menu, 0);                else if (menu->selected_entry < menu->entry_no-1)                    selectEntry(menu, menu->selected_entry+1);                makeVisible(menu);                break;            case XK_Right:#ifdef ARROWLESS_KBD            case XK_l:#endif#ifdef XK_KP_Right            case XK_KP_Right:#endif                if (menu->selected_entry>=0) {                    WMenuEntry *entry;                    entry = menu->entries[menu->selected_entry];                    if (entry->cascade >= 0 && menu->cascades                        && menu->cascades[entry->cascade]->entry_no > 0) {                        XUngrabKeyboard(dpy, CurrentTime);                        selectEntry(menu->cascades[entry->cascade], 0);                        if (!keyboardMenu(menu->cascades[entry->cascade]))                            done = 1;                        XGrabKeyboard(dpy, menu->frame->core->window, True,                                      GrabModeAsync, GrabModeAsync,                                      CurrentTime);                    }                }                break;            case XK_Left:#ifdef ARROWLESS_KBD            case XK_h:#endif#ifdef XK_KP_Left            case XK_KP_Left:#endif                if (menu->parent!=NULL && menu->parent->selected_entry>=0) {                    selectEntry(menu, -1);                    move_menus(menu, old_pos_x, old_pos_y);                    return True;                }                break;            case XK_Return:                done = 2;                break;            default:                index = check_key(menu, &event.xkey);                if (index>=0) {                    selectEntry(menu, index);                }            }            break;        default:            if (event.type==ButtonPress)                done = 1;            WMHandleEvent(&event);        }    }    XUngrabKeyboard(dpy, CurrentTime);    if (done==2 && menu->selected_entry>=0) {        entry = menu->entries[menu->selected_entry];    } else {        entry = NULL;    }    if (entry && entry->callback!=NULL && entry->flags.enabled        && entry->cascade < 0) {#if (MENU_BLINK_COUNT > 0)        int sel = menu->selected_entry;        int i;        for (i=0; i<MENU_BLINK_COUNT; i++) {            paintEntry(menu, sel, False);            XSync(dpy, 0);            wusleep(MENU_BLINK_DELAY);            paintEntry(menu, sel, True);            XSync(dpy, 0);            wusleep(MENU_BLINK_DELAY);        }#endif        selectEntry(menu, -1);        if (!menu->flags.buttoned) {            wMenuUnmap(menu);            move_menus(menu, old_pos_x, old_pos_y);        }        closeCascade(menu);        (*entry->callback)(menu, entry);    } else {        if (!menu->flags.buttoned) {            wMenuUnmap(menu);            move_menus(menu, old_pos_x, old_pos_y);        }        selectEntry(menu, -1);    }    /* returns True if returning from a submenu to a parent menu,     * False if exiting from menu */    return False;}