Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GTK+ 3 StatusIcon replacement

Tags:

I want my GTK application to have a status icon in the system tray, I have got this working quite nicely using a StatusIcon, however this has been deprecated:

gtk_status_icon_new has been deprecated since version 3.14 and should not be used in newly-written code.

Use notifications

The documentation says "Use notifications" but I have no idea what this actually means. I interpret this to mean "use desktop notifications", but as far as I can tell that serves an entirely different purpose. I want an always-visible system tray icon, not a temporary popup notification. Despite that, I can't find anything else which would seem to be what is meant by "Notifications".

Does there exist a new replacement for GtkStatusIcon which provides an icon in the system tray, with support for receiving mouse click/scroll signals and displaying popup menus?

like image 890
Tomha Avatar asked Jan 29 '17 05:01

Tomha


2 Answers

Unfortunately yes, they refer to desktop notifications. If you still want to use system tray icons, then keep using GtkStatusIcon; just note that it is likely going to be removed soon. (The point is that more things have desktop notifications than status icons, but I'm sure there's other reasons as well. GTK+ probably shouldn't deal with desktop environment-specific stuff like that anyway... There's nothing stopping anyone from making a GtkStatusIcon replacement of their own, but no one seems to have done so yet.)

like image 71
andlabs Avatar answered Oct 22 '22 03:10

andlabs


For later versions of GTK+ 3, where StatusIcon is deprecated, you can use the following code to prevent seeing any warnings about deprecation:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
// Insert Status Icon Code Here
#pragma GCC diagnostic pop

While it is deprecated, it was not removed from GTK until GTK4.

For GTK4 and others, I have created TIL - Tray Icon Library, which should work in any GTK version. There is example code how to use it with GTK4. Currently it only works using XCB via XEMBED, but more backends will be supported in the future. Long-term goal would be to support most of different backends to create tray/status icons in different circumstances.

Basically how it works is that, we create the tray icon completely using the backend we want, in this case X11. Then we get the X-Connection and X Window ID of the GTK Widget. Then we just add the X Window ID of the GTK Widget as the child of the tray icon we created using X. It makes it possible to insert any GTK Widget inside the tray icon. Tray icons in X are really just windows stuck in the tray. Not all widgets work thought.

like image 27
jocez Avatar answered Oct 22 '22 03:10

jocez