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

Reasonably functional now. Cleaning up some stuff which might have to be reverted.
author Zerotorescue
date Thu, 07 Oct 2010 17:17:43 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c6ff7ba0e8f6
1 local Type, Version = "MultiLineEditBox", 22
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, GetSpellName, ClearCursor = GetCursorInfo, GetSpellName, 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 Scripts
19 -------------------------------------------------------------------------------]]
20 local function OnClick(self) -- Button
21 self = self.obj
22 self.editBox:ClearFocus()
23 if not self:Fire("OnEnterPressed", self.editBox:GetText()) then
24 self.button:Disable()
25 end
26 end
27
28 local function OnCursorChanged(self, _, y, _, cursorHeight) -- EditBox
29 self, y = self.obj.scrollFrame, -y
30 local offset = self:GetVerticalScroll()
31 if y < offset then
32 self:SetVerticalScroll(y)
33 else
34 y = y + cursorHeight - self:GetHeight()
35 if y > offset then
36 self:SetVerticalScroll(y)
37 end
38 end
39 end
40
41 local function OnEditFocusLost(self) -- EditBox
42 self:HighlightText(0, 0)
43 end
44
45 local function OnEnter(self) -- EditBox / ScrollFrame
46 self = self.obj
47 if not self.entered then
48 self.entered = true
49 self:Fire("OnEnter")
50 end
51 end
52
53 local function OnLeave(self) -- EditBox / ScrollFrame
54 self = self.obj
55 if self.entered then
56 self.entered = nil
57 self:Fire("OnLeave")
58 end
59 end
60
61 local function OnMouseUp(self) -- ScrollFrame
62 self = self.obj.editBox
63 self:SetFocus()
64 self:SetCursorPosition(self:GetNumLetters())
65 end
66
67 local function OnReceiveDrag(self) -- EditBox / ScrollFrame
68 local type, id, info = GetCursorInfo()
69 if type == "spell" then
70 info, id = GetSpellName(id, info)
71 if id and id:match("%d") then
72 info = info .. "(" .. id .. ")"
73 end
74 elseif type ~= "item" then
75 return
76 end
77 ClearCursor()
78 self = self.obj
79 local editBox = self.editBox
80 if not editBox:HasFocus() then
81 editBox:SetFocus()
82 editBox:SetCursorPosition(editBox:GetNumLetters())
83 end
84 editBox:Insert(info)
85 self.button:Enable()
86 end
87
88 local function OnSizeChanged(self, width, height) -- ScrollFrame
89 self.obj.editBox:SetWidth(width)
90 end
91
92 local function OnTextChanged(self, userInput) -- EditBox
93 if userInput then
94 self = self.obj
95 self:Fire("OnTextChanged", self.editBox:GetText())
96 self.button:Enable()
97 end
98 end
99
100 local function OnTextSet(self) -- EditBox
101 self:HighlightText(0, 0)
102 self:SetCursorPosition(self:GetNumLetters())
103 self:SetCursorPosition(0)
104 self.obj.button:Disable()
105 end
106
107 local function OnVerticalScroll(self, offset) -- ScrollFrame
108 local editBox = self.obj.editBox
109 editBox:SetHitRectInsets(0, 0, offset, editBox:GetHeight() - offset - self:GetHeight())
110 end
111
112 --[[-----------------------------------------------------------------------------
113 Methods
114 -------------------------------------------------------------------------------]]
115 local methods = {
116 ["GetText"] = function(self)
117 return self.editBox:GetText()
118 end,
119
120 ["OnAcquire"] = function(self)
121 self.editBox:SetText("")
122 self:SetDisabled(false)
123 self:SetWidth(200)
124 self:SetNumLines()
125 self.entered = nil
126 self:SetMaxLetters(0)
127 end,
128
129 ["OnRelease"] = function(self)
130 self.frame:ClearAllPoints()
131 self.frame:Hide()
132 end,
133
134 ["SetDisabled"] = function(self, disabled)
135 local editBox = self.editBox
136 if disabled then
137 editBox:ClearFocus()
138 editBox:EnableMouse(false)
139 editBox:SetTextColor(0.5, 0.5, 0.5)
140 self.label:SetTextColor(0.5, 0.5, 0.5)
141 self.scrollFrame:EnableMouse(false)
142 self.button:Disable()
143 else
144 editBox:EnableMouse(true)
145 editBox:SetTextColor(1, 1, 1)
146 self.label:SetTextColor(1, 0.82, 0)
147 self.scrollFrame:EnableMouse(true)
148 end
149 end,
150
151 ["SetLabel"] = function(self, text)
152 if text and text ~= "" then
153 self.label:SetText(text)
154 if self.labelHeight ~= 10 then
155 self.labelHeight = 10
156 self.scrollBar:SetPoint("TOP", self.label, "BOTTOM", 0, -19)
157 self:SetHeight(self.frame.height + 10)
158 self.label:Show()
159 end
160 elseif self.labelHeight ~= 0 then
161 self.labelHeight = 0
162 self.label:Hide()
163 self.scrollBar:SetPoint("TOP", self.frame, "TOP", 0, -23)
164 self:SetHeight(self.frame.height - 10)
165 end
166 end,
167
168 ["SetNumLines"] = function(self, value)
169 if not value or value < 4 then
170 value = 4
171 end
172 self:SetHeight(value * 14 + 41 + self.labelHeight)
173 end,
174
175 ["SetText"] = function(self, text)
176 self.editBox:SetText(text)
177 end,
178
179 ["SetMaxLetters"] = function (self, num)
180 self.editBox:SetMaxLetters(num or 0)
181 end
182 }
183
184 --[[-----------------------------------------------------------------------------
185 Constructor
186 -------------------------------------------------------------------------------]]
187 local backdrop = {
188 bgFile = [[Interface\Tooltips\UI-Tooltip-Background]],
189 edgeFile = [[Interface\Tooltips\UI-Tooltip-Border]], edgeSize = 16,
190 insets = { left = 4, right = 3, top = 4, bottom = 3 }
191 }
192
193 local function Constructor()
194 local frame = CreateFrame("Frame", nil, UIParent)
195 frame:Hide()
196
197 local widgetNum = AceGUI:GetNextWidgetNum(Type)
198
199 local label = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
200 label:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, -4)
201 label:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, -4)
202 label:SetJustifyH("LEFT")
203 label:SetText(ACCEPT)
204 label:SetHeight(10)
205
206 local button = CreateFrame("Button", ("%s%dButton"):format(Type, widgetNum), frame, "UIPanelButtonTemplate2")
207 button:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 0, 4)
208 button:SetHeight(22)
209 button:SetWidth(label:GetStringWidth() + 24)
210 button:SetText(ACCEPT)
211 button:SetScript("OnClick", OnClick)
212 button:Disable()
213
214 local text = button:GetFontString()
215 text:ClearAllPoints()
216 text:SetPoint("TOPLEFT", button, "TOPLEFT", 5, -5)
217 text:SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", -5, 1)
218 text:SetJustifyV("MIDDLE")
219
220 local scrollBG = CreateFrame("Frame", nil, frame)
221 scrollBG:SetBackdrop(backdrop)
222 scrollBG:SetBackdropColor(0, 0, 0)
223 scrollBG:SetBackdropBorderColor(0.4, 0.4, 0.4)
224
225 local scrollFrame = CreateFrame("ScrollFrame", ("%s%dScrollFrame"):format(Type, widgetNum), frame, "UIPanelScrollFrameTemplate")
226
227 local scrollBar = _G[scrollFrame:GetName() .. "ScrollBar"]
228 scrollBar:ClearAllPoints()
229 scrollBar:SetPoint("TOP", label, "BOTTOM", 0, -19)
230 scrollBar:SetPoint("BOTTOM", button, "TOP", 0, 18)
231 scrollBar:SetPoint("RIGHT", frame, "RIGHT")
232
233 scrollBG:SetPoint("TOPRIGHT", scrollBar, "TOPLEFT", 0, 19)
234 scrollBG:SetPoint("BOTTOMLEFT", button, "TOPLEFT")
235
236 scrollFrame:SetPoint("TOPLEFT", scrollBG, "TOPLEFT", 5, -6)
237 scrollFrame:SetPoint("BOTTOMRIGHT", scrollBG, "BOTTOMRIGHT", -4, 4)
238 scrollFrame:SetScript("OnEnter", OnEnter)
239 scrollFrame:SetScript("OnLeave", OnLeave)
240 scrollFrame:SetScript("OnMouseUp", OnMouseUp)
241 scrollFrame:SetScript("OnReceiveDrag", OnReceiveDrag)
242 scrollFrame:SetScript("OnSizeChanged", OnSizeChanged)
243 scrollFrame:HookScript("OnVerticalScroll", OnVerticalScroll)
244
245 local editBox = CreateFrame("EditBox", nil, scrollFrame)
246 editBox:SetAllPoints()
247 editBox:SetFontObject(ChatFontNormal)
248 editBox:SetMultiLine(true)
249 editBox:EnableMouse(true)
250 editBox:SetAutoFocus(false)
251 editBox:SetCountInvisibleLetters(false)
252 editBox:SetScript("OnCursorChanged", OnCursorChanged)
253 editBox:SetScript("OnEditFocusLost", OnEditFocusLost)
254 editBox:SetScript("OnEnter", OnEnter)
255 editBox:SetScript("OnEscapePressed", editBox.ClearFocus)
256 editBox:SetScript("OnLeave", OnLeave)
257 editBox:SetScript("OnMouseDown", OnReceiveDrag)
258 editBox:SetScript("OnReceiveDrag", OnReceiveDrag)
259 editBox:SetScript("OnTextChanged", OnTextChanged)
260 editBox:SetScript("OnTextSet", OnTextSet)
261
262 scrollFrame:SetScrollChild(editBox)
263
264 local widget = {
265 button = button,
266 editBox = editBox,
267 frame = frame,
268 label = label,
269 labelHeight = 10,
270 scrollBar = scrollBar,
271 scrollFrame = scrollFrame,
272 type = Type
273 }
274 for method, func in pairs(methods) do
275 widget[method] = func
276 end
277 button.obj, editBox.obj, scrollFrame.obj = widget, widget, widget
278
279 AceGUI:RegisterAsWidget(widget)
280 return widget
281 end
282
283 AceGUI:RegisterWidgetType(Type, Constructor, Version)