Add methods of Gtk::ExText

The methods of objects can be defined by using DEFL or raw XS. Since XS is very easy to use and much more powerful than DEFL, we will use XS directly and ignore the define-func of DEFL.

Usually, the only thing you must do is to put the prototype of methods in XS file. For example, in our Gtk::ExText module, we can add a get_extext_set_point by put the following codes in GtkExText.xs.

void
gtk_extext_set_point(text, index)
        Gtk::ExText     text
        int     index

You must use exactly the same format as above example in your XS codes. The first line is the return type, the second line is the method name and a list of its atguments, and the rest lines is definitions of arguments. You must seperate the definitions of two methods by a empty line.

I don't intend to describe the details of XS programming here. You can learn more from the man pages perlguts, perlxs and perlxstut about it. You can find more details in some of Perl books like "Advanced Perl Programming" of Sriram Srinivasan.

After recompiling this package, you can use the following program to test it.

use Gtk;
use Gtk::ExText;

Gtk->init;
Gtk::ExText->init;

$w = new Gtk::Window;
$e = new Gtk::ExText;
$w->add($e);
$e->insert("asdasdasdasdasd');
$e->set_pointer(5);
$e->show;
$w->show;
Gtk->main;

Add typemap

Perl-Gtk has already known how to deal with arguments or return values of a lot of types like Gtk::Window, Gtk::Text and etc. However, it doesn't know what is Gtk::ExTextProperty yet. Therefore, we can not use the prototypes described in the previous section to define function get_extext_property_remove since the prototype of it is

Gtk::ExTextProperty
gtk_extext_property_remove(text,remove)
        Gtk::ExText     text
        Gtk::ExTextProperty     remove

We must tell perl how to translate the objects, whose type are 'Gtk::ExTextProperty', between C and perl. In the language of perl, this mapping is called 'typemap'.

The typemap can be defined in DEFL. You can use one of define-enum, define-flags, define-boxed, define-struct, define-pointer and define-object according to its C type.

We will use define-boxed to define Gtk::ExTextProperty since it is quite complex. Please add the following codes into your pkg.defs.

(define-boxed GtkExTextProperty 
       "(void)"
       "(void)")

and the following codes in GtkExText.xs

MODULE = Gtk::ExTextProperty		PACKAGE = Gtk::ExTextProperty		PREFIX = gtk_extext_

int
startpos(self,new_v=0)
    Gtk::ExTextProperty self
    int new_v
    CODE:
    {
	RETVAL = self->startpos;
	if (items>1) self->startpos = new_v;
    }
    OUTPUT:
    RETVAL

int
endpos(self,new_v=0)
    Gtk::ExTextProperty self
    int new_v
    CODE:
    {
	RETVAL = self->endpos;
	if (items>1) self->endpos = new_v;
    }
    OUTPUT:
    RETVAL

Gtk::ExTextStyle
style(self,new_style=0)
    Gtk::ExTextProperty	self
    Gtk::ExTextStyle	new_style
    CODE:
    {
	RETVAL = self->style;
	if (items>1) self->style = new_style;
    }
    OUTPUT:
    RETVAL

void *
user_data(self)
    Gtk::ExTextProperty	self
    CODE:
    {
	RETVAL = self->user_data;
    }
    OUTPUT:
    RETVAL


You can add other methods by the same way.