Zerotorescue@0: --[[----------------------------------------------------------------------------- Zerotorescue@0: EditBox Widget Zerotorescue@0: -------------------------------------------------------------------------------]] Zerotorescue@0: local Type, Version = "EditBox", 21 Zerotorescue@0: local AceGUI = LibStub and LibStub("AceGUI-3.0", true) Zerotorescue@0: if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end Zerotorescue@0: Zerotorescue@0: -- Lua APIs Zerotorescue@0: local tostring, pairs = tostring, pairs Zerotorescue@0: Zerotorescue@0: -- WoW APIs Zerotorescue@0: local PlaySound = PlaySound Zerotorescue@0: local GetCursorInfo, ClearCursor, GetSpellName = GetCursorInfo, ClearCursor, GetSpellName Zerotorescue@0: local CreateFrame, UIParent = CreateFrame, UIParent Zerotorescue@0: local _G = _G Zerotorescue@0: Zerotorescue@0: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded Zerotorescue@0: -- List them here for Mikk's FindGlobals script Zerotorescue@0: -- GLOBALS: AceGUIEditBoxInsertLink, ChatFontNormal, OKAY Zerotorescue@0: Zerotorescue@0: --[[----------------------------------------------------------------------------- Zerotorescue@0: Support functions Zerotorescue@0: -------------------------------------------------------------------------------]] Zerotorescue@0: if not AceGUIEditBoxInsertLink then Zerotorescue@0: -- upgradeable hook Zerotorescue@0: hooksecurefunc("ChatEdit_InsertLink", function(...) return _G.AceGUIEditBoxInsertLink(...) end) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: function _G.AceGUIEditBoxInsertLink(text) Zerotorescue@0: for i = 1, AceGUI:GetWidgetCount(Type) do Zerotorescue@0: local editbox = _G["AceGUI-3.0EditBox"..i] Zerotorescue@0: if editbox and editbox:IsVisible() and editbox:HasFocus() then Zerotorescue@0: editbox:Insert(text) Zerotorescue@0: return true Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function ShowButton(self) Zerotorescue@0: if not self.disablebutton then Zerotorescue@0: self.button:Show() Zerotorescue@0: self.editbox:SetTextInsets(0, 20, 3, 3) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function HideButton(self) Zerotorescue@0: self.button:Hide() Zerotorescue@0: self.editbox:SetTextInsets(0, 0, 3, 3) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --[[----------------------------------------------------------------------------- Zerotorescue@0: Scripts Zerotorescue@0: -------------------------------------------------------------------------------]] Zerotorescue@0: local function Control_OnEnter(frame) Zerotorescue@0: frame.obj:Fire("OnEnter") Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function Control_OnLeave(frame) Zerotorescue@0: frame.obj:Fire("OnLeave") Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function EditBox_OnEscapePressed(frame) Zerotorescue@0: AceGUI:ClearFocus() Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function EditBox_OnEnterPressed(frame) Zerotorescue@0: local self = frame.obj Zerotorescue@0: local value = frame:GetText() Zerotorescue@0: local cancel = self:Fire("OnEnterPressed", value) Zerotorescue@0: if not cancel then Zerotorescue@0: PlaySound("igMainMenuOptionCheckBoxOn") Zerotorescue@0: HideButton(self) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function EditBox_OnReceiveDrag(frame) Zerotorescue@0: local self = frame.obj Zerotorescue@0: local type, id, info = GetCursorInfo() Zerotorescue@0: if type == "item" then Zerotorescue@0: self:SetText(info) Zerotorescue@0: self:Fire("OnEnterPressed", info) Zerotorescue@0: ClearCursor() Zerotorescue@0: elseif type == "spell" then Zerotorescue@0: local name, rank = GetSpellName(id, info) Zerotorescue@0: if rank and rank:match("%d") then Zerotorescue@0: name = name.."("..rank..")" Zerotorescue@0: end Zerotorescue@0: self:SetText(name) Zerotorescue@0: self:Fire("OnEnterPressed", name) Zerotorescue@0: ClearCursor() Zerotorescue@0: end Zerotorescue@0: HideButton(self) Zerotorescue@0: AceGUI:ClearFocus() Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function EditBox_OnTextChanged(frame) Zerotorescue@0: local self = frame.obj Zerotorescue@0: local value = frame:GetText() Zerotorescue@0: if tostring(value) ~= tostring(self.lasttext) then Zerotorescue@0: self:Fire("OnTextChanged", value) Zerotorescue@0: self.lasttext = value Zerotorescue@0: ShowButton(self) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function Button_OnClick(frame) Zerotorescue@0: local editbox = frame.obj.editbox Zerotorescue@0: editbox:ClearFocus() Zerotorescue@0: EditBox_OnEnterPressed(editbox) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --[[----------------------------------------------------------------------------- Zerotorescue@0: Methods Zerotorescue@0: -------------------------------------------------------------------------------]] Zerotorescue@0: local methods = { Zerotorescue@0: ["OnAcquire"] = function(self) Zerotorescue@0: -- height is controlled by SetLabel Zerotorescue@0: self:SetWidth(200) Zerotorescue@0: self:SetDisabled(false) Zerotorescue@0: self:SetLabel() Zerotorescue@0: self:SetText() Zerotorescue@0: self:DisableButton(false) Zerotorescue@0: self:SetMaxLetters(0) Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: -- ["OnRelease"] = nil, Zerotorescue@0: Zerotorescue@0: ["SetDisabled"] = function(self, disabled) Zerotorescue@0: self.disabled = disabled Zerotorescue@0: if disabled then Zerotorescue@0: self.editbox:EnableMouse(false) Zerotorescue@0: self.editbox:ClearFocus() Zerotorescue@0: self.editbox:SetTextColor(0.5,0.5,0.5) Zerotorescue@0: self.label:SetTextColor(0.5,0.5,0.5) Zerotorescue@0: else Zerotorescue@0: self.editbox:EnableMouse(true) Zerotorescue@0: self.editbox:SetTextColor(1,1,1) Zerotorescue@0: self.label:SetTextColor(1,.82,0) Zerotorescue@0: end Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["SetText"] = function(self, text) Zerotorescue@0: self.lasttext = text or "" Zerotorescue@0: self.editbox:SetText(text or "") Zerotorescue@0: self.editbox:SetCursorPosition(0) Zerotorescue@0: HideButton(self) Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["SetLabel"] = function(self, text) Zerotorescue@0: if text and text ~= "" then Zerotorescue@0: self.label:SetText(text) Zerotorescue@0: self.label:Show() Zerotorescue@0: self.editbox:SetPoint("TOPLEFT",self.frame,"TOPLEFT",7,-18) Zerotorescue@0: self:SetHeight(44) Zerotorescue@0: self.alignoffset = 30 Zerotorescue@0: else Zerotorescue@0: self.label:SetText("") Zerotorescue@0: self.label:Hide() Zerotorescue@0: self.editbox:SetPoint("TOPLEFT",self.frame,"TOPLEFT",7,0) Zerotorescue@0: self:SetHeight(26) Zerotorescue@0: self.alignoffset = 12 Zerotorescue@0: end Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["DisableButton"] = function(self, disabled) Zerotorescue@0: self.disablebutton = disabled Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["SetMaxLetters"] = function (self, num) Zerotorescue@0: self.editbox:SetMaxLetters(num or 0) Zerotorescue@0: end Zerotorescue@0: } Zerotorescue@0: Zerotorescue@0: --[[----------------------------------------------------------------------------- Zerotorescue@0: Constructor Zerotorescue@0: -------------------------------------------------------------------------------]] Zerotorescue@0: local function Constructor() Zerotorescue@0: local num = AceGUI:GetNextWidgetNum(Type) Zerotorescue@0: local frame = CreateFrame("Frame", nil, UIParent) Zerotorescue@0: frame:Hide() Zerotorescue@0: Zerotorescue@0: local editbox = CreateFrame("EditBox", "AceGUI-3.0EditBox"..num, frame, "InputBoxTemplate") Zerotorescue@0: editbox:SetAutoFocus(false) Zerotorescue@0: editbox:SetFontObject(ChatFontNormal) Zerotorescue@0: editbox:SetScript("OnEnter", Control_OnEnter) Zerotorescue@0: editbox:SetScript("OnLeave", Control_OnLeave) Zerotorescue@0: editbox:SetScript("OnEscapePressed", EditBox_OnEscapePressed) Zerotorescue@0: editbox:SetScript("OnEnterPressed", EditBox_OnEnterPressed) Zerotorescue@0: editbox:SetScript("OnTextChanged", EditBox_OnTextChanged) Zerotorescue@0: editbox:SetScript("OnReceiveDrag", EditBox_OnReceiveDrag) Zerotorescue@0: editbox:SetScript("OnMouseDown", EditBox_OnReceiveDrag) Zerotorescue@0: editbox:SetTextInsets(0, 0, 3, 3) Zerotorescue@0: editbox:SetMaxLetters(256) Zerotorescue@0: editbox:SetPoint("BOTTOMLEFT", 6, 0) Zerotorescue@0: editbox:SetPoint("BOTTOMRIGHT") Zerotorescue@0: editbox:SetHeight(19) Zerotorescue@0: Zerotorescue@0: local label = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall") Zerotorescue@0: label:SetPoint("TOPLEFT", 0, -2) Zerotorescue@0: label:SetPoint("TOPRIGHT", 0, -2) Zerotorescue@0: label:SetJustifyH("LEFT") Zerotorescue@0: label:SetHeight(18) Zerotorescue@0: Zerotorescue@0: local button = CreateFrame("Button", nil, editbox, "UIPanelButtonTemplate") Zerotorescue@0: button:SetWidth(40) Zerotorescue@0: button:SetHeight(20) Zerotorescue@0: button:SetPoint("RIGHT", -2, 0) Zerotorescue@0: button:SetText(OKAY) Zerotorescue@0: button:SetScript("OnClick", Button_OnClick) Zerotorescue@0: button:Hide() Zerotorescue@0: Zerotorescue@0: local widget = { Zerotorescue@0: alignoffset = 30, Zerotorescue@0: editbox = editbox, Zerotorescue@0: label = label, Zerotorescue@0: button = button, Zerotorescue@0: frame = frame, Zerotorescue@0: type = Type Zerotorescue@0: } Zerotorescue@0: for method, func in pairs(methods) do Zerotorescue@0: widget[method] = func Zerotorescue@0: end Zerotorescue@0: editbox.obj, button.obj = widget, widget Zerotorescue@0: Zerotorescue@0: return AceGUI:RegisterAsWidget(widget) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: AceGUI:RegisterWidgetType(Type, Constructor, Version)