annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-MultiLineEditBox.lua @ 62:d6704922ef5d v8.2.0.062

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