chexcalculator.hxx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /****************************************************************************
  2. * apps/include/graphics/nxwm/chexcalculator.hxx
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. #ifndef __APPS_INCLUDE_GRAPHICS_NXWM_CHEXCALCULATOR_HXX
  21. #define __APPS_INCLUDE_GRAPHICS_NXWM_CHEXCALCULATOR_HXX
  22. /****************************************************************************
  23. * Included Files
  24. ****************************************************************************/
  25. #include <nuttx/config.h>
  26. #include <sys/types.h>
  27. #include <nuttx/nx/nxtk.h>
  28. #include "graphics/nxwidgets/cbuttonarray.hxx"
  29. #include "graphics/nxwidgets/clabel.hxx"
  30. #include "graphics/nxwidgets/cnxfont.hxx"
  31. #include "graphics/nxwm/iapplication.hxx"
  32. #include "graphics/nxwm/capplicationwindow.hxx"
  33. #include "graphics/nxwm/ctaskbar.hxx"
  34. /****************************************************************************
  35. * Pre-Processor Definitions
  36. ****************************************************************************/
  37. #define NXWM_HEXCALCULATOR_NROWS 6
  38. #define NXWM_HEXCALCULATOR_NCOLUMNS 6
  39. /****************************************************************************
  40. * Implementation Classes
  41. ****************************************************************************/
  42. #if defined(__cplusplus)
  43. namespace NxWM
  44. {
  45. /**
  46. * This class implements the Hex calculator application.
  47. */
  48. class CHexCalculator : public IApplication,
  49. private IApplicationCallback,
  50. private NXWidgets::CWidgetEventHandler
  51. {
  52. private:
  53. /**
  54. * The structure defines a pending operation.
  55. */
  56. struct SPendingOperation
  57. {
  58. int64_t value; /**< Accumulated value */
  59. uint8_t operation; /**< Identifies the operations */
  60. };
  61. /**
  62. * Calculator state data.
  63. */
  64. /**
  65. * Cached constructor parameters.
  66. */
  67. CTaskbar *m_taskbar; /**< Reference to the "parent" taskbar */
  68. CApplicationWindow *m_window; /**< Reference to the application window */
  69. /**
  70. * Widgets
  71. */
  72. NXWidgets::CButtonArray *m_keypad; /**< The calculator keyboard */
  73. NXWidgets::CLabel *m_text; /**< The accumulator text display */
  74. NXWidgets::CNxFont *m_font; /**< The font used in the calculator */
  75. /**
  76. * Calculator geometry. This stuff does not really have to be retained
  77. * in memory. If you are pinched for memory, get rid of these.
  78. */
  79. struct nxgl_size_s m_windowSize; /**< The size of the calculator window */
  80. struct nxgl_size_s m_keypadSize; /**< The size the calculator keypad */
  81. struct nxgl_size_s m_buttonSize; /**< The size of one calculator button */
  82. struct nxgl_size_s m_textSize; /**< The size of the calculator textbox */
  83. struct nxgl_point_s m_keypadPos; /**< The position the calculator keypad */
  84. struct nxgl_point_s m_textPos; /**< The position of the calculator textbox */
  85. /**
  86. * Calculator computational data. Note: Since we do not support
  87. * parentheses and support only two levels of operator precedence, it is
  88. * not necessary to maintain a stack of operations. Within there
  89. * limitations, there can be at most only one pending low prececence
  90. * operation and one pending high precedence operation. If you want
  91. * to support parentheses or more levels of precedence, they you will
  92. * have to extend the design.
  93. */
  94. int64_t m_accum; /**< The current accumulated value */
  95. int64_t m_memory; /**< The current value saved in memory */
  96. struct SPendingOperation m_low; /**< Low precedence pending operation */
  97. struct SPendingOperation m_high; /**< High precedence pending operation */
  98. bool m_hexMode; /**< True if in hex mode */
  99. bool m_result ; /**< True if the accumulator holds a previoius result */
  100. /**
  101. * Select the geometry of the calculator given the current window size.
  102. * Only called as part of construction.
  103. */
  104. inline void setGeometry(void);
  105. /**
  106. * Create the calculator keypad. Only start as part of the application
  107. * start method.
  108. */
  109. inline bool createCalculator(void);
  110. /**
  111. * Applies labels to the keys.
  112. */
  113. void labelKeypad(void);
  114. /**
  115. * Evaluate a binary operation.
  116. *
  117. * @param value1. The first value
  118. * @param value2. The second value
  119. *
  120. * @return The result of the operation
  121. */
  122. int64_t evaluateBinaryOperation(uint8_t operation, int64_t value1, int64_t value2);
  123. /**
  124. * Show the current value of the accumulator.
  125. */
  126. void updateText(void);
  127. /**
  128. * Called when the window minimize button is pressed.
  129. */
  130. void minimize(void);
  131. /**
  132. * Called when the window minimize close is pressed.
  133. */
  134. void close(void);
  135. /**
  136. * Handle a widget action event. For CImage, this is a button pre-
  137. * release event.
  138. *
  139. * @param e The event data.
  140. */
  141. void handleActionEvent(const NXWidgets::CWidgetEventArgs &e);
  142. public:
  143. /**
  144. * CHexCalculator constructor
  145. *
  146. * @param window. The application window
  147. *
  148. * @param taskbar. A pointer to the parent task bar instance
  149. * @param window. The window to be used by this application.
  150. */
  151. CHexCalculator(CTaskbar *taskbar, CApplicationWindow *window);
  152. /**
  153. * CHexCalculator destructor
  154. */
  155. ~CHexCalculator(void);
  156. /**
  157. * Each implementation of IApplication must provide a method to recover
  158. * the contained CApplicationWindow instance.
  159. */
  160. IApplicationWindow *getWindow(void) const;
  161. /**
  162. * Get the icon associated with the application
  163. *
  164. * @return An instance if IBitmap that may be used to rend the
  165. * application's icon. This is an new IBitmap instance that must
  166. * be deleted by the caller when it is no long needed.
  167. */
  168. NXWidgets::IBitmap *getIcon(void);
  169. /**
  170. * Get the name string associated with the application
  171. *
  172. * @return A copy if CNxString that contains the name of the application.
  173. */
  174. NXWidgets::CNxString getName(void);
  175. /**
  176. * Start the application (perhaps in the minimized state).
  177. *
  178. * @return True if the application was successfully started.
  179. */
  180. bool run(void);
  181. /**
  182. * Stop the application.
  183. */
  184. void stop(void);
  185. /**
  186. * Destroy the application and free all of its resources. This method
  187. * will initiate blocking of messages from the NX server. The server
  188. * will flush the window message queue and reply with the blocked
  189. * message. When the block message is received by CWindowMessenger,
  190. * it will send the destroy message to the start window task which
  191. * will, finally, safely delete the application.
  192. */
  193. void destroy(void);
  194. /**
  195. * The application window is hidden (either it is minimized or it is
  196. * maximized, but not at the top of the hierarchy
  197. */
  198. void hide(void);
  199. /**
  200. * Redraw the entire window. The application has been maximized or
  201. * otherwise moved to the top of the hierarchy. This method is call from
  202. * CTaskbar when the application window must be displayed
  203. */
  204. void redraw(void);
  205. /**
  206. * Report of this is a "normal" window or a full screen window. The
  207. * primary purpose of this method is so that window manager will know
  208. * whether or not it show draw the task bar.
  209. *
  210. * @return True if this is a full screen window.
  211. */
  212. bool isFullScreen(void) const;
  213. };
  214. class CHexCalculatorFactory : public IApplicationFactory
  215. {
  216. private:
  217. CTaskbar *m_taskbar; /**< The taskbar */
  218. public:
  219. /**
  220. * CHexCalculatorFactory Constructor
  221. *
  222. * @param taskbar. The taskbar instance used to terminate calibration
  223. */
  224. CHexCalculatorFactory(CTaskbar *taskbar);
  225. /**
  226. * CHexCalculatorFactory Destructor
  227. */
  228. inline ~CHexCalculatorFactory(void) { }
  229. /**
  230. * Create a new instance of an CHexCalculator (as IApplication).
  231. */
  232. IApplication *create(void);
  233. /**
  234. * Get the icon associated with the application
  235. *
  236. * @return An instance if IBitmap that may be used to rend the
  237. * application's icon. This is an new IBitmap instance that must
  238. * be deleted by the caller when it is no long needed.
  239. */
  240. NXWidgets::IBitmap *getIcon(void);
  241. };
  242. }
  243. #endif // __cplusplus
  244. #endif // __APPS_INCLUDE_GRAPHICS_NXWM_CHEXCALCULATOR_HXX