HTBasic Help
×
Menu
Index

Example: System Widget Event Handler

 
10     ! *********************************************************************
20     ! Example: SYSTEM Widget Event Handler
30     !
40     ! This program shows how to handle queued events from a SYSTEM
50     ! widget.  A SYSTEM widget has an attribute named *QUEUE EVENTS,
60     ! which is normally turned off. In that state, the SYSTEM widget
70     ! will not queue up events -- you get one and all later ones are lost.
80     !
90     ! If you set *QUEUE EVENTS to 1, however, events are queued --
100   ! which leads to the problem of sorting out events from each other.
110   !
120   ! This program shows how this is done by queueing up events from
130   ! three SLIDER widgets contained in a SYSTEM widget. When you
140   ! tell the program to flush the queue, it lists the queued events
150   ! by source SLIDER and event type and gives the number of
160   ! events in each category.
170   !
180   ! This is a somewhat contrived program in that the level of
190   ! event handling demonstrated here is overkill.  Under normal
200   ! circumstances, event queueing should not be a concern. In
210   ! fact, this program has to go to considerable lengths to force
220   ! the events to be queued -- by raising the SYSTEM PRIORITY
230   ! to mask out the SLIDER events, and only lowering it when
240   ! it polls a TOGGLEBUTTON and gets back a 1.
250   !
260   ! The SYSTEM MENU event that allows you to exit the program
270   ! is set to a high priority level, so you can exit the program at
280   ! any time.
290   !
300   ! **************************************************************
310   !
320   CLEAR SCREEN
330   INTEGER N                                                ! General-purpose variable
340   INTEGER D(1:4),Dw,Dh,Iw,Ih                ! Variables for display handling
350   INTEGER Px,Py,Pw,Ph                                ! PANEL parameters
360   INTEGER Gx,Gy                                                ! Internal layout gaps
370   INTEGER Nc,R,Sx,Sy,Sw,Sh                ! Parameters for SYSTEM widget SLIDERS
380   INTEGER Prx,Pry,Prw,Prh                                ! Parameters for PRINTER widget
390   INTEGER Tx,Ty,Th,Tw                                ! Parameters for TOGGLE BUTTON
400   DIM Ev$(1:2)[50],S$[50]                                ! Return event source - GP string
410   !
420   ! Set up a PANEL in the center of the printing display and put
430   ! a SYSTEM MENU on it so you can quit the program.
440   !
450   STATUS CRT,13;N
460   GESCAPE CRT,3;D(*)
470   Dw=D(3)-D(1)
480   Dh=(D(4)-D(2))*((N-7)/N)
490   !
500   Pw=Dw*.99
510   Ph=Dh*.99
520   Px=(Dw-Pw)/2
530   Py=(Dh-Ph)/2
540   !
550   ASSIGN @Main TO WIDGET "PANEL";SET ("VISIBLE":0)
560   CONTROL @Main;SET ("X":Px+100,"Y":Py+50,"WIDTH":.75*Pw,"HEIGHT":.75*Ph)
570   CONTROL @Main;SET ("SYSTEM MENU":"Quit")
580   CONTROL @Main;SET ("MAXIMIZABLE":0,"RESIZABLE":0)
590   CONTROL @Main;SET ("TITLE":" Example: SYSTEM Widget Event Handling")
600   STATUS @Main;RETURN ("INSIDE WIDTH":Iw,"INSIDE HEIGHT":Ih)
610   !
620   ! Create a SYSTEM widget as a child of the main PANEL, populate
630   ! with SLIDER widgets.
640   !
650   ASSIGN @Sys TO WIDGET "SYSTEM";PARENT @Main
660   !
670   Gx=Iw*.01
680   Gy=Gx
690   !
700   Nc=3
710   Sh=Ih*.85
720   Sw=Iw*.11
730   Sy=Gy
740   !
750   FOR C=1 TO Nc
760     Sx=(C-1)*Sw+Gy
770     S$="S"&VAL$(C)
780     CONTROL @Sys;SET ("*NAME":S$,"*CREATE":"SLIDER")
790     CONTROL @Sys;SET ("X":Sx,"Y":Sy,"WIDTH":Sw,"HEIGHT":Sh)
800   NEXT C
810   !
820   ! Set up a TOGGLEBUTTON to indicate when to trap events
830   !
840   Th=Ih-(Sh+3*Gy)
850   Tw=Iw/2
860   Tx=Gx+(3*Sw-Tw)/2
870   Ty=Ih-(Th+Gy)
880   ASSIGN @Toggle TO WIDGET "TOGGLEBUTTON";PARENT @Main
890   CONTROL @Toggle;SET ("X":Tx+50,"Y":Ty,"WIDTH":Tw,"HEIGHT":Th)
900   CONTROL @Toggle;SET ("LABEL":"Flush Queue")
910   !
920   ! Set up a PRINTER widget on the main PANEL (not as part of
930   ! the SYSTEM widget) to log event handling
940   !
950   Prw=Iw-(3*Sw+3*Gy)
960   Prh=Ih-2*Gy
970   Prx=Iw-(Prw+Gy)
980   Pry=Gy
990   !
1000  ASSIGN @Prn TO WIDGET "PRINTER";PARENT @Main
1010  CONTROL @Prn;SET ("X":Prx,"Y":Pry,"WIDTH":Prw,"HEIGHT":Prh)
1020  !
1030  ! Set up events to trap the SLIDERs in the SYSTEM widget, and
1040  ! set up a high priority trap on the "Quit" button in the SYSTEM
1050  ! MENU
1060  !
1070  CONTROL @Sys;SET ("*QUEUE EVENTS":1)
1080  ON EVENT @Sys,"CHANGED" GOSUB Handler
1090  ON EVENT @Sys,"DONE" GOSUB Handler
1100  ON EVENT @Main,"SYSTEM MENU",15 GOTO Finis
1110  !
1120  ! Set event to quit on SYSTEM MENU, make whole thing visible, loop
1130  ! -- mask out normal events until user toggles the TOGGLEBUTTON
1140  !
1150  CONTROL @Main;SET ("VISIBLE":1)
1160  LOOP
1170    SYSTEM PRIORITY 1
1180    REPEAT
1190      STATUS @Toggle;RETURN ("VALUE":N)
1200    UNTIL (N<>0)
1210    CONTROL @Toggle;SET ("VALUE":0)
1220    CONTROL @Prn;SET ("TEXT":"")
1230    SYSTEM PRIORITY 0
1240  END LOOP
1250  !
1260  ! This event handler confirms SLIDER events by printing out the
1270  ! event and its source. It performs a loop, checking to see
1280  ! if there are events in the queue. If there are, it reads the
1290  ! first event in the queue and flushes all the same events.
1300  ! (You could also read them out if you like.) It continues
1310  ! doing this until it cleans out the queue.
1320  !
1330 Handler: !
1340  LOOP
1350  !
1360  ! Determine total number of events in queue
1370  !
1380    CONTROL @Sys;SET ("*EVENT WIDGET FILTER":"")
1390    CONTROL @Sys;SET ("*EVENT NAME FILTER":"")
1400    STATUS @Sys;RETURN ("*QUEUED EVENTS":N)
1410  !
1420  EXIT IF N=0
1430  !
1440    S$="> Total events in queue:  "&VAL$(N)
1450    CONTROL @Prn;SET ("APPEND TEXT":S$)
1460    !
1470    ! Get ID of first source in queue.
1480    !
1490    STATUS @Sys;RETURN ("*QUEUED EVENT":Ev$(*))
1500    !
1510    ! Determine how many of the same events are in the
1520    ! queue and then flush them
1530    !
1540    CONTROL @Sys;SET ("*EVENT WIDGET FILTER":Ev$(1))
1550    CONTROL @Sys;SET ("*EVENT NAME FILTER":Ev$(2))
1560    STATUS @Sys;RETURN ("*FLUSH QUEUED EVENTS":N)
1570    !
1580    S$="  Widget / Event / Total: "
1590    S$=S$&Ev$(1)&" / "&Ev$(2)&" / "&VAL$(N+1)
1600    CONTROL @Prn;SET ("APPEND TEXT":S$)
1610    !
1620    CONTROL @Prn;SET ("APPEND TEXT":"")
1630    !
1640  END LOOP
1650  RETURN
1660  !
1670 Finis: !
1680  ASSIGN @Main TO *                                ! Delete PANEL widget
1690  CLEAR SCREEN
1700  END