add more documentation
[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 Accessor {args}
20 @deffn {Accessor} \args\
21 @end deffn
22 @end macro
23
24 @macro GenericFunction {args}
25 @deffn {Generic Function} \args\
26 @end deffn
27 @end macro
28
29 @macro ForeignType {args}
30 @deftp {Foreign Type} \args\
31 @end deftp
32 @end macro
33
34 @macro Variable {args}
35 @defvr {Special Variable} \args\
36 @end defvr
37 @end macro
38
39 @macro Condition {args}
40 @deftp {Condition Type} \args\
41 @end deftp
42 @end macro
43
44 @macro cffi
45 @acronym{CFFI}
46 @end macro
47
48 @macro impnote {text}
49 @quotation
50 @strong{Implementor's note:} @emph{\text\}
51 @end quotation
52 @end macro
53
54 @c Info "requires" that x-refs end in a period or comma, or ) in the
55 @c case of @pxref.  So the following implements that requirement for
56 @c the "See also" subheadings that permeate this manual, but only in
57 @c Info mode.
58 @ifinfo
59 @macro seealso {name}
60 @ref{\name\}.
61 @end macro
62 @end ifinfo
63
64 @ifnotinfo
65 @alias seealso = ref
66 @end ifnotinfo
67
68 @c Typeset comments in roman font for the TeX output.
69 @iftex
70 @alias lispcmt = r
71 @end iftex
72 @ifnottex
73 @alias lispcmt = asis
74 @end ifnottex
75
76 @c My copy of makeinfo is not generating any HTML for @result{} for
77 @c some odd reason. (It certainly used to...)
78 @ifhtml
79 @macro result
80 =>
81 @end macro
82 @end ifhtml
83
84 @c Similar macro to @result. Its purpose is to work around the fact
85 @c that ⇒ does not work properly inside @lisp.
86 @ifhtml
87 @macro res
88 @html
89 ⇒
90 @end html
91 @end macro
92 @end ifhtml
93
94 @ifnothtml
95 @alias res = result
96 @end ifnothtml
97
98 @c ============================= Macros =============================
99
100
101 @c Show types, functions, and concepts in the same index.
102 @syncodeindex tp cp
103 @syncodeindex fn cp
104
105 @titlepage
106 @title CL-GTK2
107 @subtitle A Common Lisp binding for Gtk+
108 @subtitle GObject
109 @author Dmitry Kalyanov
110 @end titlepage
111
112 @contents
113
114 @ifnottex
115 @node Top
116 @top cl-gtk2-gobject
117 @end ifnottex
118
119 @menu
120 * Introduction::
121 * Installation::
122 * GType designator::
123 * Type hierarchy and type relations::
124 * Object types information::
125 * Enum types information::
126 * Using GValues::
127 * Stable pointers::
128 * Closures::
129 * GObject low-level::
130 * GObject high-level::
131 * Subclassing GObjects and implementing GInterfaces::
132 * GBoxed::
133 * Generating type definitions by introspection::
134 @end menu
135
136 @node Introduction
137 @chapter Introduction
138
139 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.
140
141 @node Installation
142 @chapter Installation
143
144 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}.
145
146 To use the CL-GTK2-GObject, download and install CL-GTK2 bindings and load the ASDF system @code{cl-gtk2-glib}.
147
148 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.
149
150 @node GType designator
151 @chapter GType designator
152
153 @menu
154 * g-type-string::
155 * g-type-numeric::
156 @end menu
157
158 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.
159
160 An example of GType designator is a string @code{"GObject"} and the numeric value 80 that corresponds to it.
161
162 Some of the types are fundamental and have constant integer values. They are identified by constants:
163 @itemize
164 @item @code{+g-type-invalid+}. An invalid GType used as error return value in some functions which return a GType.
165 @item @code{+g-type-void+}. A fundamental type which is used as a replacement for the C @code{void} return type.
166 @item @code{+g-type-interface+}. The fundamental type from which all interfaces are derived.
167 @item @code{+g-type-char+}. 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}.
168 @item @code{+g-type-uchar+}. The fundamental type corresponding to @code{guchar}.
169 @item @code{+g-type-boolean+}. The fundamental type corresponding to @code{gboolean}.
170 @item @code{+g-type-int+}. The fundamental type corresponding to @code{gint}.
171 @item @code{+g-type-uint+}. The fundamental type corresponding to @code{guint}.
172 @item @code{+g-type-long+}. The fundamental type corresponding to @code{glong}.
173 @item @code{+g-type-ulong+}. The fundamental type corresponding to @code{gulong}.
174 @item @code{+g-type-int64+}. The fundamental type corresponding to @code{gint64}.
175 @item @code{+g-type-uint64+}. The fundamental type corresponding to @code{guint64}.
176 @item @code{+g-type-enum+}. The fundamental type from which all enumeration types are derived.
177 @item @code{+g-type-flags+}. The fundamental type from which all flags types are derived.
178 @item @code{+g-type-float+}. The fundamental type corresponding to @code{gfloat}.
179 @item @code{+g-type-double+}. The fundamental type corresponding to @code{gdouble}.
180 @item @code{+g-type-string+}. The fundamental type corresponding to null-terminated C strings.
181 @item @code{+g-type-pointer+}. The fundamental type corresponding to @code{gpointer}.
182 @item @code{+g-type-boxed+}. The fundamental type from which all boxed types are derived.
183 @item @code{+g-type-param+}. The fundamental type from which all GParamSpec types are derived.
184 @item @code{+g-type-object+}. The fundamental type for GObject.
185 @end itemize
186
187 Functions @ref{g-type-string} and @ref{g-type-numeric} return the numeric and string representations of GType designators (given any of them).
188
189 Invalid type (the GType that does not exist) is identified as a 0 or @code{NIL}.
190
191 @example
192 (g-type-numeric "GObject") @result{} 80
193 (g-type-numeric 80) @result{} 80
194 (g-type-string "GObject") @result{} "GObject"
195 (g-type-string 80) @result{} "GObject"
196 (g-type-numeric "GtkWidget") @result{} 6905648 ;;Will be different on each run
197 @end example
198
199 @node g-type-string
200 @section g-type-string
201
202 @code{(g-type-string g-type-designator) @result{} name}
203
204 @table @var
205 @item @var{g-type-designator}
206 The GType designator for the GType
207 @item @var{name}
208 The name of GType
209 @end table
210
211 Returns the name of GType.
212
213 @node g-type-numeric
214 @section g-type-numeric
215
216 @code{(g-type-numeric g-type-designator) @result{} GType}
217
218 @table @var
219 @item @var{g-type-designator}.
220 The GType designator for the GType.
221 @item @var{GType}
222 The numeric identifier of GType
223 @end table
224
225 Returns the numeric identifier of GType
226
227 @node Type hierarchy and type relations
228 @chapter Type hierarchy and type relations
229
230 @menu
231 * g-type-children::
232 * g-type-parent::
233 * g-type-fundamental::
234 * g-type-depth::
235 * g-type-next-base::
236 @end menu
237
238 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.
239
240 There are functions to query some specific information:
241 @itemize
242 @item @code{g-type-fundamental} retrieves the fundamental type for given type
243 @item @code{g-type-depth} calculates the depth of the type in type hierarchy
244 @item @code{g-type-next-base} calculates the first step in the path from base type to descendent type
245 @end itemize
246
247 @node g-type-children
248 @section g-type-children
249
250 @code{(g-type-children type) @result{} children}
251 @table @var
252 @item @var{type}
253 A GType designator
254 @item @var{children}
255 A list of GType designators
256 @end table
257
258 Returns the list of descendent types.
259
260 Example:
261 @example
262 (g-type-children "GtkButton")
263 @result{}
264 ("GtkToggleButton" "GtkColorButton" "GtkFontButton" "GtkLinkButton" "GtkScaleButton")
265 @end example
266
267 @node g-type-parent
268 @section g-type-parent
269
270 @code{(g-type-parent type) @result{} parent}
271
272 @table @var
273 @item @var{type}
274 A GType designator
275 @item @var{parent}
276 A GType designator
277 @end table
278
279 Returns the parent of @code{type}.
280
281 Example:
282 @example
283 (g-type-parent "GtkToggleButton")
284 @result{}
285 "GtkButton"
286 @end example
287 @node g-type-fundamental
288 @section g-type-fundamental
289
290 @code{(g-type-fundamental type) @result{} fundamental-type}
291
292 @table @var
293 @item @var{type}
294 A GType designator
295 @item @var{fundamental-type}
296 A GType designator for one of the fundamental types
297 @end table
298
299 Returns the fundamental type that is the ancestor of @code{type}.
300
301 Example:
302 @example
303 (g-type-fundamental "GtkButton") @result{} "GObject"
304
305 (g-type-fundamental "GtkWindowType") @result{} "GEnum"
306
307 (g-type-fundamental "GdkEvent") @result{} "GBoxed"
308 @end example
309
310 @node g-type-depth
311 @section g-type-depth
312
313 @code{(g-type-depth type) @result{} depth}
314
315 @table @var
316 @item @var{type}
317 A GType designator
318 @item @var{depth}
319 An integer
320 @end table
321
322 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.
323
324 Example:
325 @example
326 (g-type-depth "GObject") @result{} 1
327 (g-type-depth "GInitiallyUnowned") @result{} 2
328 @end example
329
330 @node g-type-next-base
331 @section g-type-next-base
332
333 @code{(g-type-next-base leaf-type root-type) @result{} base-type}
334
335 @table @var
336 @item @var{leaf-type}
337 A GType designator
338 @item @var{root-type}
339 A GType designator
340 @item @var{base-type}
341 A GType designator
342 @end table
343
344 Returns the next type that should be traversed from @code{root-type} in order to reach @code{leaf-type}. E.g., given type hierarchy:
345 @example
346 + GObject
347  \
348   + GInitiallyUnowned
349    \
350     + GtkObject
351     |\
352     | + GtkAdjustment
353      \
354       + GtkWidget
355        \
356         + GtkContainer
357          \
358           + GtkTable
359 @end example
360
361 the following will be returned:
362
363 @example
364 (g-type-next-base "GtkTable" "GObject") @result{} "GInitiallyUnowned"
365 (g-type-next-base "GtkTable" "GInitiallyUnowned") @result{} "GtkObject"
366 (g-type-next-base "GtkTable" "GtkObject") @result{} "GtkWidget"
367 (g-type-next-base "GtkTable" "GtkWidget") @result{} "GtkContainer"
368 (g-type-next-base "GtkTable" "GtkContainer") @result{} "GtkTable"
369 @end example
370
371 @node Object types information
372 @chapter Object types information
373 @menu
374 * g-class-property-definition::
375 * class-properties::
376 * class-property-info::
377 * interface-properties::
378 * signal-info::
379 * type-signals::
380 * parse-signal-name::
381 * query-signal-info::
382 * g-type-interfaces::
383 * g-type-interface-prerequisites::
384 @end menu
385
386 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.
387
388 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.
389
390 @node g-class-property-definition
391 @section g-class-property-definition
392
393 @example
394 (defstruct g-class-property-definition
395   name
396   type
397   readable
398   writable
399   constructor
400   constructor-only
401   owner-type)
402 @end example
403
404 @table @var
405 @item @var{name}
406 A string that names the property
407 @item @var{type}
408 A GType designator. Identifies the type of the property
409 @item @var{readable}
410 A boolean. Identifies whether the property can be read
411 @item @var{writable}
412 A boolean. Identifies whether the property can be assigned
413 @item @var{constructor}
414 A boolean. Identifies whether constructor of object accepts this property
415 @item @var{constructor-only}
416 A boolean. Identifies whether this property may only be set in constructor, not in property setter
417 @item @var{owner-type}
418 A GType designator. Identifies the type on which the property was defined.
419 @end table
420
421 This structure identifies a single property. Its field specify attributes of a property.
422
423 Structures of this type have shortened print syntax:
424 @example
425 #<PROPERTY gchararray GtkButton.label (flags: readable writable constructor)> 
426 @end example
427
428 (When @code{*print-readably*} is T, usual @code{defstruct} print syntax is used)
429
430 This syntax specifies:
431 @itemize
432 @item type of property
433 @item the owner type of property
434 @item name of property
435 @item additional flags of property
436 @end itemize
437
438 @node class-properties
439 @section class-properties
440
441 @code{(class-properties type) @result{} properties}
442
443 @table @var
444 @item @var{type}
445 A GType designator. Specifies the object type (class)
446 @item @var{properties}
447 A list of @code{g-property-definition} structures.
448 @end table
449
450 This function returns the list of properties that are available in class @code{type}.
451
452 Example:
453 @example
454 (class-properties "GtkWidget")
455 @result{}
456 (#<PROPERTY gpointer GtkObject.user-data (flags: readable writable)>
457  #<PROPERTY gchararray GtkWidget.name (flags: readable writable)>
458  #<PROPERTY GtkContainer GtkWidget.parent (flags: readable writable)>
459  #<PROPERTY gint GtkWidget.width-request (flags: readable writable)>
460  #<PROPERTY gint GtkWidget.height-request (flags: readable writable)>
461  #<PROPERTY gboolean GtkWidget.visible (flags: readable writable)>
462  #<PROPERTY gboolean GtkWidget.sensitive (flags: readable writable)>
463  #<PROPERTY gboolean GtkWidget.app-paintable (flags: readable writable)>
464  #<PROPERTY gboolean GtkWidget.can-focus (flags: readable writable)>
465  #<PROPERTY gboolean GtkWidget.has-focus (flags: readable writable)>
466  #<PROPERTY gboolean GtkWidget.is-focus (flags: readable writable)>
467  #<PROPERTY gboolean GtkWidget.can-default (flags: readable writable)>
468  #<PROPERTY gboolean GtkWidget.has-default (flags: readable writable)>
469  #<PROPERTY gboolean GtkWidget.receives-default (flags: readable writable)>
470  #<PROPERTY gboolean GtkWidget.composite-child (flags: readable)>
471  #<PROPERTY GtkStyle GtkWidget.style (flags: readable writable)>
472  #<PROPERTY GdkEventMask GtkWidget.events (flags: readable writable)>
473  #<PROPERTY GdkExtensionMode GtkWidget.extension-events (flags: readable writable)>
474  #<PROPERTY gboolean GtkWidget.no-show-all (flags: readable writable)>
475  #<PROPERTY gboolean GtkWidget.has-tooltip (flags: readable writable)>
476  #<PROPERTY gchararray GtkWidget.tooltip-markup (flags: readable writable)>
477  #<PROPERTY gchararray GtkWidget.tooltip-text (flags: readable writable)>
478  #<PROPERTY GdkWindow GtkWidget.window (flags: readable)>)
479 @end example
480
481 @node class-property-info
482 @section class-property-info
483 @code{(class-property-info type property-name) @result{} property}
484
485 @table @var
486 @item @var{type}
487 A GType designator
488 @item @var{property-name}
489 A string naming the property
490 @item @var{property}
491 An instance of @code{g-property-definition} structure
492 @end table
493
494 Returns the property information for a single property.
495
496 Example:
497 @example
498 (class-property-info "GtkButton" "label")
499 @result{}
500 #<PROPERTY gchararray GtkButton.label (flags: readable writable constructor)>
501 @end example
502
503 @node interface-properties
504 @section interface-properties
505
506 @code{(interface-properties type) @result{} properties}
507
508 @table @var
509 @item @var{type}
510 A GType designator
511 @item @var{properties}
512 A list of @code{g-property-definition} structures
513 @end table
514
515 This function returns the list of properties that are available in interface @code{type}.
516
517 Example:
518 @example
519 (interface-properties "GtkFileChooser")
520 @result{}
521 (#<PROPERTY GtkWidget GtkFileChooser.extra-widget (flags: readable writable)>
522  #<PROPERTY gboolean GtkFileChooser.use-preview-label (flags: readable writable)>
523  #<PROPERTY gboolean GtkFileChooser.preview-widget-active (flags: readable writable)>
524  #<PROPERTY gboolean GtkFileChooser.show-hidden (flags: readable writable)>
525  #<PROPERTY gchararray GtkFileChooser.file-system-backend (flags: writable constructor-only)>
526  #<PROPERTY GtkFileChooserAction GtkFileChooser.action (flags: readable writable)>
527  #<PROPERTY GtkFileFilter GtkFileChooser.filter (flags: readable writable)>
528  #<PROPERTY gboolean GtkFileChooser.select-multiple (flags: readable writable)>
529  #<PROPERTY GtkWidget GtkFileChooser.preview-widget (flags: readable writable)>
530  #<PROPERTY gboolean GtkFileChooser.local-only (flags: readable writable)>
531  #<PROPERTY gboolean GtkFileChooser.do-overwrite-confirmation (flags: readable writable)>)
532 @end example
533
534 @node signal-info
535 @section signal-info
536
537 @example
538 (defstruct signal-info
539   id
540   name
541   owner-type
542   flags
543   return-type
544   param-types
545   detail)
546 @end example
547
548 @table @var
549 @item @var{id}
550 An integer - the identifier of a signal
551 @item @var{name}
552 Name of a signal
553 @item @var{owner-type}
554 A GType designator identifying the type on which the signal was defined
555 @item @var{flags}
556 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
557 @item @var{return-type}
558 The return type of a signal (and signal handlers)
559 @item @var{param-types}
560 A list of GType designators that specify the types of signal parameters
561 @item @var{detail}
562 A string. Specifies the "detail" part of a signal name. E.g., @code{"label"} for signal @code{"notify::label"}.
563 @end table
564
565 When @code{*print-readably*} is nil, the following print syntax is used:
566 @example
567 #<Signal [#1] void GObject.notify::label(GParam) [RUN-FIRST, NO-RECURSE, DETAILED, ACTION, NO-HOOKS]>
568 #<Signal [#54] gboolean GtkWidget.proximity-in-event(GdkEvent) [RUN-LAST]>
569 #<Signal [#64] void GtkWidget.drag-data-received(GdkDragContext, gint, gint, GtkSelectionData, guint, guint) [RUN-LAST]>
570 #<Signal [#8] void GtkObject.destroy() [RUN-CLEANUP, NO-RECURSE, NO-HOOKS]>
571 @end example
572
573 This syntax specifies:
574 @itemize
575 @item the signal id
576 @item signal return type
577 @item owner type
578 @item signal name
579 @item detail
580 @item list of types of parameters
581 @item flags
582 @end itemize
583
584 @node type-signals
585 @section type-signals
586 @code{(type-signals type &key (include-inherited t)) @result{} signals}
587 @table @var
588 @item @var{type}
589 A GType designator
590 @item @var{signals}
591 A list of @code{signal-info} structures
592 @item @var{include-inherited}
593 A boolean that specifies whether to include signals defined on this type or also on ancestor types.
594 @end table
595
596 Returns the list of signals that are available in type @code{type}.
597
598 Example:
599 @example
600 (type-signals "GtkLabel" :include-inherited nil)
601 @result{}
602 (#<Signal [#138] void GtkLabel.move-cursor(GtkMovementStep, gint, gboolean) [RUN-LAST, ACTION]>
603  #<Signal [#139] void GtkLabel.copy-clipboard() [RUN-LAST, ACTION]>
604  #<Signal [#140] void GtkLabel.populate-popup(GtkMenu) [RUN-LAST]>)
605 @end example
606
607 @node parse-signal-name
608 @section parse-signal-name
609
610 @code{(parse-signal-name type signal-name) @result{} signal}
611
612 @table @var
613 @item @var{type}
614 A GType designator that has the signal.
615 @item @var{signal-name}
616 A string that identifies the signal.
617 @item @var{signal}
618 A list @code{signal-info} structures.
619 @end table
620
621 Parses the signal name and returns the corresponding information. @code{signal-name} may include the detail part.
622
623 Example:
624 @example
625 (parse-signal-name "GObject" "notify::label")
626 @result{}
627 #<Signal [#1] void GObject.notify::label(GParam) [RUN-FIRST, NO-RECURSE, DETAILED, ACTION, NO-HOOKS]>
628 @end example
629
630 @node query-signal-info
631 @section query-signal-info
632 @code{(query-signal-info signal-id) @result{} signal}
633 @table @var
634 @item @var{signal-id}
635 An integer identifying the signal
636 @item @var{signal}
637 An instance of @code{signal-info} structure
638 @end table
639
640 Retrieves the signal information by its id.
641
642 Example:
643 @example
644 (query-signal-info 73)
645 @result{}
646 #<Signal [#73] gboolean GtkWidget.show-help(GtkWidgetHelpType) [RUN-LAST, ACTION]>
647 @end example
648
649 @node g-type-interfaces
650 @section g-type-interfaces
651
652 @code{(g-type-interfaces type) @result{} interfaces}
653
654 @table @var
655 @item @var{type}
656 A GType designator
657 @item @var{interfaces}
658 A list of GType designators
659 @end table
660
661 Returns the list of interfaces that @code{type} implements.
662
663 Example:
664 @example
665 (g-type-interfaces "GtkButton")
666 @result{}
667 ("AtkImplementorIface" "GtkBuildable" "GtkActivatable")
668 @end example
669
670 @node g-type-interface-prerequisites
671 @section g-type-interface-prerequisites
672
673 @code{(g-type-interface-prerequisites type) @result{} types}
674
675 @table @var
676 @item @var{type}
677 A GType designator of interface
678 @item @var{types}
679 A list of GType designators specifying the interface prerequisites
680 @end table
681
682 Returns the prerequisites of an interface @code{type}. Prerequisite is a type that should be an ancestor of a type implementing interface @code{type}.
683
684 Example:
685 @example
686 (g-type-interface-prerequisites "GtkCellEditable")
687 @result{}
688 ("GtkObject" "GtkWidget")
689 @end example
690
691 @node Enum types information
692 @chapter Enum types information
693 @menu
694 * enum-item::
695 * flags-item::
696 * get-enum-items::
697 * get-flags-items::
698 @end menu
699
700 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.
701
702 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.
703
704 @node enum-item
705 @section enum-item
706 @example
707 (defstruct enum-item
708   name value nick)
709 @end example
710
711 @table @var
712 @item @var{name}
713 A string - name of enum item
714 @item @var{value}
715 An integer - numeric value of enum item
716 @item @var{nick}
717 A string - short name of an enum item
718 @end table
719
720 Structure @code{enum-item} represents a single item of an enumeration type.
721
722 Example:
723 @example
724 #S(ENUM-ITEM :NAME "GTK_WINDOW_TOPLEVEL" :VALUE 0 :NICK "toplevel")
725 @end example
726
727 @node flags-item
728 @section flags-item
729 @example
730 (defstruct flags-item
731   name value nick)
732 @end example
733
734 @table @var
735 @item @var{name}
736 A string - name of flags item
737 @item @var{value}
738 An integer - numeric value of flags item
739 @item @var{nick}
740 A string - short name of an flags item
741 @end table
742
743 Structure @code{flags-item} represents a single item of an flags type.
744
745 Example:
746 @example
747 #S(FLAGS-ITEM
748    :NAME "GDK_POINTER_MOTION_HINT_MASK"
749    :VALUE 8
750    :NICK "pointer-motion-hint-mask")
751 @end example
752
753 @node get-enum-items
754 @section get-enum-items
755
756 @code{(get-enum-items type) @result{} items}
757
758 @table @var
759 @item @var{type}
760 A GType designator of an enum type
761 @item @var{items}
762 A list of @code{enum-item} structures
763 @end table
764
765 Returns a list of items in an enumeration
766
767 Example:
768 @example
769 (get-enum-items "GtkScrollType")
770 @result{}
771 (#S(ENUM-ITEM :NAME "GTK_SCROLL_NONE" :VALUE 0 :NICK "none")
772  #S(ENUM-ITEM :NAME "GTK_SCROLL_JUMP" :VALUE 1 :NICK "jump")
773  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_BACKWARD" :VALUE 2 :NICK "step-backward")
774  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_FORWARD" :VALUE 3 :NICK "step-forward")
775  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_BACKWARD" :VALUE 4 :NICK "page-backward")
776  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_FORWARD" :VALUE 5 :NICK "page-forward")
777  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_UP" :VALUE 6 :NICK "step-up")
778  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_DOWN" :VALUE 7 :NICK "step-down")
779  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_UP" :VALUE 8 :NICK "page-up")
780  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_DOWN" :VALUE 9 :NICK "page-down")
781  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_LEFT" :VALUE 10 :NICK "step-left")
782  #S(ENUM-ITEM :NAME "GTK_SCROLL_STEP_RIGHT" :VALUE 11 :NICK "step-right")
783  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_LEFT" :VALUE 12 :NICK "page-left")
784  #S(ENUM-ITEM :NAME "GTK_SCROLL_PAGE_RIGHT" :VALUE 13 :NICK "page-right")
785  #S(ENUM-ITEM :NAME "GTK_SCROLL_START" :VALUE 14 :NICK "start")
786  #S(ENUM-ITEM :NAME "GTK_SCROLL_END" :VALUE 15 :NICK "end"))
787 @end example
788
789 @node get-flags-items
790 @section get-flags-items
791
792 @code{(get-flags-items type) @result{} items}
793
794 @table @var
795 @item @var{type}
796 A GType designator of an flags type
797 @item @var{items}
798 A list of @code{flags-item} structures
799 @end table
800
801 Returns a list of items in an flags type
802
803 Example:
804 @example
805 (get-flags-items "GtkAttachOptions")
806 @result{}
807 (#S(FLAGS-ITEM :NAME "GTK_EXPAND" :VALUE 1 :NICK "expand")
808  #S(FLAGS-ITEM :NAME "GTK_SHRINK" :VALUE 2 :NICK "shrink")
809  #S(FLAGS-ITEM :NAME "GTK_FILL" :VALUE 4 :NICK "fill"))
810 @end example
811
812 @node Using GValues
813 @chapter Using GValues
814 @menu
815 * g-value-zero::
816 * g-value-init::
817 * g-value-unset::
818 * parse-g-value::
819 * set-g-value::
820 * Registering types::
821 @end menu
822
823 GValue is a generic container for arbitrary value of type supported by GType system. Refer to GObject documentation for more detailed information.
824
825 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.
826
827 GValue is used whenever a value of unkown type should be passed. It is used in:
828 @itemize
829 @item Closure marshal functions
830 @item Property get and set functions
831 @end itemize
832
833 Example of usage:
834 @example
835 (cffi:with-foreign-object (gval 'g-value)
836   (set-g-value gval "Hello" "gchararray" :zero-g-value t)
837   (format t "~S~%" (parse-g-value gval))
838   (g-value-unset gval))
839 @result{}
840 "Hello"
841 @end example
842
843 @node g-value-zero
844 @section g-value-zero
845 @code{(g-value-zero g-value)}
846 @table @var
847 @item @var{g-value}
848 A foreign pointer to GValue structure.
849 @end table
850
851 Initializes the GValue to "unset" state. Equivalent of the following initializer in C:
852 @example
853 GValue value = @{ 0 @};
854 @end example
855
856 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).
857
858 @node g-value-init
859 @section g-value-init
860
861 @code{(g-value-init value type)}
862 @table @var
863 @item @var{value}
864 A foreign pointer to GValue structure
865 @item @var{type}
866 A GType designator
867 @end table
868
869 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).
870
871 @node g-value-unset
872 @section g-value-unset
873 @code{(g-value-unset value)}
874 @table @var
875 @item @var{value}
876 A foreign pointer to GValue structure.
877 @end table
878
879 Unsets the GValue. This frees all resources associated with GValue.
880
881 @node parse-g-value
882 @section parse-g-value
883 @code{(parse-g-value value) @result{} object}
884 @table @var
885 @item @var{value}
886 A foreign pointer to GValue structure
887 @item @var{object}
888 A Lisp object
889 @end table
890
891 Retrieves the object from GValue structure.
892
893 @node set-g-value
894 @section set-g-value
895 @code{(set-g-value gvalue object type &key zero-g-value unset-g-value (g-value-init t))}
896
897 @table @var
898 @item @var{gvalue}
899 A foreign pointer to GValue structure
900 @item @var{object}
901 An object that is to be assigned to @code{gvalue}
902 @item @var{type}
903 A GType designator specifying what GType should be set
904 @item @var{unset-g-value}
905 A boolean specifying whether to call @code{g-value-unset} before assigment.
906 @item @var{zero-g-value}
907 A boolean specifying whether to call @code{g-value-zero} before assignment
908 @item @var{g-value-init}
909 A boolean specifying whether to call @code{g-value-init} before assignment
910 @end table
911
912 Assigns the @code{object} to the @code{gvalue}. When GValue is not used, call @code{g-value-unset} to deinitialize the @code{GValue}.
913
914 @node Registering types
915 @section Registering types
916
917 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.
918
919 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.
920
921 @subsection
922 @code{(register-enum-type name type)}
923 @table @var
924 @item @var{name}
925 A string naming the GEnum type
926 @item @var{type}
927 A symbol - name of CFFI foreign enum type
928 @end table
929
930 Registers the @code{type} to be used for passing value of GEnum type @code{name} between GObject and Lisp.
931
932 Example:
933 @example
934 (defcenum text-direction
935   :none :ltr :rtl)
936 (register-enum-type "GtkTextDirection" 'text-direction)
937 @end example
938
939 @subsection
940 @code{(register-flags-type name type)}
941 @table @var
942 @item @var{name}
943 A string naming the GFlags type
944 @item @var{type}
945 A symbol - name of CFFI foreign flags type
946 @end table
947
948 Registers the @code{type} to be used for passing value of GFlags type @code{name} between GObject and Lisp.
949
950 Example:
951 @example
952 (defcenum state-type
953   :normal :active :prelight :selected :insensitive)
954 (register-enum-type "GtkStateType" 'state-type)
955 @end example
956
957 @node Stable pointers
958 @chapter Stable pointers
959 @menu
960 * allocate-stable-pointer::
961 * free-stable-pointer::
962 * stable-pointer-value::
963 * with-stable-pointer::
964 @end menu
965
966 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.
967
968 @node allocate-stable-pointer
969 @section allocate-stable-pointer
970
971 @code{(allocate-stable-pointer thing) @result{} stable-pointer}
972
973 @table @var
974 @item @var{thing}
975 An arbitrary Lisp object
976 @item @var{stable-pointer}
977 A foreign pointer
978 @end table
979
980 Allocates a stable pointer to @code{thing}.
981
982 (Note: @var{stable-pointer} should not be dereferenced with @code{cffi:mem-ref}. It should only be dereferenced with @code{stable-pointer-value})
983
984 Example:
985 @example
986 (allocate-stable-pointer (lambda (x) (+ x 10)))
987 @result{}
988 #.(SB-SYS:INT-SAP #X00000002)
989
990 (stable-pointer-value *)
991 @result{}
992 #<FUNCTION (LAMBDA (X)) @{1004D016F9@}>
993
994 (free-stable-pointer **)
995 @result{}
996 NIL
997 @end example
998
999 @node free-stable-pointer
1000 @section free-stable-pointer
1001
1002 @code{(free-stable-pointer stable-pointer)}
1003
1004 @table @var
1005 @item @var{stable-pointer}
1006 A foreign pointer that was created with @code{allocate-stable-pointer}.
1007 @end table
1008
1009 Frees the stable pointer, enabling the garbage collector to reclaim the object.
1010
1011 Example:
1012 @example
1013 (allocate-stable-pointer (lambda (x) (+ x 10)))
1014 @result{}
1015 #.(SB-SYS:INT-SAP #X00000002)
1016
1017 (stable-pointer-value *)
1018 @result{}
1019 #<FUNCTION (LAMBDA (X)) @{1004D016F9@}>
1020
1021 (free-stable-pointer **)
1022 @result{}
1023 NIL
1024 @end example
1025
1026 @node stable-pointer-value
1027 @section stable-pointer-value
1028
1029 @example
1030 (stable-pointer-value stable-pointer) @result{} thing
1031 (setf (stable-pointer-value stable-pointer) thing)
1032 @end example
1033
1034 @table @var
1035 @item @var{stable-pointer}
1036 A foreign pointer created by @code{allocate-stable-pointer}
1037 @item @var{thing}
1038 A Lisp object
1039 @end table
1040
1041 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.
1042
1043 @node with-stable-pointer
1044 @section with-stable-pointer
1045
1046 @code{(with-stable-pointer (ptr expr) &body body)}
1047
1048 @table @var
1049 @item @var{ptr}
1050 A variable that will be bound to the stable pointer
1051 @item @var{expr}
1052 An expression that will be evaluated once and its value will be bound to stable pointer's value
1053 @end table
1054
1055 Executes the body with the @code{ptr} variable being bound to a stable pointer whose value is determined by @code{expr}.
1056
1057 Example:
1058 @example
1059 (with-stable-pointer (ptr (lambda (x) (+ x 10)))
1060   (print (stable-pointer-value ptr)))
1061 ;;Prints:
1062 #<FUNCTION (LAMBDA (X)) @{1004807E79@}>
1063 @end example
1064
1065 @node Closures
1066 @chapter Closures
1067
1068 Closure are anonymous functions that capture their lexical environment.
1069
1070 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).
1071
1072 @section create-g-closure
1073 @code{(create-g-closure fn) @result{} closure}
1074
1075 @table @var
1076 @item @var{fn}
1077 A function that will be called by closure invokation
1078 @item @var{closure}
1079 A foreign pointer to allocated closure
1080 @end table
1081
1082 Allocates the closure. The closure is destroyed automatically by GObject.
1083
1084 Example:
1085 @example
1086 (create-g-closure (lambda (x) (+ x 10)))
1087 @result{}
1088 #.(SB-SYS:INT-SAP #X006D7B20)
1089 @end example
1090
1091 Example of usage from GObject binding code:
1092 @example
1093 (defun connect-signal (object signal handler &key after)
1094   (g-signal-connect-closure (ensure-object-pointer object)
1095                             signal
1096                             (create-g-closure handler)
1097                             after))
1098 @end example
1099
1100 (TODO: GObject defines finer closure API: g_closure_ref, g_closure_unref, g_closure_invoke. It should be bound.)
1101
1102 @node GObject low-level
1103 @chapter GObject low-level
1104 @menu
1105 * g-object-call-constructor::
1106 * g-type-from-object::
1107 * g-object-call-get-property::
1108 * g-object-call-set-property::
1109 @end menu
1110
1111 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.
1112
1113 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.
1114
1115 @node g-object-call-constructor
1116 @section g-object-call-constructor
1117
1118 @code{(g-object-call-constructor object-type args-names args-values &optional args-types) @result{} object-ptr}
1119
1120 @table @var
1121 @item @var{object-type}
1122 A GType designator that specifies the object type that is to be created
1123 @item @var{args-names}
1124 A list of strings naming the arguments to constructor
1125 @item @var{args-value}
1126 A list of arguments values (in the same order as args-names)
1127 @item @var{args-types}
1128 Optional list of arguments types (in the same order as args-names). If not specified, it is detected automatically
1129 @item @var{object-ptr}
1130 A foreign pointer to newly created instance
1131 @end table
1132
1133 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}.
1134
1135 Example:
1136 @example
1137 (g-object-call-constructor "GtkButton" '("label" "use-underline") '("Hello" t) '("gchararray" "gboolean"))
1138 @result{}
1139 #.(SB-SYS:INT-SAP #X006D8900)
1140
1141 (g-object-call-get-property * "label")
1142 @result{}
1143 "Hello"
1144
1145 (g-object-call-get-property ** "use-underline")
1146 @result{}
1147 T
1148 @end example
1149
1150 @node g-type-from-object
1151 @section g-type-from-object
1152
1153 @code{(g-type-from-object object-ptr) @result{} type}
1154
1155 @table @var
1156 @item @var{object-ptr}
1157 A foreign pointer to a GObject instance
1158 @item @var{type}
1159 A GType designator
1160 @end table
1161
1162 Returns the type of an object by a pointer to its instance
1163
1164 Example:
1165 @example
1166 (g-type-from-object (g-object-call-constructor "GtkButton" nil nil))
1167 @result{}
1168 "GtkButton"
1169 @end example
1170
1171 @node g-object-call-get-property
1172 @section g-object-call-get-property
1173
1174 @code{(g-object-call-get-property object-ptr property-name &optional property-type) @result{} property-value}
1175
1176 @table @var
1177 @item @var{object-ptr}
1178 A foreign pointer to a GObject instance
1179 @item @var{property-name}
1180 A string naming the property
1181 @item @var{property-type}
1182 Optional GType designator specifying the type of a property
1183 @item @var{property-value}
1184 The value of a property
1185 @end table
1186
1187 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.
1188
1189 Example:
1190 @example
1191 (g-object-call-constructor "GtkButton" '("label" "use-underline") '("Hello" t) '("gchararray" "gboolean"))
1192 @result{}
1193 #.(SB-SYS:INT-SAP #X006D8900)
1194
1195 (g-object-call-get-property * "label")
1196 @result{}
1197 "Hello"
1198
1199 (g-object-call-get-property ** "use-underline")
1200 @result{}
1201 T
1202 @end example
1203
1204 @node g-object-call-set-property
1205 @section g-object-call-set-property
1206
1207 @code{(g-object-call-set-property object-ptr property-name new-value &optional property-type)}
1208
1209 @table @var
1210 @item @var{object-ptr}
1211 A foreign pointer to a GObject instance
1212 @item @var{property-name}
1213 A string naming the property
1214 @item @var{new-value}
1215 A new value of a property
1216 @item @var{property-type}
1217 Optional GType designator specifying the type of a property
1218 @end table
1219
1220 Sets the property value of property @code{property-name} of object @code{object-ptr} to @code{new-value}.
1221
1222 Example:
1223 @example
1224 (g-object-call-constructor "GtkButton" nil nil)
1225 @result{}
1226 #.(SB-SYS:INT-SAP #X006D8B40)
1227
1228 (g-object-call-set-property * "label" "Hello")
1229 @result{}
1230 ; No value
1231
1232 (g-object-call-get-property ** "label")
1233 @result{}
1234 "Hello"
1235 @end example
1236
1237 @node GObject high-level
1238 @chapter GObject high-level
1239 @menu
1240 * GObject metaclass::
1241 * Using objects::
1242 * Signals::
1243 * GObject foreign class::
1244 @end menu
1245
1246 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):
1247 @itemize
1248 @item objects are created with @code{make-instance}
1249 @item properties are used as regular slots
1250 @end itemize
1251
1252 GObjects are reference counted, and CL-GTK2-GOBJECT manages its own reference to GObjects. This enables to have transparent garbage collection of unreferenced GObjects.
1253
1254 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.
1255
1256 Example GObject class of definition:
1257 @example
1258 (defclass dialog (gtk-window atk-implementor-iface buildable)
1259   ((has-separator :accessor dialog-has-separator
1260                   :initarg :has-separator
1261                   :allocation :gobject-property
1262                   :g-property-type "gboolean"
1263                   :g-property-name "has-separator"))
1264   (:metaclass gobject-class)
1265   (:g-type-name . "GtkDialog")
1266   (:g-type-initializer . "gtk_dialog_get_type"))
1267 @end example
1268
1269 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.
1270
1271 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.
1272
1273 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).
1274
1275 @node GObject metaclass
1276 @section GObject metaclass
1277
1278 See MOP for information what metaclass is and why is it useful.
1279
1280 GObject metaclass @code{gobject-class} bridges two object systems: GObject and CLOS.
1281
1282 Classes that correspond to GObject classes are instances of this class.
1283
1284 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}.
1285
1286 This class has the following slots:
1287 @itemize
1288 @item @var{g-type-name} (accessor @code{gobject-class-g-type-name}, initarg @code{:g-type-name})
1289
1290 Specifies the name of GObject class
1291 @item @var{g-type-initializer} (accessor @code{gobject-class-g-type-initializer}, initarg @code{:g-type-initializer})
1292
1293 Name of type initializer function. This function initializes the class and returns its GType. Typically it is named @code{class_get_type}.
1294 @item @var{interface-p} (accessor @code{gobject-class-interface-p}, initarg @code{:interface-p})
1295
1296 A boolean specifying whether this CLOS class corresponds to GInterface.
1297 @end itemize
1298
1299 This metaclass defines the GObject classes.
1300
1301 Slots which have @code{:allocation} of @code{:gobject-property} are mapped to GObject properties. Such slots have following attributes:
1302 @itemize
1303 @item @var{:g-property-type}
1304
1305 A string naming GType of property
1306 @item @var{:g-property-name}
1307
1308 A name of a property
1309 @end itemize
1310
1311 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:
1312 @itemize
1313 @item @var{:g-property-type}
1314 A CFFI foreign type of property
1315 @item @var{:g-getter}
1316 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)}.
1317 @item @var{:g-setter}
1318 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))}.
1319 @end itemize
1320
1321 Initargs of a slot are used to construct the GObject class.
1322
1323 Example:
1324 @example
1325 (defclass container (widget atk-implementor-iface buildable)
1326     ((border-width :allocation :gobject-property
1327                    :g-property-type "guint"
1328                    :accessor container-border-width
1329                    :initarg :border-width
1330                    :g-property-name "border-width")
1331      (resize-mode :allocation :gobject-property
1332                   :g-property-type "GtkResizeMode"
1333                   :accessor container-resize-mode
1334                   :initarg :resize-mode
1335                   :g-property-name "resize-mode")
1336      (child :allocation :gobject-property
1337             :g-property-type "GtkWidget"
1338             :accessor container-child
1339             :initarg :child
1340             :g-property-name "child")
1341      (focus-child :allocation :gobject-fn
1342                   :g-property-type g-object
1343                   :accessor container-focus-child
1344                   :initarg :focus-child
1345                   :g-getter "gtk_container_get_focus_child"
1346                   :g-setter "gtk_container_set_focus_child")
1347      (focus-vadjustment :allocation :gobject-fn
1348                         :g-property-type (g-object adjustment)
1349                         :accessor container-focus-vadjustment
1350                         :initarg :focus-vadjustment
1351                         :g-getter "gtk_container_get_focus_vadjustment"
1352                         :g-setter "gtk_container_set_focus_vadjustment")
1353      (focus-hadjustment :allocation :gobject-fn
1354                         :g-property-type (g-object adjustment)
1355                         :accessor container-focus-hadjustment
1356                         :initarg :focus-hadjustment
1357                         :g-getter "gtk_container_get_focus_hadjustment"
1358                         :g-setter "gtk_container_set_focus_hadjustment"))
1359     (:metaclass gobject-class)
1360     (:g-type-name . "GtkContainer")
1361     (:g-type-initializer . "gtk_container_get_type"))
1362 @end example
1363 (note the dot in @code{(:g-type-name . "GtkContainer")} and in @code{(:g-type-initializer . "gtk_container_get_type")}. It should be present)
1364
1365 @node Using objects
1366 @section Using objects
1367 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.
1368
1369 Example:
1370 @example
1371 (make-instance 'gtk:dialog :has-separator t)
1372 @result{}
1373 #<GTK:DIALOG @{10036C5A71@}>
1374
1375 (defvar *d* (make-instance 'gtk:dialog :has-separator t))
1376 @result{}
1377 *D*
1378
1379 (gtk:dialog-has-separator *d*)
1380 @result{}
1381 T
1382
1383 (setf (gtk:dialog-has-separator *d*) nil)
1384 @result{}
1385 NIL
1386
1387 (gtk:dialog-has-separator *d*)
1388 @result{}
1389 NIL
1390 @end example
1391
1392 @node Signals
1393 @section Signals
1394
1395 To connect handler to a signal, @code{connect-signal} function is used.
1396
1397 @code{(connect-signal object signal handler &key after)}
1398
1399 @table @var
1400 @item @var{object}
1401 An instance of GObject object
1402 @item @var{signal}
1403 A signal name
1404 @item @var{handler}
1405 A function
1406 @item @var{after}
1407 A boolean specifying whether the handler should be called after the default handler
1408 @end table
1409
1410 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.
1411
1412 Example:
1413 @example
1414 (defvar *d* (make-instance 'gtk:dialog))
1415 @result{}
1416 *D*
1417
1418 *d*
1419 @result{}
1420 #<GTK:DIALOG @{1002D866F1@}>
1421
1422 (parse-signal-name "GtkDialog" "response")
1423 @result{}
1424 #<Signal [#86] void GtkDialog.response(gint) [RUN-LAST]>
1425
1426 (connect-signal *d* "response" (lambda (dialog response-value) (print dialog) (print response-value)))
1427
1428 (emit-signal *d* "response" 14)
1429 @result{}
1430 ;; Prints:
1431 #<GTK:DIALOG @{1002D866F1@}>
1432 14 
1433 @end example
1434
1435 Function @code{emit-signal} is used to emit signals on objects.
1436
1437 @code{(emit-signal object signal-name &rest args) @result{} return-value}
1438
1439 @table @var
1440 @item @var{object}
1441 An object on which the signal should be emitted
1442 @item @var{signal-name}
1443 A string naming the signal
1444 @item @var{args}
1445 Arguments for a signal
1446 @item @var{return-value}
1447 Return value of a signal
1448 @end table
1449
1450 Emits the signal and calls all handlers of the signal. If signal returns a value, it is returned from @code{emit-signal}.
1451
1452 Example:
1453 @example
1454 (defvar *d* (make-instance 'gtk:dialog))
1455 @result{}
1456 *D*
1457
1458 *d*
1459 @result{}
1460 #<GTK:DIALOG @{1002D866F1@}>
1461
1462 (parse-signal-name "GtkDialog" "response")
1463 @result{}
1464 #<Signal [#86] void GtkDialog.response(gint) [RUN-LAST]>
1465
1466 (connect-signal *d* "response" (lambda (dialog response-value) (print dialog) (print response-value)))
1467
1468 (emit-signal *d* "response" 14)
1469 @result{}
1470 ;; Prints:
1471 #<GTK:DIALOG @{1002D866F1@}>
1472 14 
1473 @end example
1474
1475 @node GObject foreign class
1476 @section GObject foreign class
1477
1478 To enable passing GObject instance between Lisp code and foreign code, @code{g-object} foreign type is introduced.
1479
1480 This type has the following syntax:
1481 @code{(g-object &optional type)} or @code{g-object}.
1482
1483 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.
1484
1485 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.
1486
1487 This defines the function that may be called with instances of types @code{container} and @code{widget}:
1488 @example
1489 (defcfun (container-add "gtk_container_add") :void
1490   (container (g-object container))
1491   (widget (g-object widget)))
1492
1493 (let ((window (make-instance 'gtk-window))
1494       (widget (make-instance 'button)))
1495   (container-add window widget))
1496 @end example
1497 (@code{gtk-window} is a subclass of @code{container}; @code{button} is a subclass of @code{widget})
1498
1499 This defines the function that returns an instance of GObject class:
1500 @example
1501 (defcfun (bin-child "gtk_bin_get_child") (g-object widget)
1502   (bin (g-object bin)))
1503
1504 (let ((window (make-instance 'gtk-window))
1505       (widget (make-instance 'button)))
1506   (container-add window widget)
1507   (bin-child window))
1508 @result{}
1509 #<GTK:BUTTON @{1002DE74B1@}>
1510 @end example
1511
1512 @node Subclassing GObjects and implementing GInterfaces
1513 @chapter Subclassing GObjects and implementing GInterfaces
1514
1515 @node GBoxed
1516 @chapter GBoxed
1517
1518 @node Generating type definitions by introspection
1519 @chapter Generating type definitions by introspection
1520 @menu
1521 * define-g-object-class::
1522 * define-g-interface::
1523 * define-g-enum::
1524 * define-g-flags::
1525 * get-g-flags-definition::
1526 * get-g-enum-definition::
1527 * get-g-interface-definition::
1528 * get-g-class-definition::
1529 * generate-types-hierarchy-to-file::
1530 @end menu
1531
1532 CL-GTK2-GOBJECT includes facilities for automatically generating parts of bindings for libraries that use GObject type system.
1533
1534 @node define-g-object-class
1535 @section define-g-object-class
1536
1537 @example
1538 (define-g-object-class g-type-name name
1539   (&key (superclass 'g-object) (export t) interfaces type-initializer)
1540   (&rest properties))
1541
1542 property ::= name accessor gname type readable writable
1543 property ::= :cffi name acessor type reader writer
1544 @end example
1545
1546 Parameters of @code{define-g-object-class}
1547 @table @var
1548 @item @var{superclass}
1549 A symbol naming the superclass of this class
1550 @item @var{export}
1551 Whether to export the name of the class and names of autogenerated properties names from the current package.
1552 @item @var{interfaces}
1553 A list of interfaces the this class implements
1554 @item @var{type-initializer}
1555 A string naming the type initiliazer function. It is usually named @code{class_get_type}.
1556 @item @var{properties}
1557 A list of slots of a class
1558 @end table
1559
1560 Parameters of @code{property}:
1561 @table @var
1562 @item @var{name}
1563 A symbol naming the slot
1564 @item @var{accessor}
1565 A symbol naming the accessor function for this slot
1566 @item @var{gname}
1567 A string naming the property of GObject
1568 @item @var{type}
1569 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)
1570 @item @var{readable}
1571 A boolean specifying whether the slot can be read
1572 @item @var{writable}
1573 A boolean specifying whether the slot can be assigned to
1574 @item @var{reader}
1575 A string or a symbol naming getter function. See description of @code{gobject-class} metaclass for information.
1576 @item @var{writter}
1577 A string or a symbol naming setter function. See description of @code{gobject-class} metaclass for information.
1578 @end table
1579
1580 Macro that expand to @code{defclass} for specified class. Additionally, exports accessor names and name of a class.
1581
1582 Example:
1583 @example
1584 (define-g-object-class "GtkContainer" container
1585   (:superclass widget :export t :interfaces
1586                ("AtkImplementorIface" "GtkBuildable")
1587                :type-initializer "gtk_container_get_type")
1588   ((border-width container-border-width "border-width" "guint" t t)
1589    (resize-mode container-resize-mode "resize-mode" "GtkResizeMode" t t)
1590    (child container-child "child" "GtkWidget" nil t)
1591    (:cffi focus-child container-focus-child g-object "gtk_container_get_focus_child" "gtk_container_set_focus_child")
1592    (:cffi focus-vadjustment container-focus-vadjustment (g-object adjustment) "gtk_container_get_focus_vadjustment" "gtk_container_set_focus_vadjustment")
1593    (:cffi focus-hadjustment container-focus-hadjustment (g-object adjustment) "gtk_container_get_focus_hadjustment" "gtk_container_set_focus_hadjustment")))
1594 @end example
1595
1596 @node define-g-interface
1597 @section define-g-interface
1598
1599 @node define-g-enum
1600 @section define-g-enum
1601
1602 @node define-g-flags
1603 @section define-g-flags
1604
1605 @node get-g-flags-definition
1606 @section get-g-flags-definition
1607
1608 @node get-g-enum-definition
1609 @section get-g-enum-definition
1610
1611 @node get-g-interface-definition
1612 @section get-g-interface-definition
1613
1614 @node get-g-class-definition
1615 @section get-g-class-definition
1616
1617 @node generate-types-hierarchy-to-file
1618 @section generate-types-hierarchy-to-file
1619
1620 @bye
1621