This site has been retired. For up to date information, see handbook.gnome.org or gitlab.gnome.org.


[Home] [TitleIndex] [WordIndex

What is GtkDoc ?

GtkDoc is a tool used to extract API documentation from C-code like Doxygen, but handles documentation of GObject (including signals and properties) that makes it very suitable for GTK+ apps and libraries. It uses docbook for intermediate files and can produce html by default and pdf/man-pages with some extra work.

Git repository

The project is hosted on GNOME's GitLab.

You can check out the current development snapshot using

 git clone https://gitlab.gnome.org/GNOME/gtk-doc.git

Contact

GtkDoc Documentation

You can download the source code and open gtk-doc/help/manual/C/gtk-doc-manual.xml with yelp directly. From a terminal, type:

yelp file:///absolute/path/to/gtk-doc/help/manual/C/gtk-doc-manual.xml

Building documentation using GtkDoc

Next chapter has the basics for quick reference.

Build Structure

Consider you are building your documentation in doc/ or, if you have different types of documentation (manual, tutorial, various documentation) under doc/reference.

If you have a library composed of different modules, use doc/${MODULE}.

INCLUDES= \
        -I$(top_srcdir)                 \
        -I$(top_srcdir)/libgnomescan    \
        -I$(top_srcdir)/libgnomescanui  \
        -I$(top_builddir)               \
        $(GNOMESCAN_CFLAGS)

GTKDOC_LIBS= \
        $(GNOMESCAN_LIBS)                                       \
        $(INTLLIBS)                                             \
        -lsane                                                  \
        $(top_builddir)/libgnomescan/.libs/libgnomescan.la      \
        $(top_builddir)/libgnomescanui/.libs/libgnomescanui.la

If you are using a source code management system you should only put the files you change under revision control; usually, this means:

You should consider not using the template files under tmpl/: gtk-doc can generate most of the documentation from the source code, including the sections' short and long description. This keeps the documentation tightly attached to the code it references and reduces the amounts of commits you have to perform.

Refer to the manual for further information.

The widget gallery allows yout to attach a picture to a GObject that will be shown in a gallery and on the top right corner of the corresponding documentation page. For instance, if you are building your documentation in doc/

<para role="gallery">
</para>

 <link linkend="GnomeFoo">
  <inlinegraphic fileref="gnome-foo.png" format="PNG" />
 </link>

HTML_IMAGES= \
        $(srcdir)/images/gnome-foo.png

content_files= \
        gallery.xml

<chapter>
 <title>Widget gallery</title>
 <xi:include href="gallery.xml"/>
</chapter>

Documenting a section

To document a section of the source code do something like this:

/**
 * SECTION:foo-widget
 * @short_description: The best widget, ever
 * @see_also: #BarWidget, #BazWidget
 *
 * #FooWidget is the best widget ever written. It does handle foo, bar and baz.
 *
 * #FooWidget is available since 1.0
 */

Like the rest of the gtk-doc comments, you can use the docbook markup inside them to enhance the layout.

Remember to use document sections inside the .c source files, to avoid unnecessary recompilations.

Documenting Structs

To document a structure, say your widget subclass, do something like this:

/**
 * FooWidget:
 * @bar: some #gboolean
 *
 * This is the best widget, ever.
 **/
typedef struct _FooWidget {
  /*< private >*/
  GtkWidget parent;

  /*< public >*/
  gboolean bar;
} FooWidget;

Hide private struct fields

Use /*< private >*/ before what you want to hide. Use /*< public >*/ for the reverse behavirour.

Documenting Functions

You can document functions like so, usually in the .c file:

/**
 * foo_widget_do_something:
 * @foo_widget: some foo
 * @bar: some bar
 *
 * Does something to the @foo_widget.
 *
 * Returns: %TRUE if the operation succeeded, else %FALSE.
 **/
gboolean
foo_widget_do_something(FooWidget *foo_widget,
                        gint       bar)
{
...

In case the function has variadic arguments, you should use the @... marker:

/**
 * foo_widget_set:
 * @foo_widget: a #FooWidget
 * @first_bar: the first bar to set
 * @...: a %NULL-terminated list of bars
 *
 * Sets a list of bars
 */
void
foo_widget_set (FooWidget   *foo_widget,
                const gchar *first_bar,
                ...)
{
...

Return values can be indicated using any of these markers:

the last one is the preferred way for GLib and GTK+, but you can choose any of them for your project - but please, be consistent!

/**
 * foo_widget_peek_bar:
 * @foo: a #FooWidget
 * @bar: the bar to retrieve from @foo
 *
 * Retrieves a pointer to the value of @bar from a #FooWidget.
 *
 * Returns: the value of the requested bar, or %NULL.
 *   The value is owned by the #FooWidget and should never be
 *   modified or freed
 *
 * Since: 1.4
 */
G_CONST_RETURN gchar *
foo_widget_peek_bar (FooWidget   *foo,
                     const gchar *bar)
{
...

Please remember to:

TODO: Use a more realistic example.

Documenting Properties

You can document GObject properties inline with their initialisation:

/**
 * FooWidget:is-cool:
 *
 * Whether this widget is cool or not.
 **/
g_object_class_install_property (object_class,
                                 PROP_IS_COOL,
  ...

Documenting Signals

You can document GObject signals inline with their parameters and flags, using mostly the same syntax as for a function:

/**
 * FooWidget::foobarized:
 * @widget: the widget that received the signal
 * @foo: some foo
 * @bar: some bar
 *
 * The ::foobarized signal is emitted each time someone tries to foobarize @widget.
 **/
foo_signals[FOOBARIZE] =
  g_signal_new ("foobarize",
                ...

Please remember to

Documenting enums

/**
 * Something:
 * @SOMETHING_FOO: something foo
 * @SOMETHING_BAR: something bar
 *
 * Enum values used for the thing, to specify the thing.
 *
 **/
typedef enum {
  SOMETHING_FOO,
  SOMETHING_BAR,
} Something;

Versioning information

You can add versioning information to all documentation elements to tell when an api was introduced, or when it was deprecated, for example:

/**
 * foo_get_bar:
 * @foo: some foo
 * @returns: @foo's bar
 * 
 * Retrieves @foo's bar.
 *
 * Since: 2.6
 *
 * Deprecated: 2.12: Use foo_baz_get_bar() instead.
 **/
Bar *
foo_get_bar(Foo *foo)
{
...

Linking

To link to an external web page, use <ulink url="link-url">Link Name</ulink>. For instance: <ulink url="http://www.gtk.org/">GTK+</ulink>.

To link to functions, just use the function name followed by braces, for instance: foo_widget_set()

To link objects, structures and enumerations, use the name of the type prefixed with an hash, for instance: #FooWidget. If you want to add a plural form, you should use an XML comment to break the name, for instance: #FooWidget<!-- -->s

To link macros use the symbol prefixed with a percent, for instance: %FOO_IS_WIDGET

The percent symbol should also be used with symbols like NULL, TRUE and FALSE.

To link a section written in a separate sgml file, assign an id to the section and then use <link linkend="sectionid"> in the gtk-doc comment

TODO: Preventing automatic linking.


CategoryDocumentationProject


2024-10-23 11:04