annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-MultiLineEditBox.lua @ 48:a671a2cf52ee

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