1 module PixelPerfectEngine.concrete.elements.checkbox;
2 
3 public import PixelPerfectEngine.concrete.elements.base;
4 
5 /**
6  * A simple toggle button.
7  */
8 public class CheckBox : WindowElement, ISmallButton {
9 	public string		iconChecked = "checkBoxB";		///Sets the icon for checked positions
10 	public string		iconUnchecked = "checkBoxA";	///Sets the icon for unchecked positions
11 	public EventDeleg 	onToggle;
12 	///CTOR for checkbox with text
13 	public this(Text text, string source, Coordinate coordinates, bool checked = false) {
14 		position = coordinates;
15 		this.text = text;
16 		this.source = source;
17 		isChecked = checked;
18 	}
19 	///Ditto
20 	public this(dstring text, string source, Coordinate coordinates, bool checked = false) {
21 		this(new Text(text, getStyleSheet().getChrFormatting("checkBox")), source, coordinates, checked);
22 	}
23 	///CTOR for small button version
24 	public this(string iconChecked, string iconUnchecked, string source, Coordinate coordinates, bool checked = false) {
25 		position = coordinates;
26 		this.iconChecked = iconChecked;
27 		this.iconUnchecked = iconUnchecked;
28 		this.source = source;
29 		isChecked = checked;
30 	}
31 	public override void draw() {
32 		parent.clearArea(position);
33 		StyleSheet ss = getStyleSheet;
34 		Bitmap8Bit icon = isChecked ? ss.getImage(iconChecked) : ss.getImage(iconUnchecked);
35 		
36 		parent.bitBLT(position.cornerUL, icon);
37 		
38 		if (text) {
39 			Coordinate textPos = position;
40 			textPos.left += ss.getImage(iconChecked).width + ss.drawParameters["TextSpacingSides"];
41 			parent.drawTextSL(textPos, text, Point(0, 0));
42 		}
43 		if (isFocused) {
44 			const int textPadding = ss.drawParameters["horizTextPadding"];
45 			parent.drawBoxPattern(position - textPadding, ss.pattern["blackDottedLine"]);
46 		}
47 		if (state == ElementState.Disabled) {
48 			parent.bitBLTPattern(position, ss.getImage("ElementDisabledPtrn"));
49 		}
50 	}
51 	
52 	
53 	public override void passMCE(MouseEventCommons mec, MouseClickEvent mce) {
54 		mce.x -= position.left;
55 		mce.y -= position.top;
56 		const int width = getStyleSheet().getImage(iconChecked).width;
57 		if (mce.button == MouseButton.Left && mce.state == ButtonState.Pressed && mce.x < width) {
58 			if (isChecked) {
59 				unCheck;
60 			} else {
61 				check;
62 			}
63 			if (onToggle !is null) {
64 				onToggle(new Event(this, EventType.Toggle, SourceType.WindowElement));
65 			}
66 		}
67 		super.passMCE(mec, mce);
68 	}
69 	/**
70 	 * Sets the value of the checkbox to checked.
71 	 * Does not inwoke any events.
72 	 */
73 	public bool check() @trusted {
74 		flags |= IS_CHECKED;
75 		draw();
76 		return isChecked;
77 	}
78 	/**
79 	 * Sets the value of the checkbox to unchecked.
80 	 * Does not inwoke any events.
81 	 */
82 	public bool unCheck() @trusted {
83 		flags &= ~IS_CHECKED;
84 		draw();
85 		return isChecked;
86 	}
87 	/**
88 	 * Toggles the checkbox.
89 	 * Inwokes an `onToggle` event if delegate is set.
90 	 */
91 	public bool toggle() {
92 		if (isChecked) {
93 			unCheck;
94 		} else {
95 			check;
96 		}
97 		if (onToggle !is null) {
98 			onToggle(new Event(this, EventType.Toggle, SourceType.WindowElement));
99 		}
100 		return isChecked();
101 	}
102 	/**
103 	 * Toggles the checkbox to the given value.
104 	 * Inwokes an `onToggle` event if delegate is set.
105 	 */
106 	public bool toggle(bool val) {
107 		if (isChecked == val)
108 			return val;
109 		else
110 			return toggle();
111 	}
112 	///Returns true if the checkbox is a small button.
113 	public bool isSmallButtonHeight(int height) {
114 		if (text) return false;
115 		else if (position.width == height && position.height == height) return true;
116 		else return false;
117 	}
118 	///Returns true if left side justified, false otherwise.
119 	public bool isLeftSide() @nogc @safe pure nothrow const {
120 		return flags & IS_LHS ? true : false;
121 	}
122 	///Sets the small button to the left side if true.
123 	public bool isLeftSide(bool val) @nogc @safe pure nothrow {
124 		if (val) flags |= IS_LHS;
125 		else flags &= ~IS_LHS;
126 		return flags & IS_LHS ? true : false;
127 	}
128 }