NXWidgets: chexcalculator.hxx Source File

NXWidgets 1.19

chexcalculator.hxx
Go to the documentation of this file.
1 /****************************************************************************
2 * NxWidgets/nxwm/include/chexcalculator.hxx
3 *
4 * Copyright (C) 2012 Gregory Nutt. All rights reserved.
5 * Author: Gregory Nutt <gnutt@nuttx.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
18 * me be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ****************************************************************************/
35
36 #ifndef __INCLUDE_CHEXCALCULATOR_HXX
37 #define __INCLUDE_CHEXCALCULATOR_HXX
38
39 /****************************************************************************
40 * Included Files
41 ****************************************************************************/
42
43 #include <nuttx/config.h>
44
45 #include <sys/types.h>
46 #include <nuttx/nx/nxtk.h>
47 #include <nuttx/nx/nxterm.h>
48
49 #include "cbuttonarray.hxx"
50 #include "clabel.hxx"
51 #include "cnxfont.hxx"
52
53 #include "iapplication.hxx"
54 #include "capplicationwindow.hxx"
55 #include "ctaskbar.hxx"
56
57 /****************************************************************************
58 * Pre-Processor Definitions
59 ****************************************************************************/
60
61 #define NXWM_HEXCALCULATOR_NROWS 6
62 #define NXWM_HEXCALCULATOR_NCOLUMNS 6
63
64 /****************************************************************************
65 * Implementation Classes
66 ****************************************************************************/
67
68 #if defined(__cplusplus)
69
70 namespace NxWM
71 {
72 /**
73 * This class implements the NxTerm application.
74 */
75
76 class CHexCalculator : public IApplication,
77 private IApplicationCallback,
78 private NXWidgets::CWidgetEventHandler
79 {
80 private:
81 /**
82 * The structure defines a pending operation.
83 */
84
85 struct SPendingOperation
86 {
87 int64_t value; /**< Accumulated value */
88 uint8_t operation; /**< Identifies the operations */
89 };
90
91 /**
92 * Calculator state data.
93 */
94
95 /**
96 * Cached constructor parameters.
97 */
98
99 CTaskbar *m_taskbar; /**< Reference to the "parent" taskbar */
100 CApplicationWindow *m_window; /**< Reference to the application window */
101
102 /**
103 * Widgets
104 */
105
106 NXWidgets::CButtonArray *m_keypad; /**< The calculator keyboard */
107 NXWidgets::CLabel *m_text; /**< The accumulator text display */
108 NXWidgets::CNxFont *m_font; /**< The font used in the calculator */
109
110 /**
111 * Calculator geometry. This stuff does not really have to be retained
112 * in memory. If you are pinched for memory, get rid of these.
113 */
114
115 struct nxgl_size_s m_windowSize; /**< The size of the calculator window */
116 struct nxgl_size_s m_keypadSize; /**< The size the calculator keypad */
117 struct nxgl_size_s m_buttonSize; /**< The size of one calculator button */
118 struct nxgl_size_s m_textSize; /**< The size of the calculator textbox */
119 struct nxgl_point_s m_keypadPos; /**< The position the calculator keypad */
120 struct nxgl_point_s m_textPos; /**< The position of the calculator textbox */
121
122 /**
123 * Calculator computational data. Note: Since we do not support
124 * parentheses and support only two levels of operator precedence, it is
125 * not necessary to maintain a stack of operations. Within there
126 * limitations, there can be at most only one pending low prececence
127 * operation and one pending high precedence operation. If you want
128 * to support parentheses or more levels of precedence, they you will
129 * have to extend the design.
130 */
131
132 int64_t m_accum; /**< The current accumulated value */
133 int64_t m_memory; /**< The current value saved in memory */
134 struct SPendingOperation m_low; /**< Low precedence pending operation */
135 struct SPendingOperation m_high; /**< Hight precedence pending operation */
136 bool m_hexMode; /**< True if in hex mode */
137 bool m_result ; /**< True if the accumulator holds a previoius result */
138
139 /**
140 * Select the geometry of the calculator given the current window size.
141 * Only called as part of construction.
142 */
143
144 inline void setGeometry(void);
145
146 /**
147 * Create the calculator keypad. Only start as part of the applicaiton
148 * start method.
149 */
150
151 inline bool createCalculator(void);
152
153 /**
154 * Applies labels to the keys.
155 */
156
157 void labelKeypad(void);
158
159 /**
160 * Evaluate a binary operation.
161 *
162 * @param value1. The first value
163 * @param value2. The second value
164 *
165 * @return The result of the operation
166 */
167
168 int64_t evaluateBinaryOperation(uint8_t operation, int64_t value1, int64_t value2);
169
170 /**
171 * Show the current value of the accumulator.
172 */
173
174 void updateText(void);
175
176 /**
177 * Called when the window minimize button is pressed.
178 */
179
180 void minimize(void);
181
182 /**
183 * Called when the window minimize close is pressed.
184 */
185
186 void close(void);
187
188 /**
189 * Handle a widget action event. For CImage, this is a button pre-
190 * release event.
191 *
192 * @param e The event data.
193 */
194
195 void handleActionEvent(const NXWidgets::CWidgetEventArgs &e);
196
197 public:
198 /**
199 * CHexCalculator constructor
200 *
201 * @param window. The application window
202 *
203 * @param taskbar. A pointer to the parent task bar instance
204 * @param window. The window to be used by this application.
205 */
206
207 CHexCalculator(CTaskbar *taskbar, CApplicationWindow *window);
208
209 /**
210 * CHexCalculator destructor
211 */
212
213 ~CHexCalculator(void);
214
215 /**
216 * Each implementation of IApplication must provide a method to recover
217 * the contained CApplicationWindow instance.
218 */
219
220 IApplicationWindow *getWindow(void) const;
221
222 /**
223 * Get the icon associated with the application
224 *
225 * @return An instance if IBitmap that may be used to rend the
226 * application's icon. This is an new IBitmap instance that must
227 * be deleted by the caller when it is no long needed.
228 */
229
230 NXWidgets::IBitmap *getIcon(void);
231
232 /**
233 * Get the name string associated with the application
234 *
235 * @return A copy if CNxString that contains the name of the application.
236 */
237
238 NXWidgets::CNxString getName(void);
239
240 /**
241 * Start the application (perhaps in the minimized state).
242 *
243 * @return True if the application was successfully started.
244 */
245
246 bool run(void);
247
248 /**
249 * Stop the application.
250 */
251
252 void stop(void);
253
254 /**
255 * Destroy the application and free all of its resources. This method
256 * will initiate blocking of messages from the NX server. The server
257 * will flush the window message queue and reply with the blocked
258 * message. When the block message is received by CWindowMessenger,
259 * it will send the destroy message to the start window task which
260 * will, finally, safely delete the application.
261 */
262
263 void destroy(void);
264
265 /**
266 * The application window is hidden (either it is minimized or it is
267 * maximized, but not at the top of the hierarchy
268 */
269
270 void hide(void);
271
272 /**
273 * Redraw the entire window. The application has been maximized or
274 * otherwise moved to the top of the hierarchy. This method is call from
275 * CTaskbar when the application window must be displayed
276 */
277
278 void redraw(void);
279
280 /**
281 * Report of this is a "normal" window or a full screen window. The
282 * primary purpose of this method is so that window manager will know
283 * whether or not it show draw the task bar.
284 *
285 * @return True if this is a full screen window.
286 */
287
288 bool isFullScreen(void) const;
289 };
290
291 class CHexCalculatorFactory : public IApplicationFactory
292 {
293 private:
294 CTaskbar *m_taskbar; /**< The taskbar */
295
296 public:
297 /**
298 * CHexCalculatorFactory Constructor
299 *
300 * @param taskbar. The taskbar instance used to terminate calibration
301 */
302
303 CHexCalculatorFactory(CTaskbar *taskbar);
304
305 /**
306 * CHexCalculatorFactory Destructor
307 */
308
309 inline ~CHexCalculatorFactory(void) { }
310
311 /**
312 * Create a new instance of an CHexCalculator (as IApplication).
313 */
314
315 IApplication *create(void);
316
317 /**
318 * Get the icon associated with the application
319 *
320 * @return An instance if IBitmap that may be used to rend the
321 * application's icon. This is an new IBitmap instance that must
322 * be deleted by the caller when it is no long needed.
323 */
324
325 NXWidgets::IBitmap *getIcon(void);
326 };
327 }
328 #endif // __cplusplus
329
330 #endif // __INCLUDE_CHEXCALCULATOR_HXX
NxWM::CHexCalculator::close
void close(void)
Definition: chexcalculator.cxx:701
NxWM::CApplicationWindow
Definition: capplicationwindow.hxx:74
NxWM::CHexCalculator::m_text
NXWidgets::CLabel * m_text
Definition: chexcalculator.hxx:107
NxWM::CHexCalculator::m_keypadSize
struct nxgl_size_s m_keypadSize
Definition: chexcalculator.hxx:116
NXWidgets::CWidgetEventHandler
Definition: cwidgeteventhandler.hxx:103
clabel.hxx
NxWM::CHexCalculator::SPendingOperation
Definition: chexcalculator.hxx:85
NxWM::CHexCalculator::labelKeypad
void labelKeypad(void)
Definition: chexcalculator.cxx:574
NxWM::CHexCalculator::m_memory
int64_t m_memory
Definition: chexcalculator.hxx:133
NxWM::CHexCalculator::createCalculator
bool createCalculator(void)
Definition: chexcalculator.cxx:499
NxWM::IApplicationFactory
Definition: iapplication.hxx:218
iapplication.hxx
NxWM::CHexCalculator::minimize
void minimize(void)
Definition: chexcalculator.cxx:692
NxWM::CHexCalculator::getWindow
IApplicationWindow * getWindow(void) const
Definition: chexcalculator.cxx:278
NxWM::CHexCalculator::evaluateBinaryOperation
int64_t evaluateBinaryOperation(uint8_t operation, int64_t value1, int64_t value2)
Definition: chexcalculator.cxx:626
NxWM::CHexCalculator::~CHexCalculator
~CHexCalculator(void)
Definition: chexcalculator.cxx:248
NxWM::CHexCalculatorFactory::~CHexCalculatorFactory
~CHexCalculatorFactory(void)
Definition: chexcalculator.hxx:309
capplicationwindow.hxx
NxWM::CHexCalculator::m_low
struct SPendingOperation m_low
Definition: chexcalculator.hxx:134
NxWM::CHexCalculator::m_keypad
NXWidgets::CButtonArray * m_keypad
Definition: chexcalculator.hxx:106
cbuttonarray.hxx
NxWM::CHexCalculator::m_textSize
struct nxgl_size_s m_textSize
Definition: chexcalculator.hxx:118
NxWM::CHexCalculatorFactory::m_taskbar
CTaskbar * m_taskbar
Definition: chexcalculator.hxx:294
NXWidgets::CNxFont
Definition: cnxfont.hxx:70
NxWM
Definition: capplicationwindow.hxx:66
NxWM::CHexCalculator::hide
void hide(void)
Definition: chexcalculator.cxx:377
NxWM::CHexCalculator::m_keypadPos
struct nxgl_point_s m_keypadPos
Definition: chexcalculator.hxx:119
NxWM::CHexCalculator::m_result
bool m_result
Definition: chexcalculator.hxx:137
NxWM::CHexCalculator::getName
NXWidgets::CNxString getName(void)
Definition: chexcalculator.cxx:305
NxWM::CHexCalculator::redraw
void redraw(void)
Definition: chexcalculator.cxx:390
NxWM::CHexCalculator::m_taskbar
CTaskbar * m_taskbar
Definition: chexcalculator.hxx:99
NxWM::CHexCalculator::stop
void stop(void)
Definition: chexcalculator.cxx:342
NXWidgets::CButtonArray
Definition: cbuttonarray.hxx:77
NxWM::CHexCalculator::m_textPos
struct nxgl_point_s m_textPos
Definition: chexcalculator.hxx:120
NxWM::CTaskbar
Definition: ctaskbar.hxx:85
NxWM::CHexCalculator::isFullScreen
bool isFullScreen(void) const
Definition: chexcalculator.cxx:423
NxWM::CHexCalculator::SPendingOperation::value
int64_t value
Definition: chexcalculator.hxx:87
NXWidgets::CWidgetEventArgs
Definition: cwidgeteventargs.hxx:127
NxWM::CHexCalculator
Definition: chexcalculator.hxx:76
NxWM::IApplicationWindow
Definition: iapplicationwindow.hxx:95
NxWM::IApplication
Definition: iapplication.hxx:70
NxWM::CHexCalculator::m_buttonSize
struct nxgl_size_s m_buttonSize
Definition: chexcalculator.hxx:117
NxWM::CHexCalculator::setGeometry
void setGeometry(void)
Definition: chexcalculator.cxx:432
cnxfont.hxx
NxWM::CHexCalculator::m_high
struct SPendingOperation m_high
Definition: chexcalculator.hxx:135
NxWM::CHexCalculator::m_windowSize
struct nxgl_size_s m_windowSize
Definition: chexcalculator.hxx:115
NXWidgets::CNxString
Definition: cnxstring.hxx:121
NxWM::CHexCalculator::m_font
NXWidgets::CNxFont * m_font
Definition: chexcalculator.hxx:108
NxWM::CHexCalculator::m_hexMode
bool m_hexMode
Definition: chexcalculator.hxx:136
NXWidgets::CLabel
Definition: clabel.hxx:112
NxWM::CHexCalculatorFactory
Definition: chexcalculator.hxx:291
ctaskbar.hxx
NxWM::CHexCalculator::updateText
void updateText(void)
Definition: chexcalculator.cxx:670
NxWM::CHexCalculator::m_accum
int64_t m_accum
Definition: chexcalculator.hxx:132
NXWidgets::IBitmap
Definition: ibitmap.hxx:103
NxWM::CHexCalculator::SPendingOperation::operation
uint8_t operation
Definition: chexcalculator.hxx:88
NxWM::CHexCalculator::CHexCalculator
CHexCalculator(CTaskbar *taskbar, CApplicationWindow *window)
Definition: chexcalculator.cxx:204
NxWM::CHexCalculator::destroy
void destroy(void)
Definition: chexcalculator.cxx:361
NxWM::CHexCalculator::handleActionEvent
void handleActionEvent(const NXWidgets::CWidgetEventArgs &e)
Definition: chexcalculator.cxx:713
NxWM::CHexCalculator::run
bool run(void)
Definition: chexcalculator.cxx:316
NxWM::CHexCalculator::getIcon
NXWidgets::IBitmap * getIcon(void)
Definition: chexcalculator.cxx:291
NxWM::CHexCalculator::m_window
CApplicationWindow * m_window
Definition: chexcalculator.hxx:100
NxWM::IApplicationCallback
Definition: iapplicationwindow.hxx:74