checkbox.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. //*****************************************************************************
  2. //
  3. // checkbox.c - Check box 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 "checkbox.h"
  28. //*****************************************************************************
  29. //
  30. //! \addtogroup checkbox_api
  31. //! @{
  32. //
  33. //*****************************************************************************
  34. //*****************************************************************************
  35. //
  36. //! Draws a check box widget.
  37. //!
  38. //! \param pWidget is a pointer to the check box widget to be drawn.
  39. //! \param bClick is a boolean that is \b true if the paint request is a result
  40. //! of a pointer click and \b false if not.
  41. //!
  42. //! This function draws a check box widget on the display. This is called in
  43. //! response to a \b #WIDGET_MSG_PAINT message.
  44. //!
  45. //! \return None.
  46. //
  47. //*****************************************************************************
  48. static void
  49. CheckBoxPaint(tWidget *pWidget, unsigned int bClick)
  50. {
  51. tCheckBoxWidget *pCheck;
  52. tRectangle sRect;
  53. tContext sCtx;
  54. int lY;
  55. //
  56. // Check the arguments.
  57. //
  58. ASSERT(pWidget);
  59. //
  60. // Convert the generic widget pointer into a check box widget pointer.
  61. //
  62. pCheck = (tCheckBoxWidget *)pWidget;
  63. //
  64. // Initialize a drawing context.
  65. //
  66. GrContextInit(&sCtx, pWidget->pDisplay);
  67. //
  68. // Initialize the clipping region based on the extents of this check box.
  69. //
  70. GrContextClipRegionSet(&sCtx, &(pWidget->sPosition));
  71. //
  72. // See if the check box fill style is selected.
  73. //
  74. if((pCheck->usStyle & CB_STYLE_FILL) && !bClick)
  75. {
  76. //
  77. // Fill the check box with the fill color.
  78. //
  79. GrContextForegroundSet(&sCtx, pCheck->ulFillColor);
  80. GrRectFill(&sCtx, &(pWidget->sPosition));
  81. }
  82. //
  83. // See if the check box outline style is selected.
  84. //
  85. if((pCheck->usStyle & CB_STYLE_OUTLINE) && !bClick)
  86. {
  87. //
  88. // Outline the check box with the outline color.
  89. //
  90. GrContextForegroundSet(&sCtx, pCheck->ulOutlineColor);
  91. GrRectDraw(&sCtx, &(pWidget->sPosition));
  92. }
  93. //
  94. // Draw the check box.
  95. //
  96. sRect.sXMin = pWidget->sPosition.sXMin + 2;
  97. sRect.sYMin = (pWidget->sPosition.sYMin +
  98. ((pWidget->sPosition.sYMax - pWidget->sPosition.sYMin -
  99. pCheck->usBoxSize + 1) / 2));
  100. sRect.sXMax = sRect.sXMin + pCheck->usBoxSize - 1;
  101. sRect.sYMax = sRect.sYMin + pCheck->usBoxSize - 1;
  102. if(!bClick)
  103. {
  104. GrContextForegroundSet(&sCtx, pCheck->ulOutlineColor);
  105. GrRectDraw(&sCtx, &sRect);
  106. }
  107. //
  108. // Select the foreground color based on whether or not the check box is
  109. // selected.
  110. //
  111. if(pCheck->usStyle & CB_STYLE_SELECTED)
  112. {
  113. GrContextForegroundSet(&sCtx, pCheck->ulOutlineColor);
  114. }
  115. else
  116. {
  117. GrContextForegroundSet(&sCtx, pCheck->ulFillColor);
  118. }
  119. //
  120. // Draw an "X" in the check box.
  121. //
  122. GrLineDraw(&sCtx, sRect.sXMin + 1, sRect.sYMin + 1, sRect.sXMax - 1,
  123. sRect.sYMax - 1);
  124. GrLineDraw(&sCtx, sRect.sXMin + 1, sRect.sYMax - 1, sRect.sXMax - 1,
  125. sRect.sYMin + 1);
  126. //
  127. // See if the check box text or image style is selected.
  128. //
  129. if((pCheck->usStyle & (CB_STYLE_TEXT | CB_STYLE_IMG)) && !bClick)
  130. {
  131. //
  132. // Shrink the clipping region by the size of the check box so that it
  133. // is not overwritten by further "decorative" portions of the widget.
  134. //
  135. sCtx.sClipRegion.sXMin += pCheck->usBoxSize + 4;
  136. //
  137. // If the check box outline style is selected then shrink the clipping
  138. // region by one pixel on each side so that the outline is not
  139. // overwritten by the text or image.
  140. //
  141. if(pCheck->usStyle & CB_STYLE_OUTLINE)
  142. {
  143. sCtx.sClipRegion.sYMin++;
  144. sCtx.sClipRegion.sXMax--;
  145. sCtx.sClipRegion.sYMax--;
  146. }
  147. //
  148. // See if the check box image style is selected.
  149. //
  150. if(pCheck->usStyle & CB_STYLE_IMG)
  151. {
  152. //
  153. // Determine where along the Y extent of the widget to draw the
  154. // image. It is drawn at the top if it takes all (or more than
  155. // all) of the Y extent of the widget, and it is drawn centered if
  156. // it takes less than the Y extent.
  157. //
  158. if(GrImageHeightGet(pCheck->pucImage) >
  159. (sCtx.sClipRegion.sYMax - sCtx.sClipRegion.sYMin))
  160. {
  161. lY = sCtx.sClipRegion.sYMin;
  162. }
  163. else
  164. {
  165. lY = (sCtx.sClipRegion.sYMin +
  166. ((sCtx.sClipRegion.sYMax - sCtx.sClipRegion.sYMin -
  167. GrImageHeightGet(pCheck->pucImage) + 1) / 2));
  168. }
  169. //
  170. // Set the foreground and background colors to use for 1 BPP
  171. // images.
  172. //
  173. GrContextForegroundSet(&sCtx, pCheck->ulTextColor);
  174. GrContextBackgroundSet(&sCtx, pCheck->ulFillColor);
  175. //
  176. // Draw the image next to the check box.
  177. //
  178. GrImageDraw(&sCtx, pCheck->pucImage, sCtx.sClipRegion.sXMin, lY);
  179. }
  180. //
  181. // See if the check box text style is selected.
  182. //
  183. if(pCheck->usStyle & CB_STYLE_TEXT)
  184. {
  185. //
  186. // Determine where along the Y extent of the widget to draw the
  187. // string. It is drawn at the top if it takes all (or more than
  188. // all) of the Y extent of the widget, and it is drawn centered if
  189. // it takes less than the Y extent.
  190. //
  191. if(GrFontHeightGet(pCheck->pFont) >
  192. (sCtx.sClipRegion.sYMax - sCtx.sClipRegion.sYMin))
  193. {
  194. lY = sCtx.sClipRegion.sYMin;
  195. }
  196. else
  197. {
  198. lY = (sCtx.sClipRegion.sYMin +
  199. ((sCtx.sClipRegion.sYMax - sCtx.sClipRegion.sYMin -
  200. GrFontHeightGet(pCheck->pFont) + 1) / 2));
  201. }
  202. //
  203. // Draw the text next to the check box.
  204. //
  205. GrContextFontSet(&sCtx, pCheck->pFont);
  206. GrContextForegroundSet(&sCtx, pCheck->ulTextColor);
  207. GrContextBackgroundSet(&sCtx, pCheck->ulFillColor);
  208. GrStringDraw(&sCtx, pCheck->pcText, -1, sCtx.sClipRegion.sXMin,
  209. lY, pCheck->usStyle & CB_STYLE_TEXT_OPAQUE);
  210. }
  211. }
  212. }
  213. //*****************************************************************************
  214. //
  215. //! Handles pointer events for a check box.
  216. //!
  217. //! \param pWidget is a pointer to the check box widget.
  218. //! \param ulMsg is the pointer event message.
  219. //! \param lX is the X coordinate of the pointer event.
  220. //! \param lY is the Y coordiante of the pointer event.
  221. //!
  222. //! This function processes pointer event messages for a check box. This is
  223. //! called in response to a \b #WIDGET_MSG_PTR_DOWN, \b #WIDGET_MSG_PTR_MOVE,
  224. //! and \b #WIDGET_MSG_PTR_UP messages.
  225. //!
  226. //! If the \b #WIDGET_MSG_PTR_UP message is received with a position within the
  227. //! extents of the check box, the check box's selected state will be toggled
  228. //! and its OnChange function is called.
  229. //!
  230. //! \return Returns 1 if the coordinates are within the extents of the check
  231. //! box and 0 otherwise.
  232. //
  233. //*****************************************************************************
  234. static int
  235. CheckBoxClick(tWidget *pWidget, unsigned int ulMsg, int lX, int lY)
  236. {
  237. tCheckBoxWidget *pCheck;
  238. //
  239. // Check the arguments.
  240. //
  241. ASSERT(pWidget);
  242. //
  243. // Convert the generic widget pointer into a check box widget pointer.
  244. //
  245. pCheck = (tCheckBoxWidget *)pWidget;
  246. //
  247. // See if the given coordinates are within the extents of the check box.
  248. //
  249. if((lX >= pWidget->sPosition.sXMin) &&
  250. (lX <= pWidget->sPosition.sXMax) &&
  251. (lY >= pWidget->sPosition.sYMin) &&
  252. (lY <= pWidget->sPosition.sYMax))
  253. {
  254. //
  255. // See if the pointer was just raised.
  256. //
  257. if(ulMsg == WIDGET_MSG_PTR_UP)
  258. {
  259. //
  260. // Toggle the selected state of this check box.
  261. //
  262. pCheck->usStyle ^= CB_STYLE_SELECTED;
  263. //
  264. // Redraw the check box based on the new selected state.
  265. //
  266. CheckBoxPaint(pWidget, 1);
  267. //
  268. // If there is an OnChange callback for this widget then call the
  269. // callback.
  270. //
  271. if(pCheck->pfnOnChange)
  272. {
  273. pCheck->pfnOnChange(pWidget,
  274. pCheck->usStyle & CB_STYLE_SELECTED);
  275. }
  276. }
  277. //
  278. // These coordinates are within the extents of the check box widget.
  279. //
  280. return(1);
  281. }
  282. //
  283. // These coordinates are not within the extents of the check box widget.
  284. //
  285. return(0);
  286. }
  287. //*****************************************************************************
  288. //
  289. //! Handles messages for a check box widget.
  290. //!
  291. //! \param pWidget is a pointer to the check box widget.
  292. //! \param ulMsg is the message.
  293. //! \param ulParam1 is the first parameter to the message.
  294. //! \param ulParam2 is the second parameter to the message.
  295. //!
  296. //! This function receives messages intended for this check box widget and
  297. //! processes them accordingly. The processing of the message varies based on
  298. //! the message in question.
  299. //!
  300. //! Unrecognized messages are handled by calling WidgetDefaultMsgProc().
  301. //!
  302. //! \return Returns a value appropriate to the supplied message.
  303. //
  304. //*****************************************************************************
  305. int
  306. CheckBoxMsgProc(tWidget *pWidget, unsigned int ulMsg, unsigned int ulParam1,
  307. unsigned int ulParam2)
  308. {
  309. //
  310. // Check the arguments.
  311. //
  312. ASSERT(pWidget);
  313. //
  314. // Determine which message is being sent.
  315. //
  316. switch(ulMsg)
  317. {
  318. //
  319. // The widget paint request has been sent.
  320. //
  321. case WIDGET_MSG_PAINT:
  322. {
  323. //
  324. // Handle the widget paint request.
  325. //
  326. CheckBoxPaint(pWidget, 0);
  327. //
  328. // Return one to indicate that the message was successfully
  329. // processed.
  330. //
  331. return(1);
  332. }
  333. //
  334. // One of the pointer requests has been sent.
  335. //
  336. case WIDGET_MSG_PTR_DOWN:
  337. case WIDGET_MSG_PTR_MOVE:
  338. case WIDGET_MSG_PTR_UP:
  339. {
  340. //
  341. // Handle the pointer request, returning the appropriate value.
  342. //
  343. return(CheckBoxClick(pWidget, ulMsg, ulParam1, ulParam2));
  344. }
  345. //
  346. // An unknown request has been sent.
  347. //
  348. default:
  349. {
  350. //
  351. // Let the default message handler process this message.
  352. //
  353. return(WidgetDefaultMsgProc(pWidget, ulMsg, ulParam1, ulParam2));
  354. }
  355. }
  356. }
  357. //*****************************************************************************
  358. //
  359. //! Initializes a check box widget.
  360. //!
  361. //! \param pWidget is a pointer to the check box widget to initialize.
  362. //! \param pDisplay is a pointer to the display on which to draw the check box.
  363. //! \param lX is the X coordinate of the upper left corner of the check box.
  364. //! \param lY is the Y coordinate of the upper left corner of the check box.
  365. //! \param lWidth is the width of the check box.
  366. //! \param lHeight is the height of the check box.
  367. //!
  368. //! This function initializes the provided check box widget.
  369. //!
  370. //! \return None.
  371. //
  372. //*****************************************************************************
  373. void
  374. CheckBoxInit(tCheckBoxWidget *pWidget, const tDisplay *pDisplay, int lX,
  375. int lY, int lWidth, int lHeight)
  376. {
  377. unsigned int ulIdx;
  378. //
  379. // Check the arguments.
  380. //
  381. ASSERT(pWidget);
  382. ASSERT(pDisplay);
  383. //
  384. // Clear out the widget structure.
  385. //
  386. for(ulIdx = 0; ulIdx < sizeof(tCheckBoxWidget); ulIdx += 4)
  387. {
  388. ((unsigned int *)pWidget)[ulIdx / 4] = 0;
  389. }
  390. //
  391. // Set the size of the check box widget structure.
  392. //
  393. pWidget->sBase.lSize = sizeof(tCheckBoxWidget);
  394. //
  395. // Mark this widget as fully disconnected.
  396. //
  397. pWidget->sBase.pParent = 0;
  398. pWidget->sBase.pNext = 0;
  399. pWidget->sBase.pChild = 0;
  400. //
  401. // Save the display pointer.
  402. //
  403. pWidget->sBase.pDisplay = pDisplay;
  404. //
  405. // Set the extents of this check box.
  406. //
  407. pWidget->sBase.sPosition.sXMin = lX;
  408. pWidget->sBase.sPosition.sYMin = lY;
  409. pWidget->sBase.sPosition.sXMax = lX + lWidth - 1;
  410. pWidget->sBase.sPosition.sYMax = lY + lHeight - 1;
  411. //
  412. // Use the check box message handler to processage messages to this check
  413. // box.
  414. //
  415. pWidget->sBase.pfnMsgProc = CheckBoxMsgProc;
  416. }
  417. //*****************************************************************************
  418. //
  419. // Close the Doxygen group.
  420. //! @}
  421. //
  422. //*****************************************************************************