1 --[[-----------------------------------------------------------------------------
3 Plain container that scrolls its content and doesn't grow in height.
4 -------------------------------------------------------------------------------]]
5 local Type, Version = "ScrollFrame", 23
6 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
7 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
10 local pairs, assert, type = pairs, assert, type
11 local min, max, floor, abs = math.min, math.max, math.floor, math.abs
14 local CreateFrame, UIParent = CreateFrame, UIParent
16 --[[-----------------------------------------------------------------------------
18 -------------------------------------------------------------------------------]]
19 local function FixScrollOnUpdate(frame)
20 frame:SetScript("OnUpdate", nil)
24 --[[-----------------------------------------------------------------------------
26 -------------------------------------------------------------------------------]]
27 local function ScrollFrame_OnMouseWheel(frame, value)
28 frame.obj:MoveScroll(value)
31 local function ScrollFrame_OnSizeChanged(frame)
32 frame:SetScript("OnUpdate", FixScrollOnUpdate)
35 local function ScrollBar_OnScrollValueChanged(frame, value)
36 frame.obj:SetScroll(value)
39 --[[-----------------------------------------------------------------------------
41 -------------------------------------------------------------------------------]]
43 ["OnAcquire"] = function(self)
45 self.scrollframe:SetScript("OnUpdate", FixScrollOnUpdate)
48 ["OnRelease"] = function(self)
50 for k in pairs(self.localstatus) do
51 self.localstatus[k] = nil
53 self.scrollframe:SetPoint("BOTTOMRIGHT")
55 self.scrollBarShown = nil
56 self.content.height, self.content.width = nil, nil
59 ["SetScroll"] = function(self, value)
60 local status = self.status or self.localstatus
61 local viewheight = self.scrollframe:GetHeight()
62 local height = self.content:GetHeight()
65 if viewheight > height then
68 offset = floor((height - viewheight) / 1000.0 * value)
70 self.content:ClearAllPoints()
71 self.content:SetPoint("TOPLEFT", 0, offset)
72 self.content:SetPoint("TOPRIGHT", 0, offset)
73 status.offset = offset
74 status.scrollvalue = value
77 ["MoveScroll"] = function(self, value)
78 local status = self.status or self.localstatus
79 local height, viewheight = self.scrollframe:GetHeight(), self.content:GetHeight()
81 if self.scrollBarShown then
82 local diff = height - viewheight
87 self.scrollbar:SetValue(min(max(status.scrollvalue + delta*(1000/(diff/45)),0), 1000))
91 ["FixScroll"] = function(self)
92 if self.updateLock then return end
93 self.updateLock = true
94 local status = self.status or self.localstatus
95 local height, viewheight = self.scrollframe:GetHeight(), self.content:GetHeight()
96 local offset = status.offset or 0
97 local curvalue = self.scrollbar:GetValue()
98 -- Give us a margin of error of 2 pixels to stop some conditions that i would blame on floating point inaccuracys
99 -- No-one is going to miss 2 pixels at the bottom of the frame, anyhow!
100 if viewheight < height + 2 then
101 if self.scrollBarShown then
102 self.scrollBarShown = nil
103 self.scrollbar:Hide()
104 self.scrollbar:SetValue(0)
105 self.scrollframe:SetPoint("BOTTOMRIGHT")
109 if not self.scrollBarShown then
110 self.scrollBarShown = true
111 self.scrollbar:Show()
112 self.scrollframe:SetPoint("BOTTOMRIGHT", -20, 0)
115 local value = (offset / (viewheight - height) * 1000)
116 if value > 1000 then value = 1000 end
117 self.scrollbar:SetValue(value)
118 self:SetScroll(value)
120 self.content:ClearAllPoints()
121 self.content:SetPoint("TOPLEFT", 0, offset)
122 self.content:SetPoint("TOPRIGHT", 0, offset)
123 status.offset = offset
126 self.updateLock = nil
129 ["LayoutFinished"] = function(self, width, height)
130 self.content:SetHeight(height or 0 + 20)
131 self.scrollframe:SetScript("OnUpdate", FixScrollOnUpdate)
134 ["SetStatusTable"] = function(self, status)
135 assert(type(status) == "table")
137 if not status.scrollvalue then
138 status.scrollvalue = 0
142 ["OnWidthSet"] = function(self, width)
143 local content = self.content
144 content.width = width
147 ["OnHeightSet"] = function(self, height)
148 local content = self.content
149 content.height = height
152 --[[-----------------------------------------------------------------------------
154 -------------------------------------------------------------------------------]]
155 local function Constructor()
156 local frame = CreateFrame("Frame", nil, UIParent)
157 local num = AceGUI:GetNextWidgetNum(Type)
159 local scrollframe = CreateFrame("ScrollFrame", nil, frame)
160 scrollframe:SetPoint("TOPLEFT")
161 scrollframe:SetPoint("BOTTOMRIGHT")
162 scrollframe:EnableMouseWheel(true)
163 scrollframe:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel)
164 scrollframe:SetScript("OnSizeChanged", ScrollFrame_OnSizeChanged)
166 local scrollbar = CreateFrame("Slider", ("AceConfigDialogScrollFrame%dScrollBar"):format(num), scrollframe, "UIPanelScrollBarTemplate")
167 scrollbar:SetPoint("TOPLEFT", scrollframe, "TOPRIGHT", 4, -16)
168 scrollbar:SetPoint("BOTTOMLEFT", scrollframe, "BOTTOMRIGHT", 4, 16)
169 scrollbar:SetMinMaxValues(0, 1000)
170 scrollbar:SetValueStep(1)
171 scrollbar:SetValue(0)
172 scrollbar:SetWidth(16)
174 -- set the script as the last step, so it doesn't fire yet
175 scrollbar:SetScript("OnValueChanged", ScrollBar_OnScrollValueChanged)
177 local scrollbg = scrollbar:CreateTexture(nil, "BACKGROUND")
178 scrollbg:SetAllPoints(scrollbar)
179 scrollbg:SetTexture(0, 0, 0, 0.4)
182 local content = CreateFrame("Frame", nil, scrollframe)
183 content:SetPoint("TOPLEFT")
184 content:SetPoint("TOPRIGHT")
185 content:SetHeight(400)
186 scrollframe:SetScrollChild(content)
189 localstatus = { scrollvalue = 0 },
190 scrollframe = scrollframe,
191 scrollbar = scrollbar,
196 for method, func in pairs(methods) do
197 widget[method] = func
199 scrollframe.obj, scrollbar.obj = widget, widget
201 return AceGUI:RegisterAsContainer(widget)
204 AceGUI:RegisterWidgetType(Type, Constructor, Version)