Optiongadget and EventTypes

Everything else that doesn't fall into one of the other PB categories.
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Optiongadget and EventTypes

Post by jacdelad »

Hi,
the OptionGadget doesn't seem to react on any EventTypes (and the help also says so). So, when I post a #PB_EventType_LeftClick or #PB_EventType_Change nothing happens.
As for me, I usually BindGadgetEvent() most of the OptionGadgets and don't have to worry to enable or disable other gadgets. But if I want my program to change the state I surely can use SetGadgetState, but the binded function is not called. Is this intentional? I think triggering LeftClick or Change would be a logical way to handle this.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Optiongadget and EventTypes

Post by BarryG »

This didn't help? -> viewtopic.php?p=582924
Last edited by BarryG on Fri Feb 24, 2023 10:37 am, edited 1 time in total.
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Optiongadget and EventTypes

Post by Little John »

jacdelad wrote: Fri Feb 24, 2023 1:27 am Hi,
the OptionGadget doesn't seem to react on any EventTypes (and the help also says so). So, when I post a #PB_EventType_LeftClick or #PB_EventType_Change nothing happens.
As for me, I usually BindGadgetEvent() most of the OptionGadgets and don't have to worry to enable or disable other gadgets. But if I want my program to change the state I surely can use SetGadgetState, but the binded function is not called. Is this intentional? I think triggering LeftClick or Change would be a logical way to handle this.
Sorry, I don't understand what exactly you want to do with Option Gadgets and the EventType() function.
Can you post a short code snippet that demonstrates your intended usage?
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Optiongadget and EventTypes

Post by mk-soft »

From too much time

Update 2

Code: Select all

;-TOP

Enumeration Windows
  #Main
EndEnumeration

Enumeration Menus
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuExitApplication
EndEnumeration

Enumeration Gadgets
  #MainOptionContainer_1
  #MainOption_11
  #MainOption_12
  #MainOptionContainer_2
  #MainOption_21
  #MainOption_22
  #MainFunction_1
  #MainFunction_2
  #MainFunction_3
  #MainFunction_4
EndEnumeration

Enumeration Status
  #MainStatusBar
EndEnumeration

; ----

Procedure doEventOptionGroup_1()
  Static last_gadget = -1 
  Protected gadget = EventGadget()
  Select EventType()
    Case #PB_EventType_LeftClick
      If gadget <> last_gadget
        last_gadget = gadget
        Select gadget
          Case #MainOption_11
            Debug "Do Option 1.1"
            ;
          Case #MainOption_12
            Debug "Do Option 1.2"
            ;
        EndSelect
      EndIf
  EndSelect
EndProcedure

Procedure doEventOptionGroup_2()
  Static last_gadget = -1 
  Protected gadget = EventGadget()
  Select EventType()
    Case #PB_EventType_LeftClick
      If gadget <> last_gadget
        last_gadget = gadget
        Select gadget
          Case #MainOption_21
            Debug "Do Option 2.1"
            ;
          Case #MainOption_22
            Debug "Do Option 2.2"
            ;
        EndSelect
      EndIf
  EndSelect
EndProcedure

Procedure SetDefaultOptionGadget(Window, Gadget)
  SetGadgetState(Gadget, #True)
  PostEvent(#PB_Event_Gadget, Window, Gadget, #PB_EventType_LeftClick)
EndProcedure

; ----

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(0)
  dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
  ; Resize Gadgets
  ResizeGadget(#MainFunction_1, 10, dy - 35, 110, 30)
  ResizeGadget(#MainFunction_2, 130, dy - 35, 110, 30)
  ResizeGadget(#MainFunction_3, 250, dy - 35, 110, 30)
  ResizeGadget(#MainFunction_4, 370, dy - 35, 110, 30)
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #WinStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 600, 400, "Test Window", #WinStyle)
    ; MenuBar
    CreateMenu(0, WindowID(0))
    MenuTitle("File")
    MenuItem(#MainMenuExitApplication, "E&xit")
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    ContainerGadget(#MainOptionContainer_1, 5, 5, 220, 75, #PB_Container_Single)
    OptionGadget(#MainOption_11, 10, 10, 200, 25, "Option 1.1")
    OptionGadget(#MainOption_12, 10, 40, 200, 25, "Option 1.2")
    CloseGadgetList()
    
    ContainerGadget(#MainOptionContainer_2, 5, 85, 220, 75, #PB_Container_Single)
    OptionGadget(#MainOption_21, 10, 10, 200, 25, "Option 2.1")
    OptionGadget(#MainOption_22, 10, 40, 200, 25, "Option 2.2")
    CloseGadgetList()
    
    ButtonGadget(#MainFunction_1, 10, dy - 35, 110, 30, "Function 1")
    ButtonGadget(#MainFunction_2, 130, dy - 35, 110, 30, "Function 2")
    ButtonGadget(#MainFunction_3, 250, dy - 35, 110, 30, "Function 3")
    ButtonGadget(#MainFunction_4, 370, dy - 35, 110, 30, "Function 4")
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    BindGadgetEvent(#MainOption_11, @doEventOptionGroup_1())
    BindGadgetEvent(#MainOption_12, @doEventOptionGroup_1())
    
    BindGadgetEvent(#MainOption_21, @doEventOptionGroup_2())
    BindGadgetEvent(#MainOption_22, @doEventOptionGroup_2())
    
    ; Main Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #main
              Break
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            Case #MainMenuExitApplication
              PostEvent(#PB_Event_CloseWindow, #Main, #Null)
              
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainFunction_1
              SetDefaultOptionGadget(#Main, #MainOption_11)
            Case #MainFunction_2
              SetDefaultOptionGadget(#Main, #MainOption_12)
            Case #MainFunction_3
              SetDefaultOptionGadget(#Main, #MainOption_21)
            Case #MainFunction_4
              SetDefaultOptionGadget(#Main, #MainOption_22)
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
Last edited by mk-soft on Fri Feb 24, 2023 4:16 pm, edited 2 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Optiongadget and EventTypes

Post by Caronte3D »

Thanks for the example :D

P.D: You do a mistake in the second event gadget numbers :wink:
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Optiongadget and EventTypes

Post by mk-soft »

Update

Yes,
with Enumeration is always better ;)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Optiongadget and EventTypes

Post by Little John »

mk-soft wrote: Fri Feb 24, 2023 11:37 am

Code: Select all

Procedure doEventOptionGroup_1()
  Static last_gadget = -1 
  Protected gadget = EventGadget()
  Select EventType()
    Case #PB_EventType_LeftClick
      If gadget <> last_gadget
        last_gadget = gadget
        Select gadget
          Case #MainOption_11
            Debug "Do Option 1.1"
            ;
          Case #MainOption_12
            Debug "Do Option 1.2"
            ;
        EndSelect
      EndIf
  EndSelect
EndProcedure

Procedure doEventOptionGroup_2()
  Static last_gadget = -1 
  Protected gadget = EventGadget()
  Select EventType()
    Case #PB_EventType_LeftClick
      If gadget <> last_gadget
        last_gadget = gadget
        Select gadget
          Case #MainOption_21
            Debug "Do Option 2.1"
            ;
          Case #MainOption_22
            Debug "Do Option 2.2"
            ;
        EndSelect
      EndIf
  EndSelect
EndProcedure

EventType() is not needed at all here. You can just do it like this:

Code: Select all

Procedure doEventOptionGroup_1()
   Static last_gadget = -1 
   Protected gadget = EventGadget()
   
   If gadget <> last_gadget
      last_gadget = gadget
      Select gadget
         Case #MainOption_11
            Debug "Do Option 1.1"
            ;
         Case #MainOption_12
            Debug "Do Option 1.2"
            ;
      EndSelect
   EndIf
EndProcedure

Procedure doEventOptionGroup_2()
   Static last_gadget = -1 
   Protected gadget = EventGadget()
   
   If gadget <> last_gadget
      last_gadget = gadget
      Select gadget
         Case #MainOption_21
            Debug "Do Option 2.1"
            ;
         Case #MainOption_22
            Debug "Do Option 2.2"
            ;
      EndSelect
   EndIf
EndProcedure
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Optiongadget and EventTypes

Post by mk-soft »

I always process the EventType, because I also sometimes create and send my own even types to gadgets.
The event type #PB_EventType_LeftClick (0) is also always available.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Optiongadget and EventTypes

Post by Little John »

mk-soft wrote: Fri Feb 24, 2023 3:32 pm I always process the EventType, because I also sometimes create and send my own even types to gadgets.
Yes, maybe. But I am talking about the code that was posted here. And using functions in code posted here that are not necessary at all can easily confuse less experienced readers. Not a good idea.

see also viewtopic.php?p=471641#p471641
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Optiongadget and EventTypes

Post by mk-soft »

@Little John
Right, not needed here. But it tends to be forgotten when it is needed.

@jacdelad
A call to SetGadgetState is not a user action. Therefore, no event is triggered. However, you can trigger it yourself if necessary. See SetDefaultOptionGadget ...

Link Update 2: viewtopic.php?p=596414#p596414
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Optiongadget and EventTypes

Post by jacdelad »

@BarryG: Argh, I don't remember that I have posted this before. Obviously this haunts my regularly...

To explain my problem:

Code: Select all

OpenWindow(0,0,0,400,300,"Window",#PB_Window_ScreenCentered)
OptionGadget(0,0,0,400,20,"Option1")
OptionGadget(1,0,0,400,20,"Option1")

Procedure ReactOnOption()
  Select EventGadget()
    Case 0
      ;Do Stuff when Option 0 is active
    Case 1
      ;Do Stuff when Option 0 is active
  EndSelect
EndProcedure

BindGadgetEvent(0,@ReactOnOption())
BindGadgetEvent(1,@ReactOnOption())

Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
This is basically the code to react on the OptionGadgets. Now I want to manually set Option0 active and trigger these events too. Calling the function doesn't work, because EventGadget() is not set/valid. Naturally I would add something like

Code: Select all

PostEvent(#PB_Event_Gadget,0,0,#PB_EventType_LeftClick)
or

Code: Select all

PostEvent(#PB_Event_Gadget,0,0,#PB_EventType_Change)
to let Option 0 receive an event...
...but OptionGadgets don't handle events. EventType() is always 0 within ReactOnOption().

So my question is (and obviously was), why does this not work or is this not intended to work?
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
idle
Always Here
Always Here
Posts: 5901
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Optiongadget and EventTypes

Post by idle »

You need to check the state

Code: Select all

OpenWindow(0,0,0,400,300,"Window",#PB_Window_ScreenCentered)
OptionGadget(0,0,0,400,20,"Option1")
OptionGadget(1,0,20,400,20,"Option2")

Procedure ReactOnOption()
  Select EventGadget()
    Case 0
      If GetGadgetState(0) 
        Debug "option1 set" 
      EndIf   
    Case 1
      If GetGadgetState(1) 
        Debug "option2 set" 
      EndIf   
      ;Do Stuff when Option 0 is active
  EndSelect
EndProcedure

BindGadgetEvent(0,@ReactOnOption())
BindGadgetEvent(1,@ReactOnOption())

Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow

Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Optiongadget and EventTypes

Post by Little John »

When PostEvent() is used properly, everything works fine. EventType() is not supported by OptionGadget(), because it's not needed at all in this context.
See freak's explanation on this topic.

//edit 2023-02-26:
Moved SetGadgetState() from the callback procedure to the event loop, since it is only necessary there.

Code: Select all

; PB 6.00 LTS

EnableExplicit

#WinMain = 0

; Gadgets
Enumeration
   #Option0
   #Option1
   #Button0
   #Button1
EndEnumeration


Procedure OptionEvent()
   Select EventGadget()
      Case 0
         Debug "Option 0 chosen"
      Case 1
         Debug "Option 1 chosen"
   EndSelect
EndProcedure


Define event.i

OpenWindow(#WinMain, 0, 0, 300, 150, "Window", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OptionGadget(#Option0,  20,  10, 80, 20, "Option0")
OptionGadget(#Option1,  20,  40, 80, 20, "Option1")
ButtonGadget(#Button0,  20, 100, 80, 30, "Button0")
ButtonGadget(#Button1, 200, 100, 80, 30, "Button1")

BindGadgetEvent(#Option0, @OptionEvent())
BindGadgetEvent(#Option1, @OptionEvent())

Repeat
   event = WaitWindowEvent()
   
   Select event
      Case #PB_Event_Gadget
         Select EventGadget()
            Case #Button0
               SetGadgetState(#Option0, 1)
               PostEvent(#PB_Event_Gadget, #WinMain, #Option0)
            Case #Button1
               SetGadgetState(#Option1, 1)
               PostEvent(#PB_Event_Gadget, #WinMain, #Option1)
         EndSelect      
   EndSelect      
Until event = #PB_Event_CloseWindow
Last edited by Little John on Sun Feb 26, 2023 3:38 pm, edited 1 time in total.
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Optiongadget and EventTypes

Post by jacdelad »

Ah, that's what I was looking for. I never got the idea of simply not using the EventType. Thanks a lot!
...and also I was not alone with this problem.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply