1 /** DGui project file.
2 
3 Copyright: Trogu Antonio Davide 2011-2013
4 
5 License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 
7 Authors: Trogu Antonio Davide
8 */
9 module dgui.core.controls.abstractbutton;
10 
11 public import dgui.core.dialogs.dialogresult;
12 public import dgui.core.controls.ownerdrawcontrol;
13 
14 /**
15   Enum that contain the check state of a _CheckBox or similar component
16   */
17 enum CheckState: uint
18 {
19 	checked = BST_CHECKED, 				///Checked State
20 	unchecked = BST_UNCHECKED,			///Unchecked State
21 	indeterminate = BST_INDETERMINATE,	///Indeterminate State
22 }
23 
24 /// Abstract class of a _Button/_CheckBox/_RadioButton
25 abstract class AbstractButton: OwnerDrawControl
26 {
27 	protected DialogResult _dr = DialogResult.none;
28 
29 	protected override void createControlParams(ref CreateControlParams ccp)
30 	{
31 		AbstractButton.setBit(this._cBits, ControlBits.ownClickMsg, true); // Let Button to handle Click Event itself
32 
33 		ccp.superclassName = WC_BUTTON;
34 		this.setStyle(WS_TABSTOP, true);
35 
36 		super.createControlParams(ccp);
37 	}
38 
39 	protected override void onReflectedMessage(ref Message m)
40 	{
41 		switch(m.msg)
42 		{
43 			case WM_COMMAND:
44 			{
45 				switch(HIWORD(m.wParam))
46 				{
47 					 case BN_CLICKED:
48 					 {
49 						MouseKeys mk = MouseKeys.none;
50 
51 						if(GetAsyncKeyState(MK_LBUTTON))
52 						{
53 							mk |= MouseKeys.left;
54 						}
55 
56 						if(GetAsyncKeyState(MK_MBUTTON))
57 						{
58 							mk |= MouseKeys.middle;
59 						}
60 
61 						if(GetAsyncKeyState(MK_RBUTTON))
62 						{
63 							mk |= MouseKeys.right;
64 						}
65 
66 						Point p = Point(LOWORD(m.lParam), HIWORD(m.lParam));
67 						scope MouseEventArgs e = new MouseEventArgs(p, mk);
68 						this.onClick(EventArgs.empty);
69 
70 						if(this._dr !is DialogResult.none)
71 						{
72 							Control c = this.topLevelControl;
73 
74 							if(c)
75 							{
76 								c.sendMessage(DGUI_SETDIALOGRESULT, this._dr, 0);
77 							}
78 						}
79 					 }
80 					 break;
81 
82 					default:
83 						break;
84 				}
85 			}
86 			break;
87 
88 			default:
89 				break;
90 		}
91 
92 		super.onReflectedMessage(m);
93 	}
94 }
95 
96 /// Abstract class of a checkable button (_CheckBox, _RadioButton, ...)
97 abstract class CheckedButton: AbstractButton
98 {
99 	public Event!(Control, EventArgs) checkChanged; ///Checked Changed Event of a Checkable _Button
100 
101 	private CheckState _checkState = CheckState.unchecked;
102 
103 	/**
104 	 Returns:
105 		True if the _Button is _checked otherwise False.
106 
107 	 See_Also:
108 		checkState() property below.
109 	 */
110 	@property public bool checked()
111 	{
112 		return this.checkState is CheckState.checked;
113 	}
114 
115 	/**
116 	  Sets the checked state of a checkable _button
117 
118 	  Params:
119 		True checks the _button, False unchecks it.
120 	  */
121 	@property public void checked(bool b)
122 	{
123 		this.checkState = b ? CheckState.checked : CheckState.unchecked;
124 	}
125 
126 	/**
127 	  Returns:
128 		A CheckState enum that returns the state of the checkable button (it includes the indeterminate state too)
129 	  */
130 	@property public CheckState checkState()
131 	{
132 		if(this.created)
133 		{
134 			return cast(CheckState)this.sendMessage(BM_GETCHECK, 0, 0);
135 		}
136 
137 		return this._checkState;
138 	}
139 
140 	/**
141 	  Sets the check state of a checkable button
142 	  */
143 	@property public void checkState(CheckState cs)
144 	{
145 		this._checkState = cs;
146 
147 		if(this.created)
148 		{
149 			this.sendMessage(BM_SETCHECK, cs, 0);
150 		}
151 	}
152 
153 	protected override void onHandleCreated(EventArgs e)
154 	{
155 		this.sendMessage(BM_SETCHECK, this._checkState, 0);
156 		super.onHandleCreated(e);
157 	}
158 
159 	protected override void onReflectedMessage(ref Message m)
160 	{
161 		switch(m.msg)
162 		{
163 			case WM_COMMAND:
164 			{
165 				switch(HIWORD(m.wParam))
166 				{
167 					 case BN_CLICKED:
168 					 {
169 						if(this._checkState !is this.checkState) //Is Check State Changed?
170 						{
171 							this._checkState = this.checkState;
172 							this.onCheckChanged(EventArgs.empty);
173 						}
174 					 }
175 					 break;
176 
177 					default:
178 						break;
179 				}
180 			}
181 			break;
182 
183 			default:
184 				break;
185 		}
186 
187 		super.onReflectedMessage(m);
188 	}
189 
190 	protected void onCheckChanged(EventArgs e)
191 	{
192 		this.checkChanged(this, e);
193 	}
194 }