Function: menu-bar-menu-at-x-y

menu-bar-menu-at-x-y is a function defined in menu.c.

Signature

(menu-bar-menu-at-x-y X Y &optional FRAME)

Documentation

Return the menu-bar menu on FRAME at pixel coordinates X, Y.

X and Y are frame-relative pixel coordinates, assumed to define a location within the menu bar. If FRAME is nil or omitted, it defaults to the selected frame.

Value is the symbol of the menu at X/Y, or nil if the specified coordinates are not within the FRAME's menu bar. The symbol can be used to look up the menu like this:

     (lookup-key MAP [menu-bar SYMBOL])

where MAP is either the current global map or the current local map, since menu-bar items come from both.

This function can return non-nil only on a text-terminal frame or on an X frame that doesn't use any GUI toolkit. Otherwise, Emacs does not manage the menu bar and cannot convert coordinates into menu items.

Source Code

// Defined in /usr/src/emacs/src/menu.c
{
  int row, col;
  struct frame *f = decode_any_frame (frame);

  if (!FRAME_LIVE_P (f))
    return Qnil;

  pixel_to_glyph_coords (f, XFIXNUM (x), XFIXNUM (y), &col, &row, NULL, 1);
  if (0 <= row && row < FRAME_MENU_BAR_LINES (f))
    {
      Lisp_Object items, item;
      int i;

      /* Find the menu bar item under `col'.  */
      item = Qnil;
      items = FRAME_MENU_BAR_ITEMS (f);
      /* This loop assumes a single menu-bar line, and will fail to
	 find an item if it is not in the first line.  Note that
	 make_lispy_event in keyboard.c makes the same assumption.  */
      for (i = 0; i < ASIZE (items); i += 4)
	{
	  Lisp_Object pos, str;

	  str = AREF (items, i + 1);
	  pos = AREF (items, i + 3);
	  if (NILP (str))
	    return item;
	  if (XFIXNUM (pos) <= col
	      /* We use <= so the blank between 2 items on a TTY is
		 considered part of the previous item.  */
	      && col <= XFIXNUM (pos) + menu_item_width (SDATA (str)))
	    {
	      item = AREF (items, i);
	      return item;
	    }
	}
    }
  return Qnil;
}