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