acd7131bbeb1cc8c778bf278c4333d4cc3b5059c
[wowui.git] / libs / AceGUI-3.0-SharedMediaWidgets / Libs / AceGUI-3.0 / widgets / AceGUIWidget-EditBox.lua
1 --[[-----------------------------------------------------------------------------
2 EditBox Widget
3 -------------------------------------------------------------------------------]]
4 local Type, Version = "EditBox", 24
5 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
6 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
7
8 -- Lua APIs
9 local tostring, pairs = tostring, pairs
10
11 -- WoW APIs
12 local PlaySound = PlaySound
13 local GetCursorInfo, ClearCursor, GetSpellInfo = GetCursorInfo, ClearCursor, GetSpellInfo
14 local CreateFrame, UIParent = CreateFrame, UIParent
15 local _G = _G
16
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: AceGUIEditBoxInsertLink, ChatFontNormal, OKAY
20
21 --[[-----------------------------------------------------------------------------
22 Support functions
23 -------------------------------------------------------------------------------]]
24 if not AceGUIEditBoxInsertLink then
25         -- upgradeable hook
26         hooksecurefunc("ChatEdit_InsertLink", function(...) return _G.AceGUIEditBoxInsertLink(...) end)
27 end
28
29 function _G.AceGUIEditBoxInsertLink(text)
30         for i = 1, AceGUI:GetWidgetCount(Type) do
31                 local editbox = _G["AceGUI-3.0EditBox"..i]
32                 if editbox and editbox:IsVisible() and editbox:HasFocus() then
33                         editbox:Insert(text)
34                         return true
35                 end
36         end
37 end
38
39 local function ShowButton(self)
40         if not self.disablebutton then
41                 self.button:Show()
42                 self.editbox:SetTextInsets(0, 20, 3, 3)
43         end
44 end
45
46 local function HideButton(self)
47         self.button:Hide()
48         self.editbox:SetTextInsets(0, 0, 3, 3)
49 end
50
51 --[[-----------------------------------------------------------------------------
52 Scripts
53 -------------------------------------------------------------------------------]]
54 local function Control_OnEnter(frame)
55         frame.obj:Fire("OnEnter")
56 end
57
58 local function Control_OnLeave(frame)
59         frame.obj:Fire("OnLeave")
60 end
61
62 local function Frame_OnShowFocus(frame)
63         frame.obj.editbox:SetFocus()
64         frame:SetScript("OnShow", nil)
65 end
66
67 local function EditBox_OnEscapePressed(frame)
68         AceGUI:ClearFocus()
69 end
70
71 local function EditBox_OnEnterPressed(frame)
72         local self = frame.obj
73         local value = frame:GetText()
74         local cancel = self:Fire("OnEnterPressed", value)
75         if not cancel then
76                 PlaySound("igMainMenuOptionCheckBoxOn")
77                 HideButton(self)
78         end
79 end
80
81 local function EditBox_OnReceiveDrag(frame)
82         local self = frame.obj
83         local type, id, info = GetCursorInfo()
84         if type == "item" then
85                 self:SetText(info)
86                 self:Fire("OnEnterPressed", info)
87                 ClearCursor()
88         elseif type == "spell" then
89                 local name = GetSpellInfo(id, info)
90                 self:SetText(name)
91                 self:Fire("OnEnterPressed", name)
92                 ClearCursor()
93         end
94         HideButton(self)
95         AceGUI:ClearFocus()
96 end
97
98 local function EditBox_OnTextChanged(frame)
99         local self = frame.obj
100         local value = frame:GetText()
101         if tostring(value) ~= tostring(self.lasttext) then
102                 self:Fire("OnTextChanged", value)
103                 self.lasttext = value
104                 ShowButton(self)
105         end
106 end
107
108 local function EditBox_OnFocusGained(frame)
109         AceGUI:SetFocus(frame.obj)
110 end
111
112 local function Button_OnClick(frame)
113         local editbox = frame.obj.editbox
114         editbox:ClearFocus()
115         EditBox_OnEnterPressed(editbox)
116 end
117
118 --[[-----------------------------------------------------------------------------
119 Methods
120 -------------------------------------------------------------------------------]]
121 local methods = {
122         ["OnAcquire"] = function(self)
123                 -- height is controlled by SetLabel
124                 self:SetWidth(200)
125                 self:SetDisabled(false)
126                 self:SetLabel()
127                 self:SetText()
128                 self:DisableButton(false)
129                 self:SetMaxLetters(0)
130         end,
131
132         ["OnRelease"] = function(self)
133                 self:ClearFocus()
134         end,
135
136         ["SetDisabled"] = function(self, disabled)
137                 self.disabled = disabled
138                 if disabled then
139                         self.editbox:EnableMouse(false)
140                         self.editbox:ClearFocus()
141                         self.editbox:SetTextColor(0.5,0.5,0.5)
142                         self.label:SetTextColor(0.5,0.5,0.5)
143                 else
144                         self.editbox:EnableMouse(true)
145                         self.editbox:SetTextColor(1,1,1)
146                         self.label:SetTextColor(1,.82,0)
147                 end
148         end,
149
150         ["SetText"] = function(self, text)
151                 self.lasttext = text or ""
152                 self.editbox:SetText(text or "")
153                 self.editbox:SetCursorPosition(0)
154                 HideButton(self)
155         end,
156
157         ["GetText"] = function(self, text)
158                 return self.editbox:GetText()
159         end,
160
161         ["SetLabel"] = function(self, text)
162                 if text and text ~= "" then
163                         self.label:SetText(text)
164                         self.label:Show()
165                         self.editbox:SetPoint("TOPLEFT",self.frame,"TOPLEFT",7,-18)
166                         self:SetHeight(44)
167                         self.alignoffset = 30
168                 else
169                         self.label:SetText("")
170                         self.label:Hide()
171                         self.editbox:SetPoint("TOPLEFT",self.frame,"TOPLEFT",7,0)
172                         self:SetHeight(26)
173                         self.alignoffset = 12
174                 end
175         end,
176
177         ["DisableButton"] = function(self, disabled)
178                 self.disablebutton = disabled
179                 if disabled then
180                         HideButton(self)
181                 end
182         end,
183
184         ["SetMaxLetters"] = function (self, num)
185                 self.editbox:SetMaxLetters(num or 0)
186         end,
187
188         ["ClearFocus"] = function(self)
189                 self.editbox:ClearFocus()
190                 self.frame:SetScript("OnShow", nil)
191         end,
192
193         ["SetFocus"] = function(self)
194                 self.editbox:SetFocus()
195                 if not self.frame:IsShown() then
196                         self.frame:SetScript("OnShow", Frame_OnShowFocus)
197                 end
198         end
199 }
200
201 --[[-----------------------------------------------------------------------------
202 Constructor
203 -------------------------------------------------------------------------------]]
204 local function Constructor()
205         local num  = AceGUI:GetNextWidgetNum(Type)
206         local frame = CreateFrame("Frame", nil, UIParent)
207         frame:Hide()
208
209         local editbox = CreateFrame("EditBox", "AceGUI-3.0EditBox"..num, frame, "InputBoxTemplate")
210         editbox:SetAutoFocus(false)
211         editbox:SetFontObject(ChatFontNormal)
212         editbox:SetScript("OnEnter", Control_OnEnter)
213         editbox:SetScript("OnLeave", Control_OnLeave)
214         editbox:SetScript("OnEscapePressed", EditBox_OnEscapePressed)
215         editbox:SetScript("OnEnterPressed", EditBox_OnEnterPressed)
216         editbox:SetScript("OnTextChanged", EditBox_OnTextChanged)
217         editbox:SetScript("OnReceiveDrag", EditBox_OnReceiveDrag)
218         editbox:SetScript("OnMouseDown", EditBox_OnReceiveDrag)
219         editbox:SetScript("OnEditFocusGained", EditBox_OnFocusGained)
220         editbox:SetTextInsets(0, 0, 3, 3)
221         editbox:SetMaxLetters(256)
222         editbox:SetPoint("BOTTOMLEFT", 6, 0)
223         editbox:SetPoint("BOTTOMRIGHT")
224         editbox:SetHeight(19)
225
226         local label = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
227         label:SetPoint("TOPLEFT", 0, -2)
228         label:SetPoint("TOPRIGHT", 0, -2)
229         label:SetJustifyH("LEFT")
230         label:SetHeight(18)
231
232         local button = CreateFrame("Button", nil, editbox, "UIPanelButtonTemplate")
233         button:SetWidth(40)
234         button:SetHeight(20)
235         button:SetPoint("RIGHT", -2, 0)
236         button:SetText(OKAY)
237         button:SetScript("OnClick", Button_OnClick)
238         button:Hide()
239
240         local widget = {
241                 alignoffset = 30,
242                 editbox     = editbox,
243                 label       = label,
244                 button      = button,
245                 frame       = frame,
246                 type        = Type
247         }
248         for method, func in pairs(methods) do
249                 widget[method] = func
250         end
251         editbox.obj, button.obj = widget, widget
252
253         return AceGUI:RegisterAsWidget(widget)
254 end
255
256 AceGUI:RegisterWidgetType(Type, Constructor, Version)