0721829 - Remove unused code
[wowui.git] / libs / AceGUI-3.0 / widgets / AceGUIWidget-Button.lua
1 --[[-----------------------------------------------------------------------------
2 Button Widget
3 Graphical Button.
4 -------------------------------------------------------------------------------]]
5 local Type, Version = "Button", 23
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 pairs = pairs
11
12 -- WoW APIs
13 local _G = _G
14 local PlaySound, CreateFrame, UIParent = PlaySound, CreateFrame, UIParent
15
16 local wowMoP
17 do
18         local _, _, _, interface = GetBuildInfo()
19         wowMoP = (interface >= 50000)
20 end
21
22 --[[-----------------------------------------------------------------------------
23 Scripts
24 -------------------------------------------------------------------------------]]
25 local function Button_OnClick(frame, ...)
26         AceGUI:ClearFocus()
27         PlaySound("igMainMenuOption")
28         frame.obj:Fire("OnClick", ...)
29 end
30
31 local function Control_OnEnter(frame)
32         frame.obj:Fire("OnEnter")
33 end
34
35 local function Control_OnLeave(frame)
36         frame.obj:Fire("OnLeave")
37 end
38
39 --[[-----------------------------------------------------------------------------
40 Methods
41 -------------------------------------------------------------------------------]]
42 local methods = {
43         ["OnAcquire"] = function(self)
44                 -- restore default values
45                 self:SetHeight(24)
46                 self:SetWidth(200)
47                 self:SetDisabled(false)
48                 self:SetAutoWidth(false)
49                 self:SetText()
50         end,
51
52         -- ["OnRelease"] = nil,
53
54         ["SetText"] = function(self, text)
55                 self.text:SetText(text)
56                 if self.autoWidth then
57                         self:SetWidth(self.text:GetStringWidth() + 30)
58                 end
59         end,
60         
61         ["SetAutoWidth"] = function(self, autoWidth)
62                 self.autoWidth = autoWidth
63                 if self.autoWidth then
64                         self:SetWidth(self.text:GetStringWidth() + 30)
65                 end
66         end,
67
68         ["SetDisabled"] = function(self, disabled)
69                 self.disabled = disabled
70                 if disabled then
71                         self.frame:Disable()
72                 else
73                         self.frame:Enable()
74                 end
75         end
76 }
77
78 --[[-----------------------------------------------------------------------------
79 Constructor
80 -------------------------------------------------------------------------------]]
81 local function Constructor()
82         local name = "AceGUI30Button" .. AceGUI:GetNextWidgetNum(Type)
83         local frame = CreateFrame("Button", name, UIParent, wowMoP and "UIPanelButtonTemplate" or "UIPanelButtonTemplate2")
84         frame:Hide()
85
86         frame:EnableMouse(true)
87         frame:SetScript("OnClick", Button_OnClick)
88         frame:SetScript("OnEnter", Control_OnEnter)
89         frame:SetScript("OnLeave", Control_OnLeave)
90
91         local text = frame:GetFontString()
92         text:ClearAllPoints()
93         text:SetPoint("TOPLEFT", 15, -1)
94         text:SetPoint("BOTTOMRIGHT", -15, 1)
95         text:SetJustifyV("MIDDLE")
96
97         local widget = {
98                 text  = text,
99                 frame = frame,
100                 type  = Type
101         }
102         for method, func in pairs(methods) do
103                 widget[method] = func
104         end
105
106         return AceGUI:RegisterAsWidget(widget)
107 end
108
109 AceGUI:RegisterWidgetType(Type, Constructor, Version)