implement a self-dimensioning TEXT widget

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

    • implement a self-dimensioning TEXT widget

      I need a self-dimensioning TEXT widget.
      I tried in this way:

      C Source Code

      1. static void cbResizableText(WM_MESSAGE * pMsg)
      2. {
      3. WM_HWIN hWin;
      4. hWin = pMsg->hWin;
      5. switch (pMsg->MsgId)
      6. {
      7. case WM_PRE_PAINT:
      8. {
      9. const GUI_FONT * pFont;
      10. char Str[20];
      11. TEXT_GetText(hWin, Str, 20);
      12. pFont = GUI_SetFont(TEXT_GetFont(pMsg->hWin));
      13. int x = GUI_GetStringDistX(Str) + 3;
      14. WM_SetXSize(hWin, x);
      15. GUI_SetFont(pFont);
      16. }
      17. break;
      18. }
      19. TEXT_Callback(pMsg);
      20. }
      21. static TEXT_Handle RESIZABLETEXTWIDGET_Create(int x0, int y0, int xSize, int ySize, WM_HWIN hParent, int WinFlags)
      22. {
      23. TEXT_Handle hWin;
      24. hWin = TEXT_CreateEx(x0, y0, xSize, ySize,
      25. hParent, WinFlags | WM_CF_MEMDEV, TEXT_CF_VCENTER | TEXT_CF_HCENTER, GUI_ID_TEXT0, "" );
      26. WM_SetCallback(hWin, cbResizableText);
      27. return hWin;
      28. }
      Display All


      unfortunately this doesn't work: It seems that there is a clip rectangle that is as large as the size with which the widget was created the first time.

      where am I wrong?

      Is there a way to be notified of changing the text string?

      best regards
      Max
    • [UPDATE] implement a self-dimensioning TEXT widget

      I tried to simulate the behavior to find out what could be wrong.
      the source code is:

      C Source Code

      1. #include "GUI.h"
      2. #include "TEXT.h"
      3. //#define TEST
      4. #ifdef TEST
      5. static void cbResizableText(WM_MESSAGE * pMsg)
      6. {
      7. WM_HWIN hWin;
      8. hWin = pMsg->hWin;
      9. switch (pMsg->MsgId)
      10. {
      11. case WM_PRE_PAINT:
      12. {
      13. const GUI_FONT * pFont;
      14. char Str[20];
      15. TEXT_GetText(hWin, Str, 20);
      16. pFont = GUI_SetFont(TEXT_GetFont(pMsg->hWin));
      17. int x = GUI_GetStringDistX(Str) + 3;
      18. WM_SetXSize(hWin, x);
      19. GUI_SetFont(pFont);
      20. }
      21. break;
      22. }
      23. TEXT_Callback(pMsg);
      24. }
      25. static TEXT_Handle RESIZABLETEXTWIDGET_Create(int x0, int y0, int xSize, int ySize, WM_HWIN hParent, int WinFlags)
      26. {
      27. TEXT_Handle hWin;
      28. hWin = TEXT_CreateEx(x0, y0, xSize, ySize,
      29. hParent, WinFlags | WM_CF_MEMDEV, TEXT_CF_VCENTER | TEXT_CF_HCENTER, GUI_ID_TEXT0, "" );
      30. WM_SetCallback(hWin, cbResizableText);
      31. // TEXT_SetBkColor(hWin, GUI_WHITE);
      32. return hWin;
      33. }
      34. #endif // TEST
      35. static void cb(WM_MESSAGE * pMsg)
      36. {
      37. WM_HWIN hWin;
      38. hWin = pMsg->hWin;
      39. switch (pMsg->MsgId)
      40. {
      41. case WM_PAINT:
      42. {
      43. GUI_RECT Rect;
      44. // WM_GetInsideRectEx(hWin, &Rect);
      45. WM_GetClientRect(&Rect);
      46. GUI_SetBkColor(GUI_DARKGREEN);
      47. GUI_ClearRectEx(&Rect);
      48. GUI_SetColor(GUI_LIGHTRED);
      49. GUI_DrawRectEx(&Rect);
      50. }
      51. break;
      52. default:
      53. WM_DefaultProc(pMsg);
      54. }
      55. }
      56. void MainTask(void)
      57. {
      58. GUI_Init();
      59. GUI_EnableAlpha(1);
      60. GUI_SetBkColor(GUI_DARKGRAY);
      61. GUI_Clear();
      62. WM_HWIN hParent, hWin;
      63. hParent = WM_CreateWindowAsChild(10, 10, 60, 25, WM_HBKWIN,
      64. WM_CF_SHOW, cb, 0);
      65. #ifdef TEST
      66. hWin = RESIZABLETEXTWIDGET_Create(4,4, 1, 12, hParent, WM_CF_SHOW);
      67. #else
      68. hWin = TEXT_CreateEx(4,4, 1, 12, hParent, WM_CF_SHOW, TEXT_CF_VCENTER | TEXT_CF_HCENTER, GUI_ID_TEXT0, "" );
      69. #endif
      70. TEXT_SetBkColor(hWin, GUI_DARKBLUE);
      71. TEXT_SetTextColor(hWin, GUI_DARKYELLOW);
      72. while(1)
      73. {
      74. GUI_Delay(2000);
      75. TEXT_SetText(hWin, "A");
      76. #ifndef TEST
      77. const GUI_FONT * pFont;
      78. pFont = GUI_SetFont(TEXT_GetFont(hWin));
      79. int x = GUI_GetStringDistX("A") + 3;
      80. WM_SetXSize(hWin, x);
      81. GUI_SetFont(pFont);
      82. #endif // TEST
      83. GUI_Delay(2000);
      84. TEXT_SetText(hWin, "WWW");
      85. #ifndef TEST
      86. pFont = GUI_SetFont(TEXT_GetFont(hWin));
      87. x = GUI_GetStringDistX("WWW") + 3;
      88. WM_SetXSize(hWin, x);
      89. GUI_SetFont(pFont);
      90. #endif // TEST
      91. }
      92. }
      Display All


      when TEST is defined the 'A' is shown as follows:

      [img]https://s18.postimg.org/405uwep2x/auto_Resize.png[/img]

      only 3 pixel are shown, but if I break into cbResizableText() I can see that x size should be 10 pixel (and 10 is passed as second parameter of WM_SetXSize())

      If TEST is not defined the WM_SetXSize() function is not called from inside the cbResizableText() callback. This way everything works:
      [img]https://s18.postimg.org/v0jnknda1/auto_Resize1.png[/img]


      It seems that the WM_SetXSize() function has problems when invoked inside the callback.

      Are there other working and reliable ways to change the size inside the callback?

      best regards
      Max