Hi,

I'm trying to make a Gnome applet, and I want it to display a
different picture when the user clicks on it. The (simplified) code is
below. The updateIcon successfully changes the tooltip, but the icon
remains the same. What am I doing wrong?

Thanks,

Cosmin

------------- The Code -------------

/**
 * Inspired from the "Writing Gnome Applets in Gnome2" article, found at
 * http://www.gnome.org/projects/ORBit2/appletstutorial.html
 */

#include <panel-applet.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkimage.h>
#include <gtk/gtktooltips.h>

#include <string.h>

GtkWidget *event_box;   // Here's the picture.
int icon;               // The current icon
GtkWidget *image;               // The icon displayed on the applet.
GtkTooltips* toolTips;  // The tooltip collection

/* Loads an image from a file, according to the specified color. */
static GtkWidget* loadImage(int color) {
        switch (color) {
                case 0:
                        return gtk_image_new_from_file 
("/usr/share/pixmaps/green.png");
                case 1:
                        return gtk_image_new_from_file 
("/usr/share/pixmaps/yellow.png");
                default:
                        return gtk_image_new_from_file 
("/usr/share/pixmaps/red.png");
        }
}

void updateIcon(int color) {
        icon = color;
        image = loadImage(icon);
        gtk_tooltips_set_tip(toolTips, event_box, "New tip", "Further 
information");
}

/* Called when the user clicks on the applet. */
static gboolean on_button_press (
        GtkWidget *event_box, GdkEventButton *event, gpointer data) {
        /* Don't react to anything other than the left mouse button;
           return FALSE so the event is passed to the default handler */
        if (event->button != 1)
                return FALSE;
        // Here's where I try to change the icon:
        updateIcon(1);
        return TRUE;
}

static gboolean myapplet_fill (
        PanelApplet *applet, const gchar *iid, gpointer data) {
        if (strcmp (iid, "OAFIID:MyApplet") != 0)
                return FALSE;

        image = loadImage(icon);
        // The GtkImage widget doesn't receive events so won't respond to any
mouseclicks.
        // We need to place the GtkImage into a GtkEventBox
        event_box = gtk_event_box_new();
        gtk_container_add (GTK_CONTAINER (event_box), GTK_WIDGET(image));
        // Now we add the event box into the applet
        gtk_container_add (GTK_CONTAINER (applet), GTK_WIDGET(event_box));
        // Set the tooltip
        toolTips = gtk_tooltips_new();
        gtk_tooltips_set_tip(toolTips, event_box, "Tool tip", "Further 
information");

        g_signal_connect (G_OBJECT (event_box), 
                        "button_press_event",
                        G_CALLBACK (on_button_press),
                        image);

        // Show the applet
        gtk_widget_show_all (GTK_WIDGET (applet));

        return TRUE;
}

PANEL_APPLET_BONOBO_FACTORY ("OAFIID:MyApplet_Factory",
                             PANEL_TYPE_APPLET,
                             "My_Applet",
                             "0",
                             my_applet_fill,
                             NULL);
_______________________________________________
gnome-devel-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/gnome-devel-list

Reply via email to