yellowfive@57: --[[----------------------------------------------------------------------------- yellowfive@57: Textarea Widget yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: local Type, Version = "AmrUiTextarea", 1 yellowfive@57: local AceGUI = LibStub and LibStub("AceGUI-3.0", true) yellowfive@57: if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end yellowfive@57: yellowfive@57: local Amr = LibStub("AceAddon-3.0"):GetAddon("AskMrRobot") yellowfive@57: yellowfive@57: --[[----------------------------------------------------------------------------- yellowfive@57: Scripts yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: yellowfive@57: -- handles clicking in the scrollframe but below the bounds of the editbox (which can't be sized to same as scrollframe) yellowfive@57: local function scrollFrameMouseUp(self) yellowfive@57: local editbox = self.obj.editbox yellowfive@57: editbox:SetFocus() yellowfive@57: editbox:SetCursorPosition(editbox:GetNumLetters()) yellowfive@57: end yellowfive@57: yellowfive@57: local function scrollFrameVerticalScroll(self, offset) yellowfive@57: local editbox = self.obj.editbox yellowfive@57: editbox:SetHitRectInsets(0, 0, offset, editbox:GetHeight() - offset - self:GetHeight()) yellowfive@57: end yellowfive@57: yellowfive@57: local function editboxCursorChanged(self, _, y, _, cursorHeight) yellowfive@57: self, y = self.obj.scrollFrame, -y yellowfive@57: local offset = self:GetVerticalScroll() yellowfive@57: if y < offset then yellowfive@57: self:SetVerticalScroll(y) yellowfive@57: else yellowfive@57: y = y + cursorHeight - self:GetHeight() yellowfive@57: if y > offset then yellowfive@57: self:SetVerticalScroll(y) yellowfive@57: end yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: local function editboxEditFocusLost(self) yellowfive@57: self:HighlightText(0, 0) yellowfive@57: self.obj:Fire("OnEditFocusLost") yellowfive@57: end yellowfive@57: yellowfive@57: local function editboxEditFocusGained(frame) yellowfive@57: AceGUI:SetFocus(frame.obj) yellowfive@57: frame.obj:Fire("OnEditFocusGained") yellowfive@57: end yellowfive@57: yellowfive@57: local function editboxTextChanged(self, userInput) yellowfive@57: if userInput then yellowfive@57: self = self.obj yellowfive@57: self:Fire("OnTextChanged", self.editbox:GetText()) yellowfive@57: end yellowfive@57: end yellowfive@57: yellowfive@57: local function editboxTextSet(self) yellowfive@57: self:HighlightText() yellowfive@57: self:SetCursorPosition(self:GetNumLetters()) yellowfive@57: self = self.obj yellowfive@57: self:Fire("OnTextSet", self.editbox:GetText()) yellowfive@57: end yellowfive@57: yellowfive@61: local function editboxEnterPressed(self) yellowfive@61: self.obj:Fire("OnEnterPressed") yellowfive@61: end yellowfive@61: yellowfive@57: -- works for both the scrollframe and the editbox, handles e.g. dragging a spell link into the textarea yellowfive@57: local function onReceiveDrag(self) yellowfive@57: local type, id, info = GetCursorInfo() yellowfive@57: if type == "spell" then yellowfive@57: info = GetSpellInfo(id, info) yellowfive@57: elseif type ~= "item" then yellowfive@57: return yellowfive@57: end yellowfive@57: ClearCursor() yellowfive@57: self = self.obj yellowfive@57: local editbox = self.editbox yellowfive@57: if not editbox:HasFocus() then yellowfive@57: editbox:SetFocus() yellowfive@57: editbox:SetCursorPosition(editbox:GetNumLetters()) yellowfive@57: end yellowfive@57: editbox:Insert(info) yellowfive@57: end yellowfive@57: yellowfive@57: --[[----------------------------------------------------------------------------- yellowfive@57: Methods yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: local methods = { yellowfive@57: ["OnAcquire"] = function(self) yellowfive@57: -- restore default values yellowfive@57: self:SetWidth(200) yellowfive@57: self:SetHeight(24) yellowfive@61: self:SetMultiLine(true) yellowfive@57: self:SetFont(Amr.CreateFont("Regular", 16, Amr.Colors.Text)) yellowfive@57: self:SetText("") yellowfive@57: self.frame:ClearAllPoints() yellowfive@57: end, yellowfive@57: yellowfive@57: ["SetText"] = function(self, text) yellowfive@57: self.editbox:SetText(text) yellowfive@57: end, yellowfive@57: yellowfive@57: ["GetText"] = function(self) yellowfive@57: return self.editbox:GetText() yellowfive@57: end, yellowfive@57: yellowfive@57: ["SetFont"] = function(self, font) yellowfive@57: self.editbox:SetFontObject(font) yellowfive@57: end, yellowfive@57: yellowfive@57: ["SetDisabled"] = function(self, disabled) yellowfive@57: self.disabled = disabled yellowfive@57: if disabled then yellowfive@57: self.editbox:Disable() yellowfive@57: else yellowfive@57: self.editbox:Enable() yellowfive@57: end yellowfive@57: end, yellowfive@57: yellowfive@57: ["OnWidthSet"] = function(self, width) yellowfive@57: self.editbox:SetWidth(width) yellowfive@57: end, yellowfive@57: yellowfive@57: ["OnHeightSet"] = function(self, height) yellowfive@57: self.editbox:SetHeight(height) yellowfive@57: end, yellowfive@57: yellowfive@57: ["ClearFocus"] = function(self) yellowfive@57: self.editbox:ClearFocus() yellowfive@61: self.editbox:HighlightText(0, 0) yellowfive@57: self.frame:SetScript("OnShow", nil) yellowfive@57: end, yellowfive@61: yellowfive@61: ["SetMultiLine"] = function(self, multi) yellowfive@61: self.editbox:SetMultiLine(multi) yellowfive@61: end, yellowfive@57: yellowfive@57: ["SetFocus"] = function(self, highlight) yellowfive@57: self.editbox:SetFocus() yellowfive@57: if highlight then yellowfive@57: self.editbox:HighlightText() yellowfive@57: end yellowfive@57: if not self.frame:IsShown() then yellowfive@57: self.frame:SetScript("OnShow", function(frame) yellowfive@57: frame.obj.editbox:SetFocus() yellowfive@57: if highlight then yellowfive@57: self.editbox:HighlightText() yellowfive@57: end yellowfive@57: frame:SetScript("OnShow", nil) yellowfive@57: end) yellowfive@57: end yellowfive@57: end yellowfive@57: } yellowfive@57: yellowfive@57: --[[----------------------------------------------------------------------------- yellowfive@57: Constructor yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: local function Constructor() yellowfive@57: local name = "AmrUiTextarea" .. AceGUI:GetNextWidgetNum(Type) yellowfive@57: local frame = CreateFrame("ScrollFrame", name, UIParent) yellowfive@57: frame:Hide() yellowfive@57: yellowfive@57: frame:SetScript("OnMouseUp", scrollFrameMouseUp) yellowfive@57: frame:SetScript("OnReceiveDrag", onReceiveDrag) yellowfive@57: frame:HookScript("OnVerticalScroll", scrollFrameVerticalScroll) yellowfive@57: yellowfive@57: local editbox = CreateFrame("EditBox", name .. "Edit", frame) yellowfive@57: editbox:SetAllPoints() yellowfive@57: editbox:EnableMouse(true) yellowfive@57: editbox:SetMultiLine(true) yellowfive@57: editbox:SetAutoFocus(false) yellowfive@57: editbox:SetCountInvisibleLetters(false) yellowfive@57: editbox:SetTextInsets(4, 4, 4, 4) yellowfive@57: yellowfive@57: editbox:SetScript("OnMouseDown", onReceiveDrag) yellowfive@57: editbox:SetScript("OnReceiveDrag", onReceiveDrag) yellowfive@57: yellowfive@57: editbox:SetScript("OnCursorChanged", editboxCursorChanged) yellowfive@57: editbox:SetScript("OnEscapePressed", editbox.ClearFocus) yellowfive@61: editbox:SetScript("OnEnterPressed", editboxEnterPressed) yellowfive@57: editbox:SetScript("OnEditFocusLost", editboxEditFocusLost) yellowfive@57: editbox:SetScript("OnTextChanged", editboxTextChanged) yellowfive@57: editbox:SetScript("OnTextSet", editboxTextSet) yellowfive@57: editbox:SetScript("OnEditFocusGained", editboxEditFocusGained) yellowfive@57: yellowfive@57: frame:SetScrollChild(editbox) yellowfive@57: yellowfive@57: local border = frame:CreateTexture(nil, "BACKGROUND") yellowfive@57: border:SetTexture(Amr.Colors.BorderGray.R, Amr.Colors.BorderGray.G, Amr.Colors.BorderGray.B, 1) yellowfive@57: border:SetAllPoints(true) yellowfive@57: yellowfive@57: local bg = frame:CreateTexture(nil, "BORDER") yellowfive@57: bg:SetTexture(Amr.Colors.BgInput.R, Amr.Colors.BgInput.G, Amr.Colors.BgInput.B, 1) yellowfive@57: bg:SetPoint("TOPLEFT", frame, "TOPLEFT", 1, -1) yellowfive@57: bg:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -1, 1) yellowfive@57: yellowfive@57: local widget = { yellowfive@57: editbox = editbox, yellowfive@57: scrollFrame = frame, yellowfive@57: frame = frame, yellowfive@57: type = Type yellowfive@57: } yellowfive@57: for method, func in pairs(methods) do yellowfive@57: widget[method] = func yellowfive@57: end yellowfive@57: editbox.obj, frame.obj = widget, widget yellowfive@57: yellowfive@57: return AceGUI:RegisterAsWidget(widget) yellowfive@57: end yellowfive@57: yellowfive@57: AceGUI:RegisterWidgetType(Type, Constructor, Version)