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