GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Oct 31, 2014 8:28 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: universal tray applet framework
PostPosted: Mon Sep 24, 2012 7:34 am 
Offline

Joined: Thu Jul 19, 2012 8:02 am
Posts: 3
I got tired of the multitude of various complex tray applets, so I cobbled together a unix-ian style tray applet that can perform unlimited tasks as a single instance. The concept is pretty simple... just give it an image(s) and optionally tooltip(s)/tooltip_file(s) left click action and right click action (a single instance can do multiple images) Then use your favorite scripting language (or compiled if necessary) to modify your image, tooltip text file or even the click action executables. I have been using SVG to generate icons on the fly with embedded images, shapes, text and even incorporated some line drawings from raphael.js and have used 9menu, getgui, gtkdialog, yad, Xdialog and zenity from bash and busybox ash/hush, but you could just as easily use gnocl, pygtk, tk, lua, etc... for the click actions and to modify the images.

The options are limited to your imagination, but should be able to mimic any other tray applet fairly closely.

I recently updated to only use glib/gio/gtk so I am hoping someone can test it on windows.

usage:
sit /path/to/image /path/to/tooltip.txt left-click-action right-click-action
/path/to/2ndimage "tooltip text" "leftaction args" "rightaction args"
/path/to/3rdimage "" "" ""
...

hope its useful to someone, I am releasing it to public domain and/or any OSI approved license without any warranty.
Code:
#include <gtk/gtk.h>

void leftclick(GtkStatusIcon *si, gpointer s){
   g_spawn_command_line_async(s,NULL);
}

void rightclick(GtkStatusIcon *si, guint b,guint a_t, gpointer s){
   g_spawn_command_line_async(s,NULL);
}

/** refreshes the status icon image from file if it changes */
void refresh(GFileMonitor *monitor, GFile *file, GFile *to_file, GFileMonitorEvent event, gpointer si){
   gtk_status_icon_set_from_file(si,g_file_get_path(file));
}

/** gets a new mouse over tooltip from file if it changes */
void updatetooltip(GFileMonitor *monitor,   GFile *file, GFile *to_file, GFileMonitorEvent event, gpointer si){
   gtk_status_icon_set_tooltip_text(si, g_mapped_file_get_contents(g_mapped_file_new(g_file_get_path(file), FALSE, NULL)));
}

int main(int argc, char *argv[]){
   GtkStatusIcon *si;
   char i=1;
gtk_init (&argc, &argv);
if ( argc < 2 ) {
   g_printerr("usage:\n%s /path/to/image /path/to/tooltip left-action right-action ...\n",argv[0]);
   return 1;
}
/** loop through icon, tooltip, click messages **/
while (i<argc){
/** status icon **/
if (g_file_test(argv[i], G_FILE_TEST_EXISTS)){
   si = gtk_status_icon_new_from_file(argv[i]);
   g_signal_connect(g_file_monitor_file(g_file_new_for_path(argv[i++]), G_FILE_MONITOR_NONE, FALSE, NULL),
      "changed", G_CALLBACK(refresh),(gpointer) si);
}else{
   g_printerr("error: could not stat file %s\n",argv[i]);
   return 2;
}
/** tooltip **/
if (g_file_test(argv[i], G_FILE_TEST_EXISTS)){
   gtk_status_icon_set_tooltip_text(si, g_mapped_file_get_contents(g_mapped_file_new(argv[i], FALSE, NULL)));
   g_signal_connect(g_file_monitor_file(g_file_new_for_path(argv[i++]), G_FILE_MONITOR_NONE, FALSE, NULL),
      "changed", G_CALLBACK(updatetooltip),(gpointer) si);
}else{
   if (argv[i]!=NULL) gtk_status_icon_set_tooltip_text(si, argv[i]);
   ++i;
}
/** left click action **/
if (argv[i]!=NULL) g_signal_connect(G_OBJECT(si), "activate", G_CALLBACK(leftclick),(gpointer) argv[i]);
++i;
/** right click action **/
if (argv[i]!=NULL) g_signal_connect(G_OBJECT(si), "popup-menu", G_CALLBACK(rightclick), (gpointer) argv[i]);
++i;
}
gtk_main();
}


Attachments:
sit.tar.gz [1.99 KiB]
Downloaded 540 times
Top
 Profile  
 
 Post subject: Re: universal tray applet framework
PostPosted: Tue Nov 20, 2012 12:34 am 
Offline

Joined: Thu Jul 19, 2012 8:02 am
Posts: 3
I posted a win32 binary here:
http://murga-linux.com/puppy/viewtopic.php?p=665746#665746
Built with mingw and the gtk+-2.24.10 bundle.
(yes I see the irony of posting the win32 binary on a linux forum, I plan to update it once I get some feedback)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group