canvas.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. //*****************************************************************************
  2. //
  3. // canvas.c - A drawing canvas 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 "canvas.h"
  28. //*****************************************************************************
  29. //
  30. //! \addtogroup canvas_api
  31. //! @{
  32. //
  33. //*****************************************************************************
  34. //*****************************************************************************
  35. //
  36. //! Draws the contents of a canvas.
  37. //!
  38. //! \param pWidget is a pointer to the canvas widget to be drawn.
  39. //!
  40. //! This function draws the contents of a canvas on the display. This is
  41. //! called in response to a \b #WIDGET_MSG_PAINT message.
  42. //!
  43. //! \return None.
  44. //
  45. //*****************************************************************************
  46. static void
  47. CanvasPaint(tWidget *pWidget)
  48. {
  49. tCanvasWidget *pCanvas;
  50. tContext sCtx;
  51. int lX, lY, lSize;
  52. //
  53. // Check the arguments.
  54. //
  55. ASSERT(pWidget);
  56. //
  57. // Convert the generic widget pointer into a canvas widget pointer.
  58. //
  59. pCanvas = (tCanvasWidget *)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 canvas.
  66. //
  67. GrContextClipRegionSet(&sCtx, &(pWidget->sPosition));
  68. //
  69. // See if the canvas fill style is selected.
  70. //
  71. if(pCanvas->ulStyle & CANVAS_STYLE_FILL)
  72. {
  73. //
  74. // Fill the canvas with the fill color.
  75. //
  76. GrContextForegroundSet(&sCtx, pCanvas->ulFillColor);
  77. GrRectFill(&sCtx, &(pWidget->sPosition));
  78. }
  79. //
  80. // See if the canvas outline style is selected.
  81. //
  82. if(pCanvas->ulStyle & CANVAS_STYLE_OUTLINE)
  83. {
  84. //
  85. // Outline the canvas with the outline color.
  86. //
  87. GrContextForegroundSet(&sCtx, pCanvas->ulOutlineColor);
  88. GrRectDraw(&sCtx, &(pWidget->sPosition));
  89. }
  90. //
  91. // See if the canvas text or image style is selected.
  92. //
  93. if(pCanvas->ulStyle & (CANVAS_STYLE_TEXT | CANVAS_STYLE_IMG))
  94. {
  95. //
  96. // Compute the center of the canvas.
  97. //
  98. lX = (pWidget->sPosition.sXMin +
  99. ((pWidget->sPosition.sXMax - pWidget->sPosition.sXMin + 1) / 2));
  100. lY = (pWidget->sPosition.sYMin +
  101. ((pWidget->sPosition.sYMax - pWidget->sPosition.sYMin + 1) / 2));
  102. //
  103. // If the canvas outline style is selected then shrink the clipping
  104. // region by one pixel on each side so that the outline is not
  105. // overwritten by the text or image.
  106. //
  107. if(pCanvas->ulStyle & CANVAS_STYLE_OUTLINE)
  108. {
  109. sCtx.sClipRegion.sXMin++;
  110. sCtx.sClipRegion.sYMin++;
  111. sCtx.sClipRegion.sXMax--;
  112. sCtx.sClipRegion.sYMax--;
  113. }
  114. //
  115. // See if the canvas image style is selected.
  116. //
  117. if(pCanvas->ulStyle & CANVAS_STYLE_IMG)
  118. {
  119. //
  120. // Set the foreground and background colors to use for 1 BPP
  121. // images.
  122. //
  123. GrContextForegroundSet(&sCtx, pCanvas->ulTextColor);
  124. GrContextBackgroundSet(&sCtx, pCanvas->ulFillColor);
  125. //
  126. // Draw the image centered in the canvas.
  127. //
  128. GrImageDraw(&sCtx, pCanvas->pucImage,
  129. lX - (GrImageWidthGet(pCanvas->pucImage) / 2),
  130. lY - (GrImageHeightGet(pCanvas->pucImage) / 2));
  131. }
  132. //
  133. // See if the canvas text style is selected.
  134. //
  135. if(pCanvas->ulStyle & CANVAS_STYLE_TEXT)
  136. {
  137. //
  138. // Set the relevant font and colors.
  139. //
  140. GrContextFontSet(&sCtx, pCanvas->pFont);
  141. GrContextForegroundSet(&sCtx, pCanvas->ulTextColor);
  142. GrContextBackgroundSet(&sCtx, pCanvas->ulFillColor);
  143. //
  144. // Determine the drawing position for the string based on the
  145. // text alignment style. First consider the horizontal case. We
  146. // enter this section with lX set to the center of the widget.
  147. //
  148. //
  149. // How wide is the string?
  150. //
  151. lSize = GrStringWidthGet(&sCtx, pCanvas->pcText, -1);
  152. if(pCanvas->ulStyle & CANVAS_STYLE_TEXT_LEFT)
  153. {
  154. //
  155. // The string is to be aligned with the left edge of
  156. // the widget. Use the clipping rectangle as reference
  157. // since this will ensure that the string doesn't
  158. // encroach on any border that is set.
  159. //
  160. lX = sCtx.sClipRegion.sXMin;
  161. }
  162. else
  163. {
  164. if(pCanvas->ulStyle & CANVAS_STYLE_TEXT_RIGHT)
  165. {
  166. //
  167. // The string is to be aligned with the right edge of
  168. // the widget. Use the clipping rectangle as reference
  169. // since this will ensure that the string doesn't
  170. // encroach on any border that is set.
  171. //
  172. lX = sCtx.sClipRegion.sXMax - lSize;
  173. }
  174. else
  175. {
  176. //
  177. // We are centering the string horizontally so adjust
  178. // the position accordingly to take into account the
  179. // width of the string.
  180. //
  181. lX -= (lSize / 2);
  182. }
  183. }
  184. //
  185. // Now consider the horizontal case. We enter this section with lY
  186. // set to the center of the widget.
  187. //
  188. // How tall is the string?
  189. //
  190. lSize = GrStringHeightGet(&sCtx);
  191. if(pCanvas->ulStyle & CANVAS_STYLE_TEXT_TOP)
  192. {
  193. //
  194. // The string is to be aligned with the top edge of
  195. // the widget. Use the clipping rectangle as reference
  196. // since this will ensure that the string doesn't
  197. // encroach on any border that is set.
  198. //
  199. lY = sCtx.sClipRegion.sYMin;
  200. }
  201. else
  202. {
  203. if(pCanvas->ulStyle & CANVAS_STYLE_TEXT_BOTTOM)
  204. {
  205. //
  206. // The string is to be aligned with the bottom edge of
  207. // the widget. Use the clipping rectangle as reference
  208. // since this will ensure that the string doesn't
  209. // encroach on any border that is set.
  210. //
  211. lY = sCtx.sClipRegion.sYMax - lSize;
  212. }
  213. else
  214. {
  215. //
  216. // We are centering the string vertically so adjust
  217. // the position accordingly to take into account the
  218. // height of the string.
  219. //
  220. lY -= (lSize / 2);
  221. }
  222. }
  223. //
  224. // Now draw the string.
  225. //
  226. GrStringDraw(&sCtx, pCanvas->pcText, -1, lX, lY,
  227. pCanvas->ulStyle & CANVAS_STYLE_TEXT_OPAQUE);
  228. }
  229. }
  230. //
  231. // See if the application-drawn style is selected.
  232. //
  233. if(pCanvas->ulStyle & CANVAS_STYLE_APP_DRAWN)
  234. {
  235. //
  236. // Call the application-supplied function to draw the canvas.
  237. //
  238. pCanvas->pfnOnPaint(pWidget, &sCtx);
  239. }
  240. }
  241. //*****************************************************************************
  242. //
  243. //! Handles messages for a canvas widget.
  244. //!
  245. //! \param pWidget is a pointer to the canvas widget.
  246. //! \param ulMsg is the message.
  247. //! \param ulParam1 is the first parameter to the message.
  248. //! \param ulParam2 is the second parameter to the message.
  249. //!
  250. //! This function receives messages intended for this canvas widget and
  251. //! processes them accordingly. The processing of the message varies based on
  252. //! the message in question.
  253. //!
  254. //! Unrecognized messages are handled by calling WidgetDefaultMsgProc().
  255. //!
  256. //! \return Returns a value appropriate to the supplied message.
  257. //
  258. //*****************************************************************************
  259. int
  260. CanvasMsgProc(tWidget *pWidget, unsigned int ulMsg, unsigned int ulParam1,
  261. unsigned int ulParam2)
  262. {
  263. //
  264. // Check the arguments.
  265. //
  266. ASSERT(pWidget);
  267. //
  268. // Determine which message is being sent.
  269. //
  270. switch(ulMsg)
  271. {
  272. //
  273. // The widget paint request has been sent.
  274. //
  275. case WIDGET_MSG_PAINT:
  276. {
  277. //
  278. // Handle the widget paint request.
  279. //
  280. CanvasPaint(pWidget);
  281. //
  282. // Return one to indicate that the message was successfully
  283. // processed.
  284. //
  285. return(1);
  286. }
  287. //
  288. // An unknown request has been sent.
  289. //
  290. default:
  291. {
  292. //
  293. // Let the default message handler process this message.
  294. //
  295. return(WidgetDefaultMsgProc(pWidget, ulMsg, ulParam1, ulParam2));
  296. }
  297. }
  298. }
  299. //*****************************************************************************
  300. //
  301. //! Initializes a canvas widget.
  302. //!
  303. //! \param pWidget is a pointer to the canvas widget to initialize.
  304. //! \param pDisplay is a pointer to the display on which to draw the canvas.
  305. //! \param lX is the X coordinate of the upper left corner of the canvas.
  306. //! \param lY is the Y coordinate of the upper left corner of the canvas.
  307. //! \param lWidth is the width of the canvas.
  308. //! \param lHeight is the height of the canvas.
  309. //!
  310. //! This function initializes the provided canvas widget.
  311. //!
  312. //! \return None.
  313. //
  314. //*****************************************************************************
  315. void
  316. CanvasInit(tCanvasWidget *pWidget, const tDisplay *pDisplay, int lX, int lY,
  317. int lWidth, int lHeight)
  318. {
  319. unsigned int ulIdx;
  320. //
  321. // Check the arguments.
  322. //
  323. ASSERT(pWidget);
  324. ASSERT(pDisplay);
  325. //
  326. // Clear out the widget structure.
  327. //
  328. for(ulIdx = 0; ulIdx < sizeof(tCanvasWidget); ulIdx += 4)
  329. {
  330. ((unsigned int *)pWidget)[ulIdx / 4] = 0;
  331. }
  332. //
  333. // Set the size of the canvas widget structure.
  334. //
  335. pWidget->sBase.lSize = sizeof(tCanvasWidget);
  336. //
  337. // Mark this widget as fully disconnected.
  338. //
  339. pWidget->sBase.pParent = 0;
  340. pWidget->sBase.pNext = 0;
  341. pWidget->sBase.pChild = 0;
  342. //
  343. // Save the display pointer.
  344. //
  345. pWidget->sBase.pDisplay = pDisplay;
  346. //
  347. // Set the extents of this canvas.
  348. //
  349. pWidget->sBase.sPosition.sXMin = lX;
  350. pWidget->sBase.sPosition.sYMin = lY;
  351. pWidget->sBase.sPosition.sXMax = lX + lWidth - 1;
  352. pWidget->sBase.sPosition.sYMax = lY + lHeight - 1;
  353. //
  354. // Use the canvas message handler to process messages to this canvas.
  355. //
  356. pWidget->sBase.pfnMsgProc = CanvasMsgProc;
  357. }
  358. //*****************************************************************************
  359. //
  360. // Close the Doxygen group.
  361. //! @}
  362. //
  363. //*****************************************************************************