rearrange stuff, import config
[QuestHelper.git] / libs / AceGUI-3.0 / widgets / AceGUIWidget-CheckBox.lua
blob98cb1d93dcf46edfbc6df40ffdd24ff96bae6c1c
1 local AceGUI = LibStub("AceGUI-3.0")
3 -- Lua APIs
4 local select = select
6 -- WoW APIs
7 local CreateFrame, UIParent = CreateFrame, UIParent
9 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
10 -- List them here for Mikk's FindGlobals script
11 -- GLOBALS: SetDesaturation, GameFontHighlight
13 --------------------------
14 -- Check Box --
15 --------------------------
16 --[[
17 Events :
18 OnValueChanged
22 local Type = "CheckBox"
23 local Version = 14
25 local function OnAcquire(self)
26 self:SetValue(false)
27 self.tristate = nil
28 self:SetHeight(24)
29 self:SetWidth(200)
30 self:SetImage()
31 end
33 local function OnRelease(self)
34 self.frame:ClearAllPoints()
35 self.frame:Hide()
36 self.check:Hide()
37 self.highlight:Hide()
38 self.down = nil
39 self.checked = nil
40 self:SetType()
41 self:SetDisabled(false)
42 self:SetDescription(nil)
43 end
45 local function CheckBox_OnEnter(this)
46 local self = this.obj
47 self.highlight:Show()
48 self:Fire("OnEnter")
49 end
51 local function CheckBox_OnLeave(this)
52 local self = this.obj
53 self.highlight:Hide()
54 self:Fire("OnLeave")
55 end
57 local function CheckBox_OnMouseUp(this)
58 local self = this.obj
59 if not self.disabled then
60 self:ToggleChecked()
61 self:Fire("OnValueChanged",self.checked)
62 self.text:SetPoint("LEFT",self.check,"RIGHT",0,0)
63 end
64 self.down = nil
65 end
67 local function CheckBox_OnMouseDown(this)
68 local self = this.obj
69 if not self.disabled then
70 self.text:SetPoint("LEFT",self.check,"RIGHT",1,-1)
71 self.down = true
72 end
73 AceGUI:ClearFocus()
74 end
76 local function SetDisabled(self,disabled)
77 self.disabled = disabled
78 if disabled then
79 self.frame:Disable()
80 self.text:SetTextColor(0.5,0.5,0.5)
81 SetDesaturation(self.check, true)
82 else
83 self.frame:Enable()
84 self.text:SetTextColor(1,1,1)
85 if self.tristate and self.checked == nil then
86 SetDesaturation(self.check, true)
87 else
88 SetDesaturation(self.check, false)
89 end
90 end
91 end
93 local function SetValue(self,value)
94 local check = self.check
95 self.checked = value
96 if value then
97 SetDesaturation(self.check, false)
98 self.check:Show()
99 else
100 --Nil is the unknown tristate value
101 if self.tristate and value == nil then
102 SetDesaturation(self.check, true)
103 self.check:Show()
104 else
105 SetDesaturation(self.check, false)
106 self.check:Hide()
109 SetDisabled(self, self.disabled)
112 local function SetTriState(self, enabled)
113 self.tristate = enabled
114 self:SetValue(self:GetValue())
117 local function GetValue(self)
118 return self.checked
121 local function SetType(self, type)
122 local checkbg = self.checkbg
123 local check = self.check
124 local highlight = self.highlight
126 if type == "radio" then
127 checkbg:SetHeight(16)
128 checkbg:SetWidth(16)
129 checkbg:SetTexture("Interface\\Buttons\\UI-RadioButton")
130 checkbg:SetTexCoord(0,0.25,0,1)
131 check:SetHeight(16)
132 check:SetWidth(16)
133 check:SetTexture("Interface\\Buttons\\UI-RadioButton")
134 check:SetTexCoord(0.25,0.5,0,1)
135 check:SetBlendMode("ADD")
136 highlight:SetTexture("Interface\\Buttons\\UI-RadioButton")
137 highlight:SetTexCoord(0.5,0.75,0,1)
138 else
139 checkbg:SetHeight(24)
140 checkbg:SetWidth(24)
141 checkbg:SetTexture("Interface\\Buttons\\UI-CheckBox-Up")
142 checkbg:SetTexCoord(0,1,0,1)
143 check:SetHeight(24)
144 check:SetWidth(24)
145 check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check")
146 check:SetTexCoord(0,1,0,1)
147 check:SetBlendMode("BLEND")
148 highlight:SetTexture("Interface\\Buttons\\UI-CheckBox-Highlight")
149 highlight:SetTexCoord(0,1,0,1)
153 local function ToggleChecked(self)
154 local value = self:GetValue()
155 if self.tristate then
156 --cycle in true, nil, false order
157 if value then
158 self:SetValue(nil)
159 elseif value == nil then
160 self:SetValue(false)
161 else
162 self:SetValue(true)
164 else
165 self:SetValue(not self:GetValue())
169 local function SetLabel(self, label)
170 self.text:SetText(label)
173 local function SetDescription(self, desc)
174 if desc then
175 if not self.desc then
176 local desc = self.frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
177 desc:ClearAllPoints()
178 desc:SetPoint("TOPLEFT", self.check, "TOPRIGHT", 5, -21)
179 desc:SetWidth(self.frame.width - 30)
180 desc:SetJustifyH("LEFT")
181 desc:SetJustifyV("TOP")
182 self.desc = desc
184 self.desc:Show()
185 --self.text:SetFontObject(GameFontNormal)
186 self.desc:SetText(desc)
187 self:SetHeight(28 + self.desc:GetHeight())
188 else
189 if self.desc then
190 self.desc:SetText("")
191 self.desc:Hide()
193 self.text:SetFontObject(GameFontHighlight)
194 self:SetHeight(24)
198 local function SetImage(self, path, ...)
199 local image = self.image
200 image:SetTexture(path)
202 if image:GetTexture() then
203 local n = select('#', ...)
204 if n == 4 or n == 8 then
205 image:SetTexCoord(...)
206 else
207 image:SetTexCoord(0, 1, 0, 1)
210 self:AlignImage()
213 local function AlignImage(self)
214 local img = self.image:GetTexture()
215 self.text:ClearAllPoints()
216 if not img then
217 self.text:SetPoint("LEFT", self.check, "RIGHT", 0, 0)
218 self.text:SetPoint("RIGHT", self.frame, "RIGHT", 0, 0)
219 else
220 self.text:SetPoint("LEFT", self.image,"RIGHT", 1, 0)
221 self.text:SetPoint("RIGHT", self.frame,"RIGHT", 0, 0)
225 local function OnWidthSet(self, width)
226 if self.desc and self.desc:GetText() ~= "" then
227 self.desc:SetWidth(width - 30)
228 self:SetHeight(28 + self.desc:GetHeight())
232 local function Constructor()
233 local frame = CreateFrame("Button",nil,UIParent)
234 local self = {}
235 self.type = Type
237 self.OnRelease = OnRelease
238 self.OnAcquire = OnAcquire
240 self.SetValue = SetValue
241 self.GetValue = GetValue
242 self.SetDisabled = SetDisabled
243 self.SetType = SetType
244 self.ToggleChecked = ToggleChecked
245 self.SetLabel = SetLabel
246 self.SetTriState = SetTriState
247 self.SetDescription = SetDescription
248 self.OnWidthSet = OnWidthSet
249 self.SetImage = SetImage
250 self.AlignImage = AlignImage
252 self.frame = frame
253 frame.obj = self
255 local text = frame:CreateFontString(nil,"OVERLAY","GameFontHighlight")
256 self.text = text
258 frame:SetScript("OnEnter",CheckBox_OnEnter)
259 frame:SetScript("OnLeave",CheckBox_OnLeave)
260 frame:SetScript("OnMouseUp",CheckBox_OnMouseUp)
261 frame:SetScript("OnMouseDown",CheckBox_OnMouseDown)
262 frame:EnableMouse()
263 local checkbg = frame:CreateTexture(nil,"ARTWORK")
264 self.checkbg = checkbg
265 checkbg:SetWidth(24)
266 checkbg:SetHeight(24)
267 checkbg:SetPoint("TOPLEFT",frame,"TOPLEFT",0,0)
268 checkbg:SetTexture("Interface\\Buttons\\UI-CheckBox-Up")
269 local check = frame:CreateTexture(nil,"OVERLAY")
270 self.check = check
271 check:SetWidth(24)
272 check:SetHeight(24)
273 check:SetPoint("CENTER",checkbg,"CENTER",0,0)
274 check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check")
276 local highlight = frame:CreateTexture(nil, "OVERLAY")
277 self.highlight = highlight
278 highlight:SetTexture("Interface\\Buttons\\UI-CheckBox-Highlight")
279 highlight:SetBlendMode("ADD")
280 highlight:SetAllPoints(checkbg)
281 highlight:Hide()
283 local image = frame:CreateTexture(nil, "OVERLAY")
284 self.image = image
285 image:SetHeight(16)
286 image:SetWidth(16)
287 image:SetPoint("LEFT", check, "RIGHT", 1, 0)
289 text:SetJustifyH("LEFT")
290 frame:SetHeight(24)
291 frame:SetWidth(200)
292 text:SetHeight(18)
293 text:SetPoint("LEFT",check,"RIGHT",0,0)
294 text:SetPoint("RIGHT",frame,"RIGHT",0,0)
296 AceGUI:RegisterAsWidget(self)
297 return self
300 AceGUI:RegisterWidgetType(Type,Constructor,Version)