/*
Copyright (C) 2015 Runtime Revolution Ltd.

This file is part of LiveCode.

LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.

LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with LiveCode.  If not see <http://www.gnu.org/licenses/>.  */

-- declaring extension as widget, followed by identifier
widget com.livecode.widget.radioButton
--

-- dependancy declarations
use com.livecode.canvas
use com.livecode.widget
---

-- metadata
metadata title is "Radio Button"
metadata author is "LiveCode"
metadata version is "1.0.2"
metadata svgicon is "M15.8,0C7.1,0,0,7.1,0,15.8s7.1,15.8,15.8,15.8s15.8-7.1,15.8-15.8S24.4,0,15.8,0z M16.2,27.5v-3c4.7-0.2,8.5-4.1,8.5-8.9c0-4.9-4-8.9-8.9-8.9c-3.8,0-7.1,2.4-8.4,5.8c0.1-0.1,0.2-0.3,0.4-0.4c3.9-3.9,10.2-3.9,14,0c-3.1-3.1-9.5-1.7-12.6,1.4C8,14.7,7.3,16,7,17.5c0.8,4,4.3,6.9,8.5,7.1v3C9.1,27.4,4,22.2,4,15.8C4,9.3,9.3,4,15.8,4s11.8,5.3,11.8,11.8C27.5,22.1,22.5,27.2,16.2,27.5z"

-- property declarations
property isChecked			get mIsChecked				set setChecked
property buttonTheme				get mTheme					set setTheme
property buttonLabel				get mLabelText				set setLabelText
---

-- private instance variables
private variable mIsChecked	as Boolean
private variable mTheme as String
private variable mLabelText as String

private variable mState as String
--

-- constants
constant kTransparent is [1,1,10]
constant kWhite is [1,1,1]
constant kBlack is [0,0,0]
--

public handler OnSave(out rProperties as Array)
	put the empty array into rProperties
	
	put mIsChecked into rProperties["checked"]
	put mTheme into rProperties["theme"]
	put mLabelText into rProperties["label"]
	
	return rProperties
end handler
----------

----------
-- this handler is called when the widget is loaded
public handler OnLoad(in pProperties as Array)
	put pProperties["theme"] into mTheme
	put pProperties["checked"] into mIsChecked
	put pProperties["label"] into mLabelText
end handler

public handler OnCreate()
	-- properties
	put false into mIsChecked
	put "macos-10.0" into mTheme
	put "Radio" into mLabelText
	
	-- variables
	put "unchecked" into mState
end handler

public handler OnPaint()

	-- draw the outer circle and fill with paint
	set the paint of this canvas to getPaint("outer circle")
	fill getPath("outer circle") on this canvas
	--
	
	-- draw the circle border by stroking the circle
	set the stroke width of this canvas to getStroke()
	set the paint of this canvas to getPaint("outer circle stroke")
	stroke getPath("outer circle") on this canvas
	--
	
	-- draw the inner circle by filling with paint
	set the paint of this canvas to getPaint("inner circle")
	fill getPath("inner circle") on this canvas
	--
	
	-- draw the label
	set the font of this canvas to getFont()
	set the paint of this canvas to getPaint("label")
	fill text mLabelText at left of getLabelRect() on this canvas
	--
	
end handler

public handler OnMouseDown()
	if mIsChecked is true then
		put "pressed and checked" into mState
	end if
	setChecked(not(mIsChecked))
	redraw all
end handler

public handler OnMouseUp()
	if mState is "pressed and checked" then
		put "checked" into mState
	end if
	redraw all
	post "buttonChecked" with [mLabelText]
end handler

private handler getPath(in pObject as String) returns Path
	if pObject is "outer circle" then
		return circle path centered at point [10, my height/2] with radius 7.5
		
	else if pObject is "inner circle" then
		variable tRadius as Real 
		
		if mIsChecked is true then
			if mTheme is "windows-8" then
				put 3.5 into tRadius
			else if mTheme contains "macos" then
				put 2.5 into tRadius
			end if
			
		else
			put 1.0 into tRadius
		end if
		
		return circle path centered at point [10, my height/2] with radius tRadius
	end if
end handler

private handler getStroke() returns Number
	if mTheme contains "macos" then
		return 1.0
	else if mTheme is "windows-8" then
		return 1.5
	end if
end handler

private handler getPaint(in pObject as String) returns Paint
	if pObject is "outer circle" then
		variable tStop1
		variable tStop2
		variable tStop3
		variable tColor1 as Color
		variable tColor2 as Color
		variable tColor3 as Color
		variable tPaint as Paint
		
		if mIsChecked is true then
			if mTheme is "macos-yosemite" then
				return solid paint with color [51/255, 153/255, 1.0]

			else if mTheme is "macos-10.0" then
				if mState is "pressed and checked" then
					put color [0/255, 102/255, 255/255, 0.3] into tColor1
					put color [0/255, 102/255, 255/255, 0.75] into tColor2
					put color [0/255, 102/255, 255/255, 0.3] into tColor3
				else
					put color [102/255, 178/255, 1.0, 0.05] into tColor1
					put color [102/255, 178/255, 1.0, 0.85] into tColor2
					put color [102/255, 178/255, 1.0, 0.05] into tColor3
				end if
			
				-- create a new gradient paint
				put gradient stop at 0.25 with tColor1 into tStop1
				put gradient stop at 0.5 with tColor2 into tStop2
				put gradient stop at 0.75 with tColor3 into tStop3
						
				put linear gradient with ramp [tStop1, tStop2, tStop3] into tPaint
				scale tPaint by [my height, my width]
				rotate tPaint by 90
			
				return tPaint
			
			else if mTheme is "windows-8" then
				return solid paint with color kWhite
			end if
		
		else if mTheme is "macos-10.0" then
	
			-- create a new gradient paint
			put gradient stop at 0.25 with color kWhite into tStop1
			put gradient stop at 0.5 with color [224/255, 224/255, 224/255, 0.25] into tStop2
			put gradient stop at 0.75 with color [192/255, 192/255, 192/255, 0.75] into tStop3
				
			put linear gradient with ramp [tStop1, tStop2, tStop3] into tPaint
			scale tPaint by [my height, my width]
			rotate tPaint by 90
			
			return tPaint
		else
			return solid paint with color kWhite
		end if
	
	else if pObject is "outer circle stroke" then
	
		if mTheme is "macos-yosemite" then
			if mIsChecked is true then
		 		return solid paint with color [51/255, 153/255, 1]
			else
		 		return solid paint with color [160/255, 160/255, 160/255]
			end if
		else if mTheme is "macos-10.0" then
			if mIsChecked is true then
				return solid paint with color [96/255, 96/255, 96/255]
			else
				return solid paint with color [160/255, 160/255, 160/255]
			end if
		else if mTheme is "windows-8" then
			return solid paint with color [96/255, 96/255, 96/255]
		end if
		
	else if pObject is "inner circle" then
		if mIsChecked is true then
			if mTheme is "macos-yosemite" then
				return solid paint with color kWhite
			else
				return solid paint with color kBlack
			end if
		else
			return solid paint with color kTransparent
		end if
		
	else if pObject is "label" then
		if mTheme is "macos-yosemite" then
			return solid paint with color [32/255, 32/255, 32/255]
		else if mTheme is "macos-10.0" then
			return solid paint with color kBlack
		else if mTheme is "windows-8" then
			return solid paint with color [96/255, 96/255, 96/255]
		end if
		
	end if
end handler

private handler getFont() returns Font
	if mTheme is "macos-yosemite" then
		return font "Helvetica Neue" at size 12
	else if mTheme is "macos-10.0" then
		return font "Lucida Grande" at size 11
	else if mTheme is "windows-8" then
		return font "Segoe WP" at size 12
	end if
end handler

private handler getLabelRect() returns Rectangle
	variable tLabelRect as Rectangle
	put my bounds into tLabelRect
	add 22 to the x of tLabelRect
	return tLabelRect
end handler

private handler setChecked(in pIsChecked as Boolean) returns nothing
    put pIsChecked into mIsChecked
    redraw all
end handler

public handler setTheme(in pTheme as String) returns nothing
	put pTheme into mTheme
	redraw all
end handler

public handler setLabelText(in pLabel as String) returns nothing
	put pLabel into mLabelText
	redraw all
end handler

end widget
