MaxGUI – another little hack

Sometime I would like to see immediate result interacting with gadgets: move a slider and see a value changing somewhere.

So I did this little monster

Rem
				link together 2 (or more) gadgets
				when act on ONE gadget, the effect are on the other(s)
End Rem


SuperStrict
Import maxgui.drivers


Global window:tgadget=		CreateWindow("Main",100,100,300,300,,WINDOW_TITLEBAR|WINDOW_CENTER)

Global lbl_result:tgadget=	CreateLabel("",10,10,80,20,window,LABEL_FRAME|LABEL_CENTER)
Global lbl_2:tgadget=		CreateLabel("",100,10,80,20,window,LABEL_SUNKENFRAME|LABEL_CENTER)
Global lbl_3:tgadget=		CreateLabel("",120,100,40,20,window,LABEL_SUNKENFRAME|LABEL_CENTER)

Global slider:tgadget=		CreateSlider(10,40,270,20,window,SLIDER_HORIZONTAL)
Global bar:tgadget=			CreateProgBar(10,100,100,10,window)
Global bar2:tgadget=		CreateProgBar(10,112,100,10,window)
Global lb:tgadget=			CreateComboBox(120,150,100,100,window)

For Local s:Int=0 Until 50
	AddGadgetItem lb,"Item "+s
Next

SetSliderRange slider,0,100
SetGadgetColor lbl_2,250,200,10
SetGadgetColor lbl_result,200,100,200
CreateLink(slider,lbl_result)
AddGadgetLink(slider,lbl_2)
AddGadgetLink(slider,bar2)
'RemoveGadgetLink(slider,lbl_2)
'ClearGadgetLink(slider)

CreateLink(bar,lbl_3)
CreateLink(lb,lbl_2)


Local tmr:Ttimer=CreateTimer(20)
Local n:Int
Repeat
	WaitEvent()
	
	Select EventID()
		Case EVENT_APPTERMINATE, EVENT_WINDOWCLOSE
			End
		Case EVENT_TIMERTICK
			UpdateProgBar bar,Float(n)/100
			n=(n+1) Mod 100
			Tgadgetlink.Check(bar)
		
		Case EVENT_GADGETACTION
			Select EventSource()
				Case slider
					Tgadgetlink.Check(slider)
				Case lb
					tgadgetlink.Check(lb)
					
			End Select
	End Select
Forever

Function AddGadgetLink:Int(s:tgadget,d:tgadget)
	tgadgetlink.Target(s,d,0)
End Function
Function RemoveGadgetLink:Int(s:Tgadget,d:Tgadget)
	tgadgetlink.Target(s,d,1)
End Function

Function CreateLink:Int(source:Tgadget,dest:Tgadget)
		Local ln:TgadgetLink=Tgadgetlink.Add(source)
		ln.AddTarget(dest)
End Function

Function ClearGadgetLink:Int(source:Tgadget)
	TgadgetLink.Clear(source)
End Function

Type Tgadgetlink

	Global map_link:Tmap
	
	Field source:Tgadget
	Field list_target:TList	'a list of possible target
	
	Method AddTarget:Int(_dest:Tgadget)
		list_target.addlast _dest
	End Method
	
	Method ClearLinks:Int()
		ClearList list_target
	End Method
	
	Method Destroy:Int()
		ClearList list_target
		ClearMap map_link
		source=Null
		list_target=Null
		
	End Method
	
	Method RemoveTarget:Int(_dest:Tgadget)
		ListRemove list_target,_dest
	End Method
	
	Method UpdateLink:Int()
		If list_target=Null Return 0
		If list_target.count()=0 Return 0
		
		Local gc:Int,gv:String
		
		gc=GadgetClass(source)
		
		Select gc
			Case GADGET_SLIDER
				gv=SliderValue(source)
			Case GADGET_PROGBAR,GADGET_COMBOBOX
				gv=source.SelectedItem()
		End Select
		
		
		For Local g:Tgadget=EachIn list_target
			If g
			Select GadgetClass(g)	
				Case GADGET_LABEL,GADGET_BUTTON,GADGET_TEXTFIELD 
					SetGadgetText g,gv
				Case GADGET_PROGBAR
					UpdateProgBar g,Float(gv)/100
				Case GADGET_SLIDER
					SetSliderValue g,Int(gv)
			End Select
			
				'recursion 
				
				Check(g)
			
			End If
		Next
		
	End Method
	
	Function Clear:Int(s:tgadget=Null)
		If s=Null Return 0
		
		Local l:Tgadgetlink
		L=Tgadgetlink(MapValueForKey(map_link,s))
		If L
			L.ClearLinks()
			L.Destroy()
			L=Null
		Else
			DebugLog "ERROR: no source-link found!"
		End If	

	End Function
	
	Function Check:Int(s:tgadget=Null)
		If s=Null Return 0

		Local l:Tgadgetlink
		L=Tgadgetlink(MapValueForKey(map_link,s))
		If L
			L.UpdateLink()
		End If

	End Function
	
	Function Target:Int(s:Tgadget,d:Tgadget,tipo:Int=0)
		Local l:TgadgetLink
			l=Tgadgetlink(MapValueForKey(map_link,s))
		If l
			Select tipo
				Case 0	l.AddTarget(d)'add
				Case 1	l.RemoveTarget(d)'remove
			End Select
		Else
			DebugLog "Error: Source link not found"
		End If
	End Function

		
	Function Add:Tgadgetlink(_s:tgadget)
		Local g:Tgadgetlink=New Tgadgetlink
		g.source=_s
		g.list_target=New TList
		
		If map_link=Null map_link=CreateMap()
		
		MapInsert map_link,_s,g	'add to a LINK map the 'source' gadget
		Return g
	End Function

End Type

Nothing exceptional, but sometimes could be handy.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close