method BtnText(Txt UIObject, BtnLabel UIObject, LblTxt string)
{
Dynamically size buttons based on labeltext
Text UIObject = Reference of an invisible text object w/sizeToFit
BtnLabl UIObject = Reference of the text object of the button
LblTxt string = The text for the button label
}
var
	tx,ty,tw,th,
	gx,gy,gw,gh,
	bx,by,bw,bh,
	newgw, newbw	smallInt	; Size coordinates
	Icon		logical		; Button has icon or not
endvar

const
	StdTxt 		= 395		; Standard button text width
	StdGrp 		= 885		; Standard button interior group width
	StdBtn 		= 1044		; Standard button width
	BtnTxtDiff	= 195		; Diff btwn Text(or group) & button width
endconst

					; Test for interior group (Text+Icon)
if BtnLabel.container.class = "Group" then
	Icon = True
else
	Icon = False
endif

					; Fill the invisible text object with
Txt.text = LblTxt			; the button label
Txt.getPosition(tx,ty,tw,th)		; Use the SizeToFit prop to calc width


if Icon then
					; Get button and button group coordinates
	BtnLabel.container.container.getPosition(bx,by,bw,bh)
	BtnLabel.container.getPosition(gx,gy,gw,gh)

	BtnLabel.blank()		; Remove old label (shrink to 0)

	; If TextWidth > StdText: Group size = StdGrp + increased difference
	newgw = iif(tw > StdTxt, StdGrp + (tw - StdTxt), StdGrp)

	; If NewGroupWidth > StdGrp: Button size = StdBtn + increased group diff
	newbw = iif(newgw > StdGrp, StdBtn + (newgw - StdGrp), StdBtn)

	switch
		case newgw > gw :	; Increasing button group width
			BtnLabel.blank(); Remove old label (shrink to 0)
					; Increase button width first
			BtnLabel.container.container.setPosition(bx,by,newbw,bh)
					; Then increase internal group width
			BtnLabel.container.setPosition(gx,gy,newgw,gh)

		case newgw < gw:	; Decreasing button group width
			BtnLabel.blank(); Remove old label (shrink to 0)
					; Decrease internal group width first
			BtnLabel.container.setPosition(gx,gy,newgw,gh)
					; Then decrease button width
			BtnLabel.container.container.setPosition(bx,by,newbw,bh)
	endswitch
else
					; Get button coordinates
	BtnLabel.container.getPosition(bx,by,bw,bh)

	Newbw = tw + BtnTxtDiff		; Calculate new button width
					; Set minimum width
	Newbw = iif(Newbw < (StdBtn+200), StdBtn+200, Newbw)

	BtnLabel.Text = LblTxt		; Set new button label text

	if Newbw > bw then		; Increasing button width
					; Move x left by 1/2 of increase
		BtnLabel.container.setPosition(bx-((NewBw-bw)/2),by,NewBw,bh)
	else									; Decreasing button width
					; Move x right by 1/2 of increase
		BtnLabel.container.setPosition(bx+((bw-Newbw)/2),by,NewBw,bh)
	endif

	BtnLabel.Text = LblTxt		; Set new button label text
endif

BtnLabel.Text = LblTxt			; Set new button label text
endmethod                             