Merge remote-tracking branch 'andy128k/master'
[cl-gtk2.git] / doc / gobject.ref.texi
blob18f648a31c08a64735c8f06dee899a1b15e1266a
2 @menu
3 * Introduction::
4 * GType designator::
5 * Type hierarchy and type relations::
6 * Object types information::
7 * Enum types information::
8 * Using GValues::
9 * Stable pointers::
10 * Closures::
11 * GObject low-level::
12 * GObject high-level::
13 * Creating GObjects classes and implementing GInterfaces::
14 * GBoxed::
15 * Generating type definitions by introspection::
16 @end menu
18 @node Introduction
19 @chapter Introduction
21 GObject is a part of GLib library that implements the type system. The CL-GTK2-GObject is a Common Lisp binding for relevant parts of GObject.
23 The purpose of CL-GTK2-GObject is to ease the creation of binding for libraries based on GObject.
25 Please bear in mind that this is the documentation for a work-in-progress library and is a snapshot of current situation. API and functionality may (and will) change. Largely unfinished parts are working with GBoxed types, subclassing GObjects and implementing GInterfaces.
27 CL-GTK2-GObject is logically split into several layers:
28 @itemize
29 @item FFI code. FFI (foreign functions interface) layer is a glue between Lisp code and @code{libglib}, @code{libgobject}, @code{libgthread}. This code includes basic wrapper around GType designator (it is used everywhere and should be defined first) and definitions of foreign structures and imports foreign functions.
30 @item Low-level GObject integration. These are facilities provided by GObject that capture specific aspects of type system, object system and cross-language runtime. This includes types information, GValues (generic containers for value of any type supported by GObject type system), closures, means to create and use objects. This layer also includes some non-GObject facilities: stable pointers.
31 @item High-level GObject integration. This layer includes support for interoperability between CLOS and GObject and automatic generation of corresponding definitions.
32 @end itemize
34 Naturally, users of CL-GTK2-GObject should use the high-level GObject integration, but occasionaly it may be necessary to use lower-level functionality.
36 @node GType designator
37 @chapter GType designator
39 @menu
40 * gtype-id::
41 * gtype-name::
42 * gtype::
43 @end menu
45 GObject is an object system based on GType type system. Types in it are identified by an integer value of type @code{GType}. In @code{cl-gtk2-gobject}, types are identified by GType designator structures. GType designator is a structure identifying a particular GType; it contains its name and integer value.
47 GType designators are singleton structures meaning that for every GType, there may be only one GType designator structure for it; this means that two GType designators are equal (with respect to the @code{COMMON-LISP:EQ} function) if and only if their corresponding GTypes are the same.
49 GType designators remain the same (with respect to the @code{COMMON-LISP:EQ} function) even after dumping and restarting Lisp memory image.
51 GType designators are obtained with @ref{gtype} function, and corresponding numeric and string values are accessed via @ref{gtype-id} and @ref{gtype-name} functions (you must not access @code{gtype} structure directly). If an attempt is made to obtain an invalid GType, then a warning is signalled but GType designator is still returned (which may become valid at some later time due to e.g. library being initialized).
53 Some of the types are fundamental and have constant integer values. They are identified by constants (strings in parentheses are corresponding type names):
54 @itemize
55 @item @code{+g-type-invalid+}. An invalid GType used as error return value in some functions which return a GType.
56 @item @code{+g-type-void+} ("void"). A fundamental type which is used as a replacement for the C @code{void} return type.
57 @item @code{+g-type-interface+} ("GInterface"). The fundamental type from which all interfaces are derived.
58 @item @code{+g-type-char+} ("gchar"). The fundamental type corresponding to gchar. The type designated by @code{+g-type-char+} is unconditionally an 8-bit signed integer. This may or may not be the same type a the C type @code{gchar}.
59 @item @code{+g-type-uchar+} ("guchar"). The fundamental type corresponding to @code{guchar}.
60 @item @code{+g-type-boolean+} ("gboolean"). The fundamental type corresponding to @code{gboolean}.
61 @item @code{+g-type-int+} ("gint"). The fundamental type corresponding to @code{gint}.
62 @item @code{+g-type-uint+} ("guint"). The fundamental type corresponding to @code{guint}.
63 @item @code{+g-type-long+} ("glong"). The fundamental type corresponding to @code{glong}.
64 @item @code{+g-type-ulong+} ("gulong"). The fundamental type corresponding to @code{gulong}.
65 @item @code{+g-type-int64+} ("gint64"). The fundamental type corresponding to @code{gint64}.
66 @item @code{+g-type-uint64+} ("guint64"). The fundamental type corresponding to @code{guint64}.
67 @item @code{+g-type-enum+} ("GEnum"). The fundamental type from which all enumeration types are derived.
68 @item @code{+g-type-flags+} ("GFlags"). The fundamental type from which all flags types are derived.
69 @item @code{+g-type-float+} ("gfloat"). The fundamental type corresponding to @code{gfloat}.
70 @item @code{+g-type-double+} ("gdouble"). The fundamental type corresponding to @code{gdouble}.
71 @item @code{+g-type-string+} ("gchararray"). The fundamental type corresponding to null-terminated C strings.
72 @item @code{+g-type-pointer+} ("gpointer"). The fundamental type corresponding to @code{gpointer}.
73 @item @code{+g-type-boxed+} ("GBoxed"). The fundamental type from which all boxed types are derived. Values of this type correspond to by-value structures.
74 @item @code{+g-type-param+} ("GParam"). The fundamental type from which all GParamSpec types are derived. Values of this type correspond to instances of structure @code{g-class-property-definition}.
75 @item @code{+g-type-object+} ("GObject"). The fundamental type for GObject.
76 @end itemize
78 @lisp
79 (gtype "GObject") @result{} #S(GTYPE :NAME "GObject" :%ID 80)
80 (gtype-id (gtype "GObject")) @result{} 80
81 (gtype-name (gtype "GObject")) @result{} "GObject"
82 (gtype "GtkWidget") @result{} #S(GTYPE :NAME "GtkWidget" :%ID 6963568)
83 @end lisp
85 @node gtype-name
86 @section gtype-name
88 @Function gtype-name
89 @lisp
90 (gtype-name gtype) @result{} name
91 @end lisp
93 @table @var
94 @item @var{g-type-designator}
95 The GType designator for the GType
96 @item @var{name}
97 The name of GType
98 @end table
100 Returns the name of GType. If the GType is invalid, signals warning and may return @code{NIL}.
102 @lisp
103 (gtype-name (gtype "gint")) @result{} "gint"
104 @end lisp
106 @node gtype-id
107 @section gtype-id
109 @Function gtype-id
110 @lisp
111 (gtype-id gtype) @result{} GType
112 @end lisp
114 @table @var
115 @item @var{g-type-designator}.
116 The GType designator for the GType.
117 @item @var{GType}
118 The numeric identifier of GType
119 @end table
121 Returns the numeric identifier of GType. If the GType designator is invalid, signals warning and returns 0.
123 @node gtype
124 @section gtype
126 @Function gtype
127 @lisp
128 (gtype thing) @result GType designator
129 @end lisp
131 Returns the GType designator for @var{thing}. @var{thing} may be:
132 @itemize
133 @item A string. @code{gtype} treats this as a GType name. If no GType with name is registered in GObject then warning is signalled and invalid GType designator is returned (which will become valid once GType will be registered).
134 @item An integer. @code{gtype} treats this as a GType numeric value. If no GType with this valud is registered in GObject then warning is signalled and invalid GType designator is returned (which will become valid once GType will be registered).
135 @item A GType designator. @var{thing} is returned.
136 @end itemize
138 @node Type hierarchy and type relations
139 @chapter Type hierarchy and type relations
141 @menu
142 * g-type-children::
143 * g-type-parent::
144 * g-type-fundamental::
145 * g-type-depth::
146 * g-type-next-base::
147 @end menu
149 GTypes are organized into hierarchy. Each GType (except fundamental types) has a parent type and zero or more children types. Parent of GType identified by @code{g-type-parent} function and its children are identified by @code{g-type-children} function.
151 There are functions to query some specific information:
152 @itemize
153 @item @code{g-type-fundamental} retrieves the fundamental type for given type
154 @item @code{g-type-depth} calculates the depth of the type in type hierarchy
155 @item @code{g-type-next-base} calculates the first step in the path from base type to descendent type
156 @end itemize
158 @node g-type-children
159 @section g-type-children
161 @Function g-type-children
162 @lisp
163 (g-type-children type) @result{} children
164 @end lisp
166 @table @var
167 @item @var{type}
168 A GType designator
169 @item @var{children}
170 A list of GType designators
171 @end table
173 Returns the list of descendent types.
175 Example:
176 @lisp
177 (g-type-children "GtkButton")
178 @result{}
179 ("GtkToggleButton" "GtkColorButton" "GtkFontButton" "GtkLinkButton" "GtkScaleButton")
180 @end lisp
182 @node g-type-parent
183 @section g-type-parent
185 @Function g-type-parent
186 @lisp
187 (g-type-parent type) @result{} parent
188 @end lisp
190 @table @var
191 @item @var{type}
192 A GType designator
193 @item @var{parent}
194 A GType designator
195 @end table
197 Returns the parent of @code{type}.
199 Example:
200 @lisp
201 (g-type-parent "GtkToggleButton")
202 @result{}
203 "GtkButton"
204 @end lisp
206 @node g-type-fundamental
207 @section g-type-fundamental
209 @Function g-type-fundamental
210 @lisp
211 (g-type-fundamental type) @result{} fundamental-type
212 @end lisp
214 @table @var
215 @item @var{type}
216 A GType designator
217 @item @var{fundamental-type}
218 A GType designator for one of the fundamental types
219 @end table
221 Returns the fundamental type that is the ancestor of @code{type}.
223 Example:
224 @lisp
225 (g-type-fundamental "GtkButton") @result{} "GObject"
227 (g-type-fundamental "GtkWindowType") @result{} "GEnum"
229 (g-type-fundamental "GdkEvent") @result{} "GBoxed"
230 @end lisp
232 @node g-type-depth
233 @section g-type-depth
235 @Function g-type-depth
236 @lisp
237 (g-type-depth type) @result{} depth
238 @end lisp
240 @table @var
241 @item @var{type}
242 A GType designator
243 @item @var{depth}
244 An integer
245 @end table
247 Returns the depth of the @code{type}. Depth is the number of types between the @code{type} and its fundamental types (including both @code{type} and its fundamental type). Depth of a fundamental type equals to 1.
249 Example:
250 @lisp
251 (g-type-depth "GObject") @result{} 1
252 (g-type-depth "GInitiallyUnowned") @result{} 2
253 @end lisp
255 @node g-type-next-base
256 @section g-type-next-base
258 @Function g-type-next-base
259 @lisp
260 (g-type-next-base leaf-type root-type) @result{} base-type
261 @end lisp
263 @table @var
264 @item @var{leaf-type}
265 A GType designator
266 @item @var{root-type}
267 A GType designator
268 @item @var{base-type}
269 A GType designator
270 @end table
272 Returns the next type that should be traversed from @code{root-type} in order to reach @code{leaf-type}. E.g., given type hierarchy:
273 @lisp
274 + GObject
276   + GInitiallyUnowned
277    \
278     + GtkObject
279     |\
280     | + GtkAdjustment
281      \
282       + GtkWidget
283        \
284         + GtkContainer
285          \
286           + GtkTable
287 @end lisp
289 the following will be returned:
291 @lisp
292 (g-type-next-base "GtkTable" "GObject") @result{} "GInitiallyUnowned"
293 (g-type-next-base "GtkTable" "GInitiallyUnowned") @result{} "GtkObject"
294 (g-type-next-base "GtkTable" "GtkObject") @result{} "GtkWidget"
295 (g-type-next-base "GtkTable" "GtkWidget") @result{} "GtkContainer"
296 (g-type-next-base "GtkTable" "GtkContainer") @result{} "GtkTable"
297 @end lisp
299 @node Object types information
300 @chapter Object types information
301 @menu
302 * g-class-property-definition::
303 * class-properties::
304 * class-property-info::
305 * interface-properties::
306 * signal-info::
307 * type-signals::
308 * parse-signal-name::
309 * query-signal-info::
310 * g-type-interfaces::
311 * g-type-interface-prerequisites::
312 @end menu
314 GObject classes and interfaces have properties that can be queried with @code{class-properties}, @code{class-property-info} and @code{interface-properties}. These functions represent information about properties with instances of @code{g-class-property-definition} structure.
316 Information about signals can be queries with @code{type-signals}, @code{parse-signal-name} and @code{query-signal-info} functions. Information is returned within instances of @code{signal-info} structures.
318 @node g-class-property-definition
319 @section g-class-property-definition
321 @Struct g-class-property-definition
322 @lisp
323 (defstruct g-class-property-definition
324   name
325   type
326   readable
327   writable
328   constructor
329   constructor-only
330   owner-type)
331 @end lisp
333 @table @var
334 @item @var{name}
335 A string that names the property
336 @item @var{type}
337 A GType designator. Identifies the type of the property
338 @item @var{readable}
339 A boolean. Identifies whether the property can be read
340 @item @var{writable}
341 A boolean. Identifies whether the property can be assigned
342 @item @var{constructor}
343 A boolean. Identifies whether constructor of object accepts this property
344 @item @var{constructor-only}
345 A boolean. Identifies whether this property may only be set in constructor, not in property setter
346 @item @var{owner-type}
347 A GType designator. Identifies the type on which the property was defined.
348 @end table
350 This structure identifies a single property. Its field specify attributes of a property.
352 Structures of this type have shortened print syntax:
353 @lisp
354 #<PROPERTY gchararray GtkButton.label (flags: readable writable constructor)> 
355 @end lisp
357 (When @code{*print-readably*} is T, usual @code{defstruct} print syntax is used)
359 This syntax specifies:
360 @itemize
361 @item type of property
362 @item the owner type of property
363 @item name of property
364 @item additional flags of property
365 @end itemize
367 @node class-properties
368 @section class-properties
370 @Function class-properties
371 @lisp
372 (class-properties type) @result{} properties
373 @end lisp
375 @table @var
376 @item @var{type}
377 A GType designator. Specifies the object type (class)
378 @item @var{properties}
379 A list of @code{g-property-definition} structures.
380 @end table
382 This function returns the list of properties that are available in class @code{type}.
384 Example:
385 @lisp
386 (class-properties "GtkWidget")
387 @result{}
388 (#<PROPERTY gpointer GtkObject.user-data (flags: readable writable)>
389  #<PROPERTY gchararray GtkWidget.name (flags: readable writable)>
390  #<PROPERTY GtkContainer GtkWidget.parent (flags: readable writable)>
391  #<PROPERTY gint GtkWidget.width-request (flags: readable writable)>
392  #<PROPERTY gint GtkWidget.height-request (flags: readable writable)>
393  #<PROPERTY gboolean GtkWidget.visible (flags: readable writable)>
394  #<PROPERTY gboolean GtkWidget.sensitive (flags: readable writable)>
395  #<PROPERTY gboolean GtkWidget.app-paintable (flags: readable writable)>
396  #<PROPERTY gboolean GtkWidget.can-focus (flags: readable writable)>
397  #<PROPERTY gboolean GtkWidget.has-focus (flags: readable writable)>
398  #<PROPERTY gboolean GtkWidget.is-focus (flags: readable writable)>
399  #<PROPERTY gboolean GtkWidget.can-default (flags: readable writable)>
400  #<PROPERTY gboolean GtkWidget.has-default (flags: readable writable)>
401  #<PROPERTY gboolean GtkWidget.receives-default (flags: readable writable)>
402  #<PROPERTY gboolean GtkWidget.composite-child (flags: readable)>
403  #<PROPERTY GtkStyle GtkWidget.style (flags: readable writable)>
404  #<PROPERTY GdkEventMask GtkWidget.events (flags: readable writable)>
405  #<PROPERTY GdkExtensionMode GtkWidget.extension-events (flags: readable writable)>
406  #<PROPERTY gboolean GtkWidget.no-show-all (flags: readable writable)>
407  #<PROPERTY gboolean GtkWidget.has-tooltip (flags: readable writable)>
408  #<PROPERTY gchararray GtkWidget.tooltip-markup (flags: readable writable)>
409  #<PROPERTY gchararray GtkWidget.tooltip-text (flags: readable writable)>
410  #<PROPERTY GdkWindow GtkWidget.window (flags: readable)>)
411 @end lisp
413 @node class-property-info
414 @section class-property-info
415 @Function class-property-info
416 @lisp
417 (class-property-info type property-name) @result{} property
418 @end lisp
420 @table @var
421 @item @var{type}
422 A GType designator
423 @item @var{property-name}
424 A string naming the property
425 @item @var{property}
426 An instance of @code{g-property-definition} structure
427 @end table
429 Returns the property information for a single property.
431 Example:
432 @lisp
433 (class-property-info "GtkButton" "label")
434 @result{}
435 #<PROPERTY gchararray GtkButton.label (flags: readable writable constructor)>
436 @end lisp
438 @node interface-properties
439 @section interface-properties
441 @Function interface-properties
442 @lisp
443 (interface-properties type) @result{} properties
444 @end lisp
446 @table @var
447 @item @var{type}
448 A GType designator
449 @item @var{properties}
450 A list of @code{g-property-definition} structures
451 @end table
453 This function returns the list of properties that are available in interface @code{type}.
455 Example:
456 @lisp
457 (interface-properties "GtkFileChooser")
458 @result{}
459 (#<PROPERTY GtkWidget GtkFileChooser.extra-widget (flags: readable writable)>
460  #<PROPERTY gboolean GtkFileChooser.use-preview-label (flags: readable writable)>
461  #<PROPERTY gboolean GtkFileChooser.preview-widget-active (flags: readable writable)>
462  #<PROPERTY gboolean GtkFileChooser.show-hidden (flags: readable writable)>
463  #<PROPERTY gchararray GtkFileChooser.file-system-backend (flags: writable constructor-only)>
464  #<PROPERTY GtkFileChooserAction GtkFileChooser.action (flags: readable writable)>
465  #<PROPERTY GtkFileFilter GtkFileChooser.filter (flags: readable writable)>
466  #<PROPERTY gboolean GtkFileChooser.select-multiple (flags: readable writable)>
467  #<PROPERTY GtkWidget GtkFileChooser.preview-widget (flags: readable writable)>
468  #<PROPERTY gboolean GtkFileChooser.local-only (flags: readable writable)>
469  #<PROPERTY gboolean GtkFileChooser.do-overwrite-confirmation (flags: readable writable)>)
470 @end lisp
472 @node signal-info
473 @section signal-info
475 @Struct signal-info
476 @lisp
477 (defstruct signal-info
478   id
479   name
480   owner-type
481   flags
482   return-type
483   param-types
484   detail)
485 @end lisp
487 @table @var
488 @item @var{id}
489 An integer - the identifier of a signal
490 @item @var{name}
491 Name of a signal
492 @item @var{owner-type}
493 A GType designator identifying the type on which the signal was defined
494 @item @var{flags}
495 A list of keywords of type @code{'(member :run-first :run-last :run-cleanup :no-recurse :detailed :action :no-hooks)}. Specifies the attributes of a signals
496 @item @var{return-type}
497 The return type of a signal (and signal handlers)
498 @item @var{param-types}
499 A list of GType designators that specify the types of signal parameters
500 @item @var{detail}
501 A string. Specifies the "detail" part of a signal name. E.g., @code{"label"} for signal @code{"notify::label"}.
502 @end table
504 When @code{*print-readably*} is nil, the following print syntax is used:
505 @lisp
506 #<Signal [#1] void GObject.notify::label(GParam) [RUN-FIRST, NO-RECURSE, DETAILED, ACTION, NO-HOOKS]>
507 #<Signal [#54] gboolean GtkWidget.proximity-in-event(GdkEvent) [RUN-LAST]>
508 #<Signal [#64] void GtkWidget.drag-data-received(GdkDragContext, gint, gint, GtkSelectionData, guint, guint) [RUN-LAST]>
509 #<Signal [#8] void GtkObject.destroy() [RUN-CLEANUP, NO-RECURSE, NO-HOOKS]>
510 @end lisp
512 This syntax specifies:
513 @itemize
514 @item the signal id
515 @item signal return type
516 @item owner type
517 @item signal name
518 @item detail
519 @item list of types of parameters
520 @item flags
521 @end itemize
523 @node type-signals
524 @section type-signals
525 @Function type-signals
526 @lisp
527 (type-signals type &key (include-inherited t)) @result{} signals
528 @end lisp
529 @table @var
530 @item @var{type}
531 A GType designator
532 @item @var{signals}
533 A list of @code{signal-info} structures
534 @item @var{include-inherited}
535 A boolean that specifies whether to include signals defined on this type or also on ancestor types.
536 @end table
538 Returns the list of signals that are available in type @code{type}.
540 Example:
541 @lisp
542 (type-signals "GtkLabel" :include-inherited nil)
543 @result{}
544 (#<Signal [#138] void GtkLabel.move-cursor(GtkMovementStep, gint, gboolean) [RUN-LAST, ACTION]>
545  #<Signal [#139] void GtkLabel.copy-clipboard() [RUN-LAST, ACTION]>
546  #<Signal [#140] void GtkLabel.populate-popup(GtkMenu) [RUN-LAST]>)
547 @end lisp
549 @node parse-signal-name
550 @section parse-signal-name
552 @Function parse-signal-name
553 @lisp
554 (parse-signal-name type signal-name) @result{} signal
555 @end lisp
557 @table @var
558 @item @var{type}
559 A GType designator that has the signal.
560 @item @var{signal-name}
561 A string that identifies the signal.
562 @item @var{signal}
563 A list @code{signal-info} structures.
564 @end table
566 Parses the signal name and returns the corresponding information. @code{signal-name} may include the detail part.
568 Example:
569 @lisp
570 (parse-signal-name "GObject" "notify::label")
571 @result{}
572 #<Signal [#1] void GObject.notify::label(GParam) [RUN-FIRST, NO-RECURSE, DETAILED, ACTION, NO-HOOKS]>
573 @end lisp
575 @node query-signal-info
576 @section query-signal-info
577 @Function query-signal-info
578 @lisp
579 (query-signal-info signal-id) @result{} signal
580 @end lisp
581 @table @var
582 @item @var{signal-id}
583 An integer identifying the signal
584 @item @var{signal}
585 An instance of @code{signal-info} structure
586 @end table
588 Retrieves the signal information by its id.
590 Example:
591 @lisp
592 (query-signal-info 73)
593 @result{}
594 #<Signal [#73] gboolean GtkWidget.show-help(GtkWidgetHelpType) [RUN-LAST, ACTION]>
595 @end lisp
597 @node g-type-interfaces
598 @section g-type-interfaces
600 @Function g-type-interfaces
601 @lisp
602 (g-type-interfaces type) @result{} interfaces
603 @end lisp
605 @table @var
606 @item @var{type}
607 A GType designator
608 @item @var{interfaces}
609 A list of GType designators
610 @end table
612 Returns the list of interfaces that @code{type} implements.
614 Example:
615 @lisp
616 (g-type-interfaces "GtkButton")
617 @result{}
618 ("AtkImplementorIface" "GtkBuildable" "GtkActivatable")
619 @end lisp
621 @node g-type-interface-prerequisites
622 @section g-type-interface-prerequisites
624 @Function g-type-interface-prerequisites
625 @lisp
626 (g-type-interface-prerequisites type) @result{} types
627 @end lisp
629 @table @var
630 @item @var{type}
631 A GType designator of interface
632 @item @var{types}
633 A list of GType designators specifying the interface prerequisites
634 @end table
636 Returns the prerequisites of an interface @code{type}. Prerequisite is a type that should be an ancestor of a type implementing interface @code{type}.
638 Example:
639 @lisp
640 (g-type-interface-prerequisites "GtkCellEditable")
641 @result{}
642 ("GtkObject" "GtkWidget")
643 @end lisp
645 @node Enum types information
646 @chapter Enum types information
647 @menu
648 * enum-item::
649 * flags-item::
650 * get-enum-items::
651 * get-flags-items::
652 @end menu
654 Enum types have items that can be listed with @code{get-enum-items} function. This information is exposed within instances of @code{enum-item} structure.
656 Flags types (flags is a kind of enum whose values can be combined) have items that can be queried with @code{get-flags-items} function. This information is exposed within instances of @code{flags-item} structure.
658 @node enum-item
659 @section enum-item
660 @Struct enum-item
661 @lisp
662 (defstruct enum-item
663   name value nick)
664 @end lisp
666 @table @var
667 @item @var{name}
668 A string - name of enum item
669 @item @var{value}
670 An integer - numeric value of enum item
671 @item @var{nick}
672 A string - short name of an enum item
673 @end table
675 Structure @code{enum-item} represents a single item of an enumeration type.
677 Example:
678 @lisp
679 #S(ENUM-ITEM :NAME "GTK_WINDOW_TOPLEVEL" :VALUE 0 :NICK "toplevel")
680 @end lisp
682 @node flags-item
683 @section flags-item
684 @Struct flags-item
685 @lisp
686 (defstruct flags-item
687   name value nick)
688 @end lisp
690 @table @var
691 @item @var{name}
692 A string - name of flags item
693 @item @var{value}
694 An integer - numeric value of flags item
695 @item @var{nick}
696 A string - short name of an flags item
697 @end table
699 Structure @code{flags-item} represents a single item of an flags type.
701 Example:
702 @lisp
703 #S(FLAGS-ITEM
704    :NAME "GDK_POINTER_MOTION_HINT_MASK"
705    :VALUE 8
706    :NICK "pointer-motion-hint-mask")
707 @end lisp
709 @node get-enum-items
710 @section get-enum-items
712 @Function get-enum-items
713 @lisp
714 (get-enum-items type) @result{} items
715 @end lisp
717 @table @var
718 @item @var{type}
719 A GType designator of an enum type
720 @item @var{items}
721 A list of @code{enum-item} structures
722 @end table
724 Returns a list of items in an enumeration
726 Example:
727 @lisp
728 (get-enum-items "GtkScrollType")
729 @result{}
730 (#S(ENUM-ITEM :NAME "GTK_SCROLL_NONE" :VALUE 0 :NICK "none")
731  #S(ENUM-ITEM :NAME "GTK_SCROLL_JUMP" :VALUE 1 :NICK "jump")
732  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_BACKWARD" :VALUE 2 :NICK "step-backward")
733  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_FORWARD" :VALUE 3 :NICK "step-forward")
734  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_BACKWARD" :VALUE 4 :NICK "page-backward")
735  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_FORWARD" :VALUE 5 :NICK "page-forward")
736  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_UP" :VALUE 6 :NICK "step-up")
737  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_DOWN" :VALUE 7 :NICK "step-down")
738  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_UP" :VALUE 8 :NICK "page-up")
739  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_DOWN" :VALUE 9 :NICK "page-down")
740  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_LEFT" :VALUE 10 :NICK "step-left")
741  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_RIGHT" :VALUE 11 :NICK "step-right")
742  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_LEFT" :VALUE 12 :NICK "page-left")
743  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_RIGHT" :VALUE 13 :NICK "page-right")
744  #S(ENUM-ITEM :NAME "GTK_SCROLL_START" :VALUE 14 :NICK "start")
745  #S(ENUM-ITEM :NAME "GTK_SCROLL_END" :VALUE 15 :NICK "end"))
746 @end lisp
748 @node get-flags-items
749 @section get-flags-items
751 @Function get-flags-items
752 @lisp
753 (get-flags-items type) @result{} items
754 @end lisp
756 @table @var
757 @item @var{type}
758 A GType designator of an flags type
759 @item @var{items}
760 A list of @code{flags-item} structures
761 @end table
763 Returns a list of items in an flags type
765 Example:
766 @lisp
767 (get-flags-items "GtkAttachOptions")
768 @result{}
769 (#S(FLAGS-ITEM :NAME "GTK_EXPAND" :VALUE 1 :NICK "expand")
770  #S(FLAGS-ITEM :NAME "GTK_SHRINK" :VALUE 2 :NICK "shrink")
771  #S(FLAGS-ITEM :NAME "GTK_FILL" :VALUE 4 :NICK "fill"))
772 @end lisp
774 @node Using GValues
775 @chapter Using GValues
776 @menu
777 * g-value-zero::
778 * g-value-init::
779 * g-value-unset::
780 * parse-g-value::
781 * set-g-value::
782 * Registering types::
783 @end menu
785 GValue is a generic container for arbitrary value of type supported by GType system. Refer to GObject documentation for more detailed information.
787 CL-GTK2-GOBJECT works with GValue as a foreign type @code{g-value}. Functions @code{g-value-zero}, @code{g-value-type}, @code{g-value-init}, @code{parse-g-value}, @code{set-g-value} are used to inspect and assign GValues. @code{g-value} is a CFFI foreign type that is used by all these functions. Pointer to foreign instance of this type is passed to them.
789 GValue is used whenever a value of unkown type should be passed. It is used in:
790 @itemize
791 @item Closure marshal functions
792 @item Property get and set functions
793 @end itemize
795 Example of usage:
796 @lisp
797 (cffi:with-foreign-object (gval 'g-value)
798   (set-g-value gval "Hello" "gchararray" :zero-g-value t)
799   (format t "~S~%" (parse-g-value gval))
800   (g-value-unset gval))
801 @result{}
802 "Hello"
803 @end lisp
805 @node g-value-zero
806 @section g-value-zero
807 @Function g-value-zero
808 @lisp
809 (g-value-zero g-value)
810 @end lisp
811 @table @var
812 @item @var{g-value}
813 A foreign pointer to GValue structure.
814 @end table
816 Initializes the GValue to "unset" state. Equivalent of the following initializer in C:
817 @lisp
818 GValue value = @{ 0 @};
819 @end lisp
821 Must be called before other functions that work with GValue (except @code{set-g-value} with keyword argument @code{:zero-g-value} set to true).
823 @node g-value-init
824 @section g-value-init
826 @Function g-value-init
827 @lisp
828 (g-value-init value type)
829 @end lisp
830 @table @var
831 @item @var{value}
832 A foreign pointer to GValue structure
833 @item @var{type}
834 A GType designator
835 @end table
837 Initializes the GValue to store instances of type @code{type}. Must be called before other functions operate on GValue (except @code{g-value-zero} and @code{set-g-value} with keyword argument @code{:g-value-init} set to true).
839 @node g-value-unset
840 @section g-value-unset
841 @Function g-value-unset
842 @lisp
843 (g-value-unset value)
844 @end lisp
845 @table @var
846 @item @var{value}
847 A foreign pointer to GValue structure.
848 @end table
850 Unsets the GValue. This frees all resources associated with GValue.
852 @node parse-g-value
853 @section parse-g-value
854 @Function parse-g-value
855 @lisp
856 (parse-g-value value) @result{} object
857 @end lisp
858 @table @var
859 @item @var{value}
860 A foreign pointer to GValue structure
861 @item @var{object}
862 A Lisp object
863 @end table
865 Retrieves the object from GValue structure.
867 @node set-g-value
868 @section set-g-value
869 @Function set-g-value
870 @lisp
871 (set-g-value gvalue object type &key zero-g-value unset-g-value (g-value-init t))
872 @end lisp
874 @table @var
875 @item @var{gvalue}
876 A foreign pointer to GValue structure
877 @item @var{object}
878 An object that is to be assigned to @code{gvalue}
879 @item @var{type}
880 A GType designator specifying what GType should be set
881 @item @var{unset-g-value}
882 A boolean specifying whether to call @code{g-value-unset} before assigment.
883 @item @var{zero-g-value}
884 A boolean specifying whether to call @code{g-value-zero} before assignment
885 @item @var{g-value-init}
886 A boolean specifying whether to call @code{g-value-init} before assignment
887 @end table
889 Assigns the @code{object} to the @code{gvalue}. When GValue is not used, call @code{g-value-unset} to deinitialize the @code{GValue}.
891 @node Registering types
892 @section Registering types
894 In order to be able to parse GValues and set them, it is necessary for GValue binding to know type mapping between GObject types and Lisp types. Type registration serves to this purpose.
896 GEnum and GFlags are mapped to CFFI @code{defcenum} and @code{defbitfield} types. Functions @code{register-enum-type} and @code{register-flags-type} add the type to the mapping.
898 @subsection register-enum-type
899 @Function register-enum-type
900 @lisp
901 (register-enum-type name type)
902 @end lisp
903 @table @var
904 @item @var{name}
905 A string naming the GEnum type
906 @item @var{type}
907 A symbol - name of CFFI foreign enum type
908 @end table
910 Registers the @code{type} to be used for passing value of GEnum type @code{name} between GObject and Lisp.
912 Example:
913 @lisp
914 (defcenum text-direction
915   :none :ltr :rtl)
916 (register-enum-type "GtkTextDirection" 'text-direction)
917 @end lisp
919 @subsection register-flags-type
920 @Function register-flags-type
921 @lisp
922 (register-flags-type name type)
923 @end lisp
924 @table @var
925 @item @var{name}
926 A string naming the GFlags type
927 @item @var{type}
928 A symbol - name of CFFI foreign flags type
929 @end table
931 Registers the @code{type} to be used for passing value of GFlags type @code{name} between GObject and Lisp.
933 Example:
934 @lisp
935 (defcenum state-type
936   :normal :active :prelight :selected :insensitive)
937 (register-enum-type "GtkStateType" 'state-type)
938 @end lisp
940 @node Stable pointers
941 @chapter Stable pointers
942 @menu
943 * allocate-stable-pointer::
944 * free-stable-pointer::
945 * stable-pointer-value::
946 * with-stable-pointer::
947 @end menu
949 Sometimes it is necessary to pass arbitrary Lisp object to C code and then receive it back. Stable pointer serve to this purpose. Stable pointer is an integer (that is passed to C code as a @code{void*} pointer) that is created on Lisp side by call to @code{allocate-stable-pointer} and can be dereferenced by Lisp side at any time by calling @code{stable-pointer-value}. Stable pointer exists and does not change its value until explicitly freed by calling @code{free-stable-poitner}. Convenience macro @code{with-stable-pointer} binds the stable pointer for the duration of its body.
951 @node allocate-stable-pointer
952 @section allocate-stable-pointer
954 @Function allocate-stable-pointer
955 @lisp
956 (allocate-stable-pointer thing) @result{} stable-pointer
957 @end lisp
959 @table @var
960 @item @var{thing}
961 An arbitrary Lisp object
962 @item @var{stable-pointer}
963 A foreign pointer
964 @end table
966 Allocates a stable pointer to @code{thing}.
968 (Note: @var{stable-pointer} should not be dereferenced with @code{cffi:mem-ref}. It should only be dereferenced with @code{stable-pointer-value})
970 Example:
971 @lisp
972 (allocate-stable-pointer (lambda (x) (+ x 10)))
973 @result{}
974 #.(SB-SYS:INT-SAP #X00000002)
976 (stable-pointer-value *)
977 @result{}
978 #<FUNCTION (LAMBDA (X)) @{1004D016F9@}>
980 (free-stable-pointer **)
981 @result{}
983 @end lisp
985 @node free-stable-pointer
986 @section free-stable-pointer
988 @Function free-stable-pointer
989 @lisp
990 (free-stable-pointer stable-pointer)
991 @end lisp
993 @table @var
994 @item @var{stable-pointer}
995 A foreign pointer that was created with @code{allocate-stable-pointer}.
996 @end table
998 Frees the stable pointer, enabling the garbage collector to reclaim the object.
1000 Example:
1001 @lisp
1002 (allocate-stable-pointer (lambda (x) (+ x 10)))
1003 @result{}
1004 #.(SB-SYS:INT-SAP #X00000002)
1006 (stable-pointer-value *)
1007 @result{}
1008 #<FUNCTION (LAMBDA (X)) @{1004D016F9@}>
1010 (free-stable-pointer **)
1011 @result{}
1013 @end lisp
1015 @node stable-pointer-value
1016 @section stable-pointer-value
1018 @Accessor stable-pointer-value
1019 @lisp
1020 (stable-pointer-value stable-pointer) @result{} thing
1021 (setf (stable-pointer-value stable-pointer) thing)
1022 @end lisp
1024 @table @var
1025 @item @var{stable-pointer}
1026 A foreign pointer created by @code{allocate-stable-pointer}
1027 @item @var{thing}
1028 A Lisp object
1029 @end table
1031 Dereferences a @code{stable-pointer}, returning the stable pointer value. @code{stable-pointer-value} is a SETFable form, SETFing it sets the stable pointer's value to new value.
1033 @node with-stable-pointer
1034 @section with-stable-pointer
1036 @Macro with-stable-pointer
1037 @lisp
1038 (with-stable-pointer (ptr expr) &body body)
1039 @end lisp
1041 @table @var
1042 @item @var{ptr}
1043 A variable that will be bound to the stable pointer
1044 @item @var{expr}
1045 An expression that will be evaluated once and its value will be bound to stable pointer's value
1046 @end table
1048 Executes the body with the @code{ptr} variable being bound to a stable pointer whose value is determined by @code{expr}.
1050 Example:
1051 @lisp
1052 (with-stable-pointer (ptr (lambda (x) (+ x 10)))
1053   (print (stable-pointer-value ptr)))
1054 ;;Prints:
1055 #<FUNCTION (LAMBDA (X)) @{1004807E79@}>
1056 @end lisp
1058 @node Closures
1059 @chapter Closures
1060 @menu
1061 * create-signal-handler-closure::
1062 * Object-bound foreign functions::
1063 @end menu
1065 Closure are anonymous functions that capture their lexical environment.
1067 GObject supports using closures (as instances of type GClosure) as signal handlers and in some other places where a function is expected. Function @code{create-signal-handler-closure} create closure from lisp function that can be used a signal handler. The GClosure is finalized automatically when GObject no longer needs it (e.g., when GClosure is disconnected from signal).
1069 (TODO: GObject defines finer closure API: g_closure_ref, g_closure_unref, g_closure_invoke. It should be bound.)
1071 @node create-signal-handler-closure
1072 @section create-signal-handler-closure
1073 @Function create-signal-handler-closure
1074 @lisp
1075 (create-signal-handler-closure object fn) @result{} closure
1076 @end lisp
1078 @table @var
1079 @item @var{object}
1080 An object for which the closure is created
1081 @item @var{fn}
1082 A function that will be called by closure invokation
1083 @item @var{closure}
1084 A foreign pointer to allocated closure
1085 @end table
1087 Allocates the closure. The closure is destroyed automatically by GObject.
1089 Example:
1090 @lisp
1091 (create-signal-handler-closure obj (lambda (x) (+ x 10)))
1092 @result{}
1093 #.(SB-SYS:INT-SAP #X006D7B20)
1094 @end lisp
1096 Example of usage from GObject binding code:
1097 @lisp
1098 (defun connect-signal (object signal handler &key after)
1099   (g-signal-connect-closure (ensure-object-pointer object)
1100                             signal
1101                             (create-signal-handler-closure object handler)
1102                             after))
1103 @end lisp
1105 @node Object-bound foreign functions
1106 @section Object-bound foreign functions
1108 A common idiom for Gtk+ for passing user-defined function is as follows. Callback function has (besides its 'useful' arguments) an additional argument at the end - the 'data' pointer. This 'data' pointer, along with the pointer to 'destroy-notify' function is specified when passing the function. Destroy-notify function allows to free the function object (the Lisp closure) when it is not used by an object.
1110 @RMacro define-cb-methods
1111 @lisp
1112 (define-cb-methods name return-type ((arg-1 type-1) ... (arg-n type-n)))
1113 @end lisp
1115 Defines two CFFI callbacks assosiated with the callback function type @var{name}. Creates @var{name}-cb - a callback to be passed as an function and create @var{name}-destroy-notify - a callback to be passed as 'destroy-notify' function. These callbacks are intended to work together with @ref{create-fn-ref}.
1117 Arguments and return-type are the same as CFFI arguments and return-type for callbacks. Arguments do not include the last 'data' pointer.
1119 @RFunction create-fn-ref
1120 @lisp
1121 (create-fn-ref object function) @result{} foreign-pointer
1122 @end lisp
1124 This function creates a foreign structure containing the data neccesary for callbacks defined by @ref{define-cb-methods} to call and dispose of the @var{function}. The @var{function} is bound to the @var{object}. This is neccesary for correct work of garbage collector. The created structure is freed by 'destroy-notify' function.
1126 Example:
1127 @lisp
1128 (defcfun gtk-assistant-set-forward-page-func :void
1129   (assistant (g-object assistant))
1130   (page-func :pointer)
1131   (data :pointer)
1132   (destroy-notify :pointer))
1134 (define-cb-methods assistant-page-func :int ((current-page :int)))
1136 (defun set-assistant-forward-page-function (assistant function)
1137   (if function
1138       (gtk-assistant-set-forward-page-func assistant
1139                                            (callback assistant-page-func-cb)
1140                                            (create-fn-ref assistant function)
1141                                            (callback assistant-page-func-destroy-notify))
1142       (gtk-assistant-set-forward-page-func assistant (null-pointer) (null-pointer) (null-pointer))))
1143 @end lisp
1145 @node GObject low-level
1146 @chapter GObject low-level
1147 @menu
1148 * g-object-call-constructor::
1149 * g-type-from-object::
1150 * g-object-call-get-property::
1151 * g-object-call-set-property::
1152 @end menu
1154 GObject low-level support includes facilities for working with objects as foreign pointers and using explicit function to get and set properties. This low-level support does not deal with integration of GObject with CLOS; GObject high-level support does that.
1156 Function @code{g-type-from-object} identifies the type of the object. Function @code{g-object-call-get-property} retrieves the value of the property and function @code{g-object-call-set-property} sets the value of the property. Function @code{g-object-call-constructor} calls the constructor of the GObject type.
1158 @node g-object-call-constructor
1159 @section g-object-call-constructor
1161 @Function g-object-call-constructor
1162 @lisp
1163 (g-object-call-constructor object-type args-names args-values &optional args-types) @result{} object-ptr
1164 @end lisp
1166 @table @var
1167 @item @var{object-type}
1168 A GType designator that specifies the object type that is to be created
1169 @item @var{args-names}
1170 A list of strings naming the arguments to constructor
1171 @item @var{args-value}
1172 A list of arguments values (in the same order as args-names)
1173 @item @var{args-types}
1174 Optional list of arguments types (in the same order as args-names). If not specified, it is detected automatically
1175 @item @var{object-ptr}
1176 A foreign pointer to newly created instance
1177 @end table
1179 Creates the object of type @code{object-type} by calling its constructors with arguments specified by @code{args-names}, @code{args-values}, @code{args-types}.
1181 Example:
1182 @lisp
1183 (g-object-call-constructor "GtkButton" '("label" "use-underline") '("Hello" t) '("gchararray" "gboolean"))
1184 @result{}
1185 #.(SB-SYS:INT-SAP #X006D8900)
1187 (g-object-call-get-property * "label")
1188 @result{}
1189 "Hello"
1191 (g-object-call-get-property ** "use-underline")
1192 @result{}
1194 @end lisp
1196 @node g-type-from-object
1197 @section g-type-from-object
1199 @Function g-type-from-object
1200 @lisp
1201 (g-type-from-object object-ptr) @result{} type
1202 @end lisp
1204 @table @var
1205 @item @var{object-ptr}
1206 A foreign pointer to a GObject instance
1207 @item @var{type}
1208 A GType designator
1209 @end table
1211 Returns the type of an object by a pointer to its instance
1213 Example:
1214 @lisp
1215 (g-type-from-object (g-object-call-constructor "GtkButton" nil nil))
1216 @result{}
1217 "GtkButton"
1218 @end lisp
1220 @node g-object-call-get-property
1221 @section g-object-call-get-property
1223 @Function g-object-call-get-property
1224 @lisp
1225 (g-object-call-get-property object-ptr property-name &optional property-type) @result{} property-value
1226 @end lisp
1228 @table @var
1229 @item @var{object-ptr}
1230 A foreign pointer to a GObject instance
1231 @item @var{property-name}
1232 A string naming the property
1233 @item @var{property-type}
1234 Optional GType designator specifying the type of a property
1235 @item @var{property-value}
1236 The value of a property
1237 @end table
1239 Retrieves the value of a property @code{property-name} of object pointed to by @code{object-ptr}. @code{property-type} specifies the type of a property; it may be omitted.
1241 Example:
1242 @lisp
1243 (g-object-call-constructor "GtkButton" '("label" "use-underline") '("Hello" t) '("gchararray" "gboolean"))
1244 @result{}
1245 #.(SB-SYS:INT-SAP #X006D8900)
1247 (g-object-call-get-property * "label")
1248 @result{}
1249 "Hello"
1251 (g-object-call-get-property ** "use-underline")
1252 @result{}
1254 @end lisp
1256 @node g-object-call-set-property
1257 @section g-object-call-set-property
1259 @Function g-object-call-set-property
1260 @lisp
1261 (g-object-call-set-property object-ptr property-name new-value &optional property-type)
1262 @end lisp
1264 @table @var
1265 @item @var{object-ptr}
1266 A foreign pointer to a GObject instance
1267 @item @var{property-name}
1268 A string naming the property
1269 @item @var{new-value}
1270 A new value of a property
1271 @item @var{property-type}
1272 Optional GType designator specifying the type of a property
1273 @end table
1275 Sets the property value of property @code{property-name} of object @code{object-ptr} to @code{new-value}.
1277 Example:
1278 @lisp
1279 (g-object-call-constructor "GtkButton" nil nil)
1280 @result{}
1281 #.(SB-SYS:INT-SAP #X006D8B40)
1283 (g-object-call-set-property * "label" "Hello")
1284 @result{}
1285 ; No value
1287 (g-object-call-get-property ** "label")
1288 @result{}
1289 "Hello"
1290 @end lisp
1292 @node GObject high-level
1293 @chapter GObject high-level
1294 @menu
1295 * g-object::
1296 * g-initially-unowned::
1297 * GObject metaclass::
1298 * Using objects::
1299 * Signals::
1300 * GObject foreign class::
1301 @end menu
1303 GObject high-level support includes integration of GObject and CLOS systems. This enables to use GObjects classes as CLOS classes (with support from @code{gobject-class} metaclass):
1304 @itemize
1305 @item objects are created with @code{make-instance}
1306 @item properties are used as regular slots
1307 @end itemize
1309 GObjects are reference counted, and CL-GTK2-GOBJECT manages its own reference to GObjects. This enables to have transparent garbage collection of unreferenced GObjects.
1311 To be able to use particular GObject class with CLOS, it should be defined and registered. This is accomplished by @code{defclass}'ing it with @code{gobject-class} metaclass. After GObject class is defined, it may be used as CLOS class.
1313 Example GObject class of definition:
1314 @lisp
1315 (defclass dialog (gtk-window atk-implementor-iface buildable)
1316   ((has-separator :accessor dialog-has-separator
1317                   :initarg :has-separator
1318                   :allocation :gobject-property
1319                   :g-property-type "gboolean"
1320                   :g-property-name "has-separator"))
1321   (:metaclass gobject-class)
1322   (:g-type-name . "GtkDialog")
1323   (:g-type-initializer . "gtk_dialog_get_type"))
1324 @end lisp
1326 This example defines the CLOS class @code{dialog} that corresponds to GObject class @code{GtkDialog}. Whenever object of GObject type @code{GtkDialog} are to be received from foreign functions or passed to foreign functions, it will be mapped to CLOS class @code{dialog}. Properties that have @code{:allocation} of @code{:gobject-property} are mapped to GObject properties, and reading or writing this slot reads or writes corresponding GObject class property.
1328 GObject does not expose objects methods. Because of this, methods are not automatically mapped to CLOS generic functions and methods. Methods should be manually wrapped with CFFI as foreign functions. Foreign type @code{g-object} aids in it. This type automatically wraps (and unwraps) the GObject class instances and handles the reference counting.
1330 GObject high-level support enables connect signals to signal handlers. Any function may be connected as a signal handler, and GObject will release the reference on signal handler whenever it become unneded (e.g., when object is destroyed or handler is disconnected).
1332 @node g-object
1333 @section g-object
1335 @Class g-object
1337 A base class for all GObject classes.
1339 @Accessor pointer g-object
1341 An accessor that accesses the foreign pointer to object.
1343 @Function release
1344 @lisp
1345 (release object)
1346 @end lisp
1348 Releases the @var{object} by dropping the reference from it in advance before GC reclaims it. Use this function as an optimization measure when you know that some object will not be needed. All access to the object's properties will be invalid after this function is called.
1350 @Macro using
1351 @lisp
1352 (using (object expr) &body body)
1353 @end lisp
1355 Evaluates and returns the result of evaluation of the @var{body} with @var{object} being bound to the result of evaluating @var{expr}. Ensures that @code{release} is called on @var{object} after the @var{body} is evaluated.
1357 @Macro using
1358 @lisp
1359 (using ((var1 expr1) (var2 expr2) ... (varn exprn)) &body body)
1360 @end lisp
1362 Evaluates and returns the result of evaluation of the @var{body} with @var{var}s being bound to the results of evaluating @var{expr}s. Ensures that @code{release} is called on every @var{var} after the @var{body} is evaluated.
1364 @node g-initially-unowned
1365 @section g-initially-unowned
1367 @Class g-initially-unowned
1369 Superclass: @ref{g-object}
1371 A base class for all GObject classes whose initial reference is floating.
1373 @node GObject metaclass
1374 @section GObject metaclass
1376 See MOP for information what metaclass is and why is it useful.
1378 GObject metaclass @code{gobject-class} bridges two object systems: GObject and CLOS.
1380 Classes that correspond to GObject classes are instances of this class. Each CLOS class of @code{gobject-class} metaclass is mapped to one GObject class. Two or more CLOS classes may map into one GObject class. GObject and CLOS inheritance must be consistent: if class @code{X} is a subclass or the same class as @code{Y} in CLOS, then this relation must hold for @code{X'} and @code{Y'}, where @code{X'} is a GObject class to which @code{X} class maps to.
1382 For each instance of GObject-related CLOS class there is a corresponding instance of GObject class (of a GObject class to which the CLOS class maps). Whenever the GObject class instance reference enters the Lisp memory (by creating instance with @code{make-instance}, as the return value of foreign function or as a slot value of GObject class instance), an instance of CLOS class is created.
1384 Defining the class with metaclass @code{gobject-class} registers the type @code{:g-type-name} for conversions using GValue and CFFI foreign type @code{g-object}.
1386 This class has the following slots:
1387 @itemize
1388 @item @var{g-type-name} (accessor @code{gobject-class-g-type-name}, initarg @code{:g-type-name})
1390 Specifies the name of corresponding GObject class. String or NIL is allowed. If the name is NIL, then the same GObject class as its parent. Only one class may have specified a given @code{:g-type-name}.
1391 @item @var{g-type-initializer} (accessor @code{gobject-class-g-type-initializer}, initarg @code{:g-type-initializer})
1393 Name of foreign type initializer function. This function initializes the class and returns its GType. Typically it is named @code{class_get_type}. String or NIL is allowed.
1394 @item @var{interface-p} (accessor @code{gobject-class-interface-p}, initarg @code{:interface-p})
1396 A boolean specifying whether this CLOS class corresponds to GInterface. It is NIL by default.
1397 @end itemize
1399 This metaclass defines the GObject classes.
1401 Slots which have @code{:allocation} of @code{:gobject-property} are mapped to GObject properties. Such slots have following attributes:
1402 @itemize
1403 @item @var{:g-property-type}
1405 A string naming GType of property
1406 @item @var{:g-property-name}
1408 A name of a property
1409 @end itemize
1411 Slots which have @code{:allocation} of @code{:gobject-fn} are mapped to a pair of accessor functions (usually named @code{class_get_property} and @code{class_set_property}). This is included because some properties are not exposed as GObject properties. Such slots have following attributes:
1412 @itemize
1413 @item @var{:g-property-type}
1414 A CFFI foreign type of property
1415 @item @var{:g-getter}
1416 A string naming foreign getter function of a property or a symbol designating Lisp getter function. Foreign getter function should have signature @code{type class_get_property(object *)}. Lisp function should be of type @code{(function (class) type)}.
1417 @item @var{:g-setter}
1418 A string naming foreign setter function of a property or a symbol designating Lisp setter function. Foreign setter function should have signature @code{void class_set_property(object *, type)}. Lisp function should be of type @code{(function (class type))}.
1419 @end itemize
1421 Initargs of a slot are used to construct the GObject class.
1423 Example:
1424 @lisp
1425 (defclass container (widget atk-implementor-iface buildable)
1426     ((border-width :allocation :gobject-property
1427                    :g-property-type "guint"
1428                    :accessor container-border-width
1429                    :initarg :border-width
1430                    :g-property-name "border-width")
1431      (resize-mode :allocation :gobject-property
1432                   :g-property-type "GtkResizeMode"
1433                   :accessor container-resize-mode
1434                   :initarg :resize-mode
1435                   :g-property-name "resize-mode")
1436      (child :allocation :gobject-property
1437             :g-property-type "GtkWidget"
1438             :accessor container-child
1439             :initarg :child
1440             :g-property-name "child")
1441      (focus-child :allocation :gobject-fn
1442                   :g-property-type g-object
1443                   :accessor container-focus-child
1444                   :initarg :focus-child
1445                   :g-getter "gtk_container_get_focus_child"
1446                   :g-setter "gtk_container_set_focus_child")
1447      (focus-vadjustment :allocation :gobject-fn
1448                         :g-property-type (g-object adjustment)
1449                         :accessor container-focus-vadjustment
1450                         :initarg :focus-vadjustment
1451                         :g-getter "gtk_container_get_focus_vadjustment"
1452                         :g-setter "gtk_container_set_focus_vadjustment")
1453      (focus-hadjustment :allocation :gobject-fn
1454                         :g-property-type (g-object adjustment)
1455                         :accessor container-focus-hadjustment
1456                         :initarg :focus-hadjustment
1457                         :g-getter "gtk_container_get_focus_hadjustment"
1458                         :g-setter "gtk_container_set_focus_hadjustment"))
1459     (:metaclass gobject-class)
1460     (:g-type-name . "GtkContainer")
1461     (:g-type-initializer . "gtk_container_get_type"))
1462 @end lisp
1463 (note the dot in @code{(:g-type-name . "GtkContainer")} and in @code{(:g-type-initializer . "gtk_container_get_type")}. It should be present)
1465 @node Using objects
1466 @section Using objects
1467 Instances are created with @code{make-instance}. If initargs of GObject properties are supplied, they are passed to constructor. Some slots (properties) may only be set at construction time (e.g., @code{type} property of @code{GtkWindow}). Properties may be accessed (read or assigned) with defined @code{:accessor}, @code{:reader} or @code{:writer} functions.
1469 Example:
1470 @lisp
1471 (make-instance 'gtk:dialog :has-separator t)
1472 @result{}
1473 #<GTK:DIALOG @{10036C5A71@}>
1475 (defvar *d* (make-instance 'gtk:dialog :has-separator t))
1476 @result{}
1479 (gtk:dialog-has-separator *d*)
1480 @result{}
1483 (setf (gtk:dialog-has-separator *d*) nil)
1484 @result{}
1487 (gtk:dialog-has-separator *d*)
1488 @result{}
1490 @end lisp
1492 @node Signals
1493 @section Signals
1495 To connect handler to a signal, @code{connect-signal} function is used. Function @code{disconnect-signal} removes the connected signal.
1497 @Function connect-signal
1498 @lisp
1499 (connect-signal object signal handler &key after) @result{} handler-id
1500 @end lisp
1502 @table @var
1503 @item @var{object}
1504 An instance of GObject object
1505 @item @var{signal}
1506 A signal name
1507 @item @var{handler}
1508 A function
1509 @item @var{after}
1510 A boolean specifying whether the handler should be called after the default handler
1511 @item @var{handler-id}
1512 An integer - identifier of signal handler; can be used to disconnect the signal handler with @code{disconnect-signal}
1513 @end table
1515 Connects the @code{handler} to signal @code{signal} on object @code{object}. Signature of @code{handler} should comply with signature of a signal. @code{handler} will be called with arguments of type specified by signal with the object (on which the signal was emitted) prepended to them and it should return the value of the signal's return type.
1517 @Function disconnect-signal
1518 @lisp
1519 (disconnect-signal object handler-id)
1520 @end lisp
1522 Disconnects the signal handler identified by @var{handler-id} from the corresponding signal for @var{object}. @var{handler-id} is the integer identifying the signal handler; @code{connect-signal} returns handler identifiers.
1524 Example:
1525 @lisp
1526 (defvar *d* (make-instance 'gtk:dialog))
1527 @result{}
1531 @result{}
1532 #<GTK:DIALOG @{1002D866F1@}>
1534 (parse-signal-name "GtkDialog" "response")
1535 @result{}
1536 #<Signal [#86] void GtkDialog.response(gint) [RUN-LAST]>
1538 (connect-signal *d* "response" (lambda (dialog response-value) (print dialog) (print response-value)))
1540 (emit-signal *d* "response" 14)
1541 @result{}
1542 ;; Prints:
1543 #<GTK:DIALOG @{1002D866F1@}>
1544 14 
1545 @end lisp
1547 Function @code{emit-signal} is used to emit signals on objects.
1549 @RFunction emit-signal
1550 @code{(emit-signal object signal-name &rest args) @result{} return-value}
1552 @table @var
1553 @item @var{object}
1554 An object on which the signal should be emitted
1555 @item @var{signal-name}
1556 A string naming the signal
1557 @item @var{args}
1558 Arguments for a signal
1559 @item @var{return-value}
1560 Return value of a signal
1561 @end table
1563 Emits the signal and calls all handlers of the signal. If signal returns a value, it is returned from @code{emit-signal}.
1565 Example:
1566 @lisp
1567 (defvar *d* (make-instance 'gtk:dialog))
1568 @result{}
1572 @result{}
1573 #<GTK:DIALOG @{1002D866F1@}>
1575 (parse-signal-name "GtkDialog" "response")
1576 @result{}
1577 #<Signal [#86] void GtkDialog.response(gint) [RUN-LAST]>
1579 (connect-signal *d* "response" (lambda (dialog response-value) (print dialog) (print response-value)))
1581 (emit-signal *d* "response" 14)
1582 @result{}
1583 ;; Prints:
1584 #<GTK:DIALOG @{1002D866F1@}>
1585 14 
1586 @end lisp
1588 @node GObject foreign class
1589 @section GObject foreign class
1591 To enable passing GObject instance between Lisp code and foreign code, @code{g-object} foreign type is introduced.
1593 This type has the following syntax:
1594 @code{(g-object [type] [:already-referenced])} or @code{g-object}. (Brackets indicate optional arguments)
1596 When the @code{g-object} foreign type is specified as a return type of a function, the value is converted to instance of corresponding CLOS class. If @code{type} is specified then it is checked that object is of this type. If @code{:already-referenced} is included then it is assumed that the function returns already referenced object (so that it is not needed to call @code{g-object-ref} on returned object).
1598 When the @code{g-object} foreign type is specified as a type of function's argument, the value is converted to pointer to GObject. If @code{type} is specified then it is checked that the object is of this type.
1600 This defines the function that may be called with instances of types @code{container} and @code{widget}:
1601 @lisp
1602 (defcfun (container-add "gtk_container_add") :void
1603   (container (g-object container))
1604   (widget (g-object widget)))
1606 (let ((window (make-instance 'gtk-window))
1607       (widget (make-instance 'button)))
1608   (container-add window widget))
1609 @end lisp
1610 (@code{gtk-window} is a subclass of @code{container}; @code{button} is a subclass of @code{widget})
1612 This defines the function that returns an instance of GObject class:
1613 @lisp
1614 (defcfun (bin-child "gtk_bin_get_child") (g-object widget)
1615   (bin (g-object bin)))
1617 (let ((window (make-instance 'gtk-window))
1618       (widget (make-instance 'button)))
1619   (container-add window widget)
1620   (bin-child window))
1621 @result{}
1622 #<GTK:BUTTON @{1002DE74B1@}>
1623 @end lisp
1625 This example shows the use of @code{:already-referenced} option:
1626 @lisp
1627 (defcfun (widget-create-pango-layout "gtk_widget_create_pango_layout") (g-object gdk::pango-layout :already-referenced)
1628   (widget (g-object widget))
1629   (text :string))
1631 (defcfun gdk-gc-new (g-object graphics-context :already-referenced)
1632   (drawable (g-object drawable)))
1633 @end lisp
1635 @node Creating GObjects classes and implementing GInterfaces
1636 @chapter Creating GObjects classes and implementing GInterfaces
1638 @menu
1639 * define-vtable::
1640 * register-object-type-implementation::
1641 @end menu
1643 Creating GObject classes from Lisp is the most complex part of GObject binding.
1645 GObject binding at the moment provides only limited scenarios of creating GObject classes. It lets register GObject class (as a subclass of another class or of GObject), specify its properties and implemented interfaces. Each property is associated with Lisp getter and setter functions. Each interface is associated wth vtable (table of virtual function pointers, see @uref{http://en.wikipedia.org/wiki/Vtable}) that specifies a list of methods and their signatures. If class is ever created from GObject side (not from Lisp side, must be constructable with no parameters).
1647 Each virtual function is mapped to a generic function for which class should provide a specialized method. This function should not be called by user. Rather, user code should call corresponding foreign function.
1649 Practically speaking, creating GObject class requires defining CLOS class that correspond to GObject class and calling @code{register-object-type-implementation} with information about the class (its GType name, superclass, interfaces and properties).
1651 Interface that is implemented by a class should have its vtable defined by @code{define-vtable}. Vtable definitions consists of a list of functions's names and signatures and their locations in vtable.
1653 Unfortunately, GObject does not provide information about vtables, and does not support using GClosures to implement virtual functions. Therefore, implementation for all interface's functions are defined as CFFI foreign callbacks. These callbacks in turn call corresponding generic functions that should be specialized on required objects.
1655 @node define-vtable
1656 @section define-vtable
1658 @Macro define-vtable
1659 @lisp
1660 (define-vtable (g-type-name type-name)
1661   &body item*)
1663 item ::= (:skip cffi-structure-item)
1664 item ::= (method-name (return-type &rest arg*) &key impl-call)
1665 arg ::= (arg-name arg-type)
1666 impl-call ::= ((arg-name*) &body call-code)
1667 @end lisp
1669 @table @var
1670 @item @var{g-type-name}
1671 A string naming the GObject type of interface
1672 @item @var{cffi-structure-item}
1673 A structure item that is inserted verbatim into foreign structure definition of vtable and is not used as a pointer to method
1674 @item @var{method-name}
1675 A name for implementation generic function
1676 @item @var{return-type}
1677 A CFFI specifier for foreign function return type
1678 @item @var{arg-name}
1679 A symbol naming the argument of interface method
1680 @item @var{arg-type}
1681 A CFFI specifier for foreign function argument type
1682 @item @var{call-code}
1683 A body of code that is used to convert arguments and return values between interface signature and desired implementor generic function signature
1684 @end table
1686 Macro that specifies the vtable for an interface. Vtable for an interface is a structure that contains pointer to implementations of interface methods. Vtable is used to dispach method calls to corresponding method implementations. In cl-gtk2-gobject, vtables are needed to create classes that implement GObject interfaces.
1688 GObject interfaces are implemented in the following way. For every method, an implementor generic function is defined. This generic function is called by interface method callback, and CLOS classes specialize on this generic function to implement an interface. The generic function has the same signature as the interface's function, but signatures may differ.
1690 This macro defines generic functions (named by concatenatinag @var{type-name}, @var{name} and @code{impl}; e.g., @code{get-flags} method of class @code{tree-model} will have generic function named @code{tree-model-get-flags-impl}) that correspond to methods of an interface. On these generic functions methods should be defined that implement the interface method. @code{item}s specify the CFFI foreign structure for vtable. Vtable contains not only function pointers, but other slots. Such slots should be specified here with @code{:skip} prepended to them. This is needed to be able to correctly calculate offsets to function pointers in vtable.
1692 In some cases, the signature of interface method is not very lispy: it may pass @code{void*} pointers, pointers to places where return values should be stored. To conceal such unlispy things, you specify your own code that will call the generic function and translate arguments for implementor generic function. This is implemented by specifying @var{impl-call} method option. @var{impl-call} specifies the signature of implementor function and code that calls the generic function and returns its result. The code is put in return position of callback, and it has access to the arguments of callback and its return value becomes the return value of callback.
1694 Example:
1695 @lisp
1696 (define-vtable ("GtkTreeModel" tree-model)
1697   (:skip parent-instance g-type-interface)
1698   ;;some signals
1699   (:skip tree-model-row-changed :pointer)
1700   (:skip tree-model-row-inserted :pointer)
1701   (:skip tree-model-row-has-child-toggled :pointer)
1702   (:skip tree-model-row-deleted :pointer)
1703   (:skip tree-model-rows-reordered :pointer)
1704   ;;methods
1705   (get-flags (tree-model-flags (tree-model g-object)))
1706   (get-value (:void
1707               (tree-model g-object)
1708               (iter (g-boxed-foreign tree-iter))
1709               (n :int)
1710               (value (:pointer g-value)))
1711              :impl-call
1712              ((tree-model iter n)
1713               (multiple-value-bind (v type) (tree-model-get-value-impl tree-model iter n)
1714                 (set-g-value value v type)))))
1716 (defmethod tree-model-get-flags-impl ((model array-list-store))
1717   '(:list-only))
1719 (defmethod tree-model-get-value-impl ((model array-list-store) iter n)
1720   (let ((n-row (tree-iter-user-data iter)))
1721     (values (funcall (aref (store-getters model) n) 
1722                      (aref (store-items model) n-row))
1723             (aref (store-types model) n))))
1724 @end lisp
1726 @node register-object-type-implementation
1727 @section register-object-type-implementation
1729 @Macro register-object-type-implementation
1730 @lisp
1731 (register-object-type-implementation name class parent interfaces properties)
1732 @end lisp
1734 @table @var
1735 @item @var{name}
1736 A string naming the new GObject class.
1737 @item @var{class}
1738 A class name of corresponding CLOS class. It should be inherited from @code{g-object} or its descendants.
1739 @item @var{parent}
1740 A string naming the GObject superclass
1741 @item @var{interfaces}
1742 A list of names of interfaces that this class implements.
1743 @item @var{properties}
1744 A list of properties that this class provides.
1745 Each property is defined as
1746 @lisp
1747 property ::= (property-name property-type accessor property-get-fn property-set-fn)
1748 @end lisp
1749 @end table
1751 A macro that creates a new GObject type and registers the Lisp implementation for it.
1753 Example:
1754 @lisp
1755 (register-object-type-implementation "LispArrayListStore" array-list-store "GObject" ("GtkTreeModel") nil)
1756 @end lisp
1758 @node GBoxed
1759 @chapter GBoxed
1760 @menu
1761 * define-g-boxed-cstruct::
1762 * define-g-boxed-variant-cstruct::
1763 * define-g-boxed-opaque::
1764 * g-boxed-opaque::
1765 * define-boxed-opaque-accessor::
1766 * boxed-related-symbols::
1767 * GBoxed foreign type::
1768 * copy-boxed-slots-to-foreign::
1769 * with-boxed-foreign-array::
1770 @end menu
1772 GObject manual defines this type in the following way:
1774 ``GBoxed is a generic wrapper mechanism for arbitrary C structures. The only thing the type system needs to know about the structures is how to copy and free them, beyond that they are treated as opaque chunks of memory.
1776 Boxed types are useful for simple value-holder structures like rectangles or points. They can also be used for wrapping structures defined in non-GObject based libraries.''
1778 Naturally, it is hard to provide support for ``arbitrary C structures''. We support a few useful use cases of GBoxed types:
1779 @itemize
1780 @item Simple C structures. A Lisp structure is a one-to-one correspondence to C structure and is passes to and from foreign code by copying the data it contains. Examples of simple structures are GdkPoint, GdkRectangle.
1781 @item ``Variant'' C structures. A one common idiom of C is to define a union of structures sharing the same parts in order to implement the polymorphism of structures. These structures are mapped to a hierarchy of Lisp structures (where one structure subclasses another via the @code{:include} @code{defstruct} option).
1783 For example, Gdk has structure GdkEvent which is a union of GdkEventAny, GdkEventExpose and other structures. These structures have common slots: ``type'', ``window'', ``send_event''. By dispatching on ``type'', user or GdkEvent structure knows which of GdkEvent* structures it has and can access other fields.
1784 @item Opaque C structures. A C structure the has ``hidden'' fields and should only created/destroyed with specific functions and be accessed only with specific accessors. Example of such structures is GtkTreePath.
1785 @end itemize
1787 @node define-g-boxed-cstruct
1788 @section define-g-boxed-cstruct
1789 @Macro define-g-boxed-cstruct
1790 @lisp
1791 (define-g-boxed-cstruct name g-type-name
1792   &body slot*)
1794 slot ::= (slot-name slot-type &key count initform inline)
1795 @end lisp
1797 @table @var
1798 @item @var{name}
1799 A symbol naming the type being defined
1800 @item @var{g-type-name}
1801 A string specifying the GType name of this GBoxed. This may be nil if this type is not registered with GObject type system.
1802 @item @var{slot-name}
1803 A symbol naming the slot of a structure
1804 @item @var{slot-type}
1805 A foreign type of a slot
1806 @item @var{count}
1807 An integer. Corresponds to @code{:count} option of slot in CFFI @code{defcstruct}. If @code{count} is not NIL, then the slot is mapped to Lisp array.
1808 @item @var{initform}
1809 A form that is the initform of Lisp structure slot
1810 @item @var{inline}
1811 A boolean. If it is true, then the slot contains GBoxed structure whose name is @code{slot-type}.
1812 @end table
1814 Defines the ``simple'' GBoxed structure corresponding to C structure. The slot specification is analogous to CFFI @code{defstruct} slot specification with the addition of @code{inline} option. This also defines the @var{name}-cstruct CFFI structure definition with equivalent structure.
1816 Example of usage:
1817 @lisp
1818 (define-g-boxed-cstruct rectangle "GdkRectangle"
1819   (left :int :initform 0)
1820   (top :int :initform 0)
1821   (width :int :initform 0)
1822   (height :int :initform 0))
1824 (define-g-boxed-cstruct point nil
1825   (x :int :initform 0)
1826   (y :int :initform 0))
1828 (define-g-boxed-cstruct vector4 nil
1829   (coords :double :count 4 :initform (vector 0d0 0d0 0d0 0d0)))
1831 (define-g-boxed-cstruct segment nil
1832   (a point :inline t :initform (make-point))
1833   (b point :inline t :initform (make-point)))
1834 @end lisp
1836 @node define-g-boxed-variant-cstruct
1837 @section define-g-boxed-variant-cstruct
1839 @Macro define-g-boxed-variant-cstruct
1840 @lisp
1841 (define-g-boxed-variant-cstruct name g-type-name
1842   &body slot-or-variant-specification*)
1844 slot ::= (slot-name slot-type &key count initform inline)
1845 variant-specification ::= (:variant dispatching-slot-name structure-variant*)
1846 structure-variant ::= (dispatching-slot-values structure-name &body slot-or-variant-specification*)
1847 @end lisp
1849 @table @var
1850 @item @var{name}
1851 A symbol naming the type being defined
1852 @item @var{g-type-name}
1853 A string specifying the GType name of this GBoxed. This may be nil if this type is not registered with GObject type system.
1854 @item @var{slot-name}
1855 A symbol naming the slot of a structure
1856 @item @var{slot-type}
1857 A foreign type of a slot
1858 @item @var{count}
1859 An integer. Corresponds to @code{:count} option of slot in CFFI @code{defcstruct}. If @code{count} is not NIL, then the slot is mapped to Lisp array.
1860 @item @var{initform}
1861 A form that is the initform of Lisp structure slot
1862 @item @var{inline}
1863 A boolean. If it is true, then the slot contains GBoxed structure whose name is @code{slot-type}.
1864 @item @var{dispatching-slot-name}
1865 A name of the dispatching slot
1866 @item @var{dispatching-slot-values}
1867 A single value or a list of values.
1868 @item @var{structure-name}
1869 A symbol naming the structure
1870 @end table
1872 Defines the variant GBoxed structure. Slots of variant structures are defined the same way as the slots of ``simple'' cstructs. After the last slot, @code{variant-specification} may be used to specify the variants of the structure. For this, dispatching slot is specified. The value of this slot specifies which variant of structure is used. Each variant is specified by values of the dispatching slot, by its slots and its variants.
1874 Variant structure is represented in Lisp via a hierarchy on structures. For example, @code{GdkEvent} structure has variants @code{GdkEventAny}, @code{GdkEventButton}, @code{GdkEventMotion}. In Lisp, @code{event} structure is defined with all common fields of these structures and @code{event-button}, @code{event-motion} structures inherit from @code{event} structure.
1876 It is assumed that the variant of structures can be represented as C structures with fields of their ``parent'' structures prepended to them. This assumption breaks when structures include their ``parent'' structure as a first field (this changes the memory alignment and changes offsets of fields).
1878 This also defines the @var{name}-cstruct, @var{structure-name}-cstruct, @var{structure-name}-cunion CFFI structures definitions with equivalent structures (unions).
1880 For example, for these structures this assumption holds:
1881 @example
1882 union GdkEvent
1884   GdkEventType   type;
1885   GdkEventKey    key;
1886   GdkEventButton button;
1889 struct GdkEventKey @{
1890   GdkEventType type; //
1891   GdkWindow *window; // These fields are common
1892   gint8 send_event;  //
1893   guint32 time;
1894   guint state;
1895   guint keyval;
1896   ...
1899 struct GdkEventButton @{
1900   GdkEventType type; //
1901   GdkWindow *window; // These fields are common
1902   gint8 send_event;  //
1903   guint32 time;
1904   gdouble x;
1905   gdouble y;
1906   ...
1908 @end example
1910 Example:
1911 @lisp
1912 (define-g-boxed-variant-cstruct event "GdkEvent"
1913   (type event-type)
1914   (window (g-object gdk-window))
1915   (send-event (:boolean :int8))
1916   (:variant type
1917             ((:key-press :key-release) event-key
1918              (time :uint32)
1919              (state modifier-type)
1920              (keyval :uint)
1921              (length :int)
1922              (string (:string :free-from-foreign nil
1923                               :free-to-foreign nil))
1924              (hardware-keycode :uint16)
1925              (group :uint8)
1926              (is-modifier :uint))
1927             ((:button-press :2button-press :3button-press
1928               :button-release) event-button
1929              (time :uint32)
1930              (x :double)
1931              (y :double)
1932              (axes (fixed-array :double 2))
1933              (state :uint)
1934              (button :uint)
1935              (device (g-object device))
1936              (x-root :double)
1937              (y-root :double))
1938              ...))
1939 @end lisp
1941 This code defines following structures:
1942 @lisp
1943 (defstruct event
1944   type window send-event)
1946 (defstruct (event-key (:include event))
1947   time state keyval length string
1948   hardware-keycode group is-modifier)
1950 (defstruct (event-button (:include event))
1951   time x y axes state button device x-root y-root)
1952 @end lisp
1954 @node define-g-boxed-opaque
1955 @section define-g-boxed-opaque
1957 @Macro define-g-boxed-opaque
1958 @lisp
1959 (define-g-boxed-opaque name g-type-name &key alloc)
1960 @end lisp
1962 @table @var
1963 @item @var{name}
1964 A name of boxed type
1965 @item @var{g-type-name}
1966 A string; the name of GType
1967 @item @var{alloc}
1968 A form that when evaluated produces a pointer to newly allocated structure. This pointer should be copiable with @code{g_boxed_copy} and freeable with @code{g_boxed_free} function.
1969 @end table
1971 Defines a opaque boxed structure. A class named @var{name} is defined as a subclass of @code{g-boxed-opaque} class. Instances of this class contain pointers to corresponding structures. An @code{:after} method for @code{initialize-instance} generic function is defined that speciales on class @var{name}. This method either accepts a @code{:pointer} initarg or evaluates @var{alloc} form if @code{:pointer} is not specified; the resulting pointer is saved in instance; finalizer is registered to free the pointer when the garbage collectors deletes this object.
1973 Example:
1974 @lisp
1975 (defcfun gtk-tree-path-new :pointer)
1977 (define-g-boxed-opaque tree-path "GtkTreePath"
1978   :alloc (gtk-tree-path-new))
1979 @end lisp
1980 @node g-boxed-opaque
1981 @section g-boxed-opaque
1982 @Class g-boxed-opaque
1983 @lisp
1984 (defclass g-boxed-opaque ()
1985   ((pointer :initarg :pointer
1986             :initform nil
1987             :accessor g-boxed-opaque-pointer)))
1988 @end lisp
1990 A class that is the base class for wrappers of opaque structures. Contains a pointer to the wrapped opaque structure.
1992 Accessor function @code{g-boxed-opaque-pointer} is used to access the pointer. Pointer should not be modified directly, only read.
1993 @node define-boxed-opaque-accessor
1994 @section define-boxed-opaque-accessor
1995 @Macro define-boxed-opaque-accessor
1996 @lisp
1997 (define-boxed-opaque-accessor
1998   boxed-name accessor-name &key type reader writer)
1999 @end lisp
2001 @table @var
2002 @item @var{boxed-name}
2003 A symbol naming the opaque structure type for which the accessor is being defined
2004 @item @var{accessor-name}
2005 A symbol naming the accessor
2006 @item @var{type}
2007 A CFFI foreign type of the property for which the accessor is being defined
2008 @item @var{reader}
2009 A @code{NIL} or a string or a function designator for the reader function
2010 @item @var{writer}
2011 A @code{NIL} or a string or a function designator for the writer function
2012 @end table
2014 Defines the accessor named @var{accessor-name} for property of opaque structure named @var{boxed-name} of type specified by CFFI foreign-type @var{type}.
2016 @var{reader} is a string naming a foreign function of one argument of CFFI foreign-type @code{(g-boxed-foreign @var{boxed-name})} that returns a value of CFFI foreign-type @var{type}; or a function designator for a function that accepts a single argument - an instance of @code{g-boxed-opaque} class and returns the value of a property; or a @code{NIL} if the property is not readable.
2018 @var{writer} is a string naming a foreign function of two arguments: of types @var{type} and @code{(g-boxed-foreign @var{boxed-name})} (with the first argument being the new value and the second being the object); or a function designator for a function of two arguments: a new value and an instance of @code{g-boxed-opaque} class; or a @code{NIL} if the property is not writable.
2020 Example:
2021 @lisp
2022 (define-boxed-opaque-accessor text-iter text-iter-child-anchor
2023   :reader "gtk_text_iter_get_child_anchor" :type (g-object text-child-anchor))
2025 (define-boxed-opaque-accessor text-iter text-iter-tags
2026   :reader "gtk_text_iter_get_tags" :type (gslist (g-object text-tag) :free-from-foreign t))
2028 (define-boxed-opaque-accessor text-iter text-iter-chars-in-line
2029   :reader "gtk_text_iter_get_chars_in_line" :type :int)
2031 (define-boxed-opaque-accessor text-iter text-iter-offset
2032   :reader "gtk_text_iter_get_offset" :writer "gtk_text_iter_set_offset" :type :int)
2033 @end lisp
2035 @node boxed-related-symbols
2036 @section boxed-related-symbols
2038 @Function boxed-related-symbols
2039 @lisp
2040 (boxed-related-symbols name) @result{} symbols
2041 @end lisp
2043 @table @var
2044 @item @var{name}
2045 A symbol naming the boxed type
2046 @item @var{symbols}
2047 A list of symbols
2048 @end table
2050 This function returns the list of symbols that are related to GBoxed type @var{name}. These symbols are returned:
2051 @itemize
2052 @item name of boxed type
2053 @item name of all accessors of cstruct and variant-cstruct boxed types
2054 @item names of all variants of variant-cstruct boxed types
2055 @item names of constructors and copiers of cstruct and variant-cstruct boxed-types
2056 @end itemize
2058 Typical usage of this function is to export the symbols related to given boxed type.
2060 Example:
2061 @lisp
2062 (define-g-boxed-cstruct rectangle "GdkRectangle"
2063   (x :int :initform 0)
2064   (y :int :initform 0)
2065   (width :int :initform 0)
2066   (height :int :initform 0))
2068 (boxed-related-symbols 'rectangle)
2069 @result{}
2070 (RECTANGLE MAKE-RECTANGLE COPY-RECTANGLE RECTANGLE-X RECTANGLE-Y
2071  RECTANGLE-WIDTH RECTANGLE-HEIGHT)
2072 @end lisp
2074 @node GBoxed foreign type
2075 @section GBoxed foreign type
2077 @ForeignType g-boxed-foreign
2078 @lisp
2079 (g-boxed-foreign name &rest option*)
2081 option ::= :return
2082 @end lisp
2084 @table @var
2085 @item @var{name}
2086 Name of GBoxed type
2087 @item @var{option}
2088 Option of foreign type
2089 @item @code{:return}
2090 An option that identifies the foreign type which is used at return position (as foreign function return type or as a callback return type)
2091 @end table
2093 @code{g-boxed-foreign} type deals with marshaling data between Lisp code and foreign code. The marshaling follows the following principles:
2094 @itemize
2095 @item All operations on Lisp objects corresponding to GBoxed types are type-safe and should never lead to any form of memory corruption (if some operation is impossible due to e.g., pointer in opaque pointer wrapper being invalidated, error should be signalled)
2096 @item Lisp objects should not be manually managed and are properly reclaimed by garbage collector, leaving no memory leaks
2097 @item Foreign code can change objects that are passed to them as arguments. This is required for functions that operate by modifying their arguments
2098 @item Lisp code in callbacks can change objects that are passed as arguments. This is required to be able to implement interfaces that have functions that operate by modifying their arguments
2099 @end itemize
2101 The @code{:return} option is required to be able to properly manage memory of opaque pointer wrappers and propagate changes to foreign and lisp structures.
2103 In order to be able to correctly use @code{g-boxed-foreign} foreign type in callbacks, you should use @code{glib-defcallback}. This macro is a thin wrapper around @code{cffi:defcallback} that adds proper handling of @code{g-boxed-foreign} foreign types.
2105 Examples of usage:
2106 @lisp
2107 (define-vtable ("GtkTreeModel" c-gtk-tree-model)
2108   ...
2109   (tree-model-get-path-impl tree-model-get-path-cb
2110     (g-boxed-foreign tree-path :return) (tree-model g-object) (iter (g-boxed-foreign tree-iter)))
2111   (tree-model-get-value-impl tree-model-get-value-cb
2112     :void (tree-model g-object) (iter (g-boxed-foreign tree-iter)) (n :int) (value (:pointer g-value)))
2113   (tree-model-iter-next-impl tree-model-iter-next-cb
2114     :boolean (tree-model g-object) (iter (g-boxed-foreign tree-iter)))
2115   ...)
2117 (defcfun gtk-text-iter-forward-search :boolean
2118   (iter (g-boxed-foreign text-iter))
2119   (str (:string :free-to-foreign t))
2120   (flags text-search-flags)
2121   (match-start (g-boxed-foreign text-iter))
2122   (match-end (g-boxed-foreign text-iter))
2123   (limit (g-boxed-foreign text-iter)))
2124 @end lisp
2127 @node copy-boxed-slots-to-foreign
2128 @section copy-boxed-slots-to-foreign
2130 @Function copy-boxed-slots-to-foreign
2131 @lisp
2132 (copy-boxed-slots-to-foreign structure native-ptr &optional (type (and structure (type-of structure))))
2133 @end lisp
2135 @table @var
2136 @item @var{structure}
2137 A Lisp structure corresponding to some GBoxed type
2138 @item @var{native-ptr}
2139 A foreign pointer
2140 @item @code{type}
2141 Name of the GBoxed type. It is optional but may be included for optimization purposes
2142 @end table
2144 Copies the contents of @var{structure} to C structure pointed to by @var{native-ptr}. @var{type} is used to determine which slots and which cstruct definition should be used.
2146 Examples:
2147 @lisp
2148 (cffi:with-foreign-object (point-ptr 'gdk::point-cstruct)
2149   (gobject:copy-boxed-slots-to-foreign (gdk:make-point :x 10 :y 10) point-ptr 'gdk:point))
2150 @end lisp
2152 @node with-boxed-foreign-array
2153 @section with-boxed-foreign-array
2155 @Macro with-boxed-foreign-array
2156 @lisp
2157 (with-foreign-boxed-array (n-var array-var type values-seq) &body body)
2158 @end lisp
2160 @table @var
2161 @item @var{n-var}
2162 A variable that will contain the count of items in @var{values-seq}
2163 @item @var{array-var}
2164 A variable that will contain the pointer to array of C structures
2165 @item @var{type}
2166 A symbol that specifies the type of GBoxed structure
2167 @item @var{values-seq}
2168 An expression that returns the sequence of structures (list or array)
2169 @end table
2171 Evaluates the @var{body} within the scope and extent of foreign array that contains copies of structures that are returned by @var{values-seq}. Binds @var{n-var} to the length of @var{values-seq}, @var{array-var} to the pointer to array of structures.
2173 Examples:
2174 @lisp
2175 (defcfun gdk-region-polygon (g-boxed-foreign region :return)
2176   (points :pointer)
2177   (n-points :int)
2178   (fill-rule gdk-fill-rule))
2180 (defun region-from-polygon (points fill-rule)
2181   (with-foreign-boxed-array (n pts point points)
2182     (gdk-region-polygon pts n fill-rule)))
2183 @end lisp
2185 @node Generating type definitions by introspection
2186 @chapter Generating type definitions by introspection
2187 @menu
2188 * define-g-object-class::
2189 * define-g-interface::
2190 * define-g-enum::
2191 * define-g-flags::
2192 * get-g-enum-definition::
2193 * get-g-flags-definition::
2194 * get-g-interface-definition::
2195 * get-g-class-definition::
2196 * get-g-type-definition::
2197 * Specifying additional properties for CLOS classes::
2198 * Generating names for CLOS classes and accessors::
2199 * generate-types-hierarchy-to-file::
2200 @end menu
2202 CL-GTK2-GOBJECT includes facilities for automatically generating parts of bindings for libraries that use GObject type system.
2204 @node define-g-object-class
2205 @section define-g-object-class
2207 @Macro define-g-object-class
2208 @lisp
2209 (define-g-object-class g-type-name name
2210   (&key (superclass 'g-object) (export t) interfaces type-initializer)
2211   (&rest property*))
2213 property ::= (name accessor gname type readable writable)
2214 property ::= (:cffi name acessor type reader writer)
2215 @end lisp
2217 Parameters of @code{define-g-object-class}
2218 @table @var
2219 @item @var{superclass}
2220 A symbol naming the superclass of this class
2221 @item @var{export}
2222 Whether to export the name of the class and names of autogenerated properties names from the current package.
2223 @item @var{interfaces}
2224 A list of interfaces the this class implements
2225 @item @var{type-initializer}
2226 A string naming the type initiliazer function. It is usually named @code{class_get_type}.
2227 @item @var{properties}
2228 A list of slots of a class
2229 @end table
2231 Parameters of @code{property}:
2232 @table @var
2233 @item @var{name}
2234 A symbol naming the slot
2235 @item @var{accessor}
2236 A symbol naming the accessor function for this slot
2237 @item @var{gname}
2238 A string naming the property of GObject
2239 @item @var{type}
2240 A string naming the type of property of GObject (for GObject properties); or a symbol naming CFFI foreign type (for slots mapped to foreign accessors)
2241 @item @var{readable}
2242 A boolean specifying whether the slot can be read
2243 @item @var{writable}
2244 A boolean specifying whether the slot can be assigned to
2245 @item @var{reader}
2246 A string or a symbol naming getter function. See description of @code{gobject-class} metaclass for information.
2247 @item @var{writter}
2248 A string or a symbol naming setter function. See description of @code{gobject-class} metaclass for information.
2249 @end table
2251 Macro that expands to @code{defclass} for specified class. Additionally, if @code{export} is true, it exports accessor names and name of a class.
2253 Example:
2254 @lisp
2255 (define-g-object-class "GtkContainer" container
2256   (:superclass widget :export t :interfaces
2257                ("AtkImplementorIface" "GtkBuildable")
2258                :type-initializer "gtk_container_get_type")
2259   ((border-width container-border-width "border-width" "guint" t t)
2260    (resize-mode container-resize-mode "resize-mode" "GtkResizeMode" t t)
2261    (child container-child "child" "GtkWidget" nil t)
2262    (:cffi focus-child container-focus-child g-object "gtk_container_get_focus_child" "gtk_container_set_focus_child")
2263    (:cffi focus-vadjustment container-focus-vadjustment (g-object adjustment) "gtk_container_get_focus_vadjustment" "gtk_container_set_focus_vadjustment")
2264    (:cffi focus-hadjustment container-focus-hadjustment (g-object adjustment) "gtk_container_get_focus_hadjustment" "gtk_container_set_focus_hadjustment")))
2265 @end lisp
2267 @node define-g-interface
2268 @section define-g-interface
2270 @Macro define-g-interface
2271 @lisp
2272 (define-g-interface g-type-name name (&key (export t) type-initializer)
2273   &body property*)
2275 property ::= (name accessor gname type readable writable)
2276 property ::= (:cffi name acessor type reader writer)
2277 @end lisp
2279 Parameters of @code{define-g-interface}
2280 @table @var
2281 @item @var{export}
2282 Whether to export the name of the interface and names of autogenerated properties names from the current package.
2283 @item @var{type-initializer}
2284 A string naming the type initiliazer function. It is usually named @code{interface_get_type}.
2285 @item @var{properties}
2286 A list of slots of a interface
2287 @end table
2289 Parameters of @code{property}:
2290 @table @var
2291 @item @var{name}
2292 A symbol naming the slot
2293 @item @var{accessor}
2294 A symbol naming the accessor function for this slot
2295 @item @var{gname}
2296 A string naming the property of GObject
2297 @item @var{type}
2298 A string naming the type of property of GObject (for GObject properties); or a symbol naming CFFI foreign type (for slots mapped to foreign accessors)
2299 @item @var{readable}
2300 A boolean specifying whether the slot can be read
2301 @item @var{writable}
2302 A boolean specifying whether the slot can be assigned to
2303 @item @var{reader}
2304 A string or a symbol naming getter function. See description of @code{gobject-class} metaclass for information.
2305 @item @var{writter}
2306 A string or a symbol naming setter function. See description of @code{gobject-class} metaclass for information.
2307 @end table
2309 Macro that expands to @code{defclass} for specified interface. Additionally, if @code{export} is true, it exports accessor names and name of a interface.
2311 Example:
2312 @lisp
2313 (define-g-interface "GtkFileChooser" file-chooser
2314   (:export t :type-initializer "gtk_file_chooser_get_type")
2315   (do-overwrite-confirmation file-chooser-do-overwrite-confirmation "do-overwrite-confirmation" "gboolean" t t)
2316   (select-multiple file-chooser-select-multiple "select-multiple" "gboolean" t t)
2317   (filter file-chooser-filter "filter" "GtkFileFilter" t t)
2318   (local-only file-chooser-local-only "local-only" "gboolean" t t)
2319   (preview-widget file-chooser-preview-widget "preview-widget" "GtkWidget" t t)
2320   (use-preview-label file-chooser-use-preview-label "use-preview-label" "gboolean" t t)
2321   (preview-widget-active file-chooser-preview-widget-active "preview-widget-active" "gboolean" t t)
2322   (file-system-backend file-chooser-file-system-backend "file-system-backend" "gchararray" nil nil)
2323   (extra-widget file-chooser-extra-widget "extra-widget" "GtkWidget" t t)
2324   (show-hidden file-chooser-show-hidden "show-hidden" "gboolean" t t)
2325   (action file-chooser-action "action" "GtkFileChooserAction" t t)
2326   (:cffi current-name file-chooser-current-name
2327    (:string :free-to-foreign t :encoding :utf-8) nil "gtk_file_chooser_set_current_name")
2328   (:cffi filename file-chooser-filename
2329    (g-string :free-from-foreign t :free-to-foreign t)
2330    "gtk_file_chooser_get_filename" "gtk_file_chooser_set_filename")
2331   (:cffi current-folder file-chooser-current-folder
2332    (g-string :free-from-foreign t :free-to-foreign t)
2333    "gtk_file_chooser_get_current_folder"
2334    "gtk_file_chooser_set_current_folder")
2335   (:cffi uri file-chooser-uri
2336    (g-string :free-from-foreign t :free-to-foreign t)
2337    "gtk_file_chooser_get_uri" "gtk_file_chooser_set_uri")
2338   (:cffi current-folder-uri file-chooser-current-folder-uri
2339    (g-string :free-from-foreign t :free-to-foreign t)
2340    "gtk_file_chooser_get_current_folder_uri"
2341    "gtk_file_chooser_set_current_folder_uri")
2342   (:cffi preview-filename file-chooser-preview-filename
2343    (g-string :free-from-foreign t :free-to-foreign t)
2344    "gtk_file_chooser_get_preview_filename" nil)
2345   (:cffi preview-uri file-chooser-preview-uri
2346    (g-string :free-from-foreign t :free-to-foreign t)
2347    "gtk_file_chooser_get_preview_uri" nil))
2348 @end lisp
2350 @node define-g-enum
2351 @section define-g-enum
2353 @Macro define-g-enum
2354 @lisp
2355 (define-g-enum g-name name (&key (export t) type-initializer) &body value*)
2357 value ::= :keyword
2358 value ::= (:keyword integer)
2359 @end lisp
2361 @table @var
2362 @item @var{g-name}
2363 A string naming the GEnum type
2364 @item @var{name}
2365 A symbol naming the CFFI enumeration type
2366 @item @var{export}
2367 A boolean indicating whether to export @code{name}
2368 @item @var{type-initializer}
2369 A string naming the foreign type initializer function. Usually named @code{enum_get_type}.
2370 @end table
2372 Macro that defines CFFI enumeration, registers it with GValue, and calls the type initializer.
2374 Example:
2375 @lisp
2376 (define-g-enum "GtkTextDirection" text-direction
2377   (:export t :type-initializer "gtk_text_direction_get_type")
2378   (:none 0) (:ltr 1) (:rtl 2))
2380 (define-g-enum "GtkSizeGroupMode" size-group-mode
2381  (:export t :type-initializer "gtk_size_group_mode_get_type")
2382  :none :horizontal :vertical :both)
2383 @end lisp
2385 @node define-g-flags
2386 @section define-g-flags
2388 @Macro define-g-flags
2389 @lisp
2390 (define-g-flags g-name name (&key (export t) type-initializer) &body value*)
2392 value ::= :keyword
2393 value ::= (:keyword integer)
2394 @end lisp
2396 @table @var
2397 @item @var{g-name}
2398 A string naming the GFlags type
2399 @item @var{name}
2400 A symbol naming the CFFI flags type
2401 @item @var{export}
2402 A boolean indicating whether to export @code{name}
2403 @item @var{type-initializer}
2404 A string naming the foreign type initializer function. Usually named @code{flags_get_type}.
2405 @end table
2407 Macro that defines CFFI bitfield, registers it with GValue, and calls the type initializer.
2409 Example:
2410 @lisp
2411 (define-g-flags "GtkAttachOptions" attach-options
2412   (:export t :type-initializer "gtk_attach_options_get_type")
2413   (:expand 1) (:shrink 2) (:fill 4))
2415 (define-g-flags "GtkButtonAction" button-action
2416   (:export t :type-initializer "gtk_button_action_get_type")
2417   :ignored :selects :drags :expands)
2418 @end lisp
2420 @node get-g-enum-definition
2421 @section get-g-enum-definition
2423 @Function get-g-enum-definition
2424 @lisp
2425 (get-g-enum-definition type &optional lisp-name-package) @result{} definition
2426 @end lisp
2428 @table @var
2429 @item @var{type}
2430 A string naming the GEnum type
2431 @item @var{lisp-name-package}
2432 A package that will be used as a package for generated symbols (enum name). If not specified, symbols are interned in @code{*package*}
2433 @item @var{definition}
2434 A Lisp form that when evaluated defines the GEnum.
2435 @end table
2437 Uses GObject introspection capabilities to automatically produce the definition of GEnum. The foreign library that defines the enum type should be loaded.
2439 See @ref{Generating names for CLOS classes and accessors} for information about used method for generating names.
2441 Example:
2442 @lisp
2443 (get-g-enum-definition "GtkDirectionType")
2444 @result{}
2445 (DEFINE-G-ENUM "GtkDirectionType" GTK-DIRECTION-TYPE
2446                (:EXPORT T :TYPE-INITIALIZER "gtk_direction_type_get_type")
2447                (:TAB-FORWARD 0) (:TAB-BACKWARD 1) (:UP 2) (:DOWN 3) (:LEFT 4)
2448                (:RIGHT 5))
2449 @end lisp
2451 @node get-g-flags-definition
2452 @section get-g-flags-definition
2454 @Function get-g-flags-definition
2455 @lisp
2456 (get-g-flags-definition type &optional lisp-name-package) @result{} definition
2457 @end lisp
2459 @table @var
2460 @item @var{type}
2461 A string naming the GFlags type
2462 @item @var{lisp-name-package}
2463 A package that will be used as a package for generated symbols (flags name). If not specified, symbols are interned in @code{*package*}
2464 @item @var{definition}
2465 A Lisp form that when evaluated defines the GFlags.
2466 @end table
2468 Uses GObject introspection capabilities to automatically produce the definition of GFlags. The foreign library that defines the flags type should be loaded.
2470 See @ref{Generating names for CLOS classes and accessors} for information about used method for generating names.
2472 Example:
2473 @lisp
2474 (get-g-flags-definition "GtkCalendarDisplayOptions")
2475 @result{}
2476 (DEFINE-G-FLAGS "GtkCalendarDisplayOptions" GTK-CALENDAR-DISPLAY-OPTIONS
2477                 (:EXPORT T :TYPE-INITIALIZER
2478                  "gtk_calendar_display_options_get_type")
2479                 (:SHOW-HEADING 1) (:SHOW-DAY-NAMES 2) (:NO-MONTH-CHANGE 4)
2480                 (:SHOW-WEEK-NUMBERS 8) (:WEEK-START-MONDAY 16)
2481                 (:SHOW-DETAILS 32))
2482 @end lisp
2484 @node get-g-interface-definition
2485 @section get-g-interface-definition
2487 @Function get-g-interface-definition
2488 @lisp
2489 get-g-interface-definition type &optional lisp-name-package) @result{} definition
2490 @end lisp
2492 @table @var
2493 @item @var{type}
2494 A string naming the GInterface type
2495 @item @var{lisp-name-package}
2496 A package that will be used as a package for generated symbols (type name, accessor names). If not specified, symbols are interned in @code{*package*}
2497 @item @var{definition}
2498 A Lisp form that when evaluated defines the GInterface.
2499 @end table
2501 Uses GObject introspection capabilities to automatically produce the definition of GInterface. The foreign library that defines the GInterface type should be loaded.
2503 See @ref{Generating names for CLOS classes and accessors} for information about used method for generating names.
2505 Example:
2506 @lisp
2507 (get-g-interface-definition "GtkActivatable")
2508 @result{}
2509 (DEFINE-G-INTERFACE "GtkActivatable" GTK-ACTIVATABLE
2510                     (:EXPORT T :TYPE-INITIALIZER "gtk_activatable_get_type")
2511                     (USE-ACTION-APPEARANCE
2512                      GTK-ACTIVATABLE-USE-ACTION-APPEARANCE
2513                      "use-action-appearance" "gboolean" T T)
2514                     (RELATED-ACTION GTK-ACTIVATABLE-RELATED-ACTION
2515                      "related-action" "GtkAction" T T))
2516 @end lisp
2518 @node get-g-class-definition
2519 @section get-g-class-definition
2521 @Function get-g-class-definition
2522 @lisp
2523 (get-g-class-definition type &optional lisp-name-package) @result{} definition
2524 @end lisp
2526 @table @var
2527 @item @var{type}
2528 A string naming the GObject type
2529 @item @var{lisp-name-package}
2530 A package that will be used as a package for generated symbols (type name, accessor names). If not specified, symbols are interned in @code{*package*}
2531 @item @var{definition}
2532 A Lisp form that when evaluated defines the GObject.
2533 @end table
2535 Uses GObject introspection capabilities to automatically produce the definition of GClass. The foreign library that defines the GObject type should be loaded.
2537 See @ref{Generating names for CLOS classes and accessors} for information about used method for generating names.
2539 Example:
2540 @lisp
2541 (get-g-class-definition "GtkButton")
2542 @result{}
2543 (DEFINE-G-OBJECT-CLASS "GtkButton" GTK-BUTTON
2544                        (:SUPERCLASS GTK-BIN :EXPORT T :INTERFACES
2545                         ("AtkImplementorIface" "GtkActivatable" "GtkBuildable")
2546                         :TYPE-INITIALIZER "gtk_button_get_type")
2547                        ((LABEL GTK-BUTTON-LABEL "label" "gchararray" T T)
2548                         (IMAGE GTK-BUTTON-IMAGE "image" "GtkWidget" T T)
2549                         (RELIEF GTK-BUTTON-RELIEF "relief" "GtkReliefStyle" T
2550                          T)
2551                         (USE-UNDERLINE GTK-BUTTON-USE-UNDERLINE "use-underline"
2552                          "gboolean" T T)
2553                         (USE-STOCK GTK-BUTTON-USE-STOCK "use-stock" "gboolean"
2554                          T T)
2555                         (FOCUS-ON-CLICK GTK-BUTTON-FOCUS-ON-CLICK
2556                          "focus-on-click" "gboolean" T T)
2557                         (XALIGN GTK-BUTTON-XALIGN "xalign" "gfloat" T T)
2558                         (YALIGN GTK-BUTTON-YALIGN "yalign" "gfloat" T T)
2559                         (IMAGE-POSITION GTK-BUTTON-IMAGE-POSITION
2560                          "image-position" "GtkPositionType" T T)))
2561 @end lisp
2563 @node get-g-type-definition
2564 @section get-g-type-definition
2566 @Function get-g-type-definition
2567 @lisp
2568 (get-g-class-definition type &optional lisp-name-package) @result{} definition
2569 @end lisp
2571 @table @var
2572 @item @var{type}
2573 A string naming the GEnum, GFlags, GInterface or GObject type
2574 @item @var{lisp-name-package}
2575 A package that will be used as a package for generated symbols (type name, accessor names). If not specified, symbols are interned in @code{*package*}
2576 @item @var{definition}
2577 A Lisp form that when evaluated defines the corresponding Lisp type.
2578 @end table
2580 Depending on a kind of @var{type}, calls @ref{get-g-enum-definition} or @ref{get-g-flags-definition} or @ref{get-g-interface-definition} or @ref{get-g-class-definition}.
2582 @node Specifying additional properties for CLOS classes
2583 @section Specifying additional properties for CLOS classes
2585 Some properties are not exposed through GObject introspection facilities, but are rather present as a pair of functions (@code{class_get_property}, @code{class_set_property}). @code{gobject-class} metaclass supports such properties. For these properties to be included in automatically generated class definitions, they should be made known to the generator.
2587 Definitions generator uses variable @code{*additional-properties*} to get this information.
2589 Variable @code{*additional-properties*} contains a plist that maps GType names to a list of properties definitions (See @ref{define-g-object-class} for syntax of properties definitions).
2591 To supply the bindings generator with this information, bind @code{*additional-properties*} to such list when the generator is run.
2593 Example:
2594 @lisp
2595 (("GtkTreeViewColumn"
2596   (:cffi gtk::tree-view
2597          gtk::tree-view-column-tree-view
2598          g-object "gtk_tree_view_column_get_tree_view" nil)
2599   (:cffi gtk::sort-column-id
2600          gtk::tree-view-column-sort-column-id
2601          :int "gtk_tree_view_column_get_sort_column_id" "gtk_tree_view_column_set_sort_column_id")
2602   (:cffi gtk::cell-renderers
2603          gtk::tree-view-column-cell-renderers
2604          (glist g-object  :free-from-foreign t) "gtk_tree_view_column_get_cell_renderers" nil))
2605  ("GtkTreeSelection"
2606   (:cffi gtk::mode
2607          gtk::tree-selection-mode
2608          gtk::selection-mode "gtk_tree_selection_get_mode" "gtk_tree_selection_set_mode")
2609   (:cffi gtk::select-function
2610          gtk::tree-selection-select-function
2611          nil gtk::tree-selection-get-selection-function gtk::tree-selection-set-select-function)))
2612 @end lisp
2614 @node Generating names for CLOS classes and accessors
2615 @section Generating names for CLOS classes and accessors
2617 Names of types are generated by mapping @code{CamelCaseNames} to @code{dash-separated-names} and interning them in specified package. Additionally, prefix from beginning of the name may be stripped (@code{"GtkWidget"} has prefix @code{"Gtk"}, after stripping it maps to @code{widget}). Some names may require special processing (e.g., @code{"GObject"}, @code{"GInitiallyUnowned"} should map to class names in @code{gobject} package; @code{"GtkWindow"} and @code{"GdkWindow"} should receive different @code{symbol-name}s so that they can both be imported in one package).
2619 Accessors for slots are generated by concatenating class name, dash and slot name, producing names like @code{class-slot}: @code{container-child}, @code{button-label}, etc.
2621 Name generation affected by following variables:
2622 @itemize
2623 @item @var{*strip-prefix*}
2624 A string variable specifying the prefix that should to be stripped from the names to generate symbols (e.g., if @code{(equal "Gtk" *strip-prefix*)}, then type named @code{"GtkWidget"} will map to class named @code{widget}.
2625 @item @var{*lisp-name-exceptions*}
2626 A plist mapping from strings (type names) to symbols (class names) that have special name processing.
2627 Example:
2628 @lisp
2629 `(("GObject" gobject:g-object)
2630   ("GtkObject" ,(intern "GTK-OBJECT" (find-package :gtk)))
2631   ("GInitiallyUnowned" gobject::g-initially-unowned)
2632   ("GtkWindow" ,(intern "GTK-WINDOW" (find-package :gtk)))
2633   ("GtkUIManager" ,(intern "UI-MANAGER" (find-package :gtk)))
2634   ("GtkUIManagerItemType" ,(intern "UI-MANAGER-ITEM-TYPE" (find-package :gtk))))
2635 @end lisp
2636 @end itemize
2638 @node generate-types-hierarchy-to-file
2639 @section generate-types-hierarchy-to-file
2641 @Function generate-types-hierarchy-to-file
2642 @lisp
2643 (generate-types-hierarchy-to-file file
2644                                   root-type
2645                                   &key include-referenced
2646                                   prefix
2647                                   package
2648                                   exceptions
2649                                   prologue
2650                                   interfaces
2651                                   enums
2652                                   flags
2653                                   objects
2654                                   exclusions
2655                                   additional-properties)
2656 @end lisp
2658 @table @var
2659 @item @var{file}
2660 A string or pathname naming the file, or a stream.
2661 @item @var{root-type}
2662 A GType designator for a root type. All types that inherit from this type will be defined.
2663 @item @var{&key include-referenced}
2664 A boolean. Specifies whether referenced types should be included. Type is referenced if it is an interface or a type of property of type included in generation
2665 @item @var{prefix}
2666 A string naming the prefix that should be removed from the beginning of names
2667 @item @var{package}
2668 A package which will contain generated names of types, slots and accessors. It will also be the current package when the definitions are written to file
2669 @item @var{exceptions}
2670 A plist that maps GType names to their Lisp names.
2671 See @ref{Generating names for CLOS classes and accessors} for more info on exceptions from name generation mechanism
2672 @item @var{prologue}
2673 A string that will be included verbatim in generated code file
2674 @item @var{interfaces}
2675 Additional list of interfaces that will also be included in generation
2676 @item @var{enums}
2677 Additional list of enums that will also be included in generation
2678 @item @var{flags}
2679 Additional list of flags that will also be included in generation
2680 @item @var{objects}
2681 Additional list of object types that will also be included in generation
2682 @item @var{exclusions}
2683 A list of GType names that will be excluded from generation
2684 @item @var{additional-properties}
2685 A plist of properties definitions that will be added to generated classes.
2686 See @ref{Specifying additional properties for CLOS classes} for more information.
2687 @end table
2689 Generates definitions for all types in a type hierarchy. Recursively scan types hierarchy (starting from @code{root} and @code{objects} and @code{interfaces}) (except types that were specifically excluded) and generate defintion for every mentioned type. Parameters control various aspects of definition generation.
2691 Example of usage:
2692 @lisp
2693 (generate-types-hierarchy-to-file
2694  "gtk.generated-classes.lisp"
2695  "GtkObject"
2696  :include-referenced t
2697  :prefix "Gtk"
2698  :package (or (find-package :gtk) (make-package :gtk))
2699  :exceptions `(("GObject" gobject:g-object)
2700                ("GtkObject" ,(intern "GTK-OBJECT" (find-package :gtk)))
2701                ("GInitiallyUnowned" gobject::g-initially-unowned)
2702                ("GtkWindow" ,(intern "GTK-WINDOW" (find-package :gtk)))
2703                ("GtkUIManager" ,(intern "UI-MANAGER" (find-package :gtk)))
2704                ("GtkUIManagerItemType" ,(intern "UI-MANAGER-ITEM-TYPE" (find-package :gtk))))
2705  :prologue (format nil "(in-package :gtk)")
2706  :interfaces '("GtkBuildable" "GtkCellEditable" ...)
2707  :objects '("GtkSettings" "GtkRcStyle" ...)
2708  :flags '("GtkTextSearchFlags" "GtkAccelFlags" ...)
2709  :enums '("GtkTextDirection" "GtkSizeGroupMode" ...)
2710  :exclusions '("PangoStretch" "PangoVariant" ...)
2711  :additional-properties
2712  '(("GtkTreeViewColumn"
2713     (:cffi
2714      gtk::tree-view
2715      gtk::tree-view-column-tree-view
2716      g-object
2717      "gtk_tree_view_column_get_tree_view"
2718      nil)
2719     ...)
2720    ...))
2721 @end lisp