Switching between button icon and image?

What is the proper way to switch between button icon and a child widget that is an image?
Platform is GTK4.

I am trying to switch between,

 gtk_button_set_icon_name (GTK_BUTTON(btn), "media-playback-stop-symbolic");

gtk_button_set_child (button, image);

image is given by

GtkWidget* image = gtk_image_new_from_resource('/path/…");

I seem to be able to initially apply “image” and then change to “…-stop-sybmolic” once but when I try to switch back to “image” then it usually crashes. Sometimes I had both the icon and image to overlap although the docs claim that whatever widget is there “will be removed and replaced with the image”. I cannot see how that can be true and see them both in my button.

Then I’ve tried to g_object_ref(image) before _set_child(button,image) (in case it got removed) and various takes on that but nothing helps.

Any suggestions?

I think the best thing would be to just use gtk_button_set_child (button, image) alternating between the image and the image of the icon you were using.

Setting the icon as an image may require a few more steps, but it is possible to do this and get exactly the same look as setting the icon as an icon.

1 Like

If the custom image is also an icon, you can acutally just install it as an icon, either system-wide with your app’s id prefixed to its name or in your app’s resources (at <resource_base_path>/icons/(<size>/<category> or ‘symbolic’)/ e.g. /org/gtk/example/icons/symbolic/, /org/gtk/example/icons/scalable/actions/ or /org/gtk/example/icons/512x512/places/). Then you would be able to reference it by name, just like “media-playback-stop-symbolic”.

It would be more helpful to know for what or why you need to do this.

Thanks for responding.

I am developing an application to run on both windows and linux and it is just not acceptable to use the proposed solution of installing the msys/mingw64 environment since that would introduce a variable to the functionality of the program. Nobody wants that.
The price seems to be, since some icons broke on the Windows install, to make my own. And also, I wanted to have somewhat unusual (not of the sake of being unusual, just that the subject matter is unusual) icons that would also match i slightly large representation of the same (say) object. Hence, my own png images for all that stuff.

After a bit of messing around, not really understand what I was doing, I got it to work with gtk_button_set_child(). The lerning curve here was that I had to use gtk_image_new_from_resource every time before I did that. Two years into the project, and I still struggle with the memory management concept of GTK/GLIB.

You don’t need to do that. You can use existing icons. You can take icons from Icon Library app or any FOSS icon theme e.g. Adwaita and add them as resources in your app.

In special case of taking icons from Adwaita, instead of adding them as resources, you also have the option to manually copy icons from Adwaita into share/icons/hicolor or share/icons/Adwaita directory inside your app’s folder on Windows. Although, I don’t recommend that.

Actually, you don’t need to do that either. If you add icons to your resources under {resource-base-path}/icons/{size}/{category}/ or {resource-base-path}/icons/symbolic/ (for symbolic icons), you can simply use the icons by their name.

If you need an example to look at, you can look at my app’s resources and how I use the icons just by their names.

Note however that for my convenience, I’ve changed my app’s resource-base-path to “/app/”. That is why my app’s gresource.xml has <gresource prefix="/app"/>. Your app’s resource-base-path would be dependent on its application-id and you would use that in gresource.xml instead of “/app/”.

I don’t get how whatever method you are using doesn’t add a point of possible accidental variation as well. At least with MSYS, however they build GTk, it is tested in real world by multiple users, so it has a better chance of being built properly.

I am not too sure about that. For two years (on a similar project), I had to compile libfreetype from from source in order for the program to work. It’s only lately that I’ve been able to use the libfreetype that comes with the distribution. Not doing anything fancy with it either. Used to check a couple of times per year if the provided libfreetype would work and eventually, it did.

It’s also worth a lot to know what exactly is the state of the program without external dependencies like that. It’s almost like a statically linked program, and on windows, it effectively is.

Also, the install of msys takes a lot of time and adds complexity to the installer. Now, the program installs in a few seconds. Users can download and start using it in under 20 seconds. The program itself is “only” a 50MB. Msys is way larger than that.

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