]> Pileus Git - ~andy/gtk/blob - docs/CODING-STYLE
stylecontext: Do invalidation on first resize container
[~andy/gtk] / docs / CODING-STYLE
1 GTK+ Coding Style
2 -------------------------------------------------------------------------------
3
4 This document is intended to be a short description of the preferred
5 coding style to be used for the GTK+ source code. It was strongly
6 inspired by Clutter's CODING_STYLE.
7
8 Coding style is a matter of consistency, readability and maintainance;
9 coding style is also completely arbitrary and a matter of taste. This
10 document will use examples at the very least to provide authoritative
11 and consistent answers to common questions regarding the coding style,
12 and will also try to identify the allowed exceptions.
13
14 The examples will show the preferred coding style; the negative examples
15 will be clearly identified. Please, don't submit code to GTK+ that
16 looks like any of these.
17
18 Part of the rationales for these coding style rules are available either
19 in the kernel CodingStyle document or in Cairo's CODING_STYLE one.
20
21 When in doubt, check the surrounding code and try to imitate it.
22
23 + Line width
24
25 The maximum line width for source files is 80 characters, whenever possible.
26 Longer lines are usually an indication that you either need a function
27 or a pre-processor macro.
28
29 + Indentation
30
31 Each new level is indented 2 or more spaces than the previous level:
32
33   if (condition)
34     single_statement ();
35
36 This can only be achieved using space characters. It may not be achieved
37 using tab characters alone, or using a combination of spaces and tabs.
38
39 Do not change the editor's configuration to change the meaning of a
40 tab character (see below); code using tabs to indent will not be accepted
41 into GTK+.
42
43 Even if two spaces for each indentation level allows deeper nesting than
44 8 spaces, GTK+ favours self-documenting function names that can take
45 quite some space. For this reason you should avoid deeply nested code.
46
47 + Tab characters
48
49 The tab character must always be expanded to spaces. If a literal
50 tab must be used inside the source, the tab must always be interpreted
51 according to its traditional meaning:
52
53         Advance to the next column which is a multiple of 8.
54         [ these two lines should be aligned ]
55
56 + Braces
57
58 Curly braces should not be used for single statement blocks:
59
60   if (condition)
61     single_statement ();
62   else
63     another_single_statement (arg1);
64
65 In case of multiple statements, curly braces should be put on another
66 indentation level:
67
68   if (condition)
69     {
70       statement_1 ();
71       statement_2 ();
72       statement_3 ();
73     }
74
75 The "no block for single statements" rule has only four exceptions:
76
77   ①  if the single statement covers multiple lines, e.g. for functions with
78      many arguments, and it is followed by else or else if:
79
80   /* valid */
81   if (condition)
82     {
83       a_single_statement_with_many_arguments (some_lengthy_argument,
84                                               another_lengthy_argument,
85                                               and_another_one,
86                                               plus_one);
87     }
88   else
89     another_single_statement (arg1, arg2);
90
91   ②  if the condition is composed of many lines:
92
93   /* valid */
94   if (condition1 ||
95       (condition2 && condition3) ||
96       condition4 ||
97       (condition5 && (condition6 || condition7)))
98     {
99       a_single_statement ();
100     }
101
102   ③  Nested if's, in which case the block should be placed on the
103      outermost if:
104
105   /* valid */
106   if (condition)
107     {
108       if (another_condition)
109         single_statement ();
110       else
111         another_single_statement ();
112     }
113
114   /* invalid */
115   if (condition)
116     if (another_condition)
117       single_statement ();
118     else if (yet_another_condition)
119       another_single_statement ();
120
121   ④  If either side of an if-else statement has braces, both sides
122      should, to match up indentation:
123
124   /* valid */
125   if (condition)
126     {
127       foo ();
128       bar ();
129     }
130   else
131     {
132       baz ();
133     }
134
135   /* invalid */
136   if (condition)
137     {
138       foo ();
139       bar ();
140     }
141   else
142     baz ();
143
144 In general, new blocks should be placed on a new indentation level,
145 like:
146
147   int retval = 0;
148
149   statement_1 ();
150   statement_2 ();
151
152   {
153     int var1 = 42;
154     gboolean res = FALSE;
155
156     res = statement_3 (var1);
157
158     retval = res ? -1 : 1;
159   }
160
161 While curly braces for function definitions should rest on a new line
162 they should not add an indentation level:
163
164   /* valid */
165   static void
166   my_function (int argument)
167   {
168     do_my_things ();
169   }
170
171   /* invalid */
172   static void
173   my_function (int argument) {
174     do_my_things ();
175   }
176
177   /* invalid */
178   static void
179   my_function (int argument)
180     {
181       do_my_things ();
182     }
183
184 Curly braces must not be placed on the same line as a condition:
185
186   /* invalid */
187   if (condition) {
188     statement_1 ();
189     statement_2 ();
190   }
191 + Conditions
192
193 Do not check boolean values for equality:
194
195   /* invalid */
196   if (condition == TRUE)
197     do_foo ();
198
199   /* valid */
200   if (another_condition)
201     do_bar ();
202
203 Even if C handles NULL equality like a boolean, be explicit:
204
205   /* valid */
206   if (some_pointer == NULL)
207     do_blah ();
208
209   /* invalid */
210   if (some_other_pointer)
211     do_blurp ();
212
213 In case of conditions split over multiple lines, the logical operators should
214 always go at the end of the line:
215
216   /* invalid */
217   if (condition1
218       || condition2
219       || condition3)
220     {
221       do_foo ();
222     }
223
224   /* valid */
225   if (condition1 &&
226       condition2 &&
227       (condition3 || (condition4 && condition5)))
228     {
229       do_blah ();
230     }
231
232 + Functions
233
234 Functions should be declared by placing the returned value on a separate
235 line from the function name:
236
237   void
238   my_function (void)
239   {
240   }
241
242 The arguments list must be broken into a new line for each argument,
243 with the argument names right aligned, taking into account pointers:
244
245   void
246   my_function (some_type_t     type,
247                another_type_t *a_pointer,
248                final_type_t    another_type)
249   {
250   }
251
252 The alignment also holds when invoking a function without breaking the
253 80 characters limit:
254
255   align_function_arguments (first_argument,
256                             second_argument,
257                             third_argument);
258
259 To respect the 80 characters limit do not break the function name from
260 the arguments:
261
262   /* invalid */
263   a_very_long_function_name_with_long_parameters
264     (argument_the_first, argument_the_second);
265
266   /* valid */
267   first_a = argument_the_first;
268   second_a = argument_the_second;
269   a_very_long_function_name_with_long_parameters (first_a, second_a);
270
271 + Whitespace
272
273 Always put a space before a parenthesis but never after:
274
275   /* valid */
276   if (condition)
277     do_my_things ();
278
279   /* valid */
280   switch (condition)
281     {
282     }
283
284   /* invalid */
285   if(condition)
286     do_my_things();
287
288   /* invalid */
289   if ( condition )
290     do_my_things ( );
291
292 A switch() should open a block on a new indentation level, and each case
293 should start on the same indentation level as the curly braces, with the
294 case block on a new indentation level:
295
296   /* valid */
297   switch (condition)
298     {
299     case FOO:
300       do_foo ();
301       break;
302
303     case BAR:
304       do_bar ();
305       break;
306     }
307
308   /* invalid */
309   switch (condition) {
310     case FOO: do_foo (); break;
311     case BAR: do_bar (); break;
312   }
313
314   /* invalid */
315   switch (condition)
316     {
317     case FOO: do_foo ();
318       break;
319     case BAR: do_bar ();
320       break;
321     }
322
323   /* invalid */
324   switch (condition)
325     {
326       case FOO:
327       do_foo ();
328       break;
329       case BAR:
330       do_bar ();
331       break;
332     }
333
334 It is preferable, though not mandatory, to separate the various cases with
335 a newline:
336
337   switch (condition)
338     {
339     case FOO:
340       do_foo ();
341       break;
342
343     case BAR:
344       do_bar ();
345       break;
346
347     default:
348       do_default ();
349     }
350
351 The 'break' statement for the default: case is not mandatory.
352
353 If a case block needs to declare new variables, the same rules as the
354 inner blocks (see above) apply; the break statement should be placed
355 outside of the inner block:
356
357   switch (condition)
358     {
359     case FOO:
360       {
361         int foo;
362
363         foo = do_foo ();
364       }
365       break;
366
367     ...
368     }
369
370 When declaring a structure type use newlines to separate logical sections
371 of the structure:
372
373   struct _GtkWrapBoxPrivate
374   {
375     GtkOrientation        orientation;
376     GtkWrapAllocationMode mode;
377
378     GtkWrapBoxSpreading   horizontal_spreading;
379     GtkWrapBoxSpreading   vertical_spreading;
380
381     guint16               vertical_spacing;
382     guint16               horizontal_spacing;
383
384     guint16               minimum_line_children;
385     guint16               natural_line_children;
386
387     GList                *children;
388   };
389
390
391 Do not eliminate whitespace and newlines just because something would
392 fit on 80 characters:
393
394   /* invalid */
395   if (condition) foo (); else bar ();
396
397 Do eliminate trailing whitespace on any line, preferably as a separate
398 patch or commit. Never use empty lines at the beginning or at the end of
399 a file.
400
401 Do enable the default git pre-commit hook that detect trailing
402 whitespace for you and help you to avoid corrupting GTK+'s tree with
403 it. Do that as follows:
404
405   chmod a+x .git/hooks/pre-commit
406
407 You might also find the git-stripspace utility helpful which acts as a
408 filter to remove trailing whitespace as well as initial, final, and
409 duplicate blank lines.
410
411 + Headers
412
413 Headers are special, for GTK+, in that they don't have to obey the
414 80 characters limit. The only major rule for headers is that the function
415 definitions should be vertically aligned in three columns:
416
417   return value          function_name           (type   argument,
418                                                  type   argument,
419                                                  type   argument);
420
421 The maximum width of each column is given by the longest element in the
422 column:
423
424   void         gtk_type_set_property (GtkType      *type,
425                                       const gchar  *value,
426                                       GError      **error);
427   const gchar *gtk_type_get_property (GtkType      *type);
428
429 It is also possible to align the columns to the next tab:
430
431   void          gtk_type_set_prop           (GtkType *type,
432                                              gfloat   value);
433   gfloat        gtk_type_get_prop           (GtkType *type);
434   gint          gtk_type_update_foobar      (GtkType *type);
435
436 Public headers should never be included directly:
437
438   #if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
439   #error "Only <gtk/gtk.h> can be included directly."
440   #endif
441
442 All headers should have inclusion guards (for internal usage)
443 and C++ guards:
444
445   #ifndef __GTK_FOO_H__
446   #define __GTK_FOO_H__
447
448   #include <gtk/gtk-bar.h>
449
450   G_BEGIN_DECLS
451
452   ...
453
454   G_END_DECLS
455
456   #endif /* __GTK_FOO_H__ */
457
458 + Includes
459
460 GTK+ source files should never include the global gtk.h header, but
461 instead include the individual headers that are needed. Every file must
462 include config.h first, then its own header, then other GTK+ headers
463 that it needs, then system and third-party headers that it needs.
464
465   /* valid */
466   #include "config.h"
467
468   #include "gtkfoo.h"
469
470   #include "gtkwidget.h"
471   #include "gtkbutton.h"
472
473   ...
474
475   #include <string.h>
476
477
478 + GObject
479
480 GObject classes definition and implementation require some additional
481 coding style notices.
482
483 Typedef declarations should be placed at the beginning of the file:
484
485   typedef struct _GtkFoo          GtkFoo;
486   typedef struct _GtkFooClass     GtkFooClass;
487   typedef struct _GtkFooPrivate   GtkFooPrivate;
488
489 This includes enumeration types:
490
491   typedef enum
492   {
493     GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT,
494     GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH
495   } GtkSizeRequestMode;
496
497 And callback types:
498
499   typedef void (* GtkCallback) (GtkWidget *widget,
500                                 gpointer   user_data);
501
502 Instance structures should only contain the parent type and a pointer to a
503 private data structure, and they should be annotated as "private":
504
505   struct _GtkFoo
506   {
507     /*< private >*/
508     GtkWidget parent_instance;
509
510     GtkFooPrivate *priv;
511   };
512
513 All the properties should be stored inside the private data structure, which
514 is defined inside the source file - or, if needed, inside a private header
515 file; the private header filename must end with "private.h" and must not be
516 installed.
517
518 The private data structure should only be accessed internally using the
519 pointer inside the instance structure, and never using the
520 G_TYPE_INSTANCE_GET_PRIVATE() macro or the g_type_instance_get_private()
521 function.
522
523 Always use the G_DEFINE_TYPE(), G_DEFINE_TYPE_WITH_CODE() macros, or
524 their abstract variants G_DEFINE_ABSTRACT_TYPE() and
525 G_DEFINE_ABSTRACT_TYPE_WITH_CODE(), and the similar macros for defining
526 interfaces.
527
528 Interface types should always have the dummy typedef for cast purposes:
529
530         typedef struct _GtkFoo              GtkFoo;
531
532 The interface structure should have "Interface" postfixed to the dummy typedef:
533
534         typedef struct _GtkFooInterface         GtkFooInterface;
535
536 Interfaces must have the following macros:
537
538         - Macro:                                - Expands to:
539         • GTK_TYPE_<iface_name>             <iface_name>_get_type
540         • GTK_<iface_name>                  G_TYPE_CHECK_INSTANCE_CAST
541         • GTK_IS_<iface_name>               G_TYPE_CHECK_INSTANCE_TYPE
542         • GTK_<iface_name>_GET_IFACE        G_TYPE_INSTANCE_GET_INTERFACE
543
544 + Memory allocation
545
546 When dynamically allocating data on the heap either use g_new() or,
547 if allocating multiple small data structures, g_slice_new().
548
549 Public structure types should always be returned after being zero-ed,
550 either explicitly for each member, or by using g_new0() or g_slice_new0().
551
552 + Macros
553
554 Try to avoid private macros unless strictly necessary. Remember to #undef
555 them at the end of a block or a series of functions needing them.
556
557 Inline functions are usually preferable to private macros.
558
559 Public macros should not be used unless they evaluate to a constant.
560
561 + Public API
562
563 Avoid exporting variables as public API, since this is cumbersome on some
564 platforms. It is always preferable to add getters and setters instead.
565
566 All public functions must be listed in the gtk.symbols file.
567
568 + Private API
569
570 Non-exported functions that are needed in more than one source file
571 should be named "_gtk_...", and declared in a private header file.
572
573 Underscore-prefixed functions are never exported.
574
575 Non-exported functions that are only needed in one source file
576 should be declared static.
577
578 + Documentation
579
580 All public APIs must have gtk-doc comments. For functions, these should
581 be placed in the source file, directly above the function.
582
583   /* valid */
584   /**
585    * gtk_get_flow:
586    * @widget: a #GtkWidget
587    *
588    * Gets the flow of a widget.
589    *
590    * Note that flows may be laminar or turbulent...
591    *
592    * Returns: (transfer none): the flow of @widget
593    */
594   GtkFlow *
595   gtk_get_flow (GtkWidget *widget)
596   {
597
598     ...
599
600   }
601
602 Doc comments for macros, function types, class structs, etc should be
603 placed next to the definitions, typically in headers.
604
605 Section introductions should be placed in the source file they describe,
606 after the license header:
607
608   /* valid */
609   /**
610    * SECTION:gtksizerequest
611    * @Short_Description: Height-for-width geometry management
612    * @Title: GtkSizeRequest
613    *
614    * The GtkSizeRequest interface is GTK+'s height-for-width (and
615    * width-for-height) geometry management system.
616    * ...
617    */
618
619 To properly document a new function, macro, function type or struct,
620 it needs to be listed in the gtk3-sections.txt file.
621
622 To properly document a new class, it needs to be given its own section
623 in gtk3-sections.txt, needs to be included in gtk-docs.sgml, and the
624 get_type function needs to listed in gtk3.types.
625
626 + Old code
627
628 New code that is being added to GTK+ should adhere to the style
629 explained above. Existing GTK+ code does largely follow these
630 conventions, but there are some differences, e.g. occurrences
631 of tabs, etc.
632
633 It is ok to update the style of a code block or function when you
634 are touching it anyway, but sweeping whitespace changes obscure the
635 git history and should be avoided.