Gtk StatusIcon does not update



Hi all,

I have written a program in Gtk that has an icon in system tray.
This icon's image will be changed every 1s by a thread.
The problem is, icon's image in system tray does not change as thread update it.
Any help would be great appreciated.

-----
The source code is as below:

#include <gtk/gtk.h>
#include <stdlib.h>

int image = 0;
GtkStatusIcon *tray_icon = NULL;

static GtkStatusIcon *create_tray_icon() {

  GError *error = NULL;
  tray_icon = gtk_status_icon_new_from_file
("/usr/share/pixmaps/gnome-laptop.png");
  gtk_status_icon_set_tooltip(tray_icon,
                              "Example Tray Icon");
  gtk_status_icon_set_visible(tray_icon, TRUE);
  return tray_icon;
}

void *update_icon_thread (void *tid)
{
  while (1) {
  if (1 == image) {
    gtk_status_icon_set_from_file (tray_icon,
"/usr/share/pixmaps/gnome-laptop.png");
    image = 0;
  } else {
    gtk_status_icon_set_from_file (tray_icon,
"/usr/share/pixmaps/apple-red.png");
    image = 1;
  }
    sleep(1);
  }
}

int main(int argc, char **argv) {
  GtkStatusIcon *tray_icon;

  pthread_t thread;
  int rc;

  gtk_init(&argc, &argv);
  tray_icon = create_tray_icon();

  rc = pthread_create(&thread, NULL, update_icon_thread, (void *)NULL);
  if (rc) {
    printf("ERROR; return code from pthread_create() is %d\n", rc);
    exit(-1);
  }
  gtk_main();

  return 0;
}



-- 
Nguyen Anh Phu


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]