TIP 533: Extension of the menu post command

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Author:         Marc Culler
State:          Final
Type:           Project
Vote:           Done
Created:        13-Jan-2019
Post-History:  
Keywords:       Tk, menubutton
Tcl-Version:    8.6
Tk-Branch:      bug-70e531918e

Abstract

This TIP proposes to modify the menu post command by adding one optional argument which specifies the index of a menu item. The current command accepts two arguments, x and y, which specify the screen coordinates where the upper left corner of the menu should be posted. If the proposed optional index is provided it will mean that the upper left corner of the item with that index should be located at the point specified by x and y.

Rationale

Within Tk itself the menu post command is primarily used for posting a Tk_optionMenu. This widget needs to draw the menu so that the currently selected menu item is placed on top of the widget's menuButton. This is done by calling ::tk::PostOverPoint which, like the proposed extension, accepts three arguments: two coordinates and an index. That function uses the previously computed menu geometry to compute a location for the upper left corner of the menu which will result in correct positioning of the item specified by the index argument. It then calls the menu post command with the computed x and y coordinates.

On Windows and macOS popup menus are drawn by the system. Determining the geometry of a menu on these systems involves reverse engineering, since there is no geometry specification available. Reverse engineering proprietary software is necessarily subject to errors which can be avoided if it is possible to use calls to routines provided by the system library. As it happens, there is such a routine available on macOS. Apple's NSMenu object has a method [NSMenu popUpMenuPositioningItem:atLocation:inView] which draws the menu so that the upper left corner of the specified menu item is located at the point given as the atLocation parameter. The extension proposed in this TIP makes it possible to conveniently use this NSMenu method within the platform specific TkpPostMenu function, provided that the signature of that function is also extended to include a third integer argument specifying the index of the item.

This TIP arose from an attempt to fix a number of rendering bugs for menubuttons on all platforms which were reported in ticket 70e531918e. Because these bugs exist in Tk 8.6.9 and because fixing them is greatly simplified by this very small extension to Tk, this TIP is targeting version 8.6.

Specification and documentation

pathName post x y ?index?

The change consists in the addition of the optional index argument. If index is present the menu will be posted so that the entry with that index is displayed at the given x y coordinates. Backwards compatibility is guaranteed since the command does not change its behavior if this argument is omitted. The optional index can be specified using any of the forms currently allowed in the menu man page.

Reference Implementation

All of the changes needed to implement this TIP are included in the leaf of the bug-70e531918e branch of the Tk fossil repository.

Copyright

This document has been placed in the public domain.

History