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