6d5df66 - Update to current
[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 = 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 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("LEFT", check, "RIGHT", 1, 0)
48                                 text:SetPoint("RIGHT", frame, "RIGHT", -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                         self.dropdown:SetPoint("TOPLEFT", self.frame, "BOTTOMLEFT")
136                         for k, v in pairs(self.list) do
137                                 sortedlist[#sortedlist+1] = k
138                         end
139                         table.sort(sortedlist, textSort)
140                         for i, k in ipairs(sortedlist) do
141                                 local f = GetContentLine()
142                                 local _, size, outline= f.text:GetFont()
143                                 local font = self.list[k] ~= k and self.list[k] or Media:Fetch('font',k)
144                                 f.text:SetFont(font,size,outline)
145                                 f.text:SetText(k)
146                                 if k == self.value then
147                                         f.check:Show()
148                                 end
149                                 f.obj = self
150                                 self.dropdown:AddFrame(f)
151                         end
152                         wipe(sortedlist)
153                 end
154         end
155
156         local function ClearFocus(self)
157                 if self.dropdown then
158                         self.dropdown = AGSMW:ReturnDropDownFrame(self.dropdown)
159                 end
160         end
161
162         local function OnHide(this)
163                 local self = this.obj
164                 if self.dropdown then
165                         self.dropdown = AGSMW:ReturnDropDownFrame(self.dropdown)
166                 end
167         end
168
169         local function Drop_OnEnter(this)
170                 this.obj:Fire("OnEnter")
171         end
172
173         local function Drop_OnLeave(this)
174                 this.obj:Fire("OnLeave")
175         end
176
177         local function Constructor()
178                 local frame = AGSMW:GetBaseFrame()
179                 local self = {}
180
181                 self.type = widgetType
182                 self.frame = frame
183                 frame.obj = self
184                 frame.dropButton.obj = self
185                 frame.dropButton:SetScript("OnEnter", Drop_OnEnter)
186                 frame.dropButton:SetScript("OnLeave", Drop_OnLeave)
187                 frame.dropButton:SetScript("OnClick",ToggleDrop)
188                 frame:SetScript("OnHide", OnHide)
189
190                 self.alignoffset = 31
191
192                 self.OnRelease = OnRelease
193                 self.OnAcquire = OnAcquire
194                 self.ClearFocus = ClearFocus
195                 self.SetText = SetText
196                 self.SetValue = SetValue
197                 self.GetValue = GetValue
198                 self.SetList = SetList
199                 self.SetLabel = SetLabel
200                 self.SetDisabled = SetDisabled
201                 self.AddItem = AddItem
202                 self.SetMultiselect = SetMultiselect
203                 self.GetMultiselect = GetMultiselect
204                 self.SetItemValue = SetItemValue
205                 self.SetItemDisabled = SetItemDisabled
206                 self.ToggleDrop = ToggleDrop
207
208                 AceGUI:RegisterAsWidget(self)
209                 return self
210         end
211
212         AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion)
213
214 end