home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga MA Magazine 1998 #3
/
amigamamagazinepolishissue1998.iso
/
maksiu
/
extensions
/
jwindows.lha
/
ListViewc.asc
< prev
next >
Wrap
Text File
|
1996-04-25
|
8KB
|
211 lines
'******************************************************************
'*
'* ListView
'*
'******************************************************************
' This example demonstrates the use of a simple listview gadget and
'how to manipulate a list. The controls allow you to add, move, change and
'delete items from the list to your hearts content. The routine is fairly
'useful in many programs so nick it if you want.
' All the work for the list is done in the _HANDLEGADGETS procedure, and
'no changes whatsoever have been made to the GadToolsBox generated procs.
'_HANDLEGADGETS makes any changes required to the list, then updates all
'the gadgets, disabling or enabling buttons as required.
' Working with lists can be slightly hazardous if you're not careful,
'so nodes are ALWAYS checked before they are changed. This isn't strictly
'necessary since I don't think there are bugs in the code, but I'm not
'taking chances...
'******************************************************************
Global _SCRAPTAGS,SCRAPTAGS,_PORTLIST,_MESSLIST
Global PATH$,OSVER
Global FHEIGHT,FWIDTH,MBAR,OX,OY,SW,SH
Dim _LISTVIEWLISTS(1)
Global _LISTVIEWLISTS()
Dim _LISTVIEWGADS(6)
Global _LISTVIEWGADS()
Global _LISTSTRING,_LISTVIEW,_UP,_DOWN,_ADD,_DEL
Dim _LISTVIEWZOOM(1)
Global _LISTVIEWZOOM()
Global _LISTVIEWWIND
'** This variable tracks the presently selected item in the listview
' Items in the listview are numbered 0 to numitems-1, or -1 if no item
' is selected. It starts as -1 since there are initially no items in the
' list, so obviously no item is selected.
' Note that commands like J Find Node number the nodes 1 to numitems, so
' we usually add one to this value before use.
Global _LISTSEL
_LISTSEL=-1
On Error Proc _CLEANUP
_INITIALIZE
_GUIDATA
_SETUPALL
_SETPORTS
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_GADGETUP")
_HANDLEGADGETS
End If
K=J Next Message
Wend
Loop
Procedure _HANDLEGADGETS
On Error Proc _CLEANUP
'G is the gadget selected, C is the code field of the message.
'This is done purely to give me less typing.
G=J Tag Data(_MESSLIST,4)
C=J Tag Data(_MESSLIST,2)
'First, we deal with the gadgets...
If G=_LISTVIEWGADS(_ADD)
'The add button adds a node to the list. If no item is presently
'selected, it is added to the end of the list using J Make Node.
'If an item is selected, we find the present node using J Find Node,
'then insert the new node after this using J Insert Node.
'Note that all the strings are made with length 30. This allows us
'to overwrite a longer string when the name is changed without making
'a new string. The maximum length of strings in the string gadget has
'been set to 30 so we never over-run this limit.
If _LISTSEL=-1
N=J Make Node(_LISTVIEWLISTS(0),J Make String("New Item",30),0)
Else
N=J Find Node(_LISTVIEWLISTS(0),_LISTSEL+1)
If N
N=J Insert Node(_LISTVIEWLISTS(0),N,J Make String("New Item",30),0)
End If
End If
Else If G=_LISTVIEWGADS(_LISTVIEW)
'If the user has selected a new item by clicking on the listview, we
'change the value of _LISTSEL.
_LISTSEL=C
Else If G=_LISTVIEWGADS(_DEL)
'The Del button removes a node. First, we find the presently selected
'node, and check this exists (in case something has gone wrong..).
'The node is then removed using J Remove Node. We then need to adjust
'_LISTSEL. Users writing for V39+ could use J Get Gadget Data to do
'this more easily.
If _LISTSEL<>-1
N=J Find Node(_LISTVIEWLISTS(0),_LISTSEL+1)
If N
N=J Remove Node(N)
If _LISTSEL+1>J Count Nodes(_LISTVIEWLISTS(0))
Dec _LISTSEL
End If
End If
End If
Else If G=_LISTVIEWGADS(_LISTSTRING)
'The string gadget allows strings to be changed or entered. If a string
'is entered when no item is selected, we make this into a new node.
'Otherwise, we change the string for the presently selected item.
'This is done using J Change String. Note that the string pointer
'returned from J Gadget String points to a READ ONLY string which we
'copy.
If _LISTSEL<>-1
N=J Find Node(_LISTVIEWLISTS(0),_LISTSEL+1)
If N
J Change String J Node Name(N),J Read String$(J Gadget String(_LISTVIEWGADS(_LISTSTRING)))
End If
Else
N=J Make Node(_LISTVIEWLISTS(0),J Make String(J Read String$(J Gadget String(_LISTVIEWGADS(_LISTSTRING))),30),0)
End If
Else If G=_LISTVIEWGADS(_UP)
'To move an item upwards, we first find the present node, then the node
'2 nodes before - this is the node we will insert it after. For example,
'moving node 3 up, it becomes node 2, and should thus be inserted
'AFTER node 1. Weird, but that's how J Move Node works.
'We then adjust _LISTSEL to keep the moving node selected.
N=J Find Node(_LISTVIEWLISTS(0),_LISTSEL+1)
If N
M=J Prev Node(N)
M=J Prev Node(M)
J Move Node _LISTVIEWLISTS(0),N,M
Dec _LISTSEL
End If
Else If G=_LISTVIEWGADS(_DOWN)
'To move an item down, we insert it after the next node. Very like
'the above...
N=J Find Node(_LISTVIEWLISTS(0),_LISTSEL+1)
If N
M=J Next Node(N)
J Move Node _LISTVIEWLISTS(0),N,M
Inc _LISTSEL
End If
End If
'Since every routine above causes some change, we should update all
'the gadgets now...
'First we update the ListView. If any part of the List being displayed
'is changed, we must send a GTLV_Labels tag, or the change doesn't show
'up. Also, because the Up and Down gadgets change the _LISTSEL variable,
'we must change the selected item on the list with the GTLV_Selcted tag.
J Tag SCRAPTAGS,1,Equ("GTLV_Labels"),_LISTVIEWLISTS(0)
J Tag Equ("GTLV_Selected"),_LISTSEL
J Tag 0,0
J Set Gadget Data _LISTVIEWGADS(_LISTVIEW),_LISTVIEWWIND,SCRAPTAGS
'Now for the Del button. As long as an item is selected we can delete it,
'but if not then the button should be disabled. Note the double check
'using J List Not Empty.
If J List Not Empty(_LISTVIEWLISTS(0)) and _LISTSEL<>-1
J Tag SCRAPTAGS,1,Equ("GA_Disabled"),False
Else
J Tag SCRAPTAGS,1,Equ("GA_Disabled"),True
End If
J Tag 0,0
J Set Gadget Data _LISTVIEWGADS(_DEL),_LISTVIEWWIND,SCRAPTAGS
'As long as the selected item is the 2nd node or higher, the Up button is
'enabled. Note that a J Tag 0,0 line is not required here since it's still
'present from changing the last button.
If _LISTSEL>0
J Tag SCRAPTAGS,1,Equ("GA_Disabled"),False
Else
J Tag SCRAPTAGS,1,Equ("GA_Disabled"),True
End If
J Set Gadget Data _LISTVIEWGADS(_UP),_LISTVIEWWIND,SCRAPTAGS
'If an item is selected, and it isn't the last item in the list, the
'Down button is enabled.
If _LISTSEL+1<J Count Nodes(_LISTVIEWLISTS(0)) and _LISTSEL<>-1
J Tag SCRAPTAGS,1,Equ("GA_Disabled"),False
Else
J Tag SCRAPTAGS,1,Equ("GA_Disabled"),True
End If
J Set Gadget Data _LISTVIEWGADS(_DOWN),_LISTVIEWWIND,SCRAPTAGS
'The Add button is always enabled, so we can ignore this.
End Proc
'Gadstools created procs
Procedure _INITIALIZE
Procedure _SETUPALL
Procedure _GUIDATA
Procedure _MAKELISTVIEWGADS
Procedure _MAKELISTVIEWWIND[SC]
Procedure _DOREFRESH
Procedure _SETPORTS
Procedure _FREEWIND[W,G,M,A,C]
Procedure _CLEANUP