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