56591cf - Delete unnecessary libraries
[wowui.git] / OmaRFConfig / libs / AceGUI-3.0 / widgets / AceGUIWidget-Label.lua
1 --[[-----------------------------------------------------------------------------
2 Label Widget
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
8
9 -- Lua APIs
10 local max, select, pairs = math.max, select, pairs
11
12 -- WoW APIs
13 local CreateFrame, UIParent = CreateFrame, UIParent
14
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
18
19 --[[-----------------------------------------------------------------------------
20 Support functions
21 -------------------------------------------------------------------------------]]
22
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
29         local height
30
31         label:ClearAllPoints()
32         image:ClearAllPoints()
33
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
38                         image:SetPoint("TOP")
39                         label:SetPoint("TOP", image, "BOTTOM")
40                         label:SetPoint("LEFT")
41                         label:SetWidth(width)
42                         height = image:GetHeight() + label:GetHeight()
43                 else
44                         -- image on the left
45                         image:SetPoint("TOPLEFT")
46                         if image:GetHeight() > label:GetHeight() then
47                                 label:SetPoint("LEFT", image, "RIGHT", 4, 0)
48                         else
49                                 label:SetPoint("TOPLEFT", image, "TOPRIGHT", 4, 0)
50                         end
51                         label:SetWidth(width - imagewidth - 4)
52                         height = max(image:GetHeight(), label:GetHeight())
53                 end
54         else
55                 -- no image shown
56                 label:SetPoint("TOPLEFT")
57                 label:SetWidth(width)
58                 height = label:GetHeight()
59         end
60         
61         self.resizing = true
62         frame:SetHeight(height)
63         frame.height = height
64         self.resizing = nil
65 end
66
67 --[[-----------------------------------------------------------------------------
68 Methods
69 -------------------------------------------------------------------------------]]
70 local methods = {
71         ["OnAcquire"] = function(self)
72                 -- set the flag to stop constant size updates
73                 self.resizing = true
74                 -- height is set dynamically by the text and image size
75                 self:SetWidth(200)
76                 self:SetText()
77                 self:SetImage(nil)
78                 self:SetImageSize(16, 16)
79                 self:SetColor()
80                 self:SetFontObject()
81                 self:SetJustifyH("LEFT")
82                 self:SetJustifyV("TOP")
83
84                 -- reset the flag
85                 self.resizing = nil
86                 -- run the update explicitly
87                 UpdateImageAnchor(self)
88         end,
89
90         -- ["OnRelease"] = nil,
91
92         ["OnWidthSet"] = function(self, width)
93                 UpdateImageAnchor(self)
94         end,
95
96         ["SetText"] = function(self, text)
97                 self.label:SetText(text)
98                 UpdateImageAnchor(self)
99         end,
100
101         ["SetColor"] = function(self, r, g, b)
102                 if not (r and g and b) then
103                         r, g, b = 1, 1, 1
104                 end
105                 self.label:SetVertexColor(r, g, b)
106         end,
107
108         ["SetImage"] = function(self, path, ...)
109                 local image = self.image
110                 image:SetTexture(path)
111                 
112                 if image:GetTexture() then
113                         self.imageshown = true
114                         local n = select("#", ...)
115                         if n == 4 or n == 8 then
116                                 image:SetTexCoord(...)
117                         else
118                                 image:SetTexCoord(0, 1, 0, 1)
119                         end
120                 else
121                         self.imageshown = nil
122                 end
123                 UpdateImageAnchor(self)
124         end,
125
126         ["SetFont"] = function(self, font, height, flags)
127                 self.label:SetFont(font, height, flags)
128         end,
129
130         ["SetFontObject"] = function(self, font)
131                 self:SetFont((font or GameFontHighlightSmall):GetFont())
132         end,
133
134         ["SetImageSize"] = function(self, width, height)
135                 self.image:SetWidth(width)
136                 self.image:SetHeight(height)
137                 UpdateImageAnchor(self)
138         end,
139
140         ["SetJustifyH"] = function(self, justifyH)
141                 self.label:SetJustifyH(justifyH)
142         end,
143
144         ["SetJustifyV"] = function(self, justifyV)
145                 self.label:SetJustifyV(justifyV)
146         end,
147 }
148
149 --[[-----------------------------------------------------------------------------
150 Constructor
151 -------------------------------------------------------------------------------]]
152 local function Constructor()
153         local frame = CreateFrame("Frame", nil, UIParent)
154         frame:Hide()
155
156         local label = frame:CreateFontString(nil, "BACKGROUND", "GameFontHighlightSmall")
157         local image = frame:CreateTexture(nil, "BACKGROUND")
158
159         -- create widget
160         local widget = {
161                 label = label,
162                 image = image,
163                 frame = frame,
164                 type  = Type
165         }
166         for method, func in pairs(methods) do
167                 widget[method] = func
168         end
169
170         return AceGUI:RegisterAsWidget(widget)
171 end
172
173 AceGUI:RegisterWidgetType(Type, Constructor, Version)