'********************************************************************
'* 
'*     All Gadgets 2 
'* 
'********************************************************************

'   This program expands on the original AllGadgets program by making
'all the gadgets do something. This mainly entails connecting various
'gadgets so that they effect each other. 
'   For example, the listview gadget has been connected to the string gadget 
'below it to allow you to change the entries in the listview, and the  
'checkboxes now disable various other gadgets, but there are many other
'features, so have a play. 

'   This mainly demonstrates how easy it can be to set up an interesting 
'program from a GadToolsBox framework. It also shows how to use the
'J Set Gadget Data command, along with the J Gadget Number/String commands 
'(though for v39 upwards, it is more correct to use J Get Gadget Data).  

'   To help show how it was constructed, the changes made have been written
'below.

'   Changes made:
'
'      A handler has been added to interconnect the gadgets, this is in the
'   wait loop. To do the actual processing, the _HANDLEGADGETS procedure has 
'   been added.
'      The global variable _LISTSEL has been added to keep track of which item 
'   of the listview gadget has been selected.
'      The size of the strings defined for the listview gadget in the _GUIDATA 
'   routine have been set to a standard length of 30. This is the same as the
'   maximum string length for the attached string gadget. This allows us to  
'   edit the strings safely without the worry of writing out of the end of the 
'   string.
'      The global variable MYASL has been added. Code for creating a file
'   requester has been placed before the events loop, and a line to remove 
'   it in the cleanup routine
'      The global variables DIS1-4 have been added to track which gadgets
'   are disabled.
Global _SCRAPTAGS,SCRAPTAGS,_PORTLIST,_MESSLIST
Global PATH$,OSVER
Global FHEIGHT,FWIDTH,MBAR,OX,OY,SW,SH
Dim _GADGETSLABELS(1,9)
Global _GADGETSLABELS()
Dim _GADGETSSTRINGS(1)
Global _GADGETSSTRINGS()
Dim _GADGETSLISTS(1)
Global _GADGETSLISTS()
Dim _GADGETSGADS(19)
Global _GADGETSGADS()
Global _BUTTON1,_BUTTON2,_BUTTON3,_FILESTRING,_FILEGADGET,_CYCLE,_TEXT,_PALETTE,_SLIDER,_MX,_SCROLLER,_LISTVIEW,_LISTSTRING,_CBOX1,_CBOX2,_CBOX3,_CBOX4,_INTEGER,_NUMBER
Dim _GADGETSZOOM(1)
Global _GADGETSZOOM()
Global _GADGETSWIND

'** this is used to record the selected item in the listview 

Global _LISTSEL
_LISTSEL=-1

'** this is used for the asl file requester  

Global MYASL

'** these track whether certain gadgets are disabled 
'  DIS1 is the file gadget, DIS2 is the palette
'  DIS3 is the number gadget, DIS4 is other 3 check boxes

Global DIS1,DIS2,DIS3,DIS4

On Error Proc _CLEANUP
_INITIALIZE
_GUIDATA
_SETUPALL
_SETPORTS

'** here we create the file requester, with a few appropriate tags.
'Note that this is done AFTER opening the window otherwise ASLFR_Window
'would be rather pointless.
'Remember to add a J Free Asl Requester line to the _CLEANUP routine 

J Tag _SCRAPTAGS,1,Equ("ASLFR_TitleText"),J Make String("Select a file")
J Tag Equ("ASLFR_SleepWindow"),True
J Tag Equ("ASLFR_Window"),_GADGETSWIND
J Tag Equ("ASLFR_RejectIcons"),True
J Tag 0,0
MYASL=J Create Asl Requester(Equ("ASL_FileRequest"),_SCRAPTAGS)

Do 
   K=J Wait Message
   While K
      C=J Tag Data(_MESSLIST,1)
      If C=Equ("IDCMP_CLOSEWINDOW")
         _CLEANUP
      Else If C=Equ("IDCMP_REFRESHWINDOW")
         _DOREFRESH
      Else If C=Equ("IDCMP_GADGETDOWN") or C=Equ("IDCMP_GADGETUP") or C=Equ("IDCMP_MOUSEMOVE")
         _HANDLEGADGETS
      End If 
      K=J Next Message
   Wend 
Loop 

'** this procedure actually controls the gadgets. It's not as complex
'   as it might at first seem

Procedure _HANDLEGADGETS
   On Error Proc _CLEANUP
   
   'these two connect the scroller to the mx gadget 
   If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_SCROLLER)
      J Tag SCRAPTAGS,1,Equ("GTMX_Active"),J Tag Data(_MESSLIST,2)
      J Tag 0,0
      J Set Gadget Data _GADGETSGADS(_MX),_GADGETSWIND,SCRAPTAGS
      
   Else If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_MX)
      J Tag SCRAPTAGS,1,Equ("GTSC_Top"),J Tag Data(_MESSLIST,2)
      J Tag 0,0
      J Set Gadget Data _GADGETSGADS(_SCROLLER),_GADGETSWIND,SCRAPTAGS
      
      'this connects the integer to the number gadget
   Else If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_INTEGER)
      J Tag SCRAPTAGS,1,Equ("GTNM_Number"),J Gadget Number(_GADGETSGADS(_INTEGER))*20
      J Tag 0,0
      J Set Gadget Data _GADGETSGADS(_NUMBER),_GADGETSWIND,SCRAPTAGS
      
      'these two connect the slider and the palette
   Else If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_PALETTE)
      J Tag SCRAPTAGS,1,Equ("GTSL_Level"),J Tag Data(_MESSLIST,2)
      J Tag 0,0
      J Set Gadget Data _GADGETSGADS(_SLIDER),_GADGETSWIND,SCRAPTAGS
      
   Else If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_SLIDER)
      J Tag SCRAPTAGS,1,Equ("GTPA_Color"),J Tag Data(_MESSLIST,2)
      J Tag 0,0
      J Set Gadget Data _GADGETSGADS(_PALETTE),_GADGETSWIND,SCRAPTAGS
      
      'this connects the cycle to the text 
   Else If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_CYCLE)
      J Tag SCRAPTAGS,1,Equ("GTTX_Text"),_GADGETSLABELS(0,J Tag Data(_MESSLIST,2))
      J Tag 0,0
      J Set Gadget Data _GADGETSGADS(_TEXT),_GADGETSWIND,SCRAPTAGS
      
      'this records which listview item is selected
   Else If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_LISTVIEW)
      _LISTSEL=J Tag Data(_MESSLIST,2)
      
      'this changes an item in the listview
   Else If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_LISTSTRING)
      If _LISTSEL<>-1
         'find the string to be changed from  the list
         S=J Node Name(J Find Node(_GADGETSLISTS(0),_LISTSEL+1))
         'change it 
         J Change String S,J Read String$(J Gadget String(_GADGETSGADS(_LISTSTRING)))
         'update the gadget (otherwise the change doesn't appear) 
         J Tag SCRAPTAGS,1,Equ("GTLV_Labels"),_GADGETSLISTS(0)
         J Tag 0,0
         J Set Gadget Data _GADGETSGADS(_LISTVIEW),_GADGETSWIND,SCRAPTAGS
      End If 
      
      'this brings up a file requester and puts the result in the string 
   Else If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_FILEGADGET)
      F$=J File Request$(MYASL,0)
      If F$<>""
         J Tag SCRAPTAGS,1,Equ("GTST_String"),Varptr(F$)
         J Tag 0,0
         J Set Gadget Data _GADGETSGADS(_FILESTRING),_GADGETSWIND,SCRAPTAGS
      End If 
      
      'these benale functions control the buttons
   Else If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_BUTTON1)
      J Beep 
      
   Else If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_BUTTON2)
      Boom 
      
   Else If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_BUTTON3)
      Shoot 
      
      'disable the file string and button
   Else If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_CBOX1)
      Add DIS1,1,0 To 1
      J Tag SCRAPTAGS,1,Equ("GA_Disabled"),DIS1
      J Tag 0,0
      J Set Gadget Data _GADGETSGADS(_FILESTRING),_GADGETSWIND,SCRAPTAGS
      J Set Gadget Data _GADGETSGADS(_FILEGADGET),_GADGETSWIND,SCRAPTAGS
      
      'disable the palette and slider
   Else If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_CBOX2)
      Add DIS2,1,0 To 1
      J Tag SCRAPTAGS,1,Equ("GA_Disabled"),DIS2
      J Tag 0,0
      J Set Gadget Data _GADGETSGADS(_PALETTE),_GADGETSWIND,SCRAPTAGS
      J Set Gadget Data _GADGETSGADS(_SLIDER),_GADGETSWIND,SCRAPTAGS
      
      'disable the integer gadget
   Else If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_CBOX3)
      Add DIS3,1,0 To 1
      J Tag SCRAPTAGS,1,Equ("GA_Disabled"),DIS3
      J Tag 0,0
      J Set Gadget Data _GADGETSGADS(_INTEGER),_GADGETSWIND,SCRAPTAGS
      
      'disable the first 3 checkboxes
   Else If J Tag Data(_MESSLIST,4)=_GADGETSGADS(_CBOX4)
      Add DIS4,1,0 To 1
      J Tag SCRAPTAGS,1,Equ("GA_Disabled"),DIS4
      J Tag 0,0
      J Set Gadget Data _GADGETSGADS(_CBOX1),_GADGETSWIND,SCRAPTAGS
      J Set Gadget Data _GADGETSGADS(_CBOX2),_GADGETSWIND,SCRAPTAGS
      J Set Gadget Data _GADGETSGADS(_CBOX3),_GADGETSWIND,SCRAPTAGS
   End If 
   
End Proc

'these procedures are all from the GadToolsBox program, though _GUIDATA and
'_CLEANUP have had minor changes as detailed above.

Procedure _INITIALIZE
Procedure _SETUPALL
Procedure _GUIDATA
Procedure _MAKEGADGETSGADS
Procedure _MAKEGADGETSWIND[SC]
Procedure _DOREFRESH
Procedure _SETPORTS
Procedure _FREEWIND[W,G,M,A,C]
Procedure _CLEANUP
