I have a test app built on Ubuntu 22.04 using gtk3 that basically does this to create a status icon with an attached menu:
#include <gtk/gtk.h>
//#include <glib/glib.h>
static void trayExit(GtkMenuItem *item,
gpointer user_data)
{
printf("Quit\n");
gtk_main_quit();
}
static void trayIconOnClick(GtkStatusIcon* statusIcon,
gpointer trayMenu)
{
printf("Clicked on tray icon\n");
gtk_menu_popup_at_pointer(GTK_MENU(trayMenu), nullptr);
}
static void trayIconOnMenu(GtkStatusIcon* statusIcon,
guint button,
guint activateTime,
gpointer trayMenu)
{
printf("Popup menu\n");
gtk_menu_popup(GTK_MENU(trayMenu), nullptr, nullptr,
gtk_status_icon_position_menu, statusIcon,
button, activateTime);
}
static GtkWidget* createTrayMenu()
{
auto* menu = gtk_menu_new();
auto* menuItemExit = gtk_menu_item_new_with_label ("Exit");
g_signal_connect(G_OBJECT(menuItemExit), "activate", G_CALLBACK(trayExit), nullptr);
gtk_menu_shell_append (GTK_MENU_SHELL(menu), menuItemExit);
gtk_widget_show_all(menu);
return menu;
}
static GtkStatusIcon *createTrayIcon()
{
auto* trayMenu = createTrayMenu();
auto* trayIcon = gtk_status_icon_new();
g_signal_connect(G_OBJECT(trayIcon), "activate",
G_CALLBACK(trayIconOnClick), trayMenu);
g_signal_connect(G_OBJECT(trayIcon),
"popup-menu",
G_CALLBACK(trayIconOnMenu), trayMenu);
gtk_status_icon_set_from_file(trayIcon, "/tmp/icon.png");
gtk_status_icon_set_visible(trayIcon, TRUE);
return trayIcon;
}
int main(int argc, char **argv)
{
gtk_init(0, nullptr);
auto trayIcon = createTrayIcon();
gtk_main();
return 0;
}
More info:
ubuntu: libgtk-3-dev
centos: gtk3-devel
g++ `pkg-config --cflags gtk+-3.0` -o main main.cpp `pkg-config --libs gtk+-3.0`
`pkg-config --cflags glib-2.0` `pkg-config --libs glib-2.0`
It works fine on 22.04. However, when I copy the app to 20.04, the status icon doesn't display. The app is running, ldd shows it's linking to the proper libraries, etc.
Does Ubuntu 20.04 treat status icons differently? I've installed some apps on 20.04 that use them, but I don't think they're written in C.