1: <?php
2:
3:
4:
5:
6:
7:
8: class CalculatorWidget extends QDialog {
9:
10: public $pnlValueDisplay;
11: public $pxyNumberControl;
12: public $pxyOperationControl;
13:
14: public $btnEqual;
15: public $btnPoint;
16: public $btnClear;
17:
18: public $btnUpdate;
19: public $btnCancel;
20:
21: protected $intWidth = 240;
22:
23:
24: protected $strCloseCallback;
25: protected $fltValue;
26:
27:
28: protected $blnMatteClickable = false;
29: protected $strTemplate = 'CalculatorWidget.tpl.php';
30: protected $strCssClass = 'calculator_widget';
31:
32: protected $fltInternalValue;
33: protected $strCurrentOperation;
34: protected $blnNextClears;
35:
36: public function __construct($strCloseCallback, $objParentObject, $strControlId = null) {
37: parent::__construct($objParentObject, $strControlId);
38: $this->strCloseCallback = $strCloseCallback;
39: $this->DialogClass = $this->strCssClass;
40:
41:
42: $this->pnlValueDisplay = new QPanel($this);
43:
44: $this->pnlValueDisplay->CssClass = 'calculator_display';
45:
46:
47: $this->pxyNumberControl = new QControlProxy($this);
48: $this->pxyNumberControl->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'pxyNumber_Click'));
49:
50: $this->pxyOperationControl = new QControlProxy($this);
51: $this->pxyOperationControl->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'pxyOperation_Click'));
52:
53: $this->btnEqual = new QButton($this);
54: $this->btnEqual->Text = '=';
55: $this->btnEqual->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnEqual_Click'));
56:
57: $this->btnPoint = new QButton($this);
58: $this->btnPoint->Text = '.';
59: $this->btnPoint->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnPoint_Click'));
60:
61: $this->btnClear = new QButton($this);
62: $this->btnClear->Text = 'C';
63: $this->btnClear->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnClear_Click'));
64:
65: $this->btnUpdate = new QButton($this);
66: $this->btnUpdate->Text = 'Save';
67: $this->btnUpdate->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnUpdate_Click'));
68:
69: $this->btnCancel = new QButton($this);
70: $this->btnCancel->Text = 'Cancel';
71: $this->btnCancel->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnCancel_Click'));
72: }
73:
74: public function pxyNumber_Click($strFormId, $strControlId, $strParameter) {
75: if ($this->blnNextClears) {
76: $this->blnNextClears = false;
77: $this->pnlValueDisplay->Text = $strParameter;
78: } else if ($this->pnlValueDisplay->Text === '0') {
79: $this->pnlValueDisplay->Text = $strParameter;
80: } else if (strlen($this->pnlValueDisplay->Text) < 13)
81: $this->pnlValueDisplay->Text .= $strParameter;
82: }
83:
84: public function btnPoint_Click() {
85: if ($this->blnNextClears) {
86: $this->pnlValueDisplay->Text = '0.';
87: $this->blnNextClears = false;
88: } else {
89: if (strpos($this->pnlValueDisplay->Text, '.') === false)
90: $this->pnlValueDisplay->Text .= '.';
91: }
92: }
93:
94: public function pxyOperation_Click($strFormId, $strControlId, $strParameter) {
95: if ($this->strCurrentOperation && !$this->blnNextClears)
96: $this->btnEqual_Click();
97: $this->strCurrentOperation = $strParameter;
98: $this->blnNextClears = true;
99: if (strpos($this->pnlValueDisplay->Text, '.') !== false)
100: $this->pnlValueDisplay->Text .= '0';
101:
102: $this->fltInternalValue = QType::Cast($this->pnlValueDisplay->Text, QType::Float);
103: try {
104: $this->fltInternalValue = QType::Cast($this->pnlValueDisplay->Text, QType::Integer);
105: } catch (QInvalidCastException $objExc) {}
106:
107: $this->pnlValueDisplay->Text = $this->fltInternalValue;
108: }
109:
110: public function btnEqual_Click() {
111: $this->blnNextClears = true;
112:
113: if (strpos($this->pnlValueDisplay->Text, '.') !== false)
114: $this->pnlValueDisplay->Text .= '0';
115: $fltOtherValue = QType::Cast($this->pnlValueDisplay->Text, QType::Float);
116: try {
117: $fltOtherValue = QType::Cast($this->pnlValueDisplay->Text, QType::Integer);
118: } catch (QInvalidCastException $objExc) {}
119:
120: switch ($this->strCurrentOperation) {
121: case '+':
122: $this->fltInternalValue = $this->fltInternalValue + $fltOtherValue;
123: break;
124: case '-':
125: $this->fltInternalValue = $this->fltInternalValue - $fltOtherValue;
126: break;
127: case '*':
128: $this->fltInternalValue = $this->fltInternalValue * $fltOtherValue;
129: break;
130: case '/':
131: if ($fltOtherValue == 0)
132: $this->fltInternalValue = 0;
133: else
134: $this->fltInternalValue = $this->fltInternalValue / $fltOtherValue;
135: break;
136: }
137:
138: $this->strCurrentOperation = null;
139: $this->pnlValueDisplay->Text = substr('' . $this->fltInternalValue, 0, 13);
140: }
141:
142: public function btnClear_Click() {
143: $this->fltValue = 0;
144: $this->pnlValueDisplay->Text = 0;
145:
146: $this->fltInternalValue = 0;
147: $this->blnNextClears = true;
148: $this->strCurrentOperation = null;
149: }
150:
151: public function btnCancel_Click() {
152: $this->Close();
153: }
154:
155: public function btnUpdate_Click() {
156: $this->fltValue = $this->pnlValueDisplay->Text;
157: call_user_func(array($this->objForm, $this->strCloseCallback));
158: $this->Close();
159: }
160:
161: public function Open() {
162: parent::Open();
163: $this->pnlValueDisplay->Text = ($this->fltValue) ? $this->fltValue : '0';
164:
165: $this->fltInternalValue = 0;
166: $this->blnNextClears = true;
167: $this->strCurrentOperation = null;
168: }
169:
170: public function ShowDialogBox() {
171: parent::ShowDialogBox();
172: $this->pnlValueDisplay->Text = ($this->fltValue) ? $this->fltValue : '0';
173:
174: $this->fltInternalValue = 0;
175: $this->blnNextClears = true;
176: $this->strCurrentOperation = null;
177: }
178:
179: public function __get($strName) {
180: switch ($strName) {
181: case "Value": return $this->fltValue;
182:
183: default:
184: try {
185: return parent::__get($strName);
186: } catch (QCallerException $objExc) {
187: $objExc->IncrementOffset();
188: throw $objExc;
189: }
190: }
191: }
192:
193: public function __set($strName, $mixValue) {
194: $this->blnModified = true;
195:
196: switch ($strName) {
197: case "Value":
198:
199:
200: $this->fltValue = 0;
201: try {
202: $this->fltValue = QType::Cast($mixValue, QType::Float);
203: break;
204: } catch (QInvalidCastException $objExc) {}
205: try {
206: $this->fltValue = QType::Cast($mixValue, QType::Integer);
207: break;
208: } catch (QInvalidCastException $objExc) {}
209: break;
210:
211: default:
212: try {
213: parent::__set($strName, $mixValue);
214: } catch (QCallerException $objExc) {
215: $objExc->IncrementOffset();
216: throw $objExc;
217: }
218: break;
219: }
220: }
221: }
222: ?>