--[[-------------------------------------------------------------------- |
PhanxConfig-EditBox |
Simple text input widget generator. |
Requires LibStub. |
|
This library is not intended for use by other authors. Absolutely no |
support of any kind will be provided for other authors using it, and |
its internals may change at any time without notice. |
----------------------------------------------------------------------]] |
|
local CreateButton = LibStub( "PhanxConfig-Button", true ) and LibStub( "PhanxConfig-Button" ).CreateButton |
assert( CreateButton, "PhanxConfig-EditBox requires PhanxConfig-Button!" ) |
|
local MINOR_VERSION = tonumber( string.match( "$Revision: 28 $", "%d+" ) ) |
|
local lib, oldminor = LibStub:NewLibrary( "PhanxConfig-EditBox", MINOR_VERSION ) |
if not lib then return end |
|
lib.editboxes = lib.editboxes or { } |
|
if not PhanxConfigEditBoxInsertLink then |
hooksecurefunc( "ChatEdit_InsertLink", function( ... ) return _G.PhanxConfigEditBoxInsertLink( ... ) end ) |
end |
|
function PhanxConfigEditBoxInsertLink( link ) |
for i = 1, #lib.editboxes do |
local editbox = lib.editboxes[ i ] |
if editbox and editbox:IsVisible() and editbox:HasFocus() then |
editbox:Insert( link ) |
return true |
end |
end |
end |
|
local function OnEnter( self ) |
local text = self:GetParent().desc |
if text then |
GameTooltip:SetOwner( self, "ANCHOR_RIGHT" ) |
GameTooltip:SetText( text, nil, nil, nil, nil, true ) |
end |
end |
|
local function OnLeave() |
GameTooltip:Hide() |
end |
|
local function OnEditFocusGained( self ) |
self.currText = self:GetText() |
self:HighlightText() |
end |
|
local function OnTextChanged( self ) |
local text = self:GetText() |
local parent = self:GetParent() |
if parent.OnTextChanged and text ~= self.currText then |
parent:OnTextChanged( text ) |
self.currText = text |
end |
self.button:Enable() |
end |
|
local function OnEnterPressed( self ) |
local parent = self:GetParent() |
if parent.OnValueChanged then |
parent:OnValueChanged( self:GetText() ) |
end |
self:ClearFocus() |
self.currText = nil |
parent.button:Disable() |
end |
|
local function OnEscapePressed( self ) |
self:ClearFocus() |
self.currText = nil |
self:GetParent().button:Disable() |
end |
|
local function OnReceiveDrag( self ) |
local type, id, info = GetCursorInfo() |
if type == "item" then |
self:SetText( info ) |
OnEnterPressed( self ) |
ClearCursor() |
elseif type == "spell" then |
local name = GetSpellInfo( id, info ) |
self:SetText( name ) |
OnEnterPressed( self ) |
ClearCursor() |
end |
end |
|
local function OnClick( self ) |
return OnEnterPressed( self:GetParent().editbox ) |
end |
|
local function SetValue( self, text ) |
return self.editbox:SetText( text ) |
end |
|
function lib.CreateEditBox( parent, name, maxLetters, noButton ) |
local frame = CreateFrame( "Frame", nil, parent ) |
frame:SetWidth( 144 ) |
frame:SetHeight( 42 ) |
|
-- local bg = frame:CreateTexture(nil, "BACKGROUND") |
-- bg:SetAllPoints(frame) |
-- bg:SetTexture(0, 0, 0) |
-- frame.bg = bg |
|
local button = CreateButton( frame, OKAY ) |
button:SetWidth( 24 ) |
button:SetScript( "OnClick", OnClick ) |
button:Disable() |
if noButton then button:Hide() end |
frame.button = button |
|
local editbox = CreateFrame( "EditBox", nil, frame ) |
editbox:SetPoint( "LEFT", 5, 0 ) |
editbox:SetPoint( "RIGHT", button, "LEFT", -5, 0 ) |
editbox:SetHeight( 19 ) |
editbox:EnableMouse( true ) |
editbox:SetAutoFocus( false ) |
editbox:SetFontObject( ChatFontNormal ) |
editbox:SetMaxLetters( maxLetters or 256 ) |
editbox:SetTextInsets( 0, 0, 3, 3 ) |
editbox:SetScript( "OnEnter", OnEnter ) |
editbox:SetScript( "OnLeave", OnLeave ) |
editbox:SetScript( "OnEditFocusGained", OnEditFocusGained ) |
editbox:SetScript( "OnTextChanged", OnTextChanged ) |
editbox:SetScript( "OnEnterPressed", OnEnterPressed ) |
editbox:SetScript( "OnEscapePressed", OnEscapePressed ) |
editbox:SetScript( "OnReceiveDrag", OnReceiveDrag ) |
lib.editboxes[ #lib.editboxes + 1 ] = editbox |
frame.editbox = editbox |
|
editbox.bgLeft = editbox:CreateTexture( nil, "BACKGROUND" ) |
editbox.bgLeft:SetPoint( "LEFT", -5, 0 ) |
editbox.bgLeft:SetSize( 8, 20 ) |
editbox.bgLeft:SetTexture( [[Interface\Common\Common-Input-Border]] ) |
editbox.bgLeft:SetTexCoord( 0, 0.0625, 0, 0.625 ) |
|
editbox.bgRight = editbox:CreateTexture( nil, "BACKGROUND" ) |
editbox.bgRight:SetPoint( "RIGHT", 0, 0 ) |
editbox.bgRight:SetSize( 8, 20 ) |
editbox.bgRight:SetTexture( [[Interface\Common\Common-Input-Border]] ) |
editbox.bgRight:SetTexCoord( 0.9375, 1, 0, 0.625 ) |
|
editbox.bgMiddle = editbox:CreateTexture( nil, "BACKGROUND" ) |
editbox.bgMiddle:SetPoint( "LEFT", editbox.bgLeft, "RIGHT" ) |
editbox.bgMiddle:SetPoint( "RIGHT", editbox.bgRight, "LEFT" ) |
editbox.bgMiddle:SetSize( 10, 20 ) |
editbox.bgMiddle:SetTexture( [[Interface\Common\Common-Input-Border]] ) |
editbox.bgMiddle:SetTexCoord( 0.0625, 0.9375, 0, 0.625 ) |
|
local label = slider:CreateFontString( nil, "ARTWORK", "GameFontNormal" ) |
label:SetPoint( "BOTTOMLEFT", slider, "TOPLEFT" ) |
label:SetPoint( "BOTTOMRIGHT", slider, "TOPRIGHT" ) |
label:SetJustifyH( "LEFT" ) |
label:SetText( name ) |
frame.label = label |
|
frame.noButton = noButton |
|
frame.SetValue = SetValue |
|
return frame |
end |