radiobutton.c 16 KB

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