1553757 - Refactor updateIndicators
[wowui.git] / libs / AceGUI-3.0-SharedMediaWidgets / Libs / AceGUI-3.0 / widgets / AceGUIWidget-DropDown-Items.lua
1 --[[ $Id: AceGUIWidget-DropDown-Items.lua 996 2010-12-01 18:34:17Z nevcairiel $ ]]--
2
3 local AceGUI = LibStub("AceGUI-3.0")
4
5 -- Lua APIs
6 local select, assert = select, assert
7
8 -- WoW APIs
9 local PlaySound = PlaySound
10 local CreateFrame = CreateFrame
11
12 local function fixlevels(parent,...)
13         local i = 1
14         local child = select(i, ...)
15         while child do
16                 child:SetFrameLevel(parent:GetFrameLevel()+1)
17                 fixlevels(child, child:GetChildren())
18                 i = i + 1
19                 child = select(i, ...)
20         end
21 end
22
23 local function fixstrata(strata, parent, ...)
24         local i = 1
25         local child = select(i, ...)
26         parent:SetFrameStrata(strata)
27         while child do
28                 fixstrata(strata, child, child:GetChildren())
29                 i = i + 1
30                 child = select(i, ...)
31         end
32 end
33
34 -- ItemBase is the base "class" for all dropdown items.
35 -- Each item has to use ItemBase.Create(widgetType) to
36 -- create an initial 'self' value. 
37 -- ItemBase will add common functions and ui event handlers.
38 -- Be sure to keep basic usage when you override functions.
39
40 local ItemBase = {
41         -- NOTE: The ItemBase version is added to each item's version number
42         --       to ensure proper updates on ItemBase changes.
43         --       Use at least 1000er steps.
44         version = 1000,
45         counter = 0,
46 }
47
48 function ItemBase.Frame_OnEnter(this)
49         local self = this.obj
50
51         if self.useHighlight then
52                 self.highlight:Show()
53         end
54         self:Fire("OnEnter")
55         
56         if self.specialOnEnter then
57                 self.specialOnEnter(self)
58         end
59 end
60
61 function ItemBase.Frame_OnLeave(this)
62         local self = this.obj
63         
64         self.highlight:Hide()
65         self:Fire("OnLeave")
66         
67         if self.specialOnLeave then
68                 self.specialOnLeave(self)
69         end
70 end
71
72 -- exported, AceGUI callback
73 function ItemBase.OnAcquire(self)
74         self.frame:SetToplevel(true)
75         self.frame:SetFrameStrata("FULLSCREEN_DIALOG")
76 end
77
78 -- exported, AceGUI callback
79 function ItemBase.OnRelease(self)
80         self:SetDisabled(false)
81         self.pullout = nil
82         self.frame:SetParent(nil)
83         self.frame:ClearAllPoints()
84         self.frame:Hide()
85 end
86
87 -- exported
88 -- NOTE: this is called by a Dropdown-Pullout.
89 --       Do not call this method directly
90 function ItemBase.SetPullout(self, pullout)
91         self.pullout = pullout
92         
93         self.frame:SetParent(nil)
94         self.frame:SetParent(pullout.itemFrame)
95         self.parent = pullout.itemFrame
96         fixlevels(pullout.itemFrame, pullout.itemFrame:GetChildren())
97 end
98
99 -- exported
100 function ItemBase.SetText(self, text)
101         self.text:SetText(text or "")
102 end
103
104 -- exported
105 function ItemBase.GetText(self)
106         return self.text:GetText()
107 end
108
109 -- exported
110 function ItemBase.SetPoint(self, ...)
111         self.frame:SetPoint(...)
112 end
113
114 -- exported
115 function ItemBase.Show(self)
116         self.frame:Show()
117 end
118
119 -- exported
120 function ItemBase.Hide(self)
121         self.frame:Hide()
122 end
123
124 -- exported
125 function ItemBase.SetDisabled(self, disabled)
126         self.disabled = disabled
127         if disabled then
128                 self.useHighlight = false
129                 self.text:SetTextColor(.5, .5, .5)
130         else
131                 self.useHighlight = true
132                 self.text:SetTextColor(1, 1, 1)
133         end
134 end
135
136 -- exported
137 -- NOTE: this is called by a Dropdown-Pullout.
138 --       Do not call this method directly
139 function ItemBase.SetOnLeave(self, func)
140         self.specialOnLeave = func
141 end
142
143 -- exported
144 -- NOTE: this is called by a Dropdown-Pullout.
145 --       Do not call this method directly
146 function ItemBase.SetOnEnter(self, func)
147         self.specialOnEnter = func
148 end
149
150 function ItemBase.Create(type)
151         -- NOTE: Most of the following code is copied from AceGUI-3.0/Dropdown widget
152         local count = AceGUI:GetNextWidgetNum(type)
153         local frame = CreateFrame("Button", "AceGUI30DropDownItem"..count)
154         local self = {}
155         self.frame = frame
156         frame.obj = self
157         self.type = type
158         
159         self.useHighlight = true
160         
161         frame:SetHeight(17)
162         frame:SetFrameStrata("FULLSCREEN_DIALOG")
163         
164         local text = frame:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
165         text:SetTextColor(1,1,1)
166         text:SetJustifyH("LEFT")
167         text:SetPoint("TOPLEFT",frame,"TOPLEFT",18,0)
168         text:SetPoint("BOTTOMRIGHT",frame,"BOTTOMRIGHT",-8,0)
169         self.text = text
170
171         local highlight = frame:CreateTexture(nil, "OVERLAY")
172         highlight:SetTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight")
173         highlight:SetBlendMode("ADD")
174         highlight:SetHeight(14)
175         highlight:ClearAllPoints()
176         highlight:SetPoint("RIGHT",frame,"RIGHT",-3,0)
177         highlight:SetPoint("LEFT",frame,"LEFT",5,0)
178         highlight:Hide()
179         self.highlight = highlight
180
181         local check = frame:CreateTexture("OVERLAY")    
182         check:SetWidth(16)
183         check:SetHeight(16)
184         check:SetPoint("LEFT",frame,"LEFT",3,-1)
185         check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check")
186         check:Hide()
187         self.check = check
188
189         local sub = frame:CreateTexture("OVERLAY")
190         sub:SetWidth(16)
191         sub:SetHeight(16)
192         sub:SetPoint("RIGHT",frame,"RIGHT",-3,-1)
193         sub:SetTexture("Interface\\ChatFrame\\ChatFrameExpandArrow")
194         sub:Hide()
195         self.sub = sub  
196         
197         frame:SetScript("OnEnter", ItemBase.Frame_OnEnter)
198         frame:SetScript("OnLeave", ItemBase.Frame_OnLeave)
199         
200         self.OnAcquire = ItemBase.OnAcquire
201         self.OnRelease = ItemBase.OnRelease
202         
203         self.SetPullout = ItemBase.SetPullout
204         self.GetText    = ItemBase.GetText
205         self.SetText    = ItemBase.SetText
206         self.SetDisabled = ItemBase.SetDisabled
207         
208         self.SetPoint   = ItemBase.SetPoint
209         self.Show       = ItemBase.Show
210         self.Hide       = ItemBase.Hide
211         
212         self.SetOnLeave = ItemBase.SetOnLeave
213         self.SetOnEnter = ItemBase.SetOnEnter
214         
215         return self
216 end
217
218 -- Register a dummy LibStub library to retrieve the ItemBase, so other addons can use it.
219 local IBLib = LibStub:NewLibrary("AceGUI-3.0-DropDown-ItemBase", ItemBase.version)
220 if IBLib then
221         IBLib.GetItemBase = function() return ItemBase end
222 end
223
224 --[[
225         Template for items:
226         
227 -- Item:
228 --
229 do
230         local widgetType = "Dropdown-Item-"
231         local widgetVersion = 1
232         
233         local function Constructor()
234                 local self = ItemBase.Create(widgetType)
235                 
236                 AceGUI:RegisterAsWidget(self)
237                 return self
238         end
239         
240         AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version)
241 end
242 --]]
243
244 -- Item: Header
245 -- A single text entry.
246 -- Special: Different text color and no highlight
247 do
248         local widgetType = "Dropdown-Item-Header"
249         local widgetVersion = 1
250         
251         local function OnEnter(this)
252                 local self = this.obj
253                 self:Fire("OnEnter")
254                 
255                 if self.specialOnEnter then
256                         self.specialOnEnter(self)
257                 end
258         end
259         
260         local function OnLeave(this)
261                 local self = this.obj
262                 self:Fire("OnLeave")
263                 
264                 if self.specialOnLeave then
265                         self.specialOnLeave(self)
266                 end
267         end
268         
269         -- exported, override
270         local function SetDisabled(self, disabled)
271                 ItemBase.SetDisabled(self, disabled)
272                 if not disabled then
273                         self.text:SetTextColor(1, 1, 0)
274                 end
275         end
276         
277         local function Constructor()
278                 local self = ItemBase.Create(widgetType)
279                 
280                 self.SetDisabled = SetDisabled
281                 
282                 self.frame:SetScript("OnEnter", OnEnter)
283                 self.frame:SetScript("OnLeave", OnLeave)
284                 
285                 self.text:SetTextColor(1, 1, 0)
286                 
287                 AceGUI:RegisterAsWidget(self)
288                 return self
289         end
290         
291         AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version)
292 end
293
294 -- Item: Execute
295 -- A simple button
296 do
297         local widgetType = "Dropdown-Item-Execute"
298         local widgetVersion = 1
299         
300         local function Frame_OnClick(this, button)
301                 local self = this.obj
302                 if self.disabled then return end
303                 self:Fire("OnClick")
304                 if self.pullout then
305                         self.pullout:Close()
306                 end
307         end
308         
309         local function Constructor()
310                 local self = ItemBase.Create(widgetType)
311                 
312                 self.frame:SetScript("OnClick", Frame_OnClick)
313                 
314                 AceGUI:RegisterAsWidget(self)
315                 return self
316         end
317         
318         AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version)
319 end
320
321 -- Item: Toggle
322 -- Some sort of checkbox for dropdown menus.
323 -- Does not close the pullout on click.
324 do
325         local widgetType = "Dropdown-Item-Toggle"
326         local widgetVersion = 3
327         
328         local function UpdateToggle(self)
329                 if self.value then
330                         self.check:Show()
331                 else
332                         self.check:Hide()
333                 end
334         end
335         
336         local function OnRelease(self)
337                 ItemBase.OnRelease(self)
338                 self:SetValue(nil)
339         end
340         
341         local function Frame_OnClick(this, button)
342                 local self = this.obj
343                 if self.disabled then return end
344                 self.value = not self.value
345                 if self.value then
346                         PlaySound("igMainMenuOptionCheckBoxOn")
347                 else
348                         PlaySound("igMainMenuOptionCheckBoxOff")
349                 end
350                 UpdateToggle(self)
351                 self:Fire("OnValueChanged", self.value)
352         end
353         
354         -- exported
355         local function SetValue(self, value)
356                 self.value = value
357                 UpdateToggle(self)
358         end
359         
360         -- exported
361         local function GetValue(self)
362                 return self.value
363         end
364         
365         local function Constructor()
366                 local self = ItemBase.Create(widgetType)
367                 
368                 self.frame:SetScript("OnClick", Frame_OnClick)
369                 
370                 self.SetValue = SetValue
371                 self.GetValue = GetValue
372                 self.OnRelease = OnRelease
373                 
374                 AceGUI:RegisterAsWidget(self)
375                 return self
376         end
377         
378         AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version)
379 end
380
381 -- Item: Menu
382 -- Shows a submenu on mouse over
383 -- Does not close the pullout on click
384 do
385         local widgetType = "Dropdown-Item-Menu"
386         local widgetVersion = 2
387         
388         local function OnEnter(this)
389                 local self = this.obj
390                 self:Fire("OnEnter")
391                 
392                 if self.specialOnEnter then
393                         self.specialOnEnter(self)
394                 end
395                 
396                 self.highlight:Show()
397                 
398                 if not self.disabled and self.submenu then
399                         self.submenu:Open("TOPLEFT", self.frame, "TOPRIGHT", self.pullout:GetRightBorderWidth(), 0, self.frame:GetFrameLevel() + 100)
400                 end
401         end
402         
403         local function OnHide(this)
404                 local self = this.obj
405                 if self.submenu then
406                         self.submenu:Close()
407                 end
408         end
409         
410         -- exported
411         local function SetMenu(self, menu)
412                 assert(menu.type == "Dropdown-Pullout")
413                 self.submenu = menu
414         end
415                 
416         -- exported
417         local function CloseMenu(self)
418                 self.submenu:Close()
419         end
420                 
421         local function Constructor()
422                 local self = ItemBase.Create(widgetType)
423                 
424                 self.sub:Show()
425                 
426                 self.frame:SetScript("OnEnter", OnEnter)
427                 self.frame:SetScript("OnHide", OnHide)
428                 
429                 self.SetMenu   = SetMenu
430                 self.CloseMenu = CloseMenu
431                 
432                 AceGUI:RegisterAsWidget(self)
433                 return self
434         end
435         
436         AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version)
437 end
438
439 -- Item: Separator
440 -- A single line to separate items
441 do
442         local widgetType = "Dropdown-Item-Separator"
443         local widgetVersion = 1
444         
445         -- exported, override
446         local function SetDisabled(self, disabled)
447                 ItemBase.SetDisabled(self, disabled)
448                 self.useHighlight = false
449         end
450                 
451         local function Constructor()
452                 local self = ItemBase.Create(widgetType)
453                 
454                 self.SetDisabled = SetDisabled
455                 
456                 local line = self.frame:CreateTexture(nil, "OVERLAY")
457                 line:SetHeight(1)
458                 line:SetTexture(.5, .5, .5)
459                 line:SetPoint("LEFT", self.frame, "LEFT", 10, 0)
460                 line:SetPoint("RIGHT", self.frame, "RIGHT", -10, 0)
461                 
462                 self.text:Hide()
463                 
464                 self.useHighlight = false
465                 
466                 AceGUI:RegisterAsWidget(self)
467                 return self
468         end
469         
470         AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version)
471 end