imgbutton.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. //*****************************************************************************
  2. //
  3. // imgbutton.c - An image-based button widget.
  4. //
  5. // Copyright (c) 2009-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 "imgbutton.h"
  28. //*****************************************************************************
  29. //
  30. //! \addtogroup imgbutton_api
  31. //! @{
  32. //
  33. //*****************************************************************************
  34. //*****************************************************************************
  35. //
  36. //! Draws an image button.
  37. //!
  38. //! \param pWidget is a pointer to the image button widget to be drawn.
  39. //!
  40. //! This function draws a rectangular image button 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. ImageButtonPaint(tWidget *pWidget)
  48. {
  49. const unsigned char *pucImage;
  50. tImageButtonWidget *pPush;
  51. tContext sCtx;
  52. int lX, lY;
  53. //
  54. // Check the arguments.
  55. //
  56. ASSERT(pWidget);
  57. //
  58. // Convert the generic widget pointer into a image button widget pointer.
  59. //
  60. pPush = (tImageButtonWidget *)pWidget;
  61. //
  62. // Initialize a drawing context.
  63. //
  64. GrContextInit(&sCtx, pWidget->pDisplay);
  65. //
  66. // Initialize the clipping region based on the extents of this rectangular
  67. // image button.
  68. //
  69. GrContextClipRegionSet(&sCtx, &(pWidget->sPosition));
  70. //
  71. // Compute the center of the image button.
  72. //
  73. lX = (pWidget->sPosition.sXMin +
  74. ((pWidget->sPosition.sXMax - pWidget->sPosition.sXMin + 1) / 2));
  75. lY = (pWidget->sPosition.sYMin +
  76. ((pWidget->sPosition.sYMax - pWidget->sPosition.sYMin + 1) / 2));
  77. //
  78. // Do we need to fill the widget background with a color?
  79. //
  80. if(pPush->ulStyle & IB_STYLE_FILL)
  81. {
  82. //
  83. // Yes. Set the appropriate color depending upon whether or not
  84. // the widget is currently pressed.
  85. //
  86. GrContextForegroundSet(&sCtx,
  87. ((pPush->ulStyle & IB_STYLE_PRESSED) ?
  88. pPush->ulPressedColor :
  89. pPush->ulBackgroundColor));
  90. GrRectFill(&sCtx, &(pWidget->sPosition));
  91. }
  92. //
  93. // Set the foreground and background colors to use for 1 BPP
  94. // images and text
  95. //
  96. GrContextForegroundSet(&sCtx, pPush->ulForegroundColor);
  97. GrContextBackgroundSet(&sCtx,
  98. ((pPush->ulStyle & IB_STYLE_PRESSED) ?
  99. pPush->ulPressedColor :
  100. pPush->ulBackgroundColor));
  101. //
  102. // Do we need to draw the background image?
  103. //
  104. if(!(pPush->ulStyle & IB_STYLE_IMAGE_OFF))
  105. {
  106. //
  107. // Get the background image to be drawn.
  108. //
  109. pucImage = ((pPush->ulStyle & IB_STYLE_PRESSED) ?
  110. pPush->pucPressImage : pPush->pucImage);
  111. //
  112. // Draw the image centered in the image button.
  113. //
  114. GrImageDraw(&sCtx, pucImage, lX - (GrImageWidthGet(pucImage) / 2),
  115. lY - (GrImageHeightGet(pucImage) / 2));
  116. }
  117. //
  118. // Adjust the drawing position if the button is pressed.
  119. //
  120. lX += ((pPush->ulStyle & IB_STYLE_PRESSED) ? pPush->sXOffset : 0);
  121. lY += ((pPush->ulStyle & IB_STYLE_PRESSED) ? pPush->sYOffset : 0);
  122. //
  123. // If there is a keycap image and it is not disabled, center this on the
  124. // top of the button, applying any offset defined if the button is
  125. // currently pressed.
  126. //
  127. if(pPush->pucKeycapImage && !(pPush->ulStyle & IB_STYLE_KEYCAP_OFF))
  128. {
  129. //
  130. // Draw the keycap image.
  131. //
  132. GrImageDraw(&sCtx, pPush->pucKeycapImage,
  133. lX - (GrImageWidthGet(pPush->pucKeycapImage) / 2),
  134. lY - (GrImageHeightGet(pPush->pucKeycapImage) / 2));
  135. }
  136. //
  137. // See if the button text style is selected.
  138. //
  139. if(pPush->ulStyle & IB_STYLE_TEXT)
  140. {
  141. //
  142. // Draw the text centered in the middle of the button with offset
  143. // applied if the button is currently pressed.
  144. //
  145. GrContextFontSet(&sCtx, pPush->pFont);
  146. GrStringDrawCentered(&sCtx, pPush->pcText, -1, lX, lY, 0);
  147. }
  148. }
  149. //*****************************************************************************
  150. //
  151. //! Handles pointer events for a rectangular image button.
  152. //!
  153. //! \param pWidget is a pointer to the image button widget.
  154. //! \param ulMsg is the pointer event message.
  155. //! \param lX is the X coordinate of the pointer event.
  156. //! \param lY is the Y coordinate of the pointer event.
  157. //!
  158. //! This function processes pointer event messages for a rectangular push
  159. //! button. This is called in response to a \b #WIDGET_MSG_PTR_DOWN,
  160. //! \b #WIDGET_MSG_PTR_MOVE, and \b #WIDGET_MSG_PTR_UP messages.
  161. //!
  162. //! If the \b #WIDGET_MSG_PTR_UP message is received with a position within the
  163. //! extents of the image button, the image button's OnClick callback function is
  164. //! called.
  165. //!
  166. //! \return Returns 1 if the coordinates are within the extents of the push
  167. //! button and 0 otherwise.
  168. //
  169. //*****************************************************************************
  170. static int
  171. ImageButtonClick(tWidget *pWidget, unsigned int ulMsg, int lX, int lY)
  172. {
  173. tImageButtonWidget *pPush;
  174. //
  175. // Check the arguments.
  176. //
  177. ASSERT(pWidget);
  178. //
  179. // Convert the generic widget pointer into a image button widget pointer.
  180. //
  181. pPush = (tImageButtonWidget *)pWidget;
  182. //
  183. // See if this is a pointer up message.
  184. //
  185. if(ulMsg == WIDGET_MSG_PTR_UP)
  186. {
  187. //
  188. // Indicate that this image button is no longer pressed.
  189. //
  190. pPush->ulStyle &= ~(IB_STYLE_PRESSED);
  191. //
  192. // Redraw the button in the released state.
  193. //
  194. ImageButtonPaint(pWidget);
  195. //
  196. // If the pointer is still within the button bounds, and it is a
  197. // release notify button, call the notification function here.
  198. //
  199. if(GrRectContainsPoint(&pWidget->sPosition, lX, lY) &&
  200. (pPush->ulStyle & IB_STYLE_RELEASE_NOTIFY) && pPush->pfnOnClick)
  201. {
  202. pPush->pfnOnClick(pWidget);
  203. }
  204. }
  205. //
  206. // See if the given coordinates are within the extents of the image button.
  207. //
  208. if(GrRectContainsPoint(&pWidget->sPosition, lX, lY))
  209. {
  210. //
  211. // See if this is a pointer down message.
  212. //
  213. if(ulMsg == WIDGET_MSG_PTR_DOWN)
  214. {
  215. //
  216. // Indicate that this image button is pressed.
  217. //
  218. pPush->ulStyle |= IB_STYLE_PRESSED;
  219. //
  220. // Draw the button in the pressed state.
  221. //
  222. ImageButtonPaint(pWidget);
  223. }
  224. //
  225. // See if there is an OnClick callback for this widget.
  226. //
  227. if(pPush->pfnOnClick)
  228. {
  229. //
  230. // If the pointer was just pressed then call the callback.
  231. //
  232. if((ulMsg == WIDGET_MSG_PTR_DOWN) &&
  233. !(pPush->ulStyle & IB_STYLE_RELEASE_NOTIFY))
  234. {
  235. pPush->pfnOnClick(pWidget);
  236. }
  237. //
  238. // See if auto-repeat is enabled for this widget.
  239. //
  240. if(pPush->ulStyle & IB_STYLE_AUTO_REPEAT)
  241. {
  242. //
  243. // If the pointer was just pressed, reset the auto-repeat
  244. // count.
  245. //
  246. if(ulMsg == WIDGET_MSG_PTR_DOWN)
  247. {
  248. pPush->ulAutoRepeatCount = 0;
  249. }
  250. //
  251. // See if the pointer was moved.
  252. //
  253. else if(ulMsg == WIDGET_MSG_PTR_MOVE)
  254. {
  255. //
  256. // Increment the auto-repeat count.
  257. //
  258. pPush->ulAutoRepeatCount++;
  259. //
  260. // If the auto-repeat count exceeds the auto-repeat delay,
  261. // and it is a multiple of the auto-repeat rate, then
  262. // call the callback.
  263. //
  264. if((pPush->ulAutoRepeatCount >=
  265. pPush->usAutoRepeatDelay) &&
  266. (((pPush->ulAutoRepeatCount -
  267. pPush->usAutoRepeatDelay) %
  268. pPush->usAutoRepeatRate) == 0))
  269. {
  270. pPush->pfnOnClick(pWidget);
  271. }
  272. }
  273. }
  274. }
  275. //
  276. // These coordinates are within the extents of the image button widget.
  277. //
  278. return(1);
  279. }
  280. //
  281. // These coordinates are not within the extents of the image button widget.
  282. //
  283. return(0);
  284. }
  285. //*****************************************************************************
  286. //
  287. //! Handles messages for an image button widget.
  288. //!
  289. //! \param pWidget is a pointer to the image button widget.
  290. //! \param ulMsg is the message.
  291. //! \param ulParam1 is the first parameter to the message.
  292. //! \param ulParam2 is the second parameter to the message.
  293. //!
  294. //! This function receives messages intended for this image button widget and
  295. //! processes them accordingly. The processing of the message varies based on
  296. //! the message in question.
  297. //!
  298. //! Unrecognized messages are handled by calling WidgetDefaultMsgProc().
  299. //!
  300. //! \return Returns a value appropriate to the supplied message.
  301. //
  302. //*****************************************************************************
  303. int
  304. ImageButtonMsgProc(tWidget *pWidget, unsigned int ulMsg,
  305. unsigned int ulParam1, unsigned int ulParam2)
  306. {
  307. //
  308. // Check the arguments.
  309. //
  310. ASSERT(pWidget);
  311. //
  312. // Determine which message is being sent.
  313. //
  314. switch(ulMsg)
  315. {
  316. //
  317. // The widget paint request has been sent.
  318. //
  319. case WIDGET_MSG_PAINT:
  320. {
  321. //
  322. // Handle the widget paint request.
  323. //
  324. ImageButtonPaint(pWidget);
  325. //
  326. // Return one to indicate that the message was successfully
  327. // processed.
  328. //
  329. return(1);
  330. }
  331. //
  332. // One of the pointer requests has been sent.
  333. //
  334. case WIDGET_MSG_PTR_DOWN:
  335. case WIDGET_MSG_PTR_MOVE:
  336. case WIDGET_MSG_PTR_UP:
  337. {
  338. //
  339. // Handle the pointer request, returning the appropriate value.
  340. //
  341. return(ImageButtonClick(pWidget, ulMsg, ulParam1, ulParam2));
  342. }
  343. //
  344. // An unknown request has been sent.
  345. //
  346. default:
  347. {
  348. //
  349. // Let the default message handler process this message.
  350. //
  351. return(WidgetDefaultMsgProc(pWidget, ulMsg, ulParam1, ulParam2));
  352. }
  353. }
  354. }
  355. //*****************************************************************************
  356. //
  357. //! Initializes an image button widget.
  358. //!
  359. //! \param pWidget is a pointer to the image button widget to initialize.
  360. //! \param pDisplay is a pointer to the display on which to draw the push
  361. //! button.
  362. //! \param lX is the X coordinate of the upper left corner of the image button.
  363. //! \param lY is the Y coordinate of the upper left corner of the image button.
  364. //! \param lWidth is the width of the image button.
  365. //! \param lHeight is the height of the image button.
  366. //!
  367. //! This function initializes the provided image button widget.
  368. //!
  369. //! \return None.
  370. //
  371. //*****************************************************************************
  372. void
  373. ImageButtonInit(tImageButtonWidget *pWidget, const tDisplay *pDisplay,
  374. int lX, int lY, int lWidth, int lHeight)
  375. {
  376. unsigned int ulIdx;
  377. //
  378. // Check the arguments.
  379. //
  380. ASSERT(pWidget);
  381. ASSERT(pDisplay);
  382. //
  383. // Clear out the widget structure.
  384. //
  385. for(ulIdx = 0; ulIdx < sizeof(tImageButtonWidget); ulIdx += 4)
  386. {
  387. ((unsigned int *)pWidget)[ulIdx / 4] = 0;
  388. }
  389. //
  390. // Set the size of the image button widget structure.
  391. //
  392. pWidget->sBase.lSize = sizeof(tImageButtonWidget);
  393. //
  394. // Mark this widget as fully disconnected.
  395. //
  396. pWidget->sBase.pParent = 0;
  397. pWidget->sBase.pNext = 0;
  398. pWidget->sBase.pChild = 0;
  399. //
  400. // Save the display pointer.
  401. //
  402. pWidget->sBase.pDisplay = pDisplay;
  403. //
  404. // Set the extents of this rectangular image button.
  405. //
  406. pWidget->sBase.sPosition.sXMin = lX;
  407. pWidget->sBase.sPosition.sYMin = lY;
  408. pWidget->sBase.sPosition.sXMax = lX + lWidth - 1;
  409. pWidget->sBase.sPosition.sYMax = lY + lHeight - 1;
  410. //
  411. // Use the rectangular image button message handler to process messages to
  412. // this image button.
  413. //
  414. pWidget->sBase.pfnMsgProc = ImageButtonMsgProc;
  415. }
  416. //*****************************************************************************
  417. //
  418. // Close the Doxygen group.
  419. //! @}
  420. //
  421. //*****************************************************************************