container.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //*****************************************************************************
  2. //
  3. // container.c - Generic container widget.
  4. //
  5. // Copyright (c) 2008-2010 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of revision 6288 of the Stellaris Graphics Library.
  22. //
  23. //*****************************************************************************
  24. #include "debug.h"
  25. #include "grlib.h"
  26. #include "widget.h"
  27. #include "container.h"
  28. //*****************************************************************************
  29. //
  30. //! \addtogroup container_api
  31. //! @{
  32. //
  33. //*****************************************************************************
  34. //*****************************************************************************
  35. //
  36. //! Draws a container widget.
  37. //!
  38. //! \param pWidget is a pointer to the container widget to be drawn.
  39. //!
  40. //! This function draws a container widget on the display. This is called in
  41. //! response to a \b #WIDGET_MSG_PAINT message.
  42. //!
  43. //! \return None.
  44. //
  45. //*****************************************************************************
  46. static void
  47. ContainerPaint(tWidget *pWidget)
  48. {
  49. tContainerWidget *pContainer;
  50. int lX1, lX2, lY;
  51. tContext sCtx;
  52. //
  53. // Check the arguments.
  54. //
  55. ASSERT(pWidget);
  56. //
  57. // Convert the generic widget pointer into a container widget pointer.
  58. //
  59. pContainer = (tContainerWidget *)pWidget;
  60. //
  61. // Initialize a drawing context.
  62. //
  63. GrContextInit(&sCtx, pWidget->pDisplay);
  64. //
  65. // Initialize the clipping region based on the extents of this container.
  66. //
  67. GrContextClipRegionSet(&sCtx, &(pWidget->sPosition));
  68. //
  69. // See if the container fill style is selected.
  70. //
  71. if(pContainer->ulStyle & CTR_STYLE_FILL)
  72. {
  73. //
  74. // Fill the container with the fill color.
  75. //
  76. GrContextForegroundSet(&sCtx, pContainer->ulFillColor);
  77. GrRectFill(&sCtx, &(pWidget->sPosition));
  78. }
  79. //
  80. // See if the container text style is selected.
  81. //
  82. if(pContainer->ulStyle & CTR_STYLE_TEXT)
  83. {
  84. //
  85. // Set the font and colors used to draw the container text.
  86. //
  87. GrContextFontSet(&sCtx, pContainer->pFont);
  88. GrContextForegroundSet(&sCtx, pContainer->ulTextColor);
  89. GrContextBackgroundSet(&sCtx, pContainer->ulFillColor);
  90. //
  91. // Get the width of the container text.
  92. //
  93. lX2 = GrStringWidthGet(&sCtx, pContainer->pcText, -1);
  94. //
  95. // Determine the position of the text. The position depends on the
  96. // the width of the string and if centering is enabled.
  97. //
  98. if(pContainer->ulStyle & CTR_STYLE_TEXT_CENTER)
  99. {
  100. lX1 = (pWidget->sPosition.sXMin +
  101. ((pWidget->sPosition.sXMax - pWidget->sPosition.sXMin + 1 -
  102. lX2 - 8) / 2));
  103. }
  104. else
  105. {
  106. lX1 = pWidget->sPosition.sXMin + 4;
  107. }
  108. //
  109. // Draw the container text.
  110. //
  111. GrStringDraw(&sCtx, pContainer->pcText, -1, lX1 + 4,
  112. pWidget->sPosition.sYMin,
  113. pContainer->ulStyle & CTR_STYLE_TEXT_OPAQUE);
  114. //
  115. // See if the container outline style is selected.
  116. //
  117. if(pContainer->ulStyle & CTR_STYLE_OUTLINE)
  118. {
  119. //
  120. // Get the position of the right side of the string.
  121. //
  122. lX2 = lX1 + lX2 + 8;
  123. //
  124. // Get the position of the vertical center of the text.
  125. //
  126. lY = (pWidget->sPosition.sYMin +
  127. (GrFontBaselineGet(pContainer->pFont) / 2));
  128. //
  129. // Set the color to draw the outline.
  130. //
  131. GrContextForegroundSet(&sCtx, pContainer->ulOutlineColor);
  132. //
  133. // Draw the outline around the container widget, leaving a gap
  134. // where the text reside across the top of the widget.
  135. //
  136. GrLineDraw(&sCtx, lX1, lY, pWidget->sPosition.sXMin, lY);
  137. GrLineDraw(&sCtx, pWidget->sPosition.sXMin, lY,
  138. pWidget->sPosition.sXMin, pWidget->sPosition.sYMax);
  139. GrLineDraw(&sCtx, pWidget->sPosition.sXMin,
  140. pWidget->sPosition.sYMax, pWidget->sPosition.sXMax,
  141. pWidget->sPosition.sYMax);
  142. GrLineDraw(&sCtx, pWidget->sPosition.sXMax,
  143. pWidget->sPosition.sYMax, pWidget->sPosition.sXMax, lY);
  144. GrLineDraw(&sCtx, pWidget->sPosition.sXMax, lY, lX2, lY);
  145. }
  146. }
  147. //
  148. // Otherwise, see if the container outline style is selected.
  149. //
  150. else if(pContainer->ulStyle & CTR_STYLE_OUTLINE)
  151. {
  152. //
  153. // Outline the container with the outline color.
  154. //
  155. GrContextForegroundSet(&sCtx, pContainer->ulOutlineColor);
  156. GrRectDraw(&sCtx, &(pWidget->sPosition));
  157. }
  158. }
  159. //*****************************************************************************
  160. //
  161. //! Handles messages for a container widget.
  162. //!
  163. //! \param pWidget is a pointer to the container widget.
  164. //! \param ulMsg is the message.
  165. //! \param ulParam1 is the first parameter to the message.
  166. //! \param ulParam2 is the second parameter to the message.
  167. //!
  168. //! This function receives messages intended for this container widget and
  169. //! processes them accordingly. The processing of the message varies based on
  170. //! the message in question.
  171. //!
  172. //! Unrecognized messages are handled by calling WidgetDefaultMsgProc().
  173. //!
  174. //! \return Returns a value appropriate to the supplied message.
  175. //
  176. //*****************************************************************************
  177. int
  178. ContainerMsgProc(tWidget *pWidget, unsigned int ulMsg, unsigned int ulParam1,
  179. unsigned int ulParam2)
  180. {
  181. //
  182. // Check the arguments.
  183. //
  184. ASSERT(pWidget);
  185. //
  186. // Determine which message is being sent.
  187. //
  188. switch(ulMsg)
  189. {
  190. //
  191. // The widget paint request has been sent.
  192. //
  193. case WIDGET_MSG_PAINT:
  194. {
  195. //
  196. // Handle the widget paint request.
  197. //
  198. ContainerPaint(pWidget);
  199. //
  200. // Return one to indicate that the message was successfully
  201. // processed.
  202. //
  203. return(1);
  204. }
  205. //
  206. // An unknown request has been sent.
  207. //
  208. default:
  209. {
  210. //
  211. // Let the default message handler process this message.
  212. //
  213. return(WidgetDefaultMsgProc(pWidget, ulMsg, ulParam1, ulParam2));
  214. }
  215. }
  216. }
  217. //*****************************************************************************
  218. //
  219. //! Initializes a container widget.
  220. //!
  221. //! \param pWidget is a pointer to the container widget to initialize.
  222. //! \param pDisplay is a pointer to the display on which to draw the container
  223. //! widget.
  224. //! \param lX is the X coordinate of the upper left corner of the container
  225. //! widget.
  226. //! \param lY is the Y coordinate of the upper left corner of the container
  227. //! widget.
  228. //! \param lWidth is the width of the container widget.
  229. //! \param lHeight is the height of the container widget.
  230. //!
  231. //! This function initializes a container widget, preparing it for placement
  232. //! into the widget tree.
  233. //!
  234. //! \return none.
  235. //
  236. //*****************************************************************************
  237. void
  238. ContainerInit(tContainerWidget *pWidget, const tDisplay *pDisplay, int lX,
  239. int lY, int lWidth, int lHeight)
  240. {
  241. unsigned int ulIdx;
  242. //
  243. // Check the arguments.
  244. //
  245. ASSERT(pWidget);
  246. ASSERT(pDisplay);
  247. //
  248. // Clear out the widget structure.
  249. //
  250. for(ulIdx = 0; ulIdx < sizeof(tContainerWidget); ulIdx += 4)
  251. {
  252. ((unsigned int *)pWidget)[ulIdx / 4] = 0;
  253. }
  254. //
  255. // Set the size of the container widget structure.
  256. //
  257. pWidget->sBase.lSize = sizeof(tContainerWidget);
  258. //
  259. // Mark this widget as fully disconnected.
  260. //
  261. pWidget->sBase.pParent = 0;
  262. pWidget->sBase.pNext = 0;
  263. pWidget->sBase.pChild = 0;
  264. //
  265. // Save the display pointer.
  266. //
  267. pWidget->sBase.pDisplay = pDisplay;
  268. //
  269. // Set the extents of this container widget.
  270. //
  271. pWidget->sBase.sPosition.sXMin = lX;
  272. pWidget->sBase.sPosition.sYMin = lY;
  273. pWidget->sBase.sPosition.sXMax = lX + lWidth - 1;
  274. pWidget->sBase.sPosition.sYMax = lY + lHeight - 1;
  275. //
  276. // Use the container widget message handler to process messages to this
  277. // container widget.
  278. //
  279. pWidget->sBase.pfnMsgProc = ContainerMsgProc;
  280. }
  281. //*****************************************************************************
  282. //
  283. // Close the Doxygen group.
  284. //! @}
  285. //
  286. //*****************************************************************************