]> git.draconx.ca Git - gob-dx.git/blob - doc/gob2.1.in
adf08b01903875b632d792f5219ab59d58e76ab9
[gob-dx.git] / doc / gob2.1.in
1 .\"
2 .\" gob manual page
3 .\" (C) 1999,2000,2001 George Lebl <jirka@5z.com>
4 .\" 
5 .\" This manual page is covered by the terms of the GNU General
6 .\" Public License.  
7 .\"
8 .TH GOB2 1 "GOB2 @VERSION@" 
9 .SH NAME
10 GOB2 \- The GObject Builder
11 .SH SYNOPSIS
12 .PP
13 .B gob
14 [ option ] ...
15 file
16 .SH OUT OF DATE
17 .PP
18 This manual is out of date.  I will be updating it when I can but it
19 mostly covers the GOB 1.0.x versions and not GOB2 yet.  There ARE wrong
20 things in this manual for now.  Keep that in mind.  However most things
21 still apply.  Just not all.
22
23 .SH DESCRIPTION
24 .PP
25 GObject Builder is a simple preprocessor for easily creating
26 GObject objects.  It does not parse any C code and ignores any C errors.  It
27 is in spirit similar to things like lex or yacc.
28
29 .SH OPTIONS
30 .PP
31 .TP
32 .B -? -h --help
33 Display a simple help screen.
34 .TP
35 .B --version
36 Display version information
37 .TP
38 .B -w --exit-on-warn
39 Exit with an error code even when you encounter a warning.
40 .TP
41 .B --no-exit-on-warn
42 Exit with an error only on errors, not on warnings, this is the default.
43 .TP
44 .B --for-cpp
45 Generate C++ code.
46 .TP
47 .B --no-extern-c
48 Never add the extern "C" to the header.
49 .TP
50 .B --no-gnu
51 Never generate any code with GNU C extensions.  However all the GNU C
52 extensions are always wrapped in #ifdef __GNUC__, so code using them compiles
53 correctly even on non-GNU compilers.  This option is for purists only.
54 (using GNU extensions some warnings are eliminated, some ugly hacks and there
55 is better argument type safety, so it's good to use them)
56 .TP
57 .B --no-touch-headers
58 Don't touch the generated header file unless it really changed, this avoids
59 spurious rebuilds, but can confuse some make systems (automake in particular),
60 so it is not enabled by default.  Private header is still touched even if
61 unchanged however.
62 .TP
63 .B --always-private-header
64 Always create a \fB<basename>-private.h\fR file, even if it would be empty.
65 Otherwise, it is only created when there are private data members in the class.
66 This option implicitly negates --no-private-header
67 .TP
68 .B --no-private-header
69 Never create a private header file.  If we use any private data members,
70 define the private data structure at the point in the .c source where
71 the class definition begins.  This option implicitly negates
72 --always-private-header
73 .TP
74 .B -n --no-write
75 Do not write any output files, just check syntax of the input file.
76 .TP
77 .B --no-lines
78 Do not print out the '#line' statements into the output.  Useful for debugging
79 the auto-generated generated code.
80 .TP
81 .B --no-self-alias
82 Do not create the Self and SelfClass type aliases and the SELF, IS_SELF
83 and SELF_CLASS macros.
84 .TP
85 .B --no-kill-underscores
86 Do not remove the initial underscore from method names.
87 .TP
88 .B --always-private-struct
89 Always include the private pointer in the public header file.  This is useful for
90 files which are part of a library and you want to reserve the right to add some
91 private data members without breaking binary compatibility.
92
93 .SH TYPENAMES
94 .PP
95 Because we need to parse out different parts of the typename, sometimes you
96 need to specify the typename with some special syntax.  Types are specified in
97 capitalized form and words are separated by ':'.  The first word of the type
98 (which can be empty) is the "namespace".  This fact is for example used for the
99 type checking macro and the type macro.  For "Gtk:New:Button", the macros will
100 be GTK_IS_NEW_BUTTON and GTK_TYPE_NEW_BUTTON.  This colon separated format of
101 typenames is used in the class declaration header and for method argument
102 types.
103
104 .SH OUTPUT FILES
105 .PP
106 The filenames are created from the typename.  The words are
107 separated by '-' and all in lower case.  For example for an object named
108 "Gtk:New:Button", the files are \fBgtk-new-button.c\fR and
109 \fBgtk-new-button.h\fR.
110 If you are using C++ mode, the output .c file will in fact be a .cc file.
111 If you have any private data members, a private header file will also
112 be created, called \fB<basename>-private.h\fR (for the example above it
113 would be gtk-new-button-private.h).
114 The public header file is created to be human readable and to be used as a
115 reference to the object.  The .c source file is not created as a human
116 readable source and is littered with #line statements, which make the
117 compiler attempt to point you to the right line in your .gob file in
118 case of parsing errors.  The output should not be edited by hand, and
119 you should only edit the .gob file.
120
121 .SH INCLUDING NORMAL C CODE IN THE OUTPUT FILES
122 .PP
123 To include some code directly in the output C file begin with '%{'
124 on an empty line and end the code with a '%}' on an empty line.  These
125 sections will appear in the output files in the order they are given.
126 There are several other \fIsections\fR to which you can put code.  You can
127 put it in the 'header' section (which can be abbreviated 'h') and it will
128 go into the public header file.  You can also put it in the 'privateheader'
129 section (abbreviated 'ph') which will make the code go into the private
130 header file.  Sometimes you want some code (other includes) to appear before
131 the extern "C" and the protecting define.  To do this you can put them
132 into the 'headertop' (or 'ht') section.  You may wish to include code or
133 comments in all the files, which you can do by putting them into the 'all'
134 (or 'a') section.  Similarly, code you wish to appear at the top of all
135 files go in the 'alltop' (or 'at') section.  For example:
136 .nf
137
138   %alltop{
139   /* this will be on top of all output files */
140   %}
141
142   %headertop{
143   /* this will be on top of the public header */
144   %}
145
146   %privateheader{
147   /* this will go into the private header file */
148   %}
149
150   %h{
151   /* will be included in the header */
152   void somefunc(int i);
153   %}
154
155   %a{
156   /* will be included in all files */
157   %}
158
159   %{
160   /* will be included in the C file */
161   void somefunc(int i)
162   {
163         /* some code */
164   }
165   %}
166
167 .fi
168
169 .SH INCLUDE FILES
170 .PP
171 Gob will automatically include the class header file at the top of the .c 
172 source file.  If you wish to include it somewhere else, put the include
173 into some %{ %} section above the class definition, and gob will not include
174 it automatically.  This way you can avoid circular includes and control
175 where in the file do you want to include the header.
176 .PP
177 If you made any data members private, gob will also create a source file
178 that will be called \fB<basename>-private.h\fR.  Same rule as above applies
179 for this just as it does for the regular header file.  If you do explicitly
180 include the regular header file, you should always include this private
181 header file below it.  That is, if you use any private data members.  If you
182 don't, the private header file automatically includes the public header file,
183 and thus the public header file will be indirectly included at the very top
184 of the file.
185
186 .SH MAKING A NEW CLASS
187 .PP
188 The class header:
189 .PP
190 There can be only one class per input file.  Defining a class
191 is sort of like in Java, you define the class and write inline code
192 directly into the class definition.  To define a class you need to specify
193 the new object name and the name of the object from which it is derived
194 from, such as this "class <new type> from <parent type> { <class code> }".
195 For example:
196 .nf
197
198   class Gtk:New:Button from Gtk:Button {
199           <class code>
200   }
201
202 .fi
203 .PP
204 Data members:
205 .PP
206 There are five types of data members.  Three of them are normal data numbers,
207 one is class wide (global) in scope and one is a virtual one, usually linked to
208 a normal data member or a class wide data member.  The three normal data
209 members are public, protected and private.  Public and protected are basically
210 just entries in the object structure, while private has it's own dynamically
211 allocated private structure.  Protected members are always put after the public
212 one in the structure and are marked protected in the header file.  There is
213 only one identifier allowed per typename unlike in normal C.  Example:
214 .nf
215
216   public int i;
217   private GtkWidget *h;
218   protected long k;
219
220 .fi
221 .PP
222 Public and protected data members are accessed normally as members of
223 the object struct.  Example where 'i' is as above a public data member:
224 .nf
225
226   object->i = 1;
227
228 .fi
229 .PP
230 The private data members are defined in a structure which is only available
231 inside the .c file, or by including a private header file.  You must access
232 them using the structure _priv.  Example
233 where 'h' is the private data member (as in the above example):
234 .nf
235
236   object->_priv->h = NULL;
237
238 .fi
239 The _priv structure is defined in the \fB<basename>-private.h\fR.
240 This file is automatically included if you don't include it yourself.  You
241 should always explicitly include it in your .gob file if you explicitly also
242 include the main header file.  The reason it is a separate header file is
243 that you can also include it in other places that need to access this objects
244 private data, such as if you have the majority of functionality of an object
245 in a separate .c file.  Or if a derived object needs to access the protected
246 methods.
247 .PP
248 In case you use the \fB--no-private-header\fR option, no
249 private header file is created and you can only access the _priv pointer
250 below the class definition in the .gob file.
251 .PP
252 Also note that this structure is dynamically allocated, and is freed in the
253 finalize handler.  If you override the finalized handler, your code will be
254 run first and only then will the _priv structure be freed.
255 .PP
256 Classwide data members:
257 .PP
258 Sometimes you want a datamember to be shared by all objects.  You then need
259 the "classwide" scope keyword.  So for example the following adds a global
260 member foo:
261 .nf
262
263   classwide int foo;
264
265 .fi
266 To access the member you do the standard voodoo of getting the class from the
267 object and casting it to your class pointer.  Thus the following would work:
268 .nf
269
270   SELF_CLASS(GTK_OBJECT(object)->klass)->foo = 20;
271
272 .fi
273 .PP
274 Automatic Initialization (0.93.0 and higher only):
275 .PP
276 You can automatically initialize the public private and protected data members
277 without having to add an init method.  The advantage here is that
278 initialization is kept close to the definition of the data member and thus
279 it's easier to check.  To do this, just add a '=' followed by a number or
280 a token.  It is also possible to include arbitrary C code for more elaborate
281 initializations by putting it all in curly braces.  Note that the curly braces
282 will not be printed into the output, but since gob does not C parsing it needs
283 them to figure out where the C code ends.  The code will be inserted into the
284 init method, above the user defined body.  So for example the following
285 will initialize an integer to -1 and a string with a newly allocated string
286 of "hello".
287 .nf
288
289   public int foo = -1;
290   private char *bar = {g_strdup("hello")};
291
292 .fi
293 .PP
294 Automatic Destruction (0.93.0 and higher only):
295 .PP
296 Most data stored as pointers needs to have a function called when the object
297 is destroyed, to either free it or give up a reference.  Gob will let you
298 define a function to be called on the data the object is destroyed.  This is
299 achieved by putting 'destroywith' followed by a function name after the
300 variable definition.  It is only called if the data you defined this on
301 is not NULL, so you cans specify functions which do not handle NULL.  It
302 is very much like the GDestroyNotify function used in GTK+ and glib in many
303 places.  Unlike many other places, gob will not enforce any kind of type
304 safety here so be a little bit more careful.  Any function you give it will
305 be called as a "void function(void *)".  It will in fact be cast into such
306 a form before called.  This is to avoid spurious warnings for gtk calls to
307 subclass methods.  The function needs not be of that form exactly, it just has
308 to take one argument which is the pointer to the data.  You should also not
309 define this on any non-pointer data as the results may be undefined.
310 Example:
311 .nf
312
313   public Gtk:Widget *window = NULL
314           destroywith gtk_widget_destroy;
315   public char *foo = {g_strdup("bar")}
316           destroywith g_free;
317
318 .fi
319 Note that the function name you give must be a real function and not macro.
320 Also note that this is always called in the "destroy" method of GtkObject.
321 It is always called after any user defined body of the destroy handler.
322 .PP
323 Sometimes you may want to run arbitrary code on destruction.  While this can
324 be perfectly well done in the destroy handler.  Depending on the style you
325 may want to include all destruction/initialization code together with the
326 definition of the data member.  Thus you may want to put arbitrary code which
327 will then be inserted into the "destroy" method of GtkObject.  This can be
328 done with the "destroy" keyword followed by arbitrary code in curly braces.  
329 Inside this code a macro called VAR will be define which refers to your
330 variable.  So for example destroying a GString can be either done with
331 a helper routine or the following code:
332 .nf
333
334   public GString *string = {g_string_new(NULL)}
335           destroy {
336                   if(VAR) g_string_free(VAR, TRUE);
337           };
338
339 .fi
340 The thing to remember with these is that there are many ways to do this
341 and you'd better be consistent in your code in how you use the above things.
342 Also defining a helper routine that will do the destruction will be a nicer
343 thing to do if that's a possibility.  The "destroy" keyword with code does
344 take up more space in the file and it may become more cluttered.
345 .PP
346 The data is zeroed out after being destroyed.  This is to make debugging easier
347 in case your code might try to access an already destroyed object.  In case
348 you have overridden the destroy method, your code will be run first and
349 only then will the destructors be called.  You should not however make any
350 assumptions about the order at which the destructors are called.  If you have
351 interdependencies between destructors for different data members, you will
352 have to do this in your own destroy override function.
353 .PP
354 GTK+ Arguments:
355 .PP
356 The fourth type of a data member an argument type.  It is a named data member
357 which is one of the features of the GTK+ object system.  You need to define a
358 get and a set handler.  They are fragments of C code that will be used to get
359 the value or set the value of the argument.  Inside them you can use the define
360 ARG to which you assign the data or get the data.  You can also use the
361 identifier "self" as pointer to the object instance.  The type is defined as
362 one of the gtk type enums, but without the GTK_TYPE_ prefix.  For example:
363 .nf
364
365   public int height;
366   argument INT height set { self->height = ARG; } get { ARG = self->height; };
367
368 .fi
369 .PP
370 If you don't define a set or a get handler it will be a read-only
371 or a write-only argument.  If you want to add extra argument flags, add
372 them into parenthesis after the argument keyword, separated by '|' and
373 without the GTK_ARG_ prefix.  For example:
374 .nf
375
376   public int height;
377   argument (CONSTRUCT) INT height get { ARG = self->height; };
378
379 .fi
380 This makes the argument settable even before the object is constructed, so
381 that people can pass it to gtk_object_new function.  Useful is also
382 CONSTRUCT_ONLY flag which makes the argument only available during
383 construction of the object.
384 .PP
385 Since 0.92.1, gob creates macros which can be used for type safe access to
386 gtk arguments.  The macros are called <type>_ARG_<argument name>(x) and
387 <type>_GET_ARG_<argument name>(x).  They define both the string and the
388 value part of the argument.  So for setting an argument of height, one would
389 use (for object type My:Object):
390 .nf
391
392   gtk_object_set (GTK_OBJECT (object),
393                   MY_OBJECT_ARG_HEIGHT (7),
394                   NULL);
395
396 .fi
397 And for getting, you would use:
398 .nf
399
400   int height;
401   gtk_object_get (GTK_OBJECT (object),
402                   MY_OBJECT_GET_ARG_HEIGHT (&height),
403                   NULL);
404
405 .fi
406 Note however that the type safety only works completely on GNU C compilers.
407 The code will compile on other compilers but with minimal type safety.
408 .PP
409 To get good type safety on POINTER types however, you should specify
410 an optional C type that gob should use.  For other then POINTER types
411 this is redundant but possible.  To do this, place '(type <c type>)'
412 right after the GTK+ type.  Example:
413 .nf
414
415   argument POINTER (type char *) foo set { /* foo */ } get { /* bar */ };
416
417 .fi
418 .PP
419 Sometimes it can become tiresome to type in the set and get handlers if
420 they are trivial.  So gob since version 0.93.0 provides automatic argument
421 linking to data members.  There are three different cases it handles, direct
422 link (keyword 'link'), string linking (keyword 'stringlink') and object
423 linking (keyword 'objectlink').  You just place the keyword after the argument
424 name instead of the get/set handlers.  It will link to a data member of the
425 same name that was defined earlier in the input file.  Best is to see examples:
426 .nf
427
428   public int foo;
429   argument INT foo link;
430
431 .fi
432 is just like
433 .nf
434
435   public int foo;
436   argument INT (type int) foo
437   get { ARG = self->foo; }
438   set { self->foo = ARG; };
439
440 .fi
441 Similarly,
442 .nf
443
444   private char * foo;
445   argument POINTER foo stringlink;
446
447 .fi
448 is just like
449 .nf
450
451   private char * foo;
452   argument POINTER (type char *) foo
453   get {
454         ARG = g_strdup(self->_priv->foo);
455   } set {
456         g_free(self->_priv->foo);
457         self->_priv->foo = g_strdup(ARG);
458   }
459
460 .fi
461 And for the objectlink we would have:
462 .nf
463
464   public Gtk:Object * foo;
465   argument POINTER foo objectlink;
466
467 .fi
468 is just like
469 .nf
470
471   protected Gtk:Object * foo;
472   argument POINTER (type Gtk:Object *) foo
473   get {
474         ARG = self->foo;
475   } set {
476         if(ARG != NULL)
477                 gtk_object_ref(ARG);
478         if(self->foo != NULL)
479                 gtk_object_unref(self->foo);
480         self->foo = ARG;
481   }
482
483 .fi
484 .PP
485 As you see it will handle NULLs correctly (for the string, g_free and g_strdup
486 handle NULLs).  And it will also handle private, protected and public members.
487 For objectlink, just a pointer is returned on get, if you wish to keep it around,
488 you should call gtk_object_ref on it.  For stringlink, get makes a copy of
489 the string which you should free after use.  This is the behaviour since 1.0.2.
490 .PP
491 You can also automatically export get and set methods for each of the arguments
492 by appending '(export)' flag before the get and set statements.  For example:
493 .nf
494
495   public int foo;
496   argument INT (type int) foo (export)
497   get { ARG = self->foo; }
498   set { self->foo = ARG; };
499
500 .fi
501 Will export public methods get_foo(self) and set_foo(self, int foo) for you
502 automatically.  Note that this behaviour is new in 1.0.10.
503 .PP
504 Methods:
505 .PP
506 There is a whole array of possible methods.  The three normal,
507 "familiar" method types are private, protected and public.  Public are
508 defined as normal functions with a prototype in the header file.  
509 Protected methods are defined as normal methods (which you can call from other
510 files), but their prototype is placed in the private header file.  Private
511 methods
512 are defined as static functions with prototypes at the top of the .c
513 file.  Then there are signal, virtual and override methods.  More on those
514 later.  You can also
515 define init and class_init methods with a special definition if you want
516 to add code to the constructors or you can just leave them out.
517 You can also not define a body for a method, by just using ';' instead of a
518 body.  This will define an empty function.  You can't do this for non-void
519 regular public, private or protected methods, however it is acceptable for
520 non-void virtual, signal and override methods.
521 .PP
522 Function argument lists:
523 .PP
524 For all but the init and class_init methods, you use the
525 following syntax for arguments.  The first argument can be just "self",
526 which gob will translate into a pointer to the object instance.  The rest
527 of the arguments are very similar to normal C arguments.  If the
528 typename is an object pointer you should use the syntax defined above
529 with the words separated by ':'
530 .nf
531 <type> <argument id>
532 or
533 <type> <argument id> (check <list of checks>)
534 .fi
535 .PP
536 The checks are glib type preconditions, and can be the following:
537 "null", which tests pointers for being NULL, "type" which checks GTK+
538 object pointers for being the right type, "<test> <number>" which tests
539 numeric arguments for being a certain value.  The test can be a <,>,<=,>=
540 != or ==.  Example:
541 .nf
542   
543   public int foo(self, int h (check > 0 < 11), Gtk:Widget *w (check null type))
544
545 .fi
546 .PP
547 This will be the prototype of a function which has a self pointer
548 as the first argument, an integer argument which will be checked and has
549 to be more then 0 and less then 11, and a pointer to a GtkWidget object
550 instance and it is checked for being null and the type will also be
551 checked.
552 .PP
553 Error return:
554 .PP
555 Methods which have a return value, there also has to be something
556 returned if there is an error, such as if a precondition is not met.  The
557 default is 0, casted to the type of the method.  If you need to return
558 something else then you can specify an "onerror" keyword after the
559 prototype and after that a number, a token (an identifier) or a bit of C
560 code enclosed in braces {}.  The braces will not be printed into the
561 output, they just delimit the string.  For example:
562 .nf
563
564   public void * get_something(self, int i (check >= 0)) onerror NULL {
565           ...
566   }
567
568 .fi
569 The onerror value is also used in overrides that have a return value, in
570 case there isn't a parent method, PARENT_HANDLER will return it.  More about
571 this later.
572 .PP
573 Default return:
574 .PP
575 Some signal and virtual methods have a return type.  But what happens if
576 there is no default handler and no one connects to a signal.  GOB will
577 normally have the wrappers return whatever you specify with onerror or '0'
578 if you haven't specified anything.  But since 0.93.2 you can specify a default
579 return value with the keyword 'defreturn'.  It's use is identical to the
580 use of onerror, and you can in fact use both at the same time.  Example
581 .nf
582
583   virtual int get_some_int(self) onerror -1 defreturn 10 ;
584
585 .fi
586 That is an empty virtual method (in C++ terms a pure virtual).  If you never
587 specify any handler for it in the derived children it will just return 10.
588 .PP
589 Constructor methods:
590 .PP
591 There are two methods that handle the construction of an object, init and
592 class_init.  You define them by just using the init or class_init keyword 
593 with an untyped argument in the argument list.  The argument will be
594 usable in your function as a pointer to your object or class depending if
595 it's init or class_init.
596 For example:
597 .nf
598
599   init(self) {
600           /* initialize the object here */
601           self->a = 9;
602           self->b = 9;
603   }
604
605   class_init(class) {
606           /* initialize the class, this is rarely needed */
607           class->blah = NULL;
608   }
609
610 .fi
611 The class_init function is very rarely needed as all standard class
612 initialization is taken care of for you by gob itself.  The init function
613 should on the other hand be used whenever you need to construct or initialize
614 anything in the object to put it into a sane state.  Sometimes you need
615 some arguments, for this you should either use a construct method and a
616 new function like many GTK+ widgets, and/or a CONSTRUCT or CONSTRUCT_ONLY
617 type of an argument.
618 .PP
619 Virtual methods:
620 .PP
621 Virtual methods are basically pointers in the class structure,
622 so that one can override the method in derived methods.  They can be empty
623 (if you put ';' instead of the C code).  A wrapper will also be defined
624 which makes calling the methods he same as public methods.  This type of
625 method is just a little bit "slower" then normal functions, but not as
626 slow as signals.  You define them by using "virtual" keyword before the
627 prototype.  If you put the keyword "private" right after the "virtual"
628 keyword, the wrapper will not be a public method, but a private one.
629 You can do the same with "protected" to make a protected wrapper.
630 .PP
631 Signals:
632 .PP
633 Signals are methods to which the user can bind other handlers
634 and override the default handler.  The default handler is basically the
635 method body.  This is the most versatile and flexible type of a method
636 and also the slowest.  You need to specify a whole bunch of things when
637 you define a signal.  One thing is when the default handler will be run,
638 first or last.  You specify that by "first" or "last" right after the
639 "signal" keyword.  Then you need to define the gtk enum types (again
640 without the GTK_TYPE_ prefix).  For that you define the return types
641 and the types of arguments after the "self" pointer (not including the
642 "self" pointer).  You put it in the following syntax "<return type> (<list
643 of arguments>)".  If the return type is void, the type should be "NONE",
644 the same should be for the argument list.  The rest of the prototype is
645 the same as for other method types.  The body can also be empty, and
646 also there is a public method wrapper which you can use for calling the
647 signal just like a public method.  Example:
648 .nf
649
650   signal first INT(POINTER,INT)
651   int do_something(self, Gtk:Widget *w (check null type), int length)
652   {
653           ...
654   }
655   
656 .fi
657 or
658 .nf
659
660   signal last NONE(NONE) void foo(self);
661
662 .fi
663 .PP
664 If you don't want the wrapper that emits the signal to be public, you can
665 include the keyword "private" after the "signal" keyword.  This will make
666 the wrapper a normal private method.  You can also make a protected wrapper
667 by using "protected" instead of "private".
668 .PP
669 If you don't define a "first" or a "last", the default will be taken as
670 "last".
671 .PP
672 You can also add additional flags.  You do this just like with the argument
673 flags, although this is probably very rare.  These are the GTK_RUN_* flags,
674 and you can add them without the GTK_RUN_ prefix into a parenthesis, just
675 after the "signal" keyword.  By default all public signals are GTK_RUN_ACTION.
676 .PP
677 Since 1.0.6, gob creates wrapper signal macros for signal connection
678 typesafety, at least on gnu compilers.  These macros are named
679 <type>_SIGNAL_<signal name>(func), where func is the function pointer.  This
680 pointer must be of the correct type, or you will get an initialization from
681 wrong pointer type warning.  This macro, much like the argument macros, wraps
682 both the name and the function pointer parameters.  For example to connect a
683 signal "changed" to a function "foo", you would do:
684 .nf
685
686   gtk_signal_connect (GTK_OBJECT (object),
687                       MY_OBJECT_SIGNAL_CHANGED (foo),
688                       NULL);
689
690 .fi
691 .PP
692 Override methods:
693 .PP
694 If you need to override some method (a signal or a virtual method
695 of some class in the parent tree of the new object), you can define and
696 override method.  After the "override" keyword, you should put the
697 typename of the class you are overriding a method from.  Other then that
698 it is the same as for other methods.  The "self" pointer in this case
699 should be the type of the method you are overriding so that you don't
700 get warnings during compilation.  Also to call the method of the parent
701 class, you can use the PARENT_HANDLER macro with your arguments.  Example:
702 .nf
703
704   override (Gtk:Container) void
705   add (Gtk:Container *self (check null type), Gtk:Widget *wid (check null type))
706   {
707           /* some code here */
708           PARENT_HANDLER(self, wid);
709   }
710
711 .fi
712 If the function has a return value, then PARENT_HANDLER is an expression that
713 you can use.  It will return whatever the parent handler returned, or the
714 "onerror" expression if there was no parent handler.
715 .PP
716 Method names:
717 .PP
718 Inside the code, aliases are set for the methods, so that you don't
719 have to type the class name before each call, just type \fBself_\fR instead
720 of the name of the class.  So to call a method called \fBblah\fR, you
721 would use the name \fBself_blah\fR.
722 Example:
723 .nf
724
725   private int
726   foo (self)
727   {
728           return self->len;
729   }
730   
731   private int
732   bar (self,int i)
733   {
734           return self_foo (self) + i;
735   }
736
737 .fi
738 .PP
739 Making new objects:
740 .PP
741 You should define a new method which should be a normal public method.  Inside
742 this method, you can use the GET_NEW macro that is defined for you and that
743 will fetch a new object, so a fairly standard new method would look like:
744 .nf
745
746   public GtkObject *
747   new(void) {
748           GtkObject *ret = GET_NEW;
749           return GTK_OBJECT (ret);
750   }
751
752 .fi
753 .PP
754 You should not a subtle peculiarity of the GTK+ object system here.  If there is any
755 code inside the GTK_OBJECT macro argument, it will get executed multiple times.  This
756 means that things such as GTK_OBJECT(GET_NEW) would actually create 4 objects, leaking
757 3 of them.  A good rule is to be careful with all macros.
758 .PP
759 Self alias casts:
760 .PP
761 There are some standard casts defined for you.  Instead of using the full
762 macros inside the .c file, you can use SELF, IS_SELF and SELF_CLASS.  Using
763 these makes it easier to for example change class names around.
764 .PP
765 Self alias types:
766 .PP
767 Since 0.93.5, there have also been defined the Self and SelfClass types inside
768 your .c file.  These serve the same function as the above, they make it easier
769 to type and easier to change typenames around which can help a lot during
770 prototyping stage.  However you should note that the Self type should not be
771 used in function prototypes as one of the arguments or as a return value type.
772 This is because this is a simple C typedef which is only available inside you
773 .c file.  You can disable both the self casting macros and the self type
774 aliases by passing --no-self-alias to
775
776 .SH DEALING WITH DIFFERENT GOB VERSIONS
777 .PP
778 Defines:
779 .PP
780 In your generated C file, you can use the defines GOB_VERSION_MAJOR
781 GOB_VERSION_MINOR and GOB_VERSION_PATCHLEVEL if you wish to for example
782 use a feature that is only available in some newer gob version.  Note however
783 that you can only use these defines in the C code portions of your .gob file,
784 and #ifdef's cannot span multiple functions.  Check the BUGS section
785 for more on using the C preprocessor and gob.  Also note that these
786 have only been available since the 0.92.1 version of gob.
787 .PP
788 Minimum version requires:
789 .PP
790 You can also make your .gob file require at least certain version of gob.  You
791 do this by putting 'requires x.y.z' (where x.y.z is the version number) outside
792 of any C block, comment or class, usually you should make this the first line
793 in the file or close to the top.  If gob finds this and the version of gob used
794 to compile the code is lower then that listed in the require, gob will generate
795 an error and exit.  For example to require that gob version 0.92.1 or higher
796 be used to compile a file, put this at the top of that file:
797 .nf
798
799   requires 0.92.1
800
801 .fi
802 It should be noted however that this feature was not added until 0.92.1, and
803 so if the file gets compiled by a lower version, gob would generate a 
804 syntax error.  Thus by putting in a requires line, you are implicitly
805 requiring at least 0.92.1.
806
807 .SH C++ MODE
808 .PP
809 There is a C++ mode so that gob creates C++ compiler friendly files.  You need
810 to use the --for-cpp argument to gob.  This will make the generated file have
811 a .cc instead of a .c extension, and several things will be adjusted to
812 make it all work for a C++ compiler.  One thing that will be missing is an
813 alias to the new method, as that clashes with C++, so instead you'll have to
814 use the full name of the method inside your code.  Also note that gob does
815 not use any C++ features, this option will just make the generated code
816 compile with a C++ compiler.
817
818 .SH OVERRIDING THE GET_TYPE METHOD
819 .PP
820 The get_type is not really a method, but a function which initializes your
821 object.  Recently objects appeared which require you to make a custom
822 get_type function (BonoboXObject currently, see next section for direct
823 BonoboXObject support).  So in 1.0.7 it is now possible
824 to override this function.  To do so, just define a new public method called
825 get_type, with no arguments.  Example:
826 .nf
827
828   public GtkType
829   get_type (void)
830   {
831         /* code goes here */
832         return some_type;
833   }
834
835 .fi
836
837 .SH DIRECT BonoboXObject SUPPORT
838 .PP
839 If you want to build a BonoboXObject class gob has direct support for these
840 classes since 1.0.9.  Just create a new object that derives from
841 Bonobo:X:Object.  Then use a "BonoboX" class flag with the interface name as an
842 argument.  The interface name should be as you would type it in C, that is with
843 underscores as namespace separators.  Then you add the methods (using exact
844 same names as in the idl file) and prepend those methods with a BonoboX
845 keyword.  For example imagine you have an interface GNOME/Foo/SomeInterface,
846 with a method fooBar that takes a single string:
847 .nf
848
849   class Foo:Some:Interface from Bonobo:X:Object
850     (BonoboX GNOME_Foo_SomeInterface) {
851
852           BonoboX
853           private void
854           fooBar (PortableServer_Servant servant,
855                   const CORBA_char *string,
856                   CORBA_Environment *ev)
857           {
858                   Self *self = SELF (bonobo_object_from_servant (servant));
859
860                   /* your code here */
861           }
862
863           /* rest of class */
864   }
865
866 .fi
867 Note that the implementation method can be private, in fact that's probably
868 a good idea to do.  It won't work to make this a signal, it can however
869 be a virtual.  Note that the method prototype must match the one from the
870 interface header file, or you will get a bad assignment warning.  You should
871 check the header file generated by orbit-idl and see the epv structure
872 for the correct prototypes if you can't figure them out from the idl itself.
873 Also note that the first argument is not "self", but the servant and you must
874 use bonobo_object_from_servant function to get the actual object pointer.
875
876 .SH IDENTIFIER CONFLICTS
877 .PP
878 Gob will need to define some local variables and functions in the generated
879 files, so you need to take some precaution not to conflict with these.  The
880 general rule of thumb is that all of these start with three underscores.  There
881 is one, "parent_class" which doesn't because it's intended for use in your
882 code.  For virtuals or signals, you cannot use the identifier __parent__
883 which is used for the parent of the object.  You should actually never access
884 __parent__ either as it not guaranteed that it will stay named this way.
885 Data members cannot be named __parent__ nor _priv.  For methods, you cannot
886 use the identifiers "init" or "class_init" unless you mean the constructor
887 methods.  You shouldn't generally use 3 underscores even in override method
888 argument lists and virtual and signal method names as it might confuse the
889 PARENT_HANDLER macro.  In fact avoiding all names with three underscores is
890 the best policy when working with gob.
891 .PP
892 Also note that starting with version 0.93.5, method names that start with a
893 an underscore are equivalent to the names without the initial underscore.  This
894 is done to avoid conflicts with the aliases.  Thus you can define the method
895 as "_name", if "name" happens to be some standard library function.  This is
896 the same as defining it as "name" except that the local alias will be "_name"
897 rather then "name".
898 .PP
899 There are a couple of defines which you shouldn't be redefining in the code
900 or other headers.  These are SELF, IS_SELF, SELF_CLASS, SELF_TYPE, ARG, VAR,
901 PARENT_HANDLER, GET_NEW, GOB_VERSION_MAJOR, GOB_VERSION_MINOR and
902 GOB_VERSION_PATCHLEVEL.
903 .PP
904 As for types, there are Self and SelfClass types which are only defined in your
905 source files.  Their generation (just like the generation of the SELF macros)
906 can be turned off, see command line options.
907
908 .SH USING GTK-DOC STYLE INLINE DOCUMENTATION
909 .PP
910 If you want to use gtk-doc style inline documentation for your objects, you
911 can do one of two things.  First, you could include the inline documentation
912 comments in your %{ %} section which will then be put verbatim into the
913 output source file.  This is the way you should use for functions you define
914 outside of the class.
915 .PP
916 For class methods, you should use a gtk+ style comment, however it can be
917 indented any number of tabs or spaces and you can use the short method name
918 without the type prefix.  Gob will automatically try to extract these and
919 translate to full names and put them in the output source file.  An example
920 would be:
921 .nf
922
923   class Gtk:Button:Example from Gtk:Button {
924           /**
925            * new:
926            *
927            * Makes a new #GtkButtonExample widget
928            *
929            * Returns: a new widget
930            **/
931           public
932           GtkWidget *
933           new(void)
934           {
935                   return GTK_WIDGET(GET_NEW);
936           }
937   } 
938
939 .fi
940 If the function you are documenting is a signal or a virtual then it will
941 be documenting the wrapper that starts that virtual function or emits
942 that signal.
943
944 .SH DEALING WITH CIRCULAR HEADERS
945 .PP
946 Sometimes you may need to use an object of type MyObjectA in the MyObjectB
947 class and vice versa.  Obviously you can't include headers for both.  So you
948 need to just declare the typedef in the header of A for B, and the other way
949 around as well.  The headers generated since v0.92.2 include a protecting
950 define before it declares the typedef.  This define is the
951 __TYPEDEF_<upper case object name>__.  So inside my-object-a.h there will be
952 this:
953 .nf
954
955   #ifndef __TYPEDEF_MY_OBJECT_A__
956   #define __TYPEDEF_MY_OBJECT_A__
957   typedef struct _MyObjectA MyObjectA;
958   #endif
959
960 .fi
961 Now instead of including my-object-a.h in the header section of
962 my-object-b.gob, just copy the above code there and you're set for using
963 MyObjectA as a type in the method parameters and public types.
964 .PP
965 Another way to get out of this problem is if you can use those types only
966 in the private members, in which case they won't be in the generated public
967 header.
968
969 .SH BUILDING WITH MAKE
970 .PP
971 If you are using normal makefiles, what you need to do is to add a generic
972 rule for .gob files.  So you would include the following in the Makefile
973 and then just use the .c and .h files as usual (make sure the space
974 before the 'gob' is a tab, not spaces):
975 .nf
976
977   %.c %.h %-private.h: %.gob
978           gob $<
979
980 .fi
981
982 .SH BUILDING WITH AUTOCONF and AUTOMAKE
983 .PP
984 This is a little bit more involved.  Basically the first thing to do is to
985 check for GOB in your configure.in file.  You can use the supplied m4 macro
986 which will also check the version of gob.  Basically you include this:
987 .nf
988
989   GOB_CHECK(0.93.4)
990
991 .fi
992 This will replace @GOB@ in your makefiles with the full path of gob.  Thus
993 when adding the generic rule to your Makefile.am file, it should look like:
994 .nf
995
996   %.c %.h %-private.h: %.gob
997           @GOB@ $<
998
999 .fi
1000 .PP
1001 For Makefile.am you have to set up a couple more things.  First you have to
1002 include the generated .c and .h files into BUILT_SOURCES variable.  You
1003 have to include both the .gob and the .c and .h files in the SOURCES for your
1004 program.
1005
1006 .SH DEBUGGING
1007 .PP
1008 GOB does several things to make debugging the code easier.  First it adds
1009 preprocessor commands into the output c file that point to the correct places
1010 in your .gob input file.  However sometimes there might be some bigger
1011 confusion and this is just not helpful.  In this case you will probably want
1012 to have gcc point you directly at the generated files.  For this use
1013 the --no-lines command line option.  You should also note that these commands
1014 are not generated for the public header file at all.  If there is an error which
1015 points you to the public header file, make sure you fix this error in the .gob
1016 file, otherwise your changes will not have any effect after gob recompiles the
1017 sources again.
1018 .PP
1019 Sometimes you might want to know which method you are in for some debugging
1020 output.  GOB will define __GOB_FUNCTION__ macro, which is just a string constant
1021 with a pretty name of the method.
1022
1023 .SH BUGS
1024 .PP
1025 The lexer does not actually parse the C code, so I'm sure that some corner
1026 cases or maybe even some not so corner cases of C syntax might confuse gob
1027 completely.  If you find any, send me the source that makes it go gaga and I'll
1028 try to make the lexer try to handle it properly, but no promises.
1029 .PP
1030 Another thing is that gob ignores preprocessor macros.  Since gob counts
1031 braces, the following code won't work:
1032 .nf
1033
1034   #ifdef SOME_DEFINE
1035   if(foo) {
1036   #else
1037   if(bar) {
1038   #endif
1039           blah();
1040   }
1041
1042 .fi
1043 To make this work, you'd have to do this:
1044 .nf
1045
1046   #ifdef SOME_DEFINE
1047   if(foo)
1048   #else
1049   if(bar)
1050   #endif
1051   {
1052           blah();
1053   }
1054
1055 .fi
1056 There is no real good way we can handle this without parsing C code, so we
1057 probably never will.  In the future, I might add #if 0 as a comment but
1058 that's about as far as I can really take it and even that is problematic.
1059 Basically, if you use gob, just don't use the C preprocessor too extensively.
1060 .PP
1061 Comments will not get through to the generated files unless inside C code.
1062 This is not the case for gtk-doc style comments which are supported.
1063 .PP
1064 The short name aliases are actually implemented as pointers to functions.  Thus
1065 if you want to get the pointer of a function using the short name alias you
1066 can't use the '&'.  Thus:
1067 .nf
1068
1069   void (*foo)(Self *);
1070
1071   /* this will NOT work */
1072   foo = &short_name;
1073
1074   /* this will work */
1075   foo = short_name;
1076
1077   /* Both of these will work */
1078   foo = &my_class_long_name;
1079   foo = my_class_long_name;
1080
1081 .fi
1082
1083 .SH AUTHOR
1084 .PP
1085 George Lebl <jirka@5z.com>