2019-02-05 09:14:31 +00:00
|
|
|
GTK Coding Style
|
2010-09-24 02:26:07 +00:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
This document is intended to be a short description of the preferred
|
2019-02-05 09:14:31 +00:00
|
|
|
coding style to be used for the GTK source code. It was strongly
|
2020-05-26 17:19:53 +00:00
|
|
|
inspired by Clutter's `CODING_STYLE`.
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-08-21 12:41:13 +00:00
|
|
|
Coding style is a matter of consistency, readability and maintenance;
|
2010-09-24 02:26:07 +00:00
|
|
|
coding style is also completely arbitrary and a matter of taste. This
|
|
|
|
document will use examples at the very least to provide authoritative
|
|
|
|
and consistent answers to common questions regarding the coding style,
|
|
|
|
and will also try to identify the allowed exceptions.
|
|
|
|
|
|
|
|
The examples will show the preferred coding style; the negative examples
|
2019-02-05 09:14:31 +00:00
|
|
|
will be clearly identified. Please, don't submit code to GTK that
|
2010-09-24 02:26:07 +00:00
|
|
|
looks like any of these.
|
|
|
|
|
|
|
|
Part of the rationales for these coding style rules are available either
|
2020-05-26 17:19:53 +00:00
|
|
|
in the kernel CodingStyle document or in Cairo's `CODING_STYLE` one.
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
When in doubt, check the surrounding code and try to imitate it.
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
### Line width
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
The maximum line width for source files is 80 characters, whenever possible.
|
|
|
|
Longer lines are usually an indication that you either need a function
|
|
|
|
or a pre-processor macro.
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
### Indentation
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Each new level is indented 2 or more spaces than the previous level:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
if (condition)
|
|
|
|
single_statement ();
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
This can only be achieved using space characters. It may not be achieved
|
|
|
|
using tab characters alone, or using a combination of spaces and tabs.
|
|
|
|
|
|
|
|
Do not change the editor's configuration to change the meaning of a
|
|
|
|
tab character (see below); code using tabs to indent will not be accepted
|
2019-02-05 09:14:31 +00:00
|
|
|
into GTK.
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Even if two spaces for each indentation level allows deeper nesting than
|
2019-02-05 09:14:31 +00:00
|
|
|
8 spaces, GTK favours self-documenting function names that can take
|
2010-09-24 02:26:07 +00:00
|
|
|
quite some space. For this reason you should avoid deeply nested code.
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
### Tab characters
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
The tab character must always be expanded to spaces. If a literal
|
|
|
|
tab must be used inside the source, the tab must always be interpreted
|
|
|
|
according to its traditional meaning:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
Advance to the next column which is a multiple of 8.
|
|
|
|
[ these two lines should be aligned ]
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
### Braces
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Curly braces should not be used for single statement blocks:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
if (condition)
|
|
|
|
single_statement ();
|
|
|
|
else
|
|
|
|
another_single_statement (arg1);
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
In case of multiple statements, curly braces should be put on another
|
|
|
|
indentation level:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
if (condition)
|
|
|
|
{
|
|
|
|
statement_1 ();
|
|
|
|
statement_2 ();
|
|
|
|
statement_3 ();
|
|
|
|
}
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
The "no block for single statements" rule has only four exceptions:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
1. if the single statement covers multiple lines, e.g. for functions with
|
2010-09-24 02:26:07 +00:00
|
|
|
many arguments, and it is followed by else or else if:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
/* valid */
|
|
|
|
if (condition)
|
|
|
|
{
|
|
|
|
a_single_statement_with_many_arguments (some_lengthy_argument,
|
|
|
|
another_lengthy_argument,
|
|
|
|
and_another_one,
|
|
|
|
plus_one);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
another_single_statement (arg1, arg2);
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
2. if the condition is composed of many lines:
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
/* valid */
|
|
|
|
if (condition1 ||
|
|
|
|
(condition2 && condition3) ||
|
|
|
|
condition4 ||
|
|
|
|
(condition5 && (condition6 || condition7)))
|
|
|
|
{
|
|
|
|
a_single_statement ();
|
|
|
|
}
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
3. Nested if's, in which case the block should be placed on the
|
2010-09-24 02:26:07 +00:00
|
|
|
outermost if:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
/* valid */
|
|
|
|
if (condition)
|
|
|
|
{
|
|
|
|
if (another_condition)
|
|
|
|
single_statement ();
|
|
|
|
else
|
|
|
|
another_single_statement ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* invalid */
|
|
|
|
if (condition)
|
|
|
|
if (another_condition)
|
|
|
|
single_statement ();
|
|
|
|
else if (yet_another_condition)
|
|
|
|
another_single_statement ();
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
4. If either side of an if-else statement has braces, both sides
|
2010-09-24 02:26:07 +00:00
|
|
|
should, to match up indentation:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
/* valid */
|
|
|
|
if (condition)
|
|
|
|
{
|
|
|
|
foo ();
|
|
|
|
bar ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
baz ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* invalid */
|
|
|
|
if (condition)
|
|
|
|
{
|
|
|
|
foo ();
|
|
|
|
bar ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
baz ();
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
In general, new blocks should be placed on a new indentation level,
|
|
|
|
like:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
statement_1 ();
|
|
|
|
statement_2 ();
|
|
|
|
|
|
|
|
{
|
|
|
|
int var1 = 42;
|
|
|
|
gboolean res = FALSE;
|
|
|
|
|
|
|
|
res = statement_3 (var1);
|
|
|
|
|
|
|
|
retval = res ? -1 : 1;
|
|
|
|
}
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
While curly braces for function definitions should rest on a new line
|
|
|
|
they should not add an indentation level:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
/* valid */
|
|
|
|
static void
|
|
|
|
my_function (int argument)
|
|
|
|
{
|
|
|
|
do_my_things ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* invalid */
|
|
|
|
static void
|
|
|
|
my_function (int argument) {
|
|
|
|
do_my_things ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* invalid */
|
|
|
|
static void
|
|
|
|
my_function (int argument)
|
|
|
|
{
|
|
|
|
do_my_things ();
|
|
|
|
}
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Curly braces must not be placed on the same line as a condition:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
/* invalid */
|
|
|
|
if (condition) {
|
|
|
|
statement_1 ();
|
|
|
|
statement_2 ();
|
|
|
|
}
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Conditions
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Do not check boolean values for equality:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
/* invalid */
|
|
|
|
if (condition == TRUE)
|
|
|
|
do_foo ();
|
|
|
|
|
|
|
|
/* valid */
|
|
|
|
if (another_condition)
|
|
|
|
do_bar ();
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Even if C handles NULL equality like a boolean, be explicit:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
/* valid */
|
|
|
|
if (some_pointer == NULL)
|
|
|
|
do_blah ();
|
|
|
|
|
|
|
|
/* invalid */
|
|
|
|
if (some_other_pointer)
|
|
|
|
do_blurp ();
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
In case of conditions split over multiple lines, the logical operators should
|
|
|
|
always go at the end of the line:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
/* invalid */
|
|
|
|
if (condition1
|
|
|
|
|| condition2
|
|
|
|
|| condition3)
|
|
|
|
{
|
|
|
|
do_foo ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* valid */
|
|
|
|
if (condition1 &&
|
|
|
|
condition2 &&
|
|
|
|
(condition3 || (condition4 && condition5)))
|
|
|
|
{
|
|
|
|
do_blah ();
|
|
|
|
}
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
### Functions
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Functions should be declared by placing the returned value on a separate
|
|
|
|
line from the function name:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
void
|
|
|
|
my_function (void)
|
|
|
|
{
|
|
|
|
}
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
The arguments list must be broken into a new line for each argument,
|
|
|
|
with the argument names right aligned, taking into account pointers:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
void
|
|
|
|
my_function (some_type_t type,
|
|
|
|
another_type_t *a_pointer,
|
|
|
|
final_type_t another_type)
|
|
|
|
{
|
|
|
|
}
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
The alignment also holds when invoking a function without breaking the
|
|
|
|
80 characters limit:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
align_function_arguments (first_argument,
|
|
|
|
second_argument,
|
|
|
|
third_argument);
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
To respect the 80 characters limit do not break the function name from
|
|
|
|
the arguments:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
/* invalid */
|
|
|
|
a_very_long_function_name_with_long_parameters
|
|
|
|
(argument_the_first, argument_the_second);
|
|
|
|
|
|
|
|
/* valid */
|
|
|
|
first_a = argument_the_first;
|
|
|
|
second_a = argument_the_second;
|
|
|
|
a_very_long_function_name_with_long_parameters (first_a, second_a);
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
### Whitespace
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Always put a space before a parenthesis but never after:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
/* valid */
|
|
|
|
if (condition)
|
|
|
|
do_my_things ();
|
|
|
|
|
|
|
|
/* valid */
|
|
|
|
switch (condition)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* invalid */
|
|
|
|
if(condition)
|
|
|
|
do_my_things();
|
|
|
|
|
|
|
|
/* invalid */
|
|
|
|
if ( condition )
|
|
|
|
do_my_things ( );
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
A `switch()` should open a block on a new indentation level, and each case
|
2010-09-24 02:26:07 +00:00
|
|
|
should start on the same indentation level as the curly braces, with the
|
|
|
|
case block on a new indentation level:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
/* valid */
|
|
|
|
switch (condition)
|
|
|
|
{
|
|
|
|
case FOO:
|
|
|
|
do_foo ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BAR:
|
|
|
|
do_bar ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* invalid */
|
|
|
|
switch (condition) {
|
|
|
|
case FOO: do_foo (); break;
|
|
|
|
case BAR: do_bar (); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* invalid */
|
|
|
|
switch (condition)
|
|
|
|
{
|
|
|
|
case FOO: do_foo ();
|
|
|
|
break;
|
|
|
|
case BAR: do_bar ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* invalid */
|
|
|
|
switch (condition)
|
|
|
|
{
|
|
|
|
case FOO:
|
|
|
|
do_foo ();
|
|
|
|
break;
|
|
|
|
case BAR:
|
|
|
|
do_bar ();
|
|
|
|
break;
|
|
|
|
}
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
It is preferable, though not mandatory, to separate the various cases with
|
|
|
|
a newline:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
switch (condition)
|
|
|
|
{
|
|
|
|
case FOO:
|
|
|
|
do_foo ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BAR:
|
|
|
|
do_bar ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
do_default ();
|
|
|
|
}
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
The `break` statement for the `default:` case is not mandatory.
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
If a case block needs to declare new variables, the same rules as the
|
|
|
|
inner blocks (see above) apply; the break statement should be placed
|
|
|
|
outside of the inner block:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
switch (condition)
|
|
|
|
{
|
|
|
|
case FOO:
|
|
|
|
{
|
|
|
|
int foo;
|
|
|
|
|
|
|
|
foo = do_foo ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
...
|
|
|
|
}
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
When declaring a structure type use newlines to separate logical sections
|
|
|
|
of the structure:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
struct _GtkWrapBoxPrivate
|
|
|
|
{
|
|
|
|
GtkOrientation orientation;
|
|
|
|
GtkWrapAllocationMode mode;
|
|
|
|
|
|
|
|
GtkWrapBoxSpreading horizontal_spreading;
|
|
|
|
GtkWrapBoxSpreading vertical_spreading;
|
|
|
|
|
|
|
|
guint16 vertical_spacing;
|
|
|
|
guint16 horizontal_spacing;
|
|
|
|
|
|
|
|
guint16 minimum_line_children;
|
|
|
|
guint16 natural_line_children;
|
|
|
|
|
|
|
|
GList *children;
|
|
|
|
};
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Do not eliminate whitespace and newlines just because something would
|
|
|
|
fit on 80 characters:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
/* invalid */
|
|
|
|
if (condition) foo (); else bar ();
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Do eliminate trailing whitespace on any line, preferably as a separate
|
|
|
|
patch or commit. Never use empty lines at the beginning or at the end of
|
|
|
|
a file.
|
|
|
|
|
|
|
|
Do enable the default git pre-commit hook that detect trailing
|
2019-02-05 09:14:31 +00:00
|
|
|
whitespace for you and help you to avoid corrupting GTK's tree with
|
2010-09-24 02:26:07 +00:00
|
|
|
it. Do that as follows:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
chmod a+x .git/hooks/pre-commit
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
You might also find the git-stripspace utility helpful which acts as a
|
|
|
|
filter to remove trailing whitespace as well as initial, final, and
|
|
|
|
duplicate blank lines.
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
### Headers
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2019-02-05 09:14:31 +00:00
|
|
|
Headers are special, for GTK, in that they don't have to obey the
|
2010-09-24 02:26:07 +00:00
|
|
|
80 characters limit. The only major rule for headers is that the function
|
|
|
|
definitions should be vertically aligned in three columns:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
return value function_name (type argument,
|
|
|
|
type argument,
|
|
|
|
type argument);
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
The maximum width of each column is given by the longest element in the
|
|
|
|
column:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2020-07-24 18:40:36 +00:00
|
|
|
void gtk_type_set_property (GtkType *type,
|
|
|
|
const char *value,
|
|
|
|
GError **error);
|
|
|
|
const char *gtk_type_get_property (GtkType *type);
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
It is also possible to align the columns to the next tab:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
void gtk_type_set_prop (GtkType *type,
|
2020-07-24 20:25:56 +00:00
|
|
|
float value);
|
|
|
|
float gtk_type_get_prop (GtkType *type);
|
2020-07-24 13:54:49 +00:00
|
|
|
int gtk_type_update_foobar (GtkType *type);
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Public headers should never be included directly:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
|
|
|
|
#error "Only <gtk/gtk.h> can be included directly."
|
|
|
|
#endif
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2018-02-07 15:01:25 +00:00
|
|
|
Private headers should include the public header first, if one exists:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2018-02-07 15:01:25 +00:00
|
|
|
#ifndef __GTK_FOO_PRIVATE_H__
|
|
|
|
#define __GTK_FOO_PRIVATE_H__
|
|
|
|
|
|
|
|
#include "gtkfoo.h"
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
#endif /* __GTK_FOO_PRIVATE_H__ */
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2018-02-07 15:01:25 +00:00
|
|
|
|
|
|
|
All headers should have inclusion guards:
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
#ifndef __GTK_FOO_H__
|
|
|
|
#define __GTK_FOO_H__
|
|
|
|
|
2018-02-07 15:01:25 +00:00
|
|
|
...
|
|
|
|
|
|
|
|
#endif /* __GTK_FOO_H__ */
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2018-02-07 15:01:25 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
You can also use the `once` pragma instead of the classic pre-processor guard:
|
2018-02-07 15:01:25 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2018-02-07 15:01:25 +00:00
|
|
|
#pragma once
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2018-02-07 15:01:25 +00:00
|
|
|
|
|
|
|
Additionally, public headers should use C++ guards around their declarations:
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
G_BEGIN_DECLS
|
|
|
|
|
2018-02-07 15:01:25 +00:00
|
|
|
GDK_AVAILABLE_IN_ALL
|
|
|
|
GType gtk_foo_get_type (void) G_GNUC_CONST;
|
|
|
|
|
|
|
|
GDK_AVAILABLE_IN_ALL
|
|
|
|
GtkWidget * gtk_foo_new (void);
|
|
|
|
|
2010-09-24 02:26:07 +00:00
|
|
|
...
|
|
|
|
|
|
|
|
G_END_DECLS
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
### Includes
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2019-02-05 09:14:31 +00:00
|
|
|
GTK source files must never include the global gtk.h header; instead, it
|
2018-02-07 15:01:25 +00:00
|
|
|
should include the individual headers that are needed.
|
|
|
|
|
|
|
|
Every source file must include config.h first, followed by the header matching
|
|
|
|
the source file, either the public installed header, or the private header, if
|
|
|
|
it exists.
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkfoo.h"
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2018-02-07 15:01:25 +00:00
|
|
|
Source files should then include project headers, in alphabetical order,
|
|
|
|
starting from headers in the current directory; then headers in
|
|
|
|
sub-directories; and, finally, in paths relative to the top-level
|
|
|
|
directory:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2018-02-07 15:01:25 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkfooprivate.h"
|
|
|
|
|
2010-09-24 02:26:07 +00:00
|
|
|
#include "gtkbutton.h"
|
2018-02-07 15:01:25 +00:00
|
|
|
#include "gtkwidget.h"
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2018-02-07 15:01:25 +00:00
|
|
|
#include "a11y/gtkwidgetaccessible.h"
|
|
|
|
|
|
|
|
#include "gdk/gdkwindowprivate.h"
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2018-02-07 15:01:25 +00:00
|
|
|
|
|
|
|
Finally, source files should include the system headers last:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2018-02-07 15:01:25 +00:00
|
|
|
#include "config.h"
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2018-02-07 15:01:25 +00:00
|
|
|
#include "gtkbarprivate.h"
|
|
|
|
|
|
|
|
#include "gtkcontainerprivate.h"
|
|
|
|
|
|
|
|
#include "a11y/gtkcontaineraccessible.h"
|
|
|
|
#include "a11y/gtkwidgetaccessible.h"
|
|
|
|
|
|
|
|
#include "gdk/gdkwindowprivate.h"
|
|
|
|
|
|
|
|
#include <graphene.h>
|
2010-09-24 02:26:07 +00:00
|
|
|
#include <string.h>
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2018-02-07 15:01:25 +00:00
|
|
|
Cyclic dependencies should be avoided if at all possible; for instance, you
|
|
|
|
could use additional headers to break cycles.
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
### GObject
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
GObject classes definition and implementation require some additional
|
|
|
|
coding style notices.
|
|
|
|
|
|
|
|
Typedef declarations should be placed at the beginning of the file:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
typedef struct _GtkFoo GtkFoo;
|
|
|
|
typedef struct _GtkFooClass GtkFooClass;
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
This includes enumeration types:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT,
|
|
|
|
GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH
|
|
|
|
} GtkSizeRequestMode;
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
And callback types:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
typedef void (* GtkCallback) (GtkWidget *widget,
|
|
|
|
gpointer user_data);
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2018-02-07 15:01:25 +00:00
|
|
|
Instance structures should only contain the parent type:
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
struct _GtkFoo
|
|
|
|
{
|
|
|
|
GtkWidget parent_instance;
|
|
|
|
};
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
You should use the `G_DECLARE_DERIVABLE_TYPE()` and `G_DECLARE_FINAL_TYPE()`
|
2018-02-07 15:01:25 +00:00
|
|
|
macros in newly written headers.
|
2013-07-04 23:09:37 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
Inside your source file, always use the `G_DEFINE_TYPE()`,
|
|
|
|
`G_DEFINE_TYPE_WITH_PRIVATE()`, and `G_DEFINE_TYPE_WITH_CODE()` macros, or their
|
|
|
|
`abstract variants G_DEFINE_ABSTRACT_TYPE()`,
|
|
|
|
`G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE()`, and `G_DEFINE_ABSTRACT_TYPE_WITH_CODE()`;
|
2018-02-07 15:01:25 +00:00
|
|
|
also, use the similar macros for defining interfaces, quarks, and boxed types.
|
2013-07-04 23:09:37 +00:00
|
|
|
|
2010-09-24 02:26:07 +00:00
|
|
|
All the properties should be stored inside the private data structure, which
|
|
|
|
is defined inside the source file - or, if needed, inside a private header
|
|
|
|
file; the private header filename must end with "private.h" and must not be
|
|
|
|
installed.
|
|
|
|
|
2013-07-04 23:09:37 +00:00
|
|
|
The private data structure should only be accessed internally either using the
|
2018-02-07 15:01:25 +00:00
|
|
|
pointer inside the instance structure, for legacy code, or the generated
|
2013-07-04 23:09:37 +00:00
|
|
|
instance private data getter function for your type. You should never use the
|
2020-05-26 17:19:53 +00:00
|
|
|
`G_TYPE_INSTANCE_GET_PRIVATE()` macro or the `g_type_instance_get_private()`
|
2010-09-24 02:26:07 +00:00
|
|
|
function.
|
|
|
|
|
|
|
|
Interface types should always have the dummy typedef for cast purposes:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2018-02-07 15:01:25 +00:00
|
|
|
typedef struct _GtkFoo GtkFoo;
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
The interface structure should have "Interface" postfixed to the dummy typedef:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
typedef struct _GtkFooInterface GtkFooInterface;
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Interfaces must have the following macros:
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
| Macro | Expands to |
|
|
|
|
|------------------------------|---------------------------------|
|
|
|
|
| `GTK_TYPE_<iface_name>` | `<iface_name>_get_type` |
|
|
|
|
| `GTK_<iface_name>` | `G_TYPE_CHECK_INSTANCE_CAST` |
|
|
|
|
| `GTK_IS_<iface_name>` | `G_TYPE_CHECK_INSTANCE_TYPE` |
|
|
|
|
| `GTK_<iface_name>_GET_IFACE` | `G_TYPE_INSTANCE_GET_INTERFACE` |
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
### Memory allocation
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
When dynamically allocating data on the heap either use `g_new()` or,
|
|
|
|
if allocating multiple small data structures, `g_slice_new()`.
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Public structure types should always be returned after being zero-ed,
|
2020-05-26 17:19:53 +00:00
|
|
|
either explicitly for each member, or by using `g_new0()` or `g_slice_new0()`.
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
### Macros
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Try to avoid private macros unless strictly necessary. Remember to #undef
|
|
|
|
them at the end of a block or a series of functions needing them.
|
|
|
|
|
|
|
|
Inline functions are usually preferable to private macros.
|
|
|
|
|
|
|
|
Public macros should not be used unless they evaluate to a constant.
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
### Symbol visibility
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
Any symbol that is not explicitly annotated using a `GDK_AVAILABLE_IN_*`
|
2021-03-05 17:44:41 +00:00
|
|
|
or `GDK_DEPRECATED_IN_*` macro is considered internal, and not exported
|
|
|
|
in the shared library.
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2018-02-07 15:01:25 +00:00
|
|
|
Never export variables as public API, since this is cumbersome on some
|
|
|
|
platforms. It is always preferable to add getters and setters instead.
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Non-exported functions that are needed in more than one source file
|
2021-03-05 17:44:41 +00:00
|
|
|
should be declared in a private header file with a name that ends in
|
|
|
|
`private.h`.
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Non-exported functions that are only needed in one source file
|
|
|
|
should be declared static.
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
### Documentation
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2021-03-05 17:44:41 +00:00
|
|
|
All public APIs must have doc comments. For functions, these should
|
2010-09-24 02:26:07 +00:00
|
|
|
be placed in the source file, directly above the function.
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
/* valid */
|
|
|
|
/**
|
|
|
|
* gtk_get_flow:
|
2021-03-05 17:44:41 +00:00
|
|
|
* @widget: a `GtkWidget`
|
2010-09-24 02:26:07 +00:00
|
|
|
*
|
|
|
|
* Gets the flow of a widget.
|
|
|
|
*
|
|
|
|
* Note that flows may be laminar or turbulent...
|
|
|
|
*
|
|
|
|
* Returns: (transfer none): the flow of @widget
|
|
|
|
*/
|
|
|
|
GtkFlow *
|
|
|
|
gtk_get_flow (GtkWidget *widget)
|
|
|
|
{
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
}
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
|
|
|
Doc comments for macros, function types, class structs, etc should be
|
|
|
|
placed next to the definitions, typically in headers.
|
|
|
|
|
2021-03-05 17:44:41 +00:00
|
|
|
The main content of the doc comments uses markdown, which can include
|
|
|
|
inline formatting, sections, tables, images, links. For links to
|
|
|
|
symbols/classes/etc, we use a markdown extension syntax like this:
|
|
|
|
|
|
|
|
[class@Gtk.Widget], [method@Gtk.ListView.get_factory],...
|
|
|
|
|
|
|
|
Every doc comment should start with a single-sentence paragraph that
|
|
|
|
can serve as a summary of sorts (it will often be placed next to a
|
|
|
|
link pointing to the full documentation for the symbol/class/etc).
|
|
|
|
The summary should not include links.
|
|
|
|
|
|
|
|
Long-form documentation for classes should be included in the doc
|
|
|
|
comment for the struct, typically at the top of the source file,
|
|
|
|
after the license header and includes:
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
```c
|
2010-09-24 02:26:07 +00:00
|
|
|
/**
|
2021-03-05 17:44:41 +00:00
|
|
|
* GtkSizeRequest:
|
|
|
|
*
|
|
|
|
* The GtkSizeRequest interface is GTK's height-for-width geometry
|
|
|
|
* geometry management system.
|
|
|
|
*
|
|
|
|
* # Geometry management
|
2010-09-24 02:26:07 +00:00
|
|
|
*
|
|
|
|
* ...
|
|
|
|
*/
|
2020-05-26 17:19:53 +00:00
|
|
|
```
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2020-05-27 12:44:22 +00:00
|
|
|
For more information on the documentation style and contribution guidelines,
|
|
|
|
please [follow the corresponding contribution guide](./reference/README.md).
|
|
|
|
|
2020-05-26 17:19:53 +00:00
|
|
|
### Old code
|
2010-09-24 02:26:07 +00:00
|
|
|
|
2019-02-05 09:14:31 +00:00
|
|
|
New code that is being added to GTK should adhere to the style
|
|
|
|
explained above. Existing GTK code does largely follow these
|
2010-09-24 02:26:07 +00:00
|
|
|
conventions, but there are some differences, e.g. occurrences
|
|
|
|
of tabs, etc.
|
|
|
|
|
|
|
|
It is ok to update the style of a code block or function when you
|
|
|
|
are touching it anyway, but sweeping whitespace changes obscure the
|
2018-02-07 15:01:25 +00:00
|
|
|
source revision history, and should be avoided.
|