8da63c0 - Add incoming res icon
[wowui.git] / OmaRFConfig / libs / AceGUI-3.0 / widgets / AceGUIContainer-ScrollFrame.lua
1 --[[-----------------------------------------------------------------------------
2 ScrollFrame Container
3 Plain container that scrolls its content and doesn't grow in height.
4 -------------------------------------------------------------------------------]]
5 local Type, Version = "ScrollFrame", 24
6 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
7 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
8
9 -- Lua APIs
10 local pairs, assert, type = pairs, assert, type
11 local min, max, floor, abs = math.min, math.max, math.floor, math.abs
12
13 -- WoW APIs
14 local CreateFrame, UIParent = CreateFrame, UIParent
15
16 --[[-----------------------------------------------------------------------------
17 Support functions
18 -------------------------------------------------------------------------------]]
19 local function FixScrollOnUpdate(frame)
20         frame:SetScript("OnUpdate", nil)
21         frame.obj:FixScroll()
22 end
23
24 --[[-----------------------------------------------------------------------------
25 Scripts
26 -------------------------------------------------------------------------------]]
27 local function ScrollFrame_OnMouseWheel(frame, value)
28         frame.obj:MoveScroll(value)
29 end
30
31 local function ScrollFrame_OnSizeChanged(frame)
32         frame:SetScript("OnUpdate", FixScrollOnUpdate)
33 end
34
35 local function ScrollBar_OnScrollValueChanged(frame, value)
36         frame.obj:SetScroll(value)
37 end
38
39 --[[-----------------------------------------------------------------------------
40 Methods
41 -------------------------------------------------------------------------------]]
42 local methods = {
43         ["OnAcquire"] = function(self) 
44                 self:SetScroll(0)
45                 self.scrollframe:SetScript("OnUpdate", FixScrollOnUpdate)
46         end,
47
48         ["OnRelease"] = function(self)
49                 self.status = nil
50                 for k in pairs(self.localstatus) do
51                         self.localstatus[k] = nil
52                 end
53                 self.scrollframe:SetPoint("BOTTOMRIGHT")
54                 self.scrollbar:Hide()
55                 self.scrollBarShown = nil
56                 self.content.height, self.content.width = nil, nil
57         end,
58
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()
63                 local offset
64
65                 if viewheight > height then
66                         offset = 0
67                 else
68                         offset = floor((height - viewheight) / 1000.0 * value)
69                 end
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
75         end,
76
77         ["MoveScroll"] = function(self, value)
78                 local status = self.status or self.localstatus
79                 local height, viewheight = self.scrollframe:GetHeight(), self.content:GetHeight()
80                 
81                 if self.scrollBarShown then
82                         local diff = height - viewheight
83                         local delta = 1
84                         if value < 0 then
85                                 delta = -1
86                         end
87                         self.scrollbar:SetValue(min(max(status.scrollvalue + delta*(1000/(diff/45)),0), 1000))
88                 end
89         end,
90
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")
106                                 self:DoLayout()
107                         end
108                 else
109                         if not self.scrollBarShown then
110                                 self.scrollBarShown = true
111                                 self.scrollbar:Show()
112                                 self.scrollframe:SetPoint("BOTTOMRIGHT", -20, 0)
113                                 self:DoLayout()
114                         end
115                         local value = (offset / (viewheight - height) * 1000)
116                         if value > 1000 then value = 1000 end
117                         self.scrollbar:SetValue(value)
118                         self:SetScroll(value)
119                         if value < 1000 then
120                                 self.content:ClearAllPoints()
121                                 self.content:SetPoint("TOPLEFT", 0, offset)
122                                 self.content:SetPoint("TOPRIGHT", 0, offset)
123                                 status.offset = offset
124                         end
125                 end
126                 self.updateLock = nil
127         end,
128
129         ["LayoutFinished"] = function(self, width, height)
130                 self.content:SetHeight(height or 0 + 20)
131                 self.scrollframe:SetScript("OnUpdate", FixScrollOnUpdate)
132         end,
133
134         ["SetStatusTable"] = function(self, status)
135                 assert(type(status) == "table")
136                 self.status = status
137                 if not status.scrollvalue then
138                         status.scrollvalue = 0
139                 end
140         end,
141
142         ["OnWidthSet"] = function(self, width)
143                 local content = self.content
144                 content.width = width
145         end,
146
147         ["OnHeightSet"] = function(self, height)
148                 local content = self.content
149                 content.height = height
150         end
151 }
152 --[[-----------------------------------------------------------------------------
153 Constructor
154 -------------------------------------------------------------------------------]]
155 local function Constructor()
156         local frame = CreateFrame("Frame", nil, UIParent)
157         local num = AceGUI:GetNextWidgetNum(Type)
158
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)
165
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)
173         scrollbar:Hide()
174         -- set the script as the last step, so it doesn't fire yet
175         scrollbar:SetScript("OnValueChanged", ScrollBar_OnScrollValueChanged)
176
177         local scrollbg = scrollbar:CreateTexture(nil, "BACKGROUND")
178         scrollbg:SetAllPoints(scrollbar)
179         scrollbg:SetColorTexture(0, 0, 0, 0.4)
180
181         --Container Support
182         local content = CreateFrame("Frame", nil, scrollframe)
183         content:SetPoint("TOPLEFT")
184         content:SetPoint("TOPRIGHT")
185         content:SetHeight(400)
186         scrollframe:SetScrollChild(content)
187
188         local widget = {
189                 localstatus = { scrollvalue = 0 },
190                 scrollframe = scrollframe,
191                 scrollbar   = scrollbar,
192                 content     = content,
193                 frame       = frame,
194                 type        = Type
195         }
196         for method, func in pairs(methods) do
197                 widget[method] = func
198         end
199         scrollframe.obj, scrollbar.obj = widget, widget
200
201         return AceGUI:RegisterAsContainer(widget)
202 end
203
204 AceGUI:RegisterWidgetType(Type, Constructor, Version)