local h,m="\067\104\101\099\107\066\111\120",26 local t=LibStub and LibStub("\065\099\101\071\085\073-\051\046\048",true)if not t or(t:GetWidgetVersion(h)or 0)>=m then return end local a,N=select,pairs local z=PlaySound local s,S=CreateFrame,UIParent local function g(h)local m=h.image:GetTexture()h.text:ClearAllPoints()if not m then h.text:SetPoint("\076\069\070\084",h.checkbg,"\082\073\071\072\084")h.text:SetPoint("\082\073\071\072\084")else h.text:SetPoint("\076\069\070\084",h.image,"\082\073\071\072\084",1,0)h.text:SetPoint("\082\073\071\072\084")end end local function i(h)h.obj:Fire("\079\110\069\110\116\101\114")end local function j(h)h.obj:Fire("\079\110\076\101\097\118\101")end local function D(h)local m=h.obj if not m.disabled then if m.image:GetTexture()then m.text:SetPoint("\076\069\070\084",m.image,"\082\073\071\072\084",2,-1)else m.text:SetPoint("\076\069\070\084",m.checkbg,"\082\073\071\072\084",1,-1)end end t:ClearFocus()end local function U(h)local m=h.obj if not m.disabled then m:ToggleChecked()if m.checked then z(856)else z(857)end m:Fire("\079\110\086\097\108\117\101\067\104\097\110\103\101\100",m.checked)g(m)end end local E={OnAcquire=function(h)h:SetType()h:SetValue(false)h:SetTriState(nil)h:SetWidth(200)h:SetImage()h:SetDisabled(nil)h:SetDescription(nil)end;OnWidthSet=function(h,m)if h.desc then h.desc:SetWidth(m-30)if h.desc:GetText()and h.desc:GetText()~=""then h:SetHeight(28+h.desc:GetStringHeight())end end end,SetDisabled=function(h,m)h.disabled=m if m then h.frame:Disable()h.text:SetTextColor(.5,.5,.5)SetDesaturation(h.check,true)if h.desc then h.desc:SetTextColor(.5,.5,.5)end else h.frame:Enable()h.text:SetTextColor(1,1,1)if h.tristate and h.checked==nil then SetDesaturation(h.check,true)else SetDesaturation(h.check,false)end if h.desc then h.desc:SetTextColor(1,1,1)end end end;SetValue=function(h,m)local t=h.check h.checked=m if m then SetDesaturation(t,false)t:Show()else if h.tristate and m==nil then SetDesaturation(t,true)t:Show()else SetDesaturation(t,false)t:Hide()end end h:SetDisabled(h.disabled)end;GetValue=function(h)return h.checked end;SetTriState=function(h,m)h.tristate=m h:SetValue(h:GetValue())end;SetType=function(h,m)local t=h.checkbg local a=h.check local N=h.highlight local z if m=="\114\097\100\105\111"then z=16 t:SetTexture(130843)t:SetTexCoord(0,.25,0,1)a:SetTexture(130843)a:SetTexCoord(.25,.5,0,1)a:SetBlendMode("\065\068\068")N:SetTexture(130843)N:SetTexCoord(.5,.75,0,1)else z=24 t:SetTexture(130755)t:SetTexCoord(0,1,0,1)a:SetTexture(130751)a:SetTexCoord(0,1,0,1)a:SetBlendMode("\066\076\069\078\068")N:SetTexture(130753)N:SetTexCoord(0,1,0,1)end t:SetHeight(z)t:SetWidth(z)end,ToggleChecked=function(h)local m=h:GetValue()if h.tristate then if m then h:SetValue(nil)elseif m==nil then h:SetValue(false)else h:SetValue(true)end else h:SetValue(not h:GetValue())end end;SetLabel=function(h,m)h.text:SetText(m)end,SetDescription=function(h,m)if m then if not h.desc then local m=h.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")m:ClearAllPoints()m:SetPoint("\084\079\080\076\069\070\084",h.checkbg,"\084\079\080\082\073\071\072\084",5,-21)m:SetWidth(h.frame.width-30)m:SetPoint("\082\073\071\072\084",h.frame,"\082\073\071\072\084",-30,0)m:SetJustifyH("\076\069\070\084")m:SetJustifyV("\084\079\080")h.desc=m end h.desc:Show()h.desc:SetText(m)h:SetHeight(28+h.desc:GetStringHeight())else if h.desc then h.desc:SetText("")h.desc:Hide()end h:SetHeight(24)end end,SetImage=function(h,m,...)local t=h.image t:SetTexture(m)if t:GetTexture()then local h=a("\035",...)if h==4 or h==8 then t:SetTexCoord(...)else t:SetTexCoord(0,1,0,1)end end g(h)end}local function Q()local m=s("\066\117\116\116\111\110",nil,S)m:Hide()m:EnableMouse(true)m:SetScript("\079\110\069\110\116\101\114",i)m:SetScript("\079\110\076\101\097\118\101",j)m:SetScript("\079\110\077\111\117\115\101\068\111\119\110",D)m:SetScript("\079\110\077\111\117\115\101\085\112",U)local a=m:CreateTexture(nil,"\065\082\084\087\079\082\075")a:SetWidth(24)a:SetHeight(24)a:SetPoint("\084\079\080\076\069\070\084")a:SetTexture(130755)local z=m:CreateTexture(nil,"\079\086\069\082\076\065\089")z:SetAllPoints(a)z:SetTexture(130751)local g=m: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")g:SetJustifyH("\076\069\070\084")g:SetHeight(18)g:SetPoint("\076\069\070\084",a,"\082\073\071\072\084")g:SetPoint("\082\073\071\072\084")local Q=m:CreateTexture(nil,"\072\073\071\072\076\073\071\072\084")Q:SetTexture(130753)Q:SetBlendMode("\065\068\068")Q:SetAllPoints(a)local L=m:CreateTexture(nil,"\079\086\069\082\076\065\089")L:SetHeight(16)L:SetWidth(16)L:SetPoint("\076\069\070\084",a,"\082\073\071\072\084",1,0)local H={checkbg=a;check=z;text=g,highlight=Q,image=L,frame=m,type=h}for h,m in N(E)do H[h]=m end return t:RegisterAsWidget(H)end t:RegisterWidgetType(h,Q,m)
--[[-----------------------------------------------------------------------------
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)
