vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1  
2 AceGUIListBox = AceGUIBorderFrame:new({
3 isUnit = TRUE,
4  
5 defaultRowHeight = 16,
6 frameWidthOffset = 25,
7 rowLeftOffset = 8,
8 scrollBarOffset = 16,
9 heightOffset = 15,
10 buttonHeightOffset = -1
11 })
12  
13 function AceGUIListBox:Setup()
14 if( not self._def.elements ) then self._def.elements = {} end
15 local elements = self._def.elements
16 if( not self._def.backdrop ) then self._def.backdrop = "small" end
17 elements.Label = {type = ACEGUI_FONTSTRING}
18 elements.NormalBackdrop = {type = ACEGUI_FRAME}
19 elements.SmallBackdrop = {type = ACEGUI_FRAME}
20 elements.ScrollBox = {
21 type = ACEGUI_SCROLL_FRAME,
22 anchors = {
23 topleft = {
24 xOffset = 10,
25 yOffset = -5
26 },
27 bottomleft = {
28 xOffset = 10,
29 yOffset = 4
30 },
31 topright = {
32 xOffset = -27,
33 yOffset = -5
34 },
35 bottomright = {
36 xOffset = -27,
37 yOffset = 4
38 }
39 }
40 }
41 elements.Spacer = {
42 type = ACEGUI_FRAME,
43 anchors = {
44 topright = {xOffset = 0, yOffset = 0},
45 bottomright = {xOffset = 0, yOffset = 0}
46 }
47 }
48 self:AddRows()
49 end
50  
51 function AceGUIListBox:AddRows()
52 local r, c = 0, 0
53 local prevRow, row, prevCol, col
54  
55 self.Rows = {}
56  
57 repeat
58 r = r + 1
59 prevRow = row
60 row = getglobal(self:GetName().."Row"..r)
61 if( row ) then
62 self.Rows[r] = row
63 row.Cols = {}
64 self._def.elements["Row"..r] = {
65 type = ACEGUI_BUTTON,
66 anchors = {
67 topleft = {
68 relTo = (prevRow or self):GetName(),
69 relPoint = prevRow and "bottomleft",
70 xOffset = (not prevRow) and self.rowLeftOffset or 0,
71 yOffset = (not prevRow) and -8 or self.buttonHeightOffset
72 },
73 topright = {
74 relTo = prevRow and prevRow:GetName() or "$parentSpacer",
75 relPoint = prevRow and "bottomright" or "topleft",
76 xOffset = 0,
77 yOffset = (not prevRow) and -8 or self.buttonHeightOffset
78 }
79 },
80 elements = {
81 Highlight = {type = ACEGUI_TEXTURE},
82 NormalText = {type = ACEGUI_FONTSTRING},
83 HighlightText = {type = ACEGUI_FONTSTRING},
84 DisabledText = {type = ACEGUI_FONTSTRING}
85 }
86 }
87 c = 1
88 col = getglobal(row:GetName().."Col"..c)
89 if( col ) then
90 prevCol = nil
91 while( col ) do
92 self._def.elements["Row"..r].elements["Col"..c] = {
93 type = self._def.colTypes
94 and self._def.colTypes[c]
95 or ACEGUI_BUTTON,
96 anchors = {
97 left = {
98 relTo = (prevCol or row):GetName(),
99 relPoint = prevCol and "right",
100 xOffset = prevCol and 0 or 2
101 }
102 },
103 }
104 self.Rows[r].Cols[c] = col
105 col.colID = c
106 col.rowIndex = r
107 if( getglobal(col:GetName().."Text") ) then
108 self._def.elements["Row"..r].elements["Col"..c].elements = {
109 Text = {type = ACEGUI_FONTSTRING}
110 }
111 end
112 col:SetScript("OnClick", function() self:CallHandler("OnItemClick") end)
113 col:SetScript("OnEnter", function() self:CallHandler("OnItemEnter") end)
114 col:SetScript("OnLeave", function() self:CallHandler("OnItemLeave") end)
115  
116 c = c + 1
117 prevCol = col
118 col = getglobal(row:GetName().."Col"..c)
119 end
120 else
121 self.Rows[r].Cols[c] = row
122 row.colID = 1
123 row.rowIndex = r
124 row:SetScript("OnClick", function() self:CallHandler("OnItemClick") end)
125 row:SetScript("OnEnter", function() self:CallHandler("OnItemEnter") end)
126 row:SetScript("OnLeave", function() self:CallHandler("OnItemLeave") end)
127 end
128 end
129 until( not row )
130 end
131  
132 function AceGUIListBox:Configure()
133 AceGUIBorderFrame.Configure(self)
134 if( self._def.barTexture ) then self.ScrollBox:ShowBarTexture() end
135 self:SetLabel()
136 end
137  
138 function AceGUIListBox:DynamicSize(w, h)
139 if( not w ) then
140 w = 0
141 for r = 1, getn(self.list), 1 do
142 local width = 0
143 if( (type(self.list[r]) == "table") and (getn(self.list[r]) > 0) ) then
144 for c = 1, getn(self.list[r]) do
145 width = width + self.Rows[1].Cols[1]:GetWidth()
146 end
147 elseif( self.Rows[1].Cols[1].NormalText ) then
148 self.Rows[1].Cols[1]:SetText(self:GetListText(r))
149 width = width + self.Rows[1].Cols[1].NormalText:GetWidth()
150 elseif( self.Rows[1].Cols[1].Text ) then
151 self.Rows[1].Cols[1]:SetText(self:GetListText(r))
152 width = width + self.Rows[1].Cols[1].Text:GetWidth()
153 end
154 if( width > w ) then w = width end
155 end
156 w = w + self.frameWidthOffset + self.Spacer:GetWidth()
157 if( w < self._def.minWidth ) then w = self._def.minWidth end
158 end
159  
160 if( not h ) then
161 if( getn(self.list) > self.visibleRows ) then
162 h = self.visibleRows * (self.rowHeight + abs(self.buttonHeightOffset)) + self.heightOffset
163 else
164 h = getn(self.list) * (self.rowHeight + abs(self.buttonHeightOffset)) + self.heightOffset
165 end
166 end
167  
168 return w, h
169 end
170  
171 function AceGUIListBox:Resize(width, height, rh, cw)
172 local cwList
173  
174 if( not width ) then width = self._def.width end
175 if( not height ) then height = self._def.height end
176 if( not rh ) then rh = self._def.rowHeight end
177 if( not cw ) then cw = self._def.colWidth end
178  
179 self.rowHeight = rh or self.defaultRowHeight
180 self.ScrollBox.rowHeight = self.rowHeight
181  
182 width, height = self:DynamicSize(width, height)
183  
184 if( not cw ) then
185 cw = floor(
186 (width - self.rowLeftOffset - self.scrollBarOffset)
187 / getn(self.Rows[1].Cols)
188 )
189 elseif( type(cw) == "table" ) then
190 cwList = cw
191 cw = nil
192 end
193  
194 self:SetWidth(width)
195 self:SetHeight(height)
196  
197 for r = 1, getn(self.Rows) do
198 self.Rows[r]:SetHeight(self.rowHeight)
199 for c = 1, getn(self.Rows[r].Cols) do
200 self.Rows[r].Cols[c]:SetWidth(cw or cwList[c])
201 self.Rows[r].Cols[c]:SetHeight(self.rowHeight)
202 end
203 end
204 end
205  
206 function AceGUIListBox:Refresh()
207 if( (not self.list) or (self.list ~= self._def.options) ) then
208 self.list = self._def.options or self:CallMethod("fill")
209 end
210  
211 if( not self.list ) then return end
212  
213 self.visibleRows = self._def.visibleRows or getn(self.Rows)
214 self.totalLines = getn(self.list)
215  
216 if( self.totalLines < self.visibleRows ) then self.visibleRows = self.totalLines end
217  
218 for r = 1, getn(self.Rows) do
219 if( r <= self.visibleRows ) then
220 self.Rows[r]:Show()
221 else
222 self.Rows[r]:Hide()
223 end
224 end
225  
226 if( self.visibleRows < self.totalLines ) then
227 self.ScrollBox:SetWidth(self:GetWidth())
228 self.ScrollBox:Show()
229 self.Spacer:SetWidth(self.scrollBarOffset)
230 else
231 self.ScrollBox:Hide()
232 self.Spacer:SetWidth(1)
233 end
234  
235 self:Resize()
236 end
237  
238 -- Mostly borrowed from FauxScrollFrame_Update()
239 function AceGUIListBox:UpdateScrollBox()
240 if( (self.ScrollBox.ScrollBar:GetValue() or 0) <= 0 ) then
241 self.ScrollBox.ScrollBar:SetValue(0)
242 end
243  
244 local scrollFrameHeight, scrollChildHeight
245  
246 if( self.totalLines > 0 ) then
247 scrollFrameHeight = (self.totalLines - self.visibleRows) * self.rowHeight
248 scrollChildHeight = self.totalLines * self.rowHeight
249 if( scrollFrameHeight < 0 ) then
250 scrollFrameHeight = 0
251 end
252 self.ScrollBox.ScrollChildFrame:Show()
253 else
254 self.ScrollBox.ScrollChildFrame:Hide()
255 end
256 self.ScrollBox.ScrollBar:SetMinMaxValues(0, scrollFrameHeight or 0)
257 self.ScrollBox.ScrollBar:SetValueStep(self.rowHeight or 0)
258 self.ScrollBox.ScrollChildFrame:SetHeight(scrollChildHeight or 0)
259  
260 -- Arrow button handling
261 if( self.ScrollBox.ScrollBar:GetValue() == 0 ) then
262 self.ScrollBox.ScrollBar.ScrollUpButton:Disable()
263 else
264 self.ScrollBox.ScrollBar.ScrollUpButton:Enable()
265 end
266 if( (self.ScrollBox.ScrollBar:GetValue() - scrollFrameHeight) == 0) then
267 self.ScrollBox.ScrollBar.ScrollDownButton:Disable()
268 else
269 self.ScrollBox.ScrollBar.ScrollDownButton:Enable()
270 end
271 end
272  
273 function AceGUIListBox:Update()
274 if( (not self.list) or (self.totalLines ~= getn(self.list)) ) then self:Refresh() end
275 if( not self.list ) then return end
276  
277 local i
278  
279 self:UpdateScrollBox()
280  
281 if( self._lockedRow ) then self._lockedRow = nil end
282  
283 for r = 1, self.visibleRows do
284 i = r + self.ScrollBox.offset
285  
286 self.Rows[r].rowID = i
287 for c = 1, getn(self.Rows[r].Cols) do
288 self.Rows[r].Cols[c]:SetValue(self:GetListText(i, c) or "")
289 self.Rows[r].Cols[c].rowID = i
290 if( self:GetListVal(i, c) == self.selectedValue ) then
291 self:SetSelected(i, c)
292 end
293 end
294 if( i == self.selectedRowID ) then
295 self.Rows[r].Highlight:Show()
296 self.Rows[r]:LockHighlight()
297 self._lockedRow = self.Rows[r]
298 else
299 self.Rows[r].Highlight:Hide()
300 self.Rows[r]:UnlockHighlight()
301 end
302 end
303 end
304  
305 function AceGUIListBox:OnVerticalScroll()
306 self.ScrollBox:OnVerticalScroll()
307 self:Update()
308 end
309  
310 function AceGUIListBox:OnMouseWheel()
311 self.ScrollBox:OnMouseWheel()
312 end
313  
314  
315 --[[--------------------------------------------------------------------------------
316 Values
317 -----------------------------------------------------------------------------------]]
318  
319 function AceGUIListBox:GetListText(r, c)
320 if( not r ) then return end
321 if( type(self.list[r]) == "table" ) then
322 return (self.list[r][c or 0] or self.list[r]).text or
323 (self.list[r][c or 0] or self.list[r]).val
324 else
325 return self.list[r]
326 end
327 end
328  
329 function AceGUIListBox:GetListVal(r, c)
330 if( not r ) then return end
331 if( type(self.list[r]) == "table" ) then
332 return (self.list[r][c or 0] or self.list[r]).val
333 else
334 return self.list[r]
335 end
336 end
337  
338 function AceGUIListBox:GetListTip(r, c)
339 if( not r ) then return end
340 if( type(self.list[r]) == "table" ) then
341 return (self.list[r][c or 0] or self.list[r]).tip
342 end
343 end
344  
345 function AceGUIListBox:GetValue()
346 return self.selectedValue
347 end
348  
349 function AceGUIListBox:SetValue(val)
350 self.selectedRowID = nil
351 self.selectedColID = nil
352 self.selectedValue = nil
353  
354 if( not val ) then return end
355  
356 for r = 1, getn(self.list) do
357 if( type(self.list[r]) == "table" ) then
358 for c = 1, getn(self.list[r]) do
359 if( self.GetListVal(r, c) == val ) then
360 self:SetSelected(r, c)
361 return
362 end
363 end
364 else
365 if( self.GetListVal(r) == val ) then
366 self:SetSelected(r, c)
367 return
368 end
369 end
370 end
371 end
372  
373 function AceGUIListBox:GetSelected()
374 return self.selectedRowID, self.selectedColID, self.selectedValue
375 end
376  
377 function AceGUIListBox:SetSelected(r, c)
378 if( (r == self.selectedRowID) and (c == self.selectedColID) ) then return end
379  
380 self.selectedRowID = r
381 self.selectedColID = c
382 self.selectedValue = self:GetListVal(r, c)
383 end
384  
385 function AceGUIListBox:Clear()
386 self:SetValue()
387 end
388  
389 function AceGUIListBox:ClearList()
390 self:SetValue()
391 self.ScrollBox:Clear()
392 self.list = nil
393 end
394  
395  
396 --[[--------------------------------------------------------------------------------
397 Events
398 -----------------------------------------------------------------------------------]]
399  
400 function AceGUIListBox:OnShow()
401 self:Update()
402 end
403  
404 function AceGUIListBox:OnItemClick()
405 self:CloseDropDowns()
406  
407 if( self._lockedRow ) then
408 self._lockedRow:UnlockHighlight()
409 if( self._lockedRow ~= self.Rows[this.rowIndex] ) then
410 self._lockedRow.Highlight:Hide()
411 end
412 self._lockedRow = nil
413 end
414  
415 if( self.selectedRowID ~= this.rowID ) then
416 self:SetSelected(this.rowID, this.colID)
417 self.Rows[this.rowIndex].Highlight:Show()
418 self.Rows[this.rowIndex]:LockHighlight()
419 self._lockedRow = self.Rows[this.rowIndex]
420 else
421 self:SetSelected()
422 end
423  
424 self:CallHandler("OnSelect")
425 end
426  
427 function AceGUIListBox:OnItemEnter()
428 local tip = self:GetListTip(this.rowID, this.colID)
429 self.Rows[this.rowIndex].Highlight:Show()
430 if( tip ) then self:ShowTooltip(self.Rows[this.rowIndex], tip) end
431 end
432  
433 function AceGUIListBox:OnItemLeave()
434 if( self._lockedRow ~= self.Rows[this.rowIndex] ) then
435 self.Rows[this.rowIndex]:UnlockHighlight()
436 self.Rows[this.rowIndex].Highlight:Hide()
437 end
438 if( GameTooltip:IsVisible() ) then GameTooltip:Hide() end
439 end