9ef0d4ccc1a7d3a7e892348c3834745501605118
[cl-gtk2.git] / doc / gobject.texi
1 \input texinfo  @c -*-texinfo-*-
2 @c %**start of header (This is for running texinfo on a region.)
3 @setfilename gobject.info
4 @settitle CL-Gtk2-GObject
5 @c %**end of header (This is for running texinfo on a region.)
6
7 @c @documentencoding utf-8
8
9 @macro Function {args}
10 @defun \args\
11 @end defun
12 @end macro
13
14 @macro Macro {args}
15 @defmac \args\
16 @end defmac
17 @end macro
18
19 @macro Struct {args}
20 @deftp {Structure} \args\
21 @end deftp
22 @end macro
23
24 @macro Class {args}
25 @deftp {Class} \args\
26 @end deftp
27 @end macro
28
29 @macro Accessor {args}
30 @deffn {Accessor} \args\
31 @end deffn
32 @end macro
33
34 @macro GenericFunction {args}
35 @deffn {Generic Function} \args\
36 @end deffn
37 @end macro
38
39 @macro ForeignType {args}
40 @deftp {Foreign Type} \args\
41 @end deftp
42 @end macro
43
44 @macro Variable {args}
45 @defvr {Special Variable} \args\
46 @end defvr
47 @end macro
48
49 @macro Condition {args}
50 @deftp {Condition Type} \args\
51 @end deftp
52 @end macro
53
54 @macro cffi
55 @acronym{CFFI}
56 @end macro
57
58 @macro impnote {text}
59 @quotation
60 @strong{Implementor's note:} @emph{\text\}
61 @end quotation
62 @end macro
63
64 @c Info "requires" that x-refs end in a period or comma, or ) in the
65 @c case of @pxref.  So the following implements that requirement for
66 @c the "See also" subheadings that permeate this manual, but only in
67 @c Info mode.
68 @ifinfo
69 @macro seealso {name}
70 @ref{\name\}.
71 @end macro
72 @end ifinfo
73
74 @ifnotinfo
75 @alias seealso = ref
76 @end ifnotinfo
77
78 @c Typeset comments in roman font for the TeX output.
79 @iftex
80 @alias lispcmt = r
81 @end iftex
82 @ifnottex
83 @alias lispcmt = asis
84 @end ifnottex
85
86 @c My copy of makeinfo is not generating any HTML for @result{} for
87 @c some odd reason. (It certainly used to...)
88 @ifhtml
89 @macro result
90 =>
91 @end macro
92 @end ifhtml
93
94 @c Similar macro to @result. Its purpose is to work around the fact
95 @c that ⇒ does not work properly inside @lisp.
96 @ifhtml
97 @macro res
98 @html
99 ⇒
100 @end html
101 @end macro
102 @end ifhtml
103
104 @ifnothtml
105 @alias res = result
106 @end ifnothtml
107
108 @c ============================= Macros =============================
109
110
111 @c Show types, functions, and concepts in the same index.
112 @syncodeindex tp cp
113 @syncodeindex fn cp
114
115 @titlepage
116 @title CL-GTK2
117 @subtitle A Common Lisp binding for Gtk+
118 @subtitle GObject
119 @author Dmitry Kalyanov
120 @end titlepage
121
122 @contents
123
124 @ifnottex
125 @node Top
126 @top cl-gtk2-gobject
127 @end ifnottex
128
129 @menu
130 * Introduction::
131 * Installation::
132 * GType designator::
133 * Type hierarchy and type relations::
134 * Object types information::
135 * Enum types information::
136 * Using GValues::
137 * Stable pointers::
138 * Closures::
139 * GObject low-level::
140 * GObject high-level::
141 * Creating GObjects classes and implementing GInterfaces::
142 * GBoxed::
143 * Generating type definitions by introspection::
144 @end menu
145
146 @node Introduction
147 @chapter Introduction
148
149 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.
150
151 The purpose of CL-GTK2-GObject is to ease the creation of binding for libraries based on GObject.
152
153 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.
154
155 CL-GTK2-GObject is logically split into several layers:
156 @itemize
157 @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.
158 @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.
159 @item High-level GObject integration. This layer includes support for interoperability between CLOS and GObject and automatic generation of corresponding definitions.
160 @end itemize
161
162 Naturally, users of CL-GTK2-GObject should use the high-level GObject integration, but occasionaly it may be necessary to use lower-level functionality.
163
164 @node Installation
165 @chapter Installation
166
167 CL-GTK2-GObject comes as a part of CL-GTK2 bindings that are avaiable at its @uref{http://common-lisp.net/project/cl-gtk2/,,website}.
168
169 To use the CL-GTK2-GObject, download and install CL-GTK2 bindings and load the ASDF system @code{cl-gtk2-glib}.
170
171 CL-GTK2-GObject defines two packages: @code{gobject} and @code{gobject.ffi}. The @code{gobject.ffi} package contains definitions for low-level CFFI imports. The @code{gobject} package contains symbols for external API of this GObject binding.
172
173 @node GType designator
174 @chapter GType designator
175
176 @menu
177 * g-type-string::
178 * g-type-numeric::
179 * g-type=::
180 * g-type/=::
181 @end menu
182
183 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 designators. GType designator is an integer (equal to corresponding GType identifier) or a string (equal to the name of corresponding type). The important difference between GType and GType designator is that GType values may change between program runs (all GTypes except fundamental GTypes will change values), but string GType designators do not change (because names of types do not change). As such, if ever GType must be saved in a code, string GType designator should be preferred.
184
185 An example of GType designator is a string @code{"GObject"} and the numeric value 80 that corresponds to it.
186
187 Some of the types are fundamental and have constant integer values. They are identified by constants (strings in parentheses are corresponding type names):
188 @itemize
189 @item @code{+g-type-invalid+}. An invalid GType used as error return value in some functions which return a GType.
190 @item @code{+g-type-void+} ("void"). A fundamental type which is used as a replacement for the C @code{void} return type.
191 @item @code{+g-type-interface+} ("GInterface"). The fundamental type from which all interfaces are derived.
192 @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}.
193 @item @code{+g-type-uchar+} ("guchar"). The fundamental type corresponding to @code{guchar}.
194 @item @code{+g-type-boolean+} ("gboolean"). The fundamental type corresponding to @code{gboolean}.
195 @item @code{+g-type-int+} ("gint"). The fundamental type corresponding to @code{gint}.
196 @item @code{+g-type-uint+} ("guint"). The fundamental type corresponding to @code{guint}.
197 @item @code{+g-type-long+} ("glong"). The fundamental type corresponding to @code{glong}.
198 @item @code{+g-type-ulong+} ("gulong"). The fundamental type corresponding to @code{gulong}.
199 @item @code{+g-type-int64+} ("gint64"). The fundamental type corresponding to @code{gint64}.
200 @item @code{+g-type-uint64+} ("guint64"). The fundamental type corresponding to @code{guint64}.
201 @item @code{+g-type-enum+} ("GEnum"). The fundamental type from which all enumeration types are derived.
202 @item @code{+g-type-flags+} ("GFlags"). The fundamental type from which all flags types are derived.
203 @item @code{+g-type-float+} ("gfloat"). The fundamental type corresponding to @code{gfloat}.
204 @item @code{+g-type-double+} ("gdouble"). The fundamental type corresponding to @code{gdouble}.
205 @item @code{+g-type-string+} ("gchararray"). The fundamental type corresponding to null-terminated C strings.
206 @item @code{+g-type-pointer+} ("gpointer"). The fundamental type corresponding to @code{gpointer}.
207 @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.
208 @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}.
209 @item @code{+g-type-object+} ("GObject"). The fundamental type for GObject.
210 @end itemize
211
212 Functions @ref{g-type-string} and @ref{g-type-numeric} return the numeric and string representations of GType designators (given any of them). Functions @ref{g-type=} and @ref{g-type/=} check types for equality.
213
214 Invalid type (the GType that does not exist) is identified as a 0 or @code{NIL}.
215
216 @lisp
217 (g-type-numeric "GObject") @result{} 80
218 (g-type-numeric 80) @result{} 80
219 (g-type-string "GObject") @result{} "GObject"
220 (g-type-string 80) @result{} "GObject"
221 (g-type-numeric "GtkWidget") @result{} 6905648 ;;Will be different on each run
222 @end lisp
223
224 @node g-type-string
225 @section g-type-string
226
227 @Function g-type-string
228 @lisp
229 (g-type-string g-type-designator) @result{} name
230 @end lisp
231
232 @table @var
233 @item @var{g-type-designator}
234 The GType designator for the GType
235 @item @var{name}
236 The name of GType
237 @end table
238
239 Returns the name of GType.
240
241 @node g-type-numeric
242 @section g-type-numeric
243
244 @Function g-type-numeric
245 @lisp
246 (g-type-numeric g-type-designator) @result{} GType
247 @end lisp
248
249 @table @var
250 @item @var{g-type-designator}.
251 The GType designator for the GType.
252 @item @var{GType}
253 The numeric identifier of GType
254 @end table
255
256 Returns the numeric identifier of GType
257
258 @node g-type=
259 @section g-type=
260
261 @Function g-type=
262 @lisp
263 (g-type= type-1 type-2) @result{} eq
264 @end lisp
265
266 @table @var
267 @item @var{type-1}
268 A GType designator
269 @item @var{type-2}
270 A GType designator
271 @item @var{eq}
272 A boolean that is true if @code{type-1} and @code{type-2} designate the same type.
273 @end table
274
275 @node g-type/=
276 @section g-type/=
277
278 @Function g-type/=
279 @lisp
280 (g-type/= type-1 type-2) @result{} eq
281 @end lisp
282
283 @table @var
284 @item @var{type-1}
285 A GType designator
286 @item @var{type-2}
287 A GType designator
288 @item @var{eq}
289 A boolean that is true if @code{type-1} and @code{type-2} designate different types.
290 @end table
291
292 @node Type hierarchy and type relations
293 @chapter Type hierarchy and type relations
294
295 @menu
296 * g-type-children::
297 * g-type-parent::
298 * g-type-fundamental::
299 * g-type-depth::
300 * g-type-next-base::
301 @end menu
302
303 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.
304
305 There are functions to query some specific information:
306 @itemize
307 @item @code{g-type-fundamental} retrieves the fundamental type for given type
308 @item @code{g-type-depth} calculates the depth of the type in type hierarchy
309 @item @code{g-type-next-base} calculates the first step in the path from base type to descendent type
310 @end itemize
311
312 @node g-type-children
313 @section g-type-children
314
315 @Function g-type-children
316 @lisp
317 (g-type-children type) @result{} children
318 @end lisp
319
320 @table @var
321 @item @var{type}
322 A GType designator
323 @item @var{children}
324 A list of GType designators
325 @end table
326
327 Returns the list of descendent types.
328
329 Example:
330 @lisp
331 (g-type-children "GtkButton")
332 @result{}
333 ("GtkToggleButton" "GtkColorButton" "GtkFontButton" "GtkLinkButton" "GtkScaleButton")
334 @end lisp
335
336 @node g-type-parent
337 @section g-type-parent
338
339 @Function g-type-parent
340 @lisp
341 (g-type-parent type) @result{} parent
342 @end lisp
343
344 @table @var
345 @item @var{type}
346 A GType designator
347 @item @var{parent}
348 A GType designator
349 @end table
350
351 Returns the parent of @code{type}.
352
353 Example:
354 @lisp
355 (g-type-parent "GtkToggleButton")
356 @result{}
357 "GtkButton"
358 @end lisp
359
360 @node g-type-fundamental
361 @section g-type-fundamental
362
363 @Function g-type-fundamental
364 @lisp
365 (g-type-fundamental type) @result{} fundamental-type
366 @end lisp
367
368 @table @var
369 @item @var{type}
370 A GType designator
371 @item @var{fundamental-type}
372 A GType designator for one of the fundamental types
373 @end table
374
375 Returns the fundamental type that is the ancestor of @code{type}.
376
377 Example:
378 @lisp
379 (g-type-fundamental "GtkButton") @result{} "GObject"
380
381 (g-type-fundamental "GtkWindowType") @result{} "GEnum"
382
383 (g-type-fundamental "GdkEvent") @result{} "GBoxed"
384 @end lisp
385
386 @node g-type-depth
387 @section g-type-depth
388
389 @Function g-type-depth
390 @lisp
391 (g-type-depth type) @result{} depth
392 @end lisp
393
394 @table @var
395 @item @var{type}
396 A GType designator
397 @item @var{depth}
398 An integer
399 @end table
400
401 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.
402
403 Example:
404 @lisp
405 (g-type-depth "GObject") @result{} 1
406 (g-type-depth "GInitiallyUnowned") @result{} 2
407 @end lisp
408
409 @node g-type-next-base
410 @section g-type-next-base
411
412 @Function g-type-next-base
413 @lisp
414 (g-type-next-base leaf-type root-type) @result{} base-type
415 @end lisp
416
417 @table @var
418 @item @var{leaf-type}
419 A GType designator
420 @item @var{root-type}
421 A GType designator
422 @item @var{base-type}
423 A GType designator
424 @end table
425
426 Returns the next type that should be traversed from @code{root-type} in order to reach @code{leaf-type}. E.g., given type hierarchy:
427 @lisp
428 + GObject
429  \
430   + GInitiallyUnowned
431    \
432     + GtkObject
433     |\
434     | + GtkAdjustment
435      \
436       + GtkWidget
437        \
438         + GtkContainer
439          \
440           + GtkTable
441 @end lisp
442
443 the following will be returned:
444
445 @lisp
446 (g-type-next-base "GtkTable" "GObject") @result{} "GInitiallyUnowned"
447 (g-type-next-base "GtkTable" "GInitiallyUnowned") @result{} "GtkObject"
448 (g-type-next-base "GtkTable" "GtkObject") @result{} "GtkWidget"
449 (g-type-next-base "GtkTable" "GtkWidget") @result{} "GtkContainer"
450 (g-type-next-base "GtkTable" "GtkContainer") @result{} "GtkTable"
451 @end lisp
452
453 @node Object types information
454 @chapter Object types information
455 @menu
456 * g-class-property-definition::
457 * class-properties::
458 * class-property-info::
459 * interface-properties::
460 * signal-info::
461 * type-signals::
462 * parse-signal-name::
463 * query-signal-info::
464 * g-type-interfaces::
465 * g-type-interface-prerequisites::
466 @end menu
467
468 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.
469
470 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.
471
472 @node g-class-property-definition
473 @section g-class-property-definition
474
475 @Struct g-class-property-definition
476 @lisp
477 (defstruct g-class-property-definition
478   name
479   type
480   readable
481   writable
482   constructor
483   constructor-only
484   owner-type)
485 @end lisp
486
487 @table @var
488 @item @var{name}
489 A string that names the property
490 @item @var{type}
491 A GType designator. Identifies the type of the property
492 @item @var{readable}
493 A boolean. Identifies whether the property can be read
494 @item @var{writable}
495 A boolean. Identifies whether the property can be assigned
496 @item @var{constructor}
497 A boolean. Identifies whether constructor of object accepts this property
498 @item @var{constructor-only}
499 A boolean. Identifies whether this property may only be set in constructor, not in property setter
500 @item @var{owner-type}
501 A GType designator. Identifies the type on which the property was defined.
502 @end table
503
504 This structure identifies a single property. Its field specify attributes of a property.
505
506 Structures of this type have shortened print syntax:
507 @lisp
508 #<PROPERTY gchararray GtkButton.label (flags: readable writable constructor)> 
509 @end lisp
510
511 (When @code{*print-readably*} is T, usual @code{defstruct} print syntax is used)
512
513 This syntax specifies:
514 @itemize
515 @item type of property
516 @item the owner type of property
517 @item name of property
518 @item additional flags of property
519 @end itemize
520
521 @node class-properties
522 @section class-properties
523
524 @Function class-properties
525 @lisp
526 (class-properties type) @result{} properties
527 @end lisp
528
529 @table @var
530 @item @var{type}
531 A GType designator. Specifies the object type (class)
532 @item @var{properties}
533 A list of @code{g-property-definition} structures.
534 @end table
535
536 This function returns the list of properties that are available in class @code{type}.
537
538 Example:
539 @lisp
540 (class-properties "GtkWidget")
541 @result{}
542 (#<PROPERTY gpointer GtkObject.user-data (flags: readable writable)>
543  #<PROPERTY gchararray GtkWidget.name (flags: readable writable)>
544  #<PROPERTY GtkContainer GtkWidget.parent (flags: readable writable)>
545  #<PROPERTY gint GtkWidget.width-request (flags: readable writable)>
546  #<PROPERTY gint GtkWidget.height-request (flags: readable writable)>
547  #<PROPERTY gboolean GtkWidget.visible (flags: readable writable)>
548  #<PROPERTY gboolean GtkWidget.sensitive (flags: readable writable)>
549  #<PROPERTY gboolean GtkWidget.app-paintable (flags: readable writable)>
550  #<PROPERTY gboolean GtkWidget.can-focus (flags: readable writable)>
551  #<PROPERTY gboolean GtkWidget.has-focus (flags: readable writable)>
552  #<PROPERTY gboolean GtkWidget.is-focus (flags: readable writable)>
553  #<PROPERTY gboolean GtkWidget.can-default (flags: readable writable)>
554  #<PROPERTY gboolean GtkWidget.has-default (flags: readable writable)>
555  #<PROPERTY gboolean GtkWidget.receives-default (flags: readable writable)>
556  #<PROPERTY gboolean GtkWidget.composite-child (flags: readable)>
557  #<PROPERTY GtkStyle GtkWidget.style (flags: readable writable)>
558  #<PROPERTY GdkEventMask GtkWidget.events (flags: readable writable)>
559  #<PROPERTY GdkExtensionMode GtkWidget.extension-events (flags: readable writable)>
560  #<PROPERTY gboolean GtkWidget.no-show-all (flags: readable writable)>
561  #<PROPERTY gboolean GtkWidget.has-tooltip (flags: readable writable)>
562  #<PROPERTY gchararray GtkWidget.tooltip-markup (flags: readable writable)>
563  #<PROPERTY gchararray GtkWidget.tooltip-text (flags: readable writable)>
564  #<PROPERTY GdkWindow GtkWidget.window (flags: readable)>)
565 @end lisp
566
567 @node class-property-info
568 @section class-property-info
569 @Function class-property-info
570 @lisp
571 (class-property-info type property-name) @result{} property
572 @end lisp
573
574 @table @var
575 @item @var{type}
576 A GType designator
577 @item @var{property-name}
578 A string naming the property
579 @item @var{property}
580 An instance of @code{g-property-definition} structure
581 @end table
582
583 Returns the property information for a single property.
584
585 Example:
586 @lisp
587 (class-property-info "GtkButton" "label")
588 @result{}
589 #<PROPERTY gchararray GtkButton.label (flags: readable writable constructor)>
590 @end lisp
591
592 @node interface-properties
593 @section interface-properties
594
595 @Function interface-properties
596 @lisp
597 (interface-properties type) @result{} properties
598 @end lisp
599
600 @table @var
601 @item @var{type}
602 A GType designator
603 @item @var{properties}
604 A list of @code{g-property-definition} structures
605 @end table
606
607 This function returns the list of properties that are available in interface @code{type}.
608
609 Example:
610 @lisp
611 (interface-properties "GtkFileChooser")
612 @result{}
613 (#<PROPERTY GtkWidget GtkFileChooser.extra-widget (flags: readable writable)>
614  #<PROPERTY gboolean GtkFileChooser.use-preview-label (flags: readable writable)>
615  #<PROPERTY gboolean GtkFileChooser.preview-widget-active (flags: readable writable)>
616  #<PROPERTY gboolean GtkFileChooser.show-hidden (flags: readable writable)>
617  #<PROPERTY gchararray GtkFileChooser.file-system-backend (flags: writable constructor-only)>
618  #<PROPERTY GtkFileChooserAction GtkFileChooser.action (flags: readable writable)>
619  #<PROPERTY GtkFileFilter GtkFileChooser.filter (flags: readable writable)>
620  #<PROPERTY gboolean GtkFileChooser.select-multiple (flags: readable writable)>
621  #<PROPERTY GtkWidget GtkFileChooser.preview-widget (flags: readable writable)>
622  #<PROPERTY gboolean GtkFileChooser.local-only (flags: readable writable)>
623  #<PROPERTY gboolean GtkFileChooser.do-overwrite-confirmation (flags: readable writable)>)
624 @end lisp
625
626 @node signal-info
627 @section signal-info
628
629 @Struct signal-info
630 @lisp
631 (defstruct signal-info
632   id
633   name
634   owner-type
635   flags
636   return-type
637   param-types
638   detail)
639 @end lisp
640
641 @table @var
642 @item @var{id}
643 An integer - the identifier of a signal
644 @item @var{name}
645 Name of a signal
646 @item @var{owner-type}
647 A GType designator identifying the type on which the signal was defined
648 @item @var{flags}
649 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
650 @item @var{return-type}
651 The return type of a signal (and signal handlers)
652 @item @var{param-types}
653 A list of GType designators that specify the types of signal parameters
654 @item @var{detail}
655 A string. Specifies the "detail" part of a signal name. E.g., @code{"label"} for signal @code{"notify::label"}.
656 @end table
657
658 When @code{*print-readably*} is nil, the following print syntax is used:
659 @lisp
660 #<Signal [#1] void GObject.notify::label(GParam) [RUN-FIRST, NO-RECURSE, DETAILED, ACTION, NO-HOOKS]>
661 #<Signal [#54] gboolean GtkWidget.proximity-in-event(GdkEvent) [RUN-LAST]>
662 #<Signal [#64] void GtkWidget.drag-data-received(GdkDragContext, gint, gint, GtkSelectionData, guint, guint) [RUN-LAST]>
663 #<Signal [#8] void GtkObject.destroy() [RUN-CLEANUP, NO-RECURSE, NO-HOOKS]>
664 @end lisp
665
666 This syntax specifies:
667 @itemize
668 @item the signal id
669 @item signal return type
670 @item owner type
671 @item signal name
672 @item detail
673 @item list of types of parameters
674 @item flags
675 @end itemize
676
677 @node type-signals
678 @section type-signals
679 @Function type-signals
680 @lisp
681 (type-signals type &key (include-inherited t)) @result{} signals
682 @end lisp
683 @table @var
684 @item @var{type}
685 A GType designator
686 @item @var{signals}
687 A list of @code{signal-info} structures
688 @item @var{include-inherited}
689 A boolean that specifies whether to include signals defined on this type or also on ancestor types.
690 @end table
691
692 Returns the list of signals that are available in type @code{type}.
693
694 Example:
695 @lisp
696 (type-signals "GtkLabel" :include-inherited nil)
697 @result{}
698 (#<Signal [#138] void GtkLabel.move-cursor(GtkMovementStep, gint, gboolean) [RUN-LAST, ACTION]>
699  #<Signal [#139] void GtkLabel.copy-clipboard() [RUN-LAST, ACTION]>
700  #<Signal [#140] void GtkLabel.populate-popup(GtkMenu) [RUN-LAST]>)
701 @end lisp
702
703 @node parse-signal-name
704 @section parse-signal-name
705
706 @Function parse-signal-name
707 @lisp
708 (parse-signal-name type signal-name) @result{} signal
709 @end lisp
710
711 @table @var
712 @item @var{type}
713 A GType designator that has the signal.
714 @item @var{signal-name}
715 A string that identifies the signal.
716 @item @var{signal}
717 A list @code{signal-info} structures.
718 @end table
719
720 Parses the signal name and returns the corresponding information. @code{signal-name} may include the detail part.
721
722 Example:
723 @lisp
724 (parse-signal-name "GObject" "notify::label")
725 @result{}
726 #<Signal [#1] void GObject.notify::label(GParam) [RUN-FIRST, NO-RECURSE, DETAILED, ACTION, NO-HOOKS]>
727 @end lisp
728
729 @node query-signal-info
730 @section query-signal-info
731 @Function query-signal-info
732 @lisp
733 (query-signal-info signal-id) @result{} signal
734 @end lisp
735 @table @var
736 @item @var{signal-id}
737 An integer identifying the signal
738 @item @var{signal}
739 An instance of @code{signal-info} structure
740 @end table
741
742 Retrieves the signal information by its id.
743
744 Example:
745 @lisp
746 (query-signal-info 73)
747 @result{}
748 #<Signal [#73] gboolean GtkWidget.show-help(GtkWidgetHelpType) [RUN-LAST, ACTION]>
749 @end lisp
750
751 @node g-type-interfaces
752 @section g-type-interfaces
753
754 @Function g-type-interfaces
755 @lisp
756 (g-type-interfaces type) @result{} interfaces
757 @end lisp
758
759 @table @var
760 @item @var{type}
761 A GType designator
762 @item @var{interfaces}
763 A list of GType designators
764 @end table
765
766 Returns the list of interfaces that @code{type} implements.
767
768 Example:
769 @lisp
770 (g-type-interfaces "GtkButton")
771 @result{}
772 ("AtkImplementorIface" "GtkBuildable" "GtkActivatable")
773 @end lisp
774
775 @node g-type-interface-prerequisites
776 @section g-type-interface-prerequisites
777
778 @Function g-type-interface-prerequisites
779 @lisp
780 (g-type-interface-prerequisites type) @result{} types
781 @end lisp
782
783 @table @var
784 @item @var{type}
785 A GType designator of interface
786 @item @var{types}
787 A list of GType designators specifying the interface prerequisites
788 @end table
789
790 Returns the prerequisites of an interface @code{type}. Prerequisite is a type that should be an ancestor of a type implementing interface @code{type}.
791
792 Example:
793 @lisp
794 (g-type-interface-prerequisites "GtkCellEditable")
795 @result{}
796 ("GtkObject" "GtkWidget")
797 @end lisp
798
799 @node Enum types information
800 @chapter Enum types information
801 @menu
802 * enum-item::
803 * flags-item::
804 * get-enum-items::
805 * get-flags-items::
806 @end menu
807
808 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.
809
810 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.
811
812 @node enum-item
813 @section enum-item
814 @Struct enum-item
815 @lisp
816 (defstruct enum-item
817   name value nick)
818 @end lisp
819
820 @table @var
821 @item @var{name}
822 A string - name of enum item
823 @item @var{value}
824 An integer - numeric value of enum item
825 @item @var{nick}
826 A string - short name of an enum item
827 @end table
828
829 Structure @code{enum-item} represents a single item of an enumeration type.
830
831 Example:
832 @lisp
833 #S(ENUM-ITEM :NAME "GTK_WINDOW_TOPLEVEL" :VALUE 0 :NICK "toplevel")
834 @end lisp
835
836 @node flags-item
837 @section flags-item
838 @Struct flags-item
839 @lisp
840 (defstruct flags-item
841   name value nick)
842 @end lisp
843
844 @table @var
845 @item @var{name}
846 A string - name of flags item
847 @item @var{value}
848 An integer - numeric value of flags item
849 @item @var{nick}
850 A string - short name of an flags item
851 @end table
852
853 Structure @code{flags-item} represents a single item of an flags type.
854
855 Example:
856 @lisp
857 #S(FLAGS-ITEM
858    :NAME "GDK_POINTER_MOTION_HINT_MASK"
859    :VALUE 8
860    :NICK "pointer-motion-hint-mask")
861 @end lisp
862
863 @node get-enum-items
864 @section get-enum-items
865
866 @Function get-enum-items
867 @lisp
868 (get-enum-items type) @result{} items
869 @end lisp
870
871 @table @var
872 @item @var{type}
873 A GType designator of an enum type
874 @item @var{items}
875 A list of @code{enum-item} structures
876 @end table
877
878 Returns a list of items in an enumeration
879
880 Example:
881 @lisp
882 (get-enum-items "GtkScrollType")
883 @result{}
884 (#S(ENUM-ITEM :NAME "GTK_SCROLL_NONE" :VALUE 0 :NICK "none")
885  #S(ENUM-ITEM :NAME "GTK_SCROLL_JUMP" :VALUE 1 :NICK "jump")
886  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_BACKWARD" :VALUE 2 :NICK "step-backward")
887  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_FORWARD" :VALUE 3 :NICK "step-forward")
888  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_BACKWARD" :VALUE 4 :NICK "page-backward")
889  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_FORWARD" :VALUE 5 :NICK "page-forward")
890  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_UP" :VALUE 6 :NICK "step-up")
891  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_DOWN" :VALUE 7 :NICK "step-down")
892  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_UP" :VALUE 8 :NICK "page-up")
893  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_DOWN" :VALUE 9 :NICK "page-down")
894  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_LEFT" :VALUE 10 :NICK "step-left")
895  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_RIGHT" :VALUE 11 :NICK "step-right")
896  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_LEFT" :VALUE 12 :NICK "page-left")
897  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_RIGHT" :VALUE 13 :NICK "page-right")
898  #S(ENUM-ITEM :NAME "GTK_SCROLL_START" :VALUE 14 :NICK "start")
899  #S(ENUM-ITEM :NAME "GTK_SCROLL_END" :VALUE 15 :NICK "end"))
900 @end lisp
901
902 @node get-flags-items
903 @section get-flags-items
904
905 @Function get-flags-items
906 @lisp
907 (get-flags-items type) @result{} items
908 @end lisp
909
910 @table @var
911 @item @var{type}
912 A GType designator of an flags type
913 @item @var{items}
914 A list of @code{flags-item} structures
915 @end table
916
917 Returns a list of items in an flags type
918
919 Example:
920 @lisp
921 (get-flags-items "GtkAttachOptions")
922 @result{}
923 (#S(FLAGS-ITEM :NAME "GTK_EXPAND" :VALUE 1 :NICK "expand")
924  #S(FLAGS-ITEM :NAME "GTK_SHRINK" :VALUE 2 :NICK "shrink")
925  #S(FLAGS-ITEM :NAME "GTK_FILL" :VALUE 4 :NICK "fill"))
926 @end lisp
927
928 @node Using GValues
929 @chapter Using GValues
930 @menu
931 * g-value-zero::
932 * g-value-init::
933 * g-value-unset::
934 * parse-g-value::
935 * set-g-value::
936 * Registering types::
937 @end menu
938
939 GValue is a generic container for arbitrary value of type supported by GType system. Refer to GObject documentation for more detailed information.
940
941 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.
942
943 GValue is used whenever a value of unkown type should be passed. It is used in:
944 @itemize
945 @item Closure marshal functions
946 @item Property get and set functions
947 @end itemize
948
949 Example of usage:
950 @lisp
951 (cffi:with-foreign-object (gval 'g-value)
952   (set-g-value gval "Hello" "gchararray" :zero-g-value t)
953   (format t "~S~%" (parse-g-value gval))
954   (g-value-unset gval))
955 @result{}
956 "Hello"
957 @end lisp
958
959 @node g-value-zero
960 @section g-value-zero
961 @Function g-value-zero
962 @lisp
963 (g-value-zero g-value)
964 @end lisp
965 @table @var
966 @item @var{g-value}
967 A foreign pointer to GValue structure.
968 @end table
969
970 Initializes the GValue to "unset" state. Equivalent of the following initializer in C:
971 @lisp
972 GValue value = @{ 0 @};
973 @end lisp
974
975 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).
976
977 @node g-value-init
978 @section g-value-init
979
980 @Function g-value-init
981 @lisp
982 (g-value-init value type)
983 @end lisp
984 @table @var
985 @item @var{value}
986 A foreign pointer to GValue structure
987 @item @var{type}
988 A GType designator
989 @end table
990
991 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).
992
993 @node g-value-unset
994 @section g-value-unset
995 @Function g-value-unset
996 @lisp
997 (g-value-unset value)
998 @end lisp
999 @table @var
1000 @item @var{value}
1001 A foreign pointer to GValue structure.
1002 @end table
1003
1004 Unsets the GValue. This frees all resources associated with GValue.
1005
1006 @node parse-g-value
1007 @section parse-g-value
1008 @Function parse-g-value
1009 @lisp
1010 (parse-g-value value) @result{} object
1011 @end lisp
1012 @table @var
1013 @item @var{value}
1014 A foreign pointer to GValue structure
1015 @item @var{object}
1016 A Lisp object
1017 @end table
1018
1019 Retrieves the object from GValue structure.
1020
1021 @node set-g-value
1022 @section set-g-value
1023 @Function set-g-value
1024 @lisp
1025 (set-g-value gvalue object type &key zero-g-value unset-g-value (g-value-init t))
1026 @end lisp
1027
1028 @table @var
1029 @item @var{gvalue}
1030 A foreign pointer to GValue structure
1031 @item @var{object}
1032 An object that is to be assigned to @code{gvalue}
1033 @item @var{type}
1034 A GType designator specifying what GType should be set
1035 @item @var{unset-g-value}
1036 A boolean specifying whether to call @code{g-value-unset} before assigment.
1037 @item @var{zero-g-value}
1038 A boolean specifying whether to call @code{g-value-zero} before assignment
1039 @item @var{g-value-init}
1040 A boolean specifying whether to call @code{g-value-init} before assignment
1041 @end table
1042
1043 Assigns the @code{object} to the @code{gvalue}. When GValue is not used, call @code{g-value-unset} to deinitialize the @code{GValue}.
1044
1045 @node Registering types
1046 @section Registering types
1047
1048 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.
1049
1050 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.
1051
1052 @subsection register-enum-type
1053 @Function register-enum-type
1054 @lisp
1055 (register-enum-type name type)
1056 @end lisp
1057 @table @var
1058 @item @var{name}
1059 A string naming the GEnum type
1060 @item @var{type}
1061 A symbol - name of CFFI foreign enum type
1062 @end table
1063
1064 Registers the @code{type} to be used for passing value of GEnum type @code{name} between GObject and Lisp.
1065
1066 Example:
1067 @lisp
1068 (defcenum text-direction
1069   :none :ltr :rtl)
1070 (register-enum-type "GtkTextDirection" 'text-direction)
1071 @end lisp
1072
1073 @subsection register-flags-type
1074 @Function register-flags-type
1075 @lisp
1076 (register-flags-type name type)
1077 @end lisp
1078 @table @var
1079 @item @var{name}
1080 A string naming the GFlags type
1081 @item @var{type}
1082 A symbol - name of CFFI foreign flags type
1083 @end table
1084
1085 Registers the @code{type} to be used for passing value of GFlags type @code{name} between GObject and Lisp.
1086
1087 Example:
1088 @lisp
1089 (defcenum state-type
1090   :normal :active :prelight :selected :insensitive)
1091 (register-enum-type "GtkStateType" 'state-type)
1092 @end lisp
1093
1094 @node Stable pointers
1095 @chapter Stable pointers
1096 @menu
1097 * allocate-stable-pointer::
1098 * free-stable-pointer::
1099 * stable-pointer-value::
1100 * with-stable-pointer::
1101 @end menu
1102
1103 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.
1104
1105 @node allocate-stable-pointer
1106 @section allocate-stable-pointer
1107
1108 @Function allocate-stable-pointer
1109 @lisp
1110 (allocate-stable-pointer thing) @result{} stable-pointer
1111 @end lisp
1112
1113 @table @var
1114 @item @var{thing}
1115 An arbitrary Lisp object
1116 @item @var{stable-pointer}
1117 A foreign pointer
1118 @end table
1119
1120 Allocates a stable pointer to @code{thing}.
1121
1122 (Note: @var{stable-pointer} should not be dereferenced with @code{cffi:mem-ref}. It should only be dereferenced with @code{stable-pointer-value})
1123
1124 Example:
1125 @lisp
1126 (allocate-stable-pointer (lambda (x) (+ x 10)))
1127 @result{}
1128 #.(SB-SYS:INT-SAP #X00000002)
1129
1130 (stable-pointer-value *)
1131 @result{}
1132 #<FUNCTION (LAMBDA (X)) @{1004D016F9@}>
1133
1134 (free-stable-pointer **)
1135 @result{}
1136 NIL
1137 @end lisp
1138
1139 @node free-stable-pointer
1140 @section free-stable-pointer
1141
1142 @Function free-stable-pointer
1143 @lisp
1144 (free-stable-pointer stable-pointer)
1145 @end lisp
1146
1147 @table @var
1148 @item @var{stable-pointer}
1149 A foreign pointer that was created with @code{allocate-stable-pointer}.
1150 @end table
1151
1152 Frees the stable pointer, enabling the garbage collector to reclaim the object.
1153
1154 Example:
1155 @lisp
1156 (allocate-stable-pointer (lambda (x) (+ x 10)))
1157 @result{}
1158 #.(SB-SYS:INT-SAP #X00000002)
1159
1160 (stable-pointer-value *)
1161 @result{}
1162 #<FUNCTION (LAMBDA (X)) @{1004D016F9@}>
1163
1164 (free-stable-pointer **)
1165 @result{}
1166 NIL
1167 @end lisp
1168
1169 @node stable-pointer-value
1170 @section stable-pointer-value
1171
1172 @Accessor stable-pointer-value
1173 @lisp
1174 (stable-pointer-value stable-pointer) @result{} thing
1175 (setf (stable-pointer-value stable-pointer) thing)
1176 @end lisp
1177
1178 @table @var
1179 @item @var{stable-pointer}
1180 A foreign pointer created by @code{allocate-stable-pointer}
1181 @item @var{thing}
1182 A Lisp object
1183 @end table
1184
1185 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.
1186
1187 @node with-stable-pointer
1188 @section with-stable-pointer
1189
1190 @Macro with-stable-pointer
1191 @lisp
1192 (with-stable-pointer (ptr expr) &body body)
1193 @end lisp
1194
1195 @table @var
1196 @item @var{ptr}
1197 A variable that will be bound to the stable pointer
1198 @item @var{expr}
1199 An expression that will be evaluated once and its value will be bound to stable pointer's value
1200 @end table
1201
1202 Executes the body with the @code{ptr} variable being bound to a stable pointer whose value is determined by @code{expr}.
1203
1204 Example:
1205 @lisp
1206 (with-stable-pointer (ptr (lambda (x) (+ x 10)))
1207   (print (stable-pointer-value ptr)))
1208 ;;Prints:
1209 #<FUNCTION (LAMBDA (X)) @{1004807E79@}>
1210 @end lisp
1211
1212 @node Closures
1213 @chapter Closures
1214
1215 Closure are anonymous functions that capture their lexical environment.
1216
1217 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-g-closure} create closure from lisp function. The GClosure is finalized automatically when GObject no longer needs it (e.g., when GClosure is disconnected from signal).
1218
1219 @section create-g-closure
1220 @Function create-g-closure
1221 @lisp
1222 (create-g-closure fn) @result{} closure
1223 @end lisp
1224
1225 @table @var
1226 @item @var{fn}
1227 A function that will be called by closure invokation
1228 @item @var{closure}
1229 A foreign pointer to allocated closure
1230 @end table
1231
1232 Allocates the closure. The closure is destroyed automatically by GObject.
1233
1234 Example:
1235 @lisp
1236 (create-g-closure (lambda (x) (+ x 10)))
1237 @result{}
1238 #.(SB-SYS:INT-SAP #X006D7B20)
1239 @end lisp
1240
1241 Example of usage from GObject binding code:
1242 @lisp
1243 (defun connect-signal (object signal handler &key after)
1244   (g-signal-connect-closure (ensure-object-pointer object)
1245                             signal
1246                             (create-g-closure handler)
1247                             after))
1248 @end lisp
1249
1250 (TODO: GObject defines finer closure API: g_closure_ref, g_closure_unref, g_closure_invoke. It should be bound.)
1251
1252 @node GObject low-level
1253 @chapter GObject low-level
1254 @menu
1255 * g-object-call-constructor::
1256 * g-type-from-object::
1257 * g-object-call-get-property::
1258 * g-object-call-set-property::
1259 @end menu
1260
1261 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.
1262
1263 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.
1264
1265 @node g-object-call-constructor
1266 @section g-object-call-constructor
1267
1268 @Function g-object-call-constructor
1269 @lisp
1270 (g-object-call-constructor object-type args-names args-values &optional args-types) @result{} object-ptr
1271 @end lisp
1272
1273 @table @var
1274 @item @var{object-type}
1275 A GType designator that specifies the object type that is to be created
1276 @item @var{args-names}
1277 A list of strings naming the arguments to constructor
1278 @item @var{args-value}
1279 A list of arguments values (in the same order as args-names)
1280 @item @var{args-types}
1281 Optional list of arguments types (in the same order as args-names). If not specified, it is detected automatically
1282 @item @var{object-ptr}
1283 A foreign pointer to newly created instance
1284 @end table
1285
1286 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}.
1287
1288 Example:
1289 @lisp
1290 (g-object-call-constructor "GtkButton" '("label" "use-underline") '("Hello" t) '("gchararray" "gboolean"))
1291 @result{}
1292 #.(SB-SYS:INT-SAP #X006D8900)
1293
1294 (g-object-call-get-property * "label")
1295 @result{}
1296 "Hello"
1297
1298 (g-object-call-get-property ** "use-underline")
1299 @result{}
1300 T
1301 @end lisp
1302
1303 @node g-type-from-object
1304 @section g-type-from-object
1305
1306 @Function g-type-from-object
1307 @lisp
1308 (g-type-from-object object-ptr) @result{} type
1309 @end lisp
1310
1311 @table @var
1312 @item @var{object-ptr}
1313 A foreign pointer to a GObject instance
1314 @item @var{type}
1315 A GType designator
1316 @end table
1317
1318 Returns the type of an object by a pointer to its instance
1319
1320 Example:
1321 @lisp
1322 (g-type-from-object (g-object-call-constructor "GtkButton" nil nil))
1323 @result{}
1324 "GtkButton"
1325 @end lisp
1326
1327 @node g-object-call-get-property
1328 @section g-object-call-get-property
1329
1330 @Function g-object-call-get-property
1331 @lisp
1332 (g-object-call-get-property object-ptr property-name &optional property-type) @result{} property-value
1333 @end lisp
1334
1335 @table @var
1336 @item @var{object-ptr}
1337 A foreign pointer to a GObject instance
1338 @item @var{property-name}
1339 A string naming the property
1340 @item @var{property-type}
1341 Optional GType designator specifying the type of a property
1342 @item @var{property-value}
1343 The value of a property
1344 @end table
1345
1346 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.
1347
1348 Example:
1349 @lisp
1350 (g-object-call-constructor "GtkButton" '("label" "use-underline") '("Hello" t) '("gchararray" "gboolean"))
1351 @result{}
1352 #.(SB-SYS:INT-SAP #X006D8900)
1353
1354 (g-object-call-get-property * "label")
1355 @result{}
1356 "Hello"
1357
1358 (g-object-call-get-property ** "use-underline")
1359 @result{}
1360 T
1361 @end lisp
1362
1363 @node g-object-call-set-property
1364 @section g-object-call-set-property
1365
1366 @Function g-object-call-set-property
1367 @lisp
1368 (g-object-call-set-property object-ptr property-name new-value &optional property-type)
1369 @end lisp
1370
1371 @table @var
1372 @item @var{object-ptr}
1373 A foreign pointer to a GObject instance
1374 @item @var{property-name}
1375 A string naming the property
1376 @item @var{new-value}
1377 A new value of a property
1378 @item @var{property-type}
1379 Optional GType designator specifying the type of a property
1380 @end table
1381
1382 Sets the property value of property @code{property-name} of object @code{object-ptr} to @code{new-value}.
1383
1384 Example:
1385 @lisp
1386 (g-object-call-constructor "GtkButton" nil nil)
1387 @result{}
1388 #.(SB-SYS:INT-SAP #X006D8B40)
1389
1390 (g-object-call-set-property * "label" "Hello")
1391 @result{}
1392 ; No value
1393
1394 (g-object-call-get-property ** "label")
1395 @result{}
1396 "Hello"
1397 @end lisp
1398
1399 @node GObject high-level
1400 @chapter GObject high-level
1401 @menu
1402 * GObject metaclass::
1403 * Using objects::
1404 * Signals::
1405 * GObject foreign class::
1406 @end menu
1407
1408 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):
1409 @itemize
1410 @item objects are created with @code{make-instance}
1411 @item properties are used as regular slots
1412 @end itemize
1413
1414 GObjects are reference counted, and CL-GTK2-GOBJECT manages its own reference to GObjects. This enables to have transparent garbage collection of unreferenced GObjects.
1415
1416 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.
1417
1418 Example GObject class of definition:
1419 @lisp
1420 (defclass dialog (gtk-window atk-implementor-iface buildable)
1421   ((has-separator :accessor dialog-has-separator
1422                   :initarg :has-separator
1423                   :allocation :gobject-property
1424                   :g-property-type "gboolean"
1425                   :g-property-name "has-separator"))
1426   (:metaclass gobject-class)
1427   (:g-type-name . "GtkDialog")
1428   (:g-type-initializer . "gtk_dialog_get_type"))
1429 @end lisp
1430
1431 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.
1432
1433 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.
1434
1435 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).
1436
1437 @node GObject metaclass
1438 @section GObject metaclass
1439
1440 See MOP for information what metaclass is and why is it useful.
1441
1442 GObject metaclass @code{gobject-class} bridges two object systems: GObject and CLOS.
1443
1444 Classes that correspond to GObject classes are instances of this class.
1445
1446 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}.
1447
1448 This class has the following slots:
1449 @itemize
1450 @item @var{g-type-name} (accessor @code{gobject-class-g-type-name}, initarg @code{:g-type-name})
1451
1452 Specifies the name of GObject class
1453 @item @var{g-type-initializer} (accessor @code{gobject-class-g-type-initializer}, initarg @code{:g-type-initializer})
1454
1455 Name of type initializer function. This function initializes the class and returns its GType. Typically it is named @code{class_get_type}.
1456 @item @var{interface-p} (accessor @code{gobject-class-interface-p}, initarg @code{:interface-p})
1457
1458 A boolean specifying whether this CLOS class corresponds to GInterface.
1459 @end itemize
1460
1461 This metaclass defines the GObject classes.
1462
1463 Slots which have @code{:allocation} of @code{:gobject-property} are mapped to GObject properties. Such slots have following attributes:
1464 @itemize
1465 @item @var{:g-property-type}
1466
1467 A string naming GType of property
1468 @item @var{:g-property-name}
1469
1470 A name of a property
1471 @end itemize
1472
1473 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:
1474 @itemize
1475 @item @var{:g-property-type}
1476 A CFFI foreign type of property
1477 @item @var{:g-getter}
1478 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)}.
1479 @item @var{:g-setter}
1480 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))}.
1481 @end itemize
1482
1483 Initargs of a slot are used to construct the GObject class.
1484
1485 Example:
1486 @lisp
1487 (defclass container (widget atk-implementor-iface buildable)
1488     ((border-width :allocation :gobject-property
1489                    :g-property-type "guint"
1490                    :accessor container-border-width
1491                    :initarg :border-width
1492                    :g-property-name "border-width")
1493      (resize-mode :allocation :gobject-property
1494                   :g-property-type "GtkResizeMode"
1495                   :accessor container-resize-mode
1496                   :initarg :resize-mode
1497                   :g-property-name "resize-mode")
1498      (child :allocation :gobject-property
1499             :g-property-type "GtkWidget"
1500             :accessor container-child
1501             :initarg :child
1502             :g-property-name "child")
1503      (focus-child :allocation :gobject-fn
1504                   :g-property-type g-object
1505                   :accessor container-focus-child
1506                   :initarg :focus-child
1507                   :g-getter "gtk_container_get_focus_child"
1508                   :g-setter "gtk_container_set_focus_child")
1509      (focus-vadjustment :allocation :gobject-fn
1510                         :g-property-type (g-object adjustment)
1511                         :accessor container-focus-vadjustment
1512                         :initarg :focus-vadjustment
1513                         :g-getter "gtk_container_get_focus_vadjustment"
1514                         :g-setter "gtk_container_set_focus_vadjustment")
1515      (focus-hadjustment :allocation :gobject-fn
1516                         :g-property-type (g-object adjustment)
1517                         :accessor container-focus-hadjustment
1518                         :initarg :focus-hadjustment
1519                         :g-getter "gtk_container_get_focus_hadjustment"
1520                         :g-setter "gtk_container_set_focus_hadjustment"))
1521     (:metaclass gobject-class)
1522     (:g-type-name . "GtkContainer")
1523     (:g-type-initializer . "gtk_container_get_type"))
1524 @end lisp
1525 (note the dot in @code{(:g-type-name . "GtkContainer")} and in @code{(:g-type-initializer . "gtk_container_get_type")}. It should be present)
1526
1527 @node Using objects
1528 @section Using objects
1529 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.
1530
1531 Example:
1532 @lisp
1533 (make-instance 'gtk:dialog :has-separator t)
1534 @result{}
1535 #<GTK:DIALOG @{10036C5A71@}>
1536
1537 (defvar *d* (make-instance 'gtk:dialog :has-separator t))
1538 @result{}
1539 *D*
1540
1541 (gtk:dialog-has-separator *d*)
1542 @result{}
1543 T
1544
1545 (setf (gtk:dialog-has-separator *d*) nil)
1546 @result{}
1547 NIL
1548
1549 (gtk:dialog-has-separator *d*)
1550 @result{}
1551 NIL
1552 @end lisp
1553
1554 @node Signals
1555 @section Signals
1556
1557 To connect handler to a signal, @code{connect-signal} function is used.
1558
1559 @Function connect-signals
1560 @lisp
1561 (connect-signal object signal handler &key after)
1562 @end lisp
1563
1564 @table @var
1565 @item @var{object}
1566 An instance of GObject object
1567 @item @var{signal}
1568 A signal name
1569 @item @var{handler}
1570 A function
1571 @item @var{after}
1572 A boolean specifying whether the handler should be called after the default handler
1573 @end table
1574
1575 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.
1576
1577 Example:
1578 @lisp
1579 (defvar *d* (make-instance 'gtk:dialog))
1580 @result{}
1581 *D*
1582
1583 *d*
1584 @result{}
1585 #<GTK:DIALOG @{1002D866F1@}>
1586
1587 (parse-signal-name "GtkDialog" "response")
1588 @result{}
1589 #<Signal [#86] void GtkDialog.response(gint) [RUN-LAST]>
1590
1591 (connect-signal *d* "response" (lambda (dialog response-value) (print dialog) (print response-value)))
1592
1593 (emit-signal *d* "response" 14)
1594 @result{}
1595 ;; Prints:
1596 #<GTK:DIALOG @{1002D866F1@}>
1597 14 
1598 @end lisp
1599
1600 Function @code{emit-signal} is used to emit signals on objects.
1601
1602 @code{(emit-signal object signal-name &rest args) @result{} return-value}
1603
1604 @table @var
1605 @item @var{object}
1606 An object on which the signal should be emitted
1607 @item @var{signal-name}
1608 A string naming the signal
1609 @item @var{args}
1610 Arguments for a signal
1611 @item @var{return-value}
1612 Return value of a signal
1613 @end table
1614
1615 Emits the signal and calls all handlers of the signal. If signal returns a value, it is returned from @code{emit-signal}.
1616
1617 Example:
1618 @lisp
1619 (defvar *d* (make-instance 'gtk:dialog))
1620 @result{}
1621 *D*
1622
1623 *d*
1624 @result{}
1625 #<GTK:DIALOG @{1002D866F1@}>
1626
1627 (parse-signal-name "GtkDialog" "response")
1628 @result{}
1629 #<Signal [#86] void GtkDialog.response(gint) [RUN-LAST]>
1630
1631 (connect-signal *d* "response" (lambda (dialog response-value) (print dialog) (print response-value)))
1632
1633 (emit-signal *d* "response" 14)
1634 @result{}
1635 ;; Prints:
1636 #<GTK:DIALOG @{1002D866F1@}>
1637 14 
1638 @end lisp
1639
1640 @node GObject foreign class
1641 @section GObject foreign class
1642
1643 To enable passing GObject instance between Lisp code and foreign code, @code{g-object} foreign type is introduced.
1644
1645 This type has the following syntax:
1646 @code{(g-object &optional type)} or @code{g-object}.
1647
1648 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.
1649
1650 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.
1651
1652 This defines the function that may be called with instances of types @code{container} and @code{widget}:
1653 @lisp
1654 (defcfun (container-add "gtk_container_add") :void
1655   (container (g-object container))
1656   (widget (g-object widget)))
1657
1658 (let ((window (make-instance 'gtk-window))
1659       (widget (make-instance 'button)))
1660   (container-add window widget))
1661 @end lisp
1662 (@code{gtk-window} is a subclass of @code{container}; @code{button} is a subclass of @code{widget})
1663
1664 This defines the function that returns an instance of GObject class:
1665 @lisp
1666 (defcfun (bin-child "gtk_bin_get_child") (g-object widget)
1667   (bin (g-object bin)))
1668
1669 (let ((window (make-instance 'gtk-window))
1670       (widget (make-instance 'button)))
1671   (container-add window widget)
1672   (bin-child window))
1673 @result{}
1674 #<GTK:BUTTON @{1002DE74B1@}>
1675 @end lisp
1676
1677 @node Creating GObjects classes and implementing GInterfaces
1678 @chapter Creating GObjects classes and implementing GInterfaces
1679
1680 @menu
1681 * define-vtable::
1682 * register-object-type-implementation::
1683 @end menu
1684
1685 Creating GObject classes from Lisp is the most complex part of GObject binding.
1686
1687 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).
1688
1689 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.
1690
1691 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).
1692
1693 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.
1694
1695 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.
1696
1697 @node define-vtable
1698 @section define-vtable
1699
1700 @Macro define-vtable
1701 @lisp
1702 (define-vtable (type-name cstruct-name)
1703   &body item*)
1704
1705 item ::= (name callback-name return-type &rest arg*)
1706 item ::= (:skip cffi-structure-item)
1707 arg ::= (arg-name arg-type)
1708 @end lisp
1709
1710 @table @var
1711 @item @var{type-name}
1712 A string naming the GObject type of interface
1713 @item @var{cstruct-name}
1714 A name for a generated CFFI foreign structure
1715 @item @var{name}
1716 A name for implementation generic function
1717 @item @var{callback-name}
1718 A name for generated callback function
1719 @item @var{return-type}
1720 A CFFI specifier for foreign function return type
1721 @item @var{arg-name}
1722 A symbol naming the argument of interface method
1723 @item @var{arg-type}
1724 A CFFI specifier for foreign function argument type
1725 @end table
1726
1727 Macro that specifies the vtable for an interface. This macro defines generic functions (named by @code{name}) 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.
1728
1729 Example:
1730 @lisp
1731 (define-vtable ("GtkTreeModel" c-gtk-tree-model)
1732   (:skip parent-instance g-type-interface)
1733   ;;some signals
1734   (:skip tree-model-row-changed :pointer)
1735   (:skip tree-model-row-inserted :pointer)
1736   (:skip tree-model-row-has-child-toggled :pointer)
1737   (:skip tree-model-row-deleted :pointer)
1738   (:skip tree-model-rows-reordered :pointer)
1739   ;;methods
1740   (tree-model-get-flags-impl tree-model-get-flags-cb
1741     tree-model-flags
1742     (tree-model g-object))
1743   (tree-model-get-n-columns-impl tree-model-get-n-columns-cb
1744     :int
1745     (tree-model g-object))
1746   (tree-model-get-column-type-impl tree-model-get-column-type-cb
1747     g-type-designator
1748     (tree-model g-object) (index :int))
1749   (tree-model-get-iter-impl tree-model-get-iter-cb
1750     :boolean
1751     (tree-model g-object) (iter (g-boxed-foreign tree-iter)) (path (g-boxed-foreign tree-path)))
1752   (tree-model-get-path-impl tree-model-get-path-cb
1753     (g-boxed-foreign tree-path :return)
1754     (tree-model g-object) (iter (g-boxed-foreign tree-iter)))
1755   (tree-model-get-value-impl tree-model-get-value-cb
1756     :void
1757     (tree-model g-object) (iter (g-boxed-foreign tree-iter)) (n :int) (value (:pointer g-value)))
1758   (tree-model-iter-next-impl tree-model-iter-next-cb
1759     :boolean
1760     (tree-model g-object) (iter (g-boxed-foreign tree-iter)))
1761   (tree-model-iter-children-impl tree-model-iter-children-cb
1762     :boolean
1763     (tree-model g-object) (iter (g-boxed-foreign tree-iter)) (parent (g-boxed-foreign tree-iter)))
1764   (tree-model-iter-has-child-impl tree-model-iter-has-child-cb
1765     :boolean
1766     (tree-model g-object) (iter (g-boxed-foreign tree-iter)))
1767   (tree-model-iter-n-children-impl tree-model-iter-n-children-cb
1768     :int
1769     (tree-model g-object) (iter (g-boxed-foreign tree-iter)))
1770   (tree-model-iter-nth-child-impl tree-model-iter-nth-child-cb
1771     :boolean
1772     (tree-model g-object) (iter (g-boxed-foreign tree-iter)) (parent (g-boxed-foreign tree-iter)) (n :int))
1773   (tree-model-iter-parent-impl tree-model-iter-parent-cb
1774     :boolean
1775     (tree-model g-object) (iter (g-boxed-foreign tree-iter)) (child (g-boxed-foreign tree-iter)))
1776   (tree-model-ref-node-impl tree-model-ref-node-cb
1777     :void
1778     (tree-model g-object) (iter (g-boxed-foreign tree-iter)))
1779   (tree-model-unref-node-impl tree-model-unref-node-cb
1780     :void
1781     (tree-model g-object) (iter (g-boxed-foreign tree-iter))))
1782 @end lisp
1783
1784 @node register-object-type-implementation
1785 @section register-object-type-implementation
1786
1787 @Macro register-object-type-implementation
1788 @lisp
1789 (register-object-type-implementation name class parent interfaces properties)
1790 @end lisp
1791
1792 @table @var
1793 @item @var{name}
1794 A string naming the new GObject class.
1795 @item @var{class}
1796 A class name of corresponding CLOS class. It should be inherited from @code{g-object} or its descendants.
1797 @item @var{parent}
1798 A string naming the GObject superclass
1799 @item @var{interfaces}
1800 A list of names of interfaces that this class implements.
1801 @item @var{properties}
1802 A list of properties that this class provides.
1803 Each property is defined as
1804 @lisp
1805 property ::= (property-name property-type accessor property-get-fn property-set-fn)
1806 @end lisp
1807 @end table
1808
1809 A macro that creates a new GObject type and registers the Lisp implementation for it.
1810
1811 Example:
1812 @lisp
1813 (register-object-type-implementation "LispArrayListStore" array-list-store "GObject" ("GtkTreeModel") nil)
1814 @end lisp
1815
1816 @node GBoxed
1817 @chapter GBoxed
1818 @menu
1819 * define-g-boxed-cstruct::
1820 * define-g-boxed-variant-cstruct::
1821 * define-g-boxed-opaque::
1822 * g-boxed-opaque::
1823 * define-boxed-opaque-accessor::
1824 * boxed-related-symbols::
1825 * GBoxed foreign type::
1826 @end menu
1827
1828 GObject manual defines this type in the following way:
1829
1830 ``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.
1831
1832 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.''
1833
1834 Naturally, it is hard to provide support for ``arbitrary C structures''. We support a few useful use cases of GBoxed types:
1835 @itemize
1836 @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.
1837 @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).
1838
1839 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.
1840 @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.
1841 @end itemize
1842
1843 @node define-g-boxed-cstruct
1844 @section define-g-boxed-cstruct
1845 @Macro define-g-boxed-cstruct
1846 @lisp
1847 (define-g-boxed-cstruct name g-type-name
1848   &body slot*)
1849
1850 slot ::= (slot-name slot-type &key count initform inline)
1851 @end lisp
1852
1853 @table @var
1854 @item @var{name}
1855 A symbol naming the type being defined
1856 @item @var{g-type-name}
1857 A string specifying the GType name of this GBoxed. This may be nil if this type is not registered with GObject type system.
1858 @item @var{slot-name}
1859 A symbol naming the slot of a structure
1860 @item @var{slot-type}
1861 A foreign type of a slot
1862 @item @var{count}
1863 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.
1864 @item @var{initform}
1865 A form that is the initform of Lisp structure slot
1866 @item @var{inline}
1867 A boolean. If it is true, then the slot contains GBoxed structure whose name is @code{slot-type}.
1868 @end table
1869
1870 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.
1871
1872 Example of usage:
1873 @lisp
1874 (define-g-boxed-cstruct rectangle "GdkRectangle"
1875   (left :int :initform 0)
1876   (top :int :initform 0)
1877   (width :int :initform 0)
1878   (height :int :initform 0))
1879
1880 (define-g-boxed-cstruct point nil
1881   (x :int :initform 0)
1882   (y :int :initform 0))
1883
1884 (define-g-boxed-cstruct vector4 nil
1885   (coords :double :count 4 :initform (vector 0d0 0d0 0d0 0d0)))
1886
1887 (define-g-boxed-cstruct segment nil
1888   (a point :inline t :initform (make-point))
1889   (b point :inline t :initform (make-point)))
1890 @end lisp
1891
1892 @node define-g-boxed-variant-cstruct
1893 @section define-g-boxed-variant-cstruct
1894
1895 @Macro define-g-boxed-variant-cstruct
1896 @lisp
1897 (define-g-boxed-variant-cstruct name g-type-name
1898   &body slot-or-variant-specification*)
1899
1900 slot ::= (slot-name slot-type &key count initform inline)
1901 variant-specification ::= (:variant dispatching-slot-name structure-variant*)
1902 structure-variant ::= (dispatching-slot-values structure-name &body slot-or-variant-specification*)
1903 @end lisp
1904
1905 @table @var
1906 @item @var{name}
1907 A symbol naming the type being defined
1908 @item @var{g-type-name}
1909 A string specifying the GType name of this GBoxed. This may be nil if this type is not registered with GObject type system.
1910 @item @var{slot-name}
1911 A symbol naming the slot of a structure
1912 @item @var{slot-type}
1913 A foreign type of a slot
1914 @item @var{count}
1915 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.
1916 @item @var{initform}
1917 A form that is the initform of Lisp structure slot
1918 @item @var{inline}
1919 A boolean. If it is true, then the slot contains GBoxed structure whose name is @code{slot-type}.
1920 @item @var{dispatching-slot-name}
1921 A name of the dispatching slot
1922 @item @var{dispatching-slot-values}
1923 A single value or a list of values.
1924 @item @var{structure-name}
1925 A symbol naming the structure
1926 @end table
1927
1928 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.
1929
1930 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.
1931
1932 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).
1933
1934 For example, for these structures this assumption holds:
1935 @example
1936 union GdkEvent
1937 @{
1938   GdkEventType   type;
1939   GdkEventKey    key;
1940   GdkEventButton button;
1941 @};
1942
1943 struct GdkEventKey @{
1944   GdkEventType type; //
1945   GdkWindow *window; // These fields are common
1946   gint8 send_event;  //
1947   guint32 time;
1948   guint state;
1949   guint keyval;
1950   ...
1951 @};
1952
1953 struct GdkEventButton @{
1954   GdkEventType type; //
1955   GdkWindow *window; // These fields are common
1956   gint8 send_event;  //
1957   guint32 time;
1958   gdouble x;
1959   gdouble y;
1960   ...
1961 @};
1962 @end example
1963
1964 Example:
1965 @lisp
1966 (define-g-boxed-variant-cstruct event "GdkEvent"
1967   (type event-type)
1968   (window (g-object gdk-window))
1969   (send-event (:boolean :int8))
1970   (:variant type
1971             ((:key-press :key-release) event-key
1972              (time :uint32)
1973              (state modifier-type)
1974              (keyval :uint)
1975              (length :int)
1976              (string (:string :free-from-foreign nil
1977                               :free-to-foreign nil))
1978              (hardware-keycode :uint16)
1979              (group :uint8)
1980              (is-modifier :uint))
1981             ((:button-press :2button-press :3button-press
1982               :button-release) event-button
1983              (time :uint32)
1984              (x :double)
1985              (y :double)
1986              (axes (fixed-array :double 2))
1987              (state :uint)
1988              (button :uint)
1989              (device (g-object device))
1990              (x-root :double)
1991              (y-root :double))
1992              ...))
1993 @end lisp
1994
1995 This code defines following structures:
1996 @lisp
1997 (defstruct event
1998   type window send-event)
1999
2000 (defstruct (event-key (:include event))
2001   time state keyval length string
2002   hardware-keycode group is-modifier)
2003
2004 (defstruct (event-button (:include event))
2005   time x y axes state button device x-root y-root)
2006 @end lisp
2007
2008 @node define-g-boxed-opaque
2009 @section define-g-boxed-opaque
2010
2011 @Macro define-g-boxed-opaque
2012 @lisp
2013 (define-g-boxed-opaque name g-type-name &key alloc)
2014 @end lisp
2015
2016 @table @var
2017 @item @var{name}
2018 A name of boxed type
2019 @item @var{g-type-name}
2020 A string; the name of GType
2021 @item @var{alloc}
2022 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.
2023 @end table
2024
2025 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.
2026
2027 Example:
2028 @lisp
2029 (defcfun gtk-tree-path-new :pointer)
2030
2031 (define-g-boxed-opaque tree-path "GtkTreePath"
2032   :alloc (gtk-tree-path-new))
2033 @end lisp
2034 @node g-boxed-opaque
2035 @section g-boxed-opaque
2036 @Class g-boxed-opaque
2037 @lisp
2038 (defclass g-boxed-opaque ()
2039   ((pointer :initarg :pointer
2040             :initform nil
2041             :accessor g-boxed-opaque-pointer)))
2042 @end lisp
2043
2044 A class that is the base class for wrappers of opaque structures. Contains a pointer to the wrapped opaque structure.
2045
2046 Accessor function @code{g-boxed-opaque-pointer} is used to access the pointer. Pointer should not be modified directly, only read.
2047 @node define-boxed-opaque-accessor
2048 @section define-boxed-opaque-accessor
2049 @Macro define-boxed-opaque-accessor
2050 @lisp
2051 (define-boxed-opaque-accessor
2052   boxed-name accessor-name &key type reader writer)
2053 @end lisp
2054
2055 @table @var
2056 @item @var{boxed-name}
2057 A symbol naming the opaque structure type for which the accessor is being defined
2058 @item @var{accessor-name}
2059 A symbol naming the accessor
2060 @item @var{type}
2061 A CFFI foreign type of the property for which the accessor is being defined
2062 @item @var{reader}
2063 A @code{NIL} or a string or a function designator for the reader function
2064 @item @var{writer}
2065 A @code{NIL} or a string or a function designator for the writer function
2066 @end table
2067
2068 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}.
2069
2070 @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.
2071
2072 @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.
2073
2074 Example:
2075 @lisp
2076 (define-boxed-opaque-accessor text-iter text-iter-child-anchor
2077   :reader "gtk_text_iter_get_child_anchor" :type (g-object text-child-anchor))
2078
2079 (define-boxed-opaque-accessor text-iter text-iter-tags
2080   :reader "gtk_text_iter_get_tags" :type (gslist (g-object text-tag) :free-from-foreign t))
2081
2082 (define-boxed-opaque-accessor text-iter text-iter-chars-in-line
2083   :reader "gtk_text_iter_get_chars_in_line" :type :int)
2084
2085 (define-boxed-opaque-accessor text-iter text-iter-offset
2086   :reader "gtk_text_iter_get_offset" :writer "gtk_text_iter_set_offset" :type :int)
2087 @end lisp
2088
2089 @node boxed-related-symbols
2090 @section boxed-related-symbols
2091
2092 @Function boxed-related-symbols
2093 @lisp
2094 (boxed-related-symbols name) @result{} symbols
2095 @end lisp
2096
2097 @table @var
2098 @item @var{name}
2099 A symbol naming the boxed type
2100 @item @var{symbols}
2101 A list of symbols
2102 @end table
2103
2104 This function returns the list of symbols that are related to GBoxed type @var{name}. These symbols are returned:
2105 @itemize
2106 @item name of boxed type
2107 @item name of all accessors of cstruct and variant-cstruct boxed types
2108 @item names of all variants of variant-cstruct boxed types
2109 @item names of constructors and copiers of cstruct and variant-cstruct boxed-types
2110 @end itemize
2111
2112 Typical usage of this function is to export the symbols related to given boxed type.
2113
2114 Example:
2115 @lisp
2116 (define-g-boxed-cstruct rectangle "GdkRectangle"
2117   (x :int :initform 0)
2118   (y :int :initform 0)
2119   (width :int :initform 0)
2120   (height :int :initform 0))
2121
2122 (boxed-related-symbols 'rectangle)
2123 @result{}
2124 (RECTANGLE MAKE-RECTANGLE COPY-RECTANGLE RECTANGLE-X RECTANGLE-Y
2125  RECTANGLE-WIDTH RECTANGLE-HEIGHT)
2126 @end lisp
2127
2128 @node GBoxed foreign type
2129 @section GBoxed foreign type
2130
2131 @ForeignType g-boxed-foreign
2132 @lisp
2133 (g-boxed-foreign name &rest option*)
2134
2135 option ::= :return
2136 @end lisp
2137
2138 @table @var
2139 @item @var{name}
2140 Name of GBoxed type
2141 @item @var{option}
2142 Option of foreign type
2143 @item @code{:return}
2144 An option that identifies the foreign type which is used at return position (as foreign function return type or as a callback return type)
2145 @end table
2146
2147 @code{g-boxed-foreign} type deals with marshaling data between Lisp code and foreign code. The marshaling follows the following principles:
2148 @itemize
2149 @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)
2150 @item Lisp objects should not be manually managed and are properly reclaimed by garbage collector, leaving no memory leaks
2151 @item Foreign code can change objects that are passed to them as arguments. This is required for functions that operate by modifying their arguments
2152 @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
2153 @end itemize
2154
2155 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.
2156
2157 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.
2158
2159 Examples of usage:
2160 @lisp
2161 (define-vtable ("GtkTreeModel" c-gtk-tree-model)
2162   ...
2163   (tree-model-get-path-impl tree-model-get-path-cb
2164     (g-boxed-foreign tree-path :return) (tree-model g-object) (iter (g-boxed-foreign tree-iter)))
2165   (tree-model-get-value-impl tree-model-get-value-cb
2166     :void (tree-model g-object) (iter (g-boxed-foreign tree-iter)) (n :int) (value (:pointer g-value)))
2167   (tree-model-iter-next-impl tree-model-iter-next-cb
2168     :boolean (tree-model g-object) (iter (g-boxed-foreign tree-iter)))
2169   ...)
2170
2171 (defcfun gtk-text-iter-forward-search :boolean
2172   (iter (g-boxed-foreign text-iter))
2173   (str (:string :free-to-foreign t))
2174   (flags text-search-flags)
2175   (match-start (g-boxed-foreign text-iter))
2176   (match-end (g-boxed-foreign text-iter))
2177   (limit (g-boxed-foreign text-iter)))
2178 @end lisp
2179
2180 @node Generating type definitions by introspection
2181 @chapter Generating type definitions by introspection
2182 @menu
2183 * define-g-object-class::
2184 * define-g-interface::
2185 * define-g-enum::
2186 * define-g-flags::
2187 * get-g-enum-definition::
2188 * get-g-flags-definition::
2189 * get-g-interface-definition::
2190 * get-g-class-definition::
2191 * Specifying additional properties for CLOS classes::
2192 * Generating names for CLOS classes and accessors::
2193 * generate-types-hierarchy-to-file::
2194 @end menu
2195
2196 CL-GTK2-GOBJECT includes facilities for automatically generating parts of bindings for libraries that use GObject type system.
2197
2198 @node define-g-object-class
2199 @section define-g-object-class
2200
2201 @Macro define-g-object-class
2202 @lisp
2203 (define-g-object-class g-type-name name
2204   (&key (superclass 'g-object) (export t) interfaces type-initializer)
2205   (&rest property*))
2206
2207 property ::= (name accessor gname type readable writable)
2208 property ::= (:cffi name acessor type reader writer)
2209 @end lisp
2210
2211 Parameters of @code{define-g-object-class}
2212 @table @var
2213 @item @var{superclass}
2214 A symbol naming the superclass of this class
2215 @item @var{export}
2216 Whether to export the name of the class and names of autogenerated properties names from the current package.
2217 @item @var{interfaces}
2218 A list of interfaces the this class implements
2219 @item @var{type-initializer}
2220 A string naming the type initiliazer function. It is usually named @code{class_get_type}.
2221 @item @var{properties}
2222 A list of slots of a class
2223 @end table
2224
2225 Parameters of @code{property}:
2226 @table @var
2227 @item @var{name}
2228 A symbol naming the slot
2229 @item @var{accessor}
2230 A symbol naming the accessor function for this slot
2231 @item @var{gname}
2232 A string naming the property of GObject
2233 @item @var{type}
2234 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)
2235 @item @var{readable}
2236 A boolean specifying whether the slot can be read
2237 @item @var{writable}
2238 A boolean specifying whether the slot can be assigned to
2239 @item @var{reader}
2240 A string or a symbol naming getter function. See description of @code{gobject-class} metaclass for information.
2241 @item @var{writter}
2242 A string or a symbol naming setter function. See description of @code{gobject-class} metaclass for information.
2243 @end table
2244
2245 Macro that expands to @code{defclass} for specified class. Additionally, if @code{export} is true, it exports accessor names and name of a class.
2246
2247 Example:
2248 @lisp
2249 (define-g-object-class "GtkContainer" container
2250   (:superclass widget :export t :interfaces
2251                ("AtkImplementorIface" "GtkBuildable")
2252                :type-initializer "gtk_container_get_type")
2253   ((border-width container-border-width "border-width" "guint" t t)
2254    (resize-mode container-resize-mode "resize-mode" "GtkResizeMode" t t)
2255    (child container-child "child" "GtkWidget" nil t)
2256    (:cffi focus-child container-focus-child g-object "gtk_container_get_focus_child" "gtk_container_set_focus_child")
2257    (:cffi focus-vadjustment container-focus-vadjustment (g-object adjustment) "gtk_container_get_focus_vadjustment" "gtk_container_set_focus_vadjustment")
2258    (:cffi focus-hadjustment container-focus-hadjustment (g-object adjustment) "gtk_container_get_focus_hadjustment" "gtk_container_set_focus_hadjustment")))
2259 @end lisp
2260
2261 @node define-g-interface
2262 @section define-g-interface
2263
2264 @Macro define-g-interface
2265 @lisp
2266 (define-g-interface g-type-name name (&key (export t) type-initializer)
2267   &body property*)
2268
2269 property ::= (name accessor gname type readable writable)
2270 property ::= (:cffi name acessor type reader writer)
2271 @end lisp
2272
2273 Parameters of @code{define-g-interface}
2274 @table @var
2275 @item @var{export}
2276 Whether to export the name of the interface and names of autogenerated properties names from the current package.
2277 @item @var{type-initializer}
2278 A string naming the type initiliazer function. It is usually named @code{interface_get_type}.
2279 @item @var{properties}
2280 A list of slots of a interface
2281 @end table
2282
2283 Parameters of @code{property}:
2284 @table @var
2285 @item @var{name}
2286 A symbol naming the slot
2287 @item @var{accessor}
2288 A symbol naming the accessor function for this slot
2289 @item @var{gname}
2290 A string naming the property of GObject
2291 @item @var{type}
2292 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)
2293 @item @var{readable}
2294 A boolean specifying whether the slot can be read
2295 @item @var{writable}
2296 A boolean specifying whether the slot can be assigned to
2297 @item @var{reader}
2298 A string or a symbol naming getter function. See description of @code{gobject-class} metaclass for information.
2299 @item @var{writter}
2300 A string or a symbol naming setter function. See description of @code{gobject-class} metaclass for information.
2301 @end table
2302
2303 Macro that expands to @code{defclass} for specified interface. Additionally, if @code{export} is true, it exports accessor names and name of a interface.
2304
2305 Example:
2306 @lisp
2307 (define-g-interface "GtkFileChooser" file-chooser
2308   (:export t :type-initializer "gtk_file_chooser_get_type")
2309   (do-overwrite-confirmation file-chooser-do-overwrite-confirmation "do-overwrite-confirmation" "gboolean" t t)
2310   (select-multiple file-chooser-select-multiple "select-multiple" "gboolean" t t)
2311   (filter file-chooser-filter "filter" "GtkFileFilter" t t)
2312   (local-only file-chooser-local-only "local-only" "gboolean" t t)
2313   (preview-widget file-chooser-preview-widget "preview-widget" "GtkWidget" t t)
2314   (use-preview-label file-chooser-use-preview-label "use-preview-label" "gboolean" t t)
2315   (preview-widget-active file-chooser-preview-widget-active "preview-widget-active" "gboolean" t t)
2316   (file-system-backend file-chooser-file-system-backend "file-system-backend" "gchararray" nil nil)
2317   (extra-widget file-chooser-extra-widget "extra-widget" "GtkWidget" t t)
2318   (show-hidden file-chooser-show-hidden "show-hidden" "gboolean" t t)
2319   (action file-chooser-action "action" "GtkFileChooserAction" t t)
2320   (:cffi current-name file-chooser-current-name
2321    (:string :free-to-foreign t :encoding :utf-8) nil "gtk_file_chooser_set_current_name")
2322   (:cffi filename file-chooser-filename
2323    (g-string :free-from-foreign t :free-to-foreign t)
2324    "gtk_file_chooser_get_filename" "gtk_file_chooser_set_filename")
2325   (:cffi current-folder file-chooser-current-folder
2326    (g-string :free-from-foreign t :free-to-foreign t)
2327    "gtk_file_chooser_get_current_folder"
2328    "gtk_file_chooser_set_current_folder")
2329   (:cffi uri file-chooser-uri
2330    (g-string :free-from-foreign t :free-to-foreign t)
2331    "gtk_file_chooser_get_uri" "gtk_file_chooser_set_uri")
2332   (:cffi current-folder-uri file-chooser-current-folder-uri
2333    (g-string :free-from-foreign t :free-to-foreign t)
2334    "gtk_file_chooser_get_current_folder_uri"
2335    "gtk_file_chooser_set_current_folder_uri")
2336   (:cffi preview-filename file-chooser-preview-filename
2337    (g-string :free-from-foreign t :free-to-foreign t)
2338    "gtk_file_chooser_get_preview_filename" nil)
2339   (:cffi preview-uri file-chooser-preview-uri
2340    (g-string :free-from-foreign t :free-to-foreign t)
2341    "gtk_file_chooser_get_preview_uri" nil))
2342 @end lisp
2343
2344 @node define-g-enum
2345 @section define-g-enum
2346
2347 @Macro define-g-enum
2348 @lisp
2349 (define-g-enum g-name name (&key (export t) type-initializer) &body value*)
2350
2351 value ::= :keyword
2352 value ::= (:keyword integer)
2353 @end lisp
2354
2355 @table @var
2356 @item @var{g-name}
2357 A string naming the GEnum type
2358 @item @var{name}
2359 A symbol naming the CFFI enumeration type
2360 @item @var{export}
2361 A boolean indicating whether to export @code{name}
2362 @item @var{type-initializer}
2363 A string naming the foreign type initializer function. Usually named @code{enum_get_type}.
2364 @end table
2365
2366 Macro that defines CFFI enumeration, registers it with GValue, and calls the type initializer.
2367
2368 Example:
2369 @lisp
2370 (define-g-enum "GtkTextDirection" text-direction
2371   (:export t :type-initializer "gtk_text_direction_get_type")
2372   (:none 0) (:ltr 1) (:rtl 2))
2373
2374 (define-g-enum "GtkSizeGroupMode" size-group-mode
2375  (:export t :type-initializer "gtk_size_group_mode_get_type")
2376  :none :horizontal :vertical :both)
2377 @end lisp
2378
2379 @node define-g-flags
2380 @section define-g-flags
2381
2382 @Macro define-g-flags
2383 @lisp
2384 (define-g-flags g-name name (&key (export t) type-initializer) &body value*)
2385
2386 value ::= :keyword
2387 value ::= (:keyword integer)
2388 @end lisp
2389
2390 @table @var
2391 @item @var{g-name}
2392 A string naming the GFlags type
2393 @item @var{name}
2394 A symbol naming the CFFI flags type
2395 @item @var{export}
2396 A boolean indicating whether to export @code{name}
2397 @item @var{type-initializer}
2398 A string naming the foreign type initializer function. Usually named @code{flags_get_type}.
2399 @end table
2400
2401 Macro that defines CFFI bitfield, registers it with GValue, and calls the type initializer.
2402
2403 Example:
2404 @lisp
2405 (define-g-flags "GtkAttachOptions" attach-options
2406   (:export t :type-initializer "gtk_attach_options_get_type")
2407   (:expand 1) (:shrink 2) (:fill 4))
2408
2409 (define-g-flags "GtkButtonAction" button-action
2410   (:export t :type-initializer "gtk_button_action_get_type")
2411   :ignored :selects :drags :expands)
2412 @end lisp
2413
2414 @node get-g-enum-definition
2415 @section get-g-enum-definition
2416
2417 @Function get-g-enum-definition
2418 @lisp
2419 (get-g-enum-definition type &optional lisp-name-package) @result{} definition
2420 @end lisp
2421
2422 @table @var
2423 @item @var{type}
2424 A string naming the GEnum type
2425 @item @var{lisp-name-package}
2426 A package that will be used as a package for generated symbols (enum name). If not specified, symbols are interned in @code{*package*}
2427 @item @var{definition}
2428 A Lisp form that when evaluated defines the GEnum.
2429 @end table
2430
2431 Uses GObject introspection capabilities to automatically produce the definition of GEnum. The foreign library that defines the enum type should be loaded.
2432
2433 See @ref{Generating names for CLOS classes and accessors} for information about used method for generating names.
2434
2435 Example:
2436 @lisp
2437 (get-g-enum-definition "GtkDirectionType")
2438 @result{}
2439 (DEFINE-G-ENUM "GtkDirectionType" GTK-DIRECTION-TYPE
2440                (:EXPORT T :TYPE-INITIALIZER "gtk_direction_type_get_type")
2441                (:TAB-FORWARD 0) (:TAB-BACKWARD 1) (:UP 2) (:DOWN 3) (:LEFT 4)
2442                (:RIGHT 5))
2443 @end lisp
2444
2445 @node get-g-flags-definition
2446 @section get-g-flags-definition
2447
2448 @Function get-g-flags-definition
2449 @lisp
2450 (get-g-flags-definition type &optional lisp-name-package) @result{} definition
2451 @end lisp
2452
2453 @table @var
2454 @item @var{type}
2455 A string naming the GFlags type
2456 @item @var{lisp-name-package}
2457 A package that will be used as a package for generated symbols (flags name). If not specified, symbols are interned in @code{*package*}
2458 @item @var{definition}
2459 A Lisp form that when evaluated defines the GFlags.
2460 @end table
2461
2462 Uses GObject introspection capabilities to automatically produce the definition of GFlags. The foreign library that defines the flags type should be loaded.
2463
2464 See @ref{Generating names for CLOS classes and accessors} for information about used method for generating names.
2465
2466 Example:
2467 @lisp
2468 (get-g-flags-definition "GtkCalendarDisplayOptions")
2469 @result{}
2470 (DEFINE-G-FLAGS "GtkCalendarDisplayOptions" GTK-CALENDAR-DISPLAY-OPTIONS
2471                 (:EXPORT T :TYPE-INITIALIZER
2472                  "gtk_calendar_display_options_get_type")
2473                 (:SHOW-HEADING 1) (:SHOW-DAY-NAMES 2) (:NO-MONTH-CHANGE 4)
2474                 (:SHOW-WEEK-NUMBERS 8) (:WEEK-START-MONDAY 16)
2475                 (:SHOW-DETAILS 32))
2476 @end lisp
2477
2478 @node get-g-interface-definition
2479 @section get-g-interface-definition
2480
2481 @Function get-g-interface-definition
2482 @lisp
2483 get-g-interface-definition type &optional lisp-name-package) @result{} definition
2484 @end lisp
2485
2486 @table @var
2487 @item @var{type}
2488 A string naming the GInterface type
2489 @item @var{lisp-name-package}
2490 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*}
2491 @item @var{definition}
2492 A Lisp form that when evaluated defines the GInterface.
2493 @end table
2494
2495 Uses GObject introspection capabilities to automatically produce the definition of GInterface. The foreign library that defines the GInterface type should be loaded.
2496
2497 See @ref{Generating names for CLOS classes and accessors} for information about used method for generating names.
2498
2499 Example:
2500 @lisp
2501 (get-g-interface-definition "GtkActivatable")
2502 @result{}
2503 (DEFINE-G-INTERFACE "GtkActivatable" GTK-ACTIVATABLE
2504                     (:EXPORT T :TYPE-INITIALIZER "gtk_activatable_get_type")
2505                     (USE-ACTION-APPEARANCE
2506                      GTK-ACTIVATABLE-USE-ACTION-APPEARANCE
2507                      "use-action-appearance" "gboolean" T T)
2508                     (RELATED-ACTION GTK-ACTIVATABLE-RELATED-ACTION
2509                      "related-action" "GtkAction" T T))
2510 @end lisp
2511
2512 @node get-g-class-definition
2513 @section get-g-class-definition
2514
2515 @Function get-g-class-definition
2516 @lisp
2517 (get-g-class-definition type &optional lisp-name-package) @result{} definition
2518 @end lisp
2519
2520 @table @var
2521 @item @var{type}
2522 A string naming the GObject type
2523 @item @var{lisp-name-package}
2524 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*}
2525 @item @var{definition}
2526 A Lisp form that when evaluated defines the GObject.
2527 @end table
2528
2529 Uses GObject introspection capabilities to automatically produce the definition of GClass. The foreign library that defines the GObject type should be loaded.
2530
2531 See @ref{Generating names for CLOS classes and accessors} for information about used method for generating names.
2532
2533 Example:
2534 @lisp
2535 (get-g-class-definition "GtkButton")
2536 @result{}
2537 (DEFINE-G-OBJECT-CLASS "GtkButton" GTK-BUTTON
2538                        (:SUPERCLASS GTK-BIN :EXPORT T :INTERFACES
2539                         ("AtkImplementorIface" "GtkActivatable" "GtkBuildable")
2540                         :TYPE-INITIALIZER "gtk_button_get_type")
2541                        ((LABEL GTK-BUTTON-LABEL "label" "gchararray" T T)
2542                         (IMAGE GTK-BUTTON-IMAGE "image" "GtkWidget" T T)
2543                         (RELIEF GTK-BUTTON-RELIEF "relief" "GtkReliefStyle" T
2544                          T)
2545                         (USE-UNDERLINE GTK-BUTTON-USE-UNDERLINE "use-underline"
2546                          "gboolean" T T)
2547                         (USE-STOCK GTK-BUTTON-USE-STOCK "use-stock" "gboolean"
2548                          T T)
2549                         (FOCUS-ON-CLICK GTK-BUTTON-FOCUS-ON-CLICK
2550                          "focus-on-click" "gboolean" T T)
2551                         (XALIGN GTK-BUTTON-XALIGN "xalign" "gfloat" T T)
2552                         (YALIGN GTK-BUTTON-YALIGN "yalign" "gfloat" T T)
2553                         (IMAGE-POSITION GTK-BUTTON-IMAGE-POSITION
2554                          "image-position" "GtkPositionType" T T)))
2555 @end lisp
2556
2557 @node Specifying additional properties for CLOS classes
2558 @section Specifying additional properties for CLOS classes
2559
2560 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.
2561
2562 Definitions generator uses variable @code{*additional-properties*} to get this information.
2563
2564 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).
2565
2566 To supply the bindings generator with this information, bind @code{*additional-properties*} to such list when the generator is run.
2567
2568 Example:
2569 @lisp
2570 (("GtkTreeViewColumn"
2571   (:cffi gtk::tree-view
2572          gtk::tree-view-column-tree-view
2573          g-object "gtk_tree_view_column_get_tree_view" nil)
2574   (:cffi gtk::sort-column-id
2575          gtk::tree-view-column-sort-column-id
2576          :int "gtk_tree_view_column_get_sort_column_id" "gtk_tree_view_column_set_sort_column_id")
2577   (:cffi gtk::cell-renderers
2578          gtk::tree-view-column-cell-renderers
2579          (glist g-object  :free-from-foreign t) "gtk_tree_view_column_get_cell_renderers" nil))
2580  ("GtkTreeSelection"
2581   (:cffi gtk::mode
2582          gtk::tree-selection-mode
2583          gtk::selection-mode "gtk_tree_selection_get_mode" "gtk_tree_selection_set_mode")
2584   (:cffi gtk::select-function
2585          gtk::tree-selection-select-function
2586          nil gtk::tree-selection-get-selection-function gtk::tree-selection-set-select-function)))
2587 @end lisp
2588
2589 @node Generating names for CLOS classes and accessors
2590 @section Generating names for CLOS classes and accessors
2591
2592 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).
2593
2594 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.
2595
2596 Name generation affected by following variables:
2597 @itemize
2598 @item @var{*strip-prefix*}
2599 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}.
2600 @item @var{*lisp-name-exceptions*}
2601 A plist mapping from strings (type names) to symbols (class names) that have special name processing.
2602 Example:
2603 @lisp
2604 `(("GObject" gobject:g-object)
2605   ("GtkObject" ,(intern "GTK-OBJECT" (find-package :gtk)))
2606   ("GInitiallyUnowned" gobject::g-initially-unowned)
2607   ("GtkWindow" ,(intern "GTK-WINDOW" (find-package :gtk)))
2608   ("GtkUIManager" ,(intern "UI-MANAGER" (find-package :gtk)))
2609   ("GtkUIManagerItemType" ,(intern "UI-MANAGER-ITEM-TYPE" (find-package :gtk))))
2610 @end lisp
2611 @end itemize
2612
2613 @node generate-types-hierarchy-to-file
2614 @section generate-types-hierarchy-to-file
2615
2616 @Function generate-types-hierarchy-to-file
2617 @lisp
2618 (generate-types-hierarchy-to-file file
2619                                   root-type
2620                                   &key include-referenced
2621                                   prefix
2622                                   package
2623                                   exceptions
2624                                   prologue
2625                                   interfaces
2626                                   enums
2627                                   flags
2628                                   objects
2629                                   exclusions
2630                                   additional-properties)
2631 @end lisp
2632
2633 @table @var
2634 @item @var{file}
2635 A string or pathname naming the file, or a stream.
2636 @item @var{root-type}
2637 A GType designator for a root type. All types that inherit from this type will be defined.
2638 @item @var{&key include-referenced}
2639 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
2640 @item @var{prefix}
2641 A string naming the prefix that should be removed from the beginning of names
2642 @item @var{package}
2643 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
2644 @item @var{exceptions}
2645 A plist that maps GType names to their Lisp names.
2646 See @ref{Generating names for CLOS classes and accessors} for more info on exceptions from name generation mechanism
2647 @item @var{prologue}
2648 A string that will be included verbatim in generated code file
2649 @item @var{interfaces}
2650 Additional list of interfaces that will also be included in generation
2651 @item @var{enums}
2652 Additional list of enums that will also be included in generation
2653 @item @var{flags}
2654 Additional list of flags that will also be included in generation
2655 @item @var{objects}
2656 Additional list of object types that will also be included in generation
2657 @item @var{exclusions}
2658 A list of GType names that will be excluded from generation
2659 @item @var{additional-properties}
2660 A plist of properties definitions that will be added to generated classes.
2661 See @ref{Specifying additional properties for CLOS classes} for more information.
2662 @end table
2663
2664 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.
2665
2666 Example of usage:
2667 @lisp
2668 (generate-types-hierarchy-to-file
2669  "gtk.generated-classes.lisp"
2670  "GtkObject"
2671  :include-referenced t
2672  :prefix "Gtk"
2673  :package (or (find-package :gtk) (make-package :gtk))
2674  :exceptions `(("GObject" gobject:g-object)
2675                ("GtkObject" ,(intern "GTK-OBJECT" (find-package :gtk)))
2676                ("GInitiallyUnowned" gobject::g-initially-unowned)
2677                ("GtkWindow" ,(intern "GTK-WINDOW" (find-package :gtk)))
2678                ("GtkUIManager" ,(intern "UI-MANAGER" (find-package :gtk)))
2679                ("GtkUIManagerItemType" ,(intern "UI-MANAGER-ITEM-TYPE" (find-package :gtk))))
2680  :prologue (format nil "(in-package :gtk)")
2681  :interfaces '("GtkBuildable" "GtkCellEditable" ...)
2682  :objects '("GtkSettings" "GtkRcStyle" ...)
2683  :flags '("GtkTextSearchFlags" "GtkAccelFlags" ...)
2684  :enums '("GtkTextDirection" "GtkSizeGroupMode" ...)
2685  :exclusions '("PangoStretch" "PangoVariant" ...)
2686  :additional-properties
2687  '(("GtkTreeViewColumn"
2688     (:cffi
2689      gtk::tree-view
2690      gtk::tree-view-column-tree-view
2691      g-object
2692      "gtk_tree_view_column_get_tree_view"
2693      nil)
2694     ...)
2695    ...))
2696 @end lisp
2697
2698 @bye
2699