Examples Using Deprecated Libraries

These examples use deprecated libraries or technologies that are no longer actively developed by upstream. It is not recommended to base new code on them. These examples might be less checked than the rest or do no longer compile.

GNOME 2 Panel Applet

Note: The new user interface of Gnome as well as Unity don't support panel applets anymore. If you want to modify the new Gnome interface then you should have a look at Gnome Shell's extension system. Also, this example uses Bonobo, which is highly deprecated in favor of D-Bus.

panel.vala

using Panel;

public class MainApplet : GLib.Object {

    public static bool panel_factory (Applet applet, string iid) {
        var button = new Gtk.Button.with_label ("Vala Panel");
        applet.add (button);
        applet.show_all ();
        return false;
    }

    public static int main (string[] args) {
        var program = Gnome.Program.init ("Vala_Applet", "0", Gnome.libgnomeui_module,
                                          args, "sm-connect", false);
        return Applet.factory_main ("OAFIID:Vala_Applet_Factory",
                                    typeof (Panel.Applet),
                                    MainApplet.panel_factory);
    }
}

$ valac --pkg gtk+-2.0 --pkg libpanelapplet-2.0 panel.vala

panel.server

Bonobo server file "panel.server" belongs into "/usr/lib/bonobo/servers/".

Don't forget to change "/path/to/panel" in the panel.server file with the location of panel executable.

<oaf_info>
    <oaf_server iid="OAFIID:Vala_Applet_Factory" type="exe" location="/home/frederik/src/vala/samples/panel">
        <oaf_attribute name="repo_ids" type="stringv">
            <item value="IDL:Bonobo/GenericFactory:1.0"/>
            <item value="IDL:Bonobo/Unknown:1.0"/>
        </oaf_attribute>
        <oaf_attribute name="name" type="string" value="Vala Panel Example"/>
        <oaf_attribute name="description" type="string" value="Vala Panel Example"/>
    </oaf_server>

    <oaf_server iid="OAFIID:Vala_Applet" type="factory" location="OAFIID:Vala_Applet_Factory">
        <oaf_attribute name="repo_ids" type="stringv">
            <item value="IDL:GNOME/Vertigo/PanelAppletShell:1.0"/>
            <item value="IDL:Bonobo/Control:1.0"/>
            <item value="IDL:Bonobo/Unknown:1.0"/>
        </oaf_attribute>
        <oaf_attribute name="name" type="string" value="Vala Panel Example"/>
        <oaf_attribute name="description" type="string" value="Vala Panel Example"/>
        <oaf_attribute name="panel:category" type="string" value="Utility"/>
        <oaf_attribute name="panel:icon" type="string" value="computer.png"/>
    </oaf_server>

</oaf_info>

Test

$ panel-test-applets --iid "OAFIID:Vala_Applet"

Improved Panel Applet Example

Shows:

  • How to add "right click" applet menu with "About".
  • How to make applet with transparent background.

using Panel;

public class MainApplet : Panel.Applet {

    public static bool factory (Applet applet, string iid) {
        ((MainApplet) applet).create ();
        return true;
    }

    private void on_change_background (Panel.Applet applet, Panel.AppletBackgroundType type,
                                       Gdk.Color? color, Gdk.Pixmap? pixmap) {
        applet.set_style (null);

        var rc_style = new Gtk.RcStyle ();
        applet.modify_style (rc_style);

        switch (type) {
        case Panel.AppletBackgroundType.COLOR_BACKGROUND:
            applet.modify_bg (Gtk.StateType.NORMAL, color);
            break;
        case Panel.AppletBackgroundType.PIXMAP_BACKGROUND:
            applet.style.bg_pixmap[0] = pixmap;
            set_style (style);
            break;
        }
    }

    private void create () {
        change_background.connect (on_change_background);

        var label = new Gtk.Label ("Vala Panel");
        add (label);

        string menu_definition = 
            "<popup name=\"button3\">" +
                "<menuitem debuname=\"About\" verb=\"About\" _label=\"_About...\" pixtype=\"stock\" pixname=\"gnome-stock-about\"/>" +
            "</popup>";

        var verb = BonoboUI.Verb ();
        verb.cname = "About";
        verb.cb = on_about_clicked;

        var verbs = new BonoboUI.Verb[] { verb };
        setup_menu (menu_definition, verbs, null);

        show_all();
    }
        
    private static void on_about_clicked (BonoboUI.Component component,
                                          void* user_data, string cname) {
        var dialog = new Gtk.MessageDialog (
            null,
            Gtk.DialogFlags.DESTROY_WITH_PARENT,
            Gtk.MessageType.ERROR,
            Gtk.ButtonsType.CLOSE,
            "About");
        dialog.secondary_text = "About dialog";
        dialog.run ();
        dialog.destroy ();
    }

    public static int main (string[] args) {
        var program = Gnome.Program.init ("Vala_Applet", "0", Gnome.libgnomeui_module,
                                          args, "sm-connect", false);
        return Applet.factory_main ("OAFIID:Vala_Applet_Factory",
                                    typeof (MainApplet), factory);
    }
}

panel.server, compile and test as above.

Projects/Vala/DeprecatedSamples (last edited 2013-11-22 16:48:30 by WilliamJonMcCann)