.\" Automatically generated by Pod::Man 4.10 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Gtk2::devel 3pm" .TH Gtk2::devel 3pm "2019-09-16" "perl v5.28.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Gtk2::devel \- The internal workings of the gtk2\-perl language bindings .SH "DESCRIPTION" .IX Header "DESCRIPTION" This document is a supplement to Glib::devel, and assumes you have read and understood all about how the base Glib bindings work. Most of this will seem like nonsense, otherwise. .PP Here we focus on the ways in which Gtk2 extends Glib's concepts for binding the Gtk+ C libraries to perl, a methodology and set of tools you can use to wrap your own GObject-based libraries. .SH "GtkObject" .IX Header "GtkObject" GtkObject adds the idea of a floating reference to GObject. A GObject is created with one reference which must be explicitly removed by its owner. GtkObject has a floating reference which is sunk by the code which wants to own it. This makes it less painful to create lots of objects in a row (you don't have to unref them). .PP To allow for this difference in procedure for taking ownership of an object, Glib allows you to register a \*(L"sink\*(R" function for a particular class. When asked to create a wrapper that owns the object, gperl_new_object will compare the list of registered sink functions with the type of the object; if the object is descended from a type, that sink function will be run on the object. The default one is \fBg_object_unref()\fR, of course. (this is inspired by pygtk.) .PP Thus, in Gtk2::Object's boot code, we register gtk_object_sink as the sink func for types derived from GtkObject. Now all wrappers for these types will be owned the proper way. .PP Of course, since \fBgtk_object_sink()\fR does nothing if the object isn't floating, it doesn't hurt anything if you always call gperl_new_object with \*(L"own\*(R" set to \&\s-1TRUE.\s0 So, to make life a little easier, Gtk2 defines another function .PP .Vb 1 \& SV * gtk2perl_new_gtkobject (GtkObject * o); .Ve .PP Which does nothing more than .PP .Vb 3 \& { \& return gperl_new_object (G_OBJECT (o), TRUE); \& } .Ve .PP It's also important to know that this is largely done for you by the typemap. .SH "Typemap scheme" .IX Header "Typemap scheme" In the same way that the Glib module uses explicit one-to-one GType to package registrations, it is most foolproof to use an explicit, even exhaustive \s-1XS\s0 typemap. In this way we avoid problems such as finding the proper set of regexes to map \f(CW$var\fR to the type macro and all sort of other problems of extensibility. This of course means it must be autogenerated, but that's easy (and is described in the next section). .PP The other main feature of the typemap is that it masks in a very sensible way the differences between GObject and GtkObject, and makes it very easy to specify whether a wrapper owns the object it wraps. This is handled through the idea of a \*(L"variant\*(R", which is a term I made up just now because it sounds about right. .PP Basically, a variant is the name of the class with some suffix. For example, for the a GBoxed subclass such as GdkEvent, a header would do this: .PP .Vb 2 \& typedef GdkEvent GdkEvent_ornull; \& typedef GdkEvent GdkEvent_own; \& \& #define SvGdkEvent(s) (gperl_get_boxed_check ((s), GDK_TYPE_EVENT)) \& #define SvGdkEvent_ornull(s) ((s)==&PL_sv_undef ? NULL : SvGdkEvent(s)) \& \& #define newSVGdkEvent(e) (gperl_new_boxed ((e), GDK_TYPE_EVENT, FALSE)) \& #define newSVGdkEvent_own(e) (gperl_new_boxed ((e), GDK_TYPE_EVENT, TRUE)) \& #define newSVGdkEvent_ornull(e) (e == NULL ? &PL_sv_undef ? newSVGdkEvent (e)) .Ve .PP Then the typemap entries for its various variants would look like this: .PP .Vb 4 \& TYPEMAP \& GdkEvent * T_GDK_TYPE_EVENT \& GdkEvent_ornull * T_GDK_TYPE_EVENT_ORNULL \& GdkEvent_own * T_GDK_TYPE_EVENT_OWN \& \& INPUT \& T_GDK_TYPE_EVENT \& $var = SvGdkEvent ($arg); \& T_GDK_TYPE_EVENT_ORNULL \& $var = SvGdkEvent_ornull ($arg); \& \& OUTPUT \& T_GDK_TYPE_EVENT \& $arg = newSVGdkEvent ($var); \& T_GDK_TYPE_EVENT_ORNULL \& $arg = newSVGdkEvent_ornull ($var); \& T_GDK_TYPE_EVENT_OWN \& $arg = newSVGdkEvent_own ($var); .Ve .PP And with that, your \s-1XS\s0 wrapper code can look as simple as this: .PP .Vb 5 \& GdkEvent_own * \& gdk_get_event (class) \& SV * class \& C_ARGS: \& /*void*/ \& \& guint \& gdk_event_get_time (event) \& GdkEvent * event .Ve .PP Isn't that nice and simple? .PP We have different variants for different types, and some are applicable only to input or output. The ones used by gtk2\-perl generally follow the convention outlined in this table: .PP .Vb 8 \& Variant I O Description \& \-\-\-\-\-\-\-\-\-\-\-\- \- \- \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& GBoxed \& /* no ext */ * * object will not be destroyed with wrapper \& _own * object will be destroyed with wrapper \& _copy * object will be copied (and copy will be owned) \& _ornull * * undef/NULL is legal \& _own_ornull * if object not NULL, wrapper will own object \& \& GObject \& /* no ext */ * * object\*(Aqs refcount will be increased (=>not owned) \& _noinc * object\*(Aqs refcount will not be increased (=>owned) \& _ornull * * undef/NULL is legal \& \& GtkObject \& /* no ext */ * * everything is peachy \& _ornull * * undef/NULL is legal .Ve .SH "Autogeneration" .IX Header "Autogeneration" The typemap scheme described above is great, but involves creating a lot of typedefs and macros. For a large library like Gtk, with over three hundred types to bind, you'd have to be crazy to write all of those by hand. .PP Gtk2 handles this by using a code generation module to write the code for us as part of the Makefile.PL configuration step. See Gtk2::CodeGen for details on how to use the generators. Here I'll describe what gets generated and why. .SS "maps" .IX Subsection "maps" This is the starting point for autogeneration, the input for the code generator. This map lists the \s-1TYPE\s0 macro for each of the GObject types in all of the gtk headers (including gdk, gdk-pixbuf, atk, and pango), along with the actual name of the class, name of the package into which it is to be blessed, and the base type (not exactly the fundamental type). Most of those should be obvious except for the base type. The base type is one of GEnum, GFlags, GBoxed, GObject, GInterface, or GtkObject. This is the important flag which determines what kind of code gets created for each record; GObjects must be handled by completely different code than GBoxed objects, for instance. As noted elsewhere, the distinction between GObject and GtkObject is not strictly necessary, but is kept for historical and aesthetic reasons. .PP In this file, you can change the explicit name of an object. If you don't like PangoFontDescription being Gtk2::Pango::FontDescription, you can change it to Gtk2::Pango::Font::Desc::ription if you were so inclined (but please don't). .PP If you wish to use Gtk2's autogeneration tools in your own project, you'll need to create a maps file. This can be done by hand, but that's tedious and error-prone; I used a script (called genmaps.pl in \s-1CVS\s0) that actually scans the gtk header files and creates and runs a small program to generate the maps file. The advantage here is that the type information comes directly from the code and I don't have to worry about clerical errors making the software incorrect. In practice, this should need to be run only when new classes are added to the base libraries. .SS "gtk2perl\-autogen.h" .IX Subsection "gtk2perl-autogen.h" This file contains the generated typedefs and cast macros. This includes all the variant stuff described above. .SS "gtk2perl.typemap" .IX Subsection "gtk2perl.typemap" The exhaustive typemap uses the macros defined in gtk2perl\-autogen.h so that you are assured to get the same results from typemap generated code as from hand-written perl stack manipulation. .SS "register.xsh" .IX Subsection "register.xsh" Included from the \s-1BOOT\s0 section of the toplevel Gtk2 module (xs/Gtk2.xs), this file lists all of the types in the maps file as a series of calls to the appropriate package registration functions (gperl_register_boxed, gperl_register_object, or gperl_register_fundamental). This is done before the boot code below so that hand-written code may override it. This code gets executed when your perl script does a \*(L"use Gtk2\*(R". .SS "boot.xsh" .IX Subsection "boot.xsh" The Gtk2 module is made up of dozens of \s-1XS\s0 files but only one \s-1PM\s0 file. Gtk2.pm calls bootstrap on Gtk2, but not on any of the others (because it doesn't know about them). It is a module's boot code which binds the xsubs into perl, so it's imperative that the modules get booted! .PP So, \f(CW\*(C`Gtk2::CodeGen\->write_boot\*(C'\fR (called from Makefile.PL) scans the xs/ subdirectory for all the \*(L"\s-1MODULE\s0 = ...\*(R" lines in the \s-1XS\s0 files. It maps these to boot code symbols, and generates code to call these symbols in boot.xsh, which is then included by the \s-1BOOT:\s0 section for the toplevel module, right after register.xsh. (The generation code takes steps to avoid spitting out the same symbol more than once, and will not emit code to boot the toplevel module (or else you get an infinite loop).) .PP Just a point of style; you can change packages in an \s-1XS\s0 file by repeating the \&\s-1MODULE\s0 = ... line with a different \s-1PACKAGE\s0 (and possibly \s-1PREFIX\s0) value. It's a good idea, however, to keep the \s-1MODULE\s0 the same, so that only one boot symbol gets generated per file. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fBperl\fR\|(1), \fBperlxs\fR\|(1), \fBGtk2\fR\|(3pm), \fBGlib\fR\|(3pm), \fBGlib::devel\fR\|(3pm), \&\fBGlib::xsapi\fR\|(3pm), \fBGtk2::CodeGen\fR\|(3pm) .SH "AUTHOR" .IX Header "AUTHOR" muppet .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright (C) 2003 by the gtk2\-perl team (see the file \s-1AUTHORS\s0 for the full list) .PP This library is free software; you can redistribute it and/or modify it under the terms of the \s-1GNU\s0 Library General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. .PP This library is distributed in the hope that it will be useful, but \s-1WITHOUT ANY WARRANTY\s0; without even the implied warranty of \s-1MERCHANTABILITY\s0 or \s-1FITNESS FOR A PARTICULAR PURPOSE.\s0 See the \s-1GNU\s0 Library General Public License for more details. .PP You should have received a copy of the \s-1GNU\s0 Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, \s-1MA\s0 02110\-1301 \s-1USA.\s0