annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-MultiLineEditBox.lua @ 11:371e14cd2feb

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