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