GTK 4: how to retrieve the label in an InfoBar?

In my gtk-3-fortran library, I had a function which received a pointer to an InfoBar and a message to show in that bar. To get the label that was put (in another part of the program) in the InfoBar, I was using that code:

   content = gtk_info_bar_get_content_area(infobar)
   children = gtk_container_get_children(content)
   label = g_list_nth_data(children, 0_c_int)
   call gtk_label_set_text(label,message)

But in GTK 4, gtk_info_bar_get_content_area() and gtk_container_get_children() were removed. How can I retrieve the label inside my InfoBar?

In general, in GTK4 all widgets can have children, so you can use gtk_widget_get_first_child() and gtk_widget_get_next_sibling() to iterate over the children of a widget.

In any case, though, you should not retrieve the contents of an info bar widget: those are, and have always been, considered private. Accessing them with gtk_container_get_children() was an indication that what you wanted to achieve was already not recommended.

You should keep a reference to the GtkLabel you set as the content of the GtkInfoBar, if you want to change the message being displayed after creation.

1 Like

You could use g_object_set_data() to attach the label’s address, if you don’t want to pass around a structure.

1 Like

A great thank you @ebassi and @PeterB,

my code does not use the Object Oriented features of Fortran, therefore the g_object_set_data() trick is perfect to keep track of my label, even if I create several infobars:

! To keep track of the label inside the infobar object:
call g_object_set_data (infobar, "info_label"//c_null_char, label) 
! We retrieve the label we have named "info_label" inside the infobar:
label = g_object_get_data (infobar, "info_label"//c_null_char)
call gtk_label_set_text(label, message)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.