cancel
Showing results for 
Search instead for 
Did you mean: 

Creating TextArea Programmatically With Wildcard and Buffer

ilyus
Senior II

So far, I've been able to create a TextArea with a wildcard in TouchGFX and then edit its buffer via program code - that works. I just edit the array and invalidate. Done.

However, I fail miserably when I try to create Textarea using code only. I have created boxes, for example, and scrollable containers, put stuff inside each other and they work (and many of them in arrays that I loop through when drawing), but textareas are pretty wild and also weirdly (under)documented, and there are pretty much no examples in the internet, I googled my fingers to blood and left almost no links unclicked in google.

My logic was to look at TextAreas created by TouchGFX generator and reproduce the procedure - worked with other things, but doesn't work for TextArea. Here is a WORKING automatically generated by TouchGFX code from ViewBase:

textArea1.setXY(0, 51);
textArea1.setColor(touchgfx::Color::getColorFrom24BitRGB(0, 0, 0));
textArea1.setLinespacing(0);
Unicode::snprintf(textArea1Buffer, TEXTAREA1_SIZE, "%s", touchgfx::TypedText(T_SINGLEUSEID63).getText());
textArea1.setWildcard(textArea1Buffer);
textArea1.resizeToCurrentText();
textArea1.setTypedText(touchgfx::TypedText(T_SINGLEUSEID62));
scrollableContainerMeasurement.add(textArea1);
scrollableContainerMeasurement.setScrollbarsPermanentlyVisible();

First of all, this function, Unicode::snprintf(), is weirdly documented and I have still no clue how to use it.

More specifically I have no idea what I have to put (when I create my own TextArea) instead of its parameter touchgfx::TypedText(T_SINGLEUSEID63).getText(). Documentation says it's TypedText type - what is it? How do I generate one? From what? What exactly is it? Documentation for it doesn't seem to have any functions that produce object of this obscure type.

Also, if I already do setWildcard(), why do I do setTypedText two lines lower? With some other Single Use ID, whatever that is. Seems like completely useless line of code. I don't understand what it does if I already have a Wildcardbuffer attached to TextArea.

Whenever I manually change the TextArea wildcard with code, I just change the Buffer array and invalidate - I don't need any TypedText or anything. But whenever I just dropped those lines that I don't understand (it doesn't compile with unchanged lines with my own TextAreas - I need to change them somehow, I guess?) and tied wildcardbuffer to the Textarea, it simply didn't show anything on the screen - as if I didn't create anything.

Any hint would be appreciated, thank you!

23 REPLIES 23

Now, I want the textareas to appear on the screen immediately. Nothing stops you from putting the generating code into some other function, of course. Such as after button press. But remember, that you can do "add" only once, otherwise it will crash. Keep it in mind.

I think, most of the code there is pretty easy to read.

Setting the coordinates, then setting color using TouchGFX color, then line spacing.

Then we do .setWildcard to associate a buffer array with the TextArea. It won't immediately produce text on the screen after this function tho, because we haven't hooked up the text formatting yet.

Next we do SetTypedText - the function that took me the longest to understand. This actually IS formatting. You need to add there the Resource ID of the formatting we created earlier - it was ID3. If you don't know what exactly to write there, open "TextKeysAndLanguages.hpp", there is a list of resource IDs, find the one with the number youi created. Notice they are all capitalized even if your resource has a name with small letters (confusing!).

So basically we said "Hey, create me a TextArea, use that array as wildcard's buffer and use that resource for defining font, alignment, direction, position of wildcard, etc.". Our wildcard takes 100% of that.

Next, we do the funny "Unicode::snprintf(testTextAreaBuffer, 10, "Meow meow");" This actually puts the string "Meow meow" into the buffer and adds termination character at the end so you don't have to worry about it. This function actually defines what TextArea shows. "10" is a buffer length. You can have a string of 1 character but have buffer length 10, the function will take care of it.

Then I decided to resize the TextArea, but you can set fixed width of course, it's all in TouchGFX documentation online. I just wanted to see all, since I'm doing proof of concept here.

Then I add textArea to the screen. It could very well be container1.add or something else.

And then invalidate() forces TouchGFX to redraw the widget to make it show up to date content of the buffer. If you want to change the content of TextArea, you do Unicode::snprintf(testTextAreaBuffer, 10, "New Text"); followed by invalidate().

The second TextArea does exactly the same; it has the same Resource but its own buffer array.

I have a semi-opaque background box on the screen, other than that, the result is the following:

0693W00000BcSTfQAN.png 

Congrats, that's it!

If you have a coding problem, use this function:

Unicode::strncpy(textArea1Buffer, (char *) display_buffer, TEXTAREABUFFER_SIZE-1);

textArea1Buffer is UTF8

display_buffer is string from ASCII

It helped me get rid of the ???? on the display

AMiro.5
Associate II

Beware that

Unicode::snprintf(textArea1Buffer, TEXTAREA1_SIZE, "%s", touchgfx::TypedText(T_SINGLEUSEID63).getText());

textArea1.setWildcard(textArea1Buffer);

should go strictly BEFORE both of functions

textArea1.resizeToCurrentText();

AND

textArea1.setTypedText(touchgfx::TypedText(T_SINGLEUSEID62));

This is because resizeToCurrentText will also calculate wildcard text's width - and if wildcard's buffer is not initialized with null-terminated string - it will calculate till the first zero somewhere in memory, and can hang or crash.

And setTypedText calls resizeToCurrentText internally with the same effect.

I swear god I spent 2 hours only because I wrote 

Unicode::snprintf(testTextAreaBuffer2, 10, "Woof woof");

 wrong....

Thanks man