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