yellowfive@57: --[[ $Id: AceGUIWidget-DropDown-Items.lua 996 2010-12-01 18:34:17Z nevcairiel $ ]]-- yellowfive@57: yellowfive@57: local AceGUI = LibStub("AceGUI-3.0") yellowfive@57: yellowfive@57: -- Lua APIs yellowfive@57: local select, assert = select, assert yellowfive@57: yellowfive@57: -- WoW APIs yellowfive@57: local PlaySound = PlaySound yellowfive@57: local CreateFrame = CreateFrame yellowfive@57: yellowfive@57: local function fixlevels(parent,...) yellowfive@57: local i = 1 yellowfive@57: local child = select(i, ...) yellowfive@57: while child do yellowfive@57: child:SetFrameLevel(parent:GetFrameLevel()+1) yellowfive@57: fixlevels(child, child:GetChildren()) yellowfive@57: i = i + 1 yellowfive@57: child = select(i, ...) yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: local function fixstrata(strata, parent, ...) yellowfive@57: local i = 1 yellowfive@57: local child = select(i, ...) yellowfive@57: parent:SetFrameStrata(strata) yellowfive@57: while child do yellowfive@57: fixstrata(strata, child, child:GetChildren()) yellowfive@57: i = i + 1 yellowfive@57: child = select(i, ...) yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: -- ItemBase is the base "class" for all dropdown items. yellowfive@57: -- Each item has to use ItemBase.Create(widgetType) to yellowfive@57: -- create an initial 'self' value. yellowfive@57: -- ItemBase will add common functions and ui event handlers. yellowfive@57: -- Be sure to keep basic usage when you override functions. yellowfive@57: yellowfive@57: local ItemBase = { yellowfive@57: -- NOTE: The ItemBase version is added to each item's version number yellowfive@57: -- to ensure proper updates on ItemBase changes. yellowfive@57: -- Use at least 1000er steps. yellowfive@57: version = 1000, yellowfive@57: counter = 0, yellowfive@57: } yellowfive@57: yellowfive@57: function ItemBase.Frame_OnEnter(this) yellowfive@57: local self = this.obj yellowfive@57: yellowfive@57: if self.useHighlight then yellowfive@57: self.highlight:Show() yellowfive@57: end yellowfive@57: self:Fire("OnEnter") yellowfive@57: yellowfive@57: if self.specialOnEnter then yellowfive@57: self.specialOnEnter(self) yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: function ItemBase.Frame_OnLeave(this) yellowfive@57: local self = this.obj yellowfive@57: yellowfive@57: self.highlight:Hide() yellowfive@57: self:Fire("OnLeave") yellowfive@57: yellowfive@57: if self.specialOnLeave then yellowfive@57: self.specialOnLeave(self) yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: -- exported, AceGUI callback yellowfive@57: function ItemBase.OnAcquire(self) yellowfive@57: self.frame:SetToplevel(true) yellowfive@57: self.frame:SetFrameStrata("FULLSCREEN_DIALOG") yellowfive@57: end yellowfive@57: yellowfive@57: -- exported, AceGUI callback yellowfive@57: function ItemBase.OnRelease(self) yellowfive@57: self:SetDisabled(false) yellowfive@57: self.pullout = nil yellowfive@57: self.frame:SetParent(nil) yellowfive@57: self.frame:ClearAllPoints() yellowfive@57: self.frame:Hide() yellowfive@57: end yellowfive@57: yellowfive@57: -- exported yellowfive@57: -- NOTE: this is called by a Dropdown-Pullout. yellowfive@57: -- Do not call this method directly yellowfive@57: function ItemBase.SetPullout(self, pullout) yellowfive@57: self.pullout = pullout yellowfive@57: yellowfive@57: self.frame:SetParent(nil) yellowfive@57: self.frame:SetParent(pullout.itemFrame) yellowfive@57: self.parent = pullout.itemFrame yellowfive@57: fixlevels(pullout.itemFrame, pullout.itemFrame:GetChildren()) yellowfive@57: end yellowfive@57: yellowfive@57: -- exported yellowfive@57: function ItemBase.SetText(self, text) yellowfive@57: self.text:SetText(text or "") yellowfive@57: end yellowfive@57: yellowfive@57: -- exported yellowfive@57: function ItemBase.GetText(self) yellowfive@57: return self.text:GetText() yellowfive@57: end yellowfive@57: yellowfive@57: -- exported yellowfive@57: function ItemBase.SetPoint(self, ...) yellowfive@57: self.frame:SetPoint(...) yellowfive@57: end yellowfive@57: yellowfive@57: -- exported yellowfive@57: function ItemBase.Show(self) yellowfive@57: self.frame:Show() yellowfive@57: end yellowfive@57: yellowfive@57: -- exported yellowfive@57: function ItemBase.Hide(self) yellowfive@57: self.frame:Hide() yellowfive@57: end yellowfive@57: yellowfive@57: -- exported yellowfive@57: function ItemBase.SetDisabled(self, disabled) yellowfive@57: self.disabled = disabled yellowfive@57: if disabled then yellowfive@57: self.useHighlight = false yellowfive@57: self.text:SetTextColor(.5, .5, .5) yellowfive@57: else yellowfive@57: self.useHighlight = true yellowfive@57: self.text:SetTextColor(1, 1, 1) yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: -- exported yellowfive@57: -- NOTE: this is called by a Dropdown-Pullout. yellowfive@57: -- Do not call this method directly yellowfive@57: function ItemBase.SetOnLeave(self, func) yellowfive@57: self.specialOnLeave = func yellowfive@57: end yellowfive@57: yellowfive@57: -- exported yellowfive@57: -- NOTE: this is called by a Dropdown-Pullout. yellowfive@57: -- Do not call this method directly yellowfive@57: function ItemBase.SetOnEnter(self, func) yellowfive@57: self.specialOnEnter = func yellowfive@57: end yellowfive@57: yellowfive@57: function ItemBase.Create(type) yellowfive@57: -- NOTE: Most of the following code is copied from AceGUI-3.0/Dropdown widget yellowfive@57: local count = AceGUI:GetNextWidgetNum(type) yellowfive@57: local frame = CreateFrame("Button", "AceGUI30DropDownItem"..count) yellowfive@57: local self = {} yellowfive@57: self.frame = frame yellowfive@57: frame.obj = self yellowfive@57: self.type = type yellowfive@57: yellowfive@57: self.useHighlight = true yellowfive@57: yellowfive@57: frame:SetHeight(17) yellowfive@57: frame:SetFrameStrata("FULLSCREEN_DIALOG") yellowfive@57: yellowfive@57: local text = frame:CreateFontString(nil,"OVERLAY","GameFontNormalSmall") yellowfive@57: text:SetTextColor(1,1,1) yellowfive@57: text:SetJustifyH("LEFT") yellowfive@57: text:SetPoint("TOPLEFT",frame,"TOPLEFT",18,0) yellowfive@57: text:SetPoint("BOTTOMRIGHT",frame,"BOTTOMRIGHT",-8,0) yellowfive@57: self.text = text yellowfive@57: yellowfive@57: local highlight = frame:CreateTexture(nil, "OVERLAY") yellowfive@57: highlight:SetTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight") yellowfive@57: highlight:SetBlendMode("ADD") yellowfive@57: highlight:SetHeight(14) yellowfive@57: highlight:ClearAllPoints() yellowfive@57: highlight:SetPoint("RIGHT",frame,"RIGHT",-3,0) yellowfive@57: highlight:SetPoint("LEFT",frame,"LEFT",5,0) yellowfive@57: highlight:Hide() yellowfive@57: self.highlight = highlight yellowfive@57: yellowfive@57: local check = frame:CreateTexture("OVERLAY") yellowfive@57: check:SetWidth(16) yellowfive@57: check:SetHeight(16) yellowfive@57: check:SetPoint("LEFT",frame,"LEFT",3,-1) yellowfive@57: check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check") yellowfive@57: check:Hide() yellowfive@57: self.check = check yellowfive@57: yellowfive@57: local sub = frame:CreateTexture("OVERLAY") yellowfive@57: sub:SetWidth(16) yellowfive@57: sub:SetHeight(16) yellowfive@57: sub:SetPoint("RIGHT",frame,"RIGHT",-3,-1) yellowfive@57: sub:SetTexture("Interface\\ChatFrame\\ChatFrameExpandArrow") yellowfive@57: sub:Hide() yellowfive@57: self.sub = sub yellowfive@57: yellowfive@57: frame:SetScript("OnEnter", ItemBase.Frame_OnEnter) yellowfive@57: frame:SetScript("OnLeave", ItemBase.Frame_OnLeave) yellowfive@57: yellowfive@57: self.OnAcquire = ItemBase.OnAcquire yellowfive@57: self.OnRelease = ItemBase.OnRelease yellowfive@57: yellowfive@57: self.SetPullout = ItemBase.SetPullout yellowfive@57: self.GetText = ItemBase.GetText yellowfive@57: self.SetText = ItemBase.SetText yellowfive@57: self.SetDisabled = ItemBase.SetDisabled yellowfive@57: yellowfive@57: self.SetPoint = ItemBase.SetPoint yellowfive@57: self.Show = ItemBase.Show yellowfive@57: self.Hide = ItemBase.Hide yellowfive@57: yellowfive@57: self.SetOnLeave = ItemBase.SetOnLeave yellowfive@57: self.SetOnEnter = ItemBase.SetOnEnter yellowfive@57: yellowfive@57: return self yellowfive@57: end yellowfive@57: yellowfive@57: -- Register a dummy LibStub library to retrieve the ItemBase, so other addons can use it. yellowfive@57: local IBLib = LibStub:NewLibrary("AceGUI-3.0-DropDown-ItemBase", ItemBase.version) yellowfive@57: if IBLib then yellowfive@57: IBLib.GetItemBase = function() return ItemBase end yellowfive@57: end yellowfive@57: yellowfive@57: --[[ yellowfive@57: Template for items: yellowfive@57: yellowfive@57: -- Item: yellowfive@57: -- yellowfive@57: do yellowfive@57: local widgetType = "Dropdown-Item-" yellowfive@57: local widgetVersion = 1 yellowfive@57: yellowfive@57: local function Constructor() yellowfive@57: local self = ItemBase.Create(widgetType) yellowfive@57: yellowfive@57: AceGUI:RegisterAsWidget(self) yellowfive@57: return self yellowfive@57: end yellowfive@57: yellowfive@57: AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version) yellowfive@57: end yellowfive@57: --]] yellowfive@57: yellowfive@57: -- Item: Header yellowfive@57: -- A single text entry. yellowfive@57: -- Special: Different text color and no highlight yellowfive@57: do yellowfive@57: local widgetType = "Dropdown-Item-Header" yellowfive@57: local widgetVersion = 1 yellowfive@57: yellowfive@57: local function OnEnter(this) yellowfive@57: local self = this.obj yellowfive@57: self:Fire("OnEnter") yellowfive@57: yellowfive@57: if self.specialOnEnter then yellowfive@57: self.specialOnEnter(self) yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: local function OnLeave(this) yellowfive@57: local self = this.obj yellowfive@57: self:Fire("OnLeave") yellowfive@57: yellowfive@57: if self.specialOnLeave then yellowfive@57: self.specialOnLeave(self) yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: -- exported, override yellowfive@57: local function SetDisabled(self, disabled) yellowfive@57: ItemBase.SetDisabled(self, disabled) yellowfive@57: if not disabled then yellowfive@57: self.text:SetTextColor(1, 1, 0) yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: local function Constructor() yellowfive@57: local self = ItemBase.Create(widgetType) yellowfive@57: yellowfive@57: self.SetDisabled = SetDisabled yellowfive@57: yellowfive@57: self.frame:SetScript("OnEnter", OnEnter) yellowfive@57: self.frame:SetScript("OnLeave", OnLeave) yellowfive@57: yellowfive@57: self.text:SetTextColor(1, 1, 0) yellowfive@57: yellowfive@57: AceGUI:RegisterAsWidget(self) yellowfive@57: return self yellowfive@57: end yellowfive@57: yellowfive@57: AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version) yellowfive@57: end yellowfive@57: yellowfive@57: -- Item: Execute yellowfive@57: -- A simple button yellowfive@57: do yellowfive@57: local widgetType = "Dropdown-Item-Execute" yellowfive@57: local widgetVersion = 1 yellowfive@57: yellowfive@57: local function Frame_OnClick(this, button) yellowfive@57: local self = this.obj yellowfive@57: if self.disabled then return end yellowfive@57: self:Fire("OnClick") yellowfive@57: if self.pullout then yellowfive@57: self.pullout:Close() yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: local function Constructor() yellowfive@57: local self = ItemBase.Create(widgetType) yellowfive@57: yellowfive@57: self.frame:SetScript("OnClick", Frame_OnClick) yellowfive@57: yellowfive@57: AceGUI:RegisterAsWidget(self) yellowfive@57: return self yellowfive@57: end yellowfive@57: yellowfive@57: AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version) yellowfive@57: end yellowfive@57: yellowfive@57: -- Item: Toggle yellowfive@57: -- Some sort of checkbox for dropdown menus. yellowfive@57: -- Does not close the pullout on click. yellowfive@57: do yellowfive@57: local widgetType = "Dropdown-Item-Toggle" yellowfive@57: local widgetVersion = 3 yellowfive@57: yellowfive@57: local function UpdateToggle(self) yellowfive@57: if self.value then yellowfive@57: self.check:Show() yellowfive@57: else yellowfive@57: self.check:Hide() yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: local function OnRelease(self) yellowfive@57: ItemBase.OnRelease(self) yellowfive@57: self:SetValue(nil) yellowfive@57: end yellowfive@57: yellowfive@57: local function Frame_OnClick(this, button) yellowfive@57: local self = this.obj yellowfive@57: if self.disabled then return end yellowfive@57: self.value = not self.value yellowfive@57: if self.value then yellowfive@57: PlaySound("igMainMenuOptionCheckBoxOn") yellowfive@57: else yellowfive@57: PlaySound("igMainMenuOptionCheckBoxOff") yellowfive@57: end yellowfive@57: UpdateToggle(self) yellowfive@57: self:Fire("OnValueChanged", self.value) yellowfive@57: end yellowfive@57: yellowfive@57: -- exported yellowfive@57: local function SetValue(self, value) yellowfive@57: self.value = value yellowfive@57: UpdateToggle(self) yellowfive@57: end yellowfive@57: yellowfive@57: -- exported yellowfive@57: local function GetValue(self) yellowfive@57: return self.value yellowfive@57: end yellowfive@57: yellowfive@57: local function Constructor() yellowfive@57: local self = ItemBase.Create(widgetType) yellowfive@57: yellowfive@57: self.frame:SetScript("OnClick", Frame_OnClick) yellowfive@57: yellowfive@57: self.SetValue = SetValue yellowfive@57: self.GetValue = GetValue yellowfive@57: self.OnRelease = OnRelease yellowfive@57: yellowfive@57: AceGUI:RegisterAsWidget(self) yellowfive@57: return self yellowfive@57: end yellowfive@57: yellowfive@57: AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version) yellowfive@57: end yellowfive@57: yellowfive@57: -- Item: Menu yellowfive@57: -- Shows a submenu on mouse over yellowfive@57: -- Does not close the pullout on click yellowfive@57: do yellowfive@57: local widgetType = "Dropdown-Item-Menu" yellowfive@57: local widgetVersion = 2 yellowfive@57: yellowfive@57: local function OnEnter(this) yellowfive@57: local self = this.obj yellowfive@57: self:Fire("OnEnter") yellowfive@57: yellowfive@57: if self.specialOnEnter then yellowfive@57: self.specialOnEnter(self) yellowfive@57: end yellowfive@57: yellowfive@57: self.highlight:Show() yellowfive@57: yellowfive@57: if not self.disabled and self.submenu then yellowfive@57: self.submenu:Open("TOPLEFT", self.frame, "TOPRIGHT", self.pullout:GetRightBorderWidth(), 0, self.frame:GetFrameLevel() + 100) yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: local function OnHide(this) yellowfive@57: local self = this.obj yellowfive@57: if self.submenu then yellowfive@57: self.submenu:Close() yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: -- exported yellowfive@57: local function SetMenu(self, menu) yellowfive@57: assert(menu.type == "Dropdown-Pullout") yellowfive@57: self.submenu = menu yellowfive@57: end yellowfive@57: yellowfive@57: -- exported yellowfive@57: local function CloseMenu(self) yellowfive@57: self.submenu:Close() yellowfive@57: end yellowfive@57: yellowfive@57: local function Constructor() yellowfive@57: local self = ItemBase.Create(widgetType) yellowfive@57: yellowfive@57: self.sub:Show() yellowfive@57: yellowfive@57: self.frame:SetScript("OnEnter", OnEnter) yellowfive@57: self.frame:SetScript("OnHide", OnHide) yellowfive@57: yellowfive@57: self.SetMenu = SetMenu yellowfive@57: self.CloseMenu = CloseMenu yellowfive@57: yellowfive@57: AceGUI:RegisterAsWidget(self) yellowfive@57: return self yellowfive@57: end yellowfive@57: yellowfive@57: AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version) yellowfive@57: end yellowfive@57: yellowfive@57: -- Item: Separator yellowfive@57: -- A single line to separate items yellowfive@57: do yellowfive@57: local widgetType = "Dropdown-Item-Separator" yellowfive@57: local widgetVersion = 1 yellowfive@57: yellowfive@57: -- exported, override yellowfive@57: local function SetDisabled(self, disabled) yellowfive@57: ItemBase.SetDisabled(self, disabled) yellowfive@57: self.useHighlight = false yellowfive@57: end yellowfive@57: yellowfive@57: local function Constructor() yellowfive@57: local self = ItemBase.Create(widgetType) yellowfive@57: yellowfive@57: self.SetDisabled = SetDisabled yellowfive@57: yellowfive@57: local line = self.frame:CreateTexture(nil, "OVERLAY") yellowfive@57: line:SetHeight(1) yellowfive@57: line:SetTexture(.5, .5, .5) yellowfive@57: line:SetPoint("LEFT", self.frame, "LEFT", 10, 0) yellowfive@57: line:SetPoint("RIGHT", self.frame, "RIGHT", -10, 0) yellowfive@57: yellowfive@57: self.text:Hide() yellowfive@57: yellowfive@57: self.useHighlight = false yellowfive@57: yellowfive@57: AceGUI:RegisterAsWidget(self) yellowfive@57: return self yellowfive@57: end yellowfive@57: yellowfive@57: AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion + ItemBase.version) yellowfive@57: end