local p,K="\067\104\101\099\107\066\111\120",26 local a=LibStub and LibStub("\065\099\101\071\085\073-\051\046\048",true)if not a or(a:GetWidgetVersion(p)or 0)>=K then return end local w,R=select,pairs local H=PlaySound local C,c=CreateFrame,UIParent local function f(p)local K=p.image:GetTexture()p.text:ClearAllPoints()if not K then p.text:SetPoint("\076\069\070\084",p.checkbg,"\082\073\071\072\084")p.text:SetPoint("\082\073\071\072\084")else p.text:SetPoint("\076\069\070\084",p.image,"\082\073\071\072\084",1,0)p.text:SetPoint("\082\073\071\072\084")end end local function U(p)p.obj:Fire("\079\110\069\110\116\101\114")end local function W(p)p.obj:Fire("\079\110\076\101\097\118\101")end local function G(p)local K=p.obj if not K.disabled then if K.image:GetTexture()then K.text:SetPoint("\076\069\070\084",K.image,"\082\073\071\072\084",2,-1)else K.text:SetPoint("\076\069\070\084",K.checkbg,"\082\073\071\072\084",1,-1)end end a:ClearFocus()end local function M(p)local K=p.obj if not K.disabled then K:ToggleChecked()if K.checked then H(856)else H(857)end K:Fire("\079\110\086\097\108\117\101\067\104\097\110\103\101\100",K.checked)f(K)end end local b={OnAcquire=function(p)p:SetType()p:SetValue(false)p:SetTriState(nil)p:SetWidth(200)p:SetImage()p:SetDisabled(nil)p:SetDescription(nil)end,OnWidthSet=function(p,K)if p.desc then p.desc:SetWidth(K-30)if p.desc:GetText()and p.desc:GetText()~=""then p:SetHeight(28+p.desc:GetStringHeight())end end end,SetDisabled=function(p,K)p.disabled=K if K then p.frame:Disable()p.text:SetTextColor(.5,.5,.5)SetDesaturation(p.check,true)if p.desc then p.desc:SetTextColor(.5,.5,.5)end else p.frame:Enable()p.text:SetTextColor(1,1,1)if p.tristate and p.checked==nil then SetDesaturation(p.check,true)else SetDesaturation(p.check,false)end if p.desc then p.desc:SetTextColor(1,1,1)end end end,SetValue=function(p,K)local a=p.check p.checked=K if K then SetDesaturation(a,false)a:Show()else if p.tristate and K==nil then SetDesaturation(a,true)a:Show()else SetDesaturation(a,false)a:Hide()end end p:SetDisabled(p.disabled)end,GetValue=function(p)return p.checked end;SetTriState=function(p,K)p.tristate=K p:SetValue(p:GetValue())end;SetType=function(p,K)local a=p.checkbg local w=p.check local R=p.highlight local H if K=="\114\097\100\105\111"then H=16 a:SetTexture(130843)a:SetTexCoord(0,.25,0,1)w:SetTexture(130843)w:SetTexCoord(.25,.5,0,1)w:SetBlendMode("\065\068\068")R:SetTexture(130843)R:SetTexCoord(.5,.75,0,1)else H=24 a:SetTexture(130755)a:SetTexCoord(0,1,0,1)w:SetTexture(130751)w:SetTexCoord(0,1,0,1)w:SetBlendMode("\066\076\069\078\068")R:SetTexture(130753)R:SetTexCoord(0,1,0,1)end a:SetHeight(H)a:SetWidth(H)end;ToggleChecked=function(p)local K=p:GetValue()if p.tristate then if K then p:SetValue(nil)elseif K==nil then p:SetValue(false)else p:SetValue(true)end else p:SetValue(not p:GetValue())end end;SetLabel=function(p,K)p.text:SetText(K)end,SetDescription=function(p,K)if K then if not p.desc then local K=p.frame:CreateFontString(nil,"\079\086\069\082\076\065\089","\071\097\109\101\070\111\110\116\072\105\103\104\108\105\103\104\116\083\109\097\108\108")K:ClearAllPoints()K:SetPoint("\084\079\080\076\069\070\084",p.checkbg,"\084\079\080\082\073\071\072\084",5,-21)K:SetWidth(p.frame.width-30)K:SetPoint("\082\073\071\072\084",p.frame,"\082\073\071\072\084",-30,0)K:SetJustifyH("\076\069\070\084")K:SetJustifyV("\084\079\080")p.desc=K end p.desc:Show()p.desc:SetText(K)p:SetHeight(28+p.desc:GetStringHeight())else if p.desc then p.desc:SetText("")p.desc:Hide()end p:SetHeight(24)end end,SetImage=function(p,K,...)local a=p.image a:SetTexture(K)if a:GetTexture()then local p=w("\035",...)if p==4 or p==8 then a:SetTexCoord(...)else a:SetTexCoord(0,1,0,1)end end f(p)end}local function k()local K=C("\066\117\116\116\111\110",nil,c)K:Hide()K:EnableMouse(true)K:SetScript("\079\110\069\110\116\101\114",U)K:SetScript("\079\110\076\101\097\118\101",W)K:SetScript("\079\110\077\111\117\115\101\068\111\119\110",G)K:SetScript("\079\110\077\111\117\115\101\085\112",M)local w=K:CreateTexture(nil,"\065\082\084\087\079\082\075")w:SetWidth(24)w:SetHeight(24)w:SetPoint("\084\079\080\076\069\070\084")w:SetTexture(130755)local H=K:CreateTexture(nil,"\079\086\069\082\076\065\089")H:SetAllPoints(w)H:SetTexture(130751)local f=K:CreateFontString(nil,"\079\086\069\082\076\065\089","\071\097\109\101\070\111\110\116\072\105\103\104\108\105\103\104\116")f:SetJustifyH("\076\069\070\084")f:SetHeight(18)f:SetPoint("\076\069\070\084",w,"\082\073\071\072\084")f:SetPoint("\082\073\071\072\084")local k=K:CreateTexture(nil,"\072\073\071\072\076\073\071\072\084")k:SetTexture(130753)k:SetBlendMode("\065\068\068")k:SetAllPoints(w)local e=K:CreateTexture(nil,"\079\086\069\082\076\065\089")e:SetHeight(16)e:SetWidth(16)e:SetPoint("\076\069\070\084",w,"\082\073\071\072\084",1,0)local D={checkbg=w;check=H,text=f,highlight=k,image=e,frame=K;type=p}for p,K in R(b)do D[p]=K end return a:RegisterAsWidget(D)end a:RegisterWidgetType(p,k,K)
--[[-----------------------------------------------------------------------------
Checkbox Widget
-------------------------------------------------------------------------------]]
local Type, Version = "CheckBox", 26
local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end

-- Lua APIs
local select, pairs = select, pairs

-- WoW APIs
local PlaySound = PlaySound
local CreateFrame, UIParent = CreateFrame, UIParent

--[[-----------------------------------------------------------------------------
Support functions
-------------------------------------------------------------------------------]]
local function AlignImage(self)
	local img = self.image:GetTexture()
	self.text:ClearAllPoints()
	if not img then
		self.text:SetPoint("LEFT", self.checkbg, "RIGHT")
		self.text:SetPoint("RIGHT")
	else
		self.text:SetPoint("LEFT", self.image, "RIGHT", 1, 0)
		self.text:SetPoint("RIGHT")
	end
end

--[[-----------------------------------------------------------------------------
Scripts
-------------------------------------------------------------------------------]]
local function Control_OnEnter(frame)
	frame.obj:Fire("OnEnter")
end

local function Control_OnLeave(frame)
	frame.obj:Fire("OnLeave")
end

local function CheckBox_OnMouseDown(frame)
	local self = frame.obj
	if not self.disabled then
		if self.image:GetTexture() then
			self.text:SetPoint("LEFT", self.image,"RIGHT", 2, -1)
		else
			self.text:SetPoint("LEFT", self.checkbg, "RIGHT", 1, -1)
		end
	end
	AceGUI:ClearFocus()
end

local function CheckBox_OnMouseUp(frame)
	local self = frame.obj
	if not self.disabled then
		self:ToggleChecked()

		if self.checked then
			PlaySound(856) -- SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_ON
		else -- for both nil and false (tristate)
			PlaySound(857) -- SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_OFF
		end

		self:Fire("OnValueChanged", self.checked)
		AlignImage(self)
	end
end

--[[-----------------------------------------------------------------------------
Methods
-------------------------------------------------------------------------------]]
local methods = {
	["OnAcquire"] = function(self)
		self:SetType()
		self:SetValue(false)
		self:SetTriState(nil)
		-- height is calculated from the width and required space for the description
		self:SetWidth(200)
		self:SetImage()
		self:SetDisabled(nil)
		self:SetDescription(nil)
	end,

	-- ["OnRelease"] = nil,

	["OnWidthSet"] = function(self, width)
		if self.desc then
			self.desc:SetWidth(width - 30)
			if self.desc:GetText() and self.desc:GetText() ~= "" then
				self:SetHeight(28 + self.desc:GetStringHeight())
			end
		end
	end,

	["SetDisabled"] = function(self, disabled)
		self.disabled = disabled
		if disabled then
			self.frame:Disable()
			self.text:SetTextColor(0.5, 0.5, 0.5)
			SetDesaturation(self.check, true)
			if self.desc then
				self.desc:SetTextColor(0.5, 0.5, 0.5)
			end
		else
			self.frame:Enable()
			self.text:SetTextColor(1, 1, 1)
			if self.tristate and self.checked == nil then
				SetDesaturation(self.check, true)
			else
				SetDesaturation(self.check, false)
			end
			if self.desc then
				self.desc:SetTextColor(1, 1, 1)
			end
		end
	end,

	["SetValue"] = function(self, value)
		local check = self.check
		self.checked = value
		if value then
			SetDesaturation(check, false)
			check:Show()
		else
			--Nil is the unknown tristate value
			if self.tristate and value == nil then
				SetDesaturation(check, true)
				check:Show()
			else
				SetDesaturation(check, false)
				check:Hide()
			end
		end
		self:SetDisabled(self.disabled)
	end,

	["GetValue"] = function(self)
		return self.checked
	end,

	["SetTriState"] = function(self, enabled)
		self.tristate = enabled
		self:SetValue(self:GetValue())
	end,

	["SetType"] = function(self, type)
		local checkbg = self.checkbg
		local check = self.check
		local highlight = self.highlight

		local size
		if type == "radio" then
			size = 16
			checkbg:SetTexture(130843) -- Interface\\Buttons\\UI-RadioButton
			checkbg:SetTexCoord(0, 0.25, 0, 1)
			check:SetTexture(130843) -- Interface\\Buttons\\UI-RadioButton
			check:SetTexCoord(0.25, 0.5, 0, 1)
			check:SetBlendMode("ADD")
			highlight:SetTexture(130843) -- Interface\\Buttons\\UI-RadioButton
			highlight:SetTexCoord(0.5, 0.75, 0, 1)
		else
			size = 24
			checkbg:SetTexture(130755) -- Interface\\Buttons\\UI-CheckBox-Up
			checkbg:SetTexCoord(0, 1, 0, 1)
			check:SetTexture(130751) -- Interface\\Buttons\\UI-CheckBox-Check
			check:SetTexCoord(0, 1, 0, 1)
			check:SetBlendMode("BLEND")
			highlight:SetTexture(130753) -- Interface\\Buttons\\UI-CheckBox-Highlight
			highlight:SetTexCoord(0, 1, 0, 1)
		end
		checkbg:SetHeight(size)
		checkbg:SetWidth(size)
	end,

	["ToggleChecked"] = function(self)
		local value = self:GetValue()
		if self.tristate then
			--cycle in true, nil, false order
			if value then
				self:SetValue(nil)
			elseif value == nil then
				self:SetValue(false)
			else
				self:SetValue(true)
			end
		else
			self:SetValue(not self:GetValue())
		end
	end,

	["SetLabel"] = function(self, label)
		self.text:SetText(label)
	end,

	["SetDescription"] = function(self, desc)
		if desc then
			if not self.desc then
				local f = self.frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
				f:ClearAllPoints()
				f:SetPoint("TOPLEFT", self.checkbg, "TOPRIGHT", 5, -21)
				f:SetWidth(self.frame.width - 30)
				f:SetPoint("RIGHT", self.frame, "RIGHT", -30, 0)
				f:SetJustifyH("LEFT")
				f:SetJustifyV("TOP")
				self.desc = f
			end
			self.desc:Show()
			--self.text:SetFontObject(GameFontNormal)
			self.desc:SetText(desc)
			self:SetHeight(28 + self.desc:GetStringHeight())
		else
			if self.desc then
				self.desc:SetText("")
				self.desc:Hide()
			end
			--self.text:SetFontObject(GameFontHighlight)
			self:SetHeight(24)
		end
	end,

	["SetImage"] = function(self, path, ...)
		local image = self.image
		image:SetTexture(path)

		if image:GetTexture() then
			local n = select("#", ...)
			if n == 4 or n == 8 then
				image:SetTexCoord(...)
			else
				image:SetTexCoord(0, 1, 0, 1)
			end
		end
		AlignImage(self)
	end
}

--[[-----------------------------------------------------------------------------
Constructor
-------------------------------------------------------------------------------]]
local function Constructor()
	local frame = CreateFrame("Button", nil, UIParent)
	frame:Hide()

	frame:EnableMouse(true)
	frame:SetScript("OnEnter", Control_OnEnter)
	frame:SetScript("OnLeave", Control_OnLeave)
	frame:SetScript("OnMouseDown", CheckBox_OnMouseDown)
	frame:SetScript("OnMouseUp", CheckBox_OnMouseUp)

	local checkbg = frame:CreateTexture(nil, "ARTWORK")
	checkbg:SetWidth(24)
	checkbg:SetHeight(24)
	checkbg:SetPoint("TOPLEFT")
	checkbg:SetTexture(130755) -- Interface\\Buttons\\UI-CheckBox-Up

	local check = frame:CreateTexture(nil, "OVERLAY")
	check:SetAllPoints(checkbg)
	check:SetTexture(130751) -- Interface\\Buttons\\UI-CheckBox-Check

	local text = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
	text:SetJustifyH("LEFT")
	text:SetHeight(18)
	text:SetPoint("LEFT", checkbg, "RIGHT")
	text:SetPoint("RIGHT")

	local highlight = frame:CreateTexture(nil, "HIGHLIGHT")
	highlight:SetTexture(130753) -- Interface\\Buttons\\UI-CheckBox-Highlight
	highlight:SetBlendMode("ADD")
	highlight:SetAllPoints(checkbg)

	local image = frame:CreateTexture(nil, "OVERLAY")
	image:SetHeight(16)
	image:SetWidth(16)
	image:SetPoint("LEFT", checkbg, "RIGHT", 1, 0)

	local widget = {
		checkbg   = checkbg,
		check     = check,
		text      = text,
		highlight = highlight,
		image     = image,
		frame     = frame,
		type      = Type
	}
	for method, func in pairs(methods) do
		widget[method] = func
	end

	return AceGUI:RegisterAsWidget(widget)
end

AceGUI:RegisterWidgetType(Type, Constructor, Version)
