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