1 --[[-----------------------------------------------------------------------------
3 Displays text and optionally an icon.
4 -------------------------------------------------------------------------------]]
5 local Type, Version = "Label", 23
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)
84 -- run the update explicitly
85 UpdateImageAnchor(self)
88 -- ["OnRelease"] = nil,
90 ["OnWidthSet"] = function(self, width)
91 UpdateImageAnchor(self)
94 ["SetText"] = function(self, text)
95 self.label:SetText(text)
96 UpdateImageAnchor(self)
99 ["SetColor"] = function(self, r, g, b)
100 if not (r and g and b) then
103 self.label:SetVertexColor(r, g, b)
106 ["SetImage"] = function(self, path, ...)
107 local image = self.image
108 image:SetTexture(path)
110 if image:GetTexture() then
111 self.imageshown = true
112 local n = select("#", ...)
113 if n == 4 or n == 8 then
114 image:SetTexCoord(...)
116 image:SetTexCoord(0, 1, 0, 1)
119 self.imageshown = nil
121 UpdateImageAnchor(self)
124 ["SetFont"] = function(self, font, height, flags)
125 self.label:SetFont(font, height, flags)
128 ["SetFontObject"] = function(self, font)
129 self:SetFont((font or GameFontHighlightSmall):GetFont())
132 ["SetImageSize"] = function(self, width, height)
133 self.image:SetWidth(width)
134 self.image:SetHeight(height)
135 UpdateImageAnchor(self)
139 --[[-----------------------------------------------------------------------------
141 -------------------------------------------------------------------------------]]
142 local function Constructor()
143 local frame = CreateFrame("Frame", nil, UIParent)
146 local label = frame:CreateFontString(nil, "BACKGROUND", "GameFontHighlightSmall")
147 label:SetJustifyH("LEFT")
148 label:SetJustifyV("TOP")
150 local image = frame:CreateTexture(nil, "BACKGROUND")
159 for method, func in pairs(methods) do
160 widget[method] = func
163 return AceGUI:RegisterAsWidget(widget)
166 AceGUI:RegisterWidgetType(Type, Constructor, Version)