Bug 170631 - [Widgets] First nested composite takes focus unexpectedly
Summary: [Widgets] First nested composite takes focus unexpectedly
Status: REOPENED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.6   Edit
Hardware: PC All
: P3 normal with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Platform-SWT-Inbox CLA
QA Contact: Felipe Heidrich CLA
URL:
Whiteboard:
Keywords: triaged
: 237882 237886 237888 (view as bug list)
Depends on:
Blocks:
 
Reported: 2007-01-16 10:00 EST by Nick Edgar CLA
Modified: 2022-02-28 04:54 EST (History)
10 users (show)

See Also:


Attachments
Snippet showing the problem (1.26 KB, application/octet-stream)
2007-01-16 10:01 EST, Nick Edgar CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nick Edgar CLA 2007-01-16 10:00:14 EST
3.3 M3

In the attached example, when setFocus() is called on the shell, it ends up giving focus to the Canvas (which should not take focus), instead of the Text.

Steve says: 
Looks like a bug.  If you click in the text, then you can't tab into the canvas.  Does this code really represent what you are seeing?

Nick replies: 
> If you click in the text, then you can't tab into the canvas.  

I don't want it to do that.  The canvas should never take keyboard focus, since we're not hooking a key listener on it - it's just for visual display of an icon or throbber (I know you'll just love that word).

I expected it to find the first control that could take focus when the containing composite is told to setFocus().  Instead, the first Composite (in a depth-first traversal) gets focus if its children can't take focus, so focus is hidden.  
It's this latter problem that we're facing.  

By default, the editor framework sets focus on the editor's outermost control.  In this case, its structure is like:
Composite A
  Composite B
    AnimatedLabel (a Canvas without key listener)
    Label
  Composite C
    Text

So when it does A.setFocus(), the AnimatedLabel gets focus (hidden) instead of the Text.
There's some other intermediate composites thrown in there just for fun, but that's the gist of it.

Steve responds:
I agree.  We have a bug.  I meant that when you clicked on the text and hit tab and it didn't traverse, it confirmed our bug even more.  Enter a bug report, CC me and we will fix it.
Comment 1 Nick Edgar CLA 2007-01-16 10:01:25 EST
Created attachment 56960 [details]
Snippet showing the problem
Comment 2 Steve Northover CLA 2007-05-15 19:35:29 EDT
We won't get to this for 3.3.
Comment 3 Steve Northover CLA 2007-05-15 19:35:35 EDT
We won't get to this for 3.3.
Comment 4 Steve Northover CLA 2008-06-20 11:23:09 EDT
*** Bug 237882 has been marked as a duplicate of this bug. ***
Comment 5 Steve Northover CLA 2008-06-20 11:24:09 EDT
NOTE: This needs to be fixed too:

"If a Composite has only no focusable children (e.g. Labels), setFocus() call
for that Composite returns true and the focus is set to that Composite, even
though it cannot receive focus."
Comment 6 Steve Northover CLA 2008-06-20 11:32:27 EDT
*** Bug 237886 has been marked as a duplicate of this bug. ***
Comment 7 Steve Northover CLA 2008-06-20 11:34:24 EDT
NOTE:

When focus is assigned to a composite before a shell is opened, then none of the children take focus and the composited is remembered as the saved focus control.  Ensure that this case is fixed when this bug is fixed.
Comment 8 Steve Northover CLA 2008-06-20 17:01:47 EDT
*** Bug 237888 has been marked as a duplicate of this bug. ***
Comment 9 Steve Northover CLA 2008-06-20 17:02:54 EDT
NOTE:  Ensure that tabList() is taken into account when setting focus.
Comment 10 Randy Hudson CLA 2008-06-27 10:28:22 EDT
What is the correct behavior when setting focus on a Canvas, when both the Canvas and a child can have focus?  It seems to be depth-first/post-order selection (the child gets focus), so we're using forceFocus() when we want the Canvas to get focus.

Another strange thing is that we're doing this on MouseDown on the Canvas.  If the canvas can hold focus, shouldn't it get it "for free" on mousedown?
Comment 11 Viliam Durina CLA 2008-09-30 10:53:17 EDT
This still reproduces with SWT 3.4. Is there any plan to fix it? While ago, I filled several bugs that all have something to do with this one (see bugs marked as duplicate of this one). Today, I spent several hours debugging another focus problem which again related to this one (TAB traversal failed in forward direction, because there was Composite with no-focusable children only, that stole the focus. In backward direction it worked).
Comment 12 Dani Megert CLA 2010-03-18 10:00:33 EDT
This also burns us when setting the focus into the status text editor composite: instead of giving the focus to a button which is in the second child composite it forces the focus on the first one which only has labels in it.
Comment 13 Dani Megert CLA 2010-10-29 02:12:51 EDT
Ping.
Comment 14 Felipe Heidrich CLA 2010-10-29 10:51:55 EDT
Not sure we can change this behaviour. Calling setFocus() in a composite will cause it call setFocus() in all its children (in the same order they were created).
Calling setFocus() on Canvas will give focus to it (Note: by default Canvas don't take part on tab navegation and don't take focus on mouse down, but calling setFocus() on it works).


You can: 
a) Change the order setFocus() is called on the children by using 
canvas.moveBelow(text);

b) create the canvas with SWT.NO_FOCUS 

Silenio, anything to add ?
Comment 15 Eric Williams CLA 2017-06-30 10:13:01 EDT
Running the snippet and hitting tab causes the Text to get focus. I am pretty sure this is correct behaviour. Once the Text gets focus it doesn't lose it.

AFAICT this is correct. Can anyone confirm? If not I will close this bug in a bit.

Bug triaged, visit https://wiki.eclipse.org/SWT/Devel/Triage for more
inforamation.
Comment 16 Eclipse Genie CLA 2020-05-27 17:16:23 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 17 Rolf Theunissen CLA 2022-02-28 04:54:42 EST
Still a relevant issue:
There is different focus behavior between the OS default and SWT behavior. e.g. win32 prefers tab-stop to get focus over the first control.

Note there is different interpretation of what a focusable control is. When set SWT.NO_FOCUS it is not focusable, but how about items that are not tabItem?

Also, SWT doesn't respect its own tab-order in setFocus.

Note that Comment 7 is fixed in Bug 297510.