Tk Source Code

View Ticket
Login
Ticket UUID: 1001070
Title: Mac label doesn't display -highlightbackground
Type: Bug Version: obsolete: 8.4.6
Submitter: johnseal Created on: 2004-07-31 04:44:29
Subsystem: 03. [*button] and [label] Assigned To: marc_culler
Priority: 4 Severity: Minor
Status: Closed Last Modified: 2019-04-24 17:36:50
Resolution: Fixed Closed By: marc_culler
    Closed on: 2019-04-24 17:36:50
Description:
This may be related to the following Scriptics bug:
http://groups.google.com/groups?th=5ce791bec63b3058

Labels don't seem to properly draw the highlight border, at least on 
TclTkAquaBI and Classic MacTcl.  A short transcript is shown below.  
I have also played around with -takefocus and all other color-
related configuration options.  I don't have a PC handy, and I don't 
have X11 Tk installed on my Mac right now, so I don't know how 
they work.

() 1 % label .foo -text foo -width 40 -height 5 -relief solid
.foo
() 2 % grid .foo -sticky nsew -padx 3 -pady 4
() 3 % .foo config -bd 4 -highlightthickness 4
() 4 % .foo config -bg yellow
() 5 % .foo config -bg white
() 6 % .foo config -fg yellow
() 7 % .foo config -fg black
() 8 % .foo config -highlightbackground yellow
() 9 % info patchlevel
8.4.6
() 10 % parray tcl_platform
tcl_platform(byteOrder) = bigEndian
tcl_platform(machine)   = Power Macintosh
tcl_platform(os)        = Darwin
tcl_platform(osVersion) = 7.4.0
tcl_platform(platform)  = unix
tcl_platform(threaded)  = 1
tcl_platform(user)      = john
tcl_platform(wordSize)  = 4
User Comments: marc_culler (claiming to be Marc Culler) added on 2019-04-24 17:36:50:
Closing this ticket as the bug fix branch has been merged.

marc_culler (claiming to be Marc Culler) added on 2019-03-31 16:00:27:
It is not really true that on the Mac buttons have the yellow highlight
*border*.  Something else is going on, which is essential on the Mac.

The code for drawing the rounded rectangular native buttons on the Mac
uses the highlightbackground color differently.  When a button is drawn,
the first step is to fill the Tk bounding rectangle with the highlightbackground
color.  Then the rounded rectangular button is drawn inside of the Tk bounding
rectangle on top of the highlightbackground fill. The padding between the
minimal bounding rectangle of the rounded button and the Tk bounding rectangle
is the sum of the borderwidth and the highlightthickness. No rectangular border
or highlight border is ever drawn. Also, the highlightcolor is ignored as is
the background color. 

If you want your native buttons to look decent it is essential to make the
highlightbackground color be the same as the background color of the enclosing
toplevel or frame. Otherwise you end up with a colored rectangle containing a
native button, which looks pretty bad.

The mac PushButtons are always the same height, which is only large enough for
a single line of text.  The drawing code checks the height of the button's text
contents and if it is too large the button is drawn as a rectangular
BevelButton.  The same process is used for drawing those buttons, though, and
it is still essential to make the highlightbackground match the background
color of the button's container.

The situation with checkbuttons and radiobuttons is similar.  However, for some
reason, the Tk bounding rectangle is not filled with the highlightbackground
color before drawing the button.  Instead it is filled with the background
color.  I have no idea why the fill color is determined differently for the
different types of buttons.  It would seem much more natural to use the
background color in all cases.  However, rationalizing that convention would
surely break lots and lots of code.  So I think we should leave that alone.

Anyway, the bottom line is that we should not touch the button code on the Mac.

marc_culler (claiming to be Marc Culler) added on 2019-03-31 14:53:29:
I found the solution to the mystery.  The TkpGetBorder function actually
allocates a UnixBorder and sets its solidGC field to NULL.  Since the Mac
code links against tkUnix3d.o it will be using the unix TkpGetBorder.
This means there is no danger of segfaults.  The only issue is that the user's
specification of a color for a solid border will be ignored and the border
will be black.

I am going to replace the call to Tk_Draw3DRectangle in tkMacOSXMenuButton.c
which specifies solid relief in the same way as the call in tkMacOSXbutton.c.

fvogel added on 2019-03-31 14:48:11:

In [adf48193] I have now limited my changes on Windows to honor -highlightbackground for labels only, dans don't change anything for *buttons (that is: buttons, checkbuttons, radiobuttons).

On Linux we have a different behavior, fine!

On the Mac, note that buttons have the yellow highlight border as of now. Perhaps they shouldn't. I leave this to you.


marc_culler (claiming to be Marc Culler) added on 2019-03-31 14:32:31:
On a tangential topic, I think that all of the calls to Tk_Draw3DRectangle
in the macosx code are wrong.  (There are 4 more, besides the one that was
removed from tkMacOSXButton.c.)  All of these calls pass a Tk_3DBorder pointer
as a parameter, but the Tk_Draw3DRectangle code eventually passes that pointer
along to code in unix3D.c which is expecting a pointer to a struct UnixBorder,
which has one extra field that is a pointer to a GC named solidGC meant
for drawing solid borders.  The declaration of UnixBorder is private to
tkUnix3d.c, so I don't see how any external code would know how to add the
extra field. Nonetheless, by some miracle which I do not understand, accessing
the Tk_3Dborder as if it were a UnixBorder does not cause a segfault.  But
the solidGC pointer ends up NULL, which causes the code in unix3D.c to create
its own GC with a black foreground color.  So the border ends up black, no
matter what color the user had specified.

While this pretty sketchy practice, one could possibly argue that it is OK
unless the call to Tk_Draw3DRectangle specifies a solid relief, since that
might be the only time when the solidGC pointer is dereferenced.  Of the 4
remaining calls, there is one in tkMacOSXMenubutton.c that specifies solid
relief. I think I should at least replace that call in the same way that I
replaced the one in tkMacOSXButton.c.

I suspect that a similar issue exists with Tk_Fill3DRectangle, but that one
is never called with solid relief.

marc_culler (claiming to be Marc Culler) added on 2019-03-31 14:03:19:
I think that on the Mac the platform specific convention should be to not
use any borders or highlight borders for most widgets.  I say that because
most of the native widgets are not rectangular.  Instead they are rounded
rectangles. The rounded rectangles are drawn inside of the Tk bounding
rectangle, leaving enough room around the rounded rectangle to draw any of
the platform specific decorations that the Mac might put around a widget,
including highlight borders and shadows.  These decorations are rounded
rectangles when the widget is a rounded rectangle.  I think that even the
rectangular native widgets, if they have platform specific decorations, will
draw the decorations inside of the padding in the Tk bounding rectangle.
So the Tk border or highlight border is only used when drawing foreign
X11-style widgets.  In that case one should presumably follow X11 conventions
with respect to the borders.

fvogel added on 2019-03-31 13:05:43:

Since the code for drawing labels uses code for drawing buttons I was wondering what is the behavior with buttons. Try this on all three platforms, at [ffb39c14] (note: this is not the current tip of the bug-1001070 branch):

package require Tk

label .foo -text foo -width 40 -height 5 -relief solid
grid .foo -sticky nsew -padx 3 -pady 4
.foo config -bd 4 -highlightthickness 4
.foo config -bg yellow
.foo config -bg white
.foo config -fg yellow
.foo config -fg black
.foo config -highlightbackground yellow

.foo config -highlightthickness 10
.foo config -highlightcolor red

button .b -text button -bd 6 -highlightthickness 10 \
        -highlightcolor red -highlightbackground yellow
grid .b
checkbutton .cb -text checkbutton -bd 6 -highlightthickness 10\
        -highlightcolor red -highlightbackground yellow
grid .cb
radiobutton .rb -text radiobutton -bd 6 -highlightthickness 10\
        -highlightcolor red -highlightbackground yellow
grid .rb

On Linux, all four widgets have the yellow border and when using keyboard traversal to switch the focus among them, the label is correctly ignored and does not get the red border indicated the widget has input focus.

On macOS, the button does show the yellow border but not the checkbutton nor the radiobutton. Focus traversal seems to work but has no effect on the border.

On Windows, no widget shows the yellow border. I can make it appear using patch [3a96d3de8d]. With this, labels, checkbuttons and radiobuttons now honor the -highlighbackground and -highlighcolor options. But buttons still do not.

Well, not exactly. I stumbled then on this (which made me read the man for the -default option of buttons - interesting reading BTW). So, on Windows, buttons already honor the -highlightbackground option, but only when the button has "-default active". This would mean that on Windows the highlight border is used to indicate an active button, and that it should not be displayed otherwise even if the user requested it. On Linux, playing with the -default option also shows different things.

At the end of the day I'm suspecting the code tried to respect platform-specific conventions or rules, and we're likely breaking those by adding a border around all labels, buttons, checkbuttons and radiobuttons because Linux does it. What does you think? Maybe we should clarify the specification before implementing further.


fvogel added on 2019-03-30 21:38:29:
Agreed.

Now there is the Windows case left. I'll have a look.

marc_culler (claiming to be Marc Culler) added on 2019-03-30 17:38:14:

OK. I fixed that in e1ebc0a563.

Now changing -highlightcolor has no effect for a label, but changing -highlightbackground does have the expected effect.


marc_culler (claiming to be Marc Culler) added on 2019-03-30 17:16:04:

François, I think you are saying that the correct behavior is that the yellow rectangle should be produced by:

.foo configure -highlightbackground yellow

whereas the command

.foo configure -highlightcolor yellow

should have no effect since a label widget never has focus. This makes total sense to me. It is consistent with the manual and it means that the user has complete control over the highlight border of a label. And that is what the original report was expecting. I somehow mixed up those two options. I will modify the branch to make it work like that and remove the changes to the manual.


fvogel added on 2019-03-30 16:28:50:

This new version of the code looks much saner.

However I'm really wondering. Now that I'm testing I'm observing we have the same behavior on Windows as originally reported: the snapshot for Windows is like the one chrstphrchvz posted below for Aqua.

And in the source code, drawing this highlight border is explicitely prevented in the Windows code. This test on butPtr->type != TYPE_LABEL is here since the first revision of the code.

So perhaps it's the Linux port that is wrong after all, and in the macOS version we should not add the || butPtr->type == TYPE_LABEL test in tkMacOSXButton.c:228.

Also note that at the current point [c1956ebf] the -highlightbackground option is not honored on macOS (and it was not either with the previous version [e329b49d] of the fix). It is also ignored on Windows (which is then expected since the highlight border is not drawn at all).


chrstphrchvz added on 2019-03-30 16:02:24:

A strict reading of the manual indicates that the X11 behavior was wrong and the Aqua behavior was correct.

Good point. I hadn't yet tried this on Windows, indeed it resembles Aqua instead of X11:

The manual says that the highlight rectangle "is drawn around the widget when it has the input focus". It also says that a label is non-interactive. It does not make sense to say that a non-interactive widget has input focus.

I wondered about that as well, but failed to speak up.


marc_culler (claiming to be Marc Culler) added on 2019-03-30 14:48:17:

I reworked this. Now it simply draws the solid color highlight border directly without entering the serpentine maze of tkUnix3D code that is dealing with all of the complications of 3D borders and shadows while using private C structs that are not exposed in any header files.

COMMENT: A strict reading of the manual indicates that the X11 behavior was wrong and the Aqua behavior was correct. The manual says that the highlight rectangle "is drawn around the widget when it has the input focus". It also says that a label is non-interactive. It does not make sense to say that a non-interactive widget has input focus.

However, that would mean that the highlight rectangle of a label could never be used. It seems better to allow users the option. Also, it seems that the behavior of X11 Tk is generally more authoritative than the official documentation. So I have updated the manual to say that, for the purpose of the highlight rectangle, a label is considered to always have input focus.

Please review the new fix!!!


marc_culler (claiming to be Marc Culler) added on 2019-03-29 23:16:37:
You're right.  It is crazy to go through all of that code to do something as
simple as drawing a border around a box.  Plus, it is only an accident that it
didn't segfault when passed that struct.  I am going to redo this in a way that
is simple and safe and avoids all interaction with the unix code.

fvogel added on 2019-03-29 22:09:51:
In tkUnix3d.c I have added a cross-reference to the UnixBorder struct so that in case this struct changes in the future then there is a chance that it will be changed in both places at the same time.

This way of defining the same thing at two different places and have to warn for the need of keeping both in sync looks however not very good to me. Perhaps we could do better.

marc_culler (claiming to be Marc Culler) added on 2019-03-29 21:46:10:
Fixed in check-in e329b49d to the branch bug-1001070.

Please review!!!

chrstphrchvz added on 2019-03-29 09:27:16:

This issue still exists in Tk 8.6.9.1.

Screenshot of TkAqua:

Screenshot of Tk X11: