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,
79  {
80  private:
81  /**
82  * The structure defines a pending operation.
83  */
84 
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 
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 
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 
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 
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 
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 
326  };
327 }
328 #endif // __cplusplus
329 
330 #endif // __INCLUDE_CHEXCALCULATOR_HXX
NXWidgets::CLabel * m_text
struct nxgl_size_s m_keypadSize
IApplicationWindow * getWindow(void) const
int64_t evaluateBinaryOperation(uint8_t operation, int64_t value1, int64_t value2)
struct SPendingOperation m_low
NXWidgets::CButtonArray * m_keypad
struct nxgl_size_s m_textSize
struct nxgl_point_s m_keypadPos
NXWidgets::CNxString getName(void)
struct nxgl_point_s m_textPos
bool isFullScreen(void) const
struct nxgl_size_s m_buttonSize
struct SPendingOperation m_high
struct nxgl_size_s m_windowSize
NXWidgets::CNxFont * m_font
CHexCalculator(CTaskbar *taskbar, CApplicationWindow *window)
void handleActionEvent(const NXWidgets::CWidgetEventArgs &e)
NXWidgets::IBitmap * getIcon(void)
CApplicationWindow * m_window