Writing Anjuta plugins in Vala

Writing Anjuta plugins in Vala

17:50

Thursday, June 5. 2008

Update : this is now bug 538021
Since I had a break from my exams (next is on monday), I wrote a little tutorial about writing Anjuta plugins in Vala, it is still incomplete.
I'm posting it here before sending a patch to have more visibility and hopefully someone can proofread it. I'm not really confident about it's quality (and this is the first time I'm writing a software documentation).
Enough speaking, Here you go :

Writing plugins in Vala

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);
}

Writing a plugin description

Anjuta needs a file with a plugin extension to know about our plugin, its name and description, where it is located, when to load it. Here is an example for our basic plugin :

[Anjuta Plugin]
Name=Hello World
Description=An example hello world plugin.
Location=anjuta-hello-world:HelloWorldPlugin
Icon=anjuta-sample-plugin-48.png

Generally, Name and Description should be translated. Using intltool, you can do this by prefixing them with _. Location is of the form libraryname:ClassName : libraryname is the name of the library file (without the lib prefix and .so suffix), and ClassName is the full class name including the namespace (and not separated by a period as you would write it in Vala). Icon is the icon to be used by the plugin, here we put sample plugin icon which is distributed along with Anjuta, but you could put your own icon instead. For more information about plugin description files, refer to Plugin description file.

Compiling and installing the plugin

Manual compilation

Assuming you saved the plugin as hello-plugin.vala, you can compile it with :
valac --ccode --pkg libanjuta-1.0 hello-plugin.vala
gcc -shared -o libanjuta-hello-world.so hello-plugin.c
      

Using autotools

Coming soon.


You can get the vapi (and deps) for Anjuta from my mercurial repository (direct links : vapi, deps), add --vapidir /path/to/libanjuta-1.0.vapi to valac command line.
Posted by Abderrahim Kitouni in GNOME | Comments (3) | Trackbacks (0)
Defined tags for this entry: anjuta, vala
Related entries by tags:
anjuta-vala new home
Some news
Anjuta-valaplugin 0.3
Vala plugin news
GDB Vala support

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

H.B.Tennakoonhow should I name,
public class Anjuta.Plugin : GLib.Object{ and
public class HelloWorldPlugin: Plugin { classes?
And how and where should I save the description file ?
#1 H.B.Tennakoon (Homepage) on 2009-11-06 03:12 (Reply)
emuVotre écriture est très élégant, très vivante et animée, j'ai vraiment comme vous, vous souhaite de continuer à écrire des articles de meilleure qualité, je vais souvent essayer de préoccupation, oh!
#2 emu (Homepage) on 2010-02-21 06:18 (Reply)
Scott DeaganNice! Thanks for taking the time to share this. I'll try it out when I get home...
#3 Scott Deagan (Homepage) on 2010-02-24 11:15 (Reply)

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 

Layout by Ricky Wilson | Serendipity Template by Carl Galloway | Login


Read More

Calendar

« March '10 »
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

Quicksearch

Archives

March 2010
February 2010
January 2010
Recent...
Older...

Categories

  • XML english
  • XML GNOME


All categories
XML RSS 2.0 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Comments

Powered by

Serendipity PHP Weblog

Nuage de tags

xml anjuta
xml ctags
xml gdb
xml git
xml gnome
xml hg-git
xml mercurial
xml vala

Template dropdown