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(Box(position.left, position.top, position.left + icon.width - 1, position.top + icon.height - 1 49 ), ss.getImage("ElementDisabledPtrn")); 50 } 51 52 if (onDraw !is null) { 53 onDraw(); 54 } 55 } 56 57 58 public override void passMCE(MouseEventCommons mec, MouseClickEvent mce) { 59 if (state != ElementState.Enabled) return; 60 mce.x -= position.left; 61 mce.y -= position.top; 62 const int width = getStyleSheet().getImage(iconChecked).width; 63 if (mce.button == MouseButton.Left && mce.state == ButtonState.Pressed && mce.x < width) { 64 if (isChecked) { 65 unCheck; 66 } else { 67 check; 68 } 69 if (onToggle !is null) { 70 onToggle(new Event(this, EventType.Toggle, SourceType.WindowElement)); 71 } 72 } 73 super.passMCE(mec, mce); 74 } 75 /** 76 * Sets the value of the checkbox to checked. 77 * Does not inwoke any events. 78 */ 79 public bool check() @trusted { 80 flags |= IS_CHECKED; 81 draw(); 82 return isChecked; 83 } 84 /** 85 * Sets the value of the checkbox to unchecked. 86 * Does not inwoke any events. 87 */ 88 public bool unCheck() @trusted { 89 flags &= ~IS_CHECKED; 90 draw(); 91 return isChecked; 92 } 93 /** 94 * Toggles the checkbox. 95 * Inwokes an `onToggle` event if delegate is set. 96 */ 97 public bool toggle() { 98 if (isChecked) { 99 unCheck; 100 } else { 101 check; 102 } 103 if (onToggle !is null) { 104 onToggle(new Event(this, EventType.Toggle, SourceType.WindowElement)); 105 } 106 return isChecked(); 107 } 108 /** 109 * Toggles the checkbox to the given value. 110 * Inwokes an `onToggle` event if delegate is set. 111 */ 112 public bool toggle(bool val) { 113 if (isChecked == val) 114 return val; 115 else 116 return toggle(); 117 } 118 ///Returns true if the checkbox is a small button. 119 public bool isSmallButtonHeight(int height) { 120 if (text) return false; 121 else if (position.width == height && position.height == height) return true; 122 else return false; 123 } 124 ///Returns true if left side justified, false otherwise. 125 public bool isLeftSide() @nogc @safe pure nothrow const { 126 return flags & IS_LHS ? true : false; 127 } 128 ///Sets the small button to the left side if true. 129 public bool isLeftSide(bool val) @nogc @safe pure nothrow { 130 if (val) flags |= IS_LHS; 131 else flags &= ~IS_LHS; 132 return flags & IS_LHS ? true : false; 133 } 134 }