annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-MultiLineEditBox.lua @ 0:fc346da3afd9

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