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