Writing Applets

Table of Contents
Hello World Applet
Defining a Popup Context Menu
Detecting Changes in the Panel.
Session/Preference Saving.
Multiple Applets

Writing applets is very simple. You take some boiler plate code like below, change a couple of things and write the code that implements your widgetry. The hardest part is writing your widgetry - and its completely up to yourself how hard that should be.


Hello World Applet

As usual, following the pointless tradition of starting with an example of how get 'Hello World' on the screen in some form, here's just about the simplest applet you could write.

#include <string.h>

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

static BonoboObject *
hello_applet_new ()
{
        PanelApplet *applet;
        GtkWidget   *label;

        label = gtk_label_new ("Hello World");

        applet = panel_applet_new (label);

        return BONOBO_OBJECT (panel_applet_get_control (applet));
}

static BonoboObject *
hello_applet_factory (BonoboGenericFactory *this,
                      const gchar          *iid,
                      gpointer              data)
{
        BonoboObject *applet = NULL;

        if (!strcmp (iid, "OAFIID:GNOME_HelloApplet"))
                applet = hello_applet_new ();

        return applet;
}


PANEL_APPLET_BONOBO_FACTORY ("OAFIID:GNOME_HelloApplet_Factory",
                             "The Hello World Applet",
                             "0",
                             hello_applet_factory,
                             NULL)
      

The code here is very similar to writing a normal Bonobo control. You define a factory using PANEL_APPLET_BONOBO_FACTORY(), passing it a factory functions like hello_applet_factory().

To crete a #PanelApplet object, you first should set up the widgets for you applet and then call panel_applet_new(), passing the top-level widget of the applet. For example, if you were writing a cdplayer applet, you would stuff all the buttons for cdplayer into a #GtkHBox, and then call

panel_applet_new (hbox);