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