annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-MultiLineEditBox.lua @ 57:01b63b8ed811 v21

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