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

First public revision. At this point ItemAuditor watches mail for auctions sold or purchased, watches for buy/sell (money and 1 item type change) and conversions/tradeskills. Milling isn't working yet because there is too much time between the first event and the last event.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Thu, 20 May 2010 19:22:19 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:169f5211fc7f
1
2 --[[
3 --Multiline Editbox Widget, Originally by bam
4
5 --]]
6 local AceGUI = LibStub("AceGUI-3.0")
7
8 -- Lua APIs
9 local format, pairs, tostring = string.format, pairs, tostring
10
11 -- WoW APIs
12 local GetCursorInfo, ClearCursor, GetSpellName = GetCursorInfo, ClearCursor, GetSpellName
13 local CreateFrame, UIParent = CreateFrame, UIParent
14
15 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
16 -- List them here for Mikk's FindGlobals script
17 -- GLOBALS: ChatFontNormal, ACCEPT
18
19 local Version = 11
20 ---------------------
21 -- Common Elements --
22 ---------------------
23
24 local FrameBackdrop = {
25 bgFile="Interface\\DialogFrame\\UI-DialogBox-Background",
26 edgeFile="Interface\\DialogFrame\\UI-DialogBox-Border",
27 tile = true, tileSize = 32, edgeSize = 32,
28 insets = { left = 8, right = 8, top = 8, bottom = 8 }
29 }
30
31 local PaneBackdrop = {
32
33 bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
34 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
35 tile = true, tileSize = 16, edgeSize = 16,
36 insets = { left = 3, right = 3, top = 5, bottom = 3 }
37 }
38
39 local ControlBackdrop = {
40 bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
41 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
42 tile = true, tileSize = 16, edgeSize = 16,
43 insets = { left = 3, right = 3, top = 3, bottom = 3 }
44 }
45
46 --------------------------
47 -- Edit box --
48 --------------------------
49 --[[
50 Events :
51 OnTextChanged
52 OnEnterPressed
53
54 ]]
55 do
56 local Type = "MultiLineEditBox"
57
58 local MultiLineEditBox = {}
59
60 local function EditBox_OnEnterPressed(this)
61 local self = this.obj
62 local value = this:GetText()
63 local cancel = self:Fire("OnEnterPressed",value)
64 if not cancel then
65 self.button:Disable()
66 end
67 end
68
69 local function Button_OnClick(this)
70 local editbox = this.obj.editbox
71 editbox:ClearFocus()
72 EditBox_OnEnterPressed(editbox)
73 end
74
75 local function EditBox_OnReceiveDrag(this)
76 local self = this.obj
77 local type, id, info = GetCursorInfo()
78 if type == "item" then
79 self:SetText(info)
80 self:Fire("OnEnterPressed",info)
81 ClearCursor()
82 elseif type == "spell" then
83 local name, rank = GetSpellName(id, info)
84 if rank and rank:match("%d") then
85 name = name.."("..rank..")"
86 end
87 self:SetText(name)
88 self:Fire("OnEnterPressed",name)
89 ClearCursor()
90 end
91 --self.button:Disable()
92 AceGUI:ClearFocus()
93 end
94
95 function MultiLineEditBox:OnAcquire()
96 self:SetWidth(200)
97 self:SetHeight(116)
98 self:SetNumLines(4)
99 self:SetDisabled(false)
100 self:ShowButton(true)
101 end
102
103 function MultiLineEditBox:OnRelease()
104 self.frame:ClearAllPoints()
105 self.frame:Hide()
106 self:SetDisabled(false)
107 end
108
109 function MultiLineEditBox:SetDisabled(disabled)
110 self.disabled = disabled
111 if disabled then
112 self.editbox:EnableMouse(false)
113 self.scrollframe:EnableMouse(false)
114 self.editbox:ClearFocus()
115 self.editbox:SetTextColor(0.5, 0.5, 0.5)
116 self.label:SetTextColor(0.5,0.5,0.5)
117 self.button:Disable()
118 else
119 self.editbox:EnableMouse(true)
120 self.scrollframe:EnableMouse(true)
121 self.editbox:SetTextColor(1, 1, 1)
122 self.label:SetTextColor(1,.82,0)
123 self.button:Enable()
124 end
125 end
126
127 function MultiLineEditBox:SetText(text)
128 text = text or ""
129 local editbox = self.editbox
130 local oldText = editbox:GetText()
131 local dummy = format(" %s", text)
132 self.lasttext = dummy -- prevents OnTextChanged from firing
133 editbox:SetText(dummy)
134 editbox:HighlightText(0, 1)
135 self.lasttext = oldText
136 editbox:Insert("")
137 end
138
139 function MultiLineEditBox:SetLabel(text)
140 if (text or "") == "" then
141 self.backdrop:SetPoint("TOPLEFT",self.frame,"TOPLEFT",0,0)
142 self.label:Hide()
143 self.label:SetText("")
144 else
145 self.backdrop:SetPoint("TOPLEFT",self.frame,"TOPLEFT",0,-20)
146 self.label:Show()
147 self.label:SetText(text)
148 end
149 end
150
151 function MultiLineEditBox:SetNumLines(number)
152 number = number or 4
153 self:SetHeight(60 + (14*number))
154 end
155
156 function MultiLineEditBox:GetText()
157 return self.editbox:GetText()
158 end
159
160 function MultiLineEditBox:ShowButton(show)
161 if show then
162 self.backdrop:SetPoint("BOTTOMRIGHT",self.frame,"BOTTOMRIGHT",0,22)
163 self.button:Show()
164 else
165 self.backdrop:SetPoint("BOTTOMRIGHT",self.frame,"BOTTOMRIGHT",0,0)
166 self.button:Hide()
167 end
168 end
169
170 local function Constructor()
171 local frame = CreateFrame("Frame", nil, UIParent)
172 local backdrop = CreateFrame("Frame", nil, frame)
173 local self = {}
174 for k, v in pairs(MultiLineEditBox) do self[k] = v end
175 self.type = Type
176 self.frame = frame
177 self.backdrop = backdrop
178 frame.obj = self
179
180 backdrop:SetBackdrop(ControlBackdrop)
181 backdrop:SetBackdropColor(0, 0, 0)
182 backdrop:SetBackdropBorderColor(0.4, 0.4, 0.4)
183
184 backdrop:SetPoint("TOPLEFT",frame,"TOPLEFT",0, -20)
185 backdrop:SetPoint("BOTTOMRIGHT",frame,"BOTTOMRIGHT",0,22)
186
187 local scrollframe = CreateFrame("ScrollFrame", format("%s@%s@%s", Type, "ScrollFrame", tostring(self)), backdrop, "UIPanelScrollFrameTemplate")
188 scrollframe:SetPoint("TOPLEFT", 5, -6)
189 scrollframe:SetPoint("BOTTOMRIGHT", -28, 6)
190 scrollframe.obj = self
191 self.scrollframe = scrollframe
192
193 --local scrollchild = CreateFrame("Frame", nil, scrollframe)
194 --scrollframe:SetScrollChild(scrollchild)
195 --scrollchild:SetHeight(2)
196 --scrollchild:SetWidth(2)
197
198 local label = frame:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
199 label:SetPoint("TOPLEFT",frame,"TOPLEFT",0,-2)
200 label:SetPoint("TOPRIGHT",frame,"TOPRIGHT",0,-2)
201 label:SetJustifyH("LEFT")
202 label:SetHeight(18)
203 self.label = label
204
205 local editbox = CreateFrame("EditBox", nil, scrollframe)
206 self.editbox = editbox
207 editbox.obj = self
208 editbox:SetPoint("TOPLEFT")
209 editbox:SetPoint("BOTTOMLEFT")
210 editbox:SetHeight(50)
211 editbox:SetWidth(50)
212 editbox:SetMultiLine(true)
213 -- editbox:SetMaxLetters(7500)
214 editbox:SetTextInsets(5, 5, 3, 3)
215 editbox:EnableMouse(true)
216 editbox:SetAutoFocus(false)
217 editbox:SetFontObject(ChatFontNormal)
218 scrollframe:SetScrollChild(editbox)
219
220 local button = CreateFrame("Button",nil,scrollframe,"UIPanelButtonTemplate")
221 button:SetWidth(80)
222 button:SetHeight(20)
223 button:SetPoint("BOTTOMLEFT",frame,"BOTTOMLEFT",0,2)
224 button:SetText(ACCEPT)
225 button:SetScript("OnClick", Button_OnClick)
226 button:SetFrameLevel(editbox:GetFrameLevel() + 1)
227 button:Disable()
228 button:Hide()
229 self.button = button
230 button.obj = self
231
232 scrollframe:EnableMouse(true)
233 scrollframe:SetScript("OnMouseUp", function() editbox:SetFocus() end)
234 scrollframe:SetScript("OnEnter", function(this) this.obj:Fire("OnEnter") end)
235 scrollframe:SetScript("OnLeave", function(this) this.obj:Fire("OnLeave") end)
236
237 editbox:SetScript("OnEnter", function(this) this.obj:Fire("OnEnter") end)
238 editbox:SetScript("OnLeave", function(this) this.obj:Fire("OnLeave") end)
239
240 local function FixSize()
241 --scrollchild:SetHeight(scrollframe:GetHeight())
242 --scrollchild:SetWidth(scrollframe:GetWidth())
243 editbox:SetWidth(scrollframe:GetWidth())
244 end
245 scrollframe:SetScript("OnShow", FixSize)
246 scrollframe:SetScript("OnSizeChanged", FixSize)
247
248 editbox:SetScript("OnEscapePressed", editbox.ClearFocus)
249 editbox:SetScript("OnTextChanged", function(_, ...)
250 scrollframe:UpdateScrollChildRect()
251 local value = editbox:GetText()
252 if value ~= self.lasttext then
253 self:Fire("OnTextChanged", value)
254 self.lasttext = value
255 if not self.disabled then
256 self.button:Enable()
257 end
258 end
259 end)
260
261 editbox:SetScript("OnReceiveDrag", EditBox_OnReceiveDrag)
262 editbox:SetScript("OnMouseDown", EditBox_OnReceiveDrag)
263
264 do
265 local cursorOffset, cursorHeight
266 local idleTime
267 local function FixScroll(_, elapsed)
268 if cursorOffset and cursorHeight then
269 idleTime = 0
270 local height = scrollframe:GetHeight()
271 local range = scrollframe:GetVerticalScrollRange()
272 local scroll = scrollframe:GetVerticalScroll()
273 local size = height + range
274 cursorOffset = -cursorOffset
275 while cursorOffset < scroll do
276 scroll = scroll - (height / 2)
277 if scroll < 0 then scroll = 0 end
278 scrollframe:SetVerticalScroll(scroll)
279 end
280 while cursorOffset + cursorHeight > scroll + height and scroll < range do
281 scroll = scroll + (height / 2)
282 if scroll > range then scroll = range end
283 scrollframe:SetVerticalScroll(scroll)
284 end
285 elseif not idleTime or idleTime > 2 then
286 frame:SetScript("OnUpdate", nil)
287 idleTime = nil
288 else
289 idleTime = idleTime + elapsed
290 end
291 cursorOffset = nil
292 end
293 editbox:SetScript("OnCursorChanged", function(_, x, y, w, h)
294 cursorOffset, cursorHeight = y, h
295 if not idleTime then
296 frame:SetScript("OnUpdate", FixScroll)
297 end
298 end)
299 end
300
301 AceGUI:RegisterAsWidget(self)
302 return self
303 end
304
305 AceGUI:RegisterWidgetType(Type, Constructor, Version)
306 end
307
308
309