annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-EditBox.lua @ 8:1b2d819b4fa8

Now using the global MailAddonBusy to indicate MailOpener is busy, read comments in Core.lua for more info. Default status is now ?enabled without automatic mail opening? to let first time users look around before MailOpener messes up their heads. A StaticPopupDialog will be added later to ask if they want to auto-enable. When ?enabled without automatic mail opening? is on and you toggle the mail opening checkbox on, mail opening will automatically start.
author Zerotorescue
date Thu, 09 Sep 2010 10:53:19 +0200
parents 823e33465b6e
children
rev   line source
Zerotorescue@0 1 --[[-----------------------------------------------------------------------------
Zerotorescue@0 2 EditBox Widget
Zerotorescue@0 3 -------------------------------------------------------------------------------]]
Zerotorescue@0 4 local Type, Version = "EditBox", 21
Zerotorescue@0 5 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
Zerotorescue@0 6 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
Zerotorescue@0 7
Zerotorescue@0 8 -- Lua APIs
Zerotorescue@0 9 local tostring, pairs = tostring, pairs
Zerotorescue@0 10
Zerotorescue@0 11 -- WoW APIs
Zerotorescue@0 12 local PlaySound = PlaySound
Zerotorescue@0 13 local GetCursorInfo, ClearCursor, GetSpellName = GetCursorInfo, ClearCursor, GetSpellName
Zerotorescue@0 14 local CreateFrame, UIParent = CreateFrame, UIParent
Zerotorescue@0 15 local _G = _G
Zerotorescue@0 16
Zerotorescue@0 17 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
Zerotorescue@0 18 -- List them here for Mikk's FindGlobals script
Zerotorescue@0 19 -- GLOBALS: AceGUIEditBoxInsertLink, ChatFontNormal, OKAY
Zerotorescue@0 20
Zerotorescue@0 21 --[[-----------------------------------------------------------------------------
Zerotorescue@0 22 Support functions
Zerotorescue@0 23 -------------------------------------------------------------------------------]]
Zerotorescue@0 24 if not AceGUIEditBoxInsertLink then
Zerotorescue@0 25 -- upgradeable hook
Zerotorescue@0 26 hooksecurefunc("ChatEdit_InsertLink", function(...) return _G.AceGUIEditBoxInsertLink(...) end)
Zerotorescue@0 27 end
Zerotorescue@0 28
Zerotorescue@0 29 function _G.AceGUIEditBoxInsertLink(text)
Zerotorescue@0 30 for i = 1, AceGUI:GetWidgetCount(Type) do
Zerotorescue@0 31 local editbox = _G["AceGUI-3.0EditBox"..i]
Zerotorescue@0 32 if editbox and editbox:IsVisible() and editbox:HasFocus() then
Zerotorescue@0 33 editbox:Insert(text)
Zerotorescue@0 34 return true
Zerotorescue@0 35 end
Zerotorescue@0 36 end
Zerotorescue@0 37 end
Zerotorescue@0 38
Zerotorescue@0 39 local function ShowButton(self)
Zerotorescue@0 40 if not self.disablebutton then
Zerotorescue@0 41 self.button:Show()
Zerotorescue@0 42 self.editbox:SetTextInsets(0, 20, 3, 3)
Zerotorescue@0 43 end
Zerotorescue@0 44 end
Zerotorescue@0 45
Zerotorescue@0 46 local function HideButton(self)
Zerotorescue@0 47 self.button:Hide()
Zerotorescue@0 48 self.editbox:SetTextInsets(0, 0, 3, 3)
Zerotorescue@0 49 end
Zerotorescue@0 50
Zerotorescue@0 51 --[[-----------------------------------------------------------------------------
Zerotorescue@0 52 Scripts
Zerotorescue@0 53 -------------------------------------------------------------------------------]]
Zerotorescue@0 54 local function Control_OnEnter(frame)
Zerotorescue@0 55 frame.obj:Fire("OnEnter")
Zerotorescue@0 56 end
Zerotorescue@0 57
Zerotorescue@0 58 local function Control_OnLeave(frame)
Zerotorescue@0 59 frame.obj:Fire("OnLeave")
Zerotorescue@0 60 end
Zerotorescue@0 61
Zerotorescue@0 62 local function EditBox_OnEscapePressed(frame)
Zerotorescue@0 63 AceGUI:ClearFocus()
Zerotorescue@0 64 end
Zerotorescue@0 65
Zerotorescue@0 66 local function EditBox_OnEnterPressed(frame)
Zerotorescue@0 67 local self = frame.obj
Zerotorescue@0 68 local value = frame:GetText()
Zerotorescue@0 69 local cancel = self:Fire("OnEnterPressed", value)
Zerotorescue@0 70 if not cancel then
Zerotorescue@0 71 PlaySound("igMainMenuOptionCheckBoxOn")
Zerotorescue@0 72 HideButton(self)
Zerotorescue@0 73 end
Zerotorescue@0 74 end
Zerotorescue@0 75
Zerotorescue@0 76 local function EditBox_OnReceiveDrag(frame)
Zerotorescue@0 77 local self = frame.obj
Zerotorescue@0 78 local type, id, info = GetCursorInfo()
Zerotorescue@0 79 if type == "item" then
Zerotorescue@0 80 self:SetText(info)
Zerotorescue@0 81 self:Fire("OnEnterPressed", info)
Zerotorescue@0 82 ClearCursor()
Zerotorescue@0 83 elseif type == "spell" then
Zerotorescue@0 84 local name, rank = GetSpellName(id, info)
Zerotorescue@0 85 if rank and rank:match("%d") then
Zerotorescue@0 86 name = name.."("..rank..")"
Zerotorescue@0 87 end
Zerotorescue@0 88 self:SetText(name)
Zerotorescue@0 89 self:Fire("OnEnterPressed", name)
Zerotorescue@0 90 ClearCursor()
Zerotorescue@0 91 end
Zerotorescue@0 92 HideButton(self)
Zerotorescue@0 93 AceGUI:ClearFocus()
Zerotorescue@0 94 end
Zerotorescue@0 95
Zerotorescue@0 96 local function EditBox_OnTextChanged(frame)
Zerotorescue@0 97 local self = frame.obj
Zerotorescue@0 98 local value = frame:GetText()
Zerotorescue@0 99 if tostring(value) ~= tostring(self.lasttext) then
Zerotorescue@0 100 self:Fire("OnTextChanged", value)
Zerotorescue@0 101 self.lasttext = value
Zerotorescue@0 102 ShowButton(self)
Zerotorescue@0 103 end
Zerotorescue@0 104 end
Zerotorescue@0 105
Zerotorescue@0 106 local function Button_OnClick(frame)
Zerotorescue@0 107 local editbox = frame.obj.editbox
Zerotorescue@0 108 editbox:ClearFocus()
Zerotorescue@0 109 EditBox_OnEnterPressed(editbox)
Zerotorescue@0 110 end
Zerotorescue@0 111
Zerotorescue@0 112 --[[-----------------------------------------------------------------------------
Zerotorescue@0 113 Methods
Zerotorescue@0 114 -------------------------------------------------------------------------------]]
Zerotorescue@0 115 local methods = {
Zerotorescue@0 116 ["OnAcquire"] = function(self)
Zerotorescue@0 117 -- height is controlled by SetLabel
Zerotorescue@0 118 self:SetWidth(200)
Zerotorescue@0 119 self:SetDisabled(false)
Zerotorescue@0 120 self:SetLabel()
Zerotorescue@0 121 self:SetText()
Zerotorescue@0 122 self:DisableButton(false)
Zerotorescue@0 123 self:SetMaxLetters(0)
Zerotorescue@0 124 end,
Zerotorescue@0 125
Zerotorescue@0 126 -- ["OnRelease"] = nil,
Zerotorescue@0 127
Zerotorescue@0 128 ["SetDisabled"] = function(self, disabled)
Zerotorescue@0 129 self.disabled = disabled
Zerotorescue@0 130 if disabled then
Zerotorescue@0 131 self.editbox:EnableMouse(false)
Zerotorescue@0 132 self.editbox:ClearFocus()
Zerotorescue@0 133 self.editbox:SetTextColor(0.5,0.5,0.5)
Zerotorescue@0 134 self.label:SetTextColor(0.5,0.5,0.5)
Zerotorescue@0 135 else
Zerotorescue@0 136 self.editbox:EnableMouse(true)
Zerotorescue@0 137 self.editbox:SetTextColor(1,1,1)
Zerotorescue@0 138 self.label:SetTextColor(1,.82,0)
Zerotorescue@0 139 end
Zerotorescue@0 140 end,
Zerotorescue@0 141
Zerotorescue@0 142 ["SetText"] = function(self, text)
Zerotorescue@0 143 self.lasttext = text or ""
Zerotorescue@0 144 self.editbox:SetText(text or "")
Zerotorescue@0 145 self.editbox:SetCursorPosition(0)
Zerotorescue@0 146 HideButton(self)
Zerotorescue@0 147 end,
Zerotorescue@0 148
Zerotorescue@0 149 ["SetLabel"] = function(self, text)
Zerotorescue@0 150 if text and text ~= "" then
Zerotorescue@0 151 self.label:SetText(text)
Zerotorescue@0 152 self.label:Show()
Zerotorescue@0 153 self.editbox:SetPoint("TOPLEFT",self.frame,"TOPLEFT",7,-18)
Zerotorescue@0 154 self:SetHeight(44)
Zerotorescue@0 155 self.alignoffset = 30
Zerotorescue@0 156 else
Zerotorescue@0 157 self.label:SetText("")
Zerotorescue@0 158 self.label:Hide()
Zerotorescue@0 159 self.editbox:SetPoint("TOPLEFT",self.frame,"TOPLEFT",7,0)
Zerotorescue@0 160 self:SetHeight(26)
Zerotorescue@0 161 self.alignoffset = 12
Zerotorescue@0 162 end
Zerotorescue@0 163 end,
Zerotorescue@0 164
Zerotorescue@0 165 ["DisableButton"] = function(self, disabled)
Zerotorescue@0 166 self.disablebutton = disabled
Zerotorescue@0 167 end,
Zerotorescue@0 168
Zerotorescue@0 169 ["SetMaxLetters"] = function (self, num)
Zerotorescue@0 170 self.editbox:SetMaxLetters(num or 0)
Zerotorescue@0 171 end
Zerotorescue@0 172 }
Zerotorescue@0 173
Zerotorescue@0 174 --[[-----------------------------------------------------------------------------
Zerotorescue@0 175 Constructor
Zerotorescue@0 176 -------------------------------------------------------------------------------]]
Zerotorescue@0 177 local function Constructor()
Zerotorescue@0 178 local num = AceGUI:GetNextWidgetNum(Type)
Zerotorescue@0 179 local frame = CreateFrame("Frame", nil, UIParent)
Zerotorescue@0 180 frame:Hide()
Zerotorescue@0 181
Zerotorescue@0 182 local editbox = CreateFrame("EditBox", "AceGUI-3.0EditBox"..num, frame, "InputBoxTemplate")
Zerotorescue@0 183 editbox:SetAutoFocus(false)
Zerotorescue@0 184 editbox:SetFontObject(ChatFontNormal)
Zerotorescue@0 185 editbox:SetScript("OnEnter", Control_OnEnter)
Zerotorescue@0 186 editbox:SetScript("OnLeave", Control_OnLeave)
Zerotorescue@0 187 editbox:SetScript("OnEscapePressed", EditBox_OnEscapePressed)
Zerotorescue@0 188 editbox:SetScript("OnEnterPressed", EditBox_OnEnterPressed)
Zerotorescue@0 189 editbox:SetScript("OnTextChanged", EditBox_OnTextChanged)
Zerotorescue@0 190 editbox:SetScript("OnReceiveDrag", EditBox_OnReceiveDrag)
Zerotorescue@0 191 editbox:SetScript("OnMouseDown", EditBox_OnReceiveDrag)
Zerotorescue@0 192 editbox:SetTextInsets(0, 0, 3, 3)
Zerotorescue@0 193 editbox:SetMaxLetters(256)
Zerotorescue@0 194 editbox:SetPoint("BOTTOMLEFT", 6, 0)
Zerotorescue@0 195 editbox:SetPoint("BOTTOMRIGHT")
Zerotorescue@0 196 editbox:SetHeight(19)
Zerotorescue@0 197
Zerotorescue@0 198 local label = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
Zerotorescue@0 199 label:SetPoint("TOPLEFT", 0, -2)
Zerotorescue@0 200 label:SetPoint("TOPRIGHT", 0, -2)
Zerotorescue@0 201 label:SetJustifyH("LEFT")
Zerotorescue@0 202 label:SetHeight(18)
Zerotorescue@0 203
Zerotorescue@0 204 local button = CreateFrame("Button", nil, editbox, "UIPanelButtonTemplate")
Zerotorescue@0 205 button:SetWidth(40)
Zerotorescue@0 206 button:SetHeight(20)
Zerotorescue@0 207 button:SetPoint("RIGHT", -2, 0)
Zerotorescue@0 208 button:SetText(OKAY)
Zerotorescue@0 209 button:SetScript("OnClick", Button_OnClick)
Zerotorescue@0 210 button:Hide()
Zerotorescue@0 211
Zerotorescue@0 212 local widget = {
Zerotorescue@0 213 alignoffset = 30,
Zerotorescue@0 214 editbox = editbox,
Zerotorescue@0 215 label = label,
Zerotorescue@0 216 button = button,
Zerotorescue@0 217 frame = frame,
Zerotorescue@0 218 type = Type
Zerotorescue@0 219 }
Zerotorescue@0 220 for method, func in pairs(methods) do
Zerotorescue@0 221 widget[method] = func
Zerotorescue@0 222 end
Zerotorescue@0 223 editbox.obj, button.obj = widget, widget
Zerotorescue@0 224
Zerotorescue@0 225 return AceGUI:RegisterAsWidget(widget)
Zerotorescue@0 226 end
Zerotorescue@0 227
Zerotorescue@0 228 AceGUI:RegisterWidgetType(Type, Constructor, Version)