]> git.draconx.ca Git - gob-dx.git/blob - doc/gob.1.in
426858045c287bc9e28fee27b6caf0723ac29072
[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
45
46 .SH TYPENAMES
47 .PP
48 Because we need to parse out different parts of the typename, sometimes you
49 need to specify the typename with some special syntax.  Types are specified in
50 capitalized form and words are separated by ':'.  The first word of the type
51 (which can be empty) is the "namespace".  This fact is for example used for the
52 type checking macro and the type macro.  For "Gtk:New:Button", the macros will
53 be GTK_IS_NEW_BUTTON and GTK_TYPE_NEW_BUTTON.  This colon separated format of
54 typenames is used in the class declaration header and for method argument
55 types.
56
57 .SH OUTPUT FILES
58 .PP
59 The filenames are created from the typename.  The words are
60 separated by '-' and all in lower case.  For example for an object named
61 "Gtk:New:Button", the files are gtk-new-button.c and gtk-new-button.h.
62 The header file is created to be human readable and to be used as a
63 reference to the object.  The .c source file is not created as a human
64 readable source and is littered with #line statements, which make the
65 compiler attempt to point you to the right line in your .gob file in
66 case of parsing errors.  The output should not be editted by hand, and
67 you should only edit the .gob file.
68
69 .SH INCLUDING NORMAL C CODE IN THE OUTPUT FILES
70 .PP
71 To include some code directly in the output C file begin with '%{'
72 on an empty line and end the code with a '%}' on an empty line.  To
73 put the code in the output header file, start the code with a '%h{'.
74 For example:
75 .nf
76
77   %h{
78   /* will be included in the header */
79   void somefunc(int i);
80   %}
81
82   %{
83   /* will be included in the C file */
84   void somefunc(int i)
85   {
86         /* some code */
87   }
88   %}
89
90 .fi
91
92 .SH MAKING A NEW CLASS
93 .PP
94 The class header:
95 .PP
96 There can be only one class per input file.  Defining a class
97 is sort of like in Java, you define the class and write inline code
98 directly into the class definition.  To define a class you need to specify
99 the new object name and the name of the object from which it is derived
100 from, such as this "class <new type> from <parent type> { <class code> }".
101 For example:
102 .nf
103
104   class Gtk:New:Button from Gtk:Button {
105           <class code>
106   }
107
108 .fi
109 .PP
110 Data members:
111 .PP
112 There are three types of data members.  Two of them are normal
113 data numbers, and one is a virtual one, usually linked to a normal public
114 data member.  The two normal data members are public or private. They are
115 basically just copied into the object directly.  There is only one
116 identifier allowed per typename unlike in normal C.  Example:
117 .nf
118
119   public int i;
120   private GtkWidget *h;
121
122 .fi
123 .PP
124 The private members are not currently protected from outside use,
125 they are just marked by a comment in the header file, this will most likely
126 be somehow solved in some future version.
127 .PP
128 The third type is an argument type.  It is a named datamember which
129 is one of the features of the GTK+ object system.  You need to define a get
130 and a set handler.  They are fragments of C code that will be used to 
131 get the value or set the value of the argument.  Inside them you can use the
132 define ARG to which you assign the data or get the data.  You can also use
133 the identifier "self" as pointer to the object instance.  The type is
134 defined as one of the gtk type enums, but without the GTK_TYPE_ prefix.
135 For example:
136 .nf
137
138   public int height;
139   argument INT height set { self->height = ARG; } get { ARG = self->height; };
140
141 .fi
142 .PP
143 If you don't define a set or a get handler it will be a readonly
144 or a writeonly argument.  If you want to add extra argument flags, add
145 them into parenthesis after the argument keyword, separated by '|' and
146 without the GTK_ARG_ prefix.  For example:
147 .nf
148
149   public int height;
150   argument (CONSTRUCT) INT height get { ARG = self->height; };
151
152 .fi
153 .PP
154 Methods:
155 .PP
156 There is a whole array of possible methods.  The two normal,
157 "familiar" method types are private and public.  Public are defined as
158 normal functions with a prototype in the header file.  Private methods
159 are defined as static functions with prototypes at the top of the .c
160 file.  Then there are signal, virtual and override methods.  You can also
161 define init and init_class methods with a special definition if you want
162 to add code to the constructors or you can just leave them out.
163 .PP
164 Argument lists:
165 .PP
166 For all but the init and init_class methods, you use the
167 following syntax for arguments.  The first argument can be just "self",
168 which gob will translate into a pointer to the object instance.  The rest
169 of the arguments are very similar to normal C arguments.  If the
170 typename is an object pointer you should use the syntax defined above
171 with the words separated by ':'
172 .nf
173 <type> <argument id>
174 or
175 <type> <argument id> (check <list of checks>)
176 .fi
177 .PP
178 The checks are glib type preconditions, and can be the following:
179 "null", which tests pointers for being NULL, "type" which checks GTK+
180 object pointers for being the right type, "<test> <number>" which tests
181 numeric arguments for being a certain value.  The test can be a <,>,<=,>=
182 != or ==.  Example:
183 .nf
184   
185   public int foo(self, int h (check > 0 < 11), Gtk:Widget *w (check null type))
186
187 .fi
188 .PP
189 This will be the prototype of a function which has a self pointer
190 as the first argument, an integer argument which will be checked and has
191 to be more then 0 and less then 11, and a pointer to a GtkWidget object
192 instance and it is checked for being null and the type will also be
193 checked.
194 .PP
195 Error return:
196 .PP
197 Methods which have a return value, there also has to be something
198 returned if there is an error, such as if a precondition is not met.  The
199 default is 0, casted to the type of the method.  If you need to return
200 something else then you can specify an "onerror" keyword after the
201 prototype and after that a number, a token (an identifier) or a bit of C
202 code enclosed in braces {}.  The braces will not be printed into the
203 output, they just delimit the string.  For example
204 .nf
205
206   public void * get_something(self, int i (check >= 0)) onerror NULL {
207           ...
208   }
209
210 .fi
211 .PP
212 Virtual methods:
213 .PP
214 Virtual methods are basically pointers in the class structure,
215 so that one can override the method in derived methods.  They can be empty
216 (if you put ';' instead of the C code).  A wrapper will also be defined
217 which makes calling the methods he same as public methods.  This type of
218 method is just a little bit "slower" then normal functions, but not as
219 slow as signals.  You define them by using "virtual" keyword before the
220 prototype. If you put the keyword "private" right after the "virtual"
221 keyword, the wrapper will not be a public method, but a private one.
222 .PP
223 Signals:
224 .PP
225 Signals are methods to which the user can bind other handlers
226 and override the default handler.  The default handler is basically the
227 method body.  This is the most versatile and flexible type of a method
228 and also the slowest.  You need to specify a whole bunch of things when
229 you define a signal.  One thing is when the default handler will be run,
230 first or last.  You specify that by "first" or "last" right after the
231 "signal" keyword.  Then you need to define the gtk enum types (again
232 without the GTK_TYPE_ prefix).  For that you define the return types
233 and the types of arguments after the "self" pointer (not including the
234 "self" pointer).  You put it in the following syntax "<return type> (<list
235 of arguments>)".  If the return type is void, the type should be "NONE",
236 the same should be for the argument list.  The rest of the prototype is
237 the same as for other method types.  The body can also be empty, and
238 also there is a public method wrapper which you can use for calling the
239 signal just like a public method.  Example:
240 .nf
241
242   signal first INT(POINTER,INT)
243   int do_something(self, Gtk:Widget *w (check null type), int length)
244   {
245           ...
246   }
247   
248 or
249
250   signal last NONE(NONE) void foo(self);
251
252 .fi
253 .PP
254 If you don't want the wrapper that emits the signal to be public, you can
255 include the keyword "private" after the "signal" keyword. This will make
256 the wrapper a normal private method.
257 .PP
258 If you don't define a "first" or a "last", the default will be taken as
259 "last".
260 .PP
261 Override methods:
262 .PP
263 If you need to override some method (a signal or a virtual method
264 of some class in the parent tree of the new object), you can define and
265 override method.  After the "override" keyword, you should put the
266 typename of the class you are overriding a method from.  Other then that
267 it is the same as for other methods.  The "self" pointer in this case
268 should be the type of the method you are overriding so that you don't
269 get warnings during compilation.  Also to call the method of the parent
270 class, you can use the PARENT_HANDLER macro with your arguments.  Example:
271 .nf
272
273   override (Gtk:Container) void
274   add (Gtk:Container *self (check null type), Gtk:Widget *wid (check null type))
275   {
276           /* some code here */
277           PARENT_HANDLER(self, wid);
278   }
279 .fi
280 .PP
281 Calling methods:
282 .PP
283 Inside the code, pointers are set for the methods, so that you don't
284 have to type the class name before each call, just the name of the method.
285 Example:
286 .nf
287
288   private int
289   foo(self)
290   {
291           return self->len;
292   }
293   
294   private int
295   bar(self,int i)
296   {
297           return foo(self) + i;
298   }
299
300 .fi
301 .PP
302 Making new objects:
303 .PP
304 You should define a new method which should be a normal public method.  Inside
305 this method, you can use the GET_NEW macro that is defined for you and that
306 will fetch a new object, so a fairly standard new method would look like:
307 .nf
308
309   public GtkWidget *
310   new(void) {
311           GtkObject *ret;
312           ret = GTK_OBJECT (GET_NEW);
313           return ret;
314   }
315
316 .fi
317
318 .SH C++ MODE
319 .PP
320 There is a C++ mode so that gob creates C++ compiler friendly files.  You need
321 to use the --for-cpp argument to gob.  This will make the generated file have
322 a .cc instead of a .c extention, and several things will be adjusted to
323 make it all work for a C++ compiler.  One thing that will be missing is an
324 alias to the new method, as that clashes with C++, so instead you'll have to
325 use the full name of the method inside your code.  Also note that gob does
326 not use any C++ features, this option will just make the generated code
327 compile with a C++ compiler.
328
329 .SH BUGS
330 .PP
331 The generated header file is included as the first file in the .c file, no
332 matter what. This means that you will have to put things that need to be
333 included before that, into an %h{ } section.
334 .PP
335 Also the lexer does not actually parse the C code, so I'm sure that some corner
336 cases or maybe even some not so corner cases of C syntax might confuse gob
337 completely.  If you find any, send me the source that makes it go gaga and I'll
338 try to make the lexer try to handle it properly, but no promises.
339 .PP
340 Another thing is that gob ignores preprocessor macros.  Since gob counts
341 braces, the following code won't work:
342 .nf
343
344   #ifdef SOME_DEFINE
345   if(foo) {
346   #else
347   if(bar) {
348   #endif
349           blah();
350   }
351
352 .fi
353 To make this work, you'd have to do this:
354 .nf
355
356   #ifdef SOME_DEFINE
357   if(foo)
358   #else
359   if(bar)
360   #endif
361   {
362           blah();
363   }
364
365 .fi
366 There is no real good way we can handle this without parsing C code, so we
367 probably never will.  In the future, I might add #if 0 as a comment but
368 that's about as far as I can really take it and even that is problematic.
369 Basically, if you use gob, just don't use the C preprocessor too extensively.
370
371 .SH AUTHOR
372 .PP
373 George Lebl <jirka@5z.com>