1 --[[-----------------------------------------------------------------------------
3 Container that uses tabs on top to switch between groups.
4 -------------------------------------------------------------------------------]]
5 local Type, Version = "TabGroup", 35
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 pairs, ipairs, assert, type, wipe = pairs, ipairs, assert, type, wipe
13 local PlaySound = PlaySound
14 local CreateFrame, UIParent = CreateFrame, UIParent
17 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
18 -- List them here for Mikk's FindGlobals script
19 -- GLOBALS: PanelTemplates_TabResize, PanelTemplates_SetDisabledTabState, PanelTemplates_SelectTab, PanelTemplates_DeselectTab
21 -- local upvalue storage used by BuildTabs
26 --[[-----------------------------------------------------------------------------
28 -------------------------------------------------------------------------------]]
29 local function UpdateTabLook(frame)
30 if frame.disabled then
31 PanelTemplates_SetDisabledTabState(frame)
32 elseif frame.selected then
33 PanelTemplates_SelectTab(frame)
35 PanelTemplates_DeselectTab(frame)
39 local function Tab_SetText(frame, text)
41 local width = frame.obj.frame.width or frame.obj.frame:GetWidth() or 0
42 PanelTemplates_TabResize(frame, 0, nil, nil, width, frame:GetFontString():GetStringWidth())
45 local function Tab_SetSelected(frame, selected)
46 frame.selected = selected
50 local function Tab_SetDisabled(frame, disabled)
51 frame.disabled = disabled
55 local function BuildTabsOnUpdate(frame)
56 local self = frame.obj
58 frame:SetScript("OnUpdate", nil)
61 --[[-----------------------------------------------------------------------------
63 -------------------------------------------------------------------------------]]
64 local function Tab_OnClick(frame)
65 if not (frame.selected or frame.disabled) then
66 PlaySound("igCharacterInfoTab")
67 frame.obj:SelectTab(frame.value)
71 local function Tab_OnEnter(frame)
72 local self = frame.obj
73 self:Fire("OnTabEnter", self.tabs[frame.id].value, frame)
76 local function Tab_OnLeave(frame)
77 local self = frame.obj
78 self:Fire("OnTabLeave", self.tabs[frame.id].value, frame)
81 local function Tab_OnShow(frame)
82 _G[frame:GetName().."HighlightTexture"]:SetWidth(frame:GetTextWidth() + 30)
85 --[[-----------------------------------------------------------------------------
87 -------------------------------------------------------------------------------]]
89 ["OnAcquire"] = function(self)
93 ["OnRelease"] = function(self)
95 for k in pairs(self.localstatus) do
96 self.localstatus[k] = nil
99 for _, tab in pairs(self.tabs) do
104 ["CreateTab"] = function(self, id)
105 local tabname = ("AceGUITabGroup%dTab%d"):format(self.num, id)
106 local tab = CreateFrame("Button", tabname, self.border, "OptionsFrameTabButtonTemplate")
110 tab.text = _G[tabname .. "Text"]
111 tab.text:ClearAllPoints()
112 tab.text:SetPoint("LEFT", 14, -3)
113 tab.text:SetPoint("RIGHT", -12, -3)
115 tab:SetScript("OnClick", Tab_OnClick)
116 tab:SetScript("OnEnter", Tab_OnEnter)
117 tab:SetScript("OnLeave", Tab_OnLeave)
118 tab:SetScript("OnShow", Tab_OnShow)
120 tab._SetText = tab.SetText
121 tab.SetText = Tab_SetText
122 tab.SetSelected = Tab_SetSelected
123 tab.SetDisabled = Tab_SetDisabled
128 ["SetTitle"] = function(self, text)
129 self.titletext:SetText(text or "")
130 if text and text ~= "" then
131 self.alignoffset = 25
133 self.alignoffset = 18
138 ["SetStatusTable"] = function(self, status)
139 assert(type(status) == "table")
143 ["SelectTab"] = function(self, value)
144 local status = self.status or self.localstatus
146 for i, v in ipairs(self.tabs) do
147 if v.value == value then
154 status.selected = value
156 self:Fire("OnGroupSelected",value)
160 ["SetTabs"] = function(self, tabs)
166 ["BuildTabs"] = function(self)
167 local hastitle = (self.titletext:GetText() and self.titletext:GetText() ~= "")
168 local status = self.status or self.localstatus
169 local tablist = self.tablist
170 local tabs = self.tabs
172 if not tablist then return end
174 local width = self.frame.width or self.frame:GetWidth() or 0
180 --Place Text into tabs and get thier initial width
181 for i, v in ipairs(tablist) do
184 tab = self:CreateTab(i)
190 tab:SetDisabled(v.disabled)
193 widths[i] = tab:GetWidth() - 6 --tabs are anchored 10 pixels from the right side of the previous one to reduce spacing, but add a fixed 4px padding for the text
196 for i = (#tablist)+1, #tabs, 1 do
200 --First pass, find the minimum number of rows needed to hold all tabs and the initial tab layout
201 local numtabs = #tablist
205 for i = 1, #tablist do
206 --If this is not the first tab of a row and there isn't room for it
207 if usedwidth ~= 0 and (width - usedwidth - widths[i]) < 0 then
208 rowwidths[numrows] = usedwidth + 10 --first tab in each row takes up an extra 10px
209 rowends[numrows] = i - 1
210 numrows = numrows + 1
213 usedwidth = usedwidth + widths[i]
215 rowwidths[numrows] = usedwidth + 10 --first tab in each row takes up an extra 10px
216 rowends[numrows] = #tablist
218 --Fix for single tabs being left on the last row, move a tab from the row above if applicable
220 --if the last row has only one tab
221 if rowends[numrows-1] == numtabs-1 then
222 --if there are more than 2 tabs in the 2nd last row
223 if (numrows == 2 and rowends[numrows-1] > 2) or (rowends[numrows] - rowends[numrows-1] > 2) then
224 --move 1 tab from the second last row to the last, if there is enough space
225 if (rowwidths[numrows] + widths[numtabs-1]) <= width then
226 rowends[numrows-1] = rowends[numrows-1] - 1
227 rowwidths[numrows] = rowwidths[numrows] + widths[numtabs-1]
228 rowwidths[numrows-1] = rowwidths[numrows-1] - widths[numtabs-1]
234 --anchor the rows as defined and resize tabs to fill thier row
236 for row, endtab in ipairs(rowends) do
238 for tabno = starttab, endtab do
239 local tab = tabs[tabno]
242 tab:SetPoint("TOPLEFT", self.frame, "TOPLEFT", 0, -(hastitle and 14 or 7)-(row-1)*20 )
245 tab:SetPoint("LEFT", tabs[tabno-1], "RIGHT", -10, 0)
249 -- equal padding for each tab to fill the available width,
250 -- if the used space is above 75% already
251 -- the 18 pixel is the typical width of a scrollbar, so we can have a tab group inside a scrolling frame,
252 -- and not have the tabs jump around funny when switching between tabs that need scrolling and those that don't
254 if not (numrows == 1 and rowwidths[1] < width*0.75 - 18) then
255 padding = (width - rowwidths[row]) / (endtab - starttab+1)
258 for i = starttab, endtab do
259 PanelTemplates_TabResize(tabs[i], padding + 4, nil, nil, width, tabs[i]:GetFontString():GetStringWidth())
261 starttab = endtab + 1
264 self.borderoffset = (hastitle and 17 or 10)+((numrows)*20)
265 self.border:SetPoint("TOPLEFT", 1, -self.borderoffset)
268 ["OnWidthSet"] = function(self, width)
269 local content = self.content
270 local contentwidth = width - 60
271 if contentwidth < 0 then
274 content:SetWidth(contentwidth)
275 content.width = contentwidth
277 self.frame:SetScript("OnUpdate", BuildTabsOnUpdate)
280 ["OnHeightSet"] = function(self, height)
281 local content = self.content
282 local contentheight = height - (self.borderoffset + 23)
283 if contentheight < 0 then
286 content:SetHeight(contentheight)
287 content.height = contentheight
290 ["LayoutFinished"] = function(self, width, height)
291 if self.noAutoHeight then return end
292 self:SetHeight((height or 0) + (self.borderoffset + 23))
296 --[[-----------------------------------------------------------------------------
298 -------------------------------------------------------------------------------]]
299 local PaneBackdrop = {
300 bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
301 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
302 tile = true, tileSize = 16, edgeSize = 16,
303 insets = { left = 3, right = 3, top = 5, bottom = 3 }
306 local function Constructor()
307 local num = AceGUI:GetNextWidgetNum(Type)
308 local frame = CreateFrame("Frame",nil,UIParent)
311 frame:SetFrameStrata("FULLSCREEN_DIALOG")
313 local titletext = frame:CreateFontString(nil,"OVERLAY","GameFontNormal")
314 titletext:SetPoint("TOPLEFT", 14, 0)
315 titletext:SetPoint("TOPRIGHT", -14, 0)
316 titletext:SetJustifyH("LEFT")
317 titletext:SetHeight(18)
318 titletext:SetText("")
320 local border = CreateFrame("Frame", nil, frame)
321 border:SetPoint("TOPLEFT", 1, -27)
322 border:SetPoint("BOTTOMRIGHT", -1, 3)
323 border:SetBackdrop(PaneBackdrop)
324 border:SetBackdropColor(0.1, 0.1, 0.1, 0.5)
325 border:SetBackdropBorderColor(0.4, 0.4, 0.4)
327 local content = CreateFrame("Frame", nil, border)
328 content:SetPoint("TOPLEFT", 10, -7)
329 content:SetPoint("BOTTOMRIGHT", -10, 7)
336 titletext = titletext,
343 for method, func in pairs(methods) do
344 widget[method] = func
347 return AceGUI:RegisterAsContainer(widget)
350 AceGUI:RegisterWidgetType(Type, Constructor, Version)