On Tue, Mar 22, 2011 at 6:35 PM, Evan Laforge <[email protected]> wrote:
> When I create a new window, it gets an FL_SHOW, as expected.  When I
> click in the new window, a mouse down and mouse up are sent to the new
> window, and then after the mouse up, FL_FOCUS is sent to the *old*
> window.  Every click on the new window yields a focus on the old
> window (which according to the OS, doesn't have focus at all).  If I
> type a key, the event goes to the new window as expected, and the
> spurious FL_FOCUS stops happening and clicks are normal.
>
> I can reproduce this easily.  It might be specific to my app, but at
> the moment I'm thinking it's not.  I'm investigating further, but does
> this sound familiar to anyone else?
>
> OS X 10.6.6, current svn version of fltk.

Ok, after poring over the code for a few hours I think I have a better
handle on this.

First of all, FL_FOCUS is not sent when a new window is created.
FL_SHOW is sent, and I had a hack to work around that by registering a
focus when it saw a show, because of course the newly created window
*is* focused.  At least it is in OS X.

So problem 1 was that fltk doesn't know a new window has focus even
though the OS thinks it does.  Problem 2 was that when I click on it,
it doesn't receive focus from the click.  Then fl_fix_focus comes
through, notices that the window in fl_xfocus doesn't seem to contain
focus, and sends it a FL_FOCUS... since fl_xfocus is the old unfocused
window, it's sending a FL_FOCUS to the unfocused not-clicked-on
window.

The reason fl_xfocus is wrong is that firstly window creation didn't
update it, and then the Fl::focus(new_window) triggered by the click
doesn't update it.  That's because Fl::focus() does this thing where
it walks up to the window of the widget by calling parent() until it
returns null, and then sets fl_xfocus to that.  But it starts off with
a window(), so if the widget is already a window it skips the whole
thing.

So, I feel like there are two problems here.  Firstly, I think
Fl_X::make() should probably be giving the newly created window focus,
i.e. setting fl_xfocus and maybe focus_ too, since the OS already is.
I think it should emit a FL_FOCUS as well.  I tried to do this in one
go by sticking a handle(FL_FOCUS) after the handle(FL_SHOW) in
Fl_cocoa.mm's Fl_X::make(), but it didn't actually set the focus, and
I didn't investigate further.  Probably something like that is the
right thing to do though.

Secondly, if I'm reading this correctly, Fl::focus(window) never works
because the loop doesn't expect the passed object to already be a
toplevel window.  I think that's an easy patch, simply fix that bit of
logic in there.

But this focus stuff is a real rats maze, and I'm not confident I'm
seeing everything, or that I'm not missing something about how focus
is supposed to work.  It's full of these undocumented global variables
like focus_ and fl_xfocus.

Any other input out there?

_______________________________________________
fltk-dev mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-dev

Reply via email to