Custom Callback with Button

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Custom Callback with Button

      Hi,

      The goal is to create a button with Multi strings where the strings have different fonts from each other, So as suggested by emwin documentation I have created a custom callback function but I observe that the custom callback is overwriting all the previous set properties. The codes are as follows

      Source Code

      1. hButton = WM_GetDialogItem(pMsg->hWin, ID_BUTTON1);
      2. WM_SetCallback(hButton , CallBack_Button);
      3. hButton = WM_GetDialogItem(pMsg->hWin, ID_BUTTON1);
      4. BUTTON_SetBkColor(hButton, BUTTON_CI_UNPRESSED, SKY_BLUE);
      5. BUTTON_SetBkColor(hButton, BUTTON_CI_PRESSED, HEADER_OFFWHITE);
      6. BUTTON_SetTextColor(hButton, BUTTON_CI_PRESSED, BASE_BLUE);
      7. BUTTON_SetTextColor(hButton, BUTTON_CI_UNPRESSED, BASE_BLUE);

      the callback function

      Source Code

      1. void CallBack_Button(WM_MESSAGE * pMsg) {
      2. GUI_RECT Rect;
      3. int Index;
      4. int TextAlign;
      5. switch (pMsg->MsgId) {
      6. case WM_PAINT:
      7. WM_GetClientRect(&Rect);
      8. if(BUTTON_IsPressed(pMsg->hWin)) {
      9. GUI_SetBkColor(HEADER_OFFWHITE);
      10. GUI_SetColor(BASE_BLUE);
      11. } else {
      12. GUI_SetBkColor(BASE_BLUE);
      13. GUI_SetColor(SKY_BLUE);
      14. }
      15. GUI_SetFont(&GUI_Fontsymbols_Regular_36);
      16. GUI_DispCharAt('&',Rect.x0 + 10,Rect.y0 + 10);
      17. GUI_SetFont(&GUI_Font_UI_Tab_Bold_22);
      18. GUI_DispStringAt("Stop",Rect.x0 + 40,Rect.y0 + 10);
      19. break;
      20. default:
      21. BUTTON_Callback(pMsg);
      22. break;
      23. }
      24. }
      Display All
      the result would be that
      the entire OFF_WHITE background is replaced by white color except for the two strings. Is there any method where the button_<routines> can be used along with custom modifications.

      Thanks

      The post was edited 2 times, last by Suresh ().

    • Hi,

      With the default skin of the BUTTON widget the functions BUTTON_SetBkColor() and BUTTON_SetTextColor() are not working. Either set the classic skin or use SKINFLEX properties to change properties like colors.

      If you overwrite the callback function of a widget you can call the default callback function (e.g BUTTON_Callback()) and then draw something.

      Here is a brief example how this could be done:

      C Source Code

      1. #include "DIALOG.h"
      2. /*********************************************************************
      3. *
      4. * Static code
      5. *
      6. **********************************************************************
      7. */
      8. /*********************************************************************
      9. *
      10. * _cbButton
      11. */
      12. static void _cbButton(WM_MESSAGE * pMsg) {
      13. GUI_RECT Rect;
      14. switch(pMsg->MsgId) {
      15. case WM_PAINT:
      16. BUTTON_Callback(pMsg);
      17. WM_GetClientRect(&Rect);
      18. if(BUTTON_IsPressed(pMsg->hWin)) {
      19. GUI_SetBkColor(GUI_WHITE);
      20. GUI_SetColor(GUI_BLUE);
      21. } else {
      22. GUI_SetBkColor(GUI_BLUE);
      23. GUI_SetColor(GUI_LIGHTBLUE);
      24. }
      25. GUI_SetFont(&GUI_Font13_1);
      26. GUI_DispCharAt('&',Rect.x0 + 10,Rect.y0 + 10);
      27. GUI_SetFont(&GUI_Font13B_1);
      28. GUI_DispStringAt("Stop",Rect.x0 + 40,Rect.y0 + 10);
      29. break;
      30. default:
      31. BUTTON_Callback(pMsg);
      32. }
      33. }
      34. /*********************************************************************
      35. *
      36. * _cbBk
      37. */
      38. static void _cbBk(WM_MESSAGE * pMsg) {
      39. switch (pMsg->MsgId) {
      40. case WM_PAINT:
      41. GUI_SetBkColor(GUI_BLACK);
      42. GUI_Clear();
      43. break;
      44. default:
      45. WM_DefaultProc(pMsg);
      46. break;
      47. }
      48. }
      49. /*********************************************************************
      50. *
      51. * Public code
      52. *
      53. **********************************************************************
      54. */
      55. /*********************************************************************
      56. *
      57. * MainTask
      58. */
      59. void MainTask(void) {
      60. BUTTON_Handle hButton;
      61. GUI_Init();
      62. WM_MULTIBUF_Enable(1);
      63. WM_SetCallback(WM_HBKWIN, _cbBk);
      64. hButton = BUTTON_CreateEx(10, 10, 100, 30, WM_HBKWIN, WM_CF_SHOW | WM_CF_HASTRANS, 0, GUI_ID_BUTTON0);
      65. WM_SetCallback(hButton, _cbButton);
      66. BUTTON_SetSkinClassic(hButton);
      67. BUTTON_SetBkColor(hButton, BUTTON_CI_UNPRESSED, GUI_LIGHTBLUE);
      68. BUTTON_SetBkColor(hButton, BUTTON_CI_PRESSED, GUI_WHITE);
      69. BUTTON_SetTextColor(hButton, BUTTON_CI_PRESSED, GUI_BLUE);
      70. BUTTON_SetTextColor(hButton, BUTTON_CI_UNPRESSED, GUI_BLUE);
      71. while (1) {
      72. GUI_Delay(100);
      73. }
      74. }
      75. /*************************** End of file ****************************/
      Display All

      Best regards,
      Sven
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.