local h,m="\083\099\114\111\108\108\070\114\097\109\101",26 local t=LibStub and LibStub("\065\099\101\071\085\073-\051\046\048",true)if not t or(t:GetWidgetVersion(h)or 0)>=m then return end local a,N,z=pairs,assert,type local s,S,g=math.min,math.max,math.floor local i,j=CreateFrame,UIParent local function D(h)h:SetScript("\079\110\085\112\100\097\116\101",nil)h.obj:FixScroll()end local function U(h,m)h.obj:MoveScroll(m)end local function E(h)h:SetScript("\079\110\085\112\100\097\116\101",D)end local function Q(h,m)h.obj:SetScroll(m)end local L={OnAcquire=function(h)h:SetScroll(0)h.scrollframe:SetScript("\079\110\085\112\100\097\116\101",D)end;OnRelease=function(h)h.status=nil for m in a(h.localstatus)do h.localstatus[m]=nil end h.scrollframe:SetPoint("\066\079\084\084\079\077\082\073\071\072\084")h.scrollbar:Hide()h.scrollBarShown=nil h.content.height,h.content.width,h.content.original_width=nil,nil,nil end,SetScroll=function(h,m)local t=h.status or h.localstatus local a=h.scrollframe:GetHeight()local N=h.content:GetHeight()local z if a>N then z=0 else z=g(((N-a)/1000.0)*m)end h.content:ClearAllPoints()h.content:SetPoint("\084\079\080\076\069\070\084",0,z)h.content:SetPoint("\084\079\080\082\073\071\072\084",0,z)t.offset=z t.scrollvalue=m end;MoveScroll=function(h,m)local t=h.status or h.localstatus local a,N=h.scrollframe:GetHeight(),h.content:GetHeight()if h.scrollBarShown then local z=a-N local g=1 if m<0 then g=-1 end h.scrollbar:SetValue(s(S(t.scrollvalue+g*(1000/(z/45)),0),1000))end end;FixScroll=function(h)if h.updateLock then return end h.updateLock=true local m=h.status or h.localstatus local t,a=h.scrollframe:GetHeight(),h.content:GetHeight()local N=m.offset or 0 if a<t+2 then if h.scrollBarShown then h.scrollBarShown=nil h.scrollbar:Hide()h.scrollbar:SetValue(0)h.scrollframe:SetPoint("\066\079\084\084\079\077\082\073\071\072\084")if h.content.original_width then h.content.width=h.content.original_width end h:DoLayout()end else if not h.scrollBarShown then h.scrollBarShown=true h.scrollbar:Show()h.scrollframe:SetPoint("\066\079\084\084\079\077\082\073\071\072\084",-20,0)if h.content.original_width then h.content.width=h.content.original_width-20 end h:DoLayout()end local z=(N/(a-t))*1000 if z>1000 then z=1000 end h.scrollbar:SetValue(z)h:SetScroll(z)if z<1000 then h.content:ClearAllPoints()h.content:SetPoint("\084\079\080\076\069\070\084",0,N)h.content:SetPoint("\084\079\080\082\073\071\072\084",0,N)m.offset=N end end h.updateLock=nil end;LayoutFinished=function(h,m,t)h.content:SetHeight(t or 20)h:FixScroll()h.scrollframe:SetScript("\079\110\085\112\100\097\116\101",D)end;SetStatusTable=function(h,m)N(z(m)=="\116\097\098\108\101")h.status=m if not m.scrollvalue then m.scrollvalue=0 end end,OnWidthSet=function(h,m)local t=h.content t.width=m-(h.scrollBarShown and 20 or 0)t.original_width=m end;OnHeightSet=function(h,m)local t=h.content t.height=m end}local function H()local m=i("\070\114\097\109\101",nil,j)local N=t:GetNextWidgetNum(h)local z=i("\083\099\114\111\108\108\070\114\097\109\101",nil,m)z:SetPoint("\084\079\080\076\069\070\084")z:SetPoint("\066\079\084\084\079\077\082\073\071\072\084")z:EnableMouseWheel(true)z:SetScript("\079\110\077\111\117\115\101\087\104\101\101\108",U)z:SetScript("\079\110\083\105\122\101\067\104\097\110\103\101\100",E)local s=i("\083\108\105\100\101\114",("\065\099\101\067\111\110\102\105\103\068\105\097\108\111\103\083\099\114\111\108\108\070\114\097\109\101\037\100\083\099\114\111\108\108\066\097\114"):format(N),z,"\085\073\080\097\110\101\108\083\099\114\111\108\108\066\097\114\084\101\109\112\108\097\116\101")s:SetPoint("\084\079\080\076\069\070\084",z,"\084\079\080\082\073\071\072\084",4,-16)s:SetPoint("\066\079\084\084\079\077\076\069\070\084",z,"\066\079\084\084\079\077\082\073\071\072\084",4,16)s:SetMinMaxValues(0,1000)s:SetValueStep(1)s:SetValue(0)s:SetWidth(16)s:Hide()s:SetScript("\079\110\086\097\108\117\101\067\104\097\110\103\101\100",Q)local S=s:CreateTexture(nil,"\066\065\067\075\071\082\079\085\078\068")S:SetAllPoints(s)S:SetColorTexture(0,0,0,.4)local g=i("\070\114\097\109\101",nil,z)g:SetPoint("\084\079\080\076\069\070\084")g:SetPoint("\084\079\080\082\073\071\072\084")g:SetHeight(400)z:SetScrollChild(g)local D={localstatus={scrollvalue=0};scrollframe=z;scrollbar=s;content=g,frame=m,type=h}for h,m in a(L)do D[h]=m end z.obj,s.obj=D,D return t:RegisterAsContainer(D)end t:RegisterWidgetType(h,H,m)
--[[-----------------------------------------------------------------------------
ScrollFrame Container
Plain container that scrolls its content and doesn't grow in height.
-------------------------------------------------------------------------------]]
local Type, Version = "ScrollFrame", 26
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 pairs, assert, type = pairs, assert, type
local min, max, floor = math.min, math.max, math.floor

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

--[[-----------------------------------------------------------------------------
Support functions
-------------------------------------------------------------------------------]]
local function FixScrollOnUpdate(frame)
	frame:SetScript("OnUpdate", nil)
	frame.obj:FixScroll()
end

--[[-----------------------------------------------------------------------------
Scripts
-------------------------------------------------------------------------------]]
local function ScrollFrame_OnMouseWheel(frame, value)
	frame.obj:MoveScroll(value)
end

local function ScrollFrame_OnSizeChanged(frame)
	frame:SetScript("OnUpdate", FixScrollOnUpdate)
end

local function ScrollBar_OnScrollValueChanged(frame, value)
	frame.obj:SetScroll(value)
end

--[[-----------------------------------------------------------------------------
Methods
-------------------------------------------------------------------------------]]
local methods = {
	["OnAcquire"] = function(self)
		self:SetScroll(0)
		self.scrollframe:SetScript("OnUpdate", FixScrollOnUpdate)
	end,

	["OnRelease"] = function(self)
		self.status = nil
		for k in pairs(self.localstatus) do
			self.localstatus[k] = nil
		end
		self.scrollframe:SetPoint("BOTTOMRIGHT")
		self.scrollbar:Hide()
		self.scrollBarShown = nil
		self.content.height, self.content.width, self.content.original_width = nil, nil, nil
	end,

	["SetScroll"] = function(self, value)
		local status = self.status or self.localstatus
		local viewheight = self.scrollframe:GetHeight()
		local height = self.content:GetHeight()
		local offset

		if viewheight > height then
			offset = 0
		else
			offset = floor((height - viewheight) / 1000.0 * value)
		end
		self.content:ClearAllPoints()
		self.content:SetPoint("TOPLEFT", 0, offset)
		self.content:SetPoint("TOPRIGHT", 0, offset)
		status.offset = offset
		status.scrollvalue = value
	end,

	["MoveScroll"] = function(self, value)
		local status = self.status or self.localstatus
		local height, viewheight = self.scrollframe:GetHeight(), self.content:GetHeight()

		if self.scrollBarShown then
			local diff = height - viewheight
			local delta = 1
			if value < 0 then
				delta = -1
			end
			self.scrollbar:SetValue(min(max(status.scrollvalue + delta*(1000/(diff/45)),0), 1000))
		end
	end,

	["FixScroll"] = function(self)
		if self.updateLock then return end
		self.updateLock = true
		local status = self.status or self.localstatus
		local height, viewheight = self.scrollframe:GetHeight(), self.content:GetHeight()
		local offset = status.offset or 0
		-- Give us a margin of error of 2 pixels to stop some conditions that i would blame on floating point inaccuracys
		-- No-one is going to miss 2 pixels at the bottom of the frame, anyhow!
		if viewheight < height + 2 then
			if self.scrollBarShown then
				self.scrollBarShown = nil
				self.scrollbar:Hide()
				self.scrollbar:SetValue(0)
				self.scrollframe:SetPoint("BOTTOMRIGHT")
				if self.content.original_width then
					self.content.width = self.content.original_width
				end
				self:DoLayout()
			end
		else
			if not self.scrollBarShown then
				self.scrollBarShown = true
				self.scrollbar:Show()
				self.scrollframe:SetPoint("BOTTOMRIGHT", -20, 0)
				if self.content.original_width then
					self.content.width = self.content.original_width - 20
				end
				self:DoLayout()
			end
			local value = (offset / (viewheight - height) * 1000)
			if value > 1000 then value = 1000 end
			self.scrollbar:SetValue(value)
			self:SetScroll(value)
			if value < 1000 then
				self.content:ClearAllPoints()
				self.content:SetPoint("TOPLEFT", 0, offset)
				self.content:SetPoint("TOPRIGHT", 0, offset)
				status.offset = offset
			end
		end
		self.updateLock = nil
	end,

	["LayoutFinished"] = function(self, width, height)
		self.content:SetHeight(height or 0 + 20)

		-- update the scrollframe
		self:FixScroll()

		-- schedule another update when everything has "settled"
		self.scrollframe:SetScript("OnUpdate", FixScrollOnUpdate)
	end,

	["SetStatusTable"] = function(self, status)
		assert(type(status) == "table")
		self.status = status
		if not status.scrollvalue then
			status.scrollvalue = 0
		end
	end,

	["OnWidthSet"] = function(self, width)
		local content = self.content
		content.width = width - (self.scrollBarShown and 20 or 0)
		content.original_width = width
	end,

	["OnHeightSet"] = function(self, height)
		local content = self.content
		content.height = height
	end
}
--[[-----------------------------------------------------------------------------
Constructor
-------------------------------------------------------------------------------]]
local function Constructor()
	local frame = CreateFrame("Frame", nil, UIParent)
	local num = AceGUI:GetNextWidgetNum(Type)

	local scrollframe = CreateFrame("ScrollFrame", nil, frame)
	scrollframe:SetPoint("TOPLEFT")
	scrollframe:SetPoint("BOTTOMRIGHT")
	scrollframe:EnableMouseWheel(true)
	scrollframe:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel)
	scrollframe:SetScript("OnSizeChanged", ScrollFrame_OnSizeChanged)

	local scrollbar = CreateFrame("Slider", ("AceConfigDialogScrollFrame%dScrollBar"):format(num), scrollframe, "UIPanelScrollBarTemplate")
	scrollbar:SetPoint("TOPLEFT", scrollframe, "TOPRIGHT", 4, -16)
	scrollbar:SetPoint("BOTTOMLEFT", scrollframe, "BOTTOMRIGHT", 4, 16)
	scrollbar:SetMinMaxValues(0, 1000)
	scrollbar:SetValueStep(1)
	scrollbar:SetValue(0)
	scrollbar:SetWidth(16)
	scrollbar:Hide()
	-- set the script as the last step, so it doesn't fire yet
	scrollbar:SetScript("OnValueChanged", ScrollBar_OnScrollValueChanged)

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

	--Container Support
	local content = CreateFrame("Frame", nil, scrollframe)
	content:SetPoint("TOPLEFT")
	content:SetPoint("TOPRIGHT")
	content:SetHeight(400)
	scrollframe:SetScrollChild(content)

	local widget = {
		localstatus = { scrollvalue = 0 },
		scrollframe = scrollframe,
		scrollbar   = scrollbar,
		content     = content,
		frame       = frame,
		type        = Type
	}
	for method, func in pairs(methods) do
		widget[method] = func
	end
	scrollframe.obj, scrollbar.obj = widget, widget

	return AceGUI:RegisterAsContainer(widget)
end

AceGUI:RegisterWidgetType(Type, Constructor, Version)
