Page 1 of 1

Which gadget number is lost?

Posted: Wed Dec 04, 2013 1:51 pm
by charvista
Hi

We have StickyWindow() to keep a window always on top. OK.
But if the focus get lost because the user tried to swap to another app or clicked elsewhere, how to force to keep the focus as well? The answer is probably SetActiveWindow() followed with SetActiveGadget(), but then how can I know which gadget number is lost?

Thanks.

Re: Which gadget number is lost?

Posted: Wed Dec 04, 2013 2:11 pm
by PB
This works if you click the gadgets, but not if you tab between them,
because ButtonGadgets don't support #PB_EventType_Focus. :shock:

Code: Select all

If OpenWindow(0,300,250,400,200,"Button focus",#PB_Window_SystemMenu)
  ButtonGadget(1,50,50,60,25,"1")
  ButtonGadget(2,150,50,60,25,"2")
  ButtonGadget(3,250,50,60,25,"3")
  StringGadget(4,50,100,260,21,"4")
  CheckBoxGadget(5,50,130,260,21,"5")
  SetActiveGadget(1)
  oldag=1
  Repeat
    ev=WaitWindowEvent()
    If ev=#PB_Event_Gadget
      ag=GetActiveGadget()
      If ag<>oldag
        Debug Str(oldag)+" just lost focus"
        oldag=ag
      EndIf
    EndIf
  Until ev=#PB_Event_CloseWindow
EndIf

Re: Which gadget number is lost?

Posted: Wed Dec 04, 2013 3:58 pm
by charvista
Thank you PB. It is an excellent start :)

You are right, TAB between them does not work on all gadgets. To solve it completely, I also need to trap the TAB key between the gadgets as well.
So how to solve the problem below, so the variable Ag always reflects the current active gadget ?

Code: Select all

If OpenWindow(0,300,250,400,200,"Button focus",#PB_Window_SystemMenu)
    StickyWindow(0,1); make it top window
    
    ButtonGadget(1,50,50,60,25,"1")
    ButtonGadget(2,150,50,60,25,"2")
    ButtonGadget(3,250,50,60,25,"3")
    StringGadget(4,50,100,260,21,"4")
    CheckBoxGadget(5,50,130,260,21,"5")
    TextGadget(6,50,170,350,20,"")
    
    Ag=1
    SetActiveGadget(Ag)
    SetGadgetText(6,"Current active gadget: "+Str(Ag))
    
    Repeat
        Event=WaitWindowEvent(50)
        Select Event
            
        Case #PB_Event_CloseWindow
            ExitEventLoop=#True
            
        Case #PB_Event_Gadget
            Gadget=EventGadget()      
            EvType=EventType()
            Debug Str(Gadget)+"    "+Str(EvType)
            
            
            If GetActiveWindow()<>Win
                SetActiveWindow(Win)
                SetActiveGadget(Ag)
            EndIf
            
            Select Gadget
                
            Case 1,2,3,4,5
                Ag=Gadget
                SetGadgetText(6,"Current active gadget: "+Str(Ag))
                
            EndSelect
        EndSelect
    Until ExitEventLoop
EndIf

Re: Which gadget number is lost?

Posted: Wed Dec 04, 2013 5:46 pm
by Little John
PB wrote:This works if you click the gadgets, but not if you tab between them, [...]
This works also when using the TAB keys (tested with PB 5.21 LTS on Windows XP x86):

Code: Select all

OpenWindow(0, 300, 250, 400, 200, "Button focus", #PB_Window_SystemMenu)
ButtonGadget(1, 50, 50, 60, 25, "1")
ButtonGadget(2, 150, 50, 60, 25, "2")
ButtonGadget(3, 250, 50, 60, 25, "3")
StringGadget(4, 50, 100, 260, 21, "4")
CheckBoxGadget(5, 50, 130, 260, 21, "5")
SetActiveGadget(1)
oldag = 1

Repeat
   ev = WaitWindowEvent()
   ; If ev = #PB_Event_Gadget
      ag = GetActiveGadget()
      If ag <> oldag
         Debug Str(oldag) + " just lost focus"
         oldag = ag
      EndIf
   ; EndIf
Until ev = #PB_Event_CloseWindow

Re: Which gadget number is lost?

Posted: Wed Dec 04, 2013 6:10 pm
by charvista
Thanks Little John!!
Checking for the current active gadget before checking the #PB_Event_Gadget does the trick!

:wink:

Re: Which gadget number is lost?

Posted: Wed Dec 04, 2013 6:18 pm
by Little John
charvista wrote:Checking for the current active gadget before checking the #PB_Event_Gadget does the trick!
Exactly.
The credits go to Fluid Byte, who explained it to me some years ago. :-)

Re: Which gadget number is lost?

Posted: Wed Dec 04, 2013 6:24 pm
by charvista
Even when having some experience, we are all still learning!
So, Vielen Dank to Fluid Byte as well :D

Re: Which gadget number is lost?

Posted: Wed Dec 04, 2013 6:54 pm
by RASHAD
Get the focused Gadget with Mouse click or Tab Key
Not so perfect I guess :P

Code: Select all

  If OpenWindow(0, 0, 0, 355, 300, "TreeGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(1,50,50,60,25,"1")
    ButtonGadget(2,150,50,60,25,"2")
    ButtonGadget(3,250,50,60,25,"3")
    StringGadget(4,50,100,260,21,"4")
    CheckBoxGadget(5,50,130,260,21,"5")
    TextGadget(6,50,170,350,20,"")
  
  Repeat
 
    Select WaitWindowEvent()
      Case #WM_LBUTTONDOWN,#WM_LBUTTONUP
            Debug "Gadget : "+Str(GetProp_(GetFocus_(), "PB_ID")) + " Got The Focus"
           
      Case #WM_KEYUP
           If EventwParam() = 9
                Debug "Gadget : "+Str(GetProp_(GetFocus_(), "PB_ID")) + " Got The Focus"
            EndIf           
    
      Case #PB_Event_CloseWindow
            Quit = 1
       
    EndSelect
  Until Quit = 1
  EndIf


Re: Which gadget number is lost?

Posted: Wed Dec 04, 2013 8:02 pm
by charvista
Excellent, RASHAD, my old coffee-friend :mrgreen:
Your approach is interesting because it is inside a #WM_ event. I will very probably use your idea because it is safe and Win-API is not a problem.
My focus problem goes together with the focus of the window, that you can see at http://www.purebasic.fr/english/viewtop ... 72#p432246
Can you please take a look there?

Thanks! And like last year, I'll probably be awake all night :mrgreen:

Re: Which gadget number is lost?

Posted: Thu Dec 05, 2013 8:43 am
by PB
Just a side-tip: you can replace "Case 1,2,3,4,5" with "Case 1 To 5". :)

Re: Which gadget number is lost?

Posted: Thu Dec 05, 2013 1:07 pm
by charvista
PB wrote:Just a side-tip: you can replace "Case 1,2,3,4,5" with "Case 1 To 5". :)
:wink:

Re: Which gadget number is lost?

Posted: Thu Dec 05, 2013 1:53 pm
by PB
> how to force to keep the focus as well?

The solution, made from all tips above:

Code: Select all

OpenWindow(0, 300, 250, 400, 200, "Button focus", #PB_Window_SystemMenu)

ButtonGadget(1, 50, 50, 60, 25, "1")
ButtonGadget(2, 150, 50, 60, 25, "2")
ButtonGadget(3, 250, 50, 60, 25, "3")
StringGadget(4, 50, 100, 260, 21, "4")
CheckBoxGadget(5, 50, 130, 260, 21, "5")

keepag = 1
SetActiveGadget(keepag)

Repeat
   ev = WaitWindowEvent()
   ; If ev = #PB_Event_Gadget
      ag = GetActiveGadget()
      If ag <> keepag
        SetActiveGadget(keepag)
      EndIf
   ; EndIf
Until ev = #PB_Event_CloseWindow