Friday, May 25th 2018, 5:05am UTC+2

You are not logged in.

  • Login
  • Register

mastupristi

Beginner

Date of registration: Feb 18th 2016

Posts: 40

1

Monday, February 26th 2018, 3:26pm

implement a self-dimensioning TEXT widget

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

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
static void cbResizableText(WM_MESSAGE * pMsg)
{
	WM_HWIN hWin;
	hWin = pMsg->hWin;
	switch (pMsg->MsgId)
	{
	case WM_PRE_PAINT:
		{
			const GUI_FONT * pFont;
			char Str[20];
			TEXT_GetText(hWin, Str, 20);
			pFont = GUI_SetFont(TEXT_GetFont(pMsg->hWin));
			int x = GUI_GetStringDistX(Str) + 3;
			WM_SetXSize(hWin, x);
			GUI_SetFont(pFont);
		}
		break;
	}
	TEXT_Callback(pMsg);
}


static TEXT_Handle RESIZABLETEXTWIDGET_Create(int x0, int y0, int xSize, int ySize, WM_HWIN hParent, int WinFlags)
{
	TEXT_Handle hWin;

	hWin = TEXT_CreateEx(x0, y0, xSize, ySize,
			hParent, WinFlags | WM_CF_MEMDEV, TEXT_CF_VCENTER | TEXT_CF_HCENTER, GUI_ID_TEXT0, "" );
	WM_SetCallback(hWin, cbResizableText);
	return hWin;
}


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

mastupristi

Beginner

Date of registration: Feb 18th 2016

Posts: 40

2

Monday, February 26th 2018, 6:16pm

[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/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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "GUI.h"
#include "TEXT.h"

//#define TEST

#ifdef TEST
static void cbResizableText(WM_MESSAGE * pMsg)
{
	WM_HWIN hWin;
	hWin = pMsg->hWin;
	switch (pMsg->MsgId)
	{
	case WM_PRE_PAINT:
		{
			const GUI_FONT * pFont;
			char Str[20];
			TEXT_GetText(hWin, Str, 20);
			pFont = GUI_SetFont(TEXT_GetFont(pMsg->hWin));
			int x = GUI_GetStringDistX(Str) + 3;
			WM_SetXSize(hWin, x);
			GUI_SetFont(pFont);
		}
		break;
	}
	TEXT_Callback(pMsg);
}

static TEXT_Handle RESIZABLETEXTWIDGET_Create(int x0, int y0, int xSize, int ySize, WM_HWIN hParent, int WinFlags)
{
	TEXT_Handle hWin;

	hWin = TEXT_CreateEx(x0, y0, xSize, ySize,
			hParent, WinFlags | WM_CF_MEMDEV, TEXT_CF_VCENTER | TEXT_CF_HCENTER, GUI_ID_TEXT0, "" );
	WM_SetCallback(hWin, cbResizableText);
//	TEXT_SetBkColor(hWin, GUI_WHITE);
	return hWin;
}
#endif // TEST

static void cb(WM_MESSAGE * pMsg)
{
	WM_HWIN hWin;
	hWin = pMsg->hWin;
	switch (pMsg->MsgId)
	{
	case WM_PAINT:
		{
			GUI_RECT Rect;
//			WM_GetInsideRectEx(hWin, &Rect);
			WM_GetClientRect(&Rect);
			GUI_SetBkColor(GUI_DARKGREEN);
			GUI_ClearRectEx(&Rect);

			GUI_SetColor(GUI_LIGHTRED);
			GUI_DrawRectEx(&Rect);
		}
		break;
	default:
		WM_DefaultProc(pMsg);
	}
}


void MainTask(void)
{
  GUI_Init();
  GUI_EnableAlpha(1);

  GUI_SetBkColor(GUI_DARKGRAY);
  GUI_Clear();

  WM_HWIN hParent, hWin;

  hParent = WM_CreateWindowAsChild(10, 10, 60, 25, WM_HBKWIN,
					WM_CF_SHOW, cb, 0);

#ifdef TEST
  hWin = RESIZABLETEXTWIDGET_Create(4,4, 1, 12, hParent, WM_CF_SHOW);
#else
  hWin = TEXT_CreateEx(4,4, 1, 12, hParent, WM_CF_SHOW, TEXT_CF_VCENTER | TEXT_CF_HCENTER, GUI_ID_TEXT0, "" );
#endif
  TEXT_SetBkColor(hWin, GUI_DARKBLUE);
  TEXT_SetTextColor(hWin, GUI_DARKYELLOW);

  while(1)
  {
		GUI_Delay(2000);
		TEXT_SetText(hWin, "A");
#ifndef TEST
		const GUI_FONT * pFont;
		pFont = GUI_SetFont(TEXT_GetFont(hWin));
		int x = GUI_GetStringDistX("A") + 3;
		WM_SetXSize(hWin, x);
		GUI_SetFont(pFont);
#endif // TEST

		GUI_Delay(2000);
		TEXT_SetText(hWin, "WWW");
#ifndef TEST
		pFont = GUI_SetFont(TEXT_GetFont(hWin));
		x = GUI_GetStringDistX("WWW") + 3;
		WM_SetXSize(hWin, x);
		GUI_SetFont(pFont);
#endif // TEST
  }
}


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



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:



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