1 --[[-----------------------------------------------------------------------------
3 Displays text and optionally an icon.
4 -------------------------------------------------------------------------------]]
5 local Type, Version = "Label", 24
6 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
7 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
10 local max, select, pairs = math.max, select, pairs
13 local CreateFrame, UIParent = CreateFrame, UIParent
15 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
16 -- List them here for Mikk's FindGlobals script
17 -- GLOBALS: GameFontHighlightSmall
19 --[[-----------------------------------------------------------------------------
21 -------------------------------------------------------------------------------]]
23 local function UpdateImageAnchor(self)
24 if self.resizing then return end
25 local frame = self.frame
26 local width = frame.width or frame:GetWidth() or 0
27 local image = self.image
28 local label = self.label
31 label:ClearAllPoints()
32 image:ClearAllPoints()
34 if self.imageshown then
35 local imagewidth = image:GetWidth()
36 if (width - imagewidth) < 200 or (label:GetText() or "") == "" then
37 -- image goes on top centered when less than 200 width for the text, or if there is no text
39 label:SetPoint("TOP", image, "BOTTOM")
40 label:SetPoint("LEFT")
42 height = image:GetHeight() + label:GetHeight()
45 image:SetPoint("TOPLEFT")
46 if image:GetHeight() > label:GetHeight() then
47 label:SetPoint("LEFT", image, "RIGHT", 4, 0)
49 label:SetPoint("TOPLEFT", image, "TOPRIGHT", 4, 0)
51 label:SetWidth(width - imagewidth - 4)
52 height = max(image:GetHeight(), label:GetHeight())
56 label:SetPoint("TOPLEFT")
58 height = label:GetHeight()
62 frame:SetHeight(height)
67 --[[-----------------------------------------------------------------------------
69 -------------------------------------------------------------------------------]]
71 ["OnAcquire"] = function(self)
72 -- set the flag to stop constant size updates
74 -- height is set dynamically by the text and image size
78 self:SetImageSize(16, 16)
81 self:SetJustifyH("LEFT")
82 self:SetJustifyV("TOP")
86 -- run the update explicitly
87 UpdateImageAnchor(self)
90 -- ["OnRelease"] = nil,
92 ["OnWidthSet"] = function(self, width)
93 UpdateImageAnchor(self)
96 ["SetText"] = function(self, text)
97 self.label:SetText(text)
98 UpdateImageAnchor(self)
101 ["SetColor"] = function(self, r, g, b)
102 if not (r and g and b) then
105 self.label:SetVertexColor(r, g, b)
108 ["SetImage"] = function(self, path, ...)
109 local image = self.image
110 image:SetTexture(path)
112 if image:GetTexture() then
113 self.imageshown = true
114 local n = select("#", ...)
115 if n == 4 or n == 8 then
116 image:SetTexCoord(...)
118 image:SetTexCoord(0, 1, 0, 1)
121 self.imageshown = nil
123 UpdateImageAnchor(self)
126 ["SetFont"] = function(self, font, height, flags)
127 self.label:SetFont(font, height, flags)
130 ["SetFontObject"] = function(self, font)
131 self:SetFont((font or GameFontHighlightSmall):GetFont())
134 ["SetImageSize"] = function(self, width, height)
135 self.image:SetWidth(width)
136 self.image:SetHeight(height)
137 UpdateImageAnchor(self)
140 ["SetJustifyH"] = function(self, justifyH)
141 self.label:SetJustifyH(justifyH)
144 ["SetJustifyV"] = function(self, justifyV)
145 self.label:SetJustifyV(justifyV)
149 --[[-----------------------------------------------------------------------------
151 -------------------------------------------------------------------------------]]
152 local function Constructor()
153 local frame = CreateFrame("Frame", nil, UIParent)
156 local label = frame:CreateFontString(nil, "BACKGROUND", "GameFontHighlightSmall")
157 local image = frame:CreateTexture(nil, "BACKGROUND")
166 for method, func in pairs(methods) do
167 widget[method] = func
170 return AceGUI:RegisterAsWidget(widget)
173 AceGUI:RegisterWidgetType(Type, Constructor, Version)