Writing plugins in Vala

Writing the plugin code
Writing a plugin description
Compiling and installing the plugin
Manual compilation
Using autotools

Before writing an Anjuta plugin, please read Anjuta Architecture for general concepts about how Anjuta plugins interact with Anjuta Shell and other plugins.

Anjuta API is divided in two namespaces : Anjuta and IAnjuta. Anjuta contains API to interact with the IDE, and IAnjuta contains interfaces that plugins implement to be able to interact with each other. Writing a plugin for Anjuta is as simple as subclassing Anjuta.Plugin, overriding activate and deactivate, and writing a .plugin file to let Anjuta know about your plugin.

This tutorial is still uncomplete. However most of the documentation regarding C plugins is still applicable, so you can always refer to Writing plugins.

In this tutorial, we will write a basic Hello world plugin that does nothing but adding a "Hello World" label to Anjuta.

Writing the plugin code

As said earlier, we will need to subclass Anjuta.Plugin and override some methods, so here is Anjuta.Plugin :

public class Anjuta.Plugin : GLib.Object {
        public uint add_watch (string name, Anjuta.PluginValueAdded added, Anjuta.PluginValueRemoved removed);
        public bool is_active ();
        public void remove_watch (uint id, bool send_remove);
        public virtual bool activate ();
        public virtual bool deactivate ();
        public weak Anjuta.Shell shell { get; set; }
        public signal void activated ();
        public signal void deactivated ();
}
      

The methods we'll need to override are activate and deactivate, the shell property is what we'll use to interact with Anjuta. add_watch and remove_watch are used to interface with the Values System see Anjuta Architecture for more information. The rest is pretty self explanatory, and we generally won't need it.

We will only use the add_widget method of Anjuta.Shell in this tutorial. For more informations, you can see the C API documentation.

This plugin does nothing more than showing a "Hello World" widget in Anjuta

using Gtk;
using Anjuta;

public class HelloWorldPlugin: Plugin {
    /* The hello world widget */
    Widget widget;

    /* We use the "override" keyword to override the virtual methods activate and deactivate */
    public override bool activate () {
        widget = new Label("Hello World");

        /* adding the widget */
        shell.add_widget(widget, /* the widget to add */
                         "AnjutaHelloWorldPlugin", /* name of the widget*/
                         "HelloWorldPlugin", /* title, should be translated */
                         Gtk.STOCK_ABOUT, /* icon stock id */
                         ShellPlacement.CENTER); /* placement in in the shell */
        return true; /* false if activation failed */
    }

    public override bool deactivate () {
        /* remove the widget we've added */
        shell.remove_widget(widget);

        return true; /* false if plugin doesn't want to deactivate */
    }
}

/* Initialization function, in C this would be automatically generated by the
   ANJUTA_SIMPLE_PLUGIN macro */
[ModuleInit]
public GLib.Type anjuta_glue_register_components (GLib.TypeModule module) {
    return typeof (HelloWorldPlugin);
}