Which scrollbar has been moved?

Just starting out? Need help? Post your questions and find answers here.
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Which scrollbar has been moved?

Post by eck49 »

I have several scrollbars, each linked to a number of gadgets. When one of the scrollbars is moved, I need to know which one it was so that the right set of gadgets is moved. Does the code below offer a reasonable and robust process? Another level of Container embedding does not deliver the flexibility I need.

Assume the GadgetInfo structure holds all the information needed to specify which gadgets need to move and what their current scroll position is - this needs to be updated once the new state of the relevant scrollbar is ascertained and then used to repaint the gadgets accordingly.

Code: Select all

Define.GadgetInfo   Moving

Code: Select all

Procedure Something_VScrolled()
  Protected.i vvv, egad
  Protected *Tobemoved.GadgetInfo
  
  egad=EventGadget()
  vvv=GetGadgetState(egad)
  Debug  vvv

  *Tobemoved = GetGadgetData(egad)
  ;Tobemoved tells the code which scrollbar has been moved, so I would replace these comments by code to 
  ;use vvv to adjust the information in the structure and then repaint the relevant gadgets
EndProcedure
And the scrollbar's gadget number is stored in VS. Perhaps VS and Moving will be Array or List elements. In any case, the same routine is bound for all the scrollbars (please don't ask why!)

Code: Select all

SetGadgetData(VS, @Moving)
BindGadgetEvent(VS, @Something_Vscrolled())
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Which scrollbar has been moved?

Post by collectordave »

Not quite sure what you are asking a little code may help.

Each scrollbar has a unique number when created using #_PBAny this can be used to show which scrollbar has been moved and get its value as in the code below:-

Code: Select all

Global Window_0,Event

Global Scrollbar_0,Scrollbar_1,Scrollbar_2,Scrollbar_3



  Window_0 = OpenWindow(#PB_Any, 0, 0, 600, 400, "", #PB_Window_SystemMenu)
  Scrollbar_0 = ScrollBarGadget(#PB_Any, 20, 20, 500, 20, 0, 100, 1)
  Scrollbar_1 = ScrollBarGadget(#PB_Any, 20, 50, 500, 20, 0, 100, 1)
  Scrollbar_2 = ScrollBarGadget(#PB_Any, 20, 80, 500, 20, 0, 100, 1)
  Scrollbar_3 = ScrollBarGadget(#PB_Any, 20, 110, 500, 20, 0, 100, 1)
  
  
  Repeat
    
    Event = WaitWindowEvent()
    
    Select Event
        
      Case #PB_Event_Gadget
        Select EventGadget()
            
          Case Scrollbar_0

             Debug "Scrollbar 0 Moved to " + Str(GetGadgetState(Scrollbar_0))
               
           Case Scrollbar_1

             Debug "Scrollbar 1 Moved to " + Str(GetGadgetState(Scrollbar_1))            
             
             
           Case Scrollbar_2

             Debug "Scrollbar 2 Moved to " + Str(GetGadgetState(Scrollbar_2))        
             
             
           Case Scrollbar_3

             Debug "Scrollbar 3 Moved to " + Str(GetGadgetState(Scrollbar_3))            
             
             
             
             
         EndSelect
    
    
    EndSelect
    
    
  Until event = #PB_Event_CloseWindow
  
    
 
If you need the relevant gadgets to respond immediately to a moving scrollbar the Bindgafgetevent is why you need.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: Which scrollbar has been moved?

Post by eck49 »

@collectordave
Yes, I want to use BindGadgetEvent to get the immediate response, but the callback routine cannot take parameters. The callback for each scrollbar is so similar that it makes sense for the same routine to be used for each one - provided it can be told which scrollbar caused the call. The method I outlined (using GadgetData) does work, but I wondered if there was a better one.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
User avatar
HeX0R
Addict
Addict
Posts: 980
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Which scrollbar has been moved?

Post by HeX0R »

EventGadget() is valid inside Bind-Procedures!
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Which scrollbar has been moved?

Post by collectordave »

Same as above with bind event.

Code: Select all

Global Window_0,Event

Global Scrollbar_0,Scrollbar_1,Scrollbar_2,Scrollbar_3


Procedure HandleScroll()
  
  Select EventGadget()
            
    Case Scrollbar_0

      Debug "Scrollbar 0 Moved to " + Str(GetGadgetState(Scrollbar_0))
               
    Case Scrollbar_1

      Debug "Scrollbar 1 Moved to " + Str(GetGadgetState(Scrollbar_1))            
 
    Case Scrollbar_2

      Debug "Scrollbar 2 Moved to " + Str(GetGadgetState(Scrollbar_2))  
        
    Case Scrollbar_3

      Debug "Scrollbar 3 Moved to " + Str(GetGadgetState(Scrollbar_3))  
         
  EndSelect
  

EndProcedure


  Window_0 = OpenWindow(#PB_Any, 0, 0, 600, 400, "", #PB_Window_SystemMenu)
  Scrollbar_0 = ScrollBarGadget(#PB_Any, 20, 20, 500, 20, 0, 100, 1)
  Scrollbar_1 = ScrollBarGadget(#PB_Any, 20, 50, 500, 20, 0, 100, 1)
  Scrollbar_2 = ScrollBarGadget(#PB_Any, 20, 80, 500, 20, 0, 100, 1)
  Scrollbar_3 = ScrollBarGadget(#PB_Any, 20, 110, 500, 20, 0, 100, 1)
  
  
  BindGadgetEvent(Scrollbar_0, @HandleScroll())
  BindGadgetEvent(Scrollbar_1, @HandleScroll())  
  BindGadgetEvent(Scrollbar_2, @HandleScroll())
  BindGadgetEvent(Scrollbar_3, @HandleScroll())
  
  Repeat
    
    Event = WaitWindowEvent()

  Until event = #PB_Event_CloseWindow
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: Which scrollbar has been moved?

Post by eck49 »

@collectordave

Thanks for your code fragment.

A bit off-topic, but....

If there are two files included in the project via XIncludeFile and both use #PB_Event_FirstCustomValue for user-created events, what are meant to be distinct events could easily be mixed up by being given the same number. It looks like programmer vigilance is the only defence....

What's your suggestion for good practice to avoid this issue?
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Which scrollbar has been moved?

Post by Demivec »

eck49 wrote:What's your suggestion for good practice to avoid this issue?
https://www.purebasic.fr/english/viewtopic.php?f=3&t=77085
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: Which scrollbar has been moved?

Post by eck49 »

@demivec
Thanks for the link. As jacdelad comments on that thread, it still requires a consistent approach from all parties contributing to a project. Not easy on a complex project with many pre-written contributions from all over, therefore without a single controlling mind. Fortunately, not my situation.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Post Reply