# Panel Applet Reference Manual

This document is for the Panel Applet library, version &package_version;. The latest version of this documentation can be found on-line at https://developer.gnome.org/libpanel-applet/unstable/.

## Panel Applet Library Overview

The Panel Applet Library is used in the GNOME Flashback desktop to write applets that can be added to GNOME Panel. GNOME Panel is the main user interface used in the GNOME Flashback desktop environment, through which users interact with their computer and open applications.

Applets are small applications that are embedded in the GNOME Panel. They can be used to give quick access to some features, or to display the state of something specific. Most features in GNOME Panel such as the clock, the application menu, the calendar and many others are written with this Panel Applet Library. Applets are added through the "Add to Panel" dialog which can be opened from the "Add to Panel..." option on the context menu of a GNOME Panel.

The Panel Applet Library is what should be used to write applets as it handles all the communication with the GNOME panel. It hides all of the embedding process of the applet behind a PanelApplet widget. It also provides PanelApplet API to properly integrate the applet in the panel.

The Panel Applet Library was original part of the default GNOME desktop but was deprecated with GNOME 3, when GNOME Shell was released. Nowadays development continues as part of the GNOME Flashback project. The default desktop of GNOME now uses GNOME Shell, which uses a different system for extension, so an applet written with the Panel Applet Library will not be usable in the default GNOME that users may use.

## Getting Started with the Panel Applet library

### Introduction

While applets are rather simple to write, they are not the most easy form of interaction to users for two reasons:

- It is not intuitive how to add or remove applets for many users, so users may not know about your applet, or may not know how to add it to the GNOME Panel.
- The restriction in size of applets can limit their interface, so you should only use applets for simple tasks such as displaying information, or for controlling other applications and programs.

Therefore, before writing an applet, think hard whether this is the form of interaction that is best for the feature you want to provide.

### Concepts

In order to write an applet it is important to understand the relationships between GNOME Panel and your applets. For example, how are applets registered to the GNOME Panel? How does the GNOME Panel instantiate an applet?

On a typical desktop there can be multiple GNOME Panels and each GNOME Panel can have several instances of your applet. These different instances are created by an applet factory which must be registered to the GNOME Panel. Additionally, in order to identify your applet and instantiate the correct applet, each applet is identified by an applet type.

#### Applet Registration

To make your applets and the applet factory known to GNOME Panel - so that it can show your applet in the list of applets of the "Add to Panel" dialog - and to add applets to the panel, you must create a Panel Applet File. Panel Applet files are introduced in more detail later in this document. However it is important for applet authors to understand the relationship between the panel applet file, the applet factory, and the applet type. Therefore this section contains a short example of a Panel Applet file.

##### Panel Applet Files

Panel Applet files are `ini`-file formatted files that contain the necessary information about your factory and your applet types. The following example listing shows a simple Panel Applet file.

                           [Applet Factory]
                           Id=HelloWorldFactory
                           Name=Hello World Applet Factory
                           Description=Factory for our example applets

                           [HelloWorldApplet]
                           Name=Hello World
                           Description=The Hello World example applet
                           Icon=hello-world-icon

##### Applet Types

The applet type is the identifier representing a type of applets to the panel. It is a simple string, like `HelloWorldApplet` and is unique per applet factory.

In the example listing above the applet type can be found in the second section title (e.g. `[HelloWorldApplet]`, but without the square brackets).

##### Applet Factory

The applet factory is an implementation detail that is mostly hidden by the Panel Applet Library, but it still appears in a few places such as:

- The `PANEL_APPLET_OUT_PROCESS_FACTORY()` macro, which is used to build out process applets. It gets the factory id passed as its first parameter.

- The `PANEL_APPLET_IN_PROCESS_FACTORY()` macro, which is used to build in process applets. It gets the factory id passed as its first parameter.

- A Panel Applet file, which is used to register applet types and the applet factory to the GNOME Panel. It must contain the same applet factory id as used in the above two macros.

  By default applets are out-process, if you have used the `PANEL_APPLET_IN_PROCESS_FACTORY`, then you must add: `InProces=true` to the `[Applet Factory]` section.

The applet factory is the object that will create a new applet instance when the panel requests a new applet to be created. It is identified with a simple string id, for example `HelloWorldFactory`.

In the example listing above the applet factory id can be found in the `ID`-field of the `[Applet Factory]` section.

The requests the applet factory will receive from the GNOME Panel specify which type of applet should be created. This is what makes it possible to have more than one applet type in one applet binary. In most cases, however, the applet factory will be specific to only one applet type.

There is only one applet factory per applet binary, and it is always running before any applet instance is created by the applet binary. The applet factory is created via one of these two macros:

- `PANEL_APPLET_OUT_PROCESS_FACTORY()`

, or

- `PANEL_APPLET_IN_PROCESS_FACTORY()`

.

#### Applet Instances

There is no restriction as to how many instances of one applet type can be created. The user might choose to add more than one `HelloWorldApplet` applets to his panels. This can have some implications on the design used to write applets. The most important implication is that it is generally wrong to have global variables to keep a state specific to an applet instance.

Using global variables in an applet can cause problems when the applet is instantiated several times.

### Hello World Example

In this section we will write a simple example applet. This simple applet will have no functionality and only displays a label with "Hello World" on the panel. For this simple applet only following three parts are needed:

- `PANEL_APPLET_OUT_PROCESS_FACTORY()` : this creates an applet factory named `HelloWorldFactory`, and each time our applet is added to a GNOME Panel, this applet factory will create an applet instance, and calls `hello_world_factory_callback()` with a PanelApplet object already created.

- `hello_world_factory_callback()`: this checks if the request to create an applet instance is for an applet type supported by our applet factory. Here we can see that we only support the `HelloWorldApplet` applet type. This function returns `TRUE` on success and `FALSE` on failures.

  If you return `FALSE` here, GNOME Panel will show a dialog and notify the user that the applet loading failed.

- `hello_world_applet_start()`: this is where we actually setup the PanelApplet widget for the work the applet should do. This can include adding widgets to the applet, connecting to signals, loading settings via GSettings, etc.

  The PanelApplet is a subclass of GtkBin container, to add more than one widget to it you will need to use another GtkContainer implementation such as GtkBox or GtkGrid.

An example is worth a million words, so here is the code for our Hello World applet.

Hello World applet

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

    static void
    hello_world_applet_start (PanelApplet *applet)
    {
        GtkWidget *label;

        label = gtk_label_new ("Hello World");
        gtk_container_add (GTK_CONTAINER (applet), label);
        gtk_widget_show_all (GTK_WIDGET (applet));
    }

    static gboolean
    hello_world_factory_callback (PanelApplet  *applet,
                                  const gchar  *iid,
                                  gpointer      data)
    {
        if (g_strcmp0 (iid, "HelloWorldApplet") != 0)
             return FALSE;

        hello_world_applet_start (applet);

        return TRUE;
    }

    PANEL_APPLET_OUT_PROCESS_FACTORY ("HelloWorldFactory",
                                      PANEL_TYPE_APPLET,
                                      hello_world_factory_callback,
                                      NULL)

While the previous example is simple, it can be useful to directly subclass the PanelApplet type. This makes it easy to have a per-applet instance private structure, among other benefits. Most of the code below is related to the GObject system and needed to subclass the Panel Applet. The only noteworthy difference is that the `PANEL_APPLET_OUT_PROCESS_FACTORY` macro now takes our subclassed type (e.g. `HELLO_WORLD_TYPE_APPLET`) as its second parameter, instead of `PANEL_TYPE_APPLET`.

Hello World applet, with a PanelApplet subclass

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

    #define HELLO_WORLD_TYPE_APPLET (hello_world_applet_get_type ())

    typedef struct _HelloWorldApplet        HelloWorldApplet;
    typedef struct _HelloWorldAppletClass   HelloWorldAppletClass;
    typedef struct _HelloWorldAppletPrivate HelloWorldAppletPrivate;

    struct _HelloWorldApplet {
        PanelApplet parent_object;

        /*< private >*/
        HelloWorldAppletPrivate *priv;
    };

    struct _HelloWorldAppletClass {
        PanelAppletClass parent_class;
    };

    struct _HelloWorldAppletPrivate
    {
        GtkWidget *label;
    };

    static GType hello_world_applet_get_type (void) G_GNUC_CONST;

    G_DEFINE_TYPE (HelloWorldApplet, hello_world_applet, PANEL_TYPE_APPLET);

    static void
    hello_world_applet_init (HelloWorldApplet *applet)
    {
        applet->priv = G_TYPE_INSTANCE_GET_PRIVATE (applet, HELLO_WORLD_TYPE_APPLET,
                                                    HelloWorldAppletPrivate);

        applet->priv->label = gtk_label_new ("Hello World");
        gtk_container_add (GTK_CONTAINER (applet), applet->priv->label);
        gtk_widget_show (applet->priv->label);
    }

    static void
    hello_world_applet_class_init (HelloWorldAppletClass *klass)
    {
      g_type_class_add_private (class, sizeof (HelloWorldAppletPrivate));
    }

    static
    hello_world_applet_start (HelloWorldApplet *applet)
    {
        gtk_widget_show (GTK_WIDGET (applet));
    }

    static gboolean
    hello_world_factory_callback (HelloWorldApplet *applet,
                                  const gchar      *iid,
                                  gpointer          data)
    {
        if (g_strcmp0 (iid, "HelloWorldApplet") != 0)
             return FALSE;

        hello_world_applet_start (applet);

        return TRUE;
    }

    PANEL_APPLET_OUT_PROCESS_FACTORY ("HelloWorldFactory",
                                      HELLO_WORLD_TYPE_APPLET,
                                      hello_world_factory_callback,
                                      NULL)

### Using a Context Menu

The Panel Applet library uses GAction to define menu items appearing in the context menu of the applet. This chapter provides some guidelines to applet developers and explains how to setup the context menu.

#### Guidelines for Context Menu

To help guarantee consistency in the interaction with applets, there are some guidelines that are recommended to follow:

- Do not make the context menu too long: if you have more than five or six menu items, then it might be worth investing efforts on rethinking what is important in the menu.

- For the menu item that will enable the user to configure the applet, use "Preferences" for the label, and try to avoid "Configure", "Configuration", "Settings", etc.

- Avoid putting a "Help" menu item. The user will usually explicitly add the applet, so it is expected that he knows what the applet is about. Putting a "Help" menu item in the context menu is therefore too prominent. It might make sense to add a "Help" button in the Preferences dialog, though.

- If you agree, avoid putting a "About" menu item. To the user, applets are not different applications but elements of one global application, the panel. Of course, this means that credits for working on the applet are not visible to the user.

#### Setting Up the Menu

The only part of dealing with a context menu that is specific to applets is how to setup the context menu. Once it is setup, this is really just a matter of using GtkAction.

To setup the context menu of the applet, the `panel_applet_setup_menu_from_file()` function should be used, with a path to a menu XML file and a GtkActionGroup object containing all actions that are used in the menu XML file. The example below shows how to achieve this:

Hello World applet, with a context menu

    #include <glib/gi18n.h>
    #include <gtk/gtk.h>
    #include <panel-applet.h>

    /* This would usually be defined in config.h */
    #define GETTEXT_PACKAGE "hello-world"
    /* This would usually be defined in Makefile.am */
    #define HELLO_WORLD_UI_DIR "/usr/share/hello-world"

    static void hello_world_applet_prefs (GtkAction   *action,
                                          PanelApplet *applet);
    static void hello_world_applet_say   (GtkAction   *action,
                                          PanelApplet *applet);

    static const GtkActionEntry hello_world_menu_actions [] = {
            { "HelloWorldPrefs", GTK_STOCK_HELP, N_("_Preferences"),
              NULL, NULL,
              G_CALLBACK (hello_world_applet_prefs) },
            { "HelloWorldSay", GTK_STOCK_ABOUT, N_("_Say Hello"),
              NULL, NULL,
              G_CALLBACK (hello_world_applet_say) }
    };

    static void
    hello_world_applet_prefs (GtkAction   *action,
                              PanelApplet *applet)
    {
        GtkWidget *dialog;
        dialog = gtk_message_dialog_new (NULL, 0,
                                        GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
                                        "Preferences");
        g_signal_connect (dialog, "response",
                          G_CALLBACK (gtk_widget_destroy), NULL);
        gtk_widget_show (dialog);
    }

    static void
    hello_world_applet_say (GtkAction   *action,
                            PanelApplet *applet)
    {
        GtkWidget *dialog;
        dialog = gtk_message_dialog_new (NULL, 0,
                                        GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
                                        "Hello World!");
        g_signal_connect (dialog, "response",
                          G_CALLBACK (gtk_widget_destroy), NULL);
        gtk_widget_show (dialog);
    }

    static gboolean
    hello_world_applet_start (PanelApplet *applet)
    {
        GtkWidget *label;
        GtkActionGroup *action_group;
        gchar *ui_path;

        label = gtk_label_new ("Hello World");
        gtk_container_add (GTK_CONTAINER (applet), label);

        action_group = gtk_action_group_new ("Hello World Applet Actions");
        gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
        gtk_action_group_add_actions (action_group,
                                      hello_world_menu_actions,
                                      G_N_ELEMENTS (hello_world_menu_actions),
                                      applet);

        ui_path = g_build_filename (HELLO_WORLD_UI_DIR, "hello-world-menu.xml", NULL);
        panel_applet_setup_menu_from_file (applet, ui_path, action_group);

        g_free (ui_path);
        g_object_unref (action_group);

        gtk_widget_show_all (GTK_WIDGET (applet));

        return TRUE;
    }

    static gboolean
    hello_world_factory_callback (PanelApplet  *applet,
                                  const gchar  *iid,
                                  gpointer      data)
    {
        gboolean retval = FALSE;

        if (g_strcmp0 (iid, "HelloWorldApplet") == 0)
            retval = hello_world_applet_start (applet);

        return retval;
    }

    PANEL_APPLET_OUT_PROCESS_FACTORY ("HelloWorldFactory",
                                      PANEL_TYPE_APPLET,
                                      hello_world_factory_callback,
                                      NULL)

Here are the changes compared to the simple example with no context menu:

- We define a list of GtkActionEntry entries: `hello_world_menu_actions`. This will be used later on to build GtkAction objects, with their label and callback. We obviously implement the callbacks.

- We change `hello_world_applet_start()` to define a GtkActionGroup object, to which we add, with `gtk_action_group_add_actions()`, GtkAction objects based on the GtkActionEntry entries. Note that the the last argument to `gtk_action_group_add_actions()` will be passed as user data to the callbacks.

- We also change `hello_world_applet_start()` to add this GtkActionGroup object to the context menu of the applet, by calling `panel_applet_setup_menu_from_file()`. This function takes as argument a path to the menu XML file that will define how to display the GtkAction objects in the context menu.

#### Menu XML File

The file that is used to setup menu with the `panel_applet_setup_menu_from_file()` function is a GtkUIManager UI definition file, without the top-level `<ui>` tag. It must only contain menuitem entries and separators. For example:

    <menuitem name="Hello World Prefs" action="HelloWorldPrefs" />
    <separator/>
    <menuitem name="Hello World Say"   action="HelloWorldSay" />

Alternatively, the `panel_applet_setup_menu()` function can be used with a string containing directly the XML.

### Settings

There are two main design patterns to store settings for an applet:

- Global settings: those are settings that should be shared among all instances of the same applets. For instance, the preferred unit for temperature is something that the user will want to set only one. The way to store those settings is in no way specific to the Panel Applet library, as it should work like in any other application.

- Per-applet instance settings: those are settings that might be different depending on the instance of an applet. For instance, an applet whose goal is to display a picture should make it possible for the user to choose a different picture for each instance of the applet. The Panel Applet library provides API to help with this.

There is actually a third case, which is rather rare, though: per-screen settings. It might be that some applets control some per-screen settings, like the layout of the workspaces. There is currently no API to help with this as it is a very specific case that you will probably never have to deal with for a usual applet.

#### Per Instance Settings

In order to store per instance settings for an applet it is recommended to use GSettings. The Panel Applet Library privides a function named `panel_applet_settings_new()` for this purpose. You need to provide a GSettings schema file for your applet and the Panel Applet library will setup the GSettings object for you.

TODO: describe settings-oriented API in more detail and give an example for per instance settings.

### Applet Registration, Installation and Build System

Simply installing the applet binary will obviously not be enough. The applet also needs to be registered to the panel. In order to do this you need to provide a Panel Applet File. Panel Applet files are ini-style text files with an extension named `.panel-applet`.

If you are creating an out-process applet, then you also need to provide a D-Bus service file to automatically start the binary when the panel wants to create an applet.

This chapter explains the syntax of `.panel-applet` files and gives the necessary information to setup the build system in order to install all the required parts of the applet on the system. For the build system part it is assumed that GNU Autotools (e.g. autoconf and automake) are used.

#### Panel Applet File Syntax

The `.panel-applet` file is a key file about the applet binary, describing the applet factory of the binary and the applet types this factory can create.

##### Panel Applet File Example

                [Applet Factory]
                Id=HelloWorldFactory
                Name=Hello World Applet Factory
                Description=Factory for the our example applets

                [HelloWorldApplet]
                Name=Hello World
                Description=Factory for the Hello World example applet
                Icon=hello-world-icon

##### File Format and well known Fields

The file must contain a `Applet Factory` group with the following keys:

- `Id` (string): the identifier of the applet factory. This must be the same name that will be used as the first parameter to `PANEL_APPLET_OUT_PROCESS_FACTORY()` or `PANEL_APPLET_IN_PROCESS_FACTORY()`.

- `InProcess` (boolean, optional): whether the applet should be in-process or out-of-process. By default, the applet is out-of-process.

- `Location` (string): the path to the applet binary. Only mandatory if `InProcess` is `true`.

- `Name` (localized string, optional): the name of the applet factory. For example: `Hello World Factory`.

- `Description` (localized string, optional): the description of the applet factory. For example: `Factory for the Hello World applet example`.

For each applet type, it must also contain a group named with the applet type identifier. Such a group must have the following keys:

- `Name` (localized string): the name of the applet type. For example: `Hello World`.

- `Description` (localized string, optional): the description of the applet type. For example: `Hello World applet example`.

- `Icon` (string, optional): the icon name of the applet type. For example: `hello-world-icon`. It can also be the path to an icon, but this not recommended.

##### Installation

The `.panel-applet` file must be installed in a specific directory to be discoverable by the panel. The exact path of this directory can be queried with `pkg-config`:

                      pkg-config --variable=libpanel_applet_dir libpanel-applet

The path depends on how GNOME Panel was compiled and installed so it could be different depending on which Linux distribution is used. Therefore never hard-code the path in your build system but use he above command instead. Please see the Build System section for more information on how install a Panel Applet file with autotools.

#### Out-of-Process vs In-Process

Applets can either live in their own process ("out-of-process") or in the panel process ("in-process"). The decision to choose one or the other is done at build time, with the macro that you use to define the applet factory: `PANEL_APPLET_OUT_PROCESS_FACTORY()` is used for out-of-process applets while `PANEL_APPLET_IN_PROCESS_FACTORY()` is used for in-process applets. Obviously, only one of those two macros can be used. If both types should be supported, then each macro must be placed between preprocessor conditions and switched on through a configure option. For example:

                           #ifdef MY_APPLET_OUT_PROCESS_ENABLED
                           PANEL_APPLET_OUT_PROCESS_FACTORY (...)
                           #else
                           PANEL_APPLET_IN_PROCESS_FACTORY (...)
                           #endif

For most practical matters, from the applet perspective, the two options are the same. In-process applets do offer a slightly better performance when the applet is loaded, but this should not have much effect on the user experience. However, an in-process applet can potentially affect the whole behavior of the panel, especially in case of crashes or memory corruptions: a crash in an in-process applet will crash the whole panel. It is therefore recommended to use out-of-process applets.

The communication between the panel and the applet factory is done over D-Bus. When creating an applet, the panel will send a message to the D-Bus service of the applet factory. If the D-Bus service is not running yet, it must be started automatically. We use D-Bus activation for this, which requires install a standard D-Bus service file. This is only needed for, because in-process applets do no need to have their binary autostarted for obvious reasons.

Please refer to the D-Bus documentation for more information about D-Bus service files.

Here is an example for a D-Bus Service file. It should be named `org.gnome.panel.applet.HelloWorldFactory.service.in`

                       [D-BUS Service]
                       Name=org.gnome.panel.applet.HelloWorldFactory
                       Exec=@LOCATION@

Additionally you should adjust the build system to replace `@LOCATION@` by the install location of the applet binary. The file itself must be installed into `$(datadir)/dbus-1/services/`.

The next section contains an explanation how to setup the Makefile in order to install the service file to the correct location on the system.

#### Build system

##### Configure

In `configure.ac` a variable should be defined that contains the location where the .panel-applet files are installed. You can fetch this directory during `configure` with the following code:

                           LIBPANEL_APPLET_DIR=`$PKG_CONFIG --variable=libpanel_applet_dir libpanel-applet`
                           AC_SUBST(LIBPANEL_APPLET_DIR)

##### Makefiles

A typical Makefile for that is used to build and install an applet contains the following declarations:

- A rule to install the `.panel-applet` file.

- A rule to install the D-Bus service file if the applet is an out process applet.

- The `.panel-applet` file and the D-Bus `.service` file are added to EXTRA_DIST and CLEANFILES.

###### Installing the Panel Applet File

                               appletdir       = $(LIBPANEL_APPLET_DIR)
                               applet_in_files = org.gnome.HelloWorld.panel-applet.in
                               applet_DATA     = $(applet_in_files:.panel-applet.in=.panel-applet)

                               $(applet_in_files): $(applet_in_files).in Makefile
                                   $(AM_V_GEN)sed \
                                   -e "s|\@LIBEXECDIR\@|$(libexecdir)|" \
                                   -e "s|\@VERSION\@|$(PACKAGE_VERSION)|" \
                                   $< > $@

                               %.panel-applet: %.panel-applet.in \
                                               $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*po) ; \
                                               $(INTLTOOL_MERGE) $(top_srcdir)/po $< $@ -d -u -c $(top_builddir)/po/.intltool-merge-cache

###### Installing the D-Bus service file

To install the service file the following rule can be placed in the applets Makefile:

                               servicedir       = $(datadir)/dbus-1/services
                               service_in_files = org.gnome.panel.applet.HelloWorldFactory.service.in
                               service_DATA     = $(service_in_files:.service.in=.service)

                               org.gnome.panel.applet.HelloWorldFactory.service: $(service_in_files)
                                   $(AM_V_GEN) sed -e "s|\@LOCATION\@|$(APPLET_LOCATION)|" $< > $@

###### Distribution and Cleanup

Add panel-applet .in.in and service file to EXTRA_DIST and $(applet_DATA) $(applet_DATA).in to CLEANFILES

                           EXTRA_DIST = \
                               GNOME_MixerApplet.panel-applet.in.in \
                               $(service_in_files) \
                               $(ui_DATA) \
                               $(schemas_in_files)

                           CLEANFILES = $(applet_DATA) $(applet_DATA).in $(service_DATA) $(schemas_DATA)

### Proper Integration with the Panel

Since the applets appear as part of the panel to users, it is important that they behave in a consistent way. A few steps can be completed to achieve proper integration.

#### Panel Background

The panel can have different types of background, depending on how the user configures the panel. By default, applets do not respect the background that is configured and can therefore look out of place.

The `panel_applet_set_background_widget()` function can be used to automatically have the right background drawn for a specific widget. Just using this function on the PanelApplet object itself, or its child is usually enough to have proper background integration.

In some rare cases, though, `panel_applet_set_background_widget()` will not be enough. The solution is then to connect to the `"change-background"` signal of the PanelApplet object: it will be emitted when the background has changed, and it will provide the cairo_pattern_t pattern to use as a basis to draw the background.

#### Panel Lockdown

The panel has proper support for lockdown, and when it is locked down, it is expected that all applets behave consistently in a lockdown mode too. This generally means that the preferences of the applet should not be accessible, but it could also imply a restriction on the behavior of the applet.

The `panel_applet_get_locked_down()` function can be used to query the state of the panel lockdown. It is also possible to react to changes by monitoring the `"locked-down"` property of the PanelApplet object. You can achieve this by connecting to the `"notify::locked-down"` event.

In most cases, the GBinding API is enough to respect the panel lockdown: `g_object_bind_property()` can be used to automatically update the visiblity of a menu item in the context menu of the applet. In the following example, the `"HelloWorldPrefs"` action (which is an action from the context menu) will only be displayed if the panel is not locked down.

    action = gtk_action_group_get_action (action_group, "HelloWorldPrefs");
    g_object_bind_property (applet, "locked-down",
                            action, "visible",
                            G_BINDING_DEFAULT|G_BINDING_INVERT_BOOLEAN|G_BINDING_SYNC_CREATE);

Of course it is also possible to use `g_object_bind_property()` to change the visibility of widgets that appear outside of the context menu, like a button in a window.

### Writing an applet in languages other than C

The Panel Applet library comes with support for GObject Introspection. This makes it possible to write applets in the languages that have bindings based on GObject Introspection.

Here is a simple example of a python applet:

Hello World applet, in Python

    from gi.repository import Gtk
    from gi.repository import PanelApplet

    def applet_fill(applet):
        label = Gtk.Label("Hello World")
        applet.add(label)
        applet.show_all()

    def applet_factory(applet, iid, data):
        if iid != "HelloWorldApplet":
           return False

        applet_fill(applet)

        return True

    PanelApplet.Applet.factory_main("HelloWorldFactory",
                                    PanelApplet.Applet.__gtype__,
                                    applet_factory, None)

The only limitation of writing an applet in a language other than C is that the applet will not be able to run in the panel process: it will have to stay out-of-process. However, since it is recommended to leave applets out-of-process, this limitation is mitigated.

## Porting older Applets

### Porting to a D-Bus-based Applet

#### Introduction

Applets that were written for Gnome 2.x must we ported to be usable with Gnome 3 and the latest Panel Applet Library. First of all if the applet was written for Gnome 2.x then it will still use GTK+ 2 and must be migrated to GTK+ 3. Please refer to the GTK+ 2 to GTK+ 3 migration guide for further details.

Once the GTK+ 2 to 3 migration has been completed you must migrate the Panel Applet Library specific parts of the applet. During Gnome 2 times, the Panel Applet Library used LibBonobo, which was replaced by a DBUS based API during the switch to Panel Applet Library version 4.0. During the change to version 5.0 additional API have been replaced in particular the GConf related functions have been removed and were replaced by the GSettings API. Also GtkAction related functions have been replaced by the GAction API.

The following migration guide is based on the AppletsDBUsMigration guide in the GNOME Wiki. This guide was originally written for the migration of GNOME 2 applets to GNOME 3 and Panel Applet Library version 4.0. Based on the information of the original guide this section improves some of the explanations, adds references to other parts of this manual and has been updated for the migration to version 5.0 of this library.

#### Removing LibBonobo factory

To migrate from LibBonobo to the DBUS API remove PANEL_APPLET_BONOBO_FACTORY and replace it with either PANEL_APPLET_OUT_PROCESS_FACTORY or PANEL_APPLET_IN_PROCESS_FACTORY depending if you want an out-process or in-process applet, respectively.

The new macros have the same parameters as the old, except the version and description parameters have been removed. Also the `OAFIID:GNOME_` prefix of the first parameter can be dropped both from the factory ID and the applet type name. The following two code listings show the difference between the old and new macro definitions:

                        PANEL_APPLET_BONOBO_FACTORY ("OAFIID:GNOME_HelloWorldFactory",
                                                     PANEL_TYPE_APPLET,
                                                     "WindowNavigationApplets",
                                                     "0",
                                                     hello_world_factory,
                                                     NULL)

The new version of the macro should be:

                        PANEL_APPLET_OUT_PROCESS_FACTORY ("HelloWorldFactory",
                                                          PANEL_TYPE_APPLET,
                                                          hello_world_factory,
                                                          NULL)

The applet factory callback, in this case the `hello_world_factory` usually contains a check to ensure that the applet factory supports the requested applet. Here the `OAFIID:GNOME_` prefix should be dropped as well, see the following two code listings.

When you drop the prefix here, you must also remove the prefix from the applet name in the Panel Applet File.

Old definition:

                        static gboolean
                        hello_world_factory (PanelApplet *applet,
                        const char  *iid,
                        gpointer     data)
                        {

                        if (!g_strcmp (iid, "OAFIID:GNOME_HelloWorldApplet") != 0)
                        return FALSE;

                        hello_world_applet_fill (applet);

                        return TRUE;
                        }

The new version of of the factory callback should be:

                        static gboolean
                        hello_world_factory (PanelApplet *applet,
                        const char  *iid,
                        gpointer     data)
                        {

                        if (!g_strcmp0 (iid, "HelloWorldApplet") != 0)
                        return FALSE;

                        hello_world_applet_fill (applet);

                        return TRUE;
                        }

##### Migrating Service file to Panel Applet File

The old applet will have a `service.in.in` file that was used to register the applet with the GNOME Panel. This file must be renamed to `.panel-applet` and its content changed to match the format of the new file type. See Panel Applet Files for a detailed explanation of the valid fields. If you decided to use the out-process factory macro, then you should also supply a D-BUS Service File so your applet can be started using D-BUS activation.

For compatibility you can keep the old `BonoboId`. This field can contain the old bonobo identifier. It allows the panel to load applets using existing configuration data in your panels settings. It can be a single string or a list of strings in case of applets with more than one identifier.

Example for the migrated panel applet file:

                            [Applet Factory]
                            Id=HellWorldFactory
                            Location=@LOCATION@
                            _Name=Hello World Applet Factory
                            _Description=Factory for the our example applets

                            [HelloWorldApplet]
                            _Name=Hello World
                            _Description=Factory for the Hello World example applet
                            Icon=hello-world-icon
                            BonoboId=OAFIID:GNOME_HelloWorldApplet
                            X-GNOME-Bugzilla-Bugzilla=GNOME
                            X-GNOME-Bugzilla-Product=gnome-panel
                            X-GNOME-Bugzilla-Component=hello-world-applet
                            X-GNOME-Bugzilla-Version=@VERSION@
                            X-GNOME-Bugzilla-OtherBinaries=hello-world-applet

#### Migrating LibBonobo menus to GAction

During GNOME 2 days the context menu of the applet was based on the `BonoboUiVerb` class. These old menu verb definitions have been removed and should be replaced by the GAction interface family of objects.

Previous versions of this migration guide suggested to migrate to the family of GtkActionGroup classes. Since Gtk+ 3.10 these are deprecated and with the change of the Panel Applet Library API to version 5.0 the `panel_applet_setup_menu` family of functions now accept the GActionGroup interface and its related classes.

If the applet that is being ports had a context menu then it should contain code that looks similar to the following listing:

                        static const BonoboUIVerb show_desktop_menu_verbs [] = {
                            BONOBO_UI_UNSAFE_VERB ("ShowDesktopPreferences", display_preferences_dialog),
                            BONOBO_UI_UNSAFE_VERB ("ShowDesktopAbout",       display_about_dialog),
                            BONOBO_UI_VERB_END
                        };

The new code instead looks like this:

                        static const GActionEntry menu_actions[] = {
                            { "preferences", display_preferences_dialog },
                            { "about",       display_about_dialog }
                        };

This change also means that the callbacks must be changed to match the GAction API: Old callback looked like this:

                        static void
                        display_preferences_dialog (BonoboUIComponent *uic,
                                                    gpointer          *user_data,
                                                    const gchar       *verbname)

The new version should have the following signature:

                        static void
                        display_preferences_dialog (GSimpleAction *action,
                                                    GVariant      *parameter,
                                                    gpointer       user_data)

Additionally you should use GtkBuilder to define a GMenuModel in XML and use the `panel_applet_setup_menu` family of functions to setup the menu. See Using a context menu for more details. For more information about using the GMenu and GAction APIs please refer to the GIO Reference Manual.

### Porting Applets from Panel Applet Library 4.0

The Panel Applet Library name has been renamed during the switch from version 4.0 to 5.0 and is now called `libpanel-applet`.

Note that the version information in the library name has been dropped and there is now an additional hypen between 'panel' and 'applet'.

GConf support has been removed and was replaced by GSettings. If the applet still uses GConf it must migrate to GSettings. It is recommended to use the `panel_applet_settings_new()` function to store your settings.

The `panel_applet_setup_menu` familiy of functions have been changed and now accept the GActionGroup interface instead of the deprecated GtkActionGroup. Thus older applets need to be changed and must migrate their menus to the GAction model. Please refer to the Gtk+ documentation for more details about GAction and the related interfaces.

## API Reference

## Object Hierarchy

API Index

Index of deprecated API

Index of new symbols in 3.4

Index of new symbols in 3.18

Index of new symbols in 3.22