Wednesday, May 23rd 2018, 8:51am UTC+2

You are not logged in.

  • Login
  • Register

Dear visitor, welcome to SEGGER Forum. If this is your first visit here, please read the Help. It explains how this page works. You must be registered before you can use all the page's features. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

mastupristi

Beginner

Date of registration: Feb 18th 2016

Posts: 40

1

Wednesday, February 7th 2018, 10:56pm

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/C++ Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "GUI.h"
#include "TEXT.h"
#include <stdio.h>

static int extent = 0;

void _cbCallback(WM_MESSAGE * pMsg)
{
	switch (pMsg->MsgId)
	{
	case WM_PAINT:
    	{
        	char str[20];
        	TEXT_GetText(pMsg->hWin, str, 20);
        	extent = GUI_GetStringDistX(str);
        	TEXT_Callback(pMsg);
    	}
    	break;
	default:
    	WM_DefaultProc(pMsg);
    	break;
	}
}

void MainTask(void)
{
  GUI_Init();

  GUI_SetBkColor(GUI_WHITE);
  GUI_Clear();
  GUI_SetColor(GUI_BLACK);

  GUI_DispStringAt("DONE", 10, 10);

  WM_HWIN hWin;

  hWin = TEXT_CreateEx(10, 17, 50, 20, WM_HBKWIN, WM_CF_SHOW, TEXT_CF_LEFT|TEXT_CF_TOP, GUI_ID_TEXT0, "");

  WM_SetCallback(hWin, _cbCallback);

  TEXT_SetText(hWin, "DONE");
  GUI_Delay(100);
  char str[3];
  sprintf(str, "%d", extent);
  GUI_DispStringAt(str, 10, 30);
  while(1)
  {
   	GUI_Delay(100);
  }
}


the result is:


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

best regards
Max

SEGGER - Schoenen

Super Moderator

Date of registration: Aug 13th 2015

Posts: 584

2

Thursday, February 8th 2018, 12:02pm

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/C++ Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "GUI.h"
#include "TEXT.h"
#include <stdio.h>

static int extent = 0;

void _cbCallback(WM_MESSAGE * pMsg) {
  char str[20];
  const GUI_FONT * pFont;

  switch (pMsg->MsgId) {
  case WM_PAINT:
	TEXT_GetText(pMsg->hWin, str, 20);
	pFont = GUI_SetFont(TEXT_GetFont(pMsg->hWin));
	extent = GUI_GetStringDistX(str);
	TEXT_Callback(pMsg);
	GUI_SetFont(pFont);
	break;
  default:
	WM_DefaultProc(pMsg);
	break;
  }
}

void MainTask(void) {
  WM_HWIN hWin;
  char str[3];
  GUI_Init();

  GUI_SetBkColor(GUI_WHITE);
  GUI_Clear();
  GUI_SetColor(GUI_BLACK);

  GUI_DispStringAt("DONE", 10, 10);
  
  hWin = TEXT_CreateEx(10, 17, 50, 20, WM_HBKWIN, WM_CF_SHOW, TEXT_CF_LEFT|TEXT_CF_TOP, GUI_ID_TEXT0, "");

  WM_SetCallback(hWin, _cbCallback);

  TEXT_SetText(hWin, "DONE");
  GUI_Delay(100);
  sprintf(str, "%d", extent);
  GUI_DispStringAt(str, 10, 30);
  while(1) {
	GUI_Delay(100);
  }
}


Regards
Sven