b8c7045 - Rename
[wowui.git] / OmaRFConfig / libs / AceGUI-3.0 / widgets / AceGUIWidget-EditBox.lua
1 --[[-----------------------------------------------------------------------------
2 EditBox Widget
3 -------------------------------------------------------------------------------]]
4 local Type, Version = "EditBox", 27
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(PlaySoundKitID and "igMainMenuOptionCheckBoxOn" or 856) -- SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_ON
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         elseif type == "macro" then
94                 local name = GetMacroInfo(id)
95                 self:SetText(name)
96                 self:Fire("OnEnterPressed", name)
97                 ClearCursor()
98         end
99         HideButton(self)
100         AceGUI:ClearFocus()
101 end
102
103 local function EditBox_OnTextChanged(frame)
104         local self = frame.obj
105         local value = frame:GetText()
106         if tostring(value) ~= tostring(self.lasttext) then
107                 self:Fire("OnTextChanged", value)
108                 self.lasttext = value
109                 ShowButton(self)
110         end
111 end
112
113 local function EditBox_OnFocusGained(frame)
114         AceGUI:SetFocus(frame.obj)
115 end
116
117 local function Button_OnClick(frame)
118         local editbox = frame.obj.editbox
119         editbox:ClearFocus()
120         EditBox_OnEnterPressed(editbox)
121 end
122
123 --[[-----------------------------------------------------------------------------
124 Methods
125 -------------------------------------------------------------------------------]]
126 local methods = {
127         ["OnAcquire"] = function(self)
128                 -- height is controlled by SetLabel
129                 self:SetWidth(200)
130                 self:SetDisabled(false)
131                 self:SetLabel()
132                 self:SetText()
133                 self:DisableButton(false)
134                 self:SetMaxLetters(0)
135         end,
136
137         ["OnRelease"] = function(self)
138                 self:ClearFocus()
139         end,
140
141         ["SetDisabled"] = function(self, disabled)
142                 self.disabled = disabled
143                 if disabled then
144                         self.editbox:EnableMouse(false)
145                         self.editbox:ClearFocus()
146                         self.editbox:SetTextColor(0.5,0.5,0.5)
147                         self.label:SetTextColor(0.5,0.5,0.5)
148                 else
149                         self.editbox:EnableMouse(true)
150                         self.editbox:SetTextColor(1,1,1)
151                         self.label:SetTextColor(1,.82,0)
152                 end
153         end,
154
155         ["SetText"] = function(self, text)
156                 self.lasttext = text or ""
157                 self.editbox:SetText(text or "")
158                 self.editbox:SetCursorPosition(0)
159                 HideButton(self)
160         end,
161
162         ["GetText"] = function(self, text)
163                 return self.editbox:GetText()
164         end,
165
166         ["SetLabel"] = function(self, text)
167                 if text and text ~= "" then
168                         self.label:SetText(text)
169                         self.label:Show()
170                         self.editbox:SetPoint("TOPLEFT",self.frame,"TOPLEFT",7,-18)
171                         self:SetHeight(44)
172                         self.alignoffset = 30
173                 else
174                         self.label:SetText("")
175                         self.label:Hide()
176                         self.editbox:SetPoint("TOPLEFT",self.frame,"TOPLEFT",7,0)
177                         self:SetHeight(26)
178                         self.alignoffset = 12
179                 end
180         end,
181
182         ["DisableButton"] = function(self, disabled)
183                 self.disablebutton = disabled
184                 if disabled then
185                         HideButton(self)
186                 end
187         end,
188
189         ["SetMaxLetters"] = function (self, num)
190                 self.editbox:SetMaxLetters(num or 0)
191         end,
192
193         ["ClearFocus"] = function(self)
194                 self.editbox:ClearFocus()
195                 self.frame:SetScript("OnShow", nil)
196         end,
197
198         ["SetFocus"] = function(self)
199                 self.editbox:SetFocus()
200                 if not self.frame:IsShown() then
201                         self.frame:SetScript("OnShow", Frame_OnShowFocus)
202                 end
203         end,
204
205         ["HighlightText"] = function(self, from, to)
206                 self.editbox:HighlightText(from, to)
207         end
208 }
209
210 --[[-----------------------------------------------------------------------------
211 Constructor
212 -------------------------------------------------------------------------------]]
213 local function Constructor()
214         local num  = AceGUI:GetNextWidgetNum(Type)
215         local frame = CreateFrame("Frame", nil, UIParent)
216         frame:Hide()
217
218         local editbox = CreateFrame("EditBox", "AceGUI-3.0EditBox"..num, frame, "InputBoxTemplate")
219         editbox:SetAutoFocus(false)
220         editbox:SetFontObject(ChatFontNormal)
221         editbox:SetScript("OnEnter", Control_OnEnter)
222         editbox:SetScript("OnLeave", Control_OnLeave)
223         editbox:SetScript("OnEscapePressed", EditBox_OnEscapePressed)
224         editbox:SetScript("OnEnterPressed", EditBox_OnEnterPressed)
225         editbox:SetScript("OnTextChanged", EditBox_OnTextChanged)
226         editbox:SetScript("OnReceiveDrag", EditBox_OnReceiveDrag)
227         editbox:SetScript("OnMouseDown", EditBox_OnReceiveDrag)
228         editbox:SetScript("OnEditFocusGained", EditBox_OnFocusGained)
229         editbox:SetTextInsets(0, 0, 3, 3)
230         editbox:SetMaxLetters(256)
231         editbox:SetPoint("BOTTOMLEFT", 6, 0)
232         editbox:SetPoint("BOTTOMRIGHT")
233         editbox:SetHeight(19)
234
235         local label = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
236         label:SetPoint("TOPLEFT", 0, -2)
237         label:SetPoint("TOPRIGHT", 0, -2)
238         label:SetJustifyH("LEFT")
239         label:SetHeight(18)
240
241         local button = CreateFrame("Button", nil, editbox, "UIPanelButtonTemplate")
242         button:SetWidth(40)
243         button:SetHeight(20)
244         button:SetPoint("RIGHT", -2, 0)
245         button:SetText(OKAY)
246         button:SetScript("OnClick", Button_OnClick)
247         button:Hide()
248
249         local widget = {
250                 alignoffset = 30,
251                 editbox     = editbox,
252                 label       = label,
253                 button      = button,
254                 frame       = frame,
255                 type        = Type
256         }
257         for method, func in pairs(methods) do
258                 widget[method] = func
259         end
260         editbox.obj, button.obj = widget, widget
261
262         return AceGUI:RegisterAsWidget(widget)
263 end
264
265 AceGUI:RegisterWidgetType(Type, Constructor, Version)