MaxGUI – AddGadgetItems – update

I’ve added a new interesting feature (at least for me :D) about my special command AddGadgetItems.

The function adds in one batch many items to the gadget, so you can pass an array, a map or a list to add the items to it.

One limit in the case of passing a list, is that only ‘string list’ are working, while if the list contains objects/classes, the function is meaninless.

So, I’ve decided to change a little the function, and thanks to REFLECTION it’s possible to pass to it a combination of CLASS and FIELD to specify what ‘string’ must be added to the gadget.

Very useful 🙂

The new syntax is now the following

AddGadgetItems:Int(gadget:tgadget,obj:Object=Null,sel:Int=-1,_class:String="")

where gadget is the gadget to add the items, obj is the container (array, list or map), sel to select a specific item in the item-list and finally _class

_class must be in a specific form CLASS.FIELD, as the function via Reflection will find the original class and field to extract the info to add (at the moment it works ONLY for STRING FIELD only.

The item in the gadget will point to the ‘object’… so it’s possibile to access to the class/object directly.

This is the new code for the function AddGadgetItems()

Function AddGadgetItems2:Int(gadget:tgadget,obj:Object=Null,sel:Int=-1,_class:String="")
	If gadget=Null Return -1
	If obj=Null Return -1
	Local _className:String,_classField:String
	If _class<>""
		_className:String=_class.split(".")[0]
		_classField:String=_class.split(".")[1]
	End If
	
	Local tmp:Object
	
	If obj=String[](obj)
		For Local s:String=EachIn String[](obj)
			gadget.InsertItem(gadget.ItemCount(),s,s,-1,Null,0)
		Next
	End If

	If obj=TList(obj)
			If _class=""
				For Local s:String=EachIn TList(obj)
				gadget.InsertItem(gadget.ItemCount(),s,s,-1,Null,0)		
				Next
			Else	'use reflection
				For Local s:Object=EachIn TList(obj)
					Local id:TTypeId=TTypeId.ForObject(s )
					For Local fld:TField=EachIn id.EnumFields()
						If fld.name()=_classField	
						Local gg:String=fld.getstring(s)
						gadget.InsertItem(gadget.ItemCount(),gg,gg,-1,s,0)	
						End If
					Next	
				Next
			End If
	End If
	
	If obj=tmap(obj)
			For Local s:String=EachIn MapKeys(tmap(obj))
			tmp=MapValueForKey(tmap(obj),s)
			gadget.InsertItem(gadget.ItemCount(),s,s,-1,tmp,0)
			Next
	End If
	If sel<>-1 gadget.SelectItem(sel,1)
End Function

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