local p,K="\084\114\101\101\071\114\111\117\112",47 local a=LibStub and LibStub("\065\099\101\071\085\073-\051\046\048",true)if not a or(a:GetWidgetVersion(p)or 0)>=K then return end local w,R,H,C,c=next,pairs,ipairs,assert,type local f,U,W=math.min,math.max,math.floor local G,M,b,k=select,table.remove,unpack,table.concat local e,D=CreateFrame,UIParent local h,F do local p=setmetatable({},{__mode="\107"})function h()local K=w(p)if K then p[K]=nil return K else return{}end end function F(K)for p in R(K)do K[p]=nil end p[K]=true end end local I=175 local u=true local function z(p)local K=p.parent if K and K.value then return z(K)..("\001"..p.value)else return p.value end end local function N(p,K,a,w,R)local H=p.obj local C=p.toggle local c=K.text or""local f=K.icon local U=K.iconCoords local W=K.level local G=K.value local M=K.uniquevalue local k=K.disabled p.treeline=K p.value=G p.uniquevalue=M if a then p:LockHighlight()p.selected=true else p:UnlockHighlight()p.selected=false end p.level=W if W==1 then p:SetNormalFontObject("\071\097\109\101\070\111\110\116\078\111\114\109\097\108")p:SetHighlightFontObject("\071\097\109\101\070\111\110\116\072\105\103\104\108\105\103\104\116")p.text:SetPoint("\076\069\070\084",(f and 16 or 0)+8,2)else p:SetNormalFontObject("\071\097\109\101\070\111\110\116\072\105\103\104\108\105\103\104\116\083\109\097\108\108")p:SetHighlightFontObject("\071\097\109\101\070\111\110\116\072\105\103\104\108\105\103\104\116\083\109\097\108\108")p.text:SetPoint("\076\069\070\084",(f and 16 or 0)+8*W,2)end if k then p:EnableMouse(false)p.text:SetText("\124\099\102\102\056\048\056\048\056\048"..(c..FONT_COLOR_CODE_CLOSE))else p.text:SetText(c)p:EnableMouse(true)end if f then p.icon:SetTexture(f)p.icon:SetPoint("\076\069\070\084",8*W,W==1 and 0 or 1)else p.icon:SetTexture(nil)end if U then p.icon:SetTexCoord(b(U))else p.icon:SetTexCoord(0,1,0,1)end if w then if not R then C:SetNormalTexture(130838)C:SetPushedTexture(130836)else C:SetNormalTexture(130821)C:SetPushedTexture(130820)end C:Show()else C:Hide()end end local function d(p)local K=false for p,a in H(p)do if a.children==nil and a.visible~=false then K=true elseif a.children then K=K or d(a.children)end if K then return K end end return false end local function s(p,K,a,w,R)local H=h()H.value=K.value H.text=K.text H.icon=K.icon H.iconCoords=K.iconCoords H.disabled=K.disabled H.tree=a H.level=w H.parent=R H.visible=K.visible H.uniquevalue=z(H)if K.children then H.hasChildren=true else H.hasChildren=nil end p.lines[#p.lines+1]=H return H end local function J(p)local K=p.obj p:SetScript("\079\110\085\112\100\097\116\101",nil)K:RefreshTree(nil,true)end local function Z(...)local p=G("\035",...)if p==1 then return...else return.....("\001"..Z(G(2,...)))end end local function t(p)local K=p.button local a=K.obj local w=(a.status or a.localstatus).groups w[K.uniquevalue]=not w[K.uniquevalue]a:RefreshTree()end local function T(p)local K=p.obj K:Fire("\079\110\067\108\105\099\107",p.uniquevalue,p.selected)if not p.selected then K:SetSelected(p.uniquevalue)p.selected=true p:LockHighlight()K:RefreshTree()end a:ClearFocus()end local function i(p)local K=p.obj local a=(K.status or K.localstatus).groups a[p.uniquevalue]=not a[p.uniquevalue]K:RefreshTree()end local function m(p)local K=p.obj K:Fire("\079\110\066\117\116\116\111\110\069\110\116\101\114",p.uniquevalue,p)if K.enabletooltips then local K=a.tooltip K:SetOwner(p,"\065\078\067\072\079\082\095\078\079\078\069")K:ClearAllPoints()K:SetPoint("\076\069\070\084",p,"\082\073\071\072\084")K:SetText(p.text:GetText()or"",1,.82,0,true)K:Show()end end local function o(p)local K=p.obj K:Fire("\079\110\066\117\116\116\111\110\076\101\097\118\101",p.uniquevalue,p)if K.enabletooltips then a.tooltip:Hide()end end local function L(p,K)if p.obj.noupdate then return end local w=p.obj local R=w.status or w.localstatus R.scrollvalue=W(K+.5)w:RefreshTree()a:ClearFocus()end local function B(p)p.obj:RefreshTree()end local function V(p,K)local a=p.obj if a.showscroll then local p=a.scrollbar local w,R=p:GetMinMaxValues()local H=p:GetValue()local C=f(R,U(w,H-K))if H~=C then p:SetValue(C)end end end local function j(p)p:SetBackdropColor(1,1,1,0)end local function x(p)p:SetBackdropColor(1,1,1,.8)end local function Y(p)local K=p:GetParent()K:StartSizing("\082\073\071\072\084")end local function X(p)local K=p:GetParent()local a=K.obj local w=K:GetParent()K:StopMovingOrSizing()K:SetUserPlaced(false)K:SetHeight(0)K:ClearAllPoints()K:SetPoint("\084\079\080\076\069\070\084",w,"\084\079\080\076\069\070\084",0,0)K:SetPoint("\066\079\084\084\079\077\076\069\070\084",w,"\066\079\084\084\079\077\076\069\070\084",0,0)local R=a.status or a.localstatus R.treewidth=K:GetWidth()K.obj:Fire("\079\110\084\114\101\101\082\101\115\105\122\101",K:GetWidth())K.obj:OnWidthSet(R.fullwidth)K.obj:DoLayout()end local A={OnAcquire=function(p)p:SetTreeWidth(I,u)p:EnableButtonTooltips(true)p.frame:SetScript("\079\110\085\112\100\097\116\101",J)end,OnRelease=function(p)p.status=nil p.tree=nil p.frame:SetScript("\079\110\085\112\100\097\116\101",nil)for K,a in R(p.localstatus)do if K=="\103\114\111\117\112\115"then for p in R(a)do a[p]=nil end else p.localstatus[K]=nil end end p.localstatus.scrollvalue=0 p.localstatus.treewidth=I p.localstatus.treesizable=u end,EnableButtonTooltips=function(p,K)p.enabletooltips=K end,CreateButton=function(p)local K=a:GetNextWidgetNum("\084\114\101\101\071\114\111\117\112\066\117\116\116\111\110")local w=e("\066\117\116\116\111\110",("\065\099\101\071\085\073\051\048\084\114\101\101\066\117\116\116\111\110\037\100"):format(K),p.treeframe,"\079\112\116\105\111\110\115\076\105\115\116\066\117\116\116\111\110\084\101\109\112\108\097\116\101")w.obj=p local R=w:CreateTexture(nil,"\079\086\069\082\076\065\089")R:SetWidth(14)R:SetHeight(14)w.icon=R w:SetScript("\079\110\067\108\105\099\107",T)w:SetScript("\079\110\068\111\117\098\108\101\067\108\105\099\107",i)w:SetScript("\079\110\069\110\116\101\114",m)w:SetScript("\079\110\076\101\097\118\101",o)w.toggle.button=w w.toggle:SetScript("\079\110\067\108\105\099\107",t)w.text:SetHeight(14)return w end;SetStatusTable=function(p,K)C(c(K)=="\116\097\098\108\101")p.status=K if not K.groups then K.groups={}end if not K.scrollvalue then K.scrollvalue=0 end if not K.treewidth then K.treewidth=I end if K.treesizable==nil then K.treesizable=u end p:SetTreeWidth(K.treewidth,K.treesizable)p:RefreshTree()end;SetTree=function(p,K,a)p.filter=a if K then C(c(K)=="\116\097\098\108\101")end p.tree=K p:RefreshTree()end;BuildLevel=function(p,K,a,w)local R=(p.status or p.localstatus).groups for H,C in H(K)do if C.children then if not p.filter or d(C.children)then local H=s(p,C,K,a,w)if R[H.uniquevalue]then p:BuildLevel(C.children,a+1,H)end end elseif C.visible~=false or not p.filter then s(p,C,K,a,w)end end end;RefreshTree=function(p,K,a)local w=p.buttons local C=p.lines for p,K in H(w)do K:Hide()end while C[1]do local p=M(C)for K in R(p)do p[K]=nil end F(p)end if not p.tree then return end local c=p.status or p.localstatus local f=c.groups local U=p.tree local G=p.treeframe c.scrollToSelection=c.scrollToSelection or K p:BuildLevel(U,1)local b=#C local k=W(((p.treeframe:GetHeight()or 0)-20)/18)if k<=0 then return end if p.frame:GetParent()==D and not a then p.frame:SetScript("\079\110\085\112\100\097\116\101",J)return end local e,h K=c.scrollToSelection c.scrollToSelection=nil if b<=k then c.scrollvalue=0 p:ShowScroll(false)e,h=1,b else p:ShowScroll(true)p.noupdate=true p.scrollbar:SetMinMaxValues(0,b-k)if b-c.scrollvalue<k then c.scrollvalue=b-k end p.noupdate=nil e,h=c.scrollvalue+1,c.scrollvalue+k if K and c.selected then local p for K,a in H(C)do if a.uniquevalue==c.selected then p=K end end if not p then elseif p>=e and p<=h then else if p<e then c.scrollvalue=p-1 else c.scrollvalue=p-k end e,h=c.scrollvalue+1,c.scrollvalue+k end end if p.scrollbar:GetValue()~=c.scrollvalue then p.scrollbar:SetValue(c.scrollvalue)end end local I=1 for K=e,h,1 do local a=C[K]local R=w[I]if not R then R=p:CreateButton()w[I]=R R:SetParent(G)R:SetFrameLevel(G:GetFrameLevel()+1)R:ClearAllPoints()if I==1 then if p.showscroll then R:SetPoint("\084\079\080\082\073\071\072\084",-22,-10)R:SetPoint("\084\079\080\076\069\070\084",0,-10)else R:SetPoint("\084\079\080\082\073\071\072\084",0,-10)R:SetPoint("\084\079\080\076\069\070\084",0,-10)end else R:SetPoint("\084\079\080\082\073\071\072\084",w[I-1],"\066\079\084\084\079\077\082\073\071\072\084",0,0)R:SetPoint("\084\079\080\076\069\070\084",w[I-1],"\066\079\084\084\079\077\076\069\070\084",0,0)end end N(R,a,c.selected==a.uniquevalue,a.hasChildren,f[a.uniquevalue])R:Show()I=I+1 end end,SetSelected=function(p,K)local a=p.status or p.localstatus if a.selected~=K then a.selected=K p:Fire("\079\110\071\114\111\117\112\083\101\108\101\099\116\101\100",K)end end,Select=function(p,K,...)p.filter=false local a=p.status or p.localstatus local w=a.groups local R={...}for p=1,#R,1 do w[k(R,"\001",1,p)]=true end a.selected=K p:RefreshTree(true)p:Fire("\079\110\071\114\111\117\112\083\101\108\101\099\116\101\100",K)end;SelectByPath=function(p,...)p:Select(Z(...),...)end,SelectByValue=function(p,K)p:Select(K,("\001"):split(K))end,ShowScroll=function(p,K)p.showscroll=K if K then p.scrollbar:Show()if p.buttons[1]then p.buttons[1]:SetPoint("\084\079\080\082\073\071\072\084",p.treeframe,"\084\079\080\082\073\071\072\084",-22,-10)end else p.scrollbar:Hide()if p.buttons[1]then p.buttons[1]:SetPoint("\084\079\080\082\073\071\072\084",p.treeframe,"\084\079\080\082\073\071\072\084",0,-10)end end end,OnWidthSet=function(p,K)local a=p.content local w=p.treeframe local R=p.status or p.localstatus R.fullwidth=K local H=(K-R.treewidth)-20 if H<0 then H=0 end a:SetWidth(H)a.width=H local C=f(400,K-50)if C>100 and R.treewidth>C then p:SetTreeWidth(C,R.treesizable)end if w.SetResizeBounds then w:SetResizeBounds(100,1,C,1600)else w:SetMaxResize(C,1600)end end,OnHeightSet=function(p,K)local a=p.content local w=K-20 if w<0 then w=0 end a:SetHeight(w)a.height=w end;SetTreeWidth=function(p,K,a)if not a then if c(K)=="\110\117\109\098\101\114"then a=false elseif c(K)=="\098\111\111\108\101\097\110"then a=K K=I else a=false K=I end end p.treeframe:SetWidth(K)p.dragger:EnableMouse(a)local w=p.status or p.localstatus w.treewidth=K w.treesizable=a if w.fullwidth then p:OnWidthSet(w.fullwidth)end end,GetTreeWidth=function(p)local K=p.status or p.localstatus return K.treewidth or I end,LayoutFinished=function(p,K,a)if p.noAutoHeight then return end p:SetHeight((a or 0)+20)end}local Q={bgFile="\073\110\116\101\114\102\097\099\101\092\067\104\097\116\070\114\097\109\101\092\067\104\097\116\070\114\097\109\101\066\097\099\107\103\114\111\117\110\100",edgeFile="\073\110\116\101\114\102\097\099\101\092\084\111\111\108\116\105\112\115\092\085\073-\084\111\111\108\116\105\112-\066\111\114\100\101\114",tile=true,tileSize=16,edgeSize=16,insets={left=3;right=3;top=5;bottom=3}}local S={bgFile="\073\110\116\101\114\102\097\099\101\092\084\111\111\108\116\105\112\115\092\085\073-\084\111\111\108\116\105\112-\066\097\099\107\103\114\111\117\110\100";edgeFile=nil,tile=true,tileSize=16;edgeSize=1,insets={left=3;right=3,top=7,bottom=7}}local function l()local K=a:GetNextWidgetNum(p)local w=e("\070\114\097\109\101",nil,D)local H=e("\070\114\097\109\101",nil,w,"\066\097\099\107\100\114\111\112\084\101\109\112\108\097\116\101")H:SetPoint("\084\079\080\076\069\070\084")H:SetPoint("\066\079\084\084\079\077\076\069\070\084")H:SetWidth(I)H:EnableMouseWheel(true)H:SetBackdrop(Q)H:SetBackdropColor(.1,.1,.1,.5)H:SetBackdropBorderColor(.4,.4,.4)H:SetResizable(true)if H.SetResizeBounds then H:SetResizeBounds(100,1,400,1600)else H:SetMinResize(100,1)H:SetMaxResize(400,1600)end H:SetScript("\079\110\085\112\100\097\116\101",J)H:SetScript("\079\110\083\105\122\101\067\104\097\110\103\101\100",B)H:SetScript("\079\110\077\111\117\115\101\087\104\101\101\108",V)local C=e("\070\114\097\109\101",nil,H,"\066\097\099\107\100\114\111\112\084\101\109\112\108\097\116\101")C:SetWidth(8)C:SetPoint("\084\079\080",H,"\084\079\080\082\073\071\072\084")C:SetPoint("\066\079\084\084\079\077",H,"\066\079\084\084\079\077\082\073\071\072\084")C:SetBackdrop(S)C:SetBackdropColor(1,1,1,0)C:SetScript("\079\110\069\110\116\101\114",x)C:SetScript("\079\110\076\101\097\118\101",j)C:SetScript("\079\110\077\111\117\115\101\068\111\119\110",Y)C:SetScript("\079\110\077\111\117\115\101\085\112",X)local c=e("\083\108\105\100\101\114",("\065\099\101\067\111\110\102\105\103\068\105\097\108\111\103\084\114\101\101\071\114\111\117\112\037\100\083\099\114\111\108\108\066\097\114"):format(K),H,"\085\073\080\097\110\101\108\083\099\114\111\108\108\066\097\114\084\101\109\112\108\097\116\101")c:SetScript("\079\110\086\097\108\117\101\067\104\097\110\103\101\100",nil)c:SetPoint("\084\079\080\082\073\071\072\084",-10,-26)c:SetPoint("\066\079\084\084\079\077\082\073\071\072\084",-10,26)c:SetMinMaxValues(0,0)c:SetValueStep(1)c:SetValue(0)c:SetWidth(16)c:SetScript("\079\110\086\097\108\117\101\067\104\097\110\103\101\100",L)local f=c:CreateTexture(nil,"\066\065\067\075\071\082\079\085\078\068")f:SetAllPoints(c)f:SetColorTexture(0,0,0,.4)local U=e("\070\114\097\109\101",nil,w,"\066\097\099\107\100\114\111\112\084\101\109\112\108\097\116\101")U:SetPoint("\084\079\080\076\069\070\084",H,"\084\079\080\082\073\071\072\084")U:SetPoint("\066\079\084\084\079\077\082\073\071\072\084")U:SetBackdrop(Q)U:SetBackdropColor(.1,.1,.1,.5)U:SetBackdropBorderColor(.4,.4,.4)local W=e("\070\114\097\109\101",nil,U)W:SetPoint("\084\079\080\076\069\070\084",10,-10)W:SetPoint("\066\079\084\084\079\077\082\073\071\072\084",-10,10)local G={frame=w;lines={},levels={};buttons={};hasChildren={},localstatus={groups={},scrollvalue=0},filter=false;treeframe=H,dragger=C;scrollbar=c,border=U,content=W,type=p}for p,K in R(A)do G[p]=K end H.obj,C.obj,c.obj=G,G,G return a:RegisterAsContainer(G)end a:RegisterWidgetType(p,l,K)
--[[-----------------------------------------------------------------------------
TreeGroup Container
Container that uses a tree control to switch between groups.
-------------------------------------------------------------------------------]]
local Type, Version = "TreeGroup", 47
local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end

-- Lua APIs
local next, pairs, ipairs, assert, type = next, pairs, ipairs, assert, type
local math_min, math_max, floor = math.min, math.max, math.floor
local select, tremove, unpack, tconcat = select, table.remove, unpack, table.concat

-- WoW APIs
local CreateFrame, UIParent = CreateFrame, UIParent

-- Recycling functions
local new, del
do
	local pool = setmetatable({},{__mode='k'})
	function new()
		local t = next(pool)
		if t then
			pool[t] = nil
			return t
		else
			return {}
		end
	end
	function del(t)
		for k in pairs(t) do
			t[k] = nil
		end
		pool[t] = true
	end
end

local DEFAULT_TREE_WIDTH = 175
local DEFAULT_TREE_SIZABLE = true

--[[-----------------------------------------------------------------------------
Support functions
-------------------------------------------------------------------------------]]
local function GetButtonUniqueValue(line)
	local parent = line.parent
	if parent and parent.value then
		return GetButtonUniqueValue(parent).."\001"..line.value
	else
		return line.value
	end
end

local function UpdateButton(button, treeline, selected, canExpand, isExpanded)
	local self = button.obj
	local toggle = button.toggle
	local text = treeline.text or ""
	local icon = treeline.icon
	local iconCoords = treeline.iconCoords
	local level = treeline.level
	local value = treeline.value
	local uniquevalue = treeline.uniquevalue
	local disabled = treeline.disabled

	button.treeline = treeline
	button.value = value
	button.uniquevalue = uniquevalue
	if selected then
		button:LockHighlight()
		button.selected = true
	else
		button:UnlockHighlight()
		button.selected = false
	end
	button.level = level
	if ( level == 1 ) then
		button:SetNormalFontObject("GameFontNormal")
		button:SetHighlightFontObject("GameFontHighlight")
		button.text:SetPoint("LEFT", (icon and 16 or 0) + 8, 2)
	else
		button:SetNormalFontObject("GameFontHighlightSmall")
		button:SetHighlightFontObject("GameFontHighlightSmall")
		button.text:SetPoint("LEFT", (icon and 16 or 0) + 8 * level, 2)
	end

	if disabled then
		button:EnableMouse(false)
		button.text:SetText("|cff808080"..text..FONT_COLOR_CODE_CLOSE)
	else
		button.text:SetText(text)
		button:EnableMouse(true)
	end

	if icon then
		button.icon:SetTexture(icon)
		button.icon:SetPoint("LEFT", 8 * level, (level == 1) and 0 or 1)
	else
		button.icon:SetTexture(nil)
	end

	if iconCoords then
		button.icon:SetTexCoord(unpack(iconCoords))
	else
		button.icon:SetTexCoord(0, 1, 0, 1)
	end

	if canExpand then
		if not isExpanded then
			toggle:SetNormalTexture(130838) -- Interface\\Buttons\\UI-PlusButton-UP
			toggle:SetPushedTexture(130836) -- Interface\\Buttons\\UI-PlusButton-DOWN
		else
			toggle:SetNormalTexture(130821) -- Interface\\Buttons\\UI-MinusButton-UP
			toggle:SetPushedTexture(130820) -- Interface\\Buttons\\UI-MinusButton-DOWN
		end
		toggle:Show()
	else
		toggle:Hide()
	end
end

local function ShouldDisplayLevel(tree)
	local result = false
	for k, v in ipairs(tree) do
		if v.children == nil and v.visible ~= false then
			result = true
		elseif v.children then
			result = result or ShouldDisplayLevel(v.children)
		end
		if result then return result end
	end
	return false
end

local function addLine(self, v, tree, level, parent)
	local line = new()
	line.value = v.value
	line.text = v.text
	line.icon = v.icon
	line.iconCoords = v.iconCoords
	line.disabled = v.disabled
	line.tree = tree
	line.level = level
	line.parent = parent
	line.visible = v.visible
	line.uniquevalue = GetButtonUniqueValue(line)
	if v.children then
		line.hasChildren = true
	else
		line.hasChildren = nil
	end
	self.lines[#self.lines+1] = line
	return line
end

--fire an update after one frame to catch the treeframes height
local function FirstFrameUpdate(frame)
	local self = frame.obj
	frame:SetScript("OnUpdate", nil)
	self:RefreshTree(nil, true)
end

local function BuildUniqueValue(...)
	local n = select('#', ...)
	if n == 1 then
		return ...
	else
		return (...).."\001"..BuildUniqueValue(select(2,...))
	end
end

--[[-----------------------------------------------------------------------------
Scripts
-------------------------------------------------------------------------------]]
local function Expand_OnClick(frame)
	local button = frame.button
	local self = button.obj
	local status = (self.status or self.localstatus).groups
	status[button.uniquevalue] = not status[button.uniquevalue]
	self:RefreshTree()
end

local function Button_OnClick(frame)
	local self = frame.obj
	self:Fire("OnClick", frame.uniquevalue, frame.selected)
	if not frame.selected then
		self:SetSelected(frame.uniquevalue)
		frame.selected = true
		frame:LockHighlight()
		self:RefreshTree()
	end
	AceGUI:ClearFocus()
end

local function Button_OnDoubleClick(button)
	local self = button.obj
	local status = (self.status or self.localstatus).groups
	status[button.uniquevalue] = not status[button.uniquevalue]
	self:RefreshTree()
end

local function Button_OnEnter(frame)
	local self = frame.obj
	self:Fire("OnButtonEnter", frame.uniquevalue, frame)

	if self.enabletooltips then
		local tooltip = AceGUI.tooltip
		tooltip:SetOwner(frame, "ANCHOR_NONE")
		tooltip:ClearAllPoints()
		tooltip:SetPoint("LEFT",frame,"RIGHT")
		tooltip:SetText(frame.text:GetText() or "", 1, .82, 0, true)

		tooltip:Show()
	end
end

local function Button_OnLeave(frame)
	local self = frame.obj
	self:Fire("OnButtonLeave", frame.uniquevalue, frame)

	if self.enabletooltips then
		AceGUI.tooltip:Hide()
	end
end

local function OnScrollValueChanged(frame, value)
	if frame.obj.noupdate then return end
	local self = frame.obj
	local status = self.status or self.localstatus
	status.scrollvalue = floor(value + 0.5)
	self:RefreshTree()
	AceGUI:ClearFocus()
end

local function Tree_OnSizeChanged(frame)
	frame.obj:RefreshTree()
end

local function Tree_OnMouseWheel(frame, delta)
	local self = frame.obj
	if self.showscroll then
		local scrollbar = self.scrollbar
		local min, max = scrollbar:GetMinMaxValues()
		local value = scrollbar:GetValue()
		local newvalue = math_min(max,math_max(min,value - delta))
		if value ~= newvalue then
			scrollbar:SetValue(newvalue)
		end
	end
end

local function Dragger_OnLeave(frame)
	frame:SetBackdropColor(1, 1, 1, 0)
end

local function Dragger_OnEnter(frame)
	frame:SetBackdropColor(1, 1, 1, 0.8)
end

local function Dragger_OnMouseDown(frame)
	local treeframe = frame:GetParent()
	treeframe:StartSizing("RIGHT")
end

local function Dragger_OnMouseUp(frame)
	local treeframe = frame:GetParent()
	local self = treeframe.obj
	local treeframeParent = treeframe:GetParent()
	treeframe:StopMovingOrSizing()
	--treeframe:SetScript("OnUpdate", nil)
	treeframe:SetUserPlaced(false)
	--Without this :GetHeight will get stuck on the current height, causing the tree contents to not resize
	treeframe:SetHeight(0)
	treeframe:ClearAllPoints()
	treeframe:SetPoint("TOPLEFT", treeframeParent, "TOPLEFT",0,0)
	treeframe:SetPoint("BOTTOMLEFT", treeframeParent, "BOTTOMLEFT",0,0)

	local status = self.status or self.localstatus
	status.treewidth = treeframe:GetWidth()

	treeframe.obj:Fire("OnTreeResize",treeframe:GetWidth())
	-- recalculate the content width
	treeframe.obj:OnWidthSet(status.fullwidth)
	-- update the layout of the content
	treeframe.obj:DoLayout()
end

--[[-----------------------------------------------------------------------------
Methods
-------------------------------------------------------------------------------]]
local methods = {
	["OnAcquire"] = function(self)
		self:SetTreeWidth(DEFAULT_TREE_WIDTH, DEFAULT_TREE_SIZABLE)
		self:EnableButtonTooltips(true)
		self.frame:SetScript("OnUpdate", FirstFrameUpdate)
	end,

	["OnRelease"] = function(self)
		self.status = nil
		self.tree = nil
		self.frame:SetScript("OnUpdate", nil)
		for k, v in pairs(self.localstatus) do
			if k == "groups" then
				for k2 in pairs(v) do
					v[k2] = nil
				end
			else
				self.localstatus[k] = nil
			end
		end
		self.localstatus.scrollvalue = 0
		self.localstatus.treewidth = DEFAULT_TREE_WIDTH
		self.localstatus.treesizable = DEFAULT_TREE_SIZABLE
	end,

	["EnableButtonTooltips"] = function(self, enable)
		self.enabletooltips = enable
	end,

	["CreateButton"] = function(self)
		local num = AceGUI:GetNextWidgetNum("TreeGroupButton")
		local button = CreateFrame("Button", ("AceGUI30TreeButton%d"):format(num), self.treeframe, "OptionsListButtonTemplate")
		button.obj = self

		local icon = button:CreateTexture(nil, "OVERLAY")
		icon:SetWidth(14)
		icon:SetHeight(14)
		button.icon = icon

		button:SetScript("OnClick",Button_OnClick)
		button:SetScript("OnDoubleClick", Button_OnDoubleClick)
		button:SetScript("OnEnter",Button_OnEnter)
		button:SetScript("OnLeave",Button_OnLeave)

		button.toggle.button = button
		button.toggle:SetScript("OnClick",Expand_OnClick)

		button.text:SetHeight(14) -- Prevents text wrapping

		return button
	end,

	["SetStatusTable"] = function(self, status)
		assert(type(status) == "table")
		self.status = status
		if not status.groups then
			status.groups = {}
		end
		if not status.scrollvalue then
			status.scrollvalue = 0
		end
		if not status.treewidth then
			status.treewidth = DEFAULT_TREE_WIDTH
		end
		if status.treesizable == nil then
			status.treesizable = DEFAULT_TREE_SIZABLE
		end
		self:SetTreeWidth(status.treewidth,status.treesizable)
		self:RefreshTree()
	end,

	--sets the tree to be displayed
	["SetTree"] = function(self, tree, filter)
		self.filter = filter
		if tree then
			assert(type(tree) == "table")
		end
		self.tree = tree
		self:RefreshTree()
	end,

	["BuildLevel"] = function(self, tree, level, parent)
		local groups = (self.status or self.localstatus).groups

		for i, v in ipairs(tree) do
			if v.children then
				if not self.filter or ShouldDisplayLevel(v.children) then
					local line = addLine(self, v, tree, level, parent)
					if groups[line.uniquevalue] then
						self:BuildLevel(v.children, level+1, line)
					end
				end
			elseif v.visible ~= false or not self.filter then
				addLine(self, v, tree, level, parent)
			end
		end
	end,

	["RefreshTree"] = function(self,scrollToSelection,fromOnUpdate)
		local buttons = self.buttons
		local lines = self.lines

		for i, v in ipairs(buttons) do
			v:Hide()
		end
		while lines[1] do
			local t = tremove(lines)
			for k in pairs(t) do
				t[k] = nil
			end
			del(t)
		end

		if not self.tree then return end
		--Build the list of visible entries from the tree and status tables
		local status = self.status or self.localstatus
		local groupstatus = status.groups
		local tree = self.tree

		local treeframe = self.treeframe

		status.scrollToSelection = status.scrollToSelection or scrollToSelection	-- needs to be cached in case the control hasn't been drawn yet (code bails out below)

		self:BuildLevel(tree, 1)

		local numlines = #lines

		local maxlines = (floor(((self.treeframe:GetHeight()or 0) - 20 ) / 18))
		if maxlines <= 0 then return end

		if self.frame:GetParent() == UIParent and not fromOnUpdate then
			self.frame:SetScript("OnUpdate", FirstFrameUpdate)
			return
		end

		local first, last

		scrollToSelection = status.scrollToSelection
		status.scrollToSelection = nil

		if numlines <= maxlines then
			--the whole tree fits in the frame
			status.scrollvalue = 0
			self:ShowScroll(false)
			first, last = 1, numlines
		else
			self:ShowScroll(true)
			--scrolling will be needed
			self.noupdate = true
			self.scrollbar:SetMinMaxValues(0, numlines - maxlines)
			--check if we are scrolled down too far
			if numlines - status.scrollvalue < maxlines then
				status.scrollvalue = numlines - maxlines
			end
			self.noupdate = nil
			first, last = status.scrollvalue+1, status.scrollvalue + maxlines
			--show selection?
			if scrollToSelection and status.selected then
				local show
				for i,line in ipairs(lines) do	-- find the line number
					if line.uniquevalue==status.selected then
						show=i
					end
				end
				if not show then
					-- selection was deleted or something?
				elseif show>=first and show<=last then
					-- all good
				else
					-- scrolling needed!
					if show<first then
						status.scrollvalue = show-1
					else
						status.scrollvalue = show-maxlines
					end
					first, last = status.scrollvalue+1, status.scrollvalue + maxlines
				end
			end
			if self.scrollbar:GetValue() ~= status.scrollvalue then
				self.scrollbar:SetValue(status.scrollvalue)
			end
		end

		local buttonnum = 1
		for i = first, last do
			local line = lines[i]
			local button = buttons[buttonnum]
			if not button then
				button = self:CreateButton()

				buttons[buttonnum] = button
				button:SetParent(treeframe)
				button:SetFrameLevel(treeframe:GetFrameLevel()+1)
				button:ClearAllPoints()
				if buttonnum == 1 then
					if self.showscroll then
						button:SetPoint("TOPRIGHT", -22, -10)
						button:SetPoint("TOPLEFT", 0, -10)
					else
						button:SetPoint("TOPRIGHT", 0, -10)
						button:SetPoint("TOPLEFT", 0, -10)
					end
				else
					button:SetPoint("TOPRIGHT", buttons[buttonnum-1], "BOTTOMRIGHT",0,0)
					button:SetPoint("TOPLEFT", buttons[buttonnum-1], "BOTTOMLEFT",0,0)
				end
			end

			UpdateButton(button, line, status.selected == line.uniquevalue, line.hasChildren, groupstatus[line.uniquevalue] )
			button:Show()
			buttonnum = buttonnum + 1
		end

	end,

	["SetSelected"] = function(self, value)
		local status = self.status or self.localstatus
		if status.selected ~= value then
			status.selected = value
			self:Fire("OnGroupSelected", value)
		end
	end,

	["Select"] = function(self, uniquevalue, ...)
		self.filter = false
		local status = self.status or self.localstatus
		local groups = status.groups
		local path = {...}
		for i = 1, #path do
			groups[tconcat(path, "\001", 1, i)] = true
		end
		status.selected = uniquevalue
		self:RefreshTree(true)
		self:Fire("OnGroupSelected", uniquevalue)
	end,

	["SelectByPath"] = function(self, ...)
		self:Select(BuildUniqueValue(...), ...)
	end,

	["SelectByValue"] = function(self, uniquevalue)
		self:Select(uniquevalue, ("\001"):split(uniquevalue))
	end,

	["ShowScroll"] = function(self, show)
		self.showscroll = show
		if show then
			self.scrollbar:Show()
			if self.buttons[1] then
				self.buttons[1]:SetPoint("TOPRIGHT", self.treeframe,"TOPRIGHT",-22,-10)
			end
		else
			self.scrollbar:Hide()
			if self.buttons[1] then
				self.buttons[1]:SetPoint("TOPRIGHT", self.treeframe,"TOPRIGHT",0,-10)
			end
		end
	end,

	["OnWidthSet"] = function(self, width)
		local content = self.content
		local treeframe = self.treeframe
		local status = self.status or self.localstatus
		status.fullwidth = width

		local contentwidth = width - status.treewidth - 20
		if contentwidth < 0 then
			contentwidth = 0
		end
		content:SetWidth(contentwidth)
		content.width = contentwidth

		local maxtreewidth = math_min(400, width - 50)

		if maxtreewidth > 100 and status.treewidth > maxtreewidth then
			self:SetTreeWidth(maxtreewidth, status.treesizable)
		end
		if treeframe.SetResizeBounds then
			treeframe:SetResizeBounds(100, 1, maxtreewidth, 1600)
		else
			treeframe:SetMaxResize(maxtreewidth, 1600)
		end
	end,

	["OnHeightSet"] = function(self, height)
		local content = self.content
		local contentheight = height - 20
		if contentheight < 0 then
			contentheight = 0
		end
		content:SetHeight(contentheight)
		content.height = contentheight
	end,

	["SetTreeWidth"] = function(self, treewidth, resizable)
		if not resizable then
			if type(treewidth) == 'number' then
				resizable = false
			elseif type(treewidth) == 'boolean' then
				resizable = treewidth
				treewidth = DEFAULT_TREE_WIDTH
			else
				resizable = false
				treewidth = DEFAULT_TREE_WIDTH
			end
		end
		self.treeframe:SetWidth(treewidth)
		self.dragger:EnableMouse(resizable)

		local status = self.status or self.localstatus
		status.treewidth = treewidth
		status.treesizable = resizable

		-- recalculate the content width
		if status.fullwidth then
			self:OnWidthSet(status.fullwidth)
		end
	end,

	["GetTreeWidth"] = function(self)
		local status = self.status or self.localstatus
		return status.treewidth or DEFAULT_TREE_WIDTH
	end,

	["LayoutFinished"] = function(self, width, height)
		if self.noAutoHeight then return end
		self:SetHeight((height or 0) + 20)
	end
}

--[[-----------------------------------------------------------------------------
Constructor
-------------------------------------------------------------------------------]]
local PaneBackdrop  = {
	bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
	edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
	tile = true, tileSize = 16, edgeSize = 16,
	insets = { left = 3, right = 3, top = 5, bottom = 3 }
}

local DraggerBackdrop  = {
	bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
	edgeFile = nil,
	tile = true, tileSize = 16, edgeSize = 1,
	insets = { left = 3, right = 3, top = 7, bottom = 7 }
}

local function Constructor()
	local num = AceGUI:GetNextWidgetNum(Type)
	local frame = CreateFrame("Frame", nil, UIParent)

	local treeframe = CreateFrame("Frame", nil, frame, "BackdropTemplate")
	treeframe:SetPoint("TOPLEFT")
	treeframe:SetPoint("BOTTOMLEFT")
	treeframe:SetWidth(DEFAULT_TREE_WIDTH)
	treeframe:EnableMouseWheel(true)
	treeframe:SetBackdrop(PaneBackdrop)
	treeframe:SetBackdropColor(0.1, 0.1, 0.1, 0.5)
	treeframe:SetBackdropBorderColor(0.4, 0.4, 0.4)
	treeframe:SetResizable(true)
	if treeframe.SetResizeBounds then -- WoW 10.0
		treeframe:SetResizeBounds(100, 1, 400, 1600)
	else
		treeframe:SetMinResize(100, 1)
		treeframe:SetMaxResize(400, 1600)
	end
	treeframe:SetScript("OnUpdate", FirstFrameUpdate)
	treeframe:SetScript("OnSizeChanged", Tree_OnSizeChanged)
	treeframe:SetScript("OnMouseWheel", Tree_OnMouseWheel)

	local dragger = CreateFrame("Frame", nil, treeframe, "BackdropTemplate")
	dragger:SetWidth(8)
	dragger:SetPoint("TOP", treeframe, "TOPRIGHT")
	dragger:SetPoint("BOTTOM", treeframe, "BOTTOMRIGHT")
	dragger:SetBackdrop(DraggerBackdrop)
	dragger:SetBackdropColor(1, 1, 1, 0)
	dragger:SetScript("OnEnter", Dragger_OnEnter)
	dragger:SetScript("OnLeave", Dragger_OnLeave)
	dragger:SetScript("OnMouseDown", Dragger_OnMouseDown)
	dragger:SetScript("OnMouseUp", Dragger_OnMouseUp)

	local scrollbar = CreateFrame("Slider", ("AceConfigDialogTreeGroup%dScrollBar"):format(num), treeframe, "UIPanelScrollBarTemplate")
	scrollbar:SetScript("OnValueChanged", nil)
	scrollbar:SetPoint("TOPRIGHT", -10, -26)
	scrollbar:SetPoint("BOTTOMRIGHT", -10, 26)
	scrollbar:SetMinMaxValues(0,0)
	scrollbar:SetValueStep(1)
	scrollbar:SetValue(0)
	scrollbar:SetWidth(16)
	scrollbar:SetScript("OnValueChanged", OnScrollValueChanged)

	local scrollbg = scrollbar:CreateTexture(nil, "BACKGROUND")
	scrollbg:SetAllPoints(scrollbar)
	scrollbg:SetColorTexture(0,0,0,0.4)

	local border = CreateFrame("Frame", nil, frame, "BackdropTemplate")
	border:SetPoint("TOPLEFT", treeframe, "TOPRIGHT")
	border:SetPoint("BOTTOMRIGHT")
	border:SetBackdrop(PaneBackdrop)
	border:SetBackdropColor(0.1, 0.1, 0.1, 0.5)
	border:SetBackdropBorderColor(0.4, 0.4, 0.4)

	--Container Support
	local content = CreateFrame("Frame", nil, border)
	content:SetPoint("TOPLEFT", 10, -10)
	content:SetPoint("BOTTOMRIGHT", -10, 10)

	local widget = {
		frame        = frame,
		lines        = {},
		levels       = {},
		buttons      = {},
		hasChildren  = {},
		localstatus  = { groups = {}, scrollvalue = 0 },
		filter       = false,
		treeframe    = treeframe,
		dragger      = dragger,
		scrollbar    = scrollbar,
		border       = border,
		content      = content,
		type         = Type
	}
	for method, func in pairs(methods) do
		widget[method] = func
	end
	treeframe.obj, dragger.obj, scrollbar.obj = widget, widget, widget

	return AceGUI:RegisterAsContainer(widget)
end

AceGUI:RegisterWidgetType(Type, Constructor, Version)
