How to know width in pixel of a TEXT widget from inside the callback?

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

    • How to know width in pixel of a TEXT widget from inside the callback?

      I need to know the width in pixel of a TEXT widget text from inside the (overridden) callback.
      I tried to use GUI_GetStringDistX () but it seems that the font it uses to calculate the width is different from the one user by TEXT widget to render the text.

      I have a small slice of code:

      C Source Code

      1. #include "GUI.h"
      2. #include "TEXT.h"
      3. #include <stdio.h>
      4. static int extent = 0;
      5. void _cbCallback(WM_MESSAGE * pMsg)
      6. {
      7. switch (pMsg->MsgId)
      8. {
      9. case WM_PAINT:
      10. {
      11. char str[20];
      12. TEXT_GetText(pMsg->hWin, str, 20);
      13. extent = GUI_GetStringDistX(str);
      14. TEXT_Callback(pMsg);
      15. }
      16. break;
      17. default:
      18. WM_DefaultProc(pMsg);
      19. break;
      20. }
      21. }
      22. void MainTask(void)
      23. {
      24. GUI_Init();
      25. GUI_SetBkColor(GUI_WHITE);
      26. GUI_Clear();
      27. GUI_SetColor(GUI_BLACK);
      28. GUI_DispStringAt("DONE", 10, 10);
      29. WM_HWIN hWin;
      30. hWin = TEXT_CreateEx(10, 17, 50, 20, WM_HBKWIN, WM_CF_SHOW, TEXT_CF_LEFT|TEXT_CF_TOP, GUI_ID_TEXT0, "");
      31. WM_SetCallback(hWin, _cbCallback);
      32. TEXT_SetText(hWin, "DONE");
      33. GUI_Delay(100);
      34. char str[3];
      35. sprintf(str, "%d", extent);
      36. GUI_DispStringAt(str, 10, 30);
      37. while(1)
      38. {
      39. GUI_Delay(100);
      40. }
      41. }
      Display All


      the result is:
      [img]https://s17.postimg.org/6fhzza3v3/string_Dim.png[/img]

      As you can see 24 is the whidth of the text rendered by GUI_DispStringAt () and not by TEXT widget.

      best regards
      Max
    • Hi,

      While calling GUI_GetStringDistX() within WM_PAINT the default font is still set and you will receive the string length in pixel in combination with the default font.

      To get the string length of from the TEXT widget you have to set the font used by the text widget before calling GUI_GetStringDistX().

      Try this:

      C Source Code

      1. #include "GUI.h"
      2. #include "TEXT.h"
      3. #include <stdio.h>
      4. static int extent = 0;
      5. void _cbCallback(WM_MESSAGE * pMsg) {
      6. char str[20];
      7. const GUI_FONT * pFont;
      8. switch (pMsg->MsgId) {
      9. case WM_PAINT:
      10. TEXT_GetText(pMsg->hWin, str, 20);
      11. pFont = GUI_SetFont(TEXT_GetFont(pMsg->hWin));
      12. extent = GUI_GetStringDistX(str);
      13. TEXT_Callback(pMsg);
      14. GUI_SetFont(pFont);
      15. break;
      16. default:
      17. WM_DefaultProc(pMsg);
      18. break;
      19. }
      20. }
      21. void MainTask(void) {
      22. WM_HWIN hWin;
      23. char str[3];
      24. GUI_Init();
      25. GUI_SetBkColor(GUI_WHITE);
      26. GUI_Clear();
      27. GUI_SetColor(GUI_BLACK);
      28. GUI_DispStringAt("DONE", 10, 10);
      29. hWin = TEXT_CreateEx(10, 17, 50, 20, WM_HBKWIN, WM_CF_SHOW, TEXT_CF_LEFT|TEXT_CF_TOP, GUI_ID_TEXT0, "");
      30. WM_SetCallback(hWin, _cbCallback);
      31. TEXT_SetText(hWin, "DONE");
      32. GUI_Delay(100);
      33. sprintf(str, "%d", extent);
      34. GUI_DispStringAt(str, 10, 30);
      35. while(1) {
      36. GUI_Delay(100);
      37. }
      38. }
      Display All


      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.