3 .\" (C) 1999 George Lebl <jirka@5z.com>
5 .\" This manual page is covered by the terms of the GNU General
8 .TH GOB 1 "GOB @VERSION@"
10 GOB \- The GTK+ Object Builder
13 .B gob [-?] [-h] [-w] [--exit-on-warn] [--no-exit-on-warn] [--for-cpp]
14 [--no-touch-headers] file
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.
27 Display a simple help screen.
32 Exit with an errorcode even when you encounter a warning.
35 Exit with an error only on errors, not on warnings, this is the default.
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.
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
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.
69 .SH INCLUDING NORMAL C CODE IN THE OUTPUT FILES
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{'.
78 /* will be included in the header */
83 /* will be included in the C file */
94 Gob will automatically include the class header file at the top of the .c
95 source file. If you wish to include it somewhere else, put the include
96 into some %{ %} section above the class definition, and gob will not include
97 it automatically. This way you can avoid circular includes and control
98 where in the file do you want to include the header.
100 .SH MAKING A NEW CLASS
104 There can be only one class per input file. Defining a class
105 is sort of like in Java, you define the class and write inline code
106 directly into the class definition. To define a class you need to specify
107 the new object name and the name of the object from which it is derived
108 from, such as this "class <new type> from <parent type> { <class code> }".
112 class Gtk:New:Button from Gtk:Button {
120 There are three types of data members. Two of them are normal
121 data numbers, and one is a virtual one, usually linked to a normal public
122 data member. The two normal data members are public or private. They are
123 basically just copied into the object directly. There is only one
124 identifier allowed per typename unlike in normal C. Example:
128 private GtkWidget *h;
132 Public datamembers are accessed normally as members of the object struct.
133 Example where 'i' is as above a public data member:
140 The private data members are defined in a structure which is only available
141 inside the .c file. You must access them using the structure _priv. Example
142 where 'h' is the private data member (as in the above example):
145 object->_priv->h = NULL;
148 Note that the _priv structure is only accessible to C code blocks below or
149 inside the class definition in the .gob file. If you use it above, you will
150 get a 'dereferencing incomplete pointer type' error from the C compiler.
152 The third type is an argument type. It is a named datamember which
153 is one of the features of the GTK+ object system. You need to define a get
154 and a set handler. They are fragments of C code that will be used to
155 get the value or set the value of the argument. Inside them you can use the
156 define ARG to which you assign the data or get the data. You can also use
157 the identifier "self" as pointer to the object instance. The type is
158 defined as one of the gtk type enums, but without the GTK_TYPE_ prefix.
163 argument INT height set { self->height = ARG; } get { ARG = self->height; };
167 If you don't define a set or a get handler it will be a readonly
168 or a writeonly argument. If you want to add extra argument flags, add
169 them into parenthesis after the argument keyword, separated by '|' and
170 without the GTK_ARG_ prefix. For example:
174 argument (CONSTRUCT) INT height get { ARG = self->height; };
180 There is a whole array of possible methods. The two normal,
181 "familiar" method types are private and public. Public are defined as
182 normal functions with a prototype in the header file. Private methods
183 are defined as static functions with prototypes at the top of the .c
184 file. Then there are signal, virtual and override methods. You can also
185 define init and init_class methods with a special definition if you want
186 to add code to the constructors or you can just leave them out.
190 For all but the init and init_class methods, you use the
191 following syntax for arguments. The first argument can be just "self",
192 which gob will translate into a pointer to the object instance. The rest
193 of the arguments are very similar to normal C arguments. If the
194 typename is an object pointer you should use the syntax defined above
195 with the words separated by ':'
199 <type> <argument id> (check <list of checks>)
202 The checks are glib type preconditions, and can be the following:
203 "null", which tests pointers for being NULL, "type" which checks GTK+
204 object pointers for being the right type, "<test> <number>" which tests
205 numeric arguments for being a certain value. The test can be a <,>,<=,>=
209 public int foo(self, int h (check > 0 < 11), Gtk:Widget *w (check null type))
213 This will be the prototype of a function which has a self pointer
214 as the first argument, an integer argument which will be checked and has
215 to be more then 0 and less then 11, and a pointer to a GtkWidget object
216 instance and it is checked for being null and the type will also be
221 Methods which have a return value, there also has to be something
222 returned if there is an error, such as if a precondition is not met. The
223 default is 0, casted to the type of the method. If you need to return
224 something else then you can specify an "onerror" keyword after the
225 prototype and after that a number, a token (an identifier) or a bit of C
226 code enclosed in braces {}. The braces will not be printed into the
227 output, they just delimit the string. For example
230 public void * get_something(self, int i (check >= 0)) onerror NULL {
238 Virtual methods are basically pointers in the class structure,
239 so that one can override the method in derived methods. They can be empty
240 (if you put ';' instead of the C code). A wrapper will also be defined
241 which makes calling the methods he same as public methods. This type of
242 method is just a little bit "slower" then normal functions, but not as
243 slow as signals. You define them by using "virtual" keyword before the
244 prototype. If you put the keyword "private" right after the "virtual"
245 keyword, the wrapper will not be a public method, but a private one.
249 Signals are methods to which the user can bind other handlers
250 and override the default handler. The default handler is basically the
251 method body. This is the most versatile and flexible type of a method
252 and also the slowest. You need to specify a whole bunch of things when
253 you define a signal. One thing is when the default handler will be run,
254 first or last. You specify that by "first" or "last" right after the
255 "signal" keyword. Then you need to define the gtk enum types (again
256 without the GTK_TYPE_ prefix). For that you define the return types
257 and the types of arguments after the "self" pointer (not including the
258 "self" pointer). You put it in the following syntax "<return type> (<list
259 of arguments>)". If the return type is void, the type should be "NONE",
260 the same should be for the argument list. The rest of the prototype is
261 the same as for other method types. The body can also be empty, and
262 also there is a public method wrapper which you can use for calling the
263 signal just like a public method. Example:
266 signal first INT(POINTER,INT)
267 int do_something(self, Gtk:Widget *w (check null type), int length)
274 signal last NONE(NONE) void foo(self);
278 If you don't want the wrapper that emits the signal to be public, you can
279 include the keyword "private" after the "signal" keyword. This will make
280 the wrapper a normal private method.
282 If you don't define a "first" or a "last", the default will be taken as
287 If you need to override some method (a signal or a virtual method
288 of some class in the parent tree of the new object), you can define and
289 override method. After the "override" keyword, you should put the
290 typename of the class you are overriding a method from. Other then that
291 it is the same as for other methods. The "self" pointer in this case
292 should be the type of the method you are overriding so that you don't
293 get warnings during compilation. Also to call the method of the parent
294 class, you can use the PARENT_HANDLER macro with your arguments. Example:
297 override (Gtk:Container) void
298 add (Gtk:Container *self (check null type), Gtk:Widget *wid (check null type))
301 PARENT_HANDLER(self, wid);
308 Inside the code, pointers are set for the methods, so that you don't
309 have to type the class name before each call, just the name of the method.
322 return foo(self) + i;
329 You should define a new method which should be a normal public method. Inside
330 this method, you can use the GET_NEW macro that is defined for you and that
331 will fetch a new object, so a fairly standard new method would look like:
337 ret = GTK_OBJECT (GET_NEW);
345 There is a C++ mode so that gob creates C++ compiler friendly files. You need
346 to use the --for-cpp argument to gob. This will make the generated file have
347 a .cc instead of a .c extention, and several things will be adjusted to
348 make it all work for a C++ compiler. One thing that will be missing is an
349 alias to the new method, as that clashes with C++, so instead you'll have to
350 use the full name of the method inside your code. Also note that gob does
351 not use any C++ features, this option will just make the generated code
352 compile with a C++ compiler.
356 Also the lexer does not actually parse the C code, so I'm sure that some corner
357 cases or maybe even some not so corner cases of C syntax might confuse gob
358 completely. If you find any, send me the source that makes it go gaga and I'll
359 try to make the lexer try to handle it properly, but no promises.
361 Another thing is that gob ignores preprocessor macros. Since gob counts
362 braces, the following code won't work:
374 To make this work, you'd have to do this:
387 There is no real good way we can handle this without parsing C code, so we
388 probably never will. In the future, I might add #if 0 as a comment but
389 that's about as far as I can really take it and even that is problematic.
390 Basically, if you use gob, just don't use the C preprocessor too extensively.
394 George Lebl <jirka@5z.com>