tercio@0: --[[----------------------------------------------------------------------------- tercio@0: TabGroup Container tercio@0: Container that uses tabs on top to switch between groups. tercio@0: -------------------------------------------------------------------------------]] tercio@0: local Type, Version = "TabGroup", 35 tercio@0: local AceGUI = LibStub and LibStub("AceGUI-3.0", true) tercio@0: if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end tercio@0: tercio@0: -- Lua APIs tercio@0: local pairs, ipairs, assert, type, wipe = pairs, ipairs, assert, type, wipe tercio@0: tercio@0: -- WoW APIs tercio@0: local PlaySound = PlaySound tercio@0: local CreateFrame, UIParent = CreateFrame, UIParent tercio@0: local _G = _G tercio@0: tercio@0: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded tercio@0: -- List them here for Mikk's FindGlobals script tercio@0: -- GLOBALS: PanelTemplates_TabResize, PanelTemplates_SetDisabledTabState, PanelTemplates_SelectTab, PanelTemplates_DeselectTab tercio@0: tercio@0: -- local upvalue storage used by BuildTabs tercio@0: local widths = {} tercio@0: local rowwidths = {} tercio@0: local rowends = {} tercio@0: tercio@0: --[[----------------------------------------------------------------------------- tercio@0: Support functions tercio@0: -------------------------------------------------------------------------------]] tercio@0: local function UpdateTabLook(frame) tercio@0: if frame.disabled then tercio@0: PanelTemplates_SetDisabledTabState(frame) tercio@0: elseif frame.selected then tercio@0: PanelTemplates_SelectTab(frame) tercio@0: else tercio@0: PanelTemplates_DeselectTab(frame) tercio@0: end tercio@0: end tercio@0: tercio@0: local function Tab_SetText(frame, text) tercio@0: frame:_SetText(text) tercio@0: local width = frame.obj.frame.width or frame.obj.frame:GetWidth() or 0 tercio@0: PanelTemplates_TabResize(frame, 0, nil, nil, width, frame:GetFontString():GetStringWidth()) tercio@0: end tercio@0: tercio@0: local function Tab_SetSelected(frame, selected) tercio@0: frame.selected = selected tercio@0: UpdateTabLook(frame) tercio@0: end tercio@0: tercio@0: local function Tab_SetDisabled(frame, disabled) tercio@0: frame.disabled = disabled tercio@0: UpdateTabLook(frame) tercio@0: end tercio@0: tercio@0: local function BuildTabsOnUpdate(frame) tercio@0: local self = frame.obj tercio@0: self:BuildTabs() tercio@0: frame:SetScript("OnUpdate", nil) tercio@0: end tercio@0: tercio@0: --[[----------------------------------------------------------------------------- tercio@0: Scripts tercio@0: -------------------------------------------------------------------------------]] tercio@0: local function Tab_OnClick(frame) tercio@0: if not (frame.selected or frame.disabled) then tercio@0: PlaySound("igCharacterInfoTab") tercio@0: frame.obj:SelectTab(frame.value) tercio@0: end tercio@0: end tercio@0: tercio@0: local function Tab_OnEnter(frame) tercio@0: local self = frame.obj tercio@0: self:Fire("OnTabEnter", self.tabs[frame.id].value, frame) tercio@0: end tercio@0: tercio@0: local function Tab_OnLeave(frame) tercio@0: local self = frame.obj tercio@0: self:Fire("OnTabLeave", self.tabs[frame.id].value, frame) tercio@0: end tercio@0: tercio@0: local function Tab_OnShow(frame) tercio@0: _G[frame:GetName().."HighlightTexture"]:SetWidth(frame:GetTextWidth() + 30) tercio@0: end tercio@0: tercio@0: --[[----------------------------------------------------------------------------- tercio@0: Methods tercio@0: -------------------------------------------------------------------------------]] tercio@0: local methods = { tercio@0: ["OnAcquire"] = function(self) tercio@0: self:SetTitle() tercio@0: end, tercio@0: tercio@0: ["OnRelease"] = function(self) tercio@0: self.status = nil tercio@0: for k in pairs(self.localstatus) do tercio@0: self.localstatus[k] = nil tercio@0: end tercio@0: self.tablist = nil tercio@0: for _, tab in pairs(self.tabs) do tercio@0: tab:Hide() tercio@0: end tercio@0: end, tercio@0: tercio@0: ["CreateTab"] = function(self, id) tercio@0: local tabname = ("AceGUITabGroup%dTab%d"):format(self.num, id) tercio@0: local tab = CreateFrame("Button", tabname, self.border, "OptionsFrameTabButtonTemplate") tercio@0: tab.obj = self tercio@0: tab.id = id tercio@0: tercio@0: tab.text = _G[tabname .. "Text"] tercio@0: tab.text:ClearAllPoints() tercio@0: tab.text:SetPoint("LEFT", 14, -3) tercio@0: tab.text:SetPoint("RIGHT", -12, -3) tercio@0: tercio@0: tab:SetScript("OnClick", Tab_OnClick) tercio@0: tab:SetScript("OnEnter", Tab_OnEnter) tercio@0: tab:SetScript("OnLeave", Tab_OnLeave) tercio@0: tab:SetScript("OnShow", Tab_OnShow) tercio@0: tercio@0: tab._SetText = tab.SetText tercio@0: tab.SetText = Tab_SetText tercio@0: tab.SetSelected = Tab_SetSelected tercio@0: tab.SetDisabled = Tab_SetDisabled tercio@0: tercio@0: return tab tercio@0: end, tercio@0: tercio@0: ["SetTitle"] = function(self, text) tercio@0: self.titletext:SetText(text or "") tercio@0: if text and text ~= "" then tercio@0: self.alignoffset = 25 tercio@0: else tercio@0: self.alignoffset = 18 tercio@0: end tercio@0: self:BuildTabs() tercio@0: end, tercio@0: tercio@0: ["SetStatusTable"] = function(self, status) tercio@0: assert(type(status) == "table") tercio@0: self.status = status tercio@0: end, tercio@0: tercio@0: ["SelectTab"] = function(self, value) tercio@0: local status = self.status or self.localstatus tercio@0: local found tercio@0: for i, v in ipairs(self.tabs) do tercio@0: if v.value == value then tercio@0: v:SetSelected(true) tercio@0: found = true tercio@0: else tercio@0: v:SetSelected(false) tercio@0: end tercio@0: end tercio@0: status.selected = value tercio@0: if found then tercio@0: self:Fire("OnGroupSelected",value) tercio@0: end tercio@0: end, tercio@0: tercio@0: ["SetTabs"] = function(self, tabs) tercio@0: self.tablist = tabs tercio@0: self:BuildTabs() tercio@0: end, tercio@0: tercio@0: tercio@0: ["BuildTabs"] = function(self) tercio@0: local hastitle = (self.titletext:GetText() and self.titletext:GetText() ~= "") tercio@0: local status = self.status or self.localstatus tercio@0: local tablist = self.tablist tercio@0: local tabs = self.tabs tercio@0: tercio@0: if not tablist then return end tercio@0: tercio@0: local width = self.frame.width or self.frame:GetWidth() or 0 tercio@0: tercio@0: wipe(widths) tercio@0: wipe(rowwidths) tercio@0: wipe(rowends) tercio@0: tercio@0: --Place Text into tabs and get thier initial width tercio@0: for i, v in ipairs(tablist) do tercio@0: local tab = tabs[i] tercio@0: if not tab then tercio@0: tab = self:CreateTab(i) tercio@0: tabs[i] = tab tercio@0: end tercio@0: tercio@0: tab:Show() tercio@0: tab:SetText(v.text) tercio@0: tab:SetDisabled(v.disabled) tercio@0: tab.value = v.value tercio@0: tercio@0: 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 tercio@0: end tercio@0: tercio@0: for i = (#tablist)+1, #tabs, 1 do tercio@0: tabs[i]:Hide() tercio@0: end tercio@0: tercio@0: --First pass, find the minimum number of rows needed to hold all tabs and the initial tab layout tercio@0: local numtabs = #tablist tercio@0: local numrows = 1 tercio@0: local usedwidth = 0 tercio@0: tercio@0: for i = 1, #tablist do tercio@0: --If this is not the first tab of a row and there isn't room for it tercio@0: if usedwidth ~= 0 and (width - usedwidth - widths[i]) < 0 then tercio@0: rowwidths[numrows] = usedwidth + 10 --first tab in each row takes up an extra 10px tercio@0: rowends[numrows] = i - 1 tercio@0: numrows = numrows + 1 tercio@0: usedwidth = 0 tercio@0: end tercio@0: usedwidth = usedwidth + widths[i] tercio@0: end tercio@0: rowwidths[numrows] = usedwidth + 10 --first tab in each row takes up an extra 10px tercio@0: rowends[numrows] = #tablist tercio@0: tercio@0: --Fix for single tabs being left on the last row, move a tab from the row above if applicable tercio@0: if numrows > 1 then tercio@0: --if the last row has only one tab tercio@0: if rowends[numrows-1] == numtabs-1 then tercio@0: --if there are more than 2 tabs in the 2nd last row tercio@0: if (numrows == 2 and rowends[numrows-1] > 2) or (rowends[numrows] - rowends[numrows-1] > 2) then tercio@0: --move 1 tab from the second last row to the last, if there is enough space tercio@0: if (rowwidths[numrows] + widths[numtabs-1]) <= width then tercio@0: rowends[numrows-1] = rowends[numrows-1] - 1 tercio@0: rowwidths[numrows] = rowwidths[numrows] + widths[numtabs-1] tercio@0: rowwidths[numrows-1] = rowwidths[numrows-1] - widths[numtabs-1] tercio@0: end tercio@0: end tercio@0: end tercio@0: end tercio@0: tercio@0: --anchor the rows as defined and resize tabs to fill thier row tercio@0: local starttab = 1 tercio@0: for row, endtab in ipairs(rowends) do tercio@0: local first = true tercio@0: for tabno = starttab, endtab do tercio@0: local tab = tabs[tabno] tercio@0: tab:ClearAllPoints() tercio@0: if first then tercio@0: tab:SetPoint("TOPLEFT", self.frame, "TOPLEFT", 0, -(hastitle and 14 or 7)-(row-1)*20 ) tercio@0: first = false tercio@0: else tercio@0: tab:SetPoint("LEFT", tabs[tabno-1], "RIGHT", -10, 0) tercio@0: end tercio@0: end tercio@0: tercio@0: -- equal padding for each tab to fill the available width, tercio@0: -- if the used space is above 75% already tercio@0: -- the 18 pixel is the typical width of a scrollbar, so we can have a tab group inside a scrolling frame, tercio@0: -- and not have the tabs jump around funny when switching between tabs that need scrolling and those that don't tercio@0: local padding = 0 tercio@0: if not (numrows == 1 and rowwidths[1] < width*0.75 - 18) then tercio@0: padding = (width - rowwidths[row]) / (endtab - starttab+1) tercio@0: end tercio@0: tercio@0: for i = starttab, endtab do tercio@0: PanelTemplates_TabResize(tabs[i], padding + 4, nil, nil, width, tabs[i]:GetFontString():GetStringWidth()) tercio@0: end tercio@0: starttab = endtab + 1 tercio@0: end tercio@0: tercio@0: self.borderoffset = (hastitle and 17 or 10)+((numrows)*20) tercio@0: self.border:SetPoint("TOPLEFT", 1, -self.borderoffset) tercio@0: end, tercio@0: tercio@0: ["OnWidthSet"] = function(self, width) tercio@0: local content = self.content tercio@0: local contentwidth = width - 60 tercio@0: if contentwidth < 0 then tercio@0: contentwidth = 0 tercio@0: end tercio@0: content:SetWidth(contentwidth) tercio@0: content.width = contentwidth tercio@0: self:BuildTabs(self) tercio@0: self.frame:SetScript("OnUpdate", BuildTabsOnUpdate) tercio@0: end, tercio@0: tercio@0: ["OnHeightSet"] = function(self, height) tercio@0: local content = self.content tercio@0: local contentheight = height - (self.borderoffset + 23) tercio@0: if contentheight < 0 then tercio@0: contentheight = 0 tercio@0: end tercio@0: content:SetHeight(contentheight) tercio@0: content.height = contentheight tercio@0: end, tercio@0: tercio@0: ["LayoutFinished"] = function(self, width, height) tercio@0: if self.noAutoHeight then return end tercio@0: self:SetHeight((height or 0) + (self.borderoffset + 23)) tercio@0: end tercio@0: } tercio@0: tercio@0: --[[----------------------------------------------------------------------------- tercio@0: Constructor tercio@0: -------------------------------------------------------------------------------]] tercio@0: local PaneBackdrop = { tercio@0: bgFile = "Interface\\ChatFrame\\ChatFrameBackground", tercio@0: edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", tercio@0: tile = true, tileSize = 16, edgeSize = 16, tercio@0: insets = { left = 3, right = 3, top = 5, bottom = 3 } tercio@0: } tercio@0: tercio@0: local function Constructor() tercio@0: local num = AceGUI:GetNextWidgetNum(Type) tercio@0: local frame = CreateFrame("Frame",nil,UIParent) tercio@0: frame:SetHeight(100) tercio@0: frame:SetWidth(100) tercio@0: frame:SetFrameStrata("FULLSCREEN_DIALOG") tercio@0: tercio@0: local titletext = frame:CreateFontString(nil,"OVERLAY","GameFontNormal") tercio@0: titletext:SetPoint("TOPLEFT", 14, 0) tercio@0: titletext:SetPoint("TOPRIGHT", -14, 0) tercio@0: titletext:SetJustifyH("LEFT") tercio@0: titletext:SetHeight(18) tercio@0: titletext:SetText("") tercio@0: tercio@0: local border = CreateFrame("Frame", nil, frame) tercio@0: border:SetPoint("TOPLEFT", 1, -27) tercio@0: border:SetPoint("BOTTOMRIGHT", -1, 3) tercio@0: border:SetBackdrop(PaneBackdrop) tercio@0: border:SetBackdropColor(0.1, 0.1, 0.1, 0.5) tercio@0: border:SetBackdropBorderColor(0.4, 0.4, 0.4) tercio@0: tercio@0: local content = CreateFrame("Frame", nil, border) tercio@0: content:SetPoint("TOPLEFT", 10, -7) tercio@0: content:SetPoint("BOTTOMRIGHT", -10, 7) tercio@0: tercio@0: local widget = { tercio@0: num = num, tercio@0: frame = frame, tercio@0: localstatus = {}, tercio@0: alignoffset = 18, tercio@0: titletext = titletext, tercio@0: border = border, tercio@0: borderoffset = 27, tercio@0: tabs = {}, tercio@0: content = content, tercio@0: type = Type tercio@0: } tercio@0: for method, func in pairs(methods) do tercio@0: widget[method] = func tercio@0: end tercio@0: tercio@0: return AceGUI:RegisterAsContainer(widget) tercio@0: end tercio@0: tercio@0: AceGUI:RegisterWidgetType(Type, Constructor, Version)