Step 3: Query Attributes and Add Event-Initiated Branching
Next, we will add ON EVENT and WAIT for EVENT. We will also query the SLIDER widget VALUE attribute value with the STATUS command. A typical display for the modified program follows.
Run this program and select the SLIDER button with the mouse. Move the SLIDER control up and down and notice that the METER will indicate the SLIDER value.
10 ASSIGN @Meter TO WIDGET "METER"
20 ASSIGN @Slider TO WIDGET "SLIDER"
30 CONTROL @Slider;SET ("X":0,"Y":0,"BACKGROUND":4)
40 CONTROL @Meter;SET ("X":150,"BACKGROUND":1)
50 ON EVENT @Slider,"CHANGED" GOSUB Event_handler
60 LOOP
70 WAIT FOR EVENT
80 END LOOP
90 !
100 Event_handler: !
110 STATUS @Slider;RETURN ("VALUE":Value)
120 CONTROL @Meter;SET ("VALUE":Value)
130 RETURN
140 END
For this program, line 50 calls subroutine Event_handler when the event CHANGED occurs for the SLIDER widget. The CHANGED event is generated every time the user changes the VALUE attribute by clicking on the arrows or in the trough or by clicking and dragging the slider.
For the Event_handler subroutine, the current VALUE of the SLIDER widget is returned by the STATUS command RETURN option, and is entered into the METER widget VALUE attribute with the CONTROL command SET option. Thus, when you move the SLIDER control up and down, the METER widget will indicate the SLIDER widget value.
Lines 60 through 80 continuously loop the program until the event CHANGED occursfor the SLIDER widget. When WAIT FOR EVENT is included, it allows the computer to do other things while waiting for the event to happen. This is important so as not to waste CPU resources that could be used for other tasks.