]> git.draconx.ca Git - gob-dx.git/blob - doc/gob.1.in
Release 0.92.4
[gob-dx.git] / doc / gob.1.in
1 .\"
2 .\" gob manual page
3 .\" (C) 1999 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 GOB 1 "GOB @VERSION@" 
9 .SH NAME
10 GOB \- The GTK+ Object Builder
11 .SH SYNOPSIS
12 .PP
13 .B gob
14 [ option ] ...
15 file
16 .SH DESCRIPTION
17 .PP
18 GTK+ Object Builder is a simple preprocessor for easily creating
19 GTK+ objects.  It does not parse any C code and ignores any C errors.  It
20 is in spirit similar to things like lex or yacc.
21
22 .SH OPTIONS
23 .PP
24 .TP
25 .B -?
26 .TP
27 .B -h
28 .TP
29 .B --help
30 Display a simple help screen.
31 .TP
32 .B --version
33 Display version information (note, --version was not added until 0.92.0)
34 .TP
35 .B -w
36 .TP
37 .B --exit-on-warn
38 Exit with an error code even when you encounter a warning.
39 .TP
40 .B --no-exit-on-warn
41 Exit with an error only on errors, not on warnings, this is the default.
42 .TP
43 .B --for-cpp
44 Generate C++ code.
45 .TP
46 .B --no-extern-c
47 Never add the extern "C" to the header.
48 .TP
49 .B --no-gnu
50 Never generate any code with GNU C extensions.  However all the GNU C
51 extensions are always wrapped in #ifdef __GNUC__, so code using them compiles
52 correctly even on non-GNU compilers.  This option is for purists only.
53 (using GNU extensions some warnings are eliminated, some ugly hacks and there
54 is better argument type safety, so it's good to use them)
55 .TP
56 .B --no-touch-headers
57 Don't touch the generated header file unless it really changed, this avoids
58 spurious rebuilds, but can confuse some make systems (automake in particular),
59 so it is not enabled by default.  Private header is still touched even if
60 unchanged however.
61 .TP
62 .B --always-private-header
63 Always create a \fB<basename>-private.h\fR file, even if it would be empty.
64 Otherwise, it is only created when there are private data members in the class.
65 This option implicitly negates --no-private-header
66 .TP
67 .B --no-private-header
68 Never create a private header file.  If we use any private data members,
69 define the private data structure at the point in the .c source where
70 the class definition begins.  This option implicitly negates
71 --always-private-header
72 .TP
73 .B -n
74 .TP
75 .B --no-write
76 Do not write any output files, just check syntax of the input file.
77
78 .SH TYPENAMES
79 .PP
80 Because we need to parse out different parts of the typename, sometimes you
81 need to specify the typename with some special syntax.  Types are specified in
82 capitalized form and words are separated by ':'.  The first word of the type
83 (which can be empty) is the "namespace".  This fact is for example used for the
84 type checking macro and the type macro.  For "Gtk:New:Button", the macros will
85 be GTK_IS_NEW_BUTTON and GTK_TYPE_NEW_BUTTON.  This colon separated format of
86 typenames is used in the class declaration header and for method argument
87 types.
88
89 .SH OUTPUT FILES
90 .PP
91 The filenames are created from the typename.  The words are
92 separated by '-' and all in lower case.  For example for an object named
93 "Gtk:New:Button", the files are \fBgtk-new-button.c\fR and
94 \fBgtk-new-button.h\fR.
95 If you are using C++ mode, the output .c file will in fact be a .cc file.
96 If you have any private data members, a private header file will also
97 be created, called \fB<basename>-private.h\fR (for the example above it
98 would be gtk-new-button-private.h).
99 The public header file is created to be human readable and to be used as a
100 reference to the object.  The .c source file is not created as a human
101 readable source and is littered with #line statements, which make the
102 compiler attempt to point you to the right line in your .gob file in
103 case of parsing errors.  The output should not be edited by hand, and
104 you should only edit the .gob file.
105
106 .SH INCLUDING NORMAL C CODE IN THE OUTPUT FILES
107 .PP
108 To include some code directly in the output C file begin with '%{'
109 on an empty line and end the code with a '%}' on an empty line.  These
110 sections will appear in the output files in the order they are given.
111 There are several other \fIsections\fR to which you can put code.  You can
112 put it in the 'header' section (which can be abbreviated 'h') and it will
113 go into the public header file.  You can also put it in the 'privateheader'
114 section (abbreviated 'ph') which will make the code go into the private
115 header file.  Sometimes you want some code (other includes) to appear before
116 the extern "C" and the protecting define.  To do this you can put them
117 into the 'headertop' (or 'ht') section.  You may wish to include code or
118 comments in all the files, which you can do by putting them into the 'all'
119 (or 'a') section.  Similarly, code you wish to appear at the top of all
120 files go in the 'alltop' (or 'at') section.  For example:
121 .nf
122
123   %alltop{
124   /* this will be on top of all output files */
125   %}
126
127   %headertop{
128   /* this will be on top of the public header */
129   %}
130
131   %privateheader{
132   /* this will go into the private header file */
133   %}
134
135   %h{
136   /* will be included in the header */
137   void somefunc(int i);
138   %}
139
140   %a{
141   /* will be included in all files */
142   %}
143
144   %{
145   /* will be included in the C file */
146   void somefunc(int i)
147   {
148         /* some code */
149   }
150   %}
151
152 .fi
153
154 .SH INCLUDE FILES
155 .PP
156 Gob will automatically include the class header file at the top of the .c 
157 source file.  If you wish to include it somewhere else, put the include
158 into some %{ %} section above the class definition, and gob will not include
159 it automatically.  This way you can avoid circular includes and control
160 where in the file do you want to include the header.
161 .PP
162 If you made any data members private, gob will also create a source file
163 that will be called \fB<basename>-private.h\fR.  Same rule as above applies
164 for this just as it does for the regular header file.  If you do explicitly
165 include the regular header file, you should always include this private
166 header file below it.  That is, if you use any private data members.  If you
167 don't, the private header file automatically includes the public header file,
168 and thus the public header file will be indirectly included at the very top
169 of the file.
170
171 .SH MAKING A NEW CLASS
172 .PP
173 The class header:
174 .PP
175 There can be only one class per input file.  Defining a class
176 is sort of like in Java, you define the class and write inline code
177 directly into the class definition.  To define a class you need to specify
178 the new object name and the name of the object from which it is derived
179 from, such as this "class <new type> from <parent type> { <class code> }".
180 For example:
181 .nf
182
183   class Gtk:New:Button from Gtk:Button {
184           <class code>
185   }
186
187 .fi
188 .PP
189 Data members:
190 .PP
191 There are four types of data members.  Three of them are normal
192 data numbers, and one is a virtual one, usually linked to a normal
193 data member.  The three normal data members are public, protected and
194 private.  Public and protected are basically just entries in the object
195 structure, while private has it's own dynamically allocated private
196 structure.  Protected members are always put after the public one in the
197 structure and are marked protected in the header file.  There is only one
198 identifier allowed per typename unlike in normal C.  Example:
199 .nf
200
201   public int i;
202   private GtkWidget *h;
203   protected long k;
204
205 .fi
206 .PP
207 Public and protected data members are accessed normally as members of
208 the object struct.  Example where 'i' is as above a public data member:
209 .nf
210
211   object->i = 1;
212
213 .fi
214 .PP
215 The private data members are defined in a structure which is only available
216 inside the .c file, or by including a private header file.  You must access
217 them using the structure _priv.  Example
218 where 'h' is the private data member (as in the above example):
219 .nf
220
221   object->_priv->h = NULL;
222
223 .fi
224 The _priv structure is defined in the \fB<basename>-private.h\fR.
225 This file is automatically included if you don't include it yourself.  You
226 should always explicitly include it if you explicitly also include the main
227 header file.
228 .PP
229 In case you use the \fB--no-private-header\fR option, no
230 private header file is created and you can only access the _priv pointer
231 below the class definition in the .gob file.
232 .PP
233 The fourth type is an argument type.  It is a named data member which
234 is one of the features of the GTK+ object system.  You need to define a get
235 and a set handler.  They are fragments of C code that will be used to 
236 get the value or set the value of the argument.  Inside them you can use the
237 define ARG to which you assign the data or get the data.  You can also use
238 the identifier "self" as pointer to the object instance.  The type is
239 defined as one of the gtk type enums, but without the GTK_TYPE_ prefix.
240 For example:
241 .nf
242
243   public int height;
244   argument INT height set { self->height = ARG; } get { ARG = self->height; };
245
246 .fi
247 .PP
248 If you don't define a set or a get handler it will be a read-only
249 or a write-only argument.  If you want to add extra argument flags, add
250 them into parenthesis after the argument keyword, separated by '|' and
251 without the GTK_ARG_ prefix.  For example:
252 .nf
253
254   public int height;
255   argument (CONSTRUCT) INT height get { ARG = self->height; };
256
257 .fi
258 This makes the argument settable even before the object is constructed, so
259 that people can pass it to gtk_object_new function.  Useful is also
260 CONSTRUCT_ONLY flag which makes the argument only available during
261 construction of the object.
262 .PP
263 Since 0.92.1, gob creates macros which can be used for type safe access to
264 gtk arguments.  The macros are called <type>_ARG_<argument name>(x) and
265 <type>_GET_ARG_<argument name>(x).  They define both the string and the
266 value part of the argument.  So for setting an argument of height, one would
267 use (for object type My:Object):
268 .nf
269
270   gtk_object_set(GTK_OBJECT(object),
271                  MY_OBJECT_ARG_HEIGHT(7),
272                  NULL);
273
274 .fi
275 And for getting, you would use:
276 .nf
277
278   int height;
279   gtk_object_set(GTK_OBJECT(object),
280                  MY_OBJECT_GET_ARG_HEIGHT(&height),
281                  NULL);
282
283 .fi
284 Note however that the type safety only works completely on GNU C compilers.
285 The code will compile on other compilers but with minimal type safety.
286 .PP
287 To get good type safety on POINTER types however, you should specify
288 an optional C type that gob should use.  For other then POINTER types
289 this is redundant but possible.  To do this, place '(type <c type>)'
290 right after the GTK+ type.  Example:
291 .nf
292
293   argument POINTER (type char *) foo set { /* foo */ } get { /* bar */ };
294
295 .fi
296 .PP
297 Methods:
298 .PP
299 There is a whole array of possible methods.  The three normal,
300 "familiar" method types are private, protected and public.  Public are
301 defined as normal functions with a prototype in the header file.  
302 Protected methods are defined as normal methods (which you can call from other
303 files), but their prototype is placed in the private header file.  Private
304 methods
305 are defined as static functions with prototypes at the top of the .c
306 file.  Then there are signal, virtual and override methods.  More on those
307 later.  You can also
308 define init and class_init methods with a special definition if you want
309 to add code to the constructors or you can just leave them out.
310 You can also not define a body for a method, by just using ';' instead of a
311 body.  This will define an empty function.  You can't do this for non-void
312 regular public, private or protected methods, however it is acceptable for
313 non-void virtual, signal and override methods.
314 .PP
315 Argument lists:
316 .PP
317 For all but the init and class_init methods, you use the
318 following syntax for arguments.  The first argument can be just "self",
319 which gob will translate into a pointer to the object instance.  The rest
320 of the arguments are very similar to normal C arguments.  If the
321 typename is an object pointer you should use the syntax defined above
322 with the words separated by ':'
323 .nf
324 <type> <argument id>
325 or
326 <type> <argument id> (check <list of checks>)
327 .fi
328 .PP
329 The checks are glib type preconditions, and can be the following:
330 "null", which tests pointers for being NULL, "type" which checks GTK+
331 object pointers for being the right type, "<test> <number>" which tests
332 numeric arguments for being a certain value.  The test can be a <,>,<=,>=
333 != or ==.  Example:
334 .nf
335   
336   public int foo(self, int h (check > 0 < 11), Gtk:Widget *w (check null type))
337
338 .fi
339 .PP
340 This will be the prototype of a function which has a self pointer
341 as the first argument, an integer argument which will be checked and has
342 to be more then 0 and less then 11, and a pointer to a GtkWidget object
343 instance and it is checked for being null and the type will also be
344 checked.
345 .PP
346 Error return:
347 .PP
348 Methods which have a return value, there also has to be something
349 returned if there is an error, such as if a precondition is not met.  The
350 default is 0, casted to the type of the method.  If you need to return
351 something else then you can specify an "onerror" keyword after the
352 prototype and after that a number, a token (an identifier) or a bit of C
353 code enclosed in braces {}.  The braces will not be printed into the
354 output, they just delimit the string.  For example:
355 .nf
356
357   public void * get_something(self, int i (check >= 0)) onerror NULL {
358           ...
359   }
360
361 .fi
362 The onerror value is also used in overrides that have a return value, in
363 case there isn't a parent method, PARENT_HANDLER will return it.  More about
364 this later.
365 .PP
366 Constructor methods:
367 .PP
368 There are two methods that handle the construction of an object, init and
369 class_init.  You define them by just using the init or class_init keyword 
370 with an untyped argument in the argument list.  The argument will be
371 usable in your function as a pointer to your object or class depending if
372 it's init or class_init.
373 For example:
374 .nf
375
376   init(self) {
377           /* initialize the object here */
378           self->a = 9;
379           self->b = 9;
380   }
381
382   class_init(class) {
383           /* initialize the class, this is rarely needed */
384           class->blah = NULL;
385   }
386
387 .fi
388 The class_init function is very rarely needed as all standard class
389 initialization is taken care of for you by gob itself.  The init function
390 should on the other hand be used whenever you need to construct or initialize
391 anything in the object to put it into a sane state.  Sometimes you need
392 some arguments, for this you should either use a construct method and a
393 new function like many GTK+ widgets, and/or a CONSTRUCT or CONSTRUCT_ONLY
394 type of an argument.
395 .PP
396 Virtual methods:
397 .PP
398 Virtual methods are basically pointers in the class structure,
399 so that one can override the method in derived methods.  They can be empty
400 (if you put ';' instead of the C code).  A wrapper will also be defined
401 which makes calling the methods he same as public methods.  This type of
402 method is just a little bit "slower" then normal functions, but not as
403 slow as signals.  You define them by using "virtual" keyword before the
404 prototype.  If you put the keyword "private" right after the "virtual"
405 keyword, the wrapper will not be a public method, but a private one.
406 You can do the same with "protected" to make a protected wrapper.
407 .PP
408 Signals:
409 .PP
410 Signals are methods to which the user can bind other handlers
411 and override the default handler.  The default handler is basically the
412 method body.  This is the most versatile and flexible type of a method
413 and also the slowest.  You need to specify a whole bunch of things when
414 you define a signal.  One thing is when the default handler will be run,
415 first or last.  You specify that by "first" or "last" right after the
416 "signal" keyword.  Then you need to define the gtk enum types (again
417 without the GTK_TYPE_ prefix).  For that you define the return types
418 and the types of arguments after the "self" pointer (not including the
419 "self" pointer).  You put it in the following syntax "<return type> (<list
420 of arguments>)".  If the return type is void, the type should be "NONE",
421 the same should be for the argument list.  The rest of the prototype is
422 the same as for other method types.  The body can also be empty, and
423 also there is a public method wrapper which you can use for calling the
424 signal just like a public method.  Example:
425 .nf
426
427   signal first INT(POINTER,INT)
428   int do_something(self, Gtk:Widget *w (check null type), int length)
429   {
430           ...
431   }
432   
433 or
434
435   signal last NONE(NONE) void foo(self);
436
437 .fi
438 .PP
439 If you don't want the wrapper that emits the signal to be public, you can
440 include the keyword "private" after the "signal" keyword.  This will make
441 the wrapper a normal private method.  You can also make a protected wrapper
442 by using "protected" instead of "private".
443 .PP
444 If you don't define a "first" or a "last", the default will be taken as
445 "last".
446 .PP
447 You can also add additional flags.  You do this just like with the argument
448 flags, although this is probably very rare.  These are the GTK_RUN_* flags,
449 and you can add them without the GTK_RUN_ prefix into a parenthesis, just
450 after the "signal" keyword.  By default all public signals are GTK_RUN_ACTION.
451 .PP
452 Override methods:
453 .PP
454 If you need to override some method (a signal or a virtual method
455 of some class in the parent tree of the new object), you can define and
456 override method.  After the "override" keyword, you should put the
457 typename of the class you are overriding a method from.  Other then that
458 it is the same as for other methods.  The "self" pointer in this case
459 should be the type of the method you are overriding so that you don't
460 get warnings during compilation.  Also to call the method of the parent
461 class, you can use the PARENT_HANDLER macro with your arguments.  Example:
462 .nf
463
464   override (Gtk:Container) void
465   add (Gtk:Container *self (check null type), Gtk:Widget *wid (check null type))
466   {
467           /* some code here */
468           PARENT_HANDLER(self, wid);
469   }
470
471 .fi
472 If the function has a return value, then PARENT_HANDLER is an expression that
473 you can use.  It will return whatever the parent handler returned, or the
474 "onerror" expression if there was no parent handler.
475 .PP
476 Calling methods:
477 .PP
478 Inside the code, pointers are set for the methods, so that you don't
479 have to type the class name before each call, just the name of the method.
480 Example:
481 .nf
482
483   private int
484   foo(self)
485   {
486           return self->len;
487   }
488   
489   private int
490   bar(self,int i)
491   {
492           return foo(self) + i;
493   }
494
495 .fi
496 .PP
497 Making new objects:
498 .PP
499 You should define a new method which should be a normal public method.  Inside
500 this method, you can use the GET_NEW macro that is defined for you and that
501 will fetch a new object, so a fairly standard new method would look like:
502 .nf
503
504   public GtkObject *
505   new(void) {
506           GtkObject *ret;
507           ret = GTK_OBJECT (GET_NEW);
508           return ret;
509   }
510
511 .fi
512 .PP
513 Casts:
514 .PP
515 There are some standard casts defined for you.  Instead of using the full
516 macros inside the .c file, you can use SELF, IS_SELF and SELF_CLASS.  Using
517 these makes it easier to for example change classnames around.  There is
518 however no self type, so if you're declaring a pointer to your object, you
519 still have to use the full type.
520
521 .SH DEALING WITH DIFFERENT GOB VERSIONS
522 .PP
523 Defines:
524 .PP
525 In your generated C file, you can use the defines GOB_VERSION_MAJOR
526 GOB_VERSION_MINOR and GOB_VERSION_PATCHLEVEL if you wish to for example
527 use a feature that is only available in some newer gob version.  Note however
528 that you can only use these defines in the C code portions of your .gob file,
529 and #ifdef's cannot span multiple functions.  Check the BUGS section
530 for more on using the C preprocessor and gob.  Also note that these
531 have only been available since the 0.92.1 version of gob.
532 .PP
533 Minimum version requires:
534 .PP
535 You can also make your .gob file require at least certain version of gob.  You
536 do this by putting 'requires x.y.z' (where x.y.z is the version number) outside
537 of any C block, comment or class, usually you should make this the first line
538 in the file or close to the top.  If gob finds this and the version of gob used
539 to compile the code is lower then that listed in the require, gob will generate
540 an error and exit.  For example to require that gob version 0.92.1 or higher
541 be used to compile a file, put this at the top of that file:
542 .nf
543
544   requires 0.92.1
545
546 .fi
547 It should be noted however that this feature was not added until 0.92.1, and
548 so if the file gets compiled by a lower version, gob would generate a 
549 syntax error.  Thus by putting in a requires line, you are implicitly
550 requiring at least 0.92.1.
551
552 .SH C++ MODE
553 .PP
554 There is a C++ mode so that gob creates C++ compiler friendly files.  You need
555 to use the --for-cpp argument to gob.  This will make the generated file have
556 a .cc instead of a .c extension, and several things will be adjusted to
557 make it all work for a C++ compiler.  One thing that will be missing is an
558 alias to the new method, as that clashes with C++, so instead you'll have to
559 use the full name of the method inside your code.  Also note that gob does
560 not use any C++ features, this option will just make the generated code
561 compile with a C++ compiler.
562
563 .SH IDENTIFIER CONFLICTS
564 .PP
565 Gob will need to define some local varibles and functions in the generated
566 files, so you need to take some precaution not to conflict with these.  The
567 general rule of thumb is that all of these start with three underscores.  There
568 is one, "parent_class" which doesn't because it's intended for use in your
569 code.  For virtuals or signals, you cannot use the identifier __parent__
570 which is used for the parent of the object.  You should actually never access
571 __parent__ either as it not guaranteed that it will stay named this way.
572 Data members cannot be named __parent__ nor _priv.  For methods, you cannot
573 use the identifiers "init" or "class_init" unless you mean the constructor
574 methods.  You shouldn't generally use 3 underscores even in override method
575 argument lists and virtual and signal method names as it might confuse the
576 PARENT_HANDLER macro.  In fact avoiding all names with three underscores is
577 the best policy when working with gob.
578
579 .SH USING GTK-DOC STYLE INLINE DOCUMENTATION
580 .PP
581 If you want to use gtk-doc style inline documentation for your objects, you
582 can do one of two things.  First, you could include the inline documentation
583 comments in your %{ %} section which will then be put verbatim into the
584 output source file.  This is the way you should use for functions you define
585 outside of the class.
586 .PP
587 For class methods, you should use a gtk+ style comment, however it can be
588 indented any number of tabs or spaces and you can use the short method name
589 without the type prefix.  Gob will automatically try to extract these and
590 translate to full names and put them in the output source file.  An example
591 would be:
592 .fi
593
594   class Gtk:Button:Example from Gtk:Button {
595           /**
596            * new:
597            *
598            * Makes a new #GtkButtonExample widget
599            *
600            * Returns: a new widget
601            **/
602           public
603           GtkWidget *
604           new(void)
605           {
606                   return GTK_WIDGET(GET_NEW);
607           }
608   } 
609
610 .fi
611 If the function you are documenting is a signal or a virtual then it will
612 be documentating the wrapper that starts that virtual function or emits
613 that signal.
614
615 .SH DEALING WITH CIRCULAR HEADERS
616 .PP
617 Sometimes you may need to use an object of type MyObjectA in the MyObjectB
618 class and vice versa.  Obviously you can't include headers for both.  So you
619 need to just declare the typedef in the header of A for B, and the other way
620 around as well.  The headers generated since v0.92.2 include a protecting
621 define before it declares the typedef.  This define is the
622 __TYPEDEF_<upper case object name>__.  So inside my-object-a.h there will be
623 this:
624 .nf
625
626   #ifndef __TYPEDEF_MY_OBJECT_A__
627   #define __TYPEDEF_MY_OBJECT_A__
628   typedef struct _MyObjectA MyObjectA;
629   #endif
630
631 .fi
632 Now instead of including my-object-a.h in the header section of
633 my-object-b.gob, just copy the above code there and you're set for using
634 MyObjectA as a type in the method parameters and public types.
635 .PP
636 Another way to get out of this problem is if you can use those types only
637 in the private members, in which case they won't be in the generated public
638 header.
639
640 .SH BUGS
641 .PP
642 Also the lexer does not actually parse the C code, so I'm sure that some corner
643 cases or maybe even some not so corner cases of C syntax might confuse gob
644 completely.  If you find any, send me the source that makes it go gaga and I'll
645 try to make the lexer try to handle it properly, but no promises.
646 .PP
647 Another thing is that gob ignores preprocessor macros.  Since gob counts
648 braces, the following code won't work:
649 .nf
650
651   #ifdef SOME_DEFINE
652   if(foo) {
653   #else
654   if(bar) {
655   #endif
656           blah();
657   }
658
659 .fi
660 To make this work, you'd have to do this:
661 .nf
662
663   #ifdef SOME_DEFINE
664   if(foo)
665   #else
666   if(bar)
667   #endif
668   {
669           blah();
670   }
671
672 .fi
673 There is no real good way we can handle this without parsing C code, so we
674 probably never will.  In the future, I might add #if 0 as a comment but
675 that's about as far as I can really take it and even that is problematic.
676 Basically, if you use gob, just don't use the C preprocessor too extensively.
677 .PP
678 Comments will not get through to the generated files unless inside C code.
679 This makes using something like gtk-doc harder.  However I'm planning to
680 fix this somehow.
681
682 .SH AUTHOR
683 .PP
684 George Lebl <jirka@5z.com>