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