Quantcast
--[[
	Copyright (c) 2014 Eyal Shilony <Lynxium>

	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]

local _, addon = ...

local secureButtons = { }

-- script handlers to mimic regular dropdown button behaviour
-- close menu when clicked
local function onClick(self)
	self.parent:GetParent():Hide()
end

local function onMouseDown(self)
	self.parent:SetButtonState("PUSHED")
end

local function onMouseUp(self)
	self.parent:SetButtonState("NORMAL")
end

-- prevent menu from hiding
local function onEnter(self)
	local grandParent = self.parent:GetParent()
	CloseDropDownMenus(grandParent:GetID() + 1)
	_G[self.parent:GetName().."Highlight"]:Show()
	UIDropDownMenu_StopCounting(grandParent)
end

-- hide menu after timeout
local function onLeave(self)
	_G[self.parent:GetName().."Highlight"]:Hide()
	UIDropDownMenu_StartCounting(self.parent:GetParent())
	GameTooltip:Hide()
end

local function createSecureButtonOverlay(button)
	local secureButton = CreateFrame("Button", nil, nil, "SecureActionButtonTemplate")
	secureButton:SetSize(200, 16)
	secureButton:HookScript("OnClick", onClick)
	secureButton:SetScript("OnEnter", onEnter)
	secureButton:SetScript("OnLeave", onLeave)
	secureButton:SetScript("OnMouseDown", onMouseDown)
	secureButton:SetScript("OnMouseUp", onMouseUp)
	secureButton:SetAttribute("type", "click")
	secureButton:SetAttribute("clickbutton", button)
	return secureButton
end

-- TODO: We can remove the dependency on the menu by using the dropdown itself, for now it works!
function addon:AddSecureButtonsOverlay(menu)
	local index = 0
	for _, item in pairs(menu) do
		if item.order and item.order > 0 then
			index = index + 1
			local parent = _G["DropDownList1Button" .. index]
			if not parent then
				UIDropDownMenu_CreateFrames(1, index)
				parent = _G["DropDownList1Button" .. index]
			end
			if parent and item.button then
				local button = createSecureButtonOverlay(item.button)
				table.insert(secureButtons, button)
				button.parent = parent
				button:SetParent(parent)
				button:SetAllPoints()
				--[[button:SetBackdrop({
					  bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
					  tile = true,
					  tileSize = 16,
					  --edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
					  edgeSize = 16,
					  insets = {left = 5, right = 3, top = 3, bottom = 5}
				})
				button:SetBackdropColor(0, 1, 0, 0.5)]]
				button:Show()
			end
		end
	end
end

function addon:RemoveSecureButtonsOverlay()
	if #secureButtons > 0 then
		for index, button in ipairs(secureButtons) do
			button:SetParent(UIParent)
			button:ClearAllPoints()
			button:Hide()
		end
		wipe(secureButtons)
	end
end