cmenus.hxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /////////////////////////////////////////////////////////////////////////////
  2. // apps/include/graphics/twm4nx/Cmenus.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. // Largely an original work but derives from TWM 1.0.10 in many ways:
  21. //
  22. // Copyright 1989,1998 The Open Group
  23. // Copyright 1988 by Evans & Sutherland Computer Corporation,
  24. //
  25. // Please refer to apps/twm4nx/COPYING for detailed copyright information.
  26. // Although not listed as a copyright holder, thanks and recognition need
  27. // to go to Tom LaStrange, the original author of TWM.
  28. #ifndef __APPS_INCLUDE_GRAPHICS_TWM4NX_CMENUS_HXX
  29. #define __APPS_INCLUDE_GRAPHICS_TWM4NX_CMENUS_HXX
  30. /////////////////////////////////////////////////////////////////////////////
  31. // Included Files
  32. /////////////////////////////////////////////////////////////////////////////
  33. #include <mqueue.h>
  34. #include "graphics/nxwidgets/cwidgeteventhandler.hxx"
  35. #include "graphics/nxwidgets/cwidgeteventargs.hxx"
  36. #include "graphics/nxwidgets/cnxtkwindow.hxx"
  37. #include "graphics/twm4nx/cwindow.hxx"
  38. #include "graphics/twm4nx/ctwm4nxevent.hxx"
  39. #include "graphics/twm4nx/iapplication.hxx"
  40. /////////////////////////////////////////////////////////////////////////////
  41. // Pre-processor Definitions
  42. /////////////////////////////////////////////////////////////////////////////
  43. #define TWM_WINDOWS "TwmNxWindows" // for f.menu "TwmNxWindows"
  44. #define SIZE_HINDENT 10
  45. #define SIZE_VINDENT 2
  46. #define MAXMENUDEPTH 10 // max number of nested menus
  47. #define MOVE_NONE 0 // modes of constrained move
  48. #define MOVE_VERT 1
  49. #define MOVE_HORIZ 2
  50. #define SHADOWWIDTH 5 // in pixels
  51. // Info stings defines
  52. #define INFO_LINES 30
  53. #define INFO_SIZE 200
  54. /////////////////////////////////////////////////////////////////////////////
  55. // Implementation Classes
  56. /////////////////////////////////////////////////////////////////////////////
  57. namespace NXWidgets
  58. {
  59. class CButtonArray; // Forward reference
  60. class CWidgetEventArgs; // Forward reference
  61. class CWidgetEventArgs; // Forward reference
  62. }
  63. namespace Twm4Nx
  64. {
  65. struct SEventMsg; // Forward referernce
  66. class CMenus; // Forward reference
  67. struct SMenuItem
  68. {
  69. FAR struct SMenuItem *flink; /**< Forward link to next menu item */
  70. FAR struct SMenuItem *blink; /**< Backward link previous menu item */
  71. FAR NXWidgets::CNxString text; /**< The text string for the menu item */
  72. FAR CMenus *subMenu; /**< Menu root of a pull right menu */
  73. FAR CTwm4NxEvent *handler; /**< Application event handler */
  74. uint16_t event; /**< Menu selection event */
  75. };
  76. class CMenus : protected NXWidgets::CWidgetEventHandler, public CTwm4NxEvent
  77. {
  78. private:
  79. CTwm4Nx *m_twm4nx; /**< Cached Twm4Nx session */
  80. mqd_t m_eventq; /**< NxWidget event message queue */
  81. FAR CWindow *m_menuWindow; /**< The menu window */
  82. FAR NXWidgets::CButtonArray *m_buttons; /**< The menu button array */
  83. FAR struct SMenuItem *m_menuHead; /**< First item in menu */
  84. FAR struct SMenuItem *m_menuTail; /**< Last item in menu */
  85. NXWidgets::CNxString m_menuName; /**< The name of the menu */
  86. nxgl_coord_t m_entryHeight; /**< Menu entry height */
  87. uint16_t m_nMenuItems; /**< Number of items in the menu */
  88. uint8_t m_nrows; /**< Number of rows in the button array */
  89. char m_info[INFO_LINES][INFO_SIZE];
  90. /**
  91. * Convert the position of a menu window to the position of
  92. * the containing frame.
  93. */
  94. void menuToFramePos(FAR const struct nxgl_point_s *menupos,
  95. FAR struct nxgl_point_s *framepos);
  96. /**
  97. * Convert the position of the containing frame to the position of
  98. * the menu window.
  99. */
  100. void frameToMenuPos(FAR const struct nxgl_point_s *framepos,
  101. FAR struct nxgl_point_s *menupos);
  102. /**
  103. * Convert the size of a menu window to the size of the containing
  104. * frame.
  105. */
  106. void menuToFrameSize(FAR const struct nxgl_size_s *menusize,
  107. FAR struct nxgl_size_s *framesize);
  108. /**
  109. * Convert the size of a containing frame to the size of the menu
  110. * window.
  111. */
  112. void frameToMenuSize(FAR const struct nxgl_size_s *framesize,
  113. FAR struct nxgl_size_s *menusize);
  114. /**
  115. * Create the menu window. Menu windows are always created in the
  116. * hidden state. When the menu is selected, then it should be shown.
  117. *
  118. * @result True is returned on success
  119. */
  120. bool createMenuWindow(void);
  121. /**
  122. * Calculate the optimal menu frame size
  123. *
  124. * @param frameSize The location to return the calculated frame size
  125. */
  126. void getMenuFrameSize(FAR struct nxgl_size_s &frameSize);
  127. /**
  128. * Calculate the optimal menu window size
  129. *
  130. * @param size The location to return the calculated window size
  131. */
  132. void getMenuWindowSize(FAR struct nxgl_size_s &size);
  133. /**
  134. * Update the menu window size
  135. *
  136. * @result True is returned on success
  137. */
  138. bool setMenuWindowSize(void);
  139. /**
  140. * Set the position of the menu window. Supports positioning of a
  141. * pop-up window.
  142. *
  143. * @param framePos The position of the menu window frame
  144. * @result True is returned on success
  145. */
  146. bool setMenuWindowPosition(FAR struct nxgl_point_s *framePos);
  147. /**
  148. * Set the position of the menu window. Supports presentation of a
  149. * pop-up window.
  150. *
  151. * @param framePos The position of the menu window frame
  152. * @result True is returned on success
  153. */
  154. inline bool raiseMenuWindow()
  155. {
  156. return m_menuWindow->raiseWindow();
  157. }
  158. /**
  159. * Create the menu button array
  160. *
  161. * @result True is returned on success
  162. */
  163. bool createMenuButtonArray(void);
  164. void paintMenu(void);
  165. void destroyMenu(void);
  166. /**
  167. * Handle a widget action event, overriding the CWidgetEventHandler
  168. * method. This will indicate a button pre-release event.
  169. *
  170. * @param e The event data.
  171. */
  172. void handleActionEvent(const NXWidgets::CWidgetEventArgs &e);
  173. /**
  174. * Cleanup or initialization error or on deconstruction.
  175. */
  176. void cleanup(void);
  177. public:
  178. /**
  179. * CMenus Constructor
  180. *
  181. * @param twm4nx. Twm4Nx session
  182. */
  183. CMenus(CTwm4Nx *twm4nx);
  184. /**
  185. * CMenus Destructor
  186. */
  187. ~CMenus(void);
  188. /**
  189. * CMenus Initializer. Performs the parts of the CMenus construction
  190. * that may fail. The menu window is created but is not initially
  191. * visible. Use the show() method to make the menu visible.
  192. *
  193. * @param name The menu name
  194. * @result True is returned on success
  195. */
  196. bool initialize(FAR NXWidgets::CNxString &name);
  197. /**
  198. * Add an item to a menu
  199. *
  200. * @param item Describes the menu item entry
  201. * @return True if the menu item was added successfully
  202. */
  203. bool addMenuItem(FAR IApplication *item);
  204. /**
  205. * Return the size of the menu window frame
  206. *
  207. * @param frameSize The location in which to return the current menu
  208. * window frame size.
  209. * @result True is returned on success
  210. */
  211. bool getFrameSize(FAR struct nxgl_size_s *frameSize)
  212. {
  213. return m_menuWindow->getFrameSize(frameSize);
  214. }
  215. /**
  216. * Set the position of the menu window frame
  217. *
  218. * @param framePos The new menum window frame position
  219. * @result True is returned on success
  220. */
  221. bool getFramePosition(FAR struct nxgl_point_s *framePos)
  222. {
  223. return m_menuWindow->getFramePosition(framePos);
  224. }
  225. /**
  226. * Set the position of the menu window frame
  227. *
  228. * @param framePos The new menum window frame position
  229. * @result True is returned on success
  230. */
  231. bool setFramePosition(FAR const struct nxgl_point_s *framePos)
  232. {
  233. return m_menuWindow->setFramePosition(framePos);
  234. }
  235. /**
  236. * Return true if the menu is currently being displayed
  237. *
  238. * @return True if the menu is visible
  239. */
  240. inline bool isVisible(void)
  241. {
  242. return !m_menuWindow->isIconified();
  243. }
  244. /**
  245. * Make the menu visible.
  246. *
  247. * @return True if the menu is shown.
  248. */
  249. inline bool show(void)
  250. {
  251. return m_menuWindow->deIconify();
  252. }
  253. /**
  254. * Hide the menu
  255. *
  256. * @return True if the menu was hidden.
  257. */
  258. inline bool hide(void)
  259. {
  260. return m_menuWindow->iconify();
  261. }
  262. /**
  263. * Handle MENU events.
  264. *
  265. * @param msg. The received NxWidget MENU event message.
  266. * @return True if the message was properly handled. false is
  267. * return on any failure.
  268. */
  269. bool event(FAR struct SEventMsg *msg);
  270. };
  271. }
  272. #endif // __APPS_INCLUDE_GRAPHICS_TWM4NX_CMENUS_HXX