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