Custom Widget with child widgets

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

  • Custom Widget with child widgets

    I am trying to create a custom widget for use within a dialog that has child widgets. The child widgets do not seem to be working, I am rather new to using emwin - am I doing something completely wrong?

    Widget C File:

    C Source Code

    1. #include <stdlib.h>
    2. #include <string.h>
    3. #include "WIDGET.h"
    4. #include "dialog.h"
    5. #include "utility.h"
    6. #define ID_BUTTON_1 (GUI_ID_USER + 0x200)
    7. typedef WM_HMEM MYWIDGET_Handle;
    8. /*********************************************************************
    9. *
    10. * MYWIDGET_Obj
    11. */
    12. typedef struct {
    13. BUTTON_Handle hButton1;
    14. int NumExtraBytes;
    15. } MYWIDGET_Obj;
    16. const MYWIDGET_Obj MYWIDGET_Default = {
    17. 0,
    18. 0
    19. };
    20. static void MyWidget_Callback(WM_MESSAGE * pMsg)
    21. {
    22. switch (pMsg->MsgId) {
    23. default:
    24. WINDOW_Callback(pMsg);
    25. }
    26. }
    27. static void MyWidgetButton_Callback(WM_MESSAGE * pMsg)
    28. {
    29. switch (pMsg->MsgId) {
    30. default:
    31. BUTTON_Callback(pMsg);
    32. }
    33. }
    34. MYWIDGET_Handle MyWidget_CreateIndirect(const GUI_WIDGET_CREATE_INFO *pCreateInfo,
    35. WM_HWIN hParent, int x0, int y0, WM_CALLBACK *cb)
    36. {
    37. WM_HWIN hWin;
    38. MYWIDGET_Obj MyWidget;
    39. GUI_WIDGET_CREATE_INFO CreateInfo = *pCreateInfo;
    40. GUI_WIDGET_CREATE_INFO ButtonCreateInfo = {0};
    41. MyWidget = MYWIDGET_Default;
    42. MyWidget.NumExtraBytes = pCreateInfo->NumExtraBytes;
    43. CreateInfo.NumExtraBytes = pCreateInfo->NumExtraBytes + sizeof(MYWIDGET_Obj);
    44. hWin = WINDOW_CreateIndirect(&CreateInfo, hParent, 0, 0, 0);
    45. WINDOW_SetBkColor(hWin, 0x00d2b996);
    46. ButtonCreateInfo.pName = "Button 1";
    47. ButtonCreateInfo.Id = ID_BUTTON_1;
    48. ButtonCreateInfo.x0 = 0;
    49. ButtonCreateInfo.y0 = 0;
    50. ButtonCreateInfo.xSize = 30;
    51. ButtonCreateInfo.ySize = 30;
    52. MyWidget.hButton1 = BUTTON_CreateIndirect(&ButtonCreateInfo, hWin, 0, 0, 0);
    53. WM_SetCallback(hWin, MyWidget_Callback);
    54. WM_SetCallback(MyWidget.hButton1, MyWidgetButton_Callback);
    55. WM_SetUserData(hWin, &MyWidget, sizeof(MYWIDGET_Obj));
    56. return hWin;
    57. }
    Display All


    Dialog that uses custom widget:

    C Source Code

    1. #include "DIALOG.h"
    2. typedef WM_HMEM MYWIDGET_Handle;
    3. MYWIDGET_Handle MyWidget_CreateIndirect(const GUI_WIDGET_CREATE_INFO *pCreateInfo,
    4. WM_HWIN hParent, int x0, int y0, WM_CALLBACK *cb);
    5. /*********************************************************************
    6. *
    7. * Defines
    8. *
    9. **********************************************************************
    10. */
    11. #define ID_WINDOW_0 (GUI_ID_USER + 0x00)
    12. #define ID_MY_WIDGET_0 (GUI_ID_USER + 0x30)
    13. /*********************************************************************
    14. *
    15. * Static data
    16. *
    17. **********************************************************************
    18. */
    19. /*********************************************************************
    20. *
    21. * _aDialogCreate
    22. */
    23. static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
    24. { WINDOW_CreateIndirect, "Widget Test", ID_WINDOW_0, 0, 0, 320, 240, 0, 0x0, 0 },
    25. { MyWidget_CreateIndirect, "", ID_MY_WIDGET_0, 100, 100, 130, 37, 0, 0, 0 },
    26. };
    27. /*********************************************************************
    28. *
    29. * _cbDialog
    30. */
    31. static void _cbDialog(WM_MESSAGE * pMsg) {
    32. switch (pMsg->MsgId) {
    33. default:
    34. WM_DefaultProc(pMsg);
    35. break;
    36. }
    37. }
    38. /*********************************************************************
    39. *
    40. * Public code
    41. *
    42. **********************************************************************
    43. */
    44. /*********************************************************************
    45. *
    46. * CreateTestDLG
    47. */
    48. WM_HWIN CreateTestDLG(void);
    49. WM_HWIN CreateTestDLG(void) {
    50. WM_HWIN hWin;
    51. hWin = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbDialog, WM_HBKWIN, 0, 0);
    52. return hWin;
    53. }
    54. /*************************** End of file ****************************/
    Display All


    Thanks.
  • No I do not intend to have a widget that does nothing but have a button as I child :) I have simplified the widget for the sake of illustrating the problem. Is there a reason a button cannot be a child of a window as in the code example?

    Part of my confusion lies with <WIDGET>_CreateIndirect() - this is apparently needed for a widget in a dialog, but is not documented in AN03002. Is there a problem with the implementation of the <WIDGET>_CreateIndirect() function in the code example?


    Thanks again.
  • Hello,

    Of course a BUTTON widget can be a child of a custom widget. I see that the way to implement a CreateIndirect() function for custom widgets is currently not explained. We will add an explanation for that in a future emWin version. For now I would recommend creating the custom widget in the dialog procedure as a reaction to the message WM_INIT_DIALOG.

    Best regards,
    Adrian