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