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