annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-MultiLineEditBox.lua @ 180:82598dc4fe2e

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