Getting the name of a widget?

30 views
Skip to first unread message

Svets

unread,
Aug 14, 2016, 4:08:54 AM8/14/16
to fltk.general
Is there any possible way to get the name of a widget.

For example.

Fl_Choice *ch = new Fl_Choice(10,10,100,40);  //no label.


cout
<< "The name of the Fl_Choice widget is: " <<  ch->name() << endl;


//clearly the above will not work. Is there any way of doing this or am I restricted to identifying the widget via it's x() and y() coordinates?

Thanks

Ian MacArthur

unread,
Aug 14, 2016, 5:30:44 AM8/14/16
to fltk.general


Svets,

What do you mean by the "name" of the widget? I'm assuming you do not just mean its label(), so are you referring to the type of the widget, e.g. whether it is an Fl_Choice or an Fl_Button?

If so, you can do that, but you need to build your code with RTTI enabled (Run Time Type Information) and use that to ascertain the type of the widget.

I don't have any examples on this laptop, but google will surely find some.

We do not use RTTI by default in fltk, as (at least historically) it could be quite slow to call and entails building in a lot of additional information into your application that you would not otherwise require, making the application a fair bit larger in some cases.




Albrecht Schlosser

unread,
Aug 14, 2016, 9:08:16 AM8/14/16
to fltkg...@googlegroups.com
On 14.08.2016 10:08 Svets wrote:
> Is there any possible way to get the name of a widget.
>
> For example.
>
> |
> Fl_Choice*ch =newFl_Choice(10,10,100,40); //no label.
>
>
> cout <<"The name of the Fl_Choice widget is: "<< ch->name()<<endl;
> |
>
>
> //clearly the above will not work. Is there any way of doing this or am
> I restricted to identifying the widget via it's x() and y() coordinates?

[Please read Ian's post as well]

If you want to use the label of your widgets to identify the widgets,
then you can just use the label. I think that's what your example code
implies. However, if you don't want your widget to have a visible label
you can still use one with

widget->label(FL_NO_LABEL, "my_choice");

<http://www.fltk.org/doc-1.3/classFl__Widget.html#aa3afb86e13e921c17934cf2b1f6951b4>

See also:
<http://www.fltk.org/doc-1.3/classFl__Widget.html#aa3afb86e13e921c17934cf2b1f6951b4>
<http://www.fltk.org/doc-1.3/Enumerations_8H.html#ad5774781d33328b82990ff9e25dfd61b>

Hence you can set an invisible label to identify your widget. Does this
help?

Otherwise you might use the user_data() method to set a pointer to an
own structure so that it can be used for callbacks and/or to identify
your widget. Something like:

struct my_widget_data {
void *userdata_ptr;
int userdata_int;
const char *name; // used to identify a particular widget
}

Set the user_data() widget member to point to your struct and use one or
the other struct member in callbacks or to identify the widget, resp.

Svets

unread,
Aug 15, 2016, 5:29:01 AM8/15/16
to fltk.general, Albrech...@online.de
Thanks so much Ian and Albrecht, both options are great.  I'm going to use this option due to it's simplicity.

widget->label(FL_NO_LABEL, "my_choice");

I appreciate the quick response. 

Thanks again.
Reply all
Reply to author
Forward
0 new messages