Skeleton systray applet
Jun 30, 2013 17:06:23 GMT 1
Post by barryk on Jun 30, 2013 17:06:23 GMT 1
Thanks to mechanixx, vovchik and PvE, who created the systray applet linked to from the BaCon main page. Plus vovchik's other exploratory work with embedding C functions into BaCon code.
I have written a complete skeleton systray applet that can be used as a starting point for anyone wanting to create their own applet.
The skeleton creates an icon in the tray, with tooltip message (mouse-over), a dialog window when left-click on the icon, a dialog-window when right-click on the icon, and a timer.
The timer runs a function every 5 seconds, and I have it updating the tooltip message, plus change the icon -- so the icon flips every 5 seconds.
It is quite a small program, here it is:
To compile this, you will need two 24x24 icons, blank.xpm and skeleton.xpm.
I compiled it like this:
It compiles the icons into the binary executable. Alternatively, you can use gdk_pixbuf_new_from_file().
Regards,
Barry Kauler
I have written a complete skeleton systray applet that can be used as a starting point for anyone wanting to create their own applet.
The skeleton creates an icon in the tray, with tooltip message (mouse-over), a dialog window when left-click on the icon, a dialog-window when right-click on the icon, and a timer.
The timer runs a function every 5 seconds, and I have it updating the tooltip message, plus change the icon -- so the icon flips every 5 seconds.
It is quite a small program, here it is:
'130630 Barry Kauler: skeleton tray applet, written in BaCon.
'(c) Copyright Barry Kauler, June 2013. License GPL3.
PRAGMA LDFLAGS `pkg-config --cflags --libs gtk+-2.0`
PRAGMA INCLUDE glib-2.0/glib-object.h
PRAGMA INCLUDE gtk-2.0/gtk/gtk.h
PROTO gtk_init, gtk_main, gtk_message_dialog_new, gtk_dialog_run, gtk_widget_destroy
PROTO gtk_status_icon_set_tooltip, gtk_status_icon_set_from_pixbuf, gtk_status_icon_new
PROTO gtk_status_icon_set_tooltip_text
PROTO g_signal_connect, g_timeout_add_seconds
PROTO gdk_pixbuf_new_from_xpm_data, gdk_pixbuf_new_from_file
DECLARE tray_icon TYPE GtkStatusIcon*
DECLARE blank_pixbuf TYPE GdkPixbuf*
DECLARE skeleton_pixbuf TYPE GdkPixbuf*
DECLARE gerror TYPE GError*
USEC
#include "blank.xpm"
#include "skeleton.xpm"
END USEC
'ref: http://developer.gimp.org/api/2.0/gtk/GtkDialog.html ref: http://zetcode.com/tutorials/gtktutorial/gtkdialogs/
SUB tray_icon_on_click(GtkStatusIcon* status_icon, gpointer user_data)
LOCAL dialog TYPE GtkWidget*
dialog=gtk_message_dialog_new(0,GTK_DIALOG_MODAL,GTK_MESSAGE_INFO,GTK_BUTTONS_OK,"You left-clicked on the tray icon", "Skeleton systray applet")
gtk_dialog_run(GTK_DIALOG(dialog))
gtk_widget_destroy(dialog)
END SUB
SUB tray_icon_on_menu(GtkStatusIcon* status_icon, guint button, guint activate_time, gpointer user_data)
LOCAL dialog TYPE GtkWidget*
dialog=gtk_message_dialog_new(0,GTK_DIALOG_MODAL,GTK_MESSAGE_INFO,GTK_BUTTONS_OK,"You right-clicked on the tray icon", "Skeleton systray applet")
gtk_dialog_run(GTK_DIALOG(dialog))
gtk_widget_destroy(dialog)
ENDSUB
DECLARE cnt,cnt5 TYPE int
cnt=0
cnt5=0
FUNCTION update_func(gpointer ptr)
'entered every 5 seconds.
'example, update the tooltip...
INCR cnt,5
cnt$=STR$(cnt)
newtooltip$=CONCAT$("New tooltip, count=",cnt$)
gtk_status_icon_set_tooltip(tray_icon, newtooltip$)
'example, change the icon...
IF cnt5==1 THEN
gtk_status_icon_set_from_pixbuf(tray_icon,skeleton_pixbuf)
cnt5=0
ELSE
gtk_status_icon_set_from_pixbuf(tray_icon,blank_pixbuf)
cnt5=1
ENDIF
'must return TRUE, otherwise the timer will stop...
RETURN TRUE
END FUNCTION
gtk_init(0, 0)
tray_icon = gtk_status_icon_new()
g_signal_connect(tray_icon, "activate", G_CALLBACK(tray_icon_on_click), NULL)
g_signal_connect(tray_icon, "popup-menu", G_CALLBACK(tray_icon_on_menu), NULL)
blank_pixbuf=gdk_pixbuf_new_from_xpm_data((const char**)blank_xpm)
skeleton_pixbuf=gdk_pixbuf_new_from_xpm_data((const char**)skeleton_xpm)
'skeleton_pixbuf=gdk_pixbuf_new_from_file("skeleton.xpm",&gerror)
gtk_status_icon_set_from_pixbuf(tray_icon,skeleton_pixbuf)
'void gtk_status_icon_set_tooltip_text (GtkStatusIcon *self, gchar* text);
gtk_status_icon_set_tooltip_text(tray_icon,"You have mouse-overed the skeleton applet")
'guint g_timeout_add_seconds (guint interval, GSourceFunc function, gpointer data)
g_timeout_add_seconds(5,(void*)update_func,NULL)
gtk_main()
To compile this, you will need two 24x24 icons, blank.xpm and skeleton.xpm.
I compiled it like this:
cp -a -f *.xpm /tmp/
bacon -o -s -o -Os -o -fdata-sections -o -ffunction-sections -o -Wl,--gc-sections -d /tmp skeleton_tray.bac
strip --strip-unneeded skeleton_tray
It compiles the icons into the binary executable. Alternatively, you can use gdk_pixbuf_new_from_file().
Regards,
Barry Kauler