Code: Select all
; <https://www.purebasic.fr/english/viewtopic.php?t=86619>
; tested with PB 6.20 (x64) on Windows 11
EnableExplicit
Procedure GenerateFocusEvents()
; -- This is needed because some gadgets do not natively support EventType() #PB_EventType_LostFocus and #PB_EventType_Focus.
Static prevGadget.i=-1
Protected curGadget.i
curGadget = GetActiveGadget() ; -1, if no gadget has the focus
If prevGadget <> curGadget
If IsGadget(prevGadget)
Select GadgetType(prevGadget)
Case #PB_GadgetType_CheckBox, #PB_GadgetType_ComboBox, #PB_GadgetType_ListIcon, #PB_GadgetType_Spin, #PB_GadgetType_Button
PostEvent(#PB_Event_Gadget, EventWindow(), prevGadget, #PB_EventType_LostFocus)
EndSelect
EndIf
If IsGadget(curGadget)
Select GadgetType(curGadget)
Case #PB_GadgetType_CheckBox, #PB_GadgetType_ComboBox, #PB_GadgetType_ListIcon, #PB_GadgetType_Spin, #PB_GadgetType_Button
PostEvent(#PB_Event_Gadget, EventWindow(), curGadget, #PB_EventType_Focus)
EndSelect
EndIf
EndIf
prevGadget = curGadget
EndProcedure
CompilerIf 1 ; after changing this value to 0, you will see no effect
BindEvent(#PB_Event_Repaint, @GenerateFocusEvents())
CompilerEndIf
CompilerIf #PB_Compiler_IsMainFile
; -- Demo
Define.i event
If OpenWindow(0, 100, 100, 300, 250, "Focus event demo") = 0
MessageRequester("Fatal error", "Can't open main window.")
End
EndIf
CheckBoxGadget(0, 20, 20, 120, 25, "Checkbox")
ComboBoxGadget(1, 20, 55, 120, 25)
AddGadgetItem (1, -1, "ComboBox item")
SetGadgetState(1, 0)
ListIconGadget(2, 20, 90, 120, 60, "Foo", 40)
AddGadgetItem (2, -1, "42")
SpinGadget (3, 20, 165, 120, 25, 0, 5)
ButtonGadget (4, 20, 200, 120, 25, "OK")
Repeat
event = WaitWindowEvent()
Select event
Case #PB_Event_Gadget
Select EventType()
Case #PB_EventType_LostFocus
Debug Str(EventGadget()) + ": LostFocus"
Case #PB_EventType_Focus
Debug Str(EventGadget()) + ": GotFocus"
Default
Debug Str(EventGadget()) + ": other event"
EndSelect
EndSelect
Until event = #PB_Event_CloseWindow
CompilerEndIf
-------------------------------------------------
My best tricks & tips from 15+ years
Create arrays elegantly
Extended date library
Save JSON data with object members well-arranged
Evaluate and process math expressions
Functions for sets
Statistics with R
Thue-Morse sequence
Natural sorting
Sort array indexes and parallel arrays
Time profiling
VectorIcons
Generate focus events