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