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