Page 1 of 1

Top Level Forms and Focus

Posted: Thu Jan 12, 2012 3:50 pm
by paulmansour
Consider the following code:

Code: Select all

'F1'⎕WC'Form' 'Form1'
 'F2'⎕WC'Form' 'Form2'('Posn'(F1.Posn+2))
 'F3'⎕WC'Form' 'Form3'('Posn'(F2.Posn+2))
 'F4'⎕WC'Form' 'Form3'('Posn'(F3.Posn+2))
 'F5'⎕WC'Form' 'Form3'('Posn'(F4.Posn+2))


If you execute this in the session, Form F5 will be on top and F1 will be on the bottom.

(If you run it in a function the reverse will be true. In this case to see the behavior I want to show below, simply click on the forms to get them in to the opposite order, with F5 on top)

Now close form F5. When the form closes, the focus goes to F1, currently behind F2,F3, etc, and all the way on bottom. So F1 then comes to the top.

Some questions:

0. This is long standing Dyalog behavior so I assume it is not a bug. Correct?
1. Why wouldn't the focus go to the top-most form, in this case F4, rather than the form that was created first, F1? The current behavior seems unnatural.
2. Is it indeed true, as I have observed, that the first created form is the one that will get the focus when any other form is closed?
3. What, if any, is the name of the ordering that controls this behavior? It is not (I think) "z-order" which simply describes what the order of the stacking of the forms is.
3. Is there a direct way in Dyalog to change this behavior, so that the focus goes to the form on the top of z-order, rather than the form that was created first?
4. Is there a way using []NA?
4. Or should I just track everything my self and manually set the focus back to form that most recently had it when another form is closed?

Any pointers or comments or answers would be greatly appreciated.

Re: Top Level Forms and Focus

Posted: Mon Jan 16, 2012 3:20 pm
by JohnD|Dyalog
Hi Paul,

The focus is actually going to the next form in the "tab order". The "tab order" is a list defined by the order in which the Forms were created. As F5 is the last Form in the list we wrap to the beginning of the list (F1). If you close F3 the focus is given to F4, as defined by the creation order.

If you wish to use []NA on the close of the Form to force the focus elsewere the functions you probably need are SetActiveWindow, and GetWindow.

GetWindow() can be used to return the next window in the Z-order (i.e, going "into" the screen), and SetActiveWindow() activates the window and gives it the focus. Here's some code:

Code: Select all

close m;h;SetActiveWindow;GetWindow;GW_HWNDNEXT;GW_HWNDPREV;n

 GW_HWNDNEXT←2 ⍝ "into" screen
 GW_HWNDPREV←3 ⍝ "out of" screen
 h←(⊃m)⎕WG'Handle'
 ⎕NA'u user32|SetActiveWindow I4'
 ⎕NA'u user32|GetWindow I4 I4'

 n←GetWindow h GW_HWNDNEXT
 SetActiveWindow n


Best Regards,
John Daintree

Re: Top Level Forms and Focus

Posted: Mon Jan 16, 2012 4:32 pm
by paulmansour
John,

Ah, "tab order" .... thanks, that clears up what is actually happening, and it makes sense. Thanks for the code, that should do the trick.

Paul