HomeForumSourceResearchGuide
Sign in to contribute to source. how it works
Component ui.CheckBox by barry
expand copy to clipboardexpand
const int TEXT_MARGIN = 3
const int TEXT_PAD = 6

const int CHECK_WIDTH = 16
const int CHECK_HEIGHT = 16

component provides CheckBox requires ui.Font, os.SystemInfo sysInfo, io.Output out {
	
	bool checked
	Font labelFont
	
	int width
	
	Color background = new Color(185, 185, 200, 255)
	Color checkColor = new Color(50, 50, 65, 255)
	Color textColor = new Color(0, 0, 0, 255)
	
	Color backgroundDis = new Color(185, 185, 185, 255)
	Color checkColorDis = new Color(50, 50, 50, 255)
	Color textColorDis = new Color(20, 20, 20, 255)
	
	CheckBox:CheckBox(char label[])
		{
		labelFont = new Font(sysInfo.getSystemFont(false), 15)
		text = label
		width = CHECK_WIDTH + labelFont.getTextWidth(text) + TEXT_PAD
		}
	
	void CheckBox:paint(Canvas c)
		{
		if (!disabled)
			{
			c.rect(new Rect2D(xPosition, yPosition+2, CHECK_WIDTH, CHECK_HEIGHT, background))
			if (checked) c.rect(new Rect2D(xPosition+3, yPosition+5, CHECK_WIDTH-6, CHECK_HEIGHT-6, checkColor))
			c.text(new Point2D(xPosition+CHECK_WIDTH+TEXT_MARGIN, yPosition+1, textColor), labelFont, text)
			}
			else
			{
			c.rect(new Rect2D(xPosition, yPosition+2, CHECK_WIDTH, CHECK_HEIGHT, backgroundDis))
			if (checked) c.rect(new Rect2D(xPosition+3, yPosition+5, CHECK_WIDTH-6, CHECK_HEIGHT-6, checkColorDis))
			c.text(new Point2D(xPosition+CHECK_WIDTH+TEXT_MARGIN, yPosition+1, textColorDis), labelFont, text)
			}
		}
	
	bool CheckBox:isChecked()
		{
		return checked
		}
	
	void CheckBox:setChecked(bool t)
		{
		checked = t
		postRepaint()
		}
	
	void CheckBox:setBackground(store Color c)
		{
		background = c
		postRepaint()
		}
	
	void CheckBox:setCheckColor(store Color c)
		{
		checkColor = c
		postRepaint()
		}
	
	void CheckBox:setTextColor(store Color c)
		{
		textColor = c
		postRepaint()
		}
	
	void CheckBox:setPosition(int x, int y)
		{
		xPosition = x
		yPosition = y
		}
	
	void CheckBox:click(int x, int y, int button)
		{
		if (disabled) return
		
		if (button == MouseButtons.BUTTON_LEFT)
			{
			checked = !checked
			postRepaint()
			emitevent click()
			}
		}
	
	void CheckBox:postRepaint()
		{
		emitevent repaint()
		}
	
	void CheckBox:setFocus()
		{
		emitevent requestFocus()
		}
	
	void CheckBox:setDisabled(bool d)
		{
		disabled = d
		postRepaint()
		}
	
	void CheckBox:setText(char t[])
		{
		text = t
		width = CHECK_WIDTH + labelFont.getTextWidth(text) + TEXT_PAD
		}
	
	char[] CheckBox:getText()
		{
		return text
		}
	
	Rect CheckBox:getBounds()
		{
		return new Rect(xPosition, yPosition, width, CHECK_HEIGHT)
		}
	
	WH CheckBox:getPreferredSize()
		{
		return new WH(width, CHECK_HEIGHT)
		}
	
	Point CheckBox:getPosition()
		{
		return new Point(xPosition, yPosition)
		}
	
	}
Revision history
To propose a new revision to this entity, use dana source put -uc your/new/version.dn -n ui.CheckBox -m "reason for update" -u yourUsername
Version 1 (this version) by barry
Notes for this version: Standard Library Initialisation