TIP 577: Enhanced index values for Tk

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Author:         Jan Nijtmans <jan.nijtmans@gmail.com>
State:          Final
Type:           Project
Vote:           Done
Created:        8-June-2020
Post-History:   
Keywords:       Tcl index
Tcl-Version:    8.7
Tk-Branch:      enhanced-index
Vote-Summary    Accepted 5/0/1
Votes-For:      BG, JN, KW, MC, SL 
Votes-Against:  none
Votes-Present:  KBK

Abstract

Various commands handle errors in "index" values differently. This TIP attempts to treat the different uses of index values in Tk to a more uniform way, align them better with index usage in Tcl.

The main feature of this TIP is to let Tk indices handle all forms of Tcl indices. So "4+5" and "end-1" will now be accepted, just as any other form accepted by Tcl_GetIntFormIndex() (See also TIP #544).

In addition, the empty string is accepted in Tk as valid index. It means the same as -1 so not available. In Tk this is more natural, since the default of many options is already the empty string. The default value of the -underline option becomes the empty string. Setting -underline to the empty string means no underlining. This default value change will be only done when Tk is compiled with Tcl 9 headers, in order to give applications more time to adapt to this usage (see Compatibility section below).

Rationale

Some examples (Tk):

$ wish8.6
% listbox .l
% menu .m
% .l index 0
0
% .l index none (listbox doesn't know about none)
bad listbox index "none": must be active, anchor, end, @x,y, or a number
% .m index none (menu does know about none)
none
% .m index 0
none

We see that menu's have a different way to indicate not available compared to other widgets.

A request is done already a long time ago, to make index handling in Tk more flexible. See: widgets indices enhancement This TIP implements part of this request (only the active-<integer> form is not implemented.

Specification

  • Tk indices (e.g. the value of the -underline option) will accept all forms accepted by Tcl_GetIntFromIndex(), and also the empty string. So -underline end-1 means underline one character before the last one of the string. -underline {} means no underlining, the same as -underline -1.
  • Indices in menu's, which currently use none as default, will be changed to use the empty string as default. The use of none in menu's is now considered deprecated, but still supported as long as Tk is compiled with Tcl 8.x headers.
  • Start accepting the empty string as index for entries, listboxes, menus ..... just as for button and menu-related -underline options. A new TK_OPTION_INDEX option is implemented, which is almost the same as TK_OPTION_INT, but in stead of only integers, accepts all those index forms as well.
  • When Tk is compiled with Tcl 9 headers, the default -underline value becomes the empty string in stead of -1. When compiled with Tcl 8 headers, -1 is still the default.
  • When Tk is compiled with Tcl 9 headers, all commands which currently report not available as -1, will start returning the empty string in stead.

Compatibility

When compiled with Tcl 8.x headers, this is almost 100% compatible. Except for menu commands which currently use the inconsistent none keyword (see above example) they will start using the empty string in stead (but none still works in Tcl 8.x).

Code can easily be made working in both Tcl 8.x and 9.x environments. Just don't check against the value -1 any more, but use the < 0 check: Since comparing the empty string with 0 in Tcl uses string compare, it gives the same result as comparing -1 with 0. All Tk utilities and demo's are already modified to do this check consistently.

Implementation

See the enhanced-index branch.

Copyright

This document has been placed in the public domain.

History