1 --- **AceGUI-3.0** provides access to numerous widgets which can be used to create GUIs.
2 -- AceGUI is used by AceConfigDialog to create the option GUIs, but you can use it by itself
3 -- to create any custom GUI. There are more extensive examples in the test suite in the Ace3
4 -- stand-alone distribution.
6 -- **Note**: When using AceGUI-3.0 directly, please do not modify the frames of the widgets directly,
7 -- as any "unknown" change to the widgets will cause addons that get your widget out of the widget pool
8 -- to misbehave. If you think some part of a widget should be modifiable, please open a ticket, and we"ll
9 -- implement a proper API to modify it.
11 -- local AceGUI = LibStub("AceGUI-3.0")
12 -- -- Create a container frame
13 -- local f = AceGUI:Create("Frame")
14 -- f:SetCallback("OnClose",function(widget) AceGUI:Release(widget) end)
15 -- f:SetTitle("AceGUI-3.0 Example")
16 -- f:SetStatusText("Status Bar")
17 -- f:SetLayout("Flow")
19 -- local btn = AceGUI:Create("Button")
21 -- btn:SetText("Button !")
22 -- btn:SetCallback("OnClick", function() print("Click!") end)
23 -- -- Add the button to the container
27 -- @release $Id: AceGUI-3.0.lua 924 2010-05-13 15:12:20Z nevcairiel $
28 local ACEGUI_MAJOR, ACEGUI_MINOR = "AceGUI-3.0", 33
29 local AceGUI, oldminor = LibStub:NewLibrary(ACEGUI_MAJOR, ACEGUI_MINOR)
31 if not AceGUI then return end -- No upgrade needed
34 local tconcat, tremove, tinsert = table.concat, table.remove, table.insert
35 local select, pairs, next, type = select, pairs, next, type
36 local error, assert, loadstring = error, assert, loadstring
37 local setmetatable, rawget, rawset = setmetatable, rawget, rawset
38 local math_max = math.max
41 local UIParent = UIParent
43 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
44 -- List them here for Mikk's FindGlobals script
45 -- GLOBALS: geterrorhandler, LibStub
47 --local con = LibStub("AceConsole-3.0",true)
49 AceGUI.WidgetRegistry = AceGUI.WidgetRegistry or {}
50 AceGUI.LayoutRegistry = AceGUI.LayoutRegistry or {}
51 AceGUI.WidgetBase = AceGUI.WidgetBase or {}
52 AceGUI.WidgetContainerBase = AceGUI.WidgetContainerBase or {}
53 AceGUI.WidgetVersions = AceGUI.WidgetVersions or {}
56 local WidgetRegistry = AceGUI.WidgetRegistry
57 local LayoutRegistry = AceGUI.LayoutRegistry
58 local WidgetVersions = AceGUI.WidgetVersions
61 xpcall safecall implementation
65 local function errorhandler(err)
66 return geterrorhandler()(err)
69 local function CreateDispatcher(argCount)
71 local xpcall, eh = ...
73 local function call() return method(ARGS) end
75 local function dispatch(func, ...)
77 if not method then return end
79 return xpcall(call, eh)
86 for i = 1, argCount do ARGS[i] = "arg"..i end
87 code = code:gsub("ARGS", tconcat(ARGS, ", "))
88 return assert(loadstring(code, "safecall Dispatcher["..argCount.."]"))(xpcall, errorhandler)
91 local Dispatchers = setmetatable({}, {__index=function(self, argCount)
92 local dispatcher = CreateDispatcher(argCount)
93 rawset(self, argCount, dispatcher)
96 Dispatchers[0] = function(func)
97 return xpcall(func, errorhandler)
100 local function safecall(func, ...)
101 return Dispatchers[select("#", ...)](func, ...)
104 -- Recycling functions
105 local newWidget, delWidget
107 -- Version Upgrade in Minor 29
108 -- Internal Storage of the objects changed, from an array table
109 -- to a hash table, and additionally we introduced versioning on
110 -- the widgets which would discard all widgets from a pre-29 version
111 -- anyway, so we just clear the storage now, and don't try to
112 -- convert the storage tables to the new format.
113 -- This should generally not cause *many* widgets to end up in trash,
114 -- since once dialogs are opened, all addons should be loaded already
115 -- and AceGUI should be on the latest version available on the users
117 -- -- nevcairiel - Nov 2nd, 2009
118 if oldminor and oldminor < 29 and AceGUI.objPools then
119 AceGUI.objPools = nil
122 AceGUI.objPools = AceGUI.objPools or {}
123 local objPools = AceGUI.objPools
124 --Returns a new instance, if none are available either returns a new table or calls the given contructor
125 function newWidget(type)
126 if not WidgetRegistry[type] then
127 error("Attempt to instantiate unknown widget type", 2)
130 if not objPools[type] then
134 local newObj = next(objPools[type])
136 newObj = WidgetRegistry[type]()
137 newObj.AceGUIWidgetVersion = WidgetVersions[type]
139 objPools[type][newObj] = nil
140 -- if the widget is older then the latest, don't even try to reuse it
141 -- just forget about it, and grab a new one.
142 if not newObj.AceGUIWidgetVersion or newObj.AceGUIWidgetVersion < WidgetVersions[type] then
143 return newWidget(type)
148 -- Releases an instance to the Pool
149 function delWidget(obj,type)
150 if not objPools[type] then
153 if objPools[type][obj] then
154 error("Attempt to Release Widget that is already released", 2)
156 objPools[type][obj] = true
165 -- Gets a widget Object
167 --- Create a new Widget of the given type.
168 -- This function will instantiate a new widget (or use one from the widget pool), and call the
169 -- OnAcquire function on it, before returning.
170 -- @param type The type of the widget.
171 -- @return The newly created widget.
172 function AceGUI:Create(type)
173 if WidgetRegistry[type] then
174 local widget = newWidget(type)
176 if rawget(widget, "Acquire") then
177 widget.OnAcquire = widget.Acquire
179 elseif rawget(widget, "Aquire") then
180 widget.OnAcquire = widget.Aquire
184 if rawget(widget, "Release") then
185 widget.OnRelease = rawget(widget, "Release")
189 if widget.OnAcquire then
192 error(("Widget type %s doesn't supply an OnAcquire Function"):format(type))
194 -- Set the default Layout ("List")
195 safecall(widget.SetLayout, widget, "List")
196 safecall(widget.ResumeLayout, widget)
201 --- Releases a widget Object.
202 -- This function calls OnRelease on the widget and places it back in the widget pool.
203 -- Any data on the widget is being erased, and the widget will be hidden.\\
204 -- If this widget is a Container-Widget, all of its Child-Widgets will be releases as well.
205 -- @param widget The widget to release
206 function AceGUI:Release(widget)
207 safecall(widget.PauseLayout, widget)
208 widget:Fire("OnRelease")
209 safecall(widget.ReleaseChildren, widget)
211 if widget.OnRelease then
214 -- error(("Widget type %s doesn't supply an OnRelease Function"):format(widget.type))
216 for k in pairs(widget.userdata) do
217 widget.userdata[k] = nil
219 for k in pairs(widget.events) do
220 widget.events[k] = nil
223 widget.relWidth = nil
225 widget.relHeight = nil
226 widget.noAutoHeight = nil
227 widget.frame:ClearAllPoints()
229 widget.frame:SetParent(UIParent)
230 widget.frame.width = nil
231 widget.frame.height = nil
232 if widget.content then
233 widget.content.width = nil
234 widget.content.height = nil
236 delWidget(widget, widget.type)
244 --- Called when a widget has taken focus.
245 -- e.g. Dropdowns opening, Editboxes gaining kb focus
246 -- @param widget The widget that should be focused
247 function AceGUI:SetFocus(widget)
248 if self.FocusedWidget and self.FocusedWidget ~= widget then
249 safecall(self.FocusedWidget.ClearFocus, self.FocusedWidget)
251 self.FocusedWidget = widget
255 --- Called when something has happened that could cause widgets with focus to drop it
256 -- e.g. titlebar of a frame being clicked
257 function AceGUI:ClearFocus()
258 if self.FocusedWidget then
259 safecall(self.FocusedWidget.ClearFocus, self.FocusedWidget)
260 self.FocusedWidget = nil
268 Widgets must provide the following functions
269 OnAcquire() - Called when the object is acquired, should set everything to a default hidden state
271 And the following members
272 frame - the frame or derivitive object that will be treated as the widget for size and anchoring purposes
273 type - the type of the object, same as the name given to :RegisterWidget()
275 Widgets contain a table called userdata, this is a safe place to store data associated with the wigdet
276 It will be cleared automatically when a widget is released
277 Placing values directly into a widget object should be avoided
279 If the Widget can act as a container for other Widgets the following
280 content - frame or derivitive that children will be anchored to
282 The Widget can supply the following Optional Members
283 :OnRelease() - Called when the object is Released, should remove any additional anchors and clear any data
284 :OnWidthSet(width) - Called when the width of the widget is changed
285 :OnHeightSet(height) - Called when the height of the widget is changed
286 Widgets should not use the OnSizeChanged events of thier frame or content members, use these methods instead
287 AceGUI already sets a handler to the event
288 :LayoutFinished(width, height) - called after a layout has finished, the width and height will be the width and height of the
289 area used for controls. These can be nil if the layout used the existing size to layout the controls.
293 --------------------------
294 -- Widget Base Template --
295 --------------------------
297 local WidgetBase = AceGUI.WidgetBase
299 WidgetBase.SetParent = function(self, parent)
300 local frame = self.frame
302 frame:SetParent(parent.content)
306 WidgetBase.SetCallback = function(self, name, func)
307 if type(func) == "function" then
308 self.events[name] = func
312 WidgetBase.Fire = function(self, name, ...)
313 if self.events[name] then
314 local success, ret = safecall(self.events[name], self, name, ...)
321 WidgetBase.SetWidth = function(self, width)
322 self.frame:SetWidth(width)
323 self.frame.width = width
324 if self.OnWidthSet then
325 self:OnWidthSet(width)
329 WidgetBase.SetRelativeWidth = function(self, width)
330 if width <= 0 or width > 1 then
331 error(":SetRelativeWidth(width): Invalid relative width.", 2)
333 self.relWidth = width
334 self.width = "relative"
337 WidgetBase.SetHeight = function(self, height)
338 self.frame:SetHeight(height)
339 self.frame.height = height
340 if self.OnHeightSet then
341 self:OnHeightSet(height)
345 --[[ WidgetBase.SetRelativeHeight = function(self, height)
346 if height <= 0 or height > 1 then
347 error(":SetRelativeHeight(height): Invalid relative height.", 2)
349 self.relHeight = height
350 self.height = "relative"
353 WidgetBase.IsVisible = function(self)
354 return self.frame:IsVisible()
357 WidgetBase.IsShown= function(self)
358 return self.frame:IsShown()
361 WidgetBase.Release = function(self)
365 WidgetBase.SetPoint = function(self, ...)
366 return self.frame:SetPoint(...)
369 WidgetBase.ClearAllPoints = function(self)
370 return self.frame:ClearAllPoints()
373 WidgetBase.GetNumPoints = function(self)
374 return self.frame:GetNumPoints()
377 WidgetBase.GetPoint = function(self, ...)
378 return self.frame:GetPoint(...)
381 WidgetBase.GetUserDataTable = function(self)
385 WidgetBase.SetUserData = function(self, key, value)
386 self.userdata[key] = value
389 WidgetBase.GetUserData = function(self, key)
390 return self.userdata[key]
393 WidgetBase.IsFullHeight = function(self)
394 return self.height == "fill"
397 WidgetBase.SetFullHeight = function(self, isFull)
405 WidgetBase.IsFullWidth = function(self)
406 return self.width == "fill"
409 WidgetBase.SetFullWidth = function(self, isFull)
417 -- local function LayoutOnUpdate(this)
418 -- this:SetScript("OnUpdate",nil)
419 -- this.obj:PerformLayout()
422 local WidgetContainerBase = AceGUI.WidgetContainerBase
424 WidgetContainerBase.PauseLayout = function(self)
425 self.LayoutPaused = true
428 WidgetContainerBase.ResumeLayout = function(self)
429 self.LayoutPaused = nil
432 WidgetContainerBase.PerformLayout = function(self)
433 if self.LayoutPaused then
436 safecall(self.LayoutFunc, self.content, self.children)
439 --call this function to layout, makes sure layed out objects get a frame to get sizes etc
440 WidgetContainerBase.DoLayout = function(self)
442 -- if not self.parent then
443 -- self.frame:SetScript("OnUpdate", LayoutOnUpdate)
447 WidgetContainerBase.AddChild = function(self, child, beforeWidget)
449 local siblingIndex = 1
450 for _, widget in pairs(self.children) do
451 if widget == beforeWidget then
454 siblingIndex = siblingIndex + 1
456 tinsert(self.children, siblingIndex, child)
458 tinsert(self.children, child)
460 child:SetParent(self)
465 WidgetContainerBase.AddChildren = function(self, ...)
466 for i = 1, select("#", ...) do
467 local child = select(i, ...)
468 tinsert(self.children, child)
469 child:SetParent(self)
475 WidgetContainerBase.ReleaseChildren = function(self)
476 local children = self.children
477 for i = 1,#children do
478 AceGUI:Release(children[i])
483 WidgetContainerBase.SetLayout = function(self, Layout)
484 self.LayoutFunc = AceGUI:GetLayout(Layout)
487 WidgetContainerBase.SetAutoAdjustHeight = function(self, adjust)
489 self.noAutoHeight = nil
491 self.noAutoHeight = true
495 local function FrameResize(this)
496 local self = this.obj
497 if this:GetWidth() and this:GetHeight() then
498 if self.OnWidthSet then
499 self:OnWidthSet(this:GetWidth())
501 if self.OnHeightSet then
502 self:OnHeightSet(this:GetHeight())
507 local function ContentResize(this)
508 if this:GetWidth() and this:GetHeight() then
509 this.width = this:GetWidth()
510 this.height = this:GetHeight()
515 setmetatable(WidgetContainerBase, {__index=WidgetBase})
517 --One of these function should be called on each Widget Instance as part of its creation process
519 --- Register a widget-class as a container for newly created widgets.
520 -- @param widget The widget class
521 function AceGUI:RegisterAsContainer(widget)
525 widget.base = WidgetContainerBase
526 widget.content.obj = widget
527 widget.frame.obj = widget
528 widget.content:SetScript("OnSizeChanged", ContentResize)
529 widget.frame:SetScript("OnSizeChanged", FrameResize)
530 setmetatable(widget, {__index = WidgetContainerBase})
531 widget:SetLayout("List")
535 --- Register a widget-class as a widget.
536 -- @param widget The widget class
537 function AceGUI:RegisterAsWidget(widget)
540 widget.base = WidgetBase
541 widget.frame.obj = widget
542 widget.frame:SetScript("OnSizeChanged", FrameResize)
543 setmetatable(widget, {__index = WidgetBase})
555 --- Registers a widget Constructor, this function returns a new instance of the Widget
556 -- @param Name The name of the widget
557 -- @param Constructor The widget constructor function
558 -- @param Version The version of the widget
559 function AceGUI:RegisterWidgetType(Name, Constructor, Version)
560 assert(type(Constructor) == "function")
561 assert(type(Version) == "number")
563 local oldVersion = WidgetVersions[Name]
564 if oldVersion and oldVersion >= Version then return end
566 WidgetVersions[Name] = Version
567 WidgetRegistry[Name] = Constructor
570 --- Registers a Layout Function
571 -- @param Name The name of the layout
572 -- @param LayoutFunc Reference to the layout function
573 function AceGUI:RegisterLayout(Name, LayoutFunc)
574 assert(type(LayoutFunc) == "function")
575 if type(Name) == "string" then
578 LayoutRegistry[Name] = LayoutFunc
581 --- Get a Layout Function from the registry
582 -- @param Name The name of the layout
583 function AceGUI:GetLayout(Name)
584 if type(Name) == "string" then
587 return LayoutRegistry[Name]
590 AceGUI.counts = AceGUI.counts or {}
592 --- A type-based counter to count the number of widgets created.
593 -- This is used by widgets that require a named frame, e.g. when a Blizzard
594 -- Template requires it.
595 -- @param type The widget type
596 function AceGUI:GetNextWidgetNum(type)
597 if not self.counts[type] then
598 self.counts[type] = 0
600 self.counts[type] = self.counts[type] + 1
601 return self.counts[type]
604 --- Return the number of created widgets for this type.
605 -- In contrast to GetNextWidgetNum, the number is not incremented.
606 -- @param type The widget type
607 function AceGUI:GetWidgetCount(type)
608 return self.counts[type] or 0
611 --- Return the version of the currently registered widget type.
612 -- @param type The widget type
613 function AceGUI:GetWidgetVersion(type)
614 return WidgetVersions[type]
622 A Layout is a func that takes 2 parameters
623 content - the frame that widgets will be placed inside
624 children - a table containing the widgets to layout
627 -- Very simple Layout, Children are stacked on top of each other down the left side
628 AceGUI:RegisterLayout("List",
629 function(content, children)
631 local width = content.width or content:GetWidth() or 0
632 for i = 1, #children do
633 local child = children[i]
635 local frame = child.frame
636 frame:ClearAllPoints()
639 frame:SetPoint("TOPLEFT", content)
641 frame:SetPoint("TOPLEFT", children[i-1].frame, "BOTTOMLEFT")
644 if child.width == "fill" then
645 child:SetWidth(width)
646 frame:SetPoint("RIGHT", content)
648 if child.DoLayout then
651 elseif child.width == "relative" then
652 child:SetWidth(width * child.relWidth)
654 if child.DoLayout then
659 height = height + (frame.height or frame:GetHeight() or 0)
661 safecall(content.obj.LayoutFinished, content.obj, nil, height)
664 -- A single control fills the whole content area
665 AceGUI:RegisterLayout("Fill",
666 function(content, children)
668 children[1]:SetWidth(content:GetWidth() or 0)
669 children[1]:SetHeight(content:GetHeight() or 0)
670 children[1].frame:SetAllPoints(content)
671 children[1].frame:Show()
672 safecall(content.obj.LayoutFinished, content.obj, nil, children[1].frame:GetHeight())
676 AceGUI:RegisterLayout("Flow",
677 function(content, children)
680 --width used in the current row
682 --height of the current row
687 local width = content.width or content:GetWidth() or 0
689 --control at the start of the row
696 local lastframeoffset
698 for i = 1, #children do
699 local child = children[i]
701 local frame = child.frame
702 local frameheight = frame.height or frame:GetHeight() or 0
703 local framewidth = frame.width or frame:GetWidth() or 0
704 lastframeoffset = frameoffset
705 -- HACK: Why did we set a frameoffset of (frameheight / 2) ?
706 -- That was moving all widgets half the widgets size down, is that intended?
707 -- Actually, it seems to be neccessary for many cases, we'll leave it in for now.
708 -- If widgets seem to anchor weirdly with this, provide a valid alignoffset for them.
709 -- TODO: Investigate moar!
710 frameoffset = child.alignoffset or (frameheight / 2)
712 if child.width == "relative" then
713 framewidth = width * child.relWidth
717 frame:ClearAllPoints()
719 -- anchor the first control to the top left
720 frame:SetPoint("TOPLEFT", content)
721 rowheight = frameheight
722 rowoffset = frameoffset
724 rowstartoffset = frameoffset
725 usedwidth = framewidth
726 if usedwidth > width then
730 -- if there isn't available width for the control start a new row
731 -- if a control is "fill" it will be on a row of its own full width
732 if usedwidth == 0 or ((framewidth) + usedwidth > width) or child.width == "fill" then
734 -- a previous row has already filled the entire height, there's nothing we can usefully do anymore
735 -- (maybe error/warn about this?)
738 --anchor the previous row, we will now know its height and offset
739 rowstart:SetPoint("TOPLEFT", content, "TOPLEFT", 0, -(height + (rowoffset - rowstartoffset) + 3))
740 height = height + rowheight + 3
741 --save this as the rowstart so we can anchor it after the row is complete and we have the max height and offset of controls in it
743 rowstartoffset = frameoffset
744 rowheight = frameheight
745 rowoffset = frameoffset
746 usedwidth = framewidth
747 if usedwidth > width then
750 -- put the control on the current row, adding it to the width and checking if the height needs to be increased
752 --handles cases where the new height is higher than either control because of the offsets
753 --math.max(rowheight-rowoffset+frameoffset, frameheight-frameoffset+rowoffset)
755 --offset is always the larger of the two offsets
756 rowoffset = math_max(rowoffset, frameoffset)
757 rowheight = math_max(rowheight, rowoffset + (frameheight / 2))
759 frame:SetPoint("TOPLEFT", children[i-1].frame, "TOPRIGHT", 0, frameoffset - lastframeoffset)
760 usedwidth = framewidth + usedwidth
764 if child.width == "fill" then
765 child:SetWidth(width)
766 frame:SetPoint("RIGHT", content)
770 rowstartoffset = frameoffset
772 if child.DoLayout then
775 rowheight = frame.height or frame:GetHeight() or 0
776 rowoffset = child.alignoffset or (rowheight / 2)
777 rowstartoffset = rowoffset
778 elseif child.width == "relative" then
779 child:SetWidth(width * child.relWidth)
781 if child.DoLayout then
786 frame:SetPoint("RIGHT", content)
790 if child.height == "fill" then
791 frame:SetPoint("BOTTOM", content)
796 --anchor the last row, if its full height needs a special case since its height has just been changed by the anchor
798 rowstart:SetPoint("TOPLEFT", content, "TOPLEFT", 0, -height)
800 rowstart:SetPoint("TOPLEFT", content, "TOPLEFT", 0, -(height + (rowoffset - rowstartoffset) + 3))
803 height = height + rowheight + 3
804 safecall(content.obj.LayoutFinished, content.obj, nil, height)