Tercio@11: Tercio@11: local DF = _G ["DetailsFramework"] Tercio@20: if (not DF or not DetailsFrameworkCanLoad) then Tercio@20: return Tercio@20: end Tercio@20: Tercio@11: local _ Tercio@11: local _rawset = rawset --> lua local Tercio@11: local _rawget = rawget --> lua local Tercio@11: local _setmetatable = setmetatable --> lua local Tercio@11: local _unpack = unpack --> lua local Tercio@11: local _type = type --> lua local Tercio@11: local _math_floor = math.floor --> lua local Tercio@11: local loadstring = loadstring --> lua local Tercio@11: local _string_len = string.len --> lua local Tercio@11: Tercio@11: local cleanfunction = function() end Tercio@11: local APITextEntryFunctions = false Tercio@11: Tercio@39: do Tercio@39: local metaPrototype = { Tercio@39: WidgetType = "textentry", Tercio@39: SetHook = DF.SetHook, Tercio@39: RunHooksForWidget = DF.RunHooksForWidget, Tercio@39: } Tercio@39: Tercio@39: _G [DF.GlobalWidgetControlNames ["textentry"]] = _G [DF.GlobalWidgetControlNames ["textentry"]] or metaPrototype Tercio@39: end Tercio@39: Tercio@39: local TextEntryMetaFunctions = _G [DF.GlobalWidgetControlNames ["textentry"]] Tercio@39: DF.TextEntryCounter = DF.TextEntryCounter or 1 Tercio@11: Tercio@11: ------------------------------------------------------------------------------------------------------------ Tercio@11: --> metatables Tercio@11: Tercio@11: TextEntryMetaFunctions.__call = function (_table, value) Tercio@11: --> unknow Tercio@11: end Tercio@11: Tercio@11: ------------------------------------------------------------------------------------------------------------ Tercio@11: --> members Tercio@11: Tercio@11: --> tooltip Tercio@11: local gmember_tooltip = function (_object) Tercio@11: return _object:GetTooltip() Tercio@11: end Tercio@11: --> shown Tercio@11: local gmember_shown = function (_object) Tercio@11: return _object:IsShown() Tercio@11: end Tercio@11: --> frame width Tercio@11: local gmember_width = function (_object) Tercio@11: return _object.editbox:GetWidth() Tercio@11: end Tercio@11: --> frame height Tercio@11: local gmember_height = function (_object) Tercio@11: return _object.editbox:GetHeight() Tercio@11: end Tercio@11: --> get text Tercio@11: local gmember_text = function (_object) Tercio@11: return _object.editbox:GetText() Tercio@11: end Tercio@11: Tercio@39: TextEntryMetaFunctions.GetMembers = TextEntryMetaFunctions.GetMembers or {} Tercio@39: TextEntryMetaFunctions.GetMembers ["tooltip"] = gmember_tooltip Tercio@39: TextEntryMetaFunctions.GetMembers ["shown"] = gmember_shown Tercio@39: TextEntryMetaFunctions.GetMembers ["width"] = gmember_width Tercio@39: TextEntryMetaFunctions.GetMembers ["height"] = gmember_height Tercio@39: TextEntryMetaFunctions.GetMembers ["text"] = gmember_text Tercio@11: Tercio@11: TextEntryMetaFunctions.__index = function (_table, _member_requested) Tercio@39: local func = TextEntryMetaFunctions.GetMembers [_member_requested] Tercio@11: if (func) then Tercio@11: return func (_table, _member_requested) Tercio@11: end Tercio@11: Tercio@11: local fromMe = _rawget (_table, _member_requested) Tercio@11: if (fromMe) then Tercio@11: return fromMe Tercio@11: end Tercio@11: Tercio@11: return TextEntryMetaFunctions [_member_requested] Tercio@11: end Tercio@11: Tercio@11: ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Tercio@11: Tercio@11: --> tooltip Tercio@11: local smember_tooltip = function (_object, _value) Tercio@11: return _object:SetTooltip (_value) Tercio@11: end Tercio@11: --> show Tercio@11: local smember_show = function (_object, _value) Tercio@11: if (_value) then Tercio@11: return _object:Show() Tercio@11: else Tercio@11: return _object:Hide() Tercio@11: end Tercio@11: end Tercio@11: --> hide Tercio@11: local smember_hide = function (_object, _value) Tercio@11: if (not _value) then Tercio@11: return _object:Show() Tercio@11: else Tercio@11: return _object:Hide() Tercio@11: end Tercio@11: end Tercio@11: --> frame width Tercio@11: local smember_width = function (_object, _value) Tercio@11: return _object.editbox:SetWidth (_value) Tercio@11: end Tercio@11: --> frame height Tercio@11: local smember_height = function (_object, _value) Tercio@11: return _object.editbox:SetHeight (_value) Tercio@11: end Tercio@11: --> set text Tercio@11: local smember_text = function (_object, _value) Tercio@11: return _object.editbox:SetText (_value) Tercio@11: end Tercio@11: --> set multiline Tercio@11: local smember_multiline = function (_object, _value) Tercio@11: if (_value) then Tercio@11: return _object.editbox:SetMultiLine (true) Tercio@11: else Tercio@11: return _object.editbox:SetMultiLine (false) Tercio@11: end Tercio@11: end Tercio@11: --> text horizontal pos Tercio@11: local smember_horizontalpos = function (_object, _value) Tercio@11: return _object.editbox:SetJustifyH (string.lower (_value)) Tercio@11: end Tercio@11: Tercio@39: TextEntryMetaFunctions.SetMembers = TextEntryMetaFunctions.SetMembers or {} Tercio@39: TextEntryMetaFunctions.SetMembers ["tooltip"] = smember_tooltip Tercio@39: TextEntryMetaFunctions.SetMembers ["show"] = smember_show Tercio@39: TextEntryMetaFunctions.SetMembers ["hide"] = smember_hide Tercio@39: TextEntryMetaFunctions.SetMembers ["width"] = smember_width Tercio@39: TextEntryMetaFunctions.SetMembers ["height"] = smember_height Tercio@39: TextEntryMetaFunctions.SetMembers ["text"] = smember_text Tercio@39: TextEntryMetaFunctions.SetMembers ["multiline"] = smember_multiline Tercio@39: TextEntryMetaFunctions.SetMembers ["align"] = smember_horizontalpos Tercio@11: Tercio@11: TextEntryMetaFunctions.__newindex = function (_table, _key, _value) Tercio@39: local func = TextEntryMetaFunctions.SetMembers [_key] Tercio@11: if (func) then Tercio@11: return func (_table, _value) Tercio@11: else Tercio@11: return _rawset (_table, _key, _value) Tercio@11: end Tercio@11: end Tercio@11: Tercio@11: ------------------------------------------------------------------------------------------------------------ Tercio@11: --> methods Tercio@39: local cleanfunction = function()end Tercio@39: function TextEntryMetaFunctions:SetEnterFunction (func, param1, param2) Tercio@39: if (func) then Tercio@39: _rawset (self, "func", func) Tercio@39: else Tercio@39: _rawset (self, "func", cleanfunction) Tercio@39: end Tercio@39: Tercio@39: if (param1 ~= nil) then Tercio@39: _rawset (self, "param1", param1) Tercio@39: end Tercio@39: if (param2 ~= nil) then Tercio@39: _rawset (self, "param2", param2) Tercio@39: end Tercio@39: end Tercio@11: Tercio@11: --> set point Tercio@11: function TextEntryMetaFunctions:SetPoint (MyAnchor, SnapTo, HisAnchor, x, y, Width) Tercio@11: Tercio@11: if (type (MyAnchor) == "boolean" and MyAnchor and self.space) then Tercio@11: local textWidth = self.label:GetStringWidth()+2 Tercio@11: self.editbox:SetWidth (self.space - textWidth - 15) Tercio@11: return Tercio@11: Tercio@11: elseif (type (MyAnchor) == "boolean" and MyAnchor and not self.space) then Tercio@11: self.space = self.label:GetStringWidth()+2 + self.editbox:GetWidth() Tercio@11: end Tercio@11: Tercio@11: if (Width) then Tercio@11: self.space = Width Tercio@11: end Tercio@11: Tercio@11: MyAnchor, SnapTo, HisAnchor, x, y = DF:CheckPoints (MyAnchor, SnapTo, HisAnchor, x, y, self) Tercio@11: if (not MyAnchor) then Tercio@11: print ("Invalid parameter for SetPoint") Tercio@11: return Tercio@11: end Tercio@11: Tercio@11: if (self.space) then Tercio@11: self.label:ClearAllPoints() Tercio@11: self.editbox:ClearAllPoints() Tercio@11: Tercio@11: self.label:SetPoint (MyAnchor, SnapTo, HisAnchor, x, y) Tercio@11: self.editbox:SetPoint ("left", self.label, "right", 2, 0) Tercio@11: Tercio@11: local textWidth = self.label:GetStringWidth()+2 Tercio@11: self.editbox:SetWidth (self.space - textWidth - 15) Tercio@11: else Tercio@11: self.label:ClearAllPoints() Tercio@11: self.editbox:ClearAllPoints() Tercio@11: self.editbox:SetPoint (MyAnchor, SnapTo, HisAnchor, x, y) Tercio@11: end Tercio@11: Tercio@11: end Tercio@11: Tercioo@29: function TextEntryMetaFunctions:SetText (text) Tercioo@29: self.editbox:SetText (text) Tercioo@29: end Tercioo@29: function TextEntryMetaFunctions:GetText() Tercioo@29: return self.editbox:GetText() Tercioo@29: end Tercioo@29: Tercio@11: --> frame levels Tercio@11: function TextEntryMetaFunctions:GetFrameLevel() Tercio@11: return self.editbox:GetFrameLevel() Tercio@11: end Tercio@11: function TextEntryMetaFunctions:SetFrameLevel (level, frame) Tercio@11: if (not frame) then Tercio@11: return self.editbox:SetFrameLevel (level) Tercio@11: else Tercio@11: local framelevel = frame:GetFrameLevel (frame) + level Tercio@11: return self.editbox:SetFrameLevel (framelevel) Tercio@11: end Tercio@11: end Tercio@11: Tercio@11: --> select all text Tercio@11: function TextEntryMetaFunctions:SelectAll() Tercio@11: self.editbox:HighlightText() Tercio@11: end Tercio@11: Tercio@11: --> set labal description Tercio@11: function TextEntryMetaFunctions:SetLabelText (text) Tercio@11: if (text) then Tercio@11: self.label:SetText (text) Tercio@11: else Tercio@11: self.label:SetText ("") Tercio@11: end Tercio@11: self:SetPoint (true) --> refresh Tercio@11: end Tercio@11: Tercio@11: --> set tab order Tercio@11: function TextEntryMetaFunctions:SetNext (nextbox) Tercio@11: self.next = nextbox Tercio@11: end Tercio@11: Tercio@11: --> blink Tercio@11: function TextEntryMetaFunctions:Blink() Tercio@11: self.label:SetTextColor (1, .2, .2, 1) Tercio@11: end Tercio@11: Tercio@11: --> show & hide Tercio@11: function TextEntryMetaFunctions:IsShown() Tercio@11: return self.editbox:IsShown() Tercio@11: end Tercio@11: function TextEntryMetaFunctions:Show() Tercio@11: return self.editbox:Show() Tercio@11: end Tercio@11: function TextEntryMetaFunctions:Hide() Tercio@11: return self.editbox:Hide() Tercio@11: end Tercio@11: Tercio@11: -- tooltip Tercio@11: function TextEntryMetaFunctions:SetTooltip (tooltip) Tercio@11: if (tooltip) then Tercio@11: return _rawset (self, "have_tooltip", tooltip) Tercio@11: else Tercio@11: return _rawset (self, "have_tooltip", nil) Tercio@11: end Tercio@11: end Tercio@11: function TextEntryMetaFunctions:GetTooltip() Tercio@11: return _rawget (self, "have_tooltip") Tercio@11: end Tercio@11: Tercio@11: --> hooks Tercio@11: function TextEntryMetaFunctions:Enable() Tercio@11: if (not self.editbox:IsEnabled()) then Tercio@11: self.editbox:Enable() Tercio@11: self.editbox:SetBackdropBorderColor (unpack (self.enabled_border_color)) Tercio@11: self.editbox:SetBackdropColor (unpack (self.enabled_backdrop_color)) Tercio@11: self.editbox:SetTextColor (unpack (self.enabled_text_color)) Tercio@22: if (self.editbox.borderframe) then Tercio@22: self.editbox.borderframe:SetBackdropColor (unpack (self.editbox.borderframe.onleave_backdrop)) Tercio@22: end Tercio@11: end Tercio@11: end Tercio@11: Tercio@11: function TextEntryMetaFunctions:Disable() Tercio@11: if (self.editbox:IsEnabled()) then Tercio@11: self.enabled_border_color = {self.editbox:GetBackdropBorderColor()} Tercio@11: self.enabled_backdrop_color = {self.editbox:GetBackdropColor()} Tercio@11: self.enabled_text_color = {self.editbox:GetTextColor()} Tercio@11: Tercio@11: self.editbox:Disable() Tercio@11: Tercio@11: self.editbox:SetBackdropBorderColor (.5, .5, .5, .5) Tercio@11: self.editbox:SetBackdropColor (.5, .5, .5, .5) Tercio@11: self.editbox:SetTextColor (.5, .5, .5, .5) Tercio@22: Tercio@22: if (self.editbox.borderframe) then Tercio@22: self.editbox.borderframe:SetBackdropColor (.5, .5, .5, .5) Tercio@22: end Tercio@11: end Tercio@11: end Tercio@11: Tercio@11: ------------------------------------------------------------------------------------------------------------ Tercio@39: --> scripts and hooks Tercio@39: Tercio@11: local OnEnter = function (textentry) Tercio@39: local capsule = textentry.MyObject Tercio@39: Tercio@39: local kill = capsule:RunHooksForWidget ("OnEnter", textentry, capsule) Tercio@39: if (kill) then Tercio@39: return Tercio@39: end Tercio@11: Tercio@39: if (capsule.have_tooltip) then Tercio@11: GameCooltip2:Preset (2) Tercio@39: GameCooltip2:AddLine (capsule.have_tooltip) Tercio@11: GameCooltip2:ShowCooltip (textentry, "tooltip") Tercio@11: end Tercio@11: Tercio@11: textentry.mouse_over = true Tercio@11: Tercio@11: if (textentry:IsEnabled()) then Tercio@11: textentry.current_bordercolor = textentry.current_bordercolor or {textentry:GetBackdropBorderColor()} Tercio@11: textentry:SetBackdropBorderColor (1, 1, 1, 1) Tercio@11: end Tercio@11: end Tercio@11: Tercio@11: local OnLeave = function (textentry) Tercio@39: local capsule = textentry.MyObject Tercio@39: Tercio@39: local kill = capsule:RunHooksForWidget ("OnLeave", textentry, capsule) Tercio@39: if (kill) then Tercio@39: return Tercio@11: end Tercio@39: Tercio@11: if (textentry.MyObject.have_tooltip) then Tercio@11: GameCooltip2:ShowMe (false) Tercio@11: end Tercio@11: Tercio@11: textentry.mouse_over = false Tercio@11: Tercio@11: if (textentry:IsEnabled()) then Tercio@11: textentry:SetBackdropBorderColor (unpack (textentry.current_bordercolor)) Tercio@11: end Tercio@11: end Tercio@11: Tercio@11: local OnHide = function (textentry) Tercio@39: local capsule = textentry.MyObject Tercio@39: Tercio@39: local kill = capsule:RunHooksForWidget ("OnHide", textentry, capsule) Tercio@39: if (kill) then Tercio@39: return Tercio@11: end Tercio@11: end Tercio@11: Tercio@11: local OnShow = function (textentry) Tercio@39: local capsule = textentry.MyObject Tercio@39: Tercio@39: local kill = capsule:RunHooksForWidget ("OnShow", textentry, capsule) Tercio@39: if (kill) then Tercio@39: return Tercio@11: end Tercio@11: end Tercio@11: Tercio@39: local OnEnterPressed = function (textentry, byScript) Tercio@39: local capsule = textentry.MyObject Tercio@11: Tercio@39: local kill = capsule:RunHooksForWidget ("OnEnterPressed", textentry, capsule) Tercio@39: if (kill) then Tercio@39: return Tercio@11: end Tercio@11: Tercio@11: local texto = DF:trim (textentry:GetText()) Tercio@11: if (_string_len (texto) > 0) then Tercio@11: textentry.text = texto Tercio@11: if (textentry.MyObject.func) then Tercio@11: textentry.MyObject.func (textentry.MyObject.param1, textentry.MyObject.param2, texto, textentry, byScript or textentry) Tercio@11: end Tercio@11: else Tercio@11: textentry:SetText ("") Tercio@11: textentry.MyObject.currenttext = "" Tercio@11: end Tercio@11: Tercio@39: if (not capsule.NoClearFocusOnEnterPressed) then Tercio@39: textentry.focuslost = true --> quando estiver editando e clicar em outra caixa Tercio@39: textentry:ClearFocus() Tercio@39: Tercio@39: if (textentry.MyObject.tab_on_enter and textentry.MyObject.next) then Tercio@39: textentry.MyObject.next:SetFocus() Tercio@39: end Tercio@11: end Tercio@11: end Tercio@11: Tercio@11: local OnEscapePressed = function (textentry) Tercio@39: local capsule = textentry.MyObject Tercio@11: Tercio@39: local kill = capsule:RunHooksForWidget ("OnEscapePressed", textentry, capsule) Tercio@39: if (kill) then Tercio@39: return Tercio@39: end Tercio@39: Tercio@11: textentry.focuslost = true Tercio@11: textentry:ClearFocus() Tercio@11: end Tercio@11: Tercio@39: local OnSpacePressed = function (textentry) Tercio@39: local capsule = textentry.MyObject Tercio@39: Tercio@39: local kill = capsule:RunHooksForWidget ("OnSpacePressed", textentry, capsule) Tercio@39: if (kill) then Tercio@39: return Tercio@39: end Tercio@39: end Tercio@39: Tercio@11: local OnEditFocusLost = function (textentry) Tercio@11: Tercio@39: local capsule = textentry.MyObject Tercio@39: Tercio@11: if (textentry:IsShown()) then Tercio@11: Tercio@39: local kill = capsule:RunHooksForWidget ("OnEditFocusLost", textentry, capsule) Tercio@39: if (kill) then Tercio@39: return Tercio@11: end Tercio@11: Tercio@11: if (not textentry.focuslost) then Tercio@11: local texto = DF:trim (textentry:GetText()) Tercio@11: if (_string_len (texto) > 0) then Tercio@11: textentry.MyObject.currenttext = texto Tercio@11: if (textentry.MyObject.func) then Tercio@11: textentry.MyObject.func (textentry.MyObject.param1, textentry.MyObject.param2, texto, textentry, nil) Tercio@11: end Tercio@11: else Tercio@11: textentry:SetText ("") Tercio@11: textentry.MyObject.currenttext = "" Tercio@11: end Tercio@11: else Tercio@11: textentry.focuslost = false Tercio@11: end Tercio@11: Tercio@11: textentry.MyObject.label:SetTextColor (.8, .8, .8, 1) Tercio@11: Tercio@11: end Tercio@11: end Tercio@11: Tercio@11: local OnEditFocusGained = function (textentry) Tercio@39: Tercio@39: local capsule = textentry.MyObject Tercio@39: Tercio@39: local kill = capsule:RunHooksForWidget ("OnEditFocusGained", textentry, capsule) Tercio@39: if (kill) then Tercio@39: return Tercio@11: end Tercio@39: Tercio@11: textentry.MyObject.label:SetTextColor (1, 1, 1, 1) Tercio@11: end Tercio@11: Tercio@39: local OnChar = function (textentry, char) Tercio@39: local capsule = textentry.MyObject Tercio@39: Tercio@39: local kill = capsule:RunHooksForWidget ("OnChar", textentry, char, capsule) Tercio@39: if (kill) then Tercio@39: return Tercio@11: end Tercio@11: end Tercio@11: Tercio@11: local OnTextChanged = function (textentry, byUser) Tercio@39: local capsule = textentry.MyObject Tercio@39: Tercio@39: local kill = capsule:RunHooksForWidget ("OnTextChanged", textentry, byUser, capsule) Tercio@39: if (kill) then Tercio@39: return Tercio@11: end Tercio@11: end Tercio@11: Tercio@11: local OnTabPressed = function (textentry) Tercio@39: Tercio@39: local capsule = textentry.MyObject Tercio@39: Tercio@39: local kill = capsule:RunHooksForWidget ("OnTabPressed", textentry, byUser, capsule) Tercio@39: if (kill) then Tercio@39: return Tercio@11: end Tercio@11: Tercio@11: if (textentry.MyObject.next) then Tercio@11: OnEnterPressed (textentry, false) Tercio@11: textentry.MyObject.next:SetFocus() Tercio@11: end Tercio@11: end Tercio@11: Tercio@11: function TextEntryMetaFunctions:PressEnter (byScript) Tercio@11: OnEnterPressed (self.editbox, byScript) Tercio@11: end Tercio@11: Tercio@11: ------------------------------------------------------------------------------------------------------------ Tercio@22: Tercio@22: function TextEntryMetaFunctions:SetTemplate (template) Tercio@22: if (template.width) then Tercioo@29: self.editbox:SetWidth (template.width) Tercio@22: end Tercio@22: if (template.height) then Tercioo@29: self.editbox:SetHeight (template.height) Tercio@22: end Tercio@22: Tercio@22: if (template.backdrop) then Tercioo@29: self.editbox:SetBackdrop (template.backdrop) Tercio@22: end Tercio@22: if (template.backdropcolor) then Tercio@22: local r, g, b, a = DF:ParseColors (template.backdropcolor) Tercioo@29: self.editbox:SetBackdropColor (r, g, b, a) Tercio@22: self.onleave_backdrop = {r, g, b, a} Tercio@22: end Tercio@22: if (template.backdropbordercolor) then Tercio@22: local r, g, b, a = DF:ParseColors (template.backdropbordercolor) Tercioo@29: self.editbox:SetBackdropBorderColor (r, g, b, a) Tercio@22: self.editbox.current_bordercolor[1] = r Tercio@22: self.editbox.current_bordercolor[2] = g Tercio@22: self.editbox.current_bordercolor[3] = b Tercio@22: self.editbox.current_bordercolor[4] = a Tercio@22: self.onleave_backdrop_border_color = {r, g, b, a} Tercio@22: end Tercio@22: end Tercio@22: Tercio@22: ------------------------------------------------------------------------------------------------------------ Tercio@11: --> object constructor Tercio@11: Tercio@22: function DF:CreateTextEntry (parent, func, w, h, member, name, with_label, entry_template, label_template) Tercio@22: return DF:NewTextEntry (parent, parent, name, member, w, h, func, nil, nil, nil, with_label, entry_template, label_template) Tercio@11: end Tercio@11: Tercio@22: function DF:NewTextEntry (parent, container, name, member, w, h, func, param1, param2, space, with_label, entry_template, label_template) Tercio@11: Tercio@11: if (not name) then Tercio@11: name = "DetailsFrameworkTextEntryNumber" .. DF.TextEntryCounter Tercio@11: DF.TextEntryCounter = DF.TextEntryCounter + 1 Tercio@11: Tercio@11: elseif (not parent) then Tercioo@29: return error ("Details! FrameWork: parent not found.", 2) Tercio@11: end Tercio@11: Tercio@11: if (not container) then Tercio@11: container = parent Tercio@11: end Tercio@11: Tercio@11: if (name:find ("$parent")) then Tercioo@29: local parentName = DF.GetParentName (parent) Tercioo@29: name = name:gsub ("$parent", parentName) Tercio@11: end Tercio@11: Tercio@11: local TextEntryObject = {type = "textentry", dframework = true} Tercio@11: Tercio@11: if (member) then Tercio@11: parent [member] = TextEntryObject Tercio@11: end Tercio@11: Tercio@11: if (parent.dframework) then Tercio@11: parent = parent.widget Tercio@11: end Tercio@11: if (container.dframework) then Tercio@11: container = container.widget Tercio@11: end Tercio@11: Tercio@11: --> default members: Tercio@11: --> hooks Tercio@11: TextEntryObject.OnEnterHook = nil Tercio@11: TextEntryObject.OnLeaveHook = nil Tercio@11: TextEntryObject.OnHideHook = nil Tercio@11: TextEntryObject.OnShowHook = nil Tercio@11: TextEntryObject.OnEnterPressedHook = nil Tercio@11: TextEntryObject.OnEscapePressedHook = nil Tercio@11: TextEntryObject.OnEditFocusGainedHook = nil Tercio@11: TextEntryObject.OnEditFocusLostHook = nil Tercio@11: TextEntryObject.OnCharHook = nil Tercio@11: TextEntryObject.OnTextChangedHook = nil Tercio@11: TextEntryObject.OnTabPressedHook = nil Tercio@11: Tercio@11: --> misc Tercio@11: TextEntryObject.container = container Tercio@11: TextEntryObject.have_tooltip = nil Tercio@11: Tercio@11: TextEntryObject.editbox = CreateFrame ("EditBox", name, parent, "DetailsFrameworkEditBoxTemplate2") Tercio@11: TextEntryObject.widget = TextEntryObject.editbox Tercio@11: Tercio@11: TextEntryObject.editbox:SetTextInsets (3, 0, 0, -3) Tercio@11: Tercio@11: if (not APITextEntryFunctions) then Tercio@11: APITextEntryFunctions = true Tercio@11: local idx = getmetatable (TextEntryObject.editbox).__index Tercio@11: for funcName, funcAddress in pairs (idx) do Tercio@11: if (not TextEntryMetaFunctions [funcName]) then Tercio@11: TextEntryMetaFunctions [funcName] = function (object, ...) Tercio@20: local x = loadstring ( "return _G['"..object.editbox:GetName().."']:"..funcName.."(...)") Tercio@11: return x (...) Tercio@11: end Tercio@11: end Tercio@11: end Tercio@11: end Tercio@11: Tercio@11: TextEntryObject.editbox.MyObject = TextEntryObject Tercio@11: Tercio@11: if (not w and space) then Tercio@11: w = space Tercio@11: elseif (w and space) then Tercio@11: if (DF.debug) then Tercio@39: --print ("warning: you are using width and space, try use only space for better results.") Tercio@11: end Tercio@11: end Tercio@11: Tercio@11: TextEntryObject.editbox:SetWidth (w) Tercio@11: TextEntryObject.editbox:SetHeight (h) Tercio@11: Tercio@11: TextEntryObject.editbox:SetJustifyH ("center") Tercio@11: TextEntryObject.editbox:EnableMouse (true) Tercio@11: TextEntryObject.editbox:SetText ("") Tercio@11: Tercio@11: TextEntryObject.editbox:SetAutoFocus (false) Tercio@11: TextEntryObject.editbox:SetFontObject ("GameFontHighlightSmall") Tercio@11: Tercio@11: TextEntryObject.editbox.current_bordercolor = {1, 1, 1, 0.7} Tercio@11: TextEntryObject.editbox:SetBackdropBorderColor (1, 1, 1, 0.7) Tercio@11: TextEntryObject.enabled_border_color = {TextEntryObject.editbox:GetBackdropBorderColor()} Tercio@11: TextEntryObject.enabled_backdrop_color = {TextEntryObject.editbox:GetBackdropColor()} Tercio@11: TextEntryObject.enabled_text_color = {TextEntryObject.editbox:GetTextColor()} Tercio@22: TextEntryObject.onleave_backdrop = {TextEntryObject.editbox:GetBackdropColor()} Tercio@22: TextEntryObject.onleave_backdrop_border_color = {TextEntryObject.editbox:GetBackdropBorderColor()} Tercio@11: Tercio@11: TextEntryObject.func = func Tercio@11: TextEntryObject.param1 = param1 Tercio@11: TextEntryObject.param2 = param2 Tercio@11: TextEntryObject.next = nil Tercio@11: TextEntryObject.space = space Tercio@11: TextEntryObject.tab_on_enter = false Tercio@11: Tercio@11: TextEntryObject.label = _G [name .. "_Desc"] Tercio@11: Tercio@11: TextEntryObject.editbox:SetBackdrop ({bgFile = DF.folder .. "background", tileSize = 64, edgeFile = DF.folder .. "border_2", edgeSize = 10, insets = {left = 1, right = 1, top = 1, bottom = 1}}) Tercio@11: Tercio@11: --> hooks Tercio@39: Tercio@39: TextEntryObject.HookList = { Tercio@39: OnEnter = {}, Tercio@39: OnLeave = {}, Tercio@39: OnHide = {}, Tercio@39: OnShow = {}, Tercio@39: OnEnterPressed = {}, Tercio@39: OnEscapePressed = {}, Tercio@39: OnSpacePressed = {}, Tercio@39: OnEditFocusLost = {}, Tercio@39: OnEditFocusGained = {}, Tercio@39: OnChar = {}, Tercio@39: OnTextChanged = {}, Tercio@39: OnTabPressed = {}, Tercio@39: } Tercio@39: Tercio@11: TextEntryObject.editbox:SetScript ("OnEnter", OnEnter) Tercio@11: TextEntryObject.editbox:SetScript ("OnLeave", OnLeave) Tercio@11: TextEntryObject.editbox:SetScript ("OnHide", OnHide) Tercio@11: TextEntryObject.editbox:SetScript ("OnShow", OnShow) Tercio@11: Tercio@11: TextEntryObject.editbox:SetScript ("OnEnterPressed", OnEnterPressed) Tercio@11: TextEntryObject.editbox:SetScript ("OnEscapePressed", OnEscapePressed) Tercio@39: TextEntryObject.editbox:SetScript ("OnSpacePressed", OnSpacePressed) Tercio@11: TextEntryObject.editbox:SetScript ("OnEditFocusLost", OnEditFocusLost) Tercio@11: TextEntryObject.editbox:SetScript ("OnEditFocusGained", OnEditFocusGained) Tercio@11: TextEntryObject.editbox:SetScript ("OnChar", OnChar) Tercio@11: TextEntryObject.editbox:SetScript ("OnTextChanged", OnTextChanged) Tercio@11: TextEntryObject.editbox:SetScript ("OnTabPressed", OnTabPressed) Tercio@11: Tercio@11: _setmetatable (TextEntryObject, TextEntryMetaFunctions) Tercio@11: Tercio@22: if (with_label) then Tercio@22: local label = DF:CreateLabel (TextEntryObject.editbox, with_label, nil, nil, nil, "label", nil, "overlay") Tercio@22: label.text = with_label Tercio@22: TextEntryObject.editbox:SetPoint ("left", label.widget, "right", 2, 0) Tercio@22: if (label_template) then Tercio@22: label:SetTemplate (label_template) Tercio@22: end Tercio@22: with_label = label Tercio@22: end Tercio@11: Tercio@22: if (entry_template) then Tercio@22: TextEntryObject:SetTemplate (entry_template) Tercio@22: end Tercio@22: Tercio@22: return TextEntryObject, with_label Tercio@22: Tercio@22: end Tercio@22: Tercio@22: function DF:NewSpellEntry (parent, func, w, h, param1, param2, member, name) Tercio@22: local editbox = DF:NewTextEntry (parent, parent, name, member, w, h, func, param1, param2) Tercio@22: Tercio@39: -- editbox:SetHook ("OnEditFocusGained", SpellEntryOnEditFocusGained) Tercio@39: -- editbox:SetHook ("OnTextChanged", SpellEntryOnTextChanged) Tercio@22: Tercio@22: return editbox Tercio@11: end Tercio@11: Tercio@11: local function_gettext = function (self) Tercio@11: return self.editbox:GetText() Tercio@11: end Tercio@11: local function_settext = function (self, text) Tercio@11: return self.editbox:SetText (text) Tercio@11: end Tercio@11: local function_clearfocus = function (self) Tercio@11: return self.editbox:ClearFocus() Tercio@11: end Tercio@11: local function_setfocus = function (self) Tercio@11: return self.editbox:SetFocus (true) Tercio@11: end Tercio@11: Tercio@11: function DF:NewSpecialLuaEditorEntry (parent, w, h, member, name, nointent) Tercio@11: Tercio@11: if (name:find ("$parent")) then Tercioo@29: local parentName = DF.GetParentName (parent) Tercioo@29: name = name:gsub ("$parent", parentName) Tercio@11: end Tercio@11: Tercio@11: local borderframe = CreateFrame ("Frame", name, parent) Tercio@11: borderframe:SetSize (w, h) Tercio@11: Tercio@11: if (member) then Tercio@11: parent [member] = borderframe Tercio@11: end Tercio@11: Tercio@11: local scrollframe = CreateFrame ("ScrollFrame", name, borderframe, "DetailsFrameworkEditBoxMultiLineTemplate") Tercio@11: Tercio@11: scrollframe:SetScript ("OnSizeChanged", function (self) Tercio@11: scrollframe.editbox:SetSize (self:GetSize()) Tercio@11: end) Tercio@11: Tercio@11: scrollframe:SetPoint ("topleft", borderframe, "topleft", 10, -10) Tercio@11: scrollframe:SetPoint ("bottomright", borderframe, "bottomright", -30, 10) Tercio@11: Tercio@11: scrollframe.editbox:SetMultiLine (true) Tercio@11: scrollframe.editbox:SetJustifyH ("left") Tercio@11: scrollframe.editbox:SetJustifyV ("top") Tercio@11: scrollframe.editbox:SetMaxBytes (1024000) Tercio@11: scrollframe.editbox:SetMaxLetters (128000) Tercio@11: Tercio@11: borderframe.GetText = function_gettext Tercio@11: borderframe.SetText = function_settext Tercio@11: borderframe.ClearFocus = function_clearfocus Tercio@11: borderframe.SetFocus = function_setfocus Tercio@11: Tercio@22: borderframe.Enable = TextEntryMetaFunctions.Enable Tercio@22: borderframe.Disable = TextEntryMetaFunctions.Disable Tercio@22: Tercio@22: borderframe.SetTemplate = TextEntryMetaFunctions.SetTemplate Tercio@22: Tercio@11: if (not nointent) then Tercio@11: IndentationLib.enable (scrollframe.editbox, nil, 4) Tercio@11: end Tercio@11: Tercio@11: borderframe:SetBackdrop ({bgFile = [[Interface\Tooltips\UI-Tooltip-Background]], edgeFile = [[Interface\Tooltips\UI-Tooltip-Border]], Tercio@11: tile = 1, tileSize = 16, edgeSize = 16, insets = {left = 5, right = 5, top = 5, bottom = 5}}) Tercio@22: Tercio@22: scrollframe.editbox.current_bordercolor = {1, 1, 1, 0.7} Tercio@22: borderframe:SetBackdropBorderColor (1, 1, 1, 0.7) Tercio@11: borderframe:SetBackdropColor (0.090195, 0.090195, 0.188234, 1) Tercio@22: Tercio@22: borderframe.enabled_border_color = {borderframe:GetBackdropBorderColor()} Tercio@22: borderframe.enabled_backdrop_color = {borderframe:GetBackdropColor()} Tercio@22: borderframe.enabled_text_color = {scrollframe.editbox:GetTextColor()} Tercio@22: Tercio@22: borderframe.onleave_backdrop = {scrollframe.editbox:GetBackdropColor()} Tercio@22: borderframe.onleave_backdrop_border_color = {scrollframe.editbox:GetBackdropBorderColor()} Tercio@11: Tercio@11: borderframe.scroll = scrollframe Tercio@11: borderframe.editbox = scrollframe.editbox Tercio@22: borderframe.editbox.borderframe = borderframe Tercio@11: Tercio@11: return borderframe Tercio@39: end Tercio@39: Tercio@39: Tercio@39: ------------------------------------------------------------------------------------ Tercio@39: --auto complete Tercio@39: Tercio@39: -- block ------------------- Tercio@39: --code author Saiket from http://www.wowinterface.com/forums/showpost.php?p=245759&postcount=6 Tercio@39: --- @return StartPos, EndPos of highlight in this editbox. Tercio@39: local function GetTextHighlight ( self ) Tercio@39: local Text, Cursor = self:GetText(), self:GetCursorPosition(); Tercio@39: self:Insert( "" ); -- Delete selected text Tercio@39: local TextNew, CursorNew = self:GetText(), self:GetCursorPosition(); Tercio@39: -- Restore previous text Tercio@39: self:SetText( Text ); Tercio@39: self:SetCursorPosition( Cursor ); Tercio@39: local Start, End = CursorNew, #Text - ( #TextNew - CursorNew ); Tercio@39: self:HighlightText( Start, End ); Tercio@39: return Start, End; Tercio@39: end Tercio@39: local StripColors; Tercio@39: do Tercio@39: local CursorPosition, CursorDelta; Tercio@39: --- Callback for gsub to remove unescaped codes. Tercio@39: local function StripCodeGsub ( Escapes, Code, End ) Tercio@39: if ( #Escapes % 2 == 0 ) then -- Doesn't escape Code Tercio@39: if ( CursorPosition and CursorPosition >= End - 1 ) then Tercio@39: CursorDelta = CursorDelta - #Code; Tercio@39: end Tercio@39: return Escapes; Tercio@39: end Tercio@39: end Tercio@39: --- Removes a single escape sequence. Tercio@39: local function StripCode ( Pattern, Text, OldCursor ) Tercio@39: CursorPosition, CursorDelta = OldCursor, 0; Tercio@39: return Text:gsub( Pattern, StripCodeGsub ), OldCursor and CursorPosition + CursorDelta; Tercio@39: end Tercio@39: --- Strips Text of all color escape sequences. Tercio@39: -- @param Cursor Optional cursor position to keep track of. Tercio@39: -- @return Stripped text, and the updated cursor position if Cursor was given. Tercio@39: function StripColors ( Text, Cursor ) Tercio@39: Text, Cursor = StripCode( "(|*)(|c%x%x%x%x%x%x%x%x)()", Text, Cursor ); Tercio@39: return StripCode( "(|*)(|r)()", Text, Cursor ); Tercio@39: end Tercio@39: end Tercio@39: Tercio@39: local COLOR_END = "|r"; Tercio@39: --- Wraps this editbox's selected text with the given color. Tercio@39: local function ColorSelection ( self, ColorCode ) Tercio@39: local Start, End = GetTextHighlight( self ); Tercio@39: local Text, Cursor = self:GetText(), self:GetCursorPosition(); Tercio@39: if ( Start == End ) then -- Nothing selected Tercio@39: --Start, End = Cursor, Cursor; -- Wrap around cursor Tercio@39: return; -- Wrapping the cursor in a color code and hitting backspace crashes the client! Tercio@39: end Tercio@39: -- Find active color code at the end of the selection Tercio@39: local ActiveColor; Tercio@39: if ( End < #Text ) then -- There is text to color after the selection Tercio@39: local ActiveEnd; Tercio@39: local CodeEnd, _, Escapes, Color = 0; Tercio@39: while ( true ) do Tercio@39: _, CodeEnd, Escapes, Color = Text:find( "(|*)(|c%x%x%x%x%x%x%x%x)", CodeEnd + 1 ); Tercio@39: if ( not CodeEnd or CodeEnd > End ) then Tercio@39: break; Tercio@39: end Tercio@39: if ( #Escapes % 2 == 0 ) then -- Doesn't escape Code Tercio@39: ActiveColor, ActiveEnd = Color, CodeEnd; Tercio@39: end Tercio@39: end Tercio@39: Tercio@39: if ( ActiveColor ) then Tercio@39: -- Check if color gets terminated before selection ends Tercio@39: CodeEnd = 0; Tercio@39: while ( true ) do Tercio@39: _, CodeEnd, Escapes = Text:find( "(|*)|r", CodeEnd + 1 ); Tercio@39: if ( not CodeEnd or CodeEnd > End ) then Tercio@39: break; Tercio@39: end Tercio@39: if ( CodeEnd > ActiveEnd and #Escapes % 2 == 0 ) then -- Terminates ActiveColor Tercio@39: ActiveColor = nil; Tercio@39: break; Tercio@39: end Tercio@39: end Tercio@39: end Tercio@39: end Tercio@39: Tercio@39: local Selection = Text:sub( Start + 1, End ); Tercio@39: -- Remove color codes from the selection Tercio@39: local Replacement, CursorReplacement = StripColors( Selection, Cursor - Start ); Tercio@39: Tercio@39: self:SetText( ( "" ):join( Tercio@39: Text:sub( 1, Start ), Tercio@39: ColorCode, Replacement, COLOR_END, Tercio@39: ActiveColor or "", Text:sub( End + 1 ) Tercio@39: ) ); Tercio@39: Tercio@39: -- Restore cursor and highlight, adjusting for wrapper text Tercio@39: Cursor = Start + CursorReplacement; Tercio@39: if ( CursorReplacement > 0 ) then -- Cursor beyond start of color code Tercio@39: Cursor = Cursor + #ColorCode; Tercio@39: end Tercio@39: if ( CursorReplacement >= #Replacement ) then -- Cursor beyond end of color Tercio@39: Cursor = Cursor + #COLOR_END; Tercio@39: end Tercio@39: Tercio@39: self:SetCursorPosition( Cursor ); Tercio@39: -- Highlight selection and wrapper Tercio@39: self:HighlightText( Start, #ColorCode + ( #Replacement - #Selection ) + #COLOR_END + End ); Tercio@39: end Tercio@39: -- end of the block --------------------- Tercio@39: Tercio@39: local get_last_word = function (self) Tercio@39: self.lastword = "" Tercio@39: local cursor_pos = self.editbox:GetCursorPosition() Tercio@39: local text = self.editbox:GetText() Tercio@39: for i = cursor_pos, 1, -1 do Tercio@39: local character = text:sub (i, i) Tercio@39: if (character:match ("%a")) then Tercio@39: self.lastword = character .. self.lastword Tercio@39: else Tercio@39: break Tercio@39: end Tercio@39: end Tercio@39: end Tercio@39: Tercio@39: --On Text Changed Tercio@39: local AutoComplete_OnTextChanged = function (editboxWidget, byUser, capsule) Tercio@39: capsule = capsule or editboxWidget.MyObject Tercio@39: Tercio@39: local chars_now = editboxWidget:GetText():len() Tercio@39: if (not editboxWidget.ignore_textchange) then Tercio@39: --> backspace Tercio@39: if (chars_now == capsule.characters_count -1) then Tercio@39: capsule.lastword = capsule.lastword:sub (1, capsule.lastword:len()-1) Tercio@39: --> delete lots of text Tercio@39: elseif (chars_now < capsule.characters_count) then Tercio@39: --o auto complete selecionou outra palavra bem menor e caiu nesse filtro Tercio@39: editboxWidget.end_selection = nil Tercio@39: capsule:GetLastWord() Tercio@39: end Tercio@39: else Tercio@39: editboxWidget.ignore_textchange = nil Tercio@39: end Tercio@39: capsule.characters_count = chars_now Tercio@39: end Tercio@39: Tercio@39: local AutoComplete_OnSpacePressed = function (editboxWidget, capsule) Tercio@39: capsule = capsule or editboxWidget.MyObject Tercio@39: Tercio@39: -- if (not gotMatch) then Tercio@39: --editboxWidget.end_selection = nil Tercio@39: -- end Tercio@39: end Tercio@39: Tercio@39: local AutoComplete_OnEscapePressed = function (editboxWidget) Tercio@39: editboxWidget.end_selection = nil Tercio@39: end Tercio@39: Tercio@39: local AutoComplete_OnEnterPressed = function (editboxWidget) Tercio@39: Tercio@39: local capsule = editboxWidget.MyObject Tercio@39: if (editboxWidget.end_selection) then Tercio@39: editboxWidget:SetCursorPosition (editboxWidget.end_selection) Tercio@39: editboxWidget:HighlightText (0, 0) Tercio@39: editboxWidget.end_selection = nil Tercio@39: --editboxWidget:Insert (" ") --estava causando a adição de uma palavra a mais quando o próximo catactere for um espaço Tercio@39: else Tercio@39: if (editboxWidget:IsMultiLine()) then Tercio@39: editboxWidget:Insert ("\n") Tercio@39: --reseta a palavra se acabou de ganhar focus e apertou enter Tercio@39: if (editboxWidget.focusGained) then Tercio@39: capsule.lastword = "" Tercio@39: editboxWidget.focusGained = nil Tercio@39: end Tercio@39: else Tercio@39: editboxWidget:Insert ("") Tercio@39: editboxWidget.focuslost = true Tercio@39: editboxWidget:ClearFocus() Tercio@39: end Tercio@39: end Tercio@39: capsule.lastword = "" Tercio@39: Tercio@39: end Tercio@39: Tercio@39: local AutoComplete_OnEditFocusGained = function (editboxWidget) Tercio@39: local capsule = editboxWidget.MyObject Tercio@39: capsule:GetLastWord() Tercio@39: editboxWidget.end_selection = nil Tercio@39: editboxWidget.focusGained = true Tercio@39: capsule.characters_count = editboxWidget:GetText():len() Tercio@39: end Tercio@39: Tercio@39: local AutoComplete_OnChar = function (editboxWidget, char, capsule) Tercio@39: if (char == "") then Tercio@39: return Tercio@39: end Tercio@39: Tercio@39: capsule = capsule or editboxWidget.MyObject Tercio@39: editboxWidget.end_selection = nil Tercio@39: Tercio@39: if (editboxWidget.ignore_input) then Tercio@39: return Tercio@39: end Tercio@39: Tercio@39: --reseta a palavra se acabou de ganhar focus e apertou espaço Tercio@39: if (editboxWidget.focusGained and char == " ") then Tercio@39: capsule.lastword = "" Tercio@39: editboxWidget.focusGained = nil Tercio@39: else Tercio@39: editboxWidget.focusGained = nil Tercio@39: end Tercio@39: Tercio@39: if (char:match ("%a") or (char == " " and capsule.lastword ~= "")) then Tercio@39: capsule.lastword = capsule.lastword .. char Tercio@39: else Tercio@39: capsule.lastword = "" Tercio@39: end Tercio@39: Tercio@39: editboxWidget.ignore_input = true Tercio@39: if (capsule.lastword:len() >= 2) then Tercio@39: Tercio@39: local wordList = capsule [capsule.poolName] Tercio@39: if (not wordList) then Tercio@39: if (DF.debug) then Tercio@39: error ("Details! Framework: Invalid word list table.") Tercio@39: end Tercio@39: return Tercio@39: end Tercio@39: Tercio@39: for i = 1, #wordList do Tercio@39: local thisWord = wordList [i] Tercio@39: if (thisWord and (thisWord:find ("^" .. capsule.lastword) or thisWord:lower():find ("^" .. capsule.lastword))) then Tercio@39: local rest = thisWord:gsub (capsule.lastword, "") Tercio@39: rest = rest:lower():gsub (capsule.lastword, "") Tercio@39: local cursor_pos = editboxWidget:GetCursorPosition() Tercio@39: editboxWidget:Insert (rest) Tercio@39: editboxWidget:HighlightText (cursor_pos, cursor_pos + rest:len()) Tercio@39: editboxWidget:SetCursorPosition (cursor_pos) Tercio@39: editboxWidget.end_selection = cursor_pos + rest:len() Tercio@39: editboxWidget.ignore_textchange = true Tercio@39: break Tercio@39: end Tercio@39: end Tercio@39: Tercio@39: end Tercio@39: editboxWidget.ignore_input = false Tercio@39: end Tercio@39: Tercio@39: function TextEntryMetaFunctions:SetAsAutoComplete (poolName) Tercio@39: Tercio@39: self.lastword = "" Tercio@39: self.characters_count = 0 Tercio@39: self.poolName = poolName Tercio@39: self.GetLastWord = get_last_word --editbox:GetLastWord() Tercio@39: self.NoClearFocusOnEnterPressed = true --avoid auto clear focus Tercio@39: Tercio@39: self:SetHook ("OnEditFocusGained", AutoComplete_OnEditFocusGained) Tercio@39: self.editbox:HookScript ("OnEscapePressed", AutoComplete_OnEscapePressed) Tercio@39: Tercio@39: -- self:SetHook ("OnTextChanged", AutoComplete_OnTextChanged) Tercio@39: self:SetHook ("OnEnterPressed", AutoComplete_OnEnterPressed) Tercio@39: -- self:SetHook ("OnChar", AutoComplete_OnChar) Tercio@39: -- self:SetHook ("OnSpacePressed", AutoComplete_OnSpacePressed) Tercio@39: Tercio@39: self.editbox:SetScript ("OnTextChanged", AutoComplete_OnTextChanged) Tercio@39: -- self.editbox:SetScript ("OnEnterPressed", AutoComplete_OnEnterPressed) Tercio@39: self.editbox:SetScript ("OnChar", AutoComplete_OnChar) Tercio@39: self.editbox:SetScript ("OnSpacePressed", AutoComplete_OnSpacePressed) Tercio@39: Tercio@39: end Tercio@39: Tercio@39: -- endp