local M,d="\076\097\098\101\108",28 local l=LibStub and LibStub("\065\099\101\071\085\073-\051\046\048",true)if not l or(l:GetWidgetVersion(M)or 0)>=d then return end local U,j,v=math.max,select,pairs local x,C=CreateFrame,UIParent local function i(M)if M.resizing then return end local d=M.frame local l=d.width or d:GetWidth()or 0 local j=M.image local v=M.label local x v:ClearAllPoints()j:ClearAllPoints()if M.imageshown then local M=j:GetWidth()if l-M<200 or(v:GetText()or"")==""then j:SetPoint("\084\079\080")v:SetPoint("\084\079\080",j,"\066\079\084\084\079\077")v:SetPoint("\076\069\070\084")v:SetWidth(l)x=j:GetHeight()+v:GetStringHeight()else j:SetPoint("\084\079\080\076\069\070\084")if j:GetHeight()>v:GetStringHeight()then v:SetPoint("\076\069\070\084",j,"\082\073\071\072\084",4,0)else v:SetPoint("\084\079\080\076\069\070\084",j,"\084\079\080\082\073\071\072\084",4,0)end v:SetWidth((l-M)-4)x=U(j:GetHeight(),v:GetStringHeight())end else v:SetPoint("\084\079\080\076\069\070\084")v:SetWidth(l)x=v:GetStringHeight()end if not x or x==0 then x=1 end M.resizing=true d:SetHeight(x)d.height=x M.resizing=nil end local A={OnAcquire=function(M)M.resizing=true M:SetWidth(200)M:SetText()M:SetImage(nil)M:SetImageSize(16,16)M:SetColor()M:SetFontObject()M:SetJustifyH("\076\069\070\084")M:SetJustifyV("\084\079\080")M.resizing=nil i(M)end;OnWidthSet=function(M,d)i(M)end;SetText=function(M,d)M.label:SetText(d)i(M)end,SetColor=function(M,d,l,U)if not(d and(l and U))then d,l,U=1,1,1 end M.label:SetVertexColor(d,l,U)end;SetImage=function(M,d,...)local l=M.image l:SetTexture(d)if l:GetTexture()then M.imageshown=true local d=j("\035",...)if d==4 or d==8 then l:SetTexCoord(...)else l:SetTexCoord(0,1,0,1)end else M.imageshown=nil end i(M)end;SetFont=function(d,U,j,v)if not d.fontObject then d.fontObject=CreateFont("\065\099\101\071\085\073\051\048\076\097\098\101\108\070\111\110\116"..l:GetNextWidgetNum(M))end d.fontObject:SetFont(U,j,v)d:SetFontObject(d.fontObject)end;SetFontObject=function(M,d)M.label:SetFontObject(d or GameFontHighlightSmall)i(M)end;SetImageSize=function(M,d,l)M.image:SetWidth(d)M.image:SetHeight(l)i(M)end,SetJustifyH=function(M,d)M.label:SetJustifyH(d)end,SetJustifyV=function(M,d)M.label:SetJustifyV(d)end}local function m()local d=x("\070\114\097\109\101",nil,C)d:Hide()local U=d:CreateFontString(nil,"\066\065\067\075\071\082\079\085\078\068","\071\097\109\101\070\111\110\116\072\105\103\104\108\105\103\104\116\083\109\097\108\108")local j=d:CreateTexture(nil,"\066\065\067\075\071\082\079\085\078\068")local i={label=U,image=j,frame=d;type=M}for M,d in v(A)do i[M]=d end return l:RegisterAsWidget(i)end l:RegisterWidgetType(M,m,d)
--[[-----------------------------------------------------------------------------
Label Widget
Displays text and optionally an icon.
-------------------------------------------------------------------------------]]
local Type, Version = "Label", 28
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 max, select, pairs = math.max, select, pairs

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

--[[-----------------------------------------------------------------------------
Support functions
-------------------------------------------------------------------------------]]

local function UpdateImageAnchor(self)
	if self.resizing then return end
	local frame = self.frame
	local width = frame.width or frame:GetWidth() or 0
	local image = self.image
	local label = self.label
	local height

	label:ClearAllPoints()
	image:ClearAllPoints()

	if self.imageshown then
		local imagewidth = image:GetWidth()
		if (width - imagewidth) < 200 or (label:GetText() or "") == "" then
			-- image goes on top centered when less than 200 width for the text, or if there is no text
			image:SetPoint("TOP")
			label:SetPoint("TOP", image, "BOTTOM")
			label:SetPoint("LEFT")
			label:SetWidth(width)
			height = image:GetHeight() + label:GetStringHeight()
		else
			-- image on the left
			image:SetPoint("TOPLEFT")
			if image:GetHeight() > label:GetStringHeight() then
				label:SetPoint("LEFT", image, "RIGHT", 4, 0)
			else
				label:SetPoint("TOPLEFT", image, "TOPRIGHT", 4, 0)
			end
			label:SetWidth(width - imagewidth - 4)
			height = max(image:GetHeight(), label:GetStringHeight())
		end
	else
		-- no image shown
		label:SetPoint("TOPLEFT")
		label:SetWidth(width)
		height = label:GetStringHeight()
	end

	-- avoid zero-height labels, since they can used as spacers
	if not height or height == 0 then
		height = 1
	end

	self.resizing = true
	frame:SetHeight(height)
	frame.height = height
	self.resizing = nil
end

--[[-----------------------------------------------------------------------------
Methods
-------------------------------------------------------------------------------]]
local methods = {
	["OnAcquire"] = function(self)
		-- set the flag to stop constant size updates
		self.resizing = true
		-- height is set dynamically by the text and image size
		self:SetWidth(200)
		self:SetText()
		self:SetImage(nil)
		self:SetImageSize(16, 16)
		self:SetColor()
		self:SetFontObject()
		self:SetJustifyH("LEFT")
		self:SetJustifyV("TOP")

		-- reset the flag
		self.resizing = nil
		-- run the update explicitly
		UpdateImageAnchor(self)
	end,

	-- ["OnRelease"] = nil,

	["OnWidthSet"] = function(self, width)
		UpdateImageAnchor(self)
	end,

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

	["SetColor"] = function(self, r, g, b)
		if not (r and g and b) then
			r, g, b = 1, 1, 1
		end
		self.label:SetVertexColor(r, g, b)
	end,

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

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

	["SetFont"] = function(self, font, height, flags)
		if not self.fontObject then
			self.fontObject = CreateFont("AceGUI30LabelFont" .. AceGUI:GetNextWidgetNum(Type))
		end
		self.fontObject:SetFont(font, height, flags)
		self:SetFontObject(self.fontObject)
	end,

	["SetFontObject"] = function(self, font)
		self.label:SetFontObject(font or GameFontHighlightSmall)
		UpdateImageAnchor(self)
	end,

	["SetImageSize"] = function(self, width, height)
		self.image:SetWidth(width)
		self.image:SetHeight(height)
		UpdateImageAnchor(self)
	end,

	["SetJustifyH"] = function(self, justifyH)
		self.label:SetJustifyH(justifyH)
	end,

	["SetJustifyV"] = function(self, justifyV)
		self.label:SetJustifyV(justifyV)
	end,
}

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

	local label = frame:CreateFontString(nil, "BACKGROUND", "GameFontHighlightSmall")
	local image = frame:CreateTexture(nil, "BACKGROUND")

	-- create widget
	local widget = {
		label = label,
		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)
