comparison Libs/AceGUI-3.0/widgets/AceGUIWidget-MultiLineEditBox.lua @ 0:98c6f55e6619

First commit
author Xiiph
date Sat, 05 Feb 2011 16:45:02 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:98c6f55e6619
1 local Type, Version = "MultiLineEditBox", 25
2 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
3 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
4
5 -- Lua APIs
6 local pairs = pairs
7
8 -- WoW APIs
9 local GetCursorInfo, GetSpellInfo, ClearCursor = GetCursorInfo, GetSpellInfo, ClearCursor
10 local CreateFrame, UIParent = CreateFrame, UIParent
11 local _G = _G
12
13 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
14 -- List them here for Mikk's FindGlobals script
15 -- GLOBALS: ACCEPT, ChatFontNormal
16
17 --[[-----------------------------------------------------------------------------
18 Support functions
19 -------------------------------------------------------------------------------]]
20 local function Layout(self)
21 self:SetHeight(self.numlines * 14 + (self.disablebutton and 19 or 41) + self.labelHeight)
22
23 if self.labelHeight == 0 then
24 self.scrollBar:SetPoint("TOP", self.frame, "TOP", 0, -23)
25 else
26 self.scrollBar:SetPoint("TOP", self.label, "BOTTOM", 0, -19)
27 end
28
29 if self.disablebutton then
30 self.scrollBar:SetPoint("BOTTOM", self.frame, "BOTTOM", 0, 21)
31 self.scrollBG:SetPoint("BOTTOMLEFT", 0, 4)
32 else
33 self.scrollBar:SetPoint("BOTTOM", self.button, "TOP", 0, 18)
34 self.scrollBG:SetPoint("BOTTOMLEFT", self.button, "TOPLEFT")
35 end
36 end
37
38 --[[-----------------------------------------------------------------------------
39 Scripts
40 -------------------------------------------------------------------------------]]
41 local function OnClick(self) -- Button
42 self = self.obj
43 self.editBox:ClearFocus()
44 if not self:Fire("OnEnterPressed", self.editBox:GetText()) then
45 self.button:Disable()
46 end
47 end
48
49 local function OnCursorChanged(self, _, y, _, cursorHeight) -- EditBox
50 self, y = self.obj.scrollFrame, -y
51 local offset = self:GetVerticalScroll()
52 if y < offset then
53 self:SetVerticalScroll(y)
54 else
55 y = y + cursorHeight - self:GetHeight()
56 if y > offset then
57 self:SetVerticalScroll(y)
58 end
59 end
60 end
61
62 local function OnEditFocusLost(self) -- EditBox
63 self:HighlightText(0, 0)
64 end
65
66 local function OnEnter(self) -- EditBox / ScrollFrame
67 self = self.obj
68 if not self.entered then
69 self.entered = true
70 self:Fire("OnEnter")
71 end
72 end
73
74 local function OnLeave(self) -- EditBox / ScrollFrame
75 self = self.obj
76 if self.entered then
77 self.entered = nil
78 self:Fire("OnLeave")
79 end
80 end
81
82 local function OnMouseUp(self) -- ScrollFrame
83 self = self.obj.editBox
84 self:SetFocus()
85 self:SetCursorPosition(self:GetNumLetters())
86 end
87
88 local function OnReceiveDrag(self) -- EditBox / ScrollFrame
89 local type, id, info = GetCursorInfo()
90 if type == "spell" then
91 info = GetSpellInfo(id, info)
92 elseif type ~= "item" then
93 return
94 end
95 ClearCursor()
96 self = self.obj
97 local editBox = self.editBox
98 if not editBox:HasFocus() then
99 editBox:SetFocus()
100 editBox:SetCursorPosition(editBox:GetNumLetters())
101 end
102 editBox:Insert(info)
103 self.button:Enable()
104 end
105
106 local function OnSizeChanged(self, width, height) -- ScrollFrame
107 self.obj.editBox:SetWidth(width)
108 end
109
110 local function OnTextChanged(self, userInput) -- EditBox
111 if userInput then
112 self = self.obj
113 self:Fire("OnTextChanged", self.editBox:GetText())
114 self.button:Enable()
115 end
116 end
117
118 local function OnTextSet(self) -- EditBox
119 self:HighlightText(0, 0)
120 self:SetCursorPosition(self:GetNumLetters())
121 self:SetCursorPosition(0)
122 self.obj.button:Disable()
123 end
124
125 local function OnVerticalScroll(self, offset) -- ScrollFrame
126 local editBox = self.obj.editBox
127 editBox:SetHitRectInsets(0, 0, offset, editBox:GetHeight() - offset - self:GetHeight())
128 end
129
130 local function OnShowFocus(frame)
131 frame.obj.editBox:SetFocus()
132 frame:SetScript("OnShow", nil)
133 end
134
135 local function OnFocusGained(frame)
136 AceGUI:SetFocus(frame.obj)
137 end
138
139 --[[-----------------------------------------------------------------------------
140 Methods
141 -------------------------------------------------------------------------------]]
142 local methods = {
143 ["OnAcquire"] = function(self)
144 self.editBox:SetText("")
145 self:SetDisabled(false)
146 self:SetWidth(200)
147 self:DisableButton(false)
148 self:SetNumLines()
149 self.entered = nil
150 self:SetMaxLetters(0)
151 end,
152
153 ["OnRelease"] = function(self)
154 self:ClearFocus()
155 end,
156
157 ["SetDisabled"] = function(self, disabled)
158 local editBox = self.editBox
159 if disabled then
160 editBox:ClearFocus()
161 editBox:EnableMouse(false)
162 editBox:SetTextColor(0.5, 0.5, 0.5)
163 self.label:SetTextColor(0.5, 0.5, 0.5)
164 self.scrollFrame:EnableMouse(false)
165 self.button:Disable()
166 else
167 editBox:EnableMouse(true)
168 editBox:SetTextColor(1, 1, 1)
169 self.label:SetTextColor(1, 0.82, 0)
170 self.scrollFrame:EnableMouse(true)
171 end
172 end,
173
174 ["SetLabel"] = function(self, text)
175 if text and text ~= "" then
176 self.label:SetText(text)
177 if self.labelHeight ~= 10 then
178 self.labelHeight = 10
179 self.label:Show()
180 end
181 elseif self.labelHeight ~= 0 then
182 self.labelHeight = 0
183 self.label:Hide()
184 end
185 Layout(self)
186 end,
187
188 ["SetNumLines"] = function(self, value)
189 if not value or value < 4 then
190 value = 4
191 end
192 self.numlines = value
193 Layout(self)
194 end,
195
196 ["SetText"] = function(self, text)
197 self.editBox:SetText(text)
198 end,
199
200 ["GetText"] = function(self)
201 return self.editBox:GetText()
202 end,
203
204 ["SetMaxLetters"] = function (self, num)
205 self.editBox:SetMaxLetters(num or 0)
206 end,
207
208 ["DisableButton"] = function(self, disabled)
209 self.disablebutton = disabled
210 if disabled then
211 self.button:Hide()
212 else
213 self.button:Show()
214 end
215 Layout(self)
216 end,
217
218 ["ClearFocus"] = function(self)
219 self.editBox:ClearFocus()
220 self.frame:SetScript("OnShow", nil)
221 end,
222
223 ["SetFocus"] = function(self)
224 self.editBox:SetFocus()
225 if not self.frame:IsShown() then
226 self.frame:SetScript("OnShow", OnShowFocus)
227 end
228 end
229 }
230
231 --[[-----------------------------------------------------------------------------
232 Constructor
233 -------------------------------------------------------------------------------]]
234 local backdrop = {
235 bgFile = [[Interface\Tooltips\UI-Tooltip-Background]],
236 edgeFile = [[Interface\Tooltips\UI-Tooltip-Border]], edgeSize = 16,
237 insets = { left = 4, right = 3, top = 4, bottom = 3 }
238 }
239
240 local function Constructor()
241 local frame = CreateFrame("Frame", nil, UIParent)
242 frame:Hide()
243
244 local widgetNum = AceGUI:GetNextWidgetNum(Type)
245
246 local label = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
247 label:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, -4)
248 label:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, -4)
249 label:SetJustifyH("LEFT")
250 label:SetText(ACCEPT)
251 label:SetHeight(10)
252
253 local button = CreateFrame("Button", ("%s%dButton"):format(Type, widgetNum), frame, "UIPanelButtonTemplate2")
254 button:SetPoint("BOTTOMLEFT", 0, 4)
255 button:SetHeight(22)
256 button:SetWidth(label:GetStringWidth() + 24)
257 button:SetText(ACCEPT)
258 button:SetScript("OnClick", OnClick)
259 button:Disable()
260
261 local text = button:GetFontString()
262 text:ClearAllPoints()
263 text:SetPoint("TOPLEFT", button, "TOPLEFT", 5, -5)
264 text:SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", -5, 1)
265 text:SetJustifyV("MIDDLE")
266
267 local scrollBG = CreateFrame("Frame", nil, frame)
268 scrollBG:SetBackdrop(backdrop)
269 scrollBG:SetBackdropColor(0, 0, 0)
270 scrollBG:SetBackdropBorderColor(0.4, 0.4, 0.4)
271
272 local scrollFrame = CreateFrame("ScrollFrame", ("%s%dScrollFrame"):format(Type, widgetNum), frame, "UIPanelScrollFrameTemplate")
273
274 local scrollBar = _G[scrollFrame:GetName() .. "ScrollBar"]
275 scrollBar:ClearAllPoints()
276 scrollBar:SetPoint("TOP", label, "BOTTOM", 0, -19)
277 scrollBar:SetPoint("BOTTOM", button, "TOP", 0, 18)
278 scrollBar:SetPoint("RIGHT", frame, "RIGHT")
279
280 scrollBG:SetPoint("TOPRIGHT", scrollBar, "TOPLEFT", 0, 19)
281 scrollBG:SetPoint("BOTTOMLEFT", button, "TOPLEFT")
282
283 scrollFrame:SetPoint("TOPLEFT", scrollBG, "TOPLEFT", 5, -6)
284 scrollFrame:SetPoint("BOTTOMRIGHT", scrollBG, "BOTTOMRIGHT", -4, 4)
285 scrollFrame:SetScript("OnEnter", OnEnter)
286 scrollFrame:SetScript("OnLeave", OnLeave)
287 scrollFrame:SetScript("OnMouseUp", OnMouseUp)
288 scrollFrame:SetScript("OnReceiveDrag", OnReceiveDrag)
289 scrollFrame:SetScript("OnSizeChanged", OnSizeChanged)
290 scrollFrame:HookScript("OnVerticalScroll", OnVerticalScroll)
291
292 local editBox = CreateFrame("EditBox", nil, scrollFrame)
293 editBox:SetAllPoints()
294 editBox:SetFontObject(ChatFontNormal)
295 editBox:SetMultiLine(true)
296 editBox:EnableMouse(true)
297 editBox:SetAutoFocus(false)
298 editBox:SetCountInvisibleLetters(false)
299 editBox:SetScript("OnCursorChanged", OnCursorChanged)
300 editBox:SetScript("OnEditFocusLost", OnEditFocusLost)
301 editBox:SetScript("OnEnter", OnEnter)
302 editBox:SetScript("OnEscapePressed", editBox.ClearFocus)
303 editBox:SetScript("OnLeave", OnLeave)
304 editBox:SetScript("OnMouseDown", OnReceiveDrag)
305 editBox:SetScript("OnReceiveDrag", OnReceiveDrag)
306 editBox:SetScript("OnTextChanged", OnTextChanged)
307 editBox:SetScript("OnTextSet", OnTextSet)
308 editBox:SetScript("OnEditFocusGained", OnFocusGained)
309
310
311 scrollFrame:SetScrollChild(editBox)
312
313 local widget = {
314 button = button,
315 editBox = editBox,
316 frame = frame,
317 label = label,
318 labelHeight = 10,
319 numlines = 4,
320 scrollBar = scrollBar,
321 scrollBG = scrollBG,
322 scrollFrame = scrollFrame,
323 type = Type
324 }
325 for method, func in pairs(methods) do
326 widget[method] = func
327 end
328 button.obj, editBox.obj, scrollFrame.obj = widget, widget, widget
329
330 return AceGUI:RegisterAsWidget(widget)
331 end
332
333 AceGUI:RegisterWidgetType(Type, Constructor, Version)