local x,w="\083\099\114\111\108\108\070\114\097\109\101",26 local v=LibStub and LibStub("\065\099\101\071\085\073-\051\046\048",true)if not v or(v:GetWidgetVersion(x)or 0)>=w then return end local N,l,e=pairs,assert,type local z,i,A=math.min,math.max,math.floor local n,L=CreateFrame,UIParent local function c(x)x:SetScript("\079\110\085\112\100\097\116\101",nil)x.obj:FixScroll()end local function f(x,w)x.obj:MoveScroll(w)end local function o(x)x:SetScript("\079\110\085\112\100\097\116\101",c)end local function u(x,w)x.obj:SetScroll(w)end local Q={OnAcquire=function(x)x:SetScroll(0)x.scrollframe:SetScript("\079\110\085\112\100\097\116\101",c)end,OnRelease=function(x)x.status=nil for w in N(x.localstatus)do x.localstatus[w]=nil end x.scrollframe:SetPoint("\066\079\084\084\079\077\082\073\071\072\084")x.scrollbar:Hide()x.scrollBarShown=nil x.content.height,x.content.width,x.content.original_width=nil,nil,nil end;SetScroll=function(x,w)local v=x.status or x.localstatus local N=x.scrollframe:GetHeight()local l=x.content:GetHeight()local e if N>l then e=0 else e=A(((l-N)/1000.0)*w)end x.content:ClearAllPoints()x.content:SetPoint("\084\079\080\076\069\070\084",0,e)x.content:SetPoint("\084\079\080\082\073\071\072\084",0,e)v.offset=e v.scrollvalue=w end;MoveScroll=function(x,w)local v=x.status or x.localstatus local N,l=x.scrollframe:GetHeight(),x.content:GetHeight()if x.scrollBarShown then local e=N-l local A=1 if w<0 then A=-1 end x.scrollbar:SetValue(z(i(v.scrollvalue+A*(1000/(e/45)),0),1000))end end,FixScroll=function(x)if x.updateLock then return end x.updateLock=true local w=x.status or x.localstatus local v,N=x.scrollframe:GetHeight(),x.content:GetHeight()local l=w.offset or 0 if N<v+2 then if x.scrollBarShown then x.scrollBarShown=nil x.scrollbar:Hide()x.scrollbar:SetValue(0)x.scrollframe:SetPoint("\066\079\084\084\079\077\082\073\071\072\084")if x.content.original_width then x.content.width=x.content.original_width end x:DoLayout()end else if not x.scrollBarShown then x.scrollBarShown=true x.scrollbar:Show()x.scrollframe:SetPoint("\066\079\084\084\079\077\082\073\071\072\084",-20,0)if x.content.original_width then x.content.width=x.content.original_width-20 end x:DoLayout()end local e=(l/(N-v))*1000 if e>1000 then e=1000 end x.scrollbar:SetValue(e)x:SetScroll(e)if e<1000 then x.content:ClearAllPoints()x.content:SetPoint("\084\079\080\076\069\070\084",0,l)x.content:SetPoint("\084\079\080\082\073\071\072\084",0,l)w.offset=l end end x.updateLock=nil end,LayoutFinished=function(x,w,v)x.content:SetHeight(v or 20)x:FixScroll()x.scrollframe:SetScript("\079\110\085\112\100\097\116\101",c)end,SetStatusTable=function(x,w)l(e(w)=="\116\097\098\108\101")x.status=w if not w.scrollvalue then w.scrollvalue=0 end end,OnWidthSet=function(x,w)local v=x.content v.width=w-(x.scrollBarShown and 20 or 0)v.original_width=w end,OnHeightSet=function(x,w)local v=x.content v.height=w end}local function k()local w=n("\070\114\097\109\101",nil,L)local l=v:GetNextWidgetNum(x)local e=n("\083\099\114\111\108\108\070\114\097\109\101",nil,w)e:SetPoint("\084\079\080\076\069\070\084")e:SetPoint("\066\079\084\084\079\077\082\073\071\072\084")e:EnableMouseWheel(true)e:SetScript("\079\110\077\111\117\115\101\087\104\101\101\108",f)e:SetScript("\079\110\083\105\122\101\067\104\097\110\103\101\100",o)local z=n("\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(l),e,"\085\073\080\097\110\101\108\083\099\114\111\108\108\066\097\114\084\101\109\112\108\097\116\101")z:SetPoint("\084\079\080\076\069\070\084",e,"\084\079\080\082\073\071\072\084",4,-16)z:SetPoint("\066\079\084\084\079\077\076\069\070\084",e,"\066\079\084\084\079\077\082\073\071\072\084",4,16)z:SetMinMaxValues(0,1000)z:SetValueStep(1)z:SetValue(0)z:SetWidth(16)z:Hide()z:SetScript("\079\110\086\097\108\117\101\067\104\097\110\103\101\100",u)local i=z:CreateTexture(nil,"\066\065\067\075\071\082\079\085\078\068")i:SetAllPoints(z)i:SetColorTexture(0,0,0,.4)local A=n("\070\114\097\109\101",nil,e)A:SetPoint("\084\079\080\076\069\070\084")A:SetPoint("\084\079\080\082\073\071\072\084")A:SetHeight(400)e:SetScrollChild(A)local c={localstatus={scrollvalue=0};scrollframe=e,scrollbar=z,content=A;frame=w;type=x}for x,w in N(Q)do c[x]=w end e.obj,z.obj=c,c return v:RegisterAsContainer(c)end v:RegisterWidgetType(x,k,w)
--[[-----------------------------------------------------------------------------
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)
