29b33e2 - Create slash command to load config addon
[wowui.git] / RaidFrameCustomizationConfig / libs / AceGUI-3.0-SharedMediaWidgets / StatusbarWidget.lua
1 -- Widget is based on the AceGUIWidget-DropDown.lua supplied with AceGUI-3.0
2 -- Widget created by Yssaril
3
4 local AceGUI = LibStub("AceGUI-3.0")
5 local Media = LibStub("LibSharedMedia-3.0")
6
7 local AGSMW = LibStub("AceGUISharedMediaWidgets-1.0")
8
9 do
10         local widgetType = "LSM30_Statusbar"
11         local widgetVersion = 11
12
13         local contentFrameCache = {}
14         local function ReturnSelf(self)
15                 self:ClearAllPoints()
16                 self:Hide()
17                 self.check:Hide()
18                 table.insert(contentFrameCache, self)
19         end
20
21         local function ContentOnClick(this, button)
22                 local self = this.obj
23                 self:Fire("OnValueChanged", this.text:GetText())
24                 if self.dropdown then
25                         self.dropdown = AGSMW:ReturnDropDownFrame(self.dropdown)
26                 end
27         end
28
29         local function GetContentLine()
30                 local frame
31                 if next(contentFrameCache) then
32                         frame = table.remove(contentFrameCache)
33                 else
34                         frame = CreateFrame("Button", nil, UIParent)
35                                 --frame:SetWidth(200)
36                                 frame:SetHeight(18)
37                                 frame:SetHighlightTexture([[Interface\QuestFrame\UI-QuestTitleHighlight]], "ADD")
38                                 frame:SetScript("OnClick", ContentOnClick)
39                         local check = frame:CreateTexture("OVERLAY")
40                                 check:SetWidth(16)
41                                 check:SetHeight(16)
42                                 check:SetPoint("LEFT",frame,"LEFT",1,-1)
43                                 check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check")
44                                 check:Hide()
45                         frame.check = check
46                         local bar = frame:CreateTexture("ARTWORK")
47                                 bar:SetHeight(16)
48                                 bar:SetPoint("LEFT",check,"RIGHT",1,0)
49                                 bar:SetPoint("RIGHT",frame,"RIGHT",-1,0)
50                         frame.bar = bar
51                         local text = frame:CreateFontString(nil,"OVERLAY","GameFontWhite")
52
53                                 local font, size = text:GetFont()
54                                 text:SetFont(font,size,"OUTLINE")
55
56                                 text:SetPoint("TOPLEFT", check, "TOPRIGHT", 3, 0)
57                                 text:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -2, 0)
58                                 text:SetJustifyH("LEFT")
59                                 text:SetText("Test Test Test Test Test Test Test")
60                         frame.text = text
61                         frame.ReturnSelf = ReturnSelf
62                 end
63                 frame:Show()
64                 return frame
65         end
66
67         local function OnAcquire(self)
68                 self:SetHeight(44)
69                 self:SetWidth(200)
70         end
71
72         local function OnRelease(self)
73                 self:SetText("")
74                 self:SetLabel("")
75                 self:SetDisabled(false)
76
77                 self.value = nil
78                 self.list = nil
79                 self.open = nil
80                 self.hasClose = nil
81
82                 self.frame:ClearAllPoints()
83                 self.frame:Hide()
84         end
85
86         local function SetValue(self, value) -- Set the value to an item in the List.
87                 if self.list then
88                         self:SetText(value or "")
89                 end
90                 self.value = value
91         end
92
93         local function GetValue(self)
94                 return self.value
95         end
96
97         local function SetList(self, list) -- Set the list of values for the dropdown (key => value pairs)
98                 self.list = list or Media:HashTable("statusbar")
99         end
100
101
102         local function SetText(self, text) -- Set the text displayed in the box.
103                 self.frame.text:SetText(text or "")
104                 local statusbar = self.list[text] ~= text and self.list[text] or Media:Fetch('statusbar',text)
105                 self.bar:SetTexture(statusbar)
106         end
107
108         local function SetLabel(self, text) -- Set the text for the label.
109                 self.frame.label:SetText(text or "")
110         end
111
112         local function AddItem(self, key, value) -- Add an item to the list.
113                 self.list = self.list or {}
114                 self.list[key] = value
115         end
116         local SetItemValue = AddItem -- Set the value of a item in the list. <<same as adding a new item>>
117
118         local function SetMultiselect(self, flag) end -- Toggle multi-selecting. <<Dummy function to stay inline with the dropdown API>>
119         local function GetMultiselect() return false end-- Query the multi-select flag. <<Dummy function to stay inline with the dropdown API>>
120         local function SetItemDisabled(self, key) end-- Disable one item in the list. <<Dummy function to stay inline with the dropdown API>>
121
122         local function SetDisabled(self, disabled) -- Disable the widget.
123                 self.disabled = disabled
124                 if disabled then
125                         self.frame:Disable()
126                 else
127                         self.frame:Enable()
128                 end
129         end
130
131         local function textSort(a,b)
132                 return string.upper(a) < string.upper(b)
133         end
134
135         local sortedlist = {}
136         local function ToggleDrop(this)
137                 local self = this.obj
138                 if self.dropdown then
139                         self.dropdown = AGSMW:ReturnDropDownFrame(self.dropdown)
140                         AceGUI:ClearFocus()
141                 else
142                         AceGUI:SetFocus(self)
143                         self.dropdown = AGSMW:GetDropDownFrame()
144                         local width = self.frame:GetWidth()
145                         self.dropdown:SetPoint("TOPLEFT", self.frame, "BOTTOMLEFT")
146                         self.dropdown:SetPoint("TOPRIGHT", self.frame, "BOTTOMRIGHT", width < 160 and (160 - width) or 0, 0)
147                         for k, v in pairs(self.list) do
148                                 sortedlist[#sortedlist+1] = k
149                         end
150                         table.sort(sortedlist, textSort)
151                         for i, k in ipairs(sortedlist) do
152                                 local f = GetContentLine()
153                                 f.text:SetText(k)
154                                 --print(k)
155                                 if k == self.value then
156                                         f.check:Show()
157                                 end
158
159                                 local statusbar = self.list[k] ~= k and self.list[k] or Media:Fetch('statusbar',k)
160                                 f.bar:SetTexture(statusbar)
161                                 f.obj = self
162                                 f.dropdown = self.dropdown
163                                 self.dropdown:AddFrame(f)
164                         end
165                         wipe(sortedlist)
166                 end
167         end
168
169         local function ClearFocus(self)
170                 if self.dropdown then
171                         self.dropdown = AGSMW:ReturnDropDownFrame(self.dropdown)
172                 end
173         end
174
175         local function OnHide(this)
176                 local self = this.obj
177                 if self.dropdown then
178                         self.dropdown = AGSMW:ReturnDropDownFrame(self.dropdown)
179                 end
180         end
181
182         local function Drop_OnEnter(this)
183                 this.obj:Fire("OnEnter")
184         end
185
186         local function Drop_OnLeave(this)
187                 this.obj:Fire("OnLeave")
188         end
189
190         local function Constructor()
191                 local frame = AGSMW:GetBaseFrame()
192                 local self = {}
193
194                 self.type = widgetType
195                 self.frame = frame
196                 frame.obj = self
197                 frame.dropButton.obj = self
198                 frame.dropButton:SetScript("OnEnter", Drop_OnEnter)
199                 frame.dropButton:SetScript("OnLeave", Drop_OnLeave)
200                 frame.dropButton:SetScript("OnClick",ToggleDrop)
201                 frame:SetScript("OnHide", OnHide)
202
203                 local bar = frame:CreateTexture(nil, "OVERLAY")
204                         bar:SetPoint("TOPLEFT", frame,"TOPLEFT",6,-25)
205                         bar:SetPoint("BOTTOMRIGHT", frame,"BOTTOMRIGHT", -21, 5)
206                         bar:SetAlpha(0.5)
207                 self.bar = bar
208
209                 self.alignoffset = 31
210
211                 self.OnRelease = OnRelease
212                 self.OnAcquire = OnAcquire
213                 self.ClearFocus = ClearFocus
214                 self.SetText = SetText
215                 self.SetValue = SetValue
216                 self.GetValue = GetValue
217                 self.SetList = SetList
218                 self.SetLabel = SetLabel
219                 self.SetDisabled = SetDisabled
220                 self.AddItem = AddItem
221                 self.SetMultiselect = SetMultiselect
222                 self.GetMultiselect = GetMultiselect
223                 self.SetItemValue = SetItemValue
224                 self.SetItemDisabled = SetItemDisabled
225                 self.ToggleDrop = ToggleDrop
226
227                 AceGUI:RegisterAsWidget(self)
228                 return self
229         end
230
231         AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion)
232
233 end