How to set text to a box dinamically.

52 views
Skip to first unread message

jorgem...@gmail.com

unread,
Apr 26, 2015, 5:11:39 PM4/26/15
to fltkg...@googlegroups.com
I need to display some information in a window surrounded by a frame and I guess that using a Fl_Box is a good idea. The information to be displayed is contained in a variable because it might change. I have been trying for a while to do it without result, My main issue is how to assign the text to be box. This is what I have:

Fl_Box* boxTopScores;
boxTopScores = new Fl_Box(FL_EMBOSSED_BOX, 200, 200, 400, 300, "");

I know I can set box label properties using:

boxTopScores->labelcolor, boxTopScores->labelfont, boxTopScores->labelsize, etc. but I have not found how to set text.

I will very much appreciate your support.

Respectfully,
Jorge Maldonado

Ian MacArthur

unread,
Apr 26, 2015, 5:26:18 PM4/26/15
to fltkg...@googlegroups.com
On Sun Apr 26 2015 22:11:39, jorgem...@gmail.com wrote:
> I need to display some information in a window surrounded by a frame and I guess that using a Fl_Box is a good idea. The information to be displayed is contained in a variable because it might change. I have been trying for a while to do it without result, My main issue is how to assign the text to be box. This is what I have:
>
> Fl_Box* boxTopScores;
> boxTopScores = new Fl_Box(FL_EMBOSSED_BOX, 200, 200, 400, 300, "");
>
> I know I can set box label properties using:
>
> boxTopScores->labelcolor, boxTopScores->labelfont, boxTopScores->labelsize, etc. but I have not found how to set text.


You need to spend more time reading the manual, I fear:

You probably want either box->label(my_label); or box->copy_label(another_label);





Message has been deleted

jorgem...@gmail.com

unread,
Apr 26, 2015, 8:07:00 PM4/26/15
to fltkg...@googlegroups.com
I have spent much time reading documentation, I always try solving things by myself in the first place. In fact, I have been all day long studying documentation and solving many issues but sometimes easy things are bypassed.

What I have done so far is to read the information from a text file and concatenate such information into a string variable which finally should be like the value in "strTopScores" below (includes tabs and new lines). My problem is that "boxTopScores->label()" does not support my string variable and I have not found a solution. 

        string strTopScores;
Fl_Box* boxTopScores = new Fl_Box(FL_ENGRAVED_BOX, 200, 150, 400, 350, "");
//strTopScores = "JAM\t100\nMCH\t50\nSCM\t80\nABC\t10\nXYZ\t25\n";
boxTopScores->label(strTopScores);
boxTopScores->labelfont(FL_COURIER_BOLD_ITALIC);
boxTopScores->labelsize(40);
boxTopScores->labelcolor(FL_BLUE);

Best regards,
Jorge Maldonado

joe63074

unread,
Apr 26, 2015, 8:18:04 PM4/26/15
to fltkg...@googlegroups.com
It will work, FLTK just needs the pointer to the text buffer within the string class. The "data()" method in the string class will return the needed pointer. Try doing this:

Fl_Box* boxTopScores = new Fl_Box(FL_ENGRAVED_BOX, 200, 150, 400, 350, "");
std::string strTopScores = "JAM\t100\nMCH\t50\nSCM\t80\nABC\t10\nXYZ\t25\n";
boxTopScores->copy_label(strTopScores.data());

MacArthur, Ian (Selex ES, UK)

unread,
Apr 27, 2015, 4:56:02 AM4/27/15
to fltkg...@googlegroups.com
> I have spent much time reading documentation, I always try solving
> things by myself in the first place. In fact, I have been all day long
> studying documentation and solving many issues but sometimes easy things
> are bypassed.

It's good that you try and fix things yourself, that's the only way...

However, in this case I think no amount of reading the fltk docs will help; it looks like what you are hitting is a C++ coding issue, rather than something peculiar to fltk.
Note that the fltk label() methods take a char*, whereas you are trying to pass in a C++ string. This is not going to work.

What you want instead is:

boxTopScores->copy_label(strTopScores.c_str());

Note that I am explicitly using copy_label() here rather than just label() as it is not always certain that a C++ string will not change its underlying storage location...

I see that Joe suggested you use strTopScores.data() - in practice .c_str() and .data() are synonyms, but I think that c_str() is better in that it makes it more exlicit that it is a C-style char* that is wanted.





Selex ES Ltd
Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex SS14 3EL
A company registered in England & Wales. Company no. 02426132
********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************

Edzard Egberts

unread,
Apr 27, 2015, 5:20:46 AM4/27/15
to 'ed' via fltk.general
MacArthur, Ian (Selex ES, UK) wrote:
> What you want instead is:
>
> boxTopScores->copy_label(strTopScores.c_str());
>
> Note that I am explicitly using copy_label() here rather than just
> label() as it is not always certain that a C++ string will not change
> its underlying storage location...

http://www.cplusplus.com/reference/string/string/c_str/:

"The pointer returned may be invalidated by further calls to other
member functions that modify the object."

When the string itself remains to be valid (e.g. as a class member), it
is no problem to use string::c_str() - this pointer also remains and
won't change in an unexpected way.

But it is true, that the pointer needs to be reassigned, when there was
a change of content:

strTopScores= "First output";
boxTopScores->label(strTopScores.c_str());
strTopScores= "Next output";
boxTopScores->label(strTopScores.c_str()); // this is necessary

Reply all
Reply to author
Forward
0 new messages